Skip to content

Commit

Permalink
auto generate record properties to class not instance
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatYYX committed Apr 10, 2019
1 parent 82849cb commit 6173ce7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions rltk/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def wrapper(cls):
return wrapper


@remove_raw_object
class AutoGeneratedRecord(Record):
"""
Properties are auto generated based on the keys in `raw_object`.
Expand All @@ -164,13 +163,24 @@ class AutoGeneratedRecord(Record):

def __init__(self, raw_object: dict):
super().__init__(raw_object)
for k, v in raw_object.items():
for k in raw_object.keys():
if k == self.__class__._id_key:
if not self.__class__._id_keep_original:
continue
setattr(self, k, v)

@cached_property
if not hasattr(self.__class__, k):
setattr(self.__class__, k, self.__class__._generate_property(k))

@staticmethod
def _generate_property(k):

@property
def get_value(ins):
return ins.raw_object[k]

return get_value

@property
def id(self):
id_ = self.raw_object[self.__class__._id_key]
function_ = self.__class__._id_function
Expand Down

0 comments on commit 6173ce7

Please sign in to comment.