一个尝试生成笛卡尔积的块
This commit is contained in:
@@ -537,4 +537,141 @@ export const turn_to_int = {
|
|||||||
this.setOutput(true, Number);
|
this.setOutput(true, Number);
|
||||||
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOHEX)
|
this.setTooltip(Blockly.Msg.MIXLY_PYTHON_TOOLTIP_TOHEX)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const generate_cartesian_product = {
|
||||||
|
/**
|
||||||
|
* Block for creating a list with any number of elements of any type.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
init: function () {
|
||||||
|
this.setColour(MATH_HUE);
|
||||||
|
this.itemCount_ = 1;
|
||||||
|
this.updateShape_();
|
||||||
|
this.setMutator(new Blockly.icons.MutatorIcon(['lists_create_with_item'], this));
|
||||||
|
this.appendValueInput('repeat')
|
||||||
|
.appendField(Blockly.Msg.MIXLY_EVERY_PER_ELEPER_ELEMENT);
|
||||||
|
this.appendDummyInput()
|
||||||
|
.appendField(Blockly.Msg.CONTROLS_REPEAT_TITLE_TIMES);
|
||||||
|
this.setPreviousStatement(false)
|
||||||
|
this.setNextStatement(false)
|
||||||
|
this.setOutput(true)
|
||||||
|
this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_PYTHON_TOOLTIP);
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Create XML to represent list inputs.
|
||||||
|
* @return {Element} XML storage element.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
mutationToDom: function () {
|
||||||
|
var container = document.createElement('mutation');
|
||||||
|
container.setAttribute('items', this.itemCount_);
|
||||||
|
return container;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Parse XML to restore the list inputs.
|
||||||
|
* @param {!Element} xmlElement XML storage element.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
domToMutation: function (xmlElement) {
|
||||||
|
this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
|
||||||
|
this.updateShape_();
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Populate the mutator's dialog with this block's components.
|
||||||
|
* @param {!Blockly.Workspace} workspace Mutator's workspace.
|
||||||
|
* @return {!Blockly.Block} Root block in mutator.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
decompose: function (workspace) {
|
||||||
|
var containerBlock =
|
||||||
|
workspace.newBlock('lists_create_with_container');
|
||||||
|
containerBlock.initSvg();
|
||||||
|
var connection = containerBlock.getInput('STACK').connection;
|
||||||
|
for (var i = 0; i < this.itemCount_; i++) {
|
||||||
|
var itemBlock = workspace.newBlock('lists_create_with_item');
|
||||||
|
itemBlock.initSvg();
|
||||||
|
connection.connect(itemBlock.previousConnection);
|
||||||
|
connection = itemBlock.nextConnection;
|
||||||
|
}
|
||||||
|
return containerBlock;
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Reconfigure this block based on the mutator dialog's components.
|
||||||
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
compose: function (containerBlock) {
|
||||||
|
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||||
|
// Count number of inputs.
|
||||||
|
var connections = [];
|
||||||
|
var i = 0;
|
||||||
|
while (itemBlock) {
|
||||||
|
connections[i] = itemBlock.valueConnection_;
|
||||||
|
itemBlock = itemBlock.nextConnection &&
|
||||||
|
itemBlock.nextConnection.targetBlock();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
this.itemCount_ = i;
|
||||||
|
this.updateShape_();
|
||||||
|
// Reconnect any child blocks.
|
||||||
|
for (var i = 0; i < this.itemCount_; i++) {
|
||||||
|
if (connections[i]) {
|
||||||
|
this.getInput('ADD' + i).connection.connect(connections[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Store pointers to any connected child blocks.
|
||||||
|
* @param {!Blockly.Block} containerBlock Root block in mutator.
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
saveConnections: function (containerBlock) {
|
||||||
|
var itemBlock = containerBlock.getInputTargetBlock('STACK');
|
||||||
|
var i = 0;
|
||||||
|
while (itemBlock) {
|
||||||
|
var input = this.getInput('ADD' + i);
|
||||||
|
itemBlock.valueConnection_ = input && input.connection.targetConnection;
|
||||||
|
i++;
|
||||||
|
itemBlock = itemBlock.nextConnection &&
|
||||||
|
itemBlock.nextConnection.targetBlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Modify this block to have the correct number of inputs.
|
||||||
|
* @private
|
||||||
|
* @this Blockly.Block
|
||||||
|
*/
|
||||||
|
updateShape_: function () {
|
||||||
|
// Delete everything.
|
||||||
|
if (this.getInput('EMPTY')) {
|
||||||
|
this.removeInput('EMPTY');
|
||||||
|
} else {
|
||||||
|
var i = 0;
|
||||||
|
while (this.getInput('ADD' + i)) {
|
||||||
|
this.removeInput('ADD' + i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Rebuild block.
|
||||||
|
if (this.itemCount_ == 0) {
|
||||||
|
this.appendDummyInput('EMPTY')
|
||||||
|
.appendField(Blockly.Msg.MIXLY_EMPTY_REMINDER);
|
||||||
|
} else {
|
||||||
|
for (var i = 0; i < this.itemCount_; i++) {
|
||||||
|
var input = this.appendValueInput('ADD' + i);
|
||||||
|
if (i == 0) {
|
||||||
|
input.appendField(Blockly.Msg.MIXLY_PRODUCT+Blockly.Msg.MIXLY_GENERATE_CARTESIAN_PRODUCT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getVars: function () {
|
||||||
|
return [this.getFieldValue('VAR')];
|
||||||
|
},
|
||||||
|
renameVar: function (oldName, newName) {
|
||||||
|
if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
|
||||||
|
this.setTitleValue(newName, 'VAR');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -307,4 +307,18 @@ export const turn_to_int = function (_, generator) {
|
|||||||
generator.definitions_.import_hexlify = "from ubinascii import hexlify";
|
generator.definitions_.import_hexlify = "from ubinascii import hexlify";
|
||||||
var str = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
|
var str = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
|
||||||
return ["hexlify(" + str + ').decode()', generator.ORDER_ATOMIC];
|
return ["hexlify(" + str + ').decode()', generator.ORDER_ATOMIC];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const generate_cartesian_product = function (_, generator) {
|
||||||
|
generator.definitions_.import_itertools = "import itertools";
|
||||||
|
var re = generator.valueToCode(this, 'repeat', generator.ORDER_ATOMIC);
|
||||||
|
var code = new Array(this.itemCount_);
|
||||||
|
var default_value = '0';
|
||||||
|
for (var n = 0; n < this.itemCount_; n++) {
|
||||||
|
code[n] = generator.valueToCode(this, 'ADD' + n,
|
||||||
|
generator.ORDER_NONE) || default_value;
|
||||||
|
}
|
||||||
|
// var code = '[' + code.join(', ') + ']';
|
||||||
|
// var code = 'itertools.product('+'repeat='+re+')';
|
||||||
|
return [code, generator.ORDER_ATOMIC];
|
||||||
}
|
}
|
||||||
@@ -389,6 +389,13 @@
|
|||||||
</shadow>
|
</shadow>
|
||||||
</value>
|
</value>
|
||||||
</block>
|
</block>
|
||||||
|
<block type="generate_cartesian_product">
|
||||||
|
<value name="repeat">
|
||||||
|
<shadow type="math_number">
|
||||||
|
<field name="NUM">2</field>
|
||||||
|
</shadow>
|
||||||
|
</value>
|
||||||
|
</block>
|
||||||
</category>
|
</category>
|
||||||
<category id="catLogic" colour="210">
|
<category id="catLogic" colour="210">
|
||||||
<block type="logic_compare"></block>
|
<block type="logic_compare"></block>
|
||||||
|
|||||||
@@ -3906,4 +3906,9 @@ ZhHans.MIXLY_TIMESTAMP_TO_DATA ="转化为日期";
|
|||||||
ZhHans.MIXLY_TO_INDEX_SEQUENCE = "的序号和内容组合为索引序列";
|
ZhHans.MIXLY_TO_INDEX_SEQUENCE = "的序号和内容组合为索引序列";
|
||||||
ZhHans.MIXLY_INDEX = "序号";
|
ZhHans.MIXLY_INDEX = "序号";
|
||||||
ZhHans.MIXLY_TOTO_INDEX_SEQUENC_TOOLTIP = "将列表的所有项和序号组合为索引序列,例如(0,'A')为一个索引序列";
|
ZhHans.MIXLY_TOTO_INDEX_SEQUENC_TOOLTIP = "将列表的所有项和序号组合为索引序列,例如(0,'A')为一个索引序列";
|
||||||
|
ZhHans.MIXLY_generate_cartesian_product_TOOLTIP = "生成所给参数的笛卡尔积,每个元素重复次数根据参数决定";
|
||||||
|
ZhHans.MIXLY_PRODUCT = "生成";
|
||||||
|
ZhHans.MIXLY_GENERATE_CARTESIAN_PRODUCT = "笛卡尔积";
|
||||||
|
ZhHans.MIXLY_EVERY_PER_ELEPER_ELEMENT = "每个元素重复";
|
||||||
|
ZhHans.MIXLY_EMPTY_REMINDER = "至少需要一个可迭代对象";
|
||||||
})();
|
})();
|
||||||
Reference in New Issue
Block a user