def getLookup(self, attrName: str, withDuplicates: bool = False): ''' create a lookup dictionary by the given attribute name Args: attrName(str): the attribute to lookup withDuplicates(bool): whether to retain single values or lists Return: a dictionary for lookup or a tuple dictionary,list of duplicates depending on withDuplicates ''' return LOD.getLookup(self.getList(), attrName, withDuplicates)
def testGetLookupIssue31And32(self): ''' test for https://github.com/WolfgangFahl/pyLoDStorage/issues/31 test for https://github.com/WolfgangFahl/pyLoDStorage/issues/32 ''' lod = [ { "name": "Athens", "Q": 1524}, { "name": "Paris", "Q": 90}, { "name": ["München", "Munich"], "Q": 1726}, { "name": "Athens", "Q": 1524}, ] cityMap,duplicates = LOD.getLookup(lod, "name") if self.debug: print(cityMap) self.assertEqual(1,len(duplicates)) self.assertEqual(4,len(cityMap)) self.assertEqual(cityMap["München"],cityMap["Munich"])
def checkHandleListTypeResult(self,lod,expectedLen,expected): ''' check the result of the handleListType function Args: lod(list): the list of dicts to check expectedLen(int): the expected Length expected(str): the expected entry for the München,Munich Q1524 record with a list ''' if self.debug: print(lod) self.assertEqual(expectedLen,len(lod)) cityByQ,_duplicates=LOD.getLookup(lod, "Q") if self.debug: print(cityByQ) if expected is not None: munichRecord=cityByQ[1726] self.assertEqual(expected,munichRecord["name"]) else: self.assertFalse(1726 in cityByQ)