A simple cinema ticket management system that serves as a playground for learning code refactoring techniques and code quality analysis.
python-refactor-playground/
├── src/ # Source code
│ └── cinema/ # Main package
│ ├── __init__.py # Package initialization
│ ├── cinema_manager.py # Cinema management logic
│ ├── movie.py # Movie class definition
│ └── ticket.py # Ticket class definition
├── tests/ # Test suite
│ ├── __init__.py # Test package initialization
│ └── test_cinema.py # Unit tests
├── tools/ # Development tools
│ ├── analyze_code_quality.py # Code quality analysis tool
│ └── auto_code_fixer_libraries_only.py # Auto code fixer
├── scripts/ # Utility scripts (if any)
├── main.py # Main application entry point
├── setup.py # Package setup
├── requirements.txt # Python dependencies
├── README.md # This file
├── ESTRUCTURA.md # Project structure documentation
├── EXERCISE_README.md # Exercise documentation
├── EXERCISE_SCRIPT.md # Step-by-step guide (Linux/macOS)
├── EXERCISE_SCRIPT_WINDOWS.md # Step-by-step guide (Windows)
├── run.bat # Windows batch runner
├── run.ps1 # Windows PowerShell runner
└── setup_windows.bat # Windows setup script
- Movie Management: Add movies with title, duration, genre, and price
- Ticket Sales: Sell tickets for specific movies and seats
- Seat Management: Track available seats (1-10 rows, 1-20 seats per row)
- Revenue Tracking: Monitor total revenue and ticket sales
- Ticket Validation: Prevent double-use of tickets
- Refund System: Refund unused tickets
- Movie Reviews: Add reviews and calculate average ratings
- Install dependencies:
run.bat installOr with PowerShell:
.\run.ps1 install- Run the application:
run.bat runOr with PowerShell:
.\run.ps1 run- Run tests:
run.bat testOr with PowerShell:
.\run.ps1 test- Analyze code quality:
run.bat analyzeOr with PowerShell:
.\run.ps1 analyze- Auto-fix code issues:
run.bat fixOr with PowerShell:
.\run.ps1 fix- Install dependencies:
pip install -r requirements.txt- Run the application:
python main.py- Run tests:
# Con pytest (recomendado)
pytest
# Con unittest
python -m unittest discover
# Con Makefile
make test- Analyze code quality:
make analyze
# o
python tools/analyze_code_quality.py- Auto-fix code issues:
make fix
# o
python tools/auto_code_fixer_libraries_only.pyFor Windows, use the automated setup script:
setup_windows.batOr manually install dependencies:
pip install -e .Or install with development dependencies:
pip install -e ".[dev]"For development, install the package in editable mode:
pip install -e .Or install with development dependencies:
pip install -e ".[dev]"This project intentionally contains various code quality issues for educational purposes:
- Inefficient linear searches instead of using dictionaries/sets
- Inefficient seat management using lists instead of sets
- Inefficient ticket removal using loops instead of list comprehensions
- Missing type annotations
- Inconsistent naming conventions
- Missing docstrings
- Long methods that could be broken down
- Incorrect rating calculation in Movie class
- Boolean comparisons using
==instead ofis - Missing input validation
- No input sanitization
- Potential for SQL injection (if using databases)
run.bat help # Show all available commands
run.bat test # Run tests
run.bat test-coverage # Run tests with coverage
run.bat lint # Run linting checks
run.bat format # Format code
run.bat run # Run the application
run.bat analyze # Run code quality analysis
run.bat fix # Auto-fix code issues
run.bat clean # Clean temporary files.\run.ps1 help # Show all available commands
.\run.ps1 test # Run tests
.\run.ps1 test-coverage # Run tests with coverage
.\run.ps1 lint # Run linting checks
.\run.ps1 format # Format code
.\run.ps1 run # Run the application
.\run.ps1 analyze # Run code quality analysis
.\run.ps1 fix # Auto-fix code issues
.\run.ps1 clean # Clean temporary filesmake help # Show all available commands
make test # Run tests
make test-coverage # Run tests with coverage
make lint # Run linting checks
make format # Format code
make run # Run the application
make analyze # Run code quality analysis
make fix # Auto-fix code issues
make clean # Clean temporary filespython main.py- Run the main applicationpytest- Run all tests (recommended)python -m unittest discover- Run tests with unittestpython tools/analyze_code_quality.py- Run comprehensive code quality analysispython tools/auto_code_fixer_libraries_only.py- Run automated code fixing using libraries
The quality analysis tool provides:
- Complexity Metrics: Cyclomatic complexity and maintainability index
- Linting: Ruff, Black, isort, Pylint, Flake8
- Type Checking: MyPy static type analysis
- Security: Bandit security analysis and dependency checking
- Code Quality: Dead code detection, duplicate code analysis
- Performance: Profiling with Scalene
- Coverage: Test coverage analysis
This project helps you learn:
- Code Quality Metrics: Understanding complexity, maintainability, and technical debt
- Static Analysis Tools: Using linters, formatters, and type checkers
- Refactoring Techniques: Improving code without changing functionality
- Testing: Writing and running unit tests
- Performance Optimization: Identifying and fixing bottlenecks
- Security Best Practices: Finding and fixing security vulnerabilities
- Project Structure: Organizing code in a professional manner
Try these refactoring exercises:
- Fix the rating calculation in
Movie.add_review() - Optimize seat management by using sets instead of lists
- Add type annotations to all methods
- Improve search algorithms by using dictionaries
- Add input validation and error handling
- Break down large methods into smaller, focused functions
- Add comprehensive docstrings
- Implement proper logging instead of print statements
- Ruff: Fast Python linter with auto-fixes
- Black: Code formatter
- isort: Import organizer
- MyPy: Type checker
- Bandit: Security linter
- Radon: Complexity metrics
- Coverage: Test coverage
- Pylint: Comprehensive linter
- Flake8: Style checker
- autopep8: PEP8 style fixes
- pyupgrade: Python syntax modernization
- autoflake: Code cleanup
- vulture: Dead code detection
If you encounter execution policy errors with PowerShell scripts:
# Check current policy
Get-ExecutionPolicy
# Set policy for current user (if needed)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser- Windows uses backslashes (
\) in file paths - Use quotes around paths with spaces:
"C:\Program Files\Python\python.exe"
- Ensure Python is in your
PATHenvironment variable - Check with:
echo %PATH%(CMD) or$env:PATH(PowerShell)
- Linux/macOS: Use
EXERCISE_SCRIPT.md - Windows: Use
EXERCISE_SCRIPT_WINDOWS.md
This is a learning project. Feel free to:
- Fix the intentional issues
- Add new features
- Improve the code structure
- Add more comprehensive tests
- Experiment with different refactoring techniques