How to call class method in another app?

2

I want to create a file called config.py and put some settings inside a dictionary for whenever you need to invoke these parameters.

But how do I call this parameter in Python?

For example, I'm going to create the class Config .

class Config:

@classmethod
def getConfig(self):
    parameters = {
        'url_m'                 : 'domain',
        'url_ws_m'              : 'domain.api',
        'url_get_token'         : 'domain.token',
        'url_external_access'   : 'external',
        'name_ws_m'             : 'service'
    }

    return parameters

In the other app I did so:

from pp.core.Config import *
.
.
.
a = Config().getConfig()['url_m']
So it worked. But is there a more elegant method of importing the class with the method, or is this the right way to do it?

    
asked by anonymous 29.12.2017 / 17:02

2 answers

2

It can be simpler and more elegant. Use the module only and create a function that gives access to an internal dictionary of the module. Ex:

"Config.py" file

_parameters = {
    'url_m'                 : 'domain',
    'url_ws_m'              : 'domain.api',
    'url_get_token'         : 'domain.token',
    'url_external_access'   : 'external',
    'name_ws_m'             : 'service'
}

def get(key):
    return _parameters[key]

And to use, call it like this:

import pp.core.Config as Config

a = Config.get('url_m')

The class, in your case, is just putting a layer of complexity that you do not need.

    
30.12.2017 / 00:08
1

You do not need to create a class for this. You can create a config.py file with a dictioner and then import that dictionary where you want to use it.

In the config.py file

parameters = {
    'url_m'                 : 'domain',
    'url_ws_m'              : 'domain.api',
    'url_get_token'         : 'domain.token',
    'url_external_access'   : 'external',
    'name_ws_m'             : 'service'
}

And in foo.py

from config import parameters

a = parameters['url_m']
    
30.12.2017 / 23:07