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