| Conditions | 2 |
| Total Lines | 101 |
| Code Lines | 61 |
| 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 | from urllib.request import urlretrieve |
||
| 125 | def metadata(): |
||
| 126 | """Writes metadata JSON string into table comment.""" |
||
| 127 | # Prepare variables |
||
| 128 | osm_config = egon.data.config.datasets()["openstreetmap"]["original_data"][ |
||
| 129 | "osm" |
||
| 130 | ] |
||
| 131 | spatial_and_date = os.path.basename(osm_config["file"]).split("-") |
||
| 132 | spatial_extend = spatial_and_date[0] |
||
| 133 | osm_data_date = ( |
||
| 134 | "20" |
||
| 135 | + spatial_and_date[1][0:2] |
||
| 136 | + "-" |
||
| 137 | + spatial_and_date[1][2:4] |
||
| 138 | + "-" |
||
| 139 | + spatial_and_date[1][4:6] |
||
| 140 | ) |
||
| 141 | osm_url = osm_config["url"] |
||
| 142 | |||
| 143 | # Insert metadata for each table |
||
| 144 | for table in osm_config["output_tables"]: |
||
| 145 | table_suffix = table.split("_")[1] |
||
| 146 | meta = { |
||
| 147 | "title": f"OpenStreetMap (OSM) - Germany - {table_suffix}", |
||
| 148 | "description": "OpenStreetMap is a free, editable map of the " |
||
| 149 | "whole world that is being built by volunteers " |
||
| 150 | "largely from scratch and released with " |
||
| 151 | "an open-content license.", |
||
| 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": "Geofabrik - Download - OpenStreetMap Data " |
||
| 171 | "Extracts", |
||
| 172 | "description": "Data dump of reference date. Thereof, a " |
||
| 173 | "subset is selected using" |
||
| 174 | "osm2pgsql with oedb.style style file", |
||
| 175 | "path": f"{osm_url}", |
||
| 176 | "licenses": [ |
||
| 177 | { |
||
| 178 | "name": "Open Data Commons Open Database " |
||
| 179 | "License 1.0", |
||
| 180 | "title": "", |
||
| 181 | "path": "https://opendatacommons.org/licenses/" |
||
| 182 | "odbl/1.0/", |
||
| 183 | "instruction": "You are free: To Share, To " |
||
| 184 | "Create, To Adapt; As long as you: " |
||
| 185 | "Attribute, Share-Alike, Keep " |
||
| 186 | "open!", |
||
| 187 | "attribution": "© Reiner Lemoine Institut", |
||
| 188 | } |
||
| 189 | ], |
||
| 190 | } |
||
| 191 | ], |
||
| 192 | "licenses": [ |
||
| 193 | { |
||
| 194 | "name": "Open Data Commons Open Database License 1.0", |
||
| 195 | "title": "", |
||
| 196 | "path": "https://opendatacommons.org/licenses/odbl/1.0/", |
||
| 197 | "instruction": "You are free: To Share, To Create, To " |
||
| 198 | "Adapt; As long as you: Attribute, " |
||
| 199 | "Share-Alike, Keep open!", |
||
| 200 | "attribution": "© Reiner Lemoine Institut", |
||
| 201 | } |
||
| 202 | ], |
||
| 203 | "contributors": [ |
||
| 204 | { |
||
| 205 | "title": "Guido Pleßmann", |
||
| 206 | "email": "http://github.com/gplssm", |
||
| 207 | "date": time.strftime("%Y-%m-%d"), |
||
| 208 | "object": "", |
||
| 209 | "comment": "Imported data", |
||
| 210 | } |
||
| 211 | ], |
||
| 212 | "metaMetadata": { |
||
| 213 | "metadataVersion": "OEP-1.4.0", |
||
| 214 | "metadataLicense": { |
||
| 215 | "name": "CC0-1.0", |
||
| 216 | "title": "Creative Commons Zero v1.0 Universal", |
||
| 217 | "path": "https://creativecommons.org/publicdomain/" |
||
| 218 | "zero/1.0/", |
||
| 219 | }, |
||
| 220 | }, |
||
| 221 | } |
||
| 222 | |||
| 223 | meta_json = "'" + json.dumps(meta) + "'" |
||
| 224 | |||
| 225 | db.submit_comment(meta_json, "openstreetmap", table) |
||
| 226 |