Skip to content

Latest commit

History

38 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

destructure

Easy declarative schema validation with optional name-binding.

>>>fromdestructureimportBinding, match>>>>>>o=Binding()
>>>schema= {
... 'string': str,
... 'any': ...,
... 'binding': o.name,
... 'sequence': [1, 2, ...],
... 'mapping': {'a': int, ...: ...},
... }
>>>data= {
... 'string': 'a',
... 'any': 5j,
... 'binding': 42,
... 'sequence': [1, 2, 3, 4],
... 'mapping': {'a': 1, 'b': 2, 'c': 3},
... }
>>>guard=lambda : o.name>10>>>data==match(schema, data, guard)
True>>>o.name42

Pick between several schemas with a handy Switch.case.

>>>fromdestructureimportBinding, Switch>>>>>>o=Binding()
>>>schema1= [1, o.x, 3]
>>>schema2= [2, 4, o.x]
>>>>>>s=Switch([2, 4, 6])
>>>ifs.case(schema1):
... print(o.x)
... elifs.case(schema2):
... print(o.x)
... else:
... print('otherwise')
6

Schemas may include many kinds of objects, though to make use of Ellipses or Binding, the class must support keyword arguments and cannot be too strict with parameter type-checking.

>>>classFoo:
... def__init__(self, bar):
... self.bar=bar>>>bind=Binding()
>>>schema=Foo(bar=bind.x)
>>>result=match(schema, Foo(bar=1))
>>>bind.x1

See some example scripts for a practical usage, in context.

About

Easy declarative schema validation with optional name-binding. Gives Python a switch/case.

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages