| Conditions | 2 |
| Total Lines | 90 |
| Code Lines | 39 |
| 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:
| 1 | """Module containing functions to insert gas abroad |
||
| 18 | def insert_generators(gen, scn_name): |
||
| 19 | """Insert gas generators for foreign countries in database |
||
| 20 | |||
| 21 | This function inserts the gas generators for foreign countries |
||
| 22 | with the following steps: |
||
| 23 | * Clean the database |
||
| 24 | * Receive the gas production capacities per foreign node |
||
| 25 | * For eGon2035, put them in the righ format |
||
| 26 | * Add missing columns (generator_id and carrier) |
||
| 27 | * Insert the table into the database |
||
| 28 | |||
| 29 | Parameters |
||
| 30 | ---------- |
||
| 31 | gen : pandas.DataFrame |
||
| 32 | Gas production capacities per foreign node |
||
| 33 | scn_name : str |
||
| 34 | Name of the scenario |
||
| 35 | |||
| 36 | Returns |
||
| 37 | ------- |
||
| 38 | None |
||
| 39 | |||
| 40 | """ |
||
| 41 | carrier = "CH4" |
||
| 42 | sources = config.datasets()["gas_neighbours"]["sources"] |
||
| 43 | targets = config.datasets()["gas_neighbours"]["targets"] |
||
| 44 | |||
| 45 | # Delete existing data |
||
| 46 | db.execute_sql( |
||
| 47 | f""" |
||
| 48 | DELETE FROM |
||
| 49 | {targets['generators']['schema']}.{targets['generators']['table']}
|
||
| 50 | WHERE bus IN ( |
||
| 51 | SELECT bus_id FROM |
||
| 52 | {sources['buses']['schema']}.{sources['buses']['table']}
|
||
| 53 | WHERE country != 'DE' |
||
| 54 | AND scn_name = '{scn_name}')
|
||
| 55 | AND scn_name = '{scn_name}'
|
||
| 56 | AND carrier = '{carrier}';
|
||
| 57 | """ |
||
| 58 | ) |
||
| 59 | |||
| 60 | if scn_name == "eGon2035": |
||
| 61 | map_buses = get_map_buses() |
||
| 62 | scn_params = get_sector_parameters("gas", scn_name)
|
||
| 63 | |||
| 64 | # Set bus_id |
||
| 65 | gen.loc[ |
||
| 66 | gen[gen["index"].isin(map_buses.keys())].index, "index" |
||
| 67 | ] = gen.loc[ |
||
| 68 | gen[gen["index"].isin(map_buses.keys())].index, "index" |
||
| 69 | ].map( |
||
| 70 | map_buses |
||
| 71 | ) |
||
| 72 | gen.loc[:, "bus"] = ( |
||
| 73 | get_foreign_gas_bus_id().loc[gen.loc[:, "index"]].values |
||
| 74 | ) |
||
| 75 | |||
| 76 | gen["p_nom"] = gen["cap_2035"] |
||
| 77 | gen["marginal_cost"] = ( |
||
| 78 | gen["share_LNG_2035"] * scn_params["marginal_cost"]["CH4"] * 1.3 |
||
| 79 | + gen["share_conv_pipe_2035"] * scn_params["marginal_cost"]["CH4"] |
||
| 80 | + gen["share_bio_2035"] * scn_params["marginal_cost"]["biogas"] |
||
| 81 | ) |
||
| 82 | gen["scn_name"] = scn_name |
||
| 83 | |||
| 84 | # Remove useless columns |
||
| 85 | gen = gen.drop( |
||
| 86 | columns=[ |
||
| 87 | "index", |
||
| 88 | "share_LNG_2035", |
||
| 89 | "share_conv_pipe_2035", |
||
| 90 | "share_bio_2035", |
||
| 91 | "cap_2035", |
||
| 92 | ] |
||
| 93 | ) |
||
| 94 | |||
| 95 | # Add missing columns |
||
| 96 | new_id = db.next_etrago_id("generator")
|
||
| 97 | gen["generator_id"] = range(new_id, new_id + len(gen)) |
||
| 98 | |||
| 99 | gen["carrier"] = carrier |
||
| 100 | |||
| 101 | # Insert data to db |
||
| 102 | gen.to_sql( |
||
| 103 | targets["generators"]["table"], |
||
| 104 | db.engine(), |
||
| 105 | schema=targets["generators"]["schema"], |
||
| 106 | index=False, |
||
| 107 | if_exists="append", |
||
| 108 | ) |
||
| 288 |