← Work05 / 05

Python · Programming · Automation

Pyra

Three messy files, one reliable list.

Pyra is a small data-processing tool written in Python with zero dependencies. The scenario is fictional; the code is real: it lives in this site's repository, and the output shown below comes from an actual run.

Status
Personal concept — 2026
Role
Design, architecture, development
Nature
Personal concept — never deployed or used commercially
Language
Python 3, standard library only
Source
tools/pyra/pyra.py — about 160 lines

A small business receives appointments from three sources: the website form, a calendar tool and a hand-kept spreadsheet. Three delimiters, three date formats, duplicates, typos.

01 — Approach

Separate what varies from what doesn't.

What changes between sources — delimiter, column names, date format — is described in a configuration table, one “adapter” per source. The rest of the pipeline only knows a single data model.

Adding a fourth source means adding one entry to that table, without touching the logic.

site.csvséparateur , · AAAA-MM-JJ
agenda.csvséparateur ; · JJ.MM.AAAA
tableur.csvséparateur , · JJ/MM/AA(AA)
01adapter()
02normalize_*()
03validate
04dedupe(email, date)
reservations.csv7 lignes propres
rapport.jsondoublons · rejets
Data flow: read → normalise → validate → de-duplicate → outputs.

02 — Model

One booking, one format.

pyra.pypython
@dataclass(frozen=True)
class Booking:
    name: str
    email: str
    phone: str
    date: str  # ISO 8601 (AAAA-MM-JJ)
    service: str
    source: str
An immutable dataclass: once normalised, a booking never changes.

03 — Adapters

Configuration instead of conditionals.

pyra.pypython
SOURCES = {
    "site":    {"delimiter": ",", "date_formats": ["%Y-%m-%d"], ...},
    "agenda":  {"delimiter": ";", "date_formats": ["%d.%m.%Y"], ...},
    "tableur": {"delimiter": ",", "date_formats": ["%d/%m/%Y", "%d/%m/%y"], ...},
}

04 — Normalisation

Five ways to write a number, one way to store it.

pyra.pypython
def normalize_phone(raw: str) -> str:
    """Ramène un numéro suisse au format +41 XX XXX XX XX."""
    digits = re.sub(r"\D", "", raw)
    if digits.startswith("0041"):
        digits = digits[4:]
    elif digits.startswith("41") and len(digits) == 11:
        digits = digits[2:]
    elif digits.startswith("0"):
        digits = digits[1:]
    if len(digits) != 9:
        raise ValueError(f"téléphone invalide « {raw} »")
    return f"+41 {digits[0:2]} {digits[2:5]} {digits[5:7]} {digits[7:9]}"
“079 412 33 10”, “+41 79…”, “0041 79…” or “079.555.12.40” all give the same result. An incomplete number is rejected, never guessed. (Code comments are in French.)

05 — Validation & duplicates

Reject with a reason, merge with a rule.

pyra.pypython
for line, row in enumerate(csv.DictReader(handle, delimiter=spec["delimiter"]), start=2):
    report.read += 1
    try:
        booking = parse_row(row, source)
    except (ValueError, KeyError) as error:
        report.rejected.append({"source": source, "line": line, "reason": str(error)})
        continue
    key = (booking.email, booking.date)  # même personne, même jour = doublon
    if key in seen:
        report.duplicates += 1
        continue
    seen.add(key)
    bookings.append(booking)
Each rejection keeps its source and line number, so the original file can be fixed.
$ python3 pyra.py samples --out output
pyra · 3 sources · 12 lignes lues
  ✓ 7 réservations conservées
  ⧉ 2 doublons fusionnés
  ✕ 3 lignes rejetées
      site:4  e-mail invalide « ines.ferraz@exemple »
      agenda:4  téléphone invalide « 079 88 41 »
      tableur:4  date illisible « 31/02/2026 »
→ output/reservations.csv
Real output from a run on the sample files (messages in French).

06 — Challenges

The real problems are in the details.

  1. 01

    Ambiguous dates

    Is “7/3/26” the 7th of March or the 3rd of July? Pyra only tries the formats declared for each source, in order. February 31st is rejected, not corrected.

  2. 02

    The duplicate key

    Same email, same day: simple and explainable. But two appointments on the same day for one person would be wrongly merged.

  3. 03

    Never guess

    Doubtful data is flagged rather than silently repaired. An automation tool has to be predictable before being clever.

07 — Limitations

What Pyra doesn't do yet.

  1. 01

    No automated tests

    Next step: unit tests for every normalisation function.

  2. 02

    No fuzzy matching

    “Marc Aubry” and “Marc Aubri” would be two people.

  3. 03

    Command-line interface

    Fine for a developer, not for the end user.

08 — What I take away

42 taught me rigour, Python taught me readability.

  1. 01

    Handle every error case

    A habit from writing C at 42: every invalid input has its path.

  2. 02

    Write to be re-read

    Explicit names and one responsibility per function.

  3. 03

    Design helps code too

    Treat the error report as an interface: clear, actionable, honest.