using datetime containers

2008-11-07 Thread indika
Hi,
I'm a newbie to python but have some experience in programming.
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.  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.

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.

I would really appreciate if you would give me some pointers into
this.

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: using datetime containers

2008-11-08 Thread indika


John Machin wrote:
> On Nov 8, 6:06�pm, indika <[EMAIL PROTECTED]> 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].
>

thanks.

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

I was referring to something like this

eg. in an Image processing lib

struct Image
{
char* p_Data; // image data
int i_DataLen; // length of data
void* p_UserData; // user attaches whatever
}
If the lib user attaches some struct related to image name, file
location ... to p_UserData
whenever a Image* is passed around the user has access to those.

Similarly, if a datetime.date object had an attribute which the user
can access he could
d1 = datetime.date.(2008, 1, 1)
d1.UserData = x1 // hypothetical

d2 = datetime.date.(2008, 1, 2)
d2.UserData = x2 // hypothetical

mylist.append([d1, d2])

Hope i'm making some sense :-)




--
http://mail.python.org/mailman/listinfo/python-list


Re: using datetime containers

2008-11-08 Thread indika


Marc 'BlackJack' Rintsch wrote:
> On Sat, 08 Nov 2008 08:07:15 -0800, indika wrote:
>
> > John Machin wrote:
> >> On Nov 8, 6:06�pm, indika <[EMAIL PROTECTED]> wrote:
> >> > 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.
> >
> > I was referring to something like this
> >
> > eg. in an Image processing lib
> >
> > struct Image
> > {
> > char* p_Data; // image data
> > int i_DataLen; // length of data
> > void* p_UserData; // user attaches whatever }
> > If the lib user attaches some struct related to image name, file
> > location ... to p_UserData
> > whenever a Image* is passed around the user has access to those.
> >
> > Similarly, if a datetime.date object had an attribute which the user can
> > access he could
> > d1 = datetime.date.(2008, 1, 1)
> > d1.UserData = x1 // hypothetical
> >
> > d2 = datetime.date.(2008, 1, 2)
> > d2.UserData = x2 // hypothetical
> >
> > mylist.append([d1, d2])
> >
> > Hope i'm making some sense :-)
>
> You can subclass `datetime.date` and attach whatever attributes you
> want.  Be sure to overwrite `__new__()` because `datetime.date` objects
> are immutable.
>
> Ciao,
>   Marc 'BlackJack' Rintsch


Thanks.

As I read somewhere python has almost everything you need. So I
wouldn't go to subclassing existing stuff and making life harder for
me.

Eventhough it may be costly to sort after adding all items to the
dict(as opposed to inserting with a custom sort function) I would go
for that.

Anyway, I saw the UserDict module which may help in creating a custom
dictionary with a custom comparison function. (I didn't go to detail
as the documentation was not very elaborate )

--
http://mail.python.org/mailman/listinfo/python-list


Re: using datetime containers

2008-11-08 Thread indika


indika wrote:
> Marc 'BlackJack' Rintsch wrote:
> > On Sat, 08 Nov 2008 08:07:15 -0800, indika wrote:
> >
> > > John Machin wrote:
> > >> On Nov 8, 6:06�pm, indika <[EMAIL PROTECTED]> wrote:
> > >> > 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.
> > >
> > > I was referring to something like this
> > >
> > > eg. in an Image processing lib
> > >
> > > struct Image
> > > {
> > > char* p_Data; // image data
> > > int i_DataLen; // length of data
> > > void* p_UserData; // user attaches whatever }
> > > If the lib user attaches some struct related to image name, file
> > > location ... to p_UserData
> > > whenever a Image* is passed around the user has access to those.
> > >
> > > Similarly, if a datetime.date object had an attribute which the user can
> > > access he could
> > > d1 = datetime.date.(2008, 1, 1)
> > > d1.UserData = x1 // hypothetical
> > >
> > > d2 = datetime.date.(2008, 1, 2)
> > > d2.UserData = x2 // hypothetical
> > >
> > > mylist.append([d1, d2])
> > >
> > > Hope i'm making some sense :-)
> >
> > You can subclass `datetime.date` and attach whatever attributes you
> > want.  Be sure to overwrite `__new__()` because `datetime.date` objects
> > are immutable.
> >
> > Ciao,
> > Marc 'BlackJack' Rintsch
>
>
> Thanks.
>
> As I read somewhere python has almost everything you need. So I
> wouldn't go to subclassing existing stuff and making life harder for
> me.
>
> Eventhough it may be costly to sort after adding all items to the
> dict(as opposed to inserting with a custom sort function) I would go
> for that.
>
> Anyway, I saw the UserDict module which may help in creating a custom
> dictionary with a custom comparison function. (I didn't go to detail
> as the documentation was not very elaborate )

while trying out the sorting method i realized that u can *never*
insert the sorted data to dict !!!
>>> m1
{datetime.date(2008, 1, 1): 'b', datetime.date(2008, 1, 3): 'c',
datetime.date(2008, 1, 2): 'a'}

>>> l = sorted(m1.items(), cmp=cmpr) // cmpr is date comp function
>>> for i, j in l:
... m3[i] = j;
...
>>> m3
{datetime.date(2008, 1, 1): 'b', datetime.date(2008, 1, 3): 'c',
datetime.date(2008, 1, 2): 'a'}

so then there's no point in sorting.
it will only be possible to do a linear search i.e. sort and add to
list then search from top.

I would really like to suggest some wrapper or an enhanced dict
structure which has a configurable comparison function that will be
used to insert and find keys.
this will help, for example, if we want to go to a specific key and
iterate from there onwards


--
http://mail.python.org/mailman/listinfo/python-list