Skip to content

CoreSheep/Advanced-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Advanced Python Development Techniques

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                                                               โ•‘
โ•‘   ๐ŸŽฏ Decorators  |  โšก Generators  |  ๐Ÿ”ฎ Metaprogramming     โ•‘
โ•‘                                                               โ•‘
โ•‘   ๐Ÿงช Unit Testing  |  ๐ŸŽจ Design Patterns  |  ๐Ÿš€ Best Practicesโ•‘
โ•‘                                                               โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Master the art of advanced Python programming with hands-on examples

Python Flask License


๐Ÿ“š Table of Contents


๐ŸŽ“ Overview

This repository contains a comprehensive collection of advanced Python programming techniques learned through hands-on practice and real-world applications. The project demonstrates mastery of Python's powerful features that separate intermediate developers from advanced practitioners.

๐ŸŒŸ What Makes This Advanced?

  • Functional Programming Paradigms - Leveraging decorators and higher-order functions
  • Memory Optimization - Using generators for efficient data processing
  • Dynamic Code Generation - Metaprogramming with metaclasses and the type() function
  • Test-Driven Development - Building production-ready applications with comprehensive test coverage

๐Ÿ“ Project Structure

Advanced-Python/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ README.md                    # This file
โ”œโ”€โ”€ ๐Ÿ“„ LICENSE                      # MIT License
โ”‚
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ ๐ŸŽจ decorator/               # Decorator patterns and applications
    โ”‚   โ”œโ”€โ”€ decorator_timer_demo.py       # Performance timing decorator
    โ”‚   โ”œโ”€โ”€ decorator_cache_demo.py       # Memoization/caching decorator
    โ”‚   โ”œโ”€โ”€ decorator_logging_demo.py     # Logging decorator
    โ”‚   โ”œโ”€โ”€ decorator_demo.md             # Comprehensive decorator guide
    โ”‚   โ””โ”€โ”€ test.py                       # Decorator tests
    โ”‚
    โ”œโ”€โ”€ โšก generator/                # Generator functions
    โ”‚   โ””โ”€โ”€ generator.py                  # Generator examples
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ”ฎ metaprogramming/          # Metaclasses and dynamic programming
    โ”‚   โ””โ”€โ”€ metaprogramming_demo.py       # Dynamic class creation
    โ”‚
    โ””โ”€โ”€ ๐Ÿงช unit_test/                # Test-Driven Development
        โ””โ”€โ”€ demo/                         # Flask blog application
            โ”œโ”€โ”€ app.py                    # Flask API endpoints
            โ”œโ”€โ”€ requirements.txt          # Python dependencies
            โ”œโ”€โ”€ tests/                    # Comprehensive test suite
            โ”‚   โ”œโ”€โ”€ conftest.py          # pytest fixtures
            โ”‚   โ””โ”€โ”€ test_app.py          # Integration tests
            โ”œโ”€โ”€ templates/               # HTML templates
            โ””โ”€โ”€ static/                  # JavaScript and CSS

๐ŸŽฏ Topics Covered

๐ŸŽจ Decorators

Decorators are one of Python's most powerful features, allowing you to modify or enhance functions without changing their code. This section explores practical decorator applications:

๐Ÿ“Š Timer Decorator

Measure function execution time for performance optimization:

@timer
def matrix_multiply(a, b):
    return np.array(a) @ np.array(b)

# Output: Function matrix_multiply took 0.000123s to execute.

๐Ÿ’พ Cache Decorator

Implement memoization to speed up expensive computations:

@cache
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

๐Ÿ“ Logging Decorator

Automatically log function calls and arguments for debugging:

@log_function
def process_user_data(user_id, data):
    # Processing logic here
    pass

Key Concepts:

  • *args and **kwargs for flexible function signatures
  • Wrapper functions and closures
  • Function metadata preservation with functools.wraps
  • Practical applications in real-world projects

โšก Generators

Generators enable memory-efficient iteration over large datasets by yielding items one at a time instead of loading everything into memory.

Benefits:

  • ๐Ÿš€ Memory Efficient - Process large files without loading entire content
  • โฑ๏ธ Lazy Evaluation - Compute values only when needed
  • โ™พ๏ธ Infinite Sequences - Generate unlimited data streams

Use Cases:

  • Processing large CSV files
  • Reading log files line by line
  • Creating data pipelines
  • Implementing custom iterators

