views:

55

answers:

2

Please consult me with your opinions on the following topic:

I have a model - a structure of the objects. Such as:

  • Event, containing participants

  • Current task

  • Assignee of each task

The model is going to be pickled on the server and transferred over the network to the client GUI application. Because of the pickle I'd want to keep the model classes as simple as possible (i.e. just simple classes with data fields only, no any single method inside). As a result I can't create signals (such as OnUpdate) on the model objects.

Sometimes the server will send model updates. Such as "Task text changed". When the update is applied, I need it to be reflected in the UI. In the case of task text change it shall be change of the label in the UI. I'd want only related controls to be changed, so the whole UI update is not the best solution.

On the other hand, would not like to traverse the whole model in the search of changes - that would be too resource intensive.

So, what is the best pattern of notifying the UI about changes on the plain data structures?

A: 

You may be operating under a mis-conception: pickles don't include the code from classes that are pickled. You can add methods to your data structures, and it will not increase the size of your pickles.

This is a common misunderstanding about pickles. They don't include code.

Ned Batchelder
Thanks for you answer!But what shall happen if I'll add event handlers to the model on the server side (i.e., the object would contain data about the event listeners), pickle the object and then unpickle it on the client side?
Slava Tutushkin
@Slava Tutushkin: The class definitions exist in both places. Only the changeable attribute data is serialized.
S.Lott
A: 

You may add a flag, e.g. self.isOnClientSide, and check it in every update handler, so that you can use different logic in either case.

def onUpdateFoo(self):
  if self.isOnClientSide:
    return self.onUpdateFooOnClient()
  else:
    return self.onUpdateFooOnServer()

Change this flag accordingly right after un-pickling.

9000