Module xdynamo.model
Classes
class DynModel (*args, id=Default, **initial_values)
-
Used to easily parse/generate JSON from xyn_sdk model's for use in Dynamo. So it will take advantage of all the other features of the xyn_sdk models. This includes automatically converting dates to/from strings, converting strings to numbers, looking up child-objects from other tables automatically, etc. It also will modify the results JSON so remove blank values; to easily prevent these sorts of errors in dynamo boto3 library.
We pass in None for name/service to indicate we don't have an associated table, that we are more of an abstract class.
Creates a new model object. The first/second params need to be passed as positional arguments. The rest must be sent as key-word arguments. Everything is optional.
Args
id
- Specify the
BaseModel.id
attribute, if you know it. If left as Default, nothing will be set on it. It could be set to something via args[0] (ie: a JSON dict). If you do provide a value, it be set last after everything else has been set. *args
-
I don't want to take names from what you could put into 'initial_values', so I keep it as position-only *args. Once Python 3.8 comes out, we can use a new feature where you can specify some arguments as positional-only and not keyword-able.
FirstArg - If Dict:
If raw dictionary parsed from JSON string. It just calls
self.api.update_from_json(args[0])
for you.FirstArt - If BaseModel:
If a
BaseModel
, will copy fields over that have the same name. You can use this to duplicate a Model object, if you want to copy it. Or can be used to copy fields from one model type into another, on fields that are the same name.Will ignore fields that are present on one but not the other. Only copy fields that are on both models types.
**initial_values
- Let's you specify other attribute values for convenience.
They will be set into the object the same way you would normally doing it:
ie:
model_obj.some_attr = v
is the same asModelClass(some_attr=v)
.
Expand source code
class DynModel( RemoteModel, dyn_name=None, dyn_service=Default, dyn_environment=Default, lazy_loader=lazy_load_types_for_dyn_api ): """ Used to easily parse/generate JSON from xyn_sdk model's for use in Dynamo. So it will take advantage of all the other features of the xyn_sdk models. This includes automatically converting dates to/from strings, converting strings to numbers, looking up child-objects from other tables automatically, etc. It also will modify the results JSON so remove blank values; to easily prevent these sorts of errors in dynamo boto3 library. We pass in None for name/service to indicate we don't have an associated table, that we are more of an abstract class. """ api: DynApi[Self] id: str @property def id(self) -> Optional[str]: # We could do some intelligent caching, but for now just calculate each time. try: return DynKey.via_obj(self).id except XModelDynamoNoHashKeyDefinedError: # There is something wrong with class structure, there is no hash-key defined! raise except XRemoteError: # Any other error, we simply don't have a full `id` value assigned to object. return None @id.setter def id(self, value): structure = self.api.structure if type(value) is str: parsed_value = value.split('|') hash_value = parsed_value[0] range_value = parsed_value[1] if len(parsed_value) == 2 else None self.__setattr__(structure.dyn_hash_field.name, hash_value) if range_value: self.__setattr__(structure.dyn_range_field.name, range_value) return raise NotImplementedError( "Read-only for now, but want to support it. " "Supporting it would involve parsing ID with DynKey, and taking hash/range key " "components and setting them on the proper attributes." "\n\n" "Also, want to eventually support for using 'id' as a HashField " "(ie: a single/only key called 'id' in dynamo-db)" )
Ancestors
- xmodel.remote.model.RemoteModel
- xmodel.base.model.BaseModel
- abc.ABC
Class variables
var api : DynApi[xmodel.base.model.BaseModel]
Instance variables
prop id : str
-
Expand source code
@property def id(self) -> Optional[str]: # We could do some intelligent caching, but for now just calculate each time. try: return DynKey.via_obj(self).id except XModelDynamoNoHashKeyDefinedError: # There is something wrong with class structure, there is no hash-key defined! raise except XRemoteError: # Any other error, we simply don't have a full `id` value assigned to object. return None