Skip to content

Commit

Permalink
[dtypes] Default value use on Empty and None
Browse files Browse the repository at this point in the history
Closes G-Node#245
  • Loading branch information
mpsonntag committed Mar 16, 2018
1 parent fee1703 commit de1e0b4
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions odml/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def float_get(string):


def str_get(string):
if not string:
return default_values("string")
if sys.version_info < (3, 0):
return unicode(string)
return str(string)
Expand All @@ -145,7 +147,7 @@ def str_get(string):

def time_get(string):
if not string:
return None
return default_values("time")
if type(string) is datetime.time:
return datetime.datetime.strptime(string.strftime('%H:%M:%S'),
'%H:%M:%S').time()
Expand All @@ -155,15 +157,15 @@ def time_get(string):

def time_set(value):
if not value:
return None
return default_values("time")
if type(value) is datetime.time:
return value.strftime("%H:%M:%S")
return value.isoformat()


def date_get(string):
if not string:
return None
return default_values("date")
if type(string) is datetime.date:
return datetime.datetime.strptime(string.isoformat(),
'%Y-%m-%d').date()
Expand All @@ -176,7 +178,7 @@ def date_get(string):

def datetime_get(string):
if not string:
return None
return default_values("datetime")
if type(string) is datetime.datetime:
return datetime.datetime.strptime(string.strftime('%Y-%m-%d %H:%M:%S'),
'%Y-%m-%d %H:%M:%S')
Expand All @@ -186,16 +188,16 @@ def datetime_get(string):

def datetime_set(value):
if not value:
return None
return default_values("datetime")
if type(value) is datetime.datetime:
return value.strftime('%Y-%m-%d %H:%M:%S')
else:
return datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S')


def boolean_get(string):
if string is None:
return None
if not string:
return default_values("boolean")
if type(string) in (unicode, str):
string = string.lower()
truth = ["true", "1", True, "t"] # be kind, spec only accepts True / False
Expand All @@ -209,6 +211,7 @@ def boolean_get(string):

# Alias boolean_set to boolean_get. Both perform same function.


boolean_set = boolean_get
bool_get = boolean_get
bool_set = boolean_set
Expand Down

0 comments on commit de1e0b4

Please sign in to comment.