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

Asked

Viewed 1,144 times

1

In PHP, we can use a PHP script to just 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"

While trying to "imitate" this in Python, I was unsuccessful because I made a mistake.

Example db.py

return [
    1, 2, 3
]

Upshot:

'Return' Outside Function

In this case, if I wanted to upload a file to python, only for data return purpose for configuration, as I could do?

3 answers

2


Yes, it is possible. The most widespread way to do this is to do as the Django framework ago: 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 module settings config.py looked like this:

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

Then you could import in main.py or in any other module the namespace of the settings file in this way:

# current mod: main.py

import config

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

Or still import parts of the settings file, only 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 looks much better to me than the way PHP does. ;)

Also, the instruction Return can only be used in setting and function scope, so you are getting an error.

Note

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

  • Thinking about your answer, it kind of looks like the way it’s done in Django

1

According to this response in the SOEN, I believe that a good solution would be to use a file json and analyse it by python.

This can be done through the module json.

Example JSON:

{
   "version" : "2.1",
}

Python example:

import json

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

print(config['version']) # Imprime "2.1"
  • I think this example is mixed between Javascript and Python ;)

  • I was referring to var and the comment preceded by // instead of # (before you correct; now it is correct). But you can understand your suggestion without problem.

  • Okay, @sergiopereira. doesn’t have that in python :)

1

I don’t know a way to make it identical to PHP, using Imports directly. But you can use a file. py 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 define a variable config. If you use some similar convention and apply it to all your configuration files, you can then put this logic of reading the file into a utility function.

Browser other questions tagged

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