metadsl
Library to help create DSLs in Python.
- class metadsl.CaptureLogging(events=<factory>)[source]
Capture all logging.
- with CaputureLogging() as results:
…
# results contains a list of all log messages, formatted
Some inspiration from unittest.assertLogs
- old_handlers: List[logging.Handler]
- class metadsl.Expression(function, args, kwargs)[source]
Top level object. Subclass this type and provide relevent methods for your type. Do not add any fields.
Properties:
Calling the function, after replacing the typevars in it (if it is a bound method), with the args and kwargs should resualt in an equivalent expression:
replace_fn_typevars(self.function, self.typevars)(*self.args, **self.kwargs) == self
The return type of the function, inferred by replacing the typevars in and with these args and kwargs, should match the type of the expression. If the return type of the function is not subclass of expression, then this should be a PlaceholderExpression of that type.
- function: Callable
- class metadsl.ExpressionReference(_graph, _optional_index)[source]
This is a data structure that holds onto a graph of expression and has a pointer to one node, to represent it.
- property children: metadsl.normalized.Children
Returns the direct children of this node if it has any.
- Return type
- property descendents: Iterable[metadsl.normalized.ExpressionReference]
Returns all nodes of this expression reference. If any of them get their expression replaced, it will replace that sub-node of this expression.
If this is the root node, will return them in topo order, otherwise will be arbitrary
- Return type
- metadsl.Hash(x)
- class metadsl.PlaceholderExpression(function, args, kwargs)[source]
An expression that represents a type of T, for example T could be int.
This is needed when a function returns a non expression type, it still has to return an expression under the covers until it has been replaced.
It is also needed when using Wildcards in expressions when doing matching.
- function: Callable
- metadsl.create_iterated_placeholder(i)[source]
If a placeholder is of an iterable T, calling iter on it should return an iterable placeholder of the inner type.
Used in matching with variable args.
- Return type
- metadsl.export_from(module_name, *submodules, local=[])[source]
Sets the __all__ for the module to be the union of all the __all__’s of the submodules, plus the locals.
Needed so that the modules themselves are not present in the __init__.py file, but all the exports are.
- Return type
- metadsl.expression(fn)[source]
Creates an expresion object by wrapping a Python function and providing a function that will take in the args and return an expression of the right type.
- metadsl.hash_value(value)[source]
Computes some hash for a value that should be stable. Either use the built in hash, or if we cannot (like the object is mutable) then use the id.
It’s a single dispatch function so that you can register custom hashes for objects you don’t control.
- Return type