426,083 Members | 1,608 Online
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

# How to initialize a table of months.

 P: n/a 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 Apr 16 '07 #1
9 Replies

 P: n/a On Apr 15, 9:30 pm, "Steven W. Orr" >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. Apr 16 '07 #2

 P: n/a On Apr 15, 7:30 pm, "Steven W. Orr"

 P: n/a On Apr 15, 9:30 pm, "7stud"

 P: n/a On Apr 15, 10:33 pm, "7stud"

 P: n/a In article , "Steven W. Orr"

 P: n/a In , 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 Apr 16 '07 #7

 P: n/a On Apr 16, 1:14 pm, "Paul McGuire"

 P: n/a 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 Apr 16 '07 #9

 P: n/a 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 Apr 16 '07 #10

### This discussion thread is closed

Replies have been disabled for this discussion.