From 231187235d33983721a20c972a96ffd9101ec360 Mon Sep 17 00:00:00 2001 From: holgern <holgernahrstaedt@gmx.de> Date: Thu, 18 Apr 2019 21:10:29 +0200 Subject: [PATCH] Fix float entered into a Amount will be reduced by 0.001 due to rounding issues * fix Amount.amount and added Amount.amount_decimal * Prevent that wrong reputation in a Comment API answer break the Comment object --- CHANGELOG.rst | 6 +++++ beem/amount.py | 46 +++++++++++++++++++++++++++---------- beem/comment.py | 5 +++- beem/version.py | 2 +- beemapi/version.py | 2 +- beembase/version.py | 2 +- beemgraphenebase/version.py | 2 +- setup.py | 2 +- tests/beem/test_amount.py | 25 ++++++++++++++++++++ 9 files changed, 74 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9c40f167..f904db0c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,5 +1,11 @@ Changelog ========= +0.20.21 +------- +* Fix float entered in Amount will be reduced by 0.001 due to rounding issues +* fix Amount.amount and added Amount.amount_decimal +* Prevent that wrong reputation in a Comment API answer break the Comment object + 0.20.20 ------- * Fix typo (PR #161) diff --git a/beem/amount.py b/beem/amount.py index 25176686..132a55a3 100644 --- a/beem/amount.py +++ b/beem/amount.py @@ -117,30 +117,45 @@ class Amount(dict): self["symbol"] = self["asset"]["symbol"] self["amount"] = Decimal(amount["amount"]) / Decimal(10 ** self["asset"]["precision"]) - elif amount and asset and isinstance(asset, Asset): - self["amount"] = amount - self["symbol"] = asset["symbol"] - self["asset"] = asset - elif amount and asset and isinstance(asset, string_types): - self["amount"] = amount - self["asset"] = Asset(asset, steem_instance=self.steem) - self["symbol"] = self["asset"]["symbol"] - elif isinstance(amount, (integer_types, float, Decimal)) and asset and isinstance(asset, Asset): - self["amount"] = amount + elif isinstance(amount, (float)) and asset and isinstance(asset, Asset): + self["amount"] = str(amount) self["asset"] = asset self["symbol"] = self["asset"]["symbol"] - elif isinstance(amount, (integer_types, float, Decimal)) and asset and isinstance(asset, dict): + elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, Asset): self["amount"] = amount self["asset"] = asset self["symbol"] = self["asset"]["symbol"] + + elif isinstance(amount, (float)) and asset and isinstance(asset, dict): + self["amount"] = str(amount) + self["asset"] = asset + self["symbol"] = self["asset"]["symbol"] - elif isinstance(amount, (integer_types, float, Decimal)) and asset and isinstance(asset, string_types): + elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, dict): self["amount"] = amount + self["asset"] = asset + self["symbol"] = self["asset"]["symbol"] + + elif isinstance(amount, (float)) and asset and isinstance(asset, string_types): + self["amount"] = str(amount) self["asset"] = Asset(asset, steem_instance=self.steem) self["symbol"] = asset + + elif isinstance(amount, (integer_types, Decimal)) and asset and isinstance(asset, string_types): + self["amount"] = amount + self["asset"] = Asset(asset, steem_instance=self.steem) + self["symbol"] = asset + elif amount and asset and isinstance(asset, Asset): + self["amount"] = amount + self["symbol"] = asset["symbol"] + self["asset"] = asset + elif amount and asset and isinstance(asset, string_types): + self["amount"] = amount + self["asset"] = Asset(asset, steem_instance=self.steem) + self["symbol"] = self["asset"]["symbol"] else: raise ValueError if self.fixed_point_arithmetic: @@ -155,12 +170,19 @@ class Amount(dict): amount=self["amount"], asset=self["asset"].copy(), new_appbase_format=self.new_appbase_format, + fixed_point_arithmetic=self.fixed_point_arithmetic, steem_instance=self.steem) @property def amount(self): """ Returns the amount as float """ + return float(self["amount"]) + + @property + def amount_decimal(self): + """ Returns the amount as decimal + """ return self["amount"] @property diff --git a/beem/comment.py b/beem/comment.py index 9b640919..06fa6e89 100644 --- a/beem/comment.py +++ b/beem/comment.py @@ -129,7 +129,10 @@ class Comment(BlockchainObject): ] for p in parse_int: if p in vote and isinstance(vote.get(p), string_types): - vote[p] = int(vote.get(p, "0")) + try: + vote[p] = int(vote.get(p, "0")) + except: + vote[p] = int(0) new_active_votes.append(vote) comment["active_votes"] = new_active_votes return comment diff --git a/beem/version.py b/beem/version.py index 2438e231..bb3e0bd3 100644 --- a/beem/version.py +++ b/beem/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.20.20' +version = '0.20.21' diff --git a/beemapi/version.py b/beemapi/version.py index 2438e231..bb3e0bd3 100644 --- a/beemapi/version.py +++ b/beemapi/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.20.20' +version = '0.20.21' diff --git a/beembase/version.py b/beembase/version.py index 2438e231..bb3e0bd3 100644 --- a/beembase/version.py +++ b/beembase/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.20.20' +version = '0.20.21' diff --git a/beemgraphenebase/version.py b/beemgraphenebase/version.py index 2438e231..bb3e0bd3 100644 --- a/beemgraphenebase/version.py +++ b/beemgraphenebase/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.20.20' +version = '0.20.21' diff --git a/setup.py b/setup.py index ed43a4b8..1451c567 100755 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ except LookupError: ascii = codecs.lookup('ascii') codecs.register(lambda name, enc=ascii: {True: enc}.get(name == 'mbcs')) -VERSION = '0.20.20' +VERSION = '0.20.21' tests_require = ['mock >= 2.0.0', 'pytest', 'pytest-mock', 'parameterized'] diff --git a/tests/beem/test_amount.py b/tests/beem/test_amount.py index 501d85a2..664ef94e 100644 --- a/tests/beem/test_amount.py +++ b/tests/beem/test_amount.py @@ -140,11 +140,36 @@ class Testcases(unittest.TestCase): self.assertEqual( int(Amount("0.9999", self.symbol)), 999) + self.assertEqual( + int(Amount(0.151, self.symbol)), + 151) + self.assertEqual( + int(Amount(round(0.1509,3), self.symbol)), + 151) + self.assertEqual( + int(Amount(round(0.1509,3), self.asset)), + 151) + self.assertEqual( + int(Amount(int(1), self.symbol)), + 1000) + self.assertEqual( + int(Amount(amount=round(0.1509,3), asset=Asset("SBD"))), + 151) + + def test_dict(self): + self.assertEqual(int(Amount({'amount': '150', 'nai': '@@000000021', 'precision': 3})), 150) + def test_float(self): self.assertEqual( float(Amount("1", self.symbol)), 1.00000) + self.assertEqual( + float(Amount(0.151, self.symbol)), + 0.151) + self.assertEqual( + float(Amount(round(0.1509, 3), self.symbol)), + 0.151) def test_plus(self): a1 = Amount(1, self.symbol) -- GitLab