Shapeshift

Shapeshift Ports

PythonGo

humanize in Go

The number, file size and list functions of humanize 4.17.0, in Go, giving the text Python gives. It ports humanize 4.17.0 (tag 4.17.0, commit a950045).

100%

42,962 of 42,962 calls recorded from the Python original answer the same in the port. Replayed again on .

What it ports

ordinal, intcomma, intword, apnumber, fractional, scientific, clamp, metric, naturalsize, natural_list.

Install

Version 0.1.0 on Go modules (github.com/Pushkal-Gupta/shapeshift-humanize-go). Needs Go 1.22 or later and a C compiler, for cgo.

go get github.com/Pushkal-Gupta/shapeshift-humanize-go@v0.1.0

Use it

import "github.com/Pushkal-Gupta/shapeshift-humanize-go/humanize"

s, _ := humanize.Intcomma(humanize.Int(1234567), nil)                  // "1,234,567"
s, _ = humanize.Intword(humanize.Int(1_200_000_000), "%.1f")           // "1.2 billion"
s, _ = humanize.Naturalsize(humanize.Int(3000), true, false, "%.1f")   // "2.9 KiB"
s, _ = humanize.Ordinal("103")                                         // "103rd"
s, _ = humanize.NaturalList([]humanize.Value{"one", "two", "three"})   // "one, two and three"

Checked by humanize/example_test.go, run by go test.

Why use the port

Speed, as measured in the repository

100,000 calls each on the same 1,000 integers, best of five runs, run three times on one arm64 machine under macOS (bench/bench.py and bench/main.go). Times for 100,000 calls; lower is faster. Other machines will differ.

RunTime
intcomma, Python 3.12.352.4 to 54.8 ms
intcomma, Go 1.27.1, this port21.3 to 21.4 ms
intword, Python 3.12.374.9 to 83.5 ms
intword, Go 1.27.1, this port58.1 to 63.9 ms
naturalsize, Python 3.12.372.8 to 76.2 ms
naturalsize, Go 1.27.1, this port44.3 to 46.1 ms

The tapes

A tape is a file of calls recorded from the original while it ran: the arguments, and the result or the exception type. A call matches when the port gives the same result or raises the same exception type. The counted tapes make up the headline figure.

TapeWhere the calls came fromCallsMatchCounted
shadow/suite.jsonlhumanize's own tests for these functions and the examples in their docstrings475475yes
shadow/random.jsonlrandom inputs from fuzz/gen_tape.py: huge ints, floats across the exponent range, malformed strings, random format strings4,0004,000yes
shadow/edge.jsonlhand-picked edge cases from fuzz/edge_tape.py: format specifications, printf formats, rounding boundaries, Unicode18,50518,505yes
shadow/holdout.jsonlthe random generator with another seed, recorded after the port matched and never used to change it19,98219,982yes

Replay it yourself

With pgremlin installed:

git clone https://github.com/Pushkal-Gupta/shapeshift-humanize-go
cd shapeshift-humanize-go
go build -o adapter ./cmd/adapter
goblin shapeshift replay shadow/suite.jsonl -- ./adapter
goblin shapeshift replay shadow/random.jsonl -- ./adapter
goblin shapeshift replay shadow/edge.jsonl -- ./adapter
goblin shapeshift replay shadow/holdout.jsonl -- ./adapter

Or with the public checker, Shapeshifter, from the repository root:

shapeshifter replay shadow/suite.jsonl -- ./adapter
shapeshifter replay shadow/random.jsonl -- ./adapter
shapeshifter replay shadow/edge.jsonl -- ./adapter
shapeshifter replay shadow/holdout.jsonl -- ./adapter

What the tapes do not cover