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 ¶
- type Array
- type ByteArray
- type Callable
- type Dict
- func (*Dict) Call(args ...interface{}) (interface{}, error)
- func (d *Dict) Get(key interface{}) (interface{}, bool)
- func (d *Dict) Keys() []interface{}
- func (d *Dict) Len() int
- func (d *Dict) MustGet(key interface{}) interface{}
- func (d *Dict) Set(key, value interface{})
- func (d *Dict) String() string
- type DictEntry
- type DictSetter
- type FrozenSet
- type GenericClass
- type GenericObject
- type List
- type ListAppender
- type ObjectClass
- type OrderedDict
- type OrderedDictClass
- type OrderedDictEntry
- type PyAttrSettable
- type PyDictSettable
- type PyNewable
- type PyStateSettable
- type Reconstructor
- type Set
- type SetAdder
- type Tuple
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
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 ¶
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.
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 (*Dict) Get ¶
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 ¶
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.
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.
type GenericClass ¶
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 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.
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 NewSetFromSlice ¶
func NewSetFromSlice(slice []interface{}) *Set
NewSetFromSlice makes and returns a new Set initialized with the elements of the given slice.