[fast search] use set to have faster search

pull/21/head
Mathieu Beligon 2023-05-31 15:49:47 -04:00
parent 3226638040
commit 4f19cc1934
2 changed files with 3 additions and 2 deletions

View File

@ -33,6 +33,7 @@ class WarningList():
def __init__(self, warninglist: Dict[str, Any], slow_search: bool=False): def __init__(self, warninglist: Dict[str, Any], slow_search: bool=False):
self.warninglist = warninglist self.warninglist = warninglist
self.list = self.warninglist['list'] self.list = self.warninglist['list']
self.set = set(self.list)
self.description = self.warninglist['description'] self.description = self.warninglist['description']
self.version = int(self.warninglist['version']) self.version = int(self.warninglist['version'])
self.name = self.warninglist['name'] self.name = self.warninglist['name']
@ -71,7 +72,7 @@ class WarningList():
return json.dumps(self, default=json_default) return json.dumps(self, default=json_default)
def _fast_search(self, value) -> bool: def _fast_search(self, value) -> bool:
return value in self.list return value in self.set
def _network_index(self) -> List: def _network_index(self) -> List:
to_return = [] to_return = []

View File

@ -39,7 +39,7 @@ class TestPyMISPWarningLists(unittest.TestCase):
def test_slow_search(self): def test_slow_search(self):
self.warninglists = WarningLists(True) self.warninglists = WarningLists(True)
results = self.warninglists.search('8.8.8.8') results = self.warninglists.search('8.8.8.8')
self.assertEqual(results[0].name, 'List of known IPv4 public DNS resolvers') self.assertIn('List of known IPv4 public DNS resolvers', [r.name for r in results])
results = self.warninglists.search('100.64.1.56') results = self.warninglists.search('100.64.1.56')
self.assertEqual(results[0].name, 'List of RFC 6598 CIDR blocks') self.assertEqual(results[0].name, 'List of RFC 6598 CIDR blocks')
results = self.warninglists.search('2001:DB8::34:1') results = self.warninglists.search('2001:DB8::34:1')