My First Newbie Python Program

Just started my Python class few days ago and it’s pretty cooler compared to PHP, I just got to notice that Python doesn’t like long codes it wants everything short and simple still compared to PHP. Stuffs like:

PHP:

<br />
&lt;?php<br />
for( $i = 1; $i &lt;= 10; $i++ ) {<br />
	$r[] = $i;<br />
}<br />
echo implode( '&lt;br /&gt;', $r );</p>
<p>

Is shorter and simple in Python:

<br />
#!/usr/bin/python3<br />
r = []<br />
for i in range( 1, 10 ):<br />
	r.extend( str( i ) )<br />
print( '&lt;br /&gt;'.join( r ) )<br />

Am not a pro yet, am just that newbie 😀 So, I spent my time today to write a simple log preparer. This prepares any log file to html where you can view it in browser, mainly I did it for Apache access logs but I see it can work in other logs. Test and give me feedback 🙂 My codes might be childish but I pray to learn more soon 🙂

Source Code:

<br />
#!/usr/bin/python3<br />
import sys, os</p>
<p>def prepare_log( log_dir = None, log_file = None, out = None ):<br />
	if log_dir is None:<br />
		log_dir = '/var/log/apache2/'<br />
	if log_file is None:<br />
		log_file = 'access.log'<br />
	if out is None:<br />
		out = 'access.log.html'<br />
	File = os.path.join( log_dir, log_file )</p>
<p>	try:<br />
		print( 'Opening file...' )<br />
		with open( File, 'r' ) as log:<br />
			print( 'Reading file...' )<br />
			log_data_rows = log.read().split( '\n' )<br />
			with open( out, 'w' ) as out_file:<br />
				print( 'Writing file to ' + out )<br />
				out_file.write( prepareHTML( log_data_rows ) )<br />
				print( 'Done' )<br />
	except FileNotFoundError as e:<br />
		print( 'Unable to open file &quot;' + File + '&quot; \r\nReason: ' + e.strerror )<br />
		sys.exit()</p>
<p>def prepareHTML( rows ):<br />
	html = &quot;&quot;&quot;<br />
	&lt;html&gt;<br />
		&lt;head&gt;<br />
			&lt;title&gt;Apache Access Logs&lt;/title&gt;<br />
			&lt;meta content=\&quot;text/html;charset=utf-8\&quot; name=\&quot;Content-Type\&quot;&gt;<br />
			&lt;style&gt;<br />
				table tr td {<br />
				  	padding : 7px;<br />
				  	border-bottom: 2px dotted #e7e7e7;<br />
			  	}</p>
<p>			  	table tr.correct, .correct {<br />
			  		color : #00ac77;<br />
			 	}</p>
<p>			  	table tr.failed, .failed {<br />
			  		color : #ef1616;<br />
			  	}</p>
<p>			  	table tr:nth-child(odd) {<br />
			  		background : #e7e7e7;<br />
			  	}<br />
			&lt;/style&gt;</p>
<p>		&lt;body&gt;<br />
			&lt;table&gt;&quot;&quot;&quot;<br />
	row = []<br />
	i = 1<br />
	for line in rows:<br />
		if( line ):<br />
			data = '&lt;tr&gt;&lt;td&gt; ' + str( i ) + '. &lt;/td&gt; &lt;td&gt;' + line + '&lt;/td&gt;&lt;/tr&gt;'<br />
			row.extend( data )<br />
			i += 1<br />
	html += ''.join( row ) + &quot;&quot;&quot;<br />
			&lt;/table&gt;<br />
		&lt;/body&gt;<br />
	&lt;/html&gt;&quot;&quot;&quot;<br />
	return html</p>
<p>try:<br />
	dir_path = str( input( 'Log directry path: ' ) )<br />
	file_name = str( input( 'Log file name' ) )<br />
	output = str( input( 'Output file (Default is access.log.html): ' ) )<br />
except ValueError:<br />
	print( 'Only strings needed' )</p>
<p>if not dir_path:<br />
	dir_path = None<br />
if not file_name:<br />
	file_name = None<br />
if not output:<br />
	output = None<br />
prepare_log( dir_path, file_name, output )

Save and run. Cheers!

Waiting feed back 🙂