Passed
Pull Request — master (#2)
by Uwe
01:01
created

scenario_builder.mobility.scenario_mobility()   A

Complexity

Conditions 2

Size

Total Lines 56
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nop 2
dl 0
loc 56
rs 9.5
c 0
b 0
f 0

How to fix   Long Method   

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:

1
"""Create a basic scenario from the internal data structure.
2
3
SPDX-FileCopyrightText: 2016-2019 Uwe Krien <[email protected]>
4
5
SPDX-License-Identifier: MIT
6
"""
7
8
import pandas as pd
9
from reegis import mobility
10
11
from scenario_builder import config as cfg
12
13
14
def scenario_mobility(year, table):
15
    """
16
17
    Parameters
18
    ----------
19
    year
20
    table
21
22
    Returns
23
    -------
24
25
    Examples
26
    --------
27
    >>> my_table = scenario_mobility(2015, {})
28
    >>> my_table["mobility_mileage"]["DE"].sum()
29
    diesel    3.769021e+11
30
    petrol    3.272263e+11
31
    other     1.334462e+10
32
    dtype: float64
33
    >>> my_table["mobility_spec_demand"]["DE"].loc["passenger car"]
34
    diesel    0.067
35
    petrol    0.079
36
    other     0.000
37
    Name: passenger car, dtype: float64
38
    >>> my_table["mobility_energy_content"]["DE"]["diesel"]
39
    energy_per_liter [MJ/l]    34.7
40
    Name: diesel, dtype: float64
41
    """
42
43
    table["mobility_mileage"] = mobility.get_mileage_by_type_and_fuel(year)
44
45
    # fetch table of specific demand by fuel and vehicle type (from 2011)
46
    table["mobility_spec_demand"] = (
47
        pd.DataFrame(
48
            cfg.get_dict_list("fuel consumption"),
49
            index=["diesel", "petrol", "other"],
50
        )
51
        .astype(float)
52
        .transpose()
53
    )
54
55
    # fetch the energy content of the different fuel types
56
    table["mobility_energy_content"] = pd.DataFrame(
57
        cfg.get_dict("energy_per_liter"), index=["energy_per_liter [MJ/l]"]
58
    )[["diesel", "petrol", "other"]]
59
60
    for key in [
61
        "mobility_mileage",
62
        "mobility_spec_demand",
63
        "mobility_energy_content",
64
    ]:
65
        # Add "DE" as region level to be consistent to other tables
66
        table[key].columns = pd.MultiIndex.from_product(
67
            [["DE"], table[key].columns]
68
        )
69
    return table
70