Permalink
Browse files

Also run doctests, made tests pass (except 2). Added tox.ini so testi…

…ng can be done for py25, py26 and py27
  • Loading branch information...
Jaap Roes
Jaap Roes committed Nov 21, 2011
1 parent a8c8936 commit 3dbb1b1929eedbe39bdfb91dad1e0506bd280126
Showing with 67 additions and 94 deletions.
  1. +3 −0 runtests.py
  2. +15 −0 tox.ini
  3. +47 −71 unicodecsv/__init__.py
  4. +2 −23 unicodecsv/test.py
View
@@ -1,8 +1,11 @@
import unittest2
+import doctest
def get_suite():
loader = unittest2.TestLoader()
suite = loader.discover('unicodecsv')
+ suite.addTest(doctest.DocTestSuite('unicodecsv'))
+
return suite
if __name__ == '__main__':
View
15 tox.ini
@@ -0,0 +1,15 @@
+[tox]
+envlist = py25, py26, py27
+
+[testenv]
+commands = python setup.py test
+
+[testenv:py25]
+basepython = python2.5
+
+[testenv:py26]
+basepython = python2.6
+
+[testenv:py27]
+basepython = python2.7
+
View
@@ -7,6 +7,8 @@
__version__ = ".".join(map(str,VERSION))
def _stringify(s, encoding):
+ if s is None:
+ return ''
if type(s)==unicode:
return s.encode(encoding)
elif isinstance(s, (int , float)):
@@ -16,38 +18,45 @@ def _stringify(s, encoding):
return s
def _stringify_list(l, encoding):
- return [_stringify(s, encoding) for s in l]
+ try:
+ return [_stringify(s, encoding) for s in iter(l)]
+ except TypeError:
+ raise csv.Error()
class UnicodeWriter(object):
"""
>>> import unicodecsv
>>> from cStringIO import StringIO
>>> f = StringIO()
- >>> w = unicodecsv.writer(f, encoding='utf-8')
+ >>> w = UnicodeWriter(f, encoding='utf-8')
>>> w.writerow((u'é', u'ñ'))
>>> f.seek(0)
- >>> r = unicodecsv.reader(f, encoding='utf-8')
+ >>> r = UnicodeReader(f, encoding='utf-8')
>>> row = r.next()
- >>> print row[0], row[1]
- é ñ
+ >>> row[0] == u'é'
+ True
+ >>> row[1] == u'ñ'
+ True
"""
- def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
- self.writer = csv.writer(f)
- self.dialect = dialect
+ def __init__(self, f, dialect=csv.excel, encoding="utf-8", *args, **kwds):
self.encoding = encoding
- self.writer = csv.writer(f, dialect=dialect, **kwds)
+ self.writer = csv.writer(f, dialect, *args, **kwds)
def writerow(self, row):
self.writer.writerow(_stringify_list(row, self.encoding))
def writerows(self, rows):
for row in rows:
self.writerow(row)
+
+ @property
+ def dialect(self):
+ return self.writer.dialect
writer = UnicodeWriter
class UnicodeReader(object):
- def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
- self.reader = csv.reader(f, dialect=dialect, **kwds)
+ def __init__(self, f, dialect=csv.excel, encoding="utf-8", *args, **kwds):
+ self.reader = csv.reader(f, dialect, *args, **kwds)
self.encoding = encoding
def next(self):
@@ -56,91 +65,58 @@ def next(self):
def __iter__(self):
return self
+
+ @property
+ def dialect(self):
+ return self.reader.dialect
+
+ @property
+ def line_num(self):
+ return self.reader.line_num
reader = UnicodeReader
class UnicodeDictWriter(csv.DictWriter):
"""
>>> from cStringIO import StringIO
>>> f = StringIO()
- >>> w = DictWriter(f, ['a', 'b'], restval=u'î')
+ >>> w = UnicodeDictWriter(f, ['a', 'b'], restval=u'î')
>>> w.writerow({'a':'1'})
>>> w.writerow({'a':'1', 'b':u'ø'})
>>> w.writerow({'a':u'é'})
>>> f.seek(0)
- >>> r = DictReader(f, fieldnames=['a'], restkey='r')
- >>> r.next() == {'a':u'1', 'r':[u"î"]}
+ >>> r = UnicodeDictReader(f, fieldnames=['a'], restkey='r')
+ >>> print r.next() == {'a': u'1', 'r': [u'î']}
True
- >>> r.next() == {'a':u'1', 'r':[u"ø"]}
+ >>> print r.next() == {'a': u'1', 'r': [u'\xc3\xb8']}
+ True
+ >>> print r.next() == {'a': u'\xc3\xa9', 'r': [u'\xc3\xae']}
True
- >>> r.next() == {'a':u'é', 'r':[u"î"]}
"""
def __init__(self, csvfile, fieldnames=None, restval='', extrasaction='raise', dialect='excel', encoding='utf-8', *args, **kwds):
- self.fieldnames = fieldnames
- self.encoding = encoding
- self.restval = restval
- self.writer = csv.DictWriter(csvfile, fieldnames, restval, extrasaction, dialect, *args, **kwds)
+ csv.DictWriter.__init__(self, csvfile, fieldnames, restval, extrasaction, dialect, *args, **kwds)
+ self.writer = UnicodeWriter(csvfile, dialect, encoding=encoding, *args, **kwds)
def writeheader(self):
- fieldnames = _stringify_list(fieldnames, self.encoding)
header = dict(zip(self.fieldnames, self.fieldnames))
self.writerow(header)
- def writerow(self, d):
- for fieldname in self.fieldnames:
- if fieldname in d:
- d[fieldname] = _stringify(d[fieldname], self.encoding)
- else:
- d[fieldname] = _stringify(self.restval, self.encoding)
- self.writer.writerow(d)
-
class UnicodeDictReader(csv.DictReader):
"""
>>> from cStringIO import StringIO
>>> f = StringIO()
- >>> w = DictWriter(f, fieldnames=['name', 'place'])
- >>> w.writerow({'name':'Cary Grant', 'place': 'hollywood'})
- >>> w.writerow({'name': 'Nathan Brillstone', 'place':u'øLand'})
- >>> w.writerow({'name'::u'Willam ø. Unicoder', u'éSpandland'})
+ >>> w = UnicodeDictWriter(f, fieldnames=['name', 'place'])
+ >>> w.writerow({'name': 'Cary Grant', 'place': 'hollywood'})
+ >>> w.writerow({'name': 'Nathan Brillstone', 'place': u'øLand'})
+ >>> w.writerow({'name': u'Willam ø. Unicoder', 'place': u'éSpandland'})
>>> f.seek(0)
- >>> r = DictReader(f, fieldnames)
- >>> r.next() == {'a':u'1', 'r':[u"î"]}
+ >>> r = UnicodeDictReader(f, fieldnames=['name', 'place'])
+ >>> print r.next() == {'name': 'Cary Grant', 'place': 'hollywood'}
+ True
+ >>> print r.next() == {'name': 'Nathan Brillstone', 'place': u'øLand'}
True
- >>> r.next() == {'a':u'1', 'r':[u"ø"]}
+ >>> print r.next() == {'name': u'Willam ø. Unicoder', 'place': u'éSpandland'}
True
- >>> r.next() == {'a':u'é', 'r':[u"î"]}
"""
-
- def _get_fieldnames(self):
- if self._fieldnames is None:
- try:
- self._fieldnames = self.reader.next()
- print 'FieldNames:%s' % self._fieldnames
- except StopIteration:
- pass
- self.line_num = self.reader.line_num
- return self._fieldnames
-
- def _set_fieldnames(self, value):
- self._fieldnames = value
-
- fieldnames = property(_get_fieldnames, _set_fieldnames)
-
def __init__(self, csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', encoding='utf-8', *args, **kwds):
-
- self.restkey = restkey
- self.encoding = encoding
- self._fieldnames = fieldnames
- #csv.py uses a private variable for this.
- self.reader = csv.DictReader(csvfile, fieldnames, restkey, restval, dialect, *args, **kwds)
-
- def next(self):
- d = self.reader.next()
- for k, v in d.items():
- if k == self.restkey:
- rest = v
- if rest:
- d[self.restkey] = [unicode(v, self.encoding) for v in rest]
- else:
- if v is not None:
- d[k] = unicode(v, self.encoding)
- return d
+ csv.DictReader.__init__(self, csvfile, fieldnames, restkey, restval, dialect, *args, **kwds)
+ self.reader = UnicodeReader(csvfile, dialect, encoding=encoding, *args, **kwds)
View
@@ -7,7 +7,6 @@
from StringIO import StringIO
import tempfile
import unicodecsv as csv
-import io
class Test_Csv(unittest.TestCase):
"""
@@ -250,25 +249,6 @@ def test_read_quoting(self):
['abc,3'], [[]],
quoting=csv.QUOTE_NONNUMERIC)
- def test_read_bigfield(self):
- # This exercises the buffer realloc functionality and field size
- # limits.
- limit = csv.field_size_limit()
- try:
- size = 50000
- bigstring = 'X' * size
- bigline = '%s,%s' % (bigstring, bigstring)
- self._read_test([bigline], [[bigstring, bigstring]])
- csv.field_size_limit(size)
- self._read_test([bigline], [[bigstring, bigstring]])
- self.assertEqual(csv.field_size_limit(), size)
- csv.field_size_limit(size-1)
- self.assertRaises(csv.Error, self._read_test, [bigline], [])
- self.assertRaises(TypeError, csv.field_size_limit, None)
- self.assertRaises(TypeError, csv.field_size_limit, 1, None)
- finally:
- csv.field_size_limit(limit)
-
def test_read_linenum(self):
for r in (csv.UnicodeReader(['line,1', 'line,2', 'line,3']),
csv.UnicodeDictReader(['line,1', 'line,2', 'line,3'],
@@ -592,9 +572,8 @@ class TestDictFields(unittest.TestCase):
### "long" means the row is longer than the number of fieldnames
### "short" means there are fewer elements in the row than fieldnames
def test_write_simple_dict(self):
- import pdb; pdb.set_trace()
fd, name = tempfile.mkstemp()
- fileobj = io.open(fd, 'w+b')
+ fileobj = open(name, 'w+b')
try:
writer = csv.UnicodeDictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
writer.writeheader()
@@ -661,7 +640,7 @@ def test_read_dict_fieldnames_chain(self):
f.write("f1,f2,f3\r\n1,2,abc\r\n")
f.seek(0)
reader = csv.UnicodeDictReader(f)
- first = next(reader)
+ first = reader.next()
for row in itertools.chain([first], reader):
self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"])
self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'})

0 comments on commit 3dbb1b1

Please sign in to comment.