示例#1
0
 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 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)