Skip to main content

A minimal Python 3.11+ CLI package and reusable library function for converting semicolon-separated CSV input into grouped customer kickback rows and per-currency totals.

Project description

KickbackCalculator by packr.build

A tiny Python CLI and library for turning semicolon CSV rows into grouped kickback totals.

Table of Contents

Why this package

KickbackCalculator is a minimal Python package for a very specific job: take semicolon-separated CSV input, group rows by currency, customer, and kickback percent, then emit a clean CSV report with per-group kickbacks and per-currency totals.

It is designed as both a reusable library and a command-line tool. That means you can call calculate_kickbacks(csv_text) directly in tests or scripts, or run the kickback-calculator command against files from your shell.

The implementation stays intentionally small and standard-library only. There is no web framework, no database, no external CSV dependency, and no runtime package beyond Python itself.

Features

  • Library API: kickbackcalculator.calculate_kickbacks(csv_text: str) -> str
  • CLI command: kickback-calculator input.csv -o output.csv
  • Stdout fallback when -o/--output is omitted
  • UTF-8 input and output by default, with --encoding support
  • Semicolon-delimited CSV parsing via the Python standard library
  • Skips the first row as a header
  • Ignores blank rows and rows with fewer than 11 columns
  • Uses fixed PHP-compatible columns: customer=2, currency=8, amount=9, kickback percent=10
  • Uses decimal.Decimal internally for money-like calculations
  • Emits per-currency TOTAL rows
  • Formats output with two decimals, decimal comma, and space thousands separators
  • Deterministic output ordering

Requirements

  • Python >=3.11 (Python 3.11 and 3.12 supported)
  • Standard library only at runtime
  • pytest ^8.0 for development and testing
  • No framework required

Installation

KickbackCalculator is published to PyPI as jake142-kickbackcalculator.

Install from PyPI

uv pip install jake142-kickbackcalculator

Development

If you cloned the repository, install the project and its dev dependencies with:

uv sync

Configuration

KickbackCalculator does not use environment variables or config keys.

  • Environment variables: none
  • Config keys: none

Quick start

Create a small input file and run the CLI:

cat > input.csv <<'CSV'
col0;col1;col2;col3;col4;col5;col6;col7;col8;col9;col10
x;y;Customer A;z;z;z;z;z;SEK;1000,00;10
x;y;Customer A;z;z;z;z;z;SEK;500,00;10
CSV

kickback-calculator input.csv -o output.csv
cat output.csv

Expected output:

customer;kickback;currency
Customer A;150,00;SEK
TOTAL;150,00;SEK

Usage

Python API

Use the public function when you want to convert CSV text in memory:

from kickbackcalculator import calculate_kickbacks

csv_text = """col0;col1;col2;col3;col4;col5;col6;col7;col8;col9;col10
x;y;Customer A;z;z;z;z;z;SEK;1000,00;10
x;y;Customer A;z;z;z;z;z;SEK;500,00;10
"""

result = calculate_kickbacks(csv_text)
print(result)

CLI

Convert a file and write the result to another file:

kickback-calculator input.csv -o output.csv

Write to stdout instead:

kickback-calculator input.csv

Read and write using a different encoding if needed:

kickback-calculator input.csv -o output.csv --encoding latin-1

Input and output rules

Input format

  • Delimiter: semicolon (;)
  • The first row is always treated as a header and skipped
  • Blank rows are ignored
  • Rows with fewer than 11 columns are ignored
  • Fixed zero-based columns:
    • customer: column 2
    • currency: column 8
    • amount: column 9
    • kickback percent: column 10

Parsing rules

  • Amount and kickback percent values are trimmed before parsing
  • Decimal commas are normalized to decimal points before conversion
  • Malformed numeric rows are skipped by default
  • The library does not print warnings
  • The CLI reports user/input errors concisely on stderr

Grouping and totals

Rows are grouped by:

  1. currency
  2. customer
  3. kickback percent

For each group, kickback is calculated as:

summed amount * percent / 100

The output then appends one TOTAL row per currency.

Output format

  • Header: customer;kickback;currency
  • Rows: {customer};{formatted_kickback};{currency}
  • Totals: TOTAL;{formatted_total};{currency}
  • Numbers use exactly two decimals
  • Decimal comma is used in output
  • A space is used as the thousands separator
  • Output ends with a trailing newline

Example formatting:

  • 1234.5 -> 1 234,50
  • 10 -> 10,00

Testing

Run the test suite with uv:

uv run pytest

The tests cover the public library API and the CLI behavior, including file output and stdout fallback. No external services are called.

Project layout

The package uses a small src/ layout:

pyproject.toml
README.md
src/kickbackcalculator/__init__.py
src/kickbackcalculator/core.py
src/kickbackcalculator/cli.py
tests/test_core.py
tests/test_cli.py

The public import path is kickbackcalculator, and the console script is kickback-calculator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jake142_kickbackcalculator-0.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jake142_kickbackcalculator-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file jake142_kickbackcalculator-0.1.1.tar.gz.

File metadata

File hashes

Hashes for jake142_kickbackcalculator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d09952404804244b40b101c03818fb406d2614429954f533351e9bcb9b92b29c
MD5 d9fc373a22a4805e80da25e910183919
BLAKE2b-256 14ea62d3bd46f28421c4cef9621e319b19633927f32ec4fa7efdfd39e1ab80c9

See more details on using hashes here.

File details

Details for the file jake142_kickbackcalculator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jake142_kickbackcalculator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7932e70790447723784ca6cde2c4d2835c12933e1f5d9db10b41eec305b812b2
MD5 aafdf9927c979fd33b8a0107adba97c9
BLAKE2b-256 e219fad506128bb6e5d7628964b6482ab9c67968d998346d0b939e4b1941af68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page