candv package¶
Submodules¶
candv.core module¶
Defines base constant and base container for constants.
-
class
candv.core.
SimpleConstant
[source]¶ Bases:
object
Base class for all constants.
Variables: name (str) – constant’s name: set up automatically and is equal to the name of the container’s attribute -
full_name
¶
-
merge_into_group
(group)[source]¶ Called automatically by the container after group construction.
Note
Redefine this method in all derived classes. Attach all custom attributes and methods to the group here.
Parameters: group – an instance of Constants
or of its subclass into which this constant will be mergedReturns: None
-
to_group
(group_class, **group_members)[source]¶ Convert a constant into a constants group.
Parameters: - group_class (class) – a class of group container which will be created
- group_members – unpacked dict which defines group members
Returns: a lazy constants group which will be evaluated by the container. Method
merge_into_group()
will be called during evaluation of the groupExample:
from candv import Constants from candv import SimpleConstant class FOO(Constants): A = SimpleConstant() B = SimpleConstant().to_group(Constants, B2 = SimpleConstant(), B0 = SimpleConstant(), B1 = SimpleConstant(), )
-
-
class
candv.core.
Constants
[source]¶ Bases:
object
Base class for creating constants containers.
Each constant defined within the container will remember its creation order.
See an example in
constants()
.Variables: constant_class – defines a class of constants which a container will store. This attribute MUST be set up manually when you define a new container type. Otherwise container will not be initialized. Default: None
Raises: CandvContainerMisusedError – if you try to create an instance of container. Containers are singletons and they cannot be instantiated. Their attributes must be used directly. -
constant_class
¶ Defines a top-level class of constants which can be stored by container
alias of
SimpleConstant
-
full_name
= 'Constants'¶
-
name
= 'Constants'¶
-
-
candv.core.
with_constant_class
(the_class)[source]¶ Create a mixin class with
constant_class
attribute.Allows to set a constant class for constants container outside container itself. This may help to create more readable container definition, e.g.:
from candv import Constants from candv import SimpleConstant from candv import with_constant_class class CustomConstant(SimpleConstant): ... class FOO(with_constant_class(CustomConstant), Constants): A = CustomConstant() B = CustomConstant()
>>> FOO.constant_class <class '__main__.CustomConstant'>
candv.exceptions module¶
-
exception
candv.exceptions.
CandvException
[source]¶ Bases:
Exception
Base exception
New in version 1.4.0.
-
exception
candv.exceptions.
CandvTypeError
[source]¶ Bases:
TypeError
,candv.exceptions.CandvException
New in version 1.4.0.
-
exception
candv.exceptions.
CandvInvalidGroupMemberError
[source]¶ Bases:
candv.exceptions.CandvTypeError
New in version 1.4.0.
-
exception
candv.exceptions.
CandvInvalidConstantClass
[source]¶ Bases:
candv.exceptions.CandvTypeError
New in version 1.4.0.
-
exception
candv.exceptions.
CandvContainerMisusedError
[source]¶ Bases:
candv.exceptions.CandvTypeError
New in version 1.4.0.
-
exception
candv.exceptions.
CandvConstantAlreadyBoundError
[source]¶ Bases:
ValueError
,candv.exceptions.CandvException
New in version 1.4.0.
-
exception
candv.exceptions.
CandvMissingConstantError
[source]¶ Bases:
KeyError
,candv.exceptions.CandvException
New in version 1.4.0.
-
exception
candv.exceptions.
CandvValueNotFoundError
[source]¶ Bases:
ValueError
,candv.exceptions.CandvException
New in version 1.4.0.
candv.ext module¶
Provides extra ready-to-use classes for constructing custom constants.
-
class
candv.ext.
VerboseMixin
(*args, **kwargs)[source]¶ Bases:
object
Adds
verbose_name
andhelp_text
attributes to constants.Arguments must be passed as kwargs.
Parameters: Example:
class CustomConstant(object): def __init__(self, arg1, arg2, kwarg1=None): pass class VerboseCustomConstant(VerboseMixin, CustomConstant): def __init__(self, arg1, arg2, kwarg1=None, verbose_name=None, help_text=None): super().__init__( arg1, arg2, kwarg1=kwarg1, verbose_name=verbose_name, help_text=help_text, )
-
class
candv.ext.
VerboseConstant
(verbose_name=None, help_text=None)[source]¶ Bases:
candv.ext.VerboseMixin
,candv.core.SimpleConstant
A constant with optional verbose name and optional help text.
Parameters: Variables:
-
class
candv.ext.
ValueConstant
(value)[source]¶ Bases:
candv.core.SimpleConstant
A constant with ability to hold arbitrary values.
Parameters: value – a value to attach to constant Variables: value – constant’s value
-
class
candv.ext.
VerboseValueConstant
(value, verbose_name=None, help_text=None)[source]¶ Bases:
candv.ext.VerboseMixin
,candv.ext.ValueConstant
A constant which can have both verbose name, help text, and a value.
Parameters: Variables:
-
class
candv.ext.
Values
[source]¶ Bases:
candv.core.Constants
A container for
ValueConstant
and its derivatives.Supports getting and filtering constants by their values plus listing values of all constants in container.
-
constant_class
¶ alias of
ValueConstant
-
classmethod
filter_by_value
(value)[source]¶ Get all constants which have given value.
Parameters: value – value of the constants to look for Returns: list of all found constants with given value
-
full_name
= 'Values'¶
-
classmethod
get_by_value
(value)[source]¶ Get a constant by its value.
Parameters: value – value of the constant to look for Returns: first found constant with given value Raises: CandvValueNotFoundError – if no constant in container has given value
-
classmethod
itervalues
()[source]¶ Get an iterator over values of all constants in the order they were defined.
Same as
values()
but returns an interator.Note
Overrides
itervalues()
since 1.1.2.
-
name
= 'Values'¶
-
classmethod
values
()[source]¶ List values of all constants in the order they were defined.
Returns: list
of valuesExample:
from candv import Values from candv import ValueConstant class FOO(Values): TWO = ValueConstant(2) ONE = ValueConstant(1) SOME = ValueConstant("some string")
>>> FOO.values() [2, 1, 'some string']
Note
Overrides
values()
since 1.1.2.
-