Skip to main content

CommandLine Args In Python

 Command line args in Python :


    When the program comes to Python , it's all about libraries .Argparser is the library used for the taking inputs from the command line. Here you can validate the arguments which are entered by the user without writing the another function to validate from the if conditions.

suppose give a filename like cmdprocess.py
You can give the expecting arguments like below


cmdprocess.py --help

    it will give the total arguments with respective commands to represent

Required =True :

          When the required is true. You have to enter the argument.
 

Type = function name:

         Type is very useful to validate the input entered by the user. so that you can send a message like user need to enter the defined arguments only. This function always expects the input argument .


Custom-Exceptions In Python:

   There are multiple exceptions which are already present in python like AssertionError, TypeError ...etc. But when you want implement your own Exceptions like whenever you see , you  can say this one happened because of the particular problem is occurred .

YOU Can inherit the Exception class on this you can write your own exceptions like below

class InvalidResolutionException(Exception):
    def __init__(self, code, message):
        self.code = code
        self.message = message


Total program:


    If you don't know the which arguments needs to be parsed ,then do the --help command it will display the no of arguments and how to pass reference below.

Execute process

Passing the arguments and resultant show the user entered arguments for different command inputs


    When the programmer is added the required is true in the add_argument statement and , the user didn't choose to enter the argument then it will show the error like that required argument is missing

reference below.




Comments

Popular posts from this blog

MQTT set up Flask

 MQTT Introduction: It is message broker which holds the data when ever a consumer will connected to the topic then he will receive the data . mqtt is a machine to machine message broker, mainly used in IOT application, the default port is 1883   Installation: sudo apt-get update sudo apt-get install mosquitto Command line execution: Producer mosquitto_pub -h 127.0.0.1 -t 'topic_name' -m '{'name':'Sony'}' Subscriber mosquitto_sub -h 127.0.0.1 -t 'topic_name' -h : it is the host address -t : topic name -m : message payload QOS in MQTT: QOS(quality of services) there are mainly 3 services are available in mqtt  qos=0 qos=1 qos=2 QOS (0,0): At most once service, publisher will send a message to broker at most once, the broker passes a message to subscriber one time     

Swagger set up for FLASK

 Flask     A Micro framework , where everything is customized, if we want to use restful api's we can add flask-restful library, or we wanted to go by rest plus we can  use rest plus library   After writing the all api's we need something to test  the api's and it should contain all the required parameters like schema , for these things we can use swagger  project structure should be add service __init__.py below code for swagger load app = Flask(__name__ , instance_relative_config = True ) CORS(app) # swagger specific SWAGGER_URL = '/swagger' API_URL = '/static/swagger.yml' SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint( SWAGGER_URL , API_URL , config ={ 'app_name' : "service Name" } ) add the blue print app.register_blueprint(SWGGER_BLUPRINT, url_prefix=SWAGGER_URL)   Start the service python app.py  on the default port 5000 it will load and endpoint /swagger 127.0.0.1:5000/swagger