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
See DataSetMeta for details about the special inner Meta class
See Using Dataset for more examples of usage.
returns iterable key/dict pairs.
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'
Configures a DataSet class.
When defining a DataSet class, declare this as DataSet.Meta to configure the DataSet. The following are acknowledged attributes:
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" ...
Bases: fixture.dataset.dataset.DataContainer, fixture.dataset.dataset.DataSetContainer
A set of DataSet classes.
each attribute / key is a DataSet instance.
>>> 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'
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.
>>> 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'
Meta class for creating DataSet classes.
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:
a DataSet row, values accessible by attibute or key.
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.
A reference to a value in a row of a DataSet class.
Contains data accessible by attribute and/or key.
for all internally used attributes, use the inner class Meta. On instances, use self.meta instead.