forked from tjpalmer/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_compressed.js
More file actions
76 lines (75 loc) · 31.6 KB
/
python_compressed.js
File metadata and controls
76 lines (75 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Do not edit this file; automatically generated by build.py.
"use strict";
Blockly.Python=Blockly.Generator.get("Python");Blockly.Python.addReservedWords("and,as,assert,break,class,continue,def,del,elif,else,except,exec,finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,return,try,while,with,yield,True,False,None,NotImplemented,Ellipsis,__debug__,quit,exit,copyright,license,credits,abs,divmod,input,open,staticmethod,all,enumerate,int,ord,str,any,eval,isinstance,pow,sum,basestring,execfile,issubclass,print,super,bin,file,iter,property,tuple,bool,filter,len,range,type,bytearray,float,list,raw_input,unichr,callable,format,locals,reduce,unicode,chr,frozenset,long,reload,vars,classmethod,getattr,map,repr,xrange,cmp,globals,max,reversed,zip,compile,hasattr,memoryview,round,__import__,complex,hash,min,set,apply,delattr,help,next,setattr,buffer,dict,hex,object,slice,coerce,dir,id,oct,sorted,intern");
Blockly.Python.ORDER_ATOMIC=0;Blockly.Python.ORDER_COLLECTION=1;Blockly.Python.ORDER_STRING_CONVERSION=1;Blockly.Python.ORDER_MEMBER=2;Blockly.Python.ORDER_FUNCTION_CALL=2;Blockly.Python.ORDER_EXPONENTIATION=3;Blockly.Python.ORDER_UNARY_SIGN=4;Blockly.Python.ORDER_BITWISE_NOT=4;Blockly.Python.ORDER_MULTIPLICATIVE=5;Blockly.Python.ORDER_ADDITIVE=6;Blockly.Python.ORDER_BITWISE_SHIFT=7;Blockly.Python.ORDER_BITWISE_AND=8;Blockly.Python.ORDER_BITWISE_XOR=9;Blockly.Python.ORDER_BITWISE_OR=10;
Blockly.Python.ORDER_RELATIONAL=11;Blockly.Python.ORDER_LOGICAL_NOT=12;Blockly.Python.ORDER_LOGICAL_AND=13;Blockly.Python.ORDER_LOGICAL_OR=14;Blockly.Python.ORDER_CONDITIONAL=15;Blockly.Python.ORDER_LAMBDA=16;Blockly.Python.ORDER_NONE=99;Blockly.Python.INFINITE_LOOP_TRAP=null;
Blockly.Python.init=function(){Blockly.Python.definitions_={};if(Blockly.Variables){Blockly.Python.variableDB_?Blockly.Python.variableDB_.reset():Blockly.Python.variableDB_=new Blockly.Names(Blockly.Python.RESERVED_WORDS_);for(var a=[],b=Blockly.Variables.allVariables(),c=0;c<b.length;c++)a[c]=Blockly.Python.variableDB_.getName(b[c],Blockly.Variables.NAME_TYPE)+" = None";Blockly.Python.definitions_.variables=a.join("\n")}};
Blockly.Python.finish=function(a){var b=[],c=[],d;for(d in Blockly.Python.definitions_){var e=Blockly.Python.definitions_[d];e.match(/^(from\s+\S+\s+)?import\s+\S+/)?b.push(e):c.push(e)}return(b.join("\n")+"\n\n"+c.join("\n\n")).replace(/\n\n+/g,"\n\n").replace(/\n*$/,"\n\n\n")+a};Blockly.Python.scrubNakedValue=function(a){return a+"\n"};Blockly.Python.quote_=function(a){a=a.replace(/\\/g,"\\\\").replace(/\n/g,"\\\n").replace(/\%/g,"\\%").replace(/'/g,"\\'");return"'"+a+"'"};
Blockly.Python.scrub_=function(a,b){if(null===b)return"";var c="";if(!a.outputConnection||!a.outputConnection.targetConnection){var d=a.getCommentText();d&&(c+=Blockly.Generator.prefixLines(d,"# ")+"\n");for(var e=0;e<a.inputList.length;e++)if(a.inputList[e].type==Blockly.INPUT_VALUE&&(d=a.inputList[e].connection.targetBlock()))(d=Blockly.Generator.allNestedComments(d))&&(c+=Blockly.Generator.prefixLines(d,"# "))}e=a.nextConnection&&a.nextConnection.targetBlock();e=this.blockToCode(e);return c+b+
e};Blockly.Python.colour={};Blockly.Python.colour_picker=function(){return["'"+this.getTitleValue("COLOUR")+"'",Blockly.Python.ORDER_ATOMIC]};Blockly.Python.colour_random=function(){Blockly.Python.definitions_.import_random="import random";return["'#%06x' % random.randint(0, 2**24 - 1)",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.colour_rgb=function(){if(!Blockly.Python.definitions_.colour_rgb){var a=Blockly.Python.variableDB_.getDistinctName("colour_rgb",Blockly.Generator.NAME_TYPE);Blockly.Python.colour_rgb.functionName=a;var b=[];b.push("def "+a+"(r, g, b):");b.push(" r = round(min(100, max(0, r)) * 2.55)");b.push(" g = round(min(100, max(0, g)) * 2.55)");b.push(" b = round(min(100, max(0, b)) * 2.55)");b.push(" return '#%02x%02x%02x' % (r, g, b)");Blockly.Python.definitions_.colour_rgb=b.join("\n")}var a=
Blockly.Python.valueToCode(this,"RED",Blockly.Python.ORDER_NONE)||0,b=Blockly.Python.valueToCode(this,"GREEN",Blockly.Python.ORDER_NONE)||0,c=Blockly.Python.valueToCode(this,"BLUE",Blockly.Python.ORDER_NONE)||0;return[Blockly.Python.colour_rgb.functionName+"("+a+", "+b+", "+c+")",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.colour_blend=function(){if(!Blockly.Python.definitions_.colour_blend){var a=Blockly.Python.variableDB_.getDistinctName("colour_blend",Blockly.Generator.NAME_TYPE);Blockly.Python.colour_blend.functionName=a;var b=[];b.push("def "+a+"(colour1, colour2, ratio):");b.push(" r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)");b.push(" g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)");b.push(" b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)");b.push(" ratio = min(1, max(0, ratio))");
b.push(" r = round(r1 * (1 - ratio) + r2 * ratio)");b.push(" g = round(g1 * (1 - ratio) + g2 * ratio)");b.push(" b = round(b1 * (1 - ratio) + b2 * ratio)");b.push(" return '#%02x%02x%02x' % (r, g, b)");Blockly.Python.definitions_.colour_blend=b.join("\n")}var a=Blockly.Python.valueToCode(this,"COLOUR1",Blockly.Python.ORDER_NONE)||"'#000000'",b=Blockly.Python.valueToCode(this,"COLOUR2",Blockly.Python.ORDER_NONE)||"'#000000'",c=Blockly.Python.valueToCode(this,"RATIO",Blockly.Python.ORDER_NONE)||
0;return[Blockly.Python.colour_blend.functionName+"("+a+", "+b+", "+c+")",Blockly.Python.ORDER_FUNCTION_CALL]};Blockly.Python.control={};
Blockly.Python.controls_if=function(){for(var a=0,b=Blockly.Python.valueToCode(this,"IF"+a,Blockly.Python.ORDER_NONE)||"False",c=Blockly.Python.statementToCode(this,"DO"+a)||" pass\n",d="if "+b+":\n"+c,a=1;a<=this.elseifCount_;a++)b=Blockly.Python.valueToCode(this,"IF"+a,Blockly.Python.ORDER_NONE)||"False",c=Blockly.Python.statementToCode(this,"DO"+a)||" pass\n",d+="elif "+b+":\n"+c;this.elseCount_&&(c=Blockly.Python.statementToCode(this,"ELSE")||" pass\n",d+="else:\n"+c);return d};
Blockly.Python.controls_repeat=function(){var a=Number(this.getTitleValue("TIMES")),b=Blockly.Python.statementToCode(this,"DO")||" pass\n";Blockly.Python.INFINITE_LOOP_TRAP&&(b=Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,"'"+this.id+"'")+b);return"for "+Blockly.Python.variableDB_.getDistinctName("count",Blockly.Variables.NAME_TYPE)+" in range("+a+"):\n"+b};
Blockly.Python.controls_whileUntil=function(){var a="UNTIL"==this.getTitleValue("MODE"),a=Blockly.Python.valueToCode(this,"BOOL",a?Blockly.Python.ORDER_LOGICAL_NOT:Blockly.Python.ORDER_NONE)||"False",b=Blockly.Python.statementToCode(this,"DO")||" pass\n";Blockly.Python.INFINITE_LOOP_TRAP&&(b=Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,'"'+this.id+'"')+b);"UNTIL"==this.getTitleValue("MODE")&&(a.match(/^\w+$/)||(a="("+a+")"),a="not "+a);return"while "+a+":\n"+b};
Blockly.Python.controls_for=function(){var a=Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE),b=Blockly.Python.valueToCode(this,"FROM",Blockly.Python.ORDER_NONE)||"0",c=Blockly.Python.valueToCode(this,"TO",Blockly.Python.ORDER_NONE)||"0",d=Blockly.Python.statementToCode(this,"DO")||" pass\n";Blockly.Python.INFINITE_LOOP_TRAP&&(d=Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,'"'+this.id+'"')+d);var e="";if(b.match(/^-?\d+$/)&&c.match(/^-?\d+$/))b=parseInt(b,
10),c=parseInt(c,10),b<=c?(c++,c=0==b?c:b+", "+c):(c--,c=b+", "+c+", -1"),c="range("+c+")";else{var f=b;!b.match(/^\w+$/)&&!b.match(/^-?\d+$/)&&(f=Blockly.Python.variableDB_.getDistinctName(a+"_start",Blockly.Variables.NAME_TYPE),e+=f+" = "+b+"\n");b=c;!c.match(/^\w+$/)&&!c.match(/^-?\d+$/)&&(b=Blockly.Python.variableDB_.getDistinctName(a+"_end",Blockly.Variables.NAME_TYPE),e+=b+" = "+c+"\n");c="("+f+" <= "+b+") and range("+f+", "+b+" + 1) or range("+f+", "+b+" - 1, -1)"}return e+("for "+a+" in "+
c+":\n"+d)};Blockly.Python.controls_forEach=function(){var a=Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE),b=Blockly.Python.valueToCode(this,"LIST",Blockly.Python.ORDER_RELATIONAL)||"[]",c=Blockly.Python.statementToCode(this,"DO")||" pass\n";Blockly.Python.INFINITE_LOOP_TRAP&&(c=Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,'"'+this.id+'"')+c);return"for "+a+" in "+b+":\n"+c};
Blockly.Python.controls_flow_statements=function(){switch(this.getTitleValue("FLOW")){case "BREAK":return"break\n";case "CONTINUE":return"continue\n"}throw"Unknown flow statement.";};Blockly.Python.lists={};Blockly.Python.lists_create_empty=function(){return["[]",Blockly.Python.ORDER_ATOMIC]};Blockly.Python.lists_create_with=function(){for(var a=Array(this.itemCount_),b=0;b<this.itemCount_;b++)a[b]=Blockly.Python.valueToCode(this,"ADD"+b,Blockly.Python.ORDER_NONE)||"None";a="["+a.join(", ")+"]";return[a,Blockly.Python.ORDER_ATOMIC]};
Blockly.Python.lists_repeat=function(){var a=Blockly.Python.valueToCode(this,"ITEM",Blockly.Python.ORDER_NONE)||"None",b=Blockly.Python.valueToCode(this,"NUM",Blockly.Python.ORDER_MULTIPLICATIVE)||"0";return["["+a+"] * "+b,Blockly.Python.ORDER_MULTIPLICATIVE]};Blockly.Python.lists_length=function(){return["len("+(Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"[]")+")",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.lists_isEmpty=function(){return["not len("+(Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"[]")+")",Blockly.Python.ORDER_LOGICAL_NOT]};
Blockly.Python.lists_indexOf=function(){var a=Blockly.Python.valueToCode(this,"FIND",Blockly.Python.ORDER_NONE)||"[]",b=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_MEMBER)||"''";if("FIRST"==this.getTitleValue("END")){if(!Blockly.Python.definitions_.first_index){var c=Blockly.Python.variableDB_.getDistinctName("first_index",Blockly.Generator.NAME_TYPE);Blockly.Python.lists_indexOf.first_index=c;var d=[];d.push("def "+c+"(myList, elem):");d.push(" try: theIndex = myList.index(elem) + 1");
d.push(" except: theIndex = 0");d.push(" return theIndex");Blockly.Python.definitions_.first_index=d.join("\n")}a=Blockly.Python.lists_indexOf.first_index+"("+b+", "+a+")"}else Blockly.Python.definitions_.last_index||(c=Blockly.Python.variableDB_.getDistinctName("last_index",Blockly.Generator.NAME_TYPE),Blockly.Python.lists_indexOf.last_index=c,d=[],d.push("def "+c+"(myList, elem):"),d.push(" try: theIndex = len(myList) - myList[::-1].index(elem)"),d.push(" except: theIndex = 0"),d.push(" return theIndex"),
Blockly.Python.definitions_.last_index=d.join("\n")),a=Blockly.Python.lists_indexOf.last_index+"("+b+", "+a+")";return[a,Blockly.Python.ORDER_MEMBER]};
Blockly.Python.lists_getIndex=function(){var a=this.getTitleValue("MODE")||"GET",b=this.getTitleValue("WHERE")||"FROM_START",c=Blockly.Python.valueToCode(this,"AT",Blockly.Python.ORDER_UNARY_SIGN)||"1",d=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_MEMBER)||"[]";if("FIRST"==b){if("GET"==a)return[d+"[0]",Blockly.Python.ORDER_MEMBER];d+=".pop(0)";if("GET_REMOVE"==a)return[d,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==a)return d+"\n"}else if("LAST"==b){if("GET"==a)return[d+"[-1]",
Blockly.Python.ORDER_MEMBER];d+=".pop()";if("GET_REMOVE"==a)return[d,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==a)return d+"\n"}else if("FROM_START"==b){c=c.match(/^-?\d+$/)?parseInt(c,10)-1:c+" - 1";if("GET"==a)return[d+"["+c+"]",Blockly.Python.ORDER_MEMBER];d=d+".pop("+c+")";if("GET_REMOVE"==a)return[d,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==a)return d+"\n"}else if("FROM_END"==b){if("GET"==a)return[d+"[-"+c+"]",Blockly.Python.ORDER_MEMBER];d=d+".pop(-"+c+")";if("GET_REMOVE"==a)return[d,
Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==a)return d+"\n"}else if("RANDOM"==b){Blockly.Python.definitions_.import_random="import random";if("GET"==a)return["random.choice("+d+")",Blockly.Python.ORDER_FUNCTION_CALL];Blockly.Python.definitions_.lists_remove_random_item||(b=Blockly.Python.variableDB_.getDistinctName("lists_remove_random_item",Blockly.Generator.NAME_TYPE),Blockly.Python.lists_getIndex.random=b,c=[],c.push("def "+b+"(myList):"),c.push(" x = int(random.random() * len(myList))"),
c.push(" return myList.pop(x)"),Blockly.Python.definitions_.lists_remove_random_item=c.join("\n"));d=Blockly.Python.lists_getIndex.random+"("+d+")";if("GET"==a||"GET_REMOVE"==a)return[d,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==a)return d+"\n"}throw"Unhandled combination (lists_getIndex).";};
Blockly.Python.lists_setIndex=function(){var a=Blockly.Python.valueToCode(this,"LIST",Blockly.Python.ORDER_MEMBER)||"[]",b=this.getTitleValue("MODE")||"GET",c=this.getTitleValue("WHERE")||"FROM_START",d=Blockly.Python.valueToCode(this,"AT",Blockly.Python.ORDER_NONE)||"1",e=Blockly.Python.valueToCode(this,"TO",Blockly.Python.ORDER_NONE)||"None";if("FIRST"==c){if("SET"==b)return a+"[0] = "+e+"\n";if("INSERT"==b)return a+".insert(0, "+e+")\n"}else if("LAST"==c){if("SET"==b)return a+"[-1] = "+e+"\n";
if("INSERT"==b)return a+".append("+e+")\n"}else if("FROM_START"==c){d=d.match(/^-?\d+$/)?parseInt(d,10)-1:d+" - 1";if("SET"==b)return a+"["+d+"] = "+e+"\n";if("INSERT"==b)return a+".insert("+d+", "+e+")\n"}else if("FROM_END"==c){if("SET"==b)return a+"[-"+d+"] = "+e+"\n";if("INSERT"==b)return a+".insert(-"+d+", "+e+")\n"}else if("RANDOM"==c){Blockly.Python.definitions_.import_random="import random";a.match(/^\w+$/)?c="":(c=Blockly.Python.variableDB_.getDistinctName("tmp_list",Blockly.Variables.NAME_TYPE),
d=c+" = "+a+"\n",a=c,c=d);d=Blockly.Python.variableDB_.getDistinctName("tmp_x",Blockly.Variables.NAME_TYPE);c+=d+" = int(random.random() * len("+a+"))\n";if("SET"==b)return c+(a+"["+d+"] = "+e+"\n");if("INSERT"==b)return c+=a+".insert("+d+", "+e+")\n"}throw"Unhandled combination (lists_setIndex).";};
Blockly.Python.lists_getSublist=function(){var a=Blockly.Python.valueToCode(this,"LIST",Blockly.Python.ORDER_MEMBER)||"[]",b=this.getTitleValue("WHERE1"),c=this.getTitleValue("WHERE2"),d=Blockly.Python.valueToCode(this,"AT1",Blockly.Python.ORDER_ADDITIVE)||"1",e=Blockly.Python.valueToCode(this,"AT2",Blockly.Python.ORDER_ADDITIVE)||"1";"FIRST"==b||"FROM_START"==b&&"1"==d?d="":"FROM_START"==b?d=d.match(/^-?\d+$/)?parseInt(d,10)-1:d+" - 1":"FROM_END"==b&&(d="-"+d);"LAST"==c||"FROM_END"==c&&"1"==e?e=
"":"FROM_START"!=b&&"FROM_END"==b&&(e=e.match(/^-?\d+$/)?1-parseInt(e,10):"1 - "+e,Blockly.Python.definitions_.import_sys="import sys",e+=" or sys.maxsize");return[a+"["+d+" : "+e+"]",Blockly.Python.ORDER_MEMBER]};Blockly.Python.logic={};Blockly.Python.logic_compare=function(){var a=this.getTitleValue("OP"),a=Blockly.Python.logic_compare.OPERATORS[a],b=Blockly.Python.ORDER_RELATIONAL,c=Blockly.Python.valueToCode(this,"A",b)||"0",d=Blockly.Python.valueToCode(this,"B",b)||"0";return[c+" "+a+" "+d,b]};Blockly.Python.logic_compare.OPERATORS={EQ:"==",NEQ:"!=",LT:"<",LTE:"<=",GT:">",GTE:">="};
Blockly.Python.logic_operation=function(){var a="AND"==this.getTitleValue("OP")?"and":"or",b="and"==a?Blockly.Python.ORDER_LOGICAL_AND:Blockly.Python.ORDER_LOGICAL_OR,c=Blockly.Python.valueToCode(this,"A",b)||"False",d=Blockly.Python.valueToCode(this,"B",b)||"False";return[c+" "+a+" "+d,b]};Blockly.Python.logic_negate=function(){return["not "+(Blockly.Python.valueToCode(this,"BOOL",Blockly.Python.ORDER_LOGICAL_NOT)||"False"),Blockly.Python.ORDER_LOGICAL_NOT]};
Blockly.Python.logic_boolean=function(){return["TRUE"==this.getTitleValue("BOOL")?"True":"False",Blockly.Python.ORDER_ATOMIC]};Blockly.Python.logic_null=function(){return["None",Blockly.Python.ORDER_ATOMIC]};
Blockly.Python.logic_ternary=function(){var a=Blockly.Python.valueToCode(this,"IF",Blockly.Python.ORDER_CONDITIONAL)||"False",b=Blockly.Python.valueToCode(this,"THEN",Blockly.Python.ORDER_CONDITIONAL)||"None",c=Blockly.Python.valueToCode(this,"ELSE",Blockly.Python.ORDER_CONDITIONAL)||"None";return[b+" if "+a+" else "+c,Blockly.Python.ORDER_CONDITIONAL]};Blockly.Python.math={};Blockly.Python.addReservedWords("math,random");Blockly.Python.math_number=function(){var a=window.parseFloat(this.getTitleValue("NUM"));return[a,0>a?Blockly.Python.ORDER_UNARY_SIGN:Blockly.Python.ORDER_ATOMIC]};Blockly.Python.math_arithmetic=function(){var a=this.getTitleValue("OP"),b=Blockly.Python.math_arithmetic.OPERATORS[a],a=b[0],b=b[1],c=Blockly.Python.valueToCode(this,"A",b)||"0",d=Blockly.Python.valueToCode(this,"B",b)||"0";return[c+a+d,b]};
Blockly.Python.math_arithmetic.OPERATORS={ADD:[" + ",Blockly.Python.ORDER_ADDITIVE],MINUS:[" - ",Blockly.Python.ORDER_ADDITIVE],MULTIPLY:[" * ",Blockly.Python.ORDER_MULTIPLICATIVE],DIVIDE:[" / ",Blockly.Python.ORDER_MULTIPLICATIVE],POWER:[" ** ",Blockly.Python.ORDER_EXPONENTIATION]};
Blockly.Python.math_single=function(){var a=this.getTitleValue("OP"),b,c;if("NEG"==a)return b=Blockly.Python.valueToCode(this,"NUM",Blockly.Python.ORDER_UNARY_SIGN)||"0",["-"+b,Blockly.Python.ORDER_UNARY_SIGN];Blockly.Python.definitions_.import_math="import math";c="SIN"==a||"COS"==a||"TAN"==a?Blockly.Python.valueToCode(this,"NUM",Blockly.Python.ORDER_MULTIPLICATIVE)||"0":Blockly.Python.valueToCode(this,"NUM",Blockly.Python.ORDER_NONE)||"0";switch(a){case "ABS":b="math.fabs("+c+")";break;case "ROOT":b=
"math.sqrt("+c+")";break;case "LN":b="math.log("+c+")";break;case "LOG10":b="math.log10("+c+")";break;case "EXP":b="math.exp("+c+")";break;case "POW10":b="math.pow(10,"+c+")";break;case "ROUND":b="round("+c+")";break;case "ROUNDUP":b="math.ceil("+c+")";break;case "ROUNDDOWN":b="math.floor("+c+")";break;case "SIN":b="math.sin("+c+" / 180.0 * math.pi)";break;case "COS":b="math.cos("+c+" / 180.0 * math.pi)";break;case "TAN":b="math.tan("+c+" / 180.0 * math.pi)"}if(b)return[b,Blockly.Python.ORDER_FUNCTION_CALL];
switch(a){case "ASIN":b="math.asin("+c+") / math.pi * 180";break;case "ACOS":b="math.acos("+c+") / math.pi * 180";break;case "ATAN":b="math.atan("+c+") / math.pi * 180";break;default:throw"Unknown math operator: "+a;}return[b,Blockly.Python.ORDER_MULTIPLICATIVE]};Blockly.Python.math_constant=function(){var a=this.getTitleValue("CONSTANT");"INFINITY"!=a&&(Blockly.Python.definitions_.import_math="import math");return Blockly.Python.math_constant.CONSTANTS[a]};
Blockly.Python.math_constant.CONSTANTS={PI:["math.pi",Blockly.Python.ORDER_MEMBER],E:["math.e",Blockly.Python.ORDER_MEMBER],GOLDEN_RATIO:["(1 + math.sqrt(5)) / 2",Blockly.Python.ORDER_MULTIPLICATIVE],SQRT2:["math.sqrt(2)",Blockly.Python.ORDER_MEMBER],SQRT1_2:["math.sqrt(1 / 2)",Blockly.Python.ORDER_MEMBER],INFINITY:["float('inf')",Blockly.Python.ORDER_ATOMIC]};
Blockly.Python.math_number_property=function(){var a=Blockly.Python.valueToCode(this,"NUMBER_TO_CHECK",Blockly.Python.ORDER_MULTIPLICATIVE);if(!a)return["False",Blockly.Python.ORDER_ATOMIC];var b=this.getTitleValue("PROPERTY"),c;if("PRIME"==b)return Blockly.Python.definitions_.isPrime||(b=Blockly.Python.variableDB_.getDistinctName("isPrime",Blockly.Generator.NAME_TYPE),Blockly.Python.logic_prime=b,c=[],c.push("def "+b+"(n):"),c.push(" # http://en.wikipedia.org/wiki/Primality_test#Naive_methods"),
c.push(" # If n is not a number but a string, try parsing it."),c.push(" if type(n) not in (int, float, long):"),c.push(" try:"),c.push(" n = float(n)"),c.push(" except:"),c.push(" return False"),c.push(" if n == 2 or n == 3:"),c.push(" return True"),c.push(" # False if n is negative, is 1, or not whole, or if n is divisible by 2 or 3."),c.push(" if n <= 1 or n % 1 != 0 or n % 2 == 0 or n % 3 == 0:"),c.push(" return False"),c.push(" # Check all the numbers of form 6k +/- 1, up to sqrt(n)."),
c.push(" for x in range(6, int(math.sqrt(n)) + 2, 6):"),c.push(" if n % (x - 1) == 0 or n % (x + 1) == 0:"),c.push(" return False"),c.push(" return True"),Blockly.Python.definitions_.isPrime=c.join("\n")),c=Blockly.Python.logic_prime+"("+a+")",[c,Blockly.Python.ORDER_FUNCTION_CALL];switch(b){case "EVEN":c=a+" % 2 == 0";break;case "ODD":c=a+" % 2 == 1";break;case "WHOLE":c=a+" % 1 == 0";break;case "POSITIVE":c=a+" > 0";break;case "NEGATIVE":c=a+" < 0";break;case "DIVISIBLE_BY":b=Blockly.Python.valueToCode(this,
"DIVISOR",Blockly.Python.ORDER_MULTIPLICATIVE);if(!b||"0"==b)return["False",Blockly.Python.ORDER_ATOMIC];c=a+" % "+b+" == 0"}return[c,Blockly.Python.ORDER_RELATIONAL]};Blockly.Python.math_change=function(){var a=Blockly.Python.valueToCode(this,"DELTA",Blockly.Python.ORDER_ADDITIVE)||"0",b=Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE);return b+" = ("+b+" if type("+b+") in (int, float, long) else 0) + "+a+"\n"};Blockly.Python.math_round=Blockly.Python.math_single;
Blockly.Python.math_trig=Blockly.Python.math_single;
Blockly.Python.math_on_list=function(){var a=this.getTitleValue("OP"),b=Blockly.Python.valueToCode(this,"LIST",Blockly.Python.ORDER_NONE)||"[]";switch(a){case "SUM":a="sum("+b+")";break;case "MIN":a="min("+b+")";break;case "MAX":a="max("+b+")";break;case "AVERAGE":if(!Blockly.Python.definitions_.math_mean){var c=Blockly.Python.variableDB_.getDistinctName("math_mean",Blockly.Generator.NAME_TYPE);Blockly.Python.math_on_list.math_mean=c;a=[];a.push("def "+c+"(myList):");a.push(" localList = [e for e in myList if type(e) in (int, float, long)]");
a.push(" if not localList: return");a.push(" return float(sum(localList)) / len(localList)");Blockly.Python.definitions_.math_mean=a.join("\n")}a=Blockly.Python.math_on_list.math_mean+"("+b+")";break;case "MEDIAN":Blockly.Python.definitions_.math_median||(c=Blockly.Python.variableDB_.getDistinctName("math_median",Blockly.Generator.NAME_TYPE),Blockly.Python.math_on_list.math_median=c,a=[],a.push("def "+c+"(myList):"),a.push(" localList = sorted([e for e in myList if type(e) in (int, float, long)])"),
a.push(" if not localList: return"),a.push(" if len(localList) % 2 == 0:"),a.push(" return (localList[len(localList) / 2 - 1] + localList[len(localList) / 2]) / 2.0"),a.push(" else:"),a.push(" return localList[(len(localList) - 1) / 2]"),Blockly.Python.definitions_.math_median=a.join("\n"));a=Blockly.Python.math_on_list.math_median+"("+b+")";break;case "MODE":Blockly.Python.definitions_.math_modes||(c=Blockly.Python.variableDB_.getDistinctName("math_modes",Blockly.Generator.NAME_TYPE),Blockly.Python.math_on_list.math_modes=
c,a=[],a.push("def "+c+"(some_list):"),a.push(" modes = []"),a.push(" # Using a lists of [item, count] to keep count rather than dict"),a.push(' # to avoid "unhashable" errors when the counted item is itself a list or dict.'),a.push(" counts = []"),a.push(" maxCount = 1"),a.push(" for item in some_list:"),a.push(" found = False"),a.push(" for count in counts:"),a.push(" if count[0] == item:"),a.push(" count[1] += 1"),a.push(" maxCount = max(maxCount, count[1])"),a.push(" found = True"),
a.push(" if not found:"),a.push(" counts.append([item, 1])"),a.push(" for counted_item, item_count in counts:"),a.push(" if item_count == maxCount:"),a.push(" modes.append(counted_item)"),a.push(" return modes"),Blockly.Python.definitions_.math_modes=a.join("\n"));a=Blockly.Python.math_on_list.math_modes+"("+b+")";break;case "STD_DEV":Blockly.Python.definitions_.math_standard_deviation||(Blockly.Python.definitions_.import_math="import math",c=Blockly.Python.variableDB_.getDistinctName("math_standard_deviation",
Blockly.Generator.NAME_TYPE),Blockly.Python.math_on_list.math_standard_deviation=c,a=[],a.push("def "+c+"(numbers):"),a.push(" n = len(numbers)"),a.push(" if n == 0: return"),a.push(" mean = float(sum(numbers)) / n"),a.push(" variance = sum((x - mean) ** 2 for x in numbers) / n"),a.push(" return math.sqrt(variance)"),Blockly.Python.definitions_.math_standard_deviation=a.join("\n"));a=Blockly.Python.math_on_list.math_standard_deviation+"("+b+")";break;case "RANDOM":Blockly.Python.definitions_.import_random=
"import random";a="random.choice("+b+")";break;default:throw"Unknown operator: "+a;}return[a,Blockly.Python.ORDER_FUNCTION_CALL]};Blockly.Python.math_modulo=function(){var a=Blockly.Python.valueToCode(this,"DIVIDEND",Blockly.Python.ORDER_MULTIPLICATIVE)||"0",b=Blockly.Python.valueToCode(this,"DIVISOR",Blockly.Python.ORDER_MULTIPLICATIVE)||"0";return[a+" % "+b,Blockly.Python.ORDER_MULTIPLICATIVE]};
Blockly.Python.math_constrain=function(){var a=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"0",b=Blockly.Python.valueToCode(this,"LOW",Blockly.Python.ORDER_NONE)||"0",c=Blockly.Python.valueToCode(this,"HIGH",Blockly.Python.ORDER_NONE)||"float('inf')";return["min(max("+a+", "+b+"), "+c+")",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.math_random_int=function(){Blockly.Python.definitions_.import_random="import random";var a=Blockly.Python.valueToCode(this,"FROM",Blockly.Python.ORDER_NONE)||"0",b=Blockly.Python.valueToCode(this,"TO",Blockly.Python.ORDER_NONE)||"0";return["random.randint("+a+", "+b+")",Blockly.Python.ORDER_FUNCTION_CALL]};Blockly.Python.math_random_float=function(){Blockly.Python.definitions_.import_random="import random";return["random.random()",Blockly.Python.ORDER_FUNCTION_CALL]};Blockly.Python.procedures={};
Blockly.Python.procedures_defreturn=function(){for(var a=Blockly.Variables.allVariables(this),b=a.length-1;0<=b;b--){var c=a[b];-1==this.arguments_.indexOf(c)?a[b]=Blockly.Python.variableDB_.getName(c,Blockly.Variables.NAME_TYPE):a.splice(b,1)}a=a.length?" global "+a.join(", ")+"\n":"";b=Blockly.Python.variableDB_.getName(this.getTitleValue("NAME"),Blockly.Procedures.NAME_TYPE);c=Blockly.Python.statementToCode(this,"STACK");Blockly.Python.INFINITE_LOOP_TRAP&&(c=Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,'"'+
this.id+'"')+c);var d=Blockly.Python.valueToCode(this,"RETURN",Blockly.Python.ORDER_NONE)||"";d?d=" return "+d+"\n":c||(c=" pass");for(var e=[],f=0;f<this.arguments_.length;f++)e[f]=Blockly.Python.variableDB_.getName(this.arguments_[f],Blockly.Variables.NAME_TYPE);a="def "+b+"("+e.join(", ")+"):\n"+a+c+d;a=Blockly.Python.scrub_(this,a);Blockly.Python.definitions_[b]=a;return null};Blockly.Python.procedures_defnoreturn=Blockly.Python.procedures_defreturn;
Blockly.Python.procedures_callreturn=function(){for(var a=Blockly.Python.variableDB_.getName(this.getTitleValue("NAME"),Blockly.Procedures.NAME_TYPE),b=[],c=0;c<this.arguments_.length;c++)b[c]=Blockly.Python.valueToCode(this,"ARG"+c,Blockly.Python.ORDER_NONE)||"None";return[a+"("+b.join(", ")+")",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.procedures_callnoreturn=function(){for(var a=Blockly.Python.variableDB_.getName(this.getTitleValue("NAME"),Blockly.Procedures.NAME_TYPE),b=[],c=0;c<this.arguments_.length;c++)b[c]=Blockly.Python.valueToCode(this,"ARG"+c,Blockly.Python.ORDER_NONE)||"None";return a+"("+b.join(", ")+")\n"};
Blockly.Python.procedures_ifreturn=function(){var a="if "+(Blockly.Python.valueToCode(this,"CONDITION",Blockly.Python.ORDER_NONE)||"False")+":\n";if(this.hasReturnValue_)var b=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"None",a=a+(" return "+b+"\n");else a+=" return\n";return a};Blockly.Python.text={};Blockly.Python.text=function(){return[Blockly.Python.quote_(this.getTitleValue("TEXT")),Blockly.Python.ORDER_ATOMIC]};
Blockly.Python.text_join=function(){var a;if(0==this.itemCount_)return["''",Blockly.Python.ORDER_ATOMIC];if(1==this.itemCount_)return a=Blockly.Python.valueToCode(this,"ADD0",Blockly.Python.ORDER_NONE)||"''",["str("+a+")",Blockly.Python.ORDER_FUNCTION_CALL];if(2==this.itemCount_){a=Blockly.Python.valueToCode(this,"ADD0",Blockly.Python.ORDER_NONE)||"''";var b=Blockly.Python.valueToCode(this,"ADD1",Blockly.Python.ORDER_NONE)||"''";return["str("+a+") + str("+b+")",Blockly.Python.ORDER_UNARY_SIGN]}a=
[];for(b=0;b<this.itemCount_;b++)a[b]=Blockly.Python.valueToCode(this,"ADD"+b,Blockly.Python.ORDER_NONE)||"''";b=Blockly.Python.variableDB_.getDistinctName("temp_value",Blockly.Variables.NAME_TYPE);a="''.join([str("+b+") for "+b+" in ["+a.join(", ")+"]])";return[a,Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.text_append=function(){var a=Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE),b=Blockly.Python.valueToCode(this,"TEXT",Blockly.Python.ORDER_NONE)||"''";return a+" = str("+a+") + str("+b+")\n"};Blockly.Python.text_length=function(){return["len("+(Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"''")+")",Blockly.Python.ORDER_FUNCTION_CALL]};
Blockly.Python.text_isEmpty=function(){return["not len("+(Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"''")+")",Blockly.Python.ORDER_LOGICAL_NOT]};Blockly.Python.text_indexOf=function(){var a="FIRST"==this.getTitleValue("END")?"find":"rfind",b=Blockly.Python.valueToCode(this,"FIND",Blockly.Python.ORDER_NONE)||"''";return[(Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_MEMBER)||"''")+"."+a+"("+b+") + 1",Blockly.Python.ORDER_MEMBER]};
Blockly.Python.text_charAt=function(){var a=this.getTitleValue("WHERE")||"FROM_START",b=Blockly.Python.valueToCode(this,"AT",Blockly.Python.ORDER_UNARY_SIGN)||"1",c=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_MEMBER)||"''";switch(a){case "FIRST":return[c+"[0]",Blockly.Python.ORDER_MEMBER];case "LAST":return[c+"[-1]",Blockly.Python.ORDER_MEMBER];case "FROM_START":return b=b.match(/^-?\d+$/)?parseInt(b,10)-1:b+" - 1",[c+"["+b+"]",Blockly.Python.ORDER_MEMBER];case "FROM_END":return[c+
"[-"+b+"]",Blockly.Python.ORDER_MEMBER];case "RANDOM":return Blockly.Python.definitions_.text_random_letter||(Blockly.Python.definitions_.import_random="import random",a=Blockly.Python.variableDB_.getDistinctName("text_random_letter",Blockly.Generator.NAME_TYPE),Blockly.Python.text_charAt.text_random_letter=a,b=[],b.push("def "+a+"(text):"),b.push(" x = int(random.random() * len(text))"),b.push(" return text[x];"),Blockly.Python.definitions_.text_random_letter=b.join("\n")),c=Blockly.Python.text_charAt.text_random_letter+
"("+c+")",[c,Blockly.Python.ORDER_FUNCTION_CALL]}throw"Unhandled option (text_charAt).";};
Blockly.Python.text_getSubstring=function(){var a=Blockly.Python.valueToCode(this,"STRING",Blockly.Python.ORDER_MEMBER)||"''",b=this.getTitleValue("WHERE1"),c=this.getTitleValue("WHERE2"),d=Blockly.Python.valueToCode(this,"AT1",Blockly.Python.ORDER_ADDITIVE)||"1",e=Blockly.Python.valueToCode(this,"AT2",Blockly.Python.ORDER_ADDITIVE)||"1";"FIRST"==b||"FROM_START"==b&&"1"==d?d="":"FROM_START"==b?d=d.match(/^-?\d+$/)?parseInt(d,10)-1:d+" - 1":"FROM_END"==b&&(d="-"+d);"LAST"==c||"FROM_END"==c&&"1"==e?
e="":"FROM_START"!=b&&"FROM_END"==b&&(e=e.match(/^-?\d+$/)?1-parseInt(e,10):"1 - "+e,Blockly.Python.definitions_.import_sys="import sys",e+=" or sys.maxsize");return[a+"["+d+" : "+e+"]",Blockly.Python.ORDER_MEMBER]};Blockly.Python.text_changeCase=function(){var a=this.getTitleValue("CASE"),a=Blockly.Python.text_changeCase.OPERATORS[a];return[(Blockly.Python.valueToCode(this,"TEXT",Blockly.Python.ORDER_MEMBER)||"''")+a,Blockly.Python.ORDER_MEMBER]};
Blockly.Python.text_changeCase.OPERATORS={UPPERCASE:".upper()",LOWERCASE:".lower()",TITLECASE:".title()"};Blockly.Python.text_trim=function(){var a=this.getTitleValue("MODE"),a=Blockly.Python.text_trim.OPERATORS[a];return[(Blockly.Python.valueToCode(this,"TEXT",Blockly.Python.ORDER_MEMBER)||"''")+a,Blockly.Python.ORDER_MEMBER]};Blockly.Python.text_trim.OPERATORS={LEFT:".lstrip()",RIGHT:".rstrip()",BOTH:".strip()"};
Blockly.Python.text_print=function(){return"print("+(Blockly.Python.valueToCode(this,"TEXT",Blockly.Python.ORDER_NONE)||"''")+")\n"};
Blockly.Python.text_prompt=function(){if(!Blockly.Python.definitions_.text_prompt){var a=Blockly.Python.variableDB_.getDistinctName("text_prompt",Blockly.Generator.NAME_TYPE);Blockly.Python.text_prompt.text_prompt=a;var b=[];b.push("def "+a+"(msg):");b.push(" try:");b.push(" return raw_input(msg)");b.push(" except NameError:");b.push(" return input(msg)");Blockly.Python.definitions_.text_prompt=b.join("\n")}a=Blockly.Python.quote_(this.getTitleValue("TEXT"));a=Blockly.Python.text_prompt.text_prompt+
"("+a+")";"NUMBER"==this.getTitleValue("TYPE")&&(a="float("+a+")");return[a,Blockly.Python.ORDER_FUNCTION_CALL]};Blockly.Python.variables={};Blockly.Python.variables_get=function(){return[Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE),Blockly.Python.ORDER_ATOMIC]};Blockly.Python.variables_set=function(){var a=Blockly.Python.valueToCode(this,"VALUE",Blockly.Python.ORDER_NONE)||"0";return Blockly.Python.variableDB_.getName(this.getTitleValue("VAR"),Blockly.Variables.NAME_TYPE)+" = "+a+"\n"};