Skip to content

Instantly share code, notes, and snippets.

@VieVie31
Last active March 2, 2019 20:32
Show Gist options
  • Save VieVie31/9dfdb0689252cb29a3ee8e7e6ca5d45b to your computer and use it in GitHub Desktop.
Save VieVie31/9dfdb0689252cb29a3ee8e7e6ca5d45b to your computer and use it in GitHub Desktop.
convert automatically all attribute of a dataclass (inherited from mine) into properties with standard setter/getter to the corresponding hidden attribute and keep in memory the timestamp of the last set/get action on it...
import gc
import os
import re
import time
import types
import pickle
import psutil
import pathlib
import threading
from dataclasses import dataclass, field
from functools import partial
from typing import Any
@dataclass
class TimedDynamicPropertyClass:
@staticmethod
def __fget(name, self):
self.__dict__["__TDPC_timeinfos"][name[2:]] = time.time()
return self.__dict__[name]
@staticmethod
def __fset(name, self, value):
self.__dict__[name] = value
self.__dict__["__TDPC_timeinfos"][name[2:]] = time.time()
@staticmethod
def __fdel(name, self):
#can't realy del an attr
#(because of the public variable being an property)
#super(TimedDynamicPropertyClass, self).__delattr__(name[2:])
super(TimedDynamicPropertyClass, self).__delattr__(name)
#self.__dict__[name] = None #just put it to None...
self.__dict__["__TDPC_timeinfos"].pop(name[2:])
gc.collect()
def __new__(cls, *args, **kwargs):
instance = super().__new__(cls) #cls == object()
super(TimedDynamicPropertyClass, instance).__setattr__("__TDPC_timeinfos", {})
return instance
def __setattr__(self, attr_name, value):
if attr_name == "__TDPC_timeinfos" and not "__TDPC_timeinfos" in self.__dict__:
if not value == {}:
raise AttributeError("__TDPC_timeinfos is a reserved attribute and can only be initialized with `{}` ...")
super(TimedDynamicPropertyClass, self).__setattr__(f"__{attr_name}", {})
return
elif attr_name == "__TDPC_timeinfos":
raise AttributeError("__TDPC_timeinfos is a reserved attribute and should not be changed...")
#act differenly if a new name of an already registred property
if attr_name in self.__dict__:
super(TimedDynamicPropertyClass, self).__setattr__(attr_name, value)
else:
this_type = self.__class__.mro()[0]
super(TimedDynamicPropertyClass, self).__setattr__(f"__{attr_name}", value)
self.__dict__["__TDPC_timeinfos"][attr_name] = time.time()
setattr(
this_type,
attr_name,
property(
partial(this_type.__fget, f"__{attr_name}"),
partial(this_type.__fset, f"__{attr_name}"),
partial(this_type.__fdel, f"__{attr_name}")
)
)
@dataclass
class D(TimedDynamicPropertyClass):
a:int
b:Any
c:str = "coucou"
#transparent instantiation for the user
d = D(1, 2)
#add an attribute who wasn't present at init time
d.dynamically_created_attribute = 5
assert d.dynamically_created_attribute == 5
#delete it
del d.dynamically_created_attribute
#try to re-create id
d.dynamically_created_attribute = 3
assert d.dynamically_created_attribute == 3
"""
>>> d.__dict__
{'__TDPC_timeinfos': {'a': 1551558414.308322,
'b': 1551558414.308336,
'c': 1551558414.3083599,
'dynamically_created_attribute': 1551558414.345006},
'__a': 1,
'__b': 2,
'__c': 'coucou',
'__dynamically_created_attribute': 3}
>>> del d.dynamically_created_attribute
>>> d.__dict__
{'__TDPC_timeinfos': {'a': 1551558414.308322,
'b': 1551558414.308336,
'c': 1551558414.3083599},
'__a': 1,
'__b': 2,
'__c': 'coucou'}
>>> time.sleep(2) #I do not remeber the value
>>> d.__dict__
{'__TDPC_timeinfos': {'a': 1551558414.308322,
'b': 1551558559.542249, <<<< the acces value has been updated
'c': 1551558414.3083599},
'__a': 1,
'__b': 2,
'__c': 'coucou'}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment