Skip to content

Commit

Permalink
Introduce WindTurbineGroup as dataclass
Browse files Browse the repository at this point in the history
This has the advantage that it is easier to find in the documentation.
  • Loading branch information
uvchik committed Aug 28, 2019
1 parent 8fa7848 commit f718f66
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
6 changes: 3 additions & 3 deletions tests/test_wind_turbine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import pytest
import os
from windpowerlib.tools import WindpowerlibUserWarning
from collections import namedtuple

from windpowerlib.wind_turbine import (get_turbine_data_from_file, WindTurbine,
get_turbine_types,
get_turbine_types, WindTurbineGroup,
load_turbine_data_from_oedb)


Expand Down Expand Up @@ -62,7 +62,7 @@ def test_to_group_method(self):
'turbine_type': 'DUMMY 3',
'path': self.source}
e_t_1 = WindTurbine(**example_turbine)
assert(isinstance(e_t_1.to_group(), tuple))
assert(isinstance(e_t_1.to_group(), WindTurbineGroup))
assert(e_t_1.to_group(5).number_of_turbines == 5)
assert(e_t_1.to_group(number_turbines=5).number_of_turbines == 5)
assert(e_t_1.to_group(total_capacity=3e6).number_of_turbines == 2.0)
Expand Down
7 changes: 6 additions & 1 deletion windpowerlib/wind_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ class WindFarm(object):
Parameters
----------
wind_turbine_fleet : :pandas:`pandas.DataFrame<frame>` or list()
Wind turbines of wind farm. DataFrame/Dictionaries must have
Wind turbines of wind farm. DataFrame must have
'wind_turbine' containing a :class:`~.wind_turbine.WindTurbine` object
and either 'number_of_turbines' (number of wind turbines of the same
turbine type in the wind farm, can be a float) or 'total_capacity'
(installed capacity of wind turbines of the same turbine type in the
wind farm) as columns/keys. See example below.
A list of :class:`~windpowerlib.wind_turbine.WindTurbineGroup` objects.
A WindTurbineGroup can be created from a
:class:`~windpowerlib.wind_turbine.WindTurbine` using the
:func:`~windpowerlib.wind_turbine.WindTurbine.to_group` method.
efficiency : float or :pandas:`pandas.DataFrame<frame>` or None (optional)
Efficiency of the wind farm. Provide as either constant (float) or
power efficiency curve (pd.DataFrame) containing 'wind_speed' and
Expand Down
35 changes: 25 additions & 10 deletions windpowerlib/wind_turbine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import warnings
import requests
import os
from collections import namedtuple
from windpowerlib.tools import WindpowerlibUserWarning
from typing import NamedTuple


class WindTurbine(object):
Expand Down Expand Up @@ -199,11 +199,14 @@ def __repr__(self):
return turbine_repr

def to_group(self, number_turbines=None, total_capacity=None):
"""
Creates a WindTurbine group as a dictionary with the keys
'number_of_turbines' and 'wind_turbine'. It can be used to calculate
the number of turbines for a given total capacity or to create a
dictionary that can be used to define a WindFarm object.
r"""
Creates a :class:`~windpowerlib.wind_turbine.WindTurbineGroup`, a
NamedTuple data container with the fields 'number_of_turbines' and
'wind_turbine'.
It can be used to calculate the number of turbines for a given total
capacity or to create a namedtuple that can be used to define a
:class:`~windpowerlib.wind_farm.WindFarm` object.
Parameters
----------
Expand All @@ -214,7 +217,7 @@ def to_group(self, number_turbines=None, total_capacity=None):
Returns
-------
namedtuple
WindTurbineGroup
A namedtuple with two fields: 'number_of_turbines' and
'wind_turbine'.
Expand Down Expand Up @@ -253,12 +256,24 @@ def to_group(self, number_turbines=None, total_capacity=None):
number_turbines = total_capacity / self.nominal_power
elif number_turbines is None:
number_turbines = 1
wind_turbine_group = namedtuple('WindTurbineGroup',
'wind_turbine number_of_turbines')
return wind_turbine_group(

return WindTurbineGroup(
wind_turbine=self, number_of_turbines=number_turbines)


# This is working for Python >= 3.5.
# There a cleaner solutions for Python >= 3.6, once the support of 3.5 is
# dropped: https://stackoverflow.com/a/50038614
class WindTurbineGroup(NamedTuple('WindTurbineGroup', [
('wind_turbine', WindTurbine), ('number_of_turbines', float)])):
"""
A simple data container to define more than one turbine of the same type.
Use the :func:`~windpowerlib.wind_turbine.WindTurbine.to_group` method to
easily create a WindTurbineGroup from a :class:`~windpowerlib.wind_turbine.WindTurbine` object.
"""
__slots__ = ()


def get_turbine_data_from_file(turbine_type, path):
r"""
Fetches turbine data from a csv file.
Expand Down

0 comments on commit f718f66

Please sign in to comment.