Created
May 27, 2017 11:14
-
-
Save psd/1d4cc608f543901795f7f4436ef6182a to your computer and use it in GitHub Desktop.
MVP register client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import csv | |
import requests | |
class Records: | |
"Minimal register client" | |
records = {} | |
url = 'https://%s.register.gov.uk/records.tsv?page-size=5000' | |
def load(self, register): | |
url = self.url % (register) | |
resp = requests.get(url=url) | |
resp.raise_for_status() | |
for row in csv.DictReader(io.StringIO(resp.text), delimiter='\t'): | |
self.records['%s:%s' % (register, row[register])] = row | |
def dump(self, fields, sep='\t'): | |
key = fields[0] | |
print(sep.join(fields)) | |
for record in sorted(self.records): | |
row = self.records[record] | |
row[key] = record | |
print(sep.join([str(row[field]) for field in fields])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment