timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Статичен анализ на python код от Виктор Драганов

Обратно към всички решения

Към профила на Виктор Драганов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 3 успешни тест(а)
  • 8 неуспешни тест(а)

Код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import ast
import re


def set_rules(rules):
    if rules.get('line_length') is None:
        rules['line_length'] = 79
    if rules.get('forbid_semicolons') is None:
        rules['forbid_semicolons'] = True
    if rules.get('indentation_size') is None:
        rules['indentation_size'] = 4
    if rules.get('forbid_trailing_whitespace') is None:
        rules['forbid_trailing_whitespace'] = True


def check_lines(code, issues, rules):
    lines = code.split('\n')
    for i in range(1, len(lines) + 1):
        issues[i] = []
        line_length = len(lines[i - 1])
        if(line_length > rules['line_length']):
            issues[i].append('line too long ({0} > {1})'.format(
                line_length, rules['line_length']))
        semicolumn_m = re.search("(;)", lines[i - 1])
        if (semicolumn_m):
            issues[i].append('multiple expressions on the same line')


def critic(code, **rules):
    issues = dict()
    set_rules(rules)
    check_lines(code, issues, rules)

    tree = ast.parse(code)
    line_counter = 1
    for node in ast.walk(tree):
        if (isinstance(node, ast.ClassDef) and
                rules['methods_per_class'] is not None):
            methods = check_classdef_methods(node, rules['methods_per_class'])
            if methods is not None:
                issue = 'too many methods in class({0} > {1})'.format(
                    methods, rules['methods_per_class'])
                issues[line_counter] = issue
        line_counter += 1
    return issues


def check_classdef_methods(classdef, number):
    methods = 0
    for node in ast.walk(classdef):
        if isinstance(node, ast.FunctionDef):
            methods += 1
    if methods <= number:
        return None
    else:
        return methods

Лог от изпълнението

FFF..FFFFF.
======================================================================
FAIL: test_dict_nesting (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 6 != 0

======================================================================
FAIL: test_forbid_trailing_whitespace (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 2 != 1

======================================================================
FAIL: test_indentation (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 2 != 1

======================================================================
FAIL: test_max_lines_per_function (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 8 != 1

======================================================================
FAIL: test_multiple_issues_all_over_the_place (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 16 != 5

======================================================================
FAIL: test_no_issues (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 8 != 0

======================================================================
FAIL: test_too_deep_nesting (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 6 != 1

======================================================================
FAIL: test_too_many_arguments (test.TestCritic)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 11 tests in 0.101s

FAILED (failures=8)

История (1 версия и 0 коментара)

Виктор обнови решението на 18.05.2016 16:51 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import ast
import re


def set_rules(rules):
    if rules.get('line_length') is None:
        rules['line_length'] = 79
    if rules.get('forbid_semicolons') is None:
        rules['forbid_semicolons'] = True
    if rules.get('indentation_size') is None:
        rules['indentation_size'] = 4
    if rules.get('forbid_trailing_whitespace') is None:
        rules['forbid_trailing_whitespace'] = True


def check_lines(code, issues, rules):
    lines = code.split('\n')
    for i in range(1, len(lines) + 1):
        issues[i] = []
        line_length = len(lines[i - 1])
        if(line_length > rules['line_length']):
            issues[i].append('line too long ({0} > {1})'.format(
                line_length, rules['line_length']))
        semicolumn_m = re.search("(;)", lines[i - 1])
        if (semicolumn_m):
            issues[i].append('multiple expressions on the same line')


def critic(code, **rules):
    issues = dict()
    set_rules(rules)
    check_lines(code, issues, rules)

    tree = ast.parse(code)
    line_counter = 1
    for node in ast.walk(tree):
        if (isinstance(node, ast.ClassDef) and
                rules['methods_per_class'] is not None):
            methods = check_classdef_methods(node, rules['methods_per_class'])
            if methods is not None:
                issue = 'too many methods in class({0} > {1})'.format(
                    methods, rules['methods_per_class'])
                issues[line_counter] = issue
        line_counter += 1
    return issues


def check_classdef_methods(classdef, number):
    methods = 0
    for node in ast.walk(classdef):
        if isinstance(node, ast.FunctionDef):
            methods += 1
    if methods <= number:
        return None
    else:
        return methods