I am trying to simulate a system access portal using classes and methods. I want to be able to ask the user for their username using input(), check if that input is an object of class User and if so, check if the password is correct for the username. When I use instance of it is returning false. How can I modify this to work?
class User():
def __init__(self, username, password):
self.username = username
self.password = password
def change_pw(self, new_password):
self.password = new_password
jshannon = User("jshannon","AbC!23")
print(jshannon.username)
print(jshannon.password)
jshannon.change_pw("(i*u&y1")
print(jshannon.password)
input_user = input("What is your username? ")
print(isinstance(input_user, User))
User inputs are strings. Always. Period. So you can not "check if that input is an object of class User" - it will never be.
The solution here is to maintain a collection of User instances, and use the input string to search for a matching user.
class UsersCollection(object):
def __init__(self):
# we store users in a dict, with user.usernale as key
self._users = {}
def add(self, user):
if user.username in self._users:
raise ValueError("username '{}' already in used".format(user.username))
self._users[user.username] = user
def get(self, username):
return self._users.get(username)
users = UsersCollection()
users.add(User("jshannon","AbC!23"))
input_user = input("What is your username? ").strip()
userfound = users.get(input_user)
if userfound:
# you can do something with your user
else:
print("sorry, we don't know you")
Note that this is only suitable as a toy project of course.
If you are using Python 3.x (which I'm going to assume), input returns a string so isinstance(input_user, User) will always be False.
You will need to keep track of all User objects created and search for the object with the inputted name.
There are several different ways to do that. I'm going to assume that usernames are unique so I will use them in a shared set:
class User:
users = set()
def __init__(self, username, password):
self.username = username
self.password = password
self.users.add(username)
def change_pw(self, new_password):
self.password = new_password
jshannon = User("jshannon", "AbC!23")
print(jshannon.username)
print(jshannon.password)
jshannon.change_pw("(i*u&y1")
print(jshannon.password)
input_user = input("What is your username? ")
print(input_user in User.users)
# will output True if input_user is jshannon, otherwise False
Note that this is just an example, and it is not bullet-proof nor the best design (one may argue if the users set even belongs to the User class Hint: probably not). If an object's username changes after the initialization the set will not be updated and you may get wrong results. This particular problem can be solved by changing self.username to a property but I suppose that is out of scope of this Q&A.
I am not sure if this is what you want to do but you cant try this
add a list to your class list_of_usernames = []
and then in __init__() append username to the list_of_usernames and at the end
print(input_user in User.list_of_usernames)
so your code will look like this
class User():
list_of_usernames = []
def __init__(self, username, password):
self.username = username
self.password = password
self.list_of_usernames.append(username)
def change_pw(self, new_password):
self.password = new_password
jshannon = User("jshannon","AbC!23")
print(jshannon.username)
print(jshannon.password)
jshannon.change_pw("(i*u&y1")
print(jshannon.password)
input_user = input("What is your username? ")
print(input_user in User.list_of_usernames)
Related
Writing a python script that prompts the user by asking if they are are new or an existing user. If new, they will create a username, password, age. A User class instance should be created with that information along with adding the new username and password to the security dictionary. Once registered, or an existing user, it will be prompted to enter the username and password.
The username and password will be checked in the security dictionary (key is username, value is password). If in the dictionary, then the user can run a couple commands that affect the user class instance. For example, a function to increase the age by 1 (for birthdays).
Question: How do I load and save the security dictionary (username and password) and the user class instances (database for the user data: username, password, age, and height) so that a user can login in and out?
Here is my code:
class users:
def __init__(self, username, password, age):
self.username = username
self.password = password
self.age = age
#classmethod
def register(cls):
return cls(
str(input("Email: ")),
int(input("ID Pin Number XXXX: ")),
int(input("Age: "))
)
def load_func(file):
#Open the security file if present
if file == "security":
try:
with open("security.txt", "rb") as sct:
return pickle.load(sct)
except IOError:
with open("security.txt", "w+") as sct:
pickle.dump(dict(), sct)
return dict()
elif file == "userDatabase":
try:
with open("userDatabase.txt", "rb") as dat:
return pickle.load(dat)
except IOError:
with open("userDatabase.txt", "w+") as dat:
pickle.dump(dict(), dat)
return dict()
def saveData(file, data):
if file == "security":
with open("security.txt", "wb") as sct:
pickle.dump(data, sct)
elif file == "userDatabase":
with open("userDatabase.txt", "wb") as dat:
pickle.dump(data, dat)
else:
print("Error with saving file")
First of all: If you ever save passwords from users other than fictive testing users, be sure to hash the passwords using hashlib.
Now to your problem: Your users class is meant to represent one single user and should be renamed to User, in order to match PEP8 code standard (classes are CapitalLetters) and because it's more intuitive that a class named User represents one user.
You could then save the username and password combination using dill. Later on, you could load the dill string to re-create the exact object that it was before saving:
import dill
class User:
def __init__(self, username, password, age):
self.username = username
self.password = password
self.age = age
#classmethod
def register(cls):
return cls(
str(input("Email: ")),
int(input("ID Pin Number XXXX: ")),
int(input("Age: "))
)
#classmethod
def load(cls, file: str):
with open(file, "rb") as f:
return dill.load(f)
def save(self):
with open(self.username, "wb") as f:
dill.dump(self, f)
Max = User("Max", "SecUrEP4$$W0rD", 42)
Max.save()
Max2 = User.load("Max")
print(Max2.username)
If you found this helpful, please leave an UP,
if you have any further questions, leave a comment! :)
Suppose I had this class
class employee(object)
def __init__(self,name):
return self.name
def something(self):
pass
...
Leading to
if __name__== "__main__":
name = input("what is your name ")
user1 = employee(name)
user1.something()
...
I want the user1 instance to be the name inputted by the user so that I can have unique instances. How do I go about adding instances based on user input in the main section?
so if I run the program and inputted "tim", the outcome I would want is:
tim.name = "tim"
....
UPDATE
Seems like the above is unclear, let me try to explain using my actual code:
So I have this Spotify API:
class Spotify(object):
def __init__(self,user):
self.client_id = ''
self.client_secret = ''
def client_credentials(self):
pass
def get_header_token(self):
pass
...
In the end,
if __name__== "__main__":
user = input("Enter username ")
user = Spotify(user)
user.request_author()
...
I am trying to get the user variable to the input the user provides, such as if the user inputted "tim123", the user variable would also be tim123.
So I could perform:
tim123.name
Think my mind is going completely blank and there should be an easy solution for this. I am sure this is very unpractical but I don't know how I would do this in case I ever needed to.
Change
return self.name
to
self.name = name
if name== "main":
variable_name = raw_input("Enter variable name:") # User.
enters "tim123"
name = input("Enter Name")
globals()[variable_name] = employee(name)
tim123.name
Based on your comment, it sounds like you are looking for exec() or eval(). Link. My solution would be to do something like:
class employee(object):
def __init__(self,name):
self.name = name
name = input("what is your name ")
exec(f"{name} = employee('{name}')")
(and then you would access joe.name, if the user inputted joe, or bob.name, if the user inputted bob, etc.).
Alternatively, you could use locals() or globals()
Hope this helped!
I want to create some users just to populate my database, so using decorators I created a method inside a class for it. I returned the results into a list and used the information without a problem. But after a while I thought that once I was working with classes wouldn't be dumb to use lists? I feel that I am kind of moving backwards since, at least in my understanding, it might be possible to access those values directly.. But the truth is I struggled on attempting to instantiate dynamically and access those instances. So, how could I do that properly? Here is the code I wrote in the first attempt:
class User:
def __init__(self, user_name, password, email, i=0):
self.user_name = user_name
self.password = password
self.email = email
#classmethod
def from_generate(cls, amount):
user_name = 'user' + str(amount)
password = 'password000.' + str(amount)
email = 'user' + str(amount) + "#whatever.com"
return cls(user_name, password, email)
in another file:
def user_generator(user_qty=0):
user_list = []
for i in range(1, user_qty + 1):
# call the class method to generate users
instance = User.from_generate(i)
user_list.append(instance)
return(user_list)
users = user_generator(5)
if __name__ == "__user_generator__":
user_generator()
I need help, I really dont see a problem here why my add_pass method does not add the arguments to the dictionary.
class Password:
def __init__(self,media,password):
self.password = password
self.media = media
self.all_passwords = {}
self.all_passwords.setdefault(self.media,self.password)
def add_pass(self,media,password):
self.all_passwords[self.media] = self.password
b = Password('instagram','pass123')
b.add_pass('fb', 'pass12345')
In your add_pass function, remove self. from self.media and self.password. Those refer to the class attributes rather than the arguments:
def add_pass(self,media,password):
self.all_passwords[media] = password
I'm trying to make a function that checks if the user is logged in. I've placed the function outside of the mainpage class and it gives no errors until I try to use it insie the def get(self) within the MainPage class. The function looks like this:
def LoginCheck():
username = self.request.cookies.get('username')
password = self.request.cookies.get('password')
if username and password:
checkq = db.GqlQuery("SELECT * FROM Users WHERE username = :1 AND password = :2", username, password)
checkresult = checkq.get()
if checkresult is None:
self.redirect("/wrong")
else:
self.redirect("/wrong2")
and When I try to use it it returns:
line 14, in LoginCheck
username = self.request.cookies.get('username')
NameError: global name 'self' is not defined
What am I doing wrong?
You'll have to add "self" to your function definition. See section 9.3.2 of python's tutorial.
def LoginCheck(self):
username = self.request.cookies.get('username')
password = self.request.cookies.get('password')
if username and password:
checkq = db.GqlQuery("SELECT * FROM Users WHERE username = :1 AND password = :2", username, password)
checkresult = checkq.get()
if checkresult is None:
self.redirect("/wrong")
else:
self.redirect("/wrong2")