Python Forum
Criticism on one of my first completed programs in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Criticism on one of my first completed programs in Python
#3
Thank you for the feedback. It has opened my eyes to a lot that I can improve. It's also opened my eyes to the fact that my Comp Sci 150 class didn't teach me a lot. However, I'm going to spend the next few hours improving :)

(Jul-05-2018, 07:26 PM)ichabod801 Wrote: Relative paths are better if you can manage them. Otherwise note that you can generate absolute paths to the program file dynamically (os.path.abspath(__file__)), and then add relative paths onto that to get at specific files. Then you don't have to hunt down and change file paths when moving your programs around.

When you talk about this, how exactly would you go about doing this in detail. You can be vague about it, that way you're not giving me the answer to solve my problem. I just have never worked with relative file paths.


EDIT:
Did some research on relative files and came up with this. This is a modified version of the genAcctNum function in the bankAccount file above.

# The projects main directory
project_dir = os.path.dirname(os.path.abspath(__file__)).split("\Scripts")[0]

def get_abs_file_path(rel_path):
    # Take a relative file path and join to the main directory of the project
    file_path = os.path.join(project_dir, rel_path).replace('\\', '/')
    return file_path


def gen_acct_num():
    # Set the paths for the account numbers text file and translog folder
    acct_num_path  = get_abs_file_path('Login_Info/acctNum.txt')
    trans_log_path = get_abs_file_path('Trans Logs')

    # Open the acct_num_file in read mode and read the contents of the file to compare
    # This will be used to check if the generated acct # is already in the database of acct #'s
    acct_num_file = open(acct_num_path)
    acct_nums = [line.strip for line in acct_num_file]
    while True:
        generated_acct_num = str(random.randint(100000000000, 999999999999))
        if generated_acct_num not in acct_nums:
            break

    # Once the file has been searched and the account # is generated, append the
    # account # to the database.
    acct_num_file = open(acct_num_path, 'a')
    acct_num_file.write(generated_acct_num + "\n")
    acct_num_file.close()

    # Create trans log file
    trans_log = open(trans_log_path+"/transLog"+generated_acct_num+".txt", 'w+')
    trans_log.close()

    # Return the account number
    return generated_acct_num
(Jul-05-2018, 07:26 PM)ichabod801 Wrote: Your check for existing account numbers is very odd and complicated. Why not:

I also implemented your more simple design to check if the account number has already been generated.

I plan on working to implement your feedback on using relative file paths instead. Your feedback has been very much appreciated :)
Reply


Messages In This Thread
RE: Criticism on one of my first completed programs in Python - by Zombie_Programming - Jul-05-2018, 08:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter Tic Tac Toe With Enhanced Features - Completed adt 2 2,968 Dec-10-2019, 06:22 AM
Last Post: adt
  my earliest completed script Skaperen 0 2,018 Mar-08-2019, 09:50 PM
Last Post: Skaperen
  [link]Creating-a-repo-for-your-completed-scripts metulburr 0 7,647 Aug-29-2018, 01:19 PM
Last Post: metulburr
  completed module: validip.py Skaperen 8 7,461 Jan-04-2017, 06:39 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020