Skip to content
This repository was archived by the owner on Aug 5, 2020. It is now read-only.

Commit ce8765a

Browse files
committed
Merge pull request apache#279 from datastax/PYTHON-246
PYTHON-246 - Deprecate cqlengine.Float(double_precision=True)
2 parents 7c03eac + 833feeb commit ce8765a

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

cassandra/cqlengine/columns.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -591,18 +591,9 @@ def to_python(self, value):
591591
return self.validate(value)
592592

593593

594-
class Float(Column):
595-
"""
596-
Stores a floating point value
597-
"""
598-
db_type = 'double'
599-
600-
def __init__(self, double_precision=True, **kwargs):
601-
self.db_type = 'double' if double_precision else 'float'
602-
super(Float, self).__init__(**kwargs)
603-
594+
class BaseFloat(Column):
604595
def validate(self, value):
605-
value = super(Float, self).validate(value)
596+
value = super(BaseFloat, self).validate(value)
606597
if value is None:
607598
return
608599
try:
@@ -617,6 +608,31 @@ def to_database(self, value):
617608
return self.validate(value)
618609

619610

611+
class Float(BaseFloat):
612+
"""
613+
Stores a single-precision floating-point value
614+
"""
615+
db_type = 'float'
616+
617+
def __init__(self, double_precision=None, **kwargs):
618+
if double_precision is None or bool(double_precision):
619+
msg = "Float(double_precision=True) is deprecated. Use Double() type instead."
620+
double_precision = True
621+
warnings.warn(msg, DeprecationWarning)
622+
log.warning(msg)
623+
624+
self.db_type = 'double' if double_precision else 'float'
625+
626+
super(Float, self).__init__(**kwargs)
627+
628+
629+
class Double(BaseFloat):
630+
"""
631+
Stores a double-precision floating-point value
632+
"""
633+
db_type = 'double'
634+
635+
620636
class Decimal(Column):
621637
"""
622638
Stores a variable precision decimal value

docs/api/cassandra/cqlengine/columns.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Columns of all types are initialized by passing :class:`.Column` attributes to t
5858

5959
.. autoclass:: Decimal(**kwargs)
6060

61+
.. autoclass:: Double
62+
6163
.. autoclass:: Float
6264

6365
.. autoclass:: Integer(**kwargs)

tests/integration/cqlengine/columns/test_value_io.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class BaseColumnIOTest(BaseCassEngTestCase):
5050
def setUpClass(cls):
5151
super(BaseColumnIOTest, cls).setUpClass()
5252

53-
#if the test column hasn't been defined, bail out
54-
if not cls.column: return
53+
# if the test column hasn't been defined, bail out
54+
if not cls.column:
55+
return
5556

5657
# create a table with the given column
5758
class IOTestModel(Model):
@@ -62,7 +63,7 @@ class IOTestModel(Model):
6263
cls._generated_model = IOTestModel
6364
sync_table(cls._generated_model)
6465

65-
#tupleify the tested values
66+
# tupleify the tested values
6667
if not isinstance(cls.pkey_val, tuple):
6768
cls.pkey_val = cls.pkey_val,
6869
if not isinstance(cls.data_val, tuple):
@@ -71,7 +72,8 @@ class IOTestModel(Model):
7172
@classmethod
7273
def tearDownClass(cls):
7374
super(BaseColumnIOTest, cls).tearDownClass()
74-
if not cls.column: return
75+
if not cls.column:
76+
return
7577
drop_table(cls._generated_model)
7678

7779
def comparator_converter(self, val):
@@ -80,31 +82,35 @@ def comparator_converter(self, val):
8082

8183
def test_column_io(self):
8284
""" Tests the given models class creates and retrieves values as expected """
83-
if not self.column: return
85+
if not self.column:
86+
return
8487
for pkey, data in zip(self.pkey_val, self.data_val):
85-
#create
88+
# create
8689
m1 = self._generated_model.create(pkey=pkey, data=data)
8790

88-
#get
91+
# get
8992
m2 = self._generated_model.get(pkey=pkey)
9093
assert m1.pkey == m2.pkey == self.comparator_converter(pkey), self.column
9194
assert m1.data == m2.data == self.comparator_converter(data), self.column
9295

93-
#delete
96+
# delete
9497
self._generated_model.filter(pkey=pkey).delete()
9598

99+
96100
class TestBlobIO(BaseColumnIOTest):
97101

98102
column = columns.Blob
99103
pkey_val = six.b('blake'), uuid4().bytes
100104
data_val = six.b('eggleston'), uuid4().bytes
101105

106+
102107
class TestBlobIO2(BaseColumnIOTest):
103108

104109
column = columns.Blob
105110
pkey_val = bytearray(six.b('blake')), uuid4().bytes
106111
data_val = bytearray(six.b('eggleston')), uuid4().bytes
107112

113+
108114
class TestTextIO(BaseColumnIOTest):
109115

110116
column = columns.Text
@@ -118,18 +124,21 @@ class TestNonBinaryTextIO(BaseColumnIOTest):
118124
pkey_val = 'bacon'
119125
data_val = '0xmonkey'
120126

127+
121128
class TestInteger(BaseColumnIOTest):
122129

123130
column = columns.Integer
124131
pkey_val = 5
125132
data_val = 6
126133

134+
127135
class TestBigInt(BaseColumnIOTest):
128136

129137
column = columns.BigInt
130138
pkey_val = 6
131139
data_val = pow(2, 63) - 1
132140

141+
133142
class TestDateTime(BaseColumnIOTest):
134143

135144
column = columns.DateTime
@@ -138,6 +147,7 @@ class TestDateTime(BaseColumnIOTest):
138147
pkey_val = now
139148
data_val = now + timedelta(days=1)
140149

150+
141151
class TestDate(BaseColumnIOTest):
142152

143153
column = columns.Date
@@ -146,6 +156,7 @@ class TestDate(BaseColumnIOTest):
146156
pkey_val = now
147157
data_val = now + timedelta(days=1)
148158

159+
149160
class TestUUID(BaseColumnIOTest):
150161

151162
column = columns.UUID
@@ -156,6 +167,7 @@ class TestUUID(BaseColumnIOTest):
156167
def comparator_converter(self, val):
157168
return val if isinstance(val, UUID) else UUID(val)
158169

170+
159171
class TestTimeUUID(BaseColumnIOTest):
160172

161173
column = columns.TimeUUID
@@ -166,13 +178,29 @@ class TestTimeUUID(BaseColumnIOTest):
166178
def comparator_converter(self, val):
167179
return val if isinstance(val, UUID) else UUID(val)
168180

181+
182+
# until Floats are implicitly single:
183+
class FloatSingle(columns.Float):
184+
def __init__(self, **kwargs):
185+
super(FloatSingle, self).__init__(double_precision=False, **kwargs)
186+
187+
169188
class TestFloatIO(BaseColumnIOTest):
170189

171-
column = columns.Float
190+
column = FloatSingle
191+
192+
pkey_val = 4.75
193+
data_val = -1.5
194+
195+
196+
class TestDoubleIO(BaseColumnIOTest):
197+
198+
column = columns.Double
172199

173200
pkey_val = 3.14
174201
data_val = -1982.11
175202

203+
176204
class TestDecimalIO(BaseColumnIOTest):
177205

178206
column = columns.Decimal
@@ -183,6 +211,7 @@ class TestDecimalIO(BaseColumnIOTest):
183211
def comparator_converter(self, val):
184212
return Decimal(val)
185213

214+
186215
class TestQuoter(unittest.TestCase):
187216

188217
def test_equals(self):

tests/integration/cqlengine/model/test_udts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class AllDatatypes(UserType):
196196
e = columns.Date()
197197
f = columns.DateTime()
198198
g = columns.Decimal()
199-
h = columns.Float()
199+
h = columns.Float(double_precision=False)
200200
i = columns.Inet()
201201
j = columns.Integer()
202202
k = columns.Text()
@@ -227,7 +227,7 @@ class AllDatatypes(UserType):
227227
e = columns.Date()
228228
f = columns.DateTime()
229229
g = columns.Decimal()
230-
h = columns.Float(double_precision=True)
230+
h = columns.Double()
231231
i = columns.Inet()
232232
j = columns.Integer()
233233
k = columns.Text()

0 commit comments

Comments
 (0)