osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generator - Provide parameters to url - requests


"Sayth Renshaw"  wrote in message 
news:328f42bb-ba23-4199-9f3a-9ec1829bc448 at googlegroups.com...
>
> 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
>
> dateValues = datesRange(-210,0)

Are you sure that this works?

The easiest way to test it is -

>>> list(datesRange(-210, 0))

If I try this I get AttributeError: 'generator object has no attribute 'day'

I would write it like this -
  def datesRange(max, min):
     for day in range(max, min):
        date = base - datetime.timedelta(days=day)
        yield date.day, date.month, date.year

Actually, I have just read Peter's response, and his version is much better, 
but this one is closer to your original code.

>
> 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
>

To do this, you need some kind of loop to iterate over your generator -

    for day, month, year in datesRange(-210, 0):
          # do something

Does this help?

Frank Millman