1
Good night, I’m trying to make a simple messaging application that unites Python with Redis, I was able to connect to the database in a simple set/get, but I’m not able to do any hashing to save/fetch messages or users in redis nor are the inputs working. I am using Visualstudiocode with powershell to do it. Does anyone know how to proceed? Here comes the code --->
from cli import User
import redis
Tests connection with redis (here it works)
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('fruta', 'pera')
True
r.get('fruta')
b'pera'
class Servidor(nick.User):
def __init__(self, host = 'localhost', port = 6379,):
self.__host = host
self.__port = port
self.__nick = nick
self.__password = password
from chat import Servidor
import redis
import getpass
import datetime
Tests connection with redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('fruta', 'manga')
True
r.get('fruta')
b'manga'
class User:
def __init__(self, nick, password, msg, host = '127.0.0.1', port = 9999):
self.__nick = nick
self.__password = password
self.__msg = msg
self.host = host
self.port = port
Creates new user
def new_user(self, nick, apelidos, host, port):
r.hset.i = input('Digite seu apelido único: ')
for nick in apelidos:
apelidos:str = input('Apelido já escolhido, seja mais original e escolha outro')
Sets the access password
def escolha_senha(self, password):
password = getpass.getpass (input('Digite sua senha: '))
password2 = getpass.getpass (input('Digite novamente sua senha: '))
if password == password2:
password = r.hset
else:
return False
Sends message without spaces to any user (but not yet saved in redis)
def send(self, sender, msg):
self.msg = r.hset(input(msg.strip()))
self.sender = self.__nick
Stores message in a hash
def storage_msg(self, msg, time):
self.msg = r.hset
self.time = datetime.datetime.utcnow()
while msg != '/bye':
msg = input(print("Envie sua mensagem: ",msg," - ",time))
Look - someone might unfold to answer this question - but you’re trying to use several different concepts without understanding some basic things before - for example, recreating a
for
within itselffor
, its functionnew_user
never have create an instance of the classuser
, neither has aif
to test whether something exists or not - etc... quite a mess indeed. Try clearing up one part at a time by going back to the documentation or course you’re learning from - and maybe it’s a good idea to let the redis go a little later: use a simple Python dictionary until it works– jsbueno
A nice concept:
input
is a nice function to pick up data entry, but she for the program, and prevents any kind of unit test. Separate the logic of your program so that the check/connection functions never have a call toinput
, and yes, receive the value that the user will enter as a parameter. There you write other functions that make up the "print/input" part and call the first ones.– jsbueno
Another thing - Python has no "private" attributes - some older documentation confuses the
__
prefix with private attributes - disregard that, and don’t use it - it only makes the code harder to read and deal with, at the stage where you’re getting familiar with the language.– jsbueno