Skip to main content

Library for wrapping data storage in a dict interface.

Project description

Build StatusPyPI

dictwrap

Python library for wrapping persistent data storage in a dictionary interface.

json_dict

Example usage:

from dictwrap import json_dict

config = json_dict("config_file.json")
config["hello"] = "world"

This will establish a dictionary wrapper around the contents of config_file.json. Changes to the dictionary will be written to the JSON file automatically.

To override automatic updates to the JSON file and save changes manually:

from dictwrap import json_dict

config = json_dict("config_file.json", autosave=False)

config["hello"] = "world"  # This change is not written to the file yet.
config.save()  # The change is now written to the file.

sql_row

Example usage:

from dictwrap import sql_row

row = sql_row("db_driver://user:pass@host/db_name", table="example", lookup_field="id", lookup_value=1)

By default you will not set save changes to the ditionary until the save method is called:

row["foo"] = "bar"  # Not saved to the database yet.
row.save()    # Database is now updated.

You can override this using the autosave attribute when creating the row:

row = sql_row("sqlite://", table="example", lookup_field="id", lookup_id=1, autosave=True)
row["foo"] = "bar"  # Database is automatically updated.

To update data for the dictionary from the database, call the refresh method:

row.refresh()

By using the autorefresh attribute when creating the row, data will be updated from the database whenever a key value is looked up on the dictionary:

row = sql_row("sqlite://", table="example", lookup_field="id", lookup_id=1, autorefresh=True)
print(row["foo"])  # Accessing the key now calls the database to update any data that has been changed.

If you only need certain fields from the database to be included in the dictionary, pass a list of field names when creating the row:

row = sql_row("sqlite://", table="example", lookup_field="id", lookup_id=1, fields=["foo", "bar"])

To create a new row in the database, do not provide a lookup_field or lookup_id attribute and pass a dictionary into the data attribute:

row = sql_row("sqlite://", table="example", update={"foo": "bar"})
# Creates a row in the `example` table with the `foo` field set to "bar".

You will need to provide valid data for all fields required by the database for this work.

csv_row

Example usage:

from dictwrap import csv_row

row = csv_row("file_path.csv", row_id=0)

This will create a dictionary that reads and writes to the first row of the CSV file provided. (Remember what index number arrays start at.)

You can identify which row in the CSV file to wrap in the dictionary either by row number, or by passing a dictionary with a key-value pairing for the row you want to find. For example, if the CSV file has a field called Author you would be able to lookup the row for Ursula LeGuin like this:

row = csv_row("authors.csv", row_id={"Author": "Ursula LeGuin"})

By default, fieldnames will be read from the first row of the CSV file. If the file lacks such a header row, fieldnames can be passed explicitly as a list:

row = csv_row("authors.csv", row_id=1, fieldnames=["ID", "Name", "Rating"])

api_record

Example usage:

from dictwrap import api_record

record = api_record("httsp://fake.org/api/record/1/", auth=("user", "pass"))

This will call the GET request method on the URL endpoint provided, and provide the username/password provided in the tuple for authentication. Saving will call the PATCH method on the same method. Later I intend to create more flexibility for this type.

Contact

This project is obviously very early in development. I started it to create an easily-installable package with some dictionary wrappers I found myself frequently rewriting. I've been adding more types for fun.

Contact me at psbleep@protonmail.com or open an issue/pull request on Github with questions, suggestions, or improvements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dictwrap, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size dictwrap-0.0.2-py3-none-any.whl (17.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size dictwrap-0.0.2.tar.gz (4.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page