Wimpy
Wimpy is a Python utility library (anti-copy-pasta) containing commonly used helper functions that the author extracted to avoid repeatedly copying them into new projects. It provides a collection of small, general-purpose utilities for everyday Python programming tasks. The library is currently at version 0.6 and is actively maintained, with releases focusing on adding new features and improving existing ones.
Common errors
-
from wimpy import WimpyError ModuleNotFoundError: No module named 'WimpyError'
cause The `WimpyError` exception is located in the `wimpy.exceptions` submodule, not directly in the top-level `wimpy` module.fixChange the import statement to `from wimpy.exceptions import WimpyError`. -
wimpy.exceptions.WimpyError: Prefix 'non_existent_' not found in 'hello_world'
cause You are using `strip_prefix` or `strip_suffix` with `strict=True`, and the specified prefix/suffix was not present in the target string.fixEither ensure the prefix/suffix always exists, or handle the `wimpy.exceptions.WimpyError` exception, or remove `strict=True` to revert to the default behavior of returning the original string when the prefix/suffix is missing.
Warnings
- gotcha The `strip_prefix` and `strip_suffix` functions in v0.6+ introduce a `strict` parameter (default `False`). If `strict=True`, the function will raise a `WimpyError` if the prefix/suffix is not found. Prior to v0.6, there was no `strict` option, and missing prefixes/suffixes would simply return the original string.
- gotcha The `chunks` function had a bug in versions prior to v0.6 where negative `overlap` values were not handled correctly and could lead to unexpected results. This was fixed in v0.6.
- gotcha The `strip_prefix` and `strip_suffix` functions by default will silently return the original string if the specified prefix or suffix is not found. This might be unexpected if you anticipated an empty string or an error.
Install
-
pip install wimpy
Imports
- strip_prefix
from wimpy import strip_prefix
- strip_suffix
from wimpy import strip_suffix
- is_subsequence
from wimpy import is_subsequence
- chunks
from wimpy import chunks
- grouper
from wimpy import grouper
- ceiling_division
from wimpy import ceiling_division
- WimpyError
from wimpy import WimpyError
from wimpy.exceptions import WimpyError
Quickstart
from wimpy import strip_prefix, is_subsequence, chunks
# Example 1: Stripping prefixes
text = "hello_world"
stripped_text = strip_prefix(text, "hello_")
print(f"Original: {text}, Stripped: {stripped_text}")
# Example 2: Checking for subsequence
sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sub = [2, 5, 8]
is_sub = is_subsequence(sub, sequence)
print(f"Is {sub} a subsequence of {sequence}? {is_sub}")
# Example 3: Chunking an iterable
data = list(range(10))
chunk_size = 3
overlap = 1
result_chunks = list(chunks(data, chunk_size, overlap))
print(f"Original data: {data}, Chunks (size={chunk_size}, overlap={overlap}): {result_chunks}")