types

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: BSD-2-Clause Imports: 10 Imported by: 13

Documentation

Overview

Package types provides a minimal implementation for commonly used Python classes, objects and functions.

Most of the types implemented here are related to Python builtins or the Python standard library.

Only a minimal subset of traits is reproduced here for the sole purpose of making them work with the unpickling machine and, more in general, in the context of the packages provided by the GoPickle library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array added in v0.3.0

type Array struct{}

Array unpickles array.array values as documented in:

https://docs.python.org/3/library/array.html

func (Array) Call added in v0.3.0

func (Array) Call(args ...interface{}) (interface{}, error)

type ByteArray

type ByteArray []byte

ByteArray represents a Python "bytearray" (builtin type).

func NewByteArray

func NewByteArray() *ByteArray

NewByteArray makes and returns a new empty ByteArray.

func NewByteArrayFromSlice

func NewByteArrayFromSlice(slice []byte) *ByteArray

NewByteArrayFromSlice makes and returns a new ByteArray initialized with the elements of the given slice.

The new ByteArray is a simple type cast of the input slice; the slice is _not_ copied.

func (*ByteArray) Get

func (b *ByteArray) Get(i int) byte

Get returns the element of the ByteArray at the given index.

It panics if the index is out of range.

func (*ByteArray) Len

func (b *ByteArray) Len() int

Len returns the length of the ByteArray.

type Callable

type Callable interface {
	// Call mimics a direct invocation on a Python value, such as a function
	// or class (constructor).
	Call(args ...interface{}) (interface{}, error)
}

Callable is implemented by any value that can be directly called to get a new value.

It is usually implemented by Python-like functions (returning a value given some arguments), or classes (typically returning an instance given some constructor arguments).

type Dict

type Dict []DictEntry

Dict represents a Python "dict" (builtin type).

It is implemented as a slice, instead of a map, because in Go not all types can be map's keys (e.g. slices).

func NewDict

func NewDict() *Dict

NewDict makes and returns a new empty Dict.

func (*Dict) Call added in v0.3.0

func (*Dict) Call(args ...interface{}) (interface{}, error)

func (*Dict) Get

func (d *Dict) Get(key interface{}) (interface{}, bool)

Get returns the value associated with the given key (if any), and whether the key is present or not.

func (*Dict) Keys added in v0.3.0

func (d *Dict) Keys() []interface{}

Keys returns the keys of the dict

func (*Dict) Len

func (d *Dict) Len() int

Len returns the length of the Dict, that is, the amount of key/value pairs contained by the Dict.

func (*Dict) MustGet

func (d *Dict) MustGet(key interface{}) interface{}

MustGet returns the value associated with the given key, if if it exists, otherwise it panics.

func (*Dict) Set

func (d *Dict) Set(key, value interface{})

Set sets into the Dict the given key/value pair.

func (*Dict) String added in v0.3.0

func (d *Dict) String() string

type DictEntry

type DictEntry struct {
	Key   interface{}
	Value interface{}
}

type DictSetter

type DictSetter interface {
	Set(key, value interface{})
}

DictSetter is implemented by any value that exhibits a dict-like behaviour, allowing arbitrary key/value pairs to be set.

type FrozenSet

type FrozenSet map[interface{}]frozenSetEmptyStruct

FrozenSet represents a Python "frozenset" (builtin type).

It is implemented in Go as a map with empty struct values; the actual set of generic "interface{}" items is thus represented by all the keys.

func NewFrozenSetFromSlice

func NewFrozenSetFromSlice(slice []interface{}) *FrozenSet

NewFrozenSetFromSlice makes and returns a new FrozenSet initialized with the elements of the given slice.

func (*FrozenSet) Has

func (f *FrozenSet) Has(v interface{}) bool

Has returns whether the given value is present in the FrozenSet (true) or not (false).

func (*FrozenSet) Len

func (f *FrozenSet) Len() int

Len returns the length of the FrozenSet.

type GenericClass

type GenericClass struct {
	Module string
	Name   string
}

func NewGenericClass

func NewGenericClass(module, name string) *GenericClass

func (*GenericClass) PyNew

func (g *GenericClass) PyNew(args ...interface{}) (interface{}, error)

type GenericObject

type GenericObject struct {
	Class           *GenericClass
	ConstructorArgs []interface{}
}

type List

type List []interface{}

List represents a Python "list" (builtin type).

func NewList

func NewList() *List

NewList makes and returns a new empty List.

func NewListFromSlice

func NewListFromSlice(slice []interface{}) *List

NewListFromSlice makes and returns a new List initialized with the elements of the given slice.

The new List is a simple type cast of the input slice; the slice is _not_ copied.

func (*List) Append

func (l *List) Append(v interface{})

Append appends one element to the end of the List.

func (*List) Call added in v0.3.0

func (*List) Call(args ...interface{}) (interface{}, error)

func (*List) Get

func (l *List) Get(i int) interface{}

Get returns the element of the List at the given index.

It panics if the index is out of range.

func (*List) Len

func (l *List) Len() int

Len returns the length of the List.

func (*List) String added in v0.3.0

func (l *List) String() string

type ListAppender

type ListAppender interface {
	Append(v interface{})
}

ListAppender is implemented by any value that exhibits a list-like behaviour, allowing arbitrary values to be appended.

type ObjectClass

type ObjectClass struct{}

func (*ObjectClass) PyNew

func (o *ObjectClass) PyNew(args ...interface{}) (interface{}, error)

type OrderedDict

type OrderedDict struct {
	// Map associates a key of any type (interface{}) to OrderedDictEntry
	// pointer values. These values are shared with List.
	Map map[interface{}]*OrderedDictEntry
	// List is an ordered list of OrderedDictEntry pointers, which are
	// also shared with Map.
	List *list.List
	// PyDict represents Python "object.__dict__" dictionary of attributes.
	PyDict map[string]interface{}
}

OrderedDict is a minimal and trivial implementation of an ordered map, which represent a Python "collections.OrderedDict" object.

It is composed by a simple unordered Map, and a List to keep the order of the entries. The former is useful for direct key lookups, the latter for iteration.

func NewOrderedDict

func NewOrderedDict() *OrderedDict

NewOrderedDict makes and returns a new empty OrderedDict.

func (*OrderedDict) Get

func (o *OrderedDict) Get(k interface{}) (interface{}, bool)

Get returns the value associated with the given key (if any), and whether the key is present or not.

func (*OrderedDict) Len

func (o *OrderedDict) Len() int

Len returns the length of the OrderedDict, that is, the amount of key/value pairs contained by the OrderedDict.

func (*OrderedDict) MustGet

func (o *OrderedDict) MustGet(key interface{}) interface{}

MustGet returns the value associated with the given key, if if it exists, otherwise it panics.

func (*OrderedDict) PyDictSet

func (o *OrderedDict) PyDictSet(key, value interface{}) error

PyDictSet mimics the setting of a key/value pair on Python "__dict__" attribute of the OrderedDict.

func (*OrderedDict) Set

func (o *OrderedDict) Set(k, v interface{})

Set sets into the OrderedDict the given key/value pair. If the key does not exist yet, the new pair is positioned at the end (back) of the OrderedDict. If the key already exists, the existing associated value is replaced with the new one, and the original position is maintained.

type OrderedDictClass

type OrderedDictClass struct{}

OrderedDictClass represent Python "collections.OrderedDict" class.

This class allows the indirect creation of OrderedDict objects.

func (*OrderedDictClass) Call

func (*OrderedDictClass) Call(args ...interface{}) (interface{}, error)

Call returns a new empty OrderedDict. It is equivalent to Python constructor "collections.OrderedDict()".

No arguments are supported.

type OrderedDictEntry

type OrderedDictEntry struct {
	// Key of a single OrderedDict's entry.
	Key interface{}
	// Value of a single OrderedDict's entry.
	Value interface{}
	// ListElement is a pointer to the OrderedDict's List Element which
	// contains this very OrderedDictEntry.
	ListElement *list.Element
}

OrderedDictEntry is a single key/value pair stored in an OrderedDict.

A pointer to an OrderedDictEntry is always shared between OrderedDict's Map and List.

type PyAttrSettable

type PyAttrSettable interface {
	// PySetAttr mimics the setting of an arbitrary value to an object's
	// attribute.
	//
	// In Python this is done with "setattr" function, to which object,
	// attribute name, and value are passed. For an easy and clear
	// implementation, here instead we require this method to be implemented
	// on the "object" itself.
	//
	// See: https://docs.python.org/3/library/functions.html#setattr
	PySetAttr(key string, value interface{}) error
}

PyAttrSettable is implemented by any value on which an existing or new Python-like attribute can be set. In Python this is done with "setattr" builtin function.

type PyDictSettable

type PyDictSettable interface {
	// PyDictSet mimics the setting of a key/value pair on an object's
	//"__dict__" attribute.
	//
	// See: https://docs.python.org/3/library/stdtypes.html#object.__dict__
	PyDictSet(key, value interface{}) error
}

PyDictSettable is implemented by any value that can store dictionary-like key/value pairs. It reflects Python behavior of setting a key/value pair on an object's "__dict__" attribute.

type PyNewable

type PyNewable interface {
	// PyNew mimics Python invocation of the "__new__" method, usually
	// provided by classes.
	//
	// See: https://docs.python.org/3/reference/datamodel.html#object.__new__
	PyNew(args ...interface{}) (interface{}, error)
}

PyNewable is implemented by any value that has a Python-like "__new__" method.

It is usually implemented by values representing Python classes.

type PyStateSettable

type PyStateSettable interface {
	// PySetState mimics Python invocation of the "__setstate__" method.
	//
	// See: https://docs.python.org/3/library/pickle.html#object.__setstate__
	PySetState(state interface{}) error
}

PyStateSettable is implemented by any value that has a Python-like "__setstate__" method.

type Reconstructor

type Reconstructor struct{}

func (*Reconstructor) Call

func (r *Reconstructor) Call(args ...interface{}) (interface{}, error)

type Set

type Set map[interface{}]setEmptyStruct

Set represents a Python "set" (builtin type).

It is implemented in Go as a map with empty struct values; the actual set of generic "interface{}" items is thus represented by all the keys.

func NewSet

func NewSet() *Set

NewSet makes and returns a new empty Set.

func NewSetFromSlice

func NewSetFromSlice(slice []interface{}) *Set

NewSetFromSlice makes and returns a new Set initialized with the elements of the given slice.

func (*Set) Add

func (s *Set) Add(v interface{})

Add adds one element to the Set.

func (*Set) Has

func (s *Set) Has(v interface{}) bool

Has returns whether the given value is present in the Set (true) or not (false).

func (*Set) Len

func (s *Set) Len() int

Len returns the length of the Set.

type SetAdder

type SetAdder interface {
	Add(v interface{})
}

SetAdder is implemented by any value that exhibits a set-like behaviour, allowing arbitrary values to be added.

type Tuple

type Tuple []interface{}

func NewTupleFromSlice

func NewTupleFromSlice(slice []interface{}) *Tuple

func (*Tuple) Get

func (t *Tuple) Get(i int) interface{}

func (*Tuple) Len

func (t *Tuple) Len() int

func (*Tuple) String added in v0.3.0

func (t *Tuple) String() string

Jump to

Keyboard shortcuts

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