3131# _Entity class holds model information as well as conversions between python objects and FlatBuffers (ObjectBox data)
3232class _Entity (object ):
3333 def __init__ (self , user_type , uid : int = 0 ):
34- self .user_type = user_type
35- self .iduid = IdUid (0 , uid )
34+ self ._user_type = user_type
35+ self ._iduid = IdUid (0 , uid )
3636 self ._name = user_type .__name__
37- self .last_property_iduid = IdUid (0 , 0 )
37+ self ._last_property_iduid = IdUid (0 , 0 )
3838
39- self .properties : List [Property ] = list () # List[Property]
40- self .offset_properties = list () # List[Property]
41- self .id_property = None
42- self .fill_properties ()
39+ self ._properties : List [Property ] = list () # List[Property]
40+ self ._offset_properties = list () # List[Property]
41+ self ._id_property = None
42+ self ._fill_properties ()
4343 self ._tl = threading .local ()
4444
4545 @property
46- def id (self ) -> int :
47- return self .iduid .id
46+ def _id (self ) -> int :
47+ return self ._iduid .id
4848
4949 @property
50- def uid (self ) -> int :
51- return self .iduid .uid
50+ def _uid (self ) -> int :
51+ return self ._iduid .uid
5252
53- def has_uid (self ) -> bool :
54- return self .iduid .uid != 0
53+ def _has_uid (self ) -> bool :
54+ return self ._iduid .uid != 0
5555
56- def on_sync (self ):
56+ def _on_sync (self ):
5757 """ Method called once ID/UID are synced with the model file. """
58- assert self .iduid .is_assigned ()
59- for prop in self .properties :
58+ assert self ._iduid .is_assigned ()
59+ for prop in self ._properties :
6060 prop .on_sync ()
6161
6262 def __call__ (self , ** properties ):
6363 """ The constructor of the user Entity class. """
64- object_ = self .user_type ()
64+ object_ = self ._user_type ()
6565 for prop_name , prop_val in properties .items ():
6666 if not hasattr (object_ , prop_name ):
6767 raise Exception (f"Entity { self ._name } has no property \" { prop_name } \" " )
@@ -70,27 +70,27 @@ def __call__(self, **properties):
7070
7171 def __getattr__ (self , name ):
7272 """ Overload to get properties via "<Entity>.<Prop>" notation. """
73- for prop in self .properties :
74- if prop ._name == name :
73+ for prop in self ._properties :
74+ if prop .name == name :
7575 return prop
7676 return self .__getattribute__ (name )
7777
78- def fill_properties (self ):
78+ def _fill_properties (self ):
7979 # TODO allow subclassing and support entities with __slots__ defined
80- variables = dict (vars (self .user_type ))
80+ variables = dict (vars (self ._user_type ))
8181
8282 # filter only subclasses of Property
8383 variables = {k : v for k , v in variables .items (
8484 ) if issubclass (type (v ), Property )}
8585
8686 for prop_name , prop in variables .items ():
8787 prop .name = prop_name
88- self .properties .append (prop )
88+ self ._properties .append (prop )
8989
9090 if prop .is_id ():
91- if self .id_property :
92- raise Exception (f"Duplicate ID property: \" { self .id_property .name } \" and \" { prop .name } \" " )
93- self .id_property = prop
91+ if self ._id_property :
92+ raise Exception (f"Duplicate ID property: \" { self ._id_property .name } \" and \" { prop .name } \" " )
93+ self ._id_property = prop
9494
9595 if prop ._fb_type == flatbuffers .number_types .UOffsetTFlags :
9696 assert prop ._ob_type in [
@@ -105,34 +105,34 @@ def fill_properties(self):
105105 OBXPropertyType_DoubleVector ,
106106 OBXPropertyType_Flex ,
107107 ], "programming error - invalid type OB & FB type combination"
108- self .offset_properties .append (prop )
108+ self ._offset_properties .append (prop )
109109
110- # print('Property {}.{}: {} (ob:{} fb:{})'.format(self.name , prop.name, prop._py_type, prop._ob_type, prop._fb_type))
110+ # print('Property {}.{}: {} (ob:{} fb:{})'.format(self._name , prop.name, prop._py_type, prop._ob_type, prop._fb_type))
111111
112- if not self .id_property :
112+ if not self ._id_property :
113113 raise Exception ("ID property is not defined" )
114- elif self .id_property ._ob_type != OBXPropertyType_Long :
114+ elif self ._id_property ._ob_type != OBXPropertyType_Long :
115115 raise Exception ("ID property must be an int" )
116116
117- def get_property (self , name : str ):
117+ def _get_property (self , name : str ):
118118 """ Gets the property having the given name. """
119- for prop in self .properties :
119+ for prop in self ._properties :
120120 if prop .name == name :
121121 return prop
122122 raise Exception (f"Property \" { name } \" not found in Entity: \" { self ._name } \" " )
123123
124- def get_property_id (self , prop : Union [int , str , Property ]) -> int :
124+ def _get_property_id (self , prop : Union [int , str , Property ]) -> int :
125125 """ A convenient way to get the property ID regardless having its ID, name or Property. """
126126 if isinstance (prop , int ):
127127 return prop # We already have it!
128128 elif isinstance (prop , str ):
129- return self .get_property (prop ).id
129+ return self ._get_property (prop ).id
130130 elif isinstance (prop , Property ):
131131 return prop .id
132132 else :
133133 raise Exception (f"Unsupported Property type: { type (prop )} " )
134134
135- def get_value (self , object , prop : Property ):
135+ def _get_value (self , object , prop : Property ):
136136 # in case value is not overwritten on the object, it's the Property object itself (= as defined in the Class)
137137 val = getattr (object , prop .name )
138138 if prop ._py_type == np .ndarray :
@@ -147,22 +147,22 @@ def get_value(self, object, prop: Property):
147147 return prop ._py_type () # default (empty) value for the given type
148148 return val
149149
150- def get_object_id (self , obj ) -> int :
151- return self .get_value (obj , self .id_property )
150+ def _get_object_id (self , obj ) -> int :
151+ return self ._get_value (obj , self ._id_property )
152152
153- def set_object_id (self , obj , id_ : int ):
154- setattr (obj , self .id_property .name , id_ )
153+ def _set_object_id (self , obj , id_ : int ):
154+ setattr (obj , self ._id_property .name , id_ )
155155
156- def marshal (self , object , id : int ) -> bytearray :
156+ def _marshal (self , object , id : int ) -> bytearray :
157157 if not hasattr (self ._tl , "builder" ):
158158 self ._tl .builder = flatbuffers .Builder (256 )
159159 builder = self ._tl .builder
160160 builder .Clear ()
161161
162162 # prepare some properties that need to be built in FB before starting the main object
163163 offsets = {}
164- for prop in self .offset_properties :
165- val = self .get_value (object , prop )
164+ for prop in self ._offset_properties :
165+ val = self ._get_value (object , prop )
166166 if prop ._ob_type == OBXPropertyType_String :
167167 offsets [prop .id ] = builder .CreateString (val .encode ('utf-8' ))
168168 elif prop ._ob_type == OBXPropertyType_BoolVector :
@@ -192,17 +192,17 @@ def marshal(self, object, id: int) -> bytearray:
192192 assert False , "programming error - invalid type OB & FB type combination"
193193
194194 # start the FlatBuffers object with the largest number of properties that were ever present in the Entity
195- builder .StartObject (self .last_property_iduid .id )
195+ builder .StartObject (self ._last_property_iduid .id )
196196
197197 # add properties to the FB object
198- for prop in self .properties :
198+ for prop in self ._properties :
199199 prop_id = prop .id
200200 if prop_id in offsets :
201201 val = offsets [prop_id ]
202202 if val :
203203 builder .PrependUOffsetTRelative (val )
204204 else :
205- val = id if prop == self .id_property else self .get_value (object , prop )
205+ val = id if prop == self ._id_property else self ._get_value (object , prop )
206206 if prop ._ob_type == OBXPropertyType_Date :
207207 val = date_value_to_int (val , 1000 ) # convert to milliseconds
208208 elif prop ._ob_type == OBXPropertyType_DateNano :
@@ -214,15 +214,15 @@ def marshal(self, object, id: int) -> bytearray:
214214 builder .Finish (builder .EndObject ())
215215 return builder .Output ()
216216
217- def unmarshal (self , data : bytes ):
217+ def _unmarshal (self , data : bytes ):
218218 pos = flatbuffers .encode .Get (flatbuffers .packer .uoffset , data , 0 )
219219 table = flatbuffers .Table (data , pos )
220220
221221 # initialize an empty object
222- obj = self .user_type ()
222+ obj = self ._user_type ()
223223
224224 # fill it with the data read from FlatBuffers
225- for prop in self .properties :
225+ for prop in self ._properties :
226226 o = table .Offset (prop ._fb_v_offset )
227227 val = None
228228 ob_type = prop ._ob_type
0 commit comments