swpt_lib.utils

class swpt_lib.utils.Int64Converter(map)

Flask URL converter for signed 64-bit integers.

The converter can be registered with the Flask app like this:

from flask import Flask
from swpt_lib.utils import Int64Converter

app = Flask(__name__)
app.url_map.converters['i64'] = Int64Converter
class swpt_lib.utils.Seqnum(value: int)

A signed 32-bit integer seqnum value.

Comparisions beteen Seqnum instances correctly deal with the possible 32-bit integer wrapping.

increment() → swpt_lib.utils.Seqnum

Return an incremented instance.

swpt_lib.utils.date_to_int24(d: datetime.date) → int

Return a non-negative 24-bit integer derived from a date.

The passed date must not be before January 1st, 1970. The returned integer equals the number of days passed since January 1st, 1970.

swpt_lib.utils.get_config_value(key: str) → Optional[str]

Get the value for the configuration variable with a name key.

The returned value is either a string or None. If there is a Flask application context, the app’s config will be checked first. If that fails, the environment will be checked next. If that fails too, None will be returned.

swpt_lib.utils.i64_to_u64(value: int) → int

Convert a signed 64-bit integer to unsigned 64-bit integer.

Raises ValueError if the value is not in the range of signed 64-bit integers.

swpt_lib.utils.increment_seqnum(n: int) → int

Increment a 32-bit signed integer with wrapping.

swpt_lib.utils.is_later_event(event: Tuple[datetime.datetime, int], other_event: Tuple[Optional[datetime.datetime], Optional[int]]) → bool

Return whether event is later than other_event.

Each of the passed events must be a (datetime, int) tuple. The datetime must be the event timestamp, and the int must be the event sequential number (32-bit signed integer, with eventual wrapping).

An event with a noticeably later timestamp (>= 1s) is always considered later than an event with an earlier timestamp. Only when the two timestamps are very close (< 1s), the sequential numbers of the events are compared. When the timestamp of other_event is None, event is considered as a later event.

Note that sequential numbers are compared with possible 32-bit signed integer wrapping in mind. For example, compared to 2147483647, -21474836478 is considered a later sequential number.

swpt_lib.utils.u64_to_i64(value: int) → int

Convert an unsigned 64-bit integer to a signed 64-bit integer.

Raises ValueError if the value is not in the range of unsigned 64-bit integers.