Advertisement
HasteBin0

Python Pure Attribute Experement (draft)

Dec 5th, 2020 (edited)
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from typing import Callable, Dict, Optional, Tuple, TypeVar
  3.  
  4.  
  5. # noinspection PyTypeHints
  6. _T1 = TypeVar('ObjectType')
  7. # noinspection PyTypeHints
  8. _T2 = TypeVar('AttributeType')
  9. # common
  10. _OT2 = Optional[_T2]
  11.  
  12.  
  13. class AttributeLocker:  # https://pastebin.com/q88V9zav
  14.     _object: _T1
  15.     _attr_name: str
  16.     _original: _OT2
  17.  
  18.     def __init__(self, obj: _T1, name: str):
  19.         self._object = obj
  20.         self._attr_name = name
  21.         self._original = None
  22.  
  23.     def get(self) -> _T2:
  24.         return getattr(self._object, self._attr_name)
  25.  
  26.     def set(self, new: _T2):
  27.         setattr(self._object, self._attr_name, new)
  28.  
  29.     def apply(self, fxn: Callable[[_T2], _T2]) -> _T2:
  30.         obj, name = self._object, self._attr_name
  31.         setattr(obj, name, (new := fxn(getattr(obj, name))))
  32.         return new
  33.  
  34.     def __enter__(self):
  35.         self._original = self.get()
  36.  
  37.     def __exit__(self, *_):
  38.         self.set(self._original)
  39.         self._original = None
  40.  
  41.     @property
  42.     def name(self) -> str:
  43.         return self._attr_name
  44.  
  45.     @property
  46.     def original(self) -> _T2:
  47.         return self._original
  48.  
  49.  
  50. class AttributesLocker:
  51.     _object: _T1
  52.     _names: Tuple[str, ...]
  53.     _originals: Dict[str, _OT2]
  54.  
  55.     def __init__(self, obj: _T1, *names: str):
  56.         self._object = obj
  57.         self._names = names
  58.         self._originals = {}
  59.  
  60.     def set(self, attr: str, value: _T2):
  61.         setattr(self._object, attr, value)
  62.  
  63.     def get(self, attr: str) -> _OT2:
  64.         return getattr(self._object, attr, None)
  65.  
  66.     def apply(self, attr: str, fxn: Callable[[_T2], _T2]) -> _T2:
  67.         setattr((obj := self._object), attr, (new := fxn(getattr(obj, attr))))
  68.         return new
  69.  
  70.     def __enter__(self):
  71.         obj = self._object
  72.         self._originals.update({n: getattr(obj, n) for n in self._names})
  73.  
  74.     def __exit__(self, *_):
  75.         obj = self._object
  76.         orig = self._originals
  77.         for nv in orig.items():
  78.             setattr(obj, *nv)
  79.         orig.clear()
  80.  
  81.     @property
  82.     def names(self) -> Tuple[str, ...]:
  83.         return self._names
  84.  
  85.     def name(self, index: int) -> str:
  86.         return self._names[index]
  87.  
  88.     def original(self, attr: str, default: _OT2 = None) -> _OT2:
  89.         return self._originals.get(attr, default)
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement