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.0Use 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
The same text from Go and Python
A Go service prints numbers, sizes and lists exactly as a Python service using humanize does, down to Python's format specifications and printf formats.
No Python at run time
The port compiles into your Go program. It uses the Go standard library only, plus cgo for three C math functions.
Faster on every function measured
On the bench below, all three measured functions ran in less time than in Python, though the port was written to match, not for speed.
The proof runs with go test
go test ./... replays every tape in the repository with no other tool, so a change that breaks a recorded answer fails the build.
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.
| Run | Time |
|---|---|
| intcomma, Python 3.12.3 | 52.4 to 54.8 ms |
| intcomma, Go 1.27.1, this port | 21.3 to 21.4 ms |
| intword, Python 3.12.3 | 74.9 to 83.5 ms |
| intword, Go 1.27.1, this port | 58.1 to 63.9 ms |
| naturalsize, Python 3.12.3 | 72.8 to 76.2 ms |
| naturalsize, Go 1.27.1, this port | 44.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.
| Tape | Where the calls came from | Calls | Match | Counted |
|---|---|---|---|---|
shadow/suite.jsonl | humanize's own tests for these functions and the examples in their docstrings | 475 | 475 | yes |
shadow/random.jsonl | random inputs from fuzz/gen_tape.py: huge ints, floats across the exponent range, malformed strings, random format strings | 4,000 | 4,000 | yes |
shadow/edge.jsonl | hand-picked edge cases from fuzz/edge_tape.py: format specifications, printf formats, rounding boundaries, Unicode | 18,505 | 18,505 | yes |
shadow/holdout.jsonl | the random generator with another seed, recorded after the port matched and never used to change it | 19,982 | 19,982 | yes |
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 -- ./adapterOr 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 -- ./adapterWhat the tapes do not cover
- The humanize.time functions: their datetime arguments cannot be written as JSON, so they were not ported.
- Locales other than the default English one.
- A function as the format of clamp(), or a number as the format of intword() or naturalsize().
- Other platforms: the tapes were recorded and replayed on one arm64 machine under macOS, and another C library may round log, log10 and pow differently.
- Unicode versions after 15.0, the version Python 3.12 knows.
Links
- Repository, tapes and log: github.com/Pushkal-Gupta/shapeshift-humanize-go
- Release: shapeshift-humanize-go v0.1.0
- Licence: MIT, the licence of the original