Skip to content

Commit 9b2f687

Browse files
author
Christopher Doris
committed
implement jlwrap dict
1 parent 9f5b3d3 commit 9b2f687

File tree

4 files changed

+99
-184
lines changed

4 files changed

+99
-184
lines changed

src/jlwrap/dict.jl

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
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+
132
const pyjldicttype = pynew()
233

334
function init_jlwrap_dict()
@@ -7,6 +38,68 @@ function init_jlwrap_dict()
738
class DictValue(AnyValue):
839
__slots__ = ()
940
__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
10103
""", filename, "exec"), jl.__dict__)
11104
pycopy!(pyjldicttype, jl.DictValue)
12105
end

src/jlwrap/set.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ function init_jlwrap_set()
114114
return self._jl_callmethod($(pyjl_methodnum(pyjlset_symmetric_difference_update)), other)
115115
def update(self, other):
116116
return self._jl_callmethod($(pyjl_methodnum(pyjlset_update)), other)
117+
import collections.abc
118+
collections.abc.MutableSet.register(SetValue)
119+
del collections
117120
""", filename, "exec"), jl.__dict__)
118121
pycopy!(pyjlsettype, jl.SetValue)
119122
end

src/jlwrap/vector.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ function init_jlwrap_vector()
143143
return self._jl_callmethod($(pyjl_methodnum(pyjlvector_index)), value)
144144
def count(self, value):
145145
return self._jl_callmethod($(pyjl_methodnum(pyjlvector_count)), value)
146+
import collections.abc
147+
collections.abc.MutableSequence.register(VectorValue)
148+
del collections
146149
""", filename, "exec"), jl.__dict__)
147150
pycopy!(pyjlvectortype, jl.VectorValue)
148151
end

src/old/cpython/juliadict.jl

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)