# from Base.Class.Yaml import * # print(yaml_decode('sess:\n- json')) # # a = {"Host": '127.0.0.1', "User-Agent": 'Chrome'} # b = {"Host": 'www.baidu.com', "User-Agent": 'curl'} # c = {} # c.update(a) # c.update(b) # print(c) # # # print(locals().setdefault('a', 123)) # print(locals().get('a')) # print(globals().get('a')) # from Base.Class.Yaml import * # def _match_bool(value=None): # return value in ['是', '开启', '打开', 'True', 'true', 'TRUE', True, 'Yes', 'yes', 'YES', 'Y', 'y', '1', 1] # # print(yaml_encode({"a": 123, "b": "1 2 3"})) # class AA(dict): # def __getattr__(self, item): # return self.__getitem__(item) # # def __getitem__(self, item): # r = super().__getitem__(item) # if isinstance(r, dict): # return __class__(r) # else: # return r # # def __str__(self): # return '哈哈' # # # a = AA({"name": {"one": 1}}) # a['id'] = '163' # print(a.name.one) # print(a) # class ReText(str): # def __getitem__(self, item): # if isinstance(item, (int, bool)): # return __class__(super().__getitem__(item)) # else: # import re # return ReList(re.findall(str(item), str(self.__str__()))) # # # class ReList(list): # def __getitem__(self, item): # if isinstance(item, (int, bool)): # try: # r = super().__getitem__(item) # if isinstance(r, (list, tuple, set)): # return __class__(r) # elif isinstance(r, (dict, )): # return ReDict(r) # else: # return ReText(r) # except: # return __class__() # else: # import re # try: # return ReList(re.findall(str(item), str(self.__str__()))) # except: # return ReText('') # # def __str__(self): # try: # return ReText(self[0]) # except: # return ReText('') # # # class ReDict(dict): # def __getitem__(self, item): # if item in self: # r = super().__getitem__(item) # if isinstance(r, (dict, )): # return __class__(r) # elif isinstance(r, (list, tuple, set)): # return ReList(r) # else: # return ReText(r) # else: # import re # try: # return ReList(re.findall(str(item), str(self.__str__()))) # except: # return ReText('') # # def __str__(self): # try: # return ReText(self.__getitem__('index')) # except: # return ReText('') # # def update(self, *args, **kwargs): # super().update(*args, **kwargs) # return self # # # a = ReDict( # { # "index2": '123abc', # "a": '1', # "b": '2', # "c": '1234ABCDabcd', # "location": '1: 四川省成都市 电信2: 广东省广州市 电信', # "obj": { # "index": '1: 四川省成都市 电信2: 广东省广州市 电信', # "id": 101, # "name": '赵亚凡' # } # } # ) # # print(a['c']['(1234ABCDabcd)']['1234']) # # print(a['location']['(.*?)']) # # print(a['obj']['name']['(赵亚)']['(赵)']) # print(a.update({'index': '456efg'})) # print(a['obj']['index']['<(location)>']) # import re # string = '123\n\n 456\n' # print(re.findall('(.*?)', string, re.I)) # # # a = '123','456' # print(a) # def reparse(regexp, string): import re if isinstance(regexp, tuple): reg = str(regexp[0]) flg = eval('re.' + str(regexp[1]).upper()) else: reg = str(regexp) flg = 0 if reg in ['', 'trim', '_trim_', 'strip', '_strip_']: return ReText(string.strip()) else: return ReList(re.findall(reg, string, flg)) class ReText(str): def __getitem__(self, item): if isinstance(item, (int, bool)): return __class__(super().__getitem__(item)) else: return reparse(item, str(self.__str__())) class ReList(list): def __getitem__(self, item): if isinstance(item, (int, bool)): try: r = super().__getitem__(item) if isinstance(r, (list, tuple, set)): return __class__(r) elif isinstance(r, (dict, )): return ReDict(r) else: return ReText(r) except: return __class__() else: try: return reparse(item, str(self.__str__())) except: return ReText('') def __str__(self): try: return ReText(self[0]) except: return ReText('') class ReDict(dict): def __getitem__(self, item): if item in self: r = super().__getitem__(item) if isinstance(r, (dict, )): return __class__(r) elif isinstance(r, (list, tuple, set)): return ReList(r) else: return ReText(r) else: try: return reparse(item, str(self.__str__())) except: return ReText('') def __str__(self): try: return ReText(self.__getitem__('index')) except: return ReText('') def update(self, *args, **kwargs): super().update(*args, **kwargs) return self a = ReDict({ 'index': '#123\n\n 456\n#' }) print(a['index']['#(.*?)#','s']['(.*?)','s'][1][''])