lsendel / blockly

Automatically exported from code.google.com/p/blockly
0 stars 0 forks source link

Python: Ordered list of import, variables, and def commands #77

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Open the Blockly Code example
2. Add a procedure, the 'math_round' blocks and a second procedure 
3. Generate python code

What is the expected output? What do you see instead?
I expect an ordered list of import commands, variables, and 
definitions/procedures/functions.  

Unfortunately I get a mingled list with no order. (The order changes  with the 
position of the blocks)

What browser are you using?
Chromium-browser 18 and Firefox 15 (taken from Ubuntu Software Center)
Ubuntu version: 11.10   

Please provide any additional information below.
I made a quick fix, which is maybe not appropriate, but still working.

I Modified the Blockly.python.finish function in the python.js file within the 
generator folder. 
Since the variables are already sorted  I only checked whether the 
Blockly.Python.definitions_ entries contain 'import' in the string. If this is 
true I put the entry at the beginning of the list, else I put the entry at the 
end.

Something like that:
for (var name in Blockly.Python.definitions_) {
  var import_given = Blockly.Python.definitions_[name].match('import');
  if (import_given) {
    definitions.unshift(Blockly.Python.definitions_[name]);
  }
  else {    
    definitions.push(Blockly.Python.definitions_[name]); 
  }
}

best regards
Marc 

Original issue reported on code.google.com by borntob...@googlemail.com on 27 Sep 2012 at 2:44

GoogleCodeExporter commented 8 years ago
Thanks for the bug report and patch. r437 contains a fix very similar to yours, 
but with a new line to separate all import statements from the next block of 
variable definitions.

Original comment by Q.Neut...@gmail.com on 28 Sep 2012 at 6:41

GoogleCodeExporter commented 8 years ago
Thanks for fixing the problem. Unfortunately there is still an order problem.

All lines such as from X import Z aren't at the beginning. 

In my opinion the following line of code has to be modified:
if (def.indexOf('import ') == 0) {

Suggestion:
if ( (def.indexOf('import ') = 0) || (def.indexOf('from ') == 0) ) {

best regards Marc

And i hope it's okay for you to suggest possible solutions :)   

Original comment by borntob...@googlemail.com on 28 Sep 2012 at 10:57

GoogleCodeExporter commented 8 years ago
Ah, right. So I'll probably make it:
if (def.match(/^(from [\w.]+\s+)?import /)) 

Now the possible "from x import y" statements also brings back issue 74, so I 
will be making further changes to collect all the potential imported names into 
Blockly.Python.RESERVED_WORDS_.

Original comment by Q.Neut...@gmail.com on 28 Sep 2012 at 8:07

GoogleCodeExporter commented 8 years ago
Updated fix: if (fix: def.match(/^(from\s+\S+\s+)?import\s+\S+/)) { .

Regarding name collisions with variables, we determined that it's cumbersome to 
collect all import libraries' names into Blockly.Python.RESERVED_WORDS_ at 
runtime. 

Hence, if you build any block that requires "import x" or "from x import y", 
remember to add the names to RESERVED_WORDS_, like:

if (!Blockly.Python.RESERVED_WORDS_) {
  Blockly.Python.RESERVED_WORDS_ = '';
}
Blockly.Python.RESERVED_WORDS_ += 'math,random,'; //or your imported libraries, 
note the trailing comma.

Original comment by Q.Neut...@gmail.com on 29 Sep 2012 at 9:35