pyiotools
pyiotools is a Python library that offers various utilities for handling I/O operations, including an IOHandler class for collecting input programmatically, via command-line, or GUI with an API similar to `argparse`. It also includes utilities for caching, serialization, and secret management. The library is actively maintained with frequent minor releases, currently at version 0.3.18, but is explicitly noted as being under active development with a warning that its API may undergo significant breaking changes.
Warnings
- breaking The `pyiotools` library is explicitly stated as being 'under development'. Its API will likely undergo significant changes that may break existing code, and documentation may not always be up-to-date with the latest changes.
- gotcha The `Cache` and `Serializer` classes utilize `dill` for serialization. While `pyiotools` itself does not list `dill` as a direct dependency on PyPI, it is an implicit dependency for these features. If you use `Cache` or `Serializer`, you might need to install `dill` explicitly.
- gotcha Naming your Python script the same as the imported module (e.g., `iotools.py` importing `from iotools import IOHandler`) can lead to import errors or circular import issues, as Python might try to import your local file instead of the installed package.
Install
-
pip install pyiotools
Imports
- IOHandler
from iotools import IOHandler
- Cache
from iotools import Cache
- Secrets
from iotools import Secrets
- Serializer
from iotools import Serializer
- Console
from iotools import Console
Quickstart
import os
from iotools import IOHandler, Cache
# 1. Using IOHandler (similar to argparse)
# Create an IOHandler instance for input collection
program_io = IOHandler(
'my_app',
'A simple application demonstrating iotools input handling.'
)
program_io.add_argument('--name', help='Your name', default='Guest')
program_io.add_argument('--verbose', action='store_true', help='Enable verbose output')
# In a real script, this would parse sys.argv.
# For programmatic quickstart, we simulate parsed arguments.
class MockArgs:
def __init__(self, name, verbose):
self.name = name
self.verbose = verbose
# Use environment variables to make it runnable for testing
args = MockArgs(
name=os.environ.get('IOT_NAME', 'User'),
verbose=os.environ.get('IOT_VERBOSE', 'False').lower() == 'true'
)
print(f"Hello, {args.name}!")
if args.verbose:
print("Verbose output enabled.")
# 2. Using Cache for persistent data storage
cache_name = os.environ.get('IOT_CACHE_NAME', 'my_app_data')
my_cache = Cache(cache_name)
# Put and get data
my_cache.put('favorite_color', os.environ.get('IOT_COLOR', 'blue'))
print(f"Favorite color from cache: {my_cache.get('favorite_color')}")
# Update data
my_cache.put('favorite_color', 'red')
print(f"Updated favorite color: {my_cache.get('favorite_color')}")
# Pop data
popped_color = my_cache.pop('favorite_color')
print(f"Popped favorite color: {popped_color}")
print(f"Is favorite_color still in cache? {my_cache.get('favorite_color', 'Not Found')}")