Code Ghar

December 20, 2007

Project Euler Solutions

Filed under: code — Tags: — hs @ 8:04 pm

Head to Project Euler to solve mathematical problems using programming. I am cheating. I should not post these solutions but I want to get feedback on the quality of solutions implemented. The results are correct but are they generated from “quality” code? Only by sharing the code can I and others know.

Why am I using Python? Because this way I can learn how to program in it. Plus I get to learn more math.

Problem 1


import sys
def multipleof (multiple = 1, integer = 1):
	try:
		return (int(int(multiple) % int(integer)))
	except:
		print 'error for multiple %s and integer %s' % (multiple, integer)
		print 'error was', sys.exc_info()[0]
		raise
sum = 0
for multiple in range (1, 1000):
	try:
		if multipleof (multiple, 3) == 0 or multipleof (multiple, 5) == 0:
			sum += multiple
	except:
		pass
print 'The sum is ', sum

December 19, 2007

Learn Python

Filed under: discussion — Tags: — hs @ 4:51 pm

I haven’t had too much experience in Python. I start learning it then something else comes up. After some time I pick it up again but something else happens. So I know of Python and know some Python but don’t actually know Python. If you are in the same boat, these resources will help you very much in getting to the next level.

Byte of Python. This is a free online book which teaches you Python.

Dive Into Python. This book is a very good place to learn Python.

Building Skills in Python. This is another book which will help you with Python.

The Python Challenge. According to the website: “Python Challenge is a game in which each level can be solved by a bit of (Python) programming.”

Project Euler. This website provides mathematical problems that you have to solve by programming. And if you solve them in Python, you kill two birds with one stone: learn math and learn Python.

The way I would like to really learn Python is to read the books, try their examples, and then work on the challenges that make you not only think of algorithms but also how to implement them. Such comprehensive learning is what we should all want.

December 11, 2007

Simple Heartbeat System

Filed under: code — Tags: — hs @ 9:55 pm

The purpose of this system is to make sure a group of computers can keep track of each other’s well being. If one or more systems is unable to respond to a network request, we immediately assume something went wrong.

This combination of client-server scripts has been written in Python. All it does is this: a client sends a message periodically to the server. The server echoes back whatever the client sent. If the client has any problems connecting with the server or does not receive an echo, it sends an email to whoever you want to and then quits.

I tried to make the client script keep running after sending an email but then decided against it. The reason was that I would prefer to manually check the problem and fix it rather than have the client try to fix it itself.

The code used here was inspired by: (a) Sending email in python; (b) Simple echo server; and (c) Simple echo client.

You will need two files on the client side: echoclient.py and message.txt where message.txt will contain the message to be emailed. The server will only need echoserver.py and no other file.

echoserver.py

#!/usr/bin/python
import socket
host = ''
port = 550000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while (1):
	client, address = s.accept()
	data = client.recv(size)
	if data:
		client.send(data)
	client.close()

echoclient.py

#!/usr/bin/python
import socket
import time
import smtplib
smtpserver = 'smtp.domain.com'
AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
smtpuser = 'user@domain.com'  # for SMTP AUTH, set SMTP username here
smtppass = 'password'  # for SMTP AUTH, set SMTP password here
RECIPIENTS = ['user1@domain.com','user2@domain.com','user3@domain.com']
SENDER = 'user@domain.com'
MESSAGE_TO_SEND = 'message.txt'
host = '192.168.168.100'
port = 550000
size = 1024
while (1):
	try:
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((host,port))
		s.send('Hello, world')
		data = s.recv(size)
		s.close()
		print 'Received from ', host, ': ', data
		time.sleep (10)
	except:
		print 'There has been some problem with the connection... Sending email(s) to ', RECIPIENTS
		mssg = open(MESSAGE_TO_SEND, 'r').read()
		session = smtplib.SMTP(smtpserver)
		if AUTHREQUIRED:
			session.login(smtpuser, smtppass)
		smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
		if smtpresult:
			errstr = ""
			for recip in smtpresult.keys():
				errstr = """Could not deliver mail to: %s
				Server said: %s
				%s
				%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
			raise smtplib.SMTPException, errstr

message.txt

To: user@domain.com
From: user@domain.com
Subject: Heartbeat Problem

The server on this IP is not responding to heartbeat. Please investigate.

Please remember to put an empty line between the “Subject” line and the actual message in message.txt.

I assumed the authors wanted their code to be used freely because they had posted this code for the sole purpose of teaching people like myself how to do this stuff. If this is not the case, I would be glad to remove your copyrighted content.

Older Posts »

Blog at WordPress.com.