Python changing keywords name

V

vedrandekovic

Hello AGAIN,

I on working on windows and Python 2.4. Where can I find and CHANGE
python
grammar. ( I just want to change the keywords )

PLEASE HELP ME
SOMEBODY!!!!!!

THANKS!!!!!!!!!!!!!!!!!
 
M

Michael Hoffman

Hello AGAIN,

I on working on windows and Python 2.4. Where can I find and CHANGE
python
grammar. ( I just want to change the keywords )

PLEASE HELP ME
SOMEBODY!!!!!!

THANKS!!!!!!!!!!!!!!!!!

This is the third time you have posted this today. Please stop.

Also, you might want to moderate the tone of your messages. Some people
might find a string of all caps and exclamation marks obnoxious. I think
it is less likely to get you help.

You may find this guide helpful
<http://www.catb.org/~esr/faqs/smart-questions.html>.
 
J

James Stroud

Hello AGAIN,

I on working on windows and Python 2.4. Where can I find and CHANGE
python
grammar. ( I just want to change the keywords )

PLEASE HELP ME
SOMEBODY!!!!!!

THANKS!!!!!!!!!!!!!!!!!

1. Download the source to python
2. Unpack the source
3. Change to the "Grammar" directory
5. Edit the "Grammar" file appropriately
6. Compile python
7. Your interpreter now has new keywords
8. Use at your own risk.

For example, I changed a print statent in this file from

print_stmt: 'print' ( [ test (',' test)* [','] ] |
'>>' test [ (',' test)+ [','] ] )

to

print_stmt: 'splodnik' ( [ test (',' test)* [','] ] |
'>>' test [ (',' test)+ [','] ] )

And here are the results:

Python 2.5 (r25:51908, Jun 23 2007, 14:42:05)
[GCC 4.1.1] on linux2
Type "help", "copyright", "credits" or "license" for more information. This is a splodnik statement.

You will also want to change every instance of the word "print" in every
..py file to "splodnik" in the pythonsource before building, so that you
won't break your standard lib. Also, no one else on the planet will be
using this version of python, so you will not be able to use any one
else's python code unless you replace keywords in their python source.

Now that you know how to do this, please don't ask again.

James
 
G

Gabriel Genellina

I on working on windows and Python 2.4. Where can I find and CHANGE
python
grammar. ( I just want to change the keywords )

Instead of changing Python grammar, you could convert your
"translated" source into "original" Python using the code below, and
compile and run as usual; you can also reverse the process. Also, this
lets you use the standard Python library and all other Python code
around the world.
Unlike a simple "search and replace", it understands the lexical
structure of a Python program, and won't replace a keyword inside a
quoted string, by example.
The core function is simple:

def translate_tokens(sourcefile, tdict):
for tok_num, tok_val, _, _, _ in
tokenize.generate_tokens(sourcefile.readline):
if tok_num==token.NAME: tok_val = tdict.get(tok_val, tok_val)
yield tok_num, tok_val

translated_code = tokenize.untokenize(
translate_tokens(StringIO(original_code), translation_dictionary))

Note that you may encounter problems if the original code uses some
names matching the translated keywords.

(I hope nobody will abuse this technique... Y perdón a los
hispanoparlantes por lo horrible de la traducción).

--- begin code ---
from cStringIO import StringIO
import token
import tokenize

# "spanished" Python source from the SimplePrograms wiki page
code_es = r"""
BOARD_SIZE = 8

clase BailOut(Exception):
pasar

def validate(queens):
left = right = col = queens[-1]
para r en reversed(queens[:-1]):
left, right = left-1, right+1
si r en (left, col, right):
lanzar BailOut

def add_queen(queens):
para i en range(BOARD_SIZE):
test_queens = queens +
intentar:
validate(test_queens)
si len(test_queens) == BOARD_SIZE:
retornar test_queens
else:
retornar add_queen(test_queens)
excepto BailOut:
pasar
lanzar BailOut

queens = add_queen([])
imprimir queens
imprimir "\n".join(". "*q + "Q " + ". "*(BOARD_SIZE-q-1) para q en
queens)
"""

# english keyword -> spanish
trans_en2es = {
'and': 'y',
'as': 'como',
'assert': 'afirmar',
'break': 'afuera',
'class': 'clase',
'continue': 'siguiente',
'def': 'def',
'del': 'elim',
'elif': 'sinosi',
'else': 'sino',
'except': 'excepto',
'exec': 'ejecutar',
'finally': 'finalmente',
'for': 'para',
'from': 'desde',
'global': 'global',
'if': 'si',
'import': 'importar',
'in': 'en',
'is': 'es',
'lambda': 'lambda',
'not': 'no',
'or': 'o',
'pass': 'pasar',
'print': 'imprimir',
'raise': 'lanzar',
'return': 'retornar',
'try': 'intentar',
'while': 'mientras',
'with': 'con',
'yield': 'producir',
}
# reverse dict
trans_es2en = dict((v,k) for (k,v) in trans_en2es.items())

def translate_tokens(source, tdict):
for tok_num, tok_val, _, _, _ in
tokenize.generate_tokens(source.readline):
if tok_num==token.NAME: tok_val = tdict.get(tok_val, tok_val)
yield tok_num, tok_val

code_en = tokenize.untokenize(translate_tokens(StringIO(code_es),
trans_es2en))
print code_en
code_es2= tokenize.untokenize(translate_tokens(StringIO(code_en),
trans_en2es))
print code_es2
--- end code ---
 
S

Sion Arrowsmith

Gabriel Genellina said:
(I hope nobody will abuse this technique... Y perd=F3n a los
hispanoparlantes por lo horrible de la traducci=F3n).

Ah, I only spotted this when I came to post a response. And the
reason I was going to post a response was that these:
'assert': 'afirmar',
'exec': 'ejecutar',
'import': 'importar',
'pass': 'pasar',
'print': 'imprimir',
'raise': 'lanzar',
'return': 'retornar',
'try': 'intentar',
'yield': 'producir',

look rather odd to this non-native Spanish speaker (or at least
reader), and I was going to ask if they sounded more idiomatically
correct if it's not your nth language. I guess they don't :cool:
 
G

Gabriel Genellina

En Tue, 26 Jun 2007 13:11:50 -0300, Sion Arrowsmith
Ah, I only spotted this when I came to post a response. And the
reason I was going to post a response was that these:


look rather odd to this non-native Spanish speaker (or at least
reader), and I was going to ask if they sounded more idiomatically
correct if it's not your nth language. I guess they don't :cool:

They are... ugly, yes. If I were to choose the names in Spanish, I'd use
other words unrelated to the Python original keywords. For example, "pass"
would become "nada" ("nothing") (why was chosen "pass" in the first
place?) and "continue" would be "siguiente" ("next") and "break" would be
"salir" ("go out","quit"). "except" is hard to translate, and even in
English I don't see what is the intended meaning (is it a noun? a verb? an
adverb? all look wrong). The pair "throw/catch" would be easier to use.
And "with" would be "usando" ("using").
BTW, usage of "print" instead of "display" or "show" became obsolete 30
years ago or so...
For a "real" Python translation, new versions with new keywords are a
problem - what if "using" is added to the language and it conflicts with
the translation of "with"?
So... let's stay with the original keywords (english or dutglish or
whatever they are...)
 
M

Michael Hoffman

Gabriel said:
"except" is hard to translate, and
even in English I don't see what is the intended meaning (is it a noun?
a verb? an adverb? all look wrong).

It's a preposition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top