StiloOS
StiloOS

Reputation: 11

Check condition after each line of code in Python

I have a function with a script that reads out some data and does some actions in the SAP-GUI. Perhaps, there will pop up a message window in the GUI, which I want to catch to proceed with the script. To do so, I have to check the condition, if a popup occurs after each line of my script.

What I have:

def mysap_script():
    command1
    command2
    command3

What I have to do, but want to avoid:

def mysap_script():
    command1
    if message_window opens: do some actions
    command2
    if message_window opens: do the same action as before
    command3
    if message_window opens: do the same action as before

Is there an efficient and easier way for this problem?

Upvotes: 1

Views: 556

Answers (2)

Hamid Rajabi
Hamid Rajabi

Reputation: 69

It seems that your code can benefit from asyncio . By using the code bellow you will (almost) run both functions at the same time. It also looks cleaner.

import asyncio

async def mysap_script():
    command1
    await asyncio.sleep(0)
    command2
    await asyncio.sleep(0)
    command3

async def check_window():
    if message_window opens: do some actions
    await asyncio.sleep(0)

async def main():
    await asyncio.gather(count(), check_window())

if __name__ == "__main__":
    import time
    s = time.perf_counter()
    asyncio.run(main())
    elapsed = time.perf_counter() - s
    print(f"{__file__} executed in {elapsed:0.2f} seconds.")

Upvotes: 0

Adithya
Adithya

Reputation: 1728

You could wrap the commands with a function that check the condition you need to check.

def wrapped_cmd(cmd):
    if cond:
        something()
    cmd()

and call this instead, like this

def mysap_script():
    wrapped_cmd(cmd)

Upvotes: 0

Related Questions