potatodb


Namepotatodb JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/Kawai-Senpai/PotatoDB
SummaryPotatoDB 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.
upload_time2024-09-10 10:21:04
maintainerNone
docs_urlNone
authorRanit Bhowmick
requires_pythonNone
licenseMIT License with attribution requirement
keywords nosql database json persistence lightweight file-based
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PotatoDB: Lightweight JSON-based Database for Python

**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*](https://www.linkedin.com/in/ranitbhowmick/) & [*Sayanti Chatterjee*](https://www.linkedin.com/in/sayantichatterjee/). 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.

## Features

- **Simple Setup:** No need for external dependencies or database servers. PotatoDB works out of the box with Python's standard library.
- **JSON-based Storage:** Data is stored in human-readable JSON files, making it easy to inspect and manipulate outside of the application.
- **In-Memory Operations:** Perform operations like querying, updating, and deleting records in memory for fast performance.
- **Automatic Persistence:** Data is automatically saved to disk after each operation, ensuring data integrity.
- **Table-based Structure:** Create multiple tables within a single database instance.
- **Dynamic Schema:** No predefined schema; records can have varying fields, offering flexibility for evolving data models.

## Table of Contents

1. [Installation](#installation)
2. [Basic Usage](#basic-usage)
    - [Creating a Database Instance](#creating-a-database-instance)
    - [Creating a Table](#creating-a-table)
    - [Inserting Records](#inserting-records)
    - [Querying Records](#querying-records)
    - [Updating Records](#updating-records)
    - [Deleting Records](#deleting-records)
3. [Detailed Documentation](#detailed-documentation)
    - [Class: `PotatoDB`](#class-PotatoDB)
    - [Methods](#methods)
        - [`__init__`](#init)
        - [`create_table`](#create_table)
        - [`insert`](#insert)
        - [`query`](#query)
        - [`update`](#update)
        - [`delete`](#delete)
        - [`set_folder`](#set_folder)
        - [`save`](#save)
        - [`load`](#load)
4. [Examples](#examples)
5. [Advanced Usage](#advanced-usage)
6. [Limitations](#limitations)
7. [Future Enhancements](#future-enhancements)
8. [Contributing](#contributing)

## Installation

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.

```bash
pip install potatodb
```

Alternatively, you can copy the `potatodb.py` file directly into your project directory.

## Basic Usage

### Creating a Database Instance

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.

```python
from potatodb.db import PotatoDB

# Create a new PotatoDB instance
db = PotatoDB("example_data")
```

### Creating a Table

To create a new table within the database, use the `create_table` method.

```python
db.create_table("users")
```

### Inserting Records

Records can be inserted into a table using the `insert` method. The record should be a Python dictionary.

```python
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
```

### Querying Records

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.

```python
# 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)
```

### Updating Records

To update records, use the `update` method with a condition function and an update function.

```python
# Update the age of all users named "Alice" to 35
db.update("users", lambda record: record["name"] == "Alice", lambda record: record.update({"age": 35}))
```

### Deleting Records

To delete records, use the `delete` method with a condition function.

```python
# Delete all users named "Bob"
db.delete("users", lambda record: record["name"] == "Bob")
```

## Detailed Documentation

### Class: `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__`

```python
def __init__(self, folder="PotatoDB"):
```

- **Description:** Initializes the database instance, setting the folder for storage and loading existing data from JSON files.
- **Parameters:**
  - `folder` (str): The folder where the database files will be stored. Defaults to `"PotatoDB"`.

#### `create_table`

```python
def create_table(self, table_name):
```

- **Description:** Creates a new table within the database.
- **Parameters:**
  - `table_name` (str): The name of the table to be created.

#### `insert`

```python
def insert(self, table_name, data):
```

- **Description:** Inserts a new record into the specified table.
- **Parameters:**
  - `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.
- **Returns:** The inserted record.

#### `query`

```python
def query(self, table_name, query_func):
```

- **Description:** Queries data from the specified table using a query function.
- **Parameters:**
  - `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.
- **Returns:** A list of records that match the query.

#### `update`

```python
def update(self, table_name, condition_func, update_func):
```

- **Description:** Updates records in the specified table based on a condition.
- **Parameters:**
  - `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.
- **Returns:** `True` if the update was successful, `False` otherwise.

#### `delete`

```python
def delete(self, table_name, condition_func):
```

- **Description:** Deletes records from the specified table based on a condition.
- **Parameters:**
  - `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.
- **Returns:** `True` if the deletion was successful, `False` otherwise.

#### `set_folder`

```python
def set_folder(self, folder_name):
```

- **Description:** Sets the folder where the tables will be saved and loaded.
- **Parameters:**
  - `folder_name` (str): The name of the folder.

#### `save`

```python
def save(self, table_name=None):
```

- **Description:** Saves the specified table to a JSON file in the set folder. (This method is called automatically after each operation.)
- **Parameters:**
  - `table_name` (str): The name of the table to save. If `None`, all tables are saved.

#### `load`

```python
def load(self, table_name=None):
```

- **Description:** Loads a table from a JSON file in the set folder. (This method is called automatically when the database is initialized.)
- **Parameters:**
  - `table_name` (str): The name of the table to load. If `None`, all tables are loaded.

## Examples

Here's a complete example of how to use PotatoDB:

```python
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)
```

## Advanced Usage

### Custom Data Persistence

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.

## Limitations

- **Performance:** PotatoDB is designed for lightweight tasks and may not perform well with large datasets or complex queries.
- **Concurrency:** PotatoDB is not thread-safe and should be used with caution in multi-threaded environments.
- **Data Integrity:** Since PotatoDB writes data to JSON files, there is a risk of data corruption if the program crashes during a write operation.

## Future Enhancements

- **Indexing:** Implementing indexing for faster query performance.
- **Transactions:** Adding support for transactions to ensure data integrity during complex operations.
- **Data Export/Import:** Adding features to export and import data in different formats, such as CSV or XML.

## Contributing

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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Kawai-Senpai/PotatoDB",
    "name": "potatodb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "NoSQL, Database, JSON, Persistence, Lightweight, File-based",
    "author": "Ranit Bhowmick",
    "author_email": "bhowmickranitking@duck.com",
    "download_url": "https://files.pythonhosted.org/packages/95/1a/dbb1699b2b3744839c92bdb934779000889da66e2771b33603182c1ab249/potatodb-1.0.3.tar.gz",
    "platform": null,
    "description": "# PotatoDB: Lightweight JSON-based Database for Python\r\n\r\n**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*](https://www.linkedin.com/in/ranitbhowmick/) & [*Sayanti Chatterjee*](https://www.linkedin.com/in/sayantichatterjee/). 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.\r\n\r\n## Features\r\n\r\n- **Simple Setup:** No need for external dependencies or database servers. PotatoDB works out of the box with Python's standard library.\r\n- **JSON-based Storage:** Data is stored in human-readable JSON files, making it easy to inspect and manipulate outside of the application.\r\n- **In-Memory Operations:** Perform operations like querying, updating, and deleting records in memory for fast performance.\r\n- **Automatic Persistence:** Data is automatically saved to disk after each operation, ensuring data integrity.\r\n- **Table-based Structure:** Create multiple tables within a single database instance.\r\n- **Dynamic Schema:** No predefined schema; records can have varying fields, offering flexibility for evolving data models.\r\n\r\n## Table of Contents\r\n\r\n1. [Installation](#installation)\r\n2. [Basic Usage](#basic-usage)\r\n    - [Creating a Database Instance](#creating-a-database-instance)\r\n    - [Creating a Table](#creating-a-table)\r\n    - [Inserting Records](#inserting-records)\r\n    - [Querying Records](#querying-records)\r\n    - [Updating Records](#updating-records)\r\n    - [Deleting Records](#deleting-records)\r\n3. [Detailed Documentation](#detailed-documentation)\r\n    - [Class: `PotatoDB`](#class-PotatoDB)\r\n    - [Methods](#methods)\r\n        - [`__init__`](#init)\r\n        - [`create_table`](#create_table)\r\n        - [`insert`](#insert)\r\n        - [`query`](#query)\r\n        - [`update`](#update)\r\n        - [`delete`](#delete)\r\n        - [`set_folder`](#set_folder)\r\n        - [`save`](#save)\r\n        - [`load`](#load)\r\n4. [Examples](#examples)\r\n5. [Advanced Usage](#advanced-usage)\r\n6. [Limitations](#limitations)\r\n7. [Future Enhancements](#future-enhancements)\r\n8. [Contributing](#contributing)\r\n\r\n## Installation\r\n\r\nPotatoDB 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.\r\n\r\n```bash\r\npip install potatodb\r\n```\r\n\r\nAlternatively, you can copy the `potatodb.py` file directly into your project directory.\r\n\r\n## Basic Usage\r\n\r\n### Creating a Database Instance\r\n\r\nTo 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.\r\n\r\n```python\r\nfrom potatodb.db import PotatoDB\r\n\r\n# Create a new PotatoDB instance\r\ndb = PotatoDB(\"example_data\")\r\n```\r\n\r\n### Creating a Table\r\n\r\nTo create a new table within the database, use the `create_table` method.\r\n\r\n```python\r\ndb.create_table(\"users\")\r\n```\r\n\r\n### Inserting Records\r\n\r\nRecords can be inserted into a table using the `insert` method. The record should be a Python dictionary.\r\n\r\n```python\r\ndb.insert(\"users\", {\"name\": \"Alice\", \"age\": 30})\r\ndb.insert(\"users\", {\"name\": \"Bob\", \"age\": 25}) \r\n# The database will automatically save the data to disk after each operation\r\n```\r\n\r\n### Querying Records\r\n\r\nTo 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.\r\n\r\n```python\r\n# Query all users\r\nall_users = db.query(\"users\", lambda record: True)\r\nprint(\"All users:\", all_users)\r\n\r\n# Query users older than 30\r\nusers_above_30 = db.query(\"users\", lambda record: record[\"age\"] > 30)\r\nprint(\"Users above 30:\", users_above_30)\r\n```\r\n\r\n### Updating Records\r\n\r\nTo update records, use the `update` method with a condition function and an update function.\r\n\r\n```python\r\n# Update the age of all users named \"Alice\" to 35\r\ndb.update(\"users\", lambda record: record[\"name\"] == \"Alice\", lambda record: record.update({\"age\": 35}))\r\n```\r\n\r\n### Deleting Records\r\n\r\nTo delete records, use the `delete` method with a condition function.\r\n\r\n```python\r\n# Delete all users named \"Bob\"\r\ndb.delete(\"users\", lambda record: record[\"name\"] == \"Bob\")\r\n```\r\n\r\n## Detailed Documentation\r\n\r\n### Class: `PotatoDB`\r\n\r\nThe `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.\r\n\r\n#### `__init__`\r\n\r\n```python\r\ndef __init__(self, folder=\"PotatoDB\"):\r\n```\r\n\r\n- **Description:** Initializes the database instance, setting the folder for storage and loading existing data from JSON files.\r\n- **Parameters:**\r\n  - `folder` (str): The folder where the database files will be stored. Defaults to `\"PotatoDB\"`.\r\n\r\n#### `create_table`\r\n\r\n```python\r\ndef create_table(self, table_name):\r\n```\r\n\r\n- **Description:** Creates a new table within the database.\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table to be created.\r\n\r\n#### `insert`\r\n\r\n```python\r\ndef insert(self, table_name, data):\r\n```\r\n\r\n- **Description:** Inserts a new record into the specified table.\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table where the record will be inserted.\r\n  - `data` (dict): The record to be inserted, represented as a dictionary.\r\n- **Returns:** The inserted record.\r\n\r\n#### `query`\r\n\r\n```python\r\ndef query(self, table_name, query_func):\r\n```\r\n\r\n- **Description:** Queries data from the specified table using a query function.\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table to query.\r\n  - `query_func` (function): A function that takes a record as input and returns `True` if the record matches the query, `False` otherwise.\r\n- **Returns:** A list of records that match the query.\r\n\r\n#### `update`\r\n\r\n```python\r\ndef update(self, table_name, condition_func, update_func):\r\n```\r\n\r\n- **Description:** Updates records in the specified table based on a condition.\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table to update.\r\n  - `condition_func` (function): A function that takes a record as input and returns `True` if the record should be updated.\r\n  - `update_func` (function): A function that takes a record as input and performs the update.\r\n- **Returns:** `True` if the update was successful, `False` otherwise.\r\n\r\n#### `delete`\r\n\r\n```python\r\ndef delete(self, table_name, condition_func):\r\n```\r\n\r\n- **Description:** Deletes records from the specified table based on a condition.\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table from which records should be deleted.\r\n  - `condition_func` (function): A function that takes a record as input and returns `True` if the record should be deleted.\r\n- **Returns:** `True` if the deletion was successful, `False` otherwise.\r\n\r\n#### `set_folder`\r\n\r\n```python\r\ndef set_folder(self, folder_name):\r\n```\r\n\r\n- **Description:** Sets the folder where the tables will be saved and loaded.\r\n- **Parameters:**\r\n  - `folder_name` (str): The name of the folder.\r\n\r\n#### `save`\r\n\r\n```python\r\ndef save(self, table_name=None):\r\n```\r\n\r\n- **Description:** Saves the specified table to a JSON file in the set folder. (This method is called automatically after each operation.)\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table to save. If `None`, all tables are saved.\r\n\r\n#### `load`\r\n\r\n```python\r\ndef load(self, table_name=None):\r\n```\r\n\r\n- **Description:** Loads a table from a JSON file in the set folder. (This method is called automatically when the database is initialized.)\r\n- **Parameters:**\r\n  - `table_name` (str): The name of the table to load. If `None`, all tables are loaded.\r\n\r\n## Examples\r\n\r\nHere's a complete example of how to use PotatoDB:\r\n\r\n```python\r\nfrom potatodb.db import PotatoDB\r\n\r\n# Create a new LazyDB instance\r\ndb = PotatoDB(\"example_data\")\r\n\r\n# Create a new table called \"users\"\r\ndb.create_table(\"users\")\r\n\r\n# Insert a new record into the \"users\" table\r\ndb.insert(\"users\", {\"name\": \"Alice\", \"age\": 30})\r\ndb.insert(\"users\", {\"name\": \"Bob\", \"age\": 25})\r\n\r\n# Query all records from the \"users\" table\r\nall_users = db.query(\"users\", lambda record: True)\r\nprint(\"All users:\" , all_users)\r\n\r\n# Update the age of all users named \"Alice\" to 35\r\ndb.update(\"users\", lambda record: record[\"name\"] == \"Alice\", lambda record: record.update({\"age\": 35}))\r\n\r\n# Query all records above the age of 30 from the \"users\" table\r\nusers_above_30 = db.query(\"users\", lambda record: record[\"age\"] > 30)\r\n\r\n# Print the updated records\r\nprint(\"Users above 30:\", users_above_30)\r\n\r\n# Delete all users named \"Bob\" from the \"users\" table\r\ndb.delete(\"users\", lambda record: record[\"name\"] == \"Bob\")\r\n\r\n# Query all records from the \"users\" table after deletion\r\nremaining_users = db.query(\"users\", lambda record: True)\r\n\r\n# Print the remaining records\r\nprint(\"Remaining users:\", remaining_users)\r\n```\r\n\r\n## Advanced Usage\r\n\r\n### Custom Data Persistence\r\n\r\nWhile 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.\r\n\r\n## Limitations\r\n\r\n- **Performance:** PotatoDB is designed for lightweight tasks and may not perform well with large datasets or complex queries.\r\n- **Concurrency:** PotatoDB is not thread-safe and should be used with caution in multi-threaded environments.\r\n- **Data Integrity:** Since PotatoDB writes data to JSON files, there is a risk of data corruption if the program crashes during a write operation.\r\n\r\n## Future Enhancements\r\n\r\n- **Indexing:** Implementing indexing for faster query performance.\r\n- **Transactions:** Adding support for transactions to ensure data integrity during complex operations.\r\n- **Data Export/Import:** Adding features to export and import data in different formats, such as CSV or XML.\r\n\r\n## Contributing\r\n\r\nContributions 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.\r\n",
    "bugtrack_url": null,
    "license": "MIT License with attribution requirement",
    "summary": "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.",
    "version": "1.0.3",
    "project_urls": {
        "Download": "https://github.com/Kawai-Senpai/PotatoDB",
        "Homepage": "https://github.com/Kawai-Senpai/PotatoDB"
    },
    "split_keywords": [
        "nosql",
        " database",
        " json",
        " persistence",
        " lightweight",
        " file-based"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06a46561290cd6c311559efe99326e186e8fff36498d2cc999fec9e4f95d9387",
                "md5": "c1775a618607b32132e977dd2c5d47cc",
                "sha256": "f53feeb1418e76f98288b82157e68092efaf24aaa4e8b1c8dd5d35e8490da1aa"
            },
            "downloads": -1,
            "filename": "potatodb-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c1775a618607b32132e977dd2c5d47cc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6486,
            "upload_time": "2024-09-10T10:21:03",
            "upload_time_iso_8601": "2024-09-10T10:21:03.595829Z",
            "url": "https://files.pythonhosted.org/packages/06/a4/6561290cd6c311559efe99326e186e8fff36498d2cc999fec9e4f95d9387/potatodb-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "951adbb1699b2b3744839c92bdb934779000889da66e2771b33603182c1ab249",
                "md5": "4fc0f5c599b7df17686c065b6e9997d7",
                "sha256": "97dff079885673eecc1d9098fa4ee80b82dc542d2af0c64641cc2fea35b07b7c"
            },
            "downloads": -1,
            "filename": "potatodb-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "4fc0f5c599b7df17686c065b6e9997d7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 8646,
            "upload_time": "2024-09-10T10:21:04",
            "upload_time_iso_8601": "2024-09-10T10:21:04.700130Z",
            "url": "https://files.pythonhosted.org/packages/95/1a/dbb1699b2b3744839c92bdb934779000889da66e2771b33603182c1ab249/potatodb-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-10 10:21:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Kawai-Senpai",
    "github_project": "PotatoDB",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "potatodb"
}
        
Elapsed time: 0.30370s