Week 8: new concepts

try and except statements

try and except are a construct that allows you to attempt to perform an operation, but keep going if it fails (causes an error).

The most common use of try and except for our kind of work is when you are looping through a dataset and trying to grab, test for, and/or perform an operation on each of the items in the dataset—BUT you don't know what you're trying to do will work for every item.

For example take the list below.

Let's say that you wanted to divide everything in this list by itself, using a for loop:

One way to avoid this error is to put a type check in your code before you print, and only print the item if type == int. But another way to do this, if you want to ignore any list items that aren't numbers and still print out all the numbers in the list divided by two, is to use try and except.

Note that if you specify the type of error, Python will only ignore ("suppress") those kinds of errors. That means that if your code encounters a different type of error, it will still break.

To handle this, you have two options.

  1. if you know all of the kinds of errors you are likely to encounter, you can put them all in the except statement, using the syntax shown below.
  2. if you want to ignore ALL kinds of errors, you can just use a bare except.

sleep() function

As you've seen by now, code runs very fast! If you want your code to 'take a break' and pause for a while at any point during the execution of your script, use the sleep() function from the time module.

sleep() is useful for many things. You'll probably use it most in one of two scenarios:

  1. you are using an API that imposes a rate limit on you, meaning that you can only make a certain number of API calls per second.
  2. you want to print output to the terminal more slowly, so that you can monitor what your code is doing while it runs.

Parsing datetime objects

We'll import the libraries we want to use to parse our datetime objects.

The first is called dateutil, and you can read more about it here: http://dateutil.readthedocs.io/en/stable/

The second is just called datetime, and you can read more about it here: https://pymotw.com/2/datetime/

Dateutil and datetime modules both do a lot of useful stuff. But in this case, we don't need all of 'dateutil', just the part called 'parser', and we only need datetime class from datetime.

When you print the parsed timestamps individually, they look pretty much the same as the strings, but you can see that they're a different type of object. When you put them in a list and then print the list, you can see their true nature.

Getting individual date/time parts from datetime objects

parser identifies and parses strings that look like dates and/or times, and transforms them into datetime objects that we can work with easier. It's pretty good at automatically detecting what parts of the string are the month, day, second, etc.

Sorting datetime objects

You can sort datetime objects easily, too.

Converting datetime objects back to strings

You can use the builtin strftime function to convert a datetime object (part of all of it) into a string, formatted in whatever way you choose. See the docs for more information on how to print specific date/time values: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Writing your own functions

Writing your own functions can make your code easier to read, easier to modify, and (sometimes) shorter! Let's take the example from [52] above, and re-write it with a function.