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_locationproperty - 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 ton/Nwherenis the iteration number andNis 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
formatterattribute 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file small_py-1.1.2.tar.gz.
File metadata
- Download URL: small_py-1.1.2.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b14a3b278db01a14af73c8973f026f3a32d79d04b3f72158446733e8f7b3b0f1
|
|
| MD5 |
03d138a2f30be2004e0c67a8626e73e8
|
|
| BLAKE2b-256 |
f0e1e0e669aa43c39b36b49ae228d1483825d93c53156f65ad11ae610b56bc5a
|
Provenance
The following attestation bundles were made for small_py-1.1.2.tar.gz:
Publisher:
publish.yml on n-smalley/smallpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
small_py-1.1.2.tar.gz -
Subject digest:
b14a3b278db01a14af73c8973f026f3a32d79d04b3f72158446733e8f7b3b0f1 - Sigstore transparency entry: 1117303917
- Sigstore integration time:
-
Permalink:
n-smalley/smallpy@87627ce1bfe60bfdad94fa0ed4701c7fa48f3649 -
Branch / Tag:
refs/tags/v1.1.2 - Owner: https://github.com/n-smalley
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@87627ce1bfe60bfdad94fa0ed4701c7fa48f3649 -
Trigger Event:
push
-
Statement type:
File details
Details for the file small_py-1.1.2-py3-none-any.whl.
File metadata
- Download URL: small_py-1.1.2-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5046aa4dd67b1c64c4a2dbb245de509183d30a080da6cad0a8fe524588234059
|
|
| MD5 |
56bc8d0c99c85cf937b68c32c37af0cb
|
|
| BLAKE2b-256 |
a46037866f061d99e0fd82575ba5ce0ef3d0c9655884b8ea42e0cb6fb61150f1
|
Provenance
The following attestation bundles were made for small_py-1.1.2-py3-none-any.whl:
Publisher:
publish.yml on n-smalley/smallpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
small_py-1.1.2-py3-none-any.whl -
Subject digest:
5046aa4dd67b1c64c4a2dbb245de509183d30a080da6cad0a8fe524588234059 - Sigstore transparency entry: 1117303972
- Sigstore integration time:
-
Permalink:
n-smalley/smallpy@87627ce1bfe60bfdad94fa0ed4701c7fa48f3649 -
Branch / Tag:
refs/tags/v1.1.2 - Owner: https://github.com/n-smalley
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@87627ce1bfe60bfdad94fa0ed4701c7fa48f3649 -
Trigger Event:
push
-
Statement type: