Programming by contract.
Weatherby,Gerard
gweatherby at uchc.edu
Sat Feb 25 21:44:28 EST 2023
The discussion of asserts got me thinking about Programming by Contract. Back in the 90s, I had occasion to learn Eiffel programming language. ( https://en.wikipedia.org/wiki/Eiffel_(programming_language) The concepts are intriguing, although Eiffel itself had barriers to widespread adoption.
Reviewing the topic, I found Wikipedia mentioned some Python implementations. I kicked the tires of “deal” and found it straightforward. I’m wondering if anyone on the list has experience using any of the Programming by Contract modules?
#!/usr/bin/env python3
import argparse
import deal
@deal.pre(lambda x: x > 7)
@deal.pre(lambda x: isinstance(x, int))
def func(x):
print(x)
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--nodeal', action='store_true', help="Disable deal")
parser.add_argument('bad_choice', choices=('type', 'value'))
args = parser.parse_args()
if args.nodeal:
deal.disable()
func(8)
if args.bad_choice == 'type':
func("8")
if args.bad_choice == 'value':
func(1)
More information about the Python-list
mailing list