Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Config-Class in Python

what is the pythonic way to use a config in object oriented programming?

I thought of the following 2 setups:

class Main():
    def __init__(self):
        self.config = ConfigClass("config_file.cfg")
        self.worker = WorkerClass(self.config.USER_NAME)
        self.worker2 = WorkerClass2(self.config)

    def run(self):
        self.worker.work()
        self.worker2.work()

class ConfigClass():
    def __init__(self, file_name):
        self.config = ConfigParser.ConfigParser()
        self.config.read(file_name)

    @property
    def USER_NAME(self):
        return self.config.get("USER", "NAME")

class WorkerClass():
    def __init__(self, user_name):
        self.user = user_name

    def work(self):
        print(self.user)

class WorkerClass2():
    def __init__(self, config):
        self.config = config
        self.user = self.config.USER_NAME

    def work(self):
        print(self.user)

Is it preferred to pass the config object or just the needed strings or is there a better different approach which I didn't mention?

Thanks in advance.

like image 812
F.M.F. Avatar asked Feb 22 '17 08:02

F.M.F.


People also ask

What is config class in Python?

A configurable is a regular Python class that serves as a base class for all main classes in an application. The Configurable base class is lightweight and only does one things. This Configurable is a subclass of HasTraits that knows how to configure itself.

What is config () in Python?

The Python Configuration can be used to build a customized Python which behaves as the regular Python. For example, environment variables and command line arguments are used to configure Python. The Isolated Configuration can be used to embed Python into an application. It isolates Python from the system.

What is config path in Python?

The config file is part of the project. Then you would define the config path as config/conf. cfg in the module, cd into root and start module by doing python src/module1.py. Depending on what os you are on, this link might also be helpful.

How do you write to config in Python?

Python Configuration File The simplest way to write configuration files is to simply write a separate file that contains Python code. You might want to call it something like databaseconfig.py . Then you could add the line *config.py to your . gitignore file to avoid uploading it accidentally.


1 Answers

in flask we have the factory pattern to initialize the app in simple and flexible ways between diff environment. using python-dotenv package, allow to you to load variable in a .env file which will be unique for each environment (dev, staging, prod...). you can use the same pattern.

here is an example of .env file locally, and you want to use diff databases for diff purposes (dev, testing ...)

DATABASE_URI=postgresql://exercise_user:[email protected]:5432/exercise_new_db
DATABASE_TEST_URI=postgresql://exercise_user:[email protected]:5432/exercise_db_test
DATABASE_DOCKER_URI=postgresql://docker_exercise_user:pass@db/docker_exercise_db

to let the software know which environment is running on, write a shell start.sh script that will export the main environment variable

export ENV=development
python main.py

and here is an example of config file, where you define an abstract class contains all configurations that are the same for all envs, then you create for each environemnt a class inherited from that base class, and these class contains unique configuration.

from os import environ, path
from dotenv import load_dotenv, find_dotenv


basedir = path.abspath(path.dirname(__file__))
# here we we load environment variables from .env, must be called before init. class
load_dotenv(find_dotenv(), verbose=True)


class ConfigFactory(object):
    def factory():
        env = environ.get("ENV", "development")
        if env is 'testing':
            return Testing()
        elif env is 'development':
            return Development()
        elif env is 'docker':
            return Docker()
        elif env is 'production':
            return Production()


class Config:

    """Base config."""
    SQLALCHEMY_TRACK_MODIFICATIONS = False


class Development(Config):
    DEBUG = True
    TESTING = False
    SQLALCHEMY_DATABASE_URI = environ.get('DATABASE_URI')


class Testing(Config):
    DEBUG = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = environ.get('DATABASE_TEST_URI')


class Docker(Config):
    DEBUG = True
    TESTING = False
    SQLALCHEMY_DATABASE_URI = environ.get('DATABASE_DOCKER_URI')


class Production(Config):
    DEBUG = True
    TESTING = False
    SQLALCHEMY_DATABASE_URI = environ.get('DATABASE_URI')

and finally you import you config class in your main file and call it

like image 169
alim91 Avatar answered Sep 21 '22 14:09

alim91