Created
February 10, 2011 22:37
-
-
Save hinnerk/821509 to your computer and use it in GitHub Desktop.
A simple program consisting of a start-able setting file and a worker that contains all the code. The worker can access settings without blowing up if a setting doesn't exist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# The exercise is to make a start-able config file | |
# | |
# This file contains some settings and minimal code to make it run. | |
name = "Anonymous" | |
weight = 80 | |
size = 1.80 | |
if __name__ == "__main__": | |
from worker import run | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import startme | |
class UnitError(Exception): | |
pass | |
class Wrapper(object): | |
def __init__(self, wrapped): | |
self.wrapped = wrapped | |
def __getattr__(self, name): | |
return getattr(self. wrapped, name, None) | |
SETTINGS = Wrapper(startme) | |
def calc_bmi(weight, size): | |
""" returns the body-mass-index | |
Args | |
weight: body mass in Kilogram | |
size: body length in meter | |
Examples | |
>>> calc_bmi(85, 1.85) | |
24.84 | |
>>> calc_bmi(100, 180) | |
Traceback (most recent call last): | |
... | |
UnitError: Please insert size in meter. | |
""" | |
if not (weight and size): | |
raise UnitError("Size and / or weight are not set.") | |
if size > 10: | |
raise UnitError("Please insert size in meter.") | |
return round(float(weight) / size**2, 2) | |
def run(): | |
""" prints optional name and BMI | |
Examples (this is expected to fail if startme.py is changed) | |
>>> run() | |
BMI Anonymous: 24.69 | |
""" | |
name = " " + SETTINGS.name if SETTINGS.name else "" | |
print "BMI%s: %s" % (name, calc_bmi(SETTINGS.weight, SETTINGS.size)) | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getattr
kennt einen dritten Parameter, der den Standardwert erhält. Also