1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
Metadata-Version: 2.1
Name: StrEnum
Version: 0.4.15
Summary: An Enum that inherits from str.
Home-page: https://github.com/irgeek/StrEnum
Author: James Sinclair
Author-email: james@nurfherder.com
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: docs
Requires-Dist: sphinx ; extra == 'docs'
Requires-Dist: sphinx-rtd-theme ; extra == 'docs'
Requires-Dist: myst-parser[linkify] ; extra == 'docs'
Provides-Extra: release
Requires-Dist: twine ; extra == 'release'
Provides-Extra: test
Requires-Dist: pytest ; extra == 'test'
Requires-Dist: pytest-black ; extra == 'test'
Requires-Dist: pytest-cov ; extra == 'test'
Requires-Dist: pytest-pylint ; extra == 'test'
Requires-Dist: pylint ; extra == 'test'
# StrEnum
[](https://github.com/irgeek/StrEnum/actions)
StrEnum is a Python `enum.Enum` that inherits from `str` to complement
`enum.IntEnum` in the standard library. Supports python 3.7+.
## Installation
You can use [pip](https://pip.pypa.io/en/stable/) to install.
```bash
pip install StrEnum
```
## Usage
```python
from enum import auto
from strenum import StrEnum
class HttpMethod(StrEnum):
GET = auto()
HEAD = auto()
POST = auto()
PUT = auto()
DELETE = auto()
CONNECT = auto()
OPTIONS = auto()
TRACE = auto()
PATCH = auto()
assert HttpMethod.GET == "GET"
# You can use StrEnum values just like strings:
import urllib.request
req = urllib.request.Request('https://www.python.org/', method=HttpMethod.HEAD)
with urllib.request.urlopen(req) as response:
html = response.read()
assert len(html) == 0 # HEAD requests do not (usually) include a body
```
There are classes whose `auto()` value folds each member name to upper or lower
case:
```python
from enum import auto
from strenum import LowercaseStrEnum, UppercaseStrEnum
class Tag(LowercaseStrEnum):
Head = auto()
Body = auto()
Div = auto()
assert Tag.Head == "head"
assert Tag.Body == "body"
assert Tag.Div == "div"
class HttpMethod(UppercaseStrEnum):
Get = auto()
Head = auto()
Post = auto()
assert HttpMethod.Get == "GET"
assert HttpMethod.Head == "HEAD"
assert HttpMethod.Post == "POST"
```
As well as classes whose `auto()` value converts each member name to camelCase,
PascalCase, kebab-case, snake_case and MACRO_CASE:
```python
from enum import auto
from strenum import CamelCaseStrEnum, PascalCaseStrEnum
from strenum import KebabCaseStrEnum, SnakeCaseStrEnum
from strenum import MacroCaseStrEnum
class CamelTestEnum(CamelCaseStrEnum):
OneTwoThree = auto()
class PascalTestEnum(PascalCaseStrEnum):
OneTwoThree = auto()
class KebabTestEnum(KebabCaseStrEnum):
OneTwoThree = auto()
class SnakeTestEnum(SnakeCaseStrEnum):
OneTwoThree = auto()
class MacroTestEnum(MacroCaseStrEnum):
OneTwoThree = auto()
assert CamelTestEnum.OneTwoThree == "oneTwoThree"
assert PascalTestEnum.OneTwoThree == "OneTwoThree"
assert KebabTestEnum.OneTwoThree == "one-two-three"
assert SnakeTestEnum.OneTwoThree == "one_two_three"
assert MacroTestEnum.OneTwoThree == "ONE_TWO_THREE"
```
As with any Enum you can, of course, manually assign values.
```python
from strenum import StrEnum
class Shape(StrEnum):
CIRCLE = "Circle"
assert Shape.CIRCLE == "Circle"
```
Doing this with the case-changing classes, though, won't manipulate
values--whatever you assign is the value they end up with.
```python
from strenum import KebabCaseStrEnum
class Shape(KebabCaseStrEnum):
CIRCLE = "Circle"
# This will raise an AssertionError because the value wasn't converted to kebab-case.
assert Shape.CIRCLE == "circle"
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to
discuss what you would like to change.
Please ensure tests pass before submitting a PR. This repository uses
[Black](https://black.readthedocs.io/en/stable/) and
[Pylint](https://www.pylint.org/) for consistency. Both are run automatically
as part of the test suite.
## Running the tests
Tests can be run using `make`:
```
make test
```
This will create a virutal environment, install the module and its test
dependencies and run the tests. Alternatively you can do the same thing
manually:
```
python3 -m venv .venv
.venv/bin/pip install .[test]
.venv/bin/pytest
```
## License
[MIT](https://choosealicense.com/licenses/mit/)
**N.B. Starting with Python 3.11, `enum.StrEnum` is available in the standard
library. This implementation is _not_ a drop-in replacement for the standard
library implementation. Specifically, the Python devs have decided to case fold
name to lowercase by default when `auto()` is used which I think violates the
principle of least surprise.**
|