Re: [Tutor] Array indexing

2007-01-16 Thread Joe Abbey

On 1/16/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote:


On Tue, Jan 16, 2007 at 11:28:49AM -0500, Joe Abbey wrote:
> Hello,
>
> I'm using Active Python v2.4.3.11 on a Windows XP machine.
>
> Probably more relevant is that I'm just learning Python, as in I've been
> writing Python for less than 24 hours.
>
> While trying to implement a PE parser, I ran into the following problem:
>
> #** START CODE***
> data = file.read(128);
> directoryTable = struct.unpack('',
data);
> i=0;
> print "Export table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> print "Import table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> #** END CODE***
>
> This code throws a syntax error at the first i+=1 on "line 4".
>
> Why is this the case?
>

In Python, "i += 1" is a statement.  You have used in as an
expression.  In Python, an expression returns a value; a statement
does not.

> It seems like it would be very useful to be able to increment an index
after
> referencing into an array.
>

What you are asking for is viewed by some as useful.  But, I think
it is too confusing.  Should the variable be incremented before or
after it is used to index into the array?  C/C++ gives you a
choice: you can use either "i++" or "++i", which makes code harder
to read, I think.  And, what about:

x = y[i+=1] + z[i]

Has the second use of "i" been incremented or not.

> Is my approach busted?  Is there a better way to reference elements?
>

Instead of:

x = directoryTable[i] + directoryTable[i+=1]);

use something like:

x = directoryTable[i] + directoryTable[i+1]

And, by the way, you do not need all those semicolons at the end of
each line.  In Python, the semicolon is a statement separator, not
a statement terminator.  It is more Pythonic to use a semicolon
between statements only when there are more than one statement on a
line.  And writing more than one statement on a line is usually
discouraged anyway.

> The "fix" I'm currently using is to write the index I want:
>
> (directoryTable[0], directoryTable[1])

Or, if you need an index variable:

directoryTable[i], directoryTable[i+1])

Dave


--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




Thank you Danny for the iterator tutorial. I'll check the link.

Thanks Dave for the language lesson.  As you could tell from my code
snippet, C\C++ is what I have programmed the most in.

For this case I believe the iterator example would be most preferred.

#** START CODE***
dTable = iterator(directoryTable)
print "Export table   0x%08X + 0x%08x" % (dTable.next(), dTable.next
())
#** END CODE***

But for now the explicit indexing will work fine :)

Thanks!

Joe
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Array indexing

2007-01-16 Thread Dave Kuhlman
On Tue, Jan 16, 2007 at 11:28:49AM -0500, Joe Abbey wrote:
> Hello,
> 
> I'm using Active Python v2.4.3.11 on a Windows XP machine.
> 
> Probably more relevant is that I'm just learning Python, as in I've been
> writing Python for less than 24 hours.
> 
> While trying to implement a PE parser, I ran into the following problem:
> 
> #** START CODE***
> data = file.read(128);
> directoryTable = struct.unpack('', data);
> i=0;
> print "Export table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> print "Import table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> #** END CODE***
> 
> This code throws a syntax error at the first i+=1 on "line 4".
> 
> Why is this the case?
> 

In Python, "i += 1" is a statement.  You have used in as an
expression.  In Python, an expression returns a value; a statement
does not.

> It seems like it would be very useful to be able to increment an index after
> referencing into an array.
> 

What you are asking for is viewed by some as useful.  But, I think
it is too confusing.  Should the variable be incremented before or
after it is used to index into the array?  C/C++ gives you a
choice: you can use either "i++" or "++i", which makes code harder
to read, I think.  And, what about:

x = y[i+=1] + z[i]

Has the second use of "i" been incremented or not.

> Is my approach busted?  Is there a better way to reference elements?
> 

Instead of:

x = directoryTable[i] + directoryTable[i+=1]);

use something like:

x = directoryTable[i] + directoryTable[i+1]

And, by the way, you do not need all those semicolons at the end of
each line.  In Python, the semicolon is a statement separator, not
a statement terminator.  It is more Pythonic to use a semicolon
between statements only when there are more than one statement on a
line.  And writing more than one statement on a line is usually
discouraged anyway.

> The "fix" I'm currently using is to write the index I want:
> 
> (directoryTable[0], directoryTable[1])

Or, if you need an index variable:

directoryTable[i], directoryTable[i+1])

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Array indexing

2007-01-16 Thread Danny Yoo
> While trying to implement a PE parser, I ran into the following problem:
>
> #** START CODE***
> data = file.read(128);
> directoryTable = struct.unpack('', data);
> i=0;
> print "Export table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> print "Import table   0x%08X + 0x%08x" % (directoryTable[i+=1],
> directoryTable[i+=1]);
> #** END CODE***
>
> This code throws a syntax error at the first i+=1 on "line 4".


Hi Joe,

Yes.  Python's assignments aren't expressions --- in Python, assignments 
are meant to visually stand out.  Unfortunately, this means you can't put 
the assignment within the array indexing expression.


There are a few workarounds.  One is to treat the directoryTable as a 
stream of values that we can iterate across.  For example:

##
>>> values = (3, 1, 4, 1, 5)
>>> i = iter(values)
>>> i

##

'i' here is an "iterator" that we can repeatedly use to get sequential 
elements:

#
>>> i.next()
3
>>> i.next()
1
>>> i.next()
4
#

In some sense, this should allow you to do what you had in your original 
code, since i.next() will both give you the value and, internally, move 
the iterator forward.


See:

 http://www.python.org/doc/tut/node11.html#SECTION001190

for a quick-and-dirty introduction to iterators.


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor