Hi, I developed easy to use way of using methods of diamond pattern methods. Would you like to add it on one of modules? If you want we or you can develop it. I am just trying to help Python community <3
from types import MethodType, FunctionType
from inspect import isclass
from functools import cache
from typing import Any,Self
class base_manager:
def __init__(self, target : object,
helper_class : type) -> None:
if not isclass(helper_class):
raise TypeError(f"{helper_class.__name__} , this is not a class")
self.__target: object = target
self.__helper_class: type[Any] = helper_class
def __enter__(self) -> Self:
return self()
def __call__(self) -> object:
for obj_method in self.methods:
setattr(self.__target,obj_method.__name__,obj_method)
return self.__target
@property
@cache
def methods(self) -> list[MethodType]:
return [MethodType(method, self.__target)for method in
self.names_and_functions.values()]
@property
@cache
def names_and_functions(self) -> dict:
return {key:current_atr for key in
self.__target.__class__.__dict__
if bool(current_atr :=
self.__helper_class.__dict__.get(key,False))
and key not in object.__dict__ and
callable(current_atr)}
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
for obj_method_key in self.names_and_functions:
delattr(self.__target,obj_method_key)
No problem. And why on earth are you deleting attrs on the target in __exit__? That’s a tad, destructive. And posssibly even dangerous.
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
for obj_method_key in self.names_and_functions:
delattr(self.__target,obj_method_key)
Overall, what’s this code actually used for?
Perhaps this is just my lack of imagination and ignorance but “Diamond patterns” to my mind are a multiple inheritance pattern (I have not come across “diamond” in any other context in Python), and I can’t imagine how this code possibly helps that.
Could you please describe concertly what problem you are trying to solve? Generally, when proposing something to be added, it’s better to start with a use-case and/or problem statement instead of a solution. I have no clue when I would want to use this, and why.
I’ve got a little context manager called stackattrs which I routinely
use in setup then shutdown stuff like this:
with stackattrs(
obj,
attr1=a,
attr2=b,
):
... do stuff ...
which sets attrs on obj for the duration. Very useful in a number of
situations. Anyway, it restores the previous state of those attributes
on exit from the context. Particularly, if those attrs were not there at
the start, it removes them. (Of course, if they were there previously
with some value, that value gets restored.)
Now, I do find myself going back to the init of obj sometimes and
putting stub self.attr1 = None statements in sometimes. But often to
make linters happy.
Having the attributes gone where you’re out of the context scope can be
quite handy for discovering you’ve misused something because you get
nice loud AttributeErrors.
You can get stackattrs from my cs.context PyPI module if you want to
play with it.