-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmousedb.py
147 lines (110 loc) · 4.79 KB
/
mousedb.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
# -*- coding: utf-8 -*-
# Copyright (c) 2016, Zhang Te
# All rights reserved.
import os
import json
def load(location,option = True):
return mousedb(location,option)
class mousedb:
def __init__(self,location,option = True):
''' Creates a database object with the location.If the file dose not exist
it will be created on the frist update.If the option is True then the file
will be stored disk ,else it is still stay in memory.
--------------------------------------------------------------------------
根据输入的路径实例化一个数据库对象,如果路径不存在则在第一次实例化时新建文件
否则只是打开文件。option参数如果是True则数据存储与磁盘,False则存储于内存。
'''
self.loco = location
self.fsave = option
self.abs_location = os.path.abspath(location)
self.KEY_VALUE = 0
self.items = None
if os.path.exists(self.abs_location):
self._loaddb()
else:
self.db = {}
self._dumpdb()
def createtable(self,items):
''' Create a table in database object,and a object will support only one table.
The 0 of index in items is the key value.
-------------------------------------------------------------------------------
在一个数据库对象中建立一张表,并且一个对象管理只能是一张表。其中items第一个
元素为主键。
'''
self.items = items
self.db[u'items'] = items #items is a tuple.
self.db[items[self.KEY_VALUE]] = {}
self._dumpdb()
def intable(self,key):
if key in self.db[self.items[self.KEY_VALUE]]:
return True
else:
return False
def insertrow(self,data):
''' Insetr a row data into table.And data is a tuple.
---------------------------------
向表中插入一行数据,data是一个元组。
'''
row = dict(zip(self.items[1:],data[1:]))
self.db[self.items[self.KEY_VALUE]][data[0]] = row
self._dumpdb()
def update(self,key,item,value):
''' Update a value of the special key and item.
------------------------------------------------
根据主键和索引更新数据。
'''
if key in self.db[self.items[self.KEY_VALUE]]:
#curr = self.db[self.items[self.KEY_VALUE]][key]
if item in self.db[self.items[self.KEY_VALUE]][key]:
self.db[self.items[self.KEY_VALUE]][key][item] = value
self._dumpdb()
return True
return False
def delrow(self,key):
''' Delete a row.
-----------------
删除一行。
'''
if self.db[self.items[self.KEY_VALUE]].has_key(key):
self.db[self.items[self.KEY_VALUE]].pop(key)
self._dumpdb()
def findrow(self,key):
''' Find the special row.
-------------------------
查找指定行。
'''
if key in self.db[self.items[self.KEY_VALUE]]:
return {key:self.db[self.items[self.KEY_VALUE]][key]}
def findvalue(self,key,item):
''' Find a value reply on key and item.
---------------------------------------
根据主键值和表项查找值
'''
if key in self.db[self.items[self.KEY_VALUE]] and item in self.db[self.items[self.KEY_VALUE]][key]:
return self.db[self.items[self.KEY_VALUE]][key][item]
def printtable(self):
''' Print database with json fromat.
------------------------------------
打印表。
'''
if self.fsave:
with open(self.abs_location,'r') as f:
data = json.loads(f.read())
print (data)
def _loaddb(self):
''' Load or reload the json info from the file.
----------------------------------------------
从文件中以加载数据到内存中,并将json格式数据
进行解析。
'''
with open(self.abs_location,'r') as f:
self.db = json.loads(f.read())
self.items = self.db['items']
def _dumpdb(self):
''' Write/save reload the json dump into the file.
--------------------------------------------------
将数据库数据生成json格式并存入磁盘文件。
'''
if self.fsave:
with open(self.abs_location,'w') as f:
f.write(json.dumps(self.db,ensure_ascii=True))