object oriented – Python Back-end registration validation

I am working on a personal project (Flask CRUD app) and I am currently building the user service. I am trying to use as less as libraries as possible (that’s why I do not use WTF-forms for example, just for learning purposes). I am really not sure about a couple of things.

I am aware that form validation should be handled in both front and back-end (or at the very least on the back-end). In order to validate a new user, I am (currently) checking 3 things:

1) email does not already exist on the db (can only be verified on the back-end)

2) password is strong enough (can be done in both front and back end)

3) password and password_confirm matches (can be done in both front and back end)

My goal is to display a small error message on the registration page if either at least one those errors appears. Currently, I am checking option 1, 2 and 3 and the back-end but only return True if the registration is valid otherwise, False.

Since case 1 and 2 can directly be handle also on the front-end, can I display the error message using JavaScript and not from the back-end? I am still confused on what would be the best return for validate_registration (currently a Boolean).

Also, I am not quite sure my code is over-engineered, I made a RegistrationForm class just to validate a registration form. I did that because I am able to pick validations methods from my validators.py which may be used in other Classes as well.

So this is what I did so for for the registration form:

user/blueprints/routes.py

@user.route('/new-user',methods = ('POST'))
def register_user():
    form_email = request.form.get('email')
    form_password = request.form.get('psw')
    form_password_repeat = request.form.get('psw-repeat')
    registration_form = RegistrationForm(form_email, form_password, form_password_repeat).validate_registration()
    
    if registration_form:
        new_user = UserService().register_user(form_email, form_password)
        user_repository = UserRepository(conn, 'users')
        user_repository.add_user(new_user)
        user_repository.save()
        return "ok" #will probably change return statements later on
    return "not ok" #will probably change return statements later on

user/blueprints/forms.py

from prepsmarter.blueprints.user.validators import email_already_in_use, password_matching

class RegistrationForm():
    def __init__(self, email, pwd_1, pwd_2):
        self.email = email
        self.pwd_1 = pwd_1
        self.pwd_2 = pwd_2

    def validate_registration(self):
        is_valid = email_already_in_use(self.email) and is_strong_password(self.pwd_1) and password_matching(self.pwd_1, self.pwd_2)
        return is_valid

class LoginForm():
 # to do

user/blueprints/validators.py

import re 
from email_validator import validate_email, EmailNotValidError
from prepsmarter.extensions import conn

def is_strong_password(password):
    length_error = len(password) < 8
    digit_error = re.search(r"d", password) is None
    uppercase_error = re.search(r"(A-Z)", password) is None
    return length_error and digit_error and uppercase_error
    
def is_email_formated_correctly(email):
    is_correct = True
    try:
        validate_email(email)
    except EmailNotValidError:
        is_correct = False
    return is_correct

def password_matching(pwd_1, pwd_2):
    return pwd_1 == pwd_2

    
def email_already_in_use(email):
    sql = "SELECT CASE WHEN EXISTS ( SELECT * FROM users WHERE email = (%s)) THEN 1 ELSE 0 END;"
    cursor = conn.cursor()
    cursor.execute(sql, (email))
    res = cursor.fetchall()
    return res == 1 

javascript – How can I publish a Python game on itch.io as html?

A game made with Pygame is not playable in the browser. Games made with that tool can be put on itch, but only as a downloadable desktop package, not as a web game.

For completeness, I should mention there are a few projects that aim to get Python/Pygame working in a browser, but I would not call any of these a great option. The most prominent of these projects is Skulpt, and here are some examples of using it with Pygame.

Refer to this existing closely related question.

python – My function is supposed to work as a loop but it stops

I have this function to get the max and min. I can’t use a list for this so this is the only way I can think of doing it. It works but only with one number. It works fine if the try is not inside the else but I need it to be because if the user says ‘stop’, the function is supposed to just break (not give me any answer). So what exactly is going on? I want it to take as many numbers as I want until I say stop and then return the min and max but it gives me the result after only one number.

