|
1 | | -import csv |
2 | 1 | import enum |
3 | 2 | import gzip |
4 | 3 | import io |
|
7 | 6 | import os.path |
8 | 7 | import pathlib |
9 | 8 | import pickle |
10 | | -import textwrap |
11 | 9 | from typing import ( |
12 | 10 | Sequence, |
13 | 11 | Callable, |
|
18 | 16 | Iterator, |
19 | 17 | Dict, |
20 | 18 | Optional, |
21 | | - NoReturn, |
22 | 19 | IO, |
23 | | - Iterable, |
24 | | - Mapping, |
25 | 20 | Sized, |
26 | 21 | ) |
27 | 22 | from typing import cast |
|
38 | 33 | __all__ = [ |
39 | 34 | "INFINITE_BUFFER_SIZE", |
40 | 35 | "BUILTIN_DIR", |
41 | | - "make_repr", |
42 | | - "FrozenMapping", |
43 | | - "FrozenBunch", |
44 | | - "create_categories_file", |
45 | 36 | "read_mat", |
46 | 37 | "image_buffer_from_array", |
47 | 38 | "SequenceIterator", |
|
62 | 53 | BUILTIN_DIR = pathlib.Path(__file__).parent.parent / "_builtin" |
63 | 54 |
|
64 | 55 |
|
65 | | -def make_repr(name: str, items: Iterable[Tuple[str, Any]]) -> str: |
66 | | - def to_str(sep: str) -> str: |
67 | | - return sep.join([f"{key}={value}" for key, value in items]) |
68 | | - |
69 | | - prefix = f"{name}(" |
70 | | - postfix = ")" |
71 | | - body = to_str(", ") |
72 | | - |
73 | | - line_length = int(os.environ.get("COLUMNS", 80)) |
74 | | - body_too_long = (len(prefix) + len(body) + len(postfix)) > line_length |
75 | | - multiline_body = len(str(body).splitlines()) > 1 |
76 | | - if not (body_too_long or multiline_body): |
77 | | - return prefix + body + postfix |
78 | | - |
79 | | - body = textwrap.indent(to_str(",\n"), " " * 2) |
80 | | - return f"{prefix}\n{body}\n{postfix}" |
81 | | - |
82 | | - |
83 | | -class FrozenMapping(Mapping[K, D]): |
84 | | - def __init__(self, *args: Any, **kwargs: Any) -> None: |
85 | | - data = dict(*args, **kwargs) |
86 | | - self.__dict__["__data__"] = data |
87 | | - self.__dict__["__final_hash__"] = hash(tuple(data.items())) |
88 | | - |
89 | | - def __getitem__(self, item: K) -> D: |
90 | | - return cast(Mapping[K, D], self.__dict__["__data__"])[item] |
91 | | - |
92 | | - def __iter__(self) -> Iterator[K]: |
93 | | - return iter(self.__dict__["__data__"].keys()) |
94 | | - |
95 | | - def __len__(self) -> int: |
96 | | - return len(self.__dict__["__data__"]) |
97 | | - |
98 | | - def __setitem__(self, key: K, value: Any) -> NoReturn: |
99 | | - raise RuntimeError(f"'{type(self).__name__}' object is immutable") |
100 | | - |
101 | | - def __delitem__(self, key: K) -> NoReturn: |
102 | | - raise RuntimeError(f"'{type(self).__name__}' object is immutable") |
103 | | - |
104 | | - def __hash__(self) -> int: |
105 | | - return cast(int, self.__dict__["__final_hash__"]) |
106 | | - |
107 | | - def __eq__(self, other: Any) -> bool: |
108 | | - if not isinstance(other, FrozenMapping): |
109 | | - return NotImplemented |
110 | | - |
111 | | - return hash(self) == hash(other) |
112 | | - |
113 | | - def __repr__(self) -> str: |
114 | | - return repr(self.__dict__["__data__"]) |
115 | | - |
116 | | - |
117 | | -class FrozenBunch(FrozenMapping): |
118 | | - def __getattr__(self, name: str) -> Any: |
119 | | - try: |
120 | | - return self[name] |
121 | | - except KeyError as error: |
122 | | - raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'") from error |
123 | | - |
124 | | - def __setattr__(self, key: Any, value: Any) -> NoReturn: |
125 | | - raise RuntimeError(f"'{type(self).__name__}' object is immutable") |
126 | | - |
127 | | - def __delattr__(self, item: Any) -> NoReturn: |
128 | | - raise RuntimeError(f"'{type(self).__name__}' object is immutable") |
129 | | - |
130 | | - def __repr__(self) -> str: |
131 | | - return make_repr(type(self).__name__, self.items()) |
132 | | - |
133 | | - |
134 | | -def create_categories_file( |
135 | | - root: Union[str, pathlib.Path], name: str, categories: Sequence[Union[str, Sequence[str]]], **fmtparams: Any |
136 | | -) -> None: |
137 | | - with open(pathlib.Path(root) / f"{name}.categories", "w", newline="") as file: |
138 | | - csv.writer(file, **fmtparams).writerows(categories) |
139 | | - |
140 | | - |
141 | 56 | def read_mat(buffer: io.IOBase, **kwargs: Any) -> Any: |
142 | 57 | try: |
143 | 58 | import scipy.io as sio |
|
0 commit comments