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

    string = out.getvalue()
    assert string == "\n"
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'
Example #3
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()
Example #4
0
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')
Example #8
0
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")
Example #9
0
#!/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()