Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FeatureFlag: Add __str__ and __repr__ #171

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Feature flags: https://github.com/boxine/bx_django_utils/blob/master/bx_django_u

#### bx_django_utils.feature_flags.data_classes

* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L168) - A feature flag that persistent the state into django cache/database.
* [`FeatureFlag()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/feature_flags/data_classes.py#L18-L176) - A feature flag that persistent the state into django cache/database.

#### bx_django_utils.feature_flags.test_utils

Expand Down
8 changes: 8 additions & 0 deletions bx_django_utils/feature_flags/data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(

validate_cache_key(cache_key)
validate_cache_key(cache_key_prefix)
self.name = cache_key
self.cache_key = f'{cache_key_prefix}-{cache_key}'
validate_cache_key(self.cache_key) # Double check ;)

Expand Down Expand Up @@ -166,3 +167,10 @@ def get_by_cache_key(cls, cache_key) -> "FeatureFlag":

def __bool__(self):
return self.is_enabled

def __str__(self):
return f'<FeatureFlag {self.name}>'

def __repr__(self):
initial = self.initial_state == State.ENABLED
return f'FeatureFlag(cache_key={self.name!r}, human_name={self.human_name!r}, initial_enabled={initial!r})'
9 changes: 9 additions & 0 deletions bx_django_utils/feature_flags/tests/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ def test_reset(self):

# Reset again – should not error
flag.reset()

def test_str(self):
flag = FeatureFlag(
cache_key='be-wild',
human_name='Be wild',
initial_enabled=False,
)
self.assertEqual(str(flag), '<FeatureFlag be-wild>')
self.assertEqual(repr(flag), "FeatureFlag(cache_key='be-wild', human_name='Be wild', initial_enabled=False)")
Loading