The Entry class has it's own state to indicate it can edit or query. It is created with the table keys. So if you use an key that is not in the table keys, it will raise an error.
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 208 209 210 211 212 213 | #!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: lintao
import copy
class Entry(object):
def __init__(self, fields):
self.__fields = fields
# Some inner status
self.__isenableedit = False
self.__isenablequery = False
# The really data to save
self.__realdict = {}
def getfield(self):
return self.__fields
def __getitem__(self, key):
# keep safe to get item
if not self.__isenablequery:
raise Exception, "Please enable query first"
return
pass
if not self.__check_key(key):
raise Exception, "the Key (%s) is not in the Entry"%repr(key)
return self.__realdict.get(key, None)
pass
def __setitem__(self, key, value):
# keep safe to set item
if not self.__isenableedit:
raise Exception, "Please enable edit first"
return
if not self.__check_key(key):
raise Exception, "the Key (%s) is not in the Entry"%repr(key)
self.__realdict[key] = value
pass
def __check_key(self, key):
if not key:
return False
if isinstance(key, str):
return (key in self.__fields)
return all( (i in self.__fields) for i in key)
# use a dict to update self.__realdict
def update(self, indict):
# keep safe to set item
if not self.__isenableedit:
raise Exception, "Please enable edit first"
return
if not self.__check_key(indict.keys()):
raise Exception, "the Key (%s) may be not in the Entry"%repr(key)
self.__realdict.update(indict)
pass
def copydict(self):
return copy.deepcopy(self.__realdict)
# query
def enablequery(self):
if self.__isenablequery:
return
self.disableedit()
self.__isenablequery = True
pass
def disablequery(self):
if not self.__isenablequery:
return
self.__isenablequery = False
pass
# use with statement
class __inner_query(object):
def __init__(self, outer):
self.outer = outer
def __enter__(self):
self.outer.enablequery()
return self.outer
def __exit__(self, exc_type, exc_value, traceback):
self.outer.disablequery()
#self.outer.test_disp_status()
def query(self):
return self.__inner_query(self)
# ###########################################
# edit
def enableedit(self):
if self.__isenableedit:
return
# also to enable query
self.enablequery()
self.__isenableedit = True
pass
pass
def disableedit(self):
if not self.__isenableedit:
return
self.__isenableedit = False
self.disablequery()
pass
pass
# with statement
class __inner_edit(object):
def __init__(self, outer):
self.outer = outer
def __enter__(self):
self.outer.enableedit()
return self.outer
def __exit__(self, exc_type, exc_value, traceback):
self.outer.disableedit()
#self.outer.test_disp_status()
def edit(self):
return self.__inner_edit(self)
# ###########################################
def test_check_key(self, key):
return self.__check_key(key)
def test_disp_status(self):
print "__isenableedit", self.__isenableedit
print "__isenablequery", self.__isenablequery
pass
pass
if __name__ == '__main__':
keys = ["id", "name", "age"]
notkeys = ["not_%s"%i for i in keys]
oneentry = Entry(keys)
for i in keys:
print oneentry.test_check_key(i)
for i in notkeys:
print oneentry.test_check_key(i)
oneentry.enableedit()
oneentry.test_disp_status()
oneentry["id"] = 10
oneentry["name"] = "lintao"
oneentry["age"] = 22
oneentry.disableedit()
oneentry.test_disp_status()
oneentry.enablequery()
oneentry.test_disp_status()
print oneentry["id"]
print oneentry["name"]
print oneentry["age"]
oneentry.disablequery()
oneentry.test_disp_status()
# Exception
#print oneentry["age"]
#oneentry["name"] = "lintao"
# with statement
print "with statement:"
with oneentry.edit():
oneentry["id"] = 2
oneentry["name"] = "lintao51"
oneentry["age"] = 22
#oneentry["noage"] = "no"
with oneentry.query():
print oneentry["id"]
print oneentry["name"]
print oneentry["age"]
#print oneentry["noage"]
newdict = {"id":"3","name":"lint","age":"22"}
with oneentry.edit():
oneentry.update(newdict)
with oneentry.query():
print oneentry["id"]
print oneentry["name"]
print oneentry["age"]
def test_query(entry):
return entry.query()
with test_query(oneentry):
print oneentry["id"]
print oneentry["name"]
print oneentry["age"]
print oneentry.copydict()
# use class
#print "use class"
#class test_query_2(object):
# def __init__(self, entry):
# self.entry = entry
# def __enter__(self):
# print "enter in test_query_2"
# self.entry.enablequery()
# print "self.entry.enablequery"
# return self.entry #.query()
# def __exit__(self, exc_type, exc_value, traceback):
# self.entry.disablequery()
# print "self.entry.disablequery"
# print "exit from test_query_2"
#with test_query_2(oneentry) as f:
# #with f.query():
# print f["id"]
pass
|