• API
• FAQ
• Tools
• Archive
daily pastebin goal
41%
SHARE
TWEET

# Untitled

a guest Jan 17th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import logging
2. import difflib
3. import itertools
4. import inspect
5.
6. _logger = logging.getLogger('codediff')
7.
8.
9. class BaseMatcher:
10.     def __init__(self, report1, report2):
11.         self.report1 = report1
12.         self.report2 = report2
13.
14.     def compare(self):
15.         raise NotImplementedError('Subclasses must implement ratio()')
16.
17.
18. class SequenceMatcher(BaseMatcher):
19.     def __init__(self, report1, report2):
20.         super(SequenceMatcher, self).__init__(report1, report2)
21.
22.     def compare(self):
23.         # Currently the reports contents contain every character. In the future, we
24.         # want to calculate the ratio of a fragmented file.
25.         # To calculate the actual ratio, we iterate through the content generator
26.         # and append it to a diff ratio array.
27.         diff_ratios = []
28.         _logger.debug('========== BEGIN `%s::%s::ratio` ==========', __name__, self.__class__.__name__)
29.         _logger.info('\rComparing %s and %s', self.report1, self.report2, extra={'terminator': ''})
30.         # for character_seq, character_seq_2 in zip(self.report1.content, self.report2.content):
31.         character_seq = self.report1.content
32.         character_seq_2 = self.report2.content
33.         seq_match = difflib.SequenceMatcher(lambda x: x in " \t", character_seq, character_seq_2)
34.         diff_ratios.append(seq_match.quick_ratio())
35.
36.         # The next line is where we would determine the actual diff ratio
37.         # if the file was fragmented.
38.         diff_ratio = diff_ratios[0]
39.         _logger.debug('========== END `%s::%s::ratio` ==========', __name__, self.__class__.__name__)
40.         return diff_ratio
41.
42.
43. class SnapMatcher(BaseMatcher):
44.     def __init__(self, report1, report2):
45.         super(SnapMatcher, self).__init__(report1, report2)
46.         self._blacklisted_keys = ['project', 'path']
47.
48.     def compare(self):
49.         diff_ratio = 0.0
50.         # Take the minimium number of elements of the two reports.
51.         total_elems = max(self.report1.elems() - len(self._blacklisted_keys),
52.                           self.report2.elems() - len(self._blacklisted_keys))
53.         inc_rate = 1.0 / total_elems
54.         _logger.debug('Found %d elements; increment rate is %f', total_elems, inc_rate)
55.         _logger.info('\rComparing %s and %s', self.report1, self.report2, extra={'terminator': ''})
56.
57.         # Compare the elements
58.         sentinel = ('\0', '\0')
59.         _logger.debug('REPORT %s, %s', self.report1.name, self.report2.name)
60.         if self._cmp(self.report1.name, self.report2.name):
61.             diff_ratio += inc_rate
62.
63.         for stage_elem1, stage_elem2 in itertools.zip_longest(
64.                 self.report1.getstageelems(),
65.                 self.report2.getstageelems(),
66.                 fillvalue=sentinel):
67.             _logger.debug('STAGE %s %s', stage_elem1, stage_elem2)
68.             if self._cmp(stage_elem1[1], stage_elem2[1]):
69.                 diff_ratio += inc_rate
70.         for sprite_elem1, sprite_elem2 in itertools.zip_longest(
71.                 self.report1.stage.getspriteselems(),
72.                 self.report2.stage.getspriteselems(),
73.                 fillvalue=sentinel):
74.             _logger.debug('SPRITE %s %s', sprite_elem1, sprite_elem2)
75.             if self._cmp(sprite_elem1[1], sprite_elem2[1]):
76.                 diff_ratio += inc_rate
77.         for block_elem1, block_elem2 in itertools.zip_longest(
78.                 self.report1.getblockselems(),
79.                 self.report2.getblockselems(),
80.                 fillvalue=sentinel):
81.             _logger.debug('BLOCK %s %s', block_elem1, block_elem2)
82.             if self._cmp(block_elem1[1], block_elem2[1]):
83.                 diff_ratio += inc_rate
84.         for cvar_elem1, cvar_elem2 in itertools.zip_longest(
85.                 self.report1.getcvarselems(),
86.                 self.report2.getcvarselems(),
87.                 fillvalue=sentinel):
88.             _logger.debug('CUSTOM VAR %s %s', cvar_elem1, cvar_elem2)
89.             if self._cmp(cvar_elem1[1], cvar_elem2[1]):
90.                 diff_ratio += inc_rate
91.
92.         # Divide the number of shared elements by the total.
93.         return diff_ratio
94.
95.     def _similar(self, val1, val2, thresh):
96.         return self._cmp(val1, val2)
97.
98.     def _cmp(self, val1, val2):
99.         return val1 == val2
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top