Skip to main content
https://travis-ci.org/MacHu-GWU/constant2-project.svg?branch=master https://codecov.io/gh/MacHu-GWU/constant2-project/branch/master/graph/badge.svg https://img.shields.io/pypi/v/constant2.svg https://img.shields.io/pypi/l/constant2.svg https://img.shields.io/pypi/pyversions/constant2.svg https://img.shields.io/badge/Star_Me_on_GitHub!--None.svg?style=social

Welcome to constant2 Documentation

If you have lots of constant2 value widely used across your development. A better way is to define Constantant Variable rather than using the raw value. This can improve the readability and accessibility.

constant2 is a library provide extensive way of managing your constant2 variable.

Another powerful feature is, constant2 allows developer defines normalized entity relationship and is data in class style, which giving awesome accessibility to every single row, and every single column, free developer from memorizing things.

Usage

Version Changed 0.0.9: All nested class now has to inherit from Constant or its subclass:

# WRONG!
class ItemType(Constant):
    class Weapon:
        id = 1

# CORRECT
class ItemType(Constant):
    class Weapon(Constant):
        id = 1

# or
class Item(Constant):
    pass

class ItemType(Constant):
    class Weapon(Item):
        id = 1

Usage:

from constant2 import Constant

class Food(Constant):

    class Fruit(Constant):
        id = 1
        name = "fruit"

        class Apple(Constant):
            id = 1
            name = "apple"

            class RedApple(Constant):
                id = 1
                name = "red apple"

            class GreenApple(Constant):
                id = 2
                name = "green apple"

        class Banana(Constant):
            id = 2
            name = "banana"

            class YellowBanana(Constant):
                id = 1
                name = "yellow banana"

            class GreenBanana(Constant):
                id = 2
                name = "green banana"

    class Meat(Constant):
        id = 2
        name = "meat"

        class Pork(Constant):
            id = 1
            name = "pork"

        class Beef(Constant):
            id = 2
            name = "beef"

food = Food()

You can visit it’s data or child class data in these way:

# Use class
>>> Fruit.Items() # .Items() return it's data
[('id', 1), ('name', 'fruit')]

>>> Fruit.Keys() # .Keys() return keys
['id', 'name']

>>> Fruit.Values() # .Values() return values
[1, 'fruit']

>>> Fruit.ToDict() # return data in a dict
{'id': 1, 'name': 'fruit'}

# use instance
>>> food.items() # .Items() return it's data
[('id', 1), ('name', 'fruit')]

>>> food.keys() # .keys() return keys
['id', 'name']

>>> food.values() # .values() return values
[1, 'fruit']

>>> food.to_dict() # return data in a dict
{'id': 1, 'name': 'fruit'}

# iterate on all nested class
>>> Fruit.Subclasses(sort_by='id')
[class Apple, class Banana]

# get first nested class that kls.id == 1
# useful when you need reverse lookup
>>> Fruit.GetFirst('id', 1)
class Apple

# get all child class that kls.id == 1
>>> Fruit.GetAll('id', 1)
[class Apple, ]

And it provides built-in I/O methods allow you to dump these data in to a dictionary.

>>> data = Food.dump()
>>> data
{
    "Food": {
        "Fruit": {
            "Apple": {
                "GreenApple": {
                    "__classname__": "GreenApple",
                    "id": 2,
                    "name": "green apple"
                },
                "RedApple": {
                    "__classname__": "RedApple",
                    "id": 1,
                    "name": "red apple"
                },
                "__classname__": "Apple",
                "id": 1,
                "name": "apple"
            },
            "Banana": {
                "GreenBanana": {
                    "__classname__": "GreenBanana",
                    "id": 2,
                    "name": "green banana"
                },
                "YellowBanana": {
                    "__classname__": "YellowBanana",
                    "id": 1,
                    "name": "yellow banana"
                },
                "__classname__": "Banana",
                "id": 2,
                "name": "banana"
            },
            "__classname__": "Fruit",
            "id": 1,
            "name": "fruit"
        },
        "Meat": {
            "Beef": {
                "__classname__": "Beef",
                "id": 2,
                "name": "beef"
            },
            "Pork": {
                "__classname__": "Pork",
                "id": 1,
                "name": "pork"
            },
            "__classname__": "Meat",
            "id": 2,
            "name": "meat"
        },
        "__classname__": "Food"
    }
}

>>> Food = Constant.load(data)

Install

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

$ pip install constant2

To upgrade to latest version:

$ pip install --upgrade constant2

Metadata

Release files for constant2 0.0.13

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

Source distribution (sdist)

Source distribution for constant2 0.0.13
File Size Uploaded
constant2-0.0.13.tar.gz 92.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for constant2 0.0.13
File Interpreter ABI Platform
constant2-0.0.13-py2-none-any.whl Python 2 none any Details

Total release size: 433.5 kB

Release files / constant2-0.0.13.tar.gz

Download URL constant2-0.0.13.tar.gz
Size 92.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bbeac1293a399572ee821745c24a63b291cc5db3dd473f89514d727ea18c0604
BLAKE2b-256 checksum
How to use checksums
fb89f8a7b3d99103c679b3e61104c0fb11c3fee04c5cb2a7865ffe231185d5b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.13

Release files / constant2-0.0.13-py2-none-any.whl

Download URL constant2-0.0.13-py2-none-any.whl
Size 341.2 kB
Tags Python 2
SHA-256 checksum
How to use checksums
42a5c7609ff1acd11b6fe606bc827cd7d3ce3618f49e241dbe0ec029ffae3688
BLAKE2b-256 checksum
How to use checksums
da2accb83e6f5d329372baa347cdd4b4421d0aa82a3d8d97aa2b078ca5ee019e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.13

Release history Release notifications | RSS feed

This release

0.0.13 This release

2 release files

0.0.11

1 release file

0.0.10

1 release file

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

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