• Programming by contract.

    From Weatherby,Gerard@21:1/5 to All on Sun Feb 26 02:44:28 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)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Gerard on Sun Feb 26 14:57:58 2023
    On 26Feb2023 02:44, Weatherby,Gerard <[email protected]> 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 <[email protected]>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Gerard" on Sun Feb 26 10:55:37 2023
    "Weatherby,Gerard" <[email protected]> writes:
    I’m wondering if anyone on the list has experience using any of the Programming by Contract modules?

    I haven't used the Python ones but I used Ruby's contract gem and found
    it helpful for documentation purposes if nothing else. Note that it
    introduces a runtime penalty that can be significant if the contract
    says, e.g. that all elements of an array are supposed to have some
    property.

    Eiffel's DBC was intended to be implemented at least partly by static verification at compile time. Similarly with SPARK/ADA. That gets rid
    of runtime overhead, though sometimes it imposes choices on the
    implementation that aren't always the fastest.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)