Is there any way to use script as configuration files in python?

1

In PHP, we can use a PHP script to only return values, which is very useful for creating configuration files for an application.

Example DB.php

return array(
  'host' => 'localhost',
  'username' => 'username',
  ...
);

index.php

$config = include 'DB.php';

print_r($config); 

// Retorna o array contendo os valores retornados em "DB.php"

When trying to "imitate" this in Python , I was not successful, because it caused me an error.

Example db.py

return [
    1, 2, 3
]

Result:

  

'return' outside function

In this case, if I wanted to load a file into python , just for data-return purposes for configuration, how could I do it?

    
asked by anonymous 13.08.2015 / 15:44

3 answers

1

Yes, it is possible. The most widespread way of doing this is to make the Django framework do : Creating a file (settings.py, for example) and importing it into another module. See:

.
├── main.py
├── config.py
├── __init__.py
├── foo
|   ├── __init__.py
|   └── ...

So, if our configuration module config.py looks like this:

APPNAME = "Foo"
# server
SERVER = {
  "host": "1217.0.0.1",
  "port": 8980,
}

Then you could import into main.py or into any other module the namespace of the settings file, like this:

# current mod: main.py

import config

xpto_connect(config.SERVER["host"], config.SERVER["port"])

Or even import parts of the settings file, just the ones you need:

# current mod: main.py

from config import SERVER, APPNAME

xpto_connect(SERVER["host"], SERVER["port"])

As you can see, it's extremely easy and it seems a lot better than the way PHP does. ;)

Also, the return statement can only be used in the definition and scope of function, so you are getting an error.

Note

Unlike PHP, you should have a file named __init__.py next to the file (same level in the directory) that you want to import (which would include in PHP). This file may be empty.

    
13.08.2015 / 20:23
1

I do not know a way to make identical to PHP, using imports directly. But you give yes to using a .py file for configuration if you create some convention.

For example:

# em config/db.py
config = {
    'hostname': 'localhost',
    'port': 1234
}

# em seu script principal
db_config_file = 'config/db.py'
db_cfg = dict()
with open(db_config_file) as config_file:
    exec(compile(config_file.read(), db_config_file, 'exec'), db_cfg)
print db_cfg['config']

The convention I used is that the configuration file needs to set a config variable. If you use some similar convention and apply it to all your configuration files, you can then put this logic to read the file into a utility function.

    
13.08.2015 / 16:25
1

According to this answer in SOEN , I think a good solution would be to use a json by python .

This can be done through the json module.

JSON example:

{
   "version" : "2.1",
}

Python example:

import json

config = json.load(open('composer.json'))

print(config['version']) # Imprime "2.1"
    
13.08.2015 / 16:14