eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want to restrict scope of functions that can be passed as parameter to another function. For example, to restrict functions to be only one from two specified, or from particular module, or by signature. I tried the code below but in it there is now restrictions: as parameter can be passed any function. Is this possible in Python? def func_as_param(): print("func_as_param called") def other_func(): print("other_func called") def func_with_func_arg(func: func_as_param): # this is not giving restrictions # def func_with_func_arg(func: type(func_as_param)): # this is also not giving restrictions print("func_with_func_arg called") func() def test_func_with_func_arg(): print("test_func_with_func_arg") func_with_func_arg(func_as_param) func_with_func_arg(other_func) # <- here IDE must complain that only func_as_param is expected 

Answer:  Frameworks expecting callback functions of specific signatures might be type hinted using Callable[[Arg1Type, Arg2Type], ReturnType]  You might want to use the Callable type. This might help https://docs.python.org/3/library/typing.html#annotating-callable-objects NOTE Type annotations in Python are not make-or-break like in C. They’re optional chunks of syntax that we can add to make our code more explicit. Erroneous type annotations will do nothing more than highlight the incorrect annotation in our code editor — no errors are ever raised due to annotations. If thats necessary, you must do the checking by yourself.
Question: I am using a Python Mock object for a third-party package that needs to JSON serialize my mock. This means that I cannot change the invocation of json.dumps, so must use the solution here: https://stackoverflow.com/a/31207881/19643198 class FileItem(dict): def __init__(self, fname): dict.__init__(self, fname=fname) f = FileItem('tasks.txt') json.dumps(f) #No need to change anything here  The only problem is that my object is not of class FileItem, but needs to be a MagicMock. This suggests multiple inheritance, so something like: class FileItem(MagicMock, dict): def __init__(self): MagicMock.__init__(self) dict.__init__(self)  Unfortunately, multiple inheritance from both dict and MagicMock seems not to work. In case this helps make this problem easier to solve, the third-party library does not need to deserialize or even use the JSON serialized representation of the MagicMock.

Answer: There is no need to consider multiple inheritance. The problem is that Python's JSON module doesn't know how to serialize certain types, like MagicMock (or other common types like datetime, for that matter). You can tell json how to deal with unknown types by either using the cls= or default= parameters, but since you are asking about unit tests and don't want to modify the code you're testing, you can solve this by also mocking json.dumps (or json.dump). For instance, import json from json import dumps as _dumps from unittest.mock import MagicMock, Mock f1 = {'fname': 'tasks.txt'} # a normal dict f2 = {'fname': MagicMock()} # some value containing a MagicMock instance def dumps_wrapper(*args, **kwargs): return _dumps(*args, **(kwargs | {"default": lambda obj: "mock"})) # mock the `dumps` function json.dumps = MagicMock(wraps=dumps_wrapper) # now you can serialize objects containing MagicMock (or Mock) objects json.dumps(f1) # {"fname": "tasks.txt"} json.dumps(f2) # {"fname": "mock"}  In your unit tests, you can use the patch function to temporarily patch the json.dumps function: # my_module.py import json def make_json(obj): return json.dumps(obj) # test_mocked_json.py import unittest from unittest.mock import MagicMock, patch from json import dumps as _dumps import my_module def dumps_wrapper(*args, **kwargs): return _dumps(*args, **(kwargs | {"default": lambda obj: "mock"})) class TestMockedJSON(unittest.TestCase): def test_mocked_json(self): patch_json = patch( "my_module.json.dumps", MagicMock(wraps=dumps_wrapper)) with patch_json: f1 = {'fname': 'tasks.txt'} # a normal dict f2 = {'fname': MagicMock()} # some value containing a MagicMock instance s1 = my_module.make_json(f1) s2 = my_module.make_json(f2) assert s1 assert s2 print(s1, s2, "ok!")  Which you can run with: python -m unittest test_mocked_json.py # {"fname": "tasks.txt"} {"fname": "mock"} ok! # . # ---------------------------------------------------------------------- # Ran 1 test in 0.001s # # OK
Question: I have some trouble for installing USD library on Ubuntu. Here is the tuto I want to follow. On github, I cloned the git, run the script build_usd.py and change the env var. But when I want to run this simple code from pxr import Usd, UsdGeom stage = Usd.Stage.CreateNew('HelloWorld.usda') xformPrim = UsdGeom.Xform.Define(stage, '/hello') spherePrim = UsdGeom.Sphere.Define(stage, '/hello/world') stage.GetRootLayer().Save()  By changing the env var, eitheir the command "python " can't be found or the "module pxr" can't be found. I tried to move the lib and include in my directory in usr/lib, ur/local/lib, usr/local/include but still pxr seems not to be found. I am really confused about how to install and use library in general for python to found on ubuntu.

Answer: You should install usd-core library, which has the pxr module pip install usd-core
Question: I am trying to learn how to use the Pytorch profiler API to measure the difference in performance when training a model using different methods. In the dedicated tutorial, there is one part where they show how to do just that using the "schedule" parameter of the profiler. My problem is that when I want to use it in my code, calling step the first "wait" times prints a message  [W kineto_shim.cpp:337] Profiler is not initialized: skipping step() invocation  Since I want my profiler to sleep most of the time, my "wait" value is quite high so it pollutes my terminal with a bunch of those lines until the profiler is actually executed for the first time How can I get rid of it ? Here's a minimal code sample that reproduces the problem import torch from torch.profiler import profile, record_function, ProfilerActivity with profile( activities=[torch.profiler.ProfilerActivity.CUDA], schedule=torch.profiler.schedule(wait=15, warmup=1, active=4), profile_memory=False, record_shapes=True, with_stack=True, ) as prof: for _ in range(20): y = torch.randn(1).cuda() + torch.randn(1).cuda() prof.step() print(prof.key_averages()) 

Answer: This was just recently fixed/added in a pull request now you can set the env variable KINETO_LOG_LEVEL. For example in a bash script: export KINETO_LOG_LEVEL=3 The levels according to the source code are: enum LoggerOutputType { VERBOSE = 0, INFO = 1, WARNING = 2, ERROR = 3, STAGE = 4, ENUM_COUNT = 5 };  Thats atleast how it should work, according to this issue the changes for the log level have not been merged yet
Question: How can I modify request from inside a dependency? Basically I would like to add some information (test_value) to the request and later be able to get it from the view function (in my case root() function). Below is a simple example: from fastapi import FastAPI, Depends, Request app = FastAPI() def test(request: Request): request['test_value'] = 'test value' @app.get("/", dependencies=[Depends(test)]) async def root(request: Request): print(request.test_value) return {"test": "test root path."} 

