1import re
2
3import secret
4
5TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно."
6
7METHOD_NAME_PATTERN = r'^[A-Z0-9]$'
8OBJECT_NAME_PATTERN = r'^\w*clue\w*$'
9
10needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"}
11
12
13def validate_interesting_method(current_function):
14 """Validate if the current function is the one needed"""
15 try:
16 method_check = current_function.__self__
17 if current_function.__code__.co_argcount == 2:
18 if current_function(2) == 4 and current_function(3) == 0:
19 return True
20 if current_function.__code__.co_argcount == 3:
21 if current_function('left ', 'right') == 'left right':
22 return True
23 except AttributeError as e:
24 if str(e) == "'function' object has no attribute '__self__'":
25 return True
26 except TypeError as e:
27 if str(e) == TYPE_ERROR_TEXT_MESSAGE:
28 return True
29 except BaseException as e:
30 if str(e):
31 return True
32
33 return False
34
35
36def recursive_methodify(curr_object, gathered_methods):
37 """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number"""
38 if needed_letters == {}:
39 return
40
41 curr_dict_keys = dir(curr_object)
42
43 for key in curr_dict_keys:
44 if needed_letters == {}:
45 break
46
47 current_function_or_object = getattr(curr_object, key)
48 if (
49 callable(current_function_or_object) and
50 re.match(METHOD_NAME_PATTERN, key) and
51 key in needed_letters and
52 validate_interesting_method(current_function_or_object)
53 ):
54 needed_letters.remove(key)
55 gathered_methods[key] = gathered_methods.get(key, current_function_or_object)
56 elif re.match(OBJECT_NAME_PATTERN, key):
57 recursive_methodify(current_function_or_object, gathered_methods)
58
59
60def methodify():
61 """Collect all Methods from the secret object with such names that i gather my Faculty Number"""
62 gathered_methods = {}
63 recursive_methodify(secret, gathered_methods)
64
65 if len(needed_letters) > 0:
66 raise Exception("Not all methods were found")
67
68 return (
69 gathered_methods['F'],
70 gathered_methods['N'],
71 gathered_methods['0'],
72 gathered_methods['M'],
73 gathered_methods['I'],
74 gathered_methods['0'],
75 gathered_methods['6'],
76 gathered_methods['0'],
77 gathered_methods['0'],
78 gathered_methods['0'],
79 gathered_methods['4'],
80 gathered_methods['1'],
81 )
E
======================================================================
ERROR: test_metodify (test.TestMethodify)
Test metodify function.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 12, in test_metodify
self.assertIn(methodify(), _RESULTS.keys())
File "/tmp/solution.py", line 69, in methodify
gathered_methods['F'],
KeyError: 'F'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | import secret | 3 | import secret | ||
4 | 4 | ||||
5 | TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно." | 5 | TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно." | ||
6 | 6 | ||||
7 | METHOD_NAME_PATTERN = r'^[A-Z0-9]$' | 7 | METHOD_NAME_PATTERN = r'^[A-Z0-9]$' | ||
8 | OBJECT_NAME_PATTERN = r'^\w*clue\w*$' | 8 | OBJECT_NAME_PATTERN = r'^\w*clue\w*$' | ||
9 | 9 | ||||
10 | needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"} | 10 | needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"} | ||
n | 11 | gathered_methods = {} | n | ||
12 | 11 | ||||
13 | 12 | ||||
14 | def validate_interesting_method(current_function): | 13 | def validate_interesting_method(current_function): | ||
15 | """Validate if the current function is the one needed""" | 14 | """Validate if the current function is the one needed""" | ||
16 | try: | 15 | try: | ||
17 | method_check = current_function.__self__ | 16 | method_check = current_function.__self__ | ||
18 | if current_function.__code__.co_argcount == 2: | 17 | if current_function.__code__.co_argcount == 2: | ||
19 | if current_function(2) == 4 and current_function(3) == 0: | 18 | if current_function(2) == 4 and current_function(3) == 0: | ||
20 | return True | 19 | return True | ||
21 | if current_function.__code__.co_argcount == 3: | 20 | if current_function.__code__.co_argcount == 3: | ||
22 | if current_function('left ', 'right') == 'left right': | 21 | if current_function('left ', 'right') == 'left right': | ||
23 | return True | 22 | return True | ||
24 | except AttributeError as e: | 23 | except AttributeError as e: | ||
25 | if str(e) == "'function' object has no attribute '__self__'": | 24 | if str(e) == "'function' object has no attribute '__self__'": | ||
26 | return True | 25 | return True | ||
27 | except TypeError as e: | 26 | except TypeError as e: | ||
28 | if str(e) == TYPE_ERROR_TEXT_MESSAGE: | 27 | if str(e) == TYPE_ERROR_TEXT_MESSAGE: | ||
29 | return True | 28 | return True | ||
30 | except BaseException as e: | 29 | except BaseException as e: | ||
31 | if str(e): | 30 | if str(e): | ||
32 | return True | 31 | return True | ||
33 | 32 | ||||
34 | return False | 33 | return False | ||
35 | 34 | ||||
36 | 35 | ||||
n | 37 | def recursive_methodify(curr_object): | n | 36 | def recursive_methodify(curr_object, gathered_methods): |
38 | """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number""" | 37 | """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number""" | ||
39 | if needed_letters == {}: | 38 | if needed_letters == {}: | ||
40 | return | 39 | return | ||
41 | 40 | ||||
42 | curr_dict_keys = dir(curr_object) | 41 | curr_dict_keys = dir(curr_object) | ||
43 | 42 | ||||
44 | for key in curr_dict_keys: | 43 | for key in curr_dict_keys: | ||
45 | if needed_letters == {}: | 44 | if needed_letters == {}: | ||
46 | break | 45 | break | ||
47 | 46 | ||||
48 | current_function_or_object = getattr(curr_object, key) | 47 | current_function_or_object = getattr(curr_object, key) | ||
49 | if ( | 48 | if ( | ||
50 | callable(current_function_or_object) and | 49 | callable(current_function_or_object) and | ||
51 | re.match(METHOD_NAME_PATTERN, key) and | 50 | re.match(METHOD_NAME_PATTERN, key) and | ||
52 | key in needed_letters and | 51 | key in needed_letters and | ||
53 | validate_interesting_method(current_function_or_object) | 52 | validate_interesting_method(current_function_or_object) | ||
54 | ): | 53 | ): | ||
55 | needed_letters.remove(key) | 54 | needed_letters.remove(key) | ||
56 | gathered_methods[key] = gathered_methods.get(key, current_function_or_object) | 55 | gathered_methods[key] = gathered_methods.get(key, current_function_or_object) | ||
57 | elif re.match(OBJECT_NAME_PATTERN, key): | 56 | elif re.match(OBJECT_NAME_PATTERN, key): | ||
n | 58 | recursive_methodify(current_function_or_object) | n | 57 | recursive_methodify(current_function_or_object, gathered_methods) |
59 | 58 | ||||
60 | 59 | ||||
61 | def methodify(): | 60 | def methodify(): | ||
62 | """Collect all Methods from the secret object with such names that i gather my Faculty Number""" | 61 | """Collect all Methods from the secret object with such names that i gather my Faculty Number""" | ||
t | t | 62 | gathered_methods = {} | ||
63 | recursive_methodify(secret) | 63 | recursive_methodify(secret, gathered_methods) | ||
64 | 64 | ||||
65 | if len(needed_letters) > 0: | 65 | if len(needed_letters) > 0: | ||
66 | raise Exception("Not all methods were found") | 66 | raise Exception("Not all methods were found") | ||
67 | 67 | ||||
68 | return ( | 68 | return ( | ||
69 | gathered_methods['F'], | 69 | gathered_methods['F'], | ||
70 | gathered_methods['N'], | 70 | gathered_methods['N'], | ||
71 | gathered_methods['0'], | 71 | gathered_methods['0'], | ||
72 | gathered_methods['M'], | 72 | gathered_methods['M'], | ||
73 | gathered_methods['I'], | 73 | gathered_methods['I'], | ||
74 | gathered_methods['0'], | 74 | gathered_methods['0'], | ||
75 | gathered_methods['6'], | 75 | gathered_methods['6'], | ||
76 | gathered_methods['0'], | 76 | gathered_methods['0'], | ||
77 | gathered_methods['0'], | 77 | gathered_methods['0'], | ||
78 | gathered_methods['0'], | 78 | gathered_methods['0'], | ||
79 | gathered_methods['4'], | 79 | gathered_methods['4'], | ||
80 | gathered_methods['1'], | 80 | gathered_methods['1'], | ||
81 | ) | 81 | ) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | import secret | 3 | import secret | ||
4 | 4 | ||||
5 | TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно." | 5 | TYPE_ERROR_TEXT_MESSAGE = "Опаааааа, тука има нещо нередно." | ||
6 | 6 | ||||
7 | METHOD_NAME_PATTERN = r'^[A-Z0-9]$' | 7 | METHOD_NAME_PATTERN = r'^[A-Z0-9]$' | ||
8 | OBJECT_NAME_PATTERN = r'^\w*clue\w*$' | 8 | OBJECT_NAME_PATTERN = r'^\w*clue\w*$' | ||
9 | 9 | ||||
10 | needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"} | 10 | needed_letters = {"F", "N", "M", "I", "0", "6", "4", "1"} | ||
11 | gathered_methods = {} | 11 | gathered_methods = {} | ||
12 | 12 | ||||
13 | 13 | ||||
14 | def validate_interesting_method(current_function): | 14 | def validate_interesting_method(current_function): | ||
15 | """Validate if the current function is the one needed""" | 15 | """Validate if the current function is the one needed""" | ||
16 | try: | 16 | try: | ||
17 | method_check = current_function.__self__ | 17 | method_check = current_function.__self__ | ||
18 | if current_function.__code__.co_argcount == 2: | 18 | if current_function.__code__.co_argcount == 2: | ||
19 | if current_function(2) == 4 and current_function(3) == 0: | 19 | if current_function(2) == 4 and current_function(3) == 0: | ||
20 | return True | 20 | return True | ||
21 | if current_function.__code__.co_argcount == 3: | 21 | if current_function.__code__.co_argcount == 3: | ||
22 | if current_function('left ', 'right') == 'left right': | 22 | if current_function('left ', 'right') == 'left right': | ||
23 | return True | 23 | return True | ||
24 | except AttributeError as e: | 24 | except AttributeError as e: | ||
25 | if str(e) == "'function' object has no attribute '__self__'": | 25 | if str(e) == "'function' object has no attribute '__self__'": | ||
26 | return True | 26 | return True | ||
27 | except TypeError as e: | 27 | except TypeError as e: | ||
28 | if str(e) == TYPE_ERROR_TEXT_MESSAGE: | 28 | if str(e) == TYPE_ERROR_TEXT_MESSAGE: | ||
29 | return True | 29 | return True | ||
30 | except BaseException as e: | 30 | except BaseException as e: | ||
31 | if str(e): | 31 | if str(e): | ||
32 | return True | 32 | return True | ||
33 | 33 | ||||
34 | return False | 34 | return False | ||
35 | 35 | ||||
36 | 36 | ||||
37 | def recursive_methodify(curr_object): | 37 | def recursive_methodify(curr_object): | ||
38 | """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number""" | 38 | """Recursively collect all Methods from the secret object with such names that i gather my Faculty Number""" | ||
39 | if needed_letters == {}: | 39 | if needed_letters == {}: | ||
40 | return | 40 | return | ||
41 | 41 | ||||
42 | curr_dict_keys = dir(curr_object) | 42 | curr_dict_keys = dir(curr_object) | ||
43 | 43 | ||||
44 | for key in curr_dict_keys: | 44 | for key in curr_dict_keys: | ||
45 | if needed_letters == {}: | 45 | if needed_letters == {}: | ||
46 | break | 46 | break | ||
47 | 47 | ||||
48 | current_function_or_object = getattr(curr_object, key) | 48 | current_function_or_object = getattr(curr_object, key) | ||
49 | if ( | 49 | if ( | ||
50 | callable(current_function_or_object) and | 50 | callable(current_function_or_object) and | ||
51 | re.match(METHOD_NAME_PATTERN, key) and | 51 | re.match(METHOD_NAME_PATTERN, key) and | ||
52 | key in needed_letters and | 52 | key in needed_letters and | ||
53 | validate_interesting_method(current_function_or_object) | 53 | validate_interesting_method(current_function_or_object) | ||
54 | ): | 54 | ): | ||
55 | needed_letters.remove(key) | 55 | needed_letters.remove(key) | ||
56 | gathered_methods[key] = gathered_methods.get(key, current_function_or_object) | 56 | gathered_methods[key] = gathered_methods.get(key, current_function_or_object) | ||
57 | elif re.match(OBJECT_NAME_PATTERN, key): | 57 | elif re.match(OBJECT_NAME_PATTERN, key): | ||
58 | recursive_methodify(current_function_or_object) | 58 | recursive_methodify(current_function_or_object) | ||
59 | 59 | ||||
60 | 60 | ||||
61 | def methodify(): | 61 | def methodify(): | ||
62 | """Collect all Methods from the secret object with such names that i gather my Faculty Number""" | 62 | """Collect all Methods from the secret object with such names that i gather my Faculty Number""" | ||
63 | recursive_methodify(secret) | 63 | recursive_methodify(secret) | ||
64 | 64 | ||||
65 | if len(needed_letters) > 0: | 65 | if len(needed_letters) > 0: | ||
66 | raise Exception("Not all methods were found") | 66 | raise Exception("Not all methods were found") | ||
67 | 67 | ||||
68 | return ( | 68 | return ( | ||
t | 69 | gathered_methods['F'].__name__, | t | 69 | gathered_methods['F'], |
70 | gathered_methods['N'].__name__, | 70 | gathered_methods['N'], | ||
71 | gathered_methods['0'].__name__, | 71 | gathered_methods['0'], | ||
72 | gathered_methods['M'].__name__, | 72 | gathered_methods['M'], | ||
73 | gathered_methods['I'].__name__, | 73 | gathered_methods['I'], | ||
74 | gathered_methods['0'].__name__, | 74 | gathered_methods['0'], | ||
75 | gathered_methods['6'].__name__, | 75 | gathered_methods['6'], | ||
76 | gathered_methods['0'].__name__, | 76 | gathered_methods['0'], | ||
77 | gathered_methods['0'].__name__, | 77 | gathered_methods['0'], | ||
78 | gathered_methods['0'].__name__, | 78 | gathered_methods['0'], | ||
79 | gathered_methods['4'].__name__, | 79 | gathered_methods['4'], | ||
80 | gathered_methods['1'].__name__, | 80 | gathered_methods['1'], | ||
81 | ) | 81 | ) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|