Validating a luminaire schedule CSV with Python

Leader ●3
calendar_today ago • schedule1 min read

A luminaire schedule exported from a spreadsheet looks tidy until it reaches the quoting system. A beam angle may read '24°', a quantity may be '3.0', and a fixture ID may appear twice because a row was copied. Validate the CSV at the import boundary and return row numbers so the person maintaining the schedule can fix the source.

Define the input contract

For a small import, require fixture_id, quantity and beam_angle_deg. Keep IDs as strings so leading zeros survive. Require positive integer quantities and beam angles between 0 and 180 degrees. In this example, fixture_id identifies a schedule line, so a duplicate is an error. If the file uses a product SKU instead, add a separate line identifier because the same product can appear in multiple zones.

import csv
from decimal import Decimal, InvalidOperation


def validate_schedule(path):
    required = {"fixture_id", "quantity", "beam_angle_deg"}
    issues = []
    first_seen = {}
    with open(path, encoding="utf-8-sig", newline="") as source:
        rows = csv.DictReader(source)
        missing = required - set(rows.fieldnames or [])
        if missing:
            return [f"Missing columns: {', '.join(sorted(missing))}"]

        for row in rows:
            line = rows.line_num
            fixture_id = (row["fixture_id"] or "").strip()
            if not fixture_id:
                issues.append(f"Line {line}: missing fixture ID")
            elif fixture_id in first_seen:
                issues.append(
                    f"Line {line}: duplicate fixture ID {fixture_id!r} "
                    f"(first seen on line {first_seen[fixture_id]})"
                )
            else:
                first_seen[fixture_id] = line

            try:
                if int(row["quantity"] or "") < 1:
                    raise ValueError
            except ValueError:
                issues.append(f"Line {line}: quantity must be a positive integer")

            try:
                angle = Decimal(row["beam_angle_deg"] or "")
                if not angle.is_finite() or not 0 < angle <= 180:
                    raise InvalidOperation
            except InvalidOperation:
                issues.append(f"Line {line}: beam angle must be in (0, 180]")
    return issues

Try a small malformed file

fixture_id,quantity,beam_angle_deg
001,4,24
001,3.0,NaN
002,2,36

The second data row produces three errors: a duplicate ID, a non-integer quantity and an invalid beam angle. Report all three together so the spreadsheet owner can fix the file in one pass. Only hand the data to the importer after validation succeeds. These checks validate the file format and ranges. Confirm model-specific angles and availability against the supplier data before issuing a purchase order.

Disclosure: This article is from BOYIN Lighting, a supplier of commercial luminaires. Its product pages show examples of the fixture specifications used when building a schedule.

Want more #python & #csv deep dives?

Join 6,061+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

1 Comment

0 votes

More Posts

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

When One Python Process Fails, You Need to See All of Them

Tom Smithverified - Nov 23

Python for commerce students, from a former bio kid

rahuldk1105 - Oct 30

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Mastering Reading and Writing CSV Files in Python

Abdul Daim - Mar 27, 2024
chevron_left
632 Points • 3 Badges
1Posts
0Comments

Related Jobs

View all jobs →

Commenters (This Week)

13 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!