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 merged
Returns: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 group

Example:

from candv import Constants
from candv import SimpleConstant


class FOO(Constants):
  A = SimpleConstant()
  B = SimpleConstant().to_group(Constants,

    B2 = SimpleConstant(),
    B0 = SimpleConstant(),
    B1 = SimpleConstant(),
  )
to_primitive(*args, **kwargs)[source]

Represent the constant via Python’s primitive data structures.

Changed in version 1.5.0: The context param is replaced by *args and **kwargs.

New in version 1.3.0.

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 and help_text attributes to constants.

Arguments must be passed as kwargs.

Parameters:
  • verbose_name (str) – optional verbose name
  • help_text (str) – optional description

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,
    )
merge_into_group(group)[source]

Overrides merge_into_group() to add verbose_name with help_text attributes to the target group.

to_primitive(*args, **kwargs)[source]

Changed in version 1.5.0: The context param is replaced by *args and **kwargs.

New in version 1.3.0.

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:
  • verbose_name (str) – optional verbose name of the constant
  • help_text (str) – optional description of the constant
Variables:
  • verbose_name (str) – verbose name of the constant. Default: None
  • help_text (str) – verbose description of the constant. Default: None
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
merge_into_group(group)[source]

Redefines merge_into_group() and adds value attribute to the target group.

to_primitive(*args, **kwargs)[source]

Changed in version 1.5.0: The context param is replaced by *args and **kwargs.

New in version 1.3.0.

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:
  • value – a value to attach to the constant
  • verbose_name (str) – an optional verbose name of the constant
  • help_text (str) – an optional description of the constant
Variables:
  • value – constant’s value
  • verbose_name (str) – verbose name of the constant. Default: None
  • help_text (str) – verbose description of the constant. Default: None
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 values

Example:

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.

candv.version module

Module contents