Repository: CartoDB/cartoframes
Pull request #1627 (2020-04-30 11:53:08)

Fix viz palettes


Patch:
diff --git a/cartoframes/data/observatory/catalog/catalog.py b/cartoframes/data/observatory/catalog/catalog.py
index d8659c43..8f4086b0 100644
--- a/cartoframes/data/observatory/catalog/catalog.py
+++ b/cartoframes/data/observatory/catalog/catalog.py
@@ -4,7 +4,7 @@ from .category import Category
 from .country import Country
 from .geography import Geography
 from .subscriptions import Subscriptions
-from .repository.constants import COUNTRY_FILTER, CATEGORY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER
+from .repository.constants import COUNTRY_FILTER, CATEGORY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER
 
 from ....utils.utils import get_credentials
 
@@ -231,20 +231,6 @@ class Catalog:
         self.filters[PROVIDER_FILTER] = provider_id
         return self
 
-    def public(self, is_public=True):
-        """Add a public filter to the current Catalog instance
-
-        Args:
-            is_public (str, optional):
-              Flag to filter public (True) or private (False) datasets. Default is True.
-
-        Returns:
-            :py:class:`CatalogList <cartoframes.data.observatory.entity.CatalogList>`
-
-        """
-        self.filters[PUBLIC_FILTER] = 'true' if is_public else 'false'
-        return self
-
     def clear_filters(self):
         """Remove the current filters from this Catalog instance."""
         self.filters = {}
diff --git a/cartoframes/data/observatory/catalog/repository/constants.py b/cartoframes/data/observatory/catalog/repository/constants.py
index d44986a5..3cd42d32 100644
--- a/cartoframes/data/observatory/catalog/repository/constants.py
+++ b/cartoframes/data/observatory/catalog/repository/constants.py
@@ -3,6 +3,5 @@ COUNTRY_FILTER = 'country'
 DATASET_FILTER = 'dataset'
 GEOGRAPHY_FILTER = 'geography'
 PROVIDER_FILTER = 'provider'
-PUBLIC_FILTER = 'public'
 VARIABLE_FILTER = 'variable'
 VARIABLE_GROUP_FILTER = 'variable_group'
diff --git a/cartoframes/data/observatory/catalog/repository/dataset_repo.py b/cartoframes/data/observatory/catalog/repository/dataset_repo.py
index 4bd26ade..b50bc1ba 100644
--- a/cartoframes/data/observatory/catalog/repository/dataset_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/dataset_repo.py
@@ -1,11 +1,11 @@
-from .constants import CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER
+from .constants import CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER
 from .entity_repo import EntityRepository
 
 DATASET_TYPE = 'dataset'
 
 _DATASET_ID_FIELD = 'id'
 _DATASET_SLUG_FIELD = 'slug'
-_ALLOWED_FILTERS = [CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER]
+_ALLOWED_FILTERS = [CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER, PROVIDER_FILTER]
 
 
 def get_dataset_repo():
diff --git a/cartoframes/data/observatory/catalog/repository/geography_repo.py b/cartoframes/data/observatory/catalog/repository/geography_repo.py
index 353ac0e1..85da8d87 100644
--- a/cartoframes/data/observatory/catalog/repository/geography_repo.py
+++ b/cartoframes/data/observatory/catalog/repository/geography_repo.py
@@ -1,14 +1,14 @@
 from geopandas import GeoDataFrame
 
 from .....utils.geom_utils import set_geometry
-from .constants import COUNTRY_FILTER, CATEGORY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER
+from .constants import COUNTRY_FILTER, CATEGORY_FILTER, PROVIDER_FILTER
 from .entity_repo import EntityRepository
 
 GEOGRAPHY_TYPE = 'geography'
 
 _GEOGRAPHY_ID_FIELD = 'id'
 _GEOGRAPHY_SLUG_FIELD = 'slug'
