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 utils import (
  enable_virtual_terminal,
  clear_terminal,
  write_to_excel,
  add_to_memory,
  is_in_memory,
  wait_for_image,
  find_and_click,
  Counter,
  get_most_recent_file,
)

Console Utilities

Enable ANSI / Virtual Terminal Support (Windows)

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)

Wait for an Image

coord = wait_for_image("button.png", timeout=10)
  • Accepts a single image path or a list of paths
  • Can optionally wait for an image to disappear:
wait_for_image("loading.png", invert_search=True)

Find and Click an Image

Clicks on the center of the found image

find_and_click("submit.png")
  • Optionally offset the click location, measured in pixels from the center of the reference image
    • offset=(x_offset,y_offset)
    • increasing x offsets to right, increasing y offsets down
find_and_click("submit.png",offset=(5,5))

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-0.2.3.tar.gz (10.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-0.2.3-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: small_py-0.2.3.tar.gz
  • Upload date:
  • Size: 10.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-0.2.3.tar.gz
Algorithm Hash digest
SHA256 5c83988ba46a90caa70041877a875e3515364b06811c9e8f89f195ad8d631b43
MD5 c8737b8bcc8f0a51af42dc7c8236e3ee
BLAKE2b-256 4611cfd68dbe69df9093707b16e1123318f21d648e044b7be4ad9a4a47951c9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for small_py-0.2.3.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-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: small_py-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5cc01a9c66d851992e08dad2a90afff626df3bfca08c5b82a079290cae488a24
MD5 1e08dc3623abf1c874b8e9497ad777aa
BLAKE2b-256 8d4bb698763b0f67560541fedd3de44af2de039991fe06eeee3b2d43fc6a4fea

See more details on using hashes here.

Provenance

The following attestation bundles were made for small_py-0.2.3-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