Skip to content

Commit

Permalink
Add support for choosing enum variants at random (#42)
Browse files Browse the repository at this point in the history
* Add at_random method to base enum

* Update changelog
  • Loading branch information
Jonxslays committed Nov 14, 2023
1 parent 73b3b3f commit 4fb34e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Unreleased (Nov 2023)

## Additions

- Add `at_random` method to `BaseEnum` for picking an enum value at random.

## Changes

- Update examples in `GroupService` that work with `GroupMemberFragment`.

---

# v0.8.1 (Nov 2023)

## Additions
Expand Down
9 changes: 9 additions & 0 deletions wom/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from __future__ import annotations

import random
import typing as t
from enum import Enum

Expand Down Expand Up @@ -79,6 +80,14 @@ def from_str_maybe(cls: t.Type[T], value: str) -> t.Optional[T]:
except ValueError:
return None

@classmethod
def at_random(cls: t.Type[T]) -> T:
"""Generates a random variant of this enum.
Returns:
The randomly generated enum."""
return random.choice(tuple(cls))


class Metric(BaseEnum):
"""Represents a metric, this enum has no attributes itself.
Expand Down

0 comments on commit 4fb34e5

Please sign in to comment.