Generalities of expressions
This section explains the concept of “expression” in general terms and in the context of PathEx.
See also
- Document Expressions general semantics
A more rigorous explanation of the meaning of expressions.
- Class
Expression
Abstract base class of expressions.
An expression is a combination of symbols that complies with a set of structural rules. An expression is uniquely associated with a composition of functions over a universe of formal languages. That is, an expression generates a formal language. A formal language is a set of words and a word is a finite sequence of letters 1.
For example, the expression (a|b)c, generates sequences which two elements. The first element is either a or b and the second is c. In PathEx idiom this may be expressed as:
>>> from pathex.expressions.aliases import *
>>> expression = U('ab') + 'c'
>>> assert expression.get_language() == {'ac', 'bc'}
In this case U
is a short alias
of Union
and the overloaded operation +
that express Concatenation
is being used to construct the desired expression. Method get_language
, by default, gives the words as str
objects.
The previous example shows a case that generates a finite language. However, there are expressions which language is infinite. For example, the expression (ab)* generates a language which elements are the empty word or ab several times repeated. In PathEx idiom this may be expressed as:
>>> from pathex.expressions.aliases import *
>>> from pathex.adts.util import take
>>> expression = C('ab')*...
>>> assert {''.join(str(l) for l in w) for w in take(5, expression.get_eager_generator())} == \
... {'', 'ab', 'abab', 'ababab', 'abababab'}
>>> assert {''.join(str(l) for l in w) for w in take(7, expression.get_eager_generator())} == \
... {'', 'ab', 'abab', 'ababab', 'abababab', 'ababababab', 'abababababab'}
In this case C
is a short alias
of Concatenation
, and the overloaded operator *
with operand ...
is used to construct an unbounded repetition. Because the generated language is infinite, helper function take()
has being used to select specific amounts of words. The words are taken from the generator returned by get_eager_generator()
.
There are various kinds of expressions, which general documentation may be found in the following list:
Footnotes
- 1
In other contexts, words are also called “strings”. In these documents it is used the noun “word” to avoid confusion with
str
which is meant to represent strings in a specific context and with a slightly –but importantly– different semantic. Specifically in PathEx a letter may be any instance ofobject
, so the set of all possible words is more diverse than the set of all possible strings (akastr
instances).