473,770 Members | 4,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('hell o 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 2227
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('hell o 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*******@gmai l.comescribió en el mensaje
news:11******** *************@v 45g2000cwv.goog legroups.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
13249
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 enqueue, I do a binary search on where to put the object and then insert using that index and arraylist.insert(index, obj) The bottom of my arraylist is always my lowest, therefore, dequeueing is very fast and effecient.
16
5417
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 all from you
5
8339
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 works fine for the directory which has smaller size, but it takes 2 or 3 minutes to search the directory of size 8GB (say “C:â€). Is there any other quicker method to identify whether the given file type (txt) is available in the given directory,...
4
1888
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 Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse | Find messages by this author
1
6185
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 linked list Delete() – Deletes a string from the linked list Search() – Search for a string and return 1 if found otherwise return 0. isEmpty() – Returns 1 if the list is empty and 0 otherwise. Display() – Display all the strings in the list. ...
2
2661
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 need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
19
3399
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 out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
6
4428
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) { int i; int level; Dq<Node *> Q; Node *tmp;
1
1253
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 system. Currently I'm only interested in 2 tables. The first table is Queues which holds all info for different Queues in the system. ID, Name, etc. The second table is Tickets which holds different ticket information.
0
9439
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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
3
2832
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.