0

I have the following code that reads the configuration file and stores the results in some variables as a list

import ConfigParser

def read_config_file():
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

Now i want to get the result variables php_bsc_mdls and php_adv_mdls from the function something like read_config_file.php_bsc_mdls or read_config_file.php_adv_mdls

So is it possible to access/get the variables from the python function ?

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • `return (php_bsc_mdls, php_adv_mdls)` – Niklas R Feb 18 '13 at 07:42
  • i got this error AttributeError: 'function' object has no attribute 'php_bsc_mdls' when i tried to access like read_config_file.php_bsc_mdls – Shiva Krishna Bavandla Feb 18 '13 at 07:44
  • It depends, fundamentally there is a way in python to access the local environment of a function but if you have to do this the you are not doing something right! Also what happens if neither if clauses are hit, then the variables aren't created (python is an interpreted language) as such you are going to need to rewrite this function, have it return this values, initialized to None ... – Samy Vilar Feb 18 '13 at 07:49

3 Answers3

1

You just need to return them. They cease to exist when the function ends.

def read_config_file():
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

    if php_bsc_mdls and php_adv_bls:
        return php_bsc_mdls,php_adv_mdls
    elif php_bsc_mdls:
        return php_bsc_mdls, None

Other approach would be a class where you save them to class variables. And later get those values from the class, not the function.

Or like this:

def read_config_file():
    php_bsc_mdls = None
    php_adv_mdls = None
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

    return php_bsc_mdls, php_adv_mdls

In either case, you need to check the return values where ever you call the function. If the values are none or not.

Gjordis
  • 2,540
  • 1
  • 22
  • 32
  • k i tried that , and got the error AttributeError: 'function' object has no attribute 'php_bsc_mdls' when i tried read_config_file.php_bsc_mdls – Shiva Krishna Bavandla Feb 18 '13 at 07:44
  • You need to check if they have been created. Will modify code accordingly. – Gjordis Feb 18 '13 at 07:44
  • yes we can do it with class, but trying to make it with python functions – Shiva Krishna Bavandla Feb 18 '13 at 07:47
  • I assumed here, that you either have only basic or both. but you can modify it. Since if the codition config.has_option is not met, the variable will not be created. You could also create the variables outside the if-clauses – Gjordis Feb 18 '13 at 07:47
  • Null?! I think you mean None, eitherway just initialize both variables to None and return them at the end ... – Samy Vilar Feb 18 '13 at 07:53
  • Too many languages at work at the same time. Working on java atm. hence the null by accident – Gjordis Feb 18 '13 at 07:54
0
def read_config_file():
  php_bsc_mdls, php_adv_mdls = None, None # Init to None.
  ...
  return php_bsc_mdls, php_adv_mdls # Return at end.

# Get results
basic, advanced = read_config_file()

Alternatively, you could just make a class for this.

class Config:
  php_adv_mdls = None
  php_bsc_mdls = None
  @staticmethod
  def read_config_file():
    if not Config.php_adv_mdls and not Config.php_bsc_mdls:
      # Load the data here.
      config = ConfigParser.ConfigParser()
      ...
      Config.php_adv_mdls = config.get...
      Config.php_bsc_mdls = config.get...

Use the class variables and static methods to load your config files once.

Config.php_adv_mdls # None
Config.read_config_file() # Loads the data from config.
Config.read_config_file() # Will only attempt another load if either 
                          # class variable in not valid.
Config.php_bsc_mdls       # If successful, will be initialize with data.
Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

As it was said it would be reasonable to do this with return.

But...

In python functions are objects, so you can do smth like this:

def a():
    a.x = 10

a()
print a.x    # >> 10

Last though not an example of good code, in this case.

Ellochka Cannibal
  • 1,750
  • 2
  • 19
  • 31