Skip to content

Commit f718f66

Browse files
committed
Introduce WindTurbineGroup as dataclass
This has the advantage that it is easier to find in the documentation.
1 parent 8fa7848 commit f718f66

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

tests/test_wind_turbine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import pytest
99
import os
1010
from windpowerlib.tools import WindpowerlibUserWarning
11-
from collections import namedtuple
11+
1212
from windpowerlib.wind_turbine import (get_turbine_data_from_file, WindTurbine,
13-
get_turbine_types,
13+
get_turbine_types, WindTurbineGroup,
1414
load_turbine_data_from_oedb)
1515

1616

@@ -62,7 +62,7 @@ def test_to_group_method(self):
6262
'turbine_type': 'DUMMY 3',
6363
'path': self.source}
6464
e_t_1 = WindTurbine(**example_turbine)
65-
assert(isinstance(e_t_1.to_group(), tuple))
65+
assert(isinstance(e_t_1.to_group(), WindTurbineGroup))
6666
assert(e_t_1.to_group(5).number_of_turbines == 5)
6767
assert(e_t_1.to_group(number_turbines=5).number_of_turbines == 5)
6868
assert(e_t_1.to_group(total_capacity=3e6).number_of_turbines == 2.0)

windpowerlib/wind_farm.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ class WindFarm(object):
2222
Parameters
2323
----------
2424
wind_turbine_fleet : :pandas:`pandas.DataFrame<frame>` or list()
25-
Wind turbines of wind farm. DataFrame/Dictionaries must have
25+
Wind turbines of wind farm. DataFrame must have
2626
'wind_turbine' containing a :class:`~.wind_turbine.WindTurbine` object
2727
and either 'number_of_turbines' (number of wind turbines of the same
2828
turbine type in the wind farm, can be a float) or 'total_capacity'
2929
(installed capacity of wind turbines of the same turbine type in the
3030
wind farm) as columns/keys. See example below.
31+
32+
A list of :class:`~windpowerlib.wind_turbine.WindTurbineGroup` objects.
33+
A WindTurbineGroup can be created from a
34+
:class:`~windpowerlib.wind_turbine.WindTurbine` using the
35+
:func:`~windpowerlib.wind_turbine.WindTurbine.to_group` method.
3136
efficiency : float or :pandas:`pandas.DataFrame<frame>` or None (optional)
3237
Efficiency of the wind farm. Provide as either constant (float) or
3338
power efficiency curve (pd.DataFrame) containing 'wind_speed' and

windpowerlib/wind_turbine.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import warnings
1414
import requests
1515
import os
16-
from collections import namedtuple
1716
from windpowerlib.tools import WindpowerlibUserWarning
17+
from typing import NamedTuple
1818

1919

2020
class WindTurbine(object):
@@ -199,11 +199,14 @@ def __repr__(self):
199199
return turbine_repr
200200

201201
def to_group(self, number_turbines=None, total_capacity=None):
202-
"""
203-
Creates a WindTurbine group as a dictionary with the keys
204-
'number_of_turbines' and 'wind_turbine'. It can be used to calculate
205-
the number of turbines for a given total capacity or to create a
206-
dictionary that can be used to define a WindFarm object.
202+
r"""
203+
Creates a :class:`~windpowerlib.wind_turbine.WindTurbineGroup`, a
204+
NamedTuple data container with the fields 'number_of_turbines' and
205+
'wind_turbine'.
206+
207+
It can be used to calculate the number of turbines for a given total
208+
capacity or to create a namedtuple that can be used to define a
209+
:class:`~windpowerlib.wind_farm.WindFarm` object.
207210
208211
Parameters
209212
----------
@@ -214,7 +217,7 @@ def to_group(self, number_turbines=None, total_capacity=None):
214217
215218
Returns
216219
-------
217-
namedtuple
220+
WindTurbineGroup
218221
A namedtuple with two fields: 'number_of_turbines' and
219222
'wind_turbine'.
220223
@@ -253,12 +256,24 @@ def to_group(self, number_turbines=None, total_capacity=None):
253256
number_turbines = total_capacity / self.nominal_power
254257
elif number_turbines is None:
255258
number_turbines = 1
256-
wind_turbine_group = namedtuple('WindTurbineGroup',
257-
'wind_turbine number_of_turbines')
258-
return wind_turbine_group(
259+
260+
return WindTurbineGroup(
259261
wind_turbine=self, number_of_turbines=number_turbines)
260262

261263

264+
# This is working for Python >= 3.5.
265+
# There a cleaner solutions for Python >= 3.6, once the support of 3.5 is
266+
# dropped: https://stackoverflow.com/a/50038614
267+
class WindTurbineGroup(NamedTuple('WindTurbineGroup', [
268+
('wind_turbine', WindTurbine), ('number_of_turbines', float)])):
269+
"""
270+
A simple data container to define more than one turbine of the same type.
271+
Use the :func:`~windpowerlib.wind_turbine.WindTurbine.to_group` method to
272+
easily create a WindTurbineGroup from a :class:`~windpowerlib.wind_turbine.WindTurbine` object.
273+
"""
274+
__slots__ = ()
275+
276+
262277
def get_turbine_data_from_file(turbine_type, path):
263278
r"""
264279
Fetches turbine data from a csv file.

0 commit comments

Comments
 (0)