[boto] r1326 committed - Adding a FloatProperty class.

2 views
Skip to first unread message

codesite...@google.com

unread,
Oct 20, 2009, 3:22:09 PM10/20/09
to boto-...@googlegroups.com
Revision: 1326
Author: Mitch.Garnaat
Date: Tue Oct 20 12:21:40 2009
Log: Adding a FloatProperty class.
http://code.google.com/p/boto/source/detail?r=1326

Modified:
/trunk/boto/sdb/db/manager/sdbmanager.py
/trunk/boto/sdb/db/property.py
/trunk/boto/sdb/db/test_db.py

=======================================
--- /trunk/boto/sdb/db/manager/sdbmanager.py Thu Oct 1 11:33:52 2009
+++ /trunk/boto/sdb/db/manager/sdbmanager.py Tue Oct 20 12:21:40 2009
@@ -49,6 +49,7 @@
self.type_map = { bool : (self.encode_bool, self.decode_bool),
int : (self.encode_int, self.decode_int),
long : (self.encode_long, self.decode_long),
+ float : (self.encode_float, self.decode_float),
Model : (self.encode_reference,
self.decode_reference),
Key : (self.encode_reference,
self.decode_reference),
datetime : (self.encode_datetime,
self.decode_datetime),
@@ -163,6 +164,55 @@
else:
return False

+ def encode_float(self, value):
+ """
+ See http://tools.ietf.org/html/draft-wood-ldapext-float-00.
+ """
+ s = '%e' % value
+ l = s.split('e')
+ mantissa = l[0].ljust(18, '0')
+ exponent = l[1]
+ if value == 0.0:
+ case = '3'
+ exponent = '000'
+ elif mantissa[0] != '-' and exponent[0] == '+':
+ case = '5'
+ exponent = exponent[1:].rjust(3, '0')
+ elif mantissa[0] != '-' and exponent[0] == '-':
+ case = '4'
+ exponent = 999 + int(exponent)
+ exponent = '%03d' % exponent
+ elif mantissa[0] == '-' and exponent[0] == '-':
+ case = '2'
+ mantissa = '%f' % (10 + float(mantissa))
+ mantissa = mantissa.ljust(18, '0')
+ exponent = exponent[1:].rjust(3, '0')
+ else:
+ case = '1'
+ mantissa = '%f' % (10 + float(mantissa))
+ mantissa = mantissa.ljust(18, '0')
+ exponent = 999 - int(exponent)
+ exponent = '%03d' % exponent
+ return '%s %s %s' % (case, exponent, mantissa)
+
+ def decode_float(self, value):
+ case = value[0]
+ exponent = value[2:5]
+ mantissa = value[6:]
+ if case == '3':
+ return 0.0
+ elif case == '5':
+ pass
+ elif case == '4':
+ exponent = '%03d' % (int(exponent) - 999)
+ elif case == '2':
+ mantissa = '%f' % (float(mantissa) - 10)
+ exponent = '-' + exponent
+ else:
+ mantissa = '%f' % (float(mantissa) - 10)
+ exponent = '%03d' % abs((int(exponent) - 999))
+ return float(mantissa + 'e' + exponent)
+
def encode_datetime(self, value):
return value.strftime(ISO8601)

=======================================
--- /trunk/boto/sdb/db/property.py Thu Oct 15 14:12:10 2009
+++ /trunk/boto/sdb/db/property.py Tue Oct 20 12:21:40 2009
@@ -279,6 +279,23 @@
def empty(self, value):
return value is None

+class FloatProperty(Property):
+
+ data_type = float
+ type_name = 'Float'
+
+ def __init__(self, verbose_name=None, name=None, default=0.0,
required=False,
+ validator=None, choices=None, unique=False):
+ Property.__init__(self, verbose_name, name, default, required,
validator, choices, unique)
+
+ def validate(self, value):
+ value = float(value)
+ value = Property.validate(self, value)
+ return value
+
+ def empty(self, value):
+ return value is None
+
class DateTimeProperty(Property):

data_type = datetime.datetime
=======================================
--- /trunk/boto/sdb/db/test_db.py Tue Jun 23 11:07:18 2009
+++ /trunk/boto/sdb/db/test_db.py Tue Oct 20 12:21:40 2009
@@ -19,6 +19,11 @@
foo = BooleanProperty()
date = DateTimeProperty()

+class TestFloat(Model):
+
+ name = StringProperty()
+ value = FloatProperty()
+
class TestRequired(Model):

req = StringProperty(required=True, default='foo')
@@ -80,6 +85,23 @@
assert t.name == tt.name
#assert t.date == tt.date
return t
+
+def test_float():
+ global _objects
+ t = TestFloat()
+ t.name = 'float object'
+ t.value = 98.6
+ print 'saving object'
+ t.save()
+ _objects['test_float_t'] = t
+ time.sleep(5)
+ print 'now try retrieving it'
+ tt = TestFloat.get_by_id(t.id)
+ _objects['test_float_tt'] = tt
+ assert tt.id == t.id
+ assert tt.name == t.name
+ assert tt.value == t.value
+ return t

def test_required():
global _objects

Reply all
Reply to author
Forward
0 new messages