File: datatypes.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (24 lines) | stat: -rw-r--r-- 680 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import namedtuple
from typing import Any

from xsdata.exceptions import ConverterError
from xsdata.formats.converter import Converter
from xsdata.formats.converter import converter

Telephone = namedtuple('Telephone', ['country_code', 'area_code', 'number'])


class PhoneConverter(Converter):

    def deserialize(self, value: Any, **kwargs: Any) -> Any:
        parts = value.split("-")
        if len(parts) == 3:
            return Telephone(*map(int, parts))

        raise ConverterError()

    def serialize(self, value: Telephone, **kwargs: Any) -> str:
        return "-".join(map(str, value))


converter.register_converter(Telephone, PhoneConverter())