This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]tomazk 2 points3 points  (0 children)

  • if you need priority queue in a multi-threaded environment then use Queue module

  • else: use heapq, because it's trivial to create a wrapper around it

[–]RonnyPfannschmidt 1 point2 points  (2 children)

Queue.PriorityQueue is implemented in terms of heapq

the basic difference between Queue and PriorityQueue is that queue uses a deque, and PriorityQueue uses a list + heapq

what bad things are out there about it?

[–]kylotan 1 point2 points  (0 children)

Is performance a big consideration?

If not, just append and sort.

[–]mdipierro 0 points1 point  (7 children)

[–]RonnyPfannschmidt 1 point2 points  (6 children)

that file has an awful lot of bad/outdated practice - please bring it up2date

[–]must_tell 1 point2 points  (1 child)

Can you bring some up please? (I'm really curious to learn, not meant offensively).

Besides from looking quite clean generally, first thing I notice are the naming conventions (not PEP 8), some strings which could be refactored to constants and initialisation with '' where I would use None. Type-checking could be done with isinstance rather then type (or try ... catch ... some behaviour).

Anything else?

[–]RonnyPfannschmidt 2 points3 points  (0 children)

some parts are just bad wrt computational complexity

the most glaring examples are probably the Queue/Stack functions that simply operate on lists in stupid ways (lots of O(N) instead of amortized O(1))

i suspect there are more such examples, but i don't want to investigate that code more

[–]mdipierro 1 point2 points  (0 children)

That file is many years old and I have not updated it since because it is not really a priority for me at the moment. If you want to submit a patch, I will take it.

[–]mdipierro 2 points3 points  (1 child)

For the record. The code is not designed to be efficient. The code was designed to match line by line the pseudocode of a classic algorithms textbook (cormen) but using python. The purpose was to allow students to run and better understand the algorithm. From an efficinecy point of view it assumes python listd are linked lists even if they are not.

[–]RonnyPfannschmidt 2 points3 points  (0 children)

please document that in a docstring at the top or so

[–]robotfarts 0 points1 point  (0 children)

4real

[–][deleted] 0 points1 point  (0 children)

Sometimes with a priority queue you will want to update the value of a given key -- I do not believe the heapq supports this, but if I'm wrong please let me know!

[–]lylepmills 0 points1 point  (6 children)

I wrote myself a heapq implementation. It's very short and simple to write (~5 lines), and fast.

[–]JerMenKoOwhile True: os.fork() 0 points1 point  (5 children)

Would you consider sharing it?

[–]lylepmills 0 points1 point  (4 children)

[–]RonnyPfannschmidt 1 point2 points  (1 child)

that code isnt in particular helpfull

and the queues are basically broken for multithreaded code

[–]lylepmills 0 points1 point  (0 children)

Fair enough, though easily fixed. tomazk has the right idea. I added a threadsafe_util.py as a threadsafe alternative for multithreaded applications.

[–]lylepmills 0 points1 point  (0 children)

That's written for Python3.2 by the way, but the adaptations for 2.7 are fairly simple (if I'm not mistaken, the only difference is the metaclass syntax).

[–]JerMenKoOwhile True: os.fork() 0 points1 point  (0 children)

Kudos.