Python Forum
Class Modules, and Passing Variables: Seeking Advice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Modules, and Passing Variables: Seeking Advice
#17
(Mar-01-2018, 09:55 PM)Larz60+ Wrote: What I posted earlier is correct.
You have changed it.

it should read:
import AlyshaPass
task = AlyshaPass.AlyshaPass()
the whenever you are calling a method in AlyshaPass, use
task.read()

Thanks for the suggestion.

I prefer to use names that have meaninging for me.

So I have chosen to rewrite my main program as follows:

  1. First I've changed main() to Alysha() because that's what I want this to be called.
  2. I took your advice and used the specific import format.
  3. I then set the class name to the pointer AlyshaPass
  4. I'm using title case for this to remind me that this is pointing to a class
  5. This removes the need to use parentheses after AlyshaPass in the actual calls.
  6. I don't want to use just "ap", or "task" for this pointer because I'll forget what that means.
  7. When I see AlyshaPass.something I'll know that this is pointing to the AlyshaPass() class.
  8. According to PEP 8 style convention class names are supposed to be capitalized.

So I'm very happy with this code as written below and it seems to work just fine.

from alysha_pass.AlyshaPass import AlyshaPass
AlyshaPass = AlyshaPass()


def Alysha():
		# get tasks
	tasks = AlyshaPass.read()
	print tasks
		
		# write reply
	AlyshaPass.write("Dummy Message")
	
	
		# for documentation purposes
	help(AlyshaPass.read)
	help(AlyshaPass.write)
	
		# Just a temporary break for debugging
	a = int(raw_input("Press enter:"))
	
	return 0
if __name__ == '__main__':
	Alysha()
 

Just in case anyone is interested in the full project here's the modified AlyshaPass.py module

Just as a heads-up I've changed the following items:

  1. The text file I'm working with is now called tasks.txt (formerly commands.txt)
  2. The file_path and name are also now defined in Alysha() and passed to AlyshaPass.read()
  3. In the actual read() method I'm made some major changes.
  4. The main change was to add the ability to pass in the file_path.
  5. Basically I do everything on a single line now (line 37 in the code below)
  6. I've also renamed the variables to "words" instead of "tasks"
  7. The reason for this is that I now see where I can make this a more generalized method.
  8. Also hopefully everything has been cleaned up to meet PEP 8 style conventions.

AlyshaPass.py

class AlyshaPass():
	'''
	---- AlyshaPass class ---
	Contains read() and write() methods for reading and writing text files.
	'''
	
	def __init__(self):
		'''
		Not currently using the inititation method.
		'''
		pass
	
	def read(self, file_path):
		'''
		
		---- AlyshaPass.read() method to read the file_path.txt file ----
		This program reads the text file pointed to by file_path
		It then converts text into a Python List named "words".
		and then passes the list back to the calling program using return (words)
		'''
			# ---------- Begin Reading file_path.txt Routine --------------
			
			# Create a raw string array to hold entire file contents. 
			# Required to be able to count how many words there are.
		words_raw = []
			# Create a new string array to hold the individual words.
		words = []
			# Read the file_path.txt file using the Ptyhon "open" function.
			# Create a file-reading object named "stream_reader"
		stream_reader = open(file_path, 'r')
			# Read in the entire file contents into a temporary varible.
		words_raw = stream_reader.readlines()
			# len(words_raw) is the number of words in the file.
		for i in range(0,len(words_raw)):
				# Strip off the carriage returns and line feed,...
				# and create the indexed words[i] list of words.
			words.append(words_raw[i].strip())
			# Close the file!	
		stream_reader.close
			# Pass the words list back to the calling program.
		return (words)
			
	def write(self, message):
		'''
		
		---- AlyshaPass.write() method will write to a text file ----
		The name of the file is optional and may even include multiple names.
		'''
		print "\n" , message , "\n"
			
	
Reply


Messages In This Thread
RE: Class Modules, and Passing Variables: Seeking Advice - by Robo_Pi - Mar-01-2018, 10:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help seeking for prediction of fixed effect model gotodockforevildragon 1 187 Jun-23-2024, 08:09 AM
Last Post: johnthomson112
  Seeking advice on dask distributed sawtooth500 4 546 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Unchangeable variables in a class? Calab 12 1,908 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 4,139 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 11,616 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,996 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Passing Variables between files. victorTJ 3 2,387 Oct-17-2020, 01:45 AM
Last Post: snippsat
  New user seeking help EdRaponi 2 58,446 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Class variables menator01 2 2,123 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,110 Mar-25-2020, 04:51 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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