Commit 1facefc3 authored by Neil Fraser's avatar Neil Fraser

Support Python 3 (and 2)

parent 9746650a
......@@ -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':
......
......@@ -274,7 +274,7 @@ h1 {
<option value="lists.xml">Lists</option>
<option value="colour.xml">Colour</option>
<option value="variables.xml">Variables</option>
<option value="procedures.xml">Procedures</option>
<option value="functions.xml">Functions</option>
<option value="">Other...</option>
</select>
<input type="button" value="Load" onclick="loadXml()">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment