expressions #
Abstraction on expressions, and contexts in which to evaluate them.
BinOp #
BinOp(left: T | Expr[T], op: ast.operator, right: T | Expr[T], **k: Any)
A binary operation (like addition or division).
op
is the operator, and left
and right
are any expression nodes.
Source code in app_model/expressions/_expressions.py
416 417 418 419 420 421 422 423 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
BoolOp #
BoolOp(op: ast.boolop, values: Sequence[ConstType | Expr], **kwargs: Any)
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 app_model/expressions/_expressions.py
436 437 438 439 440 441 442 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
Compare #
Compare(left: Expr, ops: Sequence[ast.cmpop], comparators: Sequence[Expr], **kwargs: Any)
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 app_model/expressions/_expressions.py
395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
Constant #
Constant(value: V, kind: str | None = None, **kwargs: Any)
A constant value.
The value
attribute contains the Python object it represents.
types supported: NoneType, str, bytes, bool, int, float
Source code in app_model/expressions/_expressions.py
380 381 382 383 384 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
Context #
Context(*maps: MutableMapping)
Bases: ChainMap
Evented Mapping of keys to values.
Source code in app_model/expressions/_context.py
31 32 33 34 35 |
|
buffered_changes #
buffered_changes() -> Iterator[None]
Context in which to accumulated changes before emitting.
Source code in app_model/expressions/_context.py
37 38 39 40 41 |
|
new_child #
new_child(m: MutableMapping | None = None) -> Context
Create a new child context from this one.
Source code in app_model/expressions/_context.py
55 56 57 58 59 |
|
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
Name
string 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 app_model/expressions/_context_keys.py
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 app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
info
classmethod
#
info() -> list[ContextKeyInfo]
Return list of all stored context keys.
Source code in 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 app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
ContextKeyInfo #
Bases: NamedTuple
Just a recordkeeping tuple.
Retrieve all declared ContextKeys with ContextKeyInfo.info().
Parameters:
-
key
(ForwardRef('str')
, default:None
) – -
type
(ForwardRef('type | None')
, default:None
) – -
description
(ForwardRef('str | None')
, default:None
) – -
namespace
(ForwardRef('builtins.type[ContextNamespace] | None')
, default:None
) –
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 app_model/expressions/_context_keys.py
195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
dict #
dict() -> dict
Return all keys in this namespace.
Source code in app_model/expressions/_context_keys.py
223 224 225 |
|
reset #
reset(key: str) -> None
Reset keys to its default.
Source code in app_model/expressions/_context_keys.py
209 210 211 212 213 214 215 216 |
|
reset_all #
reset_all() -> None
Reset all keys to their defaults.
Source code in app_model/expressions/_context_keys.py
218 219 220 221 |
|
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 booleanand
andor
) - 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 app_model/expressions/_expressions.py
178 179 180 181 182 183 184 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
IfExp #
IfExp(test: Expr, body: Expr, orelse: Expr, **kwargs: Any)
An expression such as 'a if b else c'
.
body
if test
else orelse
Source code in app_model/expressions/_expressions.py
461 462 463 464 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
Name #
Name(id: str, ctx: ast.expr_context = LOAD, **kwargs: Any)
A variable name.
id
holds the name as a string.
Source code in app_model/expressions/_expressions.py
366 367 368 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
UnaryOp #
UnaryOp(op: ast.unaryop, operand: Expr, **kwargs: Any)
A unary operation.
op
is the operator, and operand
any expression node.
Source code in app_model/expressions/_expressions.py
451 452 |
|
bitand #
bitand(other: T | Expr[T]) -> BinOp[T]
Return bitwise self & other.
Source code in app_model/expressions/_expressions.py
302 303 304 |
|
bitor #
bitor(other: T | Expr[T]) -> BinOp[T]
Return bitwise self | other.
Source code in app_model/expressions/_expressions.py
306 307 308 |
|
eval #
eval(context: Mapping[str, object] | None = None, **ctx_kwargs: object) -> T
Evaluate this expression with names in context
.
Source code in app_model/expressions/_expressions.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
in_ #
in_(other: Any) -> Compare
Return a comparison for self
in other
.
Source code in app_model/expressions/_expressions.py
263 264 265 266 |
|
not_in #
not_in(other: Any) -> Compare
Return a comparison for self
no in other
.
Source code in app_model/expressions/_expressions.py
268 269 270 |
|
parse
classmethod
#
parse(expr: str) -> Expr
Parse string into Expr (classmethod).
see docstring of parse_expression
for details.
Source code in app_model/expressions/_expressions.py
202 203 204 205 206 207 208 209 |
|
app_model_context #
app_model_context() -> AppModelContextDict
A set of useful global context keys to use.
Source code in app_model/expressions/_context.py
156 157 158 159 160 161 162 |
|
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 app_model/expressions/_context.py
80 81 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 |
|
get_context #
get_context(obj: object) -> Context | None
Return context for any object, if found.
Source code in app_model/expressions/_context.py
151 152 153 |
|
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 app_model/expressions/_expressions.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
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
expr
is a string, it is parsed into anExpr
instance. 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 app_model/expressions/_expressions.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|