File: helper.py

package info (click to toggle)
igor2 0.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: python: 2,636; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pathlib
import numpy as _numpy
from pprint import PrettyPrinter
from igor2.binarywave import load as loadibw

data_dir = pathlib.Path(__file__).parent / "data"


class NumpyPrettyPrinter(PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, _numpy.ndarray):
            # Generate the entire array string using numpy's own mechanism but
            # ensuring dtype is displayed
            array_repr = _numpy.array2string(object, separator=', ')
            # Append dtype only once for the whole array
            formatted_array = f"array({array_repr}, dtype='{object.dtype}')"
            return (formatted_array, True, False)
        # Default handling for other types
        return super().format(object, context, maxlevels, level)


def custom_pformat(object, indent=1, width=80, depth=None, *, compact=False):
    printer = NumpyPrettyPrinter(
        indent=indent, width=width, depth=depth, compact=compact)
    return printer.pformat(object)


def assert_equal_dump_no_whitespace_no_byte(data_a, data_b):
    def repl(x):
        for old, new in [
            [" ", ""],  # ignore whitespaces
            ["b'", "'"],  # ignore bytes vs str
            ["\n", ""],  # ignore newlines
            # treat all floats as equal
            ["float32", "float"],
            ["float64", "float"],
            ["'>f4'", "float"],
            ["'>f8'", "float"],
            ["'float'", "float"],
        ]:
            x = x.replace(old, new)
        return x

    a = repl(data_a)
    b = repl(data_b)
    print("DBUG data_a: ", a)
    print("DBUG data_b: ", b)
    assert a == b


def dumpibw(filename):
    path = data_dir / filename
    data = loadibw(path)
    return format_data(data)


def format_data(data):
    lines = custom_pformat(data).splitlines()
    return '\n'.join([line.rstrip() for line in lines])


def walk_callback(dirpath, key, value):
    return 'walk callback on ({}, {}, {})'.format(
        dirpath, key, '{...}' if isinstance(value, dict) else value)