Skip to main content

package for handling local database files and data

Project description

pleasant_database

this package is a modular python package that allows for easy creation, reading, editing, and deleting local database files

License

MIT

Usage/Examples

Install Package Ensure you have access to the github repository Run the command: pip install pleasant_database GitHub repository: https://github.com/EliasRodkey/python-toolkit

Import Package

From pleasant_database import DatabaseFile, DatabaseManager, BaseTable

Set Up Table Object

Use the BaseTable class to create a table object that will be connected to using the DatabaseManager

class TableName(BaseTable):
    """A table object"""
    __tablename__ = "table_name"
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), nullable=False)
    age = Column(Integer)
    email = Column(String(100), unique=True)

The attributes here represent column names in the table, additional funcionality can be added to avoid duplicate entries, for example:

email = Column(String(100), unique=True)

This forces all emails to be unique in the table. Another option is to add a special hash row that would be unique to each entry:

    import hashlib

def generate_hash(row):
    # Concatenate values of all relevant columns
    data = f"{row["col1"]}_{row["col2"]}_{row["col3"]}"
    return hashlib.sha256(data.encode()).hexdigest()

df["unique_id"] = df.apply(generate_hash, axis=1)

Additionally the __tableargs__ can be updated to make a custom unique filter based on multiple columns to the class attributes:

__table_args__ = (
    UniqueConstraint("column1", "column2", name="unique_combo"),  # Combination must be unique
)

Creating the DataFile

The DatabaseFile object represents the actual file of the database and is required to initialize a DatabaseManager object.

file = DatabaseFile(db_name, directory="data/database")

db_name: valid .db filename
directory: relative path to database directory (default data//database)

DatabaseFile funcitons:

file.create() # Creates a db file in the directory location
file.move(target_directory) # Moves the db file to a new directory
file.exists() # Returns boolean, if the file exists
file.delete() # Deletes the actual db file

Create DatabaseManager

The DatabaseManager takes a file and table as arguments and allows for common database operations on that table

manager = DatabaseManager(table_class: BaseTable, databasefile: DatabaseFile)

The DatabaseFile functions can be accessed through the DatabaseManager.file attribute Basic database funcitons:

manager.add_item(entry: dictionary with columns matching the db table class)
manager.add_multiple_items(entries: list of entries)
manager.append_dataframe(df) # pandas DataFrame with columns that match the db table class

manager.fetch_all_items() # returns all table class instances in the table
manager.fetch_item_by_id(id: int) # returns an individual table class instance with data
manager.fetch_items_by_attribute(**kwargs) # allows filtering of table by kwargs
manager.filter_items(filters: dict, use_or=False) # allows filtering of database table, returns ORM objects
manager.to_dataframe() # returns the entire database as a pandas DataFrame

# Flexible query returning a DataFrame — supports column projection, filtering, sorting, pagination, and search
manager.query(
    columns=["name", "age"],          # optional: subset of columns to return (None = all)
    filters={"age": (">", 18)},       # optional: same filter dict format as filter_items()
    order_by=[("age", "desc"), ("name", "asc")],  # optional: str, (col, dir) tuple, or list of tuples
    ascending=True,                   # only used when order_by is a bare string
    limit=10,                         # optional: max rows to return
    offset=0,                         # optional: rows to skip before returning results
    search="john",                    # optional: case-insensitive substring match across string columns
    search_columns=["name", "email"], # optional: restrict search to these columns (default: all str columns)
)

manager.convert_orm_list_to_dataframe(orm_list) # converts a list of ORM objects to a pandas DataFrame

manager.update_item(item_id: int, **kwargs) # updates values kwargs of an item with a given ID
manager.delete_item(item_id: int) # Deletes an item with the given item id
manager.delete_items_by_attribute(**kwargs) # Deletes items in a database where column values match kwargs.
manager.delete_items_by_filter(filters: dict, use_or=False) # Deletes items in a database where filters apply.
manager.clear_table() # Deletes all items in the database table.

manager.start_session() # initiates when instance initialized
manager.end_session() # should be called before exiting program

Version History

1.4.1

  • Added search and search_columns parameters to query() for case-insensitive substring matching across string columns. Matches any column by default (OR logic), composable with filters (AND), auto-wraps term as %term%.

1.4.0

  • delete_item() and update_item() now raise ItemNotFoundError when the target ID does not exist.

1.3.x

  • query() enhancements: added limit/offset pagination, multi-column sorting, filter support, and column projection.

1.2.x

  • Added query() method returning results as a pandas DataFrame.

Project details


Download files

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

Source Distribution

pleasant_database-1.4.1.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pleasant_database-1.4.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file pleasant_database-1.4.1.tar.gz.

File metadata

  • Download URL: pleasant_database-1.4.1.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pleasant_database-1.4.1.tar.gz
Algorithm Hash digest
SHA256 8dd69f05aba1d1e602a99fc2a46f566d0abbfddea2b559413b554eea16d67d6e
MD5 3d7910e017255cff757909ea8d35967d
BLAKE2b-256 f0814bbcd470ce5b415e2a7ea67274121a8f8620a42f5b03e92f500e299c9547

See more details on using hashes here.

File details

Details for the file pleasant_database-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: pleasant_database-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pleasant_database-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 10cfc86438649d3a0c910e8fe8b6c759c2ef71618a3c6df0fe259690cbbb894f
MD5 b9f3f12fe6caa9da3326809f699c3580
BLAKE2b-256 c5109c5a27c6f88f224c0a2fb813e6da0512566d602c834ef14074c78a51a92f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page