timeit

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

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

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

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

Към профила на Георги Иванов

Резултати

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

Код

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import ast


def critic(code, **rules):
    errors = {}
    line = 1
    cnt_line = 0

    for c in code:
        if c != '\n':
            cnt_line += 1
        else:
            line += 1
            if cnt_line > 79:
                if line not in errors:
                    errors[line] = set()
                errors[line].add('line too long ({} > 79)'.format(cnt_line))
            cnt_line = 0

        if c == ';':
            if line not in errors:
                errors[line] = set()
            errors[line].add('multiple expressions on the same line')

    if cnt_line > 79:
        if line not in errors:
            errors[line] = set()
        errors[line].add('line too long ({} > 79)'.format(cnt_line))

    visited_funcs = set()
    tree = ast.parse(code)
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef):
            n1 = 0
            for funcs in node.body:
                if isinstance(funcs, ast.FunctionDef) and\
                        funcs.name not in visited_funcs:
                    visited_funcs.add(funcs.name)
                    n = check_arg_num_func(funcs)
                    if 'max_arity' in rules:
                        if n > rules['max_arity']:
                            if funcs.lineno not in errors:
                                errors[funcs.lineno] = set()
                            errors[funcs.lineno].add(
                                'too many arguments({} > {})'.format(
                                    n, rules['max_arity']))
                    n1 += 1
            if 'methods_per_class' in rules:
                if n1 > rules['methods_per_class']:
                    if node.lineno not in errors:
                        errors[node.lineno] = set()
                errors[node.lineno].add(
                    'too many methods in class({} > {})'.format(
                        n1, rules['methods_per_class']))

        elif isinstance(node, ast.FunctionDef) and\
                node.name not in visited_funcs:
            visited_funcs.add(node.name)
            n = check_arg_num_func(node)
            if 'max_arity' in rules:
                if n > rules['max_arity']:
                    if node.lineno not in errors:
                        errors[node.lineno] = set()
                    errors[node.lineno].add(
                        'too many arguments({} > {})'.format(
                            n, rules['max_arity']))
    return errors


def check_arg_num_func(node):
    n = 0
    for i in ast.walk(node.args):
        if isinstance(i, ast.arg):
            n += 1

    return n

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

.FF..FF.F..
======================================================================
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: 0 != 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: 0 != 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: 0 != 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: 3 != 5

======================================================================
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: 0 != 1

----------------------------------------------------------------------
Ran 11 tests in 0.094s

FAILED (failures=5)

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

Георги обнови решението на 18.05.2016 16:50 (преди над 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import ast


def critic(code, **rules):
    errors = {}
    line = 1
    cnt_line = 0

    for c in code:
        if c != '\n':
            cnt_line += 1
        else:
            line += 1
            if cnt_line > 79:
                if line not in errors:
                    errors[line] = set()
                errors[line].add('line too long ({} > 79)'.format(cnt_line))
            cnt_line = 0

        if c == ';':
            if line not in errors:
                errors[line] = set()
            errors[line].add('multiple expressions on the same line')

    if cnt_line > 79:
        if line not in errors:
            errors[line] = set()
        errors[line].add('line too long ({} > 79)'.format(cnt_line))

    visited_funcs = set()
    tree = ast.parse(code)
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef):
            n1 = 0
            for funcs in node.body:
                if isinstance(funcs, ast.FunctionDef) and\
                        funcs.name not in visited_funcs:
                    visited_funcs.add(funcs.name)
                    n = check_arg_num_func(funcs)
                    if 'max_arity' in rules:
                        if n > rules['max_arity']:
                            if funcs.lineno not in errors:
                                errors[funcs.lineno] = set()
                            errors[funcs.lineno].add(
                                'too many arguments({} > {})'.format(
                                    n, rules['max_arity']))
                    n1 += 1
            if 'methods_per_class' in rules:
                if n1 > rules['methods_per_class']:
                    if node.lineno not in errors:
                        errors[node.lineno] = set()
                errors[node.lineno].add(
                    'too many methods in class({} > {})'.format(
                        n1, rules['methods_per_class']))

        elif isinstance(node, ast.FunctionDef) and\
                node.name not in visited_funcs:
            visited_funcs.add(node.name)
            n = check_arg_num_func(node)
            if 'max_arity' in rules:
                if n > rules['max_arity']:
                    if node.lineno not in errors:
                        errors[node.lineno] = set()
                    errors[node.lineno].add(
                        'too many arguments({} > {})'.format(
                            n, rules['max_arity']))
    return errors


def check_arg_num_func(node):
    n = 0
    for i in ast.walk(node.args):
        if isinstance(i, ast.arg):
            n += 1

    return n