Skip to content

Instantly share code, notes, and snippets.

@igorcoding
Created August 18, 2016 18:33
Show Gist options
  • Save igorcoding/04952b3bfd81c160a38bbf0747e4d11f to your computer and use it in GitHub Desktop.
Save igorcoding/04952b3bfd81c160a38bbf0747e4d11f to your computer and use it in GitHub Desktop.
class ObjectId(JsonSerializable, str):
def __init__(self, object_id):
super().__init__()
self.object_id = int(object_id)
def __str__(self):
return str(self.object_id)
def __int__(self):
return self.object_id
def __repr__(self):
return "<ObjectId: {}>".format(self.object_id)
def __eq__(self, other):
if isinstance(other, int):
return self.object_id == other
elif isinstance(other, ObjectId):
return self.object_id == other.object_id
return super().__eq__(other)
def __ne__(self, other):
return not (self == other)
def __lt__(self, other):
if isinstance(other, int):
return self.object_id < other
elif isinstance(other, ObjectId):
return self.object_id < other.object_id
return super().__lt__(other)
def __gt__(self, other):
if isinstance(other, int):
return self.object_id > other
elif isinstance(other, ObjectId):
return self.object_id > other.object_id
return super().__gt__(other)
def __le__(self, other):
if isinstance(other, int):
return self.object_id <= other
elif isinstance(other, ObjectId):
return self.object_id <= other.object_id
return super().__le__(other)
def __ge__(self, other):
if isinstance(other, int):
return self.object_id >= other
elif isinstance(other, ObjectId):
return self.object_id >= other.object_id
return super().__ge__(other)
def __hash__(self, *args, **kwargs):
return int.__hash__(self.object_id, *args, **kwargs)
def __to_json__(self):
return str(self)
def ensure_object_id(object_id):
if object_id is None:
return None
if isinstance(object_id, ObjectId):
return object_id
return ObjectId(object_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment