Documentation for python-stdnet's DEVELOPMENT version. Get the release docs here.

Export & Load data

Stdnet comes with utilities for exporting and loading models from files. These serialization utilities are useful for backing up your models, porting your data to other databases or creating test databases.

There are two serializers included in the standard distribution: json and csv.

Exporting Data

To export data is quite strightforward, you create a Query and pass it to a Serializer.dump() method. Using the models router in the first tutorial:

>>> from stdnet import odm
>>> json = odm.get_serializer('json')
>>> qs = models.instrument.query()
>>> json.dump(qs)

So fat the json serializer has not written anything to file. Therefore we can add additional queries:

>>> qs = models.fund.query()
>>> json.dump(qs)

To write to file we use the Serializer.write() methods:

>>> with open('data.json','w') as stream:
>>>     json.write(stream)

Loading Data

To load data from a file or a stream we starts from a Router which contains all the models we need:

>>> json = odm.get_serializer('json')
>>> with open('data.json','r') as f:
>>>     data = f.read()
>>> json.load(models, data)

Creating a Serializer

To create a new serializer, one starts by subclassing the Serializer and implement the Serializer.dump() and Serializer.load() and Serializer.write() methods:

from stdnet import dm

class MySerializer(odm.Serializer):

    def dump(self, qs):
        ...

    def write(self, stream=None):
        ...

    def load(self, stream, model=None):
        ...

To be able to use the MySerializer it needs to be registered via the register_serializer():

odm.register_serializer('custom', MySerializer)

Table Of Contents

Previous topic

Double Underscore Notation

Next topic

Asynchronous usage

This Page