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.