Skip to content

Commit

Permalink
simplify some functions functionlity
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan Esmayili Barzi authored and Erfan Esmayili Barzi committed Aug 16, 2012
1 parent eceea17 commit 5a022b1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 73 deletions.
105 changes: 33 additions & 72 deletions HashPQ.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python

from threading import Event
import time
import Queue
Expand All @@ -10,17 +11,17 @@
from OrderedDict import OrderedDict

DEFAULT_PRIORITY = 0
DEFAULT_QUEUE = 0
DEFAULT_KEY = 0

class HashPQueue(object):
class HashPQ(object):
__slots__ = [ '__capacity',\
'__size',\
'__dic',\
'__lock',\
]

def __new__(cls, *args, **kwargs):
return super(HashPQueue, cls).__new__(cls, *args, **kwargs)
return super(HashPQ, cls).__new__(cls, *args, **kwargs)

def __str__(self):
return self.__dic.__str__()
Expand All @@ -46,17 +47,23 @@ def qsize(self):
def get(self):
if not self.__dic:
self.__lock.wait()
return get_nowait()

def get_nowait(self):
if not self.__dic:
raise EmptyHashPQ
# sort keys in list
self.__dic.keys().sort()
highestPriority = self.__dic.keys()[-1]
keyList = self.__dic.keys()
keyList.sort()
highestPriority = keyList[-1]
# get high priority list
highestPriorityList = self.__dic[highestPriority]
item = highestPriorityList.popitem(last=False)
self.__size -= 1
if not self.__size:
self.__lock.clear()
else:
self.__lock.set()
#else:
# self.__lock.set()
if not highestPriorityList:
del self.__dic[highestPriority]
return item[1]
Expand Down Expand Up @@ -87,17 +94,16 @@ def get_by_key(self, key, priority=None):
del self.__dic[priority]
return value


def pick(self):
def get_as_list(self):
pass

def pick_nowait(self):
def get_as_list_nowait(self):
pass

def get_as_list(self):
def pick(self):
pass

def get_as_list_nowait(self):
def pick_nowait(self):
pass

def pick_as_list(self):
Expand Down Expand Up @@ -130,7 +136,6 @@ def pick_as_list_nowait(self):
item_list.append(item)
return item_list


def pick_by_key_nowait(self, key, priority=None):
if not self.__dic:
raise EmptyHashPQ
Expand Down Expand Up @@ -169,6 +174,7 @@ def pick_by_key(self, key, priority=None):
except:
return None
return value

def get_by_key_nowait(self, key, priority=None):
if not self.__dic:
raise EmptyHashPQ
Expand Down Expand Up @@ -200,6 +206,15 @@ def put(self, item):
if self.__size == self.__capacity:
self.__lock.clear()
self.__lock.wait()
return put_nowait()

def put_nowait(self, item):
if not item:
return False
if self.__size == self.__capacity:
raise FullHashPQ
if self.__size == self.__capacity:
raise FullHashPQ
try:
priority = item[0]
key = item[1]
Expand All @@ -211,10 +226,8 @@ def put(self, item):
value = item[1]
except IndexError:
priority = DEFAULT_PRIORITY
key = DEFAULT_QUEUE
key = DEFAULT_KEY
value = item[0]
except ValueError:
return False
except:
return False
if priority not in self.__dic.keys():
Expand All @@ -224,32 +237,11 @@ def put(self, item):
self.__dic[priority][key] = value
self.__lock.set()

def get_nowait(self):
if not self.__dic:
raise EmptyHashPQ
# sort keys in list
self.__dic.keys().sort()
highestPriority = self.__dic.keys()[-1]
# get high priority list
highestPriorityList = self.__dic[highestPriority]
item = highestPriorityList.popitem(last=False)
self.__size -= 1
if not self.__size:
self.__lock.clear()
else:
self.__lock.set()
if not highestPriorityList:
del self.__dic[highestPriority]
return item[1]

def put_nowait_with_kwa(self, **kwargs):
if not kwargs:
return False
if self.__size == self.__capacity:
raise FullHashPQ
if self.__size == self.__capacity:
self.__lock.clear()
self.__lock.wait()
if 'priority' in kwargs:
priority = kwargs['priority']
else:
Expand All @@ -258,7 +250,7 @@ def put_nowait_with_kwa(self, **kwargs):
if 'key' in kwargs:
key = kwargs['key']
else:
key = DEFAULT_QUEUE
key = DEFAULT_KEY

if 'value' in kwargs:
value = kwargs['value']
Expand All @@ -273,37 +265,6 @@ def put_nowait_with_kwa(self, **kwargs):
self.__dic[priority][key] = value
self.__lock.set()

def put_nowait(self, item):
if not item:
return False
if self.__size == self.__capacity:
raise FullHashPQ
if self.__size == self.__capacity:
self.__lock.clear()
self.__lock.wait()
try:
priority = item[0]
key = item[1]
value = item[2]
except IndexError:
try:
priority = DEFAULT_PRIORITY
key = item[0]
value = item[1]
except IndexError:
priority = DEFAULT_PRIORITY
key = DEFAULT_QUEUE
value = item[0]
except:
return False
if priority not in self.__dic.keys():
self.__dic[priority] = OrderedDict()
if key not in self.__dic[priority]:
self.__size += 1
self.__dic[priority][key] = value
self.__lock.set()


class HashPQException(Exception):
pass

Expand All @@ -316,9 +277,8 @@ class FullHashPQ(HashPQException):
class InvalidHashKey(HashPQException):
pass


if __name__ == '__main__':
pq = HashPQueue()
pq = HashPQ()
pq.put_nowait((1, 3, 4))
pq.put_nowait((1, 4, 3))
pq.put_nowait((1, 4, 6))
Expand All @@ -334,7 +294,8 @@ class InvalidHashKey(HashPQException):
pq.put_nowait((8, 4, 7))
pq.put_nowait((3, 6))
pq.put_nowait((2,))
pq.put_nowait_with_kwa(priority=3, value=3)
pq.put_nowait(("2",))
pq.put_nowait_with_kwa(priority=3, value=3, key=3)
pq.get_dic()
print "get_as_list:", pq.get_as_list()
print pq.qsize()
Expand Down
Binary file added HashPQ.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion NEXTPLAN
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ Thu Apr 12 16:30:38 IRDT 2012
# queues have tails and head to easily iterate on it all
# add thread safety to lib
# add priority queue inside code instead import it

# can include any container such as set, stack or even lists.
# use signalling for dic if receive full or empty event.

2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HashMPQ is a multi hash proirity queue.
It can involve multiple queue can handles priorities.

0 comments on commit 5a022b1

Please sign in to comment.