lundi 1 juin 2020

Autohotkey: how to declare global variable after #ifwinactive

When I declare a global variable after #ifwinactive, an error message pop up showing that the variable is not declared.

I have a long script where there are many #ifwinactive ahk_exe xxx. Below those codes, I want to declare a global variable. But that doesn't work. Whenever I run the script, it warns me the global variable is not declared. Here's a brief of my script.

On the top of the script I have these environment settings:

#NoEnv  
#Warn  
SendMode Input 
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2

Then I have many window-specific hotkeys:

#ifwinactive ahk_exe Explorer.EXE
;some hotkeys
#ifwinactive ahk_exe WINWORD.EXE
;some extra hotkeys
;etc.

At the bottom, I try to declare a global variable and this is where the error occurs:

#ifwinactive
a := 1
^p::
    if (a != 1){
        a := 1
    } else {
        a := 0
    }
return 

#if winactive("- YouTube -") and a != 1
    p::msgbox,Yes
#if a = 1 and winactive("- YouTube -")
    p::msgbox,no
#if

Javascript – use X amount of object variables in if statement

I'd like to make the following code more generic/page-independent so that I can use it for other projects too.

The code is checking if a certain value in the localCache exists and if the location.pathname has a certain string. If so, it should scroll to top etc.

Now it looks like this:

if (referrer == "home" && location.pathname == "projects"){
    window.scrollTo(0,0);
    localStorage.setItem('referrer', 'projects');
}

I'd like to make more generic so that it's not depending on the exact amount of strings of the location.pathname and so that it can refer to an X amount of location.pathnames.

I tried to make an object out of the different strings like this:

let object = {
    homePage: "home",
    noScrollPages: {[
      "projects", 
      "about",
      "info"
    ]}
}

if (referrer == homePage && (location.pathname == "/" + noScrollPages[0]) || location.pathname == "/" + noScrollPages[1] || location.pathname == "/" + noScrollPages[2]){
    window.scrollTo(0,0);
    localStorage.setItem("referrer", noScrollPages[0, 1, 2 or X, whichever resolves as true in the if statement]);
    console.log(localStorage.getItem("referrer"));
}

So, basically I don't know how code it so that it doesn't rely on the explicit declaration of [0] or [1], because it could be X number of pages.

Thanks for any tips!

How to make my nested loops faster in python?

Please, can somebody improve my code, it get the job done, but is very slow.

Every row is match between 2 players, in column "Players" are only the relevant player. I compare a player against the rows before, e.g "Player2" is at index 2, so I only iterate in the rows 0 and 1 My goal is it to find, if a Player has a different "Y in his next match.

df=pd.DataFrame({"Players":["Player1","Player1","Player2","Player1","Player1"],
                 "Win":["Player1","Player4","Player5","Player1","Player6"],
                 "Loss":["Player3","Player1","Player2","Player5","Player1"],
                 "Y": ["B","A","B","B","B"]})


Results=[]
for index, player in enumerate(Players.values, 0):
    Result=[]
    Result.append(0)
    for i in range(df.shape[0]):           
        if index <= df.index.values[i]: #skip the values in the same row
           continue
        if ((player == df.Win.values[i])| (player == df.Loss.values[i])) & (df.Y[index]!=df.Y[i]):  #has previous match a different Y
           Result.append(1)
        elif ((player == df.Win.values[i])| (player == df.Loss.values[i])) & (df.Y[index]==df.Y[i]): #has previous match the same Y       
           Result.append(0)
    Results.append(Result[-1])       

The outpup should look like this:

Results=[0,1,0,1,0]

`if ((letter >= 'P') && (letter <= 'S'))` ... why does this if statement work?

Came across this statement in a C book, if ((letter >= 'P') && (letter <= 'S'))

It's trying to check if the letter falls between P and S (including both) and I was quite surprised to see it work.

  1. How is it possible to do greater-than/lesser-than type operations on alphabets (character data type)?
  2. It is a feature in only C or in other programming languages as well?

Check if two conditions are both true, only one is true or none is true in Python

I have two conditions C1 and C2. I want my program to perform something different in case that C1 and C2 are true, C1 is true but not C2, C2 is true but not C1, neither C1 nor C2 is true.

Now I have this code:

if C1 and C2:
    ...
elif C1:
    ...
elif C2:
    ...
else:
    ...

Is there a more efficient way to code this in Python?

Flutter multiple if-statement

I have the following multiple if-statement in my flutter app:

        Text(text1).toString()==Text('Info and Comms').toString()?(snapshot.data.documents[8]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Media').toString()?(snapshot.data.documents[10]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Utilities').toString()?(snapshot.data.documents[16]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Education').toString()?(snapshot.data.documents[4]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Repairs').toString()?(snapshot.data.documents[13]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Domestic Help').toString()?(snapshot.data.documents[3]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Transport').toString()?(snapshot.data.documents[15]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Manufacturing').toString()?(snapshot.data.documents[9]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Mining').toString()?(snapshot.data.documents[11]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Supply Chain').toString()?(snapshot.data.documents[14]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Government').toString()?(snapshot.data.documents[6]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Financial and Business Services').toString()?(snapshot.data.documents[5]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Construction').toString()?(snapshot.data.documents[2]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Agriculture').toString()?(snapshot.data.documents[1]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Wholesale and Retail Trade').toString()?(snapshot.data.documents[17]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Accomodation').toString()?(snapshot.data.documents[0]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Health Social and Personal').toString()?(snapshot.data.documents[7]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Movement').toString()?(snapshot.data.documents[12]['desc'].toString()).replaceAll("/n", "\n"):
        Text('Data not found').toString(),

When deploying on my mobile device, it works perfect but after launching on Play Store, it returns the first statement regardless.

Any idea why this is now broken and where to start fixing?

moving average that changes over time

how could someone help me? I'm trying to make an indicator that has 2 moving averages, but when changing the timeframe of the chart, the periods of the moving averages also change. example: in the time of 1 hour it shows me 2 SMA of 10 and 20 and when changing to the daily chart it shows me 2 SMA of 30 and 60. thanks