Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

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.

Files for python-settings, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size python_settings-0.2.2-py2.py3-none-any.whl (8.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size python-settings-0.2.2.tar.gz (6.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page