Skip to content

Commit

Permalink
pywvcsv: rename the library to just 'wvcsv'.
Browse files Browse the repository at this point in the history
And eliminate redundant 'csv' prefix on functions inside the wvcsv module,
so that we can avoid doing 'import * from wvcsv'.  Now just 'import wvcsv'
and use wvcsv.Reader(), wvcsv.quote(), etc.
  • Loading branch information
apenwarr committed Jun 26, 2010
1 parent 496d26f commit 8b6601c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 71 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

all: _wvcsv.so

_wvcsv.so: wvcsv.cc _wvcsvmodule.cc setup.py
_wvcsv.so: wvcsv.cc _wvcsv.cc setup.py
@python setup.py build
@cp build/*/_wvcsv.so .

Expand All @@ -16,4 +16,5 @@ test:
./wvtestrun $(MAKE) runtests

clean:
rm -rf build _wvcsv.so *.pyc t/*.pyc .*~ *~
rm -rf build
rm -f *.o *.so *.pyc t/*.pyc .*~ *~
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from wvcsv import *
12 changes: 6 additions & 6 deletions _wvcsvmodule.cc → _wvcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ static PyObject *pywvcsv_readline(PyObject *self, PyObject *args)


static PyMethodDef wvcsv_methods[] = {
{ "csvdequote", pywvcsv_dequote, METH_VARARGS,
{ "dequote", pywvcsv_dequote, METH_VARARGS,
"Dequote one CSV cell." },
{ "csvquote", pywvcsv_quote, METH_VARARGS,
{ "quote", pywvcsv_quote, METH_VARARGS,
"Quote one CSV cell." },
{ "csvsplitline", pywvcsv_splitline, METH_VARARGS,
{ "splitline", pywvcsv_splitline, METH_VARARGS,
"Take one CSV-encoded line and split it, decoding each cell." },
{ "csvreadline", pywvcsv_readline, METH_VARARGS,
{ "readline", pywvcsv_readline, METH_VARARGS,
"Read one CSV-encoded line from preset buffer." },
{ "csvsetup", pywvcsv_setup, METH_VARARGS,
{ "setup", pywvcsv_setup, METH_VARARGS,
"Set up CSV reading stuff." },
{ "csvtakedown", pywvcsv_takedown, METH_VARARGS,
{ "takedown", pywvcsv_takedown, METH_VARARGS,
"Take down CSV reading stuff." },
{ NULL, NULL, 0, NULL }, // sentinel
};
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"/usr/local/include/wvstreams"]

_wvcsv_mod = Extension('_wvcsv',
sources=['_wvcsvmodule.cc',
sources=['_wvcsv.cc',
'wvcsv.cc'],
include_dirs=includes + ['../cli'],
library_dirs=[WVSTREAMSLIB],
Expand Down
58 changes: 0 additions & 58 deletions t/tpywvcsv.py

This file was deleted.

58 changes: 58 additions & 0 deletions t/twvcsv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from wvtest import *
import wvcsv

import sys

@wvtest
def test_quote():
WVPASSEQ(wvcsv.quote(None), "")
WVPASSEQ(wvcsv.quote("test"), "test")
WVPASSEQ(wvcsv.quote("t,est"), '"t,est"')
WVPASSEQ(wvcsv.quote('t,e"st'), '"t,e""st"')

@wvtest
def test_dequote():
WVPASSEQ(wvcsv.dequote(""), None)
WVPASSEQ(wvcsv.dequote('test'), 'test')
WVPASSEQ(wvcsv.dequote('"te,st"'), 'te,st')
WVPASSEQ(wvcsv.dequote('"te,""st"'), 'te,"st')

WVPASSEQ(wvcsv.dequote(wvcsv.quote('"foo,"bar,')), '"foo,"bar,')

@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"'))

@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')
try:
i.next()
WVFAIL("i.next() should have thrown StopIteration")
except StopIteration:
WVPASS("wvcsv.Reader iterator throws StopIteration correctly")

@wvtest
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")
try:
i.next()
WVFAIL("i.next() should have thrown StopIteration")
except StopIteration:
WVPASS("wvcsv.Reader iterator throws StopIteration correctly")
8 changes: 4 additions & 4 deletions wvcsv.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from _wvcsv import *

class CsvReader:
class Reader:
def __init__(self, data):
self.id = csvsetup(data);
self.id = setup(data);

def __iter__(self):
while True:
r = csvreadline(self.id)
r = readline(self.id)
if r is None:
break
yield r

def __del__(self):
csvtakedown(self.id)
takedown(self.id)

0 comments on commit 8b6601c

Please sign in to comment.