Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit fbf07e65333bb8fc118b536e069dc10a97cfa227 1 parent 7771436
apenwarr authored June 30, 2010

Showing 2 changed files with 29 additions and 20 deletions. Show diff stats Hide diff stats

  1. 31  t/twvcsv.py
  2. 18  wvcsv.py
31  t/twvcsv.py
... ...
@@ -1,5 +1,6 @@
1 1
 from wvtest import *
2 2
 import wvcsv
  3
+from wvcsv import _wvcsv
3 4
 
4 5
 import sys
5 6
 
@@ -21,21 +22,21 @@ def test_dequote():
21 22
 
22 23
 @wvtest
23 24
 def test_splitline():
24  
-    WVPASSEQ(wvcsv.splitline(","), (None, None))
25  
-    WVPASSEQ(wvcsv.splitline(",def"), (None, 'def'))
26  
-    WVPASSEQ(wvcsv.splitline("abc,def"), ('abc', 'def'))
27  
-    WVPASSEQ(wvcsv.splitline('"abc,",def'), ('abc,', 'def'))
28  
-    WVPASSEQ(wvcsv.splitline('"a""bc,",def'), ('a"bc,', 'def'))
29  
-    WVPASSEQ(wvcsv.splitline('"a""bc,",",def"'), ('a"bc,', ',def'))
30  
-    WVPASSEQ(wvcsv.splitline('"a""bc,",",def"""'), ('a"bc,', ',def"'))
31  
-    WVPASSEQ(wvcsv.splitline('"a""bc,",,",def"""'), ('a"bc,', None, ',def"'))
  25
+    WVPASSEQ(_wvcsv.splitline(","), (None, None))
  26
+    WVPASSEQ(_wvcsv.splitline(",def"), (None, 'def'))
  27
+    WVPASSEQ(_wvcsv.splitline("abc,def"), ('abc', 'def'))
  28
+    WVPASSEQ(_wvcsv.splitline('"abc,",def'), ('abc,', 'def'))
  29
+    WVPASSEQ(_wvcsv.splitline('"a""bc,",def'), ('a"bc,', 'def'))
  30
+    WVPASSEQ(_wvcsv.splitline('"a""bc,",",def"'), ('a"bc,', ',def'))
  31
+    WVPASSEQ(_wvcsv.splitline('"a""bc,",",def"""'), ('a"bc,', ',def"'))
  32
+    WVPASSEQ(_wvcsv.splitline('"a""bc,",,",def"""'), ('a"bc,', None, ',def"'))
32 33
 
33 34
 @wvtest
34 35
 def test_getline_from_memory():
35 36
     r = wvcsv.Reader("ABCDEFG,efgh\n\"a\nbcd\",1234")
36 37
     i = iter(r)
37  
-    WVPASSEQ(i.next(), "ABCDEFG,efgh")
38  
-    WVPASSEQ(i.next(), '"a\nbcd",1234')
  38
+    WVPASSEQ(i.next(), ('ABCDEFG','efgh'))
  39
+    WVPASSEQ(i.next(), ('a\nbcd','1234'))
39 40
     try:
40 41
         i.next()
41 42
         WVFAIL("i.next() should have thrown StopIteration")
@@ -46,11 +47,11 @@ def test_getline_from_memory():
46 47
 def test_getline_from_file():
47 48
     r = wvcsv.Reader(open("data/test.csv"))
48 49
     i = iter(r)
49  
-    WVPASSEQ(i.next(), "abc,def")
50  
-    WVPASSEQ(i.next(), 'ghi,"j\nkl"')
51  
-    WVPASSEQ(i.next(), '1234,5678')
52  
-    WVPASSEQ(i.next(), '')
53  
-    WVPASSEQ(i.next(), "9,10")
  50
+    WVPASSEQ(i.next(), ('abc','def'))
  51
+    WVPASSEQ(i.next(), ('ghi', 'j\nkl'))
  52
+    WVPASSEQ(i.next(), ('1234', '5678'))
  53
+    WVPASSEQ(i.next(), (None,))
  54
+    WVPASSEQ(i.next(), ('9', '10'))
54 55
     try:
55 56
         i.next()
56 57
         WVFAIL("i.next() should have thrown StopIteration")
18  wvcsv.py
... ...
@@ -1,15 +1,23 @@
1  
-from _wvcsv import *
  1
+import _wvcsv
  2
+
  3
+dequote = _wvcsv.dequote
  4
+quote = _wvcsv.quote
  5
+
  6
+
  7
+def quotel(v):
  8
+    return ','.join(quote(i) for i in v) + '\r\n'
  9
+
2 10
 
3 11
 class Reader:
4 12
     def __init__(self, data):
5  
-        self.id = setup(data);
  13
+        self.id = _wvcsv.setup(data);
6 14
 
7 15
     def __iter__(self):
8 16
         while True:
9  
-            r = readline(self.id)
  17
+            r = _wvcsv.readline(self.id)
10 18
             if r is None:
11 19
                 break
12  
-            yield r
  20
+            yield _wvcsv.splitline(r)
13 21
 
14 22
     def __del__(self):
15  
-        takedown(self.id)
  23
+        _wvcsv.takedown(self.id)

0 notes on commit fbf07e6

Please sign in to comment.
Something went wrong with that request. Please try again.