code indentation

J

...:::JA:::...

Hello,

In my previously post I have been talk about running code with exec in......
So now I have problem with code indentation, as Gabriel Genellina says:
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!!!!!

Regards,
Vedran
__________________________________________________________________ Vedran
veki ICQ#: 264412055 Current ICQ status: + More ways to contact me Get ICQ!
__________________________________________________________________
 
G

Gabriel Genellina

En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
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.
 
V

vedrandekovic

En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
<[email protected]> 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.

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

Regards,

Vedran
 
S

Steve Holden

En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
<[email protected]> 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.

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

Ben Finney

Steve Holden said:
En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
<[email protected]> escribió:
So......how can I do this?????????????
I will appreciate any help!!!!!
Try with a simple example. Let's say you want to convert this:
[...]
[...] Can you give me some example script of this? Please!!!

PS: THANKS FOR YOUR TIME!!!!!!!!!!

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 [...]

And while we're on the topic of communication: The original poster
would do well to learn that increasing the number of consecutive
punctuation marks (!!!, ???) is a sure way to turn away many people
who would otherwise be helpful. Sentences need at most one '!' or '?',
adding more does not improve the chances of being taken seriously.
 
V

vedrandekovic

Steve Holden said:
En Mon, 23 Jul 2007 16:53:01 -0300, ...:::JA:::...
<[email protected]> escribió:
So......how can I do this?????????????
I will appreciate any help!!!!!
Try with a simple example. Let's say you want to convert this:
[...]
[...] Can you give me some example script of this? Please!!!
PS: THANKS FOR YOUR TIME!!!!!!!!!!
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 [...]

And while we're on the topic of communication: The original poster
would do well to learn that increasing the number of consecutive
punctuation marks (!!!, ???) is a sure way to turn away many people
who would otherwise be helpful. Sentences need at most one '!' or '?',
adding more does not improve the chances of being taken seriously.

