Skip to content
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
25 changes: 0 additions & 25 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,3 @@
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ from typing import Annotated

from data_enum import DataEnum, UNIQUE, UniqueTogether


class Currency(DataEnum):
__members__ = ("USD", "EUR", "GBP")

symbol: str
name: str
code: Annotated[str, UNIQUE]


Currency.USD = Currency(symbol="$", name="US Dollar", code="USD")
Currency.EUR = Currency(symbol="€", name="Euro", code="EUR")
Currency.GBP = Currency(symbol="£", name="Pound Sterling", code="GBP")
Expand All @@ -42,24 +44,24 @@ Currency.GBP = Currency(symbol="£", name="Pound Sterling", code="GBP")
### Access data on members

```py
Currency.USD.name # 'US Dollar'
Currency.EUR.symbol # '€'
str(Currency.USD) # 'USD'
repr(Currency.USD) # 'Currency.USD'
Currency.USD.name # 'US Dollar'
Currency.EUR.symbol # '€'
str(Currency.USD) # 'USD'
repr(Currency.USD) # 'Currency.USD'
```

### Look up members by name

```py
Currency.get("USD") # Currency.USD
Currency.get("MISSING", default=None) # None
Currency.get("USD") # Currency.USD
Currency.get("MISSING", default=None) # None
Currency.get("MISSING", default=Currency.USD) # Currency.USD
```

### Look up by unique attribute

```py
Currency.get(code="EUR") # Currency.EUR
Currency.get(code="EUR") # Currency.EUR
Currency.get(code="MISSING", default=Currency.USD) # Currency.USD
```

Expand All @@ -77,7 +79,7 @@ Currency.USD == Currency.EUR # False
Currency.USD == Currency.USD # True

{Currency.USD, Currency.EUR} # works in sets
{Currency.USD: "dollar"} # works as dict keys
{Currency.USD: "dollar"} # works as dict keys
```

### Default values
Expand All @@ -93,7 +95,8 @@ class Currency(DataEnum):
code: Annotated[str, UNIQUE]
active: bool = True

Currency.USD = Currency(symbol="$", name="US Dollar", code="USD") # active=True

Currency.USD = Currency(symbol="$", name="US Dollar", code="USD") # active=True
Currency.EUR = Currency(symbol="€", name="Euro", code="EUR", active=False) # active=False
```

Expand All @@ -115,6 +118,7 @@ class State(DataEnum):
code: Annotated[str, UniqueTogether("location")]
name: str


State.CA_US = State(country="US", code="CA", name="California")
State.TX_US = State(country="US", code="TX", name="Texas")
State.ON_CA = State(country="CA", code="ON", name="Ontario")
Expand Down Expand Up @@ -146,7 +150,7 @@ DataEnum uses PEP 681 (`@dataclass_transform`) so type checkers (mypy, pyright,

```py
Currency(symbol="$", name="US Dollar", code="USD") # type checker knows these kwargs
Currency(symbol="$") # type checker flags missing args
Currency(symbol="$") # type checker flags missing args
```

### No-data enums
Expand All @@ -155,6 +159,7 @@ Currency(symbol="$") # type checker flags missi
class Direction(DataEnum):
__members__ = ("NORTH", "SOUTH", "EAST", "WEST")


Direction.NORTH = Direction()
Direction.SOUTH = Direction()
Direction.EAST = Direction()
Expand Down Expand Up @@ -192,6 +197,7 @@ class Color(DataEnum):
__members__ = ("RED", "BLUE")
name: str


Color.RED = Color(name="Red")
Color.get("RED") # raises ConfigurationError (BLUE not yet assigned)
```
Expand Down
1 change: 1 addition & 0 deletions data_enum/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2021 Chase Finch
"""An alternative to the built-in Python `enum` implementation."""

from .data_enum import (
Expand Down
1 change: 1 addition & 0 deletions data_enum/_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2021 Chase Finch
"""Back-compatibility shims for supported Python versions.

`typing.override` only exists on Python 3.12+, but this package supports
Expand Down
1 change: 1 addition & 0 deletions data_enum/data_enum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2021 Chase Finch
"""An alternative to the built-in Python `enum` implementation."""

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion requirements/develop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ docformatter==1.7.8

wemake-python-styleguide==1.7.0

ruff==0.15.22
ruff==0.16.0

ty==0.0.61
3 changes: 3 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ line-length = 99
target-version = "py313"
output-format = "grouped"

# The axioms submodule isn't governed by this repo's style
extend-exclude = ["axioms"]

[lint]
allowed-confusables = ["\u2018", "\u2019", "\u00d7", "\u2013"]
select = ["ALL"]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2021 Chase Finch
"""Define metadata for DataEnum."""

from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Copyright (C) 2021 Chase Finch
"""Tests for the DataEnum project."""
1 change: 1 addition & 0 deletions tests/test_data_enum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (C) 2021 Chase Finch
"""Tests for DataEnum."""

import pickle
Expand Down