473,385 Members | 2,210 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,385 software developers and data experts.

Search Queue

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
4 2215
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
16
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
5
by: Dhans | last post by:
Framework version : 1.1 For a given directory (which may have subdirectories), I need to identify the number of text files (*.txt). For this I have tried recursive method to search files, it...
4
by: quigstah | last post by:
From: quigstah@gmail.com - view profile Date: Wed, Apr 5 2006 12:56 am Email: "quigs...@gmail.com" <quigs...@gmail.com> Groups: comp.unix.programmer Not yet rated Rating: show options ...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
6
by: gskbond | last post by:
Following is my logic to traverse level by level an mway search tree.... (Yeah I finally used templates to implement a queue... :) ) void mWayTree:: TraverseLevelOrder(Node *root,int lvl) {...
1
by: Steve Cooke | last post by:
Hi, I'm new to SQL and been trying different queries on a test database. I have been having issues with a join command. In my DB there are several tables from our current help desk ticketing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.