Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 6292:1e5ed659e8ca issue2550923_computed_property
Initial implementation of Computed property
It supports query/display in html, rest and xml interfaces.
You can specify a cache parameter, but using it raises
NotImplementedError.
It does not support: search, sort or grouping by the computed field.
Checking in on a branch to get more eyeballs on it and maybe some
people to help.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 27 Nov 2020 18:09:00 -0500 |
| parents | 95183d73ac64 |
| children | 30358e334232 |
comparison
equal
deleted
inserted
replaced
| 6291:a67e2b559e8d | 6292:1e5ed659e8ca |
|---|---|
| 71 individual backends may chose to use something different for | 71 individual backends may chose to use something different for |
| 72 sorting as long as the outcome is the same. | 72 sorting as long as the outcome is the same. |
| 73 """ | 73 """ |
| 74 return val | 74 return val |
| 75 | 75 |
| 76 class Computed(object): | |
| 77 """A roundup computed property type. Not inherited from _Type | |
| 78 as the value is never changed. It is defined by running a | |
| 79 method. | |
| 80 | |
| 81 This property has some restrictions. It is read only and can | |
| 82 not be set. It can not (currently) be searched or used for | |
| 83 sorting or grouping. | |
| 84 """ | |
| 85 | |
| 86 def __init__(self, function, default_value=None, cachefor=None): | |
| 87 """ function: a callable method on this class. | |
| 88 default_value: default value to be returned by the method | |
| 89 cachefor: an interval property used to determine how long to | |
| 90 cache the value from the function. Not yet | |
| 91 implemented. | |
| 92 """ | |
| 93 self.function = function | |
| 94 self.__default_value = default_value | |
| 95 self.computed = True | |
| 96 | |
| 97 # alert the user that cachefor is not valid yet. | |
| 98 if cachefor is not None: | |
| 99 raise NotImplementedError | |
| 100 | |
| 101 def __repr__(self): | |
| 102 ' more useful for dumps ' | |
| 103 return '<%s.%s computed %s>' % (self.__class__.__module__, | |
| 104 self.__class__.__name__, | |
| 105 self.function.__name__) | |
| 106 | |
| 107 def get_default_value(self): | |
| 108 """The default value when creating a new instance of this property.""" | |
| 109 return self.__default_value | |
| 110 | |
| 111 def register (self, cls, propname): | |
| 112 """Register myself to the class of which we are a property | |
| 113 the given propname is the name we have in our class. | |
| 114 """ | |
| 115 assert not getattr(self, 'cls', None) | |
| 116 self.name = propname | |
| 117 self.cls = cls | |
| 118 | |
| 119 def sort_repr(self, cls, val, name): | |
| 120 """Representation used for sorting. This should be a python | |
| 121 built-in type, otherwise sorting will take ages. Note that | |
| 122 individual backends may chose to use something different for | |
| 123 sorting as long as the outcome is the same. | |
| 124 """ | |
| 125 return val | |
| 126 | |
| 127 def message_count(self, nodeid, db): | |
| 128 """Example method that counts the number of messages for an issue. | |
| 129 | |
| 130 Adding a property to the IssueClass like: | |
| 131 | |
| 132 msgcount = Computed(Computed.message_count) | |
| 133 | |
| 134 allows querying for the msgcount property to get a count of | |
| 135 the number of messages on the issue. Note that you can not | |
| 136 currently search, sort or group using a computed property | |
| 137 like msgcount. | |
| 138 """ | |
| 139 | |
| 140 return len(db.issue.get(nodeid, 'messages')) | |
| 76 | 141 |
| 77 class String(_Type): | 142 class String(_Type): |
| 78 """An object designating a String property.""" | 143 """An object designating a String property.""" |
| 79 def __init__(self, indexme='no', required=False, default_value="", | 144 def __init__(self, indexme='no', required=False, default_value="", |
| 80 quiet=False): | 145 quiet=False): |
