Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
PotatoDB is a lightweight, file-based NoSQL database for Python projects, designed for easy setup and use in small-scale applications. Ideal for developers seeking simple data persistence without the complexity of traditional databases.
PotatoDB is a lightweight, JSON-based database solution designed for simplicity and ease of use. It allows you to create, insert, query, update, and delete records in a NoSQL fashion, using Python's built-in JSON module to store data persistently on disk developed by Ranit Bhowmick & Sayanti Chatterjee. Whether you need a quick and easy database for small projects, prototypes, or educational purposes, PotatoDB offers a flexible solution without the overhead of traditional databases.
PotatoDB is implemented in pure Python and does not require any external dependencies. To start using PotatoDB, simply download the source code and include it in your project or install it via pip.
pip install potatodb
Alternatively, you can copy the potatodb.py
file directly into your project directory.
To create a new instance of PotatoDB, import the PotatoDB
class and specify the folder where the data should be stored. If the folder does not exist, it will be created automatically.
from potatodb.db import PotatoDB
# Create a new PotatoDB instance
db = PotatoDB("example_data")
To create a new table within the database, use the create_table
method.
db.create_table("users")
Records can be inserted into a table using the insert
method. The record should be a Python dictionary.
db.insert("users", {"name": "Alice", "age": 30})
db.insert("users", {"name": "Bob", "age": 25})
# The database will automatically save the data to disk after each operation
To query records, use the query
method with a custom query function. The query function should return True
for records that match the condition and False
for those that don't.
# Query all users
all_users = db.query("users", lambda record: True)
print("All users:", all_users)
# Query users older than 30
users_above_30 = db.query("users", lambda record: record["age"] > 30)
print("Users above 30:", users_above_30)
To update records, use the update
method with a condition function and an update function.
# Update the age of all users named "Alice" to 35
db.update("users", lambda record: record["name"] == "Alice", lambda record: record.update({"age": 35}))
To delete records, use the delete
method with a condition function.
# Delete all users named "Bob"
db.delete("users", lambda record: record["name"] == "Bob")
PotatoDB
The PotatoDB
class is the core of the PotatoDB library. It provides methods for creating tables, inserting data, querying, updating, and deleting records, as well as saving and loading data to and from JSON files.
__init__
def __init__(self, folder="PotatoDB"):
folder
(str): The folder where the database files will be stored. Defaults to "PotatoDB"
.create_table
def create_table(self, table_name):
table_name
(str): The name of the table to be created.insert
def insert(self, table_name, data):
table_name
(str): The name of the table where the record will be inserted.data
(dict): The record to be inserted, represented as a dictionary.query
def query(self, table_name, query_func):
table_name
(str): The name of the table to query.query_func
(function): A function that takes a record as input and returns True
if the record matches the query, False
otherwise.update
def update(self, table_name, condition_func, update_func):
table_name
(str): The name of the table to update.condition_func
(function): A function that takes a record as input and returns True
if the record should be updated.update_func
(function): A function that takes a record as input and performs the update.True
if the update was successful, False
otherwise.delete
def delete(self, table_name, condition_func):
table_name
(str): The name of the table from which records should be deleted.condition_func
(function): A function that takes a record as input and returns True
if the record should be deleted.True
if the deletion was successful, False
otherwise.set_folder
def set_folder(self, folder_name):
folder_name
(str): The name of the folder.save
def save(self, table_name=None):
table_name
(str): The name of the table to save. If None
, all tables are saved.load
def load(self, table_name=None):
table_name
(str): The name of the table to load. If None
, all tables are loaded.Here's a complete example of how to use PotatoDB:
from potatodb.db import PotatoDB
# Create a new LazyDB instance
db = PotatoDB("example_data")
# Create a new table called "users"
db.create_table("users")
# Insert a new record into the "users" table
db.insert("users", {"name": "Alice", "age": 30})
db.insert("users", {"name": "Bob", "age": 25})
# Query all records from the "users" table
all_users = db.query("users", lambda record: True)
print("All users:" , all_users)
# Update the age of all users named "Alice" to 35
db.update("users", lambda record: record["name"] == "Alice", lambda record: record.update({"age": 35}))
# Query all records above the age of 30 from the "users" table
users_above_30 = db.query("users", lambda record: record["age"] > 30)
# Print the updated records
print("Users above 30:", users_above_30)
# Delete all users named "Bob" from the "users" table
db.delete("users", lambda record: record["name"] == "Bob")
# Query all records from the "users" table after deletion
remaining_users = db.query("users", lambda record: True)
# Print the remaining records
print("Remaining users:", remaining_users)
While PotatoDB automatically saves and loads data from JSON files, you can also manually save or load data using custom file paths or formats. This allows you to integrate PotatoDB with other data management systems or customize the storage format.
Contributions are welcome! If you find a bug or want to suggest a feature, feel free to open an issue or submit a pull request on GitHub.
FAQs
PotatoDB is a lightweight, file-based NoSQL database for Python projects, designed for easy setup and use in small-scale applications. Ideal for developers seeking simple data persistence without the complexity of traditional databases.
We found that potatodb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.