Skip to content

Commit

Permalink
correct list type hints to List
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenzie-grimes-noaa committed Jun 22, 2023
1 parent 23053a0 commit 271d8aa
Showing 1 changed file with 3 additions and 78 deletions.
81 changes: 3 additions & 78 deletions python/idsse_common/idsse/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import json
import logging
from inspect import signature
from typing import Self, Union
from typing import Self, Union, List

logger = logging.getLogger(__name__)

Expand All @@ -22,7 +22,7 @@ class Config:
"""Configuration data class"""

def __init__(self,
config: Union[dict, list[dict], str],
config: Union[dict, List[dict], str],
keys: Union[list, str] = None,
recursive: bool = False,
ignore_missing: bool = False) -> None:
Expand Down Expand Up @@ -116,7 +116,7 @@ def _from_config_dict(self, config_dict: dict, keys: str) -> Self:
# update the instance dictionary to hold all configuration attributes
self.__dict__.update(config_dict)

def _from_config_dicts(self, config_dicts: list[dict], keys: str) -> Self:
def _from_config_dicts(self, config_dicts: List[dict], keys: str) -> Self:
self._from_config_dict(config_dicts[0], keys)
for config_dict in config_dicts[1:]:
# if inherited class takes only one argument
Expand All @@ -125,78 +125,3 @@ def _from_config_dicts(self, config_dicts: list[dict], keys: str) -> Self:
else:
self._next = type(self)(config_dict, keys)
self._next._previous = self # pylint: disable=protected-access


def _example(): # pragma: no cover
class NameAsKeyConfig(Config):
"""Testing config class the uses class name as key"""
def __init__(self, config: Union[dict, str]) -> None:
"""To create a config that uses it's class name when look for nested config,
pass None to the super.__init()"""
self.idiom = None
self.metaphor = None
super().__init__(config, None)

class WithoutKeyConfig(Config):
"""Testing config class the uses no key"""
def __init__(self, config: Union[dict, str]) -> None:
"""To create a config that does NOT look for config nested under a key,
pass an empty string to the super.__init()"""
self.idiom = None
self.metaphor = None
super().__init__(config, '')

class RequiresKeyConfig(Config):
"""Testing config class that requires key to be provided"""
def __init__(self, config: Union[dict, str], key: Union[list, str]) -> None:
"""To create a config that requires a user provided name when look for nested config,
pass a key as string or list of string to the super.__init()"""
self.idiom = None
self.metaphor = None
super().__init__(config, key)

def get_config_dict(key: Union[list, str]) -> dict:
idioms = ['Out of hand',
'See eye to eye',
'Under the weather',
'Cut the mustard']
metaphors = ['blanket of stars',
'weighing on my mind',
'were a glaring light',
'floated down the river']
if key is None:
return {'idiom': random.choice(idioms),
'metaphor': random.choice(metaphors)}
if isinstance(key, str):
key = [key]
config_dict = {'idiom': random.choice(idioms),
'metaphor': random.choice(metaphors)}
for k in reversed(key):
config_dict = {k: config_dict}
return config_dict

# example of config that uses class name to identify relevant block of data
config_dict = get_config_dict('NameAsKeyConfig')
logging.info(config_dict)
config = NameAsKeyConfig(config_dict)
logging.info('Idiom:', config.idiom)
logging.info('Metaphor:', config.metaphor)

# example of config with block of data at top level
config_dict = get_config_dict(None)
logging.info(config_dict)
config = WithoutKeyConfig(config_dict)
logging.info('Idiom:', config.idiom)
logging.info('Metaphor:', config.metaphor)

# example of config with relevant block of data nested
config_dict = get_config_dict('NestKey')
logging.info(config_dict)
config = RequiresKeyConfig(config_dict, 'NestKey')
logging.info('Idiom:', config.idiom)
logging.info('Metaphor:', config.metaphor)


if __name__ == '__main__': # pragma: no cover
import random
_example()

0 comments on commit 271d8aa

Please sign in to comment.