pbf.utils.Config
使用方法
Config类用于处理配置文件。当用户编辑的配置文件不全时,可以自动根据预置内容返回默认值。
使用时编写一个MyConfig类继承自Config,并覆写originData属性。
originData属性用于存储默认值,实例化时传入的data用于存储实际值。
class MyConfig(Config):
originData = {
"WindowTitle": {
"Enable": True,
"Text": "Hello, World!"
}
}
data = {
"WindowTitle": {}
} # 我们假设这个data是从某些json配置文件中读取的,用户可能只填写了部分内容
config = MyConfig(data)
print(config.get("WindowTitle.Enable"))
config.set("WindowTitle.Enable", False)
print(config.get("WindowTitle.Enable"))
config.autoComplete()
print(config.get("WindowTitle.Text"))
print(config.data)
输出如下
True
False
Hello, World!
{'WindowTitle': {'Enable': False, 'Text': 'Hello, World!'}}
1""" 2# 使用方法 3**Config类用于处理配置文件。当用户编辑的配置文件不全时,可以自动根据预置内容返回默认值。** 4 5使用时编写一个`MyConfig`类继承自`Config`,并覆写`originData`属性。<br> 6`originData`属性用于存储默认值,实例化时传入的`data`用于存储实际值。<br> 7 8```python 9class MyConfig(Config): 10 originData = { 11 "WindowTitle": { 12 "Enable": True, 13 "Text": "Hello, World!" 14 } 15 } 16 17data = { 18 "WindowTitle": {} 19} # 我们假设这个data是从某些json配置文件中读取的,用户可能只填写了部分内容 20 21config = MyConfig(data) 22print(config.get("WindowTitle.Enable")) 23config.set("WindowTitle.Enable", False) 24print(config.get("WindowTitle.Enable")) 25config.autoComplete() 26print(config.get("WindowTitle.Text")) 27print(config.data) 28``` 29输出如下 30``` 31True 32False 33Hello, World! 34{'WindowTitle': {'Enable': False, 'Text': 'Hello, World!'}} 35``` 36""" 37 38 39from ..error import ConfigError 40 41 42class Config: 43 originData = {} 44 45 def __init__(self, data: any) -> None: 46 """ 47 传入`data`数据。`data`用于在后续`get`时提供默认值。 48 :param data: any 初始数据 49 """ 50 self.data = data 51 52 def get(self, key: str, defaultValue: any = None, passOnNotExists: bool = False) -> any: 53 """ 54 传入键获取数据。<br> 55 如果键不存在,且不传入`defaultValue`,则返回`data`(初始化类时传入的`data`)中对应的默认值。<br> 56 `key`参数可以使用`.`分隔,例如`"WindowTitle.Enable"`,表示获取`data["WindowTitle"]["Enable"]`的值。 57 :param key: str 键 58 :param defaultValue: str (可选)默认值 59 :param passOnNotExists: bool (可选)是否忽略不存在的键,如果为False,会抛出`pbf.error.ConfigError` 60 :return: any 数据 61 """ 62 keyList = key.split(".") 63 data = self.data 64 originData = self.originData 65 66 for item in keyList: 67 data = data.get(item) 68 originData = originData.get(item) 69 if originData is None: 70 if not passOnNotExists: 71 raise ConfigError(f"Unknown key {item} in {key}") 72 else: 73 return defaultValue 74 if data is None: 75 if defaultValue is None: 76 data = originData 77 else: 78 return defaultValue 79 80 return data 81 82 def set(self, key: str, value: any) -> bool: 83 """ 84 设置数据 85 :param key: str 键 86 :param value: any 值 87 :return: bool True 88 """ 89 keyList = key.split(".") 90 data = self.data 91 92 for item in keyList[:-1]: 93 if item not in data: 94 data[item] = {} 95 data = data[item] 96 97 data[keyList[-1]] = value 98 return True 99 100 def autoComplete(self) -> None: 101 """ 102 自动填充`data`中不存在的键。 103 :return: None 104 """ 105 for key in self.originData: 106 if key not in self.data: 107 self.data[key] = self.originData[key] 108 elif type(self.originData[key]) == dict: 109 for subKey in self.originData[key]: 110 if subKey not in self.data[key]: 111 self.data[key][subKey] = self.originData[key][subKey] 112 113 114if __name__ == "__main__": 115 class MyConfig(Config): 116 originData = { 117 "WindowTitle": { 118 "Enable": True, 119 "Text": "Hello, World!" 120 } 121 } 122 123 config = MyConfig({}) 124 print(config.get("WindowTitle.Enable")) 125 config.set("WindowTitle.Enable", False) 126 print(config.get("WindowTitle.Enable")) 127 config.autoComplete() 128 print(config.get("WindowTitle.Text")) 129 print(config.data)
class
Config:
43class Config: 44 originData = {} 45 46 def __init__(self, data: any) -> None: 47 """ 48 传入`data`数据。`data`用于在后续`get`时提供默认值。 49 :param data: any 初始数据 50 """ 51 self.data = data 52 53 def get(self, key: str, defaultValue: any = None, passOnNotExists: bool = False) -> any: 54 """ 55 传入键获取数据。<br> 56 如果键不存在,且不传入`defaultValue`,则返回`data`(初始化类时传入的`data`)中对应的默认值。<br> 57 `key`参数可以使用`.`分隔,例如`"WindowTitle.Enable"`,表示获取`data["WindowTitle"]["Enable"]`的值。 58 :param key: str 键 59 :param defaultValue: str (可选)默认值 60 :param passOnNotExists: bool (可选)是否忽略不存在的键,如果为False,会抛出`pbf.error.ConfigError` 61 :return: any 数据 62 """ 63 keyList = key.split(".") 64 data = self.data 65 originData = self.originData 66 67 for item in keyList: 68 data = data.get(item) 69 originData = originData.get(item) 70 if originData is None: 71 if not passOnNotExists: 72 raise ConfigError(f"Unknown key {item} in {key}") 73 else: 74 return defaultValue 75 if data is None: 76 if defaultValue is None: 77 data = originData 78 else: 79 return defaultValue 80 81 return data 82 83 def set(self, key: str, value: any) -> bool: 84 """ 85 设置数据 86 :param key: str 键 87 :param value: any 值 88 :return: bool True 89 """ 90 keyList = key.split(".") 91 data = self.data 92 93 for item in keyList[:-1]: 94 if item not in data: 95 data[item] = {} 96 data = data[item] 97 98 data[keyList[-1]] = value 99 return True 100 101 def autoComplete(self) -> None: 102 """ 103 自动填充`data`中不存在的键。 104 :return: None 105 """ 106 for key in self.originData: 107 if key not in self.data: 108 self.data[key] = self.originData[key] 109 elif type(self.originData[key]) == dict: 110 for subKey in self.originData[key]: 111 if subKey not in self.data[key]: 112 self.data[key][subKey] = self.originData[key][subKey]
Config(data: <built-in function any>)
def
get( self, key: str, defaultValue: <built-in function any> = None, passOnNotExists: bool = False) -> <built-in function any>:
53 def get(self, key: str, defaultValue: any = None, passOnNotExists: bool = False) -> any: 54 """ 55 传入键获取数据。<br> 56 如果键不存在,且不传入`defaultValue`,则返回`data`(初始化类时传入的`data`)中对应的默认值。<br> 57 `key`参数可以使用`.`分隔,例如`"WindowTitle.Enable"`,表示获取`data["WindowTitle"]["Enable"]`的值。 58 :param key: str 键 59 :param defaultValue: str (可选)默认值 60 :param passOnNotExists: bool (可选)是否忽略不存在的键,如果为False,会抛出`pbf.error.ConfigError` 61 :return: any 数据 62 """ 63 keyList = key.split(".") 64 data = self.data 65 originData = self.originData 66 67 for item in keyList: 68 data = data.get(item) 69 originData = originData.get(item) 70 if originData is None: 71 if not passOnNotExists: 72 raise ConfigError(f"Unknown key {item} in {key}") 73 else: 74 return defaultValue 75 if data is None: 76 if defaultValue is None: 77 data = originData 78 else: 79 return defaultValue 80 81 return data
传入键获取数据。
如果键不存在,且不传入defaultValue,则返回data(初始化类时传入的data)中对应的默认值。
key参数可以使用.分隔,例如"WindowTitle.Enable",表示获取data["WindowTitle"]["Enable"]的值。
Parameters
- key: str 键
- defaultValue: str (可选)默认值
- passOnNotExists: bool (可选)是否忽略不存在的键,如果为False,会抛出
pbf.error.ConfigError
Returns
any 数据
def
set(self, key: str, value: <built-in function any>) -> bool:
83 def set(self, key: str, value: any) -> bool: 84 """ 85 设置数据 86 :param key: str 键 87 :param value: any 值 88 :return: bool True 89 """ 90 keyList = key.split(".") 91 data = self.data 92 93 for item in keyList[:-1]: 94 if item not in data: 95 data[item] = {} 96 data = data[item] 97 98 data[keyList[-1]] = value 99 return True
设置数据
Parameters
- key: str 键
- value: any 值
Returns
bool True
def
autoComplete(self) -> None:
101 def autoComplete(self) -> None: 102 """ 103 自动填充`data`中不存在的键。 104 :return: None 105 """ 106 for key in self.originData: 107 if key not in self.data: 108 self.data[key] = self.originData[key] 109 elif type(self.originData[key]) == dict: 110 for subKey in self.originData[key]: 111 if subKey not in self.data[key]: 112 self.data[key][subKey] = self.originData[key][subKey]