Databases and datasets¶
Database constraints and dataset generation.
this module contains everything database related, allowing you to create primary keys, foreign keys and autoincrement fields
The classes in the module are listed below.
-
class
dammy.db.
AutoIncrement
(start=1, increment=1)¶ Represents an automatically incrementing field. By default starts by 1 and increments by 1
-
generate
(dataset=None)¶ Generate a value and perform a posterior treatment. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: The value generated by the generator
-
generate_raw
(dataset=None)¶ Generates and updates the next value
Implementation of the generate_raw() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved. It will be ignored.Returns: The next value of the sequence
-
iterator
(dataset=None)¶ Get a iterator which generates values and performs a posterior treatment on them. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A Python iterator
-
iterator_raw
(dataset=None)¶ Get a generator which generates values without posterior treatment.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: Python generator Raises: NotImplementedError
-
-
class
dammy.db.
PrimaryKey
(max_retries=100, **kwargs)¶ Represents a primary key. Every field encapsulated by this class becomes a member of the primary key. A table cannot contain more than one primary key. This class is an alias of the Unique class, with the exception that no more than a primary key can exist on each table, but multiple unique values are supported.
Parameters: k ( dammy.db.BaseGenerator
) – The fields which will be part of the primary keyIn this example the primary key of A will be the field called ‘primary’ which is an autoincrement field:
from dammy import EntityGenerator from dammy.db import PrimaryKey, AutoIncrement class A(EntityGenerator): primary = PrimaryKey(AutoIncrement()) # More attributes...
In this other example the primary key of B will be formed by the fields called ‘field1’ and ‘field2’:
from dammy import EntityGenerator from dammy.db import PrimaryKey, AutoIncrement class B(EntityGenerator): field1 = PrimaryKey(AutoIncrement()) field2 = PrimaryKey(AutoIncrement()) # More attributes...
-
generate
(dataset=None)¶ Generates a unique value
Implementation of the generate() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved.Returns: A unique value generated by the associated generator Raises: MaximumRetriesExceededException
-
generate_raw
(dataset=None)¶ Generates a unique value
Implementation of the generate_raw() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved.Returns: A unique value generated by the associated generator Raises: MaximumRetriesExceededException
-
iterator
(dataset=None)¶ Get a iterator which generates values and performs a posterior treatment on them. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A Python iterator
-
iterator_raw
(dataset=None)¶ Get a generator which generates values without posterior treatment.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: Python generator Raises: NotImplementedError
-
reset
()¶ Reset the uniqueness of the generator.
-
-
class
dammy.db.
ForeignKey
(ref_table, ref_field)¶ Represents a foreign key. The first parameter is the class where the referenced field is and the second a list of strings, each of them containing the name of a field forming the primary key. If the referenced attribut is not unique or primay key, a InvalidReferenceException is raised
Parameters: - ref_table (
dammy.db.EntityGenerator
) – The table where the referenced field is - *args (str) – List of the names of the fields forming the referenced key
Raises: -
generate
(dataset=None)¶ Generate a value and perform a posterior treatment. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: The value generated by the generator
-
generate_raw
(dataset=None)¶ Gets the values corresponding to the key from the given dataset. If the dataset is not specified, a DatasetRequiredException will be raised.
Implementation of the generate_raw() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved.Returns: A unique value generated by the associated generator Raises: DatasetRequiredException
-
get_reference
(dataset)¶ Generates a tuple of values existing in the dataset
Warning
This method is deprecated and it will be removed in the next version. Please use generate() instead
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A tuple containing the values of the fields of the key for a row randomly picked from the dataset Raises: dammy.exceptions.InvalidReferenceException
-
iterator
(dataset=None)¶ Get a iterator which generates values and performs a posterior treatment on them. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A Python iterator
-
iterator_raw
(dataset=None)¶ Get a generator which generates values without posterior treatment.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: Python generator Raises: NotImplementedError
- ref_table (
-
class
dammy.db.
Unique
(max_retries=100, **kwargs)¶ Represents a unique field. The generator encapsulated here, will be guaranteed to generate unique values
Parameters: - u (BaseGenerator) – The generator which will generate unique values
- max_retries (int) – The number of times it will retry to generate the value when it has already been generated
-
generate
(dataset=None)¶ Generates a unique value
Implementation of the generate() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved.Returns: A unique value generated by the associated generator Raises: MaximumRetriesExceededException
-
generate_raw
(dataset=None)¶ Generates a unique value
Implementation of the generate_raw() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrieved.Returns: A unique value generated by the associated generator Raises: MaximumRetriesExceededException
-
iterator
(dataset=None)¶ Get a iterator which generates values and performs a posterior treatment on them. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A Python iterator
-
iterator_raw
(dataset=None)¶ Get a generator which generates values without posterior treatment.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: Python generator Raises: NotImplementedError
-
reset
()¶ Reset the uniqueness of the generator.
-
class
dammy.db.
DatasetGenerator
(*args)¶ -
generate
(dataset=None)¶ Generate a value and perform a posterior treatment. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: The value generated by the generator
-
generate_raw
(dataset=None)¶ Generate a new dataset with the previously given specifications
Implementation of the generate_raw() method from BaseGenerator.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A dict where every key value pair is an attribute and its value Raises: dammy.exceptions.DatasetRequiredException
-
get_sql
(save_to=None, create_tables=True)¶ Gets the dataset as SQL INSERT statements. The generated SQL is always returned and if save_to is specified, it is saved to that location. Additional CREATE TABLE statements are added if create_tables is set to True
Parameters: - save_to (str) – The path where the resulting SQL will be saved.
- create_tables (bool) – If set to true, it will generate the instructions to create the tables.
Returns: A string with the SQL sentences required to insert all the tuples
Warning
This method is deprecated and it will be removed in the next release. Please use to_sql() instead.
-
iterator
(dataset=None)¶ Get a iterator which generates values and performs a posterior treatment on them. By default, no treatment is done and generate_raw() is called.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: A Python iterator
-
iterator_raw
(dataset=None)¶ Get a generator which generates values without posterior treatment.
Parameters: dataset ( dammy.db.DatasetGenerator
or dict) – The dataset from which all referenced fields will be retrievedReturns: Python generator Raises: NotImplementedError
-
to_json
(save_to=None, indent=4)¶ Get the JSON representation of the dataset. If a path is specified, a file is created and the resulting JSON is written to the file. If no path is given, the generated JSON will be returned.
Parameters: - save_to (str) – The path where the JSON will be saved
- indent (int) – The indentation level of the resulting JSON
Returns: String containing the JSON encoded dataset or none if it has been written to a file
-
to_sql
(save_to=None, create_tables=True)¶ Gets the dataset as SQL INSERT statements. The generated SQL is always returned and if save_to is specified, it is saved to that location. Additional CREATE TABLE statements are added if create_tables is set to True
Parameters: - save_to (str) – The path where the resulting SQL will be saved.
- create_tables (bool) – If set to true, it will generate the instructions to create the tables.
Returns: A string with the SQL sentences required to insert all the tuples
-