473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

using datetime containers

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
Nov 8 '08 #1
5 1777
indika wrote:
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.
Do the simplest thing that works for you. If you need your dictionary
ordered for output only, just sort it:
>>dmap = {
.... datetime.date(2001, 12, 3): 'c',
.... datetime.date(2001, 12, 1): 'a',
.... datetime.date(2001, 12, 2): 'b'
.... }
>>for date, value in sorted(dmap.items()):
.... print date, "-->", value
....
2001-12-01 --a
2001-12-02 --b
2001-12-03 --c

Peter
Nov 8 '08 #2
On Sat, 08 Nov 2008 08:07:15 -0800, indika wrote:
John Machin wrote:
>On Nov 8, 6:06�pm, indika <indikabandar...@gmail.comwrote:
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
Nov 8 '08 #3


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 <indikabandar...@gmail.comwrote:
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 )

Nov 9 '08 #4


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 <indikabandar...@gmail.comwrote:
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
Nov 9 '08 #5
indika <in*************@gmail.comwrites:
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.
That's because dicts are the wrong data structure for this. Dicts are
implemented as hashtables. You can use lists instead: look at the
module bisect. There aren't any linear structures with fast insertion
in standard Python (e.g. d-linked lists or balanced trees).
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
I'm sure that googling for 'python sorted dict' with yield useful
information and probably some implementations.

--
Arnaud
Nov 9 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: wtnt | last post by:
Hello, I've been using the STL libraries for some time, but still don't know the ins and outs of its implementation. Could this be because there's more than 1 implementation? Does anyone know of...
0
by: Happosai | last post by:
I am attempting to convert some content layout on a site that I manage from using a table to using CSS, but I cannot figure out how to do it. The following HTML is a simplification of what I am...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
4
by: Erik Hendrix | last post by:
Hi, I have a quick question, when one sets the prefetch size = extent size, then when doing a backup we will have 1 agent (db2bm) doing the reads. If we have prefetch size a multiple of extent...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
11
by: Grischa Brockhaus | last post by:
Hi, I'm trying to produce a div layout containing a header on the top with fixed height, a footer on the bottom using fixed height and a content layer using what's left of the browsers window. ...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.