Repository: kedro-org/kedro
File: kedro/io/data_catalog.py (Python)

Code before:
            >>>         }
            >>>      }
            >>> }
            >>>
            >>> catalog = DataCatalog.from_config(config, credentials)
            >>>
            >>> df = catalog.load("cars")
            >>> catalog.save("boats", df)
        """
        data_sets = {}
        dataset_patterns = {}
        catalog = copy.deepcopy(catalog) or {}
        credentials = copy.deepcopy(credentials) or {}
        save_version = save_version or generate_timestamp()
        load_versions = copy.deepcopy(load_versions) or {}

        missing_keys = load_versions.keys() - catalog.keys()
        if missing_keys:
            raise DatasetNotFoundError(
                f"'load_versions' keys [{', '.join(sorted(missing_keys))}] "
                f"are not found in the catalog."
            )

        layers: dict[str, set[str]] = defaultdict(set)
        for ds_name, ds_config in catalog.items():
            # Assume that any name with "}" in it is a dataset factory to be matched.
            if "}" in ds_name:
                # Add each factory to the dataset_patterns dict.
                dataset_patterns[ds_name] = ds_config
            else:
                ds_layer = ds_config.pop("layer", None)
                if ds_layer is not None:
                    layers[ds_layer].add(ds_name)

                ds_config = _resolve_credentials(ds_config, credentials)
                data_sets[ds_name] = AbstractDataSet.from_config(
                    ds_name, ds_config, load_versions.get(ds_name), save_version
                )
        dataset_layers = layers or None
        return cls(
            data_sets=data_sets,
            layers=dataset_layers,
            dataset_patterns=dataset_patterns,
        )

    def _get_dataset(
        self, data_set_name: str, version: Version = None, suggest: bool = True
    ) -> AbstractDataSet:
        if self.exists_in_catalog_config(data_set_name):
            if data_set_name not in self._data_sets:
                # When a dataset is "used" in the pipeline that's not in the

Review comment (suggestion):
I would've rewritten this for readability as follows:

1. Create a factory method for creating a dataset out of `def create_dataset(cls, ds_name, ds_config):`
2. Convert this single for loop with an if in it into two Python comprehensions for the two different cases

Code after:
            else:
                ds_layer = ds_config.pop("layer", None)
                if ds_layer is not None:
                    layers[ds_layer].add(ds_name)
                data_sets[ds_name] = AbstractDataSet.from_config(
                    ds_name, ds_config, load_versions.get(ds_name), save_version
                )
        dataset_layers = layers or None
        sorted_patterns = cls._sort_patterns(dataset_patterns)
        missing_keys = [
            key
            for key in load_versions.keys()
            if not (cls._match_pattern(sorted_patterns, key) or key in catalog)
        ]
        if missing_keys:
            raise DatasetNotFoundError(
                f"'load_versions' keys [{', '.join(sorted(missing_keys))}] "
                f"are not found in the catalog."
            )

        return cls(
            data_sets=data_sets,
            layers=dataset_layers,
            dataset_patterns=sorted_patterns,
            load_versions=load_versions,
            save_version=save_version,
        )

    @staticmethod
    def _is_pattern(pattern: str):
        """Check if a given string is a pattern. Assume that any name with '{' is a pattern."""
        return "{" in pattern

    @staticmethod
    def _match_pattern(
        data_set_patterns: dict[str, dict[str, Any]], data_set_name: str
    ) -> str | None:
        """Match a dataset name against patterns in a dictionary containing patterns"""
        for pattern, _ in data_set_patterns.items():
            result = parse(pattern, data_set_name)
            if result:
                return pattern
        return None

    @classmethod
    def _sort_patterns(
        cls, data_set_patterns: dict[str, dict[str, Any]]
    ) -> dict[str, dict[str, Any]]:
        """Sort a dictionary of dataset patterns according to parsing rules -
        1. Decreasing specificity (number of characters outside the curly brackets)
        2. Decreasing number of placeholders (number of curly bracket pairs)