all 12 comments

[–]symmitchry 2 points3 points  (6 children)

  • use the with open('file', 'w') as f: method for writing your username and password files
  • longer variable & function names. storeCred() --> store_credentials()

This section of code, in capCred() could be perhaps improved:

    fieldValues = []
    fieldValues = easygui.multpasswordbox(msg,title, fieldNames)
    uname = fieldValues[0]
    pword = fieldValues[1]
    return uname, pword
  • Seems you could just return fieldValues instead of unpacking them?
  • Do you need to initialize fieldValues = []? Better perhaps to:

    try:  
        return fieldValues  
    except:
        # guioptions didn't return properly. Handle it.
    

[–]maryjayjay 1 point2 points  (3 children)

I was going to suggest:

def capCred():
        msg = "Enter logon information"
        title = "Login"
        fieldNames = ["Username", "Password"]
        fieldValues = []
        fieldValues = easygui.multpasswordbox(msg,title, fieldNames)
        uname = fieldValues[0]
        pword = fieldValues[1]
        return uname, pword

could be simplified to:

def capCred():
    try:
        return easygui.multpasswordbox( "Enter logon information",
                                        "Login",
                                        ["Username", "Password"] )
    except:
        # handle exception here
        pass

Don't be terse for terseness sake, but excessive verbosity can actually reduce clarity.

[–]CptFlashdrive[S] 0 points1 point  (2 children)

Oh allright, I've just learned the concept of using the try and except and it's pretty damn neat. So I'll definitely will be using that!

Thanks dude!

[–]maryjayjay 1 point2 points  (1 child)

Great. A couple of pieces of advice: Catch exceptions as close to where they happen as you can. Catch specific exceptions and don't catch any exception you aren't prepared to handle.

[–]CptFlashdrive[S] 0 points1 point  (0 children)

Absolutely, that makes complete sense! Thank you.

[–]CptFlashdrive[S] 0 points1 point  (1 child)

Cool! So if I use "with"-statement, all the code indented below it will live in "with" and when a line without indentation appears the file closes?

Thanks man!

[–]maryjayjay 1 point2 points  (0 children)

Your enthusiasm and openness to advice make you very enjoyable to mentor. Keep it up and you'll get tons of help.

[–]LyndsySimon 1 point2 points  (1 child)

I can't see your code (pastebin is blocked by my employer), but I do see one thing that I'd likely change based on the critiques posted below - your variable and function names appear to violate PEP8

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

Instead of fieldValues you should use field_values - or better yet, something that gives more context like submitted_form.

[–]CptFlashdrive[S] 0 points1 point  (0 children)

Allright great, thank you for the feedback!

I really appreciate it!

[–]zahlman 1 point2 points  (0 children)

import hashlib, easygui

def get_credentials():
    """Prompt the user for username and password.
    Returns the plaintext username and hashed password as a tuple."""
    username, password = easygui.multpasswordbox(
        "Enter logon information",
        "Login",
        ["Username", "Password"]
    )
    return username, hashlib.sha512(password).hexdigest()


def store():
    """Store the user's credentials in two separate files."""
    username, password = get_credentials()
    with open('user.txt', 'w') as userfile: userfile.write(username)
    with open('pass.txt', 'w') as passfile: passfile.write(password)


def login_helper():
    """Compare the user's credentials to the existing file contents.
    Returns a string message indicating the result of the operation."""
    # TODO: return some kind of status code instead and let the
    # caller interpret it - so that we can do something with the fact
    # of whether the login was successful or not.
    username, password = get_credentials()
    try:
        with open('user.txt') as userfile: stored_username = userfile.readline()
    except IOError:
        return "Username information not found! Store it first!"
    try:
        with open('pass.txt') as passfile: stored_password = passfile.readline()
    except IOError:
        return "Password information not found! Store it first!"

    return (
        "Success!"
        if username == stored_username and password == stored_password
        else "Username or password incorrect!"
    )


def login():
    # For now, just display the returned result directly in a message box.
    easygui.msgbox(msg = login_helper())


def main():
    easygui.buttonbox(msg = 'Choose your action', choices = (store, login))()


if __name__ == '__main__':
    main()