Re: How to initialize a table of months.
Hi (bis) A class way : class cmon(object): Jan=1 Feb=2 Fev=2 Mar=3 Apr=4 Avr=4 May=5 Mai=5 Jun=6 Jui=6 Juin=6 Jul=7 Juil=7 Aug=8 Aou=8 Sep=9 Oct=10 Nov=11 Dec=12 print cmon.Mar print cmon.Sep print cmon.Dec -- @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
Hi! Not best, but another lisibility : mons=dict(Jan=1, Feb=2, Fev=2, Mar=3, Apr=4, Avr=4, May=5, Mai=5, Jun=6, Jui=6, Jul=7, Aug=8, Aou=8, Sep=9, Oct=10, Nov=11, Dec=12) def mon2int(m): return mons[m] def mond2int(**m): return mons[m.keys()[0]] print mons['Mar'] print mon2int('May') print mond2int(Jul=0) > 3 > 5 > 7 (The dict is mixed : French/English) -- @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
On Apr 16, 1:14 pm, "Paul McGuire" <[EMAIL PROTECTED]> wrote: > On Apr 15, 10:33 pm, "7stud" <[EMAIL PROTECTED]> wrote: > > > > > On Apr 15, 9:30 pm, "7stud" <[EMAIL PROTECTED]> wrote: > > > > On Apr 15, 7:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > > > Arrgh. > > > import calendar > > > months = calendar.month_abbr > > #returns an array with the 0 element empty > > #so the month names line up with the indexes 1-12 > > > d = {} > > for i in range(1, 13): > > d[months[i]] = i > > > print d > > This dict construction idiom is worth learning: > d = dict( (a,b) for a,b in ... some kind of list comprehension or > generator expr... ) > > In this case: > d = dict( (mon,i) for i,mon in enumerate(calendar.month_abbr) ) > > Or to avoid including that pesky 0'th blank element: > d = dict( [(mon,i) for i,mon in enumerate(calendar.month_abbr)][1:] ) > > -- Paul Great! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
In <[EMAIL PROTECTED]>, Steven W. Orr wrote: > I want to call datetime.datetim() whose arg2 is a number between 1-12 so I > have to convert the month to an integer. > I wrote this, but I have a sneaky suspicion there's a better way to do it. > > mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, > 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 } > > def mon2int( mon ): > global mons > return mons[mon] You've already got some answers, I just want to point out that the ``global`` is unnecessary here and that `mons` as a constant should be spelled in capital letters by convention. And maybe it's better to write `MONTHS` instead the abbreviation. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
In article <[EMAIL PROTECTED]>, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > I'm reading a logfile with a timestamp at the begging of each line, e.g., > > Mar 29 08:29:00 > > I want to call datetime.datetim() whose arg2 is a number between 1-12 so I > have to convert the month to an integer. > I wrote this, but I have a sneaky suspicion there's a better way to do it. > > mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, > 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 } > > def mon2int( mon ): > global mons > return mons[mon] > > Is there a generator expression or a list comprehension thingy that would > be *betterer*? (I realize it's probably not that important but I find lots > of value in learning all the idioms.) There's no harm in your method as written, though it were probably wise to tolerate variations in capitalization. But if you want to be cute about it, you could write something like this to set up your table: from datetime import date months = dict((date(1900, x+1, 1).strftime('%b').lower(), x+1) for x in xrange(12)) def month2int(mName): return months[mName.lower()] If you don't like the lookup table, you can get a nicely portable result without it by using time.strptime(), e.g., import time def month2int(mName): return time.strptime(mName, '%b').tm_mon # parses "short" names Without knowing anything further about your needs, I would probably suggest the latter simply because it makes the hard work be somebody else's problem. Cheers, -M -- Michael J. Fromberger | Lecturer, Dept. of Computer Science http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
On Apr 15, 10:33 pm, "7stud" <[EMAIL PROTECTED]> wrote: > On Apr 15, 9:30 pm, "7stud" <[EMAIL PROTECTED]> wrote: > > > On Apr 15, 7:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > > Arrgh. > > import calendar > > months = calendar.month_abbr > #returns an array with the 0 element empty > #so the month names line up with the indexes 1-12 > > d = {} > for i in range(1, 13): > d[months[i]] = i > > print d This dict construction idiom is worth learning: d = dict( (a,b) for a,b in ... some kind of list comprehension or generator expr... ) In this case: d = dict( (mon,i) for i,mon in enumerate(calendar.month_abbr) ) Or to avoid including that pesky 0'th blank element: d = dict( [(mon,i) for i,mon in enumerate(calendar.month_abbr)][1:] ) -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
On Apr 15, 9:30 pm, "7stud" <[EMAIL PROTECTED]> wrote: > On Apr 15, 7:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: Arrgh. import calendar months = calendar.month_abbr #returns an array with the 0 element empty #so the month names line up with the indexes 1-12 d = {} for i in range(1, 13): d[months[i]] = i print d -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
On Apr 15, 7:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > I'm reading a logfile with a timestamp at the begging of each line, e.g., > > Mar 29 08:29:00 > > I want to call datetime.datetim() whose arg2 is a number between 1-12 so I > have to convert the month to an integer. > I wrote this, but I have a sneaky suspicion there's a better way to do it. > > mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, > 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 } > > def mon2int( mon ): > global mons > return mons[mon] > > Is there a generator expression or a list comprehension thingy that would > be *betterer*? (I realize it's probably not that important but I find lots > of value in learning all the idioms.) > > TIA > > -- > Time flies like the wind. Fruit flies like a banana. Stranger things have .0. > happened but none stranger than this. Does your driver's license say Organ ..0 > Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 > individuals! What if this weren't a hypothetical question? > steveo at syslang.net Just in case you're still interested(despite not needing to per John Zenger's solution), you could do this: import calendar months = calendar.month_abbr #returns an array with the 0 element empty #so the month names correspond to indexes 1-12 d = {} for i in range(1, 13): d[months[i]] = i print d -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize a table of months.
On Apr 15, 9:30 pm, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > I'm reading a logfile with a timestamp at the begging of each line, e.g., > > Mar 29 08:29:00 > > I want to call datetime.datetim() whose arg2 is a number between 1-12 so I > have to convert the month to an integer. > I wrote this, but I have a sneaky suspicion there's a better way to do it. > > mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, > 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 } > > def mon2int( mon ): > global mons > return mons[mon] > > Is there a generator expression or a list comprehension thingy that would > be *betterer*? (I realize it's probably not that important but I find lots > of value in learning all the idioms.) > > TIA Well, I think you want time.strptime. >>> time.strptime("Mar 29 08:29:00", "%b %d %H:%M:%S") (1900, 3, 29, 8, 29, 0, 3, 88, -1) See http://docs.python.org/lib/node85.html However, if strptime did not exist, your dictionary solution is fine -- a tad bit slow, but easy and maintainable, which is worth a lot. -- http://mail.python.org/mailman/listinfo/python-list
How to initialize a table of months.
I'm reading a logfile with a timestamp at the begging of each line, e.g., Mar 29 08:29:00 I want to call datetime.datetim() whose arg2 is a number between 1-12 so I have to convert the month to an integer. I wrote this, but I have a sneaky suspicion there's a better way to do it. mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 } def mon2int( mon ): global mons return mons[mon] Is there a generator expression or a list comprehension thingy that would be *betterer*? (I realize it's probably not that important but I find lots of value in learning all the idioms.) TIA -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net -- http://mail.python.org/mailman/listinfo/python-list