pygritia.core module

Core functionality for lazy expressions

class pygritia.core.LazyAction[source]

Bases: object

Lazy Expression Handler

Every derived LazyAction classes must implements evaluate() method. It is called by evaluate() function with given namespace

evaluate(namespace: Mapping[Union[str, LazyMixin], Any]) → Any[source]

Evaluate expression

To substitute actual value for specific symbol, give value with keyword argument.

owner

Owner lazy expression of this action

update(val: Any, namespace: Mapping[Union[str, LazyMixin], Any]) → None[source]

Update value of expression

If the expression is readonly, it raises AttributeError

class pygritia.core.LazyMeta[source]

Bases: type

Metaclass for Lazy

It is for holding global Lazy factory and creation

classmethod create(action: pygritia.core.LazyAction, origin: Optional[LazyMixin] = None) → pygritia.core.LazyMixin[source]

Create new lazy expression with given action by default lazy factory

Parameters:
  • action (LazyAction) – Evaluation/update handler for new lazy expression
  • origin (Optional[LazyMixin]) – Lazy expression which cause creation of new expression
Returns:

Newly created lazy expression

Return type:

LazyMixin

classmethod register_factory(lazy_factory: Type[pygritia.core.LazyMixin]) → Type[pygritia.core.LazyMixin][source]

Set global default Lazy factory to lazy_factory

Parameters:lazy_factory (Type[Lazy]) – New default lazy factory
Returns:Old default lazy factory
Return type:Type[Lazy]
class pygritia.core.LazyMixin(action: pygritia.core.LazyAction, origin: Optional[LazyMixin] = None)[source]

Bases: object

Base class of all lazy expression and mixin classes

It provides Protocol for lazy expression

All derived classes of this class have __hash__ automatically, because namespace for evaluate is defined as lazy expression (normally symbol only) to value mapping. Key of mapping must be hashable.

pygritia.core.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.core.repr_(expr: Any) → str[source]

repr() for lazy expression

Native repr returns string like '<Lazy: this>'. If you want to get the repr string like other objects, use this instead of native one. It returns expression only for lazy expression and repr() for other object.

Parameters:expr – object for repr
Returns:repr string of given object
pygritia.core.setattr_

Implement setattr(self, name, value).

pygritia.core.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