Anonymous avatar Anonymous committed bc6cb0b

Renamed confusing Stats/StatCollection classes

Comments (0)

Files changed (4)

 
         self.score = 1
         self._in_conditional = False
-        self.stats = StatsCollection()
+        self.results = ComplexityResults()
         for child in node.getChildNodes():
             compiler.walk(child, self, walker=self)
 
         if in_module:
             end_line = max(1, code_or_node.count('\n') + 1)
-            self.stats.add(Stats(name='<module>',
-                                 type_='module',
-                                 score=self.score,
-                                 start_line=1,
-                                 end_line=end_line))
+            self.results.add(ComplexityScore(name='<module>',
+                                             type_='module',
+                                             score=self.score,
+                                             start_line=1,
+                                             end_line=end_line))
 
     def dispatch_children(self, node):
         for child in node.getChildNodes():
 
     def visitFunction(self, node):
         score=Complexity(node).score
-        stats = Stats(name=node.name,
-                      type_='function',
-                      score=score,
-                      start_line=node.lineno,
-                      end_line=self.highest_line_in_node(node))
-        self.stats.add(stats)
+        score = ComplexityScore(name=node.name,
+                                type_='function',
+                                score=score,
+                                start_line=node.lineno,
+                                end_line=self.highest_line_in_node(node))
+        self.results.add(score)
 
     def visitClass(self, node):
         complexity = Complexity(node)
-        self.stats.add(Stats(name=node.name,
-                             type_='class',
-                             score=complexity.score,
-                             start_line=node.lineno,
-                             end_line=self.highest_line_in_node(node)))
-        for stats_instance in complexity.stats.ordered_by_line():
-            stats_instance.name = '%s.%s' % (node.name, stats_instance.name)
-            self.stats.add(stats_instance)
+        self.results.add(ComplexityScore(
+            name=node.name,
+            type_='class',
+            score=complexity.score,
+            start_line=node.lineno,
+            end_line=self.highest_line_in_node(node)))
+        for score in complexity.results.ordered_by_line():
+            score.name = '%s.%s' % (node.name, score.name)
+            self.results.add(score)
 
     def highest_line_in_node(self, node, highest=0):
         children = node.getChildNodes()
         self.score += len(node.handlers)
 
 
-class StatsCollection:
+class ComplexityResults:
     def __init__(self):
-        self._stats = []
+        self._scores = []
 
-    def add(self, stats):
-        self._stats.append(stats)
+    def add(self, score):
+        self._scores.append(score)
 
     def ordered_by_line(self):
         OBJECT_SORT_PRIORITY = ['module', 'function', 'class']
-        def sort_key(stats):
-            return (stats.start_line,
-                    OBJECT_SORT_PRIORITY.index(stats.type_))
-        return sorted(self._stats, key=sort_key)
+        def sort_key(score):
+            return (score.start_line,
+                    OBJECT_SORT_PRIORITY.index(score.type_))
+        return sorted(self._scores, key=sort_key)
 
     def named(self, name):
-        return [s for s in self._stats if s.name == name][0]
+        return [s for s in self._scores if s.name == name][0]
 
 
-class Stats:
+class ComplexityScore:
     def __init__(self, name, type_, score, start_line, end_line):
         self.name = name
         self.type_ = type_
         self.end_line = end_line
 
     def __repr__(self):
-        return 'Stats(name=%s, score=%s, start_line=%s, end_line=%s)' % (
-            repr(self.name),
-            repr(self.score),
-            repr(self.start_line),
-            repr(self.end_line))
+        return (
+            'ComplexityScore(name=%s, score=%s, start_line=%s, end_line=%s)'
+            % (repr(self.name),
+               repr(self.score),
+               repr(self.start_line),
+               repr(self.end_line)))
 
 
 def complexity_name(complexity):
 def show_complexity():
     current_file = get_current_file_name()
     try:
-        stats = compute_stats_for(current_file)
+        scores = compute_scores_for(current_file)
     except (IndentationError, SyntaxError):
         return
 
     old_complexities = get_old_complexities(current_file)
-    new_complexities = compute_new_complexities(stats)
+    new_complexities = compute_new_complexities(scores)
     line_changes = compute_line_changes(old_complexities, new_complexities)
     update_line_markers(line_changes)
 
     return vim.eval('expand("%:p")')
 
 
-def compute_stats_for(filename):
+def compute_scores_for(filename):
     code = open(filename).read()
-    stats = compute_code_complexity(code).stats.ordered_by_line()
-    return stats
+    scores = compute_code_complexity(code).results.ordered_by_line()
+    return scores
 
 
 def get_old_complexities(current_file):
     return changes
 
 
-def compute_new_complexities(stats):
+def compute_new_complexities(scores):
     new_scores = {}
-    for stat in stats:
-        for line in range(stat.start_line, stat.end_line + 1):
-            new_scores[line] = complexity_name(stat.score)
+    for score in scores:
+        for line in range(score.start_line, score.end_line + 1):
+            new_scores[line] = complexity_name(score.score)
     return new_scores
 
 
 from compiler.visitor import ASTVisitor
 
 
+def compute_code_complexity(code):
+    return Complexity(code)
+
+
 class Complexity(ASTVisitor):
     def __init__(self, code_or_node):
         ASTVisitor.__init__(self)
 
         self.score = 1
         self._in_conditional = False
-        self.stats = StatsCollection()
+        self.results = ComplexityResults()
         for child in node.getChildNodes():
             compiler.walk(child, self, walker=self)
 
         if in_module:
             end_line = max(1, code_or_node.count('\n') + 1)
-            self.stats.add(Stats(name='<module>',
-                                 type_='module',
-                                 score=self.score,
-                                 start_line=1,
-                                 end_line=end_line))
+            self.results.add(ComplexityScore(name='<module>',
+                                             type_='module',
+                                             score=self.score,
+                                             start_line=1,
+                                             end_line=end_line))
 
-    def dispatchChildren(self, node):
+    def dispatch_children(self, node):
         for child in node.getChildNodes():
             self.dispatch(child)
 
     def visitFunction(self, node):
         score=Complexity(node).score
-        stats = Stats(name=node.name,
-                      type_='function',
-                      score=score,
-                      start_line=node.lineno,
-                      end_line=self.highest_line_in_node(node))
-        self.stats.add(stats)
+        score = ComplexityScore(name=node.name,
+                                type_='function',
+                                score=score,
+                                start_line=node.lineno,
+                                end_line=self.highest_line_in_node(node))
+        self.results.add(score)
 
     def visitClass(self, node):
         complexity = Complexity(node)
-        self.stats.add(Stats(name=node.name,
-                             type_='class',
-                             score=complexity.score,
-                             start_line=node.lineno,
-                             end_line=self.highest_line_in_node(node)))
-        for stats_instance in complexity.stats.ordered_by_line():
-            stats_instance.name = '%s.%s' % (node.name, stats_instance.name)
-            self.stats.add(stats_instance)
+        self.results.add(ComplexityScore(
+            name=node.name,
+            type_='class',
+            score=complexity.score,
+            start_line=node.lineno,
+            end_line=self.highest_line_in_node(node)))
+        for score in complexity.results.ordered_by_line():
+            score.name = '%s.%s' % (node.name, score.name)
+            self.results.add(score)
 
     def highest_line_in_node(self, node, highest=0):
         children = node.getChildNodes()
         for test in tests:
             self.dispatch(test)
         self._in_conditional = False
-        self.dispatchChildren(node)
+        self.dispatch_children(node)
 
     def _tests_for_if(self, if_node):
         try:
 
     def __processDecisionPoint(self, node):
         self.score += 1
-        self.dispatchChildren(node)
+        self.dispatch_children(node)
 
     visitFor = visitGenExprFor \
             = visitListCompFor \
             = visitWhile = __processDecisionPoint
 
     def _visit_logical_operator(self, node):
-        self.dispatchChildren(node)
+        self.dispatch_children(node)
         if self._in_conditional:
             self.score += len(node.getChildren()) - 1
 
     visitOr = _visit_logical_operator
 
     def visitTryExcept(self, node):
-        self.dispatchChildren(node)
+        self.dispatch_children(node)
         self.score += len(node.handlers)
 
 
-class StatsCollection:
+class ComplexityResults:
     def __init__(self):
-        self._stats = []
+        self._scores = []
 
-    def add(self, stats):
-        self._stats.append(stats)
+    def add(self, score):
+        self._scores.append(score)
 
     def ordered_by_line(self):
         OBJECT_SORT_PRIORITY = ['module', 'function', 'class']
-        def sort_key(stats):
-            return (stats.start_line,
-                    OBJECT_SORT_PRIORITY.index(stats.type_))
-        return sorted(self._stats, key=sort_key)
+        def sort_key(score):
+            return (score.start_line,
+                    OBJECT_SORT_PRIORITY.index(score.type_))
+        return sorted(self._scores, key=sort_key)
 
     def named(self, name):
-        return [s for s in self._stats if s.name == name][0]
+        return [s for s in self._scores if s.name == name][0]
 
 
-class Stats:
+class ComplexityScore:
     def __init__(self, name, type_, score, start_line, end_line):
         self.name = name
         self.type_ = type_
         self.end_line = end_line
 
     def __repr__(self):
-        return 'Stats(name=%s, score=%s, start_line=%s, end_line=%s)' % (
-            repr(self.name),
-            repr(self.score),
-            repr(self.start_line),
-            repr(self.end_line))
+        return (
+            'ComplexityScore(name=%s, score=%s, start_line=%s, end_line=%s)'
+            % (repr(self.name),
+               repr(self.score),
+               repr(self.start_line),
+               repr(self.end_line)))
 
 
 def complexity_name(complexity):
 def show_complexity():
     current_file = get_current_file_name()
     try:
-        stats = compute_stats_for(current_file)
+        scores = compute_scores_for(current_file)
     except (IndentationError, SyntaxError):
         return
 
     old_complexities = get_old_complexities(current_file)
-    new_complexities = compute_new_complexities(stats)
+    new_complexities = compute_new_complexities(scores)
     line_changes = compute_line_changes(old_complexities, new_complexities)
     update_line_markers(line_changes)
 
     return vim.eval('expand("%:p")')
 
 
-def compute_stats_for(filename):
+def compute_scores_for(filename):
     code = open(filename).read()
-    stats = Complexity(code).stats.ordered_by_line()
-    return stats
+    scores = compute_code_complexity(code).results.ordered_by_line()
+    return scores
 
 
 def get_old_complexities(current_file):
     return changes
 
 
-def compute_new_complexities(stats):
+def compute_new_complexities(scores):
     new_scores = {}
-    for stat in stats:
-        for line in range(stat.start_line, stat.end_line + 1):
-            new_scores[line] = complexity_name(stat.score)
+    for score in scores:
+        for line in range(score.start_line, score.end_line + 1):
+            new_scores[line] = complexity_name(score.score)
     return new_scores
 
 

tests/test_integration.py

             def foo():
                 x = lambda: x if x else x
                 y if y else y
-            """).stats.named('foo').score == 3
+            """).results.named('foo').score == 3
 
     def test_a_big_hairy_mess(self):
         assert complexity(
             """).score == 15
 
     def test_module_stat_comes_before_function_stat(self):
-        stats = complexity("def foo(): pass\npass").stats
+        stats = complexity("def foo(): pass\npass").results
         stat_names = [stat.name for stat in stats.ordered_by_line()]
         assert stat_names == ['<module>', 'foo']
 
     def test_class_stat_comes_before_module_stat(self):
-        stats = complexity("class Foo: pass\npass").stats
+        stats = complexity("class Foo: pass\npass").results
         stat_names = [stat.name for stat in stats.ordered_by_line()]
         assert stat_names == ['<module>', 'Foo']
 

tests/test_scoped_objects.py

         assert complexity(
             """
             a if a else a
-            """).stats.named('<module>').score == 2
+            """).results.named('<module>').score == 2
         assert complexity(
             """
                 0 if x else 1 if y else 2
-            """).stats.named('<module>').score == 3
+            """).results.named('<module>').score == 3
 
     def test_that_they_know_their_names(self):
-        assert complexity("").stats.named('<module>').name == '<module>'
+        assert complexity("").results.named('<module>').name == '<module>'
 
     def test_that_they_know_their_line_range(self):
-        stats = complexity("").stats.named('<module>')
+        stats = complexity("").results.named('<module>')
         assert stats.start_line == 1
         assert stats.end_line == 1
 
         stats = complexity(
             """
             a
-            """).stats.named('<module>')
+            """).results.named('<module>')
         print '-%s-' % (
             """
             a
             def foo():
                 a if a else a
             a if a else a
-            """).stats.named('<module>').score == 3
+            """).results.named('<module>').score == 3
 
 
 class describe_functions:
             """
             def foo():
                 0 if x else 1
-            """).stats.named('foo').score == 2
+            """).results.named('foo').score == 2
         assert complexity(
             """
             def foo():
                 0 if x else 1 if y else 2
-            """).stats.named('foo').score == 3
+            """).results.named('foo').score == 3
 
     def test_that_they_know_their_names(self):
         assert complexity(
             """
             def foo(): pass
-            """).stats.named('foo').name == 'foo'
+            """).results.named('foo').name == 'foo'
 
     def test_that_they_know_their_line_range(self):
-        stats = complexity("def foo(): pass").stats.named('foo')
+        stats = complexity("def foo(): pass").results.named('foo')
         assert stats.start_line == 1
         assert stats.end_line == 1
 
         stats = complexity(
             """
             def foo(): pass
-            """).stats.named('foo')
+            """).results.named('foo')
         assert stats.start_line == 2
         assert stats.end_line == 2
 
             """
             class Foo:
                 0 if x else 1
-            """).stats.named('Foo').score == 2
+            """).results.named('Foo').score == 2
         assert complexity(
             """
             class Foo:
                 0 if x else 1 if y else 2
-            """).stats.named('Foo').score == 3
+            """).results.named('Foo').score == 3
 
     def test_that_they_know_their_names(self):
         assert complexity(
             """
             class Foo: pass
-            """).stats.named('Foo').name == 'Foo'
+            """).results.named('Foo').name == 'Foo'
 
     def test_that_they_know_their_line_range(self):
-        stats = complexity("class Foo: pass").stats.named('Foo')
+        stats = complexity("class Foo: pass").results.named('Foo')
         assert stats.start_line == 1
         assert stats.end_line == 1
 
             """
             class Foo:
                 pass
-            """).stats.named('Foo')
+            """).results.named('Foo')
         assert stats.start_line == 2
         assert stats.end_line == 3
 
                 0 if x else 1
                 def foo(self): pass
                 0 if x else 1
-            """).stats.named('Foo')
+            """).results.named('Foo')
         assert stats.score == 3
         assert stats.end_line == 5
 
             class Foo:
                 def foo():
                     0 if x else 1
-            """).stats.named('Foo.foo').score == 2
+            """).results.named('Foo.foo').score == 2
         assert complexity(
             """
             class Foo:
                 def foo():
                     0 if x else 1 if y else 2
-            """).stats.named('Foo.foo').score == 3
+            """).results.named('Foo.foo').score == 3
 
     def test_that_they_know_their_names(self):
         assert complexity(
             """
             class Foo:
                 def foo(): pass
-            """).stats.named('Foo.foo').name == 'Foo.foo'
+            """).results.named('Foo.foo').name == 'Foo.foo'
 
     def test_that_they_know_their_line_range(self):
         stats = complexity(
             class Foo():
                 def foo():
                     pass
-            """).stats.named('Foo.foo')
+            """).results.named('Foo.foo')
         assert stats.start_line == 3
         assert stats.end_line == 4
 
                 def foo():
                     pass
                     pass
-            """).stats.named('Foo.foo')
+            """).results.named('Foo.foo')
         assert stats.start_line == 4
         assert stats.end_line == 6
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.