Functionalies#

Shortcut#

The Shortcut widget enables assigning keyboard shortcuts to specific actions.

Shortcut(key='Ctrl+S', func=function)

Key Attributes:

  • key (str): The keyboard shortcut (e.g., 'Ctrl+S').

  • func (function): The function to be executed when the shortcut is triggered.

Example:

Shortcut(key='Ctrl+Q', func=lambda: print('Quit triggered!'))

Screenshot#

The Screenshot function captures the current window or screen content and saves it as an image.

screenshot(filename='screenshot.png')

Key Attributes:

  • filename (str): Path to save the captured screenshot.

Example:

screenshot(filename='capture.png')

Thread#

The Thread widget allows running tasks in a separate thread to avoid blocking the main application.

thread(target=background_task)

Key Attributes:

  • target (function): The function to run in the thread.

  • args (tuple): Positional arguments to pass to the target function.

  • kwargs (dict): Keyword arguments to pass to the target function.

  • wait (bool): If True, don’t run the process immediately.

Example:

def process(name, count=1):
    for i in range(count):
        print(f"Task {name}: step {i}")

thread(target=process, args=('worker',), kwargs={'count': 3}, wait=False)

Encode#

The Encode function is used to encode data into a specified format such as Base64. Data such as images, strings, binary or any file.

encoded_data = encode(b'Hello, World!')
print(encoded_data)  # Output: 'SGVsbG8sIFdvcmxkIQ=='

Decode#

The Decode function is used to decode data from a specified encoded format such as Base64. Data such as images, strings, binary or any file.

decoded_data = decode('SGVsbG8sIFdvcmxkIQ==')
print(decoded_data)  # Output: 'Hello, World!'

Database#

The Database class provides an abstraction for interacting with an SQLite database. It abstracts common database operations such as creating tables, inserting, selecting, updating, and deleting records. Below is a detailed guide on how to use this class and its methods effectively.

Initialization#

To use the database, first, you need to initialize an instance of the Database class.

Syntax:

db = Database(name='example', PATH='/path/to/database')

Parameters:

  • name (str): The name of the database file. If not provided, it defaults to 'clera.db'. Ensure the name does not include the .db extension, as it is automatically appended.

  • PATH (str): The directory path where the database file is stored. If not provided, it defaults to the current working directory.

Example:

db = Database(name='my_database', PATH='./databases')

create Method#

This method creates a new table in the database.

Syntax:

db.create(name='table_name', data={'column_name': data_type, ...}, commit=True)

Parameters:

  • name (str): The name of the table.

  • data (dict): A dictionary where keys are column names and values are data types. Supported types include:

    • intINTEGER

    • floatREAL

    • strTEXT

    • blobBLOB

    • null/NoneNULL

  • commit (bool): Whether to commit the transaction immediately. Defaults to True

Example:

db.create('users', {'id': int, 'name': str, 'age': int})

insert Method#

This method inserts data into a table.

Syntax:

db.insert(table='table_name', value=data, commit=True)

Parameters:

  • table (str): The name of the table.

  • value (any): The data to insert. It can be:

    • dict: Key-value pairs of column names and values.

    • list/tuple: A sequence of values corresponding to the table’s columns.

  • commit (bool): Whether to commit the transaction immediately. Defaults to True.

Example:

# Insert using a dictionary
db.insert('users', {'id': 1, 'name': 'Alice', 'age': 30})

# Insert using a list or tuple
db.insert('users', (2, 'Bob', 25))

select Method#

This method retrieves data from a table.

Syntax:

db.select(table='table_name', data='*', condition='')

Parameters:

  • table (str): The name of the table.

  • data (str): The columns to retrieve. Defaults to "*", which selects all columns.

  • condition (str): An optional SQL WHERE clause to filter results.

Returns:

  • A list of tuples representing the query result.

Example:

# Select all rows
rows = db.select('users')
print(rows)

# Select specific columns
rows = db.select('users', data='name, age')

# Select with a condition
rows = db.select('users', condition="age > 25")

update Method#

This method updates data in a table.

Syntax:

db.update(table='table_name', value={'column': value, ...}, condition='SQL condition')

Parameters:

  • table (str): The name of the table.

  • value (dict): A dictionary where keys are column names and values are the new data.

  • condition (str): A SQL WHERE clause to specify which rows to update.

Example:

db.update('users', {'name': 'Charlie'}, "id = 1")

delete Method#

This method deletes rows from a table based on a condition.

Syntax:

db.delete(table='table_name', condition='SQL condition')

Parameters:

  • table (str): The name of the table.

  • condition (str): A SQL WHERE clause to filter which rows to delete.

Example:

db.delete('users', "age < 25")

drop Method#

This method removes a table from the database.

Syntax:

db.drop(table='table_name')

Parameters:

  • table (str): The name of the table.

Example:

db.drop('users')

commit and close Methods#

  • commit(): Manually commits the current transaction to the database.

    db.commit()
    
  • close(): Closes the database connection.

    db.close()
    

Example Workflow#

Here’s how you can use the Database class to manage data:

# Initialize the database
db = Database(name='my_app')

# Create a table
db.create('users', {'id': int, 'name': str, 'age': int})

# Insert data
db.insert('users', {'id': 1, 'name': 'Alice', 'age': 30})
db.insert('users', (2, 'Bob', 25))

# Retrieve data
print(db.select('users'))  # [(1, 'Alice', 30), (2, 'Bob', 25)]

# Update data
db.update('users', {'name': 'Charlie'}, "id = 1")

# Delete data
db.delete('users', "age < 25")

# Drop table
db.drop('users')

# Close the connection
db.close()