nitpick.generic module

Generic functions and classes.

class nitpick.generic.MergeDict(original_dict: Optional[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(sort=True) Dict[str, Any][source]

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

nitpick.generic.filter_names(iterable: Iterable, *partial_names: str) List[str][source]

Filter names and keep only the desired partial names.

Exclude the project name automatically.

>>> file_list = ['requirements.txt', 'tox.ini', 'setup.py', 'nitpick']
>>> filter_names(file_list)
['requirements.txt', 'tox.ini', 'setup.py']
>>> filter_names(file_list, 'ini', '.py')
['tox.ini', 'setup.py']
>>> mapping = {'requirements.txt': None, 'tox.ini': 1, 'setup.py': 2, 'nitpick': 3}
>>> filter_names(mapping)
['requirements.txt', 'tox.ini', 'setup.py']
>>> filter_names(file_list, 'x')
['requirements.txt', 'tox.ini']
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.

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
>>> is_url("github://andreoliwa/nitpick/styles/black")
True
>>> is_url("github://$VARNAME@andreoliwa/nitpick/styles/black")
True
>>> is_url("github://LitErAl@andreoliwa/nitpick/styles/black")
True
>>> is_url("github://notfirst$@andreoliwa/nitpick/styles/black")
True
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.relative_to_current_dir(path_or_str: Optional[Union[pathlib.Path, str]]) str[source]

Return a relative path to the current dir or an absolute path.

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='.', sort=True) 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: Optional[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.