Skip to main content

This module provides you easy access to your **config/settings** properties from all your python modules

Project description

# python-settings
This module provides you easy access to your **config/settings** properties from all your python modules, it supports normal and lazy initialization for each property. It is based on
[django.conf.settings](https://github.com/django/django/blob/stable/1.11.x/django/conf/__init__.py#L58').

## Installation
From pip
```bash
pip install python-settings
```

Or

Clone this repo and type
```bash
python setup.py install
```

## Usage
There are two ways to initialize this library
* **Manual configuration**. Using python modules

```python
# Avoid this way after installing python_settings
from python_settings.tests.settings.base_settings import URL_CONFIG
from python_settings.tests.settings import base_settings

#Using this module
from python_settings import settings
settings.configure(base_settings) # configure() receives a python module
assert settings.configured
assert settings.URL_CONFIG == URL_CONFIG # now you can use settings in all your project

```
* Using an **environment variable**. You must have an environment variable called **SETTINGS_MODULE** pointing to your settings module in the format {module}.
{settings}. With no .py extension.

Example:
```bash
export SETTINGS_MODULE='myproject.settings'
```
or

```python
import os
os.environ["SETTINGS_MODULE"] = 'myproject.settings'
```

Example of the settings.py, it must contain variables in capital letter format:
```python
# settings.py
from python_settings import LazySetting

DATABASE_HOST = '10.0.0.1'

DATABASE_NAME = 'DATABASENAME'

LAZY_INITIALIZATION = LazySetting(HeavyInitializationClass, "127.0.0.1:4222")
# LazySetting(Class, *args, **kwargs)
```

And from any module in your code, you should call your settings variables like this example:
```python
from python_settings import settings

print(settings.DATABASE_HOST)
print(settings.DATABASE_NAME)
# The initialization of the object will happen only once
settings.LAZY_INITIALIZATION.instantiated_object_fn()
```

## Example for different environments
You can use as many settings files as you need for different environments.
Example for development environment settings:
```python
# development_settings.py
import os

from .base_settings import *


TOKEN_API = os.environ.get("TOKEN_API")


```

Example for testing environment
```python
# testing_settings.py
import os

from .settings import *

DATABASE_HOST = '10.0.0.1'

TOKEN_API = os.environ.get("TOKEN_API")
```

And update your **SETTINGS_MODULE** variable
```bash
export SETTINGS_MODULE = 'myproject.settings.testing_settings'
```
or using the manual config

TODO LIST:
* Add compatibility with Python 2.7 in the LazyInitializer

Project details


Download files

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

Source Distribution

python_settings-0.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file python_settings-0.2.tar.gz.

File metadata

  • Download URL: python_settings-0.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for python_settings-0.2.tar.gz
Algorithm Hash digest
SHA256 e97384cd9d722829525240347d5e5709a64f0babbe3322bb92d6e66a76d1b61c
MD5 14d7a9c7f87e4afa89e08bde209c402c
BLAKE2b-256 3559db7ed0a348fd12f5e6110f67593da1af400e501a12aa6e5de8544ec0e2ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page