On Fri, 16 Dec 2011 10:45:22 +0100, Ulrich Eckhardt wrote: > Hi! > > I'm trying to create a struct_time that is e.g. one year ahead or a > month back in order to test some parsing/formatting code with different > dates. [...] > The second approach is this: > > l = list(t) # convert to a sequence > l[0] += 1 # increment year > t = time.struct_time(l) # convert to a struct_time > > This works but is ugly, because the code relies on the order inside the > list and uses magic numbers to access them. The order is AFAICT not > accessible programmatically but only documented, and not even in a way > that makes clear that it is part of the API and as such actualy > guaranteed. I could try to assert that the indices match using "if l[0] > is t.tm_year", but this is still ugly.
Feel free to propose a feature enhancement to time.struct_time, but the order of the fields is stable and won't change. So ugly or not, that way is guaranteed to work. [...] > Then I tried duck typing. If it quacks like a duck, it better not be a > crocodile! This looks like this: > > struct my_time(object): pass "struct"? [...] > Does anyone have a suggestion how to solve this elegantly and > pythonically? Also, what I'm wondering is if the lack of a clear way > should be considered a bug or not. Not a bug, but it does seem a very old and inelegant API more suited to hairy C programmers gathered around a smokey fire in a cave chewing on old dinosaur bones, and not worthy of space-age Python coders flying around on anti-gravity belts. -- Steven -- http://mail.python.org/mailman/listinfo/python-list