From 73a6c2e7ea5c83bb049b8e7f42b628bc44bde4e6 Mon Sep 17 00:00:00 2001 From: Erfan Date: Thu, 12 Apr 2012 16:39:03 +0430 Subject: [PATCH] add files and functoinality --- CHANGLELOG | 7 +++++ NEXTPLAN | 14 ++++++++++ TODO | 0 hashpq.py | 77 ++++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 CHANGLELOG create mode 100644 NEXTPLAN create mode 100644 TODO diff --git a/CHANGLELOG b/CHANGLELOG new file mode 100644 index 0000000..1392289 --- /dev/null +++ b/CHANGLELOG @@ -0,0 +1,7 @@ +At: pr 12 16:30:38 IRDT 2012 +By: Erfankam +Do: + 1. add keyword argument passing to put_nowait func + 2. fix a bug when we have just a value + 3. add changelog and todo files in project + 4. add future plan to project diff --git a/NEXTPLAN b/NEXTPLAN new file mode 100644 index 0000000..38dabd0 --- /dev/null +++ b/NEXTPLAN @@ -0,0 +1,14 @@ +Thu Apr 12 16:30:38 IRDT 2012 +# TODO or FIXME +# work on exceptions during get or put +# about return values list or tuples or simple variables +# use oredered dict instead of dic for all dic +# work on exception +# how to return items just values or both +# use keywordarguments instead of positional ones +# override functions by some options +# queues have tails and head to easily iterate on it all +# add thread safety to lib +# add priority queue inside code instead import it + + diff --git a/TODO b/TODO new file mode 100644 index 0000000..e69de29 diff --git a/hashpq.py b/hashpq.py index 7b5086e..591cd43 100644 --- a/hashpq.py +++ b/hashpq.py @@ -3,17 +3,14 @@ import time import Queue + try: from collections import OrderedDict except: # If could not, import it from local directory from OrderedDict import OrderedDict -# TODO of FIXME -# work on exceptions during get or put -# about return values list or tuples or simple variables -# use oredered dict instead of dic for all dic -# + DEFAULT_PRIORITY = 0 DEFAULT_QUEUE = 0 @@ -96,6 +93,9 @@ def get_by_key(self, key, priority=None): def pick(self): pass + def pick_nowait(self): + pass + def get_as_list(self): queue_list = self.__dic.keys() queue_list.sort(reverse=True) @@ -181,15 +181,18 @@ def put(self, item): key = item[1] value = item[2] except IndexError: - priority = DEFAULT_PRIORITY - key = item[0] - value = item[1] - except IndexError: - priority = DEFAULT_PRIORITY - key = DEFAULT_QUEUE - value = item[0] + try: + priority = DEFAULT_PRIORITY + key = item[0] + value = item[1] + except IndexError: + priority = DEFAULT_PRIORITY + key = DEFAULT_QUEUE + value = item[0] except ValueError: return False + except: + return False if priority not in self.__dic.keys(): self.__dic[priority] = OrderedDict() if key not in self.__dic[priority]: @@ -215,6 +218,37 @@ def get_nowait(self): del self.__dic[highestPriority] return item[1] + def put_nowait_with_kwa(self, **kwargs): + if not kwargs: + return False + if self.__size == self.__capacity: + return False + if self.__size == self.__capacity: + self.__lock.clear() + self.__lock.wait() + if 'priority' in kwargs: + priority = kwargs['priority'] + else: + priority = DEFAULT_PRIORITY + + if 'key' in kwargs: + key = kwargs['key'] + else: + key = DEFAULT_QUEUE + + if 'value' in kwargs: + value = kwargs['value'] + else: + raise Exception + 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() + def put_nowait(self, item): if not item: return False @@ -228,14 +262,15 @@ def put_nowait(self, item): key = item[1] value = item[2] except IndexError: - priority = DEFAULT_PRIORITY - key = item[0] - value = item[1] - except IndexError: - priority = DEFAULT_PRIORITY - key = DEFAULT_QUEUE - value = item[0] - except ValueError: + 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() @@ -260,6 +295,8 @@ def put_nowait(self, item): pq.put_nowait((5, 4, 6)) 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.get_dic() print "get_as_list:", pq.get_as_list() print pq.qsize()