Skip to content
Adalfarus edited this page Mar 27, 2024 · 16 revisions

database

The database module provides a simplified interface for interacting with databases. It encapsulates database connections, table creation, data insertion, and data retrieval into a convenient object-oriented interface.

Classes

DBManager

DBManager serves as an interface for managing SQLite databases.

Methods

  • __init__(self, path: str): Initializes a DBManager instance by connecting to the SQLite database specified by the path.
  • create_table(self, table_name: str, columns: list): Creates a table with the specified table_name and columns if it doesn't already exist.
  • update_info(self, info: list, table: str, columns: list): Inserts a new row of info into the specified table using the specified columns. The length of info must match the number of columns.
  • get_info(self, table: str, columns: list) -> list: Retrieves data from the specified table and columns, returning the results as a list of tuples.
  • close(self): Commits any pending transactions and closes the database connection.

Usage

The following example demonstrates how to use the DBManager class to interact with an SQLite database:

from aplustools.database import DBManager

# Create a new DBManager instance, connecting to my_db.db
my_db = DBManager("./my_db.db")

# Create a new table named my_table with three columns
my_db.create_table("my_table", ["column1", "column2", "column3"])

# Insert a new row of data into my_table
my_db.update_info(["info1", "info3"], "my_table", ["column1", "column2"])

# Retrieve and print data from my_table
print(my_db.get_info("my_table", ["column3"]))

# Close the database connection
my_db.close()

In this example:

  1. A DBManager instance named my_db is created, connecting to an SQLite database file named my_db.db.
  2. A new table named my_table is created with three columns: column1, column2, and column3.
  3. A new row of data is inserted into my_table, with info1 and info3 populating column1 and column2, respectively.
  4. Data is retrieved from my_table, specifically from column3, and printed to the console.
  5. The database connection is closed using the close method.

faker

This module provides a convenient way to generate test data for various fields such as names, emails, passwords, phone numbers, addresses, and birth dates. It uses randomized selections to create plausible yet fictitious data points.

Classes

Identity

A dataclass representing a user's identity with multiple attributes.

Attributes

  • name: User's first name.
  • last_name: User's last name.
  • email: User's email address.
  • password: User's password.
  • phone_number: User's phone number.
  • address: User's physical address.
  • birth_date: User's birth date.

TestDataGenerator

A class designed for generating random test data.

Methods

  • generate_random_name(): Generates a random name combining a first name and a last name.
  • generate_random_email(): Creates a random email address.
  • generate_random_password(length=10): Generates a random password of a specified length.
  • generate_random_phone_number(): Creates a random phone number in a common format.
  • generate_random_address(): Generates a random physical address.
  • generate_random_birth_date(): Creates a random birth date.
  • generate_random_identity(): Compiles a complete identity using all the above methods.
  • insert_test_data(num_accounts=10, num_events=5): Inserts a specified number of test accounts and events into a system (currently placeholder).

Usage

The following example demonstrates how to use the TestDataGenerator class to generate random test data for different purposes:

from aplustools.data import TestDataGenerator

# Create an instance of TestDataGenerator
test_data = TestDataGenerator()

# Generate a random identity
random_identity = test_data.generate_random_identity()
print(f"Random Identity: {random_identity}")

# Generate individual pieces of data
print(f"Random Name: {test_data.generate_random_name()}")
print(f"Random Email: {test_data.generate_random_email()}")
print(f"Random Password: {test_data.generate_random_password()}")
print(f"Random Phone Number: {test_data.generate_random_phone_number()}")
print(f"Random Address: {test_data.generate_random_address()}")
print(f"Random Birth Date: {test_data.generate_random_birth_date()}")

In this example, we use the TestDataGenerator to create various random data points, which can be helpful for testing databases, forms, or any other systems requiring mock data.

Clone this wiki locally