Invalid syntax when trying to add patch_http_response_read patch - python-3.x

Hello i'm trying to patch my python code with http://bobrochel.blogspot.com/2010/11/bad-servers-chunked-encoding-and.html but when adding this snippet anywhere in the code I always get invalid syntax. What am I doing wrong?
The start of my code looks like this:
import logging
import argparse
import sys
from arbitrer import Arbitrer
def patch_http_response_read(func):
def inner(*args):
try:
return func(*args)
except httplib.IncompleteRead, e:
return e.partial
return inner
httplib.HTTPResponse.read = patch_http_response_read(httplib.HTTPResponse.read)
class ArbitrerCLI:
def __init__(self):

except doesn't work that way anymore.
except httplib.IncompleteRead as e:

Indent correctly.
The try statement changed in Python 3.x.
import httplib
import logging
import argparse
import sys
from arbitrer import Arbitrer
def patch_http_response_read(func):
def inner(*args):
try:
return func(*args)
except httplib.IncompleteRead as e:
return e.partial
return inner
httplib.HTTPResponse.read = patch_http_response_read(httplib.HTTPResponse.read)
class ArbitrerCLI:
def __init__(self):
...

Related

I'm using python threading and when querying mysql the code seems to run and stays in an endless loop, returning no errors

I'm calling two functions at the same time and the functions are triggered, but when the mysql query is made, the code seems to run and stays in an infinite loop, not returning an error.
here is my code:
main.py
import threading
import siparis_kontrolu
import dolar_euro_guncelle
def dd1():
print("a")
siparis_kontrolu.siparis_kontrolu()
def dd2():
print("b")
dolar_euro_guncelle.dolar_euro_guncelle()
t1 = threading.Thread(target=dd1)
t2 = threading.Thread(target=dd2)
t1.start()
t2.start()
siparis_kontrolu.py
import mysql_db
def siparis_kontrolu():
try:
while True:
print("test1")
tum_kullanicilar = mysql_db.mysql_get_all("select * from users")
print("test2")
dolar_euro_guncelle.py
import urllib.request
import json
import mysql_db
import time
import logla
def dolar_euro_guncelle():
while True:
try:
print("f")
data = urllib.request.urlopen(
"https://finans.truncgil.com/today.json")
for line in data:
line = json.loads(line.decode('utf-8'))
USD = round(float(line['USD']["Satış"].replace(",", ".")), 2)
EUR = round(float(line['EUR']["Satış"].replace(",", ".")), 2)
mysql_db.mysql_update("update variables set EUR='" +
str(EUR)+"', USD='"+str(USD)+"' where id='4'")
time.sleep(10)
print("USD/EUR guncellendi.")
except Exception as e:
logla.logla(e, "dolar_euro_guncelle")
print(e)
mysql_db.py
from configparser import ConfigParser
import mysql.connector
config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)
mydb = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor = mydb.cursor(buffered=True, dictionary=True)
def mysql_update(sorgu):
try:
mycursor.execute(sorgu)
mydb.commit()
except Exception as e:
print(e)
def mysql_get_all(sorgu):
try:
mycursor.execute(sorgu)
return mycursor.fetchall()
except Exception as e:
print(e)
When I run main.py these are written to the console:
a
b
f
test1
test2 and USD/EUR guncellendi is not printed, I don't understand exactly what's wrong, when I trigger siparis_kontrolu.py directly without using threading, it works fine
It remains as it appears in the picture and the code does not stop.
But it doesn't do what I want it to do.
It looks like a concurrency issue with the DB connection (assuming your siparis_kontrolu.py doesn't throw syntax errors due to the missing except / finally part of the try statement, a sleep is also missing).
The "SELECT" statements conflict with the "Update" statements.
So it turns out: each thread needs its own database connection!
The easiest way is to open another database connection and create another cursor (maybe another cursor instance is enough) in your mysql_db.py:
from configparser import ConfigParser
import mysql.connector
config_file = 'config.ini'
config = ConfigParser()
config.read(config_file)
mydb = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor = mydb.cursor(buffered=True, dictionary=True)
mydb_upd = mysql.connector.connect(host=config['MYSQL']['DB_HOST'],
user=config['MYSQL']['DB_USERNAME'], passwd=config['MYSQL']['DB_PASSWORD'], database=config['MYSQL']['DB_DATABASE'])
mycursor_upd = mydb_upd.cursor(buffered=True, dictionary=True)
def mysql_update(sorgu):
try:
mycursor_upd.execute(sorgu)
mydb_upd.commit()
except Exception as e:
print(e)
def mysql_get_all(sorgu):
try:
mycursor.execute(sorgu)
return mycursor.fetchall()
except Exception as e:
print(e)
But creating a DbHandler class and use instances of it in the several threads would be much cleaner.

Python Error: AttributeError: __enter__ using Selenium

I am trying to run my selenium test, but I get an error.
First, I am creating booking.py file, which contains Booking class:
from asyncio import selector_events
from lib2to3.pgen2 import driver
import booking.constants as const
import os
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
class Booking:
def __init__(self, teardown = False):
s = Service(ChromeDriverManager().install())
self.driver = webdriver.Chrome(service=s)
self.driver.get(const.BASE_URL)
self.driver.teardown = teardown
self.driver.implicitly_wait(15)
def __exit__(self, exc_type, exc_val, exc_tb):
if self.driver.teardown:
self.driver.quit()
def cookies(self):
self.driver.find_element(By.ID, 'onetrust-accept-btn-handler').click()
def select_place_to_go(self):
self.driver.find_element(By.ID, "ss").click()
Then, I have run.py file:
from booking.booking import Booking
with Booking() as bot:
bot.cookies()
bot.select_place_to_go()
After running run.py file, I get an error:
AttributeError: __enter__
However, it works completely fine using this code:
bot = Booking()
bot.cookies()
bot.select_place_to_go()
Where is the problem?
f you have any ideas about code improvements, please, let me know. Any help is appreciated, thank you!
I'm guessing you're missing the __enter__ function on that class. When you use a with block in Python, the __enter__ method of the object in the with statement is called, the block inside the with runs, and the __exit__ method is invoked .
You'll get this error if your class doesn't have a __enter__ defined. so you have to define __enter__ method in your Booking class and return self in it
def select_place_to_go(self, place_to_go):
search_field = self.find_element(By.ID, 'ss').click()
search_field.clear()
search_field.send_keys(place_to_go)
This is the code I used and I run it without any issue

how to pytest a pyqt application that uses argparse for input parameters

I have a PyQt application that uses argparse to pass some argument.
I managed to write a simple test to see if the app starts
but I cannot set/mock the argparse arguments
I know it because inside the code I have some try/except like this
try:
if args.erase_data:
pass
except NameError:
logger.error("Error in parsing erase_data input argument \n")
that during the tests fail, while they do not fail if I run the app.
I tried this to mock args
import os
import pathlib
# import pdb
import sys
from unittest import mock
import pytest
from PyQt5 import QtTest
from PyQt5.QtWidgets import *
from pytestqt.plugin import QtBot
sys.path.append(os.getcwd())
src_dir = pathlib.Path(__file__).parents[1].absolute()
print(src_dir)
sys.path.append(src_dir.as_posix())
GUI = __import__("GUI")
#pytest.fixture(scope="module")
def qtbot_session(qapp, request):
result = QtBot(qapp)
with capture_exceptions() as e:
print(getattr(e, "message", repr(e)))
yield result
print(" TEARDOWN qtbot")
#pytest.fixture(scope="module")
def Viewer(request):
with mock.patch.object(sys, "argv", ["",'-d','2']):
print("mocking sys argv")
print(sys.argv)
# pdb.set_trace()
app, app_window = GUI.main()
qtbotbis = QtBot(app)
QtTest.QTest.qWait(0.5 * 1000)
assert app_window.isVisible()
return app, app_window, qtbotbis
but args is still not set.
any idea how to solve it?

What is the SyntaxError in the a "def" line in a python script?

I am trying to run the follow python script:
#!/usr/bin/env python
#Author Jared Adolf-Bryfogle
#Python Imports
import os
import sys
from pathlib import Path
from typing import Union
#Append Python Path
p = os.path.split(os.path.abspath(__file__))[0]+"/src"
sys.path.append(p) #Allows all modules to use all other modules, without needing to update pythonpath
#Project Imports
from pic2.modules.chains.ProposedIgChain import ProposedIgChain
from pic2.modules.chains.IgChainSet import IgChainSet
from pic2.modules.chains.IgChainFactory import IgChainFactory
from pic2.modules.chains.AbChainFactory import AbChainFactory
from pic2.tools.fasta import *
class IgClassifyFASTA:
"""
Identify CDRs from a Fasta File
"""
def __init__(self, fasta_path: Union[Path, str]):
self.fasta_path = str(fasta_path)
self.outdir = os.getcwd()
self.outname = "classified"
self.fasta_paths = split_fasta_from_fasta(os.path.abspath(self.fasta_path), "user_fasta_split_"+self.outname, self.outdir)
def __exit__(self):
for path in self.fasta_paths:
if os.path.exists(path):
os.remove(path)
def set_output_path(self, outdir: Union[Path, str]):
self.outdir = str(outdir)
def set_output_name(self, outname: Union[Path, str]):
self.outname = str(outname)
My python version is 3.8, and the pic2 is a conda env. I get the the following error:
File "IgClassifyFASTA.py", line 29
def __init__(self, fasta_path:Union[Path, str]):
SyntaxError: invalid syntax
I cannot figure out what's wrong with this line. Could you kindly give me some hint what's the wrong lying in? I will appreciate any help.
Best regards!

How to check if module import fails and output relvant error?

New to python
I want to do this:
def imp_modules(module):
try:
import module
except moduleName as failedModule:
print('Module ' + failedModule + ' failed to import ...')
sys.exit(1)
That is an example of what I want to achieve. I want all the modules I am importing to go through that function. I don't want to have any import module_name outside that function.
Can this be achieved with a solution that is windows/linux compatible?
You have to except a specific error, ModuleNotFoundError. Also, to import module from it's name, use importlib:
import importlib
def imp_modules(module_name):
try:
globals()[module_name] = importlib.import_module(module_name)
except ModuleNotFoundError as err_message:
failedModule = err_message.msg.split("'")[1]
print(f"Module '{failedModule}' failed to import ...")
sys.exit(1)
#USAGE:
imp_modules("module_name")
You could also have the function iterate over all the modules to import:
import importlib
def imp_modules(*module_names):
for module_name in module_names:
try:
globals()[module_name] = importlib.import_module(module_name)
except ModuleNotFoundError as err_message:
failedModule = err_message.msg.split("'")[1]
print(f"Module '{failedModule}' failed to import ...")
sys.exit(1)
#USAGE:
imp_modules("module_one_name","module_two_name", ...)

Resources