def filter_mapping_with_filter(mapping, filterer, anchors, flags): """ Filters given mapping with given filterer. Parameters ---------- mapping : dict_like Mapping to filter. filterer : unicode or object Filterer pattern for given mapping elements. anchors : bool, optional Whether to use Regex line anchors, i.e. *^* and *$* are added, surrounding the filterer pattern. flags : int, optional Regex flags. Returns ------- OrderedDict Filtered mapping elements. """ if anchors: filterer = '^{0}$'.format(filterer) filterer = filterer.replace('^^', '^').replace('$$', '$') elements = [ mapping[element] for element in mapping if re.match(filterer, element, flags) ] lookup = Lookup(mapping) return OrderedDict((lookup.first_key_from_value(element), element) for element in elements)
def test_first_key_from_value(self): """ Tests :meth:`colour.utilities.data_structures.\ Lookup.first_key_from_value` method. """ lookup = Lookup(first_name='Doe', last_name='John', gender='male') self.assertEqual('first_name', lookup.first_key_from_value('Doe'))
def test_keys_from_value(self): """ Tests :meth:`colour.utilities.data_structures.Lookup.keys_from_value` method. """ lookup = Lookup(John='Doe', Jane='Doe', Luke='Skywalker') self.assertListEqual(sorted(['Jane', 'John']), sorted(lookup.keys_from_value('Doe')))
def test_first_key_from_value(self): """ Tests :meth:`colour.utilities.data_structures.\ Lookup.first_key_from_value` method. """ lookup = Lookup(first_name='Doe', last_name='John', gender='male') self.assertEqual('first_name', lookup.first_key_from_value('Doe')) lookup = Lookup( A=np.array([0, 1, 2]), B=np.array([1, 2, 3]), C=np.array([2, 3, 4])) self.assertEqual('A', lookup.first_key_from_value(np.array([0, 1, 2])))
def test_raise_exception_first_key_from_value(self): """ Tests :meth:`colour.utilities.data_structures.\ Lookup.first_key_from_value` method raised exception. """ self.assertRaises(IndexError, Lookup().first_key_from_value, 'John')
def test_keys_from_value(self): """ Tests :meth:`colour.utilities.data_structures.Lookup.keys_from_value` method. """ lookup = Lookup(John='Doe', Jane='Doe', Luke='Skywalker') self.assertListEqual( sorted(['Jane', 'John']), sorted(lookup.keys_from_value('Doe'))) lookup = Lookup( A=np.array([0, 1, 2]), B=np.array([0, 1, 2]), C=np.array([1, 2, 3])) self.assertListEqual( sorted(['A', 'B']), lookup.keys_from_value(np.array([0, 1, 2])))
def filter_mapping_with_filter( mapping: Mapping, filterer: str, anchors: Boolean = True, flags: Union[Integer, RegexFlag] = re.IGNORECASE, ) -> Dict: """ Filter given mapping with given filterer. Parameters ---------- mapping Mapping to filter. filterer Filterer pattern for given mapping elements. anchors Whether to use Regex line anchors, i.e. *^* and *$* are added, surrounding the filterer pattern. flags Regex flags. Returns ------- dict Filtered mapping elements. """ if anchors: filterer = f"^{filterer}$" filterer = filterer.replace("^^", "^").replace("$$", "$") elements = [ mapping[element] for element in mapping if re.match(filterer, element, flags) ] lookup = Lookup(mapping) return { lookup.first_key_from_value(element): element for element in elements }
def test_first_key_from_value(self): """ Tests :meth:`colour.utilities.data_structures.\ Lookup.first_key_from_value` method. """ lookup = Lookup(first_name='John', last_name='Doe', gender='male') self.assertEqual('first_name', lookup.first_key_from_value('John')) lookup = Lookup(A=np.array([0, 1, 2]), B=np.array([1, 2, 3]), C=np.array([2, 3, 4])) self.assertEqual('A', lookup.first_key_from_value(np.array([0, 1, 2])))
def test_first_key_from_value(self): """ Test :meth:`colour.utilities.data_structures.\ Lookup.first_key_from_value` method. """ lookup = Lookup(first_name="John", last_name="Doe", gender="male") self.assertEqual("first_name", lookup.first_key_from_value("John")) lookup = Lookup(A=np.array([0, 1, 2]), B=np.array([1, 2, 3]), C=np.array([2, 3, 4])) self.assertEqual("A", lookup.first_key_from_value(np.array([0, 1, 2])))
def test_keys_from_value(self): """ Tests :meth:`colour.utilities.data_structures.Lookup.keys_from_value` method. """ lookup = Lookup(John='Doe', Jane='Doe', Luke='Skywalker') self.assertListEqual(sorted(['Jane', 'John']), sorted(lookup.keys_from_value('Doe'))) lookup = Lookup(A=np.array([0, 1, 2]), B=np.array([0, 1, 2]), C=np.array([1, 2, 3])) self.assertListEqual(sorted(['A', 'B']), lookup.keys_from_value(np.array([0, 1, 2])))
def test_keys_from_value(self): """ Test :meth:`colour.utilities.data_structures.Lookup.keys_from_value` method. """ lookup = Lookup(John="Doe", Jane="Doe", Luke="Skywalker") self.assertListEqual(["Jane", "John"], sorted(lookup.keys_from_value("Doe"))) lookup = Lookup(A=np.array([0, 1, 2]), B=np.array([0, 1, 2]), C=np.array([1, 2, 3])) self.assertListEqual(["A", "B"], sorted(lookup.keys_from_value(np.array([0, 1, 2]))))