Anders Hovmöller wrote:
> We try to do the same thing in various libraries. We've settled on using
> existing
> python and end up with syntax like:
> class MyForm(Form):
> field = Field()
> or in your case
> class Colors(TokenContainer):
> red = Token()
> green = Token()
> blue = Token()
> (this is using tri.token).
> The discussion on creating namespaces seem to match this type of usage more
> than what
> you've suggested here.
Here's another interesting possible pattern/DSL that I've come up with for my
value/label case. This pattern doesn't require getting fancy with the classdict
at all. It does create choices implicitly via name access, but it's the name of
an attribute of a context object, not the name of a variable in the namespace.
from choices import choices
def test_constructs_with_auto_valued_choices():
# Get choices context for Food choices.
fcc = choices()
# Optionally replace fcc.base with a subclass of fcc.base or
# fcc.mixin here if additional behavior is desirable to have
# within the class body below.
class Food(fcc.base):
# Add a new instance of fcc.base w/ value of 'APPLE' and label
# of None, and create an APPLE class attribute with a value
# of 'APPLE'
fcc.APPLE
# Just as for APPLE above.
fcc.GREEK_YOGURT
# Add an EGG_MCMUFFIN choice and class value-attribute, then
# replace it with a choice that has the same value and a label
# of 'Egg McMuffin' (since instances are immutable).
fcc.EGG_MCMUFFIN %= 'Egg McMuffin'
# Add a choice with value of '~OTHER~' and label of
# not associated with any attribute 'Something else'
fcc.append('~OTHER~', 'Something else')
# After class is defined, unlabeled instances of fcc.base have been
# auto-labeled and upgraded to instances of Food by passing through
# Food.coerce_from(obj).
# The __new__ method behavior of Food has now been locked, and raises
# TypeError for any subsequent attempts to instantiate.
# The default fcc.base class has a value/label namedtuple as a base
# class, so this is valid/successful.
assert tuple(Food) == (
('RED', 'Apple'),
('GREEN', 'Greek Yogurt'),
('EGG_MCMUFFIN', 'Egg McMuffin'),
)
# Members are instances of Food.
assert tuple(type(food) for food in Food) == (Food, Food)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/4BVORO34Q3CDKBKSMMXIXYWS5FU3LNUA/
Code of Conduct: http://python.org/psf/codeofconduct/