3 Writing robust code and error handling

 

This chapter covers

  • Types of errors in Python
  • How to effectively handle exceptions
  • Restricting function input data types
  • Making your code cleaner

In the last chapter, we discussed using source control to make it easier to track code changes and collaborate with others. As we discussed, source control is a key software engineering aspect that is incredibly useful for data scientists. Building on this foundation, we will delve further into applying software engineering concepts to data science in this chapter by discussing how to write robust code and handle errors. Suppose you’re a data scientist working on the customer churn model we discussed in the preceding chapters. While being able to easily share your code with others via source control is great, it can also be difficult to dissect someone else’s code. Making your code easy-to-read is a crucial aspect to both making it easier to collaborate with others on the same codebase, as well as making your code more maintainable in the future. One example of clean code is being more explicit about the inputs and outputs of functions (such as the data types), which is an area we’ll tackle in this chapter.

3.1 Improving the structure of your code

3.1.1 PEP8 standards

Example of a poorly written script

Importing packages

Avoiding compound statements

Naming conventions

Never use a builtin function or keyword as a variable name

Avoiding global variables

3.1.2 Using pylint to automatically check the formatting and style of your code

3.1.3 Auto-formatting your code to meet styling guidelines

3.2 Avoiding repetitive code

3.2.1 Replacing missing values in a data frame

3.2.2 Generate metrics for multiple classification models

3.2.3 Avoiding long if-else blocks

3.2.4 Modularizing your code

3.3 Restricting inputs to functions

3.3.1 Adding type hints to our modularized code

3.4 Clean code summary

3.5 How to implement exception handling in Python

3.5.1 What types of errors can we get in Python?

3.5.2 Using try/except to bypass errors