| Conditions | 4 |
| Total Lines | 72 |
| Code Lines | 41 |
| 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 | """ |
||
| 43 | def import_scn_status2019(): |
||
| 44 | """ |
||
| 45 | Read and import the scenario status2019 and import it into db |
||
| 46 | |||
| 47 | Parameters |
||
| 48 | ---------- |
||
| 49 | *No parameters required |
||
| 50 | |||
| 51 | """ |
||
| 52 | |||
| 53 | if "status2019" in config.settings()["egon-data"]["--scenarios"]: |
||
| 54 | print("Scenario status2019 was created within the pipeline. " |
||
| 55 | "It is not imported from zenodo.") |
||
| 56 | return |
||
| 57 | |||
| 58 | # Connect to the data base |
||
| 59 | con = db.engine() |
||
| 60 | |||
| 61 | # Clean existing data for status2019 |
||
| 62 | tables = pd.read_sql( |
||
| 63 | """ |
||
| 64 | SELECT tablename FROM pg_catalog.pg_tables |
||
| 65 | WHERE schemaname = 'grid' |
||
| 66 | """, |
||
| 67 | con, |
||
| 68 | ) |
||
| 69 | |||
| 70 | tables = tables[ |
||
| 71 | ~tables["tablename"].isin( |
||
| 72 | [ |
||
| 73 | "egon_etrago_carrier", |
||
| 74 | "egon_etrago_temp_resolution", |
||
| 75 | ] |
||
| 76 | ) |
||
| 77 | ] |
||
| 78 | |||
| 79 | for table in tables["tablename"]: |
||
| 80 | db.execute_sql( |
||
| 81 | f""" |
||
| 82 | DELETE FROM grid.{table} WHERE scn_name = 'status2019'; |
||
| 83 | """ |
||
| 84 | ) |
||
| 85 | |||
| 86 | config_data = config.settings()["egon-data"] |
||
| 87 | database = config_data["--database-name"] |
||
| 88 | host = config_data["--database-host"] |
||
| 89 | port = config_data["--database-port"] |
||
| 90 | user = config_data["--database-user"] |
||
| 91 | |||
| 92 | my_env = os.environ.copy() |
||
| 93 | my_env["PGPASSWORD"] = config_data["--database-password"] |
||
| 94 | |||
| 95 | for table in tables["tablename"]: |
||
| 96 | subprocess.Popen( |
||
| 97 | [ |
||
| 98 | "pg_restore", |
||
| 99 | "-d", |
||
| 100 | database, |
||
| 101 | "--host", |
||
| 102 | host, |
||
| 103 | "--port", |
||
| 104 | port, |
||
| 105 | "-U", |
||
| 106 | user, |
||
| 107 | "-a", |
||
| 108 | "--single-transaction", |
||
| 109 | f"--table={table}", |
||
| 110 | "PoWerD_status2019.backup", |
||
| 111 | ], |
||
| 112 | env=my_env, |
||
| 113 | ) |
||
| 114 | return |
||
| 115 |