Introduction to Python
The course is part of this learning path
In this first course, we introduce the Python Language, the declaration model, and how variables and functions are used in python.
Our learning objectives for this course are to introduce the python language and to be able to recognize and explain the core concepts of the Python language.
Okay, so what if we wanna parse date or time strings? To parse datetime strings, use one of the strptime methods. Now, there are two different versions, which return different representations of the date and time. In both cases you pass strptime, the time string followed by a template that uses the same directives as striftime. Time.stprtime returns a time_tuple. It is a named tuple. So to get the year for instance, you can use either tuple zero or tuple tm_year, datetime, struct_time returns a new date object. So, for example, if we take our data as a string, for the string we pass the template in, we take time_tuple, and we get the date string pass the template, pass out the detail for ourselves. Okay, that's kind of complex. There's an easier way to parse dates. The dateutil.parser module provides a parse function that will parse any date or datetime string without the need to put together derivatives, as with strptime.
The parse function has several flags to deal with variations in date formats, such as European style day first. The value returned by parse is a normal python datetime.datetime. So, for example, if we import dateutil and we take a date string, first of all, we list our sorted date strings, and then second we parse with dateutil parser.parse, which doesn't need a template. Okay so what about converting time stamps? Certain functions such as os.getintime, or os.getattime, return a unique style date stamp. This is the number of seconds since January 1st, 1970, a point in time usually called the Unix epoch. You might also get this version of a date and time from other sources. To convert this to a useful format, use datetime.fromtimestamp to convert to a datetime object, or time.localtime to convert to a time tuple. The time.time method returns the current date and time as a time stamp. Converting to a timestamp takes two steps. First get the date and time to a time tuple, and then call time.mktime. So, first we create a python datetime object. Second we extract the time tuple and use it to make Unix epoch time. Get epoch time for right now and we loop through the three epoch times. Now note Windows requires minimum values of 86,400, non-Windows platforms could use zero. We then convert the epoch time to time tuple, and then convert epoch time to python datetime object. Timezones are fairly poorly supported in the standard library. So, the daytime.daytime class has some UTC based methods, but the date YouTube module supports time zones out of the box. So, in addition that has features that can combine the most useful parts of a date time and calendar packages. If you use dateutil to pass a datetime string that has a time zone, it will automatically add that to the date or datetime object. The timezone is usually specified as the offset from UTC, so EDT would be minus four hours and PST would be minus eight hours. The calendar module provides two classes for generating calendars. The TextCalendar generates calendars as plaintext. And the HTMLCalendar generates calendars as HTML. Both support format month and format year methods. TextCalendar for convenience, also provides PR month and PR year methods, which called print on the strings returned by formatmonth and formatyear. For year calendars an option arguments specifies the number of months per row. Actually MyCalendar adds format year page, which returns an entire webpage, complete with hitters and optional CSS file names. You can add optional parameters for the width of the day column and the number of lines for each week. The defaults are two for the day and one for the lines per week, so you can alter the output to change to suit whatever your page or document object model requires. So, in our example, we create a TextCalendar object. We format one month as text, then we create an HTMLCalendar object and then we format one month as HTML.
Andrew is fanatical about helping business teams gain the maximum ROI possible from adopting, using, and optimizing Public Cloud Services. Having built 70+ Cloud Academy courses, Andrew has helped over 50,000 students master cloud computing by sharing the skills and experiences he gained during 20+ years leading digital teams in code and consulting. Before joining Cloud Academy, Andrew worked for AWS and for AWS technology partners Ooyala and Adobe.