Keeping Your Source Organized

Intuitive Python — by David Muller (38 / 41)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Maintaining Privacy in a Public World | TOC | Dodging Wildcard Variable S hadowing 👉

Just like the dinosaurs in Jurassic Park, Python codebases have a habit of breaking out from inside the fenced pens we try to create for them. One particularly vexing problem can be keeping project code organized. As time passes, code starts to live in places it shouldn’t and it can become increasingly difficult to find what files code should live in or where new code should be placed. In this section, we’ll explore a general strategy that uses Python’s built-in unittest module to keep a directory organized.

Maintaining Organization in a tests/ Directory

Many Python codebases contain tests to help verify that they are working correctly. Frequently, these tests are defined in a directory named tests/ at the top level of the codebase. The tests/ directory structure typically matches the directory structure in the corresponding source directory that is being tested.

Let’s consider a Python project with the following directory structure:

​ ├── j_park
​ │ ├── __init__.py
​ │ ├── dinosaurs
​ │ │ ├── __init__.py
​ │ │ ├── raptor.py
​ │ │ └── t_rex.py
​ │ └── fences
​ │ ├── __init__.py
​ │ ├── cable.py
​ │ └── electrified.py
​ └── tests
​ ├── __init__.py
​ ├── fences
​ │ ├── __init__.py
​ │ └──…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.