Object copies...

Bengt Richter bokr at oz.net
Tue Dec 24 00:57:21 EST 2002


On 22 Dec 2002 23:21:48 -0800, pennedinil at excite.com (DP) wrote:

>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?
If first thing in getdates you rebind the Dates arg to a new list,
it should do what you want, e.g.,

    def getdate(Dates):
        Dates = Dates[:] # a slice gets a new list, and [:] slices the whole thing
	for i, j in indexedList(Dates):
        ...

but you could do things other ways.
>
>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? I tried -
If you are comfortable with regular expressions (see re module), that's
probably the easiest. In any case way, I'd suggest defining a function to
convert a single date string to a standard format that suits your need,
e.g., perhaps return a [d,m,y] list with full integer values. Then
you can plug in an alternate function later if you want to use regex
or whatever. So you'd use it like

 try:
        d,m,y = getDateList(Dates[i])
 except: ValueError ... <etc>

instead of

>try:
>	d. m, y = Dates[i].split("/")
>except: ValueError ...<etc>

>but there are too many exceptions to the rule, which brings me back to
>a case-based algorithm.
maybe there aren't that many exceptions if you classify differently? E.g.,
looking at your data, this seems to work (assuming '' means "today"):
--
 >>> import time
 >>> def getDateList(dateString):
 ...     if dateString=='': dmy = list(time.localtime()[:3]); dmy.reverse()
 ...     elif '/' in dateString: dmy = dateString.split('/')
 ...     elif '.' in dateString: dmy = dateString.split('.')
 ...     elif '-' in dateString: dmy = dateString.split('-')
 ...     else:                   dmy = [dateString[:2],dateString[2:4],dateString[4:]]
 ...     dmy[2] = dmy[2]=='' and 2002 or int(dmy[2])<1000 and 2000+int(dmy[2]) or dmy[2]
 ...     return map(int,dmy)
 ...
 >>> for i in xrange(0,len(mydates),3): print map(getDateList, mydates[i:i+3])
 ...
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[30, 9, 2002], [30, 9, 2004], [23, 12, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [23, 12, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [23, 12, 2002]]
 [[30, 9, 2002], [30, 9, 2002], [30, 9, 2002]]
 [[31, 8, 2002], [31, 8, 2002], [31, 8, 2002]]
--
>
>Thanks in advance.
>Dinil.
>
>
># -----------------------------------------------------------------------
>def indexedList(list):
>	return map(None, range(len(list)), list)
>
>def getdate(Dates):
>	for i, j in indexedList(Dates):
>		j = j.replace(".", "/")
>		j = j.replace("-", "/")
>		j = j.replace(" ", "/")
>		Dates[i] = j
>	return Dates
>
>

>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',
>			 '30092002','30-09-2002','30-09-2002',
>			 '31082002','31-08-2002','31-08-2002',
>			 '309 2002','30.09.2002','30.09.2002',
>			 '318 2002','31.08.2002','31.08.2002',
>			 '30092002','30.09.2004',''			,
>			 '31082002','31.08.02'  ,''			,
>			 '30092002','30.09.02'  ,'30.09.2002',
>			 '31082002','31.08.02'  ,'31.08.2002',
>			 '30092002','30.09.2002','30/9/2002'	,
>			 '31082002','31/8/2002' ,'31/8/2002'	,
>			 '31082002','31082002'  ,''			,
>			 '30092002','30092002'  ,'30092002'	,
>			 '31082002','31.08.2002','31.08.2002'
>			 ]
>	newdates = getdate(mydates)
>	print newdates
>	print mydates

Regards,
Bengt Richter



More information about the Python-list mailing list