-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
day4-packagingCreating packagesCreating packages
Description
When writing code, it is important to think about how you or others can run it. A popular way is to use a command-line interface, so that your code can be executed as a script from a terminal. In this exercise we will look at the tools that Python offers for creating such interfaces.
We will use the squares.py file we used this morning for the documentation exercise. We will make the code more generic by creating a command-line interface that will make it easier to call.
Constant weight
Let's first make our first interface without weights (assuming them constant and equal to 1).
- Choose who in your team is writing
- Make sure you have a fork, a local copy of the average_squares repository, and a new branch.
- Open the file
squares.py. Make sure you can run it from a terminal! (python squares.py) - Look at the part of the file that is inside the
if __name__ == "__main__":guard. This is the code that you will work on. Currently, the input values are hardcoded into the file. - Use the
argparselibrary to rewrite this part so that it reads only thenumbersfrom the command-line (keep for now the weights hardcoded asNone). The file should be runnable aspython <numbers>...(where<numbers>should be replaced by the sequence of numbers of your choice). For example:python squares.py 3 4 5
- Look at the example in the notes to get you started, and refer to the argparse documentation as needed.
- Decide which values should be read from the command line.
- Add them as
argparserarguments. - Check the auto-generated help:
python squares.py --help. - Check that you can run the file with the new form.
- Share your solution as a pull request to the average_squares repository mentioning this issue (by including the text
Addresses UCL-ARC-RSEworkshop-summer-25/RSE-Classwork#22in the pull request description), remember to mention your team members too! (with@github_username)
Metadata
Metadata
Assignees
Labels
day4-packagingCreating packagesCreating packages