Passed
Pull Request — master (#74)
by
unknown
05:42 queued 02:29
created

marvin_general_actions   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 23.68 %

Test Coverage

Coverage 58.06%

Importance

Changes 0
Metric Value
eloc 44
dl 18
loc 76
ccs 18
cts 31
cp 0.5806
rs 10
c 0
b 0
f 0
wmc 11

3 Functions

Rating   Name   Duplication   Size   Complexity  
A getAllGeneralActions() 0 6 1
B getString() 18 18 6
A marvinMorning() 0 29 4

How to fix   Duplicated Code   

Duplicated Code

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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
introduced by
The variable res does not seem to be defined for all execution paths.
Loading history...
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