Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Add remove_key in Dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
alienzhangyw committed May 3, 2020
1 parent 2f1fa9f commit 42653a5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 16 deletions.
2 changes: 2 additions & 0 deletions BlockPi/src/msg/js/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ Blockly.Msg["DICTIONARIES_SET_KEY_TOOLTIP"] = "Sets the value of a key in a dict
Blockly.Msg["DICTIONARIES_GET_KEY_DICT"] = "in dictionary"
Blockly.Msg["DICTIONARIES_GET_KEY"] = " get value by key"
Blockly.Msg["DICTIONARIES_POP_KEY"] = "get and remove value by key"
Blockly.Msg["DICTIONARIES_DEL_KEY"] = "remove value by key"
Blockly.Msg["DICTIONARIES_GET_KEY_TOOLTIP"] = "Returns the value of a key in a dictionary."
Blockly.Msg["DICTIONARIES_POP_KEY_TOOLTIP"] = "Removes and returns the value of a key in a dictionary."
Blockly.Msg["DICTIONARIES_DEL_KEY_TOOLTIP"] = "Removes the item by the given key in a dictionary."
Blockly.Msg["DICTIONARIES_KEYS_DICT"] = "in dictionary"
Blockly.Msg["DICTIONARIES_KEYS_KEY"] = "get key list"
Blockly.Msg["DICTIONARIES_KEYS_VALUE"] = "get value list"
Expand Down
6 changes: 4 additions & 2 deletions BlockPi/src/msg/js/zh-hans.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ Blockly.Msg["DICTIONARIES_SET_KEY_TOOLTIP"] = "设置在字典中指定键的值
Blockly.Msg["DICTIONARIES_GET_KEY_DICT"] = "在字典中"
Blockly.Msg["DICTIONARIES_GET_KEY"] = "取得 值从键"
Blockly.Msg["DICTIONARIES_POP_KEY"] = "取得并移除 值从键"
Blockly.Msg["DICTIONARIES_GET_KEY_TOOLTIP"] = "返回在字典中指定键的值。"
Blockly.Msg["DICTIONARIES_POP_KEY_TOOLTIP"] = "移除并返回在字典中指定键的值。"
Blockly.Msg["DICTIONARIES_DEL_KEY"] = "移除 值从键"
Blockly.Msg["DICTIONARIES_GET_KEY_TOOLTIP"] = "从字典中返回指定键的值。"
Blockly.Msg["DICTIONARIES_POP_KEY_TOOLTIP"] = "从字典中移除并返回指定键的值。"
Blockly.Msg["DICTIONARIES_DEL_KEY_TOOLTIP"] = "从字典中移除指定键。"
Blockly.Msg["DICTIONARIES_KEYS_DICT"] = "在字典中"
Blockly.Msg["DICTIONARIES_KEYS_KEY"] = "取得 键 列表"
Blockly.Msg["DICTIONARIES_KEYS_VALUE"] = "取得 值 列表"
Expand Down
6 changes: 4 additions & 2 deletions BlockPi/src/msg/js/zh-hant.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ Blockly.Msg["DICTIONARIES_SET_KEY_TOOLTIP"] = "設置在字典中指定鍵的值
Blockly.Msg["DICTIONARIES_GET_KEY_DICT"] = "自字典"
Blockly.Msg["DICTIONARIES_GET_KEY"] = "取得 值從鍵"
Blockly.Msg["DICTIONARIES_POP_KEY"] = "取得併移除 值從鍵"
Blockly.Msg["DICTIONARIES_GET_KEY_TOOLTIP"] = "返回在字典中指定鍵的值。"
Blockly.Msg["DICTIONARIES_POP_KEY_TOOLTIP"] = "移除並返回在字典中指定鍵的值。"
Blockly.Msg["DICTIONARIES_DEL_KEY"] = "移除 值從鍵"
Blockly.Msg["DICTIONARIES_GET_KEY_TOOLTIP"] = "自字典中返回指定鍵的值。"
Blockly.Msg["DICTIONARIES_POP_KEY_TOOLTIP"] = "自字典中移除並返回指定鍵的值。"
Blockly.Msg["DICTIONARIES_DEL_KEY_TOOLTIP"] = "自字典中移除的指定鍵。"
Blockly.Msg["DICTIONARIES_KEYS_DICT"] = "自字典"
Blockly.Msg["DICTIONARIES_KEYS_KEY"] = "取得 鍵 清單"
Blockly.Msg["DICTIONARIES_KEYS_VALUE"] = "取得 值 清單"
Expand Down
5 changes: 3 additions & 2 deletions BlockPi/src/script/blocks_compressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions BlockPi/src/script/python_compressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 57 additions & 6 deletions blocks/dictionaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,82 @@ Blockly.Blocks['dictionaries_create_with_item'] = {

Blockly.Blocks['dictionaries_get_key'] = {
init: function() {
var thisBlock = this;
var dropdown = new Blockly.FieldDropdown(
var MODE =
[
[Blockly.Msg['DICTIONARIES_GET_KEY'], "GET"],
[Blockly.Msg['DICTIONARIES_POP_KEY'], "POP"]
]);
[Blockly.Msg['DICTIONARIES_POP_KEY'], "POP"],
[Blockly.Msg['DICTIONARIES_DEL_KEY'], "DEL"]
];
var modeMenu = new Blockly.FieldDropdown(MODE, function(value) {
var isStatement = (value == 'DEL');
this.getSourceBlock().updateStatement_(isStatement);
});
this.appendValueInput("DICT")
.setCheck("Dictionary")
.appendField(Blockly.Msg['DICTIONARIES_GET_KEY_DICT']);
this.appendValueInput("KEY")
.setCheck("String")
.appendField(dropdown, "MODE");
.appendField(modeMenu, "MODE");
this.setInputsInline(true);
this.setOutput(true, null);
this.setOutput(true);
this.setColour(Blockly.Msg['DICTIONARIES_HUE']);
var thisBlock = this;
this.setTooltip(function() {
var mode = thisBlock.getFieldValue('MODE');
if (mode == 'GET') {
return Blockly.Msg['DICTIONARIES_GET_KEY_TOOLTIP'];
} else if (mode == 'POP') {
return Blockly.Msg['DICTIONARIES_POP_KEY_TOOLTIP'];
} else if (mode == 'DEL') {
return Blockly.Msg['DICTIONARIES_DEL_KEY_TOOLTIP']
}
throw Error('Unknown mode: ' + mode);
});
this.setHelpUrl("");
},
/**
* Create XML to represent whether the block is a statement or a value.
* @return {Element} XML storage element.
* @this {Blockly.Block}
*/
mutationToDom: function() {
var container = Blockly.utils.xml.createElement('mutation');
var isStatement = !this.outputConnection;
container.setAttribute('statement', isStatement);
return container;
},
/**
* Parse XML to restore the 'AT' input.
* @param {!Element} xmlElement XML storage element.
* @this {Blockly.Block}
*/
domToMutation: function(xmlElement) {
// Note: Until January 2013 this block did not have mutations,
// so 'statement' defaults to false.
var isStatement = (xmlElement.getAttribute('statement') == 'true');
this.updateStatement_(isStatement);
},
/**
* Switch between a value block and a statement block.
* @param {boolean} newStatement True if the block should be a statement.
* False if the block should be a value.
* @private
* @this {Blockly.Block}
*/
updateStatement_: function (newStatement) {
var oldStatement = !this.outputConnection;
if (newStatement != oldStatement) {
this.unplug(true, true);
if (newStatement) {
this.setOutput(false);
this.setPreviousStatement(true);
this.setNextStatement(true);
} else {
this.setPreviousStatement(false);
this.setNextStatement(false);
this.setOutput(true);
}
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion generators/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ Blockly.Python.addReservedWords(
'issubclass,iter,len,license,list,locals,long,map,max,memoryview,min,' +
'next,object,oct,open,ord,pow,print,property,quit,range,raw_input,reduce,' +
'reload,repr,reversed,round,set,setattr,slice,sorted,staticmethod,str,' +
'sum,super,tuple,type,unichr,unicode,vars,xrange,zip'
'sum,super,tuple,type,unichr,unicode,vars,xrange,zip' +
'time,gpiozero,tm1637,sense_hat,sense_emu,SenseHat,sense,event'
);

/**
Expand Down
4 changes: 3 additions & 1 deletion generators/python/dictionaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ Blockly.Python['dictionaries_get_key'] = function(block) {
return [dict + '[' + key + ']', Blockly.Python.ORDER_MEMBER];
} else if (mode == 'POP') {
return [dict + '.pop(' + key + ')', Blockly.Python.ORDER_FUNCTION_CALL];
}
} else if (mode == 'DEL') {
return 'del ' + dict + '[' + key + ']\n';
}
};

Blockly.Python['dictionaries_keys'] = function(block) {
Expand Down

0 comments on commit 42653a5

Please sign in to comment.