Dynamic functions creation from json, python
Question: I am new to python and I need to create class on the fly from the following
json:

    
    
    {
        "name": "ICallback",
        "functions": [
            {
                "name": "OnNavigation", 
                "parameters": [
                    {"name":"Type", "type": "int", "value": "0"},
                    {"name":"source", "type": "int", "value": "0"},
                    {"name":"tabId", "type": "string", "value": ""},
                    {"name":"Url", "type": "string", "value": ""},
                    {"name":"Context", "type": "int", "value": "0"}
                ]
            }
        ]
    }
    

I found how to create class, but I don't understand how to create methods on
the fly. For now function will just raise `NotImplemented` exception.


Answer: So you already know to create a class:

    
    
    class Void(object):
        pass
    
    kls = Void()
    

You want to create a method from a JSON, but I'm going to do it from a string
that can be created from the JSON:

    
    
    from types import MethodType
    
    d = {}
    exec "def OnNavigation(self, param): return param" in d
    
    kls.OnNavigation = MethodType(d["OnNavigation"], kls)
    # setattr(kls, "OnNavigation", MethodType(d["OnNavigation"], kls))
    
    print kls.OnNavigation("test")
    

Should output _test_.

