0

I was reading a similar question Returning error string from a function in python. While I experimenting to create something similar in an Object Oriented programming so I could learn a few more things I got lost.

I am using Python 2.7 and I am a beginner on Object Oriented programming.

I can not figure out how to make it work.

Sample code checkArgumentInput.py:

#!/usr/bin/python

__author__ = 'author'


class Error(Exception):
    """Base class for exceptions in this module."""
    pass


class ArgumentValidationError(Error):
    pass

    def __init__(self, arguments):
        self.arguments = arguments

    def print_method(self, input_arguments):
        if len(input_arguments) != 3:
            raise ArgumentValidationError("Error on argument input!")
        else:
            self.arguments = input_arguments
            return self.arguments

And on the main.py script:

#!/usr/bin/python
import checkArgumentInput

__author__ = 'author'


argsValidation = checkArgumentInput.ArgumentValidationError(sys.argv)

if __name__ == '__main__':

    try:
        result = argsValidation.validate_argument_input(sys.argv)
        print result
    except checkArgumentInput.ArgumentValidationError as exception:
        # handle exception here and get error message
        print exception.message

When I am executing the main.py script it produces two blank lines. Even if I do not provide any arguments as input or even if I do provide argument(s) input.

So my question is how to make it work?

I know that there is a module that can do that work for me, by checking argument input argparse but I want to implement something that I could use in other cases also (try, except).

Thank you in advance for the time and effort reading and replying to my question.

Community
  • 1
  • 1
Thanos
  • 1,618
  • 4
  • 28
  • 49

2 Answers2

0

OK. So, usually the function sys.argv[] is called with brackets in the end of it, and with a number between the brackets, like: sys.argv[1]. This function will read your command line input. Exp.: sys.argv[0] is the name of the file.

main.py 42

In this case main.py is sys.argv[0] and 42 is sys.argv[1].

You need to identifi the string you're gonna take from the command line. I think that this is the problem.

For more info: https://docs.python.org/2/library/sys.html

  • Hello TheShadlest, I will get back to you on this one later on. Unfortunately I do not have access on the script right now. Again thank you for your time and effort reading and replying to my question. – Thanos Dec 07 '15 at 10:26
  • Hello again TheShadlest, apologies for so late reply but I got busy with something else. Unfortunately your answer is not correct. Well you are right about the sys.argv[0] and sys.argv[1] but my code had many errors. I spend some time and I have found the solution. I will be posting the answer to my question actually now, so someone in future might benefit from that. – Thanos Dec 08 '15 at 23:18
  • 1
    I actually will benefit from it, thanks. I say, will help me to help others in the future. Thanks for replying. – TheShadlest Dec 08 '15 at 23:20
0

I made some research and I found this useful question/ answer that helped me out to understand my error: Manually raising (throwing) an exception in Python

I am posting the correct functional code under, just in case that someone will benefit in future.

Sample code checkArgumentInput.py:

#!/usr/bin/python

__author__ = 'author'


class ArgumentLookupError(LookupError):
    pass

    def __init__(self, *args): # *args because I do not know the number of args (input from terminal)
        self.output = None
        self.argument_list = args

    def validate_argument_input(self, argument_input_list):
        if len(argument_input_list) != 3:
            raise ValueError('Error on argument input!')
        else:
            self.output = "Success"
            return self.output

The second part main.py:

#!/usr/bin/python
import sys
import checkArgumentInput

__author__ = 'author'

argsValidation = checkArgumentInput.ArgumentLookupError(sys.argv)

if __name__ == '__main__':

    try:
        result = argsValidation.validate_argument_input(sys.argv)
        print result
    except ValueError as exception:
        # handle exception here and get error message
        print exception.message

The following code prints: Error on argument input! as expected, because I violating the condition.

Any way thank you all for your time and effort, hope this answer will help someone else in future.

Community
  • 1
  • 1
Thanos
  • 1,618
  • 4
  • 28
  • 49