Clean Code: Python Application Flashcards
What is Encapsulation?
Groups data and methods –> Basically a Class does that!
What is Polymorphism?
Different classes implement the same method but have different outputs.
Example: Both the Dog and Cat classes inherit from the Animal class and implement their own version of the make_sound() method.
Creating objects
my_dog = Dog(“Buddy”) # Object of type Dog
my_cat = Cat(“Whiskers”) # Object of type Cat
# Using methods
print(f”{my_dog.name} says {my_dog.make_sound()}”)
# Output: Buddy says Woof!
print(f”{my_cat.name} says {my_cat.make_sound()}”)
# Output: Whiskers says Meow!
Explain what is the correct naming convention
- Choose a descriptive name that reflects the code’s purpose.
- Underscores are recommended for package names, modules, functions, and variables(my_function)
- Uppercase letters are recommended for classes(MyClass)
What does a package manager do, what package manager does Python use?
- Automates installation, updates, configuration, and removal of software
- In Python we use pip as a package manager
What are the benefits of using a virtual environment?
- Isolate Projects: Keeps your project separate from others.
- Prevent Conflicts: Avoids package conflicts and version mismatches.
- Manage Dependencies: Create a dedicated environment for project-specific libraries.
What is venv? How do we set it up?
- Built-in Tool: Comes with Python to create virtual environments.
- Encapsulated Environment: Allows separate package installations per project.
- Easy Setup: Use ‘python -m venv <env_name>' to create a new environment.</env_name>
What is Linting? What tool does python use for linting and how do we initialize it?
Linting is the process of analyzing source code to identify errors, style issues and inconsistencies.
We use Flake8 tool that checks code against Pep8 python style guide
To initialize flake8 we write python flake8 my_file.py
What is Docker?
Docker is a tool used for code sharing and development
Explain how the Simple Module Structure looks like
- It is used for single module python projects
my_project/ # Project directory (can use hyphens in the directory name)
│
├── README.md # Optional: Project description and documentation
├── my_module.py # The single Python module (use underscores for module names)
├── requirements.txt # Optional: List of dependencies (if any)
└── LICENSE # Optional: License file for your project
Explain how the Package module looks like
Multiple modules and packages
my_project/
│
├── my_package/ # Main package directory
│ ├── __init__.py # Initializes the package
│ ├── module1.py # First module
│ └── module2.py # Second module
│
├── tests/ # Directory for test scripts
│ └── test_module1.py # Test for module1
│
├── data/ # Directory for data files (optional, if needed)
│ └── sample_data.csv # Example data file (optional)
│
├── docs/ # Directory for documentation
│ └── index.md # Main documentation file
│
├── README.md # Project documentation (main)
├── requirements.txt # List of dependencies
├── setup.py # Packaging script for distribution
├── LICENSE # License for the project
Explain how the Monolith structure looks like
Multiple packages, modules, supporting files and resources.
my_project/
│
├── src/
│ └── my_package/
│ ├── __init__.py
│ ├── main.py
│ ├── module1.py
│ ├── module2.py
│ └── config/
│ ├── settings.yaml
│ └── additional_settings.yaml
│
├── tests/
│ ├── test_module1.py
│ ├── test_module2.py
│ └── …
│
├── data/
│ └── input.csv
│
├── docs/
│ └── index.md
│
├── README.md
├── requirements.txt
└── LICENSE
What are __init__.py files used for?
- Used within a package, tells python that directory should be treated as a package.
Project example:
my-project/
__init__.py
module1.py
subpackage/
__init__.py
In file:
# my-project/__init__.py
from .module1 import some_function1
from .subpackage.submodule import some_function2
Now we can import like this in the main:
import my_project
How does the Config.cfg file look like?
[database]
host = localhost
port = 5432
username = user
password = secret
How does the Config.yaml look like?
database:
host: localhost
port: 5432
username: user
password: secret
How does the Config.json look like?
{
“database”: {
“host”: “localhost”,
“port”: 5432,
“username”: “user”,
“password”: “secret”
}