How sako-ranj/psycopg3-python works
Project type and purpose
• This is a lightweight, command-line image database manager written in Python.
• It provides two main modes: “insert” (read images from a local folder, process them, then store them in PostgreSQL) and “display” (fetch binary image data from the database and render or save it).
File structure
• Top-level files:
• config.py – holds database credentials and the path to the image directory
• db_operations.py – encapsulates all PostgreSQL interactions using psycopg3 and asyncio
• image_operations.py – uses PIL to load and transform images to/from binary form
• main.py – the CLI entry point that parses the “insert” or “display” command and orchestrates operations
• requirements.txt – lists asyncio, psycopg3, PIL
• README.md – explains setup and usage
Extract architectural hints from the README
• Two clear use cases: insert all images vs. display all images.
• Dependencies: Python 3.7+, PostgreSQL, PIL, psycopg3 (async).
• No web server or UI—pure CLI.
Main components and their interactions
a. Components
• CLI Layer (main.py)
• Configuration Module (config.py)
• Image Service (image_operations.py)
• Database Service (db_operations.py)
• File System (local image directory)
• PostgreSQL Database
b. Interactions
1. User invokes “python main.py insert” or “display” → CLI Layer
2. CLI loads config.py → reads DATABASE_CONFIG + IMAGE_PATH
3. For “insert”:
• Image Service scans IMAGE_PATH, uses PIL to read each image and convert to bytes.
• Database Service receives image bytes + metadata → opens async connection → issues INSERT commands.
4. For “display”:
• Database Service queries the images table → returns binary blobs.
• Image Service takes each blob, uses PIL to recreate image files or display inline.
5. All DB calls use psycopg3’s async API, ensuring non-blocking I/O.
Architectural patterns and design principles
• Layered Architecture: Separate CLI, service logic (image & DB), configuration, and external resources (filesystem & DB).
• Single Responsibility:
• config.py only holds settings
• image_operations.py only handles image I/O and conversions
• db_operations.py only handles database CRUD
• Asynchronous I/O: Uses asyncio + psycopg3 for scalable DB operations