Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Last active September 8, 2016 19:18
Show Gist options
  • Save rectalogic/b01aa5f334eb0636c6582a1088775e6f to your computer and use it in GitHub Desktop.
Save rectalogic/b01aa5f334eb0636c6582a1088775e6f to your computer and use it in GitHub Desktop.
>>> import datetime
>>> import yaml
>>> def now_constructor(loader, node):
... now = datetime.datetime.utcnow()
... # Value is an optional offset in seconds
... value = loader.construct_scalar(node)
... return now + datetime.timedelta(seconds=int(value)) if value else now
...
>>> yaml.add_constructor(u'!now', now_constructor, Loader=yaml.SafeLoader)
>>> print yaml.safe_load("""{"state": "importing", "time_imported": {"$lte": !now -3600}}""")
{'state': 'importing', 'time_imported': {'$lte': datetime.datetime(2016, 9, 8, 18, 18, 6, 662177)}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment