Skip to content

Commit

Permalink
Implemented a unique function for matches
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrim authored and Vrim committed Oct 1, 2020
1 parent 9b2fee2 commit 18dee8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pastepwn/analyzers/basicanalyzer.py
Expand Up @@ -34,13 +34,24 @@ def add_action(self, action):
self._check_action(action)
self.actions.append(action)

def match(self, paste):
def match(self, paste, unique_matches=False):
"""
Checks if a certain paste is matched by the conditions set for this analyzer
:param paste: A :class:`pastepwn.core.paste` object which should be matched
:param unique_matches: A boolean that specifies if matches should be unique.
defaults to False
:return: :obj:`bool` if the paste has been matched
"""
raise NotImplementedError("Your analyzer must implement the match method!")

@staticmethod
def unique(matches):
"""
Takes a list of matches and returns a list with no duplicates.
:param matches: A list of matches
:return: A filtered list of matches retaining order
"""
return sorted(set(matches), key=matches.index)

def _check_action(self, action):
"""Check if a passed action is a subclass of BasicAction"""
Expand Down
21 changes: 21 additions & 0 deletions pastepwn/analyzers/tests/basicanalyzer_test.py
Expand Up @@ -99,6 +99,27 @@ def test_error_logging_init_class(self):

self.assertEqual(log.output, ["ERROR:pastepwn.analyzers.basicanalyzer:You passed a class as action for 'BasicAnalyzer' but an instance of an action was expected!"])

def test_unique(self):
"""Check if running unique() on a list of matches returns a list with
no duplicates"""
# Some lists with and without duplicates
test_lists = [
[],
["a", "a"],
["a", "b"],
["a", "b", "a"]
]
self.assertEqual(BasicAnalyzer.unique(test_lists[0]), [],
msg="BasicAnalyzer.unique() left a duplicate!")
self.assertEqual(BasicAnalyzer.unique(test_lists[1]), ["a"],
msg="BasicAnalyzer.unique() left a duplicate!")
# Should preserve order
self.assertEqual(BasicAnalyzer.unique(test_lists[2]), ["a", "b"],
msg="BasicAnalyzer.unique() left a duplicate!")
self.assertEqual(BasicAnalyzer.unique(test_lists[3]), ["a", "b"],
msg="BasicAnalyzer.unique() left a duplicate!")



if __name__ == '__main__':
unittest.main()

0 comments on commit 18dee8a

Please sign in to comment.