Skip to content

Commit

Permalink
python tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed May 30, 2018
1 parent f78a698 commit a6c31b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
13 changes: 6 additions & 7 deletions php/test/decimal_to_precision.php
@@ -1,13 +1,7 @@
<?php
namespace ccxt;

include_once ('ccxt.php');
use ccxt\Exchange;
use const ccxt\DECIMAL_PLACES;
use const ccxt\NO_PADDING;
use const ccxt\PAD_WITH_ZERO;
use const ccxt\ROUND;
use const ccxt\SIGNIFICANT_DIGITS;
use const ccxt\TRUNCATE;

// ----------------------------------------------------------------------------
// testDecimalToPrecisionErrorHandling
Expand All @@ -25,6 +19,11 @@
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

// ----------------------------------------------------------------------------

function decimal_to_precision ($x, $roundingMode = ROUND, $numPrecisionDigits = null, $countingMode = DECIMAL_PLACES, $paddingMode = NO_PADDING) {
return Exchange::decimal_to_precision ($x, $roundingMode, $numPrecisionDigits, $countingMode, $paddingMode);
}

// ----------------------------------------------------------------------------
// number_to_string works, not supported in Python and PHP yet
Expand Down
19 changes: 9 additions & 10 deletions python/ccxt/base/decimal_to_precision.py
Expand Up @@ -36,9 +36,6 @@ def decimal_to_precision(n, rounding_mode=ROUND, precision=None, counting_mode=D

precision = min(context.prec - 2, precision)

print('------------')
print(precision)

# all default except decimal.Underflow (raised when a number is rounded to zero)
context.traps[decimal.Underflow] = True

Expand All @@ -50,13 +47,16 @@ def power_of_10(x):

if rounding_mode == ROUND:
if counting_mode == DECIMAL_PLACES:
context.rounding = decimal.ROUND_HALF_UP
precise = str(dec.quantize(power_of_10(precision))) # ROUND_HALF_EVEN is default context
context.rounding = decimal.ROUND_HALF_EVEN
elif counting_mode == SIGNIFICANT_DIGITS:
q = precision - dec.adjusted() - 1
sigfig = power_of_10(q)
if q < 0:
print(q, sigfig, precision, string[:precision])
below = sigfig * decimal.Decimal(string[:precision])
string_to_precision = string[:precision]
# string_to_precision is '' when we have zero precision
below = sigfig * decimal.Decimal(string_to_precision if string_to_precision else '0')
above = below + sigfig
precise = str(min((below, above), key=lambda x: abs(x - dec)))
else:
Expand All @@ -72,21 +72,20 @@ def power_of_10(x):
dot = string.index('.') if '.' in string else 0
start = dot - dec.adjusted()
end = start + precision
# print(dec.adjusted())
# print(start)
# print(end)
# need to clarify these conditionals
if dot >= end:
end -= 1
# if this is a negative exponent
# not sure if we should keep this
# if dec.adjusted() < 0:
# end += 1
precise = string[:end].ljust(dot, '0')

if '.' == precise[-1]:
if '.' == (precise[-1] if precise else ''):
raise ValueError

if padding_mode == NO_PADDING:
if precise == '' and precision == 0:
return '0'
return precise.rstrip('0').rstrip('.') if '.' in precise else precise
elif padding_mode == PAD_WITH_ZERO:
if '.' in precise:
Expand Down
58 changes: 27 additions & 31 deletions python/test/test_decimal_to_precision.py
Expand Up @@ -6,8 +6,6 @@

# -----------------------------------------------------------------------------

from decimal import getcontext

from ccxt.base.decimal_to_precision import decimal_to_precision # noqa F401
from ccxt.base.decimal_to_precision import TRUNCATE # noqa F401
from ccxt.base.decimal_to_precision import ROUND # noqa F401
Expand Down Expand Up @@ -36,45 +34,43 @@
# ----------------------------------------------------------------------------
# testDecimalToPrecisionTruncationToNDigitsAfterDot

# assert(decimal_to_precision('12.3456000', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456')
# assert(decimal_to_precision('12.3456', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456')
# assert(decimal_to_precision('12.3456', TRUNCATE, 4, DECIMAL_PLACES) == '12.3456')
# assert(decimal_to_precision('12.3456', TRUNCATE, 3, DECIMAL_PLACES) == '12.345')
# assert(decimal_to_precision('12.3456', TRUNCATE, 2, DECIMAL_PLACES) == '12.34')
# assert(decimal_to_precision('12.3456', TRUNCATE, 1, DECIMAL_PLACES) == '12.3')
# assert(decimal_to_precision('12.3456', TRUNCATE, 0, DECIMAL_PLACES) == '12')
assert(decimal_to_precision('12.3456000', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456')
assert(decimal_to_precision('12.3456', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456')
assert(decimal_to_precision('12.3456', TRUNCATE, 4, DECIMAL_PLACES) == '12.3456')
assert(decimal_to_precision('12.3456', TRUNCATE, 3, DECIMAL_PLACES) == '12.345')
assert(decimal_to_precision('12.3456', TRUNCATE, 2, DECIMAL_PLACES) == '12.34')
assert(decimal_to_precision('12.3456', TRUNCATE, 1, DECIMAL_PLACES) == '12.3')
assert(decimal_to_precision('12.3456', TRUNCATE, 0, DECIMAL_PLACES) == '12')

# # assert(decimal_to_precision('12.3456', TRUNCATE, -1, DECIMAL_PLACES) == '10') # not yet supported
# # assert(decimal_to_precision('123.456', TRUNCATE, -2, DECIMAL_PLACES) == '120') # not yet supported
# # assert(decimal_to_precision('123.456', TRUNCATE, -3, DECIMAL_PLACES) == '100') # not yet supported
# assert(decimal_to_precision('12.3456', TRUNCATE, -1, DECIMAL_PLACES) == '10') # not yet supported
# assert(decimal_to_precision('123.456', TRUNCATE, -2, DECIMAL_PLACES) == '120') # not yet supported
# assert(decimal_to_precision('123.456', TRUNCATE, -3, DECIMAL_PLACES) == '100') # not yet supported

# assert(decimal_to_precision('0', TRUNCATE, 0, DECIMAL_PLACES) == '0')
assert(decimal_to_precision('0', TRUNCATE, 0, DECIMAL_PLACES) == '0')

# ----------------------------------------------------------------------------
# testDecimalToPrecisionTruncationToNSignificantDigits

# assert(decimal_to_precision('0.000123456700', TRUNCATE, 100, SIGNIFICANT_DIGITS) == '0.0001234567')
# assert(decimal_to_precision('0.0001234567', TRUNCATE, 100, SIGNIFICANT_DIGITS) == '0.0001234567')
# assert(decimal_to_precision('0.0001234567', TRUNCATE, 7, SIGNIFICANT_DIGITS) == '0.0001234567')
assert(decimal_to_precision('0.000123456700', TRUNCATE, 100, SIGNIFICANT_DIGITS) == '0.0001234567')
assert(decimal_to_precision('0.0001234567', TRUNCATE, 100, SIGNIFICANT_DIGITS) == '0.0001234567')
assert(decimal_to_precision('0.0001234567', TRUNCATE, 7, SIGNIFICANT_DIGITS) == '0.0001234567')

# assert(decimal_to_precision('0.000123456', TRUNCATE, 6, SIGNIFICANT_DIGITS) == '0.000123456')
print(decimal_to_precision('0.000123456', TRUNCATE, 5, SIGNIFICANT_DIGITS))
print(getcontext().prec)
# assert(decimal_to_precision('0.000123456', TRUNCATE, 5, SIGNIFICANT_DIGITS) == '0.00012345')
# assert(decimal_to_precision('0.000123456', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '0.00012')
# assert(decimal_to_precision('0.000123456', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '0.0001')
assert(decimal_to_precision('0.000123456', TRUNCATE, 6, SIGNIFICANT_DIGITS) == '0.000123456')
assert(decimal_to_precision('0.000123456', TRUNCATE, 5, SIGNIFICANT_DIGITS) == '0.00012345')
assert(decimal_to_precision('0.000123456', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '0.00012')
assert(decimal_to_precision('0.000123456', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '0.0001')

# assert(decimal_to_precision('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS) == '123.0000987')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 8, SIGNIFICANT_DIGITS) == '123.00009')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS) == '123')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0000')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS) == '123.0000987')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 8, SIGNIFICANT_DIGITS) == '123.00009')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS) == '123')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0000')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0')

# assert(decimal_to_precision('123.0000987654', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '120')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '100')
# assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '100')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '120')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '100')
assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '100')

# assert(decimal_to_precision('0', TRUNCATE, 0, SIGNIFICANT_DIGITS) == '0')
assert(decimal_to_precision('0', TRUNCATE, 0, SIGNIFICANT_DIGITS) == '0')

# ----------------------------------------------------------------------------
# testDecimalToPrecisionRoundingToNDigitsAfterDot
Expand Down
14 changes: 6 additions & 8 deletions transpile.js
Expand Up @@ -821,20 +821,13 @@ from ccxt.base.decimal_to_precision import NO_PADDING # noqa F401\n\
\n\
# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:\n\
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code\n\
\n\
"

const phpHeader =
"<?php\n\
namespace ccxt;\n\
\n\
include_once ('ccxt.php');\n\
use ccxt\\Exchange;\n\
use const ccxt\\DECIMAL_PLACES;\n\
use const ccxt\\NO_PADDING;\n\
use const ccxt\\PAD_WITH_ZERO;\n\
use const ccxt\\ROUND;\n\
use const ccxt\\SIGNIFICANT_DIGITS;\n\
use const ccxt\\TRUNCATE;\n\
\n\
// ----------------------------------------------------------------------------\n\
// testDecimalToPrecisionErrorHandling\n\
Expand All @@ -852,6 +845,11 @@ use const ccxt\\TRUNCATE;\n\
// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:\n\
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code\n\
\n\
// ----------------------------------------------------------------------------\n\
\n\
function decimal_to_precision ($x, $roundingMode = ROUND, $numPrecisionDigits = null, $countingMode = DECIMAL_PLACES, $paddingMode = NO_PADDING) {\n\
return Exchange::decimal_to_precision ($x, $roundingMode, $numPrecisionDigits, $countingMode, $paddingMode);\n\
}\n\
"

const python = pythonHeader + python2Body
Expand Down

0 comments on commit a6c31b3

Please sign in to comment.