smarkets package

Submodules

smarkets.configuration module

class smarkets.configuration.ConfigurationReader(directories, parser_class=<class ConfigParser.SafeConfigParser>)

Bases: object

Reads a configuration from a series of “inheriting” .ini files. You may specify what config file your file inherits from like this:

[inherit]
from=other.conf

Moreover files of the same name may be present in multiple directories ConfigurationReader is set to look for config files - in this case it will read configuration from all of them but in reverse order. For example, let’s have:

  • B.conf inherits from ``A.conf’‘

  • files present:

    • /etc/conf/B.conf
    • /home/conf/A.conf
    • /home/conf/B.conf
  • reader configured like this:

    reader = ConfigurationReader(('/etc/conf', '/home/conf'))
    

Order in which files will be read:

  • /home/conf/A.conf
  • /home/conf/B.conf
  • /etc/conf/B.conf
read(filename)

Reads the configuration into new instance of parser_class. :rtype: ConfigParser

read_into(filename, config)

Reads the configuration into config object. :type config: ConfigParser

smarkets.errors module

exception smarkets.errors.Error

Bases: exceptions.Exception

Base class for every Smarkets error

smarkets.errors.swallow(exceptions, default=None)

Swallow exception(s) when executing something. Works as function decorator and as a context manager:

>>> @swallow(NameError, default=2)
... def fun():
...     a = b  # noqa
...     return 1
...
>>> fun()
2
>>> with swallow(KeyError):
...    raise KeyError('key')
...
Parameters:default – value to return in case of an exception

smarkets.functools module

smarkets.functools.overrides(ancestor_class)

Mark method as overriding ancestor_class’ method.

Note

Overriding method can not have its own docstring.

Note

Method being overridden must be (re)defined in ancestor_class itself (see BadChild3 example below); overrides() will not follow the inheritance tree.

Usage:

>>> class Parent(object):
...     def method(self):
...         'parent docstring'
...
>>>
>>> class BadChild1(Parent):
...     @overrides(Parent)
...     def methd(self):
...         pass
... 
...
Traceback (most recent call last):
OverrideError: No method 'methd' in class <class 'smarkets.functools.Parent'> to override
>>>
>>> class BadChild2(Parent):
...     @overrides(Parent)
...     def method(self):
...         'child method docstring'
... 
...
Traceback (most recent call last):
OverrideError: No docstrings allowed in overriding method
>>>
>>> class IntermediateChild(Parent):
...     pass
...
>>> class BadChild3(IntermediateChild):
...     @overrides(IntermediateChild)
...     def method(self):
...         pass
... 
...
Traceback (most recent call last):
OverrideError: No method 'method' in class <class 'smarkets.functools.IntermediateChild'> to override
>>>
>>> class GoodChild(Parent):
...     @overrides(Parent)
...     def method(self):
...         return 1
...
>>> child = GoodChild()
>>> str(child.method.__doc__)
'parent docstring'
>>> child.method()
1
Raises:
OverrideError:Method does not exist in parent class or overriding method has docstring.
exception smarkets.functools.OverrideError

Bases: exceptions.Exception

Method override fails

smarkets.functools.lru_cache(maxsize=100, typed=False)

Least-recently-used cache decorator.

If maxsize is set to None, the LRU features are disabled and the cache can grow without bound.

If typed is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results.

Arguments to the cached function must be hashable.

View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.__wrapped__.

See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

smarkets.itertools module

smarkets.itertools.listitems(d)

Return d item list

smarkets.itertools.listkeys(d)

Return d key list

smarkets.itertools.listvalues(d)

Return d value list

smarkets.itertools.merge_dicts(*dicts)

Return dicts merged together.

If keys clash the ubsequent dictionaries have priority over preceding ones.

>>> merge_dicts() == {}
True
>>> merge_dicts({'a': 2}) == {'a': 2}
True
>>> merge_dicts({'a': 2, 'b': 3}, {'a': 1, 'c': 4}) == {'a': 1, 'b': 3, 'c': 4}
True
smarkets.itertools.listmap()

map(function, sequence[, sequence, …]) -> list

Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).

smarkets.itertools.inverse_mapping(d)

Return a dictionary with input mapping keys as values and values as keys.

Raises:
ValueError:Input mapping values aren’t uniqe.
smarkets.itertools.is_sorted(sequence, **kwargs)
Parameters:kwargssorted() kwargs
smarkets.itertools.copy_keys_if_present(source, destination, keys)

Copy keys from source mapping to destination mapping while skipping nonexistent keys.

smarkets.itertools.listmap()

map(function, sequence[, sequence, …]) -> list

Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).

smarkets.signal module

class smarkets.signal.Signal

Bases: object

All instance methods of this class are thread safe.

add(handler)

Add signal handler. You can also do:

signal = Signal()
signal += handler
fire(**kwargs)

Execute all handlers associated with this Signal.

You can also call signal object to get the same result:

signal = Signal()
signal()  # calls the signal handler
handle(handler)

Add signal handler. You can also do:

signal = Signal()
signal += handler
remove(handler)

Remove signal handler. You can also do:

signal = Signal()
# add a handler "handler"
signal -= handler

smarkets.uuid module

class smarkets.uuid.Uuid

Bases: smarkets.uuid.UuidBase

Represents a UUID

static base_n(number, chars)

Recursive helper for calculating a number in base len(chars)

chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
classmethod from_hex(hex_str)

Convert a hex uuid into a Uuid :type hex_str: byte string or unicode string

classmethod from_int(number, ttype)

Convert an integer and tag type to a Uuid

classmethod from_slug(slug, base=36, chars=None)

Convert a slug into a Uuid

high

Higher 64 bits of number

low

Lower 64 bits of number

mask64 = 18446744073709551615L
static pad_uuid(uuid, pad=32, padchar='0')

Pads a UUID with <pad> <padchar>s

shorthex

Short hex representation of Uuid

tags = {'Account': UuidTagBase(name='Account', int_tag=44225, prefix='a'), 'Comment': UuidTagBase(name='Comment', int_tag=45476, prefix='b'), 'Contract': UuidTagBase(name='Contract', int_tag=52428, prefix='c'), 'ContractGroup': UuidTagBase(name='ContractGroup', int_tag=49188, prefix='m'), 'Entity': UuidTagBase(name='Entity', int_tag=1092, prefix='n'), 'Event': UuidTagBase(name='Event', int_tag=4352, prefix='e'), 'Order': UuidTagBase(name='Order', int_tag=65520, prefix='o'), 'Referrer': UuidTagBase(name='Referrer', int_tag=20046, prefix='r'), 'Session': UuidTagBase(name='Session', int_tag=39321, prefix='s'), 'User': UuidTagBase(name='User', int_tag=3840, prefix='u')}
tags_by_hex_str = {'0444': UuidTagBase(name='Entity', int_tag=1092, prefix='n'), '0f00': UuidTagBase(name='User', int_tag=3840, prefix='u'), '1100': UuidTagBase(name='Event', int_tag=4352, prefix='e'), '4e4e': UuidTagBase(name='Referrer', int_tag=20046, prefix='r'), '9999': UuidTagBase(name='Session', int_tag=39321, prefix='s'), 'acc1': UuidTagBase(name='Account', int_tag=44225, prefix='a'), 'b1a4': UuidTagBase(name='Comment', int_tag=45476, prefix='b'), 'c024': UuidTagBase(name='ContractGroup', int_tag=49188, prefix='m'), 'cccc': UuidTagBase(name='Contract', int_tag=52428, prefix='c'), 'fff0': UuidTagBase(name='Order', int_tag=65520, prefix='o')}
tags_by_int_tag = {1092: UuidTagBase(name='Entity', int_tag=1092, prefix='n'), 3840: UuidTagBase(name='User', int_tag=3840, prefix='u'), 4352: UuidTagBase(name='Event', int_tag=4352, prefix='e'), 20046: UuidTagBase(name='Referrer', int_tag=20046, prefix='r'), 39321: UuidTagBase(name='Session', int_tag=39321, prefix='s'), 44225: UuidTagBase(name='Account', int_tag=44225, prefix='a'), 45476: UuidTagBase(name='Comment', int_tag=45476, prefix='b'), 49188: UuidTagBase(name='ContractGroup', int_tag=49188, prefix='m'), 52428: UuidTagBase(name='Contract', int_tag=52428, prefix='c'), 65520: UuidTagBase(name='Order', int_tag=65520, prefix='o')}
tags_by_prefix = {'a': UuidTagBase(name='Account', int_tag=44225, prefix='a'), 'b': UuidTagBase(name='Comment', int_tag=45476, prefix='b'), 'c': UuidTagBase(name='Contract', int_tag=52428, prefix='c'), 'e': UuidTagBase(name='Event', int_tag=4352, prefix='e'), 'm': UuidTagBase(name='ContractGroup', int_tag=49188, prefix='m'), 'n': UuidTagBase(name='Entity', int_tag=1092, prefix='n'), 'o': UuidTagBase(name='Order', int_tag=65520, prefix='o'), 'r': UuidTagBase(name='Referrer', int_tag=20046, prefix='r'), 's': UuidTagBase(name='Session', int_tag=39321, prefix='s'), 'u': UuidTagBase(name='User', int_tag=3840, prefix='u')}
to_hex(pad=32)

Convert to tagged hex representation

to_slug(prefix=True, base=36, chars=None, pad=0)

Convert to slug representation

classmethod unsplit64(high, low)

Converts a high/low 64-bit integer pair into a 128-bit large integer

class smarkets.uuid.UuidBase(number, tag)

Bases: tuple

number

Alias for field number 0

tag

Alias for field number 1

class smarkets.uuid.UuidTag

Bases: smarkets.uuid.UuidTagBase

Represents tag information

hex_str

Hex tag value

classmethod split_int_tag(number)

Splits a number into the ID and tag

tag_mult = 65536
tag_number(number)

Adds this tag to a number

class smarkets.uuid.UuidTagBase(name, int_tag, prefix)

Bases: tuple

int_tag

Alias for field number 1

name

Alias for field number 0

prefix

Alias for field number 2

smarkets.uuid.int_to_slug(number, ttype)

Convert a large integer to a slug

smarkets.uuid.int_to_uuid(number, ttype)

Convert an untagged integer into a tagged uuid

smarkets.uuid.slug_to_int(slug, return_tag=None, split=False)

Convert a slug to an integer, optionally splitting into high and low 64 bit parts

smarkets.uuid.slug_to_uuid(slug)

Convert a slug to a Smarkets UUID

smarkets.uuid.uuid_to_int(uuid, return_tag=None, split=False)

Convert a tagged uuid into an integer, optionally returning type

smarkets.uuid.uuid_to_short(uuid)

Converts a full UUID to the shortened version

smarkets.uuid.uuid_to_slug(number, prefix=True)

Convert a Smarkets UUID (128-bit hex) to a slug

Module contents