๐Ÿ”ฎ Metaprogramming

Metaprogramming is "code that writes code" - enabling dynamic class and function creation at runtime.

Dynamic Class Creation

Create user classes dynamically based on runtime requirements:

def create_user_class(class_name, attributes):
    """Dynamically generates user classes with specified attributes."""
    # Implementation uses type() to create classes on the fly
    return type(class_name, (object,), class_attrs)

# Create different user types dynamically
BasicUser = create_user_class("BasicUser", ["username", "email"])
PremiumUser = create_user_class("PremiumUser", ["username", "email", "subscription_level"])

Applications:

  • ๐Ÿ—๏ธ Building flexible frameworks
  • ๐Ÿ”ง Creating Domain-Specific Languages (DSLs)
  • ๐Ÿค– Automating repetitive class definitions
  • ๐ŸŽญ Implementing design patterns (Factory, Builder)

Key Concepts:

  • The type() function as a class factory
  • Metaclasses and __new__ method
  • Dynamic attribute assignment with setattr
  • Runtime code generation

๐Ÿงช Unit Testing (TDD)

Built a complete Flask blog application using Test-Driven Development principles. This hands-on project demonstrates production-ready testing practices.

๐ŸŽฏ Project: Intelligent Blog Application

A full-stack web application built using TDD methodology:

Features:

  • โœ… RESTful API with Flask
  • โœ… Create and retrieve blog posts
  • โœ… JSON data validation
  • โœ… Comprehensive test coverage
  • โœ… Beautiful responsive UI
  • โœ… Error handling and status codes

Tech Stack:

  • Backend: Flask 3.0.0
  • Testing: pytest 7.4.3, pytest-mock
  • Frontend: HTML5, CSS3, JavaScript (Fetch API)

Test Coverage

The project includes 5 comprehensive integration tests:

  1. โœ… test_posts_initialized_as_empty_list - Verify initial state
  2. โœ… test_get_posts - Test GET endpoint returns empty list
  3. โœ… test_create_post - Test successful post creation
  4. โœ… test_create_post_invalid_data - Test validation and error handling
  5. โœ… test_create_post_mock_posts - Test with mocked dependencies

API Endpoints

GET  /          # Render home page
GET  /posts     # Get all blog posts (JSON)
POST /posts     # Create new blog post (JSON)

Example Request:

curl -X POST http://localhost:5000/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "My First Post", "content": "Hello World!"}'

Response:

{
  "id": 1,
  "title": "My First Post",
  "content": "Hello World!"
}

TDD Workflow Demonstrated

  1. ๐Ÿ”ด Red - Write failing tests first
  2. ๐ŸŸข Green - Write minimum code to pass tests
  3. ๐Ÿ”ต Refactor - Improve code while maintaining test coverage

๐Ÿ› ๏ธ Environment Setup

Prerequisites

Ensure you have the following installed:

  • Python 3.10+ (Download)
  • pip (Python package manager)
  • virtualenv (recommended for isolated environments)

Quick Start

# Check Python version
python --version  # Should be 3.10 or higher

# Verify pip installation
pip --version

๐Ÿ’ป Installation

1๏ธโƒฃ Clone the Repository

cd ~/Documents
git clone <repository-url>
cd Advanced-Python-Development-Techniques/Advanced-Python

2๏ธโƒฃ Create Virtual Environment

Creating a virtual environment keeps your project dependencies isolated:

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate

# On Windows:
venv\Scripts\activate

You should see (venv) prefix in your terminal.

3๏ธโƒฃ Install Dependencies

Install required packages for different modules:

For Decorator Examples:

pip install numpy

For Unit Testing / Flask Blog:

cd src/unit_test/demo
pip install -r requirements.txt

This installs:

  • Flask 3.0.0 - Web framework
  • pytest 7.4.3 - Testing framework
  • pytest-mock - Mocking utilities
  • Werkzeug 3.0.1 - WSGI utility library

๐Ÿš€ Usage

Running Decorator Examples

# Navigate to decorator directory
cd src/decorator

# Run timer decorator demo
python decorator_timer_demo.py

# Expected output:
# Function add took 0.000002s to execute.
# result1: 4998
# Function matrix_multiply took 0.000123s to execute.
# result2: [[19 22]
#          [43 50]]

# Run cache decorator demo
python decorator_cache_demo.py

# Run logging decorator demo
python decorator_logging_demo.py

Running Generator Examples

cd src/generator
python generator.py

Running Metaprogramming Examples

cd src/metaprogramming
python metaprogramming_demo.py

# Output:
# Alice
# gold

Running Unit Tests (Flask Blog)

# Navigate to the project
cd src/unit_test/demo

# Run all tests with verbose output
pytest tests/ -v

# Expected output:
# tests/test_app.py::test_posts_initialized_as_empty_list PASSED
# tests/test_app.py::test_get_posts PASSED
# tests/test_app.py::test_create_post PASSED
# tests/test_app.py::test_create_post_invalid_data PASSED
# tests/test_app.py::test_create_post_mock_posts PASSED
# ========== 5 passed in 0.15s ==========

# Run specific test
pytest tests/test_app.py::test_create_post -v

# Run with coverage report
pip install pytest-cov
pytest tests/ --cov=app --cov-report=html

Running the Flask Blog Application

# Make sure you're in the demo directory
cd src/unit_test/demo

# Start the Flask development server
python app.py

# Output:
# * Running on http://127.0.0.1:5000
# * Debug mode: on

Open your browser and navigate to: http://localhost:5000

Using the API:

# Create a new post
curl -X POST http://localhost:5000/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "Advanced Python", "content": "Learning decorators!"}'

# Get all posts
curl http://localhost:5000/posts

# Test error handling (invalid data)
curl -X POST http://localhost:5000/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "Missing content"}'

๐ŸŽ“ Learning Outcomes

By completing this project, I have gained expertise in:

๐ŸŽจ Decorators

  • โœ… Understanding closures and higher-order functions
  • โœ… Creating custom decorators for cross-cutting concerns
  • โœ… Using *args, **kwargs for flexible function signatures
  • โœ… Preserving function metadata with functools.wraps
  • โœ… Practical applications: timing, caching, logging, authentication

โšก Generators

  • โœ… Memory-efficient data processing
  • โœ… Lazy evaluation and on-demand computation
  • โœ… Creating custom iterators with yield
  • โœ… Generator expressions and comprehensions
  • โœ… Use cases for large file processing

๐Ÿ”ฎ Metaprogramming

  • โœ… Dynamic class creation using type()
  • โœ… Understanding metaclasses and __new__
  • โœ… Runtime attribute manipulation
  • โœ… Building flexible, adaptable frameworks
  • โœ… Knowing when (and when not) to use metaprogramming

๐Ÿงช Test-Driven Development

  • โœ… Writing tests before implementation (Red-Green-Refactor)
  • โœ… Using pytest for Python testing
  • โœ… Creating fixtures and test configurations
  • โœ… Integration testing for web applications
  • โœ… Mocking dependencies for isolated testing
  • โœ… Writing maintainable, well-tested code

๐ŸŒ Web Development

  • โœ… Building RESTful APIs with Flask
  • โœ… Handling JSON requests and responses
  • โœ… HTTP status codes and error handling
  • โœ… Request validation and data sanitization
  • โœ… Full-stack development (Backend + Frontend)

๐Ÿ“– Resources

Official Documentation

Recommended Reading

  • Fluent Python by Luciano Ramalho
  • Python Cookbook by David Beazley
  • Effective Python by Brett Slatkin
  • Test-Driven Development with Python by Harry Percival

๐Ÿ† Key Takeaways

"Simple is better than complex. Complex is better than complicated."
โ€” The Zen of Python

What I Learned:

  1. Decorators are powerful - They enable clean separation of concerns and code reusability
  2. Generators save memory - Essential for processing large datasets efficiently
  3. Metaprogramming is a superpower - Use responsibly to create elegant solutions
  4. Tests provide confidence - TDD ensures code quality and catches bugs early
  5. Python is expressive - Advanced features enable writing elegant, maintainable code

๐Ÿค Contributing

Feel free to explore, learn, and build upon this project! If you have suggestions or improvements:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

๐Ÿ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐ŸŒŸ Acknowledgments

Special thanks to:

  • Coursera for the Advanced Python Development course
  • Python Software Foundation for creating an amazing language
  • The open-source community for continuous inspiration

๐Ÿ Happy Pythoning! ๐Ÿ

Made with โค๏ธ and lots of โ˜•

"Code is like humor. When you have to explain it, it's bad." โ€“ Cory House