Source code for pyqubes.utils

#!/usr/bin/env python
'''
Utility functions for pyqubes

Utilities have no dependencies.
'''

import logging

[docs]def object_fullname(obj): '''Returns the full absolute name of the object provided''' fullname = obj.__module__ + "." + obj.__class__.__name__ return fullname
[docs]def object_logger(obj): ''' Returns a correctly named logger for the given object. Call as self.logger = object_logger(self) ''' fullname = object_fullname(obj) logger = logging.getLogger(fullname) return logger
[docs]def flatten_list(nested_list): ''' Flatten a nested list one level. :param list nested_list: A nested list :rtype: list ''' flat_list = [item for sublist in nested_list for item in sublist] return flat_list
[docs]def assert_list_items_equal_in_nested(actual_nested, expected_list): ''' Assert that the given ``expected_list`` matches one pf the lists within ``actual_nested``, using the comparison ``sorted(list)`` :param list actual_nested: A list of lists (usually generated by test) :param list expected_list: A list of expected values ''' expected_list_sorted = sorted(expected_list) equal_list = [expected_list_sorted == sorted(actual_list) for actual_list in actual_nested] if True not in equal_list: error_message = 'List not found in nested list.\nList: {0}\nNested list: {1}'.format(expected_list, actual_nested) raise AssertionError(error_message)