1

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

1
  • Assumes the value input by the user will be in the format '%Y-%m-%d', based on, User is to respond with YYYY-MM-DD format in Python
  • args will look like Namespace(date=datetime.datetime(2020, 7, 30, 0, 0))
    • Use args.date to get 2020-07-30 00:00:00
  • As mentioned by MrFuppes in a comment, dt.strftime('%B') is a better option than importing the calendar module.
from datetime import datetime
import argparse
# import calendar

parser = argparse.ArgumentParser()
parser.add_argument('date', type=lambda d: datetime.strptime(d, '%Y-%m-%d'))
args = parser.parse_args()

# get date from args
dt = args.date

# get the year
year = dt.year

# get the month
month = dt.month

# get the day
day = dt.day

# get the month name
# mon_name = calendar.month_name[month]
mon_name = dt.strftime('%B')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158