|
14 | 14 | import typing as ty
|
15 | 15 | import warnings
|
16 | 16 | from functools import reduce
|
17 |
| -from operator import mul |
| 17 | +from operator import getitem, mul |
18 | 18 | from os.path import exists, splitext
|
19 | 19 |
|
20 | 20 | import numpy as np
|
|
26 | 26 |
|
27 | 27 | pyzstd, HAVE_ZSTD, _ = optional_package('pyzstd')
|
28 | 28 |
|
| 29 | +if ty.TYPE_CHECKING: # pragma: no cover |
| 30 | + K = ty.TypeVar('K') |
| 31 | + V = ty.TypeVar('V') |
| 32 | + |
29 | 33 | sys_is_le = sys.byteorder == 'little'
|
30 | 34 | native_code = sys_is_le and '<' or '>'
|
31 | 35 | swapped_code = sys_is_le and '>' or '<'
|
@@ -283,7 +287,10 @@ def __getitem__(self, key: ty.Hashable) -> ty.Hashable:
|
283 | 287 | raise KeyError(key)
|
284 | 288 |
|
285 | 289 |
|
286 |
| -def pretty_mapping(mapping, getterfunc=None): |
| 290 | +def pretty_mapping( |
| 291 | + mapping: ty.Mapping[K, V], |
| 292 | + getterfunc: ty.Callable[[ty.Mapping[K, V], K], V] | None = None, |
| 293 | +) -> str: |
287 | 294 | """Make pretty string from mapping
|
288 | 295 |
|
289 | 296 | Adjusts text column to print values on basis of longest key.
|
@@ -332,9 +339,8 @@ def pretty_mapping(mapping, getterfunc=None):
|
332 | 339 | longer_field : method string
|
333 | 340 | """
|
334 | 341 | if getterfunc is None:
|
335 |
| - getterfunc = lambda obj, key: obj[key] |
336 |
| - lens = [len(str(name)) for name in mapping] |
337 |
| - mxlen = np.max(lens) |
| 342 | + getterfunc = getitem |
| 343 | + mxlen = max(len(str(name)) for name in mapping) |
338 | 344 | fmt = '%%-%ds : %%s' % mxlen
|
339 | 345 | out = []
|
340 | 346 | for name in mapping:
|
|
0 commit comments