Support a testy pro ovvp-format dat
This commit is contained in:
parent
f33be6c2e9
commit
6947131a03
2 changed files with 78 additions and 0 deletions
63
seminar/ovvpfile.py
Normal file
63
seminar/ovvpfile.py
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
class OvvpFile(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.headers = {}
|
||||||
|
self.columns = []
|
||||||
|
self.rows = []
|
||||||
|
|
||||||
|
def to_lines(self):
|
||||||
|
# header
|
||||||
|
for hk in sorted(self.headers.keys()):
|
||||||
|
yield '%s\t%s\n' % (hk, self.headers[hk])
|
||||||
|
yield '\n'
|
||||||
|
# columns
|
||||||
|
yield '\t'.join([c for c in self.columns]) + '\n'
|
||||||
|
# rows
|
||||||
|
for r in self.rows:
|
||||||
|
yield '\t'.join([r[c] for c in self.columns]) + '\n'
|
||||||
|
|
||||||
|
def to_string(self):
|
||||||
|
return ''.join(self.to_lines())
|
||||||
|
|
||||||
|
def parse_from(self, source):
|
||||||
|
"Parse data from file, string or line iterator, overwriting self"
|
||||||
|
if isinstance(source, str) or isinstance(source, unicode):
|
||||||
|
return self.parse_from(source.split('\n'))
|
||||||
|
|
||||||
|
it = iter(source)
|
||||||
|
|
||||||
|
# header
|
||||||
|
self.headers = {}
|
||||||
|
for r in it:
|
||||||
|
if isinstance(r, str):
|
||||||
|
r = r.decode('utf8')
|
||||||
|
if r.endswith('\n'):
|
||||||
|
r = r[:-1]
|
||||||
|
assert isinstance(r, unicode)
|
||||||
|
if r == u"":
|
||||||
|
break
|
||||||
|
k, v = r.split(u'\t', 1)
|
||||||
|
self.headers[k] = v
|
||||||
|
|
||||||
|
# columns
|
||||||
|
r = it.next()
|
||||||
|
self.columns = [cn for cn in r.split(u'\t') if cn != ""]
|
||||||
|
|
||||||
|
# rows
|
||||||
|
self.rows = []
|
||||||
|
for r in it:
|
||||||
|
if not r:
|
||||||
|
break
|
||||||
|
rtup = r.split(u'\t')
|
||||||
|
rdict = {}
|
||||||
|
for ci in range(len(self.columns)):
|
||||||
|
rdict[self.columns[ci]] = rtup[ci]
|
||||||
|
self.rows.append(rdict)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def parse(source):
|
||||||
|
o = OvvpFile()
|
||||||
|
o.parse_from(source)
|
||||||
|
return o
|
|
@ -10,6 +10,7 @@ from django.core.management import call_command
|
||||||
|
|
||||||
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Soustredeni, Nastaveni
|
from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Soustredeni, Nastaveni
|
||||||
from seminar.testutils import create_test_data
|
from seminar.testutils import create_test_data
|
||||||
|
from seminar import ovvpfile
|
||||||
|
|
||||||
class SeminarBasicTests(TestCase):
|
class SeminarBasicTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -58,3 +59,17 @@ class SeminarBasicTests(TestCase):
|
||||||
view = resolve(url)
|
view = resolve(url)
|
||||||
assert view
|
assert view
|
||||||
|
|
||||||
|
def test_ovvpfile(self):
|
||||||
|
filetext = "H1\ta\nH2\tb\tc\n\nx\ty\tz\n0\t1\t2\n3\t4\t5\n"
|
||||||
|
o = ovvpfile.parse(filetext)
|
||||||
|
assert len(o.headers) == 2
|
||||||
|
assert o.headers['H2'] == 'b\tc'
|
||||||
|
|
||||||
|
assert o.columns == ['x','y','z']
|
||||||
|
assert len(o.rows) == 2
|
||||||
|
assert o.rows[0]['z'] == '2'
|
||||||
|
|
||||||
|
t = o.to_string()
|
||||||
|
assert t == filetext
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue