2. Models

Models are used to describe the data types you wish to use.

2.1. Base Model

The base model

2.2. Variables

Variables within a Model

class dstore.var.Variable(name, default=None, mods=None)[source]

The base class used for creating Variable types This also provides a validate method which iterates over the modifiers and runs their validate method on this variable instance.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()
validate(instance)[source]

Validate the value of an instance of this Variable type (this is used by the dstore.Model.validate method)

Parameters:instance – The instance of this variable type
Returns:If not a valid value, an exception of dstore.Error.ValidationError will be raised

2.2.1. RowID

RowID denotes an instance ID, and is not a Class but an instance of dstore.var.Number:

from dstore import var, mod
RowID = Number( "id", mods = [ mod.AutoIncrement(), mod.PrimaryKey(), mod.Unique() ])

In terms of a MySQL it equates to:

id INT NOT NULL AUTO_INCREMENT,
UNIQUE (id),
PRIMARY KEY (id)

Usage

from dstore import Model, var

class Car( Model ):
    _namespace = "cars.make"
    _vars = [
        var.RowID
    ]

2.2.2. Number

class dstore.var.Number(name, default=None, mods=None)[source]

This variable type is used to store an integer value.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var, mod
var.Number( "year", 1950, mods = [ mod.NotNull() ] )

2.2.3. Boolean

class dstore.var.Boolean(name, default=None, mods=None)[source]

This variable is used to store a boolean value.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.Boolean( "is_available", False )

2.2.4. Character

class dstore.var.Character(name, length, default=None, mods=None)[source]

This variable is used to store a string of static length.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • length – The length of the string
  • default – The default value to give this variable
  • mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list

Usage:

from dstore import var
var.Character( "is_available", 4 )

2.2.5. Binary

class dstore.var.Binary(name, length, default=None, mods=None)[source]

This variable is used to store binary data.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • length – The length of the data
  • default – The default value to give this variable
  • mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list

Usage:

from dstore import var
var.Binary( "data", 32 )

2.2.6. String

class dstore.var.String(name, length, default=None, mods=None)[source]

This variable is used to store a string of variable length.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • length – The length of the string
  • default – The default value to give this variable
  • mods – A list of modifiers. The mod dstore.mod.Length( length ) is always added to this list

Usage:

from dstore import var
var.String( "username", 32 )

2.2.7. Text

class dstore.var.Text(name, default=None, mods=None)[source]

This variable is used to store text.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.Text( "description" )

2.2.8. Float

class dstore.var.Float(name, default=None, mods=None)[source]

This variable is used to store a floating point integer.

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.Float( "price" )

2.2.9. Enum

class dstore.var.Enum(name, values, default=None, mods=None)[source]

This variable is used to store a an enum.

Parameters:
  • name (str) – The name of the Variable instance inside a Model Class
  • values (list[str]) – The list of available choices
  • default (str or None) – The default value to give this variable
  • mods (list[dstore.mod.Mod]) – A list of modifiers. The mod dstore.mod.InEnum( values ) is always added to this list

Usage:

from dstore import var, mod
var.Enum( "car_type", [ "truck", "bus", "ute", "car", "motorbike" ], mods = [ mod.NotNull() ] )

# or

var.Enum(
    name    = "car_type",
    values  = [ "truck", "bus", "ute", "car", "motorbike" ],
    default = "car",
    mods    = [ mod.NotNull() ]
)

2.2.10. ForeignKey

class dstore.var.ForeignKey(namespace)[source]

This variable is used to link the Model Instance to another Model Instance

Parameters:namespace – The namespace of the Model Class this variable references

This variable type is a subclass of dstore.var.Number, with the following mod:

  • dstore.mod.ForeignKey

The name given to this variable is the name of the Model Class being referenced with a suffix of ‘_id”

For example:

from dstore import var
var.ForeignKey( "cars.make" )

Is the same as:

from dstore import var
var.Number( "cars_make_id", mods = [ dstore.mod.ForeignKey( "cars.make" ) )

2.2.11. Date

class dstore.var.Date(name, default=None, mods=None)[source]

This variable is used to store a date value (without a timezone offset).

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.Date( "build_date" )

2.2.12. Time

class dstore.var.Time(name, default=None, mods=None)[source]

This variable is used to store a time value (without a timezone offset).

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.Time( "build_time" )

2.2.13. DateTime

class dstore.var.DateTime(name, default=None, mods=None)[source]

This variable is used to store a date and time value (without a timezone offset).

Parameters:
  • name – The name of the Variable instance inside a Model Class
  • default – The default value to give this variable
  • mods – A list of modifiers, i.e. dstore.mod.NotNull()

Usage:

from dstore import var
var.DateTime( "built_on" )

2.3. Modifiers

class dstore.mod.Mod[source]

The base class used for creating a Modifier.

A Modifier is a way to validate Model instance Variables upon add and/or update.

validate(instance, var, val)[source]
Parameters:
  • instance – The Model instance
  • var – The Model Variable Class
  • val – The Model instance variable
Returns:

If not a valid value, an exception of dstore.Error.ValidationError will be raised

2.3.1. PrimaryKey

class dstore.mod.PrimaryKey[source]

This modifier specifies that the variable is used as a Model ID.

This is automatically used by dstore.val.RowID.

2.3.2. NotNull

class dstore.mod.NotNull[source]

This modifier ensures that the value is not None.

validate(instance, var, val)[source]
Raises:Raises dstore.mod.NotNull.NotNull_Error if val is None

2.3.3. AutoIncrement

class dstore.mod.AutoIncrement[source]

This modifier specifies that the variable is automatically incremented.

This is automatically used by dstore.val.RowID.

2.3.4. Unique

class dstore.mod.Unique[source]

This modifier ensures that the value is unique to all other instances.

2.3.5. ForeignKey

class dstore.mod.ForeignKey(namespace)[source]

This modifier specifies that the value references another Model instance.

This is automatically used by dstore.val.ForeignKey.

2.3.6. Min

class dstore.mod.Min(value)[source]

This modifier ensures that the value is greater than a specific number.

Parameters:value – The minimum value allowed
validate(instance, var, val)[source]
Raises:Raises dstore.mod.Min.Min_Error if val is less than the allowed number.

2.3.7. Max

class dstore.mod.Max(value)[source]

This modifier ensures that the value is less than a specific number.

Parameters:value – The maximum value allowed
validate(instance, var, val)[source]
Raises:Raises dstore.mod.Max.Max_Error if val is greater than the allowed number.

2.3.8. Length

class dstore.mod.Length(value)[source]

This modifier ensures that the value does not exceed the given length.

Parameters:value – The maximum length of the value allowed
validate(instance, var, val)[source]
Raises:Raises dstore.mod.Length.Length_Error if the length of the value exceed the allowed size.

2.3.9. InEnum

class dstore.mod.InEnum(values)[source]

This modifier ensures that the value exists in the enumerated list.

This is automatically used by dstore.val.Enum.

Parameters:value – A list of the allowed values
validate(instance, var, val)[source]
Raises:Raises dstore.mod.InEnum.InEnum_Error if the value does not exist in the enumerated list.