cengal.text_processing.optional_formatter.versions.v_0.optional_formatter

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18from typing import Hashable, Dict, Tuple, List, Any, AnyStr
19
20"""
21Module Docstring
22Docstrings: http://www.python.org/dev/peps/pep-0257/
23"""
24
25__author__ = "ButenkoMS <gtalk@butenkoms.space>"
26__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
27__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
28__license__ = "Apache License, Version 2.0"
29__version__ = "4.4.1"
30__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
31__email__ = "gtalk@butenkoms.space"
32# __status__ = "Prototype"
33__status__ = "Development"
34# __status__ = "Production"
35
36
37class OptionalFormatter:
38    def __init__(self, item_positions: Tuple[Hashable, ...],
39                 template_per_item: Dict[Hashable, Tuple[str, str, str, str, str]]):
40        self._item_positions = item_positions
41        self._template_per_item = template_per_item
42
43    def __call__(self, arguments_per_item: Dict[Hashable, Tuple[Tuple[Any, ...], Dict[str, Any]]]):
44        result = str()
45        is_first = True
46        last_postfix = str()
47        last_r_delimiter = str()
48        for item in self._item_positions:
49            if item in arguments_per_item:
50                item_l_delimiter, item_prefix, item_template, item_postfix, item_r_delimiter = \
51                    self._template_per_item[item]
52                item_args, item_kwargs = arguments_per_item[item]
53
54                rendered_item = str()
55                if is_first:
56                    rendered_item += item_prefix
57                else:
58                    rendered_item += last_r_delimiter
59                    rendered_item += item_l_delimiter
60                last_r_delimiter = item_r_delimiter
61                last_postfix = item_postfix
62                rendered_item += item_template.format(*item_args, **item_kwargs)
63                result += rendered_item
64                if is_first:
65                    is_first = False
66        result += last_postfix
67        return result
68
69
70class OptionalFormatterHandy(OptionalFormatter):
71    """
72    template_per_item second tuple format:
73    [left_delimiter(shown if not first), prefix(shown if first), template, postfix(shown if last), right_delimiter(shown if not last)]
74
75    Example:
76        f = OptionalFormatterHandy('hour', 'word', 'minute', 'second', 'millisecond',
77                                   hour=('', '|(hours)', '{}', '|', ''),
78                                   word=('', '|(word)', '-"{}"-', '|', ''),
79                                   minutes=(':', '|(minutes)', '{}', '|', ''),
80                                   seconds=(':', '|(seconds)', '{}', '|', ''),
81                                   millisecond=('.', '|(millisecond)', '{}', '|', ''))
82
83        f(hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
84        >> |(hours)4:15:54.341|
85
86        f(hour=((0,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((0,), dict()))
87        >> |(hours)0:15:54.0|
88
89        f(minutes=((15,), dict()), seconds=((54,), dict()))
90        >> |(minutes)15:54|
91
92        f(word=(('HELLO!',), dict()), hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
93        >> |(hours)4-"HELLO!"-:15:54.341|
94    """
95    def __init__(self, *item_positions: [str, ...], **template_per_item: [str, Tuple[str, str, str, str, str]]):
96        super().__init__(item_positions, template_per_item)
97
98    def __call__(self, **arguments_per_item: [str, Tuple[Tuple[Any, ...], Dict[str, Any]]]):
99        return super(OptionalFormatterHandy, self).__call__(arguments_per_item)
class OptionalFormatter:
38class OptionalFormatter:
39    def __init__(self, item_positions: Tuple[Hashable, ...],
40                 template_per_item: Dict[Hashable, Tuple[str, str, str, str, str]]):
41        self._item_positions = item_positions
42        self._template_per_item = template_per_item
43
44    def __call__(self, arguments_per_item: Dict[Hashable, Tuple[Tuple[Any, ...], Dict[str, Any]]]):
45        result = str()
46        is_first = True
47        last_postfix = str()
48        last_r_delimiter = str()
49        for item in self._item_positions:
50            if item in arguments_per_item:
51                item_l_delimiter, item_prefix, item_template, item_postfix, item_r_delimiter = \
52                    self._template_per_item[item]
53                item_args, item_kwargs = arguments_per_item[item]
54
55                rendered_item = str()
56                if is_first:
57                    rendered_item += item_prefix
58                else:
59                    rendered_item += last_r_delimiter
60                    rendered_item += item_l_delimiter
61                last_r_delimiter = item_r_delimiter
62                last_postfix = item_postfix
63                rendered_item += item_template.format(*item_args, **item_kwargs)
64                result += rendered_item
65                if is_first:
66                    is_first = False
67        result += last_postfix
68        return result
OptionalFormatter( item_positions: typing.Tuple[typing.Hashable, ...], template_per_item: typing.Dict[typing.Hashable, typing.Tuple[str, str, str, str, str]])
39    def __init__(self, item_positions: Tuple[Hashable, ...],
40                 template_per_item: Dict[Hashable, Tuple[str, str, str, str, str]]):
41        self._item_positions = item_positions
42        self._template_per_item = template_per_item
class OptionalFormatterHandy(OptionalFormatter):
 71class OptionalFormatterHandy(OptionalFormatter):
 72    """
 73    template_per_item second tuple format:
 74    [left_delimiter(shown if not first), prefix(shown if first), template, postfix(shown if last), right_delimiter(shown if not last)]
 75
 76    Example:
 77        f = OptionalFormatterHandy('hour', 'word', 'minute', 'second', 'millisecond',
 78                                   hour=('', '|(hours)', '{}', '|', ''),
 79                                   word=('', '|(word)', '-"{}"-', '|', ''),
 80                                   minutes=(':', '|(minutes)', '{}', '|', ''),
 81                                   seconds=(':', '|(seconds)', '{}', '|', ''),
 82                                   millisecond=('.', '|(millisecond)', '{}', '|', ''))
 83
 84        f(hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
 85        >> |(hours)4:15:54.341|
 86
 87        f(hour=((0,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((0,), dict()))
 88        >> |(hours)0:15:54.0|
 89
 90        f(minutes=((15,), dict()), seconds=((54,), dict()))
 91        >> |(minutes)15:54|
 92
 93        f(word=(('HELLO!',), dict()), hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
 94        >> |(hours)4-"HELLO!"-:15:54.341|
 95    """
 96    def __init__(self, *item_positions: [str, ...], **template_per_item: [str, Tuple[str, str, str, str, str]]):
 97        super().__init__(item_positions, template_per_item)
 98
 99    def __call__(self, **arguments_per_item: [str, Tuple[Tuple[Any, ...], Dict[str, Any]]]):
