-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdq.py
More file actions
71 lines (61 loc) · 2.22 KB
/
Copy pathpdq.py
File metadata and controls
71 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from os.path import basename
try:
import apsw
except ImportError as exc:
sys.stderr.write("Error: failed to import apsw module ({})".format(exc))
class pdq(object):
""" Priority disk queue, based on sqlite.
"""
# TODO add multiple put()
# fix iterators
# consume()
# an index
def __init__(self,filename):
""" Initialise sqlite database with filename
"""
self.pdq=apsw.Connection(filename)
self.pdq.setbusytimeout(2000)
self.filename=self.pdq.filename
self.name=basename(self.filename)
pdq_table=self.pdq.cursor().execute("SELECT name FROM sqlite_master WHERE type='table' AND name='pdq'").fetchall()
if not pdq_table:
self._create()
def _create(self):
""" Local function to initalise database
"""
with self.pdq:
c=self.pdq.cursor()
c.execute('CREATE TABLE pdq (item blob,priority int)')
c.execute('CREATE INDEX priority_index ON pdq (priority)')
def _toiter(self,item):
if not hasattr(item,'__iter__'):
item=[item]
return item
def put(self,items,priority=0):
""" Put item(s) on the queue with optional priority
"""
with self.pdq:
self.pdq.cursor().executemany('insert into pdq values (?,?)',[(item,priority) for item in self._toiter(items)])
def get(self,number=1):
""" get list of item(s) from the queue based on priority
"""
with self.pdq:
c=self.pdq.cursor()
l=list(c.execute("select rowid,item from pdq order by priority desc limit ?",(number,)))
c.executemany("delete from pdq where rowid = ?",[(rowid,) for rowid,item in l])
return [link for rowid,link in l]
def count(self):
""" count item(s) on the queue
"""
with self.pdq:
(count,)=self.pdq.cursor().execute('select count(*) from pdq').next()
return count
def vacuum(self):
""" count item(s) on the queue
"""
self.pdq.cursor().execute('vacuum')
def clear(self):
""" count item(s) on the queue
"""
self.pdq.cursor().execute('drop table pdq')
self._create()