How to call class method in another app?

Asked

Viewed 528 times

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 I call this parameter in Python?

For example, I will 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']

That’s how it worked. But there is a more elegant method of importing the class with the method or this is the most correct way even?

  • Where is this config.py?

  • I created in a folder.

  • Where did you create it? At the root? inside the main app? in another app? Maybe it would help if you put the structure as it is now

  • I created inside the core folder inside the main folder of the project.

  • It would be legal if you used configparser: https://docs.python.org/3/library/configparser.html

2 answers

2


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

File "Config.py"

_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:

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 don’t need.

  • In fact, your get method is also a "layer that is not accurate": the dictionary defined in the module can be used directly.

  • How would you do it @jsbueno?

  • It looks really classy, Eric. I know these features, but I don’t remember using.

  • Why does your variable start with underscore? I come from PHP, so I’m used to camelCase...

  • "How would you do that @jsbueno?" - simply import the variable directly into the module you use the settings in instead of importing the function get and use it. "_" in the prefix is also not required.

  • The underscore is a python convention to say that that variable should not be used globally. If you really want to use nothing prevents you. But the convention stands there as a reminder.

  • 1

    Yes. The get function is not really needed. I could give a from Config import parameters (in which case I would do without underscore) and simply use parameters['myKey']. But I wanted to make a closer example of the parameters being read only. It’s a matter of personal preference. The Django’s own Settings.py uses this dictionary semantics in which the parameters can be changed in Runtime. The way I put it is only one way to do it. Not the only.

Show 2 more comments

1

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

In the file config.py

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']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.