代码拉取完成,页面将自动刷新
// Dict and StringDict type
//
// The idea is that most dicts just have strings for keys so we use
// the simpler StringDict and promote it into a Dict when necessary
package py
import "bytes"
const dictDoc = `dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)`
var (
StringDictType = NewType("dict", dictDoc)
DictType = NewType("dict", dictDoc)
expectingDict = ExceptionNewf(TypeError, "a dict is required")
)
// String to object dictionary
//
// Used for variables etc where the keys can only be strings
type StringDict map[string]Object
// Type of this StringDict object
func (o StringDict) Type() *Type {
return StringDictType
}
// Make a new dictionary
func NewStringDict() StringDict {
return make(StringDict)
}
// Make a new dictionary with reservation for n entries
func NewStringDictSized(n int) StringDict {
return make(StringDict, n)
}
// Checks that obj is exactly a dictionary and returns an error if not
func DictCheckExact(obj Object) (StringDict, error) {
dict, ok := obj.(StringDict)
if !ok {
return nil, expectingDict
}
return dict, nil
}
// Checks that obj is exactly a dictionary and returns an error if not
func DictCheck(obj Object) (StringDict, error) {
// FIXME should be checking subclasses
return DictCheckExact(obj)
}
// Copy a dictionary
func (d StringDict) Copy() StringDict {
e := make(StringDict, len(d))
for k, v := range d {
e[k] = v
}
return e
}
func (a StringDict) M__str__() (Object, error) {
return a.M__repr__()
}
func (a StringDict) M__repr__() (Object, error) {
var out bytes.Buffer
out.WriteRune('{')
spacer := false
for key, value := range a {
if spacer {
out.WriteString(", ")
}
keyStr, err := ReprAsString(String(key))
if err != nil {
return nil, err
}
valueStr, err := ReprAsString(value)
if err != nil {
return nil, err
}
out.WriteString(keyStr)
out.WriteString(": ")
out.WriteString(valueStr)
spacer = true
}
out.WriteRune('}')
return String(out.String()), nil
}
func (d StringDict) M__getitem__(key Object) (Object, error) {
str, ok := key.(String)
if ok {
res, ok := d[string(str)]
if ok {
return res, nil
}
}
return nil, ExceptionNewf(KeyError, "%v", key)
}
func (d StringDict) M__setitem__(key, value Object) (Object, error) {
str, ok := key.(String)
if !ok {
return nil, ExceptionNewf(KeyError, "FIXME can only have string keys!: %v", key)
}
d[string(str)] = value
return None, nil
}
func (a StringDict) M__eq__(other Object) (Object, error) {
b, ok := other.(StringDict)
if !ok {
return NotImplemented, nil
}
if len(a) != len(b) {
return False, nil
}
for k, av := range a {
bv, ok := b[k]
if !ok {
return False, nil
}
res, err := Eq(av, bv)
if err != nil {
return nil, err
}
if res == False {
return False, nil
}
}
return True, nil
}
func (a StringDict) M__ne__(other Object) (Object, error) {
res, err := a.M__eq__(other)
if err != nil {
return nil, err
}
if res == True {
return False, nil
}
return True, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。