Skip to content

Commit

Permalink
removed normalize() from singer.decimal to avoid scientific notation (#…
Browse files Browse the repository at this point in the history
…146)

Co-authored-by: Zach Harris <[email protected]>
  • Loading branch information
leslievandemark and zachharris1 authored Mar 22, 2021
1 parent 2f618c6 commit d48271b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions singer/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ def _transform(self, data, typ, schema, path):

if isinstance(data, (str, float, int)):
try:
return True, str(decimal.Decimal(str(data)).normalize())
return True, str(decimal.Decimal(str(data)))
except:
return False, None
elif isinstance(data, decimal.Decimal):
try:
if data.is_snan():
return True, 'NaN'
else:
return True, str(data.normalize())
return True, str(data)
except:
return False, None

Expand Down
14 changes: 9 additions & 5 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,28 @@ def test_decimal_types_transform(self):
self.assertEquals(str1, transform(str1, schema))
self.assertEquals({'percentage': '1E-13'}, transform(str2, schema))
self.assertEquals({'percentage': '1E+13'}, transform(str3, schema))
self.assertEquals({'percentage': '1E+2'}, transform(str4, schema))
self.assertEquals({'percentage': '-1E+2'}, transform(str5, schema))
self.assertEquals({'percentage': '100'}, transform(str4, schema))
self.assertEquals({'percentage': '-100'}, transform(str5, schema))

float1 = {'percentage': 12.0000000000000000000000000001234556}
float2 = {'percentage': 0.0123}
float3 = {'percentage': 100.0123}
float4 = {'percentage': -100.0123}
self.assertEquals({'percentage':'12'}, transform(float1, schema))
float5 = {'percentage': 0.000001}
float6 = {'percentage': 0.0000001}
self.assertEquals({'percentage':'12.0'}, transform(float1, schema))
self.assertEquals({'percentage':'0.0123'}, transform(float2, schema))
self.assertEquals({'percentage':'100.0123'}, transform(float3, schema))
self.assertEquals({'percentage':'-100.0123'}, transform(float4, schema))
self.assertEquals({'percentage':'0.000001'}, transform(float5, schema))
self.assertEquals({'percentage':'1E-7'}, transform(float6, schema))

int1 = {'percentage': 123}
int2 = {'percentage': 0}
int3 = {'percentage': -1000}
self.assertEquals({'percentage':'123'}, transform(int1, schema))
self.assertEquals({'percentage':'0'}, transform(int2, schema))
self.assertEquals({'percentage':'-1E+3'}, transform(int3, schema))
self.assertEquals({'percentage':'-1000'}, transform(int3, schema))

dec1 = {'percentage': decimal.Decimal('1.1010101')}
dec2 = {'percentage': decimal.Decimal('.111111111111111111111111')}
Expand All @@ -305,7 +309,7 @@ def test_decimal_types_transform(self):
self.assertEquals({'percentage':'1.1010101'}, transform(dec1, schema))
self.assertEquals({'percentage':'0.111111111111111111111111'}, transform(dec2, schema))
self.assertEquals({'percentage':'-0.111111111111111111111111'}, transform(dec3, schema))
self.assertEquals({'percentage':'1E+2'}, transform(dec4, schema))
self.assertEquals({'percentage':'100'}, transform(dec4, schema))

bad1 = {'percentage': 'fsdkjl'}
with self.assertRaises(SchemaMismatch):
Expand Down

0 comments on commit d48271b

Please sign in to comment.