Skip to main content

tasks-loader

The tasks-loader module is designed to simplify the process of managing and taking actions based on the rows of an input CSV file.

Banner

This module enables quick and easy loading and adaptation of any CSV file based on the data management needs encountered in various projects. Whether automating task creation, performing data validation, or preprocessing information for further analysis, tasks-loader offers a robust foundation for working with CSV data in Python.

Table of Contents

Installation

To install tasks-loader, run the following command:

pip install tasks-loader

Key Features

  • Dynamic Task Creation: Automatically generates tasks from CSV rows, facilitating easy manipulation and processing.
  • Flexible Validation: Supports extensive validation mechanisms including type checks, regex patterns, lambda functions, and predefined choices.
  • Customizable Default Values: Enables specifying global or field-specific default values for handling missing or invalid data.
  • Post-processing Hooks: Allows for data transformation after loading, such as converting units or normalizing strings.
  • Automatic Header Adjustment: Converts CSV headers to lowercase and replaces spaces with underscores, simplifying attribute access in Python.
  • Iterability and Validation Checks: Offers built-in support for task validity checks and iteration over tasks, streamlining application control flow.
  • Extensible Design: Designed for easy extension to include more validation rules, data transformations, or export options.
  • Increasing Task ID: No need to specify task IDs in the CSV file; the module automatically assigns incremental IDs to each task.

Task Format Dictionary

The task format dictionary defines how each field in your CSV file is processed. Here’s what you need to know about constructing this dictionary:

  • type: The expected Python type of the field (e.g., str, int).
  • required: If set to True, the field must be present and not empty; otherwise, it's considered optional.
  • default: A default value to use if the field is missing or fails validation.
  • choices: A list of acceptable values for the field.
  • validation: A regex pattern or a lambda function for additional validation criteria.
  • post_format: A function to transform the field value after all other processing steps.

If a task is invalid (e.g., missing required fields, failed validation), it will not be included in the task list. The global_default argument in the Tasks constructor allows you to specify a fallback value for any field that is not explicitly handled by the task format.

Examples

Below are examples demonstrating how to use tasks-loader with different configurations and CSV files.

Example 1: Basic Task Loading

CSV (basic_tasks.csv):

Name,Email,Price
John Doe,johndoe@example.com,19.99
Jane Smith,,

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "email": {"type": str, "required": True, "default": "no-email@example.com"},
    "price": {"type": float, "required": False, "default": 9.99},
}

Python Code:

from tasks_loader import Tasks

tasks = Tasks(input_file='basic_tasks.csv', task_format=task_format)

for task in tasks:
    print(task) 
    # Task is going to be a dict like
    # {'id': '001', 'name': 'John Doe', 'email': 'johndoe@example.com', 'price': 19.99}

Example 2: Advanced Validation with Regex and Lambda

CSV (advanced_validation.csv):

Name,Email,Role
John Doe,johndoe@example.com,admin
Jane Smith,janesmith@bademail,visitor

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "email": {
        "type": str,
        "required": True,
        "validation": r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
    },
    "role": {
        "type": str,
        "required": True,
        "choices": ["admin", "user", "guest"],
        "validation": lambda x: x in ["admin", "user", "guest"]
    },
}

Python Code:

from tasks_loader import Tasks

tasks = Tasks(input_file='advanced_validation.csv', task_format=task_format)

for task in tasks:
    print(task)

Example 3: Post-processing Data

CSV (post_processing.csv):

Name,DelaySeconds
John Doe,30
Jane Smith,45

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "delay_seconds": {
        "type": int,
        "required": True,
        "post_format": lambda x: x * 1000  # Convert seconds to milliseconds
    },
}

Python Code:

from tasks_loader import Tasks

tasks = Tasks(input_file='post_processing.csv', task_format=task_format)

for task in tasks:
    print(task)

Example 4: Ticket / Sneakers Bot-like Validation

This example demonstrates a setup tailored for automated tasks commonly used in ticket purchasing or sneaker bot scenarios. The configuration ensures all necessary information for a purchase is validated against specific criteria before proceeding with a task.

CSV (ticket_sneakers_bot.csv):

Name,Surname,Mail,URL,Min Price,Max Price,Error Delay, Monitor Delay,Credit Card Number,Credit Card Month,Credit Card Year,Credit Card CVV
John,Doe,johndoe@example.com,https://example.com,50,500,1000,5000,1234567890123456,01,2024,123
Jane,Smith,janesmith@example.com,https://invalid,0,,500,2000,1234567890123456,12,2023,456

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "delay_seconds": {
        "type": int,
        "required": True,
        "post_format": lambda x: x * 1000  # Convert seconds to milliseconds
    },
}

Task Format: This setup includes several validation steps to ensure each field meets the requirements for automated purchasing tasks:

import re

