<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-


#
# Client side of the NetworkDemo application
#
# Receive the ball from the server, and display it
#


#
# External dependencies
#
import socket
import sys
import threading
from PyQt4 import QtCore
from PyQt4 import QtGui


#
# Class to receive the ball position from the server (threaded)
#
class BallClient( threading.Thread ) :
	
	#
	# Initialization
	#
	def __init__( self, server, widget ) :

		# Initialize the thread
		super( BallClient, self ).__init__()
		
		# Server address
		self.server = server

		# Graphical user interface
		self.widget = widget

		# Ball position
		self.position = [ 0, 0 ]

	#
	# Thread main loop
	#
	def run( self ) :
		
		# Connect to the server
		connection = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
		connection.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
		connection.connect( ( self.server, 10000 ) )

		# Continuously receive messages from the server
		self.running = True
		while self.running :

			# Receive the message from the server
			message = connection.recv( 256 )
			
			# Server is dead
			if not message :
				
				# Stop the client connection
				self.running = False
				break
				
			# Decode the message to get the ball position
			sx, _, sy = message.decode( 'ascii' ).partition( ';' )

			# Save the ball position
			self.position = [ float( sx ), float( sy ) ]

			# Update the widget
			self.widget.update()

		# Close the connection
		connection.close()

		# Close the widget
		self.widget.close()


#
# Widget to display the ball
#
class BallWidget( QtGui.QWidget ) :
	
	#
	# Initialization
	#
	def __init__( self, parent = None ) :

		# Initialize the widget
		super( BallWidget, self ).__init__( parent )

		# Change the window title
		self.setWindowTitle( 'NetworkDemo' )
		
		# Change the widget background color
		self.setStyleSheet( "background-color:white;" )

		# Set the Escape key to close the application
		QtGui.QShortcut( QtGui.QKeySequence( QtCore.Qt.Key_Escape ), self ).activated.connect( self.close )
		
		# Set the F12 key to toggle fullscreen
		QtGui.QShortcut( QtGui.QKeySequence( QtCore.Qt.Key_F12 ), self ).activated.connect( self.ToggleFullScreen )
		
		# Ball client thread
		self.ball = BallClient( sys.argv[ 1 ], self )
		self.ball.start()
		
	#
	# Paint the ball
	#
	def paintEvent( self, event ) :
		
		# Set up the painter
		paint = QtGui.QPainter( self )
		paint.setRenderHint( QtGui.QPainter.Antialiasing )
		paint.setBrush( QtCore.Qt.red )
		
		# Get the ball position
		position = QtCore.QPoint( self.ball.position[0] * self.size().width(),
								  self.ball.position[1] * self.size().height() )
								  
		# Draw the ball
		paint.drawEllipse( position, 60, 60 )

	#
	# Close the widget
	#
	def closeEvent( self, event ) :

		# Stop the ball client
		if self.ball.running :
			self.ball.running = False
			self.ball.join()
			
		# Close the widget
		event.accept()
		
	#
	# Toggle widget fullscreen
	#
	def ToggleFullScreen( self ) :
		
		# Show normal
		if self.isFullScreen() :
			self.showNormal()
		
		# Show fullscreen
		else : self.showFullScreen()


#
# Main application
#
if __name__ == '__main__' :

	# Start Qt application
	application = QtGui.QApplication( sys.argv )
	widget = BallWidget()
	widget.show()
	sys.exit( application.exec_() )

<commit_msg>Handle the case where the server connection is refused.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-


#
# Client side of the NetworkDemo application
#
# Receive the ball from the server, and display it
#


#
# External dependencies
#
import socket
import sys
import threading
from PyQt4 import QtCore
from PyQt4 import QtGui


#
# Class to receive the ball position from the server (threaded)
#
class BallClient( threading.Thread ) :
	
	#
	# Initialization
	#
	def __init__( self, server, widget ) :

		# Initialize the thread
		super( BallClient, self ).__init__()
		
		# Server address
		self.server = server

		# Graphical user interface
		self.widget = widget

		# Ball position
		self.position = [ 0, 0 ]

	#
	# Thread main loop
	#
	def run( self ) :
		
		# Connect to the server
		connection = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
		connection.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
		connection.connect( ( self.server, 10000 ) )

		# Continuously receive messages from the server
		self.running = True
		while self.running :

			# Receive the message from the server
			message = connection.recv( 256 )
			
			# Server is dead
			if not message :
				
				# Stop the client connection
				self.running = False
				break
				
			# Decode the message to get the ball position
			sx, _, sy = message.decode( 'ascii' ).partition( ';' )

			# Save the ball position
			self.position = [ float( sx ), float( sy ) ]

			# Update the widget
			self.widget.update()

		# Close the connection
		connection.close()

		# Close the widget
		self.widget.close()


#
# Widget to display the ball
#
class BallWidget( QtGui.QWidget ) :
	
	#
	# Initialization
	#
	def __init__( self, parent = None ) :

		# Initialize the widget
		super( BallWidget, self ).__init__( parent )

		# Change the window title
		self.setWindowTitle( 'NetworkDemo' )
		
		# Change the widget background color
		self.setStyleSheet( "background-color:white;" )

		# Set the Escape key to close the application
		QtGui.QShortcut( QtGui.QKeySequence( QtCore.Qt.Key_Escape ), self ).activated.connect( self.close )
		
		# Set the F12 key to toggle fullscreen
		QtGui.QShortcut( QtGui.QKeySequence( QtCore.Qt.Key_F12 ), self ).activated.connect( self.ToggleFullScreen )
		
		# Ball client thread
		self.ball = BallClient( sys.argv[ 1 ], self )
		self.ball.start()
		
	#
	# Paint the ball
	#
	def paintEvent( self, event ) :
		
		# Set up the painter
		paint = QtGui.QPainter( self )
		paint.setRenderHint( QtGui.QPainter.Antialiasing )
		paint.setBrush( QtCore.Qt.red )
		
		# Get the ball position
		position = QtCore.QPoint( self.ball.position[0] * self.size().width(),
								  self.ball.position[1] * self.size().height() )
								  
		# Draw the ball
		paint.drawEllipse( position, 60, 60 )

	#
	# Close the widget
	#
	def closeEvent( self, event ) :

		# Stop the ball client
		if self.ball.isAlive() and self.ball.running :
			self.ball.running = False
			self.ball.join()
			
		# Close the widget
		event.accept()
		
	#
	# Toggle widget fullscreen
	#
	def ToggleFullScreen( self ) :
		
		# Show normal
		if self.isFullScreen() :
			self.showNormal()
		
		# Show fullscreen
		else : self.showFullScreen()


#
# Main application
#
if __name__ == '__main__' :

	# Start Qt application
	application = QtGui.QApplication( sys.argv )
	widget = BallWidget()
	widget.show()
	sys.exit( application.exec_() )


{'fraction_non_alphanumeric': 0.10328014184397163, 'fraction_numerical': 0.007387706855791962, 'mean_word_length': 3.07280385078219, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '697453', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2158, 'n_words': 648}