Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryBuilder: add support for datetime.date objects in filters #3796

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions aiida/common/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Common password and hash generation functions."""
import datetime
import hashlib
try: # Python > 3.5
from hashlib import blake2b
Expand All @@ -18,7 +19,6 @@
import time
import uuid
from collections import abc, OrderedDict
from datetime import datetime
from functools import singledispatch
from itertools import chain
from operator import itemgetter
Expand Down Expand Up @@ -234,17 +234,23 @@ def _(val, **kwargs):
return [_single_digest('none')]


@_make_hash.register(datetime)
@_make_hash.register(datetime.datetime)
def _(val, **kwargs):
"""hashes the little-endian rep of the float <epoch-seconds>.<subseconds>"""
# see also https://stackoverflow.com/a/8778548 for an excellent elaboration
if val.tzinfo is None or val.utcoffset() is None:
val = val.replace(tzinfo=pytz.utc)
timestamp = val.timestamp()

timestamp = val.timestamp()
return [_single_digest('datetime', float_to_text(timestamp, sig=AIIDA_FLOAT_PRECISION).encode('utf-8'))]


@_make_hash.register(datetime.date)
def _(val, **kwargs):
"""Hashes the string representation in ISO format of the `datetime.date` object."""
return [_single_digest('date', val.isoformat().encode('utf-8'))]


@_make_hash.register(uuid.UUID)
def _(val, **kwargs):
return [_single_digest('uuid', val.bytes)]
Expand Down
10 changes: 10 additions & 0 deletions tests/orm/test_querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def setUp(self):
self.clean_db()
self.insert_data()

def test_date_filters_support(self):
"""Verify that `datetime.date` is supported in filters."""
from datetime import datetime, date, timedelta

orm.Data(ctime=datetime.now() - timedelta(days=3)).store()
orm.Data(ctime=datetime.now() - timedelta(days=1)).store()

builder = orm.QueryBuilder().append(orm.Node, filters={'ctime': {'>': date.today() - timedelta(days=1)}})
self.assertEqual(builder.count(), 1)

def test_ormclass_type_classification(self):
"""
This tests the classifications of the QueryBuilder
Expand Down