Repository: intake/intake

Issue:
Implement __len__ on Catalog class?
Catalog types almost meet the requirements for a [Collection](https://docs.python.org/3.8/library/collections.abc.html). They implement `__iter__` and `__contains__`. But not `__len__`. If we implement `__len__`, then a `Catalog`  will be a valid `Collection`. 

My specific application is for our nascent pangeo-datastore flask app, which traverses intake catalogs. I'd like to be able to do `{{ cat|length }}` from a jinja template. This requires being able to do `len(cat)` in python.

Should be easy to implement.

Discussion:

Patch:
diff --git a/intake/catalog/base.py b/intake/catalog/base.py
index a482bd1..34c2e7c 100644
--- a/intake/catalog/base.py
+++ b/intake/catalog/base.py
@@ -306,6 +306,9 @@ class Catalog(DataSource):
         """Return an iterator over catalog entries."""
         return iter(self._get_entries())
 
+    def __len__(self):
+        return len(self._get_entries())
+
     def __contains__(self, key):
         # Avoid iterating through all entries.
         return key in self._get_entries()  # triggers reload_on_change


Test patch:
diff --git a/intake/catalog/tests/test_local.py b/intake/catalog/tests/test_local.py
index 2f46874..bcc1ea4 100644
--- a/intake/catalog/tests/test_local.py
+++ b/intake/catalog/tests/test_local.py
@@ -31,6 +31,7 @@ def test_local_catalog(catalog1):
     assert_items_equal(list(catalog1),
                        ['use_example1', 'nested', 'entry1', 'entry1_part',
                         'remote_env', 'local_env', 'text', 'arr', 'datetime'])
+    assert len(catalog1) == 9
     assert catalog1['entry1'].describe() == {
         'name': 'entry1',
         'container': 'dataframe',
@@ -708,4 +709,3 @@ def test_cat_add(tmpdir):
     # was added to the file
     cat = open_catalog(fn)
     assert list(cat) == ['cat']
-