
import rospy
from threading import Lock


class Listener:
    def __init__(self, topic_name, topic_type, lock=None):
        """
        Listener is a wrapper around a subscriber where the callback simply records the latest msg.

        Parameters:
            topic_name (str): name of topic to subscribe to
            topic_type (msg_type): type of message received on topic
            lock (Lock): optional lock object used when setting stored data
        """

        self.data = None
        self.lock = lock
        if self.lock is None:
            self.lock = Lock()
            
        self.subscriber = rospy.Subscriber(topic_name, topic_type, self.callback)
        

    def callback(self, msg):
        with self.lock:
            self.data = msg

    def get(self):
        """
        Returns the latest msg from the subscribed topic
        """
        with self.lock:
            return self.data
    


import rospy
from threading import Lock


class Listener:
    def __init__(self, topic_name, topic_type):
        """
        Listener is a wrapper around a subscriber where the callback simply records the latest msg.

        Listener does not consume the message 
            (for consuming behavior, use the standard ros callback pattern)
        Listener does not check timestamps of message headers

        Parameters:
            topic_name (str): name of topic to subscribe to
            topic_type (msg_type): type of message received on topic
            lock (Lock): optional lock object used when setting stored data
        """

        self.data = None
        self.lock = Lock()
            
        self.subscriber = rospy.Subscriber(topic_name, topic_type, self.callback)
        

    def callback(self, msg):
        with self.lock:
            self.data = msg

    def get(self):
        """
        Returns the latest msg from the subscribed topic
        """
        with self.lock:
            return self.data
    

Remove optional lock input (I can't see when it would be useful) Document when Listener should be used
Remove optional lock input (I can't see when it would be useful)
Document when Listener should be used

  
  import rospy
  from threading import Lock
  
  
  class Listener:
-     def __init__(self, topic_name, topic_type, lock=None):
+     def __init__(self, topic_name, topic_type):
          """
          Listener is a wrapper around a subscriber where the callback simply records the latest msg.
+ 
+         Listener does not consume the message 
+             (for consuming behavior, use the standard ros callback pattern)
+         Listener does not check timestamps of message headers
  
          Parameters:
              topic_name (str): name of topic to subscribe to
              topic_type (msg_type): type of message received on topic
              lock (Lock): optional lock object used when setting stored data
          """
  
          self.data = None
-         self.lock = lock
-         if self.lock is None:
-             self.lock = Lock()
+         self.lock = Lock()
              
          self.subscriber = rospy.Subscriber(topic_name, topic_type, self.callback)
          
  
      def callback(self, msg):
          with self.lock:
              self.data = msg
  
      def get(self):
          """
          Returns the latest msg from the subscribed topic
          """
          with self.lock:
              return self.data
      
  
Remove optional lock input (I can't see when it would be useful) Document when Listener should be used
## Code Before:

import rospy
from threading import Lock


class Listener:
    def __init__(self, topic_name, topic_type, lock=None):
        """
        Listener is a wrapper around a subscriber where the callback simply records the latest msg.

        Parameters:
            topic_name (str): name of topic to subscribe to
            topic_type (msg_type): type of message received on topic
            lock (Lock): optional lock object used when setting stored data
        """

        self.data = None
        self.lock = lock
        if self.lock is None:
            self.lock = Lock()
            
        self.subscriber = rospy.Subscriber(topic_name, topic_type, self.callback)
        

    def callback(self, msg):
        with self.lock:
            self.data = msg

    def get(self):
        """
        Returns the latest msg from the subscribed topic
        """
        with self.lock:
            return self.data
    

## Instruction:
Remove optional lock input (I can't see when it would be useful) Document when Listener should be used
## Code After:

import rospy
from threading import Lock


class Listener:
    def __init__(self, topic_name, topic_type):
        """
        Listener is a wrapper around a subscriber where the callback simply records the latest msg.

        Listener does not consume the message 
            (for consuming behavior, use the standard ros callback pattern)
        Listener does not check timestamps of message headers

        Parameters:
            topic_name (str): name of topic to subscribe to
            topic_type (msg_type): type of message received on topic
            lock (Lock): optional lock object used when setting stored data
        """

        self.data = None
        self.lock = Lock()
            
        self.subscriber = rospy.Subscriber(topic_name, topic_type, self.callback)
        

    def callback(self, msg):
        with self.lock:
            self.data = msg

    def get(self):
        """
        Returns the latest msg from the subscribed topic
        """
        with self.lock:
            return self.data
    
