Skip to content

Instantly share code, notes, and snippets.

@hinnerk
Created February 10, 2011 22:37
Show Gist options
  • Save hinnerk/821509 to your computer and use it in GitHub Desktop.
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.
#!/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()
#!/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()
@hinnerk
Copy link
Author

hinnerk commented Feb 12, 2011

Nett, habe es übernommen. Danke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment