granite.utils module

Utility for internal library use.

cached_property(fn)[source]

Converts a class’s method into property and cache’s the getter value.

Simple decorator a method with this function and it will be converted into a property (attribute access) and the calculation to retrieve the value will be cached so that the initial call performs the calculation, but subsequent calls will use the cache.

get_mock_patcher_types()[source]

Gets the Classes of the mock.patcher functions.

We’re using this for the automatic mocking mixin in order to determine if a class-level attribute is a patcher instance.

Returns:a unique list of the patcher types used by mock.
Return type:tuple
path_as_key(path, relative_to=None)[source]

Converts a path to a unique key.

Paths can take on several unique forms but all describe the same node on the file system. This function will take a path and produce a key that is unique such that several paths that point to the same node on the file system will all be converted to the same path.

This is useful for converting file paths to keys for a dictionary.

Note: all paths will be separated by forward slashes.

Parameters:
  • path (str) – the path to convert.
  • relative_to (str) – make the key relative to this path
Returns:

the unique key from the path

Return type:

str

Examples:

>>> path_as_key('./file.txt') == path_as_key('file.txt')
True