Skip to main content
https://travis-ci.org/MacHu-GWU/autocompvar-project.svg?branch=master https://img.shields.io/pypi/v/autocompvar.svg https://img.shields.io/pypi/l/autocompvar.svg https://img.shields.io/pypi/pyversions/autocompvar.svg

Welcome to autocompvar Documentation

In 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.

autocompvar allows developer to create a Constant def script from data, and then you don’t need to remember anything anymore.

Example

Suppose 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=cloth

Ideally, you want this:

# define your constant here
>>> from itemtype import itemtype

# 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_id
1

>>> itemtype.name____weapon.name____dagger.id
2

# or you can use a function interface to program that
>>> itemtype.name____armor.getattr_by_id(2).name
cloth

Real 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__ import print_function
from autocompvar.metadata import gen_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 list
data = {
    "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 do from your_module import item_type:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from autocompvar.base import Base

class ItemType(Base):
    __attrs__ = []
    __keys__ = []

    def __init__(self):
        pass


class SubClass(Base):
    __attrs__ = ['id', 'name']
    __keys__ = ['name']

    def __init__(self, id=None, name=None):
        self.id = id
        self.name = name


class Weapon(Base):
    __attrs__ = ['id', 'name']
    __keys__ = ['name']

    def __init__(self, id=None, name=None):
        self.id = id
        self.name = name


class Armor(Base):
    __attrs__ = ['id', 'name']
    __keys__ = ['name']

    def __init__(self, id=None, name=None):
        self.id = id
        self.name = name



item_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_sword

weapon_name_dagger = Weapon(name='dagger', id=2)
sub_class_name_weapon.name____dagger = weapon_name_dagger
item_type.name____weapon = sub_class_name_weapon

sub_class_name_armor = SubClass(name='armor', id=2)

armor_name_plate = Armor(name='plate', id=1)
sub_class_name_armor.name____plate = armor_name_plate

armor_name_armor = Armor(name='armor', id=2)
sub_class_name_armor.name____armor = armor_name_armor
item_type.name____armor = sub_class_name_armor

Now 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)
cloth

Install

autocompvar is released on PyPI, so all you need is:

$ pip install autocompvar

To upgrade to latest version:

$ pip install --upgrade autocompvar

Metadata

Release files for autocompvar 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autocompvar 0.0.3
File Size Uploaded
autocompvar-0.0.3.tar.gz 42.5 kB Details

Release files / autocompvar-0.0.3.tar.gz

Download URL autocompvar-0.0.3.tar.gz
Size 42.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cca5d6281ac85b573ed8d6cd9c4abfee696c34bec2d402e71cb1319429d9ab04
BLAKE2b-256 checksum
How to use checksums
ad3b4b780a9f6515fe23953f577b8114219a5cfab298dbf5cecc6dccee3ffb22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page