Micropython-persistent-storage (mpstore) is a lightweight persistent key-value store library for MicroPython. It provides a simple JSON-based storage API.
You can install mpstore from the REPL with mip.
# micropython REPL
import mip
mip.install("github:surdouski/micropython-persistent-storage")
Alternatively, you can install it by using mpremote if you don't have network connectivity on device.
$ mpremote mip install github:surdouski/micropython-persistent-storage
To load the entire store:
from mpstore import load_store
store = load_store()
store = load_store(path="/some/path/store.json") # Optionally define a path
print(store) # {"key": "value}
To read a specific value from the store:
from mpstore import read_store
value = read_store("your_key")
value = read_store("your_key", path="/some/path/store.json")
print(value)
To write a key-value pair to the store:
from mpstore import write_store
write_store("your_key", "your_value")
write_store("your_key", "your_value", path="/some/path/store.json")
Keys for reading and writing use a dot syntax for nested JSON. "key1.nested1.nested2"
as the key argument in the read_store
or write_store
functions would retrieve or write the value at {"key1": {"nested1": {"nested2": "value"}}}
.
Returns all keys and values from the store.
Returns the value associated with the given key. Returns None
if the key does not exist.
Writes the key-value pair to the store, overwriting any previous value.
The project includes a test suite using the unittest
framework. The tests are located in test.py
.
To run tests, do the following.
# install unittest, mounting the volume locally
$ docker run --rm -v $(pwd)/lib:/root/.micropython/lib micropython/unix micropython -m mip install unittest
# run the test, using the mounted volume for the unittest deps
$ docker run --rm -v $(pwd):/code -v $(pwd)/lib:/root/.micropython/lib -w /code micropython/unix micropython test.py
This project is licensed under the MIT License. See the LICENSE file for details.