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
#1
Class Modules, and Passing Variables: Seeking Advice

Introduction
Hello, this is my first post and I'm just learning Python so please forgive my ignorance. I'm just starting a huge project that will quickly become quite complex. For this reason I would like to get it started off on a solid foundation. The following program runs and does what I expect. However, I pieced this together using newly discovered ideas so there may be far better ways to do this. I'm seeking suggestions for how I might make make the code better. SPECIFICALLY - in the area of creating Class Modules and Passing Variables between them. I'm aware that I have not yet programed against file reading errors. I plan on doing that using try-exception, but I left that out for now for simplicity sake. Right now I'm just working on creating totally independent classes that can pass variables back to the original main program.

Short Explanation of the Project
I'm building a robot that uses a Raspberry Pi for a brain. I want to be able to communicate with this Raspberry Pi via WiFi using my Notebook computer. I have decided to do this by simply passing a small text file back and forth between them. The robot will only need a few English word commands it will be able to carry out the tasks on its own from there. So the following program is extremely simple. It just reads the text file and converts the content into a Python List.

My Immediate Goal for Now
Below there are two programs or "classes". The first is Main() the second is Aylsha_Read(). Main() will eventually become an infinite looping program that will be the robot's continually conscious brain. Main() will eventually be calling many other classes. Alysha_Read() is simply the first class I've written. I want to keep all classes that are not required in Main() as totally separate files in separate directories. This is why Alysha_Read is in a separate namespace (or folder) also called Alysha_Read. I don't want Alysha_Read() to be a method inside Main(). Keeping all my sub-classes in separate files in separate directors will become extremely important as this project grows. I can imagine this project become extremely huge as time goes on.

So for this reason I would like to get this system down pat at the very beginning of the project.

Here's my current Source File Directory Hierarchy.
(NOTE: This places Alysha_Read() in the Alysha_Read namespace)

Alysha <---- Project directory.
--> Alysha.py <---- Main() Python program.
--> Alysha_Read <---- Directory within the Alysha directory.
-------> __init__.py <---- *IMPORTANT empty file required by Python.
-------> Alysha_Read.py <---- Alysha_Read() class.


*note: every sub-directory must contain an empty __init__.py file in order for Python to recognize the directory as a valid Namespace. So if you should try to duplicate this system don't forget to create this empty file or it won't work.

And now let's take a look at the code I've written thus far.

Alysha.py

All this code does is pass an empty list named "Tasks" to Alysha_Read() using Alysha_Read(Tasks).
Then it obtains the actual task list using Tasks = Alysha_Read.Task_Commands.
Finally it prints out the newly acquire Task list.
A very simple program to be sure.
However this does require the directory hierarchy described previously in order for the import statement to work correctly.

""" 
Robot Communications Program. 
"""

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

def main():
	"""
	This is Alysha's Main Module
	"""

	# Need to create the following string array or list:
	Tasks=[]
	# Call Alysha_Read Class to read the Commands.txt file
	# And pass to it the empty list named "Tasks"
	Alysha_Read(Tasks)
	# Get the Task_Commands after the return,...
	# Update the previous empty Task list with the new list.
	Tasks = Alysha_Read.Task_Commands
	
	# --- The following are just programming tools. 
	print Tasks
	# 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()
Next is the Alysha_Read() Class.

Alysha_Read.py

class Alysha_Read():
	""" 
	---- Alysha_Read:Alysha_Read class 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 Commands.txt file -------
    # Overseer directory
	File_Path = 'C:/100_Robo_pass/Commands.txt'

	### 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 to for the individual clean commands.
	Task_Commands = []
	# Read the Commands.txt file on HP-Elite using the Ptyhon open function
	# Create the file-reading object named "Stream_Reader_1".
	# ---
	# --- B E W A R E ! ! ! ---- use the correct filename for each robot.
	# ---
	Stream_Reader_1 = open(HP_Elite_1, 'r')
	# Read in the entire file into "Raw_Task_Commands" array.
	Raw_Task_Commands = Stream_Reader_1.readlines()
	# 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
	
	def __init__(self, Task_Commands):
		""" 
		This is my initiation function in Alysha_Read
		Task_Commands is assigned to this Class
		as a passable list array using this __init__ function
		
		"""

		
Reply


Messages In This Thread
Class Modules, and Passing Variables: Seeking Advice - by Robo_Pi - Feb-28-2018, 02:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking some script design advice... involves asyncio, threads, multiprocessing sawtooth500 1 935 Jul-05-2024, 10:39 PM
Last Post: AdamHensley
  Seeking advice on dask distributed sawtooth500 4 1,824 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Unchangeable variables in a class? Calab 12 3,894 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 6,948 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 20,920 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 2,745 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Passing Variables between files. victorTJ 3 3,191 Oct-17-2020, 01:45 AM
Last Post: snippsat
  New user seeking help EdRaponi 2 72,276 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Class variables menator01 2 2,870 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,781 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