Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

make your data importable

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for autocompvar, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size autocompvar-0.0.3.tar.gz (42.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page