converter
xsdata.formats.converter
Converter
Bases: ABC
Abstract converter class.
Source code in xsdata/formats/converter.py
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 |
|
deserialize(value, **kwargs)
abstractmethod
Convert a value to a python type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Additional keyword arguments needed per converter | {} |
Returns:
Type | Description |
---|---|
Any | The converted value. |
Raises:
Type | Description |
---|---|
ConverterError | if the value can't be converted. |
Source code in xsdata/formats/converter.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
serialize(value, **kwargs)
abstractmethod
Convert value to string for serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Additional keyword arguments needed per converter | {} |
Returns:
Type | Description |
---|---|
str | The converted string value. |
Source code in xsdata/formats/converter.py
54 55 56 57 58 59 60 61 62 63 64 |
|
validate_input_type(value, tp)
classmethod
Validate the input value type matches the required type.
Source code in xsdata/formats/converter.py
66 67 68 69 70 71 72 |
|
ConverterFactory
Converter factory class.
Attributes:
Name | Type | Description |
---|---|---|
registry | Dict[Type, Converter] | The registered converters |
Source code in xsdata/formats/converter.py
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
deserialize(value, types, **kwargs)
Attempt to convert any value to one of the given types.
If all attempts fail return the value input value and emit a warning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
types | Sequence[Type] | The target candidate types | required |
**kwargs | Any | Additional keyword arguments needed per converter | {} |
Returns:
Type | Description |
---|---|
Any | The converted value or the input value. |
Source code in xsdata/formats/converter.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
serialize(value, **kwargs)
Convert the given value to string.
If the value is a list assume the value is a list of tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Additional keyword arguments needed per converter | {} |
Returns:
Type | Description |
---|---|
Any | The converted string value or None if the input value is None. |
Source code in xsdata/formats/converter.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
test(value, types, strict=False, **kwargs)
Test the given string value can be converted to one of the given types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Optional[str] | The input value | required |
types | Sequence[Type] | The candidate target types | required |
strict | bool | validate the string output also matches the original input | False |
**kwargs | Any | Additional keyword arguments needed per converter | {} |
Returns:
Type | Description |
---|---|
bool | The bool result. |
Source code in xsdata/formats/converter.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
register_converter(data_type, func)
Register a callable or converter for the given data type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_type | Type | The data type | required |
func | Union[Callable, Converter] | The callable or converter instance | required |
Source code in xsdata/formats/converter.py
173 174 175 176 177 178 179 180 181 182 183 |
|
unregister_converter(data_type)
Unregister the converter for the given data type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_type | Type | The data type | required |
Raises:
Type | Description |
---|---|
KeyError | if the data type is not registered. |
Source code in xsdata/formats/converter.py
185 186 187 188 189 190 191 192 193 194 |
|
type_converter(data_type)
Find a suitable converter for given data type.
Iterate over all but last mro items and check for registered converters, fall back to str and issue a warning if there are no matches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_type | Type | The data type | required |
Returns:
Type | Description |
---|---|
Converter | A converter instance |
Source code in xsdata/formats/converter.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
value_converter(value)
Get a suitable converter for the given value.
Source code in xsdata/formats/converter.py
223 224 225 |
|
sort_types(types)
classmethod
Sort a list of types by giving priority to strict types first.
Source code in xsdata/formats/converter.py
227 228 229 230 231 232 233 |
|
explicit_types()
classmethod
Get a list of types that need strict test.
Source code in xsdata/formats/converter.py
235 236 237 238 |
|
StringConverter
Bases: Converter
A str converter.
Source code in xsdata/formats/converter.py
271 272 273 274 275 276 277 278 279 280 |
|
deserialize(value, **kwargs)
Convert a value to string.
Source code in xsdata/formats/converter.py
274 275 276 |
|
serialize(value, **kwargs)
Convert a value to string.
Source code in xsdata/formats/converter.py
278 279 280 |
|
BoolConverter
Bases: Converter
A bool converter.
Source code in xsdata/formats/converter.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
deserialize(value, **kwargs)
Convert a value to bool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
bool | True if the value is in (True, "true", "1") |
bool | False if the value is in (False, "false", "0") |
Raises:
Type | Description |
---|---|
ConverterError | if the value can't be converted to bool. |
Source code in xsdata/formats/converter.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
serialize(value, **kwargs)
Convert a bool value to string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | bool | The input bool value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
str | "true" or "false" |
Source code in xsdata/formats/converter.py
316 317 318 319 320 321 322 323 324 325 326 |
|
IntConverter
Bases: Converter
An int converter.
Source code in xsdata/formats/converter.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
deserialize(value, **kwargs)
Convert a value to int.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
int | The int converted value. |
Raises:
Type | Description |
---|---|
ConverterError | on value or type errors. |
Source code in xsdata/formats/converter.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
serialize(value, **kwargs)
Convert an int value sto string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | int | The input int value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
str | The str converted value. |
Source code in xsdata/formats/converter.py
350 351 352 353 354 355 356 357 358 359 360 |
|
FloatConverter
Bases: Converter
A float converter.
Source code in xsdata/formats/converter.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
deserialize(value, **kwargs)
Convert a value to float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
float | The float converted value. |
Raises:
Type | Description |
---|---|
ConverterError | on value errors. |
Source code in xsdata/formats/converter.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
serialize(value, **kwargs)
Convert a float value sto string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | float | The input int value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
str | The str converted value. |
Source code in xsdata/formats/converter.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
BytesConverter
Bases: Converter
A bytes converter for base16 and base64 formats.
Source code in xsdata/formats/converter.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
deserialize(value, **kwargs)
Convert a string value to base16 or base64 format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input string value | required |
**kwargs | Any | Additional keyword arguments format: The target output format (base16|base64) | {} |
Returns:
Type | Description |
---|---|
bytes | The bytes converted value. |
Raises:
Type | Description |
---|---|
ConverterError | If format is empty or not supported or the value contains invalid characters. |
Source code in xsdata/formats/converter.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
serialize(value, **kwargs)
Convert a bytes value sto string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | bytes | The input bytes value | required |
**kwargs | Any | Additional keyword arguments format: The input value format (base16|base64) | {} |
Returns:
Type | Description |
---|---|
str | The str converted value. |
Raises:
Type | Description |
---|---|
ConverterError | If format doesn't match the value type or it's not supported. |
Source code in xsdata/formats/converter.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
DecimalConverter
Bases: Converter
A decimal converter.
Source code in xsdata/formats/converter.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
deserialize(value, **kwargs)
Convert a value to decimal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
Decimal | The decimal converted value. |
Raises:
Type | Description |
---|---|
ConverterError | on InvalidOperation errors. |
Source code in xsdata/formats/converter.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
|
serialize(value, **kwargs)
Convert a decimal value sto string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Decimal | The input decimal value | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
str | The str converted value. |
Source code in xsdata/formats/converter.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
QNameConverter
Bases: Converter
A QName converter.
Source code in xsdata/formats/converter.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
deserialize(value, ns_map=None, **kwargs)
Convert a string value to QName instance.
The method supports strings with namespace prefixes or fully namespace qualified strings.
Examples:
- xs:string -> QName("http://www.w3.org/2001/XMLSchema", "string")
- {foo}bar -> QName("foo", "bar"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | str | The input str value | required |
ns_map | Optional[Dict] | A namespace prefix-URI map | None |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
QName | A QName instance |
Raises:
Type | Description |
---|---|
ConverterError | If the prefix can't be resolved. |
Source code in xsdata/formats/converter.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
|
serialize(value, ns_map=None, **kwargs)
Convert a QName instance value sto string.
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:
- QName("http://www.w3.org/2001/XMLSchema", "int") & ns_map -> xs:int
- QName("foo, "bar") -> {foo}bar
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | QName | The qname instance to convert | required |
ns_map | Optional[Dict] | A namespace prefix-URI map, if we want to use prefixes | None |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
str | The str converted value. |
Source code in xsdata/formats/converter.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
|
resolve(value, ns_map=None)
staticmethod
Split a qname or ns prefixed string value or a uri, name pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | str | the input value to resolve | required |
ns_map | Optional[Dict] | A namespace prefix-URI map | None |
Returns:
Type | Description |
---|---|
Tuple[str, str] | A tuple of uri and name strings. |
Raises:
Type | Description |
---|---|
ConverterError | if the uri is not valid, if the prefix can't be resolved to a URI, if the name is not a valid NCName |
Source code in xsdata/formats/converter.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
|
EnumConverter
Bases: Converter
An enum converter.
Source code in xsdata/formats/converter.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
|
serialize(value, **kwargs)
Convert an enum member to a string.
Source code in xsdata/formats/converter.py
615 616 617 |
|
deserialize(value, data_type=None, **kwargs)
Convert a value to an enum member.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
data_type | Optional[EnumMeta] | The enumeration class | None |
**kwargs | Any | Additional keyword arguments needed for parsing the value to a python type. | {} |
Returns:
Type | Description |
---|---|
Enum | The enum member. |
Raises:
Type | Description |
---|---|
ConverterError | if the data type is not an enum, or the value doesn't match any of the enum members. |
Source code in xsdata/formats/converter.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
match(value, values, length, real, **kwargs)
classmethod
Match a value to one of the enumeration values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value | required |
values | Sequence | The input value as a sequence, in case of NMTokens | required |
length | int | The length of the sequence values | required |
real | Any | The enumeration value | required |
**kwargs | Any | Additional keyword arguments needed for parsing the value to a python type. | {} |
Returns:
Type | Description |
---|---|
bool | Whether the value or values matches the enumeration member value. |
Source code in xsdata/formats/converter.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
|
DateTimeBase
Bases: Converter
An abstract datetime converter.
Source code in xsdata/formats/converter.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
|
parse(value, **kwargs)
classmethod
Parse a str into a datetime instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input string value | required |
**kwargs | Any | Additional keyword argument format: The datetime format to use | {} |
Returns:
Type | Description |
---|---|
datetime | The datetime instance |
Raises:
Type | Description |
---|---|
ConverterError | If no format was provided or the value could not be converted. |
Source code in xsdata/formats/converter.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
|
serialize(value, **kwargs)
Convert a datetime instance to string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Union[date, time] | The input datetime instance | required |
**kwargs | Any | Additional keyword argument format: The datetime format to use | {} |
Returns:
Type | Description |
---|---|
str | The converted str value. |
Raises:
Type | Description |
---|---|
ConverterError | If no format was provided or the value could not be converted. |
Source code in xsdata/formats/converter.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
|
deserialize(value, **kwargs)
abstractmethod
Parse string literal value into python.
Source code in xsdata/formats/converter.py
759 760 761 |
|
TimeConverter
Bases: DateTimeBase
A datetime.time converter.
Source code in xsdata/formats/converter.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
|
deserialize(value, **kwargs)
Convert the input str to a time instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input string value | required |
**kwargs | Any | Additional keyword argument format: The time format to use | {} |
Returns:
Type | Description |
---|---|
time | The time instance |
Raises:
Type | Description |
---|---|
ConverterError | If no format was provided or the value could not be converted. |
Source code in xsdata/formats/converter.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
|
DateConverter
Bases: DateTimeBase
A datetime.date converter.
Source code in xsdata/formats/converter.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
deserialize(value, **kwargs)
Convert the input str to a date instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input string value | required |
**kwargs | Any | Additional keyword argument format: The time format to use | {} |
Returns:
Type | Description |
---|---|
date | The date instance |
Raises:
Type | Description |
---|---|
ConverterError | If no format was provided or the value could not be converted. |
Source code in xsdata/formats/converter.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
DateTimeConverter
Bases: DateTimeBase
A datetime.datetime converter.
Source code in xsdata/formats/converter.py
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
|
deserialize(value, **kwargs)
Convert the input str to a datetime instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input string value | required |
**kwargs | Any | Additional keyword argument format: The time format to use | {} |
Returns:
Type | Description |
---|---|
datetime | The datetime instance |
Raises:
Type | Description |
---|---|
ConverterError | If no format was provided or the value could not be converted. |
Source code in xsdata/formats/converter.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
|
ProxyConverter
Bases: Converter
Proxy wrapper to treat callables as converters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
factory | Callable | The callable factory | required |
Source code in xsdata/formats/converter.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
|
deserialize(value, **kwargs)
Call the instance factory and return the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | Any | The input value to convert | required |
**kwargs | Any | Unused keyword arguments | {} |
Returns:
Type | Description |
---|---|
Any | The return result of the callable. |
Raises:
Type | Description |
---|---|
ConverterError | on value errors. |
Source code in xsdata/formats/converter.py
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
|
serialize(value, **kwargs)
Cast value to str.
Source code in xsdata/formats/converter.py
857 858 859 |
|