timeit

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

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

Решение на Статичен анализ на python код от Славена Теодосиева

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

Към профила на Славена Теодосиева

Резултати

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

Код

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
import re


def actual_rules(code, rules, key):
    other_rules = {"line_length" : 79,
                   "forbid_semicolons" : True,
                   "max_nesting" : None,
                   "indentation_size" : 4,
                   "methods_per_class" : None,
                   "max_arity" : None,
                   "forbid_trailing_whitespace" : True,
                   "max_lines_per_function" : None}
    if key in rules.keys():
        return rules[key]
    else:
        return other_rules[key]
    

def check_semicolons(line):
    return re.findall(';', line)


def check_length(line):
    return len(line)


def critic(code, **rules):
    code_in_lines = code.splitlines()
    number_of_lines = len(code_in_lines)
    errors = {}
    line_length = actual_rules(code, rules, "line_length")
    forbid_semicolons = actual_rules(code, rules, "forbid_semicolons")
    max_nesting = actual_rules(code, rules, "max_nesting")
    indentation_size = actual_rules(code, rules, "indentation_size")
    methods_per_class = actual_rules(code, rules, "methods_per_class")
    max_arity = actual_rules(code, rules, "max_arity")
    forbid_trailing_whitespace = actual_rules(code, rules, "forbid_trailing_whitespace")
    max_lines_per_function = actual_rules(code, rules, "max_lines_per_function")    
    i = 0
    while i < number_of_lines:
        actual_length = check_length(code_in_lines[i])
        if actual_length > line_length:
            if i + 1 in errors.keys():                
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
            else:
                errors[i + 1] = []
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
        if forbid_semicolons and check_semicolons(code_in_lines[i]):
            if i + 1 in errors.keys():
                errors[i + 1].append('multiple expressions on the same line')
            else:
                errors[i + 1] = []
                errors[i + 1].append('multiple expressions on the same line')
        if code_in_lines[i][actual_length - 1] is ' ':
            if i + 1 in errors.keys():
                errors[i + 1].append('trailing whitespaces')
            else:
                errors[i + 1] = []
                errors[i + 1].append('trailing whitespaces')
        i += 1
    return errors

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

.FF..FEEFF.
======================================================================
ERROR: 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
IndexError: string index out of range

======================================================================
ERROR: 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
IndexError: string index out of range

======================================================================
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: Items in the first set but not the second:
'trailing whitespaces'
Items in the second set but not the first:
'trailing whitespace'

======================================================================
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_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

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

----------------------------------------------------------------------
Ran 11 tests in 0.089s

FAILED (failures=5, errors=2)

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

Славена обнови решението на 18.05.2016 15:11 (преди над 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
import re


def actual_rules(code, rules, key):
    other_rules = {"line_length" : 79,
                   "forbid_semicolons" : True,
                   "max_nesting" : None,
                   "indentation_size" : 4,
                   "methods_per_class" : None,
                   "max_arity" : None,
                   "forbid_trailing_whitespace" : True,
                   "max_lines_per_function" : None}
    if key in rules.keys():
        return rules[key]
    else:
        return other_rules[key]
    

def check_semicolons(line):
    return re.findall(';', line)


def check_length(line):
    return len(line)


#def check_whitespaces(line):
    #return re.findall('\s{2,}', line)
    

def critic(code, **rules):
    code_in_lines = code.splitlines()
    number_of_lines = len(code_in_lines)
    errors = {}
    #errors = {line : [] for line in range(number_of_lines)}
    #errors = dict.fromkeys(range(1,len(code_in_lines)))
    line_length = actual_rules(code, rules, "line_length")
    forbid_semicolons = actual_rules(code, rules, "forbid_semicolons")
    max_nesting = actual_rules(code, rules, "max_nesting")
    indentation_size = actual_rules(code, rules, "indentation_size")
    methods_per_class = actual_rules(code, rules, "methods_per_class")
    max_arity = actual_rules(code, rules, "max_arity")
    forbid_trailing_whitespace = actual_rules(code, rules, "forbid_trailing_whitespace")
    max_lines_per_function = actual_rules(code, rules, "max_lines_per_function")    
    i = 0
    while i < number_of_lines:
        actual_length = check_length(code_in_lines[i]) 
        if actual_length > line_length:
            if i + 1 in errors.keys():                
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
            else:
                errors[i + 1] = []
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
        if forbid_semicolons is True and check_semicolons(code_in_lines[i]) is not None:
            # da dobavq, che moje da ima edin red, zavyrshvasht na ;
            if i + 1 in errors.keys():
                errors[i + 1].append('multiple expressions on the same line')
            else:
                errors[i + 1] = []
                errors[i + 1].append('multiple expressions on the same line')
        #if forbid_trailing_whitespace is True and check_whitespaces(code_in_lines[i]) is not None or code_in_lines[i][actual_length - 1] is ' ':
        if code_in_lines[i][actual_length - 1] is ' ':
            if i + 1 in errors.keys():
                errors[i + 1].append('trailing whitespaces')
            else:
                errors[i + 1] = []
                errors[i + 1].append('trailing whitespaces')
        i += 1
    return errors

Славена обнови решението на 18.05.2016 16:13 (преди над 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
import re


def actual_rules(code, rules, key):
    other_rules = {"line_length" : 79,
                   "forbid_semicolons" : True,
                   "max_nesting" : None,
                   "indentation_size" : 4,
                   "methods_per_class" : None,
                   "max_arity" : None,
                   "forbid_trailing_whitespace" : True,
                   "max_lines_per_function" : None}
    if key in rules.keys():
        return rules[key]
    else:
        return other_rules[key]
    

def check_semicolons(line):
    return re.findall(';', line)


def check_length(line):
    return len(line)


def critic(code, **rules):
    code_in_lines = code.splitlines()
    number_of_lines = len(code_in_lines)
    errors = {}
    line_length = actual_rules(code, rules, "line_length")
    forbid_semicolons = actual_rules(code, rules, "forbid_semicolons")
    max_nesting = actual_rules(code, rules, "max_nesting")
    indentation_size = actual_rules(code, rules, "indentation_size")
    methods_per_class = actual_rules(code, rules, "methods_per_class")
    max_arity = actual_rules(code, rules, "max_arity")
    forbid_trailing_whitespace = actual_rules(code, rules, "forbid_trailing_whitespace")
    max_lines_per_function = actual_rules(code, rules, "max_lines_per_function")    
    i = 0
    while i < number_of_lines:
        actual_length = check_length(code_in_lines[i])
        if actual_length > line_length:
            if i + 1 in errors.keys():                
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
            else:
                errors[i + 1] = []
                errors[i + 1].append('line too long ({} > {})'.format(actual_length, line_length))
        if forbid_semicolons and check_semicolons(code_in_lines[i]):
            if i + 1 in errors.keys():
                errors[i + 1].append('multiple expressions on the same line')
            else:
                errors[i + 1] = []
                errors[i + 1].append('multiple expressions on the same line')
        if code_in_lines[i][actual_length - 1] is ' ':
            if i + 1 in errors.keys():
                errors[i + 1].append('trailing whitespaces')
            else:
                errors[i + 1] = []
                errors[i + 1].append('trailing whitespaces')
        i += 1
    return errors