task_format = {
    "name": {"type": str, "required": True, "validation": lambda x: isinstance(x, str) and x.strip() != ""},
    "surname": {"type": str, "required": True, "validation": lambda x: isinstance(x, str) and x.strip() != ""},
    "mail": {
        "type": str,
        "required": True,
        "validation": r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
    },
    "url": {
        "type": str,
        "required": True,
        "validation": r"^https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:/[a-zA-Z0-9_./-]+)*$"
    },
    "min_price": {"type": float, "required": False, "default": 0},
    "max_price": {"type": float, "required": False, "default": 10000},
    "error_delay": {"type": int, "required": False, "post_format": lambda x: x // 1000},
    "monitor_delay": {"type": int, "required": False, "post_format": lambda x: x // 1000},
    "credit_card_number": {"type": str, "required": True, "validation": lambda x: len(x) == 16 and x.isdigit()},
    "credit_card_month": {"type": str, "required": True, "validation": r"^(0[1-9]|1[0-2])$"},
    "credit_card_year": {"type": str, "required": True, "validation": r"^\d{4}$"},
    "credit_card_cvv": {"type": str, "required": True, "validation": r"^\d{3}$"},
}

Python Code:

from tasks_loader import Tasks

tasks = Tasks(input_file='ticket_sneakers_bot.csv', task_format=task_format)

for task in tasks:
    StartTask(**task) # Apply the dict of the task as kwargs

Validation Explained:

  • Name and Surname: Must be valid strings that are not just whitespace.
  • Mail: Uses a regex pattern to validate that the email address is in a proper format.
  • URL: Validates URLs with a regex pattern to ensure they start with http or https and are generally formatted correctly.
  • Min and Max Price: Validates that these are floats, with defaults of 0 and 10000 respectively if not specified or invalid.
  • Error and Monitor Delay: These are expected to be integers representing milliseconds; they are post-processed to convert to seconds.
  • Credit Card Number: Must be a 16-digit string.
  • Credit Card Month: Validates with a regex pattern that the month is in MM format, allowing only values from 01 to 12.
  • Credit Card Year: Must be a 4-digit year in YYYY format.
  • Credit Card CVV: A 3-digit security code validated with a regex pattern.

This setup is an example of an automated module that initiates tasks based on the validated entries from a CSV file, ensuring each task has all the necessary and correctly formatted information before proceeding. This kind of validation is crucial in scenarios where precise and validated data is essential for the success of automated tasks, such as in ticket purchasing or sneaker bot operations.

Example 2: Advanced Validation with Regex and Lambda

CSV (advanced_validation.csv):

Name,Email,Role
John Doe,johndoe@example.com,admin
Jane Smith,janesmith@bademail,visitor

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "email": {
        "type": str,
        "required": True,
        "validation": r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
    },
    "role": {
        "type": str,
        "required": True,
        "choices": ["admin", "user", "guest"],
        "validation": lambda x: x in ["admin", "user", "guest"]
    },
}

Python Code:

from tasks_loader import Tasks

tasks = Tasks(input_file='advanced_validation.csv', task_format=task_format)

for task in tasks:
    print(task)

Example 5: Using Pre-Made Regex Checks with the Patterns Class

CSV (regex_validation.csv):

Name,Email,Phone,Month
John Doe,johndoe@example.com,+12345678901,January
Jane Smith,janesmith@bademail,+12345,Smarch

Task Format:

task_format = {
    "name": {"type": str, "required": True},
    "email": {
        "type": str,
        "required": True,
        "validation": Patterns.MAIL
    },
    "phone": {
        "type": str,
        "required": True,
        "validation": Patterns.PHONE
    },
    "month": {
        "type": str,
        "required": True,
        "validation": Patterns.MONTH
    }
}

Python Code:

from tasks_loader import Patterns

task_format = {
    "name": {"type": str, "required": True},
    "email": {
        "type": str,
        "required": True,
        "validation": Patterns.MAIL
    },
    "phone": {
        "type": str,
        "required": True,
        "validation": Patterns.PHONE
    },
    "month": {
        "type": str,
        "required": True,
        "validation": Patterns.MONTH
    }
}

Example 6: File Check and Creation Handling

If the specified CSV file does not exist, ty can createa new one by calling the function create_file with the appropriate headers based on the task_format to ensure data consistency.

Python Code:

from tasks_loader import Tasks

# Define the CSV file and format
tasks = Tasks(input_file='new_tasks.csv', task_format=task_format)

if tasks.create_file():
    print("File created, please fill it and then restart the script")
    exit(0)
print("File aready created")

Stay in touch with me

For any inquiries or further information, please reach out:

Feel free to contact for collaborations, questions, or feedback any of my projects.

Metadata

Release files for tasks-loader 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tasks-loader 0.1.1
File Size Uploaded Provenance Actions
tasks_loader-0.1.1.tar.gz 11.3 kB Without provenance Details for tasks_loader-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tasks-loader 0.1.1
File Interpreter ABI Platform Actions
tasks_loader-0.1.1-py3-none-any.whl Python 3 none any Details for tasks_loader-0.1.1-py3-none-any.whl

Total release size: 19.9 kB

Release files / tasks_loader-0.1.1.tar.gz

Download URL tasks_loader-0.1.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
de65a05f8676481e5703bd6d4adf3a3530b089e4e937e90cd4eef47cb5cb3a46
BLAKE2b-256 checksum
How to use checksums
5f24c5d1325d11781c8c86bfec89fc229cec9cc2bd1ee9a75e112bda61f25f4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.0

Release files / tasks_loader-0.1.1-py3-none-any.whl

Download URL tasks_loader-0.1.1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ae47a7c5ab691285fee59e6c0d19cadd5e5d2cee326816c9bdd7e2bfe84d23d
BLAKE2b-256 checksum
How to use checksums
b45b6259fb1782720045604db4f1e019a67cb6f8bf623e3d98809fa40d33d03d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.0

Security for tasks-loader 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page