Merge pull request #21 from Mathieu4141/faster-fast-search

[fast search] use set to have faster search
pull/22/head
Alexandre Dulaunoy 2023-06-01 09:06:14 +02:00 committed by GitHub
commit 4d2e31cd4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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):
self.warninglist = warninglist
self.list = self.warninglist['list']
self.set = set(self.list)
self.description = self.warninglist['description']
self.version = int(self.warninglist['version'])
self.name = self.warninglist['name']
@ -71,7 +72,7 @@ class WarningList():
return json.dumps(self, default=json_default)
def _fast_search(self, value) -> bool:
return value in self.list
return value in self.set
def _network_index(self) -> List:
to_return = []

View File

@ -39,7 +39,7 @@ class TestPyMISPWarningLists(unittest.TestCase):
def test_slow_search(self):
self.warninglists = WarningLists(True)
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')
self.assertEqual(results[0].name, 'List of RFC 6598 CIDR blocks')
results = self.warninglists.search('2001:DB8::34:1')