Skip to content

Commit

Permalink
add files and functoinality
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan authored and Erfan committed Apr 12, 2012
1 parent 32c670b commit 73a6c2e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGLELOG
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions NEXTPLAN
Original file line number Diff line number Diff line change
@@ -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


Empty file added TODO
Empty file.
77 changes: 57 additions & 20 deletions hashpq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 73a6c2e

Please sign in to comment.