ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Tools
PyUtils
python
dbsqlite.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3
# @file PyUtils/python/dbsqlite.py
4
# reaped off: http://svn.python.org/view/sandbox/trunk/dbm_sqlite
5
"""Dbm based on sqlite -- Needed to support shelves
6
7
Issues:
8
9
# ??? how to coordinate with whichdb
10
# ??? Any difference between blobs and text
11
# ??? does default encoding affect str-->bytes or PySqlite3 always use UTF-8
12
# ??? what is the correct isolation mode
13
14
"""
15
16
__all__ = [
'error'
,
'open'
]
17
18
import
sqlite3
19
import
pickle
20
from
collections.abc
import
MutableMapping
21
from
operator
import
itemgetter
22
import
shelve
23
24
error = sqlite3.DatabaseError
25
26
class
SQLhash
(MutableMapping):
27
def
__init__
(self, filename=':memory:
', flags='
r', mode=None):
28
# XXX add flag/mode handling
29
# c -- create if it doesn't exist
30
# n -- new empty
31
# w -- open existing
32
# r -- readonly
33
if
'n'
in
flags:
34
import
os
35
if
os.path.exists(filename):
36
os.remove(filename)
37
38
MAKE_SHELF =
'CREATE TABLE IF NOT EXISTS shelf (key TEXT PRIMARY KEY NOT NULL, value BLOB)'
39
self.
conn
= sqlite3.connect(filename)
40
self.
conn
.text_factory = str
41
if
'r'
not
in
flags
or
filename==
':memory:'
:
42
self.
conn
.execute(MAKE_SHELF)
43
self.
conn
.commit()
44
45
def
__len__
(self):
46
GET_LEN =
'SELECT COUNT(*) FROM shelf'
47
return
self.
conn
.execute(GET_LEN).fetchone()[0]
48
49
def
__bool__
(self):
50
# returns None if count is zero
51
GET_BOOL =
'SELECT MAX(ROWID) FROM shelf'
52
return
self.
conn
.execute(GET_BOOL).fetchone()[0]
is
not
None
53
54
def
keys
(self):
55
return
list(self.
iterkeys
())
56
57
def
values
(self):
58
return
list(self.
itervalues
())
59
60
def
items
(self):
61
return
list(self.
iteritems
())
62
63
def
__iter__
(self):
64
return
self.
iterkeys
()
65
66
def
iterkeys
(self):
67
GET_KEYS =
'SELECT key FROM shelf ORDER BY ROWID'
68
return
SQLHashKeyIterator
(self.
conn
, GET_KEYS, (0,))
69
70
def
itervalues
(self):
71
GET_VALUES =
'SELECT value FROM shelf ORDER BY ROWID'
72
return
SQLHashValueIterator
(self.
conn
, GET_VALUES, (0,))
73
74
def
iteritems
(self):
75
GET_ITEMS =
'SELECT key, value FROM shelf ORDER BY ROWID'
76
return
SQLHashItemIterator
(self.
conn
, GET_ITEMS, (0, 1))
77
78
def
__contains__
(self, key):
79
HAS_ITEM =
'SELECT 1 FROM shelf WHERE key = ?'
80
return
self.
conn
.execute(HAS_ITEM, (key,)).fetchone()
is
not
None
81
82
def
__getitem__
(self, key):
83
GET_ITEM =
'SELECT value FROM shelf WHERE key = ?'
84
item = self.
conn
.execute(GET_ITEM, (key,)).fetchone()
85
if
item
is
None
:
86
raise
KeyError(key)
87
88
return
pickle.loads(item[0])
89
90
def
__setitem__
(self, key, value):
91
ADD_ITEM =
'REPLACE INTO shelf (key, value) VALUES (?,?)'
92
value = pickle.dumps(value)
93
self.
conn
.execute(ADD_ITEM, (key, value))
94
#self.conn.commit()
95
96
def
__delitem__
(self, key):
97
if
key
not
in
self:
98
raise
KeyError(key)
99
DEL_ITEM =
'DELETE FROM shelf WHERE key = ?'
100
self.
conn
.execute(DEL_ITEM, (key,))
101
#self.conn.commit()
102
103
def
update
(self, items=(), **kwds):
104
try
:
105
items = items.items()
106
items = [(k,pickle.dumps(v))
for
k,v
in
items]
107
except
AttributeError:
108
pass
109
110
UPDATE_ITEMS =
'REPLACE INTO shelf (key, value) VALUES (?, ?)'
111
self.
conn
.executemany(UPDATE_ITEMS, items)
112
self.
conn
.commit()
113
if
kwds:
114
self.
update
(kwds)
115
116
def
clear
(self):
117
CLEAR_ALL =
'DELETE FROM shelf; VACUUM;'
118
self.
conn
.executescript(CLEAR_ALL)
119
self.
conn
.commit()
120
121
def
sync
(self):
122
if
self.
conn
is
not
None
:
123
self.
conn
.commit()
124
125
def
close
(self):
126
if
self.
conn
is
not
None
:
127
self.
conn
.commit()
128
self.
conn
.
close
()
129
self.
conn
=
None
130
131
def
__del__
(self):
132
self.
close
()
133
134
def
open
(file=None, *args, **kw):
135
if
file
is
not
None
:
136
return
SQLhash
(file, *args, **kw)
137
return
SQLhash
()
138
139
def
open_shelf
(file=None, *args, **kw):
140
_db =
open
(file, *args, **kw)
141
return
shelve.Shelf(_db)
142
143
class
SQLHashKeyIterator
(object):
144
def
__init__
(self, conn, stmt, indices):
145
c = conn.cursor()
146
c.execute(stmt)
147
148
self.
iter
=
iter
(c)
149
self.
getter
= itemgetter(*indices)
150
151
def
__iter__
(self):
152
return
self
153
154
def
next
(self):
#py2
155
return
self.
getter
(self.
iter
.
next
())
156
def
__next__
(self):
#py3
157
return
self.
getter
(self.
iter
.
__next__
())
158
159
class
SQLHashValueIterator
(object):
160
def
__init__
(self, conn, stmt, indices):
161
c = conn.cursor()
162
c.execute(stmt)
163
164
self.
iter
=
iter
(c)
165
self.
getter
= itemgetter(*indices)
166
167
def
__iter__
(self):
168
return
self
169
170
def
next
(self):
#py2
171
o = self.
getter
(self.
iter
.
next
())
172
return
pickle.loads(o)
173
def
__next__
(self):
#py3
174
o = self.
getter
(self.
iter
.
__next__
())
175
return
pickle.loads(o)
176
177
class
SQLHashItemIterator
(object):
178
def
__init__
(self, conn, stmt, indices):
179
c = conn.cursor()
180
c.execute(stmt)
181
182
self.
iter
=
iter
(c)
183
self.
getter
= itemgetter(*indices)
184
185
def
__iter__
(self):
186
return
self
187
188
def
next
(self):
#py2
189
o = self.
getter
(self.
iter
.
next
())
190
k = o[0]
191
v = pickle.loads(o[1])
192
return
(k,v)
193
def
__next__
(self):
#py3
194
o = self.
getter
(self.
iter
.
__next__
())
195
k = o[0]
196
v = pickle.loads(o[1])
197
return
(k,v)
198
199
if
__name__
in
'__main___'
:
200
for
d
in
SQLhash
(flags=
'n'
),
SQLhash
(
'example'
,flags=
'n'
):
201
list(d)
202
print
(list(d),
"start"
)
203
d[
'abc'
] =
'lmno'
204
print
(d[
'abc'
])
205
d[
'abc'
] =
'rsvp'
206
d[
'xyz'
] =
'pdq'
207
print
(d.items())
208
print
(d.values())
209
print
(
'***'
, d.keys())
210
print
(list(d),
'list'
)
211
d.update(p=
'x'
, q=
'y'
, r=
'z'
)
212
print
(d.items())
213
214
del d[
'abc'
]
215
try
:
216
print
(d[
'abc'
])
217
except
KeyError:
218
pass
219
else
:
220
raise
Exception(
'oh noooo!'
)
221
222
try
:
223
del d[
'abc'
]
224
except
KeyError:
225
pass
226
else
:
227
raise
Exception(
'drat!'
)
228
229
print
(list(d))
230
print
(bool(d),
True
)
231
d.clear()
232
print
(bool(d),
False
)
233
print
(list(d))
234
d.update(p=
'x'
, q=
'y'
, r=
'z'
)
235
print
(list(d))
236
d[
'xyz'
] =
'pdq'
237
238
d[
'a_list'
] = range(5)
239
print
(d[
'a_list'
])
240
241
d[
'a_dict'
] = {1:
'one'
,2:
'two'
}
242
print
(d[
'a_dict'
])
243
244
d[
'a_tuple'
] = (1,2,3,4)
245
print
(d[
'a_tuple'
])
246
247
print
()
248
d.close()
clear
void clear()
Empty the pool.
print
void print(char *figname, TCanvas *c1)
Definition
TRTCalib_StrawStatusPlots.cxx:26
python.dbsqlite.SQLHashItemIterator
Definition
dbsqlite.py:177
python.dbsqlite.SQLHashItemIterator.__next__
__next__(self)
Definition
dbsqlite.py:193
python.dbsqlite.SQLHashItemIterator.getter
getter
Definition
dbsqlite.py:183
python.dbsqlite.SQLHashItemIterator.iter
iter
Definition
dbsqlite.py:182
python.dbsqlite.SQLHashItemIterator.__iter__
__iter__(self)
Definition
dbsqlite.py:185
python.dbsqlite.SQLHashItemIterator.next
next(self)
Definition
dbsqlite.py:188
python.dbsqlite.SQLHashItemIterator.__init__
__init__(self, conn, stmt, indices)
Definition
dbsqlite.py:178
python.dbsqlite.SQLHashKeyIterator
Definition
dbsqlite.py:143
python.dbsqlite.SQLHashKeyIterator.iter
iter
Definition
dbsqlite.py:148
python.dbsqlite.SQLHashKeyIterator.next
next(self)
Definition
dbsqlite.py:154
python.dbsqlite.SQLHashKeyIterator.__iter__
__iter__(self)
Definition
dbsqlite.py:151
python.dbsqlite.SQLHashKeyIterator.__next__
__next__(self)
Definition
dbsqlite.py:156
python.dbsqlite.SQLHashKeyIterator.getter
getter
Definition
dbsqlite.py:149
python.dbsqlite.SQLHashKeyIterator.__init__
__init__(self, conn, stmt, indices)
Definition
dbsqlite.py:144
python.dbsqlite.SQLHashValueIterator
Definition
dbsqlite.py:159
python.dbsqlite.SQLHashValueIterator.getter
getter
Definition
dbsqlite.py:165
python.dbsqlite.SQLHashValueIterator.iter
iter
Definition
dbsqlite.py:164
python.dbsqlite.SQLHashValueIterator.__init__
__init__(self, conn, stmt, indices)
Definition
dbsqlite.py:160
python.dbsqlite.SQLHashValueIterator.next
next(self)
Definition
dbsqlite.py:170
python.dbsqlite.SQLHashValueIterator.__next__
__next__(self)
Definition
dbsqlite.py:173
python.dbsqlite.SQLHashValueIterator.__iter__
__iter__(self)
Definition
dbsqlite.py:167
python.dbsqlite.SQLhash
Definition
dbsqlite.py:26
python.dbsqlite.SQLhash.__bool__
__bool__(self)
Definition
dbsqlite.py:49
python.dbsqlite.SQLhash.sync
sync(self)
Definition
dbsqlite.py:121
python.dbsqlite.SQLhash.__del__
__del__(self)
Definition
dbsqlite.py:131
python.dbsqlite.SQLhash.itervalues
itervalues(self)
Definition
dbsqlite.py:70
python.dbsqlite.SQLhash.values
values(self)
Definition
dbsqlite.py:57
python.dbsqlite.SQLhash.__len__
__len__(self)
Definition
dbsqlite.py:45
python.dbsqlite.SQLhash.__contains__
__contains__(self, key)
Definition
dbsqlite.py:78
python.dbsqlite.SQLhash.__delitem__
__delitem__(self, key)
Definition
dbsqlite.py:96
python.dbsqlite.SQLhash.update
update(self, items=(), **kwds)
Definition
dbsqlite.py:103
python.dbsqlite.SQLhash.iterkeys
iterkeys(self)
Definition
dbsqlite.py:66
python.dbsqlite.SQLhash.iteritems
iteritems(self)
Definition
dbsqlite.py:74
python.dbsqlite.SQLhash.keys
keys(self)
Definition
dbsqlite.py:54
python.dbsqlite.SQLhash.__iter__
__iter__(self)
Definition
dbsqlite.py:63
python.dbsqlite.SQLhash.__setitem__
__setitem__(self, key, value)
Definition
dbsqlite.py:90
python.dbsqlite.SQLhash.items
items(self)
Definition
dbsqlite.py:60
python.dbsqlite.SQLhash.conn
conn
Definition
dbsqlite.py:39
python.dbsqlite.SQLhash.close
close(self)
Definition
dbsqlite.py:125
python.dbsqlite.SQLhash.__init__
__init__(self, filename=':memory:', flags='r', mode=None)
Definition
dbsqlite.py:27
python.dbsqlite.SQLhash.__getitem__
__getitem__(self, key)
Definition
dbsqlite.py:82
collections.abc
python.dbsqlite.open
open(file=None, *args, **kw)
Definition
dbsqlite.py:134
python.dbsqlite.open_shelf
open_shelf(file=None, *args, **kw)
Definition
dbsqlite.py:139
Generated on
for ATLAS Offline Software by
1.17.0