[Bulk] RE: Python changing the keywords name

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 26 21:18:46 EDT 2007


(Please keep posting on the list. You can reach a lot of helpful people,  
and I don't read this email account very often)

At Sunday 24/06/2007 10:38, you wrote:

>> Instead of changing Python grammar, you could convert your"translated"  
>> source into "original" Python using the code below, andcompile and run  
>> as usual; you can also reverse the process. Also, thislets you use the  
>> standard Python library and all other Python codearound the world.

> This previously was very useful, thanks. But can you just help me more 
> something about that???  When I "translate" my code with your example I  
> get:e.g. Translated on english:
> My croatian code
>>> koristi os  # koristi as importispisi "Bok kaj ima"  # ispisi as print
> after translate I get code: " koristi os ispisi  "  I want to this  
> translateget me full code: koristi os ispisi "Bok kaj ima"

This is my original example stripped down and using a small translation  
dictionary:

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

trans_en2hr = {
     'import': 'koristi',
     'print': 'ispisi',
}
trans_hr2en = dict((v,k) for (k,v) in trans_en2hr.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

text = """
koristi os  # koristi as import
ispisi "Bok kaj ima"  # ispisi as print
"""
print tokenize.untokenize(translate_tokens(StringIO(text),trans_hr2en))
--- end code ---

And I get this output:

import os # koristi as import
print "Bok kaj ima"# ispisi as print

Hope this helps,
-- 
Gabriel Genellina



More information about the Python-list mailing list