On Jan 12, 3:53 pm, Chris Nelson <[email protected]> wrote:
> On 01/12/2012 09:14 AM, osimons wrote:
> > On Jan 12, 2:25 pm, Chris Nelson<[email protected]> wrote:
> >>> ... But the ticket attritbutes weren't copied, only the references to
> >>> them.
> >>> I tried but deepcopy() but that gives:
> >>> TypeError: __init__() takes exactly 3 arguments (1 given)
> >>> ...
>
> >>http://grokbase.com/p/python.org/python-list/2009/01/how-to-deepcopy-...
> >> notes:
>
> >> A subclass of list should populate the list in __init__, not __new__,
> >> usually by calling list.__init__, as lists are mutable and subclasses
> >> thereof should be too.
>
> >> I don't believe TracPM has any subclass of list and I think Ticket
> >> maybe which suggests to me there's something wrong in Trac core. With
> >> a little support, I'll work on the patch but core is unfamiliar to me.
>
> > And 'tickets' in your code snippet is what exactly - a list of trac
> > ticket objects?
>
> Yes.
>
> > If so, they are objects with various internals that
>
> > you can not depend on copy or deepcopy to handle. The
> > trac.ticket.model.Ticket class uses __getitem__ and __setitem__ to
> > appear as dict, but the actual dict with the information is stored in
> > t.values.
> > ...
> > Lesson: Don't copy or deepcopy objects unless either you or the class
> > know exactly what needs to be done.
>
> Thanks for the explanation. I'll find another way.
>
> Which leads me to ask why:
>
> # Remember original pred, succ values
> pred = {}
> succ = {}
> for tid in ticketsByID:
> pred[tid] = copy.copy(ticketsByID[tid]['pred'])
> succ[tid] = copy.copy(ticketsByID[tid]['succ'])
>
> # Update pred, succ as needed for scheduling
> _augmentTickets(ticketsByID)
>
> for id in ticketsByID:
> if options['schedule'] == 'alap':
> _schedule_task_alap(ticketsByID[id])
> else:
> _schedule_task_asap(ticketsByID[id])
>
> # Put original pred, succ back
> for tid in ticketsByID:
> ticketsByID[tid]['pred'] = pred[tid]
> ticketsByID[tid]['succ'] = succ[tid]
>
> leaves the scheduler changes to pred and succ visible to the caller.
Chris, your code looks way over-complicated for whatever it seems to
try to do. That said I cannot safely say I understand your use case..
However, I can elaborate on the original topic of making a copy of the
ticket values:
I left you a clue when I said that t.values will contain all the
actual ticket data - the strings, datateime and integers that makes up
the ticket data. As these are all immutable values, there is no need
to deepcopy - a straight copy would suffice:
ticket_values = {}
for t in tickets:
ticket_values[t.id] = t.values.copy()
Or if you don't want to store it in a dict with ID as key but instead
need to retain the order in a list, you need to include the ticket id
in the values as it is not stored in values. So this should make a
simplified copy of the tickets with just the values:
ticket_values = []
for t in tickets:
t_vals = t.values.copy()
t_vals['id'] = t.id
ticket_values.append(t_vals)
OK?
:::simon
https://www.coderesort.com
http://trac-hacks.org/wiki/osimons
--
You received this message because you are subscribed to the Google Groups "Trac
Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/trac-dev?hl=en.