Skip to main content
Help us improve Python packaging – donate today!

provide extensive way of managing your constant variable.

Project Description

https://travis-ci.org/MacHu-GWU/constant2-project.svg?branch=master

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.

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

Example

from constant2 import Constant

class Food(Constant):

    class Fruit:
        id = 1
        name = "fruit"

        class Apple:
            id = 1
            name = "apple"

            class RedApple:
                id = 1
                name = "red apple"

            class GreenApple:
                id = 2
                name = "green apple"

        class Banana:
            id = 2
            name = "banana"

            class YellowBanana:
                id = 1
                name = "yellow banana"

            class GreenBanana:
                id = 2
                name = "green banana"

    class Meat:
        id = 2
        name = "meat"

        class Pork:
            id = 1
            name = "pork"

        class Beef:
            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

Release history Release notifications

History Node

0.0.9

History Node

0.0.8

History Node

0.0.7

This version
History Node

0.0.6

History Node

0.0.5

History Node

0.0.4

History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
constant2-0.0.6.tar.gz (94.2 kB) Copy SHA256 hash SHA256 Source None Jul 17, 2017

Supported by

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