Skip to content

Commit

Permalink
Merge pull request #2 from dymmond/feat/settings_update
Browse files Browse the repository at this point in the history
[Feature] - Add upper option for dict and tuple
  • Loading branch information
tarsil authored Jan 3, 2024
2 parents abd3300 + 706c00e commit 23e8887
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 10 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</p>

<p align="center">
<a href="https://github.com/dymmond/dymmond-settings/workflows/Test%20Suite/badge.svg?event=push&branch=main" target="_blank">
<img src="https://github.com/dymmond/dymmond-settings/workflows/Test%20Suite/badge.svg?event=push&branch=main" alt="Test Suite">
<a href="https://github.com/dymmond/dymmond-settings/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" target="_blank">
<img src="https://github.com/dymmond/dymmond-settings/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" alt="Test Suite">
</a>

<a href="https://pypi.org/project/dymmond-settings" target="_blank">
Expand All @@ -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**.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
48 changes: 46 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</p>

<p align="center">
<a href="https://github.com/dymmond/dymmond-settings/workflows/Test%20Suite/badge.svg?event=push&branch=main" target="_blank">
<img src="https://github.com/dymmond/dymmond-settings/workflows/Test%20Suite/badge.svg?event=push&branch=main" alt="Test Suite">
<a href="https://github.com/dymmond/dymmond-settings/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" target="_blank">
<img src="https://github.com/dymmond/dymmond-settings/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" alt="Test Suite">
</a>

<a href="https://pypi.org/project/dymmond-settings" target="_blank">
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions dymmond_settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions tests/test_upper.py
Original file line number Diff line number Diff line change
@@ -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),
]

0 comments on commit 23e8887

Please sign in to comment.