Set operations for lists: pythonic hints please!

J

jmdeschamps

Working with several thousand tagged items on a Tkinter Canvas, I want
to change different configurations of objects having a certain group of
tags.

I've used the sets module, on the tuple returned by Tkinter.Canvas.
find_withtag() method.

Since this method takes only one tag at time, I use it for each tag
that is part of the look-up group, create a set object for each tuple
and intersect the resulting sets, that I tuple out again to feed a loop
to change the configuration of the items.

Anyone have a more pythonic way (or better performing ) way to suggest
?

Thanks in advance,

Jean-Marc

# My test code
# the code is not generic here
# my question only pertains to the intersection algorithm perse

from Tkinter import *
import random, sets

root=Tk()

m1 = Canvas(root,width=410,height=410)
m1.pack(fill=BOTH, expand=1)
for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)
m1.create_oval(x,y,x+10,y+10,fill="red",tags=("etoile","rouge"))

for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)

m1.create_rectangle(x,y,x+10,y+10,fill="red",tags=("artefact","rouge"))

for i in range(10):
x=random.randrange(1,400)
y=random.randrange(1,400)

m1.create_rectangle(x,y,x+10,y+10,fill="green",tags=("artefact","green"))

#
i=m1.find_withtag("artefact")
j=m1.find_withtag("rouge")

s1=sets.Set(i)
s2=sets.Set(j)

s3=s1.intersection(s2)
myIntersection=tuple(s3._data.keys())

for i in myIntersection:
m1.itemconfig(i,fill="black")

root.mainloop()
 
D

Diez B. Roggisch

Working with several thousand tagged items on a Tkinter Canvas, I want
to change different configurations of objects having a certain group of
tags.

I've used the sets module, on the tuple returned by Tkinter.Canvas.
find_withtag() method.

Since this method takes only one tag at time, I use it for each tag
that is part of the look-up group, create a set object for each tuple
and intersect the resulting sets, that I tuple out again to feed a loop
to change the configuration of the items.

Anyone have a more pythonic way (or better performing ) way to suggest
?

It depends. If the search on tags is implemented linear, it might be
better to loop over all items and filter suing a compound predicate,
like this:

rouge_artefacts = [item for m1.all_itmes() if item.has_tag("rouge") and
item.has_tag("artefact")]

Please note that I don't know TKinter's API, but I can't imagine that it
lacks the possibility to fettch a list of all items and query an item
for it tags. The synopsis may vary from above, thogh.

But if tkinter stores the tagged items as mapping of tag -> set/list,
then I think your approach is as fast as you can get. Time it out. And
maybe another thing would be to manage that tagging relation yourself.

Regards,

Diez
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top