public
Created

  • Download Gist
.gitignore
1
*.pyc
app.py
Python
1 2 3 4 5 6
from uglycode import run
 
results = run({'override': 'was overridden'})
 
print results[0]
print results[1]
bettercode.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 
def run(overrides):
__params__ = __import__('params')
for name in dir(__params__):
value = getattr(__params__, name)
if isinstance(value, (int, long, basestring)):
globals()[name] = value
 
for var, val in overrides.iteritems():
globals()[var] = val
 
# Don't want to edit below this line
output = []
output.append("override %s" % (override))
output.append("default %s" % (default))
 
if False:
override='was some local crap'
 
return output
evenbetter_app.py
Python
1 2 3 4 5 6 7
hairy = __import__('original')
hairy.override = 'was overridden'
 
results = hairy.run()
 
print results[0]
print results[1]
original.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
 
override = 'was not overridden'
default = 'was a default'
 
def run():
# Don't want to edit below this line
output = []
output.append("override %s" % (override))
output.append("default %s" % (default))
 
if False:
override='was some local crap'
 
return output
params.py
Python
1 2
override = 'was not overridden'
default = 'was a default'
python_version.txt
1
Python 2.7.1
result_of_app.txt
1 2
override was overridden
default was a default
uglycode.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
def run(overrides):
# This works
from params import *
 
# This doesn't seem to
# override = 'was not overridden'
# default = 'was a default'
 
for var, val in overrides.iteritems():
locals()[var] = val
 
# Don't want to edit below this line
output = []
output.append("override %s" % (override))
output.append("default %s" % (default))
 
if False:
override='was some local crap'
 
return output

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.