1

I'm not good with Python Classes. I know in this instance using a class would solve the problem of trying to call a function within a function but I'm not sure how to do it. This is a script that opens a website on a set time added in the variable introduced in the beginning. I want to call the openWebsite() function in setTime() function. I'm sure a class would solve my problems but I'm a bit new to Python.

import webbrowser
import time

specify a website, local time and desired time

page = "https://www.twitter.com"
today = time.strftime('%X %x')
timeToOpen = "09:53:36 02/06/22"

a function to open a website and print opened

def openWebsite():
    webbrowser.open_new(page)
    print("website opened")
    print(today)

compares real live time with a specified time in a variable in the beginning

def setTime():
    while time.strftime('%X %x') != timeToOpen:
        timeNow = time.strftime('%X %x')
        if timeNow >= timeToOpen:
            print("It's A Match")

I want to call openWebsite() here

            break
        print("waiting!")
        print (timeNow + " vs " + timeToOpen)
        time.sleep(3)

call a function

setTime()

PS: If you have any other suggestions to improve the code I would be very grateful

Thanks, Magz

Magzy
  • 25
  • 4

1 Answers1

1

Great question and one that touches on scope and argument passing approaches.

You can pass functions to a metafunction in a number of ways perhaps depending on your use case/coding style/ and PEP8. There are a few related answers with may help:

How do I pass a method as a parameter in Python

Passing functions with arguments to another function in Python?

For all of the methods below, you will need to put a parameter in your function for e.g 'func_a'(below), the function will then take on the parameter's name within the local/function scope:

def setTime(func_a):
    '''a function that opens website with
      some conditions(...)'''
    while time.strftime('%X %x') != timeToOpen:
        timeNow = time.strftime('%X %x')
        if timeNow >= timeToOpen:
            print("It's A Match")
            #call your function here
            func_a()
            break
    #..... rest of function

  1. By simply passing the function as an argument without calling ():
setTime(openWebsite)
  1. By wrapping in a tuple if you want/need to compress multiple args:
functions = (openWebsite,some_other_fuction)
#passing like this
setTime(*functions)

Writing function parameters like this:

def setTime(open_page,open_page_b):
    ''' code '''
  1. A dictionary of arguments:
functions = {'func_a':opneWebsite}
#passing like this
setTime(**functions)

With 3 you will need to use:

def setTime(func_a=None):
    ''' code '''

The converse of option 2/3 works using args or kwargs as parameters prefixed with * or ** respectively, for example:

def setTime(**kwargs):
    ''' code '''

Passing like this:

setTime(func=openWebsite)

with:

def setTime(func_a):
    '''a function that opens website with
      some conditions(...)'''
    while time.strftime('%X %x') != timeToOpen:
        timeNow = time.strftime('%X %x')
        if timeNow >= timeToOpen:
            print("It's A Match")
            #call your function here
            kwargs['func_a']()
            break
    #..... rest of function

Hope this helps <^_^>

fdsig
  • 268
  • 2
  • 5