Skip to content

hdharmarathna/Python_BP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python Modern Best Practices

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.

Why PEP Rules Matter

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.

Code Quality Tools

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.

Demo Branches

Each branch shows a different part of Python development. Switch between them to see examples in action.

Demo_01: Python Environments with UV

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 init to 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.

Demo_02: Code Quality Tools

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.

Demo_03: Docker for Python

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.

Demo_04: GitHub Actions CI/CD

Automate testing and code quality with GitHub workflows.

Demo_04_legacy: Traditional Approach

  • Uses pip, flake8, black, isort, mypy
  • GitHub Actions workflow for PR checks

Demo_04_modern: Modern Approach

  • Uses UV, ruff, ty
  • Faster workflow with better caching

Test Branches

  • demo4test/legacy: Bad code that fails legacy CI
  • demo4test/legacy#1: Good code that passes
  • demo4test/modern: Bad code for modern CI
  • demo4test/modern#1: Good code that passes

Check: GitHub Actions tab to compare run times.

Takeaway: Modern CI is faster and catches more issues.

Demo_05: Pre-commit Hooks

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.

DocRag: AI Document Processing

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.

Getting Started

  1. Clone this repo:

    git clone https://github.com/hdharmarathna/Python_BP.git
    cd Python_BP
  2. Pick a branch:

    git checkout Demo_01
  3. Follow the instructions in each branch's README.

  4. Try the tools: Install UV, ruff, etc. and run the examples.

Useful Links

These tools and practices make Python development smoother and more fun. Use this repo to learn them hands-on!

About

Learn modern Python development with UV, Ruff, Ty, and more. Covers environments, code quality, Docker, CI/CD, and pre-commit hooks. Hands-on demos for faster, reliable Python apps.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors