473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

synchronized enumerate

I see that there is a thread of a similar topic that was posted
recently ( enumerate with a start index ) but thought I would start a
new thread since what I am suggesting is a little different.

I posted a very similar item to python-dev, but they said to post it
here. Tutor also said that if anything is to be considered as PEP
worthy, it should be run through here first. So...here goes.

Whenever I use enumerate, I am doing so because I will use the index to
access some other element in the list (the previous or next, usually)
while also looking at the element that is returned from enumerate.
Several times, however, in the development phase of the work, I end up
sending a subset of the list at hand and then get bitten by the fact
that the indices returned by enumerate are not the indices of the
original list, they are the indices of the slice that I sent. e.g. in
the following, "0" is the first index but I wanted it to be "3"

###
start=3; count=5
for i, x in enumerate(range(10)[start:start+count]): .... print i, x
....
0 3
1 4
2 5
3 6
4 7 ###

There is a "flexible enumerate()" in the ASPN Cookbook that shows how
to return an arbitrary integer starting point for an iterable,

http://aspn.activestate.com/ASPN/Coo.../Recipe/307582

but I would vote against that since enumerate is suppose to give
indices of the original list. What I would like to see supported is
the concept that enumerate should give indices and items from an
iterable--it does right now--but it should also allow you to access a
different portion of the original list and *still* give the indices of
that slice. What I propose is an optional slice argument that would be
used to return the sliced indices and items:

def enum(l, slc=None):
if slc==None: #the usual ennumerate
for i, dat in enumerate(l):
yield i, dat
else:
if type(slc)<>slice:
raise TypeError, "slc must be a valid slice"
start, step = slc.start, slc.step
#
# we need actual values for start and step, so check for None
# and supply defaults
#
if step==None:step=1
if start==None:
if step>0:
start=0
else:
start=-1
for i, dat in enumerate(l[slc]):
j = i*step+start
if j<0: j+=len(l) #always give positive indices
yield j, dat
### for i, x in enum(range(10), slice(start, start+count)): .... print i, x
....
3 3
4 4
5 5
6 6
7 7 for i, j in enum(range(10), slice(None,None,-3)): .... print i,j
....
9 9
6 6
3 3
0 0 ###

An advantage to processing the iteratable with a slice argument is that
then the slice information is given only once and it can do 2 things:
slice the original iterable and provide the synchronized indices.

NOTE: the same thing that I am proposing could also be done with
xrange, islice and izip, but it's more ackward and you have to supply
the same information in two places:

###
aList = range(10) start=2;stop=2+5
for i,j in itertools.izip(xrange(start, stop), itertools.islice(range(10), start, stop)): .... print i,j
....
2 2
3 3
4 4
5 5
6 6

###

It just seems that a function which was suppose to simplify accessing
indices and elements of an iterable should easily allow you to look at
a slice of the iterable and get the same synchronized inidces.

/c

Dec 20 '05 #1
1 1787
Op 2005-12-20, sm****@gmail.com schreef <sm****@gmail.com>:
What I would like to see supported is
the concept that enumerate should give indices and items from an
iterable--it does right now--but it should also allow you to access a
different portion of the original list and *still* give the indices of
that slice. What I propose is an optional slice argument that would be
used to return the sliced indices and items:

def enum(l, slc=None):
if slc==None: #the usual ennumerate
for i, dat in enumerate(l):
yield i, dat
else:
if type(slc)<>slice:
raise TypeError, "slc must be a valid slice"
start, step = slc.start, slc.step
#
# we need actual values for start and step, so check for None
# and supply defaults
#
if step==None:step=1
if start==None:
if step>0:
start=0
else:
start=-1
for i, dat in enumerate(l[slc]):
j = i*step+start
if j<0: j+=len(l) #always give positive indices
yield j, dat
###
for i, x in enum(range(10), slice(start, start+count)):


The trouble with your suggestion (and the original enumerate,
which you rely on if no slice is provided) is that it doesn't
work as desired if you just give it a sequence that starts
at an other index than zero.

I have a table modules, tables are similar to lists but they
can start at arbitrary indexes. So if I have a table that
goes from -4 to +4, I would like enumerate(tab) or enum(tab)
to give me:

(-4, tab[-4])
(-3, tab[-3])

....

Of course this needs some cooperation from the sequence type.
Personnaly I would have though of a range method on the sequence
which could be used by enumerate to know where to start and end.

I also would prefer the possibilty to use the same notation
as with sequence subscription instead of having to use the
cumbersome slice notation. Something like:

enum(lst, start:start+count)

Instead of

enum(lst, slice(start, start+count))
Of course if you wouldn't mind brackets instead of parenthesis
we could implement enum as a vitual slice, we then would have
to write:

enum[lst, start:start+count]
Just my 2 cents.

--
Antoon Pardon
Dec 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank | last post by:
Hi, In the javadocs regarding many of the java.util classes, it states that the classes are not synchronized, and suggest using the Collections.synchronizedX(...) methods for getting...
7
by: John Thorner | last post by:
Hi, I am creating a new thread for each of the connections to the server: public class Node_C { .... while (listening) { Socket client_socket = server_socket.accept(); Node_CThread node ...
5
by: Max Ischenko | last post by:
Hi, I wrote simple implementation of the "synchronized" methods (a-la Java), could you please check if it is OK: def synchronized(method): """ Guards method execution, similar to Java's...
4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
0
by: Mike Grasso | last post by:
I've seen a few messages on this, but no responses. Here's what I found out PROBLEM: How do you use ArrayList.Synchronized to create thread-safe objects derived from ArrayList public class...
4
by: chrisben | last post by:
Hi I often use Queue.Synchronized method to create a queue for multithread writing. I also know I could use SyncRoot and lock to write Queue. Could anyone here please explain to me the pros and...
6
by: rmunson8 | last post by:
I have a derived class from the Queue base class. I need it to be thread-safe, so I am using the Synchronized method (among other things out of scope of this issue). The code below compiles, but...
8
by: ASP.Net programmer | last post by:
Hi, I have a few methods in a class that I want to synchronize (make sure they can't be used at the same time by multiple threads). As a Java programmer I just do this: public synchronized...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.