Page 1 of 1

My first python

Posted: Mon May 15, 2006 7:19 am
by shiznatix
Ok so I am having to learn some Python for work here and so I started messing around with something. I just made a simple news script and I think its alright but I am not sure if I am doing everything the best way that I should be doing it. Inclosed is my code but I would want a little bit of input about if it's any good or not (idea wise). If it is, then what is something else stupid like this that I could do to get more of a grasp on this language. (ps, this is my very first day ever doing python so nothing too insaine)

Code: Select all

#! /usr/bin/env python

#the main class
class news:
	#variable file is the text file where my news is stored
	file   = '/home/homedirs/andrew/Desktop/python/news/content.txt'
	#start with no output
	output = ''
	#destroy is exiting the program so we dont want to exit yet so we keep it at false
	destroy = False
	
	#function askDirection will ask the user what direction in the script they want to go to
	def askDirection(self):
		#print out the nice 'please select one' menu
		print 'Welcome to Pnatix News!\nPlease make a selection:\n1 - Read News\n2 - Add News\n3 - Delete News\n0 - Exit'
		#get the input
		self.selection = input('#: ')
		#call the function that checks what they input was
		self.gotoSelection()
	
	#controls where you go in the script based on your input
	def gotoSelection(self):
		#option 1 (Read News)
		if self.selection == 1:
			self.readNews()
		#option 2 (Add News)
		elif self.selection == 2:
			self.addNews()
		#option 3 (Delete News)
		elif self.selection == 3:
			self.delNews()
		#option 0 (exit the program)
		elif self.selection == 0:
			self.destroy = True
	
	#function readNews just reads the file and makes self.output equal to the contents of the file
	def readNews(self):
		#open the file
		read = open(self.file, 'r')
		#get all of the lines of the file
		lines = read.readlines()
		
		#a new line to make a nice buffer stuff
		self.output = '\n'
		
		#for each line assign the value of that line to the output
		for line in lines:
			if line.isspace() == False:
				self.output = self.output+line
		
		#add a last new line to it
		self.output = self.output+'\n'
		#close the file
		self.close(read)
	
	#function addNews gets the news information and adds it to your news file
	def addNews(self):
		#get your title
		self.title = raw_input('Enter a title: ')
		#get your body
		self.body  = raw_input('Enter the body: ')
		
		#replace the 'reserved' characters so as to not mess it up later
		self.title = self.title.replace('::', '')
		self.body  = self.body.replace('::', '')
		
		#open the file for writing
		write = open(self.file, 'a')
		#write to the end of the file
		write.write('::'+self.title+'::\n'+self.body+'\n')
		#close the file
		self.close(write)
		
		#make your output equal to a good message
		self.output = 'News Added Successfully!'
	
	#function delNews will go through each new item and ask you if you want to delete it
	def delNews(self):
		#open the file
		read = open(self.file, 'r')
		#get all the lines from the file
		lines = read.readlines()
		
		#go through each line
		for line in lines:
			#if there are 2 :: in there then it is the title of a new article so ask then about it
			if line.count('::') == 2:
				#print out the raw title
				print line.replace('::', '')
				#ask if they want to delete this new item
				confirmDel = raw_input('Delete this article? (yes/no): ')
				#if they do then delete it
				if confirmDel.lower() == 'yes':
					self.delArticle(line)
					break
		#close the file
		self.close(read)
	
	#function delArticle takes the article that you want to delete and deletes it from the file
	def delArticle(self, deletingline):
		#open the file
		read = open(self.file, 'r')
		#get all the lines of the file
		lines = read.readlines()
		
		#newFile is the text that is going back into the file which is the edited stuff
		newFile = ''
		#found = False now because we have yet to find the article to delete within the file
		found = False
		#go through each line
		for line in lines:
			#if the line is the same as the article title that we want to delete...
			if line == deletingline:
				#we found it so found = True now
				found = True
			#else if we already found the article
			elif found == True:
				#if now we have already found the article but now this next line is the beginning of the next article we go here
				if line.count('::') == 2:
					#we add this line to the file that is going back
					newFile = newFile+line
					#found is no longer true because we have already gone through the whole original article
					found = False
			#if found is false then we just add on to the new file
			elif found == False:
				newFile = newFile+line
		
		#close the file
		self.close(read)
		
		#open the file for writing and delete everything from it
		write = open(self.file, 'w')
		#write to the file the new information
		write.write(newFile)
		#close the file
		self.close(write)
		#make the output a nice message for the kids
		self.output = 'File edited successfully!'
	
	#function close closes the open file that you pass to it
	def close(self, instance):
		instance.close()
	
	#function destructor just makes output empty. this could be expanded upon later if need be
	def destructor(self):
		self.output = ''

#make a new instance of our beloved class
news = news()

#while we don't want to stop the program
while news.destroy == False:
	#find the direction they want to go
	news.askDirection()
	
	#print out the output (if any)
	print news.output
	
	#empty the output (if any)
	news.destructor()

Posted: Mon May 15, 2006 11:43 am
by AshrakTheWhite
our definitions of insane vary ;)


does it work? if it does: Sweet :)


-M.K