Object copies...

Peter Abel p-abel at t-online.de
Fri Jan 17 20:15:04 EST 2003


pennedinil at excite.com (DP) wrote in message news:<6af8e801.0212222321.3bd5b56f at posting.google.com>...
> Hi all,
> 
> Please help me clear my confusion here. I'm running the script below.
> I'm expecting to get a listing of my original object and the new
> object created by 'getdates'. Instead I'm seeing the function
> 'getdates' changing my original object, 'mydates'.
> 
> Q1: Why does 'getdates' not create a copy of 'mydates' instead of
> operating on the original object? I.e., how do I change this behavior?
> 
> Q2: [Unrelated] I'm trying to parse these dates into d, m, y and can't
> think of any other way but on a per-case basis. Any suggestions on a
> better aproach? 

[snip]

Why should'nt we do it with the good old regular expressions?
Remark: I replaced the empty strings by actual time 
        the same way Bengt Richter did.

>>> def format_date(mydates):
...   import re,time
...   m=re.compile(r'(\d\d?)[-./ ]?([01]?\d)[-./ ]?(\d+)')
...   newdates=[]
...   for date in mydates:
...     if date:
... 	  new_date=map(int,m.search(date).groups())
...     else:
...       new_date=list(time.localtime()[:3])
...       new_date.reverse()
...     if new_date[2]<2000:
... 	  new_date[2]+=2000
...     newdates.append('%02d/%02d/%04d'%tuple(new_date))
...   return newdates
... 
>>> format_date(mydates)
['31/08/2002', '31/08/2002', '31/08/2002', '30/09/2002', '30/09/2002',
'30/09/2002', '30/09/2002', '30/09/2002', '30/09/2002', '31/08/2002',
'31/08/2002', '31/08/2002', '30/09/2002', '30/09/2002', '30/09/2002',
'31/08/2002', '31/08/2002', '31/08/2002', '30/09/2002', '30/09/2002',
'30/09/2002', '30/09/2002', '30/09/2004', '18/01/2003', '31/08/2002',
'31/08/2002', '18/01/2003', '30/09/2002', '30/09/2002', '30/09/2002',
'31/08/2002', '31/08/2002', '31/08/2002', '30/09/2002', '30/09/2002',
'30/09/2002', '31/08/2002', '31/08/2002', '31/08/2002', '31/08/2002',
'31/08/2002', '18/01/2003', '30/09/2002', '30/09/2002', '30/09/2002',
'31/08/2002', '31/08/2002', '31/08/2002']
>>> 

This seems all to be good.
BUT be carefull !
In "mydates" you have some dates, which can be ambiguous.

if __name__ == "__main__":
	mydates = ['31082002','31.08.2002','31.08.2002',
                         '30092002','30.09.2002','30.09.2002',
                         '309 2'   ,'30-09-2002','30-09-2002',
                         '318 2'   ,'31.08.2002','31/08/2002',
--------------------------^^^^^

I think it will be "31/08/2002". But if you write the
following date in the same format "312 2" it could be 
interpreted as "31/02/2002" as well as "03/12/2002".

Hope, I could help you.
Peter




More information about the Python-list mailing list