Decrease code redundancy without breaking references

B

bmalinova

My settings file
-------------------
from global_settings import *

SITE_FOLDER = 'mypipe'
SITE_DOMAIN = 'localhost'
SITE_NAME = 'My Pipe'
SITE_ID = 1

TEMPLATE_DIRS = ("/clients/"+SITE_FOLDER+"/templates", "/packages/apps/
templates")
MEDIA_FILES_PREFIX = 'http://'+SITE_DOMAIN+'/media/'
VIDEO_FILES_URL = 'http://'+SITE_DOMAIN+'/video/'
VIDEO_FILES_ROOT = '/clients/'+SITE_FOLDER+'/video'
__________________________________

The first 4 settings are unique to every setting file.

The last 4 lines (TEMPLATE_DIRS - MEDIA_ROOT) are always based on the
SITE_FOLDER and SITE_DOMAIN and are the same in all my config files.

How can I separate the last 4 lines repetitive lines (I need to DRY
the code)?

Frank
http://vizualbod.com
 
C

Chris Rebert

for cfg in settings_modules:
cfg.TEMPLATE_DIRS = ("/clients/"+ cfg.SITE_FOLDER+"/templates",
"/packages/apps/templates")
cfg.MEDIA_FILES_PREFIX = 'http://'+ cfg.SITE_DOMAIN+'/media/'
cfg.VIDEO_FILES_URL = 'http://'+ cfg.SITE_DOMAIN+'/video/'
cfg.VIDEO_FILES_ROOT = '/clients/'+ cfg.SITE_FOLDER+'/video'


Without more information about how your code is structured, it's hard
to give more precise advice.

Cheers,
Chris
 
F

Frank Malina

There is the following source structure:

/packages/...
/packages/global_settings.py # this is the global settings file
imported from each client settings file
/clients/...
/clients/client1/settings.py # client specific settings file (see code
above), each client is running in its own process so they never clash
/clients/client2/settings.py
/clients/client3/settings.py
....
Now how to remove the repetitive bork, bork, bork if it relies on the
clients settings.
I am still learning python, help appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top