degal/utils.py
changeset 75 18b3b1926720
parent 73 8897352630a5
child 84 891545a38a2b
equal deleted inserted replaced
74:3bf1adf13fdf 75:18b3b1926720
     7 class LazyProperty (property) :
     7 class LazyProperty (property) :
     8     """
     8     """
     9         Lazy-loaded properties
     9         Lazy-loaded properties
    10     """
    10     """
    11 
    11 
       
    12     ATTRNAME = '_LazyProperty_values'
       
    13 
    12     def __init__ (self, func) :
    14     def __init__ (self, func) :
    13         """
    15         """
    14             Initialize with no value
    16             Initialize with no value
    15         """
    17         """
    16 
    18 
    17         super(LazyProperty, self).__init__(func)
    19         super(LazyProperty, self).__init__(func)
       
    20         
       
    21         self.func = func
       
    22         self.name = func.__name__
    18 
    23 
    19         self.value = None
    24         self.value = None
    20 
    25 
    21     def run (self, obj) :
    26     def run (self, obj) :
    22         """
    27         """
    23             Run the background func and return the to-be-cached value
    28             Run the background func and return the to-be-cached value
    24         """
    29         """
    25 
    30 
    26         return super(LazyProperty, self).__call__(obj)
    31         return self.func(obj)
    27  
    32  
       
    33     def obj_dict (self, obj):
       
    34         """
       
    35             Get the lazy-property dict of the given obj
       
    36         """
       
    37 
       
    38         dict = getattr(obj, self.ATTRNAME, None)
       
    39 
       
    40         if dict is None :
       
    41             dict = {}
       
    42 
       
    43             setattr(obj, self.ATTRNAME, dict)
       
    44 
       
    45         return dict
       
    46 
    28     def get (self, obj) :
    47     def get (self, obj) :
    29         """
    48         """
    30             Return the cached value
    49             Return the cached value
    31         """
    50         """
    32 
    51 
    33         xxx
    52         return self.obj_dict(obj)[self.name]
    34     
    53     
    35     def set (self, obj, value) :
    54     def set (self, obj, value) :
    36         """
    55         """
    37             Set the cached value
    56             Set the cached value
    38         """
    57         """
    39 
    58 
    40         xxx
    59         self.obj_dict(obj)[self.name] = value
    41 
    60 
    42     def test (self, obj) :
    61     def test (self, obj) :
    43         """
    62         """
    44             Tests if a value is set
    63             Tests if a value is set
    45         """
    64         """
    46 
    65 
    47         xxx
    66         return self.name in self.obj_dict(obj)
    48        
    67        
    49     def __call__ (self, obj) :
    68     def __get__ (self, obj, owner) :
    50         """
    69         """
    51             Return the cached value if it exists, otherwise, call the func
    70             Return the cached value if it exists, otherwise, call the func
    52         """
    71         """
    53 
    72 
    54         if self.test(obj):
    73         if not obj :
       
    74             return self
       
    75 
       
    76         if not self.test(obj) :
    55             # generate it
    77             # generate it
    56             self.set(obj, self.run(obj))
    78             self.set(obj, self.run(obj))
    57 
    79 
    58         return self.get(obj)
    80         return self.get(obj)
    59 
    81