This repo shows how to write better Python code using modern tools and practices. We cover everything from setting up environments to automating code quality checks. The goal is to make Python development faster, more reliable, and easier for teams.
PEP rules, especially PEP 8, are Python's style guide. They help you write code that's:
- Consistent: Looks the same across projects and teams
- Readable: Easy to understand and maintain
- Collaborative: Reduces arguments during code reviews
- Professional: Follows community best practices
Skip these rules and you'll end up with messy, error-prone code that's hard to work with.
These tools catch problems before they become bugs:
- Linters (flake8, ruff): Find style issues, unused code, and potential errors
- Formatters (black, isort): Automatically fix code formatting
- Type Checkers (mypy, ty): Check your type hints to prevent runtime errors
Modern tools like ruff combine everything into one fast package. Traditional tools give you more control but take longer to run.
Each branch shows a different part of Python development. Switch between them to see examples in action.
Learn modern Python setup using UV - a fast package manager from Astral.
- Install UV on any platform
- Create virtual environments with
uv venv .venv --python 3.12(no need to manually install Python versions first - UV handles it!) - Compare UV vs pip for installing packages
- Use
uv initto start new projects
Examples: See how UV speeds up package installation and handles dependencies better than pip.
Takeaway: UV makes environment management simple and fast - no more manually installing Python versions for each venv.
Compare old-school and modern Python tooling.
Traditional Tools:
- flake8: Checks for style and errors
- black: Formats your code automatically
- isort: Sorts import statements
- mypy: Checks type annotations
Modern Tools:
- ruff: Does it all (linting, formatting, imports) super fast
- ty: Modern type checker
See: Config files, before/after examples, and speed comparisons.
Takeaway: Modern tools are 10-100x faster and just as good.
Containerize your Python apps with Docker.
- Basic Dockerfiles for Python projects
- Multi-stage builds to reduce image size
- Compare build times and sizes across different base images
- Best practices for Python in containers
Examples: Alpine vs Debian images, dev vs prod setups.
Takeaway: Good Docker setup means faster builds and smaller images.
Automate testing and code quality with GitHub workflows.
- Uses pip, flake8, black, isort, mypy
- GitHub Actions workflow for PR checks
- Uses UV, ruff, ty
- Faster workflow with better caching
demo4test/legacy: Bad code that fails legacy CIdemo4test/legacy#1: Good code that passesdemo4test/modern: Bad code for modern CIdemo4test/modern#1: Good code that passes
Check: GitHub Actions tab to compare run times.
Takeaway: Modern CI is faster and catches more issues.
Stop bad code before it gets committed.
- Set up pre-commit with UV
- Configure hooks for ruff and ty
- See how it blocks commits with issues
- Examples of fixing problems before committing
Takeaway: Pre-commit keeps your codebase clean from day one.
An experimental project for smart document extraction.
- AI-powered PDF and image processing
- OCR for scanned documents
- Intelligent content analysis
- Built with modern Python patterns
Tech: Computer vision, NLP, async Python, type safety.
Note: Shows how to apply these best practices to complex real-world apps.
-
Clone this repo:
git clone https://github.com/hdharmarathna/Python_BP.git cd Python_BP -
Pick a branch:
git checkout Demo_01
-
Follow the instructions in each branch's README.
-
Try the tools: Install UV, ruff, etc. and run the examples.
- UV - Fast Python package manager
- Ruff - Fast linter and formatter
- Ty - Modern type checker
- Pre-commit - Git hooks framework
- PEP 8 Guide - Python style rules
- Black - Code formatter
- MyPy - Type checker
- Flake8 - Linter
These tools and practices make Python development smoother and more fun. Use this repo to learn them hands-on!