Skip to content

Commit a51a249

Browse files
authored
feat: add dictionary-like access to Table and Transaction (#15)
Add support for dictionary operations on tables and transactions: - `table[key]`, `table[key] = record`, `del table[key]` - `pop()`, `popitem()`, `setdefault()`, `update()` Consolidate ReadableMixin into TableMixin which provides the full interface for both Table and Transaction classes. Register TableMixin as a virtual MutableMapping subclass to maintain JSONLT's sorted iteration semantics while passing isinstance checks.
1 parent 8c044a7 commit a51a249

8 files changed

Lines changed: 952 additions & 258 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [PEP 440](https://peps.python.org/pep-0440/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Dictionary-like access for `Table` and `Transaction` (`table[key]`, `table[key] = record`, `del table[key]`, `pop`, `popitem`, `setdefault`, `update`)
13+
1014
## [0.1.0] - 2025-12-31
1115

1216
### Added

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ except ConflictError as e:
121121
print(f"Conflict on key: {e.key}")
122122
```
123123

124+
## Dictionary-like access
125+
126+
Tables can be used like dictionaries:
127+
128+
```python
129+
# Get a record (raises KeyError if not found)
130+
user = table["alice"]
131+
132+
# Set a record (key in record must match)
133+
table["alice"] = {"id": "alice", "role": "admin"}
134+
135+
# Delete a record
136+
del table["bob"]
137+
138+
# Check membership
139+
if "alice" in table:
140+
print("Found alice")
141+
142+
# Iterate over keys
143+
for key in table:
144+
print(key)
145+
146+
# Get record count
147+
print(len(table))
148+
```
149+
150+
Methods like `pop()`, `setdefault()`, and `update()` also work. The `keys()`, `values()`, and `items()` methods return sorted lists rather than views to maintain JSONLT's deterministic key ordering.
151+
124152
## Finding records
125153

126154
```python
@@ -171,7 +199,7 @@ table.reload()
171199
| `clear()` | Remove all records |
172200
| `reload()` | Reload from disk |
173201

174-
The `Table` class also supports `len(table)`, `key in table`, and `for record in table`.
202+
Tables support `table[key]`, `table[key] = record`, `del table[key]`, `len(table)`, `key in table`, and `for key in table`.
175203

176204
### Transaction
177205

@@ -184,6 +212,8 @@ The `Table` class also supports `len(table)`, `key in table`, and `for record in
184212
| `commit()` | Write to disk |
185213
| `abort()` | Discard changes |
186214

215+
Transactions support the same dictionary-like access as tables.
216+
187217
### Exceptions
188218

189219
All exceptions inherit from `JSONLTError`:

0 commit comments

Comments
 (0)