expressions #
Abstraction on expressions, and contexts in which to evaluate them.
BinOp #
BinOp(left: T | Expr[T], op: ast.operator, right: T | Expr[T], **kwargs: Unpack[_Attributes])
A binary operation (like addition or division).
op is the operator, and left and right are any expression nodes.
Source code in src/app_model/expressions/_expressions.py
449 450 451 452 453 454 455 456 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
BoolOp #
BoolOp(op: ast.boolop, values: Sequence[ConstType | Expr], **kwargs: Unpack[_Attributes])
A boolean operation, 'or' or 'and'.
op is Or or And. values are the values involved. Consecutive operations
with the same operator, such as a or b or c, are collapsed into one node
with several values.
This doesn't include not, which is a UnaryOp.
Source code in src/app_model/expressions/_expressions.py
469 470 471 472 473 474 475 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
Compare #
Compare(left: Expr, ops: Sequence[ast.cmpop], comparators: Sequence[Expr], **kwargs: Unpack[_Attributes])
A comparison of two or more values.
left is the first value in the comparison, ops the list of operators,
and comparators the list of values after the first element in the
comparison.
Source code in src/app_model/expressions/_expressions.py
431 432 433 434 435 436 437 438 439 440 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
Constant #
Constant(value: V, kind: str | None = None, **kwargs: Unpack[_Attributes])
A constant value.
Parameters:
-
value(V) –the Python object this constant represents. Types supported: NoneType, str, bytes, bool, int, float
-
kind(str | None, default:None) –The kind of constant. This is used to provide type hints when
Source code in src/app_model/expressions/_expressions.py
414 415 416 417 418 419 420 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
Context #
Context(*maps: MutableMapping)
Bases: ChainMap
Evented Mapping of keys to values.
Source code in src/app_model/expressions/_context.py
33 34 35 36 37 | |
buffered_changes #
buffered_changes() -> Iterator[None]
Context in which to accumulated changes before emitting.
Source code in src/app_model/expressions/_context.py
39 40 41 42 43 | |
new_child #
new_child(m: MutableMapping | None = None) -> Context
Create a new child context from this one.
Source code in src/app_model/expressions/_context.py
57 58 59 60 61 | |
ContextKey #
ContextKey(default_value: T | __missing = MISSING, description: str | None = None, getter: Callable[[A], T] | None = None, *, id: str = '')
Context key name, default, description, and getter.
This is intended to be used as class attribute in a ContextNamespace.
This is a subclass of Name, and is therefore usable in an Expression.
(see examples.)
Parameters:
-
default_value(Any, default:MISSING) –The default value for this key, by default MISSING
-
description(str, default:None) –Description of this key. Useful for documentation, by default None
-
getter(callable, default:None) –Callable that receives an object and retrieves the current value for this key, by default None. For example, if this ContextKey represented the length of some list, (like the layerlist) it might look like
length = ContextKey(0, 'length of the list', lambda x: len(x)) -
id(str, default:'') –Explicitly provide the
Namestring used when evaluating a context, by default the key will be taken as the attribute name to which this object is assigned as a class attribute:
Examples:
>>> class MyNames(ContextNamespace):
... some_key = ContextKey(0, "some description", lambda x: sum(x))
>>> expr = MyNames.some_key > 5 # create an expression using this key
these expressions can be later evaluated with some concrete context.
>>> expr.eval({"some_key": 3}) # False
>>> expr.eval({"some_key": 6}) # True
Source code in src/app_model/expressions/_context_keys.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
info
classmethod
#
info() -> list[ContextKeyInfo]
Return list of all stored context keys.
Source code in src/app_model/expressions/_context_keys.py
114 115 116 117 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
ContextKeyInfo #
Bases: NamedTuple
Just a recordkeeping tuple.
Retrieve all declared ContextKeys with ContextKeyInfo.info().
ContextNamespace #
ContextNamespace(context: MutableMapping)
Bases: Generic[A]
A collection of related keys in a context.
meant to be subclassed, with ContextKeys as class attributes.
Source code in src/app_model/expressions/_context_keys.py
197 198 199 200 201 202 203 204 205 206 207 208 209 | |
dict #
dict() -> dict
Return all keys in this namespace.
Source code in src/app_model/expressions/_context_keys.py
225 226 227 | |
reset #
reset(key: str) -> None
Reset keys to its default.
Source code in src/app_model/expressions/_context_keys.py
211 212 213 214 215 216 217 218 | |
reset_all #
reset_all() -> None
Reset all keys to their defaults.
Source code in src/app_model/expressions/_context_keys.py
220 221 222 223 | |
Expr #
Expr(*args: Any, **kwargs: Any)
Base Expression class providing dunder and convenience methods.
This is a subclass of ast.AST that provides rich dunder methods that
facilitate joining and comparing typed expressions. It only implements a
subset of ast Expressions (for safety of evaluation), but provides more
than ast.literal_eval.
Expressions that are supported:
- Names: 'myvar' (these must be evaluated along with some context)
- Constants: '1'
- Comparisons: 'myvar > 1'
- Boolean Operators: 'myvar & yourvar' (bitwise
&and|are overloaded here to mean booleanandandor) - Binary Operators: 'myvar + 42' (includes
//,@,^) - Unary Operators: 'not myvar'
Things that are NOT supported:
- attribute access: 'my.attr'
- calls: 'f(x)'
- containers (lists, tuples, sets, dicts)
- indexing or slicing
- joined strings (f-strings)
- named expressions (walrus operator)
- comprehensions (list, set, dict, generator)
- statements & assignments (e.g. 'a = b')
This class is not meant to be instantiated directly. Instead, use
parse_expression, or the
Expr.parse classmethod to create an expression
instance.
Once created, an expression can be joined with other expressions, or constants.
Examples:
>>> expr = parse_expression("myvar > 5")
combine expressions with operators
>>> new_expr = expr & parse_expression("v2")
nice repr
>>> new_expr
BoolOp(
op=And(),
values=[
Compare(
left=Name(id='myvar', ctx=Load()),
ops=[
Gt()],
comparators=[
Constant(value=5)]),
Name(id='v2', ctx=Load())])
evaluate in some context
>>> new_expr.eval(dict(v2="hello!", myvar=8))
'hello!'
you can also use keyword arguments. This is slightly slower
>>> new_expr.eval(v2="hello!", myvar=4)
serialize
>>> str(new_expr)
'myvar > 5 and v2'
One reason you might want to use this object is to capture named expressions that can be evaluated repeatedly as some underlying context changes.
light_is_green = Name[bool]("light_is_green")
count = Name[int]("count")
is_ready = light_is_green & count > 5
assert is_ready.eval({"count": 4, "light_is_green": True}) == False
assert is_ready.eval({"count": 7, "light_is_green": False}) == False
assert is_ready.eval({"count": 7, "light_is_green": True}) == True
this will also preserve type information:
>>> reveal_type(is_ready()) # revealed type is `bool`
Source code in src/app_model/expressions/_expressions.py
192 193 194 195 196 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
IfExp #
IfExp(test: Expr, body: Expr, orelse: Expr, **kwargs: Unpack[_Attributes])
An expression such as 'a if b else c'.
body if test else orelse
Source code in src/app_model/expressions/_expressions.py
496 497 498 499 500 501 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
Name #
Name(id: str, ctx: ast.expr_context = LOAD, *, bound: type[T] | None = None, **kwargs: Unpack[_Attributes])
A variable name.
Parameters:
-
id(str) –The name of the variable.
-
bound(Any | None, default:None) –The type of the variable represented by this name (i.e. the type to which this name evaluates to when used in an expression). This is used to provide type hints when evaluating the expression. If
None, the type is not known.
Source code in src/app_model/expressions/_expressions.py
388 389 390 391 392 393 394 395 396 397 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
UnaryOp #
UnaryOp(op: ast.unaryop, operand: Expr, **kwargs: Unpack[_Attributes])
A unary operation.
op is the operator, and operand any expression node.
Source code in src/app_model/expressions/_expressions.py
484 485 486 487 | |
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in src/app_model/expressions/_expressions.py
319 320 321 | |
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in src/app_model/expressions/_expressions.py
323 324 325 | |
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context.
Source code in src/app_model/expressions/_expressions.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
in_ #
in_(other: Any) -> Compare
Return a comparison for self in other.
Source code in src/app_model/expressions/_expressions.py
280 281 282 283 | |
not_in #
not_in(other: Any) -> Compare
Return a comparison for self no in other.
Source code in src/app_model/expressions/_expressions.py
285 286 287 | |
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in src/app_model/expressions/_expressions.py
219 220 221 222 223 224 225 226 | |
app_model_context #
app_model_context() -> AppModelContextDict
A set of useful global context keys to use.
Source code in src/app_model/expressions/_context.py
158 159 160 161 162 163 164 | |
create_context #
create_context(obj: object, max_depth: int = 20, start: int = 2, root: Context | None = None, root_class: type[Context] = Context, frame_predicate: Callable[[FrameType], bool] = _pydantic_abort) -> Context
Create context for any object.
Parameters:
-
obj(object) –Any object
-
max_depth(int, default:20) –Max frame depth to search for another object (that already has a context) off of which to scope this new context. by default 20
-
start(int, default:2) –first frame to use in search, by default 2
-
root(Optional[Context], default:None) –Root context to use, by default None
-
root_class(type[Context], default:Context) –Root class to use when creating a global root context, by default Context The global context is used when root is None.
-
frame_predicate(Callable[[FrameType], bool], default:_pydantic_abort) –Callback that can be used to abort context creation. Will be called on each frame in the stack, and if it returns True, the context will not be created. by default, uses pydantic-specific function to determine if a new pydantic BaseModel is being declared, (which means that the context will never be used)
lambda frame: frame.f_code.co_name in ("__new__", "_set_default_and_type")
Returns:
-
Optional[Context]–Context for the object, or None if no context was found
Source code in src/app_model/expressions/_context.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
get_context #
get_context(obj: object) -> Context | None
Return context for any object, if found.
Source code in src/app_model/expressions/_context.py
153 154 155 | |
parse_expression #
parse_expression(expr: Expr | str) -> Expr
Parse string expression into an Expr instance.
Parameters:
Returns:
-
Expr–Instance of
Expr.
Raises:
-
SyntaxError–If the provided string is not an expression (e.g. it's a statement), or if it uses any forbidden syntax components (e.g. Call, Attribute, Containers, Indexing, Slicing, f-strings, named expression, comprehensions.)
Source code in src/app_model/expressions/_expressions.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
safe_eval #
safe_eval(expr: str | bool | Expr, context: Mapping | None = None) -> Any
Safely evaluate expr string given context dict.
This lets you evaluate a string expression with broader expression
support than ast.literal_eval, but much less support than eval().
It also supports booleans (which are returned directly), and Expr instances,
which are evaluated in the given context.
Parameters:
-
expr(str | bool | Expr) –Expression to evaluate. If
expris a string, it is parsed into anExprinstance. If abool, it is returned directly. -
context(Mapping | None, default:None) –Context (mapping of names to objects) to evaluate the expression in.
Source code in src/app_model/expressions/_expressions.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |