Re: [Python-ideas] Callable Enum values

[redirecting back to the list] On 04/19/2017 11:15 PM, Stephan Hoyer wrote:
I'm curious, what did you find ugly with: class TestEnum(CallableEnum): @enum def hello(text): "a pleasant greeting" print('hello,', text) @enum def goodbye(text): print('goodbye,', text) -- ~Ethan~

On Thu, Apr 20, 2017 at 10:58 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
Yeah, this is actually pretty reasonable. For my use case, both the functions and their names are pretty long, so I wouldn't want to write them inside the enum class. With a decorator/function wrapper, it just gets kind of long -- especially if I only import modules as required <https://google.github.io/styleguide/pyguide.html?showone=Imports#Imports> by the Google style guide. So my real usage looks more like: class Greeting(callable_enum.Enum): HELLO = callable_enum.Value(_greet_hello) GOODBYE = callable_enum.Value(_greet_goodbye) TO_PYTHON_IDEAS = callable_enum.Value(_welcome_to_python_ideas) The large callable_enum.Value() wrappers make it harder to track what's going on. But perhaps this is really just a good use for an inline declaration, where I don't need the wrappers at all: Greeting = callable_enum.Enum('Greeting', { 'HELLO': _greet_hello, 'GOODBYE': _greet_goodbye, 'TO_PYTHON_IDEAS': _welcome_to_python_ideas, })

On Fri, Apr 21, 2017 at 9:43 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
You have to use the complete module name? Instead of `from callable_enum import Enum, Value`? Ouch
Yeah... it took me a while to come around to this one. But the rule does start to pay off in large code bases.

OT, but... You have to use the complete module name? Instead of `from callable_enum
import Enum, Value`? Ouch.
yes, but: """ Use from x import y as z if two modules named y are to be imported or if y is an inconveniently long name. """ (https://google.github.io/styleguide/pyguide.html?showone=Imports#Imports) So you can rename it if it's long: import callable_enum as ce class Greeting(callable_enum.Enum): HELLO = ce.Value(_greet_hello) GOODBYE = ce.Value(_greet_goodbye) TO_PYTHON_IDEAS =ce.Value(_welcome_to_python_ideas) not too bad, and a pattern that has been pretty universally adopted by, e.g., numpy: import numpy as np. I'm still on the fence for modules that I'm only using a couple names from though... -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

On Thu, Apr 20, 2017 at 10:58 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
Yeah, this is actually pretty reasonable. For my use case, both the functions and their names are pretty long, so I wouldn't want to write them inside the enum class. With a decorator/function wrapper, it just gets kind of long -- especially if I only import modules as required <https://google.github.io/styleguide/pyguide.html?showone=Imports#Imports> by the Google style guide. So my real usage looks more like: class Greeting(callable_enum.Enum): HELLO = callable_enum.Value(_greet_hello) GOODBYE = callable_enum.Value(_greet_goodbye) TO_PYTHON_IDEAS = callable_enum.Value(_welcome_to_python_ideas) The large callable_enum.Value() wrappers make it harder to track what's going on. But perhaps this is really just a good use for an inline declaration, where I don't need the wrappers at all: Greeting = callable_enum.Enum('Greeting', { 'HELLO': _greet_hello, 'GOODBYE': _greet_goodbye, 'TO_PYTHON_IDEAS': _welcome_to_python_ideas, })

On Fri, Apr 21, 2017 at 9:43 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
You have to use the complete module name? Instead of `from callable_enum import Enum, Value`? Ouch
Yeah... it took me a while to come around to this one. But the rule does start to pay off in large code bases.

OT, but... You have to use the complete module name? Instead of `from callable_enum
import Enum, Value`? Ouch.
yes, but: """ Use from x import y as z if two modules named y are to be imported or if y is an inconveniently long name. """ (https://google.github.io/styleguide/pyguide.html?showone=Imports#Imports) So you can rename it if it's long: import callable_enum as ce class Greeting(callable_enum.Enum): HELLO = ce.Value(_greet_hello) GOODBYE = ce.Value(_greet_goodbye) TO_PYTHON_IDEAS =ce.Value(_welcome_to_python_ideas) not too bad, and a pattern that has been pretty universally adopted by, e.g., numpy: import numpy as np. I'm still on the fence for modules that I'm only using a couple names from though... -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (3)
-
Chris Barker
-
Ethan Furman
-
Stephan Hoyer