Virtual Environments
- Setting up a virtual environment is useful for managing project dependencies.
- Using
uv
simplifies the process of creating and managing virtual environments. - There are several options other than
uv
for managing virtual environments, such asvenv
andconda
. - It’s important to version control your project from the start,
including a
.gitignore
file.
Creating A Module
- Python modules are simply directories with an
__init__.py
file in them - You can add the path to your module directory to
sys.path
to make it available for import - You can use dot notation in your imports to specify the module, file, and function you want to use
Class Objects
- Python classes are defined using the
class
keyword, followed by the class name and a colon. - The
__init__
method is a special method that is called when an instance of the class is created. - Class methods are defined like normal functions, but they must
include
self
as the first parameter.
Unit Testing
- We can use
pytest
to write and run unit tests in Python. - A good test is isolated, repeatable, fast, and clear.
- We can use fixtures to provide data or state to our tests.
- We can use monkey patching to modify the behavior of functions or classes during testing.
- Test Driven Development (TDD) is a practice where we write tests before writing the code to make the tests pass.
Extending Classes with Inheritance
- Inheritance allows us to create a new class that is a specialized version of an existing class
- We can override methods and properties in a subclass to provide specialized behavior
Inheritance and Composition
- Composition allows us to build complex functionality by combining several smaller, simpler classes
- Composition promotes separation of concerns, reusability, flexibility, and maintainability
- By using abstract base classes, we can define interfaces that subclasses must implement, allowing for flexibility in our code design
Static Code Analysis
- There are many static code analysis tools available for Python, each with its own strengths and weaknesses.
- Ruff is a fast linter and code formatter that can replace several
other tools, including
flake8
,pylint
, andisort
. - MyPy is a static type checker that can help catch type-related errors in Python code.
Building and Deploying a Package
- We can build our package locally using
uv build
, which creates adist/
directory with the built package files. - We can use GitHub Actions to automate the building and deployment of our package.
- GitHub Actions workflows are defined using YAML files in the
.github/workflows/
directory. - We can trigger our workflow to run on specific events, such as pushing a tag that follows semantic versioning.
- We can add additional steps to our workflow, such as running tests and static code analysis, to ensure code quality before deployment.