Documentation for stdnet 0.8.2. For development docs, go here.
Stdnet comes with a singleton setting instance which can be accessed by:
from stdnet.conf import settings
The setting instance contains few default parameters used in throughout the library. This parameters can be changed by the user by simply overriding them.
the default stdnet.BackendDataServer connection string. Check the registering models documentation.
Default "redis://127.0.0.1:6379/?db=7".
Set stdnet to use the internal python parser for redis. By default it is set to False which causes the library to choose the best possible available. More information is contained in the redis parser documentation.
Default False.
The maximim number of connections to have opened at the same time.
Default unlimited.
The Redis Connection class. If not set the stdnet.lib.connection.Connection will be used.
Default None
To change settings:
from stdnet.conf import settings
settings.DEFAULT_BACKEND = 'redis://127.0.0.1:6379/?db=5'
Stdnet comes with a bunch of extendible utilities for serializing model data into different formats.
Retrieve a serializer register as name. If the serializer is not available a ValueError exception will raise. A common usage pattern:
qs = MyModel.objects.query().sort_by('id')
s = odm.get_serializer('json')
s.dump(qs)
Register a new serializer to the library.
Parameters: |
|
---|
The stdnet serializer base class. During initialization, the options dictionary is used to override the default_options. These are specific to each Serializer implementation.
Dictionary of default options which are overwritten during initialisation. By default it is an empty dictionary.
Dictionary of options.
CList of data to dump into a stream.
Add a Query qs into the collection of data to dump into a stream. No writing is done until the write() method.
Write the serialized data into a stream. If stream is not provided, a python StringIO is used.
Returns: | the stream object. |
---|
Load a stream of data into the database.
Parameters: |
---|
This method must be implemented by subclasses.
The default Serializer of stdnet. It serialise/unserialise models into json data. It has one option given by the indent of the json string for pretty serialisation.
Callback when a model is about to be loaded. If it returns the model, the model will get loaded otherwise it will skip the loading.
Callback when loading of data is finished
The default JSON encoder used by stdnet. It provides JSON serialization for four additional classes:
See also
It is the default encoder for stdnet.odm.JSONField
The default JSON decoder hook. It is the inverse of stdnet.utils.jsontools.JSONDateDecimalEncoder.
Convert a flat representation of a dictionary to a nested representation. Fields in the flat representation are separated by the splitter parameters.
Parameters: |
|
---|---|
Return type: | a nested dictionary |
Multiply dictionaries by a numeric values and add them together.
Parameters: |
|
---|
Only common fields are aggregated. If a field has a non-numeric value it is not included either.
Classes used for encoding and decoding stdnet.odm.Field values.
Virtaul class for encoding data in data structures. It exposes two methods for encoding and decoding data to and from the data server.
The type of data once loaded into python
Serialize data for database
Unserialize data from database
True if this Encoder requires a stdnet.odm.Session.
Load an iterable. By default it returns a generator of data loaded via the loads() method.
Parameters: |
|
---|---|
Returns: | an iterable over decoded data. |
These are all available Encoder:
A dummy encoder class
The default unicode encoder. It converts bytes to unicode when loading data from the server. Viceversa when sending data.
It decodes values into unicode unless they are numeric, in which case they are decoded as such.
The binary encoder
A JSON encoder for maintaning python types when dealing with remote data structures.
A safe pickle serializer. By default we use protocol 2 for compatibility between python 2 and python 3.
Convert to and from python datetime objects and unix timestamps
A general StdNet exception
A stdnet.StdNetException raised when stdnet is somehow improperly configured
A stdnet.StdNetException raised by a stdnet.odm.Query.
Generic Field error
A stdnet.FieldError raised when passing a wrong value to a field. This exception is cought during the model instance validation algorithm in stdnet.odm.base.Metaclass.is_valid().
Stdnet includes a signal dispatcher which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They are especially useful when many pieces of code may be interested in the same events.
The data mapper provide with the following built-in signals in the stdnet.odm module:
It is also possible to add callback to single instances in the following way:
instance = MyModel(...)
instance.post_commit(callable)
Utility function for populating lists with random data. Useful for populating database with data for fuzzy testing. Supported data-types
For example:
populate('string',100, min_len=3, max_len=10)
create a 100 elements list with random strings with random length between 3 and 10
For example:
from datetime import date
populate('date',200, start = date(1997,1,1), end = date.today())
create a 200 elements list with random datetime.date objects between start and end
For example:
populate('integer',200, start = 0, end = 1000)
create a 200 elements list with random int between start and end
For example:
populate('float', 200, start = 0, end = 10)
create a 200 elements list with random floats between start and end
For example:
populate('choice', 200, choice_from = ['pippo','pluto','blob'])
create a 200 elements list with random elements from choice_from.
Test case classes and plugins for stdnet testing. Requires pulsar.
A unittest.TestCase subclass for testing stdnet with synchronous and asynchronous connections. It contains several class methods for testing in a parallel test suite.
class attribute which indicates which backend can run the test. There are several options:
A stdnet.BackendDataServer for this TestCase class. It is a class attribute which is different for each TestCase class and it is created by the setUpClass() method.
A DataGenerator class for creating data. The data is created during the setUpClass() class method.
The DataGenerator instance created from data_cls.
The default StdModel for this test. A class attribute.
A tuple of models which can be registered by this test. The model is always the model at index 0 in models.
A stdnet.odm.Router with all models registered with backend.
alias of DataGenerator
Set up this TestCase before test methods are run. here is where a backend server instance is created and it is unique for this TestCase class. It create the mapper, a stdnet.odm.Router with all models registered. There shouldn’t be any reason to override this method, use after_setup() class method instead.
This class method can be used to setup this TestCase class after the setUpClass() was called. By default it does nothing.
Create a new stdnet.odm.Session bind to the TestCase.backend attribute.
Shortcut function to create a query for a model.
Treat iterable as a container of asynchronous results.
Assert the value of a primary key in a backend agnostic way.
Parameters: |
|
---|
A generator of data. It must be initialised with the size parameter obtained from the command line which is avaiable as a class attribute in TestCase.
A dictionary of sizes for this generator. It is a class attribute with the following entries: tiny, small, normal, big and huge.
The actual size of the data to be generated. Obtained from the sizes and the input size code during initialisation.
Called during initialisation to generate the data. kwargs are additional key-valued parameter passed during initialisation. Must be implemented by subclasses.
A shortcut for the stdnet.utils.populate() function. If size is not given, the size is used.
Return a random string