| Total Complexity | 11 |
| Total Lines | 76 |
| Duplicated Lines | 23.68 % |
| Coverage | 58.06% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | #! /usr/bin/env python3 |
||
| 2 | # -*- coding: utf-8 -*- |
||
| 3 | |||
| 4 | """ |
||
| 5 | Make general actions for Marvin, one function for each action. |
||
| 6 | """ |
||
| 7 | 1 | import datetime |
|
| 8 | 1 | import json |
|
| 9 | 1 | import random |
|
| 10 | |||
| 11 | # Load all strings from file |
||
| 12 | 1 | with open("marvin_strings.json", encoding="utf-8") as f: |
|
| 13 | 1 | STRINGS = json.load(f) |
|
| 14 | |||
| 15 | 1 | lastDateGreeted = None |
|
| 16 | |||
| 17 | |||
| 18 | 1 | View Code Duplication | def getString(key, key1=None): |
|
|
|||
| 19 | """ |
||
| 20 | Get a string from the string database. |
||
| 21 | """ |
||
| 22 | data = STRINGS[key] |
||
| 23 | if isinstance(data, list): |
||
| 24 | res = data[random.randint(0, len(data) - 1)] |
||
| 25 | elif isinstance(data, dict): |
||
| 26 | if key1 is None: |
||
| 27 | res = data |
||
| 28 | else: |
||
| 29 | res = data[key1] |
||
| 30 | if isinstance(res, list): |
||
| 31 | res = res[random.randint(0, len(res) - 1)] |
||
| 32 | elif isinstance(data, str): |
||
| 33 | res = data |
||
| 34 | |||
| 35 | return res |
||
| 36 | |||
| 37 | |||
| 38 | 1 | def getAllGeneralActions(): |
|
| 39 | """ |
||
| 40 | Return all general actions as an array. |
||
| 41 | """ |
||
| 42 | return [ |
||
| 43 | marvinMorning |
||
| 44 | ] |
||
| 45 | |||
| 46 | |||
| 47 | 1 | def marvinMorning(row): |
|
| 48 | """ |
||
| 49 | Marvin says Good morning after someone else says it |
||
| 50 | """ |
||
| 51 | 1 | msg = None |
|
| 52 | 1 | phrases = [ |
|
| 53 | "morgon", |
||
| 54 | "godmorgon", |
||
| 55 | "god morgon", |
||
| 56 | "morrn", |
||
| 57 | "morn" |
||
| 58 | ] |
||
| 59 | |||
| 60 | 1 | morning_phrases = [ |
|
| 61 | "Godmorgon! :-)", |
||
| 62 | "Morgon allesammans", |
||
| 63 | "Morgon gott folk", |
||
| 64 | "Guten morgen", |
||
| 65 | "Morgon" |
||
| 66 | ] |
||
| 67 | |||
| 68 | global lastDateGreeted |
||
| 69 | |||
| 70 | 1 | for phrase in phrases: |
|
| 71 | 1 | if phrase in row: |
|
| 72 | 1 | if lastDateGreeted != datetime.date.today(): |
|
| 73 | 1 | lastDateGreeted = datetime.date.today() |
|
| 74 | 1 | msg = random.choice(morning_phrases) |
|
| 75 | return msg |
||
| 76 |