Programming by contract.

Cameron Simpson cs at cskk.id.au
Sat Feb 25 22:57:58 EST 2023


On 26Feb2023 02:44, Weatherby,Gerard <gweatherby at 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 at cskk.id.au>


More information about the Python-list mailing list