pyloggrid.Libs.misc

Misc small libraries that help with various tasks, too small to fit in their own library file

Module Contents

Classes

TimeTracker

Keep track of elapsed time. Supports nesting.

Functions

composed_decs(*decs)

Compose several decorators into one.

bytes2human(n[, format_, symbols])

Convert n bytes into a human readable string based on format.

rightpad_array_2D(...)

Pad a 2D+ array with zeros.

composed_decs(*decs)

Compose several decorators into one.

Example

@composed(dec_1, dec_2)
def f(...)

is equivalent to

@dec_1
@dec_2
def f(...)
bytes2human(n, format_='%(value).1f %(symbol)s', symbols='customary')

Convert n bytes into a human readable string based on format. symbols can be either “customary”, “customary_ext”, “iec” or “iec_ext”, see: https://goo.gl/kTQMs

Example

> bytes2human(0)
'0.0 B'
> bytes2human(0.9)
'0.0 B'
> bytes2human(1)
'1.0 B'
> bytes2human(1.9)
'1.0 B'
> bytes2human(1024)
'1.0 K'
> bytes2human(1048576)
'1.0 M'
> bytes2human(1099511627776127398123789121)
'909.5 Y'

> bytes2human(9856, symbols="customary")
'9.6 K'
> bytes2human(9856, symbols="customary_ext")
'9.6 kilo'
> bytes2human(9856, symbols="iec")
'9.6 Ki'
> bytes2human(9856, symbols="iec_ext")
'9.6 kibi'

> bytes2human(10000, "%(value).1f %(symbol)s/sec")
'9.8 K/sec'

> # precision can be adjusted by playing with %f operator
> bytes2human(10000, format_="%(value).5f %(symbol)s")
'9.76562 K'
class TimeTracker(initdict: dict = None)

Keep track of elapsed time. Supports nesting.

How to use:

  • to start a timer, use timer.start_timer(key) where key is a unique identifier

  • to stop a timer and add the elapsed time to the total for the key, use timer.stop_timer(key)

  • to stop and immediately restart (used for storing the current value) a timer use timer.tick_timer(key)

Wherever possible, prefer wrapping you code in a with block:

with timer(key):
    ...
__call__(key: str)
__enter__()
__exit__(*args)
get(key: str) float

Get elapsed time for key.

start_timer(key: str) None

Start timer for key

end_timer(key: str) float

Stop timer for key>, store and return total elapsed time.

tick_timer(key: str) None

Store elapsed time and reset timer.

__repr__()

Return repr(self).

rightpad_array_2D(a: pyloggrid.Libs.singlethread_numpy.np.ndarray, Nx: int, Ny: int) pyloggrid.Libs.singlethread_numpy.np.ndarray

Pad a 2D+ array with zeros.

The padding is done at the large coordinate edge (it is not centered).

Parameters:
  • a – array to pad

  • Nx – amount of pixels to pad in first dimension

  • Ny – amount of pixels to pad in second dimension