Skip to content

Commit 0b90200

Browse files
committed
wip
1 parent 56f9f94 commit 0b90200

3 files changed

Lines changed: 40 additions & 40 deletions

File tree

dictdatabase/sessions.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4747
class 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

7775
class 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

109107
class 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

144141
class 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

176172
class 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

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def use_test_dir(request):
1313

1414

1515

16+
@pytest.fixture(scope="function")
17+
def name_of_test(request):
18+
return request.function.__name__
19+
20+
21+
1622
@pytest.fixture(params=[True, False])
1723
def use_compression(request):
1824
DDB.config.use_compression = request.param

tests/test_create.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
from tests.utils import make_complex_nested_random_dict
77

88

9-
def test_create(use_test_dir, use_compression, use_orjson, indent):
10-
DDB.at("create").create(force_overwrite=True)
11-
db = DDB.at("create").read()
9+
def test_create(use_test_dir, name_of_test, use_compression, use_orjson, indent):
10+
DDB.at(name_of_test).create(force_overwrite=True)
11+
db = DDB.at(name_of_test).read()
1212
assert db == {}
1313

14-
with DDB.at("create").session(as_type=pd) as (session, d):
14+
with DDB.at(name_of_test).session(as_type=pd) as (session, d):
1515
d["a", "b", "c"] = "😁"
1616
session.write()
17-
assert DDB.at("create").read() == {"a": {"b": {"c": "😁"}}}
17+
assert DDB.at(name_of_test).read() == {"a": {"b": {"c": "😁"}}}
1818

1919
with pytest.raises(RuntimeError):
20-
DDB.at("create", where=lambda k, v: True).create(force_overwrite=True)
20+
DDB.at(name_of_test, where=lambda k, v: True).create(force_overwrite=True)
2121

2222
with pytest.raises(RuntimeError):
23-
DDB.at("create", key="any").create(force_overwrite=True)
23+
DDB.at(name_of_test, key="any").create(force_overwrite=True)
2424

2525

2626
def test_create_edge_cases(use_test_dir, use_compression, use_orjson, indent):

0 commit comments

Comments
 (0)