Re: Time travel - how to simplify?

2017-11-18 Thread Peter Otten
Andrew Z wrote:

> well, yeah, it's unidirectional and final destination is always the same
> and have little to do with the question.
> 
> Say, i have a dict:
> 
> 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'
>   }
> 
> where key is a month.
> 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..
> 
> And as such - the question is - what is the smart way to deal with cases
> like this?

Make a lookup list that is big enough for your application and then use 
slicing:

>>> def months(first, count, lookup=list("FGHJKMNQUVXZ" * 3)):
... start = first - 1
... return lookup[start: start + count]
... 
>>> months(3, 3)
['H', 'J', 'K']
>>> months(12, 3)
['Z', 'F', 'G']


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Time travel - how to simplify?

2017-11-18 Thread Andrew Z
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 
wrote:

> On Tuesday, November 14, 2017 at 1:44:17 PM UTC-6, Ben Finney wrote:
> > Andrew Z  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
> >  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
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Time travel - how to simplify?

2017-11-14 Thread Rick Johnson
On Tuesday, November 14, 2017 at 1:44:17 PM UTC-6, Ben Finney wrote:
> Andrew Z  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
>  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


Re: Time travel - how to simplify?

2017-11-14 Thread Ben Finney
Ben Finney  writes:

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

Sorry, I now realise that won't do what you want; the wrap around from
12 to 1 will cause ‘this_month >= month_choice’ to be false.

Well, I won't correct it; maybe this is a useful exercise. Do you have a
way to fix that so it will work?

-- 
 \  “I am too firm in my consciousness of the marvelous to be ever |
  `\   fascinated by the mere supernatural …” —Joseph Conrad, _The |
_o__) Shadow-Line_ |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Time travel - how to simplify?

2017-11-14 Thread Ben Finney
Andrew Z  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
 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)

-- 
 \  “God forbid that any book should be banned. The practice is as |
  `\  indefensible as infanticide.” —Dame Rebecca West |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Time travel - how to simplify?

2017-11-13 Thread Andrew Z
well, yeah, it's unidirectional and final destination is always the same
and have little to do with the question.

Say, i have a dict:

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

where key is a month.
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..

And as such - the question is - what is the smart way to deal with cases
like this?

Thank you
AZ
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Time travel - how to simplify?

2017-11-13 Thread Andrew Z
My implied solution is incorrect.


I should start with using the date type and, for example, dateutil package
for date manipulation and building the dictionary of needed dates/months.
And only after that, map against the fut_suffix.


On Tue, Nov 14, 2017 at 12:56 AM, Andrew Z  wrote:

> well, yeah, it's unidirectional and final destination is always the same
> and have little to do with the question.
>
> Say, i have a dict:
>
> 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'
>   }
>
> where key is a month.
> 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..
>
> And as such - the question is - what is the smart way to deal with cases
> like this?
>
> Thank you
> AZ
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list