Skip to content

Commit

Permalink
Add __len__ method to Index (#207)
Browse files Browse the repository at this point in the history
* Add __len__ method to Index

* Deprecate get_size()
  • Loading branch information
adamjstewart authored Feb 5, 2022
1 parent 390a060 commit 6f638cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import os.path
import pprint
import warnings

from . import core

Expand Down Expand Up @@ -289,6 +290,18 @@ def __init__(self, *args, **kwargs):
self.insert(*item)

def get_size(self):
warnings.warn(
"index.get_size() is deprecated, use len(index) instead",
DeprecationWarning,
)
return len(self)

def __len__(self):
"""The number of entries in the index.
:return: number of entries
:rtype: int
"""
try:
return self.count(self.bounds)
except core.RTreeError:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ def test_libsidx_version(self):
self.assertTrue(index.minor_version >= 7)


class IndexCount(unittest.TestCase):

def setUp(self):
self.boxes15 = np.genfromtxt('boxes_15x15.data')
self.idx = index.Index()
for i, coords in enumerate(self.boxes15):
self.idx.add(i, coords)

def test_len(self):
self.assertEqual(len(self.idx), len(self.boxes15))

def test_get_size(self):
with pytest.deprecated_call():
self.assertEqual(self.idx.get_size(), len(self.boxes15))


class IndexBounds(unittest.TestCase):

def test_invalid_specifications(self):
Expand Down

0 comments on commit 6f638cc

Please sign in to comment.