Skip to content

Commit b5db560

Browse files
committed
Doc Improvements
1 parent 8d68feb commit b5db560

File tree

6 files changed

+90
-51
lines changed

6 files changed

+90
-51
lines changed

.github/workflows/ci-workflow.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
name: Python CI
22

33
on:
4-
push: {}
4+
push:
5+
branches:
6+
- master
57
pull_request:
68
branches: [ master ]
79

.github/workflows/publish-workflow.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ jobs:
1111
runs-on: ubuntu-latest
1212

1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v3
1515
- name: Set up Python
16-
uses: actions/setup-python@v2
16+
uses: actions/setup-python@v3
1717
with:
1818
python-version: '3.x'
1919
- name: Install dependencies

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ I will try to keep up with the latest version of `TinyDB`.
2424

2525
# New Features
2626

27-
* **Event hooks**: You can now use event hooks to do something before or after an operation. See [Event Hooks](#event-hooks) for more details.
27+
* **Event Hooks**: You can now use event hooks to do something before or after an operation. See [Event Hooks](#event-hooks) for more details.
2828

29-
* **Redesigned ID & Doc class**: You can [replace](#replacing-id-&-document-class) and [customise them](#customise-id-class) more pleasingly.
29+
* **Redesigned ID & Doc Class**: You can [replace](#replacing-id-&-document-class) and [customise them](#customise-id-class) more pleasingly.
3030

31-
* **DB-level caching**: This significantly improves the performance of all operations. However, the responsibility of converting the data to the correct type is transferred to the Storage[^2][^disable-db-level].
31+
* **DB-level Caching**: This significantly improves the performance of all operations. However, the responsibility of converting the data to the correct type is transferred to the Storage[^2][^disable-db-level].
3232

3333
* **Built-in `Modifier`**: Use `Modifier` to easily [encrypt](#encryption), [compress](./docs/Modifier.md#Compression) and [extend types](./docs/Modifier.md#Conversion) of your database. Sure you can do much more than these. _(See [Modifier](./docs/Modifier.md))_
3434

3535
* **Isolation Level**: Performance or ACID? It's up to you[^isolevel].
3636

3737
* **Atomic Write**: **A**CID!
3838

39-
* **Batch search by IDs**: `search` method now takes an extra `doc_ids` argument (works like an additional condition)
39+
* **Batch Search By IDs**: `search` method now takes an extra `doc_ids` argument (works like an additional condition)
4040

4141
# How to use it?
4242

@@ -63,7 +63,7 @@ That's it.
6363

6464
******
6565

66-
## Documents For Advances Usage
66+
## Documents For Advanced Usage
6767

6868
* [Modifier](./docs/Modifier.md)
6969
* [Event Hooks](./docs/EventHooks.md)
@@ -134,8 +134,8 @@ db.isolevel = 2
134134
`isolevel`:
135135

136136
0. No isolation
137-
1. Serialised CRUD operations (Also ensures thread safety) (default)
138-
2. Deepcopy documents on CRUD (Ensures `Index` & `Query Cache` consistency)
137+
1. Serialised(Atomic) CRUD operations (Also ensures thread safety) (default)
138+
2. Deepcopy documents on insertion and searching (**CR**UD) (Ensures `Index` & `Query Cache` consistency)
139139

140140

141141

docs/Modifier.md

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ async def main():
2424
asyncio.run(main())
2525

2626
```
27-
That's it! After calling `Modifier.Encryption.AES_GCM`, the database will be encrypted.
27+
That's it!
28+
After calling `Modifier.Encryption.AES_GCM`, the database will be encrypted.
2829
**Applying modifiers at the beginning of the database instantiation is strongly recommended.**
2930

3031
## Order
@@ -93,7 +94,7 @@ Modifier.Encryption.AES_GCM(db, "your key goes here")
9394
**Order-Aware**
9495

9596
This subclass contains methods to add compression to the storage.
96-
## `brotli`
97+
### `brotli`
9798

9899
* `events`: `write.post`, `read.pre`
99100
* `input`: `str`|`bytes`
@@ -107,7 +108,7 @@ db = TinyDB("db.json", access_mode="rb+") # Binary mode is required
107108
Modifier.Compression.brotli(db)
108109
```
109110

110-
## `blosc2`
111+
### `blosc2`
111112

112113
* `events`: `write.post`, `read.pre`
113114
* `input`: `str`|`bytes`
@@ -124,15 +125,16 @@ Modifier.Compression.blosc2(db)
124125

125126
This subclass contains methods to convert the data to a different format.
126127

127-
## `ExtendedJSON`
128+
### `ExtendedJSON`
128129

129130
* `events`: `write.pre`, `read.post`
130131
* `input`: `dict`
131132
* `output`: `dict`
132133

133134
This method allows JSONStorage to store more data types.
134135

135-
### Extended Types
136+
#### Extended Types
137+
136138
* `uuid.UUID`
137139
* `datetime.datetime`: Converted to `ISO 8601` format.
138140
* `datetime.timestamp`
@@ -149,11 +151,11 @@ db = TinyDB("db.json")
149151
Modifier.Converter.ExtendedJSON(db)
150152
```
151153

152-
### Customise Extended Types
154+
#### Customise Extended Types
153155

154156
By passing `type_hooks` and `marker_hooks` arguments to the modifier, you can modifiy the behaviour of the conversion.
155157

156-
### To modify `type_hooks`
158+
##### To modify `type_hooks`
157159

158160
```Python
159161
type_hooks = {
@@ -162,24 +164,56 @@ type_hooks = {
162164
}
163165
```
164166

165-
The first argument is the data to be converted.
166-
The second argument is the converter function, useful when you are dealing with `Container` types.
167+
The first argument `x` is the data to be converted.
168+
The second argument `c` is the converter function, useful when you are dealing with `Container` types.
167169

168-
In this example, we convert `int` to a `dict` with a `$int` key (i.e. `42` -> `{"$int": "42"}`), and we remove `set` from the conversion.
170+
In this example, we convert `int` to a `dict` with a `$int` key and a `str` as the value(i.e. `42` -> `{"$int": "42"}`), and we remove `set` from the conversion.
169171
Return value of the hook does not necessarily have to be a `dict`. It can be anything that is JSON serializable.
170172

171-
### To modify `marker_hooks`
173+
##### To modify `marker_hooks`
172174

173175
A marker is a special key that is used to identify the type of the data.
174-
We use a MongoDB style marker by default (starts with `'$'`), but you can change it to anything you want.
176+
We use a MongoDB style marker by default (a `str` starts with `'$'`), but you can change it to anything you want.
175177

176178
```Python
177179
marker_hooks = {
178180
"$int": lambda x, r: int(x["$int"]),
179181
}
180182
```
181183

182-
The first argument is the data to be converted.
183-
The second argument is the reverse converter function, useful when you are dealing with `Container` types.
184+
The first argument `x` is the data to be converted.
185+
The second argument `r` is the reverse converter function, useful when you are dealing with `Container` types.
184186

185187
In this example, we convert `dict` with the `$int` marker back to an `int` (i.e. `{"$int":"42"}` -> `42`).
188+
189+
##### A more complicated example
190+
191+
```Python
192+
import asyncio
193+
from asynctinydb import TinyDB, Modifier
194+
195+
class Myclass:
196+
def __init__(self, value: bytes):
197+
self.value = value
198+
199+
type_hooks = {
200+
Myclass: lambda x, c: {"$Myclass": c(x.value)}
201+
# Converts `bytes` with the converter
202+
}
203+
204+
marker_hooks = {
205+
"$Myclass": lambda x, r: Myclass(r(x["$Myclass"]))
206+
# Converts back to `bytes` then pass it to Myclass
207+
}
208+
209+
async def main():
210+
async with TinyDB("test.json") as db:
211+
Modifier.Conversion.ExtendedJSON(db,
212+
type_hooks=type_hooks,
213+
marker_hooks=marker_hooks
214+
)
215+
db.insert({"test": Myclass(b"some bytes")})
216+
217+
asyncio.run(main())
218+
```
219+

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ classifiers = [
2222
"Programming Language :: Python :: 3",
2323
"Programming Language :: Python :: 3.10",
2424
"Programming Language :: Python :: 3.11",
25+
"Programming Language :: Python :: 3.12",
2526
"Programming Language :: Python :: Implementation :: CPython",
2627
"Programming Language :: Python :: Implementation :: PyPy",
2728
"Operating System :: OS Independent",

tests/conftest.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,36 @@
2727
"json-nocache", "json_extend"] + mods)
2828
async def db(request):
2929
with tempfile.TemporaryDirectory() as tmpdir:
30-
if request.param == "json":
31-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
32-
elif request.param == "memory":
33-
db_ = TinyDB(storage=MemoryStorage)
34-
elif request.param == "json-encrypted":
35-
db_ = TinyDB(os.path.join(tmpdir, "test.db"),
30+
match request.param:
31+
case "json":
32+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
33+
case "memory":
34+
db_ = TinyDB(storage=MemoryStorage)
35+
case "json-encrypted":
36+
db_ = TinyDB(os.path.join(tmpdir, "test.db"),
3637
storage=EncryptedJSONStorage, key=key)
37-
elif request.param == "json-encrypted-modifier":
38-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), access_mode="rb+")
39-
Modifier.Encryption.AES_GCM(db_, key=key)
40-
elif request.param == "json-isolevel0":
41-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), isolevel=0)
42-
elif request.param == "json-isolevel1":
43-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
44-
db_.isolevel = 1
45-
elif request.param == "json-isolevel2":
46-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
47-
db_.isolevel = 2
48-
elif request.param == "json-nocache":
49-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), no_dbcache=True)
50-
elif request.param == "json_extend":
51-
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
52-
Modifier.Conversion.ExtendedJSON(db_)
53-
elif isinstance(request.param, tuple):
54-
db_ = TinyDB(os.path.join(tmpdir, "test.db"),
55-
access_mode="rb+", storage=JSONStorage)
56-
for mod in request.param:
57-
mod(db_)
38+
case "json-encrypted-modifier":
39+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), access_mode="rb+")
40+
Modifier.Encryption.AES_GCM(db_, key=key)
41+
case "json-isolevel0":
42+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), isolevel=0)
43+
case "json-isolevel1":
44+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
45+
db_.isolevel = 1
46+
case "json-isolevel2":
47+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
48+
db_.isolevel = 2
49+
case "json-nocache":
50+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), no_dbcache=True)
51+
case "json_extend":
52+
db_ = TinyDB(os.path.join(tmpdir, "test.db"), storage=JSONStorage)
53+
Modifier.Conversion.ExtendedJSON(db_)
54+
case _:
55+
if isinstance(request.param, tuple):
56+
db_ = TinyDB(os.path.join(tmpdir, "test.db"),
57+
access_mode="rb+", storage=JSONStorage)
58+
for mod in request.param:
59+
mod(db_)
5860

5961
await db_.drop_tables()
6062
await db_.insert_multiple({"int": 1, "char": c} for c in "abc")

0 commit comments

Comments
 (0)