xsdata.formats.converter module

class xsdata.formats.converter.Converter[source]

Bases: object

Abstract converter class.

abstract from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

Any

abstract to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (Any) –

  • kwargs (Any) –

Return type

str

class xsdata.formats.converter.ConverterAdapter(registry=<factory>)[source]

Bases: object

Parameters

registry (Dict[Type, Converter]) – Converters registry

registry: Dict[Type, xsdata.formats.converter.Converter]
from_string(value, types, **kwargs)[source]

Attempt to convert a string to one of the given types.

If the input is not a string return the input value. If all attempts fail return the value input value and issue a warning.

Return type

Any

Returns

The first successful converted value.

Parameters
to_string(value, **kwargs)[source]

Convert the given value to string, ignore None values.

If the value is a list assume the value is a list of tokens.

Parameters
  • value (Any) –

  • kwargs (Any) –

Return type

Any

register_converter(data_type, func)[source]

Register a callable or converter for the given data type.

Callables will be wrapped in a xsdata.formats.converter.ProxyConverter

Parameters
unregister_converter(data_type)[source]

Unregister the converter for the given data type.

Raises

KeyError – if the data type is not registered.

Parameters

data_type (Type) –

type_converter(data_type)[source]

Get a suitable converter for given data type.

Try the parent type if the original doesn’t exist, fall back to str and issue a warning if no converter matches.

Parameters

data_type (Type) –

Return type

Converter

value_converter(value)[source]

Get a suitable converter for the given value.

Parameters

value (Any) –

Return type

Converter

classmethod sort_types(types)[source]

Sort a list of types by giving priority to strict types first.

Parameters

types (List[Type]) –

Return type

List[Type]

class xsdata.formats.converter.BoolConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

bool

to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (bool) –

  • kwargs (Any) –

Return type

str

class xsdata.formats.converter.IntConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

int

to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (int) –

  • kwargs (Any) –

Return type

str

class xsdata.formats.converter.StrConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

str

to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

str

class xsdata.formats.converter.FloatConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

float

to_string(value, **kwargs)[source]

Convert to string.

Parameters
Return type

str

class xsdata.formats.converter.DecimalConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

Decimal

to_string(value, **kwargs)[source]

Convert to string.

Parameters
Return type

str

class xsdata.formats.converter.QNameConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, ns_map=None, **kwargs)[source]

Convert namespace prefixed strings, or fully qualified strings to QNames.

examples:
  • xs:string -> QName(“http://www.w3.org/2001/XMLSchema”, “string”)

  • {foo}bar -> QName(“foo”, “bar”

Parameters
Return type

QName

to_string(value, ns_map=None, **kwargs)[source]

Convert a QName instance to string either with a namespace prefix if a prefix-URI namespaces mapping is provided or to a fully qualified name with the namespace.

examples:
Parameters
Return type

str

static resolve(value, ns_map)[source]
Parameters
Return type

Tuple

class xsdata.formats.converter.LxmlQNameConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, ns_map=None, **kwargs)[source]

Convert namespace prefixed strings, or fully qualified strings to QNames.

examples:
  • xs:string -> QName(“http://www.w3.org/2001/XMLSchema”, “string”)

  • {foo}bar -> QName(“foo”, “bar”

Parameters
Return type

QName

to_string(value, ns_map=None, **kwargs)[source]

Convert a QName instance to string either with a namespace prefix if a prefix-URI namespaces mapping is provided or to a fully qualified name with the namespace.

examples:
Parameters
Return type

str

class xsdata.formats.converter.EnumConverter[source]

Bases: xsdata.formats.converter.Converter

from_string(value, data_type=None, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
Return type

Enum

to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (Enum) –

  • kwargs (Any) –

Return type

str

class xsdata.formats.converter.ProxyConverter(func)[source]

Bases: xsdata.formats.converter.Converter

Parameters

func (Callable) – the callable to convert from string

func: Callable
from_string(value, **kwargs)[source]

Convert from string.

Raises

ConverterError – if converter fails with and expected ValueError

Parameters
  • value (str) –

  • kwargs (Any) –

Return type

Any

to_string(value, **kwargs)[source]

Convert to string.

Parameters
  • value (Any) –

  • kwargs (Any) –

Return type

str