[issue9218] pop multiple elements of a list at once

2010-07-10 Thread Diego Jacobi

New submission from Diego Jacobi :

I am currently working with buffer in an USB device and pyusb.
So when i read from a buffer endpoint i get an array.Array() list.
I handle this chunk of data with a thread to send a receive the information 
that i need. In this thread i load a list with all the information that is read 
from the USB device, and another layer with pop this information from the 
threads buffer.

The thing i found is that, to pop a variable chunk of data from this buffer 
without copying it and deleting the elements, i have to pop one element at the 
time.

def get_chunk(self, size):
for x in range(size):
yield self.recv_buffer.pop()

I guess that it would be improved if i can just pop a defined number of 
elements, like this:

pop self.recv_buffer[:size]
or
self.recv_buffer.pop(,-size)

That would be... "pop from the last element minus size to the last element"
in that way there is only one memory transaction.
The new list points to the old memory address and the recv_buffer is advanced 
to a new address. Data is not moved.

note that i like the idea of using "pop" as the "del" operator for lists

thanks.
Diego

--
messages: 109912
nosy: jacobidiego
priority: normal
severity: normal
status: open
title: pop multiple elements of a list at once
type: performance
versions: Python 2.6

___
Python tracker 
<http://bugs.python.org/issue9218>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9240] different behaviour with for loop... interpreted vs scripted

2010-07-12 Thread Diego Jacobi

New submission from Diego Jacobi :

Hi.
I am not a python expert and i was trying to reduce this next code:

data = []
i = 0
for j in range(packetlen+1):
print i, self.channels_in[ channels[i] ]
data.append( self.channels_in[ channels[i] ].pop() )
i += 1
if i >= len(channels):
i=0

into something like this:

data = []
for j in range(packetlen+1), i in channels:
print j, i
data.append( self.channels_in[ i ].pop() )

which is much more readable and short.
But i didnt know if this sintax is really supported (also i didnt found 
examples on internet), so i just tried.

I runned the script and it didnt complains with errors, BUT i found that the 
behavior is not what i expected.

I expected j to take value of range() and i to take the values of channels 
until range() gets empty.

well, the actual behavior is that j takes the value of the complete range() as 
in
j = range(..)

and the for loop iterates as in 
for i in channels:


ALSO i tested this un the python command line and it produces different 
behaviours:

the same sintax writen as:

for j in range(1,5), i in range(120,500,12):
   print j, i

produces 

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'i' is not defined


Thats all. I guess that if it produces different results, then there is an 
error happening here.


If there is a pythonic way to do this:
for j in range(packetlen+1), i in channels:

please tell me.

Cheers.
Diego

--
messages: 110150
nosy: jacobidiego
priority: normal
severity: normal
status: open
title: different behaviour with for loop... interpreted vs scripted
type: behavior
versions: Python 2.6

___
Python tracker 
<http://bugs.python.org/issue9240>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com