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
#3
UPDATE:

I just restructured the entire program just figuring things out on my own. I'm much happier with the the following classes and methods. So I thought I'd post what I've done just in case anyone is interested.

My Original Folly
I was originally using the Class itself as a Method. And that certainly worked. But it's not what I wanted. I actually wanted an entire class of methods. So that's what I have now. And I'll try to explain what I've changed from the original plan. This may be helpful for someone else who might want to do something similar. This example if quite small so it should be easy to understand.

The Directory Structure for the Source Code.

I've changed my previous directory structure to the following:

Alysha <---- Project directory.
--> Alysha.py <---- Main() Python program.
--> Alysha_Pass <---- Directory within the Alysha directory. (Changed the NAME of this directory)
-------> __init__.py <---- *IMPORTANT empty file required by Python.
-------> Alysha_Pass.py <---- Alysha_Pass() class. (Changed the the NAME of the actual Class)


And now for how the code works differently:

Alysha.py

Now I call the method Alysha_Read() that resides within the Alysha_Pass Class.
Now everything is done on line line of code:
Tasks = Alysha_Pass().Alysha_Read()

But far more importantly, I can have many methods contained within the Alysha_Pass Class.

(see the coding for Alysha_Pass.py)

""" 
Robot Communications Program. 
"""

# Need to import the class Alysha_Pass()
from Alysha_Pass.Alysha_Pass import *

def main():
	"""
	This is Alysha's Main Module
	"""
	
	# Call Alysha_Read Class to read the Commands.txt file
	# And pass to it a new list named "Tasks"
	Tasks = Alysha_Pass().Alysha_Read() 
	
	# --- The following are just programming tools.
	print "\nThe resulting Task List\n"
	print Tasks	

	# --- Call to Alysha_Write() just to see it working.
	Alysha_Pass().Alysha_Write("Dummy Message from Alysha_Write")

# --- Progamming Tools -----
# 	help(Alysha_Write)
#	help(Alysha_Read)
#	help(main)
	
	# Only need the following on SharpDevelop to keep, 
	# the terminal window open to read it
	a = int(raw_input("Press enter:"))
	
	# This program is done for now but will eventually,
	# loop eternally as this will become Alysha's main brain.
	return 0

if __name__ == '__main__':
	main()
Alysha_Pass.py

Notice in this new class I no longer assign a variable to the entire class using the __init__() method.

Instead, I have the method Alysha_Read() simply return the list using its return function.
There is no need to pass anything to the Alysha_Read() method.

Also, I'm able to include a method called Alysha_Write() that will require a message be passed to it. This method will eventually write to a different file to serve as the reply from the Raspberry Pi back to the Notebook computer.

This is what I wanted and apparently this is the correct way to do this. Far better than what I originally started out trying to do.

I've included a very small example of passing a message to Alysha_Write() to to have it show that it's working. Eventually this method will actually write to a file.

The nice thing about using the class Alysha_Pass() in this way is that I can add as many methods as I would like that have to do with passing information between this robot and any other devices or robots. It's nice to have all these classes separate and independent. And this was my main goal.

When all is said and done I'm sure I'll have lots of Classes containing many methods each. And now I know how to do this.

class Alysha_Pass:
	
	def Alysha_Read(self):
		""" 
		---- Alysha_Pass().Alysha_Read(self) Method to read the Commands.txt ----
		This program reads the text file in the Robo_Pass folder on the Raspberry Pi
		It converts that text file into a Python List and then passes the list
		back to the program that called this class.  
		The passing list is called	"Task_Commands"
		This is done to place the commands in a List Variable. (or String Array),
		that can then be accessed via indexes. Task_Commands[i]	
		This Class must be called using Alysha_Read(list),
		where list should be an empty list. 
		Then upon return you can obtain the Task_Commands by using:
		Commands = Alysha_Read.Task_Commands
		"""
			
		### 	--- Addresses for Files -------
		# --- File path on the Robot Overseer computer.
		File_Path = 'C:/100_Robo_pass/Commands.txt'
		# --- File path on Raspberry Pi
		### File_Path = /home/pi/Robo_pass/Commands.txt'
				
		# ---------- Begin Reading Commands.txt Routine --------------
		
		# Create a string array or list named Raw_Task_Commands
		# This reads the entire Command File
		# Required to be able to count how many commands there are.
		Raw_Task_Commands = []
		# Create a new string array  for the individual clean commands.
		Task_Commands = []
		# Read the Commands.txt file using the Ptyhon "open" function
		# Create a file-reading object named "Stream_Reader_1".
		# ---
		# --- B E W A R E ! ! ! ---- use the correct filename for each robot.
		# ---
		Stream_Reader_1 = open(File_Path, 'r')
		# Read in the entire file into "Raw_Task_Commands" array.
		Raw_Task_Commands = Stream_Reader_1.readlines()
		# The following print os for debug only.
		print "Original File Lines\n"	
		# Break out the commands, get the count, and clean them up.
		# (note) len(Raw_Task_Commands) is the count.
		for i in range(0,len(Raw_Task_Commands)):
			# Isolate the first command.
			raw_Command_String = Raw_Task_Commands[i]
			# Strip off the carriage returns and line feed.
			raw_Command_String = raw_Command_String.strip()
			# Append this into the new clean Command array.
			Task_Commands.append(raw_Command_String)
			# won't need the following print on the final version.
			# ---- For Debug Only ----	
			print raw_Command_String
		# Close the file!	
		Stream_Reader_1.close
		# Return the entire clean List of tasks. 
		return (Task_Commands)
		
	def Alysha_Write(self, Message):
		"""
		
		---- Alysha_Pass().Alysha_Write(self, Message) Method ----
		This method will eventually write to a message file to be,...
		returned to the Overseer computer.
		"""		
		print "\n" , Message , "\n"
	
	
	def __init__(self):
		pass
Summary

I made a lot of progress on this on my own in just one day! I'm feeling good about this. But I'm always open to any constructive comments or suggestions too. But I'm really happy with what I've done thus far.

Keep in mind that if you want to create a similar system, you need to make sure you include a empty __init__.py file in the directory of the classes. Otherwise Python will give you an error when you try to import from that directory.

Also for my example above, you need a Commands.txt file available to read. I've already described what that file contained in a previous post, and that hasn't changed.

This program takes the content of that text file and converts it to a nice clean Python list so I can access the commands or tasks via indexes one at a time. That was the goal of this initial project.

But a far bigger goal was to learn how to set up classes and methods in their own directories and to be able to pass variables between them. I think I have that all figured out now. So this was a major learning experience for me.

I'm just learning Python. This was my first program after "Hello World!". Big Grin
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking advice on dask distributed sawtooth500 4 498 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Unchangeable variables in a class? Calab 12 1,873 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 4,112 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 11,490 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,980 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Passing Variables between files. victorTJ 3 2,370 Oct-17-2020, 01:45 AM
Last Post: snippsat
  New user seeking help EdRaponi 2 56,429 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Class variables menator01 2 2,100 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,101 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Python 2.7 passing variables from functions zetto33 1 1,859 Mar-19-2020, 07:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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