Banal Python Micro-functions

1.0.6 · maintenance · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.

import os
from banal import ensure_list, clean_dict

# Example 1: ensure_list - ensures an argument is a list
print("--- ensure_list examples ---")
print(f"Scalar to list: {ensure_list('hello')}")
print(f"None to list: {ensure_list(None)}")
print(f"Tuple to list: {ensure_list(('a', 'b'))}")
print(f"Existing list: {ensure_list([1, 2, 3])}")

# Example 2: clean_dict - removes None values from a dict, recursively
print("\n--- clean_dict examples ---")
data_with_nones = {
    "name": "Alice",
    "age": 30,
    "email": None,
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "zip": None
    },
    "preferences": []
}
cleaned_data = clean_dict(data_with_nones)
print(f"Original: {data_with_nones}")
print(f"Cleaned: {cleaned_data}")

# clean_dict typically keeps empty strings, zeros, False, empty lists/dicts
data_with_falsey = {
    "key1": "value",
    "key2": "",
    "key3": None,
    "key4": 0,
    "key5": False,
    "key6": [],
    "key7": {},
    "nested": {
        "sub_key1": "abc",
        "sub_key2": None
    }
}
cleaned_falsey = clean_dict(data_with_falsey)
print(f"\nOriginal (falsey values): {data_with_falsey}")
print(f"Cleaned (falsey values): {cleaned_falsey}")

view raw JSON →