typedparser 0.22.18__tar.gz → 0.22.20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.22.18
3
+ Version: 0.22.20
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
@@ -25,4 +25,4 @@ __all__ = [
25
25
  ]
26
26
 
27
27
 
28
- __version__ = "0.22.18"
28
+ __version__ = "0.22.20"
@@ -4,17 +4,16 @@ Generic utilities for python objects
4
4
 
5
5
  from __future__ import annotations
6
6
 
7
- from collections import defaultdict
8
-
9
7
  import inspect
10
8
  from abc import ABCMeta, abstractmethod
11
- from attr import has, AttrsInstance
12
- from attrs import fields
9
+ from collections import defaultdict
13
10
  from collections.abc import Mapping, Iterable
14
11
  from copy import deepcopy
15
12
  from functools import partial
16
13
  from typing import Type, List, Any, Callable
17
14
 
15
+ from attr import has, AttrsInstance
16
+ from attrs import fields
18
17
 
19
18
  AttrsClass = Type[AttrsInstance]
20
19
 
@@ -479,3 +478,73 @@ def get_obj_str_with_max_len(obj: Any, max_len: int = 100):
479
478
  else obj_str
480
479
  )
481
480
  return obj_str
481
+
482
+
483
+ def _get_indent(depth, key, fmt=" "):
484
+ indent = fmt * depth
485
+ if key is not None:
486
+ indent = f"{indent}{key}: "
487
+ return indent
488
+
489
+
490
+ def repr_value(
491
+ value, max_list_items=3, max_dict_items=8, max_str_len=200, depth=0, key=None, fmt=" "
492
+ ):
493
+ repr_value_recursor = partial(
494
+ repr_value,
495
+ max_list_items=max_list_items,
496
+ max_dict_items=max_dict_items,
497
+ max_str_len=max_str_len,
498
+ depth=depth + 1,
499
+ fmt=fmt,
500
+ )
501
+ typ = type(value).__name__
502
+ indent = _get_indent(depth, key, fmt=fmt)
503
+ indent_plus_1 = _get_indent(depth + 1, None, fmt=fmt)
504
+ indent_minus_1 = _get_indent(depth - 1, None, fmt=fmt)
505
+ if isinstance(value, str):
506
+ value_str = value
507
+ if len(value) > max_str_len > 0:
508
+ value_str = "".join(
509
+ [value[: max_str_len // 2], " ... ", value[-max_str_len // 2 - 5 :]]
510
+ )
511
+ return f"{indent}{typ} len={len(value)}: {value_str}"
512
+
513
+ if isinstance(value, bytes):
514
+ value_str = value
515
+ if len(value) > max_str_len > 0:
516
+ value_str = b"".join([value[:100], b" ... ", value[-95:]])
517
+ return f"{indent}{typ} len={len(value)}: {value_str}"
518
+
519
+ # if isinstance(value, torch.Tensor):
520
+ if hasattr(value, "shape") and hasattr(value, "dtype"):
521
+ return f"{indent}{typ} shape={value.shape} dtype={value.dtype}"
522
+
523
+ if is_any_mapping(value):
524
+ keys = list(value.keys())
525
+ show_len = len(keys)
526
+ if max_dict_items > 0:
527
+ show_len = min(max_dict_items, len(keys))
528
+ reprs = [f"{indent_minus_1}{repr_value_recursor(value[k],key=k)}" for k in keys[:show_len]]
529
+ if show_len < len(keys):
530
+ reprs.append("...")
531
+ out_str = f"{indent}{typ} len={len(keys)}\n" + "\n".join(reprs)
532
+ return out_str
533
+
534
+ # note: handle all iterables that should not get recursed into, above this line.
535
+ if is_any_iterable(value):
536
+ show_len = len(value)
537
+ if max_list_items > 0:
538
+ show_len = min(max_list_items, len(value))
539
+ reprs = [repr_value_recursor(v) for v in value[:show_len]]
540
+ if show_len < len(value):
541
+ reprs.append(f"{indent_plus_1}...")
542
+ out_str = f"{indent}{typ} len={len(value)}\n" + "\n".join(reprs)
543
+ return out_str
544
+
545
+ return f"{indent}{typ}: {str(value)}"
546
+
547
+ # if value is None:
548
+ # return f"{indent}{typ}: None"
549
+ #
550
+ # raise NotImplementedError(f"repr_value not implemented for {typ}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.22.18
3
+ Version: 0.22.20
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
File without changes
File without changes
File without changes