fixture.dataset

class fixture.dataset.DataSet(default_refclass=None, default_meta=None)

Bases: fixture.dataset.dataset.DataContainer

Defines data to be loaded

A loader will typically want to load a dataset into a single storage medium. I.E. a table in a database.

For a complete example see Using DataSet.

Note that rows are always classes until the dataset instance has been loaded:

>>> class Flowers(DataSet):
...     class violets:
...         color = 'blue'
...     class roses:
...         color = 'red'
... 
>>> f = Flowers()
>>> f.violets.color
'blue'

See DataType for info on how inner classes are constructed.

Row values can also be inherited from other rows, just as normal inheritance works in Python. See the primary_key Meta attribute for how inheritance works on primary keys:

>>> class Recipes(DataSet):
...     class chowder:
...         is_soup = True
...         name = "Clam Chowder"
...     class tomato_bisque(chowder):
...         name = "Tomato Bisque"
... 
>>> r = Recipes()
>>> r.chowder.is_soup
True
>>> r.tomato_bisque.is_soup
True

Keyword Arguments:

default_refclass
A SuperSet to use if None has already been specified in Meta

See DataSetMeta for details about the special inner Meta class

See Using Dataset for more examples of usage.

__iter__()
yields keys of self.meta
data()

returns iterable key/dict pairs.

Note

If possible, use attribute-style definition of rows and columns instead (explained above)

You would only need to override this if you have a DataSet that will break unless it is ordered very specifically. Since class-style DataSet objects are just classes with attributes, its rows will be loaded in alphabetical order. The alternative is to define a DataSet as follows. However, note that this is not as functional as a class-style DataSet:

>>> class Birds(DataSet):
...     def data(self):
...         return (
...             ('blue_jay', dict(
...                 name="Blue Jay")),
...             ('crow', dict(
...                 name="Crow")),)
... 
>>> b = Birds()
>>> b.blue_jay.name
'Blue Jay'
>>> b.crow.name
'Crow'
classmethod shared_instance(**kw)
Returns or creates the singleton instance for this DataSet class
class fixture.dataset.DataSetMeta

Bases: fixture.dataset.dataset.Meta

Configures a DataSet class.

When defining a DataSet class, declare this as DataSet.Meta to configure the DataSet. The following are acknowledged attributes:

storable
an object that should be used to store this DataSet. If omitted the loader’s Style object will look for a storable object in its env, using storable_name
storable_name
the name of the storable object that the loader should fetch from its env to load this DataSet with. If omitted, the loader’s style object will try to guess the storable_name based on its env and the name of the DataSet class
primary_key
this is a list of names that should be acknowledged as primary keys in a DataSet. The default is simply ['id'].

Here is an example of using an inner Meta class to specify a custom storable object to be used when storing a DataSet:

>>> class RecipeStore(object):
...     '''pretend this knows how to save recipes'''
... 
>>> class Recipes(DataSet):
...     class Meta:
...         storable = RecipeStore
...     class chowder:
...         is_soup = True
...         name = "Clam Chowder"
...     class tomato_bisque(chowder):
...         name = "Tomato Bisque"
... 
class fixture.dataset.SuperSet(*datasets)

Bases: fixture.dataset.dataset.DataContainer, fixture.dataset.dataset.DataSetContainer

A set of DataSet classes.

each attribute / key is a DataSet instance.

For example:

>>> from fixture import DataSet
>>> from fixture.dataset import SuperSet
>>> class RecipeData(DataSet):
...     class tomato_bisque:
...         name = "Tomato Bisque"
... 
>>> class CookwareData(DataSet):
...     class pots:
...         type = "cast-iron"
... 
>>> s = SuperSet(RecipeData(), CookwareData())

Now each instance is available by class name:

>>> s.RecipeData.tomato_bisque.name
'Tomato Bisque'
>>> s.CookwareData.pots.type
'cast-iron'
class fixture.dataset.MergedSuperSet(*datasets)

Bases: fixture.dataset.dataset.SuperSet

A collection of DataSet instances.

all attributes of all DataSet classes are merged together so that they are accessible in this class. Duplicate attribute names are not allowed.

For example:

>>> from fixture import DataSet
>>> from fixture.dataset import MergedSuperSet
>>> class RecipeData(DataSet):
...     class tomato_bisque:
...         name = "Tomato Bisque"
... 
>>> class CookwareData(DataSet):
...     class pots:
...         type = "cast-iron"
... 
>>> m = MergedSuperSet(RecipeData(), CookwareData())

Now the rows of each DataSet are available as if they were rows of the MergedSuperSet:

>>> m.tomato_bisque.name
'Tomato Bisque'
>>> m.pots.type
'cast-iron'
class fixture.dataset.DataType(name, bases, cls_attr)

Bases: type

Meta class for creating DataSet classes.

decorate_row(row, name, bases, cls_attr)

Each row (an inner class) assigned to a DataSet will be customized after it is created.

This is because it’s easier to type:

class MyData(DataSet):
    class foo:
        col1 = "bz"
        col2 = "bx"

... than it is to type:

class MyData(DataSet):
class foo(Row):
col1 = “bz” col2 = “bx”

(Note the subclassing that would be required in inner classes without this behavior.)

But more importantly, rows must be able to inherit from other rows, like:

class MyData(DataSet):
    class joe:
        first_name = "Joe"
        last_name = "Phelps"
    class joe_gibbs(joe):
        last_name = "Gibbs"

Here is what happens to each inner class object as it is assigned to a DataSet:

  1. A Row._dataset property is added which is a reference to the DataSet instance.
  2. A Row.ref() property (instance of Ref) is added
  3. Any database primary key inherited from another Row is de-referenced since primary keys must be unique per row. See Using Dataset for an example of referencing primary key values that may or may not exist yet.
class fixture.dataset.DataRow(dataset)

Bases: object

a DataSet row, values accessible by attibute or key.

classmethod columns()
Classmethod that yields all attribute names (except reserved attributes) in alphabetical order
class fixture.dataset.Ref(dataset_class, row)

Bases: object

A reference to a row in a DataSet class.

An instance of this class is accessible on the inner class (a row) in a DataSet as Row.ref()

This allows a DataSet to reference an id column of a “foreign key” DataSet before it exists.

Ref is a Descriptor containing a deferred value to an attribute of a data object (like an instance of a SQLAlchemy mapped class). It provides the DataSet a way to cloak the fact that “id” is an attribute only populated after said data object is saved to the database. In other words, the DataSet doesn’t know or care when it has been loaded or not. It thinks it is referencing “id” all the same. The timing of when id is accessed is handled by the LoadableFixture.

__call__(ref_name)
Return a RefValue instance for ref_name
class fixture.dataset.RefValue(ref, attr_name)

Bases: object

A reference to a value in a row of a DataSet class.

class fixture.dataset.DataContainer(data=None, keys=None)

Bases: object

Contains data accessible by attribute and/or key.

for all internally used attributes, use the inner class Meta. On instances, use self.meta instead.

get(k, default=None)
self.meta.get(k, default)
class fixture.dataset.DataSetContainer

Bases: object

A DataSet of DataSet classes

yields DataSet classes when itered over.

Previous topic

fixture.base

Next topic

fixture.dataset.converter