Permalink
Browse files

wvcsv.py: clean up the API for quoting and iterating.

wvcsv.Reader() now yields a series of tuples, so you don't need to call
wvcsv.splitline() manually.  We can thus hide a bunch of functions back in
_wvcsv so people know not to depend on them.

Add wvcsv.quotel(), which quotes an entire list of objects and returns a
line (including terminating \r\n) suitable for writing to a .csv file.
  • Loading branch information...
1 parent 7771436 commit fbf07e65333bb8fc118b536e069dc10a97cfa227 @apenwarr committed Jul 1, 2010
Showing with 29 additions and 20 deletions.
  1. +16 −15 t/twvcsv.py
  2. +13 −5 wvcsv.py
View
31 t/twvcsv.py
@@ -1,5 +1,6 @@
from wvtest import *
import wvcsv
+from wvcsv import _wvcsv
import sys
@@ -21,21 +22,21 @@ def test_dequote():
@wvtest
def test_splitline():
- WVPASSEQ(wvcsv.splitline(","), (None, None))
- WVPASSEQ(wvcsv.splitline(",def"), (None, 'def'))
- WVPASSEQ(wvcsv.splitline("abc,def"), ('abc', 'def'))
- WVPASSEQ(wvcsv.splitline('"abc,",def'), ('abc,', 'def'))
- WVPASSEQ(wvcsv.splitline('"a""bc,",def'), ('a"bc,', 'def'))
- WVPASSEQ(wvcsv.splitline('"a""bc,",",def"'), ('a"bc,', ',def'))
- WVPASSEQ(wvcsv.splitline('"a""bc,",",def"""'), ('a"bc,', ',def"'))
- WVPASSEQ(wvcsv.splitline('"a""bc,",,",def"""'), ('a"bc,', None, ',def"'))
+ WVPASSEQ(_wvcsv.splitline(","), (None, None))
+ WVPASSEQ(_wvcsv.splitline(",def"), (None, 'def'))
+ WVPASSEQ(_wvcsv.splitline("abc,def"), ('abc', 'def'))
+ WVPASSEQ(_wvcsv.splitline('"abc,",def'), ('abc,', 'def'))
+ WVPASSEQ(_wvcsv.splitline('"a""bc,",def'), ('a"bc,', 'def'))
+ WVPASSEQ(_wvcsv.splitline('"a""bc,",",def"'), ('a"bc,', ',def'))
+ WVPASSEQ(_wvcsv.splitline('"a""bc,",",def"""'), ('a"bc,', ',def"'))
+ WVPASSEQ(_wvcsv.splitline('"a""bc,",,",def"""'), ('a"bc,', None, ',def"'))
@wvtest
def test_getline_from_memory():
r = wvcsv.Reader("ABCDEFG,efgh\n\"a\nbcd\",1234")
i = iter(r)
- WVPASSEQ(i.next(), "ABCDEFG,efgh")
- WVPASSEQ(i.next(), '"a\nbcd",1234')
+ WVPASSEQ(i.next(), ('ABCDEFG','efgh'))
+ WVPASSEQ(i.next(), ('a\nbcd','1234'))
try:
i.next()
WVFAIL("i.next() should have thrown StopIteration")
@@ -46,11 +47,11 @@ def test_getline_from_memory():
def test_getline_from_file():
r = wvcsv.Reader(open("data/test.csv"))
i = iter(r)
- WVPASSEQ(i.next(), "abc,def")
- WVPASSEQ(i.next(), 'ghi,"j\nkl"')
- WVPASSEQ(i.next(), '1234,5678')
- WVPASSEQ(i.next(), '')
- WVPASSEQ(i.next(), "9,10")
+ WVPASSEQ(i.next(), ('abc','def'))
+ WVPASSEQ(i.next(), ('ghi', 'j\nkl'))
+ WVPASSEQ(i.next(), ('1234', '5678'))
+ WVPASSEQ(i.next(), (None,))
+ WVPASSEQ(i.next(), ('9', '10'))
try:
i.next()
WVFAIL("i.next() should have thrown StopIteration")
View
18 wvcsv.py
@@ -1,15 +1,23 @@
-from _wvcsv import *
+import _wvcsv
+
+dequote = _wvcsv.dequote
+quote = _wvcsv.quote
+
+
+def quotel(v):
+ return ','.join(quote(i) for i in v) + '\r\n'
+
class Reader:
def __init__(self, data):
- self.id = setup(data);
+ self.id = _wvcsv.setup(data);
def __iter__(self):
while True:
- r = readline(self.id)
+ r = _wvcsv.readline(self.id)
if r is None:
break
- yield r
+ yield _wvcsv.splitline(r)
def __del__(self):
- takedown(self.id)
+ _wvcsv.takedown(self.id)

0 comments on commit fbf07e6

Please sign in to comment.