braille_translator.braille_page()   D
last analyzed

Complexity

Conditions 13

Size

Total Lines 69
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 59
nop 1
dl 0
loc 69
rs 4.2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
2
3
4
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
5
NUMBERS = '1234567890'
6
7
8
def convert(code):
9
    bin_code = bin(code)[2:].zfill(6)[::-1]
10
    return [[int(bin_code[j + i * 3]) for i in range(2)] for j in range(3)]
11
12
13
LETTERS_NUMBERS = list(
14
    map(
15
        convert,
16
        [
17
            1,
18
            3,
19
            9,
20
            25,
21
            17,
22
            11,
23
            27,
24
            19,
25
            10,
26
            26,
27
            5,
28
            7,
29
            13,
30
            29,
31
            21,
32
            15,
33
            31,
34
            23,
35
            14,
36
            30,
37
            37,
38
            39,
39
            62,
40
            45,
41
            61,
42
            53,
43
            47,
44
            63,
45
            55,
46
            46,
47
            26,
48
        ],
49
    )
50
)
51
CAPITAL_FORMAT = convert(32)
52
NUMBER_FORMAT = convert(60)
53
PUNCTUATION = {
54
    ",": convert(2),
55
    "-": convert(18),
56
    "?": convert(38),
57
    "!": convert(22),
58
    ".": convert(50),
59
    "_": convert(36),
60
}
61
WHITESPACE = convert(0)
62
63
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
134
135
if __name__ == '__main__':
136
    # These "asserts" using only for self-checking and not necessary for
137
    # auto-testing
138
    def checker(func, text, answer):
139
        result = func(text)
140
        return answer == tuple(tuple(row) for row in result)
141
142
    assert checker(
143
        braille_page,
144
        u"hello 1st World!",
145
        (
146
            (
147
                1,
148
                0,
149
                0,
150
                1,
151
                0,
152
                0,
153
                1,
154
                0,
155
                0,
156
                1,
157
                0,
158
                0,
159
                1,
160
                0,
161
                0,
162
                0,
163
                0,
164
                0,
165
                0,
166
                1,
167
                0,
168
                1,
169
                0,
170
                0,
171
                0,
172
                1,
173
                0,
174
                0,
175
                1,
176
            ),
177
            (
178
                1,
179
                1,
180
                0,
181
                0,
182
                1,
183
                0,
184
                1,
185
                0,
186
                0,
187
                1,
188
                0,
189
                0,
190
                0,
191
                1,
192
                0,
193
                0,
194
                0,
195
                0,
196
                0,
197
                1,
198
                0,
199
                0,
200
                0,
201
                0,
202
                1,
203
                0,
204
                0,
205
                1,
206
                1,
207
            ),
208
            (
209
                0,
210
                0,
211
                0,
212
                0,
213
                0,
214
                0,
215
                1,
216
                0,
217
                0,
218
                1,
219
                0,
220
                0,
221
                1,
222
                0,
223
                0,
224
                0,
225
                0,
226
                0,
227
                1,
228
                1,
229
                0,
230
                0,
231
                0,
232
                0,
233
                1,
234
                0,
235
                0,
236
                1,
237
                0,
238
            ),
239
            (
240
                0,
241
                0,
242
                0,
243
                0,
244
                0,
245
                0,
246
                0,
247
                0,
248
                0,
249
                0,
250
                0,
251
                0,
252
                0,
253
                0,
254
                0,
255
                0,
256
                0,
257
                0,
258
                0,
259
                0,
260
                0,
261
                0,
262
                0,
263
                0,
264
                0,
265
                0,
266
                0,
267
                0,
268
                0,
269
            ),
270
            (
271
                0,
272
                0,
273
                0,
274
                0,
275
                0,
276
                0,
277
                0,
278
                1,
279
                0,
280
                1,
281
                0,
282
                0,
283
                1,
284
                0,
285
                0,
286
                1,
287
                0,
288
                0,
289
                1,
290
                1,
291
                0,
292
                0,
293
                0,
294
                0,
295
                0,
296
                0,
297
                0,
298
                0,
299
                0,
300
            ),
301
            (
302
                0,
303
                0,
304
                0,
305
                0,
306
                0,
307
                0,
308
                1,
309
                1,
310
                0,
311
                0,
312
                1,
313
                0,
314
                1,
315
                1,
316
                0,
317
                1,
318
                0,
319
                0,
320
                0,
321
                1,
322
                0,
323
                1,
324
                1,
325
                0,
326
                0,
327
                0,
328
                0,
329
                0,
330
                0,
331
            ),
332
            (
333
                0,
334
                0,
335
                0,
336
                0,
337
                1,
338
                0,
339
                1,
340
                1,
341
                0,
342
                1,
343
                0,
344
                0,
345
                1,
346
                0,
347
                0,
348
                1,
349
                0,
350
                0,
351
                0,
352
                0,
353
                0,
354
                1,
355
                0,
356
                0,
357
                0,
358
                0,
359
                0,
360
                0,
361
                0,
362
            ),
363
        ),
364
    ), "Example"
365
    assert checker(
366
        braille_page,
367
        u"42",
368
        (
369
            (0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0),
370
            (0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0),
371
            (1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0),
372
        ),
373
    ), "42"
374
    assert checker(
375
        braille_page,
376
        u"CODE",
377
        (
378
            (0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0),
379
            (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1),
380
            (0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0),
381
        ),
382
    ), "CODE"
383