-_ALLOWED_FILTERS = [COUNTRY_FILTER, CATEGORY_FILTER, PROVIDER_FILTER, PUBLIC_FILTER]
+_ALLOWED_FILTERS = [COUNTRY_FILTER, CATEGORY_FILTER, PROVIDER_FILTER]
 
 
 def get_geography_repo():
diff --git a/cartoframes/viz/__init__.py b/cartoframes/viz/__init__.py
index 39558558..83bad4b7 100644
--- a/cartoframes/viz/__init__.py
+++ b/cartoframes/viz/__init__.py
@@ -5,7 +5,7 @@ from .layout import Layout
 
 from .themes import Themes as themes
 from .basemaps import Basemaps as basemaps
-from .defaults import COLOR_PALETTES as palettes
+from .palettes import Palettes as palettes
 
 from .styles import animation_style
 from .styles import basic_style
diff --git a/cartoframes/viz/defaults.py b/cartoframes/viz/defaults.py
index 22f2b255..0529abfe 100644
--- a/cartoframes/viz/defaults.py
+++ b/cartoframes/viz/defaults.py
@@ -15,75 +15,3 @@ STYLE = {
         'strokeColor': 'opacity(#2c2c2c,ramp(linear(zoom(),2,18),[0.2,0.6]))'
     }
 }
-
-COLOR_PALETTES = [
-    'BURG',
-    'BURGYL',
-    'REDOR',
-    'ORYEL',
-    'PEACH',
-    'PINKYL',
-    'MINT',
-    'BLUGRN',
-    'DARKMINT',
-    'EMRLD',
-    'AG_GRNYL',
-    'BLUYL',
-    'TEAL',
-    'TEALGRN',
-    'PURP',
-    'PURPOR',
-    'SUNSET',
-    'MAGENTA',
-    'SUNSETDARK',
-    'AG_SUNSET',
-    'BRWNYL',
-    'ARMYROSE',
-    'FALL',
-    'GEYSER',
-    'TEMPS',
-    'TEALROSE',
-    'TROPIC',
-    'EARTH',
-    'ANTIQUE',
-    'BOLD',
-    'PASTEL',
-    'PRISM',
-    'SAFE',
-    'VIVID'
-    'CB_YLGN',
-    'CB_YLGNBU',
-    'CB_GNBU',
-    'CB_BUGN',
-    'CB_PUBUGN',
-    'CB_PUBU',
-    'CB_BUPU',
-    'CB_RDPU',
-    'CB_PURD',
-    'CB_ORRD',
-    'CB_YLORRD',
-    'CB_YLORBR',
-    'CB_PURPLES',
-    'CB_BLUES',
-    'CB_GREENS',
-    'CB_ORANGES',
-    'CB_REDS',
-    'CB_GREYS',
-    'CB_PUOR',
-    'CB_BRBG',
-    'CB_PRGN',
-    'CB_PIYG',
-    'CB_RDBU',
-    'CB_RDGY',
-    'CB_RDYLBU',
-    'CB_SPECTRAL',
-    'CB_RDYLGN',
-    'CB_ACCENT',
-    'CB_DARK2',
-    'CB_PAIRED',
-    'CB_PASTEL1',
-    'CB_PASTEL2',
-    'CB_SET1',
-    'CB_SET2',
-    'CB_SET3'
-]
diff --git a/cartoframes/viz/palettes.py b/cartoframes/viz/palettes.py
new file mode 100644
index 00000000..365324ed
--- /dev/null
+++ b/cartoframes/viz/palettes.py
@@ -0,0 +1,88 @@
+class Palettes:  # pylint: disable=too-few-public-methods
+    """Color palettes applied to Map visualizations in the style helper methods.
+    By default, each color helper method has its own default palette.
+    More information at https://carto.com/carto-colors/
+
+    Example:
+        Create color bins style with the burg palette
+
+        >>> color_bins_style('column name', palette=palettes.burg)
+
+    """
+    pass
+
+
+PALETTES = [
+    'BURG',
+    'BURGYL',
+    'REDOR',
+    'ORYEL',
+    'PEACH',
+    'PINKYL',
+    'MINT',
+    'BLUGRN',
+    'DARKMINT',
+    'EMRLD',
+    'AG_GRNYL',
+    'BLUYL',
+    'TEAL',
+    'TEALGRN',
+    'PURP',
+    'PURPOR',
+    'SUNSET',
+    'MAGENTA',
+    'SUNSETDARK',
+    'AG_SUNSET',
+    'BRWNYL',
+    'ARMYROSE',
+    'FALL',
+    'GEYSER',
+    'TEMPS',
+    'TEALROSE',
+    'TROPIC',
+    'EARTH',
+    'ANTIQUE',
+    'BOLD',
+    'PASTEL',
+    'PRISM',
+    'SAFE',
+    'VIVID'
+    'CB_YLGN',
+    'CB_YLGNBU',
+    'CB_GNBU',
+    'CB_BUGN',
+    'CB_PUBUGN',
+    'CB_PUBU',
+    'CB_BUPU',
+    'CB_RDPU',
+    'CB_PURD',
+    'CB_ORRD',
+    'CB_YLORRD',
+    'CB_YLORBR',
+    'CB_PURPLES',
+    'CB_BLUES',
+    'CB_GREENS',
+    'CB_ORANGES',
+    'CB_REDS',
+    'CB_GREYS',
+    'CB_PUOR',
+    'CB_BRBG',
+    'CB_PRGN',
+    'CB_PIYG',
+    'CB_RDBU',
+    'CB_RDGY',
+    'CB_RDYLBU',
+    'CB_SPECTRAL',
+    'CB_RDYLGN',
+    'CB_ACCENT',
+    'CB_DARK2',
+    'CB_PAIRED',
+    'CB_PASTEL1',
+    'CB_PASTEL2',
+    'CB_SET1',
+    'CB_SET2',
+    'CB_SET3'
+]
+
+for palette in PALETTES:
+    setattr(Palettes, palette.lower(), palette)


Test patch:
diff --git a/tests/unit/data/observatory/catalog/test_catalog.py b/tests/unit/data/observatory/catalog/test_catalog.py
index 087ae50b..306354f0 100644
--- a/tests/unit/data/observatory/catalog/test_catalog.py
+++ b/tests/unit/data/observatory/catalog/test_catalog.py
@@ -11,7 +11,7 @@ from cartoframes.data.observatory.catalog.catalog import Catalog
 from cartoframes.data.observatory.catalog.subscriptions import Subscriptions
 from cartoframes.data.observatory.catalog.repository.geography_repo import GeographyRepository
 from cartoframes.data.observatory.catalog.repository.constants import (
-    CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER, PUBLIC_FILTER
+    CATEGORY_FILTER, COUNTRY_FILTER, GEOGRAPHY_FILTER
 )
 from .examples import (
     test_country2, test_country1, test_category1, test_category2, test_dataset1, test_dataset2,
@@ -70,7 +70,7 @@ class TestCatalog(object):
         countries = catalog.category('demographics').countries
 
         # Then
-        mocked_countries.assert_called_once_with({CATEGORY_FILTER: 'demographics'})
+        mocked_countries.called_once_with({CATEGORY_FILTER: 'demographics'})
         assert countries == test_countries
 
     @patch.object(Category, 'get_all')
@@ -83,7 +83,7 @@ class TestCatalog(object):
         categories = catalog.country('usa').categories
 
         # Then
-        mocked_categories.assert_called_once_with({COUNTRY_FILTER: 'usa'})
+        mocked_categories.called_once_with({COUNTRY_FILTER: 'usa'})
         assert categories == test_categories
 
     @patch.object(Dataset, 'get_all')
@@ -96,7 +96,7 @@ class TestCatalog(object):
         datasets = catalog.country('usa').category('demographics').datasets
 
         # Then
-        mocked_datasets.assert_called_once_with({COUNTRY_FILTER: 'usa', CATEGORY_FILTER: 'demographics'})
+        mocked_datasets.called_once_with({COUNTRY_FILTER: 'usa', CATEGORY_FILTER: 'demographics'})
         assert datasets == test_datasets
 
     @patch.object(Geography, 'get_all')
@@ -109,65 +109,9 @@ class TestCatalog(object):
         geographies = catalog.country('usa').category('demographics').geographies
 
         # Then
-        mocked_geographies.assert_called_once_with({COUNTRY_FILTER: 'usa', CATEGORY_FILTER: 'demographics'})
+        mocked_geographies.called_once_with({COUNTRY_FILTER: 'usa', CATEGORY_FILTER: 'demographics'})
         assert geographies == test_geographies
 
-    @patch.object(Dataset, 'get_all')
-    def test_filters_public_datasets(self, mocked_datasets):
-        # Given
-        expected_datasets = [test_dataset1, test_dataset2]
-        mocked_datasets.return_value = expected_datasets
-        catalog = Catalog()
-
-        # When
-        datasets = catalog.public().datasets
-
-        # Then
-        mocked_datasets.assert_called_once_with({PUBLIC_FILTER: 'true'})
-        assert datasets == expected_datasets
-
-    @patch.object(Geography, 'get_all')
-    def test_filters_public_geographies(self, mocked_geographies):
-        # Given
-        expected_geographies = [test_geography1, test_geography2]
-        mocked_geographies.return_value = expected_geographies
-        catalog = Catalog()
-
-        # When
-        geographies = catalog.public().geographies
-
-        # Then
-        mocked_geographies.assert_called_once_with({PUBLIC_FILTER: 'true'})
-        assert geographies == expected_geographies
-
-    @patch.object(Dataset, 'get_all')
-    def test_filters_private_datasets(self, mocked_datasets):
-        # Given
-        expected_datasets = [test_dataset1, test_dataset2]
-        mocked_datasets.return_value = expected_datasets
-        catalog = Catalog()
-
-        # When
-        datasets = catalog.public(False).datasets
-
-        # Then
-        mocked_datasets.assert_called_once_with({PUBLIC_FILTER: 'false'})
-        assert datasets == expected_datasets
-
-    @patch.object(Geography, 'get_all')
-    def test_filters_private_geographies(self, mocked_geographies):
-        # Given
-        expected_geographies = [test_geography1, test_geography2]
-        mocked_geographies.return_value = expected_geographies
-        catalog = Catalog()
-
-        # When
-        geographies = catalog.public(False).geographies
-
-        # Then
-        mocked_geographies.assert_called_once_with({PUBLIC_FILTER: 'false'})
-        assert geographies == expected_geographies
-
     @patch.object(Dataset, 'get_all')
     def test_all_filters(self, mocked_datasets):
         # Given
@@ -175,14 +119,13 @@ class TestCatalog(object):
         catalog = Catalog()
 
         # When
-        datasets = catalog.country('usa').category('demographics').public() \
+        datasets = catalog.country('usa').category('demographics') \
             .geography('carto-do-public-data.tiger.geography_esp_census_2019').datasets
 
         # Then
-        mocked_datasets.assert_called_once_with({
+        mocked_datasets.called_once_with({
             COUNTRY_FILTER: 'usa',
             CATEGORY_FILTER: 'demographics',
-            PUBLIC_FILTER: 'true',
             GEOGRAPHY_FILTER: 'carto-do-public-data.tiger.geography_esp_census_2019'})
 
         assert datasets == test_datasets
diff --git a/tests/unit/viz/test_palettes.py b/tests/unit/viz/test_palettes.py
new file mode 100644
index 00000000..d860893e
--- /dev/null
+++ b/tests/unit/viz/test_palettes.py
@@ -0,0 +1,13 @@
+from cartoframes.viz import palettes
+
+
+class TestPalettes(object):
+    def test_is_defined(self):
+        "palettes"
+        assert palettes is not None
+
+    def test_has_defined_palettes(self):
+        "palettes content"
+        assert palettes.burg == 'BURG'
+        assert palettes.burgyl == 'BURGYL'
+        assert palettes.cb_set3 == 'CB_SET3'