liveBook · Manning

Don't-care value

As explained in Software Design for Python Programmers

A don't-care attribute is a search attribute whose value is unspecified and therefore imposes no matching constraint.

A don't-care value leaves a search attribute unspecified, so that attribute imposes no matching constraint. In a book catalogue, omitting an attribute from the target makes it match any corresponding value in the catalogue. section 2.3.4 section 3.8

A don't-care search works by narrowing only on supplied constraints:

  • The target may provide any number of desired attribute values. section 3.8
  • The catalogue checks every key–value pair present in the target. section 2.3.4
  • A missing target key is not matched, allowing any catalogue value for that attribute. section 2.3.4
  • Supplied string values still compare case-insensitively, while typed values retain their required datatypes. section 2.3.4

Search by kind, author, and genre

A target containing KIND: fiction, LAST: "KING", and GENRE: horror leaves title, first name, year, and other omitted attributes as don't-care values. The search returns both Carrie and It: A Novel because their supplied target values match.

Listing 2.19 (Program 2.4 Books-4): attributes.py (part 2 of 3)
    ...
    def _is_matching_key_value(self, target_key, target_value):
        if target_key not in self._dictionary.keys(): return False    #1
        if self._dictionary[target_key] == target_value: return True   #2

        if isinstance(target_value, str):    #3
            return Attributes._equal_ignore_case(
                self._dictionary[target_key], target_value)

        return False

    def is_match(self, target_attrs):
        for target_key, target_value \
                            in target_attrs._dictionary.items():
            if not self._is_matching_key_value(target_key, 
                                               target_value):
                return False

        return True
    ...

Inspect the matching loop and see how iterating over target attributes makes omitted attributes don't-care values. section 2.3.4

How Software Design for Python Programmers covers it

How deep it goes
Implements target-driven matching and omits unspecified attributes, while deferring explicit wildcard representation.
Examples
Uses partial searches for Stephen King horror fiction and Chinese cookbooks.
What it assumes
Assumes typed book attributes and dictionary-based catalogue searches, while establishing unspecified attributes as unconstrained.
What you can do after
Apply partial target dictionaries to search a catalogue without inventing placeholder enumeration values.
How it connects
Connects search attributes with wildcard matching, case-insensitive string comparison, enumerations, and defensive programming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.