[Python] I tried to make a simple program that works on the command line using argparse.

ArgumentParser (argparse) When executing a program written in Python on the command line, you can easily use command line arguments by using ArgumentParser (argparse).

What are command line arguments?

Command line arguments are something like this:

python test.py -t 10

Try using

About the instant type

Instantaneous type is generally

e=V_{m} \sin(2 \pi f t \pm \phi)

It is represented by. This time

V_{m}[V],f[Hz],\phi[deg]

With command line variables ([] is the unit in the program) and try to draw various graphs.

Source

Load the library. You can use it by reading argparse.

import argparse

Define the initial value of each argument

    parser = argparse.ArgumentParser()
    parser.add_argument('--phase', '-p', type=float, default=0, help='Definition of a point in time')
    parser.add_argument('--voltage', '-v', type=float, default=100, help='Definition of a effective value')
    parser.add_argument('--frequency', '-f', type=float, default=60, help='Definition of a frequency')
    parser.add_argument('--sampletime', '-t', type=float, default=0.0001, help='Definition of sampletime')    
args = parser.parse_args()

Display of specified numerical value

    print('phase : {}\n'.format(args.phase))
    print('voltage : {}\n'.format(args.voltage))
    print('frequency : {}\n'.format(args.frequency))

Drawing a graph

    t = np.arange(0, 1/60, args.sampletime)
    y1 = args.voltage * np.sqrt(2) * np.sin(2 * np.pi * args.frequency * t + np.deg2rad(args.phase))
    y2 = args.voltage * np.sqrt(2) * np.sin(2 * np.pi * args.frequency * t)
    plt.plot(t, y1)
    plt.plot(t, y2)

    plt.grid(True)

    plt.show()

    #plt.savefig("img.png ")

At the command line

python test.py -p 45

given that

Figure_1.png

To get

Whole code

import numpy as np
import argparse
import matplotlib.pyplot as plt

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--phase', '-p', type=float, default=0, help='Definition of a point in time')
    parser.add_argument('--voltage', '-v', type=float, default=100, help='Definition of a effective value')
    parser.add_argument('--frequency', '-f', type=float, default=60, help='Definition of a frequency')
    parser.add_argument('--sampletime', '-t', type=float, default=0.0001, help='Definition of sampletime')

    args = parser.parse_args()

    print('phase : {}\n'.format(args.phase))
    print('voltage : {}\n'.format(args.voltage))
    print('frequency : {}\n'.format(args.frequency))

    t = np.arange(0, 1/60, args.sampletime)
    y1 = args.voltage * np.sqrt(2) * np.sin(2 * np.pi * args.frequency * t + np.deg2rad(args.phase))
    y2 = args.voltage * np.sqrt(2) * np.sin(2 * np.pi * args.frequency * t)
    plt.plot(t, y1)
    plt.plot(t, y2)

    plt.grid(True)

    plt.show()

if __name__ == '__main__':
    main()

Recommended Posts

[Python] I tried to make a simple program that works on the command line using argparse.
I tried to make a translation BOT that works on Discord using googletrans
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
I tried to make a regular expression of "amount" using Python
I created a Python library to call the LINE WORKS API
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to make a todo application using bottle with python
A note I looked up to make a command line tool in Python
I tried to make a ○ ✕ game using TensorFlow
I tried to make a simple mail sending application with tkinter of Python
I tried using the Python library "pykakasi" that can convert kanji to romaji.
[Python] Smasher tried to make the video loading process a function using a generator
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles
[Python] A program that calculates the difference between the total numbers on the diagonal line.
I tried to notify the honeypot report on LINE
Using PyAutoGui, I tried to automate the UI test of a VB6 program running on Windows Server 2003 R2 (input command cheat sheet).
A story that was convenient when I tried using the python ip address module
Miscellaneous notes that I tried using python for the matter
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I tried to make a document search slack command using Kendra announced at re: Invent 2019.
I made a program to collect images in tweets that I liked on twitter with Python
I tried to make a generator that generates a C # container class from CSV with Python
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
I tried to make a system that fetches only deleted tweets
Rubyist tried to make a simple API with Python + bottle + MySQL
[2nd] I tried to make a certain authenticator-like tool with python
[Python] I tried to implement stable sorting, so make a note
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
Simple code to call a python program from Javascript on EC2
I tried to digitize the stamp stamped on paper using OpenCV
I tried to make a periodical process with Selenium and Python
[Ev3dev] Create a program that captures the LCD (screen) using python
I tried to register a station on the IoT platform "Rimotte"
I tried to make a 2channel post notification application with Python
[4th] I tried to make a certain authenticator-like tool with python
[Python] Simple Japanese ⇒ I tried to make an English translation tool
[1st] I tried to make a certain authenticator-like tool with python
Python standard module that can be used on the command line
[Python] A program that rotates the contents of the list to the left
Create a command line tool to convert dollars to yen using Python
I tried to make a site that makes it easy to see the update information of Azure
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
A story that didn't work when I tried to log in with the Python requests module
I tried to build a SATA software RAID configuration that boots the OS on Ubuntu Server
I tried to make a Web API
A program that plays rock-paper-scissors using Python
[Python] A program that rounds the score
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to implement a blockchain that actually works with about 170 lines
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[Python] A program that calculates the number of socks to be paired
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)