| Conditions | 13 |
| Total Lines | 69 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like braille_translator.braille_page() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from copy import deepcopy |
||
| 64 | def braille_page(text): |
||
| 65 | code_stream = [[], [], []] |
||
| 66 | for i in text: |
||
| 67 | code_stream[0].append(0) |
||
| 68 | code_stream[1].append(0) |
||
| 69 | code_stream[2].append(0) |
||
| 70 | if i.lower() in LETTERS: |
||
| 71 | converted_code = deepcopy(LETTERS_NUMBERS[LETTERS.index(i.lower())]) |
||
| 72 | if i not in LETTERS: |
||
| 73 | converted_code[0] = CAPITAL_FORMAT[0] + [0] + converted_code[0] |
||
| 74 | converted_code[1] = CAPITAL_FORMAT[1] + [0] + converted_code[1] |
||
| 75 | converted_code[2] = CAPITAL_FORMAT[2] + [0] + converted_code[2] |
||
| 76 | elif i in NUMBERS: |
||
| 77 | converted_code = deepcopy(LETTERS_NUMBERS[NUMBERS.index(i)]) |
||
| 78 | converted_code[0] = NUMBER_FORMAT[0] + [0] + converted_code[0] |
||
| 79 | converted_code[1] = NUMBER_FORMAT[1] + [0] + converted_code[1] |
||
| 80 | converted_code[2] = NUMBER_FORMAT[2] + [0] + converted_code[2] |
||
| 81 | elif i in PUNCTUATION: |
||
| 82 | converted_code = deepcopy(PUNCTUATION[i]) |
||
| 83 | else: |
||
| 84 | converted_code = deepcopy(WHITESPACE) |
||
| 85 | code_stream[0] = code_stream[0] + converted_code[0] |
||
| 86 | code_stream[1] = code_stream[1] + converted_code[1] |
||
| 87 | code_stream[2] = code_stream[2] + converted_code[2] |
||
| 88 | del converted_code |
||
| 89 | for i in range(3): |
||
| 90 | code_stream[i] = code_stream[i][1:] |
||
| 91 | |||
| 92 | # cut stream into 10 symbles long each line |
||
| 93 | if len(code_stream[0]) > 29: |
||
| 94 | code_stack = [] |
||
| 95 | while len(code_stream[0]) > 29: |
||
| 96 | code_stack.append(code_stream[0][:29]) |
||
| 97 | code_stack.append(code_stream[1][:29]) |
||
| 98 | code_stack.append(code_stream[2][:29]) |
||
| 99 | code_stream[0] = code_stream[0][30:] |
||
| 100 | code_stream[1] = code_stream[1][30:] |
||
| 101 | code_stream[2] = code_stream[2][30:] |
||
| 102 | |||
| 103 | code_stack.append(code_stream[0][:29]) |
||
| 104 | code_stack.append(code_stream[1][:29]) |
||
| 105 | code_stack.append(code_stream[2][:29]) |
||
| 106 | code_stream = [] |
||
| 107 | for i in range(len(code_stack) // 3): |
||
| 108 | code_stream.append([0 for k in range(29)]) |
||
| 109 | code_stream.append( |
||
| 110 | [ |
||
| 111 | 0 if k + 1 > len(code_stack[i * 3]) else code_stack[i * 3][k] |
||
| 112 | for k in range(29) |
||
| 113 | ] |
||
| 114 | ) |
||
| 115 | code_stream.append( |
||
| 116 | [ |
||
| 117 | 0 |
||
| 118 | if k + 1 > len(code_stack[i * 3 + 1]) |
||
| 119 | else code_stack[i * 3 + 1][k] |
||
| 120 | for k in range(29) |
||
| 121 | ] |
||
| 122 | ) |
||
| 123 | code_stream.append( |
||
| 124 | [ |
||
| 125 | 0 |
||
| 126 | if k + 1 > len(code_stack[i * 3 + 2]) |
||
| 127 | else code_stack[i * 3 + 2][k] |
||
| 128 | for k in range(29) |
||
| 129 | ] |
||
| 130 | ) |
||
| 131 | return code_stream[1:] |
||
| 132 | return code_stream |
||
| 133 | |||
| 383 |