Mar-02-2018, 02:21 PM
(Mar-02-2018, 07:04 AM)Gribouillis Wrote: At line 39 you're not calling the close method! Use close()
.
Thanks for catching that! None of my IDE's caught that. And it doesn't seem to have caused any problems running the program. It doesn't appear to lock the file as open anyway. None the less I'm glad you caught that. I fix that right away.
(Mar-02-2018, 07:04 AM)Gribouillis Wrote: Note that the read method can be shrunk to
def read(self, file_path): with open(file_path, 'r') as stream_reader: return [line.strip() for line in stream_reader if line.strip()]
Yes, I'm aware that there are endless clever ways to do things using minimal coding. I have mental problems and cannot think as clearly as I used to. So I like to write my code out in a way where I can explain everything I'm doing in comments. So I'll often chose to write things out in code the way I'm thinking about them, rather than just trying to do everything on one line. I didn't mind writing
words.append(words_raw[i].strip())
because that's pretty clear what's going on.But on the return line I like to have
return (words)
, because that instantly tells me precisely what I'm returning. I can see that I'm returning the list named "words". And I may have other data that I might return as the program grows. So I like to keep the return statement containing only the names of the variable types I'm actually returning. This way I can just glance at the return statements in a method and instantly see what the method returns. This is my own personal coding convention that I've always followed. I'm new to Python, but I've been programming in other languages for years. So I have my own style that I stick with.
(Mar-02-2018, 07:04 AM)Gribouillis Wrote:close()
is run automatically at the end of thewith
block. This code also removes blank lines if any.
Yes, I've been thinking of going with the
with
statement. But again, I have traditionally always preferred to manually close files that I open. This tells me more clearly in the code precisely where I'm done working with the file. In fact, I should actually move the stream_reader.close()
up to just before the for loop, because I'm actually done reading the file at that point.My code would then look like this:
(moving stream_reader.close()
up to the point where I'm actually done reading the file. Everything I do after that no longer requires the file to be open. # ---------- 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() # Close the file stream_reader.close() # 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()) # Pass the words list back to the calling program. return (words)Note: I want my code to be easily readable to even the most neophyte programmer. So making the code easy to understand is my goal. I have no need to make it compact.
I have also chosen to indent all of my comments from the main code block. I do this because I have failing eyes and when the comments are lined up with the code it's hard for me to see which lines are comments and which lines are code. So I just indent all the comment lines and this leaves the actual code lines sticking out where they are easy to notice.