Skip to content

Environment

Create

By default, reads only environment variables:

1
2
3
4
5
from abconfig import ABConfig

class MyConfig(ABConfig):
    host = str
    port = int

define env

1
2
3
4
5
6
7
8
import os

os.environ['HOST'] = '127.0.0.1'
os.environ['PORT'] = '8000'
# recreate
config = MyConfig()
config
{'host': '127.0.0.1', 'port': 8000}

Names

Capitalized attribute names are used to search for environment variables, you can also add your own prefix for all variables in the class:

1
2
3
4
5
6
7
8
class MyConfig(ABConfig):
    __prefix__ = 'app'
    host = str
    port = int

config = MyConfig()
config
{'host': None, 'port': None}

define new env

1
2
3
4
5
6
os.environ['APP_HOST'] = '0.0.0.0'
os.environ['APP_PORT'] = '8000'

config = MyConfig()
config
{'host': '0.0.0.0', 'port': 8000}

or

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class MyConfig(ABConfig):
    app = {
        'host': str,
        'port': int
    }

config = MyConfig()
config.app
{'host': None, 'port': None}

os.environ['APP_HOST'] = '0.0.0.0'
os.environ['APP_PORT'] = '8000'

config = MyConfig() # recreate
config.app
{'host': '0.0.0.0', 'port': 8000}

Nested dict's

Will also be processed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class MyConfig(ABConfig):
    db = {
        'host': {
            'master': str
        }
    }

config = MyConfig() # recreate
config.db
{'host': {'master': None}}

os.environ['APP_HOST'] = '0.0.0.0'
os.environ['APP_PORT'] = '8000'

config = MyConfig() # recreate
config
{'host': '0.0.0.0', 'port': 8000}

Settings

  • __env__ - on/off bool value;

  • __prefix__ - prefix to each attribute in the class.

Default:

1
2
3
class MyConfig(ABConfig):
    __env__ = True
    __prefix__ = None

Also read