| Conditions | 24 |
| Total Lines | 172 |
| Code Lines | 103 |
| 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 -*- |
||
| 113 | def create_nodes(nd=None): |
||
| 114 | """Create nodes (oemof objects) from node dict |
||
| 115 | |||
| 116 | Parameters |
||
| 117 | ---------- |
||
| 118 | nd : :obj:`dict` |
||
| 119 | Nodes data |
||
| 120 | |||
| 121 | Returns |
||
| 122 | ------- |
||
| 123 | nodes : `obj`:dict of :class:`nodes <oemof.network.Node>` |
||
| 124 | """ |
||
| 125 | |||
| 126 | if not nd: |
||
| 127 | raise ValueError("No nodes data provided.") |
||
| 128 | |||
| 129 | nodes = [] |
||
| 130 | |||
| 131 | # Create Bus objects from buses table |
||
| 132 | busd = {} |
||
| 133 | |||
| 134 | for i, b in nd["buses"].iterrows(): |
||
| 135 | if b["active"]: |
||
| 136 | bus = solph.Bus(label=b["label"]) |
||
| 137 | nodes.append(bus) |
||
| 138 | |||
| 139 | busd[b["label"]] = bus |
||
| 140 | if b["excess"]: |
||
| 141 | nodes.append( |
||
| 142 | solph.components.Sink( |
||
| 143 | label=b["label"] + "_excess", |
||
| 144 | inputs={ |
||
| 145 | busd[b["label"]]: solph.Flow( |
||
| 146 | variable_costs=b["excess costs"] |
||
| 147 | ) |
||
| 148 | }, |
||
| 149 | ) |
||
| 150 | ) |
||
| 151 | if b["shortage"]: |
||
| 152 | nodes.append( |
||
| 153 | solph.components.Source( |
||
| 154 | label=b["label"] + "_shortage", |
||
| 155 | outputs={ |
||
| 156 | busd[b["label"]]: solph.Flow( |
||
| 157 | variable_costs=b["shortage costs"] |
||
| 158 | ) |
||
| 159 | }, |
||
| 160 | ) |
||
| 161 | ) |
||
| 162 | |||
| 163 | # Create Source objects from table 'commodity sources' |
||
| 164 | for i, cs in nd["commodity_sources"].iterrows(): |
||
| 165 | if cs["active"]: |
||
| 166 | nodes.append( |
||
| 167 | solph.components.Source( |
||
| 168 | label=cs["label"], |
||
| 169 | outputs={ |
||
| 170 | busd[cs["to"]]: solph.Flow( |
||
| 171 | variable_costs=cs["variable costs"] |
||
| 172 | ) |
||
| 173 | }, |
||
| 174 | ) |
||
| 175 | ) |
||
| 176 | |||
| 177 | # Create Source objects with fixed time series from 'renewables' table |
||
| 178 | for i, re in nd["renewables"].iterrows(): |
||
| 179 | if re["active"]: |
||
| 180 | # set static outflow values |
||
| 181 | outflow_args = {"nominal_capacity": re["capacity"]} |
||
| 182 | # get time series for node and parameter |
||
| 183 | for col in nd["timeseries"].columns.values: |
||
| 184 | if col.split(".")[0] == re["label"]: |
||
| 185 | outflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 186 | |||
| 187 | # create |
||
| 188 | nodes.append( |
||
| 189 | solph.components.Source( |
||
| 190 | label=re["label"], |
||
| 191 | outputs={busd[re["to"]]: solph.Flow(**outflow_args)}, |
||
| 192 | ) |
||
| 193 | ) |
||
| 194 | |||
| 195 | # Create Sink objects with fixed time series from 'demand' table |
||
| 196 | for i, de in nd["demand"].iterrows(): |
||
| 197 | if de["active"]: |
||
| 198 | # set static inflow values |
||
| 199 | inflow_args = {"nominal_capacity": de["nominal value"]} |
||
| 200 | # get time series for node and parameter |
||
| 201 | for col in nd["timeseries"].columns.values: |
||
| 202 | if col.split(".")[0] == de["label"]: |
||
| 203 | inflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 204 | |||
| 205 | # create |
||
| 206 | nodes.append( |
||
| 207 | solph.components.Sink( |
||
| 208 | label=de["label"], |
||
| 209 | inputs={busd[de["from"]]: solph.Flow(**inflow_args)}, |
||
| 210 | ) |
||
| 211 | ) |
||
| 212 | |||
| 213 | # Create Converter objects from 'converters' table |
||
| 214 | for i, t in nd["converters"].iterrows(): |
||
| 215 | if t["active"]: |
||
| 216 | # set static inflow values |
||
| 217 | inflow_args = {"variable_costs": t["variable input costs"]} |
||
| 218 | # get time series for inflow of converter |
||
| 219 | for col in nd["timeseries"].columns.values: |
||
| 220 | if col.split(".")[0] == t["label"]: |
||
| 221 | inflow_args[col.split(".")[1]] = nd["timeseries"][col] |
||
| 222 | # create |
||
| 223 | nodes.append( |
||
| 224 | solph.components.Converter( |
||
| 225 | label=t["label"], |
||
| 226 | inputs={busd[t["from"]]: solph.Flow(**inflow_args)}, |
||
| 227 | outputs={ |
||
| 228 | busd[t["to"]]: solph.Flow( |
||
| 229 | nominal_capacity=t["capacity"] |
||
| 230 | ) |
||
| 231 | }, |
||
| 232 | conversion_factors={busd[t["to"]]: t["efficiency"]}, |
||
| 233 | ) |
||
| 234 | ) |
||
| 235 | |||
| 236 | for i, s in nd["storages"].iterrows(): |
||
| 237 | if s["active"]: |
||
| 238 | nodes.append( |
||
| 239 | solph.components.GenericStorage( |
||
| 240 | label=s["label"], |
||
| 241 | inputs={ |
||
| 242 | busd[s["bus"]]: solph.Flow( |
||
| 243 | nominal_capacity=s["capacity inflow"], |
||
| 244 | variable_costs=s["variable input costs"], |
||
| 245 | ) |
||
| 246 | }, |
||
| 247 | outputs={ |
||
| 248 | busd[s["bus"]]: solph.Flow( |
||
| 249 | nominal_capacity=s["capacity outflow"], |
||
| 250 | variable_costs=s["variable output costs"], |
||
| 251 | ) |
||
| 252 | }, |
||
| 253 | nominal_capacity=s["nominal capacity"], |
||
| 254 | loss_rate=s["capacity loss"], |
||
| 255 | initial_storage_level=s["initial capacity"], |
||
| 256 | max_storage_level=s["capacity max"], |
||
| 257 | min_storage_level=s["capacity min"], |
||
| 258 | inflow_conversion_factor=s["efficiency inflow"], |
||
| 259 | outflow_conversion_factor=s["efficiency outflow"], |
||
| 260 | ) |
||
| 261 | ) |
||
| 262 | |||
| 263 | for i, p in nd["powerlines"].iterrows(): |
||
| 264 | if p["active"]: |
||
| 265 | bus1 = busd[p["bus_1"]] |
||
| 266 | bus2 = busd[p["bus_2"]] |
||
| 267 | nodes.append( |
||
| 268 | solph.components.Converter( |
||
| 269 | label="powerline" + "_" + p["bus_1"] + "_" + p["bus_2"], |
||
| 270 | inputs={bus1: solph.Flow()}, |
||
| 271 | outputs={bus2: solph.Flow()}, |
||
| 272 | conversion_factors={bus2: p["efficiency"]}, |
||
| 273 | ) |
||
| 274 | ) |
||
| 275 | nodes.append( |
||
| 276 | solph.components.Converter( |
||
| 277 | label="powerline" + "_" + p["bus_2"] + "_" + p["bus_1"], |
||
| 278 | inputs={bus2: solph.Flow()}, |
||
| 279 | outputs={bus1: solph.Flow()}, |
||
| 280 | conversion_factors={bus1: p["efficiency"]}, |
||
| 281 | ) |
||
| 282 | ) |
||
| 283 | |||
| 284 | return nodes |
||
| 285 | |||
| 443 |