Re: [Python-ideas] Callable Enum values

2017-04-21 Thread Stephan Hoyer
On Thu, Apr 20, 2017 at 10:58 AM, Ethan Furman  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)
>

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

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,
})
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Callable Enum values

2017-04-21 Thread Ethan Furman

On 04/21/2017 09:04 AM, Stephan Hoyer wrote:

On Thu, Apr 20, 2017 at 10:58 AM, Ethan Furman 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)

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
 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.


You have to use the complete module name?  Instead of `from callable_enum 
import Enum, Value`?  Ouch.

--
~Ethan~
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Callable Enum values

2017-04-21 Thread Stephan Hoyer
On Fri, Apr 21, 2017 at 9:43 AM, Ethan Furman  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.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Callable Enum values

2017-04-21 Thread Chris Barker
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.bar...@noaa.gov
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/