Skip to content

Latest commit

Β 

History

History
338 lines (260 loc) Β· 9.46 KB

File metadata and controls

338 lines (260 loc) Β· 9.46 KB

Specifications Documentation

This directory contains comprehensive specifications for building Python PyPI packages for the countries-states-cities-database project, mirroring the existing npm package ecosystem.


πŸ“š Documents Overview

Complete specifications for the Python package ecosystem

Key Topics:

  • Executive summary and business context
  • Repository architecture and directory structure
  • Detailed package specifications (countries, timezones, currencies, etc.)
  • Pydantic models and type definitions
  • API design and function signatures
  • Data loading strategies with LRU caching
  • Development tooling (Poetry, pytest, mypy)
  • CI/CD with GitHub Actions
  • Testing requirements and strategies
  • Documentation structure
  • Implementation roadmap (4-6 weeks)

When to Use: Start here for complete understanding of the project scope and architecture.


Side-by-side comparison of npm and Python implementations

Key Topics:

  • Package naming conventions
  • Installation and usage examples
  • Type systems (TypeScript vs Pydantic)
  • Data loading approaches
  • Testing frameworks
  • Build tools and toolchains
  • CI/CD workflows
  • Framework integration examples
  • API style differences

When to Use: Reference this when making design decisions to ensure consistency with npm packages.


Step-by-step implementation guide for the first package

Key Topics:

  • Phase 1: Setup (directory structure, Poetry)
  • Phase 2: Core implementation (models, loaders, API)
  • Phase 3: Testing (pytest, coverage)
  • Phase 4: Data generation (MySQL to JSON)
  • Phase 5: Package configuration (pyproject.toml)
  • Phase 6: Build and test
  • Phase 7: Publish to PyPI
  • Complete code examples
  • Pre-launch checklist
  • Troubleshooting tips

When to Use: Follow this guide for hands-on implementation of the first package.


🎯 Quick Reference

Package Names

Type NPM Python
Countries @countrystatecity/countries countrystatecity-countries
Timezones @countrystatecity/timezones countrystatecity-timezones
Currencies @countrystatecity/currencies countrystatecity-currencies
Languages @countrystatecity/languages countrystatecity-languages
Phone Codes @countrystatecity/phone-codes countrystatecity-phonecodes

Installation

# NPM
npm install @countrystatecity/countries

# Python
pip install countrystatecity-countries

Basic Usage

# Python
from countrystatecity_countries import get_countries, get_country_by_code

countries = get_countries()
usa = get_country_by_code("US")
// TypeScript
import { getCountries, getCountryByCode } from '@countrystatecity/countries';

const countries = await getCountries();
const usa = await getCountryByCode('US');

πŸ—οΈ Repository Structure (After Implementation)

countries-states-cities-database/
β”œβ”€β”€ bin/                           # [Existing] PHP export tools
β”œβ”€β”€ contributions/                 # [Existing] JSON source data
β”œβ”€β”€ json/                          # [Existing] JSON exports
β”œβ”€β”€ sql/                           # [Existing] SQL dumps
β”‚
β”œβ”€β”€ python/                        # [NEW] Python packages
β”‚   β”œβ”€β”€ packages/
β”‚   β”‚   β”œβ”€β”€ countries/             # Priority 1
β”‚   β”‚   β”œβ”€β”€ timezones/             # Priority 2
β”‚   β”‚   β”œβ”€β”€ currencies/            # Priority 3
β”‚   β”‚   β”œβ”€β”€ languages/             # Priority 4
β”‚   β”‚   └── phone-codes/           # Priority 5
β”‚   β”œβ”€β”€ shared/                    # Shared utilities
β”‚   β”œβ”€β”€ scripts/                   # Build scripts
β”‚   └── README.md
β”‚
└── specs/                         # [THIS FOLDER] Specifications
    β”œβ”€β”€ README.md                  # This file
    β”œβ”€β”€ 1-python-pypi-monorepo-plan.md
    β”œβ”€β”€ 2-python-vs-npm-comparison.md
    └── 3-python-quick-start-guide.md

πŸ“‹ Implementation Priority

Phase 1: Foundation (Weeks 1-4)

Package: countrystatecity-countries

  • Setup repository structure
  • Implement core functionality
  • Write comprehensive tests
  • Generate data structure
  • Publish to PyPI

Phase 2: Expansion (Weeks 5-8)

Packages:

  • countrystatecity-timezones
  • countrystatecity-currencies

Phase 3: Completion (Weeks 9-12)

Packages:

  • countrystatecity-languages
  • countrystatecity-phonecodes

Phase 4: Framework Integration (Weeks 13-16)

Packages:

  • countrystatecity-flask
  • countrystatecity-django

πŸ”‘ Key Design Principles

1. Consistency with NPM Packages

  • Same data structure
  • Similar API design (adapted for Python conventions)
  • Identical functionality

2. Python Best Practices

  • Type hints everywhere (Pydantic models)
  • PEP 8 compliant (black, isort)
  • Comprehensive testing (pytest, 80%+ coverage)
  • Modern packaging (Poetry, pyproject.toml)

3. Performance & Efficiency

  • Lazy loading with LRU cache
  • Minimal memory footprint
  • Fast import times (<50ms)
  • Split data files for granular loading

4. Developer Experience

  • Clear API documentation
  • Type safety with mypy
  • Comprehensive examples
  • Easy integration with frameworks

5. Data Integrity

  • Single source of truth (MySQL database)
  • Automated data generation
  • Validation at build time
  • Immutable models (Pydantic frozen)

πŸ› οΈ Technology Stack

Component Technology Purpose
Package Manager Poetry Dependency management, publishing
Type System Pydantic Data validation, immutable models
Testing pytest Unit and integration tests
Type Checking mypy Static type checking
Linting ruff Fast Python linter
Formatting black + isort Code formatting
CI/CD GitHub Actions Automated testing and publishing
Data Source MySQL Primary database
Data Format JSON Lazy-loadable data files

πŸ“Š Success Metrics

Technical Metrics

  • βœ… Test coverage: 80%+
  • βœ… Type coverage: 100% (mypy strict mode)
  • βœ… Import time: <50ms
  • βœ… Memory footprint: <10MB (base)
  • βœ… Load time: <100ms (per country)

Quality Metrics

  • βœ… Zero critical bugs
  • βœ… Issue response time: <48 hours
  • βœ… Documentation completeness: 100%
  • βœ… API consistency with npm packages

Community Metrics

  • πŸ“ˆ PyPI downloads per week
  • ⭐ GitHub stars
  • πŸ› Issue resolution rate
  • 🀝 Community contributions

πŸš€ Getting Started

For Implementers

  1. Read the specifications

  2. Follow the quick start guide

  3. Test and iterate

    • Write tests alongside code
    • Run mypy for type checking
    • Use black and isort for formatting
  4. Publish and monitor

    • Publish to TestPyPI first
    • Then publish to PyPI
    • Monitor downloads and issues

For Contributors

  1. Understand the architecture

    • Read all specification documents
    • Review existing npm packages
    • Understand data structure
  2. Set up development environment

    • Install Python 3.8+
    • Install Poetry
    • Clone repository
  3. Make changes

    • Follow coding standards
    • Write tests
    • Update documentation
  4. Submit PRs

    • Ensure tests pass
    • Include documentation
    • Reference related issues

πŸ“– Related Resources

External Links

Internal Links


❓ FAQ

Why Python in addition to npm packages?

  • Large Python community needs official packages
  • Server-side and data science use cases
  • Better integration with Django, Flask, FastAPI
  • Avoid outdated/incomplete alternatives

Why not use the existing npm packages with a bridge?

  • Python developers expect native Python packages
  • Better performance without JavaScript bridge
  • Type safety with Pydantic
  • Pythonic API design

How is data kept in sync?

  • Single source of truth: MySQL database
  • Automated data generation scripts
  • Same JSON structure as npm packages
  • CI/CD ensures consistency

What about other package registries (Conda, etc.)?

  • Start with PyPI (most common)
  • Can publish to Conda later if demand exists
  • Focus on quality over quantity

πŸ“ž Contact

For questions or clarifications about these specifications:


Last Updated: January 2025
Status: Ready for Implementation
Next Action: Begin Phase 1 of Quick Start Guide