-1

I am a newbie to Python and can't wrap my head around this one. I have a python script which takes in 2 arguments from user. Below is the code -

import datetime
from datetime import timedelta
import time
import csv
import collections
import argparse

MyVar1 = range(1,11)

parser = argparse.ArgumentParser()
parser.add_argument('Hrs', help='Enter the hours', type=int)
parser.add_argument('Mins', help='Enter the minutes', type=int)
parser.parse_args()

class TimeTesting:
    def __init__(self, Hours, Minutes):
        self.Hours = Hours
        self.Minutes = Minutes

    def TimeTest1(self):
        self.T1 = datetime.datetime.now()
        time.sleep(1)
        StopTime = self.T1 + timedelta(hours=int(self.Hours)) + timedelta(minutes=int(self.Minutes))
        print('{}'.format(StopTime.strftime('%Y/%m/%d %H:%M:%S')))

    def TimeTest2(self):
        self.T2 = datetime.datetime.now()
        self.TimeDiff = self.T2 - self.T1
        print('Timetest2 function called')


ob = TimeTesting('Hrs', 'Mins')

for item in MyVar1:
    ob.TimeTest1()
    ob.TimeTest2()

This is how I am calling the function from command line.

cmd> MyScript.py 1 30

Below is the error which I am getting.

Traceback (most recent call last): File "C:\Users\MyScript.py", line 37, in ob.TimeTest1() File "C:\Users\MyScript.py", line 25, in TimeTest1 StopTime = self.T1 + timedelta(hours=int(self.Hours)) + timedelta(minutes=int(self.Minutes)) ValueError: invalid literal for int() with base 10: 'Hrs'

I am not sure if I am passing the arguments to the class object ob correctly. I have looked here but could not find any solution to my problem.

Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
  • 1
    "Hrs" is a string, you need to pass in values for the timespan i.e `1` and `30` – Sayse Mar 23 '21 at 12:41
  • More specifically, you need to [do something with the args](https://docs.python.org/3/library/argparse.html) when you receive them – Sayse Mar 23 '21 at 12:42
  • @Sayse - I am doing that while calling the script from command line. `MyScript.py 1 30` – Vivek Kumar Singh Mar 23 '21 at 12:42
  • 1
    Right, but then you dont do anything with what the user gives you, the `Hrs` and `"Mins"` you provide to creating an instance of your class are the literal strings, not the users input – Sayse Mar 23 '21 at 12:44
  • @Sayse - You are right. I totally didn't realise that until now. – Vivek Kumar Singh Mar 23 '21 at 12:45

3 Answers3

1

The line parser.parse_args() doesn't really do anything (well, it does something, but it has no effect). You want to actually save those parsed arguments, like:

args = parser.parse_args()

and then pass the actual arguments from the command-line instead of literal strings:

ob = TimeTesting(args.Hrs, args.Mins)

Also note that those arguments are already defined with type=int, so the int() conversion inside the class is not necessary.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

Change to next two lines:

args = parser.parse_args()
....
ob = TimeTesting(args.Hrs, args.Mins)

Full working code:

Try it online!

import datetime
from datetime import timedelta
import time
import csv
import collections
import argparse

MyVar1 = range(1,11)

parser = argparse.ArgumentParser()
parser.add_argument('Hrs', help='Enter the hours', type=int)
parser.add_argument('Mins', help='Enter the minutes', type=int)
args = parser.parse_args()

class TimeTesting:
    def __init__(self, Hours, Minutes):
        self.Hours = Hours
        self.Minutes = Minutes

    def TimeTest1(self):
        self.T1 = datetime.datetime.now()
        time.sleep(1)
        StopTime = self.T1 + timedelta(hours=int(self.Hours)) + timedelta(minutes=int(self.Minutes))
        print('{}'.format(StopTime.strftime('%Y/%m/%d %H:%M:%S')))

    def TimeTest2(self):
        self.T2 = datetime.datetime.now()
        self.TimeDiff = self.T2 - self.T1
        print('Timetest2 function called')


ob = TimeTesting(args.Hrs, args.Mins)

for item in MyVar1:
    ob.TimeTest1()
    ob.TimeTest2()

Input:

python test.py 1 30

Output:

2021/03/23 17:15:36
Timetest2 function called
2021/03/23 17:15:37
Timetest2 function called
2021/03/23 17:15:38
Timetest2 function called
2021/03/23 17:15:39
Timetest2 function called
2021/03/23 17:15:40
Timetest2 function called
2021/03/23 17:15:41
Timetest2 function called
Arty
  • 14,883
  • 6
  • 36
  • 69
0

In your code, you use

ob = TimeTesting('Hrs', 'Mins')

so the attribute self.Hours of the object ob will have the value 'Hrs' (a string), which cannot be converted to an int in the later part of the code int(self.Hours).

You probably need to pass differenct values when creating the object ob, or add some error checking before doing calculations.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • What you have mentioned in your answer makes sense and is probably correct. But unfortunately that's not the solution to my problem. Thank you for sharing your valuable inputs. – Vivek Kumar Singh Mar 23 '21 at 12:49