Skip to content

Instantly share code, notes, and snippets.

@pudo
Created October 4, 2011 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudo/1261320 to your computer and use it in GitHub Desktop.
Save pudo/1261320 to your computer and use it in GitHub Desktop.
class DateAttributeType(AttributeType):
""" Date parsing. """
# TODO: simplify this, its hell!
SUFFIX = ('in the format "yyyy-mm-dd", "yyyy-mm" or "yyyy", '
'e.g. "2011-12-31".')
def test(self, row, meta):
value = unicode(self._column_or_default(row, meta))
end_value = row.get(meta.get('end_column'))
for value, is_end in [(value, False), (end_value, True)]:
column = meta.get('end_column' if is_end else 'column')
if meta['dimension'] != 'time':
msg = '"%s" can be empty or a value %s' % (column, self.SUFFIX)
if not value:
continue
else:
if is_end and not value:
continue
msg = ('"time" (here "%s") has to be %s. The "end_column", if specified, '
'might be empty') % (value, self.SUFFIX)
try:
for_datestrings(value)
except:
return msg
return True
def cast(self, row, meta):
value = unicode(self._column_or_default(row, meta))
end_value = row.get(meta.get('end_column'))
# TODO: former implementation had the following logic which
# this does not fully reproduce:
#if not value or value == PLACEHOLDER:
# if not default:
# return EMPTY_DATE
# else:
# value = default
if not value:
value = EMPTY_DATE
return for_datestrings(value, end_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment