Python Forum
Accessing varying command line arguements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing varying command line arguements
#1
I am using a java jar in Pycharm environment to achieve testing in a pythion environment.
To make the application more interactive, we plan to use commandline arguments.

Eg:

python test.py opc.tcp://localhost:XXXX AA
So here, I am testing a AA configuration server at the mentioned URL. Now with this input, all the tests are performed.

The code used is as follows.

class TestClient:
    def __init__(self, URL, Config):
        self.Url = URL
        self.config = Config

    def perform_test(self):
        # subprocess.call(['java','-version'])

        subprocess.call(
            ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion',
             '2.0'])  

    def read_results(self):
        pass

if __name__ == "__main__":
    client = TestClient("opc.tcp://localhost:XXXX", "AA")
    client.perform_test()
    client.read_results()
But to enhance further I want the user to type the version number and also the test he wants to do. As in (TEST A) as a parameter. But I also need to give a provision that if he does not provide this paramter, he should be able to perform all tests like previously.

So I have modified the code this way considering overloading.


import re
import sys

import bs4 as bs4

inputURL = sys.argv[1]
inputConfig = sys.argv[2]
version = sys.argv[3]
print(len(sys.argv))
if len(sys.argv) > 4:
    typeOfTest = sys.argv[4]


class TestClient:
    def __init__(self, URL, Config, Version):
        self.Url = URL
        self.config = Config
        self.version = Version

    def __init__(self, URL, Config, Version, TestType):
          self.testType = TestType #here i expect overloading of constructor

    def perform_test(self):
        # subprocess.call(['java','-version'])
        if len(sys.argv) > 4:
            subprocess.call(
                ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion',
                 self.version])
        else:
            subprocess.call(
                ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion',
                 self.version, '-test', self.testType])

    def read_results(self):
        pass


if __name__ == "__main__":
    if len(sys.argv) > 4:
        client = TestClient(inputURL, inputConfig, version)
    else:
        client = TestClient(inputURL, inputConfig, version, typeOfTest)
    client.perform_test()
    client.read_results()
So, I type on the command line :

python test.py opc.tcp://localhost:XXXX AA 2.0 TESTA
But i get an error saying:

Error:
client = TestClient(inputURL, inputConfig, version) TypeError: __init__() missing 1 required positional argument: 'TestType'
Is the constructor overloading not poossible here? Please can anyone guide me some alternative?

UPDATE: just understood that funciton overloading is not possible in python. How to achieve this scenario working? any suggestions please
Reply
#2
Im just a hobbyist at this but my fix would be to use a default value for TestType parameter and test for it.

def __init__(self, URL, Config, Version, TestType=None):
    if TestType == None:
        ...
    else:
        ...
snippsat and Rakshan like this post
Reply
#3
Python method overloading doesn't work that way. The last __init__ is the only. The other is unreachable. But you can get something very similar to signature overloading using the dispatch decorator.
Reply
#4
Could do it like this,so with super() inherit all the methods and properties from its parent.
Then add another parameter in the __init__() function.
I guess also popejose way could work.
import re
import sys

import bs4 as bs4

inputURL = sys.argv[1]
inputConfig = sys.argv[2]
version = sys.argv[3]
print(len(sys.argv))
if len(sys.argv) > 4:
    typeOfTest = sys.argv[4]

class Client:
    def __init__(self, URL, Config, Version):
        self.Url = URL
        self.config = Config
        self.version = Version

    def perform_test(self):
        # subprocess.call(['java','-version'])
        if len(sys.argv) > 4:
            subprocess.call(
                ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion',
                 self.version])
        else:
            subprocess.call(
                ['java', '-jar', 'XXX.jar', '-url', self.Url, '-config', self.config, '-specVersion',
                 self.version, '-test', self.testType])

    def read_results(self):
        pass

class TestClient(Client):
    def __init__(self, URL, Config, Version, TestType):
        super().__init__(URL, Config, Version)
        self.testType = TestType

if __name__ == "__main__":
    if len(sys.argv) > 4:
        client = Client(inputURL, inputConfig, version)
    else:
        client = TestClient(inputURL, inputConfig, version, typeOfTest)
        client.perform_test()
        client.read_results()
Rakshan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to input & output parameters from command line argument shantanu97 1 706 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Passing List of Objects in Command Line Python usman 7 1,119 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create a varying qty of instances of a Class and name them? KM007 2 866 May-29-2020, 12:30 PM
Last Post: KM007
  class arguements astral_travel 7 1,053 Apr-29-2020, 04:30 PM
Last Post: buran
  Taking Multiple Command Line Argument Input bwdu 6 1,617 Mar-29-2020, 05:52 PM
Last Post: buran
  python 3 from command line Dixon 1 789 Mar-01-2020, 08:35 PM
Last Post: snippsat
  Running linux command line apps... dbrdh 0 761 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  command line input (arg parse) and data exchange Simba 7 1,960 Dec-06-2019, 11:58 PM
Last Post: Simba
  code works at command line but not in .py ridgerunnersjw 4 1,094 Sep-12-2019, 11:09 PM
Last Post: ridgerunnersjw
  changing the process command line arguments in python Skaperen 3 1,386 Aug-19-2019, 02:40 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020