pygritia package

Module contents

Pygritia: Lazy Evaluation

  • Build expressions naturally
  • Evaluate expression with symbol substitution
  • Create property by a expression with special symbol
>>> class Spam:
...     egg = [42]
...     foo = this.egg[0]
>>> spam = Spam()
>>> print(this.egg * 2)
this.egg * 2
>>> evaluate(this.egg * 2, {this: spam})
[42, 42]
>>> spam.foo
42
>>> spam.foo = 9
>>> spam.egg
[9]
pygritia.evaluate(expr: _T, namespace: Mapping[Union[str, LazyMixin], Any]) → _T[source]

Evaluate lazy expression

Evaluate expression with symbol substitution according to given namespace

Parameters:
  • expr

    Lazy expression or evaluated value

    If given value is not a lazy expression, this function returns it immediately

  • namespace

    Symbol table which will be used in substitution

    The key of table can be both of string and symbol expression

Returns:

Evaluated value

pygritia.lazy_call(func: _T) → _T[source]

Make a function to support lazy expression

>>> from pygritia import this, lazy_call, evaluate
>>> @lazy_call
... def hello(obj):
...     print(obj)
>>> print(hello(this))
hello(this)
>>> hello("123")
123
>>> evaluate(hello(this), {this: "123"})
123
pygritia.lazy_delattr(obj, name)

Deletes the named attribute from the given object.

delattr(x, ‘y’) is equivalent to ``del x.y’‘

pygritia.lazy_delitem(a, b)

Same as del a[b].

pygritia.lazy_getattr()

getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, ‘y’) is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn’t exist; without it, an exception is raised in that case.

pygritia.lazy_getitem(a, b)

Same as a[b].

pygritia.lazy_setattr(obj, name, value)

Sets the named attribute on the given object to the specified value.

setattr(x, ‘y’, v) is equivalent to ``x.y = v’‘

pygritia.lazy_setitem(a, b, c)

Same as a[b] = c.

pygritia.symbol(name: str) → Any[source]

Create symbol for lazy expression

Returns:Newly created symbol expression
Return type:Any
pygritia.update(expr: _T, val: _T, namespace: Mapping[Union[str, LazyMixin], Any]) → None[source]

Update the value of lazy expression

Set the value of lazy expression to the given val Only assignable expression can be updated. ex) this[3], this.spam

Parameters:
  • expr – Assignable lazy expression
  • val – New value
  • namespace

    Symbol table which will be used in substitution

    The key of table can be both of string and symbol expression

pygritia.this
pygritia.If(cond: bool, value: _T) → Optional[_T][source]

value if cond else None

pygritia.IfThenElse(cond: bool, true: _T, false: _U) → Union[_T, _U][source]

true if cond else false

pygritia.Case(cond: _T, cases: Mapping[_T, _U], default: _U) → _U[source]

cases.get(cond, default)

pygritia.Ensure(obj: Optional[_T], default: _T) → _T[source]

obj if obj is not None else default

pygritia.is_none(obj: Any) → bool[source]

obj is None

pygritia.lazy_cast(target_type: Callable[[_T], _U], expr: _T, expr_type: Optional[Callable[[_U], _T]] = None) → _U[source]

Create automatic casting lazy expression

Parameters:
  • target_type – Converter from expression type to result type
  • expr – Target expression
  • expr_type

    Converter from result type to expression type

    If it is omitted, the constructor of evaluated expr will be used.

Returns:

Casting expression