@@ -29,13 +29,13 @@ def __enter__(self):
2929 self .data_handle = {}
3030
3131 def __exit__ (self , type , value , tb ):
32- wl = getattr (self , "write_lock" , None )
33- if wl is not None :
34- if isinstance (wl , list ):
35- for lock in wl :
32+ write_lock = getattr (self , "write_lock" , None )
33+ if write_lock is not None :
34+ if isinstance (write_lock , list ):
35+ for lock in write_lock :
3636 lock ._unlock ()
3737 else :
38- wl ._unlock ()
38+ write_lock ._unlock ()
3939 self .write_lock , self .in_session = None , False
4040
4141 def write (self ):
@@ -46,12 +46,10 @@ def write(self):
4646
4747class SessionFileFull (SessionBase , Generic [T ]):
4848 """
49- Enter:
50- >>> with DDBSession(db_name) as (session, data):
49+ Context manager for read-write access to a full file.
5150
52- Where `data` is the dict that was read from the filesystem. Modify
53- `data` and call session.write() to save changes. If you don't call it,
54- the changes will be lost after exiting the with statement.
51+ Efficiency:
52+ Reads and writes the entire file.
5553 """
5654
5755 def __init__ (self , db_name : str , as_type : T = None ):
@@ -76,12 +74,12 @@ def write(self):
7674
7775class SessionFileKey (SessionBase , Generic [T ]):
7876 """
79- Enter:
80- >>> with DDBSession(db_name) as (session, data):
77+ Context manager for read-write access to a single key-value item in a file.
8178
82- Where `data` is the dict that was read from the filesystem. Modify
83- `data` and call session.write() to save changes. If you don't call it,
84- the changes will be lost after exiting the with statement.
79+ Efficiency:
80+ Uses partial reading, which allows only reading the bytes of the key-value item.
81+ When writing, only the bytes of the key-value and the bytes of the file after
82+ the key-value are written.
8583 """
8684
8785 def __init__ (self , db_name : str , key : str , as_type : T = None ):
@@ -108,14 +106,13 @@ def write(self):
108106
109107class SessionFileWhere (SessionBase , Generic [T ]):
110108 """
111- Enter:
112- >>> with DDBSession(db_name) as (session, data):
109+ Context manager for read-write access to selection of key-value items in a file.
110+ The where callable is called with the key and value of each item in the file.
113111
114- Where `data` is the dict that was read from the filesystem. Modify
115- `data` and call session.write() to save changes. If you don't call it,
116- the changes will be lost after exiting the with statement .
112+ Efficiency:
113+ Reads and writes the entire file, so it is not more efficient than
114+ SessionFileFull .
117115 """
118-
119116 def __init__ (self , db_name : str , where : Callable [[Any , Any ], bool ], as_type : T = None ):
120117 super ().__init__ (db_name , as_type )
121118 self .where = where
@@ -143,14 +140,13 @@ def write(self):
143140
144141class SessionDirFull (SessionBase , Generic [T ]):
145142 """
146- Enter:
147- >>> with DDBSession(db_name) as (session, data):
143+ Context manager for read-write access to all files in a directory.
144+ They are provided as a dict of {str(file_name): dict(file_content)}, where the
145+ file name does not contain the directory name nor the file extension.
148146
149- Where `data` is the dict that was read from the filesystem. Modify
150- `data` and call session.write() to save changes. If you don't call it,
151- the changes will be lost after exiting the with statement.
147+ Efficiency:
148+ Fully reads and writes all files.
152149 """
153-
154150 def __init__ (self , db_name : str , as_type : T = None ):
155151 super ().__init__ (utils .find_all (db_name ), as_type )
156152
@@ -175,14 +171,12 @@ def write(self):
175171
176172class SessionDirWhere (SessionBase , Generic [T ]):
177173 """
178- Enter:
179- >>> with DDBSession(db_name) as (session, data):
174+ Context manager for read-write access to selection of files in a directory.
175+ The where callable is called with the file name and parsed content of each file.
180176
181- Where `data` is the dict that was read from the filesystem. Modify
182- `data` and call session.write() to save changes. If you don't call it,
183- the changes will be lost after exiting the with statement.
177+ Efficiency:
178+ Fully reads all files, but only writes the selected files.
184179 """
185-
186180 def __init__ (self , db_name : str , where : Callable [[Any , Any ], bool ], as_type : T = None ):
187181 super ().__init__ (utils .find_all (db_name ), as_type )
188182 self .where = where
0 commit comments