def test_writer_zero_items():
    out = io.StringIO()
    writer = Writer(out, fieldnames=[])
    writer.close()

    string = out.getvalue()
    assert string == "\n"
コード例 #2
0
class PostcodeWriter():
    def __init__(self,
                 dirname="data/postcode",
                 fieldnames=['postcode', 'point']):
        self.dirname = dirname
        self.fieldnames = fieldnames
        self.prefix = None
        self.writer = None

    def write(self, item):
        prefix = item.postcode[:2]
        if self.prefix != prefix:
            self.close()
            self.open(prefix)
        self.writer.write(item)

    def open(self, prefix):
        self.prefix = prefix
        filename = os.path.join(self.dirname, self.prefix + '.tsv')
        self.writer = Writer(open(filename, "w"), self.fieldnames)
        print("writing %s .." % filename)

    def close(self):
        if self.writer:
            self.writer.close()
def test_writer_zero_items_titles():
    out = io.StringIO()
    writer = Writer(out, fieldnames=['one', 'two', 'three'])
    writer.close()

    string = out.getvalue()
    assert string == 'one\ttwo\tthree\n'
コード例 #4
0
ファイル: test_writer_tsv.py プロジェクト: psd/openregister
def test_writer_zero_items_titles():
    out = io.StringIO()
    writer = Writer(out, fieldnames=["one", "two", "three"])
    writer.close()

    string = out.getvalue()
    assert string == "one\ttwo\tthree\n"
def test_writer_one_list_item():
    out = io.StringIO()
    writer = Writer(out, fieldnames=["name"])
    item = Item(name=["one", "two", "three"])
    writer.write(item)
    writer.close()

    string = out.getvalue()
    assert string == 'name\none;two;three\n'
def test_writer_one_item():
    out = io.StringIO()
    writer = Writer(out, fieldnames=["name"])
    item = Item(name="one")
    writer.write(item)
    writer.close()

    string = out.getvalue()
    assert string == 'name\none\n'
def test_writer_sparse_items():
    out = io.StringIO()
    writer = Writer(out, fieldnames=["name", "text", "address", "z"])
    for name in ['one', 'two', 'three']:
        item = Item(name=name, text="hello world")
        writer.write(item)
    writer.close()

    string = out.getvalue()
    assert string == ('name\ttext\taddress\tz\n'
                      'one\thello world\t\t\n'
                      'two\thello world\t\t\n'
                      'three\thello world\t\t\n')
コード例 #8
0
ファイル: test_writer_tsv.py プロジェクト: psd/openregister
def test_writer_many_items():
    out = io.StringIO()
    writer = Writer(out, fieldnames=["name", "text"])
    for name in ["one", "two", "three"]:
        item = Item(name=name, text="hello world")
        writer.write(item)
    writer.close()

    string = out.getvalue()
    assert string == ("name\ttext\n"
                      "one\thello world\n"
                      "two\thello world\n"
                      "three\thello world\n")
コード例 #9
0
 def open(self, prefix):
     self.prefix = prefix
     filename = os.path.join(self.dirname, self.prefix + '.tsv')
     self.writer = Writer(open(filename, "w"), self.fieldnames)
     print("writing %s .." % filename)
コード例 #10
0
ファイル: forces.py プロジェクト: openregister/police-data
#!/usr/bin/env python3

import sys
import json
from openregister import Item
from openregister.representations.tsv import Writer

fields = ['police-force', 'name', 'start-date', 'end-date']

writer = Writer(sys.stdout, fieldnames=fields)

for row in json.load(sys.stdin):

    force = Item()
    force['police-force'] = row['id']
    force['name'] = row['name']
    writer.write(force)

writer.close()
コード例 #11
0
        name = name.rstrip()
        companies[name] = company

uprns = {}
with open("data/premises.tsv") as f:
    for line in f:
        (premises, address) = line.split("\t")
        address = address.rstrip()
        uprns[address] = premises


# Country,Competent Authority,Approval Number,Trading name,Address 1,Address 2,Adress 3,Town/Region,Postcode,Section 0,Section I,Section II,Section III,Section IV,Section V,Section VI,Section VII,Section VIII,Section IX,Section X,Section XI,Section XII,Section XIII,Section XIV,Section XV,Section XVI,Species,Remarks,Address withheld,uprn,lat,lng,match_score,match_description

fields = ["food-premises", "premises", "name", "business", "start-date", "end-date"]

writer = Writer(sys.stdout, fields)

reader = csv.DictReader(sys.stdin)
for num, row in enumerate(reader):
    uprn = row["uprn"]
    name = row["Trading name"]

    if uprn and row["Address withheld"] != "Y":

        item = Item()
        item.__dict__["food-premises"] = uprn

        if uprn in uprns:
            item.premises = uprns[uprn]

        if name in companies: