-2

I have several dictionaries for each day of the week, Sunday ["a", "b", "c"], Monday ["d", "f", "g"], and so on, they are located in another script (xscript.py), and I would want to print them out depending on the day of the week I'm currently on, something like this:

import datetime
import xscript

def print_daily_list():
    day_of_week = datetime.datetime.today().weekday()
    print(xscript.day_of_week)

I wouldn't want to create a bunch of if statements to replace day_of_week with "Sunday" or "Monday" depending on the day.

3 Answers3

1

I believe you can make a dictionary in xscript.py

dic = { 'Monday': ['a', 'b'], 'Sunday': ['e', 'f']}  # and all the weeks

and to access that I recommend creating a function in xscript, say

def get_dict(key):
    return dic.get(key, "Default List")

And now in your main file, you can call it like:

print(xscript.get_dict(day_of_week)
  • Thank you, I missed wrote, i have multiple dictionaries for each doy of the week not lists, I'm sorry, but Thank you very much for your response – Iker Castillo Jan 14 '21 at 16:13
0

Store them in a dictionary

days = {
    'Sunday': ['a', 'b', 'c'],
    'Monday': [1, 2, 3],
    'Tuesday': ['a', 'b', 'c'],
    'Wednesday': [1, 2, 3],
    'Thursday': ['a', 'b', 'c'],
    'Friday': [1, 2, 3],
    'Saturday': [1, 2, 3],
}

Then pass the day of the week to your script, and call the dictonary with the day.

days[day]
PacketLoss
  • 5,561
  • 1
  • 9
  • 27
0

This solution uses getattr to get the attribute of an object in this instance the module xscript. If the script module has a variable named Thursday and today is Thursday this script will get and print that variable.

day_of_week = datetime.datetime.today().strftime('%A')
print(getattr(xscript, day_of_week))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33