pane.convert
pane.convert
High-level interface to pane
.
ScalarType = t.Union[str, bytes, int, bool, float, complex, None]
module-attribute
DataType = t.Union[str, bytes, int, bool, float, complex, None, t.Mapping['DataType', 'DataType'], t.Sequence['DataType'], numpy.NDArray[numpy.generic]]
module-attribute
Common data interchange type. into_data
converts to this.
Convertible = t.Union[DataType, HasConverter, t.Mapping['Convertible', 'Convertible'], t.Sequence['Convertible'], t.AbstractSet[DataType], Fraction, Decimal, datetime.datetime, datetime.date, datetime.time, os.PathLike[str], t.Pattern[str], t.Pattern[bytes], enum.Enum]
module-attribute
Types supported by from_data
.
Consists of DataType
+ HasConverter
+ supported stdlib types.
IntoConverter = t.Union[t.Type[Convertible], t.Type[t.Any], t.Mapping[str, 'IntoConverter'], t.Sequence['IntoConverter']]
module-attribute
Inputs supported by make_converter
.
Consists of t.Type[Convertible]
, mappings (struct types), and sequences (tuple types).
IntoConverterHandlers = t.Union[ConverterHandler, t.Sequence[ConverterHandler], t.Dict[type, 'Converter[t.Any]']]
module-attribute
ConvertError
Bases: Exception
pane
conversion error.
self.tree
contains a detailed error tree, and str(self)
is a human-friendly representation of the same.
Source code in pane/errors.py
tree = tree
instance-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.
HasConverter
Bases: Protocol
Protocol to add convert
functionality into an arbitrary type from data.
Source code in pane/convert.py
_converter(*args, handlers)
classmethod
Return a Converter
capable of constructing cls
.
Any given type arguments are passed as positional arguments. This function should error when passed unknown type arguments.
handlers
must be passed through to any sub-calls to make_converter
.
Source code in pane/convert.py
ConverterHandler
Bases: Protocol
Function which may be called to create a converter.
Receives two arguments, and one keyword argument handlers
.
The first argument is the base type, the second argument is a tuple of any type arguments,
and the keyword argument handlers
must be passed through to any nested calls to make_converter
.
May return NotImplemented
, in which case handling will be passed to other handlers
(including the default handlers).
Source code in pane/convert.py
ConverterHandlers
dataclass
Source code in pane/convert.py
globals = ()
class-attribute
instance-attribute
Converters passed globally to convert, into_data, or from_data.
class_local = ()
class-attribute
instance-attribute
Converters local to a given class. These will be overriden by inner classes.
_make_converter_key_f(ty, handlers=ConverterHandlers())
make_converter(ty, handlers=ConverterHandlers())
Make a Converter
for ty
.
Supports types, mappings of types, and sequences of types.
Source code in pane/convert.py
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 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 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 327 |
|
register_converter_handler(handler)
Register a handler for make_converter.
This allows extending pane
to handle third-party types, not
defined by your code or by pane
. Use sparingly, as this will
add runtime to make_converter
.
Source code in pane/convert.py
_annotated_converter(ty, args, *, handlers)
Make an annotated converter.
Wraps ty
in args
from left to right. However, Condition
annotations
are handled separately (bundled together).
Source code in pane/convert.py
into_data(val, ty=None, *, custom=None)
Convert val
of type ty
into a data interchange format.
Source code in pane/convert.py
from_data(val, ty, *, custom=None)
Convert val
from a data interchange format into type ty
.
Source code in pane/convert.py
convert(val, ty, *, custom=None)
Convert val
into type ty
, passing through a data interchange format.