1
+ struct DictPairSet{K,V,T<: AbstractDict{K,V} } <: AbstractSet{Tuple{K,V}}
2
+ dict:: T
3
+ end
4
+ Base. length (x:: DictPairSet ) = length (x. dict)
5
+ Base. iterate (x:: DictPairSet ) =
6
+ (r = iterate (x. dict); r === nothing ? nothing : (Tuple (r[1 ]), r[2 ]))
7
+ Base. iterate (x:: DictPairSet , st) =
8
+ (r = iterate (x. dict, st); r === nothing ? nothing : (Tuple (r[1 ]), r[2 ]))
9
+ Base. in (v:: Pair , x:: DictPairSet ) = v in x. dict
10
+ Base. in (v:: Tuple{Any,Any} , x:: DictPairSet ) = Pair (v[1 ], v[2 ]) in x. dict
11
+
12
+ pyjldict_iter (x:: AbstractDict ) = Py (Iterator (keys (x)))
13
+
14
+ pyjldict_contains (x:: AbstractDict , k:: Py ) = Py (haskey (x, @pyconvert (keytype (x), k, return Py (false ))))
15
+
16
+ pyjldict_clear (x:: AbstractDict ) = (empty! (x); Py (nothing ))
17
+
18
+ pyjldict_getitem (x:: AbstractDict , k:: Py ) = Py (x[pyconvert (keytype (x), k)])
19
+
20
+ pyjldict_setitem (x:: AbstractDict , k:: Py , v:: Py ) = (x[pyconvertarg (keytype (x), k, " key" )] = pyconvertarg (valtype (x), v, " value" ); Py (nothing ))
21
+
22
+ pyjldict_delitem (x:: AbstractDict , k:: Py ) = (delete! (x, pyconvert (keytype (x), k)); Py (nothing ))
23
+
24
+ function pyjldict_update (x:: AbstractDict , items_:: Py )
25
+ for item_ in items_
26
+ (k, v) = pyconvert_and_del (Tuple{keytype (x), valtype (x)}, item_)
27
+ x[k] = v
28
+ end
29
+ Py (nothing )
30
+ end
31
+
1
32
const pyjldicttype = pynew ()
2
33
3
34
function init_jlwrap_dict ()
@@ -7,6 +38,68 @@ function init_jlwrap_dict()
7
38
class DictValue(AnyValue):
8
39
__slots__ = ()
9
40
__module__ = "juliacall"
41
+ _jl_undefined_ = object()
42
+ def __iter__(self):
43
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_iter)) )
44
+ def __contains__(self, key):
45
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_contains)) , key)
46
+ def __getitem__(self, key):
47
+ if key in self:
48
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_getitem)) , key)
49
+ else:
50
+ raise KeyError(key)
51
+ def __setitem__(self, key, value):
52
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_setitem)) , key, value)
53
+ def __delitem__(self, key):
54
+ if key in self:
55
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_delitem)) , key)
56
+ else:
57
+ raise KeyError(key)
58
+ def keys(self):
59
+ return self._jl_callmethod($(pyjl_methodnum (Py ∘ keys)) )
60
+ def values(self):
61
+ return self._jl_callmethod($(pyjl_methodnum (Py ∘ values)) )
62
+ def items(self):
63
+ return self._jl_callmethod($(pyjl_methodnum (Py ∘ DictPairSet)) )
64
+ def get(self, key, default=None):
65
+ if key in self:
66
+ return self[key]
67
+ else:
68
+ return default
69
+ def setdefault(self, key, default=None):
70
+ if key not in self:
71
+ self[key] = default
72
+ return self[key]
73
+ def clear(self):
74
+ return self._jl_callmethod($(pyjl_methodnum (pyjldict_clear)) )
75
+ def pop(self, key, default=_jl_undefined_):
76
+ if key in self:
77
+ ans = self[key]
78
+ del self[key]
79
+ return ans
80
+ elif default is self._jl_undefined_:
81
+ raise KeyError(key)
82
+ else:
83
+ return default
84
+ def popitem(self):
85
+ if len(self):
86
+ return self._jl_callmethod($(pyjl_methodnum (Py ∘ pop!)) )
87
+ else:
88
+ raise KeyError()
89
+ def update(self, other=_jl_undefined_, **kwargs):
90
+ if other is self._jl_undefined_:
91
+ pass
92
+ else:
93
+ if hasattr(other, "keys"):
94
+ items = ((k, other[k]) for k in other.keys())
95
+ else:
96
+ items = other
97
+ self._jl_callmethod($(pyjl_methodnum (pyjldict_update)) , items)
98
+ if kwargs:
99
+ self.update(kwargs)
100
+ import collections.abc
101
+ collections.abc.MutableMapping.register(DictValue)
102
+ del collections
10
103
""" , filename, " exec" ), jl. __dict__)
11
104
pycopy! (pyjldicttype, jl. DictValue)
12
105
end
0 commit comments