How to iterate through a dictionary of tuples?

Viewed 377

I have the following tuples of month,day stored in the dictionary seasons:

seasons = {
    'S1': (
        ((1, 10),(5, 31))
    ),
    'S2': (
        ((9, 1),(1, 9))
    ),
    'S3': (
        ((6, 1),(9, 30))
    )
}

I want to check in which date interval a datetime dt is located and assign the name S1, S2 or S3, accordingly.

I tried to do it this way, but start and end seem to be numbers instead of tuples.

    def getSeason(dt):
        season = None    
        for t, ranges in seasons.items():
            for start, end in ranges:
                if date(dt.year,start(0),start(1)) <= dt.date() <= date(dt.year,end(0),end(1)):
                    season = t
                    break
            if season is not None:
                break
        return season
3 Answers
Related