--
\ "We have to go forth and crush every world view that doesn't |
`\ believe in tolerance and free speech." -- David Brin |
_o__) |
Ben Finney

Hi,

I was only ask for help becose I don't understand this tokenize module
so well.
And if you can help me than please help me , but if you can't then
please don't leave me some stupid
messages


Regards,

Vedran
 
W

Wildemar Wildenburger

And if you can help me than please help me , but if you can't then
please don't leave me some stupid messages

Whats stupid about this? It's sane advice.

Which looks more serious to you:

this:
I can't do it!!!! Can you PLEASE help me????!!!?!

or this:
I don't know the answer. Can you please help me?

Even if it makes no difference to you, to many people it does. So Ben is
right: People *will* take your posts more seriously if you restrict your
use of punctuation (if only because its easier to read).
Don't feel offended, nobody was trying to put you down.

/W
 
V

vedrandekovic

Whats stupid about this? It's sane advice.

Which looks more serious to you:

this:
I can't do it!!!! Can you PLEASE help me????!!!?!

or this:
I don't know the answer. Can you please help me?

Even if it makes no difference to you, to many people it does. So Ben is
right: People *will* take your posts more seriously if you restrict your
use of punctuation (if only because its easier to read).
Don't feel offended, nobody was trying to put you down.

/W

HELLO,

On this group I ask for serious help and now we talk about
communication. Then I you don't know how to help me then please DON'T
SAY ANYTHING

Regards,
Vedran
 
D

Diez B. Roggisch

HELLO,
On this group I ask for serious help and now we talk about
communication. Then I you don't know how to help me then please DON'T
SAY ANYTHING


Your lack of command of the python language and programming concepts in
general is only excelled by your inabillity to react properly in a
community of friendly people that try to point out how to behave as a
human being instead of a complete jerk.

And given your history of ignorance of sound advice, I think I'm not
stressing my crystal ball to much if I predict: you won't receive much
more help here.

Diez
 
B

Ben Finney

On this group I ask for serious help and now we talk about
communication.

Yes. You're asking for volunteer help from a group of people who have
their own priorities separate from yours. The way to garner help from
these people is to respect their time. One excellent way to do that is
to communicate clearly and maturely, so that your messages are easier
(and therefore faster) to read.

Please, before going further, read this document on how to ask
questions the smart way:

said:
Then I you don't know how to help me then please DON'T SAY ANYTHING

This is a volunteer group, run for the benefit of the community. We
want to help not only you, but *anyone* who might come here asking for
help, and especially those who are inclined to help them.

Advice on improving communication is one good way to do that: it
actively works toward better communication in future, which helps
questions get answered quicker and consumes less of the helpers' time.
 
T

Thorsten Kampe

* (Wed, 25 Jul 2007 11:22:03 -0700)
On this group I ask for serious help and now we talk about
communication. Then I you don't know how to help me then please DON'T
SAY ANYTHING

You already got serious help even though you haven't realised that
yet.

T.
 
S

Steve Holden

Thorsten said:
* (Wed, 25 Jul 2007 11:22:03 -0700)

You already got serious help even though you haven't realised that
yet.
We should be making allowances for this particular poster on account of
relative youth: I hadn't realised earlier, but we are dealing with a
fourteen-year-old. Since fourteen-year-olds already know everything we
should be honored we are being asked for help at all ;-)

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

vedrandekovic

We should be making allowances for this particular poster on account of
relative youth: I hadn't realised earlier, but we are dealing with a
fourteen-year-old. Since fourteen-year-olds already know everything we
should be honored we are being asked for help at all ;-)

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

Hi again,

Just one more question, can I maybe do this indentation with string
e.g
here is my failed example of try with string:

kl="n=90;if n==90:print'kajmakimar'"

for line in kl.split(";"):
li=[]
m=li.append(line)
if line.endswith(':'):
m.append("\n\t\t\t\t\t\t\t\t")
print m

......so maybe if you can help me with this?

Regards,

Vedran

(http://www.v-programs.com)
 
M

Michael L Torrie

.....so maybe if you can help me with this?

If I understand you correctly, you're trying to make a "pretty-printer"
in python, right? Something that will take arbitrary python source
code, recognize the blocks and so forth, and then emit clean python code
(text) with tabs for indents instead of spaces?
 
D

Diez B. Roggisch

We should be making allowances for this particular poster on account of
relative youth: I hadn't realised earlier, but we are dealing with a
fourteen-year-old. Since fourteen-year-olds already know everything we
should be honored we are being asked for help at all ;-)

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

Hi again,

Just one more question, can I maybe do this indentation with string
e.g
here is my failed example of try with string:

kl="n=90;if n==90:print'kajmakimar'"

for line in kl.split(";"):
li=[]
m=li.append(line)
if line.endswith(':'):
m.append("\n\t\t\t\t\t\t\t\t")
print m

.....so maybe if you can help me with this?

This won't work. It's impossible to know how deep to indent without any
begin/end-tokens. Consider this simple example:

if foo:
print "foo"
if bar:
print "bar"

is equal to

'if foo:;print"foo";if bar:;print "bar"'

But that could as well be

if foo:
print "foo"
if bar:
print "bar"

So - you need some block delimiters.

And as it has been said to you a bazzillion times: stop doing what
you're doing. Use python. As it is. Your limited understandig of parsing
and language design makes your task beyond your capabilites. For now.
And the forseeable future...

diez
 
B

Ben Finney

Steve Holden said:
We should be making allowances for this particular poster on account
of relative youth: I hadn't realised earlier, but we are dealing
with a fourteen-year-old.

I don't believe that's true. One of the great advantages of discussion
over the internet is that one's behaviour is what is judged, without
necessary prejudice from one's age, skin tone, facial features, or the
like. Set against the many disadvantages of internet discussion, I
think "judge only on basis of behaviour" is a good default.
 
B

Ben Finney

here is my failed example of try with string:

kl="n=90;if n==90:print'kajmakimar'"

for line in kl.split(";"):
li=[]
m=li.append(line)
if line.endswith(':'):
m.append("\n\t\t\t\t\t\t\t\t")
print m

The list.append method returns None. It appends the item to the list.
>>> lines = []
>>> result = lines.append("foo")
>>> print result
None

So, there's no need to do anything with the result of append() -- just
continue using the list object.
>>> print lines ['foo']
>>> lines.append("bar")
>>> print lines
['foo', 'bar']
 
S

Steve Holden

Ben said:
I don't believe that's true. One of the great advantages of discussion
over the internet is that one's behaviour is what is judged, without
necessary prejudice from one's age, skin tone, facial features, or the
like. Set against the many disadvantages of internet discussion, I
think "judge only on basis of behaviour" is a good default.
Fair enough, but I was simply suggesting we cut him some slack, not that
we completely ignore any obnoxious behavior we might observe.

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

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top