eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm building an SMTP server with aiosmtpd and used the examples as a base to build from. Below is the code snippet for the entry point to the program. if __name__ == '__main__': loop = asyncio.get_event_loop() loop.create_task(amain(loop=loop)) try: loop.run_forever() except KeyboardInterrupt: pass  When I run the program, I get the following warning: server.py:61: DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop()  What's the correct way to implement this?

Answer: Your code will run on Python3.10 but as of 3.11 it will be an error to call asyncio.get_event_loop when there is no running loop in the current thread. Since you need loop as an argument to amain, apparently, you must explicitly create and set it. It is better to launch your main task with asyncio.run than loop.run_forever, unless you have a specific reason for doing it that way. [But see below] Try this: if __name__ == '__main__': loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: asyncio.run(amain(loop=loop)) except KeyboardInterrupt: pass  Added April 15, 2023: There is a difference between calling asyncio.run(), which I have done here, and calling loop.run_forever() (as in the original question) or loop.run_until_complete(). When I wrote this answer I did not realize that asyncio.run() always creates a new event loop. Therefore in my code above, the variable loop that is passed to amain will not become the "running loop." So my code avoids the DeprecationWarning/RuntimeException, but it doesn't pass a useful loop into amain. To correct that, replace the line asyncio.run(amain(loop=loop))  with loop.run_until_complete(amain(loop=loop))  It would be best to modify amain to obtain the running event loop inside the function instead of passing it in. Then you could launch the program with asyncio.run. But if amain cannot be changed that won't be possible. Note that run_until_complete, unlike asyncio.run, does not clean up async generators. This is documented in the standard docs.
Question: I am using Amazon ECS on AWS Fargate, My instances can access the internet, but the connection drops after 350 seconds. On average, out of 100 times, my service is getting ConnectionResetError: [Errno 104] Connection reset by peer error approximately 5 times. I found a couple of suggestions to fix that issue on my server-side code, see here and here Cause If a connection that's using a NAT gateway is idle for 350 seconds or more, the connection times out. When a connection times out, a NAT gateway returns an RST packet to any resources behind the NAT gateway that attempt to continue the connection (it does not send a FIN packet). Solution To prevent the connection from being dropped, you can initiate more traffic over the connection. Alternatively, you can enable TCP keepalive on the instance with a value less than 350 seconds. Existing Code: url = "url to call http" params = { "year": year, "month": month } response = self.session.get(url, params=params)  To fix that I am currently using a band-aid retry logic solution using tenacity, @retry( retry=( retry_if_not_exception_type( HTTPError ) # specific: requests.exceptions.ConnectionError ), reraise=True, wait=wait_fixed(2), stop=stop_after_attempt(5), ) def call_to_api(): url = "url to call HTTP" params = { "year": year, "month": month } response = self.session.get(url, params=params)  So my basic question is how can I use python requests correctly to do any of the below solutions,  Close the connection before 350 seconds of inactivity  Enable Keep-Alive for TCP connections  

