Help us shape the future of Go! Take the 2026 Go Developer Survey

oracle

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package oracle drives NumPy/SciPy as a reference implementation from Go.

The only Python is the small dispatcher in Program: it resolves a dotted name (np.polyfit, np.polynomial.chebyshev.chebval, ...) or a method (".deriv" on the result of an earlier call), calls it with the decoded JSON arguments and encodes the result as JSON. Everything else - the requests, the comparison, the fixtures - is Go.

Transport is injected so that the package is fully testable without Python; the real "uv run" transport lives behind the "oracle" build tag (uv.go).

Index

Constants

View Source
const (
	NumPyVersion = "2.5.4"
	SciPyVersion = "1.18.1"
)

Versions pinned for the oracle.

View Source
const Program = `` /* 1637-byte string literal not displayed */

Program is the Python dispatcher given to "python -c". It reads one JSON request (a list of calls) per line on stdin and answers one JSON line.

Variables

View Source
var ErrVersion = errors.New("oracle: version mismatch")

ErrVersion is returned by HealthCheck when the oracle's libraries differ from the pinned versions.

Functions

func Arr

func Arr(v []float64) any

Arr encodes a float64 array argument.

func Cplx

func Cplx(v []complex128) any

Cplx encodes a complex128 array argument.

func F

func F(v float64) any

F encodes a float64 scalar argument (NaN/Inf allowed).

func Ref

func Ref(name string) any

Ref refers to a value stored earlier with Set.

Types

type Call

type Call struct {
	Fn   string
	Args []any
	Kw   map[string]any
	// Rep, when > 1, makes the oracle run the call Rep times and report the
	// mean time per call (Result.Time).
	Rep int
}

Call is a function call; Fn is a dotted name rooted at np, sp or ex, or ".method" to call a method on the first argument. Args may be nested Calls.

func (Call) MarshalJSON

func (c Call) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Client

type Client struct{ T Transport }

Client talks to the oracle through a Transport.

func (Client) Batch

func (c Client) Batch(items ...any) ([]Result, error)

Batch sends the items (Call or Set values) as one request and returns one Result per item.

func (Client) Do

func (c Client) Do(call Call) (Result, error)

Do runs a single call.

func (Client) HealthCheck

func (c Client) HealthCheck() error

HealthCheck verifies the oracle answers and runs numpy NumPyVersion and scipy SciPyVersion.

type Float

type Float float64

Float is a float64 with the same JSON convention as Floats.

func (Float) MarshalJSON

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Floats

type Floats []float64

Floats is a []float64 whose JSON form keeps NaN and +-Inf as the strings "nan", "inf", "-inf" and every finite value as a shortest round-trip number.

func (Floats) MarshalJSON

func (f Floats) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Floats) UnmarshalJSON

func (f *Floats) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Result

type Result struct {
	Err  string  // non-empty when NumPy raised: "ValueError: ..."
	Val  Value   // the result
	Time float64 // seconds spent in the call itself (time.perf_counter)
}

Result is the answer to one item of a batch.

type Set

type Set struct {
	Name string
	V    any
}

Set stores the value V in the oracle process under Name.

func (Set) MarshalJSON

func (s Set) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Transport

type Transport interface {
	Do(request []byte) ([]byte, error)
}

Transport sends one request line and returns the one-line answer.

type Value

type Value struct {
	Shape   []int
	Re, Im  []float64
	Complex bool
	Tuple   []Value
	Str     string
	IsStr   bool
	IsTuple bool
}

Value is a decoded result: a (possibly 0-d) array, a tuple, or a string.

func (Value) Complexes

func (v Value) Complexes() []complex128

Complexes returns the value as a complex slice (imaginary parts 0 for a real array).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL