Skip to content

foioguto/proxy-python-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐍 Proxy Pattern Demo: Client and Orders (PostgreSQL/SQLAlchemy)

This project demonstrates the implementation of the Virtual Proxy Pattern (Lazy Initialization) in Python, using SQLAlchemy to simulate database access (PostgreSQL).
The main goal is to control the loading of a high-cost domain object (ClientORM) only at the moment it is truly needed, improving performance.


πŸ“ Project Structure

The source code is isolated within the app/ package structure, following standard Python packaging practices.

/proxy-project
β”œβ”€β”€ .env                  # PostgreSQL connection environment variables
β”œβ”€β”€ requirements.txt      # Project dependencies
└── app/                  # Main Python Package
    β”œβ”€β”€ client.py         # Application Entry Point (GUI and Console Demo)
    β”œβ”€β”€ init_db.py        # Creates the Clients and Orders tables
    β”œβ”€β”€ cleanup_db.py     # **NEW:** Removes all tables (required for full reset)
    β”œβ”€β”€ seed_db.py        # **NEW:** Populates the DB with 1000 test clients
    └── src/
        β”œβ”€β”€ core/         # Domain Logic (Proxy, ORM Models)
        β”‚   β”œβ”€β”€ client_proxy.py # The Virtual Proxy implementation
        β”‚   β”œβ”€β”€ clientORM.py  # The Real Subject (SQLAlchemy Model)
        β”‚   β”œβ”€β”€ model.py    # Database Configuration (Engine, Session)
        β”‚   └── order.py    # Order Data Model
        └── gui/          # Presentation Layer (Tkinter App)
            └── client_gui.py

πŸ› οΈ Setup and Installation

1. Requirements

Ensure you have Python 3.8+ installed and a PostgreSQL server running locally (default port 5432).

2. Environment Variables

Create or update the .env file inside the app/ directory (/proxy-project/app/.env) with your database credentials:

# .env content example
DB_HOST=localhost
DB_PORT=5432
DB_NAME=futino
DB_USER=postgres
DB_PASSWORD=supra
DB_DRIVER=psycopg2

3. Python Dependencies

Install the necessary libraries using requirements.txt:

pip install -r requirements.txt

πŸš€ How to Run

All scripts must be executed from the project root directory (/proxy-project/) using Python's module notation (python -m).

1. Initialize and Seed the Database

Before running the application, you must create the tables and insert the test data (1000 clients and 2000 orders).

Command Description
python -m app.init_db Creates the clients and orders tables in the database.
python -m app.seed_db Inserts 1000 clients and 2000 orders (test data). (Requires empty tables!)

Data Cleanup (Required after "UniqueViolation" errors)
If you need to re-run seed_db after a duplicate key error, clear the tables using:

python -m app.cleanup_db
python -m app.init_db
python -m app.seed_db

2. Start the Demo Application

Execute the main entry point, which loads the graphical interface (ClientApp / Tkinter):

python -m app.client

When interacting with the GUI, observe the log (bottom part of the window) to see when the Proxy is:

  • Lazy Initializing: Loading the ClientORM object from the database (only when you click "Access Name").
  • Lazy Loading: Fetching the list of orders via SQLAlchemy (only when you click "Access Orders").

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages