Conversation
Horlabrainmoore
left a comment
There was a problem hiding this comment.
credit_validator/
├── app/
│ ├── core/
│ │ ├── luhn.py
│ │ ├── brand.py
│ │ └── validator.py
│ ├── api/
│ │ └── routes.py
│ └── main.py
├── tests/
│ └── test_validator.py
├── .github/
│ └── workflows/
│ └── ci.yml
├── Dockerfile
├── pyproject.toml
import zipfile
import os
Define file structure
base_dir = "/mnt/data/credit_validator"
structure = {
"app/core": {
"luhn.py": """
def luhn_check(card_number: str) -> bool:
digits = [int(d) for d in card_number if d.isdigit()]
checksum = 0
double = False
for d in reversed(digits):
if double:
d *= 2
if d > 9:
d -= 9
checksum += d
double = not double
return checksum % 10 == 0
""",
"brand.py": """
import re
def detect_brand(card_number: str) -> str:
patterns = {
"Visa": r"^4[0-9]{12}(?:[0-9]{3})?$",
"MasterCard": r"^5[1-5][0-9]{14}$",
"Amex": r"^3[47][0-9]{13}$",
"Discover": r"^6(?:011|5[0-9]{2})[0-9]{12}$"
}
for brand, pattern in patterns.items():
if re.match(pattern, card_number):
return brand
return "Unknown"
""",
"validator.py": """
from app.core.luhn import luhn_check
from app.core.brand import detect_brand
def validate_card(card_number: str) -> dict:
sanitized = "".join(filter(str.isdigit, card_number))
is_valid = luhn_check(sanitized)
brand = detect_brand(sanitized)
return {
"is_valid": is_valid,
"brand": brand,
"message": (
"Card is valid and recognized."
if is_valid and brand != "Unknown"
else "Card is invalid or unrecognized."
)
}
"""
},
"app/api": {
"routes.py": """
from fastapi import APIRouter
from pydantic import BaseModel
from app.core.validator import validate_card
router = APIRouter()
class CardInput(BaseModel):
card_number: str
@router.post("/validate")
async def validate(input: CardInput):
return validate_card(input.card_number)
"""
},
"app": {
"main.py": """
from fastapi import FastAPI
from app.api.routes import router
app = FastAPI(title="Credit Card Validator API")
app.include_router(router)
"""
},
"tests": {
"test_validator.py": """
from app.core.validator import validate_card
def test_valid_visa():
result = validate_card("4111111111111111")
assert result["is_valid"]
assert result["brand"] == "Visa"
def test_invalid_card():
result = validate_card("1234567812345670")
assert not result["is_valid"]
"""
},
".github/workflows": {
"ci.yml": """
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install fastapi uvicorn pytest
- name: Run tests
run: |
pytest tests
"""
},
"": {
"Dockerfile": """
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir fastapi uvicorn
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
""",
"pyproject.toml": """
[tool.poetry]
name = "credit-validator"
version = "0.1.0"
description = "Credit card validation API"
authors = ["Captain Horla Brain"]
[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.110.0"
uvicorn = "^0.29.0"
[tool.poetry.dev-dependencies]
pytest = "^8.1.0"
"""
}
}
Create files
for folder, files in structure.items():
folder_path = os.path.join(base_dir, folder)
os.makedirs(folder_path, exist_ok=True)
for filename, content in files.items():
with open(os.path.join(folder_path, filename), "w") as f:
f.write(content.strip())
Create zip file
zip_path = "/mnt/data/credit_validator_boilerplate.zip"
with zipfile.ZipFile(zip_path, 'w') as zipf:
for root, _, files in os.walk(base_dir):
for file in files:
full_path = os.path.join(root, file)
arcname = os.path.relpath(full_path, base_dir)
zipf.write(full_path, arcname)
zip_path
fixes