Package: candv
candvallows to create complex enum-like constants.In contrast to other methods of defining constants,candvhelps to organize and to document classes of constants.This is done by providing iteration and lookup facilities for containers of constants.Additionally,candvprovides an ability to attach human-readable names, descriptions, arbitrary values, and methods to constants.Inspired byConstants from TwistedandForm Fields from Django.InstallationAvailable as aPyPIpackage:pipinstallcandvBrief overviewThe most basic definition of constants:fromcandvimportConstantsfromcandvimportSimpleConstantclassTEAM(Constants):RED=SimpleConstant()BLUE=SimpleConstant()And usage:TEAM.RED# <constant 'TEAM.RED'>TEAM['RED']# <constant 'TEAM.RED'>TEAM.get('RED')# <constant 'TEAM.RED'>TEAM.get('GREEN')# NoneTEAM.RED.name# 'RED'TEAM.RED.full_name# 'TEAM.RED'TEAM.RED.to_primitive()# {'name': 'RED'}TEAM.RED.container# <constants container 'TEAM'>TEAM# <constants container 'TEAM'>TEAM.name# 'TEAM'TEAM.full_name# 'TEAM'len(TEAM)# 2TEAM.has_name('RED')# TrueTEAM.names()# ['RED', 'BLUE']TEAM.iternames()# <odict_iterator object at 0x7f451013e0e0>TEAM.constants()# [<constant 'TEAM.RED'>, <constant 'TEAM.BLUE'>]TEAM.iterconstants()# <odict_iterator object at 0x7f45100f3450>TEAM.items()# [('RED', <constant 'TEAM.RED'>), ('BLUE', <constant 'TEAM.BLUE'>)]TEAM.iteritems()# <odict_iterator object at 0x7f451013bdb0>TEAM.to_primitive()# {'name': 'TEAM', 'items': [{'name': 'RED'}, {'name': 'BLUE'}]}Using with values:fromcandvimportValuesfromcandvimportValueConstantclassTEAM(Values):RED=ValueConstant(1)BLUE=ValueConstant(2)TEAM.values()# [1, 2]TEAM.itervalues()# <map object at 0x7f450ffdb1c0>TEAM.get_by_value(1)# <constant 'TEAM.RED'>TEAM.filter_by_value(1)# [<constant 'TEAM.RED'>]TEAM.RED.value# 1TEAM.RED.to_primitive()# {'name': 'RED', 'value': 1}Using with human-readable names:fromcandvimportConstantsfromcandvimportVerboseConstantclassCountries(Constants):au=VerboseConstant("Australia")uk=VerboseConstant("United Kingdom")us=VerboseConstant("United States")Countries.au.name# 'au'Countries.au.verbose_name# 'Australia'Countries.au.help_text# NoneCountries.au.to_primitive()# {'name': 'au', 'verbose_name': 'Australia', 'help_text': None}With values and names:fromcandvimportValuesfromcandvimportVerboseValueConstantclassSkillLevel(Values):rki=VerboseValueConstant(0,"rookie")avg=VerboseValueConstant(1,"average")vtn=VerboseValueConstant(2,"veteran")ace=VerboseValueConstant(3,"ace")SkillLevel.avg.value#  1SkillLevel.avg.name# 'avg'SkillLevel.avg.full_name# 'SkillLevel.avg'SkillLevel.avg.verbose_name# 'average'SkillLevel.avg.help_text# NoneSkillLevel.avg.to_primitive()# {'name': 'avg', 'value': 1, 'verbose_name': 'average', 'help_text': None}Plays well withverboselibor, say,Django translation strings:fromcandvimportConstantsfromcandvimportVerboseConstantfromverboselibimportTranslationstranslations=Translations(domain="the_app",locale_dir_path="locale",)_=translations.gettext_lazyclassUnitType(Constants):aircraft=VerboseConstant(_("aircraft"))ship=VerboseConstant(_("ship"))train=VerboseConstant(_("train"))vehicle=VerboseConstant(_("vehicle"))Supports custom methods:fromcandvimportConstantsfromcandvimportSimpleConstantclassSupportedLanguages(Constants):en=SimpleConstant()ru=SimpleConstant()@classmethoddefget_default(cls):returncls.enSupportedLanguages.get_default()# <constant 'SupportedLanguages.en'>And custom types of constants:fromcandvimportConstantsfromcandvimportSimpleConstantfromcandvimportwith_constant_classclassMissionStatus(SimpleConstant):...classMissionStatuses(with_constant_class(MissionStatus),Constants):not_loaded=MissionStatus()loaded=MissionStatus()playing=MissionStatus()It’s also possible to define hierarchies:fromcandvimportConstantsfromcandvimportSimpleConstantclassSTATUS(Constants):SUCCESS=SimpleConstant()ERROR=SimpleConstant().to_group(Constants,INVALID=SimpleConstant(),NOT_FOUND=SimpleConstant(),INTERNAL=SimpleConstant(),)STATUS.names()# ['SUCCESS', 'ERROR']STATUS.ERROR# <constants group 'STATUS.ERROR'>STATUS.ERROR.full_name# 'STATUS.ERROR'STATUS.ERROR.INTERNAL# <constant 'STATUS.ERROR.INTERNAL'>STATUS.ERROR.INTERNAL.full_name# 'STATUS.ERROR.INTERNAL'STATUS.ERROR.names()# ['INVALID', 'NOT_FOUND', 'INTERNAL']More infoVisitthe docsfor full information.Seedjango-candv-choicesfor using aschoicesindjango.Seedjango-rf-candv-choicesfor using aschoicesindjango-rest-framework.