Skip to content

Commit

Permalink
fix: Number.csvify returns a Decimal/None, not str. Table.to_csv work…
Browse files Browse the repository at this point in the history
…s with quoting=csv.QUOTE_NONNUMERIC.
  • Loading branch information
jpmckinney committed Apr 28, 2024
1 parent 0fa340e commit 4c1d47c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
1.10.0 - April 27. 2024
1.10.1 - April 27, 2024
-----------------------

- fix: :meth:`.Number.csvify` returns a ``Decimal`` (or ``None``), instead of ``str``. :meth:`.Table.to_csv` with ``quoting=csv.QUOTE_NONNUMERIC`` now works.

1.10.0 - April 27, 2024
-----------------------

- feat: :meth:`.Table.from_csv` reads the file line by line. If ``sniff_limit=None``, it reads the file into memory once, instead of twice. If ``column_types`` is a :class:`.TypeTester`, it reads the file into memory. (#778)
Expand Down
3 changes: 3 additions & 0 deletions agate/data_types/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def cast(self, d):

raise CastError('Can not parse value "%s" as Decimal.' % d)

def csvify(self, d):
return d

def jsonify(self, d):
if d is None:
return d
Expand Down
4 changes: 4 additions & 0 deletions examples/test_quote_nonnumeric.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"number","text","boolean","date","datetime","timedelta"
1,"a","True","2015-11-04","2015-11-04T12:22:00","0:04:15"
2,"👍","False","2015-11-05","2015-11-04T12:45:00","0:06:18"
"","b","","","",""
16 changes: 16 additions & 0 deletions tests/test_table/test_to_csv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import os
import sys
from io import StringIO
Expand All @@ -23,6 +24,21 @@ def setUp(self):
Number(), Text(), Boolean(), Date(), DateTime(), TimeDelta()
]

def test_to_csv_quote_nonnumeric(self):
table = Table(self.rows, self.column_names, self.column_types)

table.to_csv('.test.csv', quoting=csv.QUOTE_NONNUMERIC)

with open('.test.csv') as f:
contents1 = f.read()

with open('examples/test_quote_nonnumeric.csv') as f:
contents2 = f.read()

self.assertEqual(contents1, contents2)

os.remove('.test.csv')

def test_to_csv(self):
table = Table(self.rows, self.column_names, self.column_types)

Expand Down

0 comments on commit 4c1d47c

Please sign in to comment.