Skip to content

VandanaJn/python-decorator-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Decorators: Engineering Resilient Architecture

This repository contains the source code and supplemental materials for the tutorial "Python Decorators Finally Make Sense." The goal of this project is to move beyond basic syntax and explore how decorators function as structural boundaries for building reusable, resilient software.

📂 Repository Structure

python-decorator-tutorial/
├── notebooks/                    # Interactive tutorials
│   └── decorator_tutorial.ipynb # Main learning resource
├── src/                         # Source code
│   └── authorize_decorator.py   # Production-ready decorator
├── tests/                       # Unit tests
│   └── test_authorize_decorator.py
├── examples/                    # Future examples
├── requirements.txt             # Dependencies
├── README.md                    # This file
└── CONTRIBUTING.md             # Contribution guidelines
  • notebooks/decorator_tutorial.ipynb: The primary learning resource. This notebook covers the journey from basic higher-order functions to building a production-ready @ai_retry decorator for handling unstable API calls.
  • src/authorize_decorator.py: A bonus professional-grade example. This script demonstrates how to implement a security layer using decorators to handle user authorization without cluttering core business logic.

🚀 Key Concepts Covered

1. Resilient Design

We build a self-healing @retry decorator. In modern development—especially when working with LLMs and network APIs—resilience is a requirement. This decorator abstracts the complexity of error handling into a separate, reusable layer.

2. The "Wrapper" Pattern

Understanding the anatomy of a decorator:

  • The Outer Function: The factory that accepts the original function.
  • The Inner Wrapper: The "interceptor" that executes logic before and after the core task.
  • @functools.wraps: Ensuring metadata (like function names and docstrings) remains intact for debugging and traceability.

🏆 The Challenge: The AI Feedback Loop

Task: Build a @graceful_tool decorator that:

  • Wraps a function meant to be used as an AI agent tool
  • Catches any exception that occurs during execution

The Design Choice: Instead of letting the program crash, what should the decorator return so the LLM understands what went wrong and how to fix it?

Think about it: If a tool fails because of a "Missing API Key" vs. "Invalid Input," should the LLM get the same message? 🤔

Share your solution! Create an issue or pull request with your @graceful_tool implementation and explain your design choices. Let's build better AI tools together! 🚀


🤝 Contributing

We welcome contributions! Here's how you can participate:

For the @graceful_tool Challenge:

  1. Create an Issue: Share your decorator implementation and explain your design choices
  2. Submit a PR: Add your solution to the repository with tests and documentation
  3. Discuss Ideas: Open issues to discuss decorator patterns and improvements

General Contributions:

  • Bug Reports: Use GitHub issues to report problems
  • Feature Requests: Suggest new decorator examples or improvements
  • Documentation: Help improve tutorials and examples

Please read our Contributing Guidelines for detailed information.


🛠️ Installation & Usage

  1. Clone the repo:
    git clone https://github.com/VandanaJn/python-decorator-tutorial.git
  2. Set up a virtual environment
    # Create the environment
    python -m venv venv
    
    # Activate it (Windows)
    .\venv\Scripts\activate
    
    # Activate it (Mac/Linux)
    source venv/bin/activate
  3. Open the Notebook: Use Jupyter Lab or VS Code to run notebooks/decorator_tutorial.ipynb to follow along with the video.
  4. Explore the Bonus Script: Check src/authorize_decorator.py to see how the pattern applies to security and access control.

🔗 Related Resources


💬 Let's Connect

If you found this helpful, I'd love to see your implementation of the @graceful_tool challenge!

LinkedIn: https://www.linkedin.com/in/vandana-jain-b44581388


This project is part of a series focused on building production-ready AI infrastructure and resilient software patterns.

About

VandanaJn/python-decorator-tutorial

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors