#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Newbie help needed - missing something basic about objects


    Hello

    I've been programming Python for a couple of months (loving it), and recently have had need to start using events. This has proved to be quite a paradigm shift, and revealed to myself that I have significant holes in my understanding of classes and objects in Python.

    I've messing with the following code (adapted from elsewhere on the net):

    Code:
    class Event:
        def __init__(self):
            self.handlers = set()
    
        def handle(self, handler):
            self.handlers.add(handler)
            return self
    
        def unhandle(self, handler):
            try:
                self.handlers.remove(handler)
            except:
                raise ValueError("Handler is not handling this event, so cannot unhandle it.")
            return self
    
        def fire(self, *args, **kargs):
            for handler in self.handlers:
                handler(*args, **kargs)
    
        def getHandlerCount(self):
            return len(self.handlers)
    
        __iadd__ = handle
        __isub__ = unhandle
        __call__ = fire
        __len__  = getHandlerCount
    
    class MockFileWatcher:
        def __init__(self):
            self.fileChanged = Event()
    
        def watchFiles(self,source_path):
            if (str(self.fileChanged)!=source_path):
              print ("old fileChanged = :"+str(self.fileChanged))
              self.fileChanged(source_path)
    
    def log_file_change(source_pathx):
        print "%r changed." % (source_pathx,)
    
    def log_file_change2(source_pathx):
        print "%r changed!" % (source_pathx,)
    
    watcher              = MockFileWatcher()
    watcher.fileChanged += log_file_change
    watcher.watchFiles("foo")
    watcher.watchFiles("goo")
    watcher.watchFiles("goo")
    watcher.watchFiles("foo")
    Output reads:
    old fileChanged = :<__main__.Event instance at 0x1004a1878>
    'foo' changed.
    old fileChanged = :<__main__.Event instance at 0x1004a1878>
    'goo' changed.
    old fileChanged = :<__main__.Event instance at 0x1004a1878>
    'goo' changed.
    old fileChanged = :<__main__.Event instance at 0x1004a1878>
    'foo' changed.


    Ok - so I was hoping to have the 'watchFiles' function only effect a change in the string sent.

    At the heart of my problem, I think is that I don't understand what is happening here:
    Code:
            self.fileChanged(source_path)
    selfChanged is an Event objectů what is happening when 'source_path' is passed to it like that?
    And how to effectively read it, as I've tried (and failed) to in my line:
    Code:
              print ("old fileChanged = :"+str(self.fileChanged))

    I really could do with some friendly help!

    Thanks!

  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    Please what output do you want?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Please what output do you want?
    I expected something like this:

    old fileChanged =
    'foo' changed.
    old fileChanged = foo
    'goo' changed.
    old fileChanged = foo
    'goo' changed.

    However, what I'm really scratching my head on is what is happening here:
    self.fileChanged(source_path)

    i don't understand how fileChanged, which is an Event object - accepts a parameter. How it stores it, and how it is read. Because Event objects don't take any parameters at creation, do they?

    Thanks again!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    You said that "fileChanged, which is an Event object..."

    That means that it is already created. You're correct, Event objects take no parameters at creation.

    Is fileChanged callable? You can test with callable

    >>> #examples in interpreter
    >>> callable(len)
    True
    >>> callable('a string')
    False
    >>>

    Yes! fileChanged is callable because it has a
    __call__ method.
    Code:
        #...
        __call__ = fire
        #...
    Next find the definition of fire.

    def fire(self, *args, **kargs):

    So there it is. fire accepts any syntax conforming arguments. Then, without processing the arguments in any way fire passes them to the handlers.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Thanks b49P23TIvg

    That's helped me a lot!

    Cheers!

IMN logo majestic logo threadwatch logo seochat tools logo