Mailing List Archive

Programming by contract.
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)
--
https://mail.python.org/mailman/listinfo/python-list
Re: Programming by contract. [ In reply to ]
On 26Feb2023 02:44, Weatherby,Gerard <gweatherby@uchc.edu> wrote:
>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?

I've been using the icontract package happily.

from icontract import require, ensure

@typechecked
@require(
lambda name: is_valid_basename(name), # pylint: disable=unnecessary-lambda
"name should be a clean file basename"
)
def TagSetClass(self, name: str) -> TaggedPath:
''' factory to create a `TaggedPath` from a `name`.
'''
fspath = joinpath(dirname(self.fspath), name)
return TaggedPath(fspath, fstags=self.fstags)

@ensure(lambda result: result == normpath(result))
def keypath(self, fspath):
''' Compute the absolute path used to index a `TaggedPath` instance.

This returns `realpath(fspath)` if `self.config.physical`,
otherwise `abspath(fspath)`.
'''
return realpath(fspath) if self.config.physical else abspath(fspath)

You can stack the decorators just like deal.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list