titanfe.utils module

a collection of useful helper functions

class titanfe.utils.DictConvertable[source]

Bases: abc.ABC

Mixin to make a dataclass convert from/into a dictionary

dicts_to_dataclasses()[source]

Convert all fields of type dataclass into an dataclass instance of the fields specified dataclass if the current value is of type dict.

classmethod from_dict(_dict)[source]
to_dict()[source]
class titanfe.utils.Flag(*, loop=None)[source]

Bases: asyncio.locks.Event

Extends the asyncio.Event to be a tad more convenient

class titanfe.utils.Timer[source]

Bases: object

a simple Timer using the performance counters from the “time”-module

>>> with Timer() as t:
>>>    # do_something()
>>>    print(t.elapsed)
>>>    # do_something_else()
>>>    print(t.elapsed)
>>>    print(t.elapsed_total)
property elapsed
property elapsed_total
property perf_counter_since_last
property perf_counter_total
property process_time_since_last
property process_time_total
update_last_access()[source]
async titanfe.utils.cancel_tasks(tasks: Sequence[_asyncio.Task], wait_cancelled=True)[source]

Cancel all tasks in sequence

Parameters:
  • tasks (Sequence[asyncio.Task]) – tasks to cancel

  • wait_cancelled (bool) – True (default): wait until all tasks returned / False: well, don’t.

titanfe.utils.create_uid(prefix='')[source]
titanfe.utils.first(iterable, default=None, key=None)[source]

Return first element of iterable that evaluates to True, else return None or optional default. Similar to one().

>>> first([0, False, None, [], (), 42])
42
>>> first([0, False, None, [], ()]) is None
True
>>> first([0, False, None, [], ()], default='ohai')
'ohai'
>>> import re
>>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
>>> m.group(1)
'bc'

The optional key argument specifies a one-argument predicate function like that used for filter(). The key argument, if supplied, should be in keyword form. For example, finding the first even number in an iterable:

>>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
4

By Hynek Schlawack, author of the original standalone module.

titanfe.utils.flatten(iterable)[source]

flatten yields all the elements from iterable while collapsing any nested iterables.

>>> nested = [[1, 2], [[3], [4, 5]]]
>>> list(flatten(nested))
[1, 2, 3, 4, 5]
titanfe.utils.generate_key(secret_key, salt)[source]
titanfe.utils.get_ip_address() → str[source]

try to get the public IPv4 address

titanfe.utils.get_module(location: Union[str, pathlib.Path]) → module[source]

Get the Brick content module

If the Brick content module cannot be found, None is returned.

Returns:

The loaded Brick content module

Return type:

(module or None)

titanfe.utils.iso_utc_time_string()[source]
titanfe.utils.ns_to_ms(ns)[source]
titanfe.utils.pairwise(iterable)[source]
titanfe.utils.time_delta_in_ms(time_ns)[source]