Answer: Posting solution for the future user who will face this issue while working on AWS Farget + NAT, We need to set the TCP keepalive settings to the values dictated by our server-side configuration, this PR helps me a lot to fix my issue: https://github.com/customerio/customerio-python/pull/70/files import socket from urllib3.connection import HTTPConnection HTTPConnection.default_socket_options = ( HTTPConnection.default_socket_options + [ (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), (socket.SOL_TCP, socket.TCP_KEEPIDLE, 300), (socket.SOL_TCP, socket.TCP_KEEPINTVL, 60) ] )
Question: I am using celery with a fastAPI. Getting Can't decode message body: ContentDisallowed('Refusing to deserialize untrusted content of type json (application/json)') while running in docker. When running the same in local machine without docker there is not issue. The configuration for the same is as below. celery_app = Celery('cda-celery-tasks', broker=CFG.BROKER_URL, backend=CFG.BACKEND_URL, include=['src.tasks.tasks'] ) celery_app.conf.task_serializer = 'pickle' celery_app.conf.result_serializer = 'pickle' celery_app.conf.accept_content = ['pickle'] celery_app.conf.enable_utc = True  While Running in docker I am getting the error continuously FROM python:3.8 WORKDIR /app COPY . . RUN pip3 install poetry ENV PATH="/root/.poetry/bin:$PATH" RUN poetry install  the celery is started using the following command from kubernetes. poetry run celery -A src.infrastructure.celery_application worker --loglevel=INFO --concurrency 2 While running I am getting the error continuously Can't decode message body: ContentDisallowed('Refusing to deserialize untrusted content of type json (application/json)') body: '{"method": "enable_events", "arguments": {}, "destination": null, "pattern": null, "matcher": null}' (99b) Traceback (most recent call last): File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/messaging.py", line 620, in _receive_callback decoded = None if on_m else message.decode() File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 194, in decode self._decoded_cache = self._decode() File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/message.py", line 198, in _decode return loads(self.body, self.content_type, File "/root/.cache/pypoetry/virtualenvs/cda-9TtSrW0h-py3.8/lib/python3.8/site-packages/kombu/serialization.py", line 242, in loads raise self._for_untrusted_content(content_type, 'untrusted') kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type json (application/json)  Could someone please tell me the possible cause and solution to manage the same? If I've missed anything, over- or under-emphasized a specific point, please let me know in the comments. Thank you so much in advance for your time.

Answer: Configuring the celery_app with the accept_content type seems to fix the issue: celery_app.conf.accept_content = ['application/json', 'application/x-python-serialize', 'pickle']
Question: I have a docker image that I want to run inside my django code. Inside that image there is an executable that I have written using c++ that writes it's output to google cloud storage. Normally when I run the django code like this: container = client.V1Container(name=container_name, command=["//usr//bin//sleep"], args=["3600"], image=container_image, env=env_list, security_context=security)  And manually go inside the container to run this: gcloud container clusters get-credentials my-cluster --region us-central1 --project proj_name && kubectl exec pod-id -c jobcontainer -- xvfb-run -a "path/to/exe"  It works as intended and gives off the output to cloud storage. (I need to use a virtual monitor so I'm using xvfb first). However I must call this through django like this: container = client.V1Container(name=container_name, command=["xvfb-run"], args=["-a","\"path/to/exe\""], image=container_image, env=env_list, security_context=security)  But when I do this, the job gets created but never finishes and does not give off an output to the storage. When I go inside my container to run ps aux I get this output: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 2888 1836 ? Ss 07:34 0:00 /bin/sh /usr/bin/xvfb-run -a "path/to/exe" root 16 0.0 1.6 196196 66256 ? S 07:34 0:00 Xvfb :99 -screen 0 1280x1024x24 -nolisten tcp -auth /tmp/xvfb-run.r5gaBO/Xauthority root 35 0.0 0.0 7016 1552 ? Rs 10:31 0:00 ps aux  It looks like it's stuck inside my code but my code does not have a loop that it can stuck inside, perhaps there is an error occurring (I don't think so since the exact same command is working when typed manually). If there is an error how can I see the console output? Why is my code get stuck and how can I get my desired output? Could there be an error caused by permissions (The code does a lot of stuff that requires permissions like writing to storage and reading files inside the pod, but like mentioned works normally when i run it via the command line)?

Answer: Apparently for anyone having a similar issue, we fixed it by adding the command we want to run at the end of the Dockerfile instead of passing it as a parameter inside django's container call like this: cmd["entrypoint.sh"]  entrypoint.sh: xvfb-run -a "path/to/exe"  Instead of calling it inside django like we did before and simply removing the command argument from the container call so it looked like this: container = client.V1Container(name=container_name, image=container_image, env=env_list, stdin=True, security_context=security)
Question: Here's an example of the file-structure of my app, which I'm trying to turn into a standalone distributable one-dir application using auto-py-to-exe: - plugins/file1.py - plugins/file2.py - plugins/... - plugins/display/file1.py - plugins/display/... - main.py - UI.py  The files which are under the plugins directory are dynamically imported using __import__. Here's the part of the code that does that: for plugin_filename in plugin_files[ plugin_path ]: plugin_file = plugin_filename.replace('.pye', '').replace('.py', '') plugin = __import__(plugin_file)  Of course, this doesn't get recognized by the Pyinstaller analysis and the plugin files don't get bundled into the exe. From the documentation I've gathered that I need to use the --hidden-import option, so I tried this but with no success. This results in ERROR: Hidden import not found:  Is there any way I can add these files to the exe?

