mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +01:00
Support Python 3 (and 2)
This commit is contained in:
@@ -30,7 +30,7 @@ goog.require('Blockly.Python');
|
|||||||
|
|
||||||
|
|
||||||
// If any new block imports any library, add that library name here.
|
// 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) {
|
Blockly.Python['math_number'] = function(block) {
|
||||||
// Numeric value.
|
// Numeric value.
|
||||||
@@ -179,12 +179,14 @@ Blockly.Python['math_number_property'] = function(block) {
|
|||||||
var code;
|
var code;
|
||||||
if (dropdown_property == 'PRIME') {
|
if (dropdown_property == 'PRIME') {
|
||||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||||
|
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||||
|
'from numbers import Number';
|
||||||
var functionName = Blockly.Python.provideFunction_(
|
var functionName = Blockly.Python.provideFunction_(
|
||||||
'math_isPrime',
|
'math_isPrime',
|
||||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):',
|
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):',
|
||||||
' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||||
' # If n is not a number but a string, try parsing it.',
|
' # 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:',
|
' try:',
|
||||||
' n = float(n)',
|
' n = float(n)',
|
||||||
' except:',
|
' except:',
|
||||||
@@ -234,12 +236,14 @@ Blockly.Python['math_number_property'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Python['math_change'] = function(block) {
|
Blockly.Python['math_change'] = function(block) {
|
||||||
// Add to a variable in place.
|
// Add to a variable in place.
|
||||||
|
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||||
|
'from numbers import Number';
|
||||||
var argument0 = Blockly.Python.valueToCode(block, 'DELTA',
|
var argument0 = Blockly.Python.valueToCode(block, 'DELTA',
|
||||||
Blockly.Python.ORDER_ADDITIVE) || '0';
|
Blockly.Python.ORDER_ADDITIVE) || '0';
|
||||||
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
||||||
Blockly.Variables.NAME_TYPE);
|
Blockly.Variables.NAME_TYPE);
|
||||||
return varName + ' = (' + varName + ' if type(' + varName +
|
return varName + ' = (' + varName + ' if isinstance(' + varName +
|
||||||
') in (int, float, long) else 0) + ' + argument0 + '\n';
|
', Number) else 0) + ' + argument0 + '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
// Rounding functions have a single operand.
|
// Rounding functions have a single operand.
|
||||||
@@ -264,30 +268,33 @@ Blockly.Python['math_on_list'] = function(block) {
|
|||||||
code = 'max(' + list + ')';
|
code = 'max(' + list + ')';
|
||||||
break;
|
break;
|
||||||
case 'AVERAGE':
|
case 'AVERAGE':
|
||||||
|
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||||
|
'from numbers import Number';
|
||||||
var functionName = Blockly.Python.provideFunction_(
|
var functionName = Blockly.Python.provideFunction_(
|
||||||
'math_mean',
|
'math_mean',
|
||||||
// This operation excludes null and values that aren't int or float:',
|
// This operation excludes null and values that aren't int or float:',
|
||||||
// math_mean([null, null, "aString", 1, 9]) == 5.0.',
|
// math_mean([null, null, "aString", 1, 9]) == 5.0.',
|
||||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
['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',
|
' if not localList: return',
|
||||||
' return float(sum(localList)) / len(localList)']);
|
' return float(sum(localList)) / len(localList)']);
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
case 'MEDIAN':
|
case 'MEDIAN':
|
||||||
|
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||||
|
'from numbers import Number';
|
||||||
var functionName = Blockly.Python.provideFunction_(
|
var functionName = Blockly.Python.provideFunction_(
|
||||||
'math_median',
|
'math_median',
|
||||||
// This operation excludes null values:
|
// This operation excludes null values:
|
||||||
// math_median([null, null, 1, 3]) == 2.0.
|
// math_median([null, null, 1, 3]) == 2.0.
|
||||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||||
' localList = sorted([e for e in myList ' +
|
' localList = sorted([e for e in myList if isinstance(e, Number)])',
|
||||||
'if type(e) in (int, float, long)])',
|
|
||||||
' if not localList: return',
|
' if not localList: return',
|
||||||
' if len(localList) % 2 == 0:',
|
' if len(localList) % 2 == 0:',
|
||||||
' return (localList[len(localList) / 2 - 1] + ' +
|
' return (localList[len(localList) // 2 - 1] + ' +
|
||||||
'localList[len(localList) / 2]) / 2.0',
|
'localList[len(localList) // 2]) / 2.0',
|
||||||
' else:',
|
' else:',
|
||||||
' return localList[(len(localList) - 1) / 2]']);
|
' return localList[(len(localList) - 1) // 2]']);
|
||||||
code = functionName + '(' + list + ')';
|
code = functionName + '(' + list + ')';
|
||||||
break;
|
break;
|
||||||
case 'MODE':
|
case 'MODE':
|
||||||
|
|||||||
@@ -274,7 +274,7 @@ h1 {
|
|||||||
<option value="lists.xml">Lists</option>
|
<option value="lists.xml">Lists</option>
|
||||||
<option value="colour.xml">Colour</option>
|
<option value="colour.xml">Colour</option>
|
||||||
<option value="variables.xml">Variables</option>
|
<option value="variables.xml">Variables</option>
|
||||||
<option value="procedures.xml">Procedures</option>
|
<option value="functions.xml">Functions</option>
|
||||||
<option value="">Other...</option>
|
<option value="">Other...</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="button" value="Load" onclick="loadXml()">
|
<input type="button" value="Load" onclick="loadXml()">
|
||||||
|
|||||||
Reference in New Issue
Block a user