

class BasicCacher(object):
    """
    Basic in-memory caching.
    """
    def __init__(self, logger=None):
        self._cache = {}
        self.logger = logger

    def set_cache(self, node, data):
        """
        Store some data on the object.
        """
        self._cache[node.label] = data

    def get_cache(self, node):
        """
        Return cached data.
        """
        return self._cache.get(node.label)

    def has_cache(self, node):
        return self._cache.get(node.label) is not None

    def clear_cache(self, node):
        del self._cache[node.label]

    def clear(self):
        self._cache = {}

    def __repr__(self):
        return "<%s>" % self.__class__.__name__





class BasicCacher(object):
    """
    Basic in-memory caching.
    """
    def __init__(self, logger=None):
        self._cache = {}
        self.logger = logger

    def set_cache(self, node, data):
        """
        Store some data on the object.
        """
        self._cache[node.label] = data

    def get_cache(self, node):
        """
        Return cached data.
        """
        return self._cache.get(node.label)

    def has_cache(self, node):
        return self._cache.get(node.label) is not None

    def clear_cache(self, node):
        if self._cache.get(node.label):
            del self._cache[node.label]

    def clear(self):
        self._cache = {}

    def __repr__(self):
        return "<%s>" % self.__class__.__name__



Test for existence of node before clearing it
Test for existence of node before clearing it

  
  
  class BasicCacher(object):
      """
      Basic in-memory caching.
      """
      def __init__(self, logger=None):
          self._cache = {}
          self.logger = logger
  
      def set_cache(self, node, data):
          """
          Store some data on the object.
          """
          self._cache[node.label] = data
  
      def get_cache(self, node):
          """
          Return cached data.
          """
          return self._cache.get(node.label)
  
      def has_cache(self, node):
          return self._cache.get(node.label) is not None
  
      def clear_cache(self, node):
+         if self._cache.get(node.label):
-         del self._cache[node.label]
+             del self._cache[node.label]
  
      def clear(self):
          self._cache = {}
  
      def __repr__(self):
          return "<%s>" % self.__class__.__name__
  
  
  
Test for existence of node before clearing it
## Code Before:


class BasicCacher(object):
    """
    Basic in-memory caching.
    """
    def __init__(self, logger=None):
        self._cache = {}
        self.logger = logger

    def set_cache(self, node, data):
        """
        Store some data on the object.
        """
        self._cache[node.label] = data

    def get_cache(self, node):
        """
        Return cached data.
        """
        return self._cache.get(node.label)

    def has_cache(self, node):
        return self._cache.get(node.label) is not None

    def clear_cache(self, node):
        del self._cache[node.label]

    def clear(self):
        self._cache = {}

    def __repr__(self):
        return "<%s>" % self.__class__.__name__



## Instruction:
Test for existence of node before clearing it
## Code After:


class BasicCacher(object):
    """
    Basic in-memory caching.
    """
    def __init__(self, logger=None):
        self._cache = {}
        self.logger = logger

    def set_cache(self, node, data):
        """
        Store some data on the object.
        """
        self._cache[node.label] = data

    def get_cache(self, node):
        """
        Return cached data.
        """
        return self._cache.get(node.label)

    def has_cache(self, node):
        return self._cache.get(node.label) is not None

    def clear_cache(self, node):
        if self._cache.get(node.label):
            del self._cache[node.label]

    def clear(self):
        self._cache = {}

    def __repr__(self):
        return "<%s>" % self.__class__.__name__


