Summary
This is a source transformation that allows one to use French equivalent keywords to the existing ones. It also demonstrates how to make use of a different file extension (
.pyfr), loading such files as if they were regular Python files.
French Python
Imagine you are a French beginner who has learned the basics of programming using a block-based environment such as Scratch or Blockly. All the text shown on these blocks was in French, the only language you know. You now want to do a transition to actually writing code in an editor, instead of putting predefined blocks together. It would be so much easier if you could use a version of Python where the keywords were in French, with most of them being identical to what you were using in the block-based environment. This is what this import hook example allows one to do.
Let’s see it in action:
> py -m ideas -a french --show
Ideas Console version 0.2.0. [Python version: 3.11.9]
ideas> pourchaque lettre dans 'Bonjour':
... afficher(lettre)
...
===========Transformed============
for lettre in 'Bonjour':
print(lettre)
-----------------------------
B
o
n
j
o
u
r
ideas>
Importing .pyfr files
Suppose we have the following two files in the usage_demo folder:
# my_program.py
print("Wrong one")
raise ImportError
and
# my_program.pyfr
afficher("Bonjour !")
Let’s see if we attempt to import my_program after
setting up the french import hook and enabling the
verbose finder:
(venv-ideas3.11) C:\Users\Andre\github\ideas
> py
Python 3.11.9...
>>> from ideas import current_state
>>> current_state.verbose = True
>>> from ideas.examples import french
>>> french.add_hook()
Added hook ideas.examples.french
Looking for files with extensions: ['.pyfr']
The following paths will not be included in the search:
PYTHON: c:\users\andre\appdata\local\programs\python\python311\lib
SITE-PACKAGES: c:\users\andre\github\ideas\venv-ideas3.11\lib\site-packages
IDEAS: c:\users\andre\github\ideas\ideas
<Ideas import hook: ideas.examples.french>
>>> from usage_demo import my_program
Searching for ~\github\ideas\usage_demo.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Searching for usage_demo.pyfr.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Searching for ~\AppData\Local\Programs\Python\Python311\python311.zip\usage_demo.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Searching for ~\AppData\Local\Programs\Python\Python311\DLLs\usage_demo.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Skipping over: PYTHON:
Searching for ~\AppData\Local\Programs\Python\Python311\usage_demo.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Searching for ~\github\ideas\venv-ideas3.11\usage_demo.pyfr
IdeasMetaFinder did not find usage_demo.pyfr
Skipping over: SITE-PACKAGES:
Searching for ~\github\ideas\usage_demo\my_program.pyfr
Found: ~\github\ideas\usage_demo\my_program.pyfr
Bonjour !
>>> import math
>>> math.pi
3.141592653589793
Caution
If you use two or more import hooks, only one of them will find your programs. If you have programs with different extensions, the order in which you add the import hooks may yield different results.