Skip to content

Commit

Permalink
[#2375] Cast integer-looking strings to ints, not floats.
Browse files Browse the repository at this point in the history
Minor improvement to the `format_resource_items` helper function
to case int-like strings to integers, not floats.
  • Loading branch information
icmurray committed Sep 13, 2012
1 parent c2b604e commit af2b29a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -1273,7 +1273,8 @@ def format_resource_items(items):
output = []
# regular expressions for detecting types in strings
reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$'
reg_ex_number = '^-?\d{1,}\.?\d*$' # int/float
reg_ex_int = '^-?\d{1,}$'
reg_ex_float = '^-?\d{1,}\.\d{1,}$'
for key, value in items:
if not value or key in blacklist:
continue
Expand All @@ -1285,8 +1286,10 @@ def format_resource_items(items):
if re.search(reg_ex_datetime, value):
datetime_ = date_str_to_datetime(value)
value = formatters.localised_nice_date(datetime_)
elif re.search(reg_ex_number, value):
elif re.search(reg_ex_float, value):
value = formatters.localised_number(float(value))
elif re.search(reg_ex_int, value):
value = formatters.localised_number(int(value))
elif isinstance(value, int) or isinstance(value, float):
value = formatters.localised_number(value)
key = key.replace('_', ' ')
Expand Down

0 comments on commit af2b29a

Please sign in to comment.