Backends

Datastore

class bread.backends.cloud_datastore.CloudDatastoreBackend[source]

Bases: bread.backend.Backend

Google Cloud Datastore backend wrapper implementation.

CURSOR_SEP = '-*^v^*-'
FAKE_STOP_CURSOR = 'STOP'
begin()[source]

Create a new BackendTransaction instance specific to the current backend.

delete(**kwargs)[source]

Delete a set of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
delete_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of delete().

Returns:A Future representing the backend computation.
fetch(**kwargs)[source]

Query entities.

Note that this implementation is unable to support the next parameter due to an issue where Cloud Datastore ALWAYS returns that there are more results after the limit set in the query, even when there are none. The gcloud-python library then conservatively changes that respose to indicate there are NEVER more results.

https://github.com/GoogleCloudPlatform/gcloud-python/issues/280

fetch_async(of_type, query, order, limit, cursor, dataset=None, collection=None)[source]

Asynchronous variant of fetch().

Warning:Calling this method on queries with multiple in filters may cause a deadlock.
Returns:A future representing the result tuple.
get(**kwargs)[source]

Get a list of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list of Entities.

get_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of get().

Returns:A Future representing a list of Entities.
save(**kwargs)[source]

Save a set of Entities by their Keys.

Note:

This method will add fields with index=False as a keyword argument to the index exclusion list.

Parameters:
  • keys (list) –
  • data (list) –
  • meta (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list representing the saved Entities.

save_async(keys, data, meta=None, dataset=None, collection=None)[source]

Asynchronous variant of save().

Returns:A Future representing a list of the saved Entities.
class bread.backends.cloud_datastore.CloudDatastoreTransaction(active_backend)[source]

Bases: bread.backend.BackendTransaction

Google Cloud Datastore transaction wrapper implementation.

commit()[source]
delete(key)[source]
rollback()[source]
save(key, serialized, meta=None)[source]

Memcache

class bread.backends.memcache.MemcacheBackend(backend, pool)[source]

Bases: bread.backend.Backend

A higher-order backend that adds implicit caching of get{,_async} operations on top of any given Backend.

The backend caches all get or get_async calls according to its Policy. Entities’ individual caches are invalidated every time they are saved or deleted.

The backend _does not_ attempt to cache queries in any way. All calls to fetch and fetch_asnyc are pass through directly to the underlying backend.

Parameters:
backend (Backend) pool (pylibmc.ThreadMappedPool)
begin()[source]

Create a new BackendTransaction instance specific to the current backend.

delete(keys, dataset=None, collection=None)[source]

Delete a set of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
delete_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of delete().

Returns:A Future representing the async computation.
get(keys, dataset=None, collection=None)[source]

Get a list of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list of Entities.

get_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of get().

Returns:A Future representing a list of Entities.
policy_class
reauthorize()[source]

Reauthorize the underlying connection for this backend. The default implementation is a no-op.

save(keys, data, meta=None, dataset=None, collection=None)[source]

Save a list of entities by Key.

Parameters:
  • keys (list) –
  • data (list) –
  • meta (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list representing the saved Entities.

save_async(keys, data, meta=None, dataset=None, collection=None)[source]

Asynchronous variant of save().

Returns:A Future representing a list of the saved Entities.
class bread.backends.memcache.MemcachePolicy[source]

Bases: bread.backend.Policy, bread.backends.memcache.Policy

Runtime policy for the Memcache Backend.

Parameters:
skip_cache (bool): If this is True, the cache is skipped
when retrieving data (i.e. in calls to get{,_async} and fetch{,_async}). Saving data will still hit the cache in order to invalidate said data.
entity_ttl (int): The amount of seconds that entities should be
cached for. This is an upper bound, entities may end up being cached for less time.
Examples:
>>> backend.set_default_policy(MemcachePolicy.build(entity_ttl=60))
>>> backend.policy
MemcachePolicy(skip_cache=False, entity_ttl=60)
>>> with backend.local_policy(skip_cache=True):
...   AModel.get("some-id")
classmethod build(skip_cache=False, entity_ttl=86400)[source]
class bread.backends.memcache.MemcacheTransaction(memcache_backend)[source]

Bases: bread.backend.BackendTransaction

commit()[source]
delete(key)[source]
rollback()[source]
save(key, serialized, meta=None)[source]

Stub

class bread.backends.stub.Entry(uuid, data)

Bases: tuple

data

Alias for field number 1

uuid

Alias for field number 0

class bread.backends.stub.StubBackend[source]

Bases: bread.backend.Backend

An in-memory backend.

The primary use-case for this backend is unit testing.

OP_TABLE = {'!=': <function ne>, '<': <function lt>, '<=': <function le>, '=': <function eq>, '>': <function gt>, '>=': <function ge>, 'in': <function contains>}
ORDER_OP_TABLE = {'asc': <function <lambda>>, 'desc': <function <lambda>>}
begin()[source]

Create a new BackendTransaction instance specific to the current backend.

delete(keys, dataset=None, collection=None)[source]

Delete a set of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
delete_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of delete().

Returns:A Future representing the async computation.
fetch(of_type, query, order, limit, cursor, dataset=None, collection=None)[source]

Query entities.

Backends must support queries where the query is a list of tuples, and where each tuple is a trio of (property, op, value), for example, [("n", ">", 10)] or [("n", ">", 10), ("x", "=", "alpha")].

Valid op values are >=, <, >, <=, =, != and in.

Backends must support sort orders where the sort order is a list of tuples, and where each tuple is a pair of (property, direction), for example: [("n", "desc")].

Valid direction values are asc and desc.

The backend must construct and return Key instances for each returned entity. Each serialized entity will be expected to have a dictionary index key with a Key instance as the value. This is required so that we do not have to know which underlying field the backend considers the “ID”.

Parameters:
  • of_type (str) – Entity type to query for.
  • query (tuple) – List of tuples representing a query.
  • order (tuple) – List of tuples representing a sorting order.
  • limit (int) – Positive integer reresenting the maximum number of entities to return.
  • cursor (str) – Opaque value representing a position in the query result set.
Returns:

Tuple of (entities, cursor, more) where entities is a list of entities in their serialized form that they were saved as, cursor is an opaque string representing a position in the query result set, and more is a boolean representing whether or not a subsequent query, passing in the returned cursor, would produce more results.

fetch_async(of_type, query, order, limit, cursor, dataset=None, collection=None)[source]

Asynchronous variant of fetch().

Returns:A future representing the result tuple.
get(keys, dataset=None, collection=None)[source]

Get a list of Entities by their Keys.

Parameters:
  • keys (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list of Entities.

get_async(keys, dataset=None, collection=None)[source]

Asynchronous variant of get().

Returns:A Future representing a list of Entities.
policy_class
save(keys, data, meta=None, dataset=None, collection=None)[source]

Save a list of entities by Key.

Parameters:
  • keys (list) –
  • data (list) –
  • meta (list) –
  • dataset (str) –
  • collection (str) –
Returns:

A list representing the saved Entities.

save_async(keys, data, meta=None, dataset=None, collection=None)[source]

Asynchronous variant of save().

Returns:A Future representing a list of the saved Entities.
class bread.backends.stub.StubPolicy[source]

Bases: bread.backend.Policy, bread.backends.stub.StubPolicy

classmethod build(**kwargs)[source]
class bread.backends.stub.StubTransaction(backend)[source]

Bases: bread.backend.BackendTransaction

commit()[source]
delete(key)[source]
rollback()[source]
save(key, serialized, meta=None)[source]
bread.backends.stub.contains(x, xs)[source]

A version of contains that has the same semantics as datastore. Non-iterable values are checked for inclusion in xs, iterables validate that at least one of their items is in xs.

bread.backends.stub.eq(p, v)[source]

Datastore equality on lists is actually a “contains” operation so it needs to be special-cased.

bread.backends.stub.ge(value, filter_value)[source]
bread.backends.stub.gt(value, filter_value)[source]
bread.backends.stub.le(value, filter_value)[source]
bread.backends.stub.lt(value, filter_value)[source]
bread.backends.stub.ne(p, v)[source]

The inverse of eq.

bread.backends.stub.sort_key(prop)[source]

Module contents