Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interfacewithpython single line loops not recognizing outside environment #1574

Open
jessdtate opened this issue Dec 1, 2016 · 5 comments
Open

Comments

@jessdtate
Copy link
Contributor

Description

When using single line loops in InterfaceWithPython, there is problem with using multiple variables. It works if the loop is self contained, ie, [ [i] for i,e in enumerate(V) if e != 0], but if it relies on another variable or imported package it doesn't recognize them, ie, [a[i] for i in b]. These all work in the python console. If the loop is expanded into long form, there are no problems. I suspect this problem and issues #1569 and #1570, are related.

Steps to Reproduce

  1. add an interfacewithpython module
  2. test the following code:
    V = [0, float('nan'), 500 ]
    print([ [i,e] for i,e in enumerate(V) if e != 0])
  3. try these lines:
    import math
    print([i for i,e in enumerate(V) if math.isnan(e)])
    a = list(range(0,10))
    b = [1, 5, 6,8]
    print([ a[i] for i in b])

Expected behavior:

All these lines should work and the output should be:
[[1, nan], [2, 500]]
[1]
[1, 5, 6, 8]

Actual behavior:

returns an error, either 'math' not found, or 'a' not found

Versions

scirun v5.0-beta.F-37-gbc96c5d
os x 10.10 and 10.8

@dcwhite dcwhite self-assigned this Dec 2, 2016
@dcwhite dcwhite added this to the 5.0-beta.G [Internal] milestone Dec 2, 2016
@dcwhite
Copy link
Member

dcwhite commented Dec 2, 2016

@jessdtate I can make the "math" error go away by importing math at the script level, so the functions are available automatically (like with the scirun API). Anymore very common modules I can import this way?

With the second error, you stumbled on a Python 3 list comprehension/variable scoping in closures/embedded shell/environment problem 😈 . Read these links for explanation (gory details included):

inducer/pudb#103 (open issue)
ipython/ipython#136 (open issue)
http://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition
http://stackoverflow.com/questions/20136955/python3-nested-list-comprehension-scope
http://stackoverflow.com/questions/22797782/scope-of-class-variable-with-list-comprehension

There are ugly workarounds where you have to define your own named function, like so:

a = list(range(0,10))
b = [1, 5, 6,8]
def aa(a, b):
	return [ a[i] for i in b]
aaa = aa(a,b)
print(aaa)

that does what you want. So I cannot solve this one myself, with the code you gave. I'm open to suggestions.

@jessdtate
Copy link
Contributor Author

It may be a good idea to import the common modules. Since the expanded loop works too, it may not be worth fixing. It will probably work in a script too, I haven't tested that yet. Definitely needs to be documented at least.

@dcwhite dcwhite modified the milestones: 5.0-beta.G [Internal], Backlog Jan 4, 2017
@dcwhite dcwhite modified the milestones: Backlog, Backlog 2016 Jan 11, 2017
@dcwhite dcwhite removed their assignment May 22, 2017
@dcwhite dcwhite modified the milestones: 2017, Backlog 2016 Jul 10, 2017
@dcwhite dcwhite modified the milestones: Backlog, 2017 Sep 11, 2017
@github-actions
Copy link

Stale issue message

@dcwhite
Copy link
Member

dcwhite commented Oct 17, 2019

@jessdtate Is this in the documentation?

@github-actions
Copy link

This issue is stale because it has been open 240 days with no activity. Remove the stale label or comment, or this will be closed in 60 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants