Today I Learned

A collection of small things I've learned, presented in a collapsible format.

Python doctests for inline testing

Aug 18, 2025

Python’s doctest module allows you to write tests directly in docstrings by mimicking interactive Python sessions. Simply write >>> followed by Python code and the expected output on the next line.

def add(a, b):
    """
    Add two numbers.
    
    >>> add(2, 3)
    5
    >>> add(-1, 1)
    0
    """
    return a + b

Run with python -m doctest filename.py or include doctest.testmod() in your script. Great for keeping examples and tests close to the code they document.


Python binarytree library for tree visualization

Aug 18, 2025

The binarytree library provides an easy way to generate, visualize, and manipulate binary trees in Python. Install with pip install binarytree.

from binarytree import Node, tree

# Create a tree manually
root = Node(1)
root.left = Node(2)
root.right = Node(3)
print(root)

# Generate random trees
my_tree = tree(height=3)
print(my_tree)

Perfect for algorithm practice, education, or debugging tree-based code. The ASCII visualization makes it easy to understand tree structure at a glance.