Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Enumerations: Two Practical Examples

00:00 Using enumerations. Two practical examples. Enumerations are great when you need to replace sets of related magic numbers such as HTTP status codes, computer ports, and exit codes.

00:14 With an enumeration, you can group these numeric constants and assign them readable and descriptive names that you can use and reuse in your code later. Let’s say you have the process_response() function seen on screen that retrieves and processes HTTP content directly from the web.

00:50 This function takes an HTTP response object as an argument. Then it gets the status code from response using the .getcode() method.

01:00 The match/case statement sequentially compares the current status code with some standard status codes provided as magic numbers.

01:07 In this example, if a match occurs, then the code block in the matched case runs. If no match occurs, then the default case runs. Note that the default case is the one that uses an underscore as a match criterion.

01:21 The rest of the code connects to a sample web page, performs a GET request, retrieves the response object, and processes it using the process_response() function.

01:41 The finally clause closes the active connection to avoid resource leaks. Even though this code works, it can be challenging to read and understand for people who are unfamiliar with HTTP status codes and their corresponding meanings.

01:59 To fix these issues and make your code more readable and maintainable, you can use an enumeration to group the HTTP status codes and give them descriptive names.

02:18 This version of the code adds a new enumeration called HTTPStatusCode to the application. This enum groups the target HTTP status codes and gives them readable names.

02:30 It also makes them strictly constant, which makes your app more reliable.

02:43 Inside process_response(), you use human-readable and descriptive names that provide context and content information. Now, anyone reading the code will immediately know the match criteria are HTTP status codes, and they’ll also spot the meaning of each target code quickly.

03:39 Another interesting use case of enumerations is using them to re-create the different possible states of a given system. If your system can be in exactly one of a finite number of states at any given time, then your system works as a state machine.

03:54 Enumerations are useful when you need to implement this common design pattern. As an example of how to use an enum to implement the state machine pattern, you create a minimal disk player simulator. To start, go ahead and create with the content seen on screen.

04:18 Here you define the State class. This class groups all the possible states of your disk player: empty, stopped, paused, and playing. You can now code the DiskPlayer player class, which would look similar to the code seen on screen.

04:43 The DiskPlayer class implements all the possible actions that your player can execute, including inserting and ejecting the disc,

05:10 playing,

05:19 pausing,

05:34 and stopping the player. Note how each method in DiskPlayer checks and updates the player’s current state by taking advantage of the state enumeration.

05:50 To complete your example, you’ll use the traditional if __name__ == "__main__" idiom. To wrap up a few lines of code that will allow you to try out the DiskPlayer class, here you first define an actions variable, which holds the sequence of methods that you’ll call from DiskPlayer in order to try out the class.

06:19 Then you create an instance of the DiskPlayer class. Finally, you start a for loop to iterate over the list of actions and run every action through the player instance, and that’s it.

06:32 The disk player simulator is ready for a test. To run it, execute this command at the command line. The command output shows that the app has gone through all of the possible states.

06:48 Of course, this example is minimal and doesn’t consider all the potential scenarios. It’s a demonstrative example of how you could use an enumeration to implement the state machine pattern in your code.

07:02 In the next section of the course, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.