Skip to content

argparse meets input() #1

@montesmariana

Description

@montesmariana

Here is a script I wrote now that uses input() more or less in the way we talked before.

Not covered

It does not use pyinputplus and the only real validation performed is with word_rate... it's up to you to set up the validation of any other argument. But it should give you the idea.

It doesn't deal with filenames either: the data is hardcoded inside the script and reset every time you call the script. In your case, you would be opening from an Excel as a pandas dataframe and then saving it again. In any case, the example does use pandas and identifies rows by their index, which I recommend. If you want to use the vendors name as an index, you can also set that up when you read the Excel file (ask if you're not sure how).

Functions

I wrote three functions here, not using classes. In your case, you could still have similar functions, but instead of appending directly to the dataframe you can instantiate your class, print the class nicely to show the user what they have added, and then call a method of your class that appends the row or modifies a row in the dataframe. So you would have to see how you organize the input() calls with the class instantiation and how the validation is performed. (Ask if you need guidance.)
One of the functions is to quickly validate the type of the word rate, which could also be done via pyinputplus I expect. The others are one to add a new entry and to modify an existing entry respectively. The main difference, other than the fact that modifying an entry requires an id, is that adding an entry will keep badgering until you provide all values, whereas modifying it will fall back to the original values if a bad value is provided. When integrating this into your code, think: is that the behaviour you would want?

Using it in the command line

Within the if __name__ == "__main__", I give you two options, depending on how you want your script to work. One is: your script can add, modify or something else. In that case, the behavior is triggered by specific flags or arguments. The other is: your script can only add or modify, and the difference is whether an id is provided that matches one of the existing ids.

You can run the first version by calling the script as is, as follows:

To add a new entry (or more)

python input_example.py -a

To edit an existing entry

python input_example.py -m jane_doe

If you want to try out the second version, comment out lines 85-95 and uncomment lines 99-109. Then, in the command line:

To add a new entry (or more)

python input_example.py

To edit an existing entry

python input_example.py jane_doe

If anything is unclear and you would like me to explain the reasoning behind some line, let me know in this issue :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions