Tomas "Jethro" Pokorny
5 years ago
10 changed files with 119 additions and 84 deletions
@ -0,0 +1,40 @@ |
|||
from django.test import TestCase |
|||
import seminar.treelib as tl |
|||
import seminar.models as m |
|||
|
|||
class SimpleTreeLibTests(TestCase): |
|||
def setUp(self): |
|||
# Vyrobíme pár nějakých Nodů |
|||
self.root = m.CastNode(root=None, first_child=None, succ=None, nadpis="Root") |
|||
self.root.save() |
|||
self.some_node = m.CastNode(root=self.root, first_child=None, succ=None, nadpis="Přetržené") |
|||
self.other_node = m.CastNode(root=self.root, first_child=None, succ=None, nadpis="Dítě") |
|||
self.some_orphan = m.CastNode(root=None, first_child=None, succ=None, nadpis="Ošklivé") |
|||
self.other_orphan = m.CastNode(root=None, first_child=None, succ=None, nadpis="Káčátko") |
|||
|
|||
# Trochu je pospojujeme |
|||
self.root.first_child = self.some_node |
|||
self.some_node.succ = self.other_node |
|||
self.some_orphan.first_child = self.other_orphan |
|||
|
|||
# Všechno uložíme |
|||
self.other_node.save() |
|||
self.some_node.save() |
|||
self.other_orphan.save() |
|||
self.some_orphan.save() |
|||
self.root.save() |
|||
|
|||
def test_safe_functions(self): |
|||
# safe_pred |
|||
self.assertEqual(tl.safe_pred(self.other_node), self.some_node) |
|||
self.assertIsNone(tl.safe_pred(self.root)) |
|||
self.assertIsNone(tl.safe_pred(self.some_node)) |
|||
self.assertIsNone(tl.safe_pred(self.some_orphan)) |
|||
self.assertIsNone(tl.safe_pred(self.other_orphan)) |
|||
|
|||
# safe_father_of_first |
|||
self.assertIsNone(tl.safe_father_of_first(self.root)) |
|||
self.assertEqual(tl.safe_father_of_first(self.some_node), self.root) |
|||
self.assertEqual(tl.safe_father_of_first(self.other_node), self.root) |
|||
self.assertIsNone(tl.safe_father_of_first(self.some_orphan)) |
|||
self.assertEqual(tl.safe_father_of_first(self.other_orphan), self.some_orphan) |
@ -1,62 +0,0 @@ |
|||
import csv, codecs |
|||
from io import StringIO |
|||
|
|||
class UTF8Recoder: |
|||
""" |
|||
Iterator that reads an encoded stream and reencodes the input to UTF-8 |
|||
""" |
|||
def __init__(self, f, encoding): |
|||
self.reader = codecs.getreader(encoding)(f) |
|||
|
|||
def __iter__(self): |
|||
return self |
|||
|
|||
def next(self): |
|||
return self.reader.next().encode("utf-8") |
|||
|
|||
class UnicodeReader: |
|||
""" |
|||
A CSV reader which will iterate over lines in the CSV file "f", |
|||
which is encoded in the given encoding. |
|||
""" |
|||
|
|||
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): |
|||
f = UTF8Recoder(f, encoding) |
|||
self.reader = csv.reader(f, dialect=dialect, **kwds) |
|||
|
|||
def next(self): |
|||
row = self.reader.next() |
|||
return [unicode(s, "utf-8") for s in row] |
|||
|
|||
def __iter__(self): |
|||
return self |
|||
|
|||
class UnicodeWriter: |
|||
""" |
|||
A CSV writer which will write rows to CSV file "f", |
|||
which is encoded in the given encoding. |
|||
""" |
|||
|
|||
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): |
|||
# Redirect output to a queue |
|||
self.queue = cStringIO.StringIO() |
|||
self.writer = csv.writer(self.queue, dialect=dialect, **kwds) |
|||
self.stream = f |
|||
self.encoder = codecs.getincrementalencoder(encoding)() |
|||
|
|||
def writerow(self, row): |
|||
self.writer.writerow([s.encode("utf-8") for s in row]) |
|||
# Fetch UTF-8 output from the queue ... |
|||
data = self.queue.getvalue() |
|||
data = data.decode("utf-8") |
|||
# ... and reencode it into the target encoding |
|||
data = self.encoder.encode(data) |
|||
# write to the target stream |
|||
self.stream.write(data) |
|||
# empty queue |
|||
self.queue.truncate(0) |
|||
|
|||
def writerows(self, rows): |
|||
for row in rows: |
|||
self.writerow(row) |
|||
|
Loading…
Reference in new issue