3

I'm building a service, which has a few cronjobs running, written in Python. However, this is my first Python-project ever, so I'm still a very beginner.

What I'm doing now, is that I have my database-connection handled on every file, so basically if I wanted to change the host, I would need to go through all the files. I'm now looking into a PHP-include() similar method for Python, so that I could include some general stuff instead of copy-pasting.

Also, the Python-files are ran in cronjob, so the method should work on cronjobs too :)

agf
  • 171,228
  • 44
  • 289
  • 238
Martti Laine
  • 12,655
  • 22
  • 68
  • 102
  • You can import a module with global values: http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – gimel Aug 15 '11 at 09:36

3 Answers3

3

If it's really just a couple of settings for a single database connection, just put it in a Python module and import it in all of your files. Why add any complexity you don't need?

If it's more complicated, use ConfigParser as @AdamMatan suggested.

# dbconfig.py
host = '127.0.0.1'
user = 'stack'
password = 'overflow'

# db.py

import dbconfig
print dbconfig.host
print dbconfig.user
print dbconfig.password
agf
  • 171,228
  • 44
  • 289
  • 238
  • 1
    This is a good start (+1). The problem with putting configuration in a Python module is that it *may* then get treated as a simple matter of configuration, and the responsibility handed over to non-programmers who don't understand Python's syntax or the significance of whitespace, leading to trouble with production systems. – johnsyweb Aug 15 '11 at 10:09
  • If it's really as simple as my example above, how can you mess up the whitespace? Certainly, if you're starting to have to carefully pick whether to use single or double quotes because of quoting needed in values, it's time to switch to another format. – agf Aug 15 '11 at 10:12
  • 1
    @agf By trying to indent sections, for example. – Adam Matan Aug 15 '11 at 10:15
  • 1
    @agf: A non-programmer wouldn't see a problem with putting a space or two at the beginning of a line. There is also the security risk of allowing people to write code in a configuration file, but that's a whole other matter. This *may* not be an issue in the OP's workplace, of course. – johnsyweb Aug 15 '11 at 10:15
  • 1
    I agree with all of the above, really. This is just the easiest, if possibly shortsighted, way to refactor out a few settings. – agf Aug 15 '11 at 10:23
  • This is perfect for my project! I'll definitely look into more advanced methods while I learn more Python :) – Martti Laine Aug 15 '11 at 11:40
1

Use an external configuration file, with your db connection (host, name, password, db, ...) in it, and read the configuration file from within the Python script.

This makes changes easy (even for non-programmers) and nicely complies with the Single Choice Principle.

Example:

db.cfg

[db]
host=127.0.0.1
user=stack
password=overflow

db.py

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open('db.cfg'))

print config.get('db', 'host')

Execution result:

127.0.0.1
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
0

If you need to call __import__(), you are doing it wrong.

You need to refactor your code so that you no longer have the database connection routines scattered throughout your codebase. Yes, it would be even nicer to have these details in a configuration file (+1 @Adam Matan), but first you need to eliminate the duplication. This will save you a world of pain in the long run.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247