| Conditions | 3 | 
| Total Lines | 101 | 
| Code Lines | 65 | 
| 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 | """The central module containing all code dealing with importing OSM data. | ||
| 107 | def add_metadata(): | ||
| 108 | """Writes metadata JSON string into table comment.""" | ||
| 109 | # Prepare variables | ||
| 110 | osm_config = egon.data.config.datasets()["openstreetmap"] | ||
| 111 | |||
| 112 | if settings()["egon-data"]["--dataset-boundary"] == "Everything": | ||
| 113 | osm_url = osm_config["original_data"]["source"]["url"] | ||
| 114 | target_path = osm_config["original_data"]["target"]["path"] | ||
| 115 | else: | ||
| 116 | osm_url = osm_config["original_data"]["source"]["url_testmode"] | ||
| 117 | target_path = osm_config["original_data"]["target"]["path_testmode"] | ||
| 118 |     spatial_and_date = Path(target_path).name.split("-") | ||
| 119 | spatial_extend = spatial_and_date[0] | ||
| 120 | osm_data_date = ( | ||
| 121 | "20" | ||
| 122 | + spatial_and_date[1][0:2] | ||
| 123 | + "-" | ||
| 124 | + spatial_and_date[1][2:4] | ||
| 125 | + "-" | ||
| 126 | + spatial_and_date[1][4:6] | ||
| 127 | ) | ||
| 128 | |||
| 129 | # Insert metadata for each table | ||
| 130 | licenses = [ | ||
| 131 |         { | ||
| 132 | "name": "Open Data Commons Open Database License 1.0", | ||
| 133 | "title": "", | ||
| 134 | "path": "https://opendatacommons.org/licenses/odbl/1.0/", | ||
| 135 | "instruction": ( | ||
| 136 | "You are free: To Share, To Create, To Adapt;" | ||
| 137 | " As long as you: Attribute, Share-Alike, Keep open!" | ||
| 138 | ), | ||
| 139 | "attribution": "© Reiner Lemoine Institut", | ||
| 140 | } | ||
| 141 | ] | ||
| 142 | for table in osm_config["processed"]["tables"]: | ||
| 143 |         table_suffix = table.split("_")[1] | ||
| 144 |         meta = { | ||
| 145 |             "title": f"OpenStreetMap (OSM) - Germany - {table_suffix}", | ||
| 146 | "description": ( | ||
| 147 | "OpenStreetMap is a free, editable map of the" | ||
| 148 | " whole world that is being built by volunteers" | ||
| 149 | " largely from scratch and released with" | ||
| 150 | " an open-content license." | ||
| 151 | ), | ||
| 152 | "language": ["EN", "DE"], | ||
| 153 |             "spatial": { | ||
| 154 | "location": "", | ||
| 155 |                 "extent": f"{spatial_extend}", | ||
| 156 | "resolution": "", | ||
| 157 | }, | ||
| 158 |             "temporal": { | ||
| 159 |                 "referenceDate": f"{osm_data_date}", | ||
| 160 |                 "timeseries": { | ||
| 161 | "start": "", | ||
| 162 | "end": "", | ||
| 163 | "resolution": "", | ||
| 164 | "alignment": "", | ||
| 165 | "aggregationType": "", | ||
| 166 | }, | ||
| 167 | }, | ||
| 168 | "sources": [ | ||
| 169 |                 { | ||
| 170 | "title": ( | ||
| 171 | "Geofabrik - Download - OpenStreetMap Data Extracts" | ||
| 172 | ), | ||
| 173 | "description": ( | ||
| 174 | 'Data dump taken on "referenceDate",' | ||
| 175 |                         f" i.e. {osm_data_date}." | ||
| 176 | " A subset of this is selected using osm2pgsql" | ||
| 177 | ' using the style file "oedb.style".' | ||
| 178 | ), | ||
| 179 |                     "path": f"{osm_url}", | ||
| 180 | "licenses": licenses, | ||
| 181 | } | ||
| 182 | ], | ||
| 183 | "licenses": licenses, | ||
| 184 | "contributors": [ | ||
| 185 |                 { | ||
| 186 | "title": "Guido Pleßmann", | ||
| 187 | "email": "http://github.com/gplssm", | ||
| 188 |                     "date": time.strftime("%Y-%m-%d"), | ||
| 189 | "object": "", | ||
| 190 | "comment": "Imported data", | ||
| 191 | } | ||
| 192 | ], | ||
| 193 |             "metaMetadata": { | ||
| 194 | "metadataVersion": "OEP-1.4.0", | ||
| 195 |                 "metadataLicense": { | ||
| 196 | "name": "CC0-1.0", | ||
| 197 | "title": "Creative Commons Zero v1.0 Universal", | ||
| 198 | "path": ( | ||
| 199 | "https://creativecommons.org/publicdomain/zero/1.0/" | ||
| 200 | ), | ||
| 201 | }, | ||
| 202 | }, | ||
| 203 | } | ||
| 204 | |||
| 205 | meta_json = "'" + json.dumps(meta) + "'" | ||
| 206 | |||
| 207 | db.submit_comment(meta_json, "openstreetmap", table) | ||
| 208 |