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.

Source code

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:

Python 3.11.9 <...>
>>> from ideas import ideas_state
>>> ideas_state.verbose_finder = True
>>> ideas_state.verbose = True
>>> from ideas.included import french
>>> french.add_hook()
Added hook ideas.included.french
Looking for files with extensions:  ['.pyfr']
The following paths will not be included in the search:
~/AppData/Local/Programs/Python/Python311/Lib C:\Users\Andre\AppData\Local\Programs\Python\Python311\Lib
~/github/token-utils/src/token_utils C:\Users\Andre\github\token-utils\src\token_utils
src/ideas C:\Users\Andre\github\ideas\src\ideas
<Ideas import hook: ideas.included.french>
>>> import mon_programme

ideas.included.french.find_spec():
No search paths were specified.
Will use the current directory as well as paths included in sys.path
These are the potential search paths
    docs_examples/french

    ~/AppData/Local/Programs/Python/Python311/python311.zip
    ~/AppData/Local/Programs/Python/Python311/DLLs
    ~/AppData/Local/Programs/Python/Python311/Lib
    ~/AppData/Local/Programs/Python/Python311
    ~/github/common_venv
    ~/github/common_venv/Lib/site-packages
    src
    ~/github/token-utils/src

The following have been set as 'excluded' for this import hook.
    ~/AppData/Local/Programs/Python/Python311/Lib
    ~/github/token-utils/src/token_utils
    src/ideas

These are the remaining search paths:
    docs_examples/french

    ~/AppData/Local/Programs/Python/Python311/python311.zip
    ~/AppData/Local/Programs/Python/Python311/DLLs
    ~/AppData/Local/Programs/Python/Python311
    ~/github/common_venv
    ~/github/common_venv/Lib/site-packages
    src
    ~/github/token-utils/src
FOUND 'C:\Users\Andre\github\ideas\docs_examples\french\mon_programme.pyfr'

ideas.included.french.find_spec():
<IdeasMetaPathFinder for ideas.included.french> cannot find 'unicodedata'
>>> carré(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'carré' is not defined

>>> mon_programme.carré(4)
16

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.