| Conditions | 14 |
| Total Lines | 55 |
| Code Lines | 44 |
| Lines | 16 |
| Ratio | 29.09 % |
| 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 playfair_cipher.encode() 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 | def GetKeyMatrix(key): |
||
| 8 | def encode(message, key): |
||
| 9 | KeyMatrix = GetKeyMatrix(key) |
||
| 10 | |||
| 11 | # convert message |
||
| 12 | message = [i for i in message.lower() if i in KeyMatrix] |
||
| 13 | i = 0 |
||
| 14 | Digraphs = [] |
||
| 15 | while i <= len(message) - 1: |
||
| 16 | # If needed, append a "z" to complete the final digraph (or "x" if the |
||
| 17 | # last letter is "z") |
||
| 18 | if i == len(message) - 1: |
||
| 19 | if message[i] != 'z': |
||
| 20 | Digraphs.append(message[i] + 'z') |
||
| 21 | else: |
||
| 22 | Digraphs.append(message[i] + 'x') |
||
| 23 | i += 1 |
||
| 24 | elif message[i] != message[i + 1]: |
||
| 25 | Digraphs.append(''.join(message[i : i + 2])) |
||
| 26 | i += 2 |
||
| 27 | # If both letters are the same, add an "x" after the first letter (for |
||
| 28 | # double "x" use "z" as completion character) |
||
| 29 | elif message[i] == message[i + 1]: |
||
| 30 | if message[i] != 'x': |
||
| 31 | Digraphs.append(message[i] + 'x') |
||
| 32 | else: |
||
| 33 | Digraphs.append(message[i] + 'z') |
||
| 34 | i += 1 |
||
| 35 | else: |
||
| 36 | print('FUCK!', message[i], message[i + 1]) |
||
| 37 | raise |
||
| 38 | |||
| 39 | EncryptedText = [] |
||
| 40 | for i in Digraphs: |
||
| 41 | row0 = KeyMatrix.index(i[0]) // 6 |
||
| 42 | col0 = KeyMatrix.index(i[0]) % 6 |
||
| 43 | row1 = KeyMatrix.index(i[1]) // 6 |
||
| 44 | col1 = KeyMatrix.index(i[1]) % 6 |
||
| 45 | View Code Duplication | if row0 == row1: |
|
|
|
|||
| 46 | col0 += 1 |
||
| 47 | col1 += 1 |
||
| 48 | if col0 > 5: |
||
| 49 | col0 -= 6 |
||
| 50 | if col1 > 5: |
||
| 51 | col1 -= 6 |
||
| 52 | elif col0 == col1: |
||
| 53 | row0 += 1 |
||
| 54 | row1 += 1 |
||
| 55 | if row0 > 5: |
||
| 56 | row0 -= 6 |
||
| 57 | if row1 > 5: |
||
| 58 | row1 -= 6 |
||
| 59 | else: |
||
| 60 | col0, col1 = col1, col0 |
||
| 61 | EncryptedText.append(KeyMatrix[row0 * 6 + col0] + KeyMatrix[row1 * 6 + col1]) |
||
| 62 | return ''.join(EncryptedText) |
||
| 63 | |||
| 115 |