model - Dynamically Creatinig Hardcoded Data Models

Module fo dynamically creating hardcoded data models.

class cassy.model.MetaModel(name: str, primary_keys: dict, clustering_keys: dict, columns: dict)[source]

Bases: object

Dynamically create a table model.

Parameters
  • name (str) – String specifying the table name. Will be the name of the CQL table for this model.

  • primary_keys (dict) –

    Dictionairy with primary keys as key and respective column data type as value as in

    primary_keys = {"my_primary_key": "Text"}
    

  • clustering_keys (dict) –

    Dictionairy with clustering keys as keys and ("Column Data Type", "clustering_order") tuple specifying column data type and clustering order as in:

    clustering_keys = {
        "my_clustering_key": ("Text", "ASC"),
        "my_other_clustering_key": ("Text", "DESC"),
    }
    

  • columns (str, container) –

    Dictionairy with column names as key and respective column data type as value as in

    columns = {
        "my_attribute": "Text",
        "my_other_attribute": "Int",
        "my_third_attribute": "Blob",
    }
    

    See the DataStax documentation for available datatypes.

Examples

Default use case:

my_meta_model = MetaModel(
    name="CrawfordCommonFruitingTrees",
    primary_keys={"Latin": "Text"},
    clustering_keys={
        "English": ("Text", "ASC"),
        "German": ("Text", "ASC"),
    },
    columns={"USDA_Hardiness": "Integer"},
)
cassy.model.create_data_model(meta_model, path, overwrite=False, backup=True)[source]

Dynamically create a table model.

Creates a Datastax Cassandry Cqlenine Data Model

Parameters
  • meta_model (MetaModel) – dataclass describing the model data.

  • path (pathlib.Path, str) – Path or string specifying the folder the hardcoded module will be located.

  • overwrite (bool, default=False) – Boolean indicating whether the hardcoded model data file should be overwritten. If backup is True, exsting file will be renamed to existing-name_backup_hash(timestamp).py

  • backup (bool, default=True) – Boolean indicating whether the hardcoded model data file should be kept as backup in case of overwriting. Exsting file will be renamed to existing-name_backup_hash(timestamp).py

Returns

Path the hardcoded data model file was created at.

Return type

pathlib.Path

Examples

Default use case:

my_meta_model = MetaModel(
    name="CrawfordCommonFruitingTrees",
    primary_keys={"Latin": "Text"},
    clustering_keys={
        "English": ("Text", "ASC"),
        "German": ("Text", "ASC"),
    },
    columns={"USDA_Hardiness": "Integer"},
)

path = create_data_model(
    meta_model=my_meta_model,
    path=os.path.join("~", ".fogd.d", "my_model.py"),
    overwrite=True,
)
cassy.model.retrieve_data_model(path, model_name)[source]

Retrieve previously hardcoded data model.

Parameters
Returns

Datastax Cassandry Cqlenine Data Model previously hardcoded in path.

Return type

DataModel

Examples

Default use case:

model = retrieve_data_model(
    path=os.path.join("~", ".fogd.d", "pytest_home_model.py"),
    model_name="CrawfordCommonFruitingTrees",
)