Skip to content

utils

messageflux.utils

AggregatedException

AggregatedException(*args, inner_exceptions=None, **kwargs)

Bases: KwargsException

an exception that can hold multiple inner exceptions

ContextDidNotEndException

ContextDidNotEndException(*args, **kwargs)

Bases: KwargsException

this exception is raised when elapsed seconds is called on a context that was not run

KwargsException

KwargsException(*args, **kwargs)

Bases: Exception

the basic exception type we use. adds the ability to add KWARGS to exception

ObservableEvent

ObservableEvent()

Bases: Generic[TEventType]

an event that can be subscribed to and fired.

fire

fire(event, continue_after_failure=True)

fires the event

Parameters:

Name Type Description Default
event TEventType

the event to fire

required
continue_after_failure bool

True means to call all the callbacks even if one fails. False means to stop if one of the callbacks fails

True

Returns:

Type Description
bool

True if all the callbacks succeeded, False otherwise

subscribe

subscribe(handler)

subscribes a callback to the event

Parameters:

Name Type Description Default
handler Callable[[TEventType], None]

the callback to subscribe

required

unsubscribe

unsubscribe(handler)

unsubscribes a callback to the event

Parameters:

Name Type Description Default
handler Callable[[TEventType], None]

the callback to unsubscribe

required

Returns:

Type Description
bool

True if the callback existed and unsubscribed. False if the callback didn't exist

StatefulListIterator

StatefulListIterator(items)

Bases: Collection[TItemType]

An list, that can be iterated from the last stopped location, in a circular fashion. every iteration starts from the last position iterated position, and iterates over all the list

ThreadLocalMember

ThreadLocalMember(init_value=...)

Bases: Generic[TValueType]

this is a descriptor, for making thread local members for class:

class MyClass: x = ThreadLocalMember() y = ThreadLocalMember(1) z = ThreadLocalMember() w = ThreadLocalMember(1) def init(): self.x = 5 self.w = 2

a = MyClass(): a.x # 5 a.x = 7 # changes only for this thread a.y # 1 - the default init value for this a.z # raises AttributeError (z was not set on class). once it's set, then value is the init value for each thread a.w # 2 for this thread, 1 for any other thread (because of the default init value)

ThreadLocalValue

ThreadLocalValue(init_value=None)

Bases: local, Generic[TValueType]

this class holds a single, thread-local value

x = ThreadLocalValue(5) x.value # 5 x.value = 7 # changes only for this thread

TimerContext

TimerContext()

a context object that records how much time did the context last

elapsed_seconds property

elapsed_seconds

returns the elapsed time for this context (between enter and exit)

Returns:

Type Description

the elapsed time (in seconds) for the context. if enter or exit weren't called, a ContextDidNotEndException exception is raised

get_random_id

get_random_id()

generates a random 16 bytes hex string

json_safe_encoder

json_safe_encoder(obj)

Convert non-serializable types to strings.

Parameters:

Name Type Description Default
obj Any

The object to make serializable.

required

Returns:

Type Description

Serializable version of the object (string-form).