Passed
Pull Request — dev (#1138)
by
unknown
02:07
created

data.datasets.emobility.motorized_individual_travel.db_classes   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 281
Duplicated Lines 17.79 %

Importance

Changes 0
Metric Value
wmc 0
eloc 102
dl 50
loc 281
rs 10
c 0
b 0
f 0

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
"""
2
DB tables / SQLAlchemy ORM classes for motorized individual travel
3
"""
4
5
from sqlalchemy import (
6
    Boolean,
7
    Column,
8
    DateTime,
9
    Float,
10
    ForeignKey,
11
    Integer,
12
    SmallInteger,
13
    String,
14
)
15
from sqlalchemy.dialects.postgresql import REAL
16
from sqlalchemy.ext.declarative import declarative_base
17
18
from egon.data.datasets.mv_grid_districts import MvGridDistricts
19
from egon.data.datasets.scenario_parameters import EgonScenario
20
21
# from sqlalchemy.orm import relationship
22
23
24
Base = declarative_base()
25
26
27
class EgonEvPool(Base):
28
    """
29
    Class definition of table demand.egon_ev_pool.
30
31
    Each row is one EV, uniquely defined by either (`ev_id`) or
32
    (`rs7_id`, `type`, `simbev_id`).
33
34
    Columns
35
    -------
36
    ev_id:
37
        Unique id of EV
38
    rs7_id:
39
        id of RegioStar7 region
40
    type:
41
        type of EV, one of
42
            * bev_mini
43
            * bev_medium
44
            * bev_luxury
45
            * phev_mini
46
            * phev_medium
47
            * phev_luxury
48
    simbev_ev_id:
49
        id of EV as exported by simBEV
50
    """
51
52
    __tablename__ = "egon_ev_pool"
53
    __table_args__ = {"schema": "demand"}
54
55
    scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True)
56
    ev_id = Column(Integer, primary_key=True)
57
    rs7_id = Column(SmallInteger)
58
    type = Column(String(11))
59
    simbev_ev_id = Column(Integer)
60
61
    # trips = relationship(
62
    #     "EgonEvTrip", cascade="all, delete", back_populates="ev"
63
    # )
64
    # mvgds = relationship(
65
    #     "EgonEvMvGridDistrict", cascade="all, delete", back_populates="ev"
66
    # )
67
68
69
class EgonEvTrip(Base):
70
    """
71
    Class definition of table demand.egon_ev_trip.
72
73
    Each row is one event of a specific electric vehicle which is
74
    uniquely defined by `rs7_id`, `ev_id` and `event_id`.
75
76
    Columns
77
    -------
78
    scenario:
79
        Scenario
80
    event_id:
81
        Unique id of EV event
82
    egon_ev_pool_ev_id:
83
        id of EV, references EgonEvPool.ev_id
84
    simbev_event_id:
85
        id of EV event, unique within a specific EV dataset
86
    location:
87
        Location of EV event, one of
88
            * "0_work"
89
            * "1_business"
90
            * "2_school"
91
            * "3_shopping"
92
            * "4_private/ridesharing"
93
            * "5_leisure"
94
            * "6_home"
95
            * "7_charging_hub"
96
            * "driving"
97
    use_case:
98
        Use case of EV event, one of
99
            * "public" (public charging)
100
            * "home" (private charging at 6_home)
101
            * "work" (private charging at 0_work)
102
            * <empty> (driving events)
103
    charging_capacity_nominal:
104
        Nominal charging capacity in kW
105
    charging_capacity_grid:
106
        Charging capacity at grid side in kW,
107
        includes efficiency of charging infrastructure
108
    charging_capacity_battery:
109
        Charging capacity at battery side in kW,
110
        includes efficiency of car charger
111
    soc_start:
112
        State of charge at start of event
113
    soc_start:
114
        State of charge at end of event
115
    charging_demand:
116
        Energy demand during parking/charging event in kWh.
117
        0 if no charging takes place.
118
    park_start:
119
        Start timestep of parking event (15min interval, e.g. 4 = 1h)
120
    park_end:
121
        End timestep of parking event (15min interval)
122
    drive_start:
123
        Start timestep of driving event (15min interval)
124
    drive_end:
125
        End timestep of driving event (15min interval)
126
    consumption:
127
        Energy demand during driving event in kWh
128
129
    Notes
130
    -----
131
    pgSQL's REAL is sufficient for floats as simBEV rounds output to 4 digits.
132
    """
133
134
    __tablename__ = "egon_ev_trip"
135
    __table_args__ = {"schema": "demand"}
136
137
    # scenario = Column(
138
    #    String, ForeignKey(EgonEvPool.scenario), primary_key=True
139
    # )
140
    scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True)
141
    event_id = Column(Integer, primary_key=True)
142
    # egon_ev_pool_ev_id = Column(
143
    #    Integer, ForeignKey(EgonEvPool.ev_id), nullable=False, index=True
144
    # )
145
    egon_ev_pool_ev_id = Column(Integer, nullable=False, index=True)
146
    simbev_event_id = Column(Integer)
147
    location = Column(String(21))
148
    use_case = Column(String(8))
149
    charging_capacity_nominal = Column(REAL)
150
    charging_capacity_grid = Column(REAL)
151
    charging_capacity_battery = Column(REAL)
152
    soc_start = Column(REAL)
153
    soc_end = Column(REAL)
154
    charging_demand = Column(REAL)
155
    park_start = Column(Integer)
156
    park_end = Column(Integer)
157
    drive_start = Column(Integer)
158
    drive_end = Column(Integer)
159
    consumption = Column(REAL)
160
161
    # __table_args__ = (
162
    #    ForeignKeyConstraint([scenario, egon_ev_pool_ev_id],
163
    #                         [EgonEvPool.scenario, EgonEvPool.ev_id]),
164
    #    {"schema": "demand"},
165
    # )
166
167
    # ev = relationship("EgonEvPool", back_populates="trips")
168
169
170 View Code Duplication
class EgonEvCountRegistrationDistrict(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
171
    """
172
    Class definition of table demand.egon_ev_count_registration_district.
173
174
    Contains electric vehicle counts per registration district.
175
176
    """
177
178
    __tablename__ = "egon_ev_count_registration_district"
179
    __table_args__ = {"schema": "demand"}
180
181
    scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True)
182
    scenario_variation = Column(String, primary_key=True)
183
    ags_reg_district = Column(Integer, primary_key=True)
184
    reg_district = Column(String)
185
    bev_mini = Column(Integer)
186
    bev_medium = Column(Integer)
187
    bev_luxury = Column(Integer)
188
    phev_mini = Column(Integer)
189
    phev_medium = Column(Integer)
190
    phev_luxury = Column(Integer)
191
192
193 View Code Duplication
class EgonEvCountMunicipality(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
194
    """
195
    Class definition of table demand.egon_ev_count_municipality.
196
197
    Contains electric vehicle counts per municipality.
198
199
    """
200
201
    __tablename__ = "egon_ev_count_municipality"
202
    __table_args__ = {"schema": "demand"}
203
204
    scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True)
205
    scenario_variation = Column(String, primary_key=True)
206
    ags = Column(Integer, primary_key=True)
207
    bev_mini = Column(Integer)
208
    bev_medium = Column(Integer)
209
    bev_luxury = Column(Integer)
210
    phev_mini = Column(Integer)
211
    phev_medium = Column(Integer)
212
    phev_luxury = Column(Integer)
213
    rs7_id = Column(SmallInteger)
214
215
216 View Code Duplication
class EgonEvCountMvGridDistrict(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
217
    """
218
    Class definition of table demand.egon_ev_count_mv_grid_district.
219
220
    Contains electric vehicle counts per MV grid district.
221
222
    """
223
224
    __tablename__ = "egon_ev_count_mv_grid_district"
225
    __table_args__ = {"schema": "demand"}
226
227
    scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True)
228
    scenario_variation = Column(String, primary_key=True)
229
    bus_id = Column(
230
        Integer, ForeignKey(MvGridDistricts.bus_id), primary_key=True
231
    )
232
    bev_mini = Column(Integer)
233
    bev_medium = Column(Integer)
234
    bev_luxury = Column(Integer)
235
    phev_mini = Column(Integer)
236
    phev_medium = Column(Integer)
237
    phev_luxury = Column(Integer)
238
    rs7_id = Column(SmallInteger)
239
240
241
class EgonEvMvGridDistrict(Base):
242
    """
243
    Class definition of table demand.egon_ev_mv_grid_district.
244
245
    Contains list of electric vehicles per MV grid district.
246
247
    """
248
249
    __tablename__ = "egon_ev_mv_grid_district"
250
    __table_args__ = {"schema": "demand"}
251
252
    id = Column(Integer, primary_key=True)
253
    scenario = Column(String, ForeignKey(EgonScenario.name), index=True)
254
    scenario_variation = Column(String, index=True)
255
    bus_id = Column(Integer, ForeignKey(MvGridDistricts.bus_id), index=True)
256
    # egon_ev_pool_ev_id = Column(Integer, ForeignKey(EgonEvPool.ev_id))
257
    egon_ev_pool_ev_id = Column(Integer, nullable=False)
258
259
    # ev = relationship("EgonEvPool", back_populates="mvgds")
260
261
262
class EgonEvMetadata(Base):
263
    """
264
    Class definition of table demand.egon_ev_metadata.
265
266
    Contains EV Pool Metadata.
267
268
    """
269
270
    __tablename__ = "egon_ev_metadata"
271
    __table_args__ = {"schema": "demand"}
272
273
    scenario = Column(String, primary_key=True)
274
    eta_cp = Column(Float)
275
    stepsize = Column(Integer)
276
    start_date = Column(DateTime)
277
    end_date = Column(DateTime)
278
    soc_min = Column(Float)
279
    grid_timeseries = Column(Boolean)
280
    grid_timeseries_by_usecase = Column(Boolean)
281