On 01/12/2012 10:35 AM, osimons wrote:
...
Chris, your code looks way over-complicated for whatever it seems to
try to do.
That may be. I'm not the strongest Python programmer in the world.
> That said I cannot safely say I understand your use case..
He's my use case. TracPM defines
class ITaskScheduler(Interface):
# Schedule each the ticket in ticketsByID with consideration for
# dependencies, estimated work, hours per day, etc.
#
# ticketsByID is a dictionary, indexed by numeric ticket ID, each
# ticket contains at least the fields returned by queryFields()
# and the whole list was processed by postQuery().
#
# On exit, each ticket has t['calc_start'] and t['calc_finish']
# set (FIXME - we should probably be able to configure those field
# names.) and can be accessed with TracPM.start() and finish().
def scheduleTasks(self, options, ticketsByID):
"""Called to schedule tasks"""
and
# tickets is an unordered list of tickets. Each ticket contains
# at least the fields returned by queryFields() and the whole list
# was processed by postQuery().
def computeSchedule(self, options, tickets):
computeSchedule() takes a list of tickets because that's the natural way
for other code to handle the data (as returned from a query, used to
display a Gantt, etc.) ITaskScheduler takes a dictionary because it had
to create one interinally anyway and I can have computeSchedule() build
it for all schedulers and use that as an opportunity to isolate the
caller of computeSchedule() from the vagaries of the scheduler (which
might want to update tickets in weird ways).
So, my implementation of computeSchedule is:
# Convert list to dictionary, making copies so schedule can
# mess with the tickets.
ticketsByID = {}
for t in tickets:
# FIXME - deepcopy fails here but with only copy, if the
# scheduler updates fields, those updates are visible to
# the caller.
ticketsByID[t['id']] = copy.copy(t)
# Schedule the tickets
self.scheduler.scheduleTasks(options, ticketsByID)
# Copy back the schedule results
for t in tickets:
for field in [ 'calc_start', 'calc_finish']:
t[field] = ticketsByID[t['id']][field]
but it doesn't work, the caller still sees the changes to other fields
that the scheduler makes.
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.
I guess my clue detector is on the fritz. Sorry.
> 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)
So, something like:
# tickets is an unordered list of tickets. Each ticket contains
# at least the fields returned by queryFields() and the whole list
# was processed by postQuery().
def computeSchedule(self, options, tickets):
# Convert list to dictionary, making copies so schedule can
# mess with the tickets.
ticketsByID = {}
for t in tickets:
ticketsByID[t['id']] = t.values.copy()
# Schedule the tickets
self.scheduler.scheduleTasks(options, ticketsByID)
# Copy back the schedule results
for t in tickets:
for field in [ 'calc_start', 'calc_finish']:
t[field] = ticketsByID[t['id']][field]
?
--
Christopher Nelson, Software Engineering Manager
SIXNET - Solutions for Your Industrial Networking Challenges
331 Ushers Road, Ballston Lake, NY 12019
Tel: +1.518.877.5173, Fax: +1.518.877.8346 www.sixnet.com
--
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.