Jshee
Jshee

Reputation: 2686

Get Return Value from one function and use on another

I'm trying to do something so seemingly basic in python, but i think im overthinking it.

Im trying to get the return value of password from create_password() and use it in store_password() where I put <<HERE>>

How can I accomplish this?

def create_password(self):
    characters = string.ascii_letters + string.punctuation  + string.digits
    password =  "".join(choice(characters) for x in range(self.pass_char) )
    return (password)

def store_password(self):
    try:
        connection = sqli.connect('pw.db')
    except:
        print("Error occurred!")

    with connection:
        cur = connection.cursor()
        cur.execute('''
                    CREATE TABLE IF NOT EXISTS my_passwords(
                    id INT PRIMARY KEY AUTOINCREMENT,
                    password text
                    '''
        )
        cur.execute('INSERT INTO my_passwords VALUES(?)', <<HERE>>)

Thank you.

Upvotes: 1

Views: 81

Answers (5)

k4ppa
k4ppa

Reputation: 4667

Assuming both function are inside the same class, called in this examples MyPasswordMaker.

You can pass the password as a parameter:

def store_password(self, password):
    try:
        connection = sqli.connect('pw.db')
    except:
        print("Error occurred!")

    with connection:
       cur = connection.cursor()
        cur.execute('''
                    CREATE TABLE IF NOT EXISTS my_passwords(
                    id INT PRIMARY KEY AUTOINCREMENT,
                    password text
                    '''
        )
        cur.execute('INSERT INTO my_passwords VALUES(?)', password)


myPasswordMaker = MyPasswordMaker()
password = myPasswordMaker.create_password()
myPasswordMaker.store_password(password)

Or you can store the password in a class variable

class MyPasswordMaker:
    def create_password(self):
        characters = string.ascii_letters + string.punctuation  + string.digits
        self.password =  "".join(choice(characters) for x in range(self.pass_char) )

    def store_password(self):
        try:
            connection = sqli.connect('pw.db')
        except:
            print("Error occurred!")

        with connection:
            cur = connection.cursor()
            cur.execute('''
                CREATE TABLE IF NOT EXISTS my_passwords(
                id INT PRIMARY KEY AUTOINCREMENT,
                password text
                '''
            )
        cur.execute('INSERT INTO my_passwords VALUES(?)', self.password)

Or call create_password() inside store_password():

cur.execute('INSERT INTO my_passwords VALUES (?)', self.create_password())

Upvotes: 0

Elijah
Elijah

Reputation: 66

This is basically using the function that you have already built create_password(self) and assuming that they both belong to the same class you should be able to call create_password(self) within the second function store_password(self)

So to return the password you created you can just call create_password(self) in place of <<HERE>>.

What tabac has suggested will also work, you will need to make sure, however that the method create_password(self) will have to be called before so that the self.password variable is assigned.

Your code would become

cur.execute('INSERT INTO my_passwords VALUES(?)', self.create_password(self))

Upvotes: 1

Noctis Skytower
Noctis Skytower

Reputation: 21991

Maybe this example might help in your situation?

import random
import sqli
import string


def main():
    password = create_password(20)
    store_password(password)


def create_password(length):
    characters = string.ascii_letters + string.punctuation + string.digits
    password = ''.join(random.choice(characters) for _ in range(length))
    return password


def store_password(password):
    try:
        connection = sqli.connect('pw.db')
    except:
        print('Error occurred!')
    else:
        with connection:
            cur = connection.cursor()
            cur.execute('''CREATE TABLE IF NOT EXISTS my_passwords (
                           id INT PRIMARY KEY AUTOINCREMENT,
                           password text)''')
            cur.execute('INSERT INTO my_passwords VALUES (?)', password)

if __name__ == '__main__':
    main()

If you provide more of your code (specifically more of the class that your methods are from and especially what calls the create_password method), it would be a lot easier to fix your code.

Upvotes: 0

576i
576i

Reputation: 8352

This line should work, if you have defined both funtions within a class.

cur.execute('INSERT INTO my_passwords VALUES(?)', self.create_password())

If not, you need to remove the self, from both and then write

cur.execute('INSERT INTO my_passwords VALUES(?)', create_password())

Upvotes: 0

tabac
tabac

Reputation: 126

I assume these methods both belong to the same class. If so you can attach the password to the class instance and use it in store_password. Use self for that.

def create_password(self):
    # code...
    self.password = password

def store_password(self):
    # Access password by doing self.password

Upvotes: 0

Related Questions