Functional Weave
Code in Python

retail.sku-generate

Build a deterministic stock-keeping unit code from a prefix and product attributes.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 15 tests, run in TypeScript, Python and Rust.

What it does

Builds a stock-keeping unit code from a product family prefix and the variant's attributes, so the same product and options always give the same SKU: `generateSku("TSH", ["Navy Blue", "Large", "Cotton"], 3)` is `"TSH-NAV-LAR-COT"`.

## The scheme

For example

  • generate_sku(TSH, Navy Blue, Large, Cotton, 3) → TSH-NAV-LAR-COT prefix kept whole, each attribute cut to three characters
  • generate_sku(TSH, Navy Blue, XL, 0) → TSH-NAVYBLUE-XL segment length 0 keeps attributes whole, spaces dropped
  • generate_sku(MUG, Red, M, 4) → MUG-RED-M an attribute shorter than the segment length is not padded

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def generate_sku(prefix: str, attributes: Sequence[str], segment_length: int) -> str
prefixstringproduct family code, kept whole; "" for none
attributesstring[]variant attributes in a fixed order: colour, size, material
segment_lengthintcharacters kept from each attribute; 0 keeps them whole
returnsstringuppercase A-Z and 0-9 segments joined by hyphens: "TSH-NAV-LAR"

Your code names it in one line, in the file that uses it

from fune.retail.sku_generate import generate_sku  # retail.sku-generate@^1
impl/python.py · 31 lines · open · raw
from typing import Sequence


def _clean(part: str) -> str:
    # Keep ASCII letters and digits only, then uppercase. Filtering first
    # matters: "ß".upper() is "SS", which would make the SKU depend on how each
    # language's Unicode tables treat one letter.
    return "".join(c for c in part if ("A" <= c <= "Z") or ("a" <= c <= "z") or ("0" <= c <= "9")).upper()


def generate_sku(prefix: str, attributes: Sequence[str], segment_length: int) -> str:
    """A deterministic SKU: the cleaned prefix and attributes, joined by hyphens.

    Each attribute is cut to ``segment_length`` characters (0 keeps it whole).
    """
    if isinstance(segment_length, bool) or not isinstance(segment_length, int) or segment_length < 0:
        raise ValueError("segmentLength must be 0 or more, received %r" % (segment_length,))
    if len(attributes) == 0:
        raise ValueError("a SKU needs at least one attribute")
    parts = []
    if prefix != "":
        cleaned = _clean(prefix)
        if cleaned == "":
            raise ValueError('prefix ("%s") has no letters or digits' % prefix)
        parts.append(cleaned)
    for index, attribute in enumerate(attributes):
        cleaned = _clean(attribute)
        if cleaned == "":
            raise ValueError('attribute %d ("%s") has no letters or digits' % (index + 1, attribute))
        parts.append(cleaned if segment_length == 0 else cleaned[:segment_length])
    return "-".join(parts)

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add retail.sku-generate
Download for Python retail.sku-generate-1.0.0-python.fune · 7,192 bytes sha256 98f1ae533e8c4c63d292f505cdeee16f53fe3e921f631f20df7d11809a134f09

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./retail.sku-generate-1.0.0-python.fune, or fetch it from a terminal with fune pull retail.sku-generate@1.0.0:python.

The whole function, every language, is one file too: retail.sku-generate-1.0.0.fune, 10,804 bytes, sha256 7ef658222abadfe67f4a35e4484720e326cfec7c0c1a48b09861c4ff9050d9db. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before retail.sku-generate

after — your function gets the result and the arguments, and returns the final result.

# fune: after retail.sku-generate

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show retail.sku-generate --steps.

# fune: step retail.sku-generate after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
prefix kept whole, each attribute cut to three characters TSH, Navy Blue, Large, Cotton, 3 → TSH-NAV-LAR-COT
segment length 0 keeps attributes whole, spaces dropped TSH, Navy Blue, XL, 0 → TSH-NAVYBLUE-XL
an attribute shorter than the segment length is not padded MUG, Red, M, 4 → MUG-RED-M
lowercase and punctuation in prefix and attributes ab-1, x/l, 50 cm , 0 → AB1-XL-50CM
an empty prefix is left out, no leading hyphen , Blue, S, 3 → BLU-S
sharp s is dropped, not uppercased to SS as a naive toUpperCase first would , Straße, 0 → STRAE
accented letters are dropped, not transliterated DES, Crème, Brûlée, 0 → DES-CRME-BRLE
a decimal size keeps its digits and loses the point SH, 10.5, UK, 0 → SH-105-UK
truncation counts characters after cleaning P, N-a-v-y, 3 → P-NAV
segment length 1 keeps initials SOCK, Black, Wool, Medium, 1 → SOCK-B-W-M
Show the other 5 tests
CaseArgumentsExpected
the prefix is never truncated LONGPREFIX, Green, 2 → LONGPREFIX-GR
no attributes is an error TSH, , 3 → error: a SKU needs at least one attribute
an attribute with no letters or digits is an error TSH, Blue, ***, 3 → error: attribute 2 ("***") has no letters or digits
a prefix with no letters or digits is an error !!!, Blue, 3 → error: prefix ("!!!") has no letters or digits
a negative segment length is an error TSH, Blue, -1 → error: segmentLength must be 0 or more

More from the author

1. Each part (the prefix, then each attribute in the order given) keeps only the ASCII letters and digits `A-Z a-z 0-9`, in order. Spaces, punctuation and every non-ASCII character are dropped. 2. What is left is uppercased. 3. Each attribute, not the prefix, is cut to its first `segmentLength` characters. `0` keeps it whole. 4. The parts are joined with `-`. An empty prefix is left out.

Filtering happens before uppercasing on purpose: German sharp s uppercases to "SS" in all three languages, so uppercasing first would turn "Straße" into `STRASSE` in some places and `STRAE` in others. Here it is always `STRAE`. Accented letters are dropped, not transliterated ("Crème" is `CRME`); run the attribute through `text.slugify` first if you want `CREME`.

## What it does not do

It does not guarantee uniqueness. Two attributes that agree in their first `segmentLength` characters collide ("Brown" and "Bronze" are both `BRO` at 3), and so do "10.5" and "105" (both `105`). A SKU scheme with a fixed segment length needs a catalogue-level check, or a longer segment, and that belongs to whoever owns the catalogue. The order of the attributes is part of the SKU, so fix it per product family (colour, then size, then material).

## Errors

No attributes, a negative `segmentLength`, or a prefix or attribute with no letters or digits at all (`"***"`) are errors, because a SKU with an empty segment would silently collide with its neighbours.

Files

PathBytes
README.md1,729
impl/python.py1,447
impl/rust.rs2,082
impl/typescript.ts1,365
vectors.json2,019