Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Custom serialization #7757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 31, 2015
Merged

Custom serialization #7757

merged 19 commits into from
Mar 31, 2015

Conversation

jasongrout
Copy link
Member

This is an attempt to address #7729.

This depends on #7798, #7801, and #7780, which have all been merged.

@jasongrout
Copy link
Member Author

@jdfreder, @SylvainCorlay - this isn't nearly ready yet, but it's getting there. Just FYI

@jdfreder
Copy link
Contributor

I'll wait to try to wrap my head around the code entirely until it's in a working state, just ping when.

})(state[k]));
}
}
return utils.resolve_promises_dict(state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block above should get moved into it's own method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree; that's a bit of a beast.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote it and think it's a lot easier to follow now.

@jdfreder
Copy link
Contributor

My biggest gripe about this design is that custom serializers are required for any variation of nested traits and I would argue that that means this doesn't scale well. Additionally, as-is this supports Unions in one direction, from Python to Javascript, but you can't really say the same in reverse.

@jasongrout
Copy link
Member Author

The branch now minimally works for widget boxes (e.g., it correctly serializes and unserializes widget references).

I'll look at your comments tomorrow. Thanks!

@jasongrout
Copy link
Member Author

@jdfreder - since any serializers/deserializers are supported, it would be easy to make default serializers that recurse into container traitlets, and thus experiment with different ways of doing things.

@jdfreder
Copy link
Contributor

@jasongrout I think this design is starting to shape up nicely. I'll need to experiment with this branch by writing some custom serializers to test it.

@jasongrout
Copy link
Member Author

@jdfreder - I'm also writing some custom serializers to test things out. Then I'll write some formal unit tests based on that.

@jasongrout jasongrout force-pushed the custom-serialization branch 4 times, most recently from 4d1810e to 2c9c7b4 Compare February 17, 2015 17:05
@rgbkrk
Copy link
Member

rgbkrk commented Feb 19, 2015

Definitely looking forward to this one, hope I can learn from it and use it soon.

@jdfreder
Copy link
Contributor

Wow, lots of changes since the last time I looked. Everything is looking good so far.

@jasongrout jasongrout force-pushed the custom-serialization branch from 047bc09 to 3055eeb Compare March 13, 2015 19:02
@jasongrout
Copy link
Member Author

Still have some errors; I'll look at these tomorrow, though.

@jasongrout jasongrout force-pushed the custom-serialization branch from a19ad12 to 9d0c441 Compare March 20, 2015 18:45
@jasongrout
Copy link
Member Author

I'm going to split this into a typo fix, the kernel.js fix, and then the widget stuff, so that it's easy to finish the widget stuff even after The Big Split.

@minrk
Copy link
Member

minrk commented Mar 21, 2015

@jasongrout that sounds perfect, thanks.

@jasongrout jasongrout force-pushed the custom-serialization branch from 9d0c441 to 0e93515 Compare March 24, 2015 15:16
@minrk
Copy link
Member

minrk commented Mar 30, 2015

@jasongrout binary websocket tests don't pass, but it's unrelated to this PR. It's just the API change from the switch to memoryviews. I opened #8200 to fix it.

@jdfreder
Copy link
Contributor

I'm reviewing this now

if (module.hasOwnProperty(target_name)) {
if (endsWith(target_name, "View")) {
widgetmanager.WidgetManager.register_widget_view(target_name, module[target_name]);
} else if (endsWith(target_name, "Model")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be assuming, even within our own project, that the class name's suffix correlates to the parent class. Here's something I whipped up that can be used to check for inheritance of backbone classes:

require(['backbone', 'widgets/js/widget', 'widgets/js/widget_button'], function(backbone, widget, button) {
    var isinstance = function(child, parent) {
        child = child.__super__;
        while (child !== null) {
            if (child === parent.prototype) return true;
            child = child.__proto__;
        }
        return false;
    };

    console.log(isinstance(button['ButtonView'], widget['DOMWidgetView']));
    console.log(isinstance(button['ButtonView'], widget['WidgetView']));
    console.log(isinstance(button['ButtonView'], widget['WidgetModel']));
});

true, true, false

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class inheritance is a much better idea than working on the name. Does Backbone not have something like isinstance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, TBH I didn't look :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like instanceof works- I haven't tested it yet though. http://stackoverflow.com/a/11125117

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasongrout yup, much simpler:

button['ButtonView'].prototype instanceof widget['DOMWidgetView']
//true
button['ButtonView'].prototype instanceof widget['WidgetView']
//true
button['ButtonView'].prototype instanceof widget['WidgetModel']
//false

The trick is the .prototype .

@jdfreder
Copy link
Contributor

@jasongrout overall I'm happy with this design too. If later we find that we need something more dynamic, like box typing that @SylvainCorlay suggested, we'll cross that bridge when/if we come to it.

Just two comments inline.

Jason Grout added 2 commits March 30, 2015 20:37
@jasongrout
Copy link
Member Author

@jdfreder: I think I addressed your two points. Thanks! I'll wait to make sure the tests pass as well.

@jasongrout
Copy link
Member Author

@jdfreder: Looks like tests passed....

@jasongrout
Copy link
Member Author

This does change the number of arguments in custom message callbacks; I'm not sure where to note that.

@jdfreder
Copy link
Contributor

Awesome! Go ahead and add a what's new rst in https://github.com/ipython/ipython/tree/master/docs/source/whatsnew/pr then I think this is 👍

@rgbkrk
Copy link
Member

rgbkrk commented Mar 31, 2015

Woohoo!

@jdfreder
Copy link
Contributor

@jasongrout IIRC you're on vacation right? Seems like we have an even greater timezone difference today! I'm going to merge this and write the what's new rst for you.

jdfreder added a commit that referenced this pull request Mar 31, 2015
@jdfreder jdfreder merged commit d6e249b into ipython:master Mar 31, 2015
@SylvainCorlay
Copy link
Member

@jdfreder @jasongrout it seems that it is breaking the loading of custom models. I am looking into this now.

@jasongrout
Copy link
Member Author

@SylvainCorlay, thanks. @jdfreder, thanks for moving ahead without waiting for me.

One incompatible change is that the custom message handlers in python now take a new argument, buffers. See the change for the click handler for buttons, for example.

@jasongrout
Copy link
Member Author

@SylvainCorlay, also note that models which have fields that encode references to other models need to now explicitly declare the serializer for that field (like in widget_box's children attribute). This, for example, will necessitate some changes to pythreejs.

@SylvainCorlay
Copy link
Member

@jasongrout thanks for the pointers.
This actually forces all custom widgets holding references to other models to have a custom model.

@jasongrout
Copy link
Member Author

Right. I think that is much better than the hack we had in serialization before.

@SylvainCorlay
Copy link
Member

I agree. Do you think that we still need the IPY_MODEL_ prefix?

@jasongrout
Copy link
Member Author

Probably less of a need. We could shorten it, but I don't think we should get rid of it.

@jasongrout
Copy link
Member Author

@tacaswell, I forgot to mention to you that this PR enables binary buffers in IPython comm messages, which may be useful to the nbagg backend.

@tacaswell
Copy link
Contributor

attn @pelson

That does seem handy. This means we can send the png as binary rather than as a base64 string?

@jasongrout
Copy link
Member Author

Exactly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants