2

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 = [...]

2 Answers2

1

for the below part,

for i in schedule.value:...

replace with,

for i in getattr(schedule, value):...

It should work. Here is the reason, in the for loop you'll get value as tuesday, wednesday, thursday, sunday or monday and it's a string. So if you access it like schedule.tuesday, definitely it should be worked. But unfortunately, you are accessing schedule.value and the value also a variable and there is no attribute called value in schedule module. So one of the easiest and more pythonic solutions is to use getattr(). I took below answer from What is getattr() exactly and how do I use it? the question.

getattr(object, 'x') is completely equivalent to object.x.

There are only two cases where getattr can be useful.

  • you can't write object.x, because you don't know in advance which attribute you want (it comes from a string). Very useful for meta-programming.
  • you want to provide a default value. object.y will raise an AttributeError if there's no y. But getattr(object, 'y', 5) will return 5.

Now you'll get a better understanding to use the getattr() function and for more information check the official documentation or the question that I've mentioned earlier.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • You're welcome @m4rk_Henry_ftw, just updated my answer to know more about what you did wrong and this the pythonic way to solve such a situation. If you didn't get any point or if you need anything to know don't hesitate to comment below, happy to help you! – Kushan Gunasekera Jan 26 '21 at 01:56
0

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]:...
Axiumin_
  • 2,107
  • 2
  • 15
  • 24