Skip to main content

Utility library for various uses

Project description

smallpy Utility Functions

A collection of reusable Python utility functions and classes for:

  • console output control
  • JSON "memory" file management
  • Excel exporting
  • basic image recognition and screen navigation
  • progression tracking with time estimation

This module is intended to be imported and reused across automation and data-processing scripts.


Features

  • Enable ANSI color / cursor control on Windows terminals
  • Clear previously printed terminal lines
  • Write Pandas DataFrames to Excel
  • Persist structured data to JSON "memory"
    • check for existing entries in "memory"
  • Wait for UI images to appear or disappear (via PyAutoGUI)
  • Click UI elements based on image matching
  • Track progess of iteration with dynamic formatting options
  • Get most recentely created file in a directory

Installation

pip install small-py

Dependencies:

pip install pandas pyautogui

Usage

Import the functions or classes you need:

from smallpy.utils import (
  enable_virtual_terminal,
  clear_terminal,
  write_to_excel,
  Counter,
  get_most_recent_file,
)
from smallpy.memory import (
  add_to_memory,
  is_in_memory
)
from smallpy.screen.navigation import (
  BaseScreen,
  ImageLocator
)

Or more concisely:

from smallpy import callable_name

Console Utilities

Enable ANSI / Virtual Terminal Support (Windows)

Need to call if using clear_terminal() in command prompt window

enable_virtual_terminal()

Clear Previously Printed Lines

clear_terminal(lines=2)

Excel Output

Write Dataframe to Excel

import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
write_to_excel(df, "output.xlsx")

Persistent Memory (JSON)

Stored in ./memory/memory.json by default

Memory Strucutre

{"key":"entry"}
{
  "key1":{
    "id":"entry1",
    "optional_field_1":"value",
    ...
    "optional_field_n":"value"
  },
  ...
}
  • "Key" identifies a shared entry structure for a particular purpose
    • All entries under the same key must share the same structure
    • A "memory" file can have multiple "keys"
  • Entries must contain an "id" field
  • New entries replace existing entries with the same id

Add or Update an Entry

entry = {"id": 1, "status": "done"}
add_to_memory(
  memory_key = "tasks",
  new_entry = entry
)

Check if an Entry Exists

exists = is_in_memory(
  memory_key="tasks",
  new_entry={"id": 1, "status": "done"},
  comparison_field="status"
)

Screen Automation (PyAutoGUI)

ImageLocator

ImageLocator provides a simple interface to wait for images to appear or disappear on screen.
It is useful for automating UI interactions based on visual cues.

Features

  • Wait for a single image or a list of images to appear on screen
  • Wait for images to disappear
  • Configurable confidence threshold for image recognition
  • Keeps a history of located coordinates
  • Access the last known location via the last_location property
  • Optional status printing with indentation

Usage

from pathlib import Path
from smallpy.screen.navigation import ImageLocator

# Initialize with a single image
locator = ImageLocator(Path("submit_button.png"))

# Initialize with multiple images
locator = ImageLocator([Path("submit_button.png"), Path("icon.png")])

# Wait for image to appear
location = locator.locate(timeout=5, confidence=0.95)

# Access the most recent location
last = locator.last_location

# Wait for images to disappear
locator.wait_for_disappear(timeout=10)

BaseScreen

BaseScreen is an abstract base class designed for screen automation.
It standardizes image management and interaction, allowing subclasses to inherit image paths and utility methods without hardcoding images.

Features

  • Dynamically load or accept a dictionary of image paths (Path) for a given domain or project.
  • Group images by base name, stripping numeric suffixes (e.g., button1.png, button2.png"button" key).
  • Click images with optional offsets, multiple clicks, and intervals.
  • Click arbitrary screen coordinates.
  • Click a series of images in order.
  • Wait for specific images to appear on screen using ImageLocator.
  • Subclasses inherit all image paths and helper methods without needing to redefine .images.

Usage

Using a Config Dictionary
from pathlib import Path
from config import IMAGE_DICT
from smallpy.screen.navigation import BaseScreen

class LoginScreen(BaseScreen):
    def __init__(self):
        super().__init__(IMAGE_DICT)

login_screen = LoginScreen()
paths = login_screen._get_path("username_field")

Progress Tracking

Counter Class

Tracks progress and estimates remaining time.

counter = Counter(
    count=10
)

for _ in range(10):
    # do work
    counter.display()
  • Output of counter.display() will default to n/N where n is the iteration number and N is the total count
  • A custom format can be passed upon initialization
counter = Counter(
    count=10,
    format = "Iteration %n/%N"
)

for _ in range(10):
    # do work
    counter.display()
  • Or by changing the formatter attribute to utilize dynamic formatting with f-strings
counter = Counter(
    count=10
)

for item in ['foo','bar','baz','qux']:
    # do work
    counter.formatter = f"Iteration %n/%N - {item}"
    counter.display()

Format tokens:

  • %n — iteration number
  • %N — total count
  • %T — estimated completion time (e.g. "02:04 PM")
  • %t — raw seconds remaining as float
  • %f — time remaining split by unit (e.g. "2h 4m 8s")

File Utilities

Get Most Recent File in a Directory

latest = get_most_recent_file("downloads")

Returns the path to the most recently created file, or "No files found".

Notes & Limitations

  • Screen automation relies on image matching and screen resolution
  • JSON memory assumes consistent dictionary structure per key
  • Designed for scripting and automation, not as a full framework

License

MIT License (or update as appropriate)

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

small_py-1.0.4.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

small_py-1.0.4-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file small_py-1.0.4.tar.gz.

File metadata

  • Download URL: small_py-1.0.4.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for small_py-1.0.4.tar.gz
Algorithm Hash digest
SHA256 f2e39c1023efa732ad237ef62860fbe0115efbdb577e912b68899640230d3b8e
MD5 d0f7f153294faa8c9c80c2a5a46c312b
BLAKE2b-256 0aa48512cc3a3559014ab8421e26bf091edcb8d4eef298ba15569f5e9dfe1f82

See more details on using hashes here.

Provenance

The following attestation bundles were made for small_py-1.0.4.tar.gz:

Publisher: publish.yml on n-smalley/smallpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file small_py-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: small_py-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for small_py-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4f3b04890f9eccfdd83fd91fa2ea9092101f17c09b8e36e6109dba354c5c4b95
MD5 1980251372efd920452588b9a509ef63
BLAKE2b-256 48e11e81be0441bf21017ae816673ba353e6a6d7ae0ed12fae1b570350579f0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for small_py-1.0.4-py3-none-any.whl:

Publisher: publish.yml on n-smalley/smallpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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