Note: User is to respond with YYYY-MM-DD format in Python: Import argparse. Saved this value to "data" and "post(data)".
SetUp:
Import requests, json, dotenv, sys, os, argparse, datetime, calendar from datetime import datetime from time import strptime
User feeds these line of Code:
def get_args()
parser = argparse.ArgumentParser('Description')
parser.add_argument('-se', '--startdate', help='The Start Date - format YYYY-MM-DD', required=True)
return args
arg = get_args()
print(arg)
data['short_description'] = args.os + ' ' + args.environment \
+ ' Patching - ' + name_month + ' ' + args.startdate
Terminal: Namespace(enddate='2020-12-30', environment='dev', os='windows', startdate='2020-12-01')
Goal:
- Take YYYY-MM-DD.
- save
- YYYY = variable
- MM = variable
- convert "MM: 03" to a February depending on user input.
feed to a JSON object and Post().
Additionally:
print(type(arg))
Terminal: <class 'argparse.Namespace'>
Please share your code
What I tried that didn't work:
#print(str(d.month))
#print(calendar.month_name[])
#print(type(arg))
#print(arg[0])
#z = arg.startdate
#y=datetime(z).strptime.month(date_time_str, '%d/%m/%y %H:%M:%S',z)
#y.strftime("%B")
#print(y)
#print(type(arg.startdate))
#print(strftime(arg.startdate))
#arg= argparse.Namespace()
#d=vars(arg)
#print(d)
#print(arg.startdate.strftime(%b))
#Terminal : <class 'argpasrse.Namespace'>
Blockers: args is a <class 'argparse.Namespace> I tried converting the output to string but not sure what is the best approach. Currently Fix (not a good option) :
def month_string_to_number(s):
m = {
'01': "January",
'02': "Febuary",
'03': "March",
'04': 'April',
'05': 'May',
'06': 'June',
'07': 'July',
'08': 'August',
'09': 'September',
'10': 'October',
'11': 'November',
'12': 'December',
}
s = arg.startdate
d = datetime.strptime(s,'%Y-%m-%d')
month = str(d.month)
name_month=m[month]
return name_month
NOTE: Need to convert user input date received from ArgParse saved in the arg=arg.startdate
. This value needs to convert to a month and year variables. So need to split YYYY-MM-DD to the corresponding month the user declares under parser.add_argument(..startdate..)
and use it in my code elsewhere.