Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument Parsing 1/3 #140

Open
dpshelio opened this issue Nov 26, 2020 · 0 comments
Open

Argument Parsing 1/3 #140

dpshelio opened this issue Nov 26, 2020 · 0 comments
Labels

Comments

@dpshelio
Copy link
Member

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 last week 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).

  1. Choose who in your team is sharing
  2. Make sure your local copy of this repository is up to date and create a new branch from week06 branch.
  3. Open the file squares.py. Make sure you can run it from a terminal!
  4. 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.
  5. Use the argparse library to rewrite this part so that it reads only the numbers from the command-line (keep for now the weights hardcoded). The file should be runnable as python squares.py <numbers>... (where <numbers> should be replaced by the sequence of numbers of your choice)
    • Look at the example in the notes to get you started, if you haven't already.
    • Decide which values should be read from the command line.
    • Add them as argparser arguments.
    • Check the auto-generated help: python squares.py --help.
    • Check that you can run the file with the new form.
  6. Share your solution as a pull request mentioning this issue (by including the text Addresses #140 in the pull request description), remember to mention your team members too! (with @github_username)
@dpshelio dpshelio changed the title Argument Parsing Argument Parsing 1/3 Nov 26, 2020
twemyss added a commit to twemyss/rse-classwork-2020 that referenced this issue Nov 26, 2020
@FMAUR92 FMAUR92 mentioned this issue Nov 26, 2020
umitozmen added a commit to umitozmen/rse-classwork-2020 that referenced this issue Nov 26, 2020
@poppynikou poppynikou mentioned this issue Nov 26, 2020
DavidScobie added a commit to DavidScobie/rse-classwork-2020 that referenced this issue Nov 26, 2020
esl-lewis added a commit to esl-lewis/rse-classwork-2020 that referenced this issue Nov 26, 2020
dpshelio added a commit that referenced this issue Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants