Skip to content

Commit

Permalink
gave up for type hint in this version
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Aug 19, 2024
1 parent cde1816 commit 8a88ceb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 11 additions & 7 deletions easy_configer/Configer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def warning_on_one_line(message, category, filename, lineno, file=None, line=Non
from .utils.Container import AttributeDict, Flag
from .IO_Converter import IO_Converter

from typing import List

class Configer(object):
'''
The Configer attemtp to make a light-weight solution for configurating your program,
Expand All @@ -22,7 +24,7 @@ class Configer(object):
Hope such trivial contribution will let your work become easier ~ ~ God bless you.
'''
def __init__(self, description:str = "", cmd_args:bool = False, split_chr:str = " = "):
def __init__(self, description:str = "", cmd_args:bool = False, split_chr:str = " = ") -> None:
'''
description (option) :
A customer helper information which describe the functionality of your configer file.
Expand All @@ -44,7 +46,7 @@ def __init__(self, description:str = "", cmd_args:bool = False, split_chr:str =

## Main interface for configuration :
# Support commendline config
def cfg_from_cli(self):
def cfg_from_cli(self) -> None:
'''
The commendline-based configuration, specific arguments from commend-line only.
( only recommend for very lightweight config )
Expand All @@ -54,7 +56,7 @@ def cfg_from_cli(self):
self.args_from_cmd()

# Support string config in cell-based intereactive enviroment
def cfg_from_str(self, raw_cfg_text:str, allow_override:bool=False):
def cfg_from_str(self, raw_cfg_text:str, allow_override:bool=False) -> None:
'''
raw_cfg_text :
The string which declare the arguments with the same syntax used in config file.
Expand All @@ -64,7 +66,7 @@ def cfg_from_str(self, raw_cfg_text:str, allow_override:bool=False):
self.__flag.__dict__ = self.__dict__

# Load .ini config from the given path
def cfg_from_ini(self, cfg_path:str, allow_override:bool=False):
def cfg_from_ini(self, cfg_path:str, allow_override:bool=False) -> None:
'''
cfg_path :
The path which locate the '*.ini' config file.
Expand Down Expand Up @@ -125,12 +127,14 @@ def __get_sec(self, cfg_str:str) -> str:
sec_key_str = cfg_str[beg+1 : end].strip()
return sec_key_str

def __idx_sec_by_dot(self, sec_keys_str:str, allow_init:bool = False) -> [dict, str]:
def __idx_sec_by_dot(self, sec_keys_str:str, allow_init:bool = False):
'''
Core function to manage the hierachical arguments.
Store args is simple, just add it in dict. But dynamically search argument in specific section is non-trivial!
Therefore, i wrote this function to deal with searching and return the "pointer" point to the section.
It's sync with self.__dict__, so use it wiselly & carefully!!
Return (dict, str), leave in comment to competible from python 3.6 ~ python >= 3.9.
'''
sec_name_lst = sec_keys_str.split('.')
# Before easy_configer 1.3.4 ver, all section is builded upon this level
Expand Down Expand Up @@ -177,7 +181,7 @@ def __get_declr_dict(self, cfg_str:str) -> dict :
return { var_name : var_val }

# core function of config parser
def __cfg_parser(self, raw_cfg_text:str, allow_override:bool):
def __cfg_parser(self, raw_cfg_text:str, allow_override:bool) -> None:
'''
raw_cfg_text :
The string which declare the arguments with the same syntax used in config file.
Expand Down Expand Up @@ -241,7 +245,7 @@ def chk_args_exists(val_dict:dict, container:dict):
if self.__cmd_args:
self.args_from_cmd()

def args_from_cmd(self):
def args_from_cmd(self) -> None:
'''
Update the arguments by commend line input string
'''
Expand Down
6 changes: 6 additions & 0 deletions test/test_Configer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def test_parsing_config(self):
# test hierachical var be redefined!
self.cfg1.cfg_from_str("lev = 42")

# test allow_override flag for cfg_from_str & cfg_from_ini
self.cfg1.cfg_from_str("i_var1 = -1", allow_override=True)
self.assertEqual(self.cfg1.i_var1, -1)
self.cfg1.cfg_from_ini(self.dtype_cfg_path, allow_override=True)
self.assertEqual(self.cfg1.i_var1, 42)

# test sub-config
self.cfg2.cfg_from_ini(self.sub_cfg_path)
self._test_subcfg(self.cfg2)
Expand Down

0 comments on commit 8a88ceb

Please sign in to comment.