File: decoding.py

package info (click to toggle)
python-xmlschema 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,208 kB
  • sloc: python: 39,174; xml: 1,282; makefile: 36
file content (108 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#
# Copyright (c), 2016-2024, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
from decimal import Decimal
from collections.abc import Iterator, MutableMapping, MutableSequence
from typing import Any, Optional, Union

from xmlschema.aliases import DecodedValueType, NumericValueType

DecodedAttributesType = Optional[MutableMapping[str, DecodedValueType]]


class EmptyType:
    _instance = None

    def __new__(cls) -> 'EmptyType':
        if cls._instance is None:
            cls._instance = super(EmptyType, cls).__new__(cls)
        return cls._instance


Empty = EmptyType()
"""A singleton instance for representing empty decode/encode results."""


def count_digits(number: NumericValueType) -> tuple[int, int]:
    """
    Counts the digits of a number.

    :param number: an int or a float or a Decimal or a string representing a number.
    :return: a couple with the number of digits of the integer part and \
    the number of digits of the decimal part.
    """
    if isinstance(number, str):
        number = str(Decimal(number)).lstrip('-+')
    elif isinstance(number, bytes):
        number = str(Decimal(number.decode())).lstrip('-+')
    else:
        number = str(number).lstrip('-+')

    if 'E' in number:
        significand, _, _exponent = number.partition('E')
    elif 'e' in number:
        significand, _, _exponent = number.partition('e')
    elif '.' not in number:
        return len(number.lstrip('0')), 0
    else:
        integer_part, _, decimal_part = number.partition('.')
        return len(integer_part.lstrip('0')), len(decimal_part.rstrip('0'))

    significand = significand.strip('0')
    exponent = int(_exponent)

    num_digits = len(significand) - 1 if '.' in significand else len(significand)
    if exponent > 0:
        return num_digits + exponent, 0
    else:
        return 0, num_digits - exponent - 1


def strictly_equal(obj1: object, obj2: object) -> bool:
    """Checks if the objects are equal and are of the same type."""
    return obj1 == obj2 and type(obj1) is type(obj2)


def raw_encode_value(value: DecodedValueType) -> Optional[str]:
    """Encodes a simple value to XML."""
    if isinstance(value, bool):
        return 'true' if value else 'false'
    elif isinstance(value, (list, tuple)):
        return ' '.join(e for e in (raw_encode_value(v) for v in value) if e is not None)
    elif isinstance(value, bytes):
        return value.decode()
    else:
        return str(value) if value is not None else None


def raw_encode_attributes(attributes: DecodedAttributesType = None) \
        -> dict[str, str]:
    attrib: dict[str, str] = {}
    if attributes:
        for k, v in attributes.items():
            value = raw_encode_value(v)
            if value is not None:
                attrib[k] = value
    return attrib


def iter_decoded_data(obj: Any, level: int = 0) \
        -> Iterator[tuple[Union[MutableMapping[Any, Any], MutableSequence[Any]], int]]:
    """
    Iterates a nested object composed by lists and dictionaries,
    pairing with the level depth.
    """
    if isinstance(obj, MutableMapping):
        yield obj, level
        for value in obj.values():
            yield from iter_decoded_data(value, level + 1)
    elif isinstance(obj, MutableSequence):
        yield obj, level
        for item in obj:
            yield from iter_decoded_data(item, level + 1)