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