Documentation
¶
Overview ¶
Package python provides a high-level interface to the Python C API.
Index ¶
- Variables
- func AsBool(o *Object) bool
- func AsFloat64(o *Object) float64
- func AsInt(o *Object) int
- func AsInt64(o *Object) int64
- func AsString(o *Object) string
- func AsUint(o *Object) uint
- func AsUint64(o *Object) uint64
- func ClearError()
- func Finalize()
- func IsBool(o PyObjector) bool
- func IsFloat(o PyObjector) bool
- func IsInt(o PyObjector) bool
- func IsList(o PyObjector) bool
- func IsString(o PyObjector) bool
- func IsTuple(o PyObjector) bool
- func LastError() error
- func MustSuccess()
- func MustUnmarshal(o *Object, v any)
- func MustUnmarshalAs[T any](o *Object) T
- func Str(o *Object) string
- func TypeName(o *Object) string
- func Unmarshal(o *Object, v any) error
- func UnmarshalAs[T any](o *Object) (T, error)
- type AnyList
- type AnyTuple
- type Exception
- type ImportError
- type IndexError
- type InvalidUnmarshalError
- type List
- func (l *List[T]) AsObject() *Object
- func (l *List[T]) AsSlice() []T
- func (l *List[T]) AsTuple() *Tuple[T]
- func (l *List[T]) DecRef()
- func (l *List[T]) Get(index int) T
- func (l *List[T]) Length() int
- func (l *List[T]) PyObject() *cpy3.PyObject
- func (l *List[T]) Set(index int, value T)
- func (l *List[T]) String() string
- func (l *List[T]) UnmarshalPyObject(o *Object) error
- type ListObject
- func (o *ListObject) AsObject() *Object
- func (o *ListObject) AsTuple() *TupleObject
- func (o *ListObject) DecRef()
- func (o *ListObject) Get(index int) *Object
- func (o *ListObject) Length() int
- func (o *ListObject) PyObject() *cpy3.PyObject
- func (o *ListObject) Set(index int, value any)
- func (o *ListObject) String() string
- type Marshaler
- type ModuleNotFoundError
- type Object
- func ImportModule(name string) (*Object, error)
- func Marshal(v any) (*Object, error)
- func MustImportModule(name string) *Object
- func MustMarshal(v any) *Object
- func NewBool(v bool) *Object
- func NewFloat64(v float64) *Object
- func NewInt(v int) *Object
- func NewInt64(v int64) *Object
- func NewObject(obj *cpy3.PyObject) *Object
- func NewString(s string) *Object
- func NewUint(v uint) *Object
- func NewUint64(v uint64) *Object
- func (o *Object) CallMethodArgs(name string, args ...any) *Object
- func (o *Object) DecRef()
- func (o *Object) Equal(o2 *Object) bool
- func (o *Object) GetAttr(name string) *Object
- func (o *Object) GetItem(key any) *Object
- func (o *Object) HasItem(value any) bool
- func (o *Object) Length() int
- func (o *Object) PyObject() *cpy3.PyObject
- func (o *Object) SetAttr(name string, value any)
- func (o *Object) SetItem(key, value any)
- func (o *Object) String() string
- func (o *Object) Type() *Object
- type Objector
- type PyObjector
- type Tuple
- func (t *Tuple[T]) AsList() *List[T]
- func (t *Tuple[T]) AsObject() *Object
- func (t *Tuple[T]) AsSlice() []T
- func (t *Tuple[T]) DecRef()
- func (t *Tuple[T]) Get(index int) T
- func (t *Tuple[T]) Length() int
- func (t *Tuple[T]) PyObject() *cpy3.PyObject
- func (t *Tuple[T]) Set(index int, value T)
- func (t *Tuple[T]) String() string
- func (t *Tuple[T]) UnmarshalPyObject(o *Object) error
- type TupleObject
- func (o *TupleObject) AsList() *ListObject
- func (o *TupleObject) AsObject() *Object
- func (o *TupleObject) DecRef()
- func (o *TupleObject) Get(index int) *Object
- func (o *TupleObject) Length() int
- func (o *TupleObject) PyObject() *cpy3.PyObject
- func (o *TupleObject) Set(index int, value any)
- func (o *TupleObject) String() string
- type UnmarshalTypeError
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
var ErrPythonInterpreterNotInitialized = "cannot initialize the python interpreter"
ErrPythonInterpreterNotInitialized is the error message when the python interpreter is not initialized.
var False = NewObject(cpy3.Py_False)
False is a wrapper of cpy3.Py_False.
var True = NewObject(cpy3.Py_True)
True is a wrapper of cpy3.Py_True.
Functions ¶
func ClearError ¶
func ClearError()
ClearError clears the last error that occurred in the Python interpreter.
func IsFloat ¶
func IsFloat(o PyObjector) bool
IsFloat returns true if the object is a Python float.
func IsInt ¶
func IsInt(o PyObjector) bool
IsInt returns whether the given object is a Python int object.
func IsString ¶
func IsString(o PyObjector) bool
IsString returns true if o is a Python string object.
func LastError ¶
func LastError() error
LastError returns the last error that occurred in the Python interpreter.
func MustUnmarshal ¶
MustUnmarshal converts the Python object to a value of the same type as v or panics if an error occurs.
func MustUnmarshalAs ¶
MustUnmarshalAs converts the Python object to a value of the same type as T or panics if an error occurs.
func UnmarshalAs ¶
UnmarshalAs converts the Python object to a value of the same type as T.
Types ¶
type AnyList ¶
AnyList is a Python tuple.
func NewListFromAny ¶
NewListFromAny converts a slice of any to a tuple.
type AnyTuple ¶
AnyTuple is a Python tuple.
func NewTupleFromAny ¶
NewTupleFromAny converts a slice of any to a tuple.
type Exception ¶
type Exception struct {
Message string
}
Exception is a Python exception.
func NewException ¶
NewException creates a new Exception.
type ImportError ¶
ImportError is returned when a Python module cannot be imported.
type IndexError ¶
type IndexError struct {
Exception
}
IndexError is returned when a sequence subscript is out of range.
type InvalidUnmarshalError ¶
An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer).
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type List ¶
type List[T any] struct { // contains filtered or unexported fields }
List is a generic Python tuple.
func NewListForType ¶
NewListForType creates a new tuple for a given type.
func NewListFromValues ¶
NewListFromValues converts a slice of any data type to a tuple.
func (*List[T]) DecRef ¶
func (l *List[T]) DecRef()
DecRef decreases the reference count of the object.
func (*List[T]) UnmarshalPyObject ¶
UnmarshalPyObject unmarshals a Python object to a list.
type ListObject ¶
ListObject is a generic Python tuple.
func (*ListObject) AsObject ¶
func (o *ListObject) AsObject() *Object
AsObject returns the tuple as Object.
func (*ListObject) AsTuple ¶
func (o *ListObject) AsTuple() *TupleObject
AsTuple converts a list to a tuple.
func (*ListObject) DecRef ¶
func (o *ListObject) DecRef()
DecRef decreases the reference count of the object.
func (*ListObject) Length ¶
func (o *ListObject) Length() int
Length returns the length of the tuple.
func (*ListObject) PyObject ¶
func (o *ListObject) PyObject() *cpy3.PyObject
PyObject returns the underlying PyObject.
func (*ListObject) Set ¶
func (o *ListObject) Set(index int, value any)
Set sets the item at index to value.
func (*ListObject) String ¶
func (o *ListObject) String() string
String returns the string representation of the object.
type Marshaler ¶
type Marshaler interface {
MarshalPyObject() *Object
}
Marshaler is the interface implemented by types that can marshal themselves into a Python object.
type ModuleNotFoundError ¶
type ModuleNotFoundError struct {
ImportError
}
ModuleNotFoundError is returned when a Python module cannot be found.
type Object ¶
Object is a wrapper around the C type python3.PyObject.
func ImportModule ¶
ImportModule is a wrapper around the C function PyImport_ImportModule.
func MustImportModule ¶
MustImportModule imports a Python module and panics if it fails.
func MustMarshal ¶
MustMarshal returns the Python object for v or panics if an error occurs.
func (*Object) CallMethodArgs ¶
CallMethodArgs calls a method of the object.
func (*Object) DecRef ¶
func (o *Object) DecRef()
DecRef decreases the reference count of the object.
type Objector ¶
type Objector interface {
AsObject() *Object
}
Objector is an interface for types that can return an Object.
type PyObjector ¶
PyObjector is an interface for types that can return a PyObject.
type Tuple ¶
type Tuple[T any] struct { // contains filtered or unexported fields }
Tuple is a generic Python tuple.
func NewTupleForType ¶
NewTupleForType creates a new tuple for a given type.
func NewTupleFromValues ¶
NewTupleFromValues converts a slice of any data type to a tuple.
func (*Tuple[T]) DecRef ¶
func (t *Tuple[T]) DecRef()
DecRef decreases the reference count of the object.
func (*Tuple[T]) UnmarshalPyObject ¶
UnmarshalPyObject unmarshals a Python object to the tuple.
type TupleObject ¶
TupleObject is a generic Python tuple.
func NewTupleObject ¶
func NewTupleObject(length int) *TupleObject
NewTupleObject creates a new tuple.
func (*TupleObject) AsList ¶
func (o *TupleObject) AsList() *ListObject
AsList converts a tuple to a list.
func (*TupleObject) AsObject ¶
func (o *TupleObject) AsObject() *Object
AsObject returns the tuple as Object.
func (*TupleObject) DecRef ¶
func (o *TupleObject) DecRef()
DecRef decreases the reference count of the object.
func (*TupleObject) Get ¶
func (o *TupleObject) Get(index int) *Object
Get returns the item at index.
func (*TupleObject) Length ¶
func (o *TupleObject) Length() int
Length returns the length of the tuple.
func (*TupleObject) PyObject ¶
func (o *TupleObject) PyObject() *cpy3.PyObject
PyObject returns the underlying PyObject.
func (*TupleObject) Set ¶
func (o *TupleObject) Set(index int, value any)
Set sets the item at index to value.
func (*TupleObject) String ¶
func (o *TupleObject) String() string
String returns the string representation of the object.
type UnmarshalTypeError ¶
type UnmarshalTypeError struct { Value string // Description of the value - "bool", "array", "number -5". Type reflect.Type // Type of Go value it could not be assigned to. Struct string // Name of the struct type containing the field. Field string // The full path from root node to the field. }
An UnmarshalTypeError describes a python Object that was not appropriate for a value of a specific Go type.
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a Python object of themselves.