Sayth Renshaw wrote: > Hi > > I am struggling to figure out how I can create a generator to provide > values to my url. My url needs to insert the year month and day in the url > not as params to the url. > > > import json > import requests > import datetime > > # using this I can create a list of dates for the first 210 days of this > # year. > > base = datetime.datetime(2017,1,1) > > def datesRange(max, min): > date_list = (base - datetime.timedelta(days=x) for x in > range(max,min)) DAY = date_list.day > MONTH = date_list.month > YEAR = date_list.year > yield DAY, MONTH, YEAR
A single yield usually doesn't make sense -- you need one loop to generate the data ONE_DAY = datetime.timedelta(days=1) def dates(first, numdays): # generate datetime objects for extra clarity # note there are no implicit arguments like `base` in your code for _ in range(numdays): yield first first += ONE_DAY ... and another one to consume it def create_url(date): return "https:/example.com/{0.year}/{0.month}/{0.day}/".format( date ) def create_filename(date): # use fixed widths for month and day to avoid ambiguous # filenames, e. g. is "2017111.json" jan-11 or nov-1? return "{0.year}{0.month:02}{0.day:02}.json".format(date) FIRST = datetime.datetime(2017, 1, 1) for date in dates(FIRST, numdays=210): url = create_url(date) r = requests.get(url) filename = create_filename(date) with open(filename, "w") as f: ... > dateValues = datesRange(-210,0) > > def create_url(day, month, year): > https://api.tatts.com/sales/vmax/web/data/racing/{0}/{1}/{2}/sr/full".format(YEAR,MONTH,DAY) > return url > > Then I want to insert them in this url one at a time from the generator > > try: > r = requests.get(INSERT_URL_HERE) > if r.status_code == requests.codes.ok then: > # open a file for writing using url paramters > with open(SR + DAY + MONTH + YEAR + '.json', 'w') as f: > # Do stuff from here not relevant to question. > > I have just gotten lost. > > Is there an easier way to go about this? > > Cheers > > Sayth -- https://mail.python.org/mailman/listinfo/python-list