diff --git a/README.md b/README.md index 45b668f..5348c4a 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@

- - Test Suite + + Test Suite @@ -20,6 +20,8 @@ --- +**Documentation**: [https://settings.dymmond.com](https://settings.dymmond.com) 📚 + **Source Code**: [https://github.com/dymmond/dymmond-settings](https://github.com/dymmond/dymmond-settings) **The official supported version is always the latest released**. @@ -106,6 +108,11 @@ This simply converts your settings module into a python dictionary. This provide of functionality of `asdict` from the Python `dataclasses` module when `exclude_none` is set to `False` (default). +**Parameters**: + +- **exclude_none** - Excludes the values containing `None`. +- **upper** - Boolean flag indicating if the keys should be in upper case. + ```python from dataclasses import dataclass, field @@ -138,6 +145,23 @@ my_settings = AppSettings() my_settings.dict(exclude_none=True) ``` +Or if you want the keys to be upper case. + +```python +from dataclasses import dataclass, field + +from dymmond_settings import Settings + + +@dataclass +class AppSettings(Settings): + ... + + +my_settings = AppSettings() +my_settings.dict(upper=True) +``` + #### tuple() This simply converts your settings module into a python tuple but is slightly different from the @@ -149,6 +173,11 @@ whereas `dymmond_settings` tuple function provides **a list of tuples key/pair v As per [dict](#dict) functionality, the `tuple()` also provides a `exclude_none` in case you want a list attributes with the values set. +**Parameters**: + +- **exclude_none** - Excludes the values containing `None`. +- **upper** - Boolean flag indicating if the keys should be in upper case. + ```python from dataclasses import dataclass, field @@ -181,6 +210,23 @@ my_settings = AppSettings() my_settings.tuple(exclude_none=True) ``` +Or if you want the tuple to contain the *keys* in upper case. + +```python +from dataclasses import dataclass, field + +from dymmond_settings import Settings + + +@dataclass +class AppSettings(Settings): + ... + + +my_settings = AppSettings() +my_settings.tuple(upper=True) +``` + ## How to use it There are many ways you can use the settings and all of them valid. Here we show just a few examples diff --git a/docs/index.md b/docs/index.md index 50b0890..5348c4a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -5,8 +5,8 @@

- - Test Suite + + Test Suite @@ -108,6 +108,11 @@ This simply converts your settings module into a python dictionary. This provide of functionality of `asdict` from the Python `dataclasses` module when `exclude_none` is set to `False` (default). +**Parameters**: + +- **exclude_none** - Excludes the values containing `None`. +- **upper** - Boolean flag indicating if the keys should be in upper case. + ```python from dataclasses import dataclass, field @@ -140,6 +145,23 @@ my_settings = AppSettings() my_settings.dict(exclude_none=True) ``` +Or if you want the keys to be upper case. + +```python +from dataclasses import dataclass, field + +from dymmond_settings import Settings + + +@dataclass +class AppSettings(Settings): + ... + + +my_settings = AppSettings() +my_settings.dict(upper=True) +``` + #### tuple() This simply converts your settings module into a python tuple but is slightly different from the @@ -151,6 +173,11 @@ whereas `dymmond_settings` tuple function provides **a list of tuples key/pair v As per [dict](#dict) functionality, the `tuple()` also provides a `exclude_none` in case you want a list attributes with the values set. +**Parameters**: + +- **exclude_none** - Excludes the values containing `None`. +- **upper** - Boolean flag indicating if the keys should be in upper case. + ```python from dataclasses import dataclass, field @@ -183,6 +210,23 @@ my_settings = AppSettings() my_settings.tuple(exclude_none=True) ``` +Or if you want the tuple to contain the *keys* in upper case. + +```python +from dataclasses import dataclass, field + +from dymmond_settings import Settings + + +@dataclass +class AppSettings(Settings): + ... + + +my_settings = AppSettings() +my_settings.tuple(upper=True) +``` + ## How to use it There are many ways you can use the settings and all of them valid. Here we show just a few examples diff --git a/dymmond_settings/base.py b/dymmond_settings/base.py index 999ef34..523c55a 100644 --- a/dymmond_settings/base.py +++ b/dymmond_settings/base.py @@ -15,21 +15,35 @@ class BaseSettings: Base of all the settings for any system. """ - def dict(self, exclude_none: bool = False) -> Dict[str, Any]: + def dict(self, exclude_none: bool = False, upper: bool = False) -> Dict[str, Any]: """ Dumps all the settings into a python dictionary. """ + original = asdict(self) + if not exclude_none: - return asdict(self) - return {k: v for k, v in self.__dict__.items() if v is not None} + if not upper: + return original + return {k.upper(): v for k, v in original.items()} + + if not upper: + return {k: v for k, v in original.items() if v is not None} + return {k.upper(): v for k, v in original.items() if v is not None} - def tuple(self, exclude_none: bool = False) -> List[Tuple[str, Any]]: + def tuple(self, exclude_none: bool = False, upper: bool = False) -> List[Tuple[str, Any]]: """ Dumps all the settings into a tuple. """ + original = asdict(self) + if not exclude_none: - return list(self.__dict__.items()) - return [(k, v) for k, v in self.__dict__.items() if v is not None] + if not upper: + return list(original.items()) + return list({k.upper(): v for k, v in original.items()}.items()) + + if not upper: + return [(k, v) for k, v in original.items() if v is not None] + return [(k.upper(), v) for k, v in original.items() if v is not None] @dataclass diff --git a/tests/test_upper.py b/tests/test_upper.py new file mode 100644 index 0000000..92c03ac --- /dev/null +++ b/tests/test_upper.py @@ -0,0 +1,76 @@ +from dataclasses import dataclass, field +from typing import Union + +from dymmond_settings import Settings, __version__ +from dymmond_settings.enums import EnvironmentType + + +@dataclass +class CustomSettings(Settings): + """ + A custom settings that allows the __repr__ + """ + + my_sett: str = field(default="works") + my_custom: Union[str, None] = None + + def __repr__(self) -> str: + return f"{self.__class__.__name__}()" + + +def test_upper_dict_exclude_none(): + settings = CustomSettings() + + dict_settings = settings.dict(exclude_none=True, upper=True) + + assert "my_custom" not in dict_settings + + assert dict_settings == { + "DEBUG": False, + "ENVIRONMENT": EnvironmentType.PRODUCTION, + "VERSION": __version__, + "MY_SETT": "works", + } + + +def test_upper_dict(): + settings = CustomSettings() + + dict_settings = settings.dict(upper=True) + + assert "my_custom" not in dict_settings + + assert dict_settings == { + "DEBUG": False, + "ENVIRONMENT": EnvironmentType.PRODUCTION, + "VERSION": __version__, + "MY_SETT": "works", + "MY_CUSTOM": None, + } + + +def test_upper_tuple_exclude_none(): + settings = CustomSettings() + + tuple_settings = settings.tuple(exclude_none=True, upper=True) + + assert tuple_settings == [ + ("DEBUG", False), + ("ENVIRONMENT", EnvironmentType.PRODUCTION), + ("VERSION", __version__), + ("MY_SETT", "works"), + ] + + +def test_upper_tuple(): + settings = CustomSettings() + + tuple_settings = settings.tuple(upper=True) + + assert tuple_settings == [ + ("DEBUG", False), + ("ENVIRONMENT", EnvironmentType.PRODUCTION), + ("VERSION", __version__), + ("MY_SETT", "works"), + ("MY_CUSTOM", None), + ]