pyskema package
Submodules
pyskema.describe module
Implement a function to describe a schema in human readable text.
- class pyskema.describe.Describer
Bases:
VisitorA visitor that implement description.
You should probably be using
describe()instead.- classmethod describe(schema)
Generate a set lines describing schema.
- describe_node(value, prefix='', indent=0)
- visit_atom(atom, indent=0)
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection, indent=0)
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map, indent=0)
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec, indent=0)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, indent=0)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, indent=0)
Visit an
pyskema.schema.Unioninstance.
- class pyskema.describe.Searcher
Bases:
VisitorA visitor implementing the search.
- classmethod search(schema, name)
Show the visit
- visit_atom(atom, name, path=())
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection, name, path=())
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map, name, path=())
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec, name, path=())
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, name, path=())
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, name, path=())
Visit an
pyskema.schema.Unioninstance.
- pyskema.describe.describe(schema, selects=())
Describe in human readable format
schema.- Parameters:
schema – The schema to describe.
selects – (optional,
()) a path into schema to describe a sub-tree element only.
- pyskema.describe.search(schema, name)
Search for name into schema.
- pyskema.describe.select(schema, selects, index=0)
pyskema.edit module
Create a new schema with edits.
Internally used to implement schema.Node.inject() and schema.Node.delete().
- class pyskema.edit.Deleter
Bases:
CopyingVisitorVisitor implementing
schema.Node.delete().- visit_atom(atom, path)
Visit an
pyskema.schema.Atominstance.
- visit_record(rec, path)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, path)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, path)
Visit an
pyskema.schema.Unioninstance.
- class pyskema.edit.Injecter
Bases:
CopyingVisitorVisitor implementing
schema.Node.inject().- visit_atom(atom, path, _element)
Visit an
pyskema.schema.Atominstance.
- visit_record(rec, path, element)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, path, element)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, path, element)
Visit an
pyskema.schema.Unioninstance.
pyskema.misc module
Miscellaneous utils.
- class pyskema.misc.Result(valid: bool, val)
Bases:
objectThe Result monad.
- property error
- classmethod fail(error)
- classmethod ok(value)
- property value
- pyskema.misc.closest(alternatives, key)
Search among
alternativesthe element tokey.Uses
lev_dist()to compute the distance.- Parameters:
alternatives – a
list[str]of elements to analyse.key – a
strkey to search for.
- pyskema.misc.lev_dist(a, b)
Compute the Levenstein distance between two
str.
pyskema.schema module
The core definition of a schema.
- class pyskema.schema.Atom(type_: AtomType, options: List[str] | None = None)
Bases:
StructureStructure of an atomic piece of data
- accept(visitor, *args, **kwargs)
- options: List[str] | None = None
- class pyskema.schema.AtomType(value)
Bases:
EnumKind of atomic value.
- BOOL = 3
- FLOAT = 1
- INT = 0
- OPTION = 4
- STR = 2
- class pyskema.schema.Collection(element: Node)
Bases:
StructureAn ordered collection of similar nodes
- accept(visitor, *args, **kwargs)
- classmethod structure_default()
- class pyskema.schema.Map(element: Node)
Bases:
StructureA key-value pair collection.
Keys are not restricted and values are of a single type.
- accept(visitor, *args, **kwargs)
- classmethod structure_default()
- class pyskema.schema.Node(structure: Structure, description: str | None = None, optional: bool = False, default: Any | None = None)
Bases:
objectBase class for a node of the schema
- copy()
Deep copy of the schema.
- default: Any = None
- delete(path)
Remove a node from an exisisting schema.
The original object is not altered, but a new schema is created instead with the same nodes (except for the deletion).
- Parameters:
path – a tuple of
strandintto indicate the place to modify. Use the names of record fields and the index of tuple fields and unions alternatives.- Returns:
a fresh schema of the same structure as
selfwith due modifications.
Example:
>>> s1 = Node.of_record({ "foo": Node.of_tuple([ Node.of_atom(AtomType.INT, description="width"), Node.of_atom(AtomType.INT, description="height"), Node.of_atom(AtomType.INT, description="depth"), ]), "bar": Node.of_atom(AtomType.STR), }) >>> s2 = s1.delete(("foo", 2)) >>> describe(s1) foo: - width value of type int - height value of type int - depth value of type int bar: value of type str >>> describe(s2) foo: - width value of type int - height value of type int bar: value of type str
- description: str | None = None
- inject(path, elem)
Inject new nodes or replace nodes in an exisisting schema.
The original object is not altered, but a new schema is created instead with the same nodes (except for the injection).
- Parameters:
path – a tuple of
strandintto indicate the place to modify. Use the names of record fields and the index of tuple fields and unions alternatives.elem – the element to insert at
path
- Returns:
a fresh schema of the same structure as
selfwith due modifications.
Example:
>>> s1 = Node.of_record({ "foo": Node.of_tuple([ Node.of_atom(AtomType.INT, description="width"), Node.of_atom(AtomType.INT, description="height"), ]), "bar": Node.of_atom(AtomType.STR), }) >>> s2 = s1.inject(("foo", 2), Node.of_atom(AtomType.INT, description="depth")) >>> describe(s1) foo: - width value of type int - height value of type int bar: value of type str >>> describe(s2) foo: - width value of type int - height value of type int - depth value of type int bar: value of type str
- classmethod of_collection(element, **kwargs)
Constructor for a
Collectionnode.- Parameters:
element – the
Nodeinstance describing an element of the collection.**kwargs – kw parameters for
Node.__init__.
- classmethod of_map(element, **kwargs)
Constructor for a
Mapnode.- Parameters:
element – the
Nodeinstance describing an element of the map.**kwargs – kw parameters for
Node.__init__.
- classmethod of_record(fields, **kwargs)
Constructor for a
Recordnode.- Parameters:
fields – a
dictofstroptions – options for
Atomconstructor.**kwargs – kw parameters for
Node.__init__.
- classmethod of_tuple(elements, **kwargs)
Constructor for a
Tuplenode.- Parameters:
elements – list of
Nodedescribing the elements of the tuple.**kwargs – kw parameters for
Node.__init__.
- classmethod of_union(options, **kwargs)
Constructor for a
Unionnode.- Parameters:
options – list of
Nodeinstances for the alternatives.**kwargs – kw parameters for
Node.__init__.
- optional: bool = False
- class pyskema.schema.Record(fields: Dict[str, Node])
Bases:
StructureA key-value pair collection.
Keys are from a defined set and values are defined per-key. Keys are supposed to be valid identifiers.
- accept(visitor, *args, **kwargs)
- classmethod structure_default()
- class pyskema.schema.Structure
Bases:
objectAbstract structure of a node
- abstract accept(visitor, *args, **kwargs)
- classmethod structure_default()
pyskema.template module
Generate template data from a schema.
- class pyskema.template.Templater
Bases:
VisitorVisitor implementing templating.
You should probably be using
template()instead.- template_node(value)
- visit_atom(atom)
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection)
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map)
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union)
Visit an
pyskema.schema.Unioninstance.
- pyskema.template.template(schema)
Generate a template from
schema
pyskema.validate module
Implement validation of data against a schema.
- exception pyskema.validate.InvalidDataError(mismatch)
Bases:
ValueErrorException signaling failure to validate data.
- class pyskema.validate.Mismatch(where: List[str], what: object, why: str)
Bases:
objectInfos on a mismatch between the schema and a piece of data.
- from_(where)
- what: object
- where: List[str]
- why: str
- class pyskema.validate.Mismatches(errors)
Bases:
objectA collection of mismatches between schema and data.
- from_(where)
- class pyskema.validate.Validator
Bases:
VisitorVisitor implementing validation.
You should probably be using
validate()instead.- classmethod validate(schema, data)
validate data against schema.
- Parameters:
schema – instance of Node
data – the data to validate
- visit_atom(atom, data) Result
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection, data) Result
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map_, data) Result
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec, data) Result
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, data) Result
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, data) Result
Visit an
pyskema.schema.Unioninstance.
- pyskema.validate.error_message(mismatch, no_loc=False)
Format the error message for
InvalidDataError.
- pyskema.validate.validate(data, schema, fail=True)
Validate a piece of data against a schema.
- Parameters:
data – a piece of data to validate.
schema – a schema to refer to
fail –
(optional,
True)if
True, raise anInvalidDataErrorwhen failing to validate dataelse, return None
pyskema.visitor module
Schema base visitor.
pyskema uses the visitor patter
to implement various operations on schema.
This module implement the base class that should be extended to implement such operation.
- class pyskema.visitor.CopyingVisitor
Bases:
VisitorA base for non mutating visitors.
Similar to Visitor, but it should create an entirely new tree.
- visit_atom(atom, *args, **kwargs)
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection, *args, **kwargs)
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map_, *args, **kwargs)
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec, *args, **kwargs)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, *args, **kwargs)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, *args, **kwargs)
Visit an
pyskema.schema.Unioninstance.
- class pyskema.visitor.Visitor
Bases:
objectThe base visitor.
- visit_atom(atom, *args, **kwargs)
Visit an
pyskema.schema.Atominstance.
- visit_collection(collection, *args, **kwargs)
Visit a
pyskema.schema.Collectioninstance.
- visit_map(map_, *args, **kwargs)
Visit a
pyskema.schema.Mapinstance.
- visit_record(rec, *args, **kwargs)
Visit a
pyskema.schema.Recordinstance.
- visit_tuple(tup, *args, **kwargs)
Visit a
pyskema.schema.Tupleinstance.
- visit_union(union, *args, **kwargs)
Visit an
pyskema.schema.Unioninstance.
Module contents
A schema definition module for validation and documentation of structured data.