def from_data_dict(cls, data_dict): # - This has to be overridden because we have to pre-process # incoming address and (maybe, eventually?) product-list # values... if data_dict.get('address'): data_dict['address'] = Address.from_dict(data_dict['address']) ####### NOTE: Changes made here, for whatever reason might # arise, may also need to be made in # HMSMongoDataObject.from_data_dict – it's the same ####### process! # - Assure that we have the collection of keys that are # allowed for the class! if cls._data_dict_keys == None: from inspect import getfullargspec argspec = getfullargspec(cls.__init__) init_args = argspec.args try: init_args.remove('self') except: pass try: init_args.remove('cls') except: pass print(argspec) if argspec.varargs: init_args.append(argspec.varargs) if argspec.varkw: init_args.append(argspec.varkw) raise AttributeError( '%s.from_data_dict cannot be used because the %s ' 'class has not specified what data-store keys are ' 'allowed to be used to create new instances from ' 'retrieved data. Set %s._data_dict_keys to a list ' 'or tuple of argument-names present in %s.__init__ ' '(%s)' % (cls.__name__, cls.__name__, cls.__name__, cls.__name__, "'" + "', '".join(init_args) + "'")) # - Remove any keys that aren't listed in the class' # initialization arguments: data_dict = dict([(key, data_dict[key]) for key in data_dict.keys() if key in cls._data_dict_keys]) # - Then create and return an instance of the class return cls(**data_dict)
def __init__( self, name: (str, ), # - Required arguments from Address street_address: (str, ), city: (str, ), # - Local optional arguments items: (dict, ) = {}, # - Optional arguments from Address building_address: (str, None) = None, region: (str, None) = None, postal_code: (str, None) = None, country: (str, None) = None, # - Optional arguments from BaseDataObject/JSONFileDataObject oid: (UUID, str, None) = None, created: (datetime, str, float, int, None) = None, modified: (datetime, str, float, int, None) = None, is_active: (bool, int, None) = None, is_deleted: (bool, int, None) = None, is_dirty: (bool, int, None) = None, is_new: (bool, int, None) = None, ): """ Object initialization. self .............. (Order instance, required) The instance to execute against name .............. (str, required) The name of the addressee street_address .... (str, required) The base street-address of the location the instance represents city .............. (str, required) The city portion of the street- address that the instance represents items ............. (dict, optional, defaults to {}) The dict of oids-to-quantities of products in the order building_address .. (str, optional, defaults to None) The second line of the street address the instance represents, if applicable region ............ (str, optional, defaults to None) The region (state, territory, etc.) portion of the street- address that the instance represents postal_code ....... (str, optional, defaults to None) The postal-code portion of the street-address that the instance represents country ........... (str, optional, defaults to None) The country portion of the street-address that the instance represents oid ............... (UUID|str, optional, defaults to None) created ........... (datetime|str|float|int, optional, defaults to None) modified .......... (datetime|str|float|int, optional, defaults to None) is_active ......... (bool|int, optional, defaults to None) is_deleted ........ (bool|int, optional, defaults to None) is_dirty .......... (bool|int, optional, defaults to None) is_new ............ (bool|int, optional, defaults to None) """ # - Call parent initializers if needed Address.__init__(self, street_address, city, building_address, region, postal_code, country) JSONFileDataObject.__init__(self, oid, created, modified, is_active, is_deleted, is_dirty, is_new) # - Set default instance property-values using _del_... methods self._del_items() self._del_name() # - Set instance property-values from arguments using # _set_... methods self._set_name(name) if items: self._set_items(items) # - Perform any other initialization needed self._set_is_dirty(False)
def _del_street_address(self) -> None: result = Address._del_street_address(self) self._set_is_dirty(True) return result
def _del_postal_code(self) -> None: result = Address._del_postal_code(self) self._set_is_dirty(True) return result
def _del_region(self) -> None: result = Address._del_region(self) self._set_is_dirty(True) return result
def _del_country(self) -> None: result = Address._del_country(self) self._set_is_dirty(True) return result
def _del_building_address(self) -> None: result = Address._del_building_address(self) self._set_is_dirty(True) return result
def _set_street_address(self, value: str) -> None: result = Address._set_street_address(self, value) self._set_is_dirty(True) return result
def _set_postal_code(self, value: (str, None)) -> None: result = Address._set_postal_code(self, value) self._set_is_dirty(True) return result
def _set_region(self, value: (str, None)) -> None: result = Address._set_region(self, value) self._set_is_dirty(True) return result
def _set_building_address(self, value: (str, None)) -> None: result = Address._set_building_address(self, value) self._set_is_dirty(True) return result
def from_data_dict(cls, data_dict: (dict, )): if data_dict.get('address'): data_dict['address'] = Address.from_dict(data_dict['address']) return cls(**data_dict)
# Imports needed after member # # definition (to resolve circular # # dependencies - avoid if at all # # possible # ####################################### ####################################### # Code to execute if the module is # # called directly # ####################################### if __name__ == '__main__': pass import json address = Address('12345 Main Street', 'City Name') a = Artisan('John Smith', '*****@*****.**', address) a.save() # print(json.dumps(a.to_data_dict(), indent=4, sort_keys=True)) # artisans = Artisan.get() # print(artisans) o = Order('name', 'street address', 'city') o.save() p = Product('name', 'summary', True, True) p.save() # products = Product.get() # print(products)