From 4d691abfa89cebdbd648cb31ceaa0ea0d4ab194a Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Wed, 23 Mar 2016 16:30:20 -0500 Subject: [PATCH 1/2] cqle: Simplify Model __eq__ a little --- cassandra/cqlengine/models.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cassandra/cqlengine/models.py b/cassandra/cqlengine/models.py index 32d9beef..4e71ccaa 100644 --- a/cassandra/cqlengine/models.py +++ b/cassandra/cqlengine/models.py @@ -492,12 +492,7 @@ class BaseModel(object): if keys != other_keys: return False - # check that all of the attributes match - for key in other_keys: - if getattr(self, key, None) != getattr(other, key, None): - return False - - return True + return all(getattr(self, key, None) == getattr(other, key, None) for key in other_keys) def __ne__(self, other): return not self.__eq__(other) From ec868395340eabc9656a58a17d55564581191081 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Wed, 23 Mar 2016 16:30:54 -0500 Subject: [PATCH 2/2] cqle: add the option to truncate datetime values to match DB quantization PYTHON-273 --- cassandra/cqlengine/columns.py | 20 ++++++++++++++++++-- docs/api/cassandra/cqlengine/columns.rst | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cassandra/cqlengine/columns.py b/cassandra/cqlengine/columns.py index 1981287d..c441b8f2 100644 --- a/cassandra/cqlengine/columns.py +++ b/cassandra/cqlengine/columns.py @@ -13,7 +13,7 @@ # limitations under the License. from copy import deepcopy, copy -from datetime import date, datetime +from datetime import date, datetime, timedelta import logging import six from uuid import UUID as _UUID @@ -429,11 +429,27 @@ class DateTime(Column): """ db_type = 'timestamp' + truncate_microseconds = False + """ + Set this ``True`` to have model instances truncate the date, quantizing it in the same way it will be in the database. + This allows equality comparison between assigned values and values read back from the database:: + + DateTime.truncate_microseconds = True + assert Model.create(id=0, d=datetime.utcnow()) == Model.objects(id=0).first() + + Defaults to ``False`` to preserve legacy behavior. May change in the future. + """ + def to_python(self, value): if value is None: return if isinstance(value, datetime): - return value + if DateTime.truncate_microseconds: + us = value.microsecond + truncated_us = us // 1000 * 1000 + return value - timedelta(microseconds=us - truncated_us) + else: + return value elif isinstance(value, date): return datetime(*(value.timetuple()[:6])) diff --git a/docs/api/cassandra/cqlengine/columns.rst b/docs/api/cassandra/cqlengine/columns.rst index 3f83eac5..670633f7 100644 --- a/docs/api/cassandra/cqlengine/columns.rst +++ b/docs/api/cassandra/cqlengine/columns.rst @@ -54,6 +54,8 @@ Columns of all types are initialized by passing :class:`.Column` attributes to t .. autoclass:: DateTime(**kwargs) + .. autoattribute:: truncate_microseconds + .. autoclass:: Decimal(**kwargs) .. autoclass:: Double(**kwargs)