#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    USA
    Posts
    12
    Rep Power
    0

    How should I divide this single Python program into two separate *.py files?


    This question is basically, "What's the relatively easiest and/or quickest way to divide this program into two *.py files (modules)?" The way that I wrote the question below is, I'm sure, much longer than it needs to be. Being new to the "point of view" of an advisor, I was unsure of what I could leave out, and so I had second thoughts when I started to abridge it all...

    It's still a small program, so I should probably not even consider dividing it into separate files at this point (I don't know), but I'm just learning Python still, and also getting ready to code the main part of the program. This phase will take much longer, and I thought it would be easier/more readable to me if I just divide up the code into two separate files, allowing me to concentrate more on this main part that's next. I had a couple of ideas on how to proceed, and searched around on the internet awhile for some clues; but my understanding of what all that I found on the topic is such that it would probably set me back a week trying the few different methods presented, and learning from my inevitable mistakes along the way. So I'm asking for advice on the matter now, so that I'm not completely held up in the process later on down the road. To give a clearer picture of where I'm at, I listed the code of the two separate files that I have below, along with some intro and summary comments to go along with each one.

    The entire program, "BOL Hand History Translator", will be a program that reads *.xlsx files (Excel 2007 workbook) that contain a poker hand history and then manipulates the data and saves it to a *.txt file, a file and presentation format that will then work with some invaluable poker software (hand history converters, hand replayers, poker databases, etc) and enable me to more easily study my game. The entire program is meant to sit in the same DIR as the *.xlsx files that it will process.
    The first file (listed below), "TestDriveGUI.py", is a small GUI part of the whole program that just gets and lists the current working directory for the user to see. Then, the program also lists the contents of that current working directory for the user to see, so that he/she can be sure that the program is in the same directory as the files to be processed. Once, the user sees which DIR the program is running from, along with the other contents of the current working DIR, the user can then either close the program or click on a button labeled "TRANSLATE" to continue on and have the program process the eligible files that are in the current working DIR. This code is:

    Code:
    #####  BEGIN TestDriveGUI.py  ##########
    
    import sys
    import os
    from Tkinter import *
    
    Hht = Tk()
    Hht.geometry("999x650+30+20")
    Hht.title("BOL Hand History Translator")
    
    S1 = "        Use this BOL Hand History Translator program to translate your BOL hand histories from their native *.xlsx format into 
    
    "
    S2 = "a more compatible *.txt format.  Then, the newly created *.txt hand histories will work with most hand history converters, "
    S3 = "poker forums, hand replayers, and poker databases."+"\n"
    S4 = "        If you use this program often enough, you might want to organize your hand history files like this:    "
    S5 = "1)  Create a new folder and call it \"AllHH's\".  Create three subfolders inside of that one and call them "
    S6 = "\"UntranslatedHH's\", \"TranslatedHH's\", and \"TextHH's\".  Put this program inside of the folder called "
    S7 = "\"UntranslatedHH's\".    "
    S8 = "2)  When you receive a new batch of BOL *.xlsx hand history files, put them in the folder called \"UntranslatedHH's\", "
    S9 = "along with this program.    "
    S10 = "3)  To translate those newly received BOL *.xlsx hand history files into a more compatible *.txt format, run this program "
    S11 = "on them (both the file(s) to be translated AND this program must be in the same folder for it to work).    "
    S12 = "4)  When this program is finished running, you'll see some files that are *.xlsx and others that are *.txt.  Move the "
    S13 = "*.xlsx files to the folder called \"TranslatedHH's\".  Move the *.txt files to the folder called \"TextHH's\".    "
    S14 = "5)  Now, you can improve your game by using the widely compatible *.txt translations of your hand(s).  You can now import "
    S15 = "them into poker databases; put them into hand history converters and then post the BB code into forums; and/or put them "
    S16 = "into a hand replayer and review your game there."+"\n"
    S17 = "        NOTE - this program will ONLY attempt to translate any files if they have an extension of *.xlsx.  If they are of the 
    
    "
    S18 = "*.xlsx type, but not BOL hand histories, then they will also not be processed.  Either way, if a file is or is not a BOL "
    S19 = "hand history file, ALL files that are successfully processed are COPIED and translated.  In other words, any original "
    S20 = "files will be preserved.  However, it's still better to be safe than sorry.  To be cautious, make sure that nothing else "
    S21 = "besides this program and the files you wish to be translated are in the same folder together.  Then as you get more "
    S22 = "comfortable, you can ignore this rule.  Now, run this thing and go review some hands.  You might learn something.........."
    S23 = "...............ENJOY!"
    HhtMessage = Message(Hht, text=S1+S2+S3+S4+S5+S6+S7+S8+S9+S10+S11+S12+S13+S14+S15+S16+S17+S18+S19+S20+S21+S22+S23, width=979)
    HhtMessage.pack(fill=X, side=TOP)
    
    CWD = os.getcwd()
    CWDlength = len(os.listdir(CWD))
    CWDFname = os.listdir(CWD)
    
    HhtLabel1 = Label(Hht, text="The current working directory, from which this program instance is running, is:", padx=5, pady=5,)
    HhtLabel1.pack()
    
    HhtLabel2 = Label(Hht, text=CWD, padx=5, pady=5, fg="blue")
    HhtLabel2.pack()
    
    HhtLabel3 = Label(Hht, text="The contents of this directory are:", padx=5, pady=5)
    HhtLabel3.pack()
    
    HhtScroll = Scrollbar(Hht)
    HhtScroll.pack(side=RIGHT, fill=Y)
    
    HhtListbox = Listbox(Hht, selectmode=SINGLE, fg="blue", width=68, height=5)
    
    xlsxCounter = 0
    for i in range(CWDlength):
       HhtListbox.insert(END, CWDFname[i])
       if CWDFname[i].endswith('.xlsx') == True:
           xlsxCounter = xlsxCounter + 1
    
    HhtListbox.pack()
    
    HhtScroll.config(command=HhtListbox.yview)
    HhtListbox.config(yscrollcommand=HhtScroll.set)
    
    S30 = "The total number of items in this directory is " + str(CWDlength) + ".  Of these items, " + str(xlsxCounter) + " is/are *.xlsx 
    
    files."
    HhtLabel4 = Label(Hht, text=S30, padx=5, pady=5)
    HhtLabel4.pack()
    
    S31 = "Click on the \"TRANSLATE\" button to begin.  Or click the \"X\" in the upper-right corner to close this program."
    HhtLabel5 = Label(Hht, text=S31, padx=5, pady=5)
    HhtLabel5.pack()
    
    HhtButton = Button(Hht, text="TRANSLATE")
    HhtButton.pack()
    
    Hht.mainloop()
    
    #####  END TestDriveGUI.py  ##########
    If needed, what that code looks like when it's ran can be seen at...:
    h t t p : / / i DOT i m g u r DOT c o m / V 3 i E Y U F DOT p n g ...that web address, if it's altered by taking out the single spaces between each character and inserting a "." in place of "DOT".
    So, with that part of the program done, and my initial learning of Python out of the way, I'm ready to start on the main part of the program, which will be the code that's responsible for all of the file processing, etc. This is the "beef" of the program, and it'll likely take me some time before I'm able to learn enough Python and complete it, so I was wanting to divide the program code into two separate files. This would allow me to concentrate better on this next code, and it would also help me avoid making mistakes and possibly mucking up the above code, and then having to track down the mistake, etc. Below, is just the beginnings of a file, with a comment block noting my thoughts on the matter.

    I think that as of now, I'm probably just at the point of knowing enough Python to be potentially counterproductive and/or sloppy". So I figured that I'd better get some experienced advice on this specific situation. If I was more knowledgeable about it, I'd read more about things like "program design" and the "conventional wisdom on how/when to start dividing code into separate files", etc. Aside from all of that though, I don't think that this program will have much "circular dependence" going on if it were to be divided into two separate *.py files, but I'm also not sure what I might be overlooking. If I go ahead and begin to use a second file (below) for the main code of the program, then I think that all I'd need to do is just re-define the variable "CWD", from the above code, to be sililar to "CWD = C:\......\MISC" instead of "CWD = os.getcwd()".
    Just to double check and see if I'm overlooking anything major, would doing something similar to what's listed in the above paragraph now, to divide the code up, be relatively hassle free when I merge the two batches of code later???

    Code:
    #####  BEGIN TestDriveTranslate.py  ##########
    
    '''
    This "TestDriveTranslate.py" goes with "TestDriveGUI.py" to form the complete prog of "BOL Hand History Translator".  Having two 
    
    separate *.py modules is mostly to keep the code more readable to me at this stage, and also to learn about what does/doesn't work 
    
    when dividing up a Py program into modules.  This module will start where TestDriveGUI.py left off; Right at the moment that the user 
    
    clicks on the "Translate" button in "TestDriveGUI.py".  When the coding starts, don't forget to make the correct imports.
    
    SINCE THE CODE IS IN TWO MODULES, I'm not sure but I think since the code is in two separate modules, but I'm only going to be
    working on this one for awhile, that I'll either want to redefine in this module all of the relevant variables from 
    TestDriveGUI.py - Making sure that when I redefine the ones (variables) that I need, to set their values to the correct ones (Where
    TestDriveGUI.py leaves them)... OR, (idk if it'll work) I might be able to have open the: Py Shell, TestDriveGUI.py, and this
    TestDriveTranslate.py.  Then, I'll want to run ONE instance of TestDriveGUI.py to get thos variables declared and their values set
    to the correct ones inside of the running instance of the Py Shell.  Then, when I run this TestDriveTranslate.py each time, it'll be
    able to access those variable values and names as it needs them (getting their correct value/name from the running instance of the
    Py Shell).  This method seems to me like the one where I'll make the least amount of mistakes.  I just have to remember to re-run
    TestDriveGUI.py if the Py Shell shuts down or re-starts (THAT COULD BE A PROBLEM - constant re-starts would slow things down)!!! -
    Maybe it's just better to run "TestDriveGUI.py", note what variables in it are used an valuated at, and re-define them in this
    module, commenting (#) just above them, in case any adaptations need to be made when finally merging the two modules of code???
    '''
    
    #####  END TestDriveTranslate.py  ##########
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    I hope that writing about your issue helped you resolve it. Your story makes my vision blur. After looking at it for a few days I'm not making headway. Aside from triple quoted strings and choice of ' or " as string delimiters, let's look at sequences of strings with only intervening white space (and in this case I would consider reading the message from a file that is easy to read in a text editor)...
    Code:
    # show that A == B
    # B is simple.
    # A = S1+S2+S3+S4+S5+S6+S7+S8+S9+S10+S11+S12+S13+S14+S15+S16+S17+S18+S19+S20+S21+S22+S23
    # print(A == B)
    
    
    
    B = (
        "        Use this BOL Hand History Translator program to translate your BOL hand histories from their native *.xlsx format into "
        "a more compatible *.txt format.  Then, the newly created *.txt hand histories will work with most hand history converters, "
        "poker forums, hand replayers, and poker databases."+"\n"
        "        If you use this program often enough, you might want to organize your hand history files like this:    "
        "1)  Create a new folder and call it \"AllHH's\".  Create three subfolders inside of that one and call them "
        "\"UntranslatedHH's\", \"TranslatedHH's\", and \"TextHH's\".  Put this program inside of the folder called "
        "\"UntranslatedHH's\".    "
        "2)  When you receive a new batch of BOL *.xlsx hand history files, put them in the folder called \"UntranslatedHH's\", "
        "along with this program.    "
        "3)  To translate those newly received BOL *.xlsx hand history files into a more compatible *.txt format, run this program "
        "on them (both the file(s) to be translated AND this program must be in the same folder for it to work).    "
        "4)  When this program is finished running, you'll see some files that are *.xlsx and others that are *.txt.  Move the "
        "*.xlsx files to the folder called \"TranslatedHH's\".  Move the *.txt files to the folder called \"TextHH's\".    "
        "5)  Now, you can improve your game by using the widely compatible *.txt translations of your hand(s).  You can now import "
        "them into poker databases; put them into hand history converters and then post the BB code into forums; and/or put them "
        "into a hand replayer and review your game there."+"\n"
        "        NOTE - this program will ONLY attempt to translate any files if they have an extension of *.xlsx.  If they are of the "
        "*.xlsx type, but not BOL hand histories, then they will also not be processed.  Either way, if a file is or is not a BOL "
        "hand history file, ALL files that are successfully processed are COPIED and translated.  In other words, any original "
        "files will be preserved.  However, it's still better to be safe than sorry.  To be cautious, make sure that nothing else "
        "besides this program and the files you wish to be translated are in the same folder together.  Then as you get more "
        "comfortable, you can ignore this rule.  Now, run this thing and go review some hands.  You might learn something.........."
        "...............ENJOY!"
        )
    
    
    S1 = "        Use this BOL Hand History Translator program to translate your BOL hand histories from their native *.xlsx format into "
    S2 = "a more compatible *.txt format.  Then, the newly created *.txt hand histories will work with most hand history converters, "
    S3 = "poker forums, hand replayers, and poker databases."+"\n"
    S4 = "        If you use this program often enough, you might want to organize your hand history files like this:    "
    S5 = "1)  Create a new folder and call it \"AllHH's\".  Create three subfolders inside of that one and call them "
    S6 = "\"UntranslatedHH's\", \"TranslatedHH's\", and \"TextHH's\".  Put this program inside of the folder called "
    S7 = "\"UntranslatedHH's\".    "
    S8 = "2)  When you receive a new batch of BOL *.xlsx hand history files, put them in the folder called \"UntranslatedHH's\", "
    S9 = "along with this program.    "
    S10 = "3)  To translate those newly received BOL *.xlsx hand history files into a more compatible *.txt format, run this program "
    S11 = "on them (both the file(s) to be translated AND this program must be in the same folder for it to work).    "
    S12 = "4)  When this program is finished running, you'll see some files that are *.xlsx and others that are *.txt.  Move the "
    S13 = "*.xlsx files to the folder called \"TranslatedHH's\".  Move the *.txt files to the folder called \"TextHH's\".    "
    S14 = "5)  Now, you can improve your game by using the widely compatible *.txt translations of your hand(s).  You can now import "
    S15 = "them into poker databases; put them into hand history converters and then post the BB code into forums; and/or put them "
    S16 = "into a hand replayer and review your game there."+"\n"
    S17 = "        NOTE - this program will ONLY attempt to translate any files if they have an extension of *.xlsx.  If they are of the "
    S18 = "*.xlsx type, but not BOL hand histories, then they will also not be processed.  Either way, if a file is or is not a BOL "
    S19 = "hand history file, ALL files that are successfully processed are COPIED and translated.  In other words, any original "
    S20 = "files will be preserved.  However, it's still better to be safe than sorry.  To be cautious, make sure that nothing else "
    S21 = "besides this program and the files you wish to be translated are in the same folder together.  Then as you get more "
    S22 = "comfortable, you can ignore this rule.  Now, run this thing and go review some hands.  You might learn something.........."
    S23 = "...............ENJOY!"
    
    
    
    A=S1+S2+S3+S4+S5+S6+S7+S8+S9+S10+S11+S12+S13+S14+S15+S16+S17+S18+S19+S20+S21+S22+S23
    print(A == B)
    Otherwise, I don't know why or where you want to split your sources. Separating the interface from the computational part often makes sense. I've worked on projects in which each small module had a specific purpose (computed response of an element in an optical signal transmission system. Optical amplifier, fiber, dispersion compensator are high level categories). The small modules are testable. Each had corresponding unit tests and we wrote the tests first. A component is a part that you understand. What's a system? A system is made of components.

    Hope that helps. I didn't read all of your message---certainly not all at once. Be well.
    Last edited by b49P23TIvg; November 2nd, 2013 at 01:12 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo