Search code examples
pythonkeyword-argument

why does my class is executing on its own?


so i'm working on a code but I encounter a error and idk why but I will give the details of the code

#sc.py
import getpass
import time
import subprocess
import sys
#time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
#class
class password_log:
    def __init__(self,**value):
        self.passwordattemptslog = value['pwdalog']
        self.maincode = value["maincode"]
        self.var_input = value["var_input"]
        self.inputpassword = value["password"]
        self.logs = value["logs"]
        self.f_obj = value["log_file"]
        self.file = open(self.f_obj,'r+')
        self.wrongpassword_attempts = 0
    def checker(self):#checker
        if self.logs == True:
            if  self.var_input == self.inputpassword:
                self.maincode
                        #logs
                self.correctpassword()
            elif self.var_input != self.inputpassword:
                print("wrong password")
                self.wrongpassword()
                self.wrongpassword_attempts += 1
            elif self.wrongpassword_attempts > self.passwordattemptslog:
                self.limitpassword()
            else:
                print("error")


        elif self.logs == False:
            if self.var_input == self.inputpassword:
                self.maincode
            elif self.var_input != self.inputpassword:
                print("wrong password") 
            else:
                print("error")
        else:
            print("error")

    #logs
    def correctpassword(self):
        self.file.write("\ncorrect password" +" time:" + current_time + "\n" )

    def wrongpassword(self):
        self.file.write("\nwrong password" +" time:" + current_time + "\n" )
        self.file.write("\n the wrong password input:" + self.var_input)
    def limitpassword(self):
        self.file.write(f"\n {self.passwordattemptslog} wrong attempts of password time:" + current_time + "\n" )

#main.py
from sc import password_log
import getpass


class main:
    @staticmethod       
    def runtest():
        print("runtest")





log_input = getpass.getpass(prompt = "password:")

paswlog = password_log(password = "2008", logs = True, var_input = log_input,maincode = main.runtest() , log_file = "filetext.txt", pwdalog = 10)

paswlog.checker()

output: runtest password:

The runtest() has already been executed, but it should only be run if the password is correct.

im just new at python and experimenting some stuff so yeah i need a little help on this one

i expect that the runtest() should only be run if the password is correct


Solution

  • My first instinct is in main.py, on the line maincode = main.runtest(), it should be maincode = main.runtest. Otherwise you would immediately call the function and set maincode to its return value. This early call of the function probably explains why your program executes immediately bypassing the password check

    Then, in sc.py in checker, wherever you want to call maincode, it should be maincode(), otherwise you would just name the function without actually calling it.

    I cannot test this so those are just my guesses. Let me know if they help.