Answer: Option 1 You could store arbitrary extra state to request.state, and use the Request object inside the endpoint to retrieve the state (the relevant implementation of Starlette's State method and class can be found here and here, respectively): from fastapi import FastAPI, Depends, Request app = FastAPI() def func(request: Request): request.state.test = 'test value' @app.get('/', dependencies=[Depends(func)]) def root(request: Request): return request.state.test  If you would like that state (i.e., test attribute above) to be globally accessible from any request/user, you might want to store it on the application instance, as described in this answer, as well this and this answer. Option 2 Instead of adding dependencies=[Depends(test)] to the decorator of the endpoint, you could use the dependency injection directly, by defining test (in this example) as an endpoint parameter and using Depends. You could then have the dependency function returning the attribute. Using this option, however, you would have to do this for every endpoint in your app/router, if you had to define a global dependency for the entire app/router, as demonstrated here and here. from fastapi import FastAPI, Depends, Request app = FastAPI() def func(request: Request): return 'test value' @app.get('/') def root(request: Request, test: str = Depends(func)): return test
Question: Looking at https://docs.celeryq.dev/en/v5.2.7/getting-started/backends-and-brokers/index.html it sounds pretty much as if it's not possible / desirable. There is a section about SQLAlchemy, but Django does not use SQLAlchemy. In way older docs, there is https://docs.celeryq.dev/en/3.1/getting-started/brokers/django.html . Is it possible with recent Celery / Django versions to use Celery with just the database for storing messages / results?

Answer: Yes you can totally do this, even if it's not the most performant/recommended way to do. I use it for simple projects in which I don't want to add Redis. To do so, first, add SQLAlchemy v1 as a dependency in your project: SQLAlchemy = "1.*"  Then in your settings.py:  if you use PostgreSQL: CELERY_BROKER_URL = sqla+postgresql://user:pwd@127.0.0.1:5432/dbname if you use SQLite: CELERY_BROKER_URL = "sqla+sqlite:///" + os.path.join(BASE_DIR, 'your_database.db') . Note that the folder holding the database must be writable. For example if your database is located in project/dbfolder/database.db, chmod 777 project/dbfolder will do the trick.  As a sidenote, I'm using django-celery-results to store results of my tasks. This way, I have a fully featured Celery without using other tech tool (like rabbitMQ or Redis) in my stack.
Question: I have the following existing Python environments: $ conda info --envs base * /home/ubuntu/anaconda3 tensorflow2_latest_p37 /home/ubuntu/anaconda3/envs/tensorflow2_latest_p37  What I want to do is to activate tensorflow2_latest_p37 environment and use it in R code. I tried the following code: library(reticulate) use_condaenv( "tensorflow2_latest_p37") library(tensorflow) tf$constant("Hello Tensorflow!")  But it failed to recognize the environment: > library(reticulate) > use_condaenv( "tensorflow2_latest_p37") /tmp/RtmpAs9fYG/file41912f80e49f.sh: 3: /home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/etc/conda/activate.d/00_activate.sh: Bad substitution Error in Sys.setenv(PATH = new_path) : wrong length for argument In addition: Warning message: In system2(Sys.which("sh"), fi, stdout = if (identical(intern, FALSE)) "" else intern) : running command ''/bin/sh' /tmp/RtmpAs9fYG/file41912f80e49f.sh' had status 2  What is the right way to do it?

Answer: I found the most reliable way is to set the RETICULATE_PYTHON system variable before running library(reticulate), since this will load the default environment and changing environments seems to be a bit of an issue. So you should try something like this: library(tidyverse) py_bin <- reticulate::conda_list() %>% filter(name == "tensorflow2_latest_p37") %>% pull(python) Sys.setenv(RETICULATE_PYTHON = py_bin) library(reticulate)  You can make this permanent by placing this in an .Renviron file. I usually place one in the project folder, so it is evaluated upon opening the project. In code this would look like that: readr::write_lines(paste0("RETICULATE_PYTHON=", py_bin), ".Renviron", append = TRUE)  Or even easier, use usethis::edit_r_environ(scope = "project") (thank you @rodrigo-zepeda!).
Question: I currently have a conda environment tf_gpu and I pip installed pipreqs in it to auto generate requirements.txt Now, in my project folder, I have app.py with the imports : import os from dotenv import load_dotenv from flask import Flask, request from predict import get_recs import urllib.request  Also, predict uses pandas, scipy, numpy, pickle So, but the requirements.txt generated by pipreqs using pipreqs ./ inside the project folder only gets me the following: Flask==2.1.3 numpy==1.23.3 pandas==1.4.4 scipy==1.9.1  Why is python-dotenv not included? It isnt a standard library right? So what's happening here?

Answer: According to the open issues in the GitHub repo, some packages don't map well. You could try opening an issue for this package.
Question: i use the requests library. how can this be the default behavior? any way to return the ID of the item created? def create_sales_order(): url = f"https://{url_account}.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder" data = { "entity": { "id": "000" }, "item": { "items": [ { "item": { "id": 25 }, "quantity": 3, "amount": 120 } ] }, "memo": "give me money", "Department": "109" } body = json.dumps(data) response = client.post(url=url, headers=headers, data=body) print(response.text) 

Answer: Ok so it turns out that the header returned in the 204 empty response contains a link to the created item (Location is the key name in the json returned) , which is sufficient to do another get request and have all the info returned.
Question: I recently updated Flask-SQLAlchemy, and now db.create_all is raising RuntimeError: working outside of application context. How do I call create_all? from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///project.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) db.create_all()  This raises the following error: Traceback (most recent call last): File "/home/david/Projects/flask-sqlalchemy/example.py", line 11, in <module> db.create_all() File "/home/david/Projects/flask-sqlalchemy/src/flask_sqlalchemy/extension.py", line 751, in create_all self._call_for_binds(bind_key, "create_all") File "/home/david/Projects/flask-sqlalchemy/src/flask_sqlalchemy/extension.py", line 722, in _call_for_binds engine = self.engines[key] File "/home/david/Projects/flask-sqlalchemy/src/flask_sqlalchemy/extension.py", line 583, in engines app = current_app._get_current_object() # type: ignore[attr-defined] File "/home/david/Projects/flask-sqlalchemy/.venv/lib/python3.10/site-packages/werkzeug/local.py", line 513, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed the current application. To solve this, set up an application context with app.app_context(). See the documentation for more information. 

Answer: As of Flask-SQLAlchemy 3.0, all access to db.engine (and db.session) requires an active Flask application context. db.create_all uses db.engine, so it requires an app context. with app.app_context(): db.create_all()  When Flask handles requests or runs CLI commands, a context is automatically pushed. You only need to push one manually outside of those situations, such as while setting up the app.  Instead of calling create_all in your code, you can also call it manually in the shell. Use flask shell to start a Python shell that already has an app context and the db object imported. $ flask shell >>> db.create_all()  Or push a context manually if using a plain python shell. $ python >>> from project import app, db >>> app.app_context().push() >>> db.create_all()
Question: To start I tried this def x(): try: 1/0 # just an division error to get an exception except: x()  And this code behaves normally in 3.10 and I get RecursionError: maximum recursion depth exceeded as I expected but 3.8 goes into a stack overflow and doesn't handle the recursion error properly. But I did remember that there was RecursionError in older versions of Python too, so I tried def x(): x()  And this gives back RecursionError in both versions of Python. It's as if (in the first snippet) the recursion error is never thrown in the except but the function called and then the error thrown at the first instruction of the function called but handled by the try-except. I then tried something else: def x(): try: x() except: x()  This is even weirder in some way, stack overflow below 3.10 but it get stuck in the loop in 3.10 Can you explain this behavior? UPDATE @MisterMiyagi found a even stranger behavior, adding a statement in the except in <=python3.9 doesn't result in a stackoverflow def x(): try: 1/0 except: print("") x() 

Answer: The different behaviors for 3.10 and other versions seem to be because of a Python issue (python/cpython#86666), you can also see the correct error on Python 2.7. The print "fixes" things because it makes Python check the recursion limit again, and through a path that is presumably not broken. You can see the code where it does that here, it also skips the repeated check if the object supports the Vectorcall calling protocol, so things like int keep the fatal error.
Question: I am creating a genetic algorithm to solve the traveling salesman problem using python and networkx. And I'm adding a condition to converge to a satisfactory solution: the path must not have crossing edges. I wonder if there's a quick function in networkx to verify if the graph has crossing edges or, at least, want to know if it's possible to create one. The graph is created with a list of points (path), each point has a coordinate in x, and a coordinate in y. The sequence of points index the path to tour. I created an object nx.Graph() like below:  G = nx.Graph() for i in range(len(path)): G.add_node(i, pos=(path[i].x, path[i].y)) for i in range(len(path)-1): G.add_edge(i, i+1) G.add_edge(len(path)-1, 0)  One example of converging not optimal solution:  printing out the points with nx.get_node_attributes(G,'pos'): {0: (494, 680), 1: (431, 679), 2: (217, 565), 3: (197, 581), 4: (162, 586), 5: (90, 522), 6:(138, 508), 7: (217, 454), 8: (256, 275), 9: (118, 57), 10: (362, 139), 11: (673, 89), 12: (738, 153), 13: (884, 119), 14: (687, 542), 15: (720, 618), 16: (745, 737), 17: (895, 887), 18: (902, 574), 19: (910, 337), 20: (823, 371), 21: (601, 345), 22: (608, 302), 23: (436, 294), 24: (515, 384), 25: (646, 495)}  Here is an article supporting the condition of convergence: http://www.ams.org/publicoutreach/feature-column/fcarc-tsp

Answer: My first reading was the same as @AveragePythonEngineer's. Normally in the travelling salesman problem, and graph theory in general, we don't care too much about the positions of the vertices, only the distances between them. And I thought you might be confusing the drawing of a graph with the graph (it's just one realization of infinite possible drawings). So while you can draw a planar graph with crossing edges if you wish (like your example), the point is that you could draw it in the plane. On re-reading your question, I think you're actually introducing the 'no crossing paths' as a constraint. To put it another way using the jargon: the path must not be self-intersecting. If that's right, then I think this question in GIS Stack Exchange will help you. It uses shapely, a very useful tool for 2D geometric questions. From the first answer:  [Check out] .is_simple  Returns True if the feature does not cross itself.   from shapely.wkt import loads l = loads('LINESTRING (9603380.577551289 2719693.31939431, 9602238.01822002 2719133.882441244, 9601011.900844947 2718804.012436028, 9599670.800095448 2718931.680117098, 9599567.204161201 2717889.384686942, 9600852.184025297 2721120.409265322, 9599710.80929024 2720511.270897166, 9602777.832940497 2718125.875545334)') print(l.is_simple) # False  If you're looking to solve the problem from scratch then this answer to a similar question, but in a different framework, has some interesting leads, especially the Bentley–Ottmann algorithm, which might be useful.
Question: I would like to calculate the Spherical Harmonics with Julia. I have done this with the following code: using GSL function radius(x, y, z) return sqrt(x^2 + y^2 + z^2) end function theta(x, y, z) return acos(z / radius(x, y, z)) end function phi(x, y, z) return atan(y, x) end function harmonics(l, m, x, y, z) return (-1)^(m) * GSL.sf_legendre_sphPlm(l, m, cos(theta(x,y,z)))*ℯ^(im*m*phi(x,y,z)) end harmonics(1, 1, 11.66, -35, -35) harmonics(1, 1, -35, -35, -35)  The output is the following: 0.07921888327321648 - 0.23779253126608726im -0.1994711402007164 - 0.19947114020071643im  But doing the same with the following python code: import scipy.special as spe import numpy as np def radius(x, y, z): return np.sqrt(x**2 + y**2 + z**2) def theta(x, y, z): return np.arccos(z / radius(x, y, z)) def phi(x, y, z): return np.arctan(y / x) def harmonics(l, m, x, y, z): return spe.sph_harm(m, l, phi(x, y, z), theta(x, y, z)) harmonics(1, 1, 11.66, -35, -35) harmonics(1, 1, -35, -35, -35)  Results in the following output: (-0.07921888327321645+0.23779253126608718j) (-0.19947114020071638-0.19947114020071635j)  So the sign of the first result is different. But since only one of the results has a different sign, the cause cannot be in the prefactor (-1)^m. I can't see through this anymore and can't explain why the results are different.

Answer: The comment by @Oscar Smith got me started on the solution. Julia uses a different convention for the angles returned by atan, provided two arguments are passed [Julia, Numpy]. If we use atan(y / x) instead of atan(y, x) in Julia we get the same result.
Question: I am trying to send an email using FastAPI-mail, and even though I am successfully sending it, when I open the email in Gmail or Outlook, the content (message) appears twice. I am looking at the code but I don't think I am attaching the message twice (also note that the top message always shows the tags, while the second doesn't (see below image). Any help will be appreciated!  main.py from fastapi import FastAPI from fastapi_mail import FastMail, MessageSchema, ConnectionConfig from starlette.requests import Request from starlette.responses import JSONResponse from pydantic import EmailStr, BaseModel from typing import List app = FastAPI() class EmailSchema(BaseModel): email: List[EmailStr] conf = ConnectionConfig( MAIL_USERNAME='myGmailAddress', MAIL_PASSWORD="myPassword", MAIL_FROM='myGmailAddress', MAIL_PORT=587, MAIL_SERVER="smtp.gmail.com", MAIL_TLS=True, MAIL_SSL=False ) @app.post("/send_mail") async def send_mail(email: EmailSchema): template = """ <html> <body> <p>Hi !!! <br>Thanks for using <b>fastapi mail</b>!!!</p> </body> </html> """ message = MessageSchema( subject="Fastapi-Mail module", recipients=email.dict().get("email"), # List of recipients, as many as you can pass body=template, subtype="html" ) template = """ <p>Hi !!! <br>Thanks for using <b>fastapi mail</b>!!! </p>""" ''' template = """ <p>Hi !!! <br>Thanks for using <b>fastapi mail</b>!!! </p>""" ''' fm = FastMail(conf) await fm.send_message(message) return JSONResponse(status_code=200, content={"message": "email has been sent"}) 

Answer: Instead of body, use the html property. message = MessageSchema( subject="Fastapi-Mail module", recipients=email.dict().get("email"), # List of recipients, as many as you can pass html=template, # <<<<<<<<< here subtype="html" )