def main():

   minimum = None
   maximum = None

   while True:
       num = input("Enter an integer or 'stop' to end: ")
       if num == 'stop': 
           break
       else:
           try:
               inum = int(num)
           except:
               print('Invalid input')
               continue                            

           if minimum is None or inum < minimum:
               minimum = inum

           if maximum is None or inum > maximum:
               maximum = inum

           print('The maximum is', maximum)
           print('The minimum is', minimum)

main()

python – code trace question urgent?

Question= Complete a code trace on the following function call by transcribing the function and noting the values of the important variables at the indicated points in the function.**

def accept_hypothesis(mu_hat, mu, sigma, n):

z_score = (mu_hat – mu) / (sigma/n**0.5)

if z_score < 0:
z_score *= -1

P = 0
if z_score < 0.5:
P = 0.5
elif z_score < 1:
P = 0.3
elif z_score < 2:
P = 0.15
else:
P = 0.02

p_value = 2*P

return p_value <= 0.05
answer = accept_hypothesis(5, 20, 4, 16)

performance – Python pynput keyboard + mouse input printer

I needed to have input shown for some things in a game I play on Linux, but Linux doesn’t seem to have any good input display programs. I decided it would be good enough to have python print to a pair of xterm windows (one for keyboard and one for mouse) using pynput. I do this using two different python programs because the only way I found to have one python programs deal with two terminals is to constantly write to a file and use tail -f, and I didn’t want to do that.

Because both programs ended up being so small and simple, I tried to write it so that the key/mouse listeners would run their functions as fast as possible. But I might have missed some things that could be done. I’m wondering if anyone knows of any other ways to optimize this for response time. I know it won’t make anything close to a noticeable difference, I’m just asking out of curiosity.

*Edited because I thought of ways to make it faster

mouse_listener.py

from pynput import mouse

def main():
    
    print_count = 0
    
    def on_click(x,
        y,
        button,
        pressed,
        local_print=print,
        dashes="--- ",
        no_dashes="    ",
        pressed_str=" mouse pressed",
        released_str=" mouse released"
        ):
        nonlocal print_count
        if print_count >= 10:
            if pressed:
                local_print(dashes + button.name + pressed_str)
                print_count = 0
            else:
                local_print(dashes + button.name + released_str)
                print_count = 0
        else:
            if pressed:
                local_print(no_dashes + button.name + pressed_str)
                print_count = print_count + 1
            else:
                local_print(no_dashes + button.name + released_str)
                print_count = print_count + 1
    
    def on_scroll(x,
        y,
        dx,
        dy,
        local_print=print,
        dashes="--- ",
        no_dashes="    ",
        up_str="scroll up",
        down_str="scroll down"
        ):
        nonlocal print_count
        if print_count >= 10:
            if dy == 1:
                local_print(dashes + up_str)
                print_count = 0
            else:
                local_print(dashes + down_str)
                print_count = 0
        else:
            if dy == 1:
                local_print(no_dashes + up_str)
                print_count = print_count + 1
            else:
                local_print(no_dashes + down_str)
                print_count = print_count + 1
    
    try:
        listener = mouse.Listener(on_click=on_click, on_scroll=on_scroll)
        listener.start()
        listener.join()
    except KeyboardInterrupt:
        listener.stop()

main()

key_listener.py

from pynput import keyboard

