using datetime containers

John Machin sjmachin at lexicon.net
Sat Nov 8 07:39:35 EST 2008


On Nov 8, 6:06 pm, indika <indikabandar... at gmail.com> wrote:
> Hi,
> I'm a newbie to python but have some experience in programming.

So work through the Python tutorial, to find out how it all hangs
together ... this will be much better than trying to translate
snippets of language X into Python.


> I came across this requirement of using datetime.date objects
> associated with some another object.
> eg. a dictionary containing datetime.date => string
>
> {
> datetime.date(2001, 12, 3): 'c',
> datetime.date(2001, 12, 1): 'a',
> datetime.date(2001, 12, 2): 'b'
>
> }
>
> However, the sorting of the dict is not user configurable.

So don't use a dict.

>  The
> desired behavior would be to provide a datetime.date comparison
> function to do the sorting(eg. STL map). This may seem a trivial
> question but I couldn't figure out a way.

datetime.date objects (like almost all objects) already have
comparison methods built-in. What you need is code to use them. For
applications like "what was the interest rate on date x" or "what are
the slope and intercept of a piecewise-linearly-continuous tax table
for a taxable income of x":

Have two parallel lists, keys and values, in keys order. Use a
function from the bisect module to find (e.g.) the largest i such that
keys[i] <= x. If such an i exists, your answer is values[i].

> Or else, I would have expected the datatime.date object has a
> writeable data member, so that iterating a calender with
> itermonthdates would allow me to access that data member.

Sorry, I can't begin to guess what you mean by that.



More information about the Python-list mailing list