@@ -55,22 +55,19 @@ def cache_key(self, key):
5555
5656 def cache_read (self ):
5757 """Read information from the cache."""
58- if self ._root :
59- keys_key = " keys"
60- else :
61- keys_key = self ._id
58+ keys_key = " keys" if self ._root else self ._id
6259
63- if self ._cache . has_key ( keys_key ) :
60+ if keys_key in self ._cache :
6461 keys = self ._cache [keys_key ].split (" " )
6562 else :
6663 return
6764
6865 for key in keys :
6966 cache_key = self .cache_key (key )
70- if not self ._cached . has_key ( key ) or self ._cached [key ]:
67+ if key not in self ._cached or self ._cached [key ]:
7168 # Key either hasn't been loaded, or is one for the cache
7269 self ._value [key ] = self ._cache [cache_key ]
73- self ._type [key ] = self ._cache [cache_key + " type" ]
70+ self ._type [key ] = self ._cache [f" { cache_key } type" ]
7471 self ._cached [key ] = 1
7572
7673 def cache_write (self , sync = 1 ):
@@ -81,50 +78,42 @@ def cache_write(self, sync=1):
8178 for key in self .keys ():
8279 cache_key = self .cache_key (key )
8380 if not self ._cached [key ]:
84- if self ._cache . has_key ( cache_key ) :
81+ if cache_key in self ._cache :
8582 # Non-cached keys need to be cleared
8683 del self ._cache [cache_key ]
87- del self ._cache [cache_key + " type" ]
84+ del self ._cache [f" { cache_key } type" ]
8885 continue
8986
9087 keys .append (key )
9188 self ._cache [cache_key ] = self ._value [key ]
92- self ._cache [cache_key + " type" ] = self ._type [key ]
93-
94- if self ._root :
95- keys_key = " keys"
96- else :
97- keys_key = self ._id
89+ self ._cache [f"{ cache_key } type" ] = self ._type [key ]
9890
91+ keys_key = " keys" if self ._root else self ._id
9992 self ._cache [keys_key ] = " " .join (keys )
10093 if sync :
10194 self ._cache .sync ()
10295
10396 def cache_clear (self , sync = 1 ):
10497 """Remove information from the cache."""
105- if self ._root :
106- keys_key = " keys"
107- else :
108- keys_key = self ._id
98+ keys_key = " keys" if self ._root else self ._id
10999
110- if self ._cache .has_key (keys_key ):
111- keys = self ._cache [keys_key ].split (" " )
112- del self ._cache [keys_key ]
113- else :
100+ if keys_key not in self ._cache :
114101 return
115102
103+ keys = self ._cache [keys_key ].split (" " )
104+ del self ._cache [keys_key ]
116105 for key in keys :
117106 cache_key = self .cache_key (key )
118107 del self ._cache [cache_key ]
119- del self ._cache [cache_key + " type" ]
108+ del self ._cache [f" { cache_key } type" ]
120109
121110 if sync :
122111 self ._cache .sync ()
123112
124113 def has_key (self , key ):
125114 """Check whether the key exists."""
126115 key = key .replace (" " , "_" )
127- return self ._value . has_key ( key )
116+ return key in self ._value
128117
129118 def key_type (self , key ):
130119 """Return the key type."""
@@ -196,9 +185,8 @@ def set_as_string(self, key, value, cached=1):
196185 def get_as_string (self , key ):
197186 """Return the key as a string value."""
198187 key = key .replace (" " , "_" )
199- if not self .has_key ( key ) :
188+ if key not in self ._value :
200189 raise KeyError (key )
201-
202190 return self ._value [key ]
203191
204192 def set_as_date (self , key , value , cached = 1 ):
@@ -216,11 +204,10 @@ def set_as_date(self, key, value, cached=1):
216204 def get_as_date (self , key ):
217205 """Return the key as a date value."""
218206 key = key .replace (" " , "_" )
219- if not self .has_key ( key ) :
207+ if key not in self ._value :
220208 raise KeyError (key )
221-
222209 value = self ._value [key ]
223- return tuple ([ int (i ) for i in value .split (" " )] )
210+ return tuple (int (i ) for i in value .split (" " ))
224211
225212 def set_as_null (self , key , value , cached = 1 ):
226213 """Set the key to the null value.
@@ -235,13 +222,13 @@ def set_as_null(self, key, value, cached=1):
235222 def get_as_null (self , key ):
236223 """Return the key as the null value."""
237224 key = key .replace (" " , "_" )
238- if not self .has_key ( key ) :
225+ if key not in self ._value :
239226 raise KeyError (key )
240227
241228 def del_key (self , key ):
242229 """Delete the given key."""
243230 key = key .replace (" " , "_" )
244- if not self .has_key ( key ) :
231+ if key not in self ._value :
245232 raise KeyError (key )
246233
247234 del self ._value [key ]
@@ -270,10 +257,9 @@ def __setattr__(self, key, value):
270257 self .set (key , value )
271258
272259 def __getattr__ (self , key ):
273- if self .has_key ( key ) :
260+ if key in self ._value :
274261 return self .get (key )
275- else :
276- raise AttributeError (key )
262+ raise AttributeError (key )
277263
278264
279265def filename (directory , filename ):
@@ -292,13 +278,6 @@ def filename(directory, filename):
292278
293279def utf8 (value ):
294280 """Return the value as a UTF-8 string."""
295- if type (value ) == str :
296- return value .encode ("utf-8" )
297- else :
298- try :
299- return str (value , "utf-8" ).encode ("utf-8" )
300- except UnicodeError :
301- try :
302- return str (value , "iso-8859-1" ).encode ("utf-8" )
303- except UnicodeError :
304- return str (value , "ascii" , "replace" ).encode ("utf-8" )
281+ if isinstance (value , str ):
282+ return value
283+ return value .decode ("utf-8" ) if isinstance (value , bytes ) else str (value )
0 commit comments