Ai
3 Star 0 Fork 0

mirrors_go-python/gpython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dict.go 3.42 KB
一键复制 编辑 原始数据 按行查看 历史
Nick Craig-Wood 提交于 2018-08-22 03:56 +08:00 . Add copyright headers to all files
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// 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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_go-python/gpython.git
git@gitee.com:mirrors_go-python/gpython.git
mirrors_go-python
gpython
gpython
v0.0.2

搜索帮助