I've been working on a small project for myself, where I use riak, python and flask framework. Like in most cases, I'd like to store creation date of my objects stored in riak, and some other dates as well (like session expiration dates). When I first tried to just store date object, I've ecountered an error, to which the solution was to use a code like this:
def date(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
def json_date():
return json.dumps(datetime.now(), default=date)
I've tested it, and happily used it, but later on I've ecountered some more complex needs, namely session expiration date in N minutes from now, so I've quickly wrote somethin like that:
def json_session_expiration_date():
return json.dumps(datetime.now() + timedelta(minutes=15), default=date)
Now that I've core of my application working, I wanted to clean up stupid and suboptimal code, before I move to writing more features, and one issue I have is with these three functions - I dont clearly understand what's the objective of the date(obj) function, and therefore I cant join them all in one function easily. Any advice is appreciated!