summaryrefslogtreecommitdiff
path: root/python/metadata.py
diff options
context:
space:
mode:
authorPeter LaFosse <peter@vector35.com>2020-05-02 11:01:35 -0400
committerPeter LaFosse <peter@vector35.com>2020-05-03 13:20:56 -0400
commit8f1bc5944b849f3ad0cbbd5972d5ab9948ed4d57 (patch)
tree43b48da8a1ec93b148575bc3346059e5316e6bf6 /python/metadata.py
parent5b84fdea8abcb4abba61d07e0d12b4115b516c52 (diff)
Fix up equality operators and had hash operators
Diffstat (limited to 'python/metadata.py')
-rw-r--r--python/metadata.py277
1 files changed, 139 insertions, 138 deletions
diff --git a/python/metadata.py b/python/metadata.py
index 27b0abb0..3fe78415 100644
--- a/python/metadata.py
+++ b/python/metadata.py
@@ -66,85 +66,78 @@ class Metadata(object):
else:
raise ValueError("{} doesn't contain type of: int, bool, str, float, list, dict".format(type(value).__name__))
- @property
- def value(self):
- if self.is_integer:
- return int(self)
- elif self.is_string:
- return str(self)
- elif self.is_raw:
- return bytes(self)
- elif self.is_float:
- return float(self)
- elif self.is_boolean:
- return bool(self)
- elif self.is_array:
- return list(self)
- elif self.is_dict:
- return self.get_dict()
- raise TypeError()
-
- def get_dict(self):
- if not self.is_dict:
- raise TypeError()
- result = {}
- for key in self:
- result[key] = self[key]
- return result
-
- @property
- def type(self):
- return MetadataType(core.BNMetadataGetType(self.handle))
-
- @property
- def is_integer(self):
- return self.is_signed_integer or self.is_unsigned_integer
-
- @property
- def is_signed_integer(self):
- return core.BNMetadataIsSignedInteger(self.handle)
-
- @property
- def is_unsigned_integer(self):
- return core.BNMetadataIsUnsignedInteger(self.handle)
-
- @property
- def is_float(self):
- return core.BNMetadataIsDouble(self.handle)
-
- @property
- def is_boolean(self):
- return core.BNMetadataIsBoolean(self.handle)
-
- @property
- def is_string(self):
- return core.BNMetadataIsString(self.handle)
-
- @property
- def is_raw(self):
- return core.BNMetadataIsRaw(self.handle)
-
- @property
- def is_array(self):
- return core.BNMetadataIsArray(self.handle)
-
- @property
- def is_dict(self):
- return core.BNMetadataIsKeyValueStore(self.handle)
-
- def remove(self, key_or_index):
- if isinstance(key_or_index, str) and self.is_dict:
- core.BNMetadataRemoveKey(self.handle, key_or_index)
- elif isinstance(key_or_index, int) and self.is_array:
- core.BNMetadataRemoveIndex(self.handle, key_or_index)
- else:
- raise TypeError("remove only valid for dict and array objects")
-
def __len__(self):
if self.is_array or self.is_dict or self.is_string or self.is_raw:
return core.BNMetadataSize(self.handle)
raise Exception("Metadata object doesn't support len()")
+ def __eq__(self, other):
+ if isinstance(other, int) and self.is_integer:
+ return int(self) == other
+ elif isinstance(other, str) and (self.is_string or self.is_raw):
+ return str(self) == other
+ elif isinstance(other, float) and self.is_float:
+ return float(self) == other
+ elif isinstance(other, bool) and self.is_boolean:
+ return bool(self) == other
+ elif self.is_array and ((isinstance(other, Metadata) and other.is_array) or isinstance(other, list)):
+ if len(self) != len(other):
+ return False
+ for a, b in zip(self, other):
+ if a != b:
+ return False
+ return True
+ elif self.is_dict and ((isinstance(other, Metadata) and other.is_dict) or isinstance(other, dict)):
+ if len(self) != len(other):
+ return False
+ for a, b in zip(self, other):
+ if a != b or self[a] != other[b]:
+ return False
+ return True
+ elif isinstance(other, Metadata) and self.is_integer and other.is_integer:
+ return int(self) == int(other)
+ elif isinstance(other, Metadata) and (self.is_string or self.is_raw) and (other.is_string or other.is_raw):
+ return str(self) == str(other)
+ elif isinstance(other, Metadata) and self.is_float and other.is_float:
+ return float(self) == float(other)
+ elif isinstance(other, Metadata) and self.is_boolean and other.is_boolean:
+ return bool(self) == bool(other)
+ return NotImplemented
+
+ def __ne__(self, other):
+ if isinstance(other, int) and self.is_integer:
+ return int(self) != other
+ elif isinstance(other, str) and (self.is_string or self.is_raw):
+ return str(self) != other
+ elif isinstance(other, float) and self.is_float:
+ return float(self) != other
+ elif isinstance(other, bool):
+ return bool(self) != other
+ elif self.is_array and ((isinstance(other, Metadata) and other.is_array) or isinstance(other, list)):
+ if len(self) != len(other):
+ return True
+ areEqual = True
+ for a, b in zip(self, other):
+ if a != b:
+ areEqual = False
+ return not areEqual
+ elif self.is_dict and ((isinstance(other, Metadata) and other.is_dict) or isinstance(other, dict)):
+ if len(self) != len(other):
+ return True
+ for a, b in zip(self, other):
+ if a != b or self[a] != other[b]:
+ return True
+ return False
+ elif isinstance(other, Metadata) and self.is_integer and other.is_integer:
+ return int(self) != int(other)
+ elif isinstance(other, Metadata) and (self.is_string or self.is_raw) and (other.is_string or other.is_raw):
+ return str(self) != str(other)
+ elif isinstance(other, Metadata) and self.is_float and other.is_float:
+ return float(self) != float(other)
+ elif isinstance(other, Metadata) and self.is_boolean and other.is_boolean:
+ return bool(self) != bool(other)
+ return NotImplemented
+
def __iter__(self):
if self.is_array:
for i in range(core.BNMetadataSize(self.handle)):
@@ -215,68 +208,76 @@ class Metadata(object):
raise ValueError("Metadata object is not boolean type")
return core.BNMetadataGetBoolean(self.handle)
- def __eq__(self, other):
- if isinstance(other, int) and self.is_integer:
- return int(self) == other
- elif isinstance(other, str) and (self.is_string or self.is_raw):
- return str(self) == other
- elif isinstance(other, float) and self.is_float:
- return float(self) == other
- elif isinstance(other, bool) and self.is_boolean:
- return bool(self) == other
- elif self.is_array and ((isinstance(other, Metadata) and other.is_array) or isinstance(other, list)):
- if len(self) != len(other):
- return False
- for a, b in zip(self, other):
- if a != b:
- return False
- return True
- elif self.is_dict and ((isinstance(other, Metadata) and other.is_dict) or isinstance(other, dict)):
- if len(self) != len(other):
- return False
- for a, b in zip(self, other):
- if a != b or self[a] != other[b]:
- return False
- return True
- elif isinstance(other, Metadata) and self.is_integer and other.is_integer:
- return int(self) == int(other)
- elif isinstance(other, Metadata) and (self.is_string or self.is_raw) and (other.is_string or other.is_raw):
- return str(self) == str(other)
- elif isinstance(other, Metadata) and self.is_float and other.is_float:
- return float(self) == float(other)
- elif isinstance(other, Metadata) and self.is_boolean and other.is_boolean:
- return bool(self) == bool(other)
- raise NotImplementedError()
+ @property
+ def value(self):
+ if self.is_integer:
+ return int(self)
+ elif self.is_string:
+ return str(self)
+ elif self.is_raw:
+ return bytes(self)
+ elif self.is_float:
+ return float(self)
+ elif self.is_boolean:
+ return bool(self)
+ elif self.is_array:
+ return list(self)
+ elif self.is_dict:
+ return self.get_dict()
+ raise TypeError()
- def __ne__(self, other):
- if isinstance(other, int) and self.is_integer:
- return int(self) != other
- elif isinstance(other, str) and (self.is_string or self.is_raw):
- return str(self) != other
- elif isinstance(other, float) and self.is_float:
- return float(self) != other
- elif isinstance(other, bool):
- return bool(self) != other
- elif self.is_array and ((isinstance(other, Metadata) and other.is_array) or isinstance(other, list)):
- if len(self) != len(other):
- return True
- areEqual = True
- for a, b in zip(self, other):
- if a != b:
- areEqual = False
- return not areEqual
- elif self.is_dict and ((isinstance(other, Metadata) and other.is_dict) or isinstance(other, dict)):
- if len(self) != len(other):
- return True
- for a, b in zip(self, other):
- if a != b or self[a] != other[b]:
- return True
- return False
- elif isinstance(other, Metadata) and self.is_integer and other.is_integer:
- return int(self) != int(other)
- elif isinstance(other, Metadata) and (self.is_string or self.is_raw) and (other.is_string or other.is_raw):
- return str(self) != str(other)
- elif isinstance(other, Metadata) and self.is_float and other.is_float:
- return float(self) != float(other)
- elif isinstance(other, Metadata) and self.is_boolean and other.is_boolean:
- return bool(self) != bool(other)
+ def get_dict(self):
+ if not self.is_dict:
+ raise TypeError()
+ result = {}
+ for key in self:
+ result[key] = self[key]
+ return result
+
+ @property
+ def type(self):
+ return MetadataType(core.BNMetadataGetType(self.handle))
+
+ @property
+ def is_integer(self):
+ return self.is_signed_integer or self.is_unsigned_integer
+
+ @property
+ def is_signed_integer(self):
+ return core.BNMetadataIsSignedInteger(self.handle)
+
+ @property
+ def is_unsigned_integer(self):
+ return core.BNMetadataIsUnsignedInteger(self.handle)
+
+ @property
+ def is_float(self):
+ return core.BNMetadataIsDouble(self.handle)
+
+ @property
+ def is_boolean(self):
+ return core.BNMetadataIsBoolean(self.handle)
+
+ @property
+ def is_string(self):
+ return core.BNMetadataIsString(self.handle)
+
+ @property
+ def is_raw(self):
+ return core.BNMetadataIsRaw(self.handle)
+
+ @property
+ def is_array(self):
+ return core.BNMetadataIsArray(self.handle)
+
+ @property
+ def is_dict(self):
+ return core.BNMetadataIsKeyValueStore(self.handle)
+
+ def remove(self, key_or_index):
+ if isinstance(key_or_index, str) and self.is_dict:
+ core.BNMetadataRemoveKey(self.handle, key_or_index)
+ elif isinstance(key_or_index, int) and self.is_array:
+ core.BNMetadataRemoveIndex(self.handle, key_or_index)
+ else:
+ raise TypeError("remove only valid for dict and array objects") \ No newline at end of file