Renaming identifiers & debugging
Diez B. Roggisch
deets at nospam.web.de
Thu Feb 25 21:06:11 CET 2010
Am 25.02.10 20:27, schrieb Luca:
> Chris Rebert wrote:
>> On Thu, Feb 25, 2010 at 7:26 AM, Luca <lucat at despammed.com> wrote:
>>> Hello, i am trying to develop an application to teach programming to
>>> kids in a similar way as Logo did in the past. I would like to use an
>>> embedded Python as underlying language but this raises a problem.
>>> The target of my app are very young kids that might be unfamiliar with
>>> english, so i am wondering if there is a way to rename/redefine
>>> and messages in the language of the kid.
>>> In UCB-Logo this is very easy with the command
>>> COPYDEF "newidentifier "oldidentifier
>>> so all you have to do is setup a startup script to redefine all the
>>> identifiers to the language of the user.
>>> Is there anything similar for python? Since python would be embedded it
>>> would not be a problem for me to do it through some API.
>> It can certainly be done (c.f. ChinesePython -
>> but I know of no framework that simplifies the task. Essentially, you
>> just have to manually modify Python's parser by swapping out the
>> english for the other language (and if you want to mess with the basic
>> types' names, their name definitions somewhere else too). There also
>> might be encoding issues to deal with.
> Yes, i am playing with Python source code, i have changed some keywords
> but the compile-process fails when it tries to compile some of python
> libraries which, of course, use english keywords... so i guess this is
> not a very clean/viable solution (without having to rewrite several
> parts of the libraries). A better solution could be to "duplicate"
> keywords adding a translated version but leaving the original version in
> place so that every module keeps working. In few words, if i was going
> to translate the keyword "if" in, say, italian "se", then i would have
> both "if" and also "se" working at the same time, in the same manner.
> I think that the best way to do this is to insert a "filter" somewhere
> that converts every "se" keyword into a "if" keyword so that python
> doesn't even see the change.
> What i would like to do is add a new keyword (lets call it "copydef"
> like in UCBLogo or whatever) that does this work at runtime by keeping a
> substitution table in RAM. If i could manage to add this new keyword to
> python then it would be easy to write a startup script that translates
> the keywords once python is "up and running" and without breaking
> existing python programs/libraries (unless the new keyword conflicts
> with functions defined inside these programs/libraries).
You could use import-hooks for importing your code. There was a
python-magazine article a while ago that showed how to use that + a
parser to import seamlessly a DSL.
Using pyparsing to write a python-grammar on the fly that simply
exchanges the keywords before passing it to the interpreter is also easy.
But to be honest: I doubt it's worth the effort. Really. Teaching people
how to code, but in something that is *not* the "real" language is of
little, if any, benefit.
And also I don't think that your concerns are valid in general. Keywords
are like brandnames or other things - the stand for a concept, and
people immediatly accept them when they want them.
Much, much, much more important would - if anything - be a
standard-library, or a wrapper for that, e.g. for turtle-graphics, that
was written in terms of your desired language.
More information about the Python-list