def main():
    
    print_count = 0
    held = set()
    
    def on_press(key,
        local_print=print,
        local_hasattr=hasattr,
        held_local=held,
        hold=held.add,
        str_lower=str.lower,
        dashes="--- ",
        no_dashes="    ",
        end_text=" pressed",
        str_for_hasattr="name"
        ):
        nonlocal print_count
        if key not in held_local:
            if local_hasattr(key, str_for_hasattr):
                if print_count >= 10:
                    local_print(dashes + key.name + end_text)
                    print_count = 0
                else:
                    local_print(no_dashes + key.name + end_text)
                    print_count = print_count + 1
            else:
                if print_count >= 10:
                    local_print(dashes + str_lower(key.char) + end_text)
                    print_count = 0
                else:
                    local_print(no_dashes + str_lower(key.char) + end_text)
                    print_count = print_count + 1
            hold(key)
    
    def on_release(key,
        local_print=print,
        local_hasattr=hasattr,
        unhold=held.remove,
        str_lower=str.lower,
        dashes="--- ",
        no_dashes="    ",
        end_text=" released",
        str_for_hasattr="name"
        ):
        nonlocal print_count
        if local_hasattr(key, str_for_hasattr):
            if print_count >= 10:
                local_print(dashes + key.name + end_text)
                print_count = 0
            else:
                local_print(no_dashes + key.name + end_text)
                print_count = print_count + 1
        else:
            if print_count >= 10:
                local_print(dashes + str_lower(key.char) + end_text)
                print_count = 0
            else:
                local_print(no_dashes + str_lower(key.char) + end_text)
                print_count = print_count + 1
        try: # I don't trust this part
            unhold(key)
        except KeyError:
            pass
    
    try: # TypeError is possible because numpad 5's char attribute is None
        listener = keyboard.Listener(on_press=on_press, on_release=on_release)
        listener.start()
        listener.join()
    except (KeyboardInterrupt, TypeError):
        listener.stop()

main()

xterm_opener.sh

xterm -xrm 'XTerm.vt100.allowTitleOps: false' -T "keyboard" -geometry 28x16 -e python3 '/home/USERNAME/input_listener/key_listener.py' &
xterm -xrm 'XTerm.vt100.allowTitleOps: false' -T "mouse" -geometry 28x16 -e python3 '/home/USERNAME/input_listener/mouse_listener.py'

python – Using haversine to order by distance

I’m trying to write a search by location into a website. I cant help but think that the code I’ve written is not very efficient.

If I were to have 1000’s of places it would be fairly slow.

Any improvements?

This is a psuedo version.

import haversine

class Place(db.Model):
   name = "TEST"
   lat = 52
   lng = -2.5

def get_places():
    return Place.query.all()

def sort_by_dist(current_lat,current_lng,limit=10):
    return sorted(get_places(), key=lambda x:haversine((current_lat,current_lng),(x.lat,x.lng)))(:limit)

performance – under 100 line Python pynput keyboard + mouse input printer

I needed to have input shown for some things in a game I play on Linux, but Linux doesn’t seem to have any good input display programs. I decided it would be good enough to have python print to a pair of xterm windows (one for keyboard and one for mouse) using pynput. I do this using two different python programs because the only way I found to have one python programs deal with two terminals is to constantly write to a file and use tail -f, and I didn’t want to do that.

Because both programs ended up being so small and simple, I tried to write it so that the key/mouse listeners would run their functions as fast as possible. But I might have missed some things that could be done. I’m wondering if anyone knows of any other ways to optimize this for response time. I know it won’t make anything close to a noticeable difference, I’m just asking out of curiosity.

mouse_listener.py

from pynput import mouse

def main():
    
    print_count = 0
    
    def on_click(x, y, button, pressed, local_print=print):
        nonlocal print_count
        try:
            if print_count >= 10:
                local_print(f"--- {button.name} mouse {'pressed' if pressed else 'released'}")
                print_count = 0
            else:
                local_print(f"    {button.name} mouse {'pressed' if pressed else 'released'}")
                print_count += 1
        except AttributeError:
            pass
    
    def on_scroll(x, y, dx, dy, local_print=print):
        nonlocal print_count
        try:
            if print_count >= 10:
                local_print(f"--- {'scroll up' if dy == 1 else 'scroll down'}")
                print_count = 0
            else:
                local_print(f"    {'scroll up' if dy == 1 else 'scroll down'}")
                print_count += 1
        except AttributeError:
            pass
    
    try:
        listener = mouse.Listener(on_click=on_click, on_scroll=on_scroll)
        listener.start()
        listener.join()
    except KeyboardInterrupt:
        listener.stop()

