new_contents: 
  from PyQt4 import Qt
  from logging import warning, error, info
  import json
  
  class Connection:
      def __init__(self, host, port, update_fn):
          self.host = host
          self.port = port
          self.update_fn = update_fn
  
          self.sock = Qt.QTcpSocket()
          self.sock.connected.connect(self.connected)
          self.sock.disconnected.connect(self.disconnected)
          self.sock.error.connect(self.socket_error)
          self.sock.readyRead.connect(self.data_ready)
          self.retry_delay_ms = 2000
          self.connect()
  
      def connect(self):
          info("Attempting to connect.")
          self.sock.connectToHost(self.host, self.port)
  
      def connected(self):
          info('Connected to host!')
  
      def disconnected(self):
          info("Disconnected from host!")
          Qt.QTimer.singleShot(self.retry_delay_ms, self.connect)
  
      def socket_error(self):
          warning("Socket error")
          Qt.QTimer.singleShot(self.retry_delay_ms, self.connect)
  
      def data_ready(self):
          if self.sock.canReadLine():
              line = str(self.sock.readLine())
              
              try:
                  obj = json.loads(line)
              except ValueError as err:
                  warning("Received invalid json: %s" % str(err))
                  return
  
              self.update_fn(obj)
  
  
subject: Split socket connection for qt code into separate file.