No project description provided
Project description
MemStore
MemStore is a lightweight in-memory database written in Python. It supports key-value storage with integer IDs,
single-field indexing, filtering by field values, and slicing records using integer-based positions. It uses
dictionaries for data storage and retrieval.
Installation
Since MemStore is a single-class implementation, you can simply include it in your project. No external package
installation is required. Alternatively, if packaged:
pip install memstore
Usage Examples
1. Initialize the Database
Create a database with optional indexes:
from memstore import MemStore
# Initialize with indexes on 'name' and 'age'
db = MemStore(indexes=['name', 'age'])
2. Insert Records
Add a single record and get its ID:
# Insert a single record
record_id = db.add({'name': 'Alice', 'age': 25, 'city': 'New York'})
print(f"Inserted record with ID: {record_id}") # Output: Inserted record with ID: 0
3. Query Records
Retrieve records by ID or filter by field values:
# Get by ID
record = db.get(0)
print(record) # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Filter by indexed field
alice_records = db.filter({'name': 'Alice'})
print(alice_records) # Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'})]
# Filter by non-indexed field
ny_records = db.filter({'city': 'New York'})
print(ny_records) # Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'})]
# Filter with multiple conditions (mixed indexed and non-indexed)
alice_25_records = db.filter({'name': 'Alice', 'age': 25})
print(alice_25_records) # Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'})]
4. List All Records
Retrieve all records in the database:
db.add({'name': 'Bob', 'age': 30, 'city': 'Boston'})
all_records = db.all()
for record_id, record in all_records:
print(f"ID {record_id}: {record}")
# Output:
# ID 0: {'name': 'Alice', 'age': 25, 'city': 'New York'}
# ID 1: {'name': 'Bob', 'age': 30, 'city': 'Boston'}
5. Slice Records with islice
Retrieve a subset of records using integer-based slicing with islice:
# Add more records for slicing examples
db.add({'name': 'Charlie', 'age': 35, 'city': 'Chicago'})
db.add({'name': 'David', 'age': 40, 'city': 'Seattle'})
# Slice from start to position 2 (exclusive)
slice_1 = list(db.islice(stop=2))
print(slice_1)
# Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'}),
# (1, {'name': 'Bob', 'age': 30, 'city': 'Boston'})]
# Slice from position 1 to 3
slice_2 = list(db.islice(1, 3))
print(slice_2)
# Output: [(1, {'name': 'Bob', 'age': 30, 'city': 'Boston'}),
# (2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'})]
# Slice with step (every second record)
slice_3 = list(db.islice(0, None, 2))
print(slice_3)
# Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'}),
# (2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'})]
# Slice with negative indexes (last two records)
slice_4 = list(db.islice(-2, None))
print(slice_4)
# Output: [(2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'}),
# (3, {'name': 'David', 'age': 40, 'city': 'Seattle'})]
6. Access Records with iloc
Retrieve records by integer position using iloc, supporting both single index and slice operations:
# Get a single record by position
record = db.iloc[1]
print(record) # Output: {'name': 'Bob', 'age': 30, 'city': 'Boston'}
# Get the last record using negative index
last_record = db.iloc[-1]
print(last_record) # Output: {'name': 'David', 'age': 40, 'city': 'Seattle'}
# Slice records with iloc
slice_iloc = db.iloc[1:3]
print(slice_iloc)
# Output: [(1, {'name': 'Bob', 'age': 30, 'city': 'Boston'}),
# (2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'})]
# Slice with step using iloc
slice_step = db.iloc[0::2]
print(slice_step)
# Output: [(0, {'name': 'Alice', 'age': 25, 'city': 'New York'}),
# (2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'})]
7. Delete Records
Remove a record by ID:
success = db.delete(0)
print(f"Delete successful: {success}") # Output: Delete successful: True
print(db.all()) # Output: [(1, {'name': 'Bob', 'age': 30, 'city': 'Boston'}),
# (2, {'name': 'Charlie', 'age': 35, 'city': 'Chicago'}),
# (3, {'name': 'David', 'age': 40, 'city': 'Seattle'})]
8. Manage Indexes
Add or remove indexes dynamically:
# Add a new index
db.add_index('city')
print(db.filter({'city': 'Boston'})) # Output: [(1, {'name': 'Bob', 'age': 30, 'city': 'Boston'})]
# Drop an index
db.drop_index('name')
print('name' in db._indexes) # Output: False
Notes
- Data Structure: Records are stored as dictionaries with integer IDs assigned sequentially.
- Indexes: Only single-field indexes are supported (e.g.,
'name'). Composite indexes are not available. - Filtering: The
filtermethod retrieves records matching all specified field-value pairs, using indexes when available for efficiency. It works with both indexed and non-indexed fields. - Slicing with
islice: Theislicemethod allows positional slicing of records using integer indexes (positive or negative), supportingstart,stop, andstepparameters. It returns a list of(id, record)tuples. - Positional Access with
iloc: Theilocproperty provides a pandas-like interface for accessing records by integer position. It supports single integer indexing (returns a record dictionary) and slicing (returns a list of(id, record)tuples). - Limitations: No field validation or update methods are provided. Deletion and retrieval are ID-based or filter-based only.
- Dependencies: Uses only Python standard library modules (
collections,functools,itertools,operator,typing).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memstore-0.2.5.2.tar.gz.
File metadata
- Download URL: memstore-0.2.5.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-55-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dad145653c8e11d4bd32536ad28cb28e2a4da8b23caba057afc088142a27bfe2
|
|
| MD5 |
d1393acebaee5eaa163c6f16ff9b4478
|
|
| BLAKE2b-256 |
bbc63aef15485ceaf961723f15d000cea18ae7a9c95462274acf55d234d31be4
|
File details
Details for the file memstore-0.2.5.2-py3-none-any.whl.
File metadata
- Download URL: memstore-0.2.5.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.10.12 Linux/6.8.0-55-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06bb6b6eb88c7d9f97a876dabe8b492ac9ec410206c4f7fe92b6d505514247af
|
|
| MD5 |
0dd7b74aa6bc66776db51f7570deb722
|
|
| BLAKE2b-256 |
5cf2a1fff6225984b9e8f2e8599dbd2bcade01416742197cd197b60ec9aa86eb
|