-
Notifications
You must be signed in to change notification settings - Fork 0
data
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.
DBManager
serves as an interface for managing SQLite databases.
-
__init__(self, path: str)
: Initializes aDBManager
instance by connecting to the SQLite database specified by thepath
. -
create_table(self, table_name: str, columns: list)
: Creates a table with the specifiedtable_name
andcolumns
if it doesn't already exist. -
update_info(self, info: list, table: str, columns: list)
: Inserts a new row ofinfo
into the specifiedtable
using the specifiedcolumns
. The length ofinfo
must match the number ofcolumns
. -
get_info(self, table: str, columns: list) -> list
: Retrieves data from the specifiedtable
andcolumns
, returning the results as a list of tuples. -
close(self)
: Commits any pending transactions and closes the database connection.
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:
- A
DBManager
instance namedmy_db
is created, connecting to an SQLite database file namedmy_db.db
. - A new table named
my_table
is created with three columns:column1
,column2
, andcolumn3
. - A new row of data is inserted into
my_table
, withinfo1
andinfo3
populatingcolumn1
andcolumn2
, respectively. - Data is retrieved from
my_table
, specifically fromcolumn3
, and printed to the console. - The database connection is closed using the
close
method.
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.
A dataclass representing a user's identity with multiple 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.
A class designed for generating random test data.
- 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).
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.