vcorelib.math package#

Subpackages#

Submodules#

vcorelib.math.constants module#

A module defining some useful constants.

vcorelib.math.constants.from_nanos(nanos: int) float[source]#

Convert a number in nano counts to a regular floating-point number.

vcorelib.math.constants.to_nanos(value: float) int[source]#

Convert a number to the same number but in nano counts.

vcorelib.math.time module#

Math utilities involving units of time and other conversions.

class vcorelib.math.time.Timer[source]#

Bases: object

A class for measuring and logging how long events take.

log(log: Logger | LoggerAdapter[Any], message: str, *args, level: int = 20, reminder: bool = False, **kwargs) Iterator[None][source]#

Log how long the caller’s context took to execute.

measure_ns() Iterator[int][source]#

Compute the time that the caller’s context takes, provides an integer token that can be used to query for the result afterwards.

result(token: int) int[source]#

Get the timer result.

vcorelib.math.time.byte_count_str(byte_count: int) str[source]#

Get a string representing a number of bytes.

vcorelib.math.time.default_time_ns() int[source]#

Get a timestamp value using a default method.

vcorelib.math.time.nano_str(nanos: int, is_time: bool = False, max_prefix: int = 3, unit: UnitSystem = (['n', 'u', 'm', '', 'k', 'M', 'G', 'T'], 1000), prefix_space: bool = False, iteration: int = 0) str[source]#

Convert an arbitrary value in a 10^-9 domain into as concise of a string as possible.

vcorelib.math.time.rate_str(period_s: float) str[source]#

Get a string representing a rate in Hz.

vcorelib.math.time.seconds_str(seconds: int) Tuple[str, int][source]#

Attempt to characterize a large amount of seconds into a string describing hours and minutes, returning the string (may be empty) and the seconds left over.

vcorelib.math.unit module#

A module for working with units.

class vcorelib.math.unit.UnitSystem(prefixes: Sequence[str], divisor: int)[source]#

Bases: NamedTuple

A pairing of prefixes defining a unit, and the amount that indicates the multiplicative step-size between them.

divisor: int#

Alias for field number 1

prefixes: Sequence[str]#

Alias for field number 0

vcorelib.math.unit.unit_traverse(val: int, unit: UnitSystem = (['n', 'u', 'm', '', 'k', 'M', 'G', 'T'], 1000), max_prefix: int = 3, iteration: int = 0) Tuple[int, int, str][source]#

Given an initial value, traverse a unit system to get the largest representative unit prefix. Also return a fractional component, in units of the next-smallest prefix.

Module contents#

Math utilities.

class vcorelib.math.FloatBuffer(depth: int = 10, initial: float = 0.0)[source]#

Bases: object

A class implementing a simple circular buffer for floats.

reset(initial: float = 0.0) None[source]#

Reset the buffer.

resize(depth: int, initial: float = 0.0) None[source]#

Set a new depth for this buffer average and reset the values.

class vcorelib.math.MovingAverage(depth: int = 10, initial: float = 0.0)[source]#

Bases: object

A class for managing a moving average of floats.

reset(initial: float = 0.0) None[source]#

Reset the average value.

resize(depth: int, initial: float = 0.0) None[source]#

Set a new depth for this moving average and reset the value.

class vcorelib.math.MovingSum(depth: int = 10, initial: float = 0.0)[source]#

Bases: FloatBuffer

A simple moving sum implementation.

reset(initial: float = 0.0) None[source]#

Reset the buffer and sum.

class vcorelib.math.RateLimiter(period_ns: int, **kwargs)[source]#

Bases: object

A class for limiting the rate of runtime work.

property rate_hz: float#

Get the underlying rate that this limiter is governing. This is useful to determine if the rate limitation is or isn’t impacting the rate of some task.

class vcorelib.math.RateTracker(**kwargs)[source]#

Bases: object

A class for managing rate information for some data channel.

measure(value: float = 1.0, timer: ~vcorelib.math.time.Timer = <vcorelib.math.time.Timer object>) Iterator[None][source]#

Track the time that the caller’s context takes.

poll(time_ns: int = None) float[source]#

Siphon accumulated time and update rate tracking.

reset() None[source]#

Reset this rate tracker.

property value: float#

An accessor for the underlying value.

with_dt(delta_s: float, value: float = 1.0) None[source]#

Update this rate by directly providing the delta-time value.

class vcorelib.math.Timer[source]#

Bases: object

A class for measuring and logging how long events take.

log(log: Logger | LoggerAdapter[Any], message: str, *args, level: int = 20, reminder: bool = False, **kwargs) Iterator[None][source]#

Log how long the caller’s context took to execute.

measure_ns() Iterator[int][source]#

Compute the time that the caller’s context takes, provides an integer token that can be used to query for the result afterwards.

result(token: int) int[source]#

Get the timer result.

class vcorelib.math.UnitSystem(prefixes: Sequence[str], divisor: int)[source]#

Bases: NamedTuple

A pairing of prefixes defining a unit, and the amount that indicates the multiplicative step-size between them.

divisor: int#

Alias for field number 1

prefixes: Sequence[str]#

Alias for field number 0

class vcorelib.math.WeightedAverage(depth: int = 10)[source]#

Bases: object

A class implementing a weighted average.

average() float[source]#

Compute the overall weighted average.

property depth: int#

This average’s depth.

reset() None[source]#

Reset the average.

vcorelib.math.byte_count_str(byte_count: int) str[source]#

Get a string representing a number of bytes.

vcorelib.math.default_time_ns() int[source]#

Get a timestamp value using a default method.

vcorelib.math.from_nanos(nanos: int) float[source]#

Convert a number in nano counts to a regular floating-point number.

vcorelib.math.nano_str(nanos: int, is_time: bool = False, max_prefix: int = 3, unit: UnitSystem = (['n', 'u', 'm', '', 'k', 'M', 'G', 'T'], 1000), prefix_space: bool = False, iteration: int = 0) str[source]#

Convert an arbitrary value in a 10^-9 domain into as concise of a string as possible.

vcorelib.math.rate_str(period_s: float) str[source]#

Get a string representing a rate in Hz.

vcorelib.math.seconds_str(seconds: int) Tuple[str, int][source]#

Attempt to characterize a large amount of seconds into a string describing hours and minutes, returning the string (may be empty) and the seconds left over.

vcorelib.math.to_nanos(value: float) int[source]#

Convert a number to the same number but in nano counts.

vcorelib.math.unit_traverse(val: int, unit: UnitSystem = (['n', 'u', 'm', '', 'k', 'M', 'G', 'T'], 1000), max_prefix: int = 3, iteration: int = 0) Tuple[int, int, str][source]#

Given an initial value, traverse a unit system to get the largest representative unit prefix. Also return a fractional component, in units of the next-smallest prefix.