Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am coming from a 'C'/unix background and learning Python. The kivy framework - for python - implements a wonderful event driven architecture, that I would like to replicate in my own app - but I'm not using Kivy due to reasons I shall avoid documenting here for sake of ease of dicsussion.

On kivy one can observe changes to a propery (set) using 'on_<property-name>'. The on_<property-name> is invoked through the hieratchy when the property value changes. If defining the class yourself:
Python
from kivy import Widget

class MyClass(Widget):
   # I guess we need to start with EventDispatcher??

   my_property = NumericProperty(1) # NumbericProperty is a kivy class

   # This method can be overriden by base clases of this class
   def on_my_property(self, instance, value):
      # Note that 'on_<property-name>' must not forget to call
      # the superclass function 'on_<property>' also ,
      # but I do not know how to do this?
      super(MyClass, self).on.... ??

      print('My property a changed to', value)

How do I replicate this architecute?
I have looked at this Kivy source file for property class https://github.com/kivy/kivy/blob/master/kivy/properties.pyx#L219 but there is far far more going on than I need and it is difficult for me to undertand the code. I do not see how or where the "On_<property-name>" method/function/event (whatever it is called) is defined so that I can override it in MyClass

So in my design, I want a root class to be:
Python
class MyProperty(object): # Is object correct?
  # I have no idea what to subclass from ?
  # or how to provide the on_<property-name> event?

So that I can subclass:
Python
class MyModel(EventDispatcher): # Is this right???
   # declare property called "data"
   data=MyProperty(...) # not sure what to do here?

class MySubModel(MyModel): # Is Object right???
   # override super class MyModel's on_data event to react to changes to "data"
   def on_data():
      # some action code

But I have no idea how to implement this the on_<property> so that the superclass is called appropriately and on_<property> can be overriden by subclasses I define.

Help please , and don't forget to give the all important explanation. Please, PLEASE dont link me to google because clearly I have got to a point where I have googled and looked at sites and pages, and come here for help. And, PLEASE dont offer another solution because I've spent so long on this, that I must now understand and implement the same design patterns for sanity !! Thank you

What I have tried:

kivy/properties.pyx at master · kivy/kivy · GitHub[^]
Posted
Updated 14-Jun-18 2:20am
v3

 
Share this answer
 
Comments
ninjaef 14-Jun-18 7:03am    
That is not what I asked for.

The example you point to, is about binding which can be done outside of a class hierarchy. My question, was specifically how to implement a "on_<property>" method with callback system through inhertiance and specfically using method names called "on_" suffixed with the property name , which is "automatically" created by virtue of simply declaring an object of class MyProperty(), as in my example, which I felt was clear enough. Do you need more clarification?
Richard MacCutchan 14-Jun-18 8:16am    
Sorry, I did indeed misread. But as you already discovered, this feature does not exist in the Python language.
Found this and the EventDispatcher code does the automatic "on_" event creation by using introspection of the __attr__ list

kivy/_event.pyx at master · kivy/kivy · GitHub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900