471,338 Members | 952 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Python changing keywords name

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!!!!!!!!!!!!!!!!!

Jun 23 '07 #1
6 2274
ve***********@v-programs.com wrote:
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>.
--
Michael Hoffman
Jun 23 '07 #2
ve***********@v-programs.com wrote:
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.
>>splodnik 'This is a splodnik statement.'
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
Jun 23 '07 #3
vedrandeko...@v-programs.com wrote:
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 + [i]
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 ---

--
Gabriel Genellina

PS: Asking just once is enough.

Jun 24 '07 #4
Gabriel Genellina <ga*******@yahoo.com.arwrote:
>(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 8-)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jun 26 '07 #5
En Tue, 26 Jun 2007 13:11:50 -0300, Sion Arrowsmith
<si***@chiark.greenend.org.ukescribió:
Gabriel Genellina <ga*******@yahoo.com.arwrote:
>(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 8-)
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...)

--
Gabriel Genellina
Jun 27 '07 #6
Gabriel Genellina wrote:
"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.
--
Michael Hoffman
Jul 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Robert-Paul | last post: by
4 posts views Thread by Ant | last post: by
2 posts views Thread by Aaron Queenan | last post: by
1 post views Thread by nsyforce | last post: by
3 posts views Thread by Bonzol | last post: by
11 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.