#!/usr/bin/python3.1 -u
# Name:	Husain AlKhamees

class MySubs:

	def __init__(self):	# self must exist. This is Python Coding Convention
		self.subs = {}	# self.subs is an instance variable

	def insert(self, word, subst): # insert a word and it's substitution into the dictionary
		self.subs[word] = subst

	def find(self, word): # search dictionary for substitution and return 'word' if not found
			return self.subs[word]	# return value from dictionary if found

		except KeyError:	# throwing an exception in case of error
			return word		# return same word if not found

subs test

#!/usr/bin/python3.1 -u
# Name:	Husain AlKhamees

import sys	# sys.argv
import re	# Regular Expression

import subsClass

subObj = subsClass.MySubs()	# creating a new object of type MySubs

iF = sys.argv[1]	# store the substitution file name
oF = sys.argv[2]	# store the toBeSubstituted file name

iF = open(iF, "r")
str = iF.readlines()	# reading all lines at once and store in a list

for line in str:
	line = line.rstrip("\n")
	(sub1, sub2) = re.split(r':',line)
	subObj.insert(sub1, sub2)	# filling up the dictionary in the created object

oF = open(oF, "r")
str = oF.readlines()	# reading all lines at once and store in a list

for line in str:
	line = line.rstrip("\n")
	splittedLine = re.split(r'[^\'\w\d]', line)	# splitting the lines word by word so we can use the find() function

	print("Before: ",line)

	for word in splittedLine:
		line = re.sub(word, subObj.find(word), line)	# this is ineffecient, but I understand that we are enforced to use find()

	print("After: ", line)


it's:it is
don't:do not

to be substituted

it's better to give than to recieve! So, don't recieve unless it's necessary!
don't wait till the last moment!
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: