diff --git a/generators/python/math.js b/generators/python/math.js index 2514d6491..a2595e00c 100644 --- a/generators/python/math.js +++ b/generators/python/math.js @@ -30,7 +30,7 @@ goog.require('Blockly.Python'); // If any new block imports any library, add that library name here. -Blockly.Python.addReservedWords('math,random'); +Blockly.Python.addReservedWords('math,random,Number'); Blockly.Python['math_number'] = function(block) { // Numeric value. @@ -179,12 +179,14 @@ Blockly.Python['math_number_property'] = function(block) { var code; if (dropdown_property == 'PRIME') { Blockly.Python.definitions_['import_math'] = 'import math'; + Blockly.Python.definitions_['from_numbers_import_Number'] = + 'from numbers import Number'; var functionName = Blockly.Python.provideFunction_( 'math_isPrime', ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):', ' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods', ' # If n is not a number but a string, try parsing it.', - ' if type(n) not in (int, float, long):', + ' if not isinstance(n, Number):', ' try:', ' n = float(n)', ' except:', @@ -234,12 +236,14 @@ Blockly.Python['math_number_property'] = function(block) { Blockly.Python['math_change'] = function(block) { // Add to a variable in place. + Blockly.Python.definitions_['from_numbers_import_Number'] = + 'from numbers import Number'; var argument0 = Blockly.Python.valueToCode(block, 'DELTA', Blockly.Python.ORDER_ADDITIVE) || '0'; var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE); - return varName + ' = (' + varName + ' if type(' + varName + - ') in (int, float, long) else 0) + ' + argument0 + '\n'; + return varName + ' = (' + varName + ' if isinstance(' + varName + + ', Number) else 0) + ' + argument0 + '\n'; }; // Rounding functions have a single operand. @@ -264,30 +268,33 @@ Blockly.Python['math_on_list'] = function(block) { code = 'max(' + list + ')'; break; case 'AVERAGE': + Blockly.Python.definitions_['from_numbers_import_Number'] = + 'from numbers import Number'; var functionName = Blockly.Python.provideFunction_( 'math_mean', // This operation excludes null and values that aren't int or float:', // math_mean([null, null, "aString", 1, 9]) == 5.0.', ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):', - ' localList = [e for e in myList if type(e) in (int, float, long)]', + ' localList = [e for e in myList if isinstance(e, Number)]', ' if not localList: return', ' return float(sum(localList)) / len(localList)']); code = functionName + '(' + list + ')'; break; case 'MEDIAN': + Blockly.Python.definitions_['from_numbers_import_Number'] = + 'from numbers import Number'; var functionName = Blockly.Python.provideFunction_( 'math_median', // This operation excludes null values: // math_median([null, null, 1, 3]) == 2.0. ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):', - ' localList = sorted([e for e in myList ' + - 'if type(e) in (int, float, long)])', + ' localList = sorted([e for e in myList if isinstance(e, Number)])', ' if not localList: return', ' if len(localList) % 2 == 0:', - ' return (localList[len(localList) / 2 - 1] + ' + - 'localList[len(localList) / 2]) / 2.0', + ' return (localList[len(localList) // 2 - 1] + ' + + 'localList[len(localList) // 2]) / 2.0', ' else:', - ' return localList[(len(localList) - 1) / 2]']); + ' return localList[(len(localList) - 1) // 2]']); code = functionName + '(' + list + ')'; break; case 'MODE': diff --git a/tests/generators/procedures.xml b/tests/generators/functions.xml similarity index 100% rename from tests/generators/procedures.xml rename to tests/generators/functions.xml diff --git a/tests/generators/index.html b/tests/generators/index.html index e7550e43a..e30de118c 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -274,7 +274,7 @@ h1 { - +