Skip to content

Commit

Permalink
Move exclude_keys() to dlt.common.utils (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash authored Oct 22, 2024
1 parent 4926d1d commit 9fdd1ee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
14 changes: 14 additions & 0 deletions dlt/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,20 @@ def without_none(d: Mapping[TKey, Optional[TValue]]) -> Mapping[TKey, TValue]:
return {k: v for k, v in d.items() if v is not None}


def exclude_keys(mapping: Mapping[str, Any], keys: Iterable[str]) -> Dict[str, Any]:
"""Create a new dictionary from the input mapping, excluding specified keys.
Args:
mapping (Mapping[str, Any]): The input mapping from which keys will be excluded.
keys (Iterable[str]): The keys to exclude.
Returns:
Dict[str, Any]: A new dictionary containing all key-value pairs from the original
mapping except those with keys specified in `keys`.
"""
return {k: v for k, v in mapping.items() if k not in keys}


def get_full_class_name(obj: Any) -> str:
cls = obj.__class__
module = cls.__module__
Expand Down
3 changes: 2 additions & 1 deletion dlt/sources/rest_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dlt.common import jsonpath
from dlt.common.schema.schema import Schema
from dlt.common.schema.typing import TSchemaContract
from dlt.common.utils import exclude_keys

from dlt.extract import Incremental, DltResource, DltSource, decorators

Expand Down Expand Up @@ -44,7 +45,7 @@
setup_incremental_object,
create_response_hooks,
)
from .utils import check_connection, exclude_keys # noqa: F401
from .utils import check_connection # noqa: F401

PARAM_TYPES: List[ParamBindType] = ["incremental", "resolve"]
MIN_SECRET_MASKING_LENGTH = 3
Expand Down
3 changes: 1 addition & 2 deletions dlt/sources/rest_api/config_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from dlt.common import logger
from dlt.common.configuration import resolve_configuration
from dlt.common.schema.utils import merge_columns
from dlt.common.utils import update_dict_nested
from dlt.common.utils import update_dict_nested, exclude_keys
from dlt.common import jsonpath

from dlt.extract.incremental import Incremental
Expand Down Expand Up @@ -65,7 +65,6 @@
Endpoint,
EndpointResource,
)
from .utils import exclude_keys


PAGINATOR_MAP: Dict[str, Type[BasePaginator]] = {
Expand Down
15 changes: 1 addition & 14 deletions dlt/sources/rest_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Dict, Any, Mapping, Iterable
from typing import Tuple

from dlt.common import logger
from dlt.extract.source import DltSource
Expand All @@ -10,19 +10,6 @@ def join_url(base_url: str, path: str) -> str:
return base_url + path.lstrip("/")


def exclude_keys(d: Mapping[str, Any], keys: Iterable[str]) -> Dict[str, Any]:
"""Removes specified keys from a dictionary and returns a new dictionary.
Args:
d (Mapping[str, Any]): The dictionary to remove keys from.
keys (Iterable[str]): The keys to remove.
Returns:
Dict[str, Any]: A new dictionary with the specified keys removed.
"""
return {k: v for k, v in d.items() if k not in keys}


def check_connection(
source: DltSource,
*resource_names: str,
Expand Down

0 comments on commit 9fdd1ee

Please sign in to comment.