(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:
- First I've changed main() to Alysha() because that's what I want this to be called.
- I took your advice and used the specific import format.
- I then set the class name to the pointer AlyshaPass
- I'm using title case for this to remind me that this is pointing to a class
- This removes the need to use parentheses after AlyshaPass in the actual calls.
- I don't want to use just "ap", or "task" for this pointer because I'll forget what that means.
- When I see AlyshaPass.something I'll know that this is pointing to the AlyshaPass() class.
- 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:
- The text file I'm working with is now called tasks.txt (formerly commands.txt)
- The file_path and name are also now defined in Alysha() and passed to AlyshaPass.read()
- In the actual read() method I'm made some major changes.
- The main change was to add the ability to pass in the file_path.
- Basically I do everything on a single line now (line 37 in the code below)
- I've also renamed the variables to "words" instead of "tasks"
- The reason for this is that I now see where I can make this a more generalized method.
- 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"