lazy_misc Module

Common miscellanous tools and constants for general use

Summary of module contents:

Name Description
DEFAULT_SAMPLE_RATE int(x=0) -> integer int(x, base=10) -> integer
rint Round to integer.
blocks General iterable blockenizer.
zero_pad Zero padding sample generator (not a Stream!).
elementwise Function auto-map decorator broadcaster.
almost_eq This is a StrategyDict instance object called almost_eq. Strategies stored: 2.
sHz Unit conversion constants.
freq2lag Converts from frequency (rad/sample) to lag (number of samples).
lag2freq Converts from lag (number of samples) to frequency (rad/sample).
freq_to_lag Deprecated. Converts from frequency (rad/sample) to lag (number of samples).
lag_to_freq Deprecated. Converts from frequency (rad/sample) to lag (number of samples).
cached Cache decorator for a function without keyword arguments
rint(x, step=1)[source]

Round to integer.

Parameters:
  • x – Input number (integer or float) to be rounded.
  • step – Quantization level (defaults to 1). If set to 2, the output will be the “best” even number.

The step multiple nearest to x. When x is exactly halfway between two possible outputs, it’ll result the one farthest to zero.

blocks(seq, size=None, hop=None, padval=0.0)[source]

General iterable blockenizer.

Generator that gets size elements from seq, and outputs them in a collections.deque (mutable circular queue) sequence container. Next output starts hop elements after the first element in last output block. Last block may be appended with padval, if needed to get the desired size.

The seq can have hybrid / hetherogeneous data, it just need to be an iterable. You can use other type content as padval (e.g. None) to help segregate the padding at the end, if desired.

Note

When hop is less than size, changing the returned contents will keep the new changed value in the next yielded container.

zero_pad(seq, left=0, right=0, zero=0.0)[source]

Zero padding sample generator (not a Stream!).

Parameters:
  • seq – Sequence to be padded.
  • left – Integer with the number of elements to be padded at left (before). Defaults to zero.
  • right – Integer with the number of elements to be padded at right (after). Defaults to zero.
  • zero – Element to be padded. Defaults to a float zero (0.0).
Returns:

A generator that pads the given seq with samples equals to zero, left times before and right times after it.

elementwise(name='', pos=None)[source]

Function auto-map decorator broadcaster.

Creates an “elementwise” decorator for one input parameter. To create such, it should know the name (for use as a keyword argument and the position “pos” (input as a positional argument). Without a name, only the positional argument will be used. Without both name and position, the first positional argument will be used.

sHz(rate)[source]

Unit conversion constants.

Useful for casting to/from the default package units (number of samples for time and rad/second for frequency). You can use expressions like 440 * Hz to get a frequency value, or assign like kHz = 1e3 * Hz to get other unit, as you wish.

Parameters:rate – Sample rate in samples per second
Returns:A tuple (s, Hz), where s is the second unit and Hz is the hertz unit, as the number of samples and radians per sample, respectively.
freq2lag(v)[source]

Converts from frequency (rad/sample) to lag (number of samples).

lag2freq(v)[source]

Converts from lag (number of samples) to frequency (rad/sample).

freq_to_lag(v)

Deprecated. Converts from frequency (rad/sample) to lag (number of samples).

lag_to_freq(v)

Deprecated. Converts from frequency (rad/sample) to lag (number of samples).

cached(func)[source]

Cache decorator for a function without keyword arguments

You can access the cache contents using the cache attribute in the resulting function, which is a dictionary mapping the arguments tuple to the previously returned function result.

lazy_misc.almost_eq StrategyDict

This is a StrategyDict instance object called almost_eq. Strategies stored: 2.

Strategy almost_eq.bits (Default). Docstring starts with:

Almost equal, based on the amount of floating point significand bits.

Strategy almost_eq.diff. Docstring starts with:

Almost equal, based on the \(|a - b|\) value.

Note

This docstring is self-generated, see the StrategyDict class and the strategies docs for more details.

diff(a, b, max_diff=1e-07, ignore_type=True, pad=0.0)

Almost equal, based on the \(|a - b|\) value.

Alternative to “a == b” for float numbers and iterables with float numbers. See almost_eq for more information.

This version based on the non-normalized absolute diff, similar to what unittest does with its assertAlmostEquals. If a and b sizes differ, at least one will be padded with the pad input value to keep going with the comparison.

Note

Be careful with endless generators!

bits(a, b, bits=32, tol=1, ignore_type=True, pad=0.0)

Almost equal, based on the amount of floating point significand bits.

Alternative to “a == b” for float numbers and iterables with float numbers, and tests for sequence contents (i.e., an elementwise a == b, that also works with generators, nested lists, nested generators, etc.). If the type of both the contents and the containers should be tested too, set the ignore_type keyword arg to False. Default version is based on 32 bits IEEE 754 format (23 bits significand). Could use 64 bits (52 bits significand) but needs a native float type with at least that size in bits. If a and b sizes differ, at least one will be padded with the pad input value to keep going with the comparison.

Note

Be careful with endless generators!