By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Search Queue

P: n/a
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.

Jan 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
abcd wrote:
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

Jan 16 '07 #2

P: n/a
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.

Jan 16 '07 #3

P: n/a
"abcd" <co*******@gmail.comescribió en el mensaje
news:11*********************@v45g2000cwv.googlegro ups.com...
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.

--
Gabriel Genellina
Jan 16 '07 #4

P: n/a
At Tuesday 16/1/2007 15:01, Dennis Lee Bieber wrote:
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

Jan 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.