Understanding 'Contract'

I’m studying the formal program discription, so: Program to start program on schedule. is the discription. The following would be the ‘Contract’, it is the algorithm for the discription.

Inform options: Application turn on or an Alarm.
Input of what program to launch (audiobook app)
Input of what program arguments are needed.(book or article title)
Input the time to start app.
AM or PM on the clock? # needs work, may need to turn into 24 hour time.
Get current time
count down time = current time - launch
Count down time on time.
OUTPUT:
Turns on the app at the right time.
Or an Alarm.

As I understand it, the next step is to do the software design (modules or functions or classes)
For what size program is this worth doing this for? The amount of paper generated is more than the program sometimes. I’m breaking the habit of writing dirty code , then clean it up. This is how a lot of code was written back before the 1990s, lots of print statements.
Thanks for your thoughts.

While I’m sure there are people here who can help you, I want to ask how this is related to Python? It seems a bit out-of-topic for a Python forum atm.

Sorry if wrong for the forum. I’m learning how to program Python. Please read my bio.
Part of programing is to learn how to set yourself or a group of programers, in an organized way. It’s about how to write in Pythonic code.

1 Like

Design by contract allows you to assume the contract conditions. The contract limits what you need to handle.

This means that your code can be simpler to implement.
You can use assets to check for pre and post conditions.
But python does not have the best suppprt for design by contract.

A while ago there was a length thread on python ideas mailing list to add better support for contracts, but nothing was added to python.
There are libraries on pypi that help with this.

By Barry Scott via Discussions on Python.org at 01Sep2022 21:39:

Design by contract allows you to assume the contract conditions. The
contract limits what you need to handle.

This means that your code can be simpler to implement.
You can use assets to check for pre and post conditions.
But python does not have the best suppprt for design by contract.

A while ago there was a length thread on python ideas mailing list to add better support for contracts, but nothing was added to python.
There are libraries on pypi that help with this.

In particular the icontract module aims to help with design by
contract. It provides some nice decorators for functions to let you
express your contract, and they get checked on function call and return.
It makes an effort to produce good error messages too. And more!

See: icontract · PyPI
There are example uses on that page.

Install with: pip install icontract

Cheers,
Cameron Simpson cs@cskk.id.au

By Jacob Nilsson via Discussions on Python.org at 01Sep2022 18:19:

While I’m sure there are people here who can help you, I want to ask
how this is related to Python? It seems a bit out-of-topic for a Python
forum atm.

I disagree. Design by contract is a perfectly respectable programme
design method, and putting it into practice in Python is a perfectly
acceptable thing to ask about.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

When programming, one would decide on the interface, data structure, and algorithm.

First, consider how the user or you want to use/call this function.
Then, it will determine the interface, the so-called API, such as start(command, time).
Next, determine the data structure and write the call with the minimum necessary data (no need to work for now). In your case, the data may be as simple as

cmd = "audiobook"
t = datetime(2022,9,3,12,0,0)
start(cmd, t)

Finally, do the implementation. For example,

from datetime import datetime
import time 
import subprocess

def start(cmd, t):
    while t > datetime.now():
        time.sleep(1)
    subprocess.Popen(cmd)

which gives the minimum code to work, I think. But to accomplish the contract, you must improve it more. When the data is bigger or more complex, you will consider using class.

Thank you for showing that I’m on the right path in my thinking, but there were two other considerations. I’m only two months into Python and am looking forward to classes. I want to get a solid understanding of the basics first. Yes, it will work but its the hard way to do it. Also, it’s not Pythonic. This is a big language with large concepts. Lot to get a handle on.