為自己Coding
為自己Coding

YO~~ 剛跨入AI人工智慧領域的小小工程師, 熱愛自學, 熱愛分享, 下班後的我想為自己Coding, 積極撰寫教學文, 想將自學的程式知識分享給大家, 不斷追求進步的自己, 希望有一天能回饋社會,幫助需要幫助的人, 如果您有什麼很酷的想法,也覺得我還行,歡迎您找我合作~~ IG: https://www.instagram.com/coding_4_me/

Coding up-Python- Argparse easy teaching


Hi, sharing a very useful python suite Argparse, I read some teachings on the Internet and sorted them out. In short, it is a method that allows us to control the internal variables of python externally. In the past, we It may be necessary to change a variable in the python file, but with this, we can easily give new variables when running the python XXX.py command

Github link

introduce

Briefly introduce, under this initialization function, some parameters that can be set

These parameters can be freely set by yourself haha, but in order to let those who use our program understand what we are doing, we still set them obediently.

  1. prog: program name
  2. usage: how to use your program (default=None, it will replace with your program name) (how to use this program)
  3. description: describe about your program
  4. epilog: additional materials

Example: parser1 = ArgumentParser(prog = “my argparse example”, usage =None, description = “learn how to use argparse”, epilog = “ https://github.com/chwang12341/Learn-Python- ”)

Actual operation

Of course, you still have to actually play it, and everyone will use it. It is very simple. When you want to change the variables in the program to external control, you can use parser.add_argument(name, type, help (when the wrong variable is entered, it will be Jump out of the reminder, for example, it should be an integer, and then use a string, which is wrong))

(ex.parser.add_argument('runtimes', type=int, help='display an integer'))


1. What are optional parameters

As the name implies, when you run the python file, you can choose to add or not add parameters


2. What is the difference between optional parameters and the original method (fixed parameters)?

Originally: parser.add_argument('runtimes', type=int, help='display an integer')

Optional: parser.add_argument(' — place', type=str, help='display an integer', dest = 'position')

That's right, there is one more "-" before the first parameter name, haha


3. dest?

When we take args(args = parser.parse_args())

a. The case where dest is not added is when we want to use this parameter returned from the outside, we will use args.place

ex.parser.add_argument(' — place', type=str, help='display an integer')

b. After adding, we will use args.position

ex.parser.add_argument(' — place', type=str, help='display an integer', dest = 'position')

In fact, after parsing, what is the name of the variable changed to?


Example:

Scenario: Today I want to write a PYTHON, I can set who runs how many times (fixed parameter), and can also set where to run and what to eat (optional parameter)

actual execution

Do you feel that this kit is very useful? Haha

Of course, there are many ways to use it!! There are also many parameters that can be changed, you can refer to its official documents

Hope it helps you~~



Reference:

argparse - Parser for command-line options, arguments and sub-commands - Python 3.8.3 documentation
Source code: Lib/argparse.py The following code is a Python program that takes a list of integers and produces either… docs.python.org

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...

Comment