Skip to main content

PyliteDB is a lightweight, fast and simple database management system

Project description

Pylite Database

Pylite is a lightweight database system built in Python. It provides a simple and intuitive interface for creating, managing, and querying data with support for table relationships, type validation, and event handling.

Features

  • 🔒 Built-in Encryption: Secure your data with AES-GCM encryption
  • 📊 Pandas Integration: Powered by pandas DataFrame for efficient data manipulation
  • 🔗 Table Relationships: Support for primary and foreign key relationships
  • 🎯 Type Validation: Built-in type checking and custom type validators
  • 🎭 Event System: Comprehensive event hooks for database operations
  • 💾 Auto-save Capability: Optional automatic saving after modifications
  • 🔄 SQL Import: Import existing SQLite databases
  • 📝 Custom Types: Includes email and password validators with fake data generation

Installation

pip install PyliteDB

Quick Start

from pylite import Database, email, password

# Create a new database
db = Database(AutoSave=True)

# Create a users table with typed columns
db.CreateTable("Users").AddColumn(
    ID=int,
    Email=email,
    Password=password,
    Username=str
)

# Insert data
db.Users.Insert(
    ID=1,
    Email="user@example.com",
    Password="SecurePass123!",
    Username="john_doe"
)

# Save database with encryption
db.Save("my_database.pylite", "your_secure_password")

# Load existing database
db = Database("my_database.pylite", "your_secure_password")

Working with Tables

Creating Tables

# Create table with columns
db.CreateTable("Products").AddColumn(
    ID=int,
    Name=str,
    Price=float,
    Stock=int
)

CRUD Operations

# Insert
db.Products.Insert(ID=1, Name="Widget", Price=9.99, Stock=100)

# Select
product = db.Products.Get(ID=1)
filtered = db.Products.Select(db.Products.Price > 5.00)

# Update
db.Products.Update(db.Products.ID == 1, Price=10.99)
db.Products.UpdateAt(0, Stock=95)

# Delete
db.Products.Delete(db.Products.Stock == 0)
db.Products.DeleteAt(0)

Table Relationships

# Create related tables
db.CreateTable("Orders").AddColumn(
    OrderID=int,
    ProductID=int,
    Quantity=int
)

# Link tables
db.Link(db.Orders.ProductID, db.Products.ID)

Event Handling

# Add event handlers
db.Users.afterInsert = lambda table, added: print(f"New user added: {added['Username']}")
db.Users.beforeDelete = lambda table: print("About to delete user(s)")

Custom Types

Pylite includes built-in custom types with validation:

Email Type

# Validates email format
db.CreateTable("Contacts").AddColumn(
    Email=email  # Ensures valid email format
)

Password Type

# Enforces password requirements
db.CreateTable("Accounts").AddColumn(
    Password=password  # Requires length, upper/lower case, numbers, special chars
)

Data Security

Pylite uses AES-GCM encryption with:

  • PBKDF2 key derivation
  • Random salt generation
  • Secure IV handling
  • Authentication tags
# Save with encryption
db.Save("secure_database.pylite", "your_strong_password")

# Load encrypted database
db = Database("secure_database.pylite", "your_strong_password")

Method Reference

Database Class Methods

Core Database Operations

Database(Path="", Password="", AutoSave=False)  # Constructor
Database.Save(Path="", Password="", asJson=False)  # Save database to file
Database.Load()  # Load database from file
Database.LoadFromSQL(SQLFilePath)  # Load from SQLite database
Database.ChangePassword(Password)  # Change database encryption password

Table Management

Database.CreateTable(TableName)  # Create new table
Database.DeleteTable(TableName)  # Delete existing table
Database.RenameTable(OldName, NewName)  # Rename table
Database.GetTables()  # Get list of all tables
Database.ClearEmptyTables()  # Remove all empty tables
Database.Link(source, target)  # Create relationship between tables

Table Class Methods

Column Operations

Table.AddColumn(**columns)  # Add new columns with types
Table.RemoveColumn(ColumnName)  # Remove column
Table.RenameColumn(OldName, NewName)  # Rename column
Table.ReorderColumns(NewOrder)  # Reorder columns

CRUD Operations

# Create
Table.Insert(**columns)  # Insert new row

# Read
Table.Select(condition=None)  # Select rows matching condition
Table.Get(**columns)  # Get first row matching conditions
Table[column_name]  # Access column data
Table[row_index]  # Access row data

# Update
Table.Update(selector=None, **columns)  # Update rows matching selector
Table.UpdateAt(index, **columns)  # Update row at specific index

# Delete
Table.Delete(condition=None, index=None, all=False)  # Delete rows
Table.DeleteAt(index)  # Delete row at specific index

Query Operations

Table.Sort(Column, ascending=True)  # Sort table by column
Table.Limit(n)  # Limit to first n rows
Table.RemoveDuplicates()  # Remove duplicate rows
Table.Exists(**columns)  # Check if rows matching conditions exist

Data Analysis

Table.ColumnStats(column)  # Get column statistics
Table.Difference(OtherTable)  # Compare with another table
Table.Intersection(OtherTable, onColumn=None)  # Get common rows
Table.Union(OtherTable)  # Combine tables
Table.Distinct(Column)  # Get unique values in column
Table.Map(Column, Mapping)  # Apply mapping to column

Properties and Attributes

Table.Columns  # List of column names
Table.Rows  # List of all rows
Table.Length  # Number of rows
Table.df  # Access underlying pandas DataFrame
len(table)  # Get number of rows

Storage Operations

Table.SaveToDisk(path)  # Save single table to file
Table.LoadFromDisk(path)  # Load single table from file
Table.toDict()  # Convert table to dictionary
Table.loadFromDict(data)  # Load table from dictionary

Event Handlers

Tables support the following event hooks:

  • beforeInsert, afterInsert
  • beforeUpdate, afterUpdate
  • beforeDelete, afterDelete
  • beforeRenameColumn, afterRenameColumn
  • beforeRemoveColumn, afterRemoveColumn
  • beforeSelect, afterSelect
  • beforeAddColumn, afterAddColumn
  • beforeCopy, afterCopy
# Example of event handler usage
table.afterInsert = lambda table, added: print(f"Added new row: {added}")

Additional Features

  • Auto-save: Enable automatic saving after modifications
  • SQL Import: Import existing SQLite databases
  • Table Operations: Sort, filter, limit, and manipulate data
  • Data Statistics: Get column statistics and table information
  • Data Export: Convert tables to dictionaries or save individually

Best Practices

  1. Always use strong passwords for database encryption
  2. Enable AutoSave for important data
  3. Implement event handlers for critical operations
  4. Use appropriate column types for data validation
  5. Regular backups of database files

Contributing

Contributions are welcome! Please feel free to submit pull requests, create issues, or suggest improvements.

License

MIT License

Support

For support, please open an issue on the GitHub repository or contact the maintainers.

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

pylitedb-0.4.1.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

PyliteDB-0.4.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file pylitedb-0.4.1.tar.gz.

File metadata

  • Download URL: pylitedb-0.4.1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for pylitedb-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b6580bfd0c80f146d062dea42e66aa5ecb51bfde1e1038731985a24e309ad8cc
MD5 959672e4d88ca0c5b5433e32ac3c3343
BLAKE2b-256 63ac2e3aced29f3c2a67eb109f3ef6809793216063263e5b0e31e841af3bd46f

See more details on using hashes here.

File details

Details for the file PyliteDB-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: PyliteDB-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for PyliteDB-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 676ca383c4c7a9e01162149eca9c9ddc433d48ca46cf7e5af931cc9bcbce5390
MD5 891eb7539b2d38ef81746b3ecb1ae97d
BLAKE2b-256 a532af4dea702652215c7ed3bc6cefc172d2e493235d2c38d6c75ac4ffc77964

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