[Instructor] Let's wrap up this chapter by taking a look at a few examples of how to work with calendars in Python. Python's library provides a couple of useful utilities for working with calendars in both text and HTML formats. Again, this is going to come up from time to time. You'll have to work, not just with dates and times, you'll have to work with calendars. Let's open up calendars underscore start dot py, and in order to use the calendar module in a Python app, you first need to import that module. So let's go ahead and do that. So this statement will import all of the various classes from the calendar module into my app.
Let's start off by just creating a plain text calendar, and to do that, I'm going to use the text calendar class. So I'll write some code for this, and I'll give it the calendar dot Sunday to indicate which day of the week I want it to start on, and then I'm going to create a variable named st. And I'm going to ask the calendar class to format a month for me, and I need to give it the specifics. So I want 2017, and January, and rest of the parameters, and then I'm going to print st.
So the format month method lets me format a particular month into a text string, and then I'm just printing the results. So, I'm going to save this, and then I'm going to run it, and I'll show the output window. So let's run, and there you can see that January 2017 is being printed out. So Sunday is the first day because of this parameter right here, and then I've got one, two, three, four, five, all the way up through the end of the month. So now lets just change the parameter to Monday, and lets save and run it again.
And now you can see that Monday is the first day of the week and it ends up here with Saturday and Sunday as the last two days. So now let's see how to do this same thing in HTML. For that, I'll use the HTML calendar class, and I'll comment out that for now. So I'll write hc equals calendar dot HTML calendar, and once again, I'll start it off on a Sunday, and I'll use format month again, and I'll print the results.
Pretty much doing the same thing, only in the case, not using text, using HTML, once again, starting on Sunday, and formatting the month of January in 2017. So I'll save and run, and now you can see that a whole bunch of HTML came back, and this HTML represents the table based calendar that I have created. And if you look in the HTML, you can see it says January 2017, and you can also see that the Python calendar library has put on some helpful class attributes for me here.
If I wanted to define some CSS classes like month, and Sunday, and Monday. and whatever else, I can do that in any CSS code that I want to include this HTML in. You can also use the calendar class to perform common operations on dates. For example, I might have a need to iterate over the days of a given month, and I can use the itermonthdays method for that. So let's go back and try that out. So I'll write for i in c dot itermonthdays.
Got to uncomment this guy because we need the calendar, and we'll set that back to Sunday, and I'll iterate over 2017 in the month of August. And I'll just print i. So this function will return numbers that represent each day of the month. All right, let's run it. Okay, so I'm going to scroll up to results here. So in August, there are 31 days, right? So here's day number one, and if we scroll down, you'll see day 31 followed by a couple of zeros.
The zeros at the start and the end indicate that there are days in that week that belong to another month. For example, in this case, the Sunday and the Monday in this output happen to belong to the month of July. And the first time we see the number one, that's a Tuesday, and then two was the Wednesday, and so on. To the calendar class also provides some useful utilities for the current locale. I can loop over the names in the month names and day name properties on the calendar class so I don't have to code in my application that knows what the names of the months are around the world.
The system's locale will just give them to me based upon where the user happens to be located. So let's try that out. All right, so I'll have a for name in calendar dot month name, and I'll just print the name, and then I'll have for day in calendar dot day name, and I'll just do the same thing. Pretty simple. Let's go ahead and run this.
And you can see that because I'm here in the US, I'm getting the months January, February, March, and so on, and the day names as well: Monday, Tuesday, Wednesday. So if I was in another part of the world, or my local system here was set to be some place, in say Europe for example, I would get the locale based names. I can get the names of both the months and the days as they would appear in the current locale for where the computer is or for where the system happens to be set to. Let's do one more example.
Let's suppose I had a team of people, and the team met on the first Friday of every month, and I wanted to write a script that would print out what those dates would be for the upcoming year. So that way, I could give that list of days to my team members, and they would know what the meeting dates for the next year are going to be. So what I need to do is calculate when the first Friday happens in each month, and then calculate the date that represents that day. So let's go ahead and write some code to do this.
So I'll write print team meetings will be on, and then I'm going to have a loop for m in range one to 13. So I'm going to loop over all the months, and remember 13 is non-inclusive in the range. So this would give me months one through 12. What I'm going to do is get an array of weeks that represent each one of the month. So I'll write cal equals calendar dot month calender, and I'll specify the year of 2018, and then the m which represents the month number.
So each time through this loop, m will be a month number, and I'm using the calendar class's month calendar function to get an array of weeks that represent the days in the given month. Now I'm going to write a couple of variables here. So week one is equal to cal zero, and then week two is equal to cal sub one. The first Friday of that month has to be somewhere within the first two weeks. So I've got two local variables here, week one and week two, which I retrieve from this cal array that I got back.
I then just need to see which of the two weeks has the first Friday. So, to do that, I'm going to use the calendar's Friday constant to index into each of these array. So I'll write if week one, and I'll say calendar dot Friday, is not equal to zero, then meet day equals week one, and that would be calendar dot Friday.
Else, meet day equals week two of calendar dot Friday. And remember, we saw this earlier. If the day number of a particular day in the week is zero, then that means that that day is part of a different month. If the Friday value of the first week is zero, that means that the Friday was in the previous month, and this particular month starts on a Saturday. So the first Friday has to be within the second week.
So now I have my meet day variable set to the date of the first Friday, and I just need to print out the month name and that day. So I'll write print, and I'm going to do some formatting here. I'll say percent 10 s and percent two d, and then I'll give it calendar dot month name indexed by m and our meet day. All right, so let's run this.
Okay, so you can see that we've looped through all the months, and now we have a list of the first Fridays of each month. So the team meetings are going to be on January 5th, February 2nd, March 2nd, April 6th, and so on. So now, I can give this list of dates to my team, and they'll know what the actual dates are for the first Friday in each one of the months when we're going to have our team meeting. Such is the power of Python.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML