eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: How can I cache requests in FastAPI? For example, there are two functions and a PostgreSQL database: @app.get("/") def home(request: Request): return templates.TemplateResponse("index.html", {"request": request}) @app.post("/api/getData") async def getData(request: Request, databody = Body()): data = databody["data"] with connection.cursor() as cursor: cursor.execute( f"INSERT INTO database (ip, useragent, datetime) VALUES ('request.headers['host']', 'request.headers['user-agent']', '{datetime.now()}'") ) return {"req": request}  Then the request is processed by JavaScript and displayed on the HTML page .

Answer: You can try fastapi-cache: from fastapi import FastAPI from starlette.requests import Request from starlette.responses import Response from fastapi_cache import FastAPICache from fastapi_cache.backends.redis import RedisBackend from fastapi_cache.decorator import cache from redis import asyncio as aioredis app = FastAPI() @cache() async def get_cache(): return 1 @app.get("/") @cache(expire=60) async def index(): return dict(hello="world") @app.on_event("startup") async def startup(): redis = aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True) FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
Question: Using Artifactory (https://cloud.google.com/artifact-registry) I intend to add a dependency with poetry (https://python-poetry.org/docs/repositories/). I can install with command: pip install --index-url https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/ <PACKAGE_NAME> (auth using keyrings.google-artifactregistry-auth) I intend to use POETRY to manage my dependencies. I don't know if using poetry adds this dependence. With poetry add --source <PACKAGE_NAME> https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO> I'm getting this error: poetry update Updating dependencies Resolving dependencies... (0.9s) RepositoryError 401 Client Error: Unauthorized for url: https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/pytest/  My pyproject.toml ... [[tool.poetry.source]] name = "SOME_LIB" url = " https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/" secondary = true  Here there is details how to config with PIP/VIRTUALENV: https://cloud.google.com/artifact-registry/docs/python/authentication but not have details about Poetry. Do you have any tips about it?

Answer: Your RepositoryError of  401 Client Error: Unauthorized for url: https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/pytest/  clearly indicates that you lack authorization for the URL mentioned. So you will need to make sure you properly get and use the authorization, that is, you will have a user with credentials who actually has the right to access it. Once you have that, you will need to authenticate, so the server you are sending the request to will be able to determine who you are and therefore to grant you proper access. Read: https://python-poetry.org/docs/repositories/#configuring-credentials The page provides the poetry config http-basic.foo <username> <password>  example, explaining that you can set up some credentials for a specific repository. You can specify only the username, but then whenever you try to access the URL, you will be prompted for the password. You can also publish by adding --username and --password to the command. You can also specify environment variables, like export POETRY_PYPI_TOKEN_PYPI=my-token export POETRY_HTTP_BASIC_PYPI_USERNAME=<username> export POETRY_HTTP_BASIC_PYPI_PASSWORD=<password>  See https://python-poetry.org/docs/configuration/#using-environment-variables Anyways, the error tells you that you were not properly authenticated via the credentials that are needed, so either you do not have credentials, they are incorrect or you lack privilege.
Question: I have a GraphQL schema defined from server and I'd like to write a nice Python GraphQL client for it. I'm looking for a way to transform my GraphQL schema into python classes with type hints such that I'll be able to see all available queries, mutations, their fields(names & types) and return vals. I cannot write manually all python classes due to schema complexity, I have many filters on each field. see this example from ent on TodoWhereInput to understand how error prune this will be. I really enjoy using GraphQL playground with auto completion, I want that experience in my python client. For example, given this schema as an input: type Book { title: String year: Int } type Author { name: String books: [Book] }  I'd like to generate this python code as an output: from dataclasses import dataclass @dataclass class Book: title: str year: int @dataclass class Author: name: str books: list[Book]  same for Inputs in schema. I already looked at:  codegen which is awesome for typescript! but doesn't have python support :/  gql_schema_codegen nice, but generating TypedDict which isn't dataclasses, I have to change each dict and pass total=False so it won't required all fields by default.  sgqlc code-generator which doesn't allow type hints. writing queries is still dynamically and error prune.  

Answer: Thanks for all the answers, but after revisiting this issue I found out Ariadne is the best solution for my case. Generating async / sync clients based on GraphQL schema + queries, highly configurable. You are welcome to try them yourself: https://github.com/mirumee/ariadne-codegen
Question: In looking at the guide What do blueprints offer that just importing doesn't? Here are some points that are unclear:  It says to have a file called http_blueprint.py in which you'd define some routes but it just looks like the regular http trigger but the decorator is a bp.route instead of an app.route. Are these also app.functions since the main file has 2 decorators per def?  Does everything in the blueprint have to be an http trigger or is that just an example that they used?  Can you have multiple blueprint files or are we limited to the single one?  

Answer: Example of repo structure. project │ file001.py │ file002.py │ function_app.py │ README.md │ host.json │ local.settings.json  file001.py: import azure.function as func import json bp01 = func.Blueprint() @bp01.route(route="route01") def method01(req:func.HttpRequest) -> func.HttpRequest: return func.HttpResponse ( json.dumps({ 'version': 1 }) )  file002.py: import azure.function as func import json bp02 = func.Blueprint() @bp02.route(route="route02") def method02(req:func.HttpRequest) -> func.HttpRequest: return func.HttpResponse ( json.dumps({ 'version': 2 }) )  This is how you register more than one blueprint in function_app.py: import azure.functions as func from file001.py import bp01 from file002.py import bp02 app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) app.register_functions(bp01) app.register_functions(bp01)  My guess blueprints are useful when you want separation of concerns but I don't know for certain. The most confusing part to me was that the main methods from the two blueprints must have a unique name otherwise the azure function will get confused and not show neither.
Question: Running a python script on VS outputs this error. How to pass -Xfrozen_modules=off to python to disable frozen modules?  I was trying to update the python version from 3.6 to 3.11 and then started seeing this message.

Answer: If you are using VS Code you can add "pythonArgs": ["-Xfrozen_modules=off"] to your debug configuration in launch.json, like this: { // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python (Xfrozen=off)", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true, "pythonArgs": ["-Xfrozen_modules=off"] } ] }  There is some additional context in the GitHub Issue fabioz/PyDev.Debugger/issues/213.
Question: Project tree /backend .env.dev docker-compose-dev.yml /project  I have the following warning: docker-compose -f docker-compose-dev.yml up --build # i am in the /backend directory WARNING: The DB_USER variable is not set. Defaulting to a blank string. WARNING: The DB_PASSWORD variable is not set. Defaulting to a blank string. WARNING: The DB_NAME variable is not set. Defaulting to a blank string.  It means that docker compose don't see my environment file. How can I fix it? docker-compose-dev.yml services: django: build: ./project # path to Dockerfile command: sh -c " sleep 3 && gunicorn -w 8 --bind 0.0.0.0:8800 core_app.wsgi" volumes: - ./project:/project - ./project/static:/project/static - ./project/media:/project/media - ./project/logs:/project/logs expose: - 8800 env_file: - ./.env.dev depends_on: - db db: image: postgres:13-alpine volumes: - pg_data:/var/lib/postgresql/data/ expose: - 5432 ports: - "5435:5432" env_file: - ./.env.dev environment: - POSTGRES_USER=${DB_USER} - POSTGRES_PASSWORD=${DB_PASSWORD} - POSTGRES_DB=${DB_NAME} .......  How can I enable env file? P.S Values are set in the file .env.dev DB_NAME=db_dev DB_USER=postgres DB_PASSWORD=passsss132132 ENV_TYPE=DEV  UPDATE Found out that this way is working docker-compose -f docker-compose-dev.yml --env-file=.env.dev up 

Answer: If the complaint is coming from docker (compose) itself, try to:  rename ./.env.dev to simply .env.dev; if not yet, rename .env.dev to .env (default) and remove the env_file entry from your compose. That will certainly work, then you can go back to investigate the issue with env_file ([1]).  Update Now facing the same problem myself -- in a similar situation trying to separate the variables of my JupyterHub from that destined to the Jupyter Notebooks --, I digged in a bit more to understand better the role of Compose's .env and env_file:. As already informed in the question, the use of option --env-file solves the issue. Which is the right answer for defining docker-compose's (yaml) environment variables in a file named differently from .env (default). The env_file option in docker-compose.yaml is meant for the container being run: only the container sees those variables.  I recently posted a similar answer to a similar question :  https://stackoverflow.com/a/75538969/687896   [1] https://docs.docker.com/compose/env-file/
Question: I have a model with some fields with a verbose_name. This verbose name is suitable for the admin edit page, but definitively too long for the list page. How to set the label to be used in the list_display admin page?

Answer: You can create custom columns. For example, there is Person model below: # "models.py" from django.db import models class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField()  Now, you can create the custom columns "my_name" and "my_age" with my_name() and my_age() and can rename them with @admin.display as shown below: @admin.register(Person) class PersonAdmin(admin.ModelAdmin): list_display = ("my_name", "my_age") # "my_name" and "my_age" need to be assigned @admin.display(description='My name') def my_name(self, obj): # ↑ Displayed return obj.name @admin.display(description='My age') def my_age(self, obj): # ↑ Displayed return obj.age  Then, MY NAME, MY AGE and the values of "name" and "age" fields are displayed as shown below:  Of course, you can assign "name" and "age" fields to list_display in addition to the custom columns "my_name" and "my_age" as shown below: @admin.register(Person) class PersonAdmin(admin.ModelAdmin): list_display = ("my_name", "my_age", "name", "age") # ↑ Here ↑ @admin.display(description='My name') def my_name(self, obj): return obj.name @admin.display(description='My age') def my_age(self, obj): return obj.age  Then, NAME, AGE and the values of "name" and "age" fields are displayed as shown below:
Question: Back when Python3 was there, I used to use: #!/usr/bin/env python3  But recently, especially with Ubuntu 22.04 or macOS, the python3 executable isn't always available in PATH, instead, I should use python to call python3. Is there any portable way to write Python3 shebang?

Answer: According to the Python docs, using #!/usr/bin/env python3 is still the recommended way -- but they admit that it does not always work:  2.4. Miscellaneous To easily use Python scripts on Unix, you need to make them executable, e.g. with chmod +x script and put an appropriate Shebang line at the top of the script. A good choice is usually #!/usr/bin/env python3 which searches for the Python interpreter in the whole PATH. However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python3 as the interpreter path.  The alternative is then to hardocode the python binary, i.e. /usr/bin/python3 (or python in your case).
Question: As you can see below, after I installed Python 3.11, I came to the realization that running pip3.10 freeze did not list me the packages I had in my Python 3.10.2 but those of my Python 3.11. This is explained by the fact that in Python311\Scripts I have both pip3.10.exe and pip3.11.exe. Is there a reason? When I want to pip install or do pip freeze with pip3.10 I need to use the absolute path now. 

Answer: This was a pip bug, I don't understand the details exactly but when pip tried to match to the correct Python version it was only accounting for single-digit version numbers, resulting in this weird behavior. Perhaps it is better explained in this thread on github. To summarize that thread (from user uranusjr):  pip contains logic to specially fix pipX.Y (and easy_install-X.Y) entry points to correctly match the Python version, but that logic only accounts for single-digit version numbers and doesn’t work with 3.10 upwards.  This was fixed with pip version 22.3.1 (Also seen on this github thread), and from what I can tell doesn't occur anymore when using python 3.11.1.
Question: I am trying to extract only the core text from a "rich" pdf document, meaning that it has a lot of tables, graphs, boxes, footers etc. in which I am not interested in. I tried with some common python packages like PyPDF2, pdfplumber or pdfreader.The problem is that apparently they extract all the text present in the pdf, including those parts listed above in which I am not interested. As an example: from PyPDF2 import PdfReader file = PdfReader(file) page = file.pages[10] text = page.extract_text()  This code will get me the whole text from page 11, including footers, box, text from a table and the number of the page, while what I would like is only the core text. Unluckily the only solution I found up to now is to copy paste in another file the core text. Is there any method/package which can automatically recognize the main text from the other parts of the pdf and return me only that? Thank you for your help!!!

Answer: per D.L's comment, please add some reproducible code and, preferably, a pdf to work with. However, I think I can answer at least part of your question. jsvine's pdfplumber is an incredibly robust python pdf processing package. pdfplumber contains a bounding box functionality that lets you extract text from within (.within_bbox(...)) or from outside (.outside_bbox) the 'bounding box' -- or geographical area -- delineated on the Page object. Every character object extracted from the page contains location information such as y1 - Distance of top of character from bottom of page and Distance of left side of character from left side of page. If the majority of pages within the .pdf you are trying to extract text from contain footnotes, I would recommend only extracting text above the y1 value. Given that footnotes are typically well below the end of a page, except for academic papers using Chicago Style citations, you should still be able to set a standard .bbox for where you want to extract text (within a set .bbox that does not include footnotes or out of a set .bbox that does not include footnotes). To your question about tables, that poses a trickier question. Tables are by far the trickiest thing to detect and/or extract from. pdfplumber offers, to my knowledge, the most robust open source table detection/extraction capabilities out there. To extract the area outside a table, I would call the .find_tables(...) function on each Page object to return a .bbox of the table and extract around that. However -- this is not perfect. It is not always able to detect tables. Regarding your 3rd question, how to exclude boxes, are you referring to text boxes? Please provide further clarification! Finally -- to reiterate my first point -- pdfplumber is an incredibly robust package. That being said, extracting text from .pdf files is really tough. Good luck -- please provide more information and I will be happy to help as best I can.
Question: While trying to run the corr() method in python using pandas module, I get the following error: FutureWarning: The default value of numeric_only in DataFrame.corr is deprecated. In a future version, it will default to False. Select only valid columns or specify the value of numeric_only to silence this warning. print(df.corr())  Note (Just for clarification) :- df is the name of the dataframe read from a csvfile. For eg:- import pandas as pd df = pd.read_csv('Data.csv') print(df.corr())  The problem only lies in the corr() method which raises the aforementioned error: FutureWarning: The default value of numeric_only in DataFrame.corr is deprecated. In a future version, it will default to False. Select only valid columns or specify the value of numeric_only to silence this warning.  I partially understand the error, however I would like to know:  Are there any other alternative methods to do the same function of corr() to identify the relationship between each column in a data set? Like is there a way to replicate the function without using corr() method?  Sorry If my question is wrong or improper in anyway, I'm open to feedbacks. Thanks in advance.

Answer: The problem only lies in the one function corr() which is not deprecated but its numeric_only Argument in the function is. So, you can set it to false or true according to needs by df.corr(numeric_only = *[True/False]*). You can learn more at its documentation. p.s - I wrote so that it is more understandable and the fact that this problem was well known to me.
Question: I have this MRE: . └── tests └── notest.py  The notest.py just do a sys.exit(1): When I run pytest --doctest-modules I get this error: ERROR collecting tests/notest.py tests/notest.py:4: in <module> sys.exit(1) E SystemExit: 1  So the --doctest-modules would try to execute my script which is not a test. Is that normal behaviour and how to prevent that?

Answer:  Is that normal behaviour?  Yes. Passing --doctest-modules will activate a special doctest collector that is not restricted to globs specified by python_files (test_*.py and *_test.py by default). Instead, it will find and collect any python module that is not __init__.py or __main__.py. Afterwards, doctest will import them to collect docstrings and execute doctests from every module.  how to prevent that?  If you have doctests in notest you want to run, you have to prevent code execution on notest module import. Place the code that causes premature exit into if __name__ == '__main__' block: # notest.py import sys if __name__ == '__main__': sys.exit(1)  Now the regular import (e.g. python -c 'import tests.notest') will exit with 0 as the main block is not executed, while running the module (e.g. python tests/notest.py or python -m tests.notest) will still exit with 1.  If you have no tests in the notest module or you can't edit its code, you can instruct pytest to ignore the module completely: $ pytest --doctest-modules --ignore=tests/notest.py  or persist the option in pyproject.toml or pytest.ini to avoid entering the option every time. Example with pyproject.toml: [tool.pytest.ini_options] addopts = "--ignore=tests/notest.py"  Alternatively, this can also be done in a conftest module. Example: # tests/conftest.py collect_ignore = ["notest.py"]  Beware that collect_ignore receives files relative to the current conftest module. Therefore, if you use a conftest in the project root directory (on the same level as tests directory), you would have to specify the correct relative path: # conftest.py collect_ignore = ["tests/notest.py"]
Question: So I have just installed python version 3.11 and changed my python interpreter in pycharm and the code runs properly now after I reinstalled the packages to my new venv. but when i debug my code I keep getting a long list of warnings and I have no idea how to fix it: ------------------------------------------------------------------------------- pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled (internal generated filenames are not absolute) pydev debugger: The debugger may still function, but it will work slower and may miss breakpoints. pydev debugger: Related bug: http://bugs.python.org/issue1666807 ------------------------------------------------------------------------------- Connected to pydev debugger (build 221.5080.212) pydev debugger: Unable to find real location for: <frozen codecs> pydev debugger: Unable to find real location for: <frozen importlib._bootstrap> pydev debugger: Unable to find real location for: <frozen importlib._bootstrap_external> pydev debugger: Unable to find real location for: <frozen zipimport> pydev debugger: Unable to find real location for: <frozen ntpath> pydev debugger: Unable to find real location for: <frozen genericpath> pydev debugger: Unable to find real location for: <frozen os> pydev debugger: Unable to find real location for: <frozen _collections_abc> pydev debugger: Unable to find real location for: <string> pydev debugger: Unable to find real location for: <frozen abc> pydev debugger: Unable to find real location for: <__array_function__ internals> pydev debugger: Unable to find real location for: <frozen io>  I installed it for all users to C:\Program Files if that helps

Answer: Should be fixed in PyCharm 2022.3 (ticket https://youtrack.jetbrains.com/issue/PY-56939/CRITICAL-WARNING-error-debugging-Python-311-code). Early Access Preview version is already available https://www.jetbrains.com/pycharm/nextversion/
Question: I have this following class: class Quiz(BaseModel): question: str subject: str choice: str = Query(choices=('eu', 'us', 'cn', 'ru'))  I can render the form bases on this class like this @api.post("/postdata") def post_data(form_data: Quiz = Depends()): return form_data  How can I display a drop down list for choice field ?

Answer: Option 1 Use literal values. Literal type is a new feature of the Python standard library as of Python 3.8 (prior to Python 3.8, it requires the typing-extensions package) and is supported by Pydantic. Example: from fastapi import FastAPI, Depends from pydantic import BaseModel from typing import Literal app = FastAPI() class Quiz(BaseModel): question: str subject: str choice: Literal['eu', 'us', 'cn', 'ru'] = 'us' @app.post('/submit') def post_data(data: Quiz = Depends()): return data  Option 2 Use Enums (also, see Python's enum module, as well as FastAPI's documentation on Predefined values). By having your Enum sub-class inheriting from str, the API docs will be able to know that the values must be of type string and will be able to render correctly. Example: from fastapi import FastAPI, Depends from pydantic import BaseModel from enum import Enum app = FastAPI() class Country(str, Enum): eu = 'eu' us = 'us' cn = 'cn' ru = 'ru' class Quiz(BaseModel): question: str subject: str choice: Country = Country.us @app.post('/submit') def post_data(data: Quiz = Depends()): return data