flywheel.model_meta module

Model metadata and metaclass objects

class flywheel.model_meta.ModelMetaclass(name, bases, dct)[source]

Bases: type

Metaclass for Model objects

Merges model metadata, sets the meta_ attribute, and performs validation checks.

class flywheel.model_meta.ModelMetadata(model)[source]

Bases: object

Container for model metadata

Parameters:model : Model

Attributes

abstract Getter for abstract
name (str) The unique name of the model. This is set by the ‘_name’ field in __metadata__. Defaults to the name of the model class.
global_indexes (list) List of global indexes (hash_key, [range_key]) pairs.
related_fields (dict) Mapping of field names to set of fields that change when that field changes (usually just that field name, but can be more if composite fields use it)
orderings (list) List of Ordering
throughput (dict) Mapping of ‘read’ and ‘write’ to the table throughput (default 5, 5)
abstract[source]

Getter for abstract

create_dynamo_schema(connection, tablenames=None, test=False, wait=False, throughput=None, namespace=())[source]

Create all Dynamo tables for this model

Parameters:

connection : DynamoDBConnection

tablenames : list, optional

List of tables that already exist. Will call ‘describe’ if not provided.

test : bool, optional

If True, don’t actually create the table (default False)

wait : bool, optional

If True, block until table has been created (default False)

throughput : dict, optional

The throughput of the table and global indexes. Has the keys ‘read’ and ‘write’. To specify throughput for global indexes, add the name of the index as a key and another ‘read’, ‘write’ dict as the value.

namespace : str or tuple, optional

The namespace of the table

Returns:

table : str

Table name that was created, or None if nothing created

ddb_tablename(namespace=())[source]

The name of the DynamoDB table

Parameters:

namespace : list or str, optional

String prefix or list of component parts of a prefix for the table name. The prefix will be this string or strings (joined by ‘-‘).

delete_dynamo_schema(connection, tablenames=None, test=False, wait=False, namespace=())[source]

Drop all Dynamo tables for this model

Parameters:

connection : DynamoDBConnection

tablenames : list, optional

List of tables that already exist. Will call ‘describe’ if not provided.

test : bool, optional

If True, don’t actually delete the table (default False)

wait : bool, optional

If True, block until table has been deleted (default False)

namespace : str or tuple, optional

The namespace of the table

Returns:

table : str

Table name that was deleted, or None if nothing deleted

get_ordering_from_fields(eq_fields, fields)[source]

Get a unique ordering from constraint fields.

This does a best-effort guess of which index is being queried. It prioritizes indexes that have a constraint on the range key. It prioritizes the primary key over local and global indexes.

Parameters:

eq_fields : list

List of field names that are constrained with ‘=’.

fields : list

List of field names that are constrained with inequality operators (‘>’, ‘<’, ‘beginswith’, etc)

Returns:

ordering : Ordering

Raises:

exc : TypeError

If more than one possible Ordering is found

get_ordering_from_index(index)[source]

Get the ordering with matching index name

hk(obj=None, scope=None)[source]

Construct the primary key value

index_pk_dict(index_name, obj=None, scope=None, ddb_dump=False)[source]

Get the primary key dict for an index (includes the table key)

pk_dict(obj=None, scope=None, ddb_dump=False)[source]

Get the dynamo primary key dict for an item

pk_tuple(obj=None, scope=None, ddb_dump=False, ddb_load=False)[source]

Get a tuple that represents the primary key for an item

post_create()[source]

Create the orderings

post_validate()[source]

Build the dict of related fields

rk(obj=None, scope=None)[source]

Construct the range key value

update_dynamo_schema(connection, test=False, wait=False, throughput=None, namespace=())[source]

Updates all Dynamo table global indexes for this model

Parameters:

connection : DynamoDBConnection

test : bool, optional

If True, don’t actually create the table (default False)

wait : bool, optional

If True, block until table has been created (default False)

throughput : dict, optional

The throughput of the table and global indexes. Has the keys ‘read’ and ‘write’. To specify throughput for global indexes, add the name of the index as a key and another ‘read’, ‘write’ dict as the value.

namespace : str or tuple, optional

The namespace of the table

Returns:

table : str

Table name that altered, or None if nothing altered

validate_model()[source]

Perform validation checks on the model declaration

class flywheel.model_meta.Ordering(meta, hash_key, range_key=None, index_name=None)[source]

Bases: object

A way that the models are ordered

This will be a combination of a hash key and a range key. It may be the primary key, a local secondary index, or a global secondary index.

pk_dict(obj=None, scope=None, ddb_dump=False)[source]

Get the dynamo primary key dict for this ordering

query_kwargs(eq_fields, fields)[source]

Get the query and filter kwargs for querying against this index

exception flywheel.model_meta.ValidationError[source]

Bases: exceptions.Exception

Model inconsistency

flywheel.model_meta.merge_metadata(cls)[source]

Merge all the __metadata__ dicts in a class’s hierarchy

keys that do not begin with ‘_’ will be inherited.

keys that begin with ‘_’ will only apply to the object that defines them.