Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [PEP 440](https://peps.python.org/pep-0440/).

## [Unreleased]

### Added

- Dictionary-like access for `Table` and `Transaction` (`table[key]`, `table[key] = record`, `del table[key]`, `pop`, `popitem`, `setdefault`, `update`)

## [0.1.0] - 2025-12-31

### Added
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ except ConflictError as e:
print(f"Conflict on key: {e.key}")
```

## Dictionary-like access

Tables can be used like dictionaries:

```python
# Get a record (raises KeyError if not found)
user = table["alice"]

# Set a record (key in record must match)
table["alice"] = {"id": "alice", "role": "admin"}

# Delete a record
del table["bob"]

# Check membership
if "alice" in table:
print("Found alice")

# Iterate over keys
for key in table:
print(key)

# Get record count
print(len(table))
```

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.

## Finding records

```python
Expand Down Expand Up @@ -171,7 +199,7 @@ table.reload()
| `clear()` | Remove all records |
| `reload()` | Reload from disk |

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

### Transaction

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

Transactions support the same dictionary-like access as tables.

### Exceptions

All exceptions inherit from `JSONLTError`:
Expand Down
Loading