Skip to content

Instantly share code, notes, and snippets.

@gnn
Forked from uvchik/invest_test.py
Last active May 11, 2016 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnn/5c13063a258725d69693679407783fff to your computer and use it in GitHub Desktop.
Save gnn/5c13063a258725d69693679407783fff to your computer and use it in GitHub Desktop.
test the invest
from oemof.network import Source
class Investment:
""" Components (Nodes?) grouped under `Investment` objects are optimized
for investment.
"""
def __init__(self, maximum=float('+inf')):
self.maximum = maximum
# As nearly always, there's more than one way to do this. The simplest would
# be:
def investlabel(node):
return node.label[2]
# but that's kinda unsafe, as it relies on the fact that we always have at
# least three components in the tuple and that the third is always the
# `Investment` instance. This is safer:
def safe_investlabel(node):
try:
return node.label[2]
except (IndexError, TypeError):
return None
# And if you really want to be fancy, you can search the label for an
# `Investment` instance, return it if you find one and return `None` if you
# can't iterate through `label` (TypeError) or if you don't find an
# `Investment` instance.
def fancy_investlabel(node):
try:
for l in node.label:
if isinstance(l, Investment):
return l
except TypeError:
return None
return None
# All of the above definitions would work with the following code:
es = EnergySystem(groupings=[investlabel])
pv_invest_max = investment(maximum=345)
Source(label=('pv', 'berlin', pv_invest_max), ....)
# And if you know that you'll always write the investment component of your
# label like "Invest" (for unbounded investment) or "Invest: 345" (for a
# maximal investment of 345), you can even get rid of the line creating
# `pv_invest_max`:
def automatic_investlabel(node):
try:
for l in node.label:
return Investment() if l == "Invest"
return Investment(float(l[8:])) if l[0:8] == "Invest: "
except TypeError:
return None
return None
es = EnergySystem(groupings=[automatic_investlabel])
Source(label=('pv', 'berlin', "Invest: 345"), ....)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment