Skip to content

Commit

Permalink
Merge pull request #139 from sr-murthy/index-repr
Browse files Browse the repository at this point in the history
Add index size and custom `__repr__` methods for index classes
  • Loading branch information
sr-murthy authored Jan 3, 2020
2 parents d202812 + 931d2e5 commit bc6ccc0
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self, *args, **kwargs):
>>> idx = index.Index(properties=p)
>>> idx # doctest: +ELLIPSIS
<rtree.index.Index object at 0x...>
rtree.index.Index(bounds=[1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308], size=0)
Insert an item into the index::
Expand Down Expand Up @@ -284,6 +284,15 @@ def __init__(self, *args, **kwargs):
for item in stream:
self.insert(*item)

def get_size(self):
try:
return self.count(self.bounds)
except core.RTreeError:
return 0

def __repr__(self):
return 'rtree.index.Index(bounds={}, size={})'.format(self.bounds, self.get_size())

def __getstate__(self):
state = self.__dict__.copy()
del state["handle"]
Expand Down Expand Up @@ -1821,7 +1830,7 @@ def __init__(self, *args, **kwargs):
>>> idx = index.RtreeContainer(properties=p)
>>> idx # doctest: +ELLIPSIS
<rtree.index.RtreeContainer object at 0x...>
rtree.index.RtreeContainer(bounds=[1.7976931348623157e+308, 1.7976931348623157e+308, -1.7976931348623157e+308, -1.7976931348623157e+308], size=0)
Insert an item into the index::
Expand All @@ -1847,6 +1856,15 @@ def __init__(self, *args, **kwargs):
self._objects = {}
return super(RtreeContainer, self).__init__(*args, **kwargs)

def get_size(self):
try:
return self.count(self.bounds)
except core.RTreeError:
return 0

def __repr__(self):
return 'rtree.index.RtreeContainer(bounds={}, size={})'.format(self.bounds, self.get_size())

def __contains__(self, obj):
return id(obj) in self._objects

Expand Down

0 comments on commit bc6ccc0

Please sign in to comment.