jono jono - 1 month ago 4
Python Question

Storing dates to skip in python config file

In my config I have

SKIP_DATES = ['2016-02-11', '2016-02-13']


Then in my script I have

if dt.date() in config.SKIP_DATES:
print "Skipping date: {0}".format(dt.date())
continue


But this will not work because dt.date is a datetime.date object and config.SKIP_DATES is a list of 'string dates'.

How can I easily get around this?

Can anyone suggest a better name for this post?

Answer

Your dates seem to be in the same format as date.isoformat():

date.isoformat()
Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. 
For example, date(2002, 12, 4).isoformat() == '2002-12-04'.

So you could do:

if dt.date().isoformat() in config.SKIP_DATES:
    print "Skipping date: {0}".format(dt.date())
    continue

Note that the string representation of datetime.date uses the same function:

date.__str__()
For a date d, str(d) is equivalent to d.isoformat().

This is why print dt.date() gives output like '2016-10-31'. Of course, you could then use a set instead of a list for storing your skip dates.

Comments