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
#19
(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 the with 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.
Reply


Messages In This Thread
RE: Class Modules, and Passing Variables: Seeking Advice - by Robo_Pi - Mar-02-2018, 02:21 PM

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