Welcome to Basilisk’s documentation!¶
Contents:
Basilisk enables Pythonic use of Redis hashes, lists and sorted sets with simple class interface as well as provides an ORM Model-like class using Redis hash or Elasticsearch inside.
A simple example:
>>> import basilisk
>>>
>>> basilisk.Config.load(redis={'host': 'localhost',
>>> 'port': 6379,
>>> 'db': 0,
>>> 'max_connections': 10},
>>> elastic={})
>>>
>>>
>>> class Item(basilisk.RedisModel):
>>> id = basilisk.MapField(key=True)
>>> name = basilisk.MapField()
>>> content = basilisk.MapField()
>>> attachments = basilisk.JsonMapField()
>>>
>>> @classmethod
>>> def select(cls):
>>> redis_items = basilisk.RedisList('items')
>>> variables = basilisk.RedisHash('items_variables')
>>> last_modified = int(variables['last_modified'] or 0)
>>>
>>> if (not len(redis_items) or
>>> not last_modified or
>>> last_modified + 30 < time.time()):
>>> items = DownloadNewItemsFromDatabase(last_modified)
>>> for item in items:
>>> redis_items.append(item.id)
>>> variables['last_modified'] = int(time.time())
>>> variables.save()
>>> return list(redis_items)
>>>
>>> items = Item.select()
>>> for item in items:
>>> print(Item.get(item).content)
-
class
basilisk.
Config
[source]¶ This class keeps appropriate config data.
It enables access as a class property and as an item (like in dict).
-
class
basilisk.
MapField
(**kwargs)[source]¶ This is a base class for all NoSQL store fields. It supports data-based initialisation, default values and prepping values to serialization.
-
get_default
()[source]¶ It returns field’s default value. Surprise surprise.
Returns: field’s default value.
-
pythonize
(data)[source]¶ This function pythonizes data coming from NoSQL store.
Parameters: data – data fetched from NoSQL store. Returns: data in Python format.
-
-
class
basilisk.
JsonMapField
(**kwargs)[source]¶ This class enables keeping JSON as field value.
-
class
basilisk.
MapModelBase
(**kwargs)[source]¶ This is the base class for NoSQL store models.
This class enables reading object with given id, saving object and data (de)serialization.
Redis connection is available in connect property. Dict of fields is available in _fields property.
Reserved property names, apart from methods, are _fields, id_field and connect.
-
exception
MapModelException
¶ Exception raised when errors related to NoSQL store handling are encountered.
-
classmethod
MapModelBase.
get
(oid)[source]¶ This method gets a model instance with given id from NoSQL store.
Parameters: oid – id of object to get. Returns: hydrated model instance.
-
classmethod
MapModelBase.
get_fields
()[source]¶ This function returns the dict of model fields.
Returns: dict containing name: field pairs.
-
MapModelBase.
get_instance_key
()[source]¶ This function returns a key in which the instance will live in NoSQL store.
Returns: Redis key name containing instance hash
-
classmethod
MapModelBase.
get_key
(oid)[source]¶ This function creates a key in which NoSQL store will save the instance with given id.
Parameters: oid – id of object for which a key should be created. Returns: NoSQL store key.
-
classmethod
MapModelBase.
pythonize
(data, loads=False)[source]¶ This method prepares the data fetched from NoSQL store for new instance.
Parameters: data – values to convert. Returns: dict of values ready to pass to __init__.
-
MapModelBase.
save
(create_id=True)[source]¶ Let’s save instance’s current state to NoSQL store.
Parameters: create_id – whether id should be created automatically if it’s not set yet. Returns: self
-
exception
-
class
basilisk.
ElasticsearchModel
(**kwargs)[source]¶ This is the base class for Elasticsearch models. Internally they are just a Elasticsearch entity. This class enables reading object with given id, saving object and data (de)serialization.
Elasticsearch connection is available in connect property. Dict of fields is available in _fields property.
Reserved property names, apart from methods, are _fields, id_field and connect.
-
exception
ElasticsearchModelException
¶ Exception raised when errors related to Redis handling are encountered.
-
ElasticsearchModel.
MapModelException
¶ alias of
ElasticsearchModelException
-
classmethod
ElasticsearchModel.
get
(oid)[source]¶ This method gets a model instance with given id from Elasticsearch.
Parameters: oid – id of object to get. Returns: hydrated model instance.
-
exception
-
class
basilisk.
ElasticsearchModelException
[source]¶ Exception raised when errors related to Redis handling are encountered.
-
class
basilisk.
RedisModel
(**kwargs)[source]¶ This is the base class for Redis models. Internally they are just a Redis hash. This class enables reading object with given id, saving object and data (de)serialization. Redis connection is available in connect property. Dict of fields is available in _fields property.
Reserved property names, apart from methods, are _fields, id_field and connect.
-
MapModelException
¶ alias of
RedisModelException
-
exception
RedisModelException
¶ Exception raised when errors related to Redis handling are encountered.
-
classmethod
RedisModel.
get
(oid)[source]¶ This method gets a model instance with given id from Redis.
Parameters: oid – id of object to get. Returns: hydrated model instance.
-
-
class
basilisk.
RedisList
(name, namespace=None)[source]¶ This class is a proxy for Redis List. It enables instant modifications to Redis entity. It has only basic operations pythonized at the moment.
-
append
(item)[source]¶ Adds element at the end of the list.
Parameters: item – element to be appended.
-
get_instance_key
()[source]¶ This function creates Redis’s instance key.
Returns: key in which instance will be saved.
-
classmethod
get_key
(name)[source]¶ This method creates a Redis key in which instance with given name will be saved.
Parameters: name – name of object for which a key is to be made. Returns: key used in Redis for given name.
-
pop
(first=False)[source]¶ Gets and removes an element from list’s edge. By default it’s the last element.
Parameters: first – Should the first element be popped instead of the last.
-
-
class
basilisk.
RedisHash
(name, namespace=None)[source]¶ This class acts as a proxy for Redis Hash. It enables delayed modifications. __setitem__ and __delitem__ methods don’t modify Redis immediately, but are instead queued in a changelist.
-
get
(*fields)[source]¶ This gets one or many items from Redis.
Parameters: fields – list of fields to get.
-
get_instance_key
()[source]¶ This function creates Redis’s instance key.
Returns: key in which instance will be saved.
-
classmethod
get_key
(name)[source]¶ This method creates a Redis key in which instance with given name will be saved.
Parameters: name – name of object for which a key is to be made. Returns: key used in Redis for given name.
-
-
class
basilisk.
RedisSortedSet
(name, namespace=None)[source]¶ This class is used to proxy Redis’s Sorted Sets. It allows value search with pagination and delayed (lazy) key alterations. Indexing works with SCORE, not MEMBER or RANK.
set_score and delete_item methods don’t interface with Redis directly, but are queued in a change list.
-
delete_item
(item)[source]¶ This method deletes given element. You need to call save() to propagate changes to Redis.
Parameters: item – element to be removed.
-
get_instance_key
()[source]¶ This function creates Redis’s instance key.
Returns: key in which instance will be saved.
-
classmethod
get_key
(name)[source]¶ This method creates a Redis key in which instance with given name will be saved.
Parameters: name – name of object for which a key is to be made. Returns: key used in Redis for given name.
-
highest
()[source]¶ Returns element with highest SCORE and its SCORE.
Returns: element with highest SCORE and its SCORE.
-
lowest
()[source]¶ Returns element with lowest SCORE and its SCORE.
Returns: element with lowest SCORE and its SCORE.
-