nitpick.generic module

Generic functions and classes.

class nitpick.generic.MergeDict(original_dict: Dict[str, Any] = None)[source]

Bases: object

A dictionary that can merge other dictionaries into it.

add(other: Dict[str, Any])None[source]

Add another dictionary to the existing data.

merge() → Dict[str, Any][source]

Merge the dictionaries, replacing values with identical keys and extending lists.

nitpick.generic.climb_directory_tree(starting_path: Union[pathlib.Path, str], file_patterns: Iterable[str]) → Optional[Set[pathlib.Path]][source]

Climb the directory tree looking for file patterns.

nitpick.generic.find_object_by_key(list_: List[dict], search_key: str, search_value: Any)dict[source]

Find an object in a list, using a key/value pair to search.

>>> fruits = [{"id": 1, "fruit": "banana"}, {"id": 2, "fruit": "apple"}, {"id": 3, "fruit": "mango"}]
>>> find_object_by_key(fruits, "id", 1) == {'id': 1, 'fruit': 'banana'}
True
>>> find_object_by_key(fruits, "fruit", "banana") == {'id': 1, 'fruit': 'banana'}
True
>>> find_object_by_key(fruits, "fruit", "pear")
{}
>>> find_object_by_key(fruits, "fruit", "mango") == {'id': 3, 'fruit': 'mango'}
True
>>> find_object_by_key(None, "fruit", "pear")
{}
nitpick.generic.flatten(dict_, parent_key='', separator='.', current_lists=None) → Dict[str, Any][source]

Flatten a nested dict.

Use unflatten() to revert.

Adapted from this StackOverflow question.

>>> expected = {'root.sub1': 1, 'root.sub2.deep': 3, 'sibling': False}
>>> flatten({"root": {"sub1": 1, "sub2": {"deep": 3}}, "sibling": False}) == expected
True
>>> expected = {'parent."with.dot".again': True, 'parent."my.my"': 1, "parent.123": "numeric-key"}
>>> flatten({"parent": {"with.dot": {"again": True}, "my.my": 1, 123: "numeric-key"}}) == expected
True
nitpick.generic.get_subclasses(cls)[source]

Recursively get subclasses of a parent class.

nitpick.generic.is_url(url: str)bool[source]

Return True if a string is a URL.

>>> is_url("")
False
>>> is_url("  ")
False
>>> is_url("http://example.com")
True
nitpick.generic.pretty_exception(err: Exception, message: str)[source]

Return a pretty error message with the full path of the Exception.

nitpick.generic.quoted_split(string_: str, separator='.') → List[str][source]

Split a string by a separator, but considering quoted parts (single or double quotes).

>>> quoted_split("my.key.without.quotes")
['my', 'key', 'without', 'quotes']
>>> quoted_split('"double.quoted.string"')
['double.quoted.string']
>>> quoted_split('"double.quoted.string".and.after')
['double.quoted.string', 'and', 'after']
>>> quoted_split('something.before."double.quoted.string"')
['something', 'before', 'double.quoted.string']
>>> quoted_split("'single.quoted.string'")
['single.quoted.string']
>>> quoted_split("'single.quoted.string'.and.after")
['single.quoted.string', 'and', 'after']
>>> quoted_split("something.before.'single.quoted.string'")
['something', 'before', 'single.quoted.string']
nitpick.generic.search_dict(jmespath_expression: Union[jmespath.parser.ParsedResult, str], data: MutableMapping[str, Any], default: Any) → Any[source]

Search a dictionary using a JMESPath expression, and returning a default value.

>>> data = {"root": {"app": [1, 2], "test": "something"}}
>>> search_dict("root.app", data, None)
[1, 2]
>>> search_dict("root.test", data, None)
'something'
>>> search_dict("root.unknown", data, "")
''
>>> search_dict("root.unknown", data, None)
>>> search_dict(jmespath.compile("root.app"), data, [])
[1, 2]
>>> search_dict(jmespath.compile("root.whatever"), data, "xxx")
'xxx'
Parameters
  • jmespath_expression – A compiled JMESPath expression or a string with an expression.

  • data – The dictionary to be searched.

  • default – Default value in case nothing is found.

Returns

The object that was found or the default value.

nitpick.generic.unflatten(dict_, separator='.')collections.OrderedDict[source]

Turn back a flattened dict created by flatten() into a nested dict.

>>> expected = {'my': {'sub': {'path': True}, 'home': 4}, 'another': {'path': 3}}
>>> unflatten({"my.sub.path": True, "another.path": 3, "my.home": 4}) == expected
True
>>> unflatten({"repo": "conflicted key", "repo.name": "?", "repo.path": "?"})
Traceback (most recent call last):
  ...
TypeError: 'str' object does not support item assignment
nitpick.generic.version_to_tuple(version: str = None) → Tuple[int, …][source]

Transform a version number into a tuple of integers, for comparison.

>>> version_to_tuple("")
()
>>> version_to_tuple("  ")
()
>>> version_to_tuple(None)
()
>>> version_to_tuple("1.0.1")
(1, 0, 1)
>>> version_to_tuple(" 0.2 ")
(0, 2)
>>> version_to_tuple(" 2 ")
(2,)
Parameters

version – String with the version number. It must be integers split by dots.

Returns

Tuple with the version number.