feature done
commit refs/heads/main
committer Reetie Fixtures <fixtures@reetie.example.test> 1796256000 +0000
data 25
Stock Ledger starter v01

M 100644 inline README.md
data 551
# Stock Ledger

A small command-line program for adding inventory movements by warehouse and SKU.

Run `python3 ledger.py fixtures/example.jsonl`. Pass `-` to read JSON Lines from standard input. Each record contains `warehouse`, `sku`, and an integer `delta`. Blank lines are ignored. The output is a JSON array with `warehouse`, `sku`, and `quantity`, sorted by warehouse and then SKU. A net quantity of zero is kept.

Invalid input produces an error on standard error and leaves standard output empty. The program uses the Python standard library.

M 100644 inline ledger.py
data 1523
"""Summarize warehouse movements from a JSON Lines file."""

import argparse
from contextlib import nullcontext
import json
import sys


def summarize(lines):
    balances = {}
    for number, line in enumerate(lines, start=1):
        if not line.strip():
            continue
        row = json.loads(line)
        if not isinstance(row, dict):
            raise ValueError(f"line {number}: expected an object")
        warehouse, sku, delta = row.get("warehouse"), row.get("sku"), row.get("delta")
        if not isinstance(warehouse, str) or not warehouse or not isinstance(sku, str) or not sku or type(delta) is not int:
            raise ValueError(f"line {number}: invalid movement")
        key = warehouse, sku
        balances[key] = balances.get(key, 0) + delta
    return [
        {"warehouse": warehouse, "sku": sku, "quantity": quantity}
        for (warehouse, sku), quantity in sorted(balances.items())
    ]


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("input", help="JSON Lines path, or - for standard input")
    args = parser.parse_args()
    try:
        stream = nullcontext(sys.stdin) if args.input == "-" else open(args.input, encoding="utf-8")
        with stream as lines:
            rows = summarize(lines)
    except (OSError, ValueError) as error:
        print(f"stock ledger: {error}", file=sys.stderr)
        return 2
    print(json.dumps(rows, ensure_ascii=False))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

M 100644 inline fixtures/example.jsonl
data 136
{"warehouse":"West","sku":"crate","delta":3}
{"warehouse":"East","sku":"crate","delta":7}
{"warehouse":"West","sku":"crate","delta":-1}


done
