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
#2
Sorry but the above post got posted by accident before it was finished. And now I can't edit it!!!

Sorry about the mistaken early post. I was interrupted and it got posted before I was done. The following is how the original OP was supposed to end.


Next is the Alysha_Read() Class.

Alysha_Read.py

This class reads a text file named "Commands.txt" located at the address contained in "File_Path".
It then converts the content of that file into a Python List and returns the list to the calling program.

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'
    # Raspberry Pi directory
	### 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(File_Path, '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
		
		"""

		
The Command.txt File contains the following information:

Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Task 7
Task 8
Task 9
Task 10

The program returns the following List as an output:

Output:
['Task 1','Task 2','Task 3','Task 4','Task 5','Task 6','Task 7','Task 8','Task 9','Task 10']
Questions

This program works as expected. My only question is to ask for any suggestions of advice on how I might improve on this software design. Specially in terms of setting up classes as independent files, and in terms of passing variables between these classes. Have I done things correctly? I'm just starting out programming in Python so I'll be surprised if I'm doing things correctly.

Any suggestions for improvements are welcome.

Thanks for reading.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking advice on dask distributed sawtooth500 4 415 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Unchangeable variables in a class? Calab 12 1,740 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,998 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 11,135 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 1,942 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Passing Variables between files. victorTJ 3 2,319 Oct-17-2020, 01:45 AM
Last Post: snippsat
  New user seeking help EdRaponi 2 51,016 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Class variables menator01 2 2,063 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,052 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Python 2.7 passing variables from functions zetto33 1 1,816 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