-
Notifications
You must be signed in to change notification settings - Fork 7
/
python3pickledb.py
207 lines (173 loc) · 6.47 KB
/
python3pickledb.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Copyright (c) 2011, Harrison Erd
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# Neither the name of the Harrison Erd nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "
# AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
import os
import json
import io
"""
This version of pickleDB is has not been tested. It should only be used for
testing or development. It was made to work with Python 3.x. There have been
some issues with Python 2.x. python3pickledb.py written by olivierlemoal.
Please report issues to bitbucket (patx/pickledb).
"""
def load(location, option):
'''Return a pickledb object. location is the path to the json file.'''
return pickledb(location, option)
class pickledb(object):
def __init__(self, location, option):
'''Creates a database object and loads the data from the location path.
If the file does not exist it will be created on the first update.'''
self.load(location, option)
def load(self, location, option):
'''Loads, reloads or changes the path to the db file.
DO NOT USE this method has it may be deprecated in the future.'''
location = os.path.expanduser(location)
self.loco = location
self.fsave = option
if os.path.exists(location):
self._loaddb()
else:
self.db = {}
return True
def dump(self):
'''Force dump memory db to file.'''
self._dumpdb(True)
return True
def set(self, key, value):
'''Set the (string,int,whatever) value of a key'''
self.db[key] = value
self._dumpdb(self.fsave)
return True
def get(self, key):
'''Get the value of a key'''
try:
return self.db[key]
except KeyError:
return None
def getall(self):
'''Return a list of all keys in db'''
return self.db.keys()
def rem(self, key):
'''Delete a key'''
del self.db[key]
self._dumpdb(self.fsave)
return True
def lcreate(self, name):
'''Create a list'''
self.db[name] = []
self._dumpdb(self.fsave)
return True
def ladd(self, name, value):
'''Add a value to a list'''
self.db[name].append(value)
self._dumpdb(self.fsave)
return True
def lgetall(self, name):
'''Return all values in a list'''
return self.db[name]
def lget(self, name, pos):
'''Return one value in a list'''
return self.db[name][pos]
def lrem(self, name):
'''Remove a list and all of its values'''
number = len(self.db[name])
del self.db[name]
self._dumpdb(self.fsave)
return number
def lpop(self, name, pos):
'''Remove one value in a list'''
value = self.db[name][pos]
del self.db[name][pos]
self._dumpdb(self.fsave)
return value
def llen(self, name):
'''Returns the length of the list'''
return len(self.db[name])
def append(self, key, more):
'''Add more to a key's value'''
tmp = self.db[key]
self.db[key] = ('%s%s' % (tmp, more))
self._dumpdb(self.fsave)
return True
def lappend(self, name, pos, more):
'''Add more to a value in a list'''
tmp = self.db[name][pos]
self.db[name][pos] = ('%s%s' % (tmp, more))
self._dumpdb(self.fsave)
return True
def dcreate(self, name):
'''Create a dict'''
self.db[name] = {}
self._dumpdb(self.fsave)
return True
def dadd(self, name, pair):
'''Add a key-value pair to a dict, "pair" is a tuple'''
self.db[name][pair[0]] = pair[1]
self._dumpdb(self.fsave)
return True
def dget(self, name, key):
'''Return the value for a key in a dict'''
return self.db[name][key]
def dgetall(self, name):
'''Return all key-value pairs from a dict'''
return self.db[name]
def drem(self, name):
'''Remove a dict and all of its pairs'''
del self.db[name]
self._dumpdb(self.fsave)
return True
def dpop(self, name, key):
'''Remove one key-value in a dict'''
value = self.db[name][key]
del self.db[name][key]
self._dumpdb(self.fsave)
return value
def dkeys(self, name):
'''Return all the keys for a dict'''
return self.db[name].keys()
def dvals(self, name):
'''Return all the values for a dict'''
return self.db[name].values()
def dexists(self, name, key):
'''Determine if a key exists or not'''
if self.db[name][key] is not None:
return 1
else:
return 0
def deldb(self):
'''Delete everything from the database'''
self.db = {}
self._dumpdb(self.fsave)
return True
def _loaddb(self):
'''Load or reload the json info from the file'''
self.db = json.load(io.open(self.loco, 'r', encoding='utf-8'))
def _dumpdb(self, forced):
'''Dump (write, save) the json dump into the file'''
if forced:
f = io.open(self.loco, 'w', encoding='utf-8')
f.write(json.dumps(self.db, ensure_ascii=False))