Commits

Alan Franzoni  committed 971b22d

Enh: very simple implementation for is_interested_in() that may just suffice our needs.

  • Participants
  • Parent commits 84470c3

Comments (0)

Files changed (3)

File mp3infodetector/albumblackboard.py

 
     confidence is METADATA and won't be taken into account
     when doing object comparison!
+
+    This happens because knowledge sources are supposed to upvote or downvote
+    an existing candidate, not to replace it.
     """
 
     def __init__(self, entry, value, initial_confidence=50):
 
 
 class AlbumBlackboard(object):
-#    SPECIAL_PREFIX = "6Qij4qmaWNnRs23aLD8Y" # prefixes metadata.
+    # TODO: add __hash__()
 
     def __init__(self, tracks_filenames, containing_dir=None, parent_dir=None):
         self.candidates = dict(
         )
 
 
+    def __eq__(self, other):
+        if not isinstance(other, AlbumBlackboard):
+            return False
+
+        return (self.containing_dir == other.containing_dir) and (self.parent_dir == other.parent_dir) and (
+            self.candidates == other.candidates) and (self.metadata == other.metadata)
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+
+
+
+
+
+
+
+
 
 
 

File mp3infodetector/ks/knowledgesource.py

 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
+
+from copy import deepcopy
 from abc import ABCMeta, abstractmethod
 
 
 class KnowledgeSource(object):
     __metaclass__ = ABCMeta
 
-    @abstractmethod
-    def is_interested_in(self, blackboard):
+    @classmethod
+    def is_interested_in(cls, blackboard):
         """
+        Default implementation is very barebone and inefficient.
+
         :type blackboard: :class: mp3infodetector.albumblackboard.AlbumBlackboard
         :rtype: bool
         """
-        raise NotImplementedError
+        blackboard_copy = deepcopy(blackboard)
+        cls.evaluate(blackboard_copy)
+        cls.kopy = blackboard_copy
+        return (blackboard_copy != blackboard)
 
     @abstractmethod
     def evaluate(self, blackboard):

File mp3infodetector/test/test_knowledgesource.py

+# -*- coding: utf-8 -*-
+
+from unittest2 import TestCase
+
+from mp3infodetector.albumblackboard import AlbumBlackboard, Candidate
+from mp3infodetector.ks.knowledgesource import KnowledgeSource
+
+
+class ExampleKnowledgeSource(KnowledgeSource):
+    @classmethod
+    def evaluate(cls, blackboard):
+        blackboard.candidates["something"]["album"].add(Candidate("album", "myalbum"))
+
+class KnowledgeSourceTest(TestCase):
+    def test_ks_interested_if_blackboard_would_change(self):
+        bb = AlbumBlackboard(["something"])
+        self.assertTrue(ExampleKnowledgeSource.is_interested_in(bb))
+
+    def test_ks_is_not_interested_if_blackboard_would_stay_the_same(self):
+        bb = AlbumBlackboard(["something"])
+        ExampleKnowledgeSource.evaluate(bb)
+        self.assertFalse(ExampleKnowledgeSource.is_interested_in(bb))
+
+
+
+