forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyDict.cs
More file actions
183 lines (158 loc) · 5.16 KB
/
PyDict.cs
File metadata and controls
183 lines (158 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Runtime.Serialization;
namespace Python.Runtime
{
/// <summary>
/// Represents a Python dictionary object. See the documentation at
/// PY2: https://docs.python.org/2/c-api/dict.html
/// PY3: https://docs.python.org/3/c-api/dict.html
/// for details.
/// </summary>
[Serializable]
public class PyDict : PyIterable
{
internal PyDict(BorrowedReference reference) : base(reference) { }
internal PyDict(in StolenReference reference) : base(reference) { }
/// <summary>
/// Creates a new Python dictionary object.
/// </summary>
public PyDict() : base(Runtime.PyDict_New().StealOrThrow()) { }
/// <summary>
/// Wraps existing dictionary object.
/// </summary>
/// <exception cref="ArgumentException">
/// Thrown if the given object is not a Python dictionary object
/// </exception>
public PyDict(PyObject o) : base(o is null ? throw new ArgumentNullException(nameof(o)) : o.Reference)
{
if (!IsDictType(o))
{
throw new ArgumentException("object is not a dict");
}
}
protected PyDict(SerializationInfo info, StreamingContext context)
: base(info, context) { }
/// <summary>
/// IsDictType Method
/// </summary>
/// <remarks>
/// Returns true if the given object is a Python dictionary.
/// </remarks>
public static bool IsDictType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PyDict_Check(value.obj);
}
/// <summary>
/// HasKey Method
/// </summary>
/// <remarks>
/// Returns true if the object key appears in the dictionary.
/// </remarks>
public bool HasKey(PyObject key)
{
if (key is null) throw new ArgumentNullException(nameof(key));
return Runtime.PyMapping_HasKey(obj, key.obj) != 0;
}
/// <summary>
/// HasKey Method
/// </summary>
/// <remarks>
/// Returns true if the string key appears in the dictionary.
/// </remarks>
public bool HasKey(string key)
{
using var str = new PyString(key);
return HasKey(str);
}
/// <summary>
/// Keys Method
/// </summary>
/// <remarks>
/// Returns a sequence containing the keys of the dictionary.
/// </remarks>
public PyIterable Keys()
{
using var items = Runtime.PyDict_Keys(Reference);
if (items.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyIterable(items.Steal());
}
/// <summary>
/// Values Method
/// </summary>
/// <remarks>
/// Returns a sequence containing the values of the dictionary.
/// </remarks>
public PyIterable Values()
{
using var items = Runtime.PyDict_Values(obj);
return new PyIterable(items.StealOrThrow());
}
/// <summary>
/// Items Method
/// </summary>
/// <remarks>
/// Returns a sequence containing the items of the dictionary.
/// </remarks>
public PyIterable Items()
{
using var items = Runtime.PyDict_Items(this.Reference);
if (items.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyIterable(items.Steal());
}
/// <summary>
/// Copy Method
/// </summary>
/// <remarks>
/// Returns a copy of the dictionary.
/// </remarks>
public PyDict Copy()
{
var op = Runtime.PyDict_Copy(Reference);
if (op.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyDict(op.Steal());
}
/// <summary>
/// Update Method
/// </summary>
/// <remarks>
/// Update the dictionary from another dictionary.
/// </remarks>
public void Update(PyObject other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
int result = Runtime.PyDict_Update(Reference, other.Reference);
if (result < 0)
{
throw PythonException.ThrowLastAsClrException();
}
}
/// <summary>
/// Clear Method
/// </summary>
/// <remarks>
/// Clears the dictionary.
/// </remarks>
public void Clear()
{
Runtime.PyDict_Clear(obj);
}
public override int GetHashCode() => rawPtr.GetHashCode();
public override bool Equals(PyObject? other)
{
if (other is null) return false;
if (obj == other.obj) return true;
if (other is PyDict || IsDictType(other)) return base.Equals(other);
return false;
}
}
}