Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

python - Using variable to get list from imported module

I am trying to make a script where my zoom classes would auto open based on the schedule.

I made a module named schedule which contains separate lists of each weekday's routine.

In the main file, I made a dictionary where the key is the integer value we get after using datetime.today().weekday() and the value is its corresponding weekday's name such as {0:monday}

In my function, if today's weekday integer value matches with the dictionary key, then it will call its corresponding value weekday from the schedule module. But when I run it, it's saying " AttributeError: module 'schedule' has no attribute 'value' "

Here's part of my code for reference

#Monday=0, Tuesday=1 , Wednesday=2, Thursday=3, Sun=6.
weekday = {'1':'tuesday',
       '2':'wednesday',
       '3':'thursday',
       '6':'sunday',
       '0':'monday'}
#Function for opening and closing link in webbrowser.
def open_webbrowser():
global activation
day = datetime.today().weekday()
for key, value in weekday.items():
    if int(key) == day:
        for i in schedule.value:...

And here's the schedule module. I replaced the original links with 'zoomlink'

#Class routine.
sunday = [
[zoomlink,'09:01','09:39'],
[zoomlink,'10:01','10:39'],
[zoomlink,'11:01','11:39'],
[zoomlink,'12:01','12:39']
]
monday = [...]
tuesday = [...]
wednesday = [...]
thursday = [...]
question from:https://stackoverflow.com/questions/65894624/using-variable-to-get-list-from-imported-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason your code isn't working is because schedule.value tries to get a variable called value in the schedule module, when you meant for it to expand to something like schedule.sunday.

One way you can fix this is by encapsulating your lists in a dictionary, where you can access the lists with a string index such as "sunday" or "monday". If you were to do this, your schedule module would look something like this:

zoomLinkData = {
    "sunday": [
        [zoomlink,'09:01','09:39'],
        [zoomlink,'10:01','10:39'],
        [zoomlink,'11:01','11:39'],
        [zoomlink,'12:01','12:39']
    ],
    "monday": [...],
    "...": [...]
}

In order to access the data in this dictionary in your loop, you could do this:

for key, value in weekday.items():
    if int(key) == day:
        for i in schedule.zoomLinkData[value]:...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...