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

Python's logical operators "and" and "or" are not always translated to "||" and "&&" in JS #561

Open
rshamsnejad opened this issue Mar 29, 2023 · 2 comments

Comments

@rshamsnejad
Copy link

In the Builder, I have defined a button that appears and disappears on two conditions :

image

The conditions are the following :

# Start
(sound_original.status != NOT_STARTED) or (sound_A.status != NOT_STARTED) or (sound_B.status != NOT_STARTED)
# Stop
(sound_original.status != STARTED) and (sound_A.status != STARTED) and (sound_B.status != STARTED)

And when I compile to JS, it produces the following :

    if ((((sound_original.status != NOT_STARTED) or (sound_A.status != NOT_STARTED) or (sound_B.status != NOT_STARTED))) && stop.status === PsychoJS.Status.NOT_STARTED) {
      // keep track of start time/frame for later
      stop.tStart = t;  // (not accounting for frame time here)
      stop.frameNStart = frameN;  // exact frame index
      
      stop.setAutoDraw(true);
    }

    if (stop.status === PsychoJS.Status.STARTED && Boolean(((sound_original.status != STARTED) and (sound_A.status != STARTED) and (sound_B.status != STARTED)))) {
      stop.setAutoDraw(false);
    }

This causes the webpage to get stuck on "initialiasing the experiment..." unless I replace all the "or" and "and" by "||" and "&&" respectively.

And thanks for this awesome tool !

@F-said
Copy link

F-said commented Jul 10, 2023

We ran into a similar issue. The workaround we implemented was to create a variable in a code component which gets flipped to True when a proposition in a component condition contains and or or.

For example, in my Begin Experiment tab I'd have:

# init variable set to False
showFirstButton = False

Then in Each Frame, I'd have:

# show button after set amount of time of passage completion
if passage_sound.status == FINISHED and t > passage_sound.getDuration() + WAIT_DURATION:
    showFirstButton = True

Then in the condition text-box of my component I'd place theshowFirstButton variable with the dollar-sign notation $showFirstButton.

Hope it helps!

@TEParsons
Copy link
Contributor

The translation from Python to JS is handled by the psychopy app, so this is one I can handle :) @apitiot if you transfer this issue over to the psychopy repo I'll be able to link it to the PR once I figure out a fix.

I actually don't think or is the problem here - if you use the same code in a Code component (as in @F-said 's solution), it translates just fine. I think it's that start/stop conditions aren't translated at all, or possibly are translated via a different function (I know we have two, one for code blocks and one for single-line expressions).

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

No branches or pull requests

3 participants