From the course: Python Projects

Program architecture - Python Tutorial

From the course: Python Projects

Program architecture

- [Instructor] Now that I've captured my requirements, it's time to figure out how I'm going to organize and structure my code for this application. Python is an object-oriented programming language, so I'll need to consider the objects in my program and the classes I might need to create. A good starting point for that is to look at the various requirements, use cases, and user stories I've written, and pick out the nouns. For example, in the set of functional requirements shown here, I see quote, forecast, location, trends, article, content, email, and recipients. These are all potential objects within my program. Now, I won't necessarily end up needing to create classes for all of these, but gathering a list of nouns serves as inspiration for elements of my program that I might want to instantiate as objects. Looking at that list of nouns from all of my requirements, I notice that the first handful of nouns, quote, forecast, location, trends, and article, are all related to content. And content was already one of the words on that list. So, I mentally group those together. Along the same lines, recipients, time, and credentials all relate to sending an email. And email was conveniently already captured on the list. So, I mentally group those together as well. That leaves me with content, email, and GUI as potential candidates to turn into classes. Now, in addition to figuring out names for the classes I might want to create, I also need to figure out their behaviors and responsibilities. To do that, I looked through my requirements again to extract simplified verb phrases, such as generate quote, retrieve forecast, format content, and send email. These are all things my program will need to do. I then assign those behaviors to one of my three potential classes, based on where it made the most sense. The responsibility for generating and retrieving the various types of content made sense to put in the content class. I decided to make the email class responsible for formatting the content into a message, as well as sending the email itself. And finally, I assigned the remaining configuration-related behaviors as responsibilities of the GUI. These simplified verb phrases easily translate into a first draft of method names. All that gives me an idea of how to structure my program in terms of classes and their corresponding methods, but there's still a lot left to figure out. For example, where should the list of digest recipients be stored? The GUI is responsible for adding and removing recipients, but the email will need that list of recipients to know who to send the digest to. So, I thought it made more sense for that attribute to be part of the email class. Along the same lines, some part of my program will need to keep track of what time to send a digest email each day. I decided that a send time property made sense as part of the email class as well, even though the GUI has a responsibility to configure it. Now, if I wanted to, I could continue this process, attempting to predict and map out every attribute and method my entire program will ever need. And if I was working on this project with a team of developers, it would be important to work out those details together so everyone knows exactly what needs to be implemented. But as a solo developer, creating a Python project for the sake of learning and fun, and with a project this small in scope, the time and effort needed to reach a high level of detail may not be worth it. This rough plan with three classes gives me enough of a structure and starting point to begin writing code. And as I start implementing parts of the program, I'll learn new things along the way and likely make changes to this original plan. Spoiler alert: if you stick with me until the end of this course, the final product will differ a bit from the plan you see here. If you want to learn more about how to organize and structure programs written in object-oriented languages like Python, I recommend checking out the Object-Oriented Design course on LinkedIn Learning.

Contents