| Conditions | 24 |
| Total Lines | 170 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
Complex classes like dispatch.create_nodes() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 100 | def create_nodes(nd=None): |
||
| 101 | """Create nodes (oemof objects) from node dict |
||
| 102 | |||
| 103 | Parameters |
||
| 104 | ---------- |
||
| 105 | nd : :obj:`dict` |
||
| 106 | Nodes data |
||
| 107 | |||
| 108 | Returns |
||
| 109 | ------- |
||
| 110 | nodes : `obj`:dict of :class:`nodes <oemof.network.Node>` |
||
| 111 | """ |
||
| 112 | |||
| 113 | if not nd: |
||
| 114 | raise ValueError("No nodes data provided.") |
||
| 115 | |||
| 116 | nodes = [] |
||
| 117 | |||
| 118 | # Create Bus objects from buses table |
||
| 119 | busd = {} |
||
| 120 | |||
| 121 | for i, b in nd["buses"].iterrows(): |
||
| 122 | if b["active"]: |
||
| 123 | bus = solph.Bus(label=b["label"]) |
||
| 124 | nodes.append(bus) |
||
| 125 | |||
| 126 | busd[b["label"]] = bus |
||
| 127 | if b["excess"]: |
||
| 128 | nodes.append( |
||
| 129 | solph.components.Sink( |
||
| 130 | label=b["label"] + "_excess", |
||
| 131 | inputs={ |
||
| 132 | busd[b["label"]]: solph.Flow( |
||
| 133 | variable_costs=b["excess costs"] |
||
| 134 | ) |
||
| 135 | }, |
||
| 136 | ) |
||
| 137 | ) |
||
| 138 | if b["shortage"]: |
||
| 139 | nodes.append( |
||
| 140 | solph.components.Source( |
||
| 141 | label=b["label"] + "_shortage", |
||
| 142 | outputs={ |
||
| 143 | busd[b["label"]]: solph.Flow( |
||
| 144 | variable_costs=b["shortage costs"] |
||
| 145 | ) |
||
| 146 | }, |
||
| 147 | ) |
||
| 148 | ) |
||
| 149 | |||
| 150 | # Create Source objects from table 'commodity sources' |
||
| 151 | for i, cs in nd["commodity_sources"].iterrows(): |
||
| 152 | if cs["active"]: |
||
| 153 | nodes.append( |
||
| 154 | solph.components.Source( |
||
| 155 | label=cs["label"], |
||
| 156 | outputs={ |
||
| 157 | busd[cs["to"]]: solph.Flow( |
||
| 158 | variable_costs=cs["variable costs"] |
||
| 159 | ) |
||
| 160 | }, |
||
| 161 | ) |
||
| 162 | ) |
||
| 163 | |||
| 164 | # Create Source objects with fixed time series from 'renewables' table |
||
| 165 | for i, re in nd["renewables"].iterrows(): |
||
| 166 | if re["active"]: |
||
| 167 | # set static outflow values |
||
| 168 | outflow_args = {"nominal_value": re["capacity"]} |
||
| 169 | # get time series for node and parameter |
||
| 170 | for col in nd["timeseries"].columns.values: |
||
| 171 | if col.split(".")[0] == re["label"]: |
||
| 172 | outflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 173 | |||
| 174 | # create |
||
| 175 | nodes.append( |
||
| 176 | solph.components.Source( |
||
| 177 | label=re["label"], |
||
| 178 | outputs={busd[re["to"]]: solph.Flow(**outflow_args)}, |
||
| 179 | ) |
||
| 180 | ) |
||
| 181 | |||
| 182 | # Create Sink objects with fixed time series from 'demand' table |
||
| 183 | for i, de in nd["demand"].iterrows(): |
||
| 184 | if de["active"]: |
||
| 185 | # set static inflow values |
||
| 186 | inflow_args = {"nominal_value": de["nominal value"]} |
||
| 187 | # get time series for node and parameter |
||
| 188 | for col in nd["timeseries"].columns.values: |
||
| 189 | if col.split(".")[0] == de["label"]: |
||
| 190 | inflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 191 | |||
| 192 | # create |
||
| 193 | nodes.append( |
||
| 194 | solph.components.Sink( |
||
| 195 | label=de["label"], |
||
| 196 | inputs={busd[de["from"]]: solph.Flow(**inflow_args)}, |
||
| 197 | ) |
||
| 198 | ) |
||
| 199 | |||
| 200 | # Create Transformer objects from 'transformers' table |
||
| 201 | for i, t in nd["transformers"].iterrows(): |
||
| 202 | if t["active"]: |
||
| 203 | # set static inflow values |
||
| 204 | inflow_args = {"variable_costs": t["variable input costs"]} |
||
| 205 | # get time series for inflow of transformer |
||
| 206 | for col in nd["timeseries"].columns.values: |
||
| 207 | if col.split(".")[0] == t["label"]: |
||
| 208 | inflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 209 | # create |
||
| 210 | nodes.append( |
||
| 211 | solph.components.Transformer( |
||
| 212 | label=t["label"], |
||
| 213 | inputs={busd[t["from"]]: solph.Flow(**inflow_args)}, |
||
| 214 | outputs={ |
||
| 215 | busd[t["to"]]: solph.Flow(nominal_value=t["capacity"]) |
||
| 216 | }, |
||
| 217 | conversion_factors={busd[t["to"]]: t["efficiency"]}, |
||
| 218 | ) |
||
| 219 | ) |
||
| 220 | |||
| 221 | for i, s in nd["storages"].iterrows(): |
||
| 222 | if s["active"]: |
||
| 223 | nodes.append( |
||
| 224 | solph.components.GenericStorage( |
||
| 225 | label=s["label"], |
||
| 226 | inputs={ |
||
| 227 | busd[s["bus"]]: solph.Flow( |
||
| 228 | nominal_value=s["capacity inflow"], |
||
| 229 | variable_costs=s["variable input costs"], |
||
| 230 | ) |
||
| 231 | }, |
||
| 232 | outputs={ |
||
| 233 | busd[s["bus"]]: solph.Flow( |
||
| 234 | nominal_value=s["capacity outflow"], |
||
| 235 | variable_costs=s["variable output costs"], |
||
| 236 | ) |
||
| 237 | }, |
||
| 238 | nominal_storage_capacity=s["nominal capacity"], |
||
| 239 | loss_rate=s["capacity loss"], |
||
| 240 | initial_storage_level=s["initial capacity"], |
||
| 241 | max_storage_level=s["capacity max"], |
||
| 242 | min_storage_level=s["capacity min"], |
||
| 243 | inflow_conversion_factor=s["efficiency inflow"], |
||
| 244 | outflow_conversion_factor=s["efficiency outflow"], |
||
| 245 | ) |
||
| 246 | ) |
||
| 247 | |||
| 248 | for i, p in nd["powerlines"].iterrows(): |
||
| 249 | if p["active"]: |
||
| 250 | bus1 = busd[p["bus_1"]] |
||
| 251 | bus2 = busd[p["bus_2"]] |
||
| 252 | nodes.append( |
||
| 253 | solph.components.Transformer( |
||
| 254 | label="powerline" + "_" + p["bus_1"] + "_" + p["bus_2"], |
||
| 255 | inputs={bus1: solph.Flow()}, |
||
| 256 | outputs={bus2: solph.Flow()}, |
||
| 257 | conversion_factors={bus2: p["efficiency"]}, |
||
| 258 | ) |
||
| 259 | ) |
||
| 260 | nodes.append( |
||
| 261 | solph.components.Transformer( |
||
| 262 | label="powerline" + "_" + p["bus_2"] + "_" + p["bus_1"], |
||
| 263 | inputs={bus2: solph.Flow()}, |
||
| 264 | outputs={bus1: solph.Flow()}, |
||
| 265 | conversion_factors={bus1: p["efficiency"]}, |
||
| 266 | ) |
||
| 267 | ) |
||
| 268 | |||
| 269 | return nodes |
||
| 270 | |||
| 425 |