How to initialize a table of months.

S

Steven W. Orr

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
 
J

John Zenger

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.
(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.
 
7

7stud

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

print d
 
7

7stud

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

print d
 
D

Dennis Lee Bieber

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.)
Having been influenced by languages without dictionaries or
equivalent...

mn = (
"jan feb mar apr may jun jul aug sep oct nov dec ".find(mon.lower())
/ 4)
#+1 if numbering 1..12, not 0..11

or maybe

mn = ["jan", "feb", "mar", "apr", ..., "dec"].index(mon.lower())
#same +1 comment


Though using the date/time parsing/formatting functions in the
libraries is probably best...

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Paul McGuire

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

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
 
M

Michael J. Fromberger

"Steven W. Orr said:
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
 
M

Marc 'BlackJack' Rintsch

Steven W. Orr said:
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
 
J

Jun.Jin.act+group.python

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


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!
 
M

Michel Claveau

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

Michel Claveau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top