Time travel - how to simplify?

Andrew Z formisc at gmail.com
Sat Nov 18 16:02:57 EST 2017


Thank you for your input, gentlemen.

I'm thinking about the following approach:

import datetime
from dateutil import relativedelta

fut_suffix ={ 1 : 'F',
              2 : 'G',
              3 : 'H',
              4 : 'J',
              5 : 'K',
              6 : 'M',
              7 : 'N',
              8 : 'Q',
              9 : 'U',
              10: 'V',
              11: 'X',
              12: 'Z'
              }

endDate = datetime.date.today()
startDate = endDate + relativedelta.relativedelta(months = -6,day = 1)

LocalSmlSuffix = [ suffix for mnth, suffix in fut_suffix.items() if
mnth == startDate.month]


On Tue, Nov 14, 2017 at 5:01 PM, Rick Johnson <rantingrickjohnson at gmail.com>
wrote:

> On Tuesday, November 14, 2017 at 1:44:17 PM UTC-6, Ben Finney wrote:
> > Andrew Z <formisc at gmail.com> writes:
> >
> > > Now i want to get certain number of months. Say, i need  3 months
> duration
> > > starting from any month in dict.
> > >
> > > so if i start @ 7th:
> > > my_choice =7
> > >  for mnth, value in fut_suffix:
> > >     if my_choice >= mnth
> > >        # life is great
> > > but if :
> > > my_choice = 12 then my "time travel" becomes pain in the neck..
> >
> > The ‘itertools’ library in the Python standard library
> > <URL:https://docs.python.org/3/library/itertools.html> has what you
> > need:
> >
> >     import itertools
> >
> >     month_values = sorted(list(fut_suffix.keys()))
> >     month_cycle = itertools.cycle(month_values)
> >
> >     month_choice = 7
> >     three_months_starting_at_choice = []
> >     while len(three_months_starting_at_choice) < 3:
> >         this_month = next(month_cycle)
> >         if this_month >= month_choice:
> >             three_months_starting_at_choice.append(this_month)
>
> Ben's advice is spot on[1], and exactly what i would do, but
> i believe it's important for you to discover how to
> implement a simple cycling algorithm yourself, _before_
> reaching for the pre-packaged junkfood in the itertools
> module. Can you do it? If not, then do as much as you can
> and then ask for help. Hey, you're only hurting yourself if
> you don't learn how. And it's surprisingly easy!
>
> [1] and it looks as though he included the kitchen sink,
> washcloths, dish soap, and some fine china as well! ಠ_ಠ
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list