Package: autocompvar
Welcome to autocompvar DocumentationIn most of the case, put constant value in your code. Because when you see the value, you don’t know what does that mean in behind. A better way is to define a constant, and use that constant variable.But sometimes, you have a great amount of constant, and you need to put that in your code anywhere. It’s really hard to remember all constant name. Especially, sometimes they are nested.autocompvarallows developer to create a Constant def script from data, and then you don’t need to remember anything anymore.Quick LinksGitHub HomepageOnline DocumentationPyPI downloadInstallIssue submit and feature requestAPI reference and source codeExampleSuppose you have a RPG game. You need to write some application that query stuff using item id. But the hell is to remember what is what! If you just use constant, then your code is unmaintainable!ItemType
|--- Weapon: subclass_id=1, name=weapon
    |--- Sword: id=1, name=sword
    |--- Dagger: id=2, name=dagger
|--- Armor: subclass_id=2, name=armor
    |--- Plate: id=1, name=plate
    |--- Cloth: id=2, name=clothIdeally, you want this:# define your constant here>>>fromitemtypeimportitemtype# when you type itemtype.name, it gives you all available subclass# when you type itemtype.name____weapon, it gives you all attributes>>>itemtype.name____weapon.subclass_id1>>>itemtype.name____weapon.name____dagger.id2# or you can use a function interface to program that>>>itemtype.name____armor.getattr_by_id(2).nameclothReal Code(Code example can be found at:https://github.com/MacHu-GWU/autocompvar-project/blob/master/example.py)First, import autocompvar, prepare your data, and generate code.#!/usr/bin/env python# -*- coding: utf-8 -*-from__future__importprint_functionfromautocompvar.metadataimportgen_code# classname# attrs: all attributes you need to define# keys: indexable attributes, like int, string field that are unique# data: the data to create instance of this class# collection: subclass listdata={"classname":"ItemType","attrs":[],"collection":[{"classname":"SubClass","attrs":["id","name"],"keys":["name"],"data":{"id":1,"name":"weapon"},"collection":[{"classname":"Weapon","attrs":["id","name"],"keys":["name"],"data":{"id":1,"name":"sword"},},{"classname":"Weapon","attrs":["id","name"],"keys":["name"],"data":{"id":2,"name":"dagger"},},],},{"classname":"SubClass","attrs":["id","name"],"keys":["name"],"data":{"id":2,"name":"armor"},"collection":[{"classname":"Armor","attrs":["id","name"],"keys":["name"],"data":{"id":1,"name":"plate"},},{"classname":"Armor","attrs":["id","name"],"keys":["name"],"data":{"id":2,"name":"armor"},},],},],}code=gen_code(data)print(code)Then you have a importable script that able to dofrom your_module import item_type:#!/usr/bin/env python# -*- coding: utf-8 -*-fromautocompvar.baseimportBaseclassItemType(Base):__attrs__=[]__keys__=[]def__init__(self):passclassSubClass(Base):__attrs__=['id','name']__keys__=['name']def__init__(self,id=None,name=None):self.id=idself.name=nameclassWeapon(Base):__attrs__=['id','name']__keys__=['name']def__init__(self,id=None,name=None):self.id=idself.name=nameclassArmor(Base):__attrs__=['id','name']__keys__=['name']def__init__(self,id=None,name=None):self.id=idself.name=nameitem_type=ItemType()sub_class_name_weapon=SubClass(name='weapon',id=1)weapon_name_sword=Weapon(name='sword',id=1)sub_class_name_weapon.name____sword=weapon_name_swordweapon_name_dagger=Weapon(name='dagger',id=2)sub_class_name_weapon.name____dagger=weapon_name_daggeritem_type.name____weapon=sub_class_name_weaponsub_class_name_armor=SubClass(name='armor',id=2)armor_name_plate=Armor(name='plate',id=1)sub_class_name_armor.name____plate=armor_name_platearmor_name_armor=Armor(name='armor',id=2)sub_class_name_armor.name____armor=armor_name_armoritem_type.name____armor=sub_class_name_armorNow you can easily with any constant like this:>>>print(item_type.name____weapon.name____sword.id)1>>>print(item_type.name____armor.name____cloth.name)clothInstallautocompvaris released on PyPI, so all you need is:$pipinstallautocompvarTo upgrade to latest version:$pipinstall--upgradeautocompvar