main()

key_listener.py

from pynput import keyboard

def main():
    
    print_count = 0
    held = set() # this is needed because it keeps calling on_press when you hold a key
    
    def on_press(key, local_print=print, held_local=held, hold=held.add, str_lower=str.lower):
        nonlocal print_count
        if key not in held_local:
            try:
                if print_count >= 10:
                    local_print(f"--- {key.name} pressed")
                    print_count = 0
                else:
                    local_print(f"    {key.name} pressed")
                    print_count += 1
            except AttributeError:
                if print_count >= 10:
                    local_print(f"--- {str_lower(key.char) if key.char is not None else 5} pressed")
                    print_count = 0
                else:
                    local_print(f"    {str_lower(key.char) if key.char is not None else 5} pressed")
                    print_count += 1
            hold(key)
    
    def on_release(key, local_print=print, unhold=held.remove, str_lower=str.lower):
        nonlocal print_count
        try:
            if print_count >= 10:
                local_print(f"--- {key.name} released")
                print_count = 0
            else:
                local_print(f"    {key.name} released")
                print_count += 1
        except AttributeError:
            if print_count >= 10:
                local_print(f"--- {str_lower(key.char) if key.char is not None else 5} released")
                print_count = 0
            else:
                local_print(f"    {str_lower(key.char) if key.char is not None else 5} released")
                print_count += 1
        try:
            unhold(key)
        except KeyError:
            pass
    
    try:
        listener = keyboard.Listener(on_press=on_press, on_release=on_release)
        listener.start()
        listener.join()
    except KeyboardInterrupt:
        listener.stop()

main()

xterm_opener.sh

xterm -xrm 'XTerm.vt100.allowTitleOps: false' -T "keyboard" -geometry 28x16 -e python3 '/home/USERNAME/input_listener/key_listener.py' &
xterm -xrm 'XTerm.vt100.allowTitleOps: false' -T "mouse" -geometry 28x16 -e python3 '/home/USERNAME/input_listener/mouse_listener.py'

python – Optimize Yahoo Finance Code for Analysis

I am trying to analyze a number of companies using financial data I gathered from Yahoo Finance. I am also using the yfinance API to get some more details about the company using functions. Since I am trying to do this for a number of companies Each Iteration needs to be quick. Currently, 1 Company takes about 3 seconds.

Is it because of the API calls or requests? Can I increase the below code efficiency?

import pandas as pd
import numpy as np
from pandas import ExcelWriter
import requests
import timeit
import yfinance as yf

def get_industry(stock):
    tickerdata = yf.Ticker(stock) 
    return tickerdata.info('industry')

def get_symbol(symbol):
    url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}&region=1&lang=en".format(symbol)
    result = requests.get(url).json()
    for x in result('ResultSet')('Result'):
        if x('symbol') == symbol:
            return x('name')
def get_currency(stock):
    tickerdata = yf.Ticker(stock) 
    return tickerdata.info('currency')


quarters = ('Q4-2020','Q3-2020','Q2-2020','Q1-2020','Q4-2019','Q3-2019','Q2-2019','Q1-2019','Q4-2018','Q3-2018','Q2-2018','Q1-2018')
start_time = timeit.default_timer()
r = ()
for i in stock(:1):
    try:
        df = pd.read_csv(i+'_quarterly_balance-sheet.csv')
        df = df.drop('ttm',axis=1,errors='ignore')
        df('name') = df('name').str.replace('t','')
        df = df.iloc(:,:13).T
        df.columns = df.iloc(0)
        df = df(1:)
        df.insert(0,'Q',quarters)
        try:
            df.insert(0,'Currency',get_currency(i))
        except:
            df.insert(0,'Currency','Not Found')
        df.insert(0,'Ticker',i)
        df.insert(0,'Company',get_symbol(i))
        try:
            df.insert(0,'Industry',get_industry(i))
        except:
            df.insert(0,'Industry','Unknown')
        df = df.loc(:,df.columns.isin(('Q','Currency','Ticker','Company','Industry','TotalRevenue')))
        r.append(df)
    except:
        continue
