By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
9 Replies


P: n/a
On Apr 15, 9:30 pm, "Steven W. Orr" <ste...@syslang.netwrote:
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.

Apr 16 '07 #2

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

P: n/a
On Apr 15, 9:30 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 15, 7:30 pm, "Steven W. Orr" <ste...@syslang.netwrote:
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

Apr 16 '07 #4

P: n/a
On Apr 15, 10:33 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 15, 9:30 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 15, 7:30 pm, "Steven W. Orr" <ste...@syslang.netwrote:

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
Apr 16 '07 #5

P: n/a
In article <ma***************************************@python. org>,
"Steven W. Orr" <st****@syslang.netwrote:
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
Apr 16 '07 #6

P: n/a
In <ma***************************************@python. org>, 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...@austin.rr.comwrote:
On Apr 15, 10:33 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 15, 9:30 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
On Apr 15, 7:30 pm, "Steven W. Orr" <ste...@syslang.netwrote:
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!

Apr 16 '07 #8

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.