How to Build Config Files in python?












-1














As I'm new and learning python, exploring different ways to build a config file for python based framework.



I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.



# config.py



class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'

AWS_DEFAULT_REGION = 'ap-northeast-2'

STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB


class DevelopmentConfig(Config):
DEBUG = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'

DATABASE_URI = 'database-uri-for-dev'

class TestConfig(Config):
DEBUG = True
TESTING = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'

DATABASE_URI = 'database-uri-for-dev'


class ProductionConfig(Config):
DEBUG = False

AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'

DATABASE_URI = 'database-uri-for-dev'


class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'


# main.py



import sys
import config

...

if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'

if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')

app.ci = config.CIConfig


What is app.config and app.ci ? How is it being used ?




  1. And also, what all other best possible pythonic way to manage config files ?

  2. If I have multiple set of profiles/credentials (username-password), how do i manage them ?

  3. Any possible encryption to files containing credentials ?


Will be of great learning to me.










share|improve this question
























  • Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
    – Bishal
    Nov 12 at 6:36












  • thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
    – StackGuru
    Nov 12 at 6:39










  • Not sure, why someone downvoted this question.
    – StackGuru
    Nov 12 at 6:40










  • @Bishal How about app.config and app.ci in main.py ?
    – StackGuru
    Nov 12 at 6:57
















-1














As I'm new and learning python, exploring different ways to build a config file for python based framework.



I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.



# config.py



class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'

AWS_DEFAULT_REGION = 'ap-northeast-2'

STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB


class DevelopmentConfig(Config):
DEBUG = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'

DATABASE_URI = 'database-uri-for-dev'

class TestConfig(Config):
DEBUG = True
TESTING = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'

DATABASE_URI = 'database-uri-for-dev'


class ProductionConfig(Config):
DEBUG = False

AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'

DATABASE_URI = 'database-uri-for-dev'


class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'


# main.py



import sys
import config

...

if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'

if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')

app.ci = config.CIConfig


What is app.config and app.ci ? How is it being used ?




  1. And also, what all other best possible pythonic way to manage config files ?

  2. If I have multiple set of profiles/credentials (username-password), how do i manage them ?

  3. Any possible encryption to files containing credentials ?


Will be of great learning to me.










share|improve this question
























  • Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
    – Bishal
    Nov 12 at 6:36












  • thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
    – StackGuru
    Nov 12 at 6:39










  • Not sure, why someone downvoted this question.
    – StackGuru
    Nov 12 at 6:40










  • @Bishal How about app.config and app.ci in main.py ?
    – StackGuru
    Nov 12 at 6:57














-1












-1








-1







As I'm new and learning python, exploring different ways to build a config file for python based framework.



I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.



# config.py



class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'

AWS_DEFAULT_REGION = 'ap-northeast-2'

STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB


class DevelopmentConfig(Config):
DEBUG = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'

DATABASE_URI = 'database-uri-for-dev'

class TestConfig(Config):
DEBUG = True
TESTING = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'

DATABASE_URI = 'database-uri-for-dev'


class ProductionConfig(Config):
DEBUG = False

AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'

DATABASE_URI = 'database-uri-for-dev'


class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'


# main.py



import sys
import config

...

if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'

if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')

app.ci = config.CIConfig


What is app.config and app.ci ? How is it being used ?




  1. And also, what all other best possible pythonic way to manage config files ?

  2. If I have multiple set of profiles/credentials (username-password), how do i manage them ?

  3. Any possible encryption to files containing credentials ?


Will be of great learning to me.










share|improve this question















As I'm new and learning python, exploring different ways to build a config file for python based framework.



I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.



# config.py



class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'

AWS_DEFAULT_REGION = 'ap-northeast-2'

STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB


class DevelopmentConfig(Config):
DEBUG = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'

DATABASE_URI = 'database-uri-for-dev'

class TestConfig(Config):
DEBUG = True
TESTING = True

AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'

DATABASE_URI = 'database-uri-for-dev'


class ProductionConfig(Config):
DEBUG = False

AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'

DATABASE_URI = 'database-uri-for-dev'


class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'


# main.py



import sys
import config

...

if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'

if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')

app.ci = config.CIConfig


What is app.config and app.ci ? How is it being used ?




  1. And also, what all other best possible pythonic way to manage config files ?

  2. If I have multiple set of profiles/credentials (username-password), how do i manage them ?

  3. Any possible encryption to files containing credentials ?