df = pd.concat(r)
# code you want to evaluate
elapsed = timeit.default_timer() - start_time
elapsed

3.041297 Seconds

Traduccion de C++ a Python

Soy nueva en Python y estoy intentando pasar un codigo de C++ a Python, pero estoy trabada en esta clase, algun consejo? Gracias

class TTablaAsoc {
    public:
    TTablaAsoc(int i);
    int& operator() (const char* c);
    friend ostream& operator<<(ostream& o,TTablaAsoc& t);
    ~TTablaAsoc();
    private:
    Tupla * pt;
    int maxLong;
    int primeroLibre;
    TTablaAsoc (const TTablaAsoc &t);
    TTablaAsoc& operator= (const TTablaAsoc &t);
};

python bytearray to C++ object

I’m wondering if I could get some help. For context, I’m using some C++ libraries to generate some large (think hundreds of Mb) objects that I want to send over a network from a server to a client.

On the server, I’ve got the following:

PyObject*  CKKSwrapper::SerializePrivateKey() {
    std::string s;
    std::ostringstream os(s);
    Serial::Serialize(m_keys.publicKey, os, SerType::BINARY);

    auto msg = os.str();
    return PyBytes_FromString(&msg(0));
}

which gives me some Python object. I then send this directly to the client via python sockets. I’m reading it in like

def _safe_recv_abstract(socket: Socket, deserializer_func):
    expected_length = _get_obj_size(socket)
    running_length = 0
    running_msg = bytearray()
    while running_length < expected_length:
        msg = socket.recv(expected_length)
        if msg:
            running_msg = cppwrapper.Accumulator(running_msg, bytearray(msg))
            running_length += len(msg)

    socket.send(_add_header_to_payload(b"ACK"))
    logger.debug("_safe_recv_unenc_obj: Received all data")
    if optional_pycrypto_deserialize_func:
        return deserializer_func(running_msg)
    return running_msg

two things:

  1. Accumulator (from cppwrapper.Accumulator() above) looks like
PyObject* CKKSwrapper::Accumulator(PyObject a, PyObject b){
    return PyByteArray_Concat(&a, &b);
}
  1. deserializer_func calls an underlying C++ function that looks like
void CKKSwrapper::DeserializeX(
    const boost::python::list &pyvals) {

    auto msg= pythonListToCppVectorBytes(pyvals);

    LPPrivateKey<DCRTPoly> sk;
    std::istringstream is(string(msg.begin(), msg.end()));
    Serial::Deserialize(sk, is, SerType::BINARY);

    this->m_keys.secretKey = sk;
}

I’m running into the following error:

Boost.Python.ArgumentError: Python argument types in
    CKKSwrapper.Accumulator(bytearray, bytearray)
did not match C++ signature:
    Accumulator(pycrypto::CKKSwrapper {lvalue}, _object*, _object*)

I completely understand what it is saying and that the types are wrong but I’m not sure WHY. From the docs

PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)
    Return value: New reference.

    Concat **bytearrays** a and b and return a new bytearray with the result.

If I understand correctly, I AM passing in bytearrays but it says that it is expecting objects?

The reason I’m trying to do it this way is that when I use a bytearray or a list for the accumulation, i.e

while running_length < expected_length:
    msg = socket.recv(expected_length)
    if msg:
        running_msg = cppwrapper.Accumulator(running_msg, bytearray(msg))
        running_length += len(msg)

the memory usage and runtime blow up