Python Forum
List data-member of object not updating inside loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List data-member of object not updating inside loop.
#3
Thanks for the help.
Now I got it and corrected myself like this:

import glob
from textblob import TextBlob

class FileClass(object):	
	def __init__(self,name):
		self.fileName = name
		self.lines = []
		self.overallSentiment = ""

	def updateLines(self,line):
		self.lines.append(line)

	def updateSentiment(self,sentiment):
		self.overallSentiment = sentiment


class FileClient(object):
	
	def __init__(self):
		self.fileObjects = []
		try:
			files = glob.glob('*.txt')
			for i,file_name in enumerate(sorted(files)):
				self.fileObjects.append(FileClass(file_name))
				with open(file_name) as f:
					for line in f:
						singleLine = line.strip(' \t\n\r').split(".")
						for sl in singleLine:
							if (sl.strip(' \t\n\r') and len(sl.strip(' \t\n\r'))!=1):
								self.fileObjects[i].updateLines(sl.strip(' \t\n\r'))
				
		except Exception as e:
			print("Error: File reading error ", e)

	
	def get_line_sentiment(self, line):
		analysis = TextBlob(line)
		return analysis.sentiment.polarity

	def calculateFileOverallSentiment(self,fileObj):
		positiveSentiment = 0
		neutralSentiment = 0
		negativeSentiment = 0
		for l in fileObj.lines:
			sentimentPolarity = self.get_line_sentiment(l)
			if sentimentPolarity > 0:
				positiveSentiment = positiveSentiment + 1
			elif sentimentPolarity == 0:
				neutralSentiment = neutralSentiment + 1
			else:
				negativeSentiment = negativeSentiment + 1

		numOflines = len(fileObj.lines)
		positiveSentimentPer = (positiveSentiment * 100)/numOflines
		negativeSentimentPer = (negativeSentiment * 100)/numOflines
		neutralSentimentPer = (neutralSentiment * 100)/numOflines

		print("Positive Sentiments: ", positiveSentimentPer)
		print("Negative Sentiments: ", negativeSentimentPer)
		print("Neutral Sentiments: ", neutralSentimentPer)

		if positiveSentimentPer >= 33.33:
			fileObj.updateSentiment("positive")
		elif neutralSentimentPer >= 33.33:
			fileObj.updateSentiment("neutral")
		elif negativeSentimentPer >= 33.33:
			fileObj.updateSentiment("negative")
		else:
			fileObj.updateSentiment("CheckTheFuntion")
		return fileObj

	def set_sentiments(self):
		for f in self.fileObjects:
			print("Processing for: ", f.fileName)
			f = self.calculateFileOverallSentiment(f)

	def get_sentiments(self):
		for f in self.fileObjects:
			print("Overall sentiment of the file", f.fileName ," is: ", f.overallSentiment)
			for l in f.lines:
				print(l)
			print("*****************************************************")
	       

def main():
	
	fileClient = FileClient()
	fileClient.set_sentiments()
	fileClient.get_sentiments()

if __name__ == "__main__":
	# calling main function
	main()
And I am getting the result as expected.

I want to ask a question that if I declare a class variable and an instance variable having same name like I have done in "FileClass" class, are they different or same ?

and thanks again.
Reply


Messages In This Thread
RE: List data-member of object not updating inside loop. - by Sagar - Aug-30-2017, 01:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 395 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Variable definitions inside loop / could be better? gugarciap 2 430 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,681 Nov-07-2023, 09:49 AM
Last Post: buran
  Class member become static Quasar999 1 670 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  Possible to create an object inside another object that is visible outside that objec MeghansUncle2 17 2,199 May-25-2023, 02:08 PM
Last Post: MeghansUncle2
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 787 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,340 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  add object and name in list 3lnyn0 4 1,267 Nov-24-2022, 07:33 PM
Last Post: buran
  Help adding a loop inside a loop Extra 31 4,517 Oct-23-2022, 12:16 AM
Last Post: Extra
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,870 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020