:py:mod:`pyloggrid.Libs.misc` ============================= .. py:module:: pyloggrid.Libs.misc .. autoapi-nested-parse:: Misc small libraries that help with various tasks, too small to fit in their own library file Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: pyloggrid.Libs.misc.TimeTracker Functions ~~~~~~~~~ .. autoapisummary:: pyloggrid.Libs.misc.composed_decs pyloggrid.Libs.misc.bytes2human pyloggrid.Libs.misc.rightpad_array_2D .. py:function:: composed_decs(*decs) Compose several decorators into one. .. rubric:: Example :: @composed(dec_1, dec_2) def f(...) is equivalent to :: @dec_1 @dec_2 def f(...) .. py:function:: 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 .. rubric:: 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' .. py: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): ... .. py:method:: __call__(key: str) .. py:method:: __enter__() .. py:method:: __exit__(*args) .. py:method:: get(key: str) -> float Get elapsed time for ``key``. .. py:method:: start_timer(key: str) -> None Start timer for ``key`` .. py:method:: end_timer(key: str) -> float Stop timer for ``key>``, store and return total elapsed time. .. py:method:: tick_timer(key: str) -> None Store elapsed time and reset timer. .. py:method:: __repr__() Return repr(self). .. py:function:: 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). :param a: array to pad :param Nx: amount of pixels to pad in first dimension :param Ny: amount of pixels to pad in second dimension