using System;
using System.Runtime.Serialization;
namespace Python.Runtime
{
///
/// 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.
///
[Serializable]
public class PyDict : PyIterable
{
internal PyDict(BorrowedReference reference) : base(reference) { }
internal PyDict(in StolenReference reference) : base(reference) { }
///
/// Creates a new Python dictionary object.
///
public PyDict() : base(Runtime.PyDict_New().StealOrThrow()) { }
///
/// Wraps existing dictionary object.
///
///
/// Thrown if the given object is not a Python dictionary object
///
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) { }
///
/// IsDictType Method
///
///
/// Returns true if the given object is a Python dictionary.
///
public static bool IsDictType(PyObject value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return Runtime.PyDict_Check(value.obj);
}
///
/// HasKey Method
///
///
/// Returns true if the object key appears in the dictionary.
///
public bool HasKey(PyObject key)
{
if (key is null) throw new ArgumentNullException(nameof(key));
return Runtime.PyMapping_HasKey(obj, key.obj) != 0;
}
///
/// HasKey Method
///
///
/// Returns true if the string key appears in the dictionary.
///
public bool HasKey(string key)
{
using var str = new PyString(key);
return HasKey(str);
}
///
/// Keys Method
///
///
/// Returns a sequence containing the keys of the dictionary.
///
public PyIterable Keys()
{
using var items = Runtime.PyDict_Keys(Reference);
if (items.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyIterable(items.Steal());
}
///
/// Values Method
///
///
/// Returns a sequence containing the values of the dictionary.
///
public PyIterable Values()
{
using var items = Runtime.PyDict_Values(obj);
return new PyIterable(items.StealOrThrow());
}
///
/// Items Method
///
///
/// Returns a sequence containing the items of the dictionary.
///
public PyIterable Items()
{
using var items = Runtime.PyDict_Items(this.Reference);
if (items.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyIterable(items.Steal());
}
///
/// Copy Method
///
///
/// Returns a copy of the dictionary.
///
public PyDict Copy()
{
var op = Runtime.PyDict_Copy(Reference);
if (op.IsNull())
{
throw PythonException.ThrowLastAsClrException();
}
return new PyDict(op.Steal());
}
///
/// Update Method
///
///
/// Update the dictionary from another dictionary.
///
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();
}
}
///
/// Clear Method
///
///
/// Clears the dictionary.
///
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;
}
}
}