how to implement a queue-like container with sort function

I

iMath

I want to a fixed length list-like container, it should have a sorted()-like function that I can use to sort it,I think there should also a function Ican use it to detect whether the numbers of items in it reaches the lengthof the container , because if the numbers of items in it reaches the length(fixed) of the container,I want to process the data in it .Is there a container in Python like this ?If not, what base container should be used to implement such container?

the container is similar to queue ,but queue doesn't have a sort function
 
C

Chris Angelico

the container is similar to queue ,but queue doesn't have a sort function

It's either a queue that can be sorted, or a list with a length limit.
You could fairly easily implement either, because in Python, anything
can be subclassed. But I think possibly the easiest way is to simply
turn your queue into a list when you want to work with it:
['1234', 'asdf', 'qwer', 'zxcv']

Though I don't know if this is what you meant. Putting too much onto a
queue.Queue will block. Were you wanting it, instead, to discard
entries? If so, which?

ChrisA
 
I

iMath

All in all,I want to first fill the container, then sort it and process all the contents in it
 
C

Chris Angelico

All in all,I want to first fill the container, then sort it and process all the contents in it

Where does the length limit come in?

By the way, a little context helps a lot with following a thread.

ChrisA
 
M

MRAB

I want to a fixed length list-like container, it should have a
sorted()-like function that I can use to sort it,I think there should
also a function I can use it to detect whether the numbers of items
in it reaches the length of the container , because if the numbers of
items in it reaches the length(fixed) of the container,I want to
process the data in it .Is there a container in Python like this ?If
not, what base container should be used to implement such container?

the container is similar to queue ,but queue doesn't have a sort
function
This is Python. You don't have to base it on an existing container.

Write a list of the methods it should have. If none of the existing
classes seem suitable as a superclass, then don't bother, just write it
from scratch. You can always use an existing container, such as a list,
as part of its implementation.
 
T

Terry Reedy

I want to a fixed length list-like container, it should have a sorted()-like function that I can use to sort it,I think there should also a function I can use it to detect whether the numbers of items in it reaches the length of the container , because if the numbers of items in it reaches the length(fixed) of the container,I want to process the data in it .Is there a container in Python like this ?If not, what base container should be used to implement such container?

the container is similar to queue ,but queue doesn't have a sort function

For single thread use, subclass list, add a new .put method that checks
the list size and either does self.append or self.sort(); process(list).

For multiple threads, you need the queue module, which has extra
features (and baggage). A PriorityQueue yields items in sorted order.
Subclass it and override .put to either call the original .put or start
a process thread.
 
I

iMath

hey , you used
this means the queue.Queue() has an attribute queue ,but I cannot find it described in the DOC ,where you find it ?
 
S

Steven D'Aprano

the container is similar to queue ,but queue doesn't have a sort
function

It's either a queue that can be sorted, or a list with a length limit.
You could fairly easily implement either, because in Python, anything
can be subclassed. But I think possibly the easiest way is to simply
turn your queue into a list when you want to work with it:
['1234', 'asdf', 'qwer', 'zxcv']

Though I don't know if this is what you meant. Putting too much onto a
queue.Queue will block. Were you wanting it, instead, to discard
entries? If so, which?


Unless the OP needs all the extra threading-related powers of the queue
module, I'd just stick to a simple, single-threaded queue object. These
easiest way to do that is with a deque, which is a double-ended queue.
The threading Queue class is based on a deque, so this will be at least
as fast:

py> from collections import deque
py> my_queue = deque([], 5) # maximum of five items
py> my_queue.append(7)
py> my_queue.append(3)
py> my_queue.append(9)
py> sorted(my_queue)
[3, 7, 9]
py> my_queue.append(2)
py> my_queue.append(0)
py> my_queue.append(4)
py> my_queue
deque([3, 9, 2, 0, 4], maxlen=5)
py> sorted(my_queue)
[0, 2, 3, 4, 9]
 
G

Gregory Ewing

iMath said:
the container is similar to queue ,but queue doesn't have a sort function

You can use a list as a queue. If you have a list l, then
l.append(x) will add an item to the end, and l.pop(0) will
remove the first item and return it.

Then you just need to check the length of the list before
adding an item, and if it's full, do something to process
the items first.

You can encapsulate all this inside a class if you want,
but that's optional.
 
I

iMath

it seems PriorityQueue satisfy my requirement here .

BTW ,the Queue object has an attribute 'queue' ,but I cannot find it described in the DOC ,what it means ?
 
I

iMath

在 2013å¹´11月29日星期五UTC+8下åˆ10æ—¶57分36秒,Mark Lawrence写é“:
Really? AttributeError: type object 'Queue' has no attribute 'queue'



--

Python is the second best programming language in the world.

But the best has yet to be invented. Christian Tismer



Mark Lawrence

you can do a check by hasattr()
 
N

Ned Batchelder

在 2013å¹´11月29日星期五UTC+8下åˆ10æ—¶57分36秒,Mark Lawrence写é“:

you can do a check by hasattr()

Yes, a Queue object has a queue attribute:
deque([])

But you shouldn't use it. It's part of the implementation of Queue, not
meant for you to use directly. In particular, if you use it directly,
you are skipping all synchronization, which is the main reason to use a
Queue in the first place.

It should have been named "_queue". We'll add that to the list of PEP-8
violations in the Queue module! :)

--Ned.
 
N

Ned Batchelder

Yes, a Queue object has a queue attribute:
import Queue
q = Queue.Queue()
q.queue
deque([])

But you shouldn't use it. It's part of the implementation of Queue, not
meant for you to use directly. In particular, if you use it directly, you
are skipping all synchronization, which is the main reason to use a Queue in
the first place.

I should apologize here; when the OP said "queue", I immediately
noticed that I could import that and use it, and mistakenly started my
testing on that, instead of using the deque type. It's deque that
should be used here. Queue is just a wrapper around deque that adds
functionality that has nothing to do with what's needed here.

ChrisA

Actually, I had a long conversation in the #python IRC channel with the
OP at the same time he was posting the question here, and it turns out
he knows exactly how many entries are going into the "queue", so a
plain-old list is the best solution. I don't know quite where the idea
of limiting the number of entries came from.

--Ned.
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top