| Total Complexity | 6 |
| Total Lines | 55 |
| Duplicated Lines | 47.27 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | """The central module containing all code dealing with small scale input-data |
||
| 2 | """ |
||
| 3 | |||
| 4 | |||
| 5 | from pathlib import Path |
||
| 6 | from urllib.request import urlretrieve |
||
| 7 | import shutil |
||
| 8 | import zipfile |
||
| 9 | |||
| 10 | from egon.data import config |
||
| 11 | from egon.data.datasets import Dataset |
||
| 12 | |||
| 13 | |||
| 14 | View Code Duplication | def download(): |
|
|
|
|||
| 15 | """ |
||
| 16 | Download small scale input data from Zenodo |
||
| 17 | Parameters |
||
| 18 | ---------- |
||
| 19 | |||
| 20 | """ |
||
| 21 | data_bundle_path = Path(".") / "data_bundle_egon_data" |
||
| 22 | # Delete folder if it already exists |
||
| 23 | if data_bundle_path.exists() and data_bundle_path.is_dir(): |
||
| 24 | shutil.rmtree(data_bundle_path) |
||
| 25 | # Get parameters from config and set download URL |
||
| 26 | sources = config.datasets()["data-bundle"]["sources"]["zenodo"] |
||
| 27 | url = ( |
||
| 28 | f"https://zenodo.org/record/{sources['deposit_id']}/files/" |
||
| 29 | "data_bundle_egon_data.zip" |
||
| 30 | ) |
||
| 31 | target_file = config.datasets()["data-bundle"]["targets"]["file"] |
||
| 32 | |||
| 33 | # check if file exists |
||
| 34 | if not Path(target_file).exists(): |
||
| 35 | # Retrieve files |
||
| 36 | urlretrieve(url, target_file) |
||
| 37 | |||
| 38 | with zipfile.ZipFile(target_file, "r") as zip_ref: |
||
| 39 | zip_ref.extractall(".") |
||
| 40 | |||
| 41 | |||
| 42 | class DataBundle(Dataset): |
||
| 43 | def __init__(self, dependencies): |
||
| 44 | deposit_id = config.datasets()["data-bundle"]["sources"][ |
||
| 45 | "zenodo" |
||
| 46 | ]["deposit_id"] |
||
| 47 | deposit_id_powerd = config.datasets()["data-bundle"]["sources"][ |
||
| 48 | "zenodo" |
||
| 49 | ]["deposit_id"] |
||
| 50 | super().__init__( |
||
| 51 | name="DataBundle", |
||
| 52 | version=f"{deposit_id}-{deposit_id_powerd}-0.0.3", |
||
| 53 | dependencies=dependencies, |
||
| 54 | tasks=(download,), |
||
| 55 | ) |
||
| 56 |