ae-base 0.3.48__py3-none-any.whl → 0.3.49__py3-none-any.whl

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.
ae/base.py CHANGED
@@ -100,6 +100,9 @@ allowing also ``None`` is an accepted argument value.
100
100
 
101
101
  to extend any class with an intelligent error message handling, add the mixin :class:`ErrorMsgMixin` to it.
102
102
 
103
+ the classes :class:`UnformattedValue` and :class:`GivenFormatter` can be used to format strings with placeholders
104
+ enclosed in curly brackets. the function :func:`format_given` is using them to format templates with placeholders.
105
+
103
106
 
104
107
  generic context manager
105
108
  -----------------------
@@ -144,6 +147,7 @@ import platform
144
147
  import re
145
148
  import shutil
146
149
  import socket
150
+ import string
147
151
  import sys
148
152
  import unicodedata
149
153
  import warnings
@@ -156,7 +160,7 @@ from types import ModuleType
156
160
  from typing import Any, Callable, Dict, Generator, Iterable, List, Optional, Tuple, Union, cast
157
161
 
158
162
 
159
- __version__ = '0.3.48'
163
+ __version__ = '0.3.49'
160
164
 
161
165
 
162
166
  os_path_abspath = os.path.abspath
@@ -469,6 +473,50 @@ def force_encoding(text: Union[str, bytes], encoding: str = DEF_ENCODING, errors
469
473
  return enc_str.decode(encoding=encoding)
470
474
 
471
475
 
476
+ class UnformattedValue:
477
+ """ helper class for :func:`~ae.base.format_given` to keep placeholder with format unchanged if not found. """
478
+ def __init__(self, key: str):
479
+ self.key = key
480
+
481
+ def __format__(self, format_spec: str):
482
+ """ overriding Python object class method to return placeholder unchanged including the curly brackets. """
483
+ return "{{{}{}}}".format(self.key, ":" + format_spec if format_spec else "")
484
+
485
+
486
+ class GivenFormatter(string.Formatter):
487
+ """ helper class for :func:`~ae.base.format_given` to keep placeholder with format unchanged if not found. """
488
+ def get_value(self, key, args, kwargs):
489
+ """ overriding to keep placeholder unchanged if not found """
490
+ try:
491
+ return super().get_value(key, args, kwargs)
492
+ except KeyError:
493
+ return UnformattedValue(key)
494
+
495
+
496
+ def format_given(text: str, placeholder_map: dict[str, Any], strict: bool = False):
497
+ """ replacement for Python's str.format_map(), keeping intact placeholders that are not in the specified mapping.
498
+
499
+ :param text: text/template in which the given/specified placeholders will get replaced. in contrary
500
+ to str.format_map() no KeyError will be raised for placeholders not specified in
501
+ :paramref:`~format_given.placeholder_map`.
502
+ :param placeholder_map: dict with placeholder keys to be replaced in :paramref:`~format_given.text` argument.
503
+ :param strict: pass True to raise error for text templates containing unpaired curly brackets.
504
+ :return: the specified :paramref:`~format_given.text` with only the placeholders specified in
505
+ :paramref:`~format_given.placeholder_map` replaced with their respective map value.
506
+ additionally any ValueError that would be thrown by str.format_map(), e.g. if the
507
+
508
+
509
+ inspired by the answer of CodeManX in `https://stackoverflow.com/questions/3536303`__
510
+ """
511
+ formatter = GivenFormatter()
512
+ try:
513
+ return formatter.vformat(text, (), placeholder_map)
514
+ except (ValueError, Exception) as ex:
515
+ if strict:
516
+ raise ex
517
+ return text
518
+
519
+
472
520
  def full_stack_trace(ex: Exception) -> str:
473
521
  """ get full stack trace from an exception.
474
522
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: ae_base
3
- Version: 0.3.48
3
+ Version: 0.3.49
4
4
  Summary: ae namespace module portion base: basic constants, helper functions and context manager
5
5
  Home-page: https://gitlab.com/ae-group/ae_base
6
6
  Author: AndiEcker
@@ -68,17 +68,17 @@ Dynamic: summary
68
68
 
69
69
  <!-- THIS FILE IS EXCLUSIVELY MAINTAINED by the project ae.ae V0.3.95 -->
70
70
  <!-- THIS FILE IS EXCLUSIVELY MAINTAINED by the project aedev.tpl_namespace_root V0.3.14 -->
71
- # base 0.3.48
71
+ # base 0.3.49
72
72
 
73
73
  [![GitLab develop](https://img.shields.io/gitlab/pipeline/ae-group/ae_base/develop?logo=python)](
74
74
  https://gitlab.com/ae-group/ae_base)
75
75
  [![LatestPyPIrelease](
76
- https://img.shields.io/gitlab/pipeline/ae-group/ae_base/release0.3.47?logo=python)](
77
- https://gitlab.com/ae-group/ae_base/-/tree/release0.3.47)
76
+ https://img.shields.io/gitlab/pipeline/ae-group/ae_base/release0.3.48?logo=python)](
77
+ https://gitlab.com/ae-group/ae_base/-/tree/release0.3.48)
78
78
  [![PyPIVersions](https://img.shields.io/pypi/v/ae_base)](
79
79
  https://pypi.org/project/ae-base/#history)
80
80
 
81
- >ae_base module 0.3.48.
81
+ >ae_base module 0.3.49.
82
82
 
83
83
  [![Coverage](https://ae-group.gitlab.io/ae_base/coverage.svg)](
84
84
  https://ae-group.gitlab.io/ae_base/coverage/index.html)
@@ -0,0 +1,7 @@
1
+ ae/base.py,sha256=V0wU87UunINFILcnar0HlBvF6gB8nLnj1tfQ-uXoCiY,61257
2
+ ae_base-0.3.49.dist-info/LICENSE.md,sha256=uoIIfORuk4V8ZeNh6SN0EUhiU79RC-indIMFqePKVhY,35002
3
+ ae_base-0.3.49.dist-info/METADATA,sha256=Ezl-iNQcsr9r0ssjPJAVgVTvZcQZDbHIqno7of0zTos,5636
4
+ ae_base-0.3.49.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
+ ae_base-0.3.49.dist-info/top_level.txt,sha256=vUdgAslSmhZLXWU48fm8AG2BjVnkOWLco8rzuW-5zY0,3
6
+ ae_base-0.3.49.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
7
+ ae_base-0.3.49.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- ae/base.py,sha256=oZBQxpBifv7BEcygj_JmsEMebDnEBaZiL5v1DCUxPOE,58770
2
- ae_base-0.3.48.dist-info/LICENSE.md,sha256=uoIIfORuk4V8ZeNh6SN0EUhiU79RC-indIMFqePKVhY,35002
3
- ae_base-0.3.48.dist-info/METADATA,sha256=HEFW7mCBlpRR8EHFK0B0ZgIEuCD1lXSVA73Bb9kRE_4,5636
4
- ae_base-0.3.48.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
- ae_base-0.3.48.dist-info/top_level.txt,sha256=vUdgAslSmhZLXWU48fm8AG2BjVnkOWLco8rzuW-5zY0,3
6
- ae_base-0.3.48.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
7
- ae_base-0.3.48.dist-info/RECORD,,