Will be of great learning to me.







json python-2.7 config






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:38

























asked Nov 12 at 6:33









StackGuru

557




557












  • Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
    – Bishal
    Nov 12 at 6:36












  • thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
    – StackGuru
    Nov 12 at 6:39










  • Not sure, why someone downvoted this question.
    – StackGuru
    Nov 12 at 6:40










  • @Bishal How about app.config and app.ci in main.py ?
    – StackGuru
    Nov 12 at 6:57


















  • Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
    – Bishal
    Nov 12 at 6:36












  • thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
    – StackGuru
    Nov 12 at 6:39










  • Not sure, why someone downvoted this question.
    – StackGuru
    Nov 12 at 6:40










  • @Bishal How about app.config and app.ci in main.py ?
    – StackGuru
    Nov 12 at 6:57
















Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
– Bishal
Nov 12 at 6:36






Line 55 env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
– Bishal
Nov 12 at 6:36














thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39




thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39












Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40




Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40












@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57




@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57












1 Answer
1






active

oldest

votes


















0














Here is a small example of how you could use config files



class Config:
APP_NAME='myapp'
ADMIN='admin'

class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'

class ProductionConfig(Config):
DEBUG = False

def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment

# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)

if __name__ == "__main__":
main()


This example does not use command line arguments like your example but should give you a good idea of building config files and using them.



In your example app.ci refers to configuration for continuous integration(CI) environment.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256964%2fhow-to-build-config-files-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here is a small example of how you could use config files



    class Config:
    APP_NAME='myapp'
    ADMIN='admin'

    class DevelopmentConfig(Config):
    DEBUG = True
    ADMIN = 'dev_admin'

    class ProductionConfig(Config):
    DEBUG = False

    def main():
    config = ProductionConfig # Change to DevelopmentConfig to experiment

    # You may now use your config where you want
    print(config.DEBUG)
    print(config.ADMIN)

    if __name__ == "__main__":
    main()


    This example does not use command line arguments like your example but should give you a good idea of building config files and using them.



    In your example app.ci refers to configuration for continuous integration(CI) environment.






    share|improve this answer


























      0














      Here is a small example of how you could use config files



      class Config:
      APP_NAME='myapp'
      ADMIN='admin'

      class DevelopmentConfig(Config):
      DEBUG = True
      ADMIN = 'dev_admin'

      class ProductionConfig(Config):
      DEBUG = False

      def main():
      config = ProductionConfig # Change to DevelopmentConfig to experiment

      # You may now use your config where you want
      print(config.DEBUG)
      print(config.ADMIN)

      if __name__ == "__main__":
      main()


      This example does not use command line arguments like your example but should give you a good idea of building config files and using them.



      In your example app.ci refers to configuration for continuous integration(CI) environment.






      share|improve this answer
























        0












        0








        0






        Here is a small example of how you could use config files



        class Config:
        APP_NAME='myapp'
        ADMIN='admin'

        class DevelopmentConfig(Config):
        DEBUG = True
        ADMIN = 'dev_admin'

        class ProductionConfig(Config):
        DEBUG = False

        def main():
        config = ProductionConfig # Change to DevelopmentConfig to experiment

        # You may now use your config where you want
        print(config.DEBUG)
        print(config.ADMIN)

        if __name__ == "__main__":
        main()


        This example does not use command line arguments like your example but should give you a good idea of building config files and using them.



        In your example app.ci refers to configuration for continuous integration(CI) environment.






        share|improve this answer












        Here is a small example of how you could use config files



        class Config:
        APP_NAME='myapp'
        ADMIN='admin'

        class DevelopmentConfig(Config):
        DEBUG = True
        ADMIN = 'dev_admin'

        class ProductionConfig(Config):
        DEBUG = False

        def main():
        config = ProductionConfig # Change to DevelopmentConfig to experiment

        # You may now use your config where you want
        print(config.DEBUG)
        print(config.ADMIN)

        if __name__ == "__main__":
        main()


        This example does not use command line arguments like your example but should give you a good idea of building config files and using them.



        In your example app.ci refers to configuration for continuous integration(CI) environment.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 22:31









        Bishal

        558216




        558216






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256964%2fhow-to-build-config-files-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."