AutoFramework/Temp/main2.py

214 lines
5.7 KiB
Python
Raw Permalink Normal View History

2022-08-02 14:14:57 +08:00
# 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)
#
#
2022-08-03 23:20:51 +08:00
# 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: <location>四川省成都市 电信</location>2: <location>广东省广州市 电信</location>',
# "obj": {
# "index": '1: <location>四川省成都市 电信</location>2: <location>广东省广州市 电信</location>',
# "id": 101,
# "name": '赵亚凡'
# }
# }
# )
# # print(a['c']['(1234ABCDabcd)']['1234'])
# # print(a['location']['<location>(.*?)</location>'])
# # print(a['obj']['name']['(赵亚)']['(赵)'])
# print(a.update({'index': '456efg'}))
# print(a['obj']['index']['<(location)>'])
# import re
# string = '<a>123</a>\n<a>\n 456\n</a>'
# print(re.findall('<a>(.*?)</a>', 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
2022-08-06 18:53:35 +08:00
if reg in ['', 'trim']:
2022-08-03 23:20:51 +08:00
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):
2022-08-06 18:53:35 +08:00
def __getattr__(self, item):
return self.__getitem__(item)
2022-08-03 23:20:51 +08:00
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': '#<a>123</a>\n<a>\n 456\n</a>#'
})
2022-08-06 18:53:35 +08:00
print(a.index['#(.*?)#','s']['<a>(.*?)</a>','s'][1][''])