473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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+cou nt]): .... 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)<>slic e:
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.islic e(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 1800
Op 2005-12-20, sm****@gmail.co m schreef <sm****@gmail.c om>:
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)<>slic e:
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+cou nt)

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+cou nt]
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
14541
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 synchronized objects. However, why couldn't one simply declare: private synchronized LinkedList l; and have the variable be automatically synchronized,...
7
8919
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 = new Node_CThread(client_socket);
5
2782
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 synchronized keyword.
4
8044
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 when to use one and not the other? For instance, if I wanted to remove an item in a SortedList, would it be better to lock() it or do it via the...
0
1151
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 MyList : ArrayList { }
4
10856
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 cons between those two approaches Thanks a lo Chris
6
3720
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 at runtime, the cast of "Queue.Synchronized(new EventQueue())" to an EventQueue object is failing stating invalid cast. Why? public class...
8
79873
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 void methodName() {...} What is the C# alternative for this?
3
2959
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 for Item (this)? Normally,do I need sync at all? Isn't that true without sync the program can run faster?
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.