wdmmg.lib.loader.Loader - a class to use in data loader

class wdmmg.lib.loader.Loader(dataset_name, unique_keys, label, description=u'', metadata=None, currency=u'gbp', time_axis='time.from.year', changeset=None)

A helper class that provides all methods required to save the data from a dataset into Open Spending in an efficient way.

Read the loaders.rst in the /doc folder.

classify_entry(entry, classifier, name)

Update the entry to be classified with classifier. entry is mutated, but not returned.

entry
A dict like object, e.g. an instance of wdmmg.model.Base.
classifier
A wdmg.model.Classifier object
name
This is the key where the value of the classifier will be saved. This my be the same as classifier[‘name’].

return:None

compute_aggregates()

This method has to be called as the last method when using the loader. It will add additional, required data to the database.

create_classifier(name, taxonomy, label=u'', description=u'', _cache=None, **classifier)

create a :class:wdmmg.model.`classifier`. The name has to be unique for the taxonomy. The classifier will be updated with the values for label, description and **classifier Note that the classifier for (name, taxonomy) is only created or updated if it is not already in the _cache.

Arguments:

name
name of the classifier. (unicode)
taxonomy
The taxonomy to which the classifier name belongs. (unicode)
label, descripiton, **classifiers
used to update the classifier for the first time
_cache
Use the given dict like object for caching. Normally not used by callers. It can be used to force an update of a classifier that was created/updated by an earlier call. With None (default), the Loader uses internal caching.

Returns: An wdmmg.model.Classifier object

Raises:
AssertionError if more than one Classifer object with the Name existes in the taxonomy
create_dimension(key, label, description, **kwargs)

Describe the data you save on entries with the key key. Describe it with a label and a description.

key
The key on the model where the data to describe is saved. (type: unicode)
label
A label (title) to display to the user (type: unicode)
description
A description to display to the user. (type: unicode)
kwargs
You can add more information as keyword arguments. All values have to be unicode.

Returns: None

Raises: TypeError if one of the arguments is of the wrong type.

create_entity(name, label=u'', description=u'', _cache=None, **entity)

Create or update an wdmmg.model.Entity object in the database when the method is called with a name for the first time. The entity is identified soley by it’s name. The entry will only be created if it is not in the _cache.

name
Name of the entity.
label, description
label an description of the entity (unicodes)
**entity
Keyword arguments that are saved in the entity.
_cache
Use the given dict like object for caching. Normally not used by callers. It can be used to force an update of an entity that was created/updated by an earlier call. With None (default), the Loader uses internal caching.

Returns: The created Entity object

Raises:
AssertionError if more than one Entity object with the Name existes.
create_entry(**entry)

Create or update an wdmmg.model.Entry object in the database.

entry

A dict containing at least these information:

amount
amount spend
from
wdmmg.model.Entity object
to
wdmmg.model.Entity object
data for unique keys
Data for all unique_keys of the Loader

If entry does not contain a currency, the currency of the loader object is used.

Returns: A mongodb query spec that can be used with wdmmg.model.Entry.find_one() to get the object.

Raises: AssertionError in some cases if the Entry violates the datamodel (fixme: full assertions and description!)

create_view(cls, add_filters, name, label, dimension, breakdown=None, view_filters={})

Create a view. The view will be computed when you call finalize().

cls
A model class (inheriting from wdmmg.model.Base)
add_filters
A mongodb query spec used as a query to select the instances of cls that will be used for the view.
name
A name for the view. This name must be unique for all views for cls in an Open Spending site.
label
A label that can be displayed to the user.
dimensions
The dimensions that will be used to compute the view
breakdown
...
view_filters
...

Returns: A wdmmg.lib.views.View object.

ensure_index(modelcls, keys)

Ensure an index is created for the collection of modelcls exists for the combination of keys. The index will be ascending for all keys.

modelcsl
A model class inheriting from wdmmg.model.Base.
keys
A list of strings.
get_classifier(name, taxonomy, _cache=None)

Get the classifier object with the name name for the taxonomy taxonomy. This will be cached to speed up the loader

name
name of the classifier. (unicode)
taxonomy
The taxonomy to which the classifier name belongs. (unicode)
_cache
Use the given dict like object for caching. Normally not used by callers. It can be used to ensure the classifier is fetched from the database

Returns: An wdmmg.model.Classifier object if found or None.

get_default_society()

Get a default wdmmg.model.Entity object that represents the society (general public) to use across all datasets. This can be called only one time.

Previous topic

Models

Next topic

wdmmg.lib.times - Convert dates to persistable date range structures

This Page