I’m Blake. I write about probability, stochastic processes, Python tooling, and the practical side of building things with code. More about me →

  • Deep comparison of Python dictionaries

    Imagine you have configuration data, API payloads, or test expectations, and you want to verify that a smaller, “subset” dictionary is fully contained within a larger “superset” dictionary, including all the nested dictionaries and lists that it may contain.

    Python’s built-in tools like dict.items() and set operations are fantastic for shallow comparisons.

    There’s some built in methods that spring to mind for this problem, and they’re perfect for flat simple dictionaries:

    • subset.items() <= superset.items()
    • all(item in superset.items() for item in subset.items())

    While useful for simple dictionaries, it only checks the top level. If a value is another dictionary, it compares the dictionary objects themselves, not their contents. For example { 'a': {'b': 1} } is not a subset of { 'a': {'b': 1, 'c': 2} } using these method, because the inner dictionaries are different objects.

    These methods are perfect for flat dictionaries but fall flat when dealing with nested data.

    from typing import Any
    
    
    def is_subset_dict(subset: dict[str, Any], superset: dict[str, Any]) -> bool:
        """Checks if a dictionary 'subset' is a deep subset of 'superset'.
    
        All keys in 'subset' must exist in 'superset', and their values
        must be either equal, or if they are dictionaries/lists, they must
        also be subsets/sublists respectively.
        """
        if not isinstance(subset, dict) or not isinstance(superset, dict):
            # If not both dictionaries, they must be strictly equal
            return subset == superset
    
        for key, value in subset.items():
            if key not in superset:
                return False
            superset_value = superset[key]
    
            if isinstance(value, dict) and isinstance(superset_value, dict):
                # Recurse for nested dictionaries
                if not is_subset_dict(value, superset_value):
                    return False
            elif isinstance(value, list) and isinstance(superset_value, list):
                # Special handling for lists (with a specific interpretation)
                if len(value) > len(superset_value):
                    return False
                # Here, we're specifically checking if the *first item*
                # of the 'subset' list is a (deep) subset of the *first item*
                # of the 'superset' list. This is a very specific interpretation
                # and might need adjustment based on your exact list comparison needs.
                # For example, if you want to check if *any* item in 'subset' list
                # is deeply contained within *any* item of 'superset' list,
                # the logic would be more complex (e.g., using 'any' and recursion).
                if value and superset_value:  # make sure lists are not empty
                    if not is_subset_dict(value[0], superset_value[0]):
                        return False
            else:
                # For all other types, values must be strictly equal
                if value != superset_value:
                    return False
        return True

    Let’s see it in action with a superset representing a desired application configuration and a subset representing current settings or a partial update:

    # Our larger, complete configuration
    superset_config = {
        "app_name": "MyAwesomeApp",
        "version": "1.0.0",
        "settings": {
            "debug_mode": True,
            "logging_level": "INFO",
            "features": {"email_notifications": True, "slack_integration": False},
        },
        "users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}],
        "database": {"host": "localhost", "port": 5432},
    }
    
    # A smaller configuration we want to check
    # Does it "fit" within superset_config?
    subset_check_1 = {
        "app_name": "MyAwesomeApp",
        "settings": {  # Nested dictionary check
            "debug_mode": True
        },
        "database": {  # Nested dictionary with fewer keys
            "host": "localhost"
        },
    }
    
    # Another subset for comparison, including the list check
    subset_check_2 = {
        "settings": {
            "features": {  # Deeply nested check
                "email_notifications": True
            }
        },
        "users": [  # List check: first item of subset vs. first item of superset
            {"id": 1}  # 'id' is a subset of the first user dict in superset_config['users']
        ],
    }
    
    # This one will fail because 'environment' key doesn't exist in superset_config
    subset_check_3_fail = {"environment": "production"}
    
    # This one will fail because 'debug_mode' value doesn't match
    subset_check_4_fail = {"settings": {"debug_mode": False}}
    
    # This one will fail because the first user's name is incorrect in the subset's list check
    subset_check_5_fail = {
        "users": [
            {"id": 1, "name": "Eve"}  # 'Eve' != 'Alice'
        ]
    }
    
    print(is_subset_dict(subset_check_1, superset_config))
    # True
    
    print(is_subset_dict(subset_check_2, superset_config))
    # True
    
    print(is_subset_dict(subset_check_3_fail, superset_config))
    # False (key missing)
    
    print(is_subset_dict(subset_check_4_fail, superset_config))
    # False (value mismatch)
    
    print(is_subset_dict(subset_check_5_fail, superset_config))
    # False (nested list item mismatch)
  • TIL: Boosting Your Python Debugging with `ipdb`

    ipdb supercharges Python’s built-in pdb debugger by integrating it with IPython. It offers all pdb’s core features, plus:

    • Syntax Highlighting: Enhanced readability.
    • Tab Completion: Faster command and variable input.
    • Better History: No retyping commands.
    • Automatic Pretty-Printing: Clearer display of complex data.
    • IPython Magic Commands: Direct access to powerful IPython features.

    Essentially, ipdb makes debugging more intuitive, efficient, and visually appealing, moving beyond pdb’s limitations. It’s a fantastic upgrade for any Python project!

    ipdb can be installed like any other python package

    pip install ipdb
    # or with `uv`:
    uv add ipdb

    To use this in python, launch python with the env var PYTHONBREAKPOINT=ipdb.set_trace, so that when the command breakpoint() is encountered, it’ll launch ipdb instead of pdb

    Using ipython, I’ve inserted this block into a startup script in ~/.ipython/profile_default/startup/start.py:

    try:
        import ipdb
        import os
    
        os.environ["PYTHONBREAKPOINT"] = "ipdb.set_trace"
    except ImportError:
        pass

    So that when I’m using ipython in a project with ipdb, ipdb is automatically loaded!

    To use the improved debugger in pytest --pdb, add this line to the [tool.pytest.ini_options] section of the pyproject.toml:

    addopts = "--pdbcls=IPython.terminal.debugger:TerminalPdb"
  • Start simple, iterate to perfection

    Starting with a “dumb” baseline in data science feels embarrassing. It looks like you’re not trying hard enough, or that you don’t know the fancy methods everyone else is using.

    But when building out a new system (outside of creative side-projects or learning exercises), one of the worst things you can do is jump straight to complexity

    When building something reliable, quality matters much more than quantity, and jumping the gun often leads to missed oportuinities and simpler solutions. The cost of this can manifest in tech debt accumulation, or literal compute costs due to poor optimization.

    Instead: start simple. A rough baseline exposes the shape of the problem faster than any polished model: what the data actually behaves like, which metrics move, and where the real bottlenecks live. You don’t discover these truths by crafting the perfect system in isolation.

    The point of the first iteration is orientation. It helps you frame your thinking optmilally for a probelm and establish how you’ll measure progress as you iterate towards perfection.

    Starting simple doesn’t say anything about your competence. It says you’re willing to face reality early instead of hiding behind perfectionism. The people who avoid simple baselines aren’t being rigorous — they’re being afraid.

    If you want high standards, define them with evidence. A quick baseline and a real metric are the fastest way to learn what “good” even is.

  • Navigating Metric Trade-offs in Machine Learning

    When building effective machine learning systems, it’s rarely as straightforward as “pick the model that maximises a metric”. There are multiple aspects of model performance, and thus several metrics, trade-offs and compromises that are unavoidable.

    For example, say we have trained a classifier model and we’re now trying the decide the threshold at which the model predicts positive samples. If we set the threshold too high, we will have poor precision because almost everything would be predicted as negative. Set the threshold too low, and the recall tanks instead because it labels almost everything as positive.

    Like Heisenberg’s uncertainty principle, machine learning models often have good precision, or good recall — but not both.

    As Andrew Ng eloquently points out in Machine Learning Yearning, successful ML often involves a strategic balancing act. His approach suggests to defining:

    • Primary metric:
      • Directly aligns with the core business objective
      • For example, precision, user engagement, etc.
      • Most valuable metrics to maximise
    • Secondary metrics:
      • Other indicators of performance that must be above a given level
      • For example, latency, false positive rate, etc.
      • We want these metrics to stay above a certain threshold
      • But it might be worth accepting a model that maximises the primary metric at the cost of secondary metric performance

    This multi-metric approach ensures our models are performant, as well as responsible and practical.

    Consider a medical diagnostic classifier designed to detect a rare but critical disease. Our primary metric is maximizing recall — ensuring we identify as many true positive cases as possible to avoid missed diagnoses.

    A crucial secondary constraint might be maintaining precision above 0.9, meaning fewer than 10% of our positive predictions are actually false alarms, thereby reducing unnecessary patient anxiety and follow-up procedures. Now, imagine we find that by slightly loosening our precision constraint to 0.85, our recall jumps by an impressive 15%.

    This isn’t a failure; it’s a strategic opportunity. The key is to intentionally and thoughtfully make these compromises, understanding the implications of each shift.

    The goal isn’t to optimize every individual metric to its absolute peak — this is often an impossible task. Instead, we focus on crafting a system that delivers the most value within defined boundaries.