Answer: I’ve managed to solve this by adding the absolute path of the plugins folder to paths and adding file1, file2, etc. to the —-hidden-import option. I also had to get rid of the if statement that checks if the file is a Python one and replaced that block with a loop that looks something like this: modules = [‘file1’, ‘file2’] for module in modules: plugin = __import__(module)
Question: I am developing a GUI using Flask. I am running Ubuntu 20.04. The data that I need is from MongoDB, which is running on a Docker container on a server. My Mongo URI is "mongodb://<USERNAME>:<PASSWORD>@localhost:9999/<DB>?authSource=<AUTHDB>". First I tunnel the SSH connection to the server: ssh -L 9999:localhost:27017 -N <USER>@<HOST>  When I connect to a Mongo shell after tunneling the connection, it works as expected. The issue is when I try to run the Flask app. PyMongo cannot seem to connect to the database (connection refused). pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:9999: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 62fa50c9015294b6e1df5811, topology_type: Unknown, servers: [<ServerDescription ('127.0.0.1', 9999) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:9999: [Errno 111] Connection refused')>]>  Another error I've been seeing is PyMongo authentication failed. This only happens if I have a Docker container with Mongo running on my local machine. (The previous error only happens when the local container is not running). pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed', '$clusterTime': {'clusterTime': Timestamp(1660576452, 1), 'signature': {'hash': b'\x16\x8c\x11\xb2Uf\xe3\x80\x85\xb0 >\xcb\xbe\xa5S\x8f\xec(g', 'keyId': 7128054189753630722}}, 'operationTime': Timestamp(1660576452, 1)}  One last thing: I have not experienced any errors when trying to connect to the local MongoDB running in a local Docker container. This leads me to believe that I am missing a step when connecting to the remote MongoDB. And it's not a problem with the URI since I can log into a Mongo shell with it when the SSH connection is up. Here is some code from my Flask app: init.py from flask import Flask from flask_bootstrap import Bootstrap from .frontend import frontend from .database import mongo def create_app(configfile=None): app = Flask(__name__) app.config["MONGO_URI"] = "mongodb://<USERNAME>:<PASSWORD>@localhost:9999/<DB>?authSource=<AUTHDB>" Bootstrap(app) mongo.init_app(app) app.register_blueprint(frontend) app.secret_key = os.environ['SECRET_KEY'] app.config['BOOTSTRAP_SERVER_LOCAL'] = True return app  database.py from flask_pymongo import PyMongo mongo = PyMongo()  frontend.py from flask import Blueprint from .database import mongo frontend = Blueprint('frontend', __name__) # Rest of file contains routes with calls to database  Does anyone know why I am getting these errors? Thanks.

Answer: I discovered that pymongo isn't setting the port correctly, possibly due to a bug in the package. Even though I specify the port as 9999, it thinks it should connect to 27017. If I set the URI to "mongodb://<USERNAME>:<PASSWORD>@garbage:9999/<DB>?authSource=<AUTHDB>", then it will actually try to connect to port 9999. I ended up changing my ssh tunnel to port 27017, and everything worked. ssh -L 27017:localhost:27017 -N <USER>@<HOST>
Question: I'm working on a python project and the package supports python 3.6 - 3.10. There were these 2 lines in the install_requires list in setup.py:  "numpy>=1.18.5, <=1.19.5; python_version=='3.6'", "numpy>=1.19.5; python_version>='3.7'",  And I tried to change them to  "numpy>=1.18.5, <=1.19.5; python_version=='3.6'", "numpy>=1.23.1; python_version>='3.10'", "numpy>=1.19.5; python_version>='3.7', <'3.10'",  And when I ran python setup.py install, I got this error: $ python setup.py install # yeah, I know `pip install .` is a better command. error in mypackage setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Parse error at "", <'3.10"": Expected string_end  I tried some different variants to specify a python_version range of 3.7 to 3.9, but none of them worked. So how do I specify python version range for a specific dependency in setup.py?

Answer: Referring to Complete Grammar, you can use and to achieve your purpose. "numpy>=1.18.5, <=1.19.5; python_version=='3.6'", "numpy>=1.23.1; python_version>='3.10'", "numpy>=1.19.5; python_version>='3.7' and python_version<'3.10'",
Question: I'm reading env variables from .prod.env file in my config.py: from pydantic import BaseSettings class Settings(BaseSettings): A: int class Config: env_file = ".prod.env" env_file_encoding = "utf-8" settings = Settings()  in my main.py I'm creating the app like so: from fastapi import FastAPI from app.config import settings app = FastAPI() print(settings.A)  I am able to override settings variables like this in my conftest.py: import pytest from fastapi.testclient import TestClient from app.main import app from app.config import settings settings.A = 42 @pytest.fixture(scope="module") def test_clinet(): with TestClient(app) as client: yield client  This works fine, whenever I use settings.A I get 42. But is it possible to override the whole env_file from .prod.env to another env file .test.env? Also I probably want to call settings.A = 42 in conftest.py before I import app, right?

Answer: You can override the env file you use by creating a Settings instance with the _env_file keyword argument. From documentation:  Passing a file path via the _env_file keyword argument on instantiation (method 2) will override the value (if any) set on the Config class. If the above snippets were used in conjunction, prod.env would be loaded while .env would be ignored.  For example, this should work for your test - import pytest from fastapi.testclient import TestClient import app.config as conf from app.config import Settings # replace the settings object that you created in the module conf.settings = Settings(_env_file='.test.env') from app.main import app # just to show you that you changed the module-level # settings from app.config import settings @pytest.fixture(scope="module") def test_client(): with TestClient(app) as client: yield client def test_settings(): print(conf.settings) print(settings)  And you could create a .test.env, set A=10000000, and run with pytest -rP conftest.py # stuff ----- Captured stdout call ----- A=10000000 A=10000000  This looks a little messy (though this is probably only used for test purposes), so I'd recommend not creating a settings object that is importable by everything in your code, and instead making it something you create in, say, your __main__ that actually creates and runs the app, but that's a design choice for you to make.
Question: I am currently trying to save my whole Jupyter Notebook environment (working throught Anaconda 3). By environment, I mean, all the objects created (dataframes, lists, tuples, models, ...). Unfortunately I don't have Linux even though there seemed to be Linux command solutions. I tried finding a solution with pickle as recommended in the following topic but it seems that you have to specify which objects you want to dump and load. saving-and-loading-multiple-objects-in-pickle What I would like to do is that everything saves and loads as it can be done with R, where you just save and load an .RData file.

Answer: You can use Dill to store your session pip install dill  Save a Notebook session: import dill dill.dump_session('notebook_env.db')  Restore a Notebook session: import dill dill.load_session('notebook_env.db')
Question: I have two sets of column vectors X = (X_1 ... X_n), Y = (Y_1 ... Y_n) of same shape. I would like to compute something like this:  i.e the minimal L2 norm between X and Y up to column permutation. Is it possible to do it in less than O(n!)? Is it already implemented in Numpy for instance? Thank you in advance.

Answer: Apply scipy.optimize.linear_sum_assignment to the matrix A where Aij = ‖Xi − Yj‖.
Question: I am trying to add a horizontal bar at the bottom of each pie chart in a figure. I am using subplots to achieve this, but I don't know how to customise the subplots with the horizontal bars. import matplotlib.pyplot as plt fig, axes = plt.subplots(28, 11) countery=0 for y in range(1,15): counterx=0 for x in range(1,12): axes[countery,counterx].pie([70,20]) axes[countery+1,counterx].barh('a',40,height=1) axes[countery+1,counterx].barh('a',60,left=40,height=1) axes[countery+1,counterx].axis('off') counterx=counterx+1 countery=countery+2 plt.show()   I would like to change the size of the horizontal bar so it doesn't take all the horizontal space. and make it look smaller overall. I would like it to look something like this for each:  I've tried changing wspace and hspace in plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=5) but no luck. Any help with this?

Answer: You can set the box_aspect of the bar charts. Also, using 14 rows with subgridspecs of 2 rows each is slightly faster than the 28 rows gridspec: import matplotlib.pyplot as plt fig = plt.figure() gs = fig.add_gridspec(14, 11) for y in range(gs.nrows): for x in range(gs.ncols): ax_pie, ax_bar = gs[y, x].subgridspec(2, 1, height_ratios=[5, 1]).subplots() ax_pie.pie([70,20]) ax_bar.barh('a', 40, height=1) ax_bar.barh('a', 60, left=40, height=1) ax_bar.set_box_aspect(1/5) ax_bar.axis('off')
Question: I have an app which convert the image into pencil sketch in that app i need three changes in the buttons  Need to align the both buttons into center Need to give some colour to the buttons The both button should be in same size  Sample Code:  import streamlit as st #web app and camera import numpy as np # for image processing from PIL import Image #Image processing import cv2 #computer vision def dodgeV2(x, y): return cv2.divide(x, 255 - y, scale=256) def pencilsketch(inp_img): img_gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY) img_invert = cv2.bitwise_not(img_gray) img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0) final_img = dodgeV2(img_gray, img_smoothing) return(final_img) def download_image(x): with open(x, "rb") as file: btn = st.download_button( label="Download image", data=file, file_name=x, mime="image/jpg" ) def email_box(x): if st.checkbox("Email"): form = st.form(key='my-form') name = form.text_input('Enter your name') submit = form.form_submit_button('Send Email') if submit: st.write(f'x {name}') file_image = st.camera_input(label = "Take a pic of you to be sketched out") if file_image: input_img = Image.open(file_image) final_sketch = pencilsketch(np.array(input_img)) st.write("**Output Pencil Sketch**") st.image(final_sketch, use_column_width=True) download_image("final_image.jpeg") email_box("hello") else: st.write("You haven't uploaded any image file")   

Answer: I have modified the above code. Hope it helps customized_button = st.markdown(""" <style > .stDownloadButton, div.stButton {text-align:center} .stDownloadButton button, div.stButton > button:first-child { background-color: #ADD8E6; color:#000000; padding-left: 20px; padding-right: 20px; } .stDownloadButton button:hover, div.stButton > button:hover { background-color: #ADD8E6; color:#000000; } } </style>""", unsafe_allow_html=True)
Question: In newly installed Qt Creator 8.0.1, the Design Mode is disabled. I can only code in QML in Edit Mode. I can easily reproduce the problem by just creating a new Python Qt Quick Project as shown below. The Design Button on the left menu is always disabled.  I tried to add all modules related to the Design Mode with Qt maintenance tool, but it's still disabled. I really get stuck on this configuration problem.

Answer: You can enable the QmlDesigner by going to Help > About Plugins... and using the filter to find QmlDesigner. Activate the CheckBox and restart QtCreator. https://www.qt.io/blog/qt-creator-6-released  The integrated Qt Quick Designer is now disabled by default. Qt Creator will open .ui.qml files in Qt Design Studio. This is a step towards a more integrated workflow between Qt Design Studio and Qt Creator (video). Qt Quick Designer is still there, you can manually enable it again by checking the QmlDesigner plugin in Help > About Plugins.
Question: I have a string that resembles the following string: 'My substring1. My substring2: My substring3: My substring4'  Ideally, my aim is to extract 'My substring2' from this string with Python regex. However, I would also be pleased with a result that resembles '. My substring2:' So far, I am able to extract '. My substring2: My substring3:'  with "\.\s.*:"  Alternatively, I have been able to extract - by using Wiktor Stribiżew's solution that deals with a somewhat similar problem posted in How can i extract words from a string before colon and excluding \n from them in python using regex - 'My substring1. My substring2'  specifically with r'^[^:-][^:]*'  However, I have been unable, after many hours of searching and trying (I am quite new to regex), to combine the two results into a single effective regex expression that will extract 'My substring2' out of my aforementioned string. I would be eternally greatfull if someone could help me find to correct regex expression to extract 'My substring2'. Thanks!

Answer: You can use non-greedy regex (with ?): import re s = "My substring1. My substring2: My substring3: My substring4" print(re.search(r"\.\s*(.*?):", s).group(1))  Prints: My substring2