utils¶
The utils package contains common functions that are used in more than one layer.
cipher¶
times¶
-
eventsourcing.utils.times.
decimaltimestamp_from_uuid
(value: uuid.UUID) → decimal.Decimal[source]¶ Return a floating point unix timestamp from UUID value.
Parameters: value – Returns: Unix timestamp in seconds, with microsecond precision. Return type: Decimal
-
eventsourcing.utils.times.
timestamp_long_from_uuid
(value: uuid.UUID) → int[source]¶ Returns an integer value representing a unix timestamp in tenths of microseconds.
Parameters: value – Returns: Unix timestamp integer in tenths of microseconds. Return type: int
-
eventsourcing.utils.times.
decimaltimestamp
(t: Optional[float] = None) → decimal.Decimal[source]¶ A UNIX timestamp as a Decimal object (exact number type).
Returns current time when called without args, otherwise converts given floating point number
t
to a Decimal with 9 decimal places.Parameters: t – Floating point UNIX timestamp (“seconds since epoch”). Returns: A Decimal with 6 decimal places, representing the given floating point or the value returned by time.time(). Return type: Decimal
topic¶
-
eventsourcing.utils.topic.
get_topic
(domain_class: type) → str[source]¶ Returns a string describing a class.
- Args:
- domain_class: A class.
- Returns:
- A string describing the class.
-
eventsourcing.utils.topic.
resolve_topic
(topic: str) → Any[source]¶ Return class described by given topic.
- Args:
- topic: A string describing a class.
- Returns:
- A class.
- Raises:
- TopicResolutionError: If there is no such class.
-
eventsourcing.utils.topic.
resolve_attr
(obj: Any, path: str) → Any[source]¶ A recursive version of getattr for navigating dotted paths.
- Args:
- obj: An object for which we want to retrieve a nested attribute. path: A dot separated string containing zero or more attribute names.
- Returns:
- The attribute referred to by obj.a1.a2.a3…
- Raises:
- AttributeError: If there is no such attribute.
transcoding¶
-
eventsourcing.utils.transcoding.
encoderpolicy
(arg=None)[source]¶ Decorator for encoder policy.
Allows default behaviour to be built up from methods registered for different types of things, rather than chain of isinstance() calls in a long if-else block.
-
eventsourcing.utils.transcoding.
decoderpolicy
(arg=None)[source]¶ Decorator for decoder policy.
Allows default behaviour to be built up from methods registered for different named keys, rather than chain of “in dict” queries in a long if-else block.
-
class
eventsourcing.utils.transcoding.
ObjectJSONEncoder
(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶ Bases:
json.encoder.JSONEncoder
-
iterencode
(o, _one_shot=False)[source]¶ Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
-
default
(obj)[source]¶ Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
-
-
class
eventsourcing.utils.transcoding.
ObjectJSONDecoder
(object_hook=None, **kwargs)[source]¶ Bases:
json.decoder.JSONDecoder
-
__init__
(object_hook=None, **kwargs)[source]¶ object_hook
, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the givendict
. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).object_pairs_hook
, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value ofobject_pairs_hook
will be used instead of thedict
. This feature can be used to implement custom decoders. Ifobject_hook
is also defined, theobject_pairs_hook
takes priority.parse_float
, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).parse_int
, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).parse_constant
, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered.If
strict
is false (true is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including'\t'
(tab),'\n'
,'\r'
and'\0'
.
-
random¶
-
eventsourcing.utils.random.
encoded_random_bytes
(num_bytes: int) → str[source]¶ Generates random bytes, encoded as Base64 unicode string.
Parameters: num_bytes – Number of random bytes to generate. Returns: Random bytes of specified length, encoded as Base64 unicode string.
-
eventsourcing.utils.random.
encode_random_bytes
(num_bytes: int) → str¶ Generates random bytes, encoded as Base64 unicode string.
Parameters: num_bytes – Number of random bytes to generate. Returns: Random bytes of specified length, encoded as Base64 unicode string.
-
eventsourcing.utils.random.
random_bytes
(num_bytes: int) → bytes[source]¶ Generates random bytes.
Parameters: num_bytes – Number of random bytes to generate. Returns: Random bytes of specified length. Type: bytes