firmant.utils

Utilities that don’t fit anywhere else.

Modules in this package:

exceptions
paths
workarounds

Functions

firmant.utils.class_name(cls)

The string representation of a class’s name.

For example if we define the class Foo, the full name of the class is firmant.utils.Foo. class_name() will return this as a string.

>>> class Foo(object): pass
...
>>> class_name(Foo)
'firmant.utils.Foo'

If an object that is not a class is passed to class_name(), then a TypeError will be raised.

>>> class_name('Foo')
Traceback (most recent call last):
TypeError: `cls` does not name a type.
firmant.utils.get_obj(path)

Get an object by its Python import path.

>>> get_obj('os.path.join') 
<function join at 0x...>
firmant.utils.merge_dicts(a, *args)

Merge two dictionaries in a manner that doesn’t lose information.

One or two dictionaries with disjoint sets of keys will merge with the same rules as the update() function for dictionaries.

>>> pprint(merge_dicts({'a': 1}))
{'a': 1}
>>> pprint(merge_dicts({'a': 1}, {'b': 2}))
{'a': 1, 'b': 2}

If two keys have the same value, the merge happens cleanly.

>>> pprint(merge_dicts({'a': 1, 'b': 2}, {'b': 2}))
{'a': 1, 'b': 2}

An arbitrary number of dictionaries may be merged using merge_dicts().

>>> pprint(merge_dicts({'a': 1, 'b': 2}, {'b': 2}, {'c':3}))
{'a': 1, 'b': 2, 'c': 3}

If two instances have the same key, but different values, a ValueError will be raised.

>>> pprint(merge_dicts({'a': 1, 'b': 2}, {'b': 3}))
Traceback (most recent call last):
ValueError: Conflicting values for 'b'
firmant.utils.strptime(date_string, formats)

Cast the date_string to the first format to match.

Each format string provided by formats is considered. The first format to match date_string will be used to determine the datetime.

>>> strptime('2009-02-01 11:51:15', ['%Y-%m-%d %H:%M:%S'])
datetime.datetime(2009, 2, 1, 11, 51, 15)
>>> strptime('11:51:15', ['%Y-%m-%d', '%H:%M:%S'])
datetime.datetime(1900, 1, 1, 11, 51, 15)

If the time does not match, a value error will be raised.

>>> strptime('AB:CD:EF', ['%H:%M:%S'])
Traceback (most recent call last):
ValueError: time data 'AB:CD:EF' does not match any format.

Table Of Contents

Previous topic

firmant.settings

Next topic

firmant.utils.exceptions

This Page