I'm trying to create a Class representing a regex to execute for my application. For each regex I have a link to the regex101.com page where users can find unit tests. I wanna use this solution to have near the class declaration this link but without having them in the class code. The code I think about has to look like this:
class TestUrl(object):
def __init__(self, url) -> None:
self.url = url
def __call__(self, cls) -> Any:
functools.update_wrapper(self, cls)
cls.url = self.url
def wrapper(*args: Any, **kwds: Any):
return cls(*args, **kwds)
return wrapper
def test_url(url):
def wrapper_class(cls):
cls.test_url = url
@functools.wraps(cls)
def wrapper(*args, **kwargs):
return cls(*args, **kwargs)
return wrapper
return wrapper_class
class Regex:
def __init__(self, pattern, repl, flags) -> None:
self.exp = re.compile(pattern, flags=flags)
self.repl = repl
self.flags = flags
def sub(self, string: str):
return self.exp.sub(self.repl, string)
@test_url("https://regex101.com/r/.../...")
class SubRegex(Regex):
def __init__(self):
super().__init__(r'...', r'...', re.MULTILINE | re.DOTALL)
But my problem is that when I wanns cycle on all classes in the module using this code:
def test_all_regexs(regex):
module = importlib.import_module("...")
print(f"Looking at {module}")
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj):
print(f"Class: {name}, {obj}")
if inspect.isfunction(obj):
print(f"Func: {name}, {obj}")
The output is always:
Func: SubRegex, <function SubRegex at ...>
Class: Regex, <class '....Regex'>
Class: TestUrl, <class '....TestUrl'>
Func: test_url, <function test_url at ...>
I can't figure out how obtain SubRegex as Class, not as Func. How can I get this?
P.S.: Do you have another way to don't mix application logic like regex patterns with the url I use only for documentation?
Aucun commentaire:
Enregistrer un commentaire