Banal is a Python library providing a collection of 'micro-functions' focused on handling and buffering type uncertainties. It's designed as an outsourced utility module to simplify common tasks like checking if an object is list-like or ensuring an argument is a list. The current version is 1.0.6, released in February 2021, indicating a low release cadence and a mature, stable codebase.
pip install banalVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the `ensure_list` function for normalizing various inputs into lists and the `clean_dict` function for recursively removing `None` values from dictionaries. The library's functions are generally self-contained and directly imported.
Review the documentation for each function to understand its specific type-handling behavior and ensure it aligns with your application's requirements. Explicitly validate types before passing if strictness is required.
Test thoroughly on newer Python versions. For new projects, consider if a more actively maintained utility library better suits long-term compatibility and feature needs. For existing projects, be aware of potential subtle incompatibilities with very recent Python versions.
For new Python 3-only projects, verify that `six` is not being implicitly installed or relied upon if not explicitly needed. For older projects, be aware of this dependency if migrating between Python versions.
Install the library using pip: `pip install banal`
Correct the function name to one of the available functions, such as `banal.is_listish`.
If you intend to flatten an iterable into a list of its individual elements, manually convert it. If you explicitly want to wrap any non-list into a single-element list, ensure your subsequent code handles the wrapped iterable correctly.
```python
import banal
# Scenario leading to TypeError (original intent: flatten tuple)
my_data = (1, 2, 3)
processed_data = banal.ensure_list(my_data) # Result: [(1, 2, 3)]
# Assuming you then iterate expecting individual integers:
# for item in processed_data: # item becomes (1, 2, 3)
# result = item + 1 # This would raise TypeError
# Fix: If the goal is to flatten an iterable:
if not isinstance(my_data, list):
my_data = list(my_data) # Converts (1, 2, 3) to
# Or, if `ensure_list` is desired but you need to process the inner iterable:
processed_data = banal.ensure_list(my_data)
if isinstance(processed_data, (tuple, list)): # Check if it wrapped an iterable
final_data = list(processed_data) # Flatten it
else:
final_data = processed_data
# Now final_data will be
for item in final_data:
result = item + 1 # Works correctly
```