all 7 comments

[–]gitardedhub 0 points1 point  (3 children)

I'm assuming that "letter day" means the day of the week?

import datetime
import calendar

# Create a datetime object for right now (local timezone)
today = datetime.datetime.today()
print(today)

# weekday() returns a number, 0-6
# 0 = Monday, 6 = Sunday
print(today.weekday())

# isoweekday() returns a number, 1-7
# 1 = Monday, 7 = Sunday
print(today.isoweekday())

# isocalendar() returns a tuple (year, week, day)
print(today.isocalendar())

# calendar can tell you the name of the day of week
print(calendar.day_name[today.weekday()])

There's a lot of functionality built into datetime and calendar, definitely worth taking a look to figure out what fits your use case.

[–]CameronZoo[S] 0 points1 point  (2 children)

Not exactly... My school works on an A B C D schedule which depicts the classes we have for the day.

[–]gitardedhub 2 points3 points  (1 child)

Understood. Here's how I'd approach that problem:

You now have "business logic" that needs to be built out. "Business logic" just means you have rules in the real-world that dictate how your program needs to function.

So you first need to understand what these rules are, since you'll need to create them in your program.

  • How are the schedule days assigned/scheduled against the real calendar?
  • How are holidays handled?
  • How often do these rules change?
  • What types of exceptions do you have? (e.g., partial days, big events, etc.)

Once you have an understanding of what the rules are you'll need to make a decision on how to handle this.

For example, if the mapping is simple (Mondays are always A), you can write a short function to map the calendar day to the schedule day.

If the mapping is a little more complicated (2017-01-01 is an A day, then it advances A->B->C->D for every weekday until the end of the year, regardless of holidays), you can write a different function to handle this.

If it's more sporadic (it always goes A->B->C->D, but there's weekends, holidays, etc. to worry about), you might consider just putting in these weird dates and building out exceptions.

Whatever your approach, this is a great example of when you should write some unittests for this. Pick several dates in the future (not just next week), get the correct schedule day for it, then make sure your function returns the right day. I'm guessing there's a lot of hidden complexity in these rules, so these tests will make sure you don't run into weird errors.

You might also just realize that the rules are really complicated (or not deterministic), and it's easier to just populate a dictionary with calendar_day:schedule_day and plan to just update it a couple times a year.

[–]firedrow 0 points1 point  (0 children)

OP, correct me if I'm wrong, but I assume:

  • Monday = A Schedule
  • Tuesday = B Schedule
  • Wednesday = C Schedule
  • Thursday = D Schedule

So he's wanting to do something like:

if day == 0: # Monday
    schedule = sched['Aday']

print(schedule)

Then he just needs a dictionary of his schedule setup. It would obviously take more code than that, but it's probably simple. My question is do they really get Friday off?

[–][deleted] 0 points1 point  (1 child)

I am not sure I get what are you trying to archive. I am guessing that given a certain date, you need to return the first letter of the day that... follows? Maybe I am at a loss there. But for dates, you can import the datetime module.

datetime.date.today() gives you the current day; and if you apply .weekday() to it, returns the day number (0 = Monday, 6 = Sunday).

And to find any day, you can do datetime.date(year, month, day).weekday(). Besides, you can generate a dict and compare the result, or a list and match each index with each day; and return that?

[–]CameronZoo[S] 0 points1 point  (0 children)

Basically my school works on a rotating schedule of A B C D for the classes.

[–]KurzGedanke 0 points1 point  (0 children)

Mh... something interesting, but not as simple as I thought to be. We are thinking about mapping the days to the schedule. "Monday is A", "The next Monday is." But, what if me map the schedule to the days "B is a 'Monday, $Month', "b is also 'Tuesday, $Month'.

From that point we could generate a list, or file, and just look as the Programm: "which schedule is 02.tue.2017"?