Skip to content

Commit

Permalink
fix price for py27
Browse files Browse the repository at this point in the history
  • Loading branch information
holgern committed Feb 27, 2018
1 parent 9c0d7e9 commit 026ad28
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
63 changes: 30 additions & 33 deletions beem/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,75 +69,71 @@ class Price(dict):
"""
def __init__(
self,
*args,
price,
base=None,
quote=None,
base_asset=None, # to identify sell/buy
steem_instance=None
):

self.steem = steem_instance or shared_steem_instance()

if (len(args) == 1 and isinstance(args[0], str) and not base and not quote):
if price is "":
price = None
if (isinstance(price, str) and not base and not quote):
import re
price, assets = args[0].split(" ")
price, assets = price.split(" ")
base_symbol, quote_symbol = assets_from_string(assets)
base = Asset(base_symbol, steem_instance=self.steem)
quote = Asset(quote_symbol, steem_instance=self.steem)
frac = Fraction(float(price)).limit_denominator(10 ** base["precision"])
self["quote"] = Amount(amount=frac.denominator, asset=quote, steem_instance=self.steem)
self["base"] = Amount(amount=frac.numerator, asset=base, steem_instance=self.steem)

elif (len(args) == 1 and isinstance(args[0], dict) and
"base" in args[0] and
"quote" in args[0]):
assert "price" not in args[0], "You cannot provide a 'price' this way"
elif (isinstance(price, dict) and
"base" in price and
"quote" in price):
assert "price" not in price, "You cannot provide a 'price' this way"
# Regular 'price' objects according to steem-core
base_id = args[0]["base"]["asset_id"]
if args[0]["base"]["asset_id"] == base_id:
self["base"] = Amount(args[0]["base"], steem_instance=self.steem)
self["quote"] = Amount(args[0]["quote"], steem_instance=self.steem)
base_id = price["base"]["asset_id"]
if price["base"]["asset_id"] == base_id:
self["base"] = Amount(price["base"], steem_instance=self.steem)
self["quote"] = Amount(price["quote"], steem_instance=self.steem)
else:
self["quote"] = Amount(args[0]["base"], steem_instance=self.steem)
self["base"] = Amount(args[0]["quote"], steem_instance=self.steem)
self["quote"] = Amount(price["base"], steem_instance=self.steem)
self["base"] = Amount(price["quote"], steem_instance=self.steem)

elif len(args) == 1 and (isinstance(base, Asset) and isinstance(quote, Asset)):
price = args[0]
elif (price is not None and isinstance(base, Asset) and isinstance(quote, Asset)):
frac = Fraction(float(price)).limit_denominator(10 ** base["precision"])
self["quote"] = Amount(amount=frac.denominator, asset=quote, steem_instance=self.steem)
self["base"] = Amount(amount=frac.numerator, asset=base, steem_instance=self.steem)

elif (len(args) == 1 and isinstance(base, str) and isinstance(quote, str)):
price = args[0]
elif (price is not None and isinstance(base, str) and isinstance(quote, str)):
base = Asset(base, steem_instance=self.steem)
quote = Asset(quote, steem_instance=self.steem)
frac = Fraction(float(price)).limit_denominator(10 ** base["precision"])
self["quote"] = Amount(amount=frac.denominator, asset=quote, steem_instance=self.steem)
self["base"] = Amount(amount=frac.numerator, asset=base, steem_instance=self.steem)

elif (len(args) == 0 and isinstance(base, str) and isinstance(quote, str)):
elif (price is None and isinstance(base, str) and isinstance(quote, str)):
self["quote"] = Amount(quote, steem_instance=self.steem)
self["base"] = Amount(base, steem_instance=self.steem)

elif (price is not None and isinstance(price, str) and isinstance(base, str)):
self["quote"] = Amount(price, steem_instance=self.steem)
self["base"] = Amount(base, steem_instance=self.steem)
# len(args) > 1
elif len(args) == 2 and isinstance(args[0], str) and isinstance(args[1], str):
self["base"] = Amount(args[1], steem_instance=self.steem)
self["quote"] = Amount(args[0], steem_instance=self.steem)

elif len(args) == 2 and isinstance(args[0], Amount) and isinstance(args[1], Amount):
self["quote"], self["base"] = args[0], args[1]
elif isinstance(price, Amount) and isinstance(base, Amount):
self["quote"], self["base"] = price, base

# len(args) == 0
elif (isinstance(base, Amount) and isinstance(quote, Amount)):
elif (price is None and isinstance(base, Amount) and isinstance(quote, Amount)):
self["quote"] = quote
self["base"] = base

elif (len(args) == 2 and
(isinstance(args[0], float) or isinstance(args[0], int)) and
isinstance(args[1], str)):
elif ((isinstance(price, float) or isinstance(price, int)) and
isinstance(base, str)):
import re
price = args[0]
base_symbol, quote_symbol = assets_from_string(args[1])
base_symbol, quote_symbol = assets_from_string(base)
base = Asset(base_symbol, steem_instance=self.steem)
quote = Asset(quote_symbol, steem_instance=self.steem)
frac = Fraction(float(price)).limit_denominator(10 ** base["precision"])
Expand All @@ -158,6 +154,7 @@ def __setitem__(self, key, value):

def copy(self):
return Price(
None,
base=self["base"].copy(),
quote=self["quote"].copy())

Expand Down Expand Up @@ -287,11 +284,11 @@ def __div__(self, other):
else:
raise InvalidAssetException
a["base"] = Amount(
float(self["quote"] / other["quote"]), other["quote"]["symbol"],
float(self["base"].amount / other["base"].amount), other["quote"]["symbol"],
steem_instance=self.steem
)
a["quote"] = Amount(
float(self["base"] / other["base"]), self["quote"]["symbol"],
float(self["quote"].amount / other["quote"].amount), self["quote"]["symbol"],
steem_instance=self.steem
)
elif isinstance(other, Amount):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_init(self):
Price({
"base": {"amount": 1, "asset_id": "SBD"},
"quote": {"amount": 10, "asset_id": "STEEM"}})
Price(quote="10 SBD", base="1 STEEM")
Price("", quote="10 SBD", base="1 STEEM")
Price("10 SBD", "1 STEEM")
Price(Amount("10 SBD"), Amount("1 STEEM"))

Expand Down

0 comments on commit 026ad28

Please sign in to comment.