added __contains__ for maps so std python "in" syntax works for checking keys. added to tests.

This commit is contained in:
Randall C. O'Reilly 2019-06-10 03:50:28 -06:00 committed by Sebastien Binet
parent 4590bed3b4
commit e28bf163ce
4 changed files with 33 additions and 8 deletions

View File

@ -28,6 +28,8 @@ if testall:
print('map a[1]:', a[1])
print('map a[2]:', a[2])
print('2 in map:', 2 in a)
print('3 in map:', 3 in a)
# TODO: not sure why python2 doesn't just catch this error, but it doesn't seem to..
# try:

View File

@ -198,11 +198,7 @@ otherwise parameter is a python list that we copy from
g.pywrap.Printf("def keys(self):\n")
g.pywrap.Indent()
if ksym.hasHandle() {
g.pywrap.Printf("return %s(handle=_%s_keys(self.handle))\n", keyslnm, qNm)
} else {
g.pywrap.Printf("return %s(handle=_%s_keys(self.handle))\n", keyslnm, qNm)
}
g.pywrap.Printf("return %s(handle=_%s_keys(self.handle))\n", keyslnm, qNm)
g.pywrap.Outdent()
g.pywrap.Printf("def values(self):\n")
@ -232,6 +228,15 @@ otherwise parameter is a python list that we copy from
g.pywrap.Printf("return iter(self.items())\n")
g.pywrap.Outdent()
g.pywrap.Printf("def __contains__(self, key):\n")
g.pywrap.Indent()
if ksym.hasHandle() {
g.pywrap.Printf("return _%s_contains(self.handle, key.handle)\n", qNm)
} else {
g.pywrap.Printf("return _%s_contains(self.handle, key)\n", qNm)
}
g.pywrap.Outdent()
// g.pywrap.Printf("def __next__(self):\n")
// g.pywrap.Indent()
// g.pywrap.Printf("if self.index >= len(self):\n")
@ -291,6 +296,22 @@ otherwise parameter is a python list that we copy from
g.pybuild.Printf("mod.add_function('%s_elem', retval('%s'), [param('%s', 'handle'), param('%s', '_ky')])\n", slNm, esym.cpyname, PyHandle, ksym.cpyname)
// contains
g.gofile.Printf("//export %s_contains\n", slNm)
g.gofile.Printf("func %s_contains(handle CGoHandle, _ky %s) C.char {\n", slNm, ksym.cgoname)
g.gofile.Indent()
g.gofile.Printf("s := *ptrFromHandle_%s(handle)\n", slNm)
if ksym.py2go != "" {
g.gofile.Printf("_, ok := s[%s(_ky)%s]\n", ksym.py2go, ksym.py2goParenEx)
} else {
g.gofile.Printf("_, ok := s[_ky]\n")
}
g.gofile.Printf("return boolGoToPy(ok)\n")
g.gofile.Outdent()
g.gofile.Printf("}\n\n")
g.pybuild.Printf("mod.add_function('%s_contains', retval('bool'), [param('%s', 'handle'), param('%s', '_ky')])\n", slNm, PyHandle, ksym.cpyname)
// set
g.gofile.Printf("//export %s_set\n", slNm)
g.gofile.Printf("func %s_set(handle CGoHandle, _ky %s, _vl %s) {\n", slNm, ksym.cgoname, esym.cgoname)

View File

@ -28,7 +28,7 @@
"Run Proj"
],
"Find": {
"Find": "genMethod",
"Find": "bool",
"Replace": "VarFromHandle",
"IgnoreCase": false,
"Langs": [
@ -37,6 +37,7 @@
],
"Loc": "FindLocAll",
"FindHist": [
"bool",
"genMethod",
"genFuncBody",
"boolPyToGo",
@ -85,8 +86,7 @@
"interface{}",
"types.New",
"C.Raise",
"mutex",
"t.Parallel"
"mutex"
],
"ReplHist": [
"VarFromHandle",

View File

@ -529,6 +529,8 @@ func TestBuiltinMaps(t *testing.T) {
lang: features[path],
want: []byte(`map a[1]: 3.0
map a[2]: 5.0
2 in map: True
3 in map: False
maps.Sum from Go map: 8.0
map b: {1: 3.0, 2: 5.0}
maps.Sum from Python dictionary: 8.0