Skip to content
Browse files

* Bug fix: Skip invalid or empty cells on conversion to daviz-json in…

…stead of falling back to 0 [voineali refs #5453]
  • Loading branch information...
1 parent d0fc801 commit 69fba9b33403d96a3f07e42fb1884dc80c7d4eb7 voineali committed Aug 16, 2012
View
3 docs/HISTORY.txt
@@ -3,6 +3,9 @@ Changelog
4.7-dev - (unreleased)
----------------------
+* Bug fix: Skip invalid or empty cells on conversion to daviz-json instead
+ of falling back to 0.
+ [voineali refs #5453]
4.6 - (2012-08-10)
------------------
View
21 eea/app/visualization/converter/converter.py
@@ -212,19 +212,14 @@ def __call__(self, datafile, column_types=None):
fmt = '%Y-%m-%d'
util = queryUtility(IGuessType, name=typo)
- fallback = None
- if typo in ('latitude', 'longitude', 'number'):
- fallback = 0
- elif typo in ('boolean',):
- fallback = False
- elif typo in ('latlong',):
- fallback = '0, 0'
- elif typo in ('date',):
- fallback = '1970-01-01'
-
- text = (util.convert(text, fallback=fallback, format=fmt)
- if util else text)
- data[col] = text
+ try:
+ text = (util.convert(text, fallback=None, format=fmt)
+ if util else text)
+ except ValueError, err:
+ # Skip key: value from JSON
+ logger.debug(err)
+ else:
+ data[col] = text
properties[col] = {"valueType": valueType, "order": order}
order += 1
View
13 eea/app/visualization/converter/types/boolean.py
@@ -36,8 +36,6 @@ def convert(self, text, fallback=None, **options):
True
>>> guess.convert('n')
False
- >>> guess.convert('Oui')
- 'Oui'
You can use fallback to force text to boolean:
@@ -46,6 +44,14 @@ def convert(self, text, fallback=None, **options):
>>> guess.convert('Oui', fallback=bool)
True
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('Oui')
+ Traceback (most recent call last):
+ ...
+ ValueError: Oui
+
"""
res = text.strip().lower()
@@ -58,7 +64,8 @@ def convert(self, text, fallback=None, **options):
if callable(fallback):
return fallback(text)
return fallback
- return text
+ else:
+ raise ValueError(text)
def __call__(self, text, label=''):
"""
View
13 eea/app/visualization/converter/types/date.py
@@ -35,9 +35,6 @@ def convert(self, text, fallback=None, **options):
>>> guess.convert('Jan 1973')
datetime.datetime(1973, 1...)
- >>> guess.convert('1 Ianuarie 2012')
- '1 Ianuarie 2012'
-
You can use fallback to force text to datetime:
>>> from datetime import datetime
@@ -49,6 +46,14 @@ def convert(self, text, fallback=None, **options):
... fallback=lambda x: parse(x.replace('Ianuarie', 'Jan')))
datetime.datetime(2012, 1, 1, 0, 0)
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('1 Ianuarie 2012')
+ Traceback (most recent call last):
+ ...
+ ValueError: 1 Ianuarie 2012
+
You can also convert given text to another text providing 'format'
keyword:
@@ -64,6 +69,8 @@ def convert(self, text, fallback=None, **options):
text = fallback(text)
else:
text = fallback
+ else:
+ raise ValueError(text)
if not isinstance(text, datetime):
return text
View
10 eea/app/visualization/converter/types/latitude.py
@@ -33,6 +33,14 @@ def convert(self, text, fallback=None, **options):
... fallback=lambda x: float(x.strip('.')))
42.345...
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('A latitude: 23.45')
+ Traceback (most recent call last):
+ ...
+ ValueError: A latitude: 23.45
+
You can also convert given text to another text providing 'format'
keyword:
@@ -48,6 +56,8 @@ def convert(self, text, fallback=None, **options):
text = fallback(text)
else:
text = fallback
+ else:
+ raise ValueError(text)
if not isinstance(text, float):
return text
View
10 eea/app/visualization/converter/types/latlong.py
@@ -32,6 +32,14 @@ def convert(self, text, fallback=None, **options):
... fallback=lambda x: x.replace(',', '.').replace(' ', ', '))
'42.3456, 25.345'
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('A latlong: 23.45')
+ Traceback (most recent call last):
+ ...
+ ValueError: A latlong: 23.45
+
You can also convert given text to another text providing 'format'
keyword:
@@ -45,6 +53,8 @@ def convert(self, text, fallback=None, **options):
if callable(fallback):
return fallback(text)
return fallback
+ else:
+ raise ValueError(text)
lat, lng = res
guess = getUtility(IGuessType, 'latitude')
View
10 eea/app/visualization/converter/types/longitude.py
@@ -30,6 +30,14 @@ def convert(self, text, fallback=None, **options):
... fallback=lambda x: float(x.strip('.')))
42.345...
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('A longitude: 23.45')
+ Traceback (most recent call last):
+ ...
+ ValueError: A longitude: 23.45
+
You can also convert given text to another text providing 'format'
keyword:
@@ -45,6 +53,8 @@ def convert(self, text, fallback=None, **options):
text = fallback(text)
else:
text = fallback
+ else:
+ raise ValueError(text)
if not isinstance(text, float):
return text
View
21 eea/app/visualization/converter/types/number.py
@@ -30,8 +30,6 @@ def convert(self, text, fallback=None, **options):
2.3
>>> print guess.convert('2,3')
2.3
- >>> guess.convert('A number: 23.45')
- 'A number: 23.45'
You can use fallback to force text to number:
@@ -41,6 +39,14 @@ def convert(self, text, fallback=None, **options):
... fallback=lambda x: int(x.replace('.', '')))
234
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert('A number: 23.45')
+ Traceback (most recent call last):
+ ...
+ ValueError: A number: 23.45
+
You can also convert given text to another text providing 'format'
keyword:
@@ -65,11 +71,14 @@ def convert(self, text, fallback=None, **options):
except Exception:
error = True
- if error and fallback is not None:
- if callable(fallback):
- text = fallback(text)
+ if error:
+ if fallback is not None:
+ if callable(fallback):
+ text = fallback(text)
+ else:
+ text = fallback
else:
- text = fallback
+ raise ValueError(text)
if not isinstance(text, (int, float)):
return text
View
22 eea/app/visualization/converter/types/text.py
@@ -30,11 +30,25 @@ def convert(self, text, fallback=None, **options):
>>> guess.convert('', fallback=lambda x: unicode(x))
u''
+ If you don't provide a fallback for a wrong value, a ValueError will be
+ raised:
+
+ >>> guess.convert(None)
+ Traceback (most recent call last):
+ ...
+ ValueError: None
+
"""
- if not text and fallback is not None:
- if callable(fallback):
- return fallback(text)
- return fallback
+ if not text:
+ if fallback is not None:
+ if callable(fallback):
+ return fallback(text)
+ return fallback
+ else:
+ if text == "":
+ return text
+ raise ValueError(text)
+
return text
def __call__(self, text, label=''):

0 comments on commit 69fba9b

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