100        return super(OptionalFormatterHandy, self).__call__(arguments_per_item)

template_per_item second tuple format: [left_delimiter(shown if not first), prefix(shown if first), template, postfix(shown if last), right_delimiter(shown if not last)]

Example: f = OptionalFormatterHandy('hour', 'word', 'minute', 'second', 'millisecond', hour=('', '|(hours)', '{}', '|', ''), word=('', '|(word)', '-"{}"-', '|', ''), minutes=(':', '|(minutes)', '{}', '|', ''), seconds=(':', '|(seconds)', '{}', '|', ''), millisecond=('.', '|(millisecond)', '{}', '|', ''))

f(hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
>> |(hours)4:15:54.341|

f(hour=((0,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((0,), dict()))
>> |(hours)0:15:54.0|

f(minutes=((15,), dict()), seconds=((54,), dict()))
>> |(minutes)15:54|

f(word=(('HELLO!',), dict()), hour=((4,), dict()), minutes=((15,), dict()), seconds=((54,), dict()), millisecond=((341,), dict()))
>> |(hours)4-"HELLO!"-:15:54.341|
OptionalFormatterHandy( *item_positions: [<class 'str'>, Ellipsis], **template_per_item: [<class 'str'>, typing.Tuple[str, str, str, str, str]])
96    def __init__(self, *item_positions: [str, ...], **template_per_item: [str, Tuple[str, str, str, str, str]]):
97        super().__init__(item_positions, template_per_item)