pane.converters
pane.converters
Converter types, which do the hard work of recursive validation.
NestedSequence = t.Union[T, t.Sequence['NestedSequence[T]']]
module-attribute
DatetimeT = t.TypeVar('DatetimeT', bound=t.Union[datetime.datetime, datetime.date, datetime.time])
module-attribute
Converter
Base class for a converter to a given type T_co
.
Source code in pane/converters.py
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Convert val
into a data interchange format.
val
should be of a type returned by this converter,
but don't count on it.
expected(plural=False)
abstractmethod
Return a descriptive string indicating the value(s) expected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plural
|
bool
|
Whether to pluralize the descriptive string |
False
|
try_convert(val)
abstractmethod
Attempt to convert val
to T
.
Should raise ParseInterrupt
(and only ParseInterrupt
)
when a given parsing path reaches a dead end.
collect_errors(val)
abstractmethod
Return an error tree caused by converting val
to T
.
collect_errors
should return None
iff convert
doesn't raise.
AnyConverter
dataclass
Converter for t.Any
.
Source code in pane/converters.py
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Convert val
into a data interchange format.
val
should be of a type returned by this converter,
but don't count on it.
try_convert(val)
expected(plural=False)
collect_errors(val)
ScalarConverter
dataclass
Bases: Converter[T]
Converter for a simple scalar type, constructible from a list of allowed types.
Source code in pane/converters.py
ty
instance-attribute
Type to convert into.
allowed
instance-attribute
Type or list of allowed types.
expect = None
class-attribute
instance-attribute
Singular form of expected value.
expect_plural = None
class-attribute
instance-attribute
Plural form of expected value.
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
NoneConverter
dataclass
Bases: Converter[None]
Converter which accepts only None
.
Source code in pane/converters.py
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Convert val
into a data interchange format.
val
should be of a type returned by this converter,
but don't count on it.
try_convert(val)
expected(plural=False)
collect_errors(val)
LiteralConverter
dataclass
Bases: Converter[T_co]
Converter which accepts any of a list of literal values.
Source code in pane/converters.py
vals
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Convert val
into a data interchange format.
val
should be of a type returned by this converter,
but don't count on it.
try_convert(val)
expected(plural=False)
collect_errors(val)
UnionConverter
dataclass
Converter for an untagged union of subtypes. Unions are always evaluated left-to-right.
Source code in pane/converters.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
types = tuple(flatten_union_args(types))
instance-attribute
List of potential types
converters = tuple(make_converter(ty, handlers) for ty in types)
instance-attribute
List of type converters
constructor = constructor
instance-attribute
Constructor to call with parsed value.
Called with (val, index of type in union)
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
expected(plural=False)
into_data(val)
Source code in pane/converters.py
construct(val, i)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
TaggedUnionConverter
dataclass
Bases: UnionConverter
Converter for a tagged union of subtypes.
Tagged unions may be parsed in three ways. The default
is an 'internally tagged' union, where the tag is found
by looking for a given key in the given object. This is the
default.
An 'externally tagged' union is stored as a dict with a key
and a single value {tag: content}
. This may be specified
using external=True
.
Finally, a 'adjacently tagged' union may be specified using
two keys external=(t, c)
. The union is stored as
{t: tag, c: content}
Source code in pane/converters.py
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 327 328 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 361 362 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 406 407 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 |
|
types = tuple(flatten_union_args(types))
instance-attribute
List of potential types
converters = tuple(make_converter(ty, handlers) for ty in types)
instance-attribute
List of type converters
constructor = constructor
instance-attribute
Constructor to call with parsed value.
Called with (val, index of type in union)
tag = tag
instance-attribute
external = external if isinstance(external, t.Sequence) else bool(external)
class-attribute
instance-attribute
Tagged union representation. False: internal representation True: external representation (t, c): adjacent representation
tag_map = {}
instance-attribute
Map from tags to indices into self.types/self.converters
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
construct(val, i)
tag_expected()
obj_expected(plural=False)
expected(plural=False)
Source code in pane/converters.py
into_data(val)
Source code in pane/converters.py
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
StructConverter
dataclass
Bases: Converter[T]
Converter for a simple, hetereogeneous struct-like type, constructible from a dict.
Source code in pane/converters.py
ty
instance-attribute
Type to convert into. Must be constructible from a dict/mapping.
fields
instance-attribute
List of fields and their types
name = None
class-attribute
instance-attribute
Optional name of struct
handlers = ConverterHandlers()
class-attribute
instance-attribute
opt_fields = dataclasses.field(default_factory=set[str])
class-attribute
instance-attribute
Set of fields which are optional
field_converters = dataclasses.field(init=False)
class-attribute
instance-attribute
Dict of sub-converters for each field
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
expected(plural=False)
into_data(val)
Source code in pane/converters.py
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
TupleConverter
dataclass
Bases: Generic[T]
, Converter[T]
Converter for a simple, heterogeneous tuple-like type
Source code in pane/converters.py
ty = ty
instance-attribute
Type to convert into. Must be constructible from an iterable
converters = tuple(make_converter(ty, handlers) for ty in types)
instance-attribute
List of sub-converters for each field
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
DictConverter
dataclass
Bases: Generic[FromDataK, FromDataV]
, Converter[Mapping[FromDataK, FromDataV]]
Converter for a homogenous dict-like type.
Source code in pane/converters.py
ty = ty
instance-attribute
Type to convert into. Must be constructible from a dict (unless constructor
is specified)
k_conv = make_converter(k, handlers)
instance-attribute
Sub-converter for keys
v_conv = make_converter(v, handlers)
instance-attribute
Sub-converter for values
constructor = self.ty if constructor is None else constructor
instance-attribute
handlers = handlers
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Source code in pane/converters.py
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
SequenceConverter
dataclass
Bases: Generic[FromDataT]
, Converter[Sequence[FromDataT]]
Converter for a homogenous sequence-like type
Source code in pane/converters.py
ty = ty
instance-attribute
Type to convert into. Must be constructible from an iterator.
v_conv = make_converter(v, handlers)
instance-attribute
Sub-converter for values
handlers = handlers
instance-attribute
constructor = self.ty if constructor is None else constructor
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Source code in pane/converters.py
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
NestedSequenceConverter
dataclass
Bases: Generic[T, U]
, Converter[T]
Converter which delegates to a sub-converter, and then attempts to construct a different type
Source code in pane/converters.py
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 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
|
val_type
instance-attribute
Inner type to convert to
constructor
instance-attribute
Constructor to call.
handlers = ConverterHandlers()
class-attribute
instance-attribute
ragged = False
class-attribute
instance-attribute
Whether to accept ragged arrays.
into_data_f = None
class-attribute
instance-attribute
val_conv = dataclasses.field(init=False)
class-attribute
instance-attribute
Converter
for value type
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
expected(plural=False)
Source code in pane/converters.py
into_data(val)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
ConditionalConverter
dataclass
Bases: Generic[FromDataT]
, Converter[FromDataT]
Converter which applies an arbitrary pre-condition to the converted value.
Source code in pane/converters.py
inner_type
instance-attribute
Inner type to apply condition to
condition
instance-attribute
Function to evaluate condition
condition_name
instance-attribute
Human-readable name of condition
make_expected
instance-attribute
Function which takes (expected, plural)
and makes a compound expected
.
handlers = ConverterHandlers()
class-attribute
instance-attribute
inner = dataclasses.field(init=False)
class-attribute
instance-attribute
Inner sub-converter
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
EnumConverter
Source code in pane/converters.py
ty = ty
instance-attribute
val_map = {member.value: memberfor member in members}
instance-attribute
member_vals = tuple(self.val_map.keys())
instance-attribute
inner_ty = type_union(map(type, self.member_vals))
instance-attribute
inner_conv = make_converter(self.inner_ty, handlers)
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
expected(plural=False)
try_convert(val)
collect_errors(val)
Source code in pane/converters.py
DelegateConverter
dataclass
Bases: Generic[T, U]
, Converter[T]
Converter which delegates to a sub-converter, and then attempts to construct a different type
Source code in pane/converters.py
from_type
instance-attribute
Inner type to convert to
constructor
instance-attribute
Constructor for outer type
expect = None
class-attribute
instance-attribute
Expected value. Defaults to inner expected value.
expect_plural = None
class-attribute
instance-attribute
Plural expected value. Defaults to inner expected value.
handlers = ConverterHandlers()
class-attribute
instance-attribute
inner = dataclasses.field(init=False)
class-attribute
instance-attribute
Inner sub-converter
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
Source code in pane/converters.py
expected(plural=False)
Source code in pane/converters.py
try_convert(val)
collect_errors(val)
Source code in pane/converters.py
PatternConverter
dataclass
Bases: Generic[AnyStr]
, Converter[Pattern[AnyStr]]
Source code in pane/converters.py
ty = ty
instance-attribute
ty_conv = make_converter(self.ty, handlers)
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
into_data(val)
expected(plural=False)
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
DatetimeConverter
Bases: Converter[DatetimeT]
, Generic[DatetimeT]
Converter for a simple scalar type, constructible from a list of allowed types.
Source code in pane/converters.py
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 |
|
ty = ty
instance-attribute
super_ty
instance-attribute
convert(val)
Convert val
to T_co
. Raises a ConvertError
on failure.
Source code in pane/converters.py
expected(plural=False)
into_data(val)
from_datetime(dt)
Source code in pane/converters.py
from_date(dt)
Source code in pane/converters.py
try_convert(val)
Source code in pane/converters.py
collect_errors(val)
Source code in pane/converters.py
data_is_sequence(val)
data_is_iterable(val)
Return whether val
is an iterable (not str or bytes) data type.