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
-
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
¶
-
property
-
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.
first
(iterable, default=None, key=None)[source]¶ Return first element of iterable that evaluates to
True
, else returnNone
or optional default. Similar toone()
.>>> 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]