code indentation

Steve Holden steve at holdenweb.com
Tue Jul 24 14:10:12 EDT 2007


vedrandekovic at v-programs.com wrote:
> On 24 srp, 05:20, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>> En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
>> <vedrandeko... at v-programs.com> escribió:
>>
>>
>>
>>>> If you are using the tokenize module as suggested some time ago, try to
>>>> analyze the token sequence you get using { } (or perhaps begin/end pairs
>>>> in your own language, that are easier to distinguish from a dictionary
>>>> display) and the sequence you get from the "real" python code. Then
>>>> write
>>>> a script to transform one into another:
>>>> from tokenize import generate_tokens
>>>> from token import tok_name
>>>  >from cStringIO import StringIO
>>>> def analyze(source):
>>>   >   g = generate_tokens(StringIO(source).readline)
>>>    >  for toknum, tokval, _, _, _  in g:
>>>      >    print tok_name[toknum], repr(tokval)
>>>> I think you basically will have to ignore INDENT, DEDENT, and replace
>>>> NAME+"begin" with INDENT, NAME+"end" with DEDENT.
>>> So......how can I do this?????????????
>>> I will appreciate any help!!!!!
>> Try with a simple example. Let's say you want to convert this:
>>
>> for x in range(10):
>> begin
>> print x
>> end
>>
>> into this:
>>
>> for x in range(10):
>>    print x
>>
>> Using the analyze() function above, the former block (pseudo-python) gives
>> this sequence of tokens:
>>
>> NAME 'for'
>> NAME 'x'
>> NAME 'in'
>> NAME 'range'
>> OP '('
>> NUMBER '10'
>> OP ')'
>> OP ':'
>> NEWLINE '\n'
>> NAME 'begin'
>> NEWLINE '\n'
>> NAME 'print'
>> NAME 'x'
>> NEWLINE '\n'
>> NAME 'end'
>> ENDMARKER ''
>>
>> The latter block ("real" python) gives this sequence:
>>
>> NAME 'for'
>> NAME 'x'
>> NAME 'in'
>> NAME 'range'
>> OP '('
>> NUMBER '10'
>> OP ')'
>> OP ':'
>> NEWLINE '\n'
>> INDENT '  '
>> NAME 'print'
>> NAME 'x'
>> DEDENT ''
>> ENDMARKER ''
>>
>> If you feed this token sequence into untokenize, in response you get a
>> source code equivalent to the "real" python example above. So, to convert
>> your "pseudo" python into the "real" python, it's enough to convert the
>> first token sequence into the second - and from that, you can reconstruct
>> the "real" python code. Converting from one sequence into the other is a
>> programming exercise and has nothing to do with the details of the
>> tokenize module, nor is very Python-specific - looking at both sequences
>> you should figure out how to convert one into the other. (Hint: a few
>> additional newlines are not important)
>>
>> It is even simpler than the example given in the tokenize documentation:
>> <http://docs.python.org/lib/module-tokenize.html> - which transforms
>> 3.1416 into Decimal("3.1416") by example.
>>
>> Once you get this simple case working, you may try what happens with this:
>>
>> for x in range(10):
>>    begin
>>      print x
>>    end
>>
>> and this:
>>
>> for x in range(10): begin
>>    print x
>> end
>>
>> and later this:
>>
>> for x in range(10):
>>    begin
>>      print x
>> end
>>
>> You are now using explicit begin/end pairs to group statements, so
>> indentation is no more significant. You may want to preprocess the
>> pseudo-python source, stripping any leading blanks, before using tokenize
>> - else you'll get indentation errors (which are bogus in your
>> pseudo-python dialect).
>>
>> Since this will be your own Python dialect, don't expect that someone else
>> will do the work for you - you'll have to do it yourself. But it's not too
>> dificult if you do the things in small steps. In case you get stuck at any
>> stage and have specific questions feel free to ask.
>>
>> --
>> Gabriel Genellina
> 
> Hello,
> 
> Sorry, now I become very nuisance and stupid but please I really need
> this.Do you remember my topic "python changing keywords name" ,and do
> you remember example that you give me for translate keywords? Can you
> give me some example script of this? Please!!!
> 
> PS:   THANKS FOR YOUR TIME!!!!!!!!!!

I think you may have to accept that the task you have undertaken is, for 
the moment, beyond your capabilities.

It's unfortunate that you are having difficulty with two languages 
simultaneously: your command of English, though impressive, appears to 
be insufficient for you to explain the problem in enough detail for 
someone else to solve it for you (even if someone should feel so 
generous). Your command of Python is not enough to solve it for yourself.

Perhaps you should re-think your approach and consider that it may take 
you longer than you anticipated.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list