The standardaction module#
This module defines StandardAction.
A StandardAction is a singleton object. Instantiate a StandardAction with a string name. The same name actually returns the same object:
>>> from parce.standardaction import StandardAction
>>> StandardAction('test') is StandardAction('test')
True
A StandardAction stores its name in the _name
attribute. Accessing any
attribute (without underscore) creates that attribute as a new instance, with
the current instance in the _parent
attribute:
>>> Name = StandardAction('Name')
>>> Name.Function is Name.Function
True
>>> Name.Function._parent
Name
This way a new action type can be created that shares its parent with other types, a concept borrowed from pygments. An example:
>>> Comment = StandardAction("Comment")
>>> Literal = StandardAction("Literal")
>>> String = Literal.String
>>> String.DoubleQuotedString
Literal.String.DoubleQuotedString
>>> String.SingleQuotedString
Literal.String.SingleQuotedString
StandardAction instances support iteration and membership methods. Iteration yields the instance ifself and then the parents:
>>> for i in String.DoubleQuoted:
... print(i)
...
Literal.String.DoubleQuoted
Literal.String
Literal
And the in
operator returns True when a standard action belongs to
another one, i.e. the other one is one of the ancestors of the current action:
>>> String.DoubleQuotedString in String
True
>>> Literal in String
False
The in
operator also works with a string:
>>> 'String' in Literal.String.DoubleQuoted
True
>>> 'Literal' in String
True
The last one could be surprising, but String is defined as Literal.String
:
>>> String
Literal.String
Finally, the & operator returns the common ancestor, if any:
>>> String & Number
Literal
>>> String & Text
>>>
See for the full list of pre-defined standard actions Standard actions.