Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
1facefc3
Commit
1facefc3
authored
Jan 04, 2016
by
Neil Fraser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support Python 3 (and 2)
parent
9746650a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
11 deletions
+18
-11
generators/python/math.js
generators/python/math.js
+17
-10
tests/generators/functions.xml
tests/generators/functions.xml
+0
-0
tests/generators/index.html
tests/generators/index.html
+1
-1
No files found.
generators/python/math.js
View file @
1facefc3
...
...
@@ -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
typ
e(
'
+
varName
+
'
) in (int, float, long
) else 0) +
'
+
argument0
+
'
\n
'
;
return
varName
+
'
= (
'
+
varName
+
'
if
isinstanc
e(
'
+
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
'
:
...
...
tests/generators/
procedure
s.xml
→
tests/generators/
function
s.xml
View file @
1facefc3
File moved
tests/generators/index.html
View file @
1facefc3
...
...
@@ -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"
>
Procedure
s
</option>
<option
value=
"
functions.xml"
>
Function
s
</option>
<option
value=
""
>
Other...
</option>
</select>
<input
type=
"button"
value=
"Load"
onclick=
"loadXml()"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment