Search Queue

A

abcd

I have a class such as...

id = 0
class Foo:
def __init__(self, data):
self.id = id
id += 1
self.data = data

And I am storing them in a Queue.Queue...

import Queue
q = Queue.Queue()
q.put(Foo('blah'))
q.put(Foo('hello world'))
q.put(Foo('test'))

how can I search "q" for an instance of Foo which has 'id' equal to say
2? Typically a queue only lets you put and get, not really search.

Thanks.
 
T

Tim Golden

abcd said:
I have a class such as...

[... ]
And I am storing them in a Queue.Queue...

import Queue
q = Queue.Queue()
q.put(Foo('blah'))
q.put(Foo('hello world'))
q.put(Foo('test'))

how can I search "q" for an instance of Foo which has 'id' equal to say
2? Typically a queue only lets you put and get, not really search.

It's possible you're seeing the Queue structure as
some sort of list / array, rather than its intended purpose
as a thread-safe channel. If I'm wrong, ignore the rest of
this post. If, however, you're just after a container for
various instances of Foo then use a list or a dict,
depending on what you're after.

eg,

<code>
class Foo:
def __init__ (self, id):
self.id = id

list_of_foos = [Foo('blah'), Foo('hello'), Foo('test')]
dict_of_foos = dict ((f.id, f) for f in list_of_foos)

print dict_of_foos['blah']

</code>

TJG
 
A

abcd

Yea basically I need Queue like functionality with the ability to
search for a particular instance in it. I guess I could just use a
list and search it as needed.

Thanks.
 
G

Gabriel Genellina

Yea basically I need Queue like functionality with the ability to
search for a particular instance in it. I guess I could just use a
list and search it as needed.

Perhaps the Queue class should be called ThreadQueue, or be contained in the
threading module, or something like that. The current name is misleading.
 
D

Dennis Lee Bieber

Yea basically I need Queue like functionality with the ability to
search for a particular instance in it. I guess I could just use a
list and search it as needed.
If "Queue like functionality" means inserting at the end, and
removing from the front... Just use a list
lq = [3, 4, 8]
lq.append(5)
lq [3, 4, 8, 5]
lq.pop(0) 3
lq [4, 8, 5]
lq.index(8) 1

lq.insert(0, 99)
lq [99, 4, 8, 5]

# .pop() without the 0, with .append(), act as a stack: LIFO
# using a mix of .pop(), .pop(0), .insert(0, xxx), .append(xxx)
# gives the features of a deque

The Queue module isn't meant to be used as a data structure -- it is
a /message passing/ mechanism for use between threads, ensuring that the
internal queue structure is not corrupted by multiple threads accessing
it concurrently.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
G

Gabriel Genellina

At said:
If "Queue like functionality" means inserting at the end, and
removing from the front... Just use a list

Someone said that time complexity should be part of an object public
interfase. Implementing a queue using Python lists is trivial in
terms of lines of code needed, but has a big time penalty. Queue
users expect that put() and get() were fast, O(1) operations, but
list.pop(0) is O(n).
Python>=2.4 has a deque class (in the collections module) that can be
used as a simple queue, and is efficient both in time and memory used.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top