λ encoding
Warning
The following is just a quick first draft. The code shown here might be completely out of date and not work as written. As of version 0.0.17, the console example shown below did work.
Suppose we want to run a program that has a custom encoding;
in this case, we use λ
to represent Python’s lambda
keyword:
# coding: lambda-encoding
square = λ x: x**2
assert square(3) == 9
print("Using lambda-encoding: λ") # λ is not converted inside strings
if __name__ == '__main__':
print("The square of 5 is", square(5))
Before we can do this, we need to make Python aware of the existence of our custom encoding. This can be done by having the following program:
# usercustomize.py
from ideas.examples import lambda_codec # noqa
print(" --> usercustomize.py was executed")
By setting PYTHONPATH
to a directory where a program named
usercustomize.py
is found, any such program is automatically run before
the main script.
As we can see, when we run it, the source transformation is done correctly.
TESTS:\lambda_encoding
$ set PYTHONPATH=%CD%
TESTS:\lambda_encoding
$ python short_program.py
--> usercustomize.py was executed
Using lambda-encoding: λ
The square of 5 is 25
We can also use the ideas console and have our special encoding be used.:
>>> from ideas.examples import lambda_codec
>>> from ideas import console
>>> console.start()
Configuration values for the console:
transform_source from ideas.examples.lambda_codec
--------------------------------------------------
Ideas Console version 0.0.17. [Python version: 3.7.3]
~>> sq = λ x: x*x
~>> sq(3)
9
lambda_codec.py
This codec replaces any Python identifier (token) represented by the single Greek letter ‘λ’ by the corresponding string ‘lambda’ which is the Python keyword.
The source is assumed to be actually encoded in utf-8.
lambda_encoding.py
This codec replaces any Python identifier (token) represented by the single Greek letter ‘λ’ by the corresponding string ‘lambda’ which is the Python keyword.
The source is assumed to be actually encoded in utf-8.