Documentation for python-stdnet's DEVELOPMENT version. Get the release docs here.
The object-data mapper (ODM) is the core of the library. It defines an API for mapping data in the backend key-value store to objects in Python. Its name is closely related to object relational Mapping (ORM), a programming technique for converting data between incompatible type systems in traditional relational databases and object-oriented programming languages.
The object data mapper presents a method of associating user-defined Python classes, referred as models, with data in a stdnet.BackendDataServer. These python classes are subclasses of stdnet.odm.StdModel.
A Model which contains data in Field. This represents the main class of stdnet.odm module.
True if this StdModel instance has all back-end data loaded. This applies to persistent instances only. This property is used when committing changes. If all data is available, the commit will replace the previous object data entirely, otherwise it will only update it.
Generator of fields loaded from database
Generator of fields,values pairs. Fields correspond to the ones which have been loaded (usually all of them) or not loaded but modified. Check the load_only query function for more details.
If exclude_cache evaluates to True, fields with Field.as_cache attribute set to True won’t be included.
Return type: | a generator of two-elements tuples |
---|
Set cache fields to None. Check Field.as_cache for information regarding fields which are considered cache.
Retrieve the value for the attribute name. The name can be nested following the double underscore notation, for example group__name. If the attribute is not available it raises AttributeError.
Utility method for cloning the instance as a new object.
Parameters: | data – additional which override field data. |
---|---|
Return type: | a new instance of this class. |
Kick off the validation algorithm by checking all StdModel.loadedfields against their respective validation algorithm.
Return type: | Boolean indicating if the model validates. |
---|
Return a dictionary of serialised scalar field for pickling. If the exclude_cache flag is True, fields with Field.as_cache attribute set to True will be excluded.
Return a JSON serialisable dictionary representation.
Load a the ForeignKey field name if this is part of the fields of this model and if the related object is not already loaded. It is used by the lazy loading mechanism of one-to-many relationships.
Parameters: |
|
---|---|
Returns: | the related StdModel instance. |
Returns the Field instance at name if available, otherwise it returns None.
This is the base class for both StdModel and Structure classes. It implements the uuid attribute which provides the universal unique identifier for an instance of a model.
A class attribute which is an instance of ModelMeta, it containes all the information needed by a stdnet.BackendDataServer.
The Session which loaded the instance. Only available, when the instance has been loaded from a stdnet.BackendDataServer via a query operation.
Exception raised when an instance of a model does not exist.
alias of ObjectNotFound
Exception raised when an instance of a model does not validate. Usually raised when trying to save an invalid instance.
alias of ObjectNotValidated
Return the current ModelState for this Model. If kwargs parameters are passed a new ModelState is created, otherwise it returns the cached value.
Value of primary key
The current Session for this model.
The stdnet.BackendDatServer for this instance.
It can be None.
The read stdnet.BackendDatServer for this instance.
It can be None.
Provided for compatibility with StdModel.get_attr_value(). For this class it simply get the attribute at name:
return getattr(self, name)
Create a Model class for objects requiring and interface similar to StdModel. We refers to this type of models as local models since instances of such models are not persistent on a stdnet.BackendDataServer.
Parameters: |
|
---|---|
Returns: | a local Model class. |
A class for storing meta data for a Model class. To override default behaviour you can specify the Meta class as an inner class of Model in the following way:
from datetime import datetime
from stdnet import odm
class MyModel(odm.StdModel):
timestamp = odm.DateTimeField(default = datetime.now)
...
class Meta:
ordering = '-timestamp'
name = 'custom'
Parameters: |
|
---|
This is the list of attributes and methods available. All attributes, but the ones mantioned above, are initialized by the object relational mapper.
If True, This is an abstract Meta class.
Unless specified it is the name of the directory or file (if at top level) containing the Model definition. It can be customised.
The modelkey which is by default given by app_label.name.
Optional name of a Field in the model. If provided, model indices will be sorted with respect to the value of the specified field. It can also be a autoincrement instance. Check the sorting documentation for more details.
Default: None.
Ordered list of all Field which are not StructureField. The order is the same as in the Model definition. The pk field is not included.
List of Field which are indices (Field.index attribute set to True).
Dictionary of related.RelatedManager for the model. It is created at runtime by the object data mapper.
List of ManyToManyField names for the model. This information is useful during registration.
Model type, either structure or object.
Primary key name. A shortcut for self.pk.name.
Convert the primary key value to a valid python representation.
Perform validation for instance and stores serialized data, indexes and errors into local cache. Return True if the instance is ready to be saved to database.
Return a two elements tuple containing a list of fields names and a list of field attribute names.
Model metadata in a dictionary
The database state of a Model.
Action to be performed by the backend server when committing changes to the instance of Model for which this is a state.
True if the instance is persistent in the backend server.
Instance primary key or a temporary key if not yet available.
An autoincrement is used in a StdModel Meta class to specify a model with incremental sorting.
The amount to increment the score by when a duplicate element is saved.
Default: 1.
For example, the stdnet.apps.searchengine.Word model is defined as:
class Word(odm.StdModel):
id = odm.SymbolField(primary_key = True)
class Meta:
ordering = -autoincrement()
This means every time we save a new instance of Word, and that instance has an id already available, the score of that word is incremented by the incrby attribute.
Base class for Query and QueryElement.
The StdModel._meta attribute.
the stdnet.BackendDataServer class for this query.
A Q performs a series of operations and ultimately generate of set of matched elements ids. If on the other hand, a different field is required, it can be specified with the get_field() method. For example, lets say a model has a field called object_id which contains ids of another model, we could use:
qs = session.query(MyModel).get_field('object_id')
to obtain a set containing the values of matched elements object_id fields.
Parameters: | field – the name of the field which will be used to obtained the matched elements value. Must be an index. |
---|---|
Return type: | a new Q instance. |
This is a virtual method with different implementation in Query and QueryElement.
A Query is produced in terms of a given Session, using the Session.query() method:
qs = session.query(MyModel)
A query is equivalent to a collection of SELECT statements for a standard relational database. It has a a generative interface whereby successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.
ATTRIBUTES
The StdModel._meta attribute.
The Session which created the Query via the Session.query() method.
the stdnet.BackendDataServer holding the data to query.
When iterating over a Query, you get back instances of the model class. However, if _get_field is specified you get back values of the field specified. This can be changed via the get_field() method:
qs = query.get_field('name').all()
the results is a list of name values (provided the model has a name field of course).
Default: None.
Dictionary containing the filter lookup parameters each one of them corresponding to a where clause of a select. This value is manipulated via the filter() method.
Default: {}.
Dictionary containing the exclude lookup parameters each one of them corresponding to a where clause of a select. This value is manipulated via the exclude() method.
Default: {}.
optional ordering field.
optional text to filter result on.
Default: "".
METHODS
A Query is not initialized directly but via the Session.query() or Manager.query() methods.
Create a new Query with additional clauses corresponding to where or limit in a SQL SELECT statement.
Parameters: | kwargs – dictionary of limiting clauses. |
---|---|
Return type: | a new Query instance. |
For example:
qs = session.query(MyModel)
result = qs.filter(group = 'planet')
Returns a new Query with additional clauses corresponding to EXCEPT in a SQL SELECT statement.
Parameters: | kwargs – dictionary of limiting clauses. |
---|---|
Return type: | a new Query instance. |
Using an equivalent example to the filter() method:
qs = session.query(MyModel)
result1 = qs.exclude(group = 'planet')
result2 = qs.exclude(group__in = ('planet','stars'))
Return a new Query obtained form the union of this Query with one or more queries. For example, lets say we want to have the union of two queries obtained from the filter() method:
query = session.query(MyModel)
qs = query.filter(field1 = 'bla').union(query.filter(field2 = 'foo'))
Return a new Query obtained form the intersection of this Query with one or more queries. Workds the same way as the union() method.
Sort the query by the given field
Parameters: | ordering – a string indicating the class:Field name to sort by. If prefixed with -, the sorting will be in descending order, otherwise in ascending order. |
---|---|
Return type: | a new Query instance. |
Search text in model. A search engine needs to be installed for this function to be available.
Parameters: | text – a string to search. |
---|---|
Return type: | a new Query instance. |
For backend supporting scripting, it is possible to construct complex queries which execute the scripting code against each element in the query. The coe should reference an instance of model by this keyword.
Parameters: |
|
---|---|
Returns: | a new Query |
Return a new QueryElem for q applying a text search.
It returns a new Query that automatically follows the foreign-key relationship related.
Parameters: |
|
---|
This function is performance boost when accessing the related fields of all (most) objects in your query.
If Your model contains more than one foreign key, you can use this function in a generative way:
qs = myquery.load_related('rel1').load_related('rel2','field1','field2')
Return type: | a new Query. |
---|
This is provides a performance boost in cases when you need to load a subset of fields of your model. The boost achieved is less than the one obtained when using Query.load_related(), since it does not reduce the number of requests to the database. However, it can save you lots of bandwidth when excluding data intensive fields you don’t need.
Works like load_only() to provides a performance boost in cases when you need to load all fields except a subset specified by fields.
Return an instance of a model matching the query. A special case is the query on id which provides a direct access to the session instances. If the given primary key is present in the session, the object is returned directly without performing any query.
Return the number of objects in self. This method is efficient since the Query does not receive any data from the server apart from the number of matched elements. It construct the queries and count the objects on the server side.
Build the QueryElement representing this query.
Build and return the stdnet.utils.async.BackendQuery. This is a lazy method in the sense that it is evaluated once only and its result stored for future retrieval.
Test if a given field fieldname has a unique value in model. The field must be an index of the model. If the field value is not unique and the instance is not the same an exception is raised.
Parameters: | |
---|---|
Returns: | value |
Perform a map/reduce operation on this query.
Aggregate lookup parameters.
An element of a Query.
the Query which contains this QueryElement.
the element contained in the QueryElement. This underlying is an iterable or another Query.
if False this QueryElement has no underlying elements and won’t produce any query.
Stdnet search engine driver. This is an abstract class which expose the base functionalities for full text-search on model instances. Stdnet also provides a python implementation of this interface.
The main methods to be implemented are add_item(), remove_index() and search_model().
A list of middleware functions for preprocessing text to be indexed. A middleware function has arity 1 by accepting an iterable of words and returning an iterable of words. Word middleware functions are added to the search engine via the add_word_middleware() method.
For example this function remove a group of words from the index:
se = SearchEngine()
class stopwords(object):
def __init__(self, *swords):
self.swords = set(swords)
def __call__(self, words):
for word in words:
if word not in self.swords:
yield word
se.add_word_middleware(stopwords('and','or','this','that',...))
Maximum number of instances to be reindexed in one session. Default 1000.
Backend for this search engine.
Register a StdModel with this search SearchEngine. When registering a model, every time an instance is created, it will be indexed by the search engine.
Parameters: |
|
---|
Generator of indexable words in text. This functions loop through the word_middleware attribute to process the text.
Parameters: |
|
---|
return a list of cleaned words.
Split text into words and return an iterable over them. Can and should be reimplemented by subclasses.
Add a middleware function to the list of word_middleware, for preprocessing words to be indexed.
Parameters: |
|
---|
This is the main function for indexing items. It extracts content from the given item and add it to the index.
Parameters: | item – an instance of a stdnet.odm.StdModel. |
---|
Return a query for model when it needs to be indexed.
Create a session for the search engine
Main method for indexing items.
It extracts content from a list of items belonging to model and add it to the index.
Parameters: |
|
---|
Remove an item from the search indices
Create indices for item and each word in words. Must be implemented by subclasses.
Parameters: |
|
---|
Full text search. Must be implemented by subclasses.
Parameters: |
|
---|
Search text in model instances.
This is the functions needing implementation by custom search engines.
Parameters: | |
---|---|
Returns: | An updated Query. |
Clean the search engine
Re-index models.
Remove existing indices indexes and rebuilding them by iterating through all the instances of REGISTERED_MODELS.
Data structures are subclasses of Structure, which in term is a subclass of Model. They are rarely used in stand alone mode, instead they form the back-end of data structure fields.
There are five of them:
An additional structure is provided in the stdnet.apps.columnts module
- stdnet.apps.columnts.ColumnTS a numeric multivariate time-series structure (useful for modelling financial data for example).
Note
Stand alone data structures are available for redis back-end only. Usually, one uses these models via a data-structure fields.
Creating the five structures available in stdnet is accomplished in the following way:
from stdnet import odm
models = odm.Router('redis://localhost:6379')
li = models.register(odm.List())
At this point the li instance is registered with a Router and the session API can be used:
with models.session().begin() as t:
t.add(li)
li.push_back('bla')
li.push_back('foo')
If no id is specified, stdnet will create one for you:
>>> l.id
'2d0cbac9'
A Model for remote data-structures.
Remote structures are the backend of structured fields but they can also be used as stand alone objects. For example:
import stdnet
db = stdnet.getdb(...)
mylist = db.list('bla')
A python StructureCache for this Structure. The cache is used when adding or removing data via Transaction as well as for storing the results obtained form a call to items().
Class used for serialize and unserialize values. If None the stdnet.utils.encoders.NumericDefault will be used.
Default None.
The StructureField which this owns this Structure. Default None.
Returns the stdnet.BackendStructure.
Returns the stdnet.BackendStructure.
Load data from the stdnet.BackendDataServer.
Returns the stdnet.BackendStructure.
Returns the stdnet.BackendStructure for reading.
Mixin for a Structure which implements a kind of sequence container. The elements in a sequence container are ordered following a linear sequence.
A mixin for structures with which holds pairs. It is the parent class of KeyValueMixin and it is used as base class for the ordered set structure Zset.
An encoder for the additional value in the pair. The additional value is a field key for Hashtable, a numeric score for Zset and a tim value for TS.
Add a pair to the structure.
Add mapping dictionary to hashtable. Equivalent to python dictionary update method.
Parameters: | mapping – a dictionary of field values. |
---|
A mixin for ordered and unordered key-valued pair containers. A key-value pair container has the values() and items() methods, while its iterator is over keys.
Retrieve a single element from the structure. If the element is not available return the default value.
Parameters: |
|
---|
Remove keys from the key-value container.
A mixin for a Structure which maintains ordering with respect a numeric value, the score.
Return the front pair of the structure
Return the back pair of the structure
Count the number of elements bewteen start and stop.
Return a range with scores between start and end.
Return the range by rank between start and end.
pop a range by score from the OrderedMixin
pop a range from the OrderedMixin
Interface for all Structure.cache classes.
Clear the cache for data
A doubly-linked list Structure. It expands the Sequence mixin with functionalities to add and remove from the front of the list in an efficient manner.
Remove the first element from of the list.
Remove the last element from of the list. If no elements are available, blocks for at least timeout seconds.
Remove the first element from of the list. If no elements are available, blocks for at least timeout seconds.
Appends a copy of value to the beginning of the list.
An unordered set Structure. Equivalent to a python set.
Add value to the set
Add iterable values to the set
Remove an element value from a set if it is a member.
Remove an element value from a set if it is a member.
Remove an iterable of values from the set.
An ordered version of Set. It derives from OrderedMixin and PairMixin.
The rank of a given value. This is the position of value in the OrderedMixin container.
A Structure which is the networked equivalent to a Python dict. Derives from KeyValueMixin.
A timeseries is a Structure which derives from OrderedMixin and KeyValueMixin. It represents an ordered associative container where keys are timestamps and values are objects.
The rank of a given dte in the timeseries
The times between times start and stop.
The times between rank start and stop.
A Session is the middleware between a Manager and a stdnet.BackendDataServer. It is obtained from either the Router.session() or, equivalently, from the Manager.session(). A Session is an holding zone for SessionModel and it communicates with the stdnet.BackendDataServer via Transaction.
The middleware for persistent operations on the back-end.
It is created via the Mapper.session() method.
A Transaction instance. Not None if this Session is in a transactional state
Begin a new Transaction. If this Session is already in a transactional state, an error will occur. It returns the transaction attribute.
This method is mostly used within a with statement block:
with session.begin() as t:
t.add(...)
...
which is equivalent to:
t = session.begin()
t.add(...)
...
session.commit()
options parameters are passed to the Transaction constructor.
Commit the current transaction.
If no transaction is in progress, this method open one. Rarely used directly, see the begin() method for details on how to start and close a transaction using the with construct.
Update or create a new instance of model.
This method can raise an exception if the kwargs dictionary contains field data that does not validate.
Parameters: |
|
---|---|
Returns: | A two elements tuple containing the instance and a boolean indicating if the instance was created or not. |
Add an instance to the session.
If the session is not in a transactional state, this operation commits changes to the back-end server immediately.
Parameters: | |
---|---|
Returns: | the instance. |
If the instance is persistent (it is already stored in the database), an updated will be performed, otherwise a new entry will be created once the commit() method is invoked.
Delete an instance or a query.
Adds instance_or_query to this Session list of data to be deleted. If the session is not in a transactional state, this operation commits changes to the backend server immediately.
Parameters: | instance_or_query – a Model instance or a Query. |
---|
Completely flush a Model from the database. No keys associated with the model will exists after this operation.
Remove empty keys for a Model from the database. No empty keys associated with the model will exists after this operation.
Retrieve all keys for a model.
Returns the SessionModel for model which can be Model, or a MetaClass, or an instance of Model.
A SessionModel is the container of all objects for a given Model in a stdnet Session.
The backend for this SessionModel.
The read-only backend for this SessionModel.
The Model for this SessionModel.
The set of all new instances within this SessionModel. This instances will be inserted in the database.
The set of all modified instances within this Session. This instances will.
The set of all instances which have changed, but not deleted, within this SessionModel.
Ordered iterator over dirty elements.
Add a new instance to this SessionModel.
Parameters: |
|
---|---|
Return type: | The instance added to the session |
delete an instance
Remove instance from the SessionModel. Instance could be a Model or an id.
Parameters: | instance – a Model or an id. |
---|---|
Return type: | the Model removed from session or None if it was not in the session. |
Remove instance from the Session. Instance could be a Model or an id.
Parameters: | instance – a Model or an id |
---|---|
Return type: | the Model removed from session or None if it was not in the session. |
Process results after a commit.
Parameters: | results – iterator over stdnet.instance_session_result items. |
---|---|
Return type: | a two elements tuple containing a list of instances saved and a list of ids of instances deleted. |
Completely flush model from the database. No keys associated with the model will exists after this operation.
Remove empty keys for a model from the database. No empty keys associated with the model will exists after this operation.
Retrieve all keys for a model. Uses the Manager.read_backend.
Transaction class for pipelining commands to the backend server. An instance of this class is usually obtained via the Session.begin() or the Manager.transaction() methods:
t = session.begin()
or using the with context manager:
with session.begin() as t:
...
ATTRIBUTES
Optional Transaction name
the stdnet.BackendDataServer to which the transaction is being performed.
If True, a signal for each model in the transaction is triggered just after changes are committed. The signal carries a list of updated instances of the model, the Session and the Transaction itself.
default True.
If True a signal for each model in the transaction is triggered just after deletion of instances. The signal carries the list ids of deleted instances of the mdoel, the Session and the Transaction itself.
default True.
Dictionary of list of ids deleted from the backend server after a commit operation. This dictionary is only available once the transaction has finished.
Dictionary of list of ids saved in the backend server after a commit operation. This dictionary is only available once the transaction has finished.
True when this transaction has been executed.
A transaction can be executed once only via the commit() method. An executed transaction if finished once a response from the backend server has been processed.
A convenience proxy for Session.add() method.
A convenience proxy for Session.delete() method.
A convenience proxy for Session.expunge() method.
A convenience proxy for Session.query() method.
A convenience proxy for Session.model() method.
Close the transaction and commit session to the backend.
Before a StdModel can be used in conjunction with a backend server, a Manager must be associated with it via a Mapper. Check the registration tutorial for further info:
class MyModel(odm.StdModel):
group = odm.SymbolField()
flag = odm.BooleanField()
models = odm.Mapper()
models.register(MyModel)
manager = models[MyModel]
Managers are used as Session and Query factories for a given StdModel:
session = router[MyModel].session()
query = router[MyModel].query()
A model can specify a custom manager by creating a Manager subclass with additional methods:
class MyModelManager(odm.Manager):
def special_query(self, **kwargs):
...
At this point we need to tell the model about the custom manager, and we do so by setting the manager_class attribute in the StdModel:
class MyModel(odm.StdModel):
...
manager_class = MyModelManager
The StdModel for this Manager. This attribute is assigned by the Object data mapper at runtime.
The stdnet.BackendDataServer for this Manager.
A stdnet.BackendDataServer for read-only operations (Queries).
Create a new instance of model and commit it to the backend server. This a shortcut method for the more verbose:
instance = manager.session().add(MyModel(**kwargs))
Save an existing instance of model. This a shortcut method for the more verbose:
instance = manager.session().add(instance)
Invokes the Session.update_or_create method.
Return all instances for this manager. Equivalent to:
self.query().all()
A method which can implement table creation. For sql models. Does nothing for redis or mongo.
Returns a new Query for Manager.model.
Returns an empty Query for Manager.model.
Returns a new Query for Manager.model with a filter.
Returns a new Query for Manager.model with a exclude filter.
Returns a new Query for Manager.model with a full text search value.
Shortcut for self.query().get**kwargs).
Return the primary key value for instance.
Base class for descriptors used by ForeignKey and StructureField.
The Field which create this descriptor. Either a ForeignKey or a StructureField.
Load the lazy data for this descriptor. Implemented by subclasses.
To interact with a stdnet.BackendDataServer, Models must registered. Registration is obtained via a Router which has two methods for registering models. The first one is the Router.register() method which is used to register a model and, possibly, all its related models. The second method is the Router.register_applications() which registers all Model from a list of python dotted paths or python modules.
Check the registering models tutorial for further explanation and examples.