extended_choices.helpers module

Provides classes used to construct a full Choices instance.

Notes

The documentation format in this file is numpydoc.

class extended_choices.helpers.ChoiceAttributeMixin(value, choice_entry)[source]

Bases: future.types.newobject.newobject

Base class to represent an attribute of a ChoiceEntry.

Used for constant, name, and display.

It must be used as a mixin with another type, and the final class will be a type with added attributes to access the ChoiceEntry instance and its attributes.

choice_entry

instance of ChoiceEntry – The ChoiceEntry instance that hold the current value, used to access its constant, value and display name.

constant

property – Returns the choice field holding the constant of the attached ChoiceEntry.

value

property – Returns the choice field holding the value of the attached ChoiceEntry.

display

property – Returns the choice field holding the display name of the attached ChoiceEntry.

original_value

? – The value used to create the current instance.

creator_type

type – The class that created a new class. Will be ChoiceAttributeMixin except if it was overridden by the author.

Example

Classes can be created manually:

>>> class IntChoiceAttribute(ChoiceAttributeMixin, int): pass
>>> field = IntChoiceAttribute(1, ChoiceEntry(('FOO', 1, 'foo')))
>>> field
1
>>> field.constant, field.value, field.display
('FOO', 1, 'foo')
>>> field.choice_entry
('FOO', 1, 'foo')

Or via the get_class_for_value class method:

>>> klass = ChoiceAttributeMixin.get_class_for_value(1.5)
>>> klass.__name__
'FloatChoiceAttribute'
>>> float in klass.mro()
True
constant

Property that returns the constant attribute of the attached ChoiceEntry.

display

Property that returns the display attribute of the attached ChoiceEntry.

classmethod get_class_for_value(value)[source]

Class method to construct a class based on this mixin and the type of the given value.

Parameters:value – The value from which to extract the type to create the new class.

Notes

The create classes are cached (in cls.__classes_by_type) to avoid recreating already created classes.

value

Property that returns the value attribute of the attached ChoiceEntry.

class extended_choices.helpers.ChoiceEntry[source]

Bases: tuple

Represents a choice in a Choices object, with easy access to its attribute.

Expecting a tuple with three entries. (constant, value, display name), it will add three attributes to access then: constant, value and display.

By passing a dict after these three first entries, in the tuple, it’s also possible to add some other attributes to the ChoiceEntry` instance.

Parameters:tuple (tuple) – A tuple with three entries, the name of the constant, the value, and the display name. A dict could be added as a fourth entry to add additional attributes.

Example

>>> entry = ChoiceEntry(('FOO', 1, 'foo'))
>>> entry
('FOO', 1, 'foo')
>>> (entry.constant, entry.value, entry.display)
('FOO', 1, 'foo')
>>> entry.choice
(1, 'foo')

You can also pass attributes to add to the instance to create:

>>> entry = ChoiceEntry(('FOO', 1, 'foo', {'bar': 1, 'baz': 2}))
>>> entry
('FOO', 1, 'foo')
>>> entry.bar
1
>>> entry.baz
2
Raises:AssertionError – If the number of entries in the tuple is not expected. Must be 3 or 4.
class ChoiceAttributeMixin(value, choice_entry)

Bases: future.types.newobject.newobject

Base class to represent an attribute of a ChoiceEntry.

Used for constant, name, and display.

It must be used as a mixin with another type, and the final class will be a type with added attributes to access the ChoiceEntry instance and its attributes.

choice_entry

instance of ChoiceEntry – The ChoiceEntry instance that hold the current value, used to access its constant, value and display name.

constant

property – Returns the choice field holding the constant of the attached ChoiceEntry.

value

property – Returns the choice field holding the value of the attached ChoiceEntry.

display

property – Returns the choice field holding the display name of the attached ChoiceEntry.

original_value

? – The value used to create the current instance.

creator_type

type – The class that created a new class. Will be ChoiceAttributeMixin except if it was overridden by the author.

Example

Classes can be created manually:

>>> class IntChoiceAttribute(ChoiceAttributeMixin, int): pass
>>> field = IntChoiceAttribute(1, ChoiceEntry(('FOO', 1, 'foo')))
>>> field
1
>>> field.constant, field.value, field.display
('FOO', 1, 'foo')
>>> field.choice_entry
('FOO', 1, 'foo')

Or via the get_class_for_value class method:

>>> klass = ChoiceAttributeMixin.get_class_for_value(1.5)
>>> klass.__name__
'FloatChoiceAttribute'
>>> float in klass.mro()
True
constant

Property that returns the constant attribute of the attached ChoiceEntry.

display

Property that returns the display attribute of the attached ChoiceEntry.

classmethod get_class_for_value(value)

Class method to construct a class based on this mixin and the type of the given value.

Parameters:value – The value from which to extract the type to create the new class.

Notes

The create classes are cached (in cls.__classes_by_type) to avoid recreating already created classes.

value

Property that returns the value attribute of the attached ChoiceEntry.

extended_choices.helpers.create_choice_attribute(creator_type, value, choice_entry)[source]

Create an instance of a subclass of ChoiceAttributeMixin for the given value.

Parameters:
  • creator_type (type) – ChoiceAttributeMixin or a subclass, from which we’ll call the get_class_for_value class-method.
  • value – The value for which we want to create an instance of a new subclass of creator_type.
  • choice_entry (ChoiceEntry) – The ChoiceEntry instance that hold the current value, used to access its constant, value and display name.
Returns:

An instance of a subclass of creator_type for the given value

Return type:

ChoiceAttributeMixin