Skip to main content

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

Project description

Build Status

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.

Installation

From pip

pip install python-settings

Or

Clone this repo and type

python setup.py install

How to configure

Create a python file like settings.py in your project, the variable names must be in Capital Letters (A-Z), example:

# settings.py

# Variables definition
DATABASE_HOST = '10.0.0.1'

DATABASE_NAME = 'DATABASENAME'

Two optional patterns to initialize this library

  • Option 1. Using an environment variable. You must have an environment variable called SETTINGS_MODULE and as a value your just created python settings file in the format {module}. {name}. With no .py extension.

    Example in bash:

     export SETTINGS_MODULE=settings
    

    Example in Python

    import os
    os.environ["SETTINGS_MODULE"] = 'settings' 
    
  • Option 2. Calling the configure function from our settings module and passing it your python file

    from python_settings import settings
    from . import settings as my_local_settings
    
    settings.configure(my_local_settings) # configure() receives a python module
    assert settings.configured # now you are set
    

How to use

Import the settings module and access directly to your properties:

from python_settings import settings 

print(settings.DATABASE_HOST) # Will print '10.0.0.1'
print(settings.DATABASE_NAME) # Will print 'DATABASENAME'

Lazy Initialization

Every time you start/restart your python project, all your defined variables are evaluated many times, if you are dealing with heavy to instantiate objects like database connections or similar network calls you will expect some delay.

Using Lazy Initialization increases the performance of this process, changing the behavior of evaluating the variables only when is needed.

Use the Lazy Initializer

In your python settings file, you have to import our LazySetting class located in python_settings.

from python_settings import LazySetting
from my_awesome_library import HeavyInitializationClass # Heavy to initialize object

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

Only the first time you call this property, the HeavyInitializationClass will be instantiated and the *args and **kwargs parameters will be passed. Every time you call this property the same instance will be returned.

And now from any place in your code, you have to call the property

from python_settings import settings 

object_initialized = settings.LAZY_INITIALIZATION # Will return an instance of your object

Example for different environments

You can use as many settings files as you need for different environments. Example for development environment settings:

# development_settings.py
import os

from .base_settings import *


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

Example for testing environment

# 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

export SETTINGS_MODULE = 'myproject.settings.testing_settings'

or use the config function

TODO LIST:

  • Add function to update default environment variable name

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.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

python_settings-0.2.2-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-settings-0.2.2.tar.gz.

File metadata

  • Download URL: python-settings-0.2.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for python-settings-0.2.2.tar.gz
Algorithm Hash digest
SHA256 d98bfcf772f94a6f44d4220dce30032a707bd963e7a0a1e011dc75a2608c880a
MD5 c79bc66877fe853d45bece1c369389d6
BLAKE2b-256 cb439a8cb1dbc08884fbf009d0533f77cf937be70e3301f5099bd910f356cc5a

See more details on using hashes here.

File details

Details for the file python_settings-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: python_settings-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for python_settings-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 41a32caaa598e000b686ee54ac926b0a203e3201f01f30e8d5b6cae1215eb2d3
MD5 da5fb345c681ab923defb2d84126ea8e
BLAKE2b-256 7d9e8d61145b0e1a4b42d14cbf003dc68caf2cbc5e996a1bfdd4175e2b3a6c99

See more details on using hashes here.

Supported by

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