mock-alchemy
A module for utils for the mock-alchemy library.
mock_alchemy.utils.
build_identity_map
Builds identity map.
Utility for building identity map from given SQLAlchemy models.
items (Sequence[Any]) – A sequence of SQLAlchemy objects.
Sequence
Any
Dict
An identity map of the given SQLAlchemy objects.
For example:
>>> from sqlalchemy import Column, Integer, String >>> from sqlalchemy.ext.declarative import declarative_base >>> Base = declarative_base() >>> class SomeClass(Base): ... __tablename__ = 'some_table' ... pk1 = Column(Integer, primary_key=True) ... pk2 = Column(Integer, primary_key=True) ... name = Column(String(50)) ... def __repr__(self): ... return str(self.pk1) >>> build_identity_map([SomeClass(pk1=1, pk2=2)]) {(1, 2): 1}
copy_and_update
Copy and update dictionary.
Copy dictionary and update it all in one operation.
target (Dict) – The dictionary to copy and update.
updater (Dict) – The updating dictionary.
A new dictionary of the target copied and updated by updater.
target
updater
>>> a = {'foo': 'bar'} >>> b = copy_and_update(a, {1: 2}) >>> a is b False >>> b == {'foo': 'bar', 1: 2} True
get_item_attr
Access dictionary in different methods.
Utility for accessing dict by different key types (for get).
idmap (Dict) – A dictionary of identity map of SQLAlchemy objects.
access (Union[Dict, Tuple, Any]) – The access pattern which should either be basic data type, dictionary, or a tuple. If it is dictionary it should map to the names of the primary keys of the SQLAlchemy objects. If it is a tuple, it should be a set of keys to search for. If it is not a dict or a tuple, then the objects in question must have only one primary key of the type passed (such as a string, integer, etc.).
Union
Tuple
An SQlAlchemy object that was requested.
>>> idmap = {(1,): 2} >>> get_item_attr(idmap, 1) 2 >>> idmap = {(1,): 2} >>> get_item_attr(idmap, {"pk": 1}) 2 >>> get_item_attr(idmap, (1,)) 2
indexof
Gets the index of some item in a sequence.
Find an index of needle in haystack by looking for exact same item by pointer ids vs usual list.index() which finds by object comparison.
needle
haystack
list.index()
needle (Any) – The object or item to find in the sequence.
haystack (Sequence[Any]) – The sequence of items to search for the needle.
int
The index of the needle in the haystack.
ValueError – If the needle is not found inside the haystack.
>>> a = {} >>> b = {} >>> haystack = [1, a, 2, b] >>> indexof(b, haystack) 3 >>> indexof(None, haystack) Traceback (most recent call last): ... ValueError: None is not in [1, {}, 2, {}]
match_type
Match the string type.
Matches the string type with the provided type and returns the string of the desired type.
s (Union[bytes, str]) – The string to match the type with.
bytes
str
t (Union[Type[bytes], Type[str]]) – The type to make the string with.
Type
Union[bytes, str]
An object of the desired type of type t.
t
>>> assert type(match_type(b'hello', bytes)) is bytes >>> assert type(match_type(u'hello', str)) is str >>> assert type(match_type(b'hello', str)) is str >>> assert type(match_type(u'hello', bytes)) is bytes
raiser
Raises an exception with the given args.
Utility for raising exceptions Useful in one-liners.
exp (Type[Exception]) – The exception to raise.
Exception
args (Any) – The args to use for the exception.
kwargs (Any) – The kwargs to use for the exception.
exp – The parameterized exception of the specified kind.
>>> a = lambda x: not x and raiser(ValueError, 'error message') >>> _ = a(True) >>> _ = a(False) Traceback (most recent call last): ... ValueError: error message
Type[Exception]
setattr_tmp
Set the atrributes of object temporarily.
Utility for temporarily setting value in an object.
obj (object) – An object to set the attribute of.
object
name (str) – The name of the attribute.
value (Any) – The value to set the attribute to.
A context manager that can be used.
Used for the context manager so that this function can be used as with setattr_tmp.
with setattr_tmp
>>> class Foo(object): ... foo = 'foo' >>> print(Foo.foo) foo >>> with setattr_tmp(Foo, 'foo', 'bar'): ... print(Foo.foo) bar >>> print(Foo.foo) foo >>> Foo.foo = None >>> with setattr_tmp(Foo, 'foo', 'bar'): ... print(Foo.foo) None