Propulsing the shelve module to the cloud
Project description
Cloud Shelve
Cloud Shelve (cshelve) is a Python package that provides a seamless way to store and manage data in the cloud using the familiar Python Shelve interface. It is designed for efficient and scalable storage solutions, allowing you to leverage cloud providers for persistent storage while keeping the simplicity of the shelve API.
Installation
Install cshelve via pip:
pip install cshelve # For testing purposes
pip install cshelve[azure-blob] # For Azure Blob Storage
Usage
The cshelve module provides a simple key-value interface for storing data in the cloud.
Quick Start Example
Here is a quick example demonstrating how to store and retrieve data using cshelve:
import cshelve
# Open a local database file
d = cshelve.open('local.db')
# Store data
d['my_key'] = 'my_data'
# Retrieve data
print(d['my_key']) # Output: my_data
# Close the database
d.close()
Cloud Storage Example (e.g., Azure)
To configure remote cloud storage, you need to provide an INI file containing your cloud provider's configuration. The file should have a .ini extension. Remote storage also requires the installation of optional dependencies for the cloud provider you want to use.
Example Azure Blob Configuration
First, install the Azure Blob Storage provider:
pip install cshelve[azure-blob]
Then, create an INI file with the following configuration:
$ cat azure-blob.ini
[default]
provider = azure-blob
account_url = https://myaccount.blob.core.windows.net
auth_type = passwordless
container_name = mycontainer
Once the INI file is ready, you can interact with remote storage the same way as with local storage. Here's an example using Azure:
import cshelve
# Open using the remote storage configuration
d = cshelve.open('azure-blob.ini')
# Store data
d['my_key'] = 'my_data'
# Retrieve data
print(d['my_key']) # Output: my_data
# Close the connection to the remote storage
d.close()
Advanced Scenario: Storing DataFrames in the Cloud
In this advanced example, we will demonstrate how to store and retrieve a Pandas DataFrame using cshelve with Azure Blob Storage.
First, install the required dependencies:
pip install cshelve[azure-blob] pandas
Create an INI file with the Azure Blob Storage configuration:
$ cat azure-blob.ini
[default]
provider = azure-blob
account_url = https://myaccount.blob.core.windows.net
auth_type = passwordless
container_name = mycontainer
Here's the code to store and retrieve a DataFrame:
import cshelve
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['New York', 'Los Angeles', 'Chicago']
})
# Open the remote storage using the Azure Blob configuration
with cshelve.open('azure-blob.ini') as db:
# Store the DataFrame
db['my_dataframe'] = df
# Retrieve the DataFrame
with cshelve.open('azure-blob.ini') as db:
retrieved_df = db['my_dataframe']
print(retrieved_df)
More configuration examples for other cloud providers can be found here.
Providers configuration
In Memory
Provider: in-memory
Installation: No additional installation required.
The In-Memory provider uses an in-memory data structure to simulate storage. This is useful for testing and development purposes.
| Option | Description | Required | Default Value |
|---|---|---|---|
persist-key |
If set, its value will be conserved and reused during the program execution. | :x: | None |
exists |
If True, the database exists; otherwise, it will be created. | :x: | False |
Azure Blob
Provider: azure-blob
Installation: pip install cshelve[azure-blob]
The Azure provider uses Azure Blob Storage as remote storage.
The module considers the provided container as dedicated to the application. The impact might be significant. For example, if the flag n is provided to the open function, the entire container will be purged, aligning with the official interface.
| Option | Description | Required | Default Value |
|---|---|---|---|
account_url |
The URL of your Azure storage account. | :x: | |
auth_type |
The authentication method to use: access_key, passwordless, connection_string or anonymous. |
:white_check_mark: | |
container_name |
The name of the container in your Azure storage account. | :white_check_mark: |
Depending on the open flag, the permissions required by cshelve for blob storage vary.
| Flag | Description | Permissions Needed |
|---|---|---|
r |
Open an existing blob storage container for reading only. | Storage Blob Data Reader |
w |
Open an existing blob storage container for reading and writing. | Storage Blob Data Contributor |
c |
Open a blob storage container for reading and writing, creating it if it doesn't exist. | Storage Blob Data Contributor |
n |
Purge the blob storage container before using it. | Storage Blob Data Contributor |
Authentication type supported:
| Auth Type | Description | Advantage | Disadvantage | Example Configuration |
|---|---|---|---|---|
| Access Key | Uses an Access Key or a Shared Access Signature for authentication. | Fast startup as no additional credential retrieval is needed. | Credentials need to be securely managed and provided. | Example |
| Anonymous | No authentication for anonymous access on public blob storage. | No configuration or credentials needed. | Read-only access. | Example |
| Connection String | Uses a connection string for authentication. Credentials are provided directly in the string. | Fast startup as no additional credential retrieval is needed. | Credentials need to be securely managed and provided. | Example |
| Passwordless | Uses passwordless authentication methods such as Managed Identity. | Recommended for better security and easier credential management. | May impact startup time due to the need to retrieve authentication credentials. | Example |
Contributing
We welcome contributions from the community! Have a look at our issues.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contact
If you have any questions, issues, or feedback, feel free to open an issue.
Project details
Release history Release notifications | RSS feed
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 cshelve-1.0.0.tar.gz.
File metadata
- Download URL: cshelve-1.0.0.tar.gz
- Upload date:
- Size: 127.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3c6a6e01ef3564cdef64cf71c10756ef7569008690689587fd4c51c3a732e22
|
|
| MD5 |
ed39dfd416a12e8af00266337c2dfc29
|
|
| BLAKE2b-256 |
e02664dec917706bbce29759cf4482b33b2b79e77e1b926c21ab6790e8935316
|
File details
Details for the file cshelve-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cshelve-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.5.26
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d69f4e870590f343cb31cbb30324a9b5fca02797f0f5c418c458fa76509b34f
|
|
| MD5 |
dc7b86ddfe025b11948334f2a60cc7a8
|
|
| BLAKE2b-256 |
7261fa48cca4bf97d6e81ab1be96c9b7265661f456bbbc56f2dd52b29deb4237
|