eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to package my tool with Hatch and want to include some extra files found in /docs in the below directory tree: this_project │ .gitattributes │ .gitignore │ LICENSE │ MANIFEST.in │ pyproject.toml │ README.md │ ├───docs │ default.primers │ └───ribdif __init__.py __main__.py  I am installing the tool with pip install git+https://github.com/Rob-murphys/ribdif.git but am only getting the expected file inside ribdif despite specifying in the pyproject.toml per https://hatch.pypa.io/latest/config/build/#file-selection: [build-system] requires = ["hatchling"] build-backend = "hatchling.build" [project] name = "ribdif" version = "1.1.2" authors = [ { name="Robert Murphy", email="Robert.murphy@bio.ku.dk" }, ] description = "A program to analyse and correct for the usefulness of amplicon sequences" readme = "README.md" requires-python = ">=3.11" classifiers = [ "Programming Language :: Python :: 3.11", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Operating System :: OS Independent", ] [tool.hatch.build] include = [ "ribdif/*.py", "/docs", ] [project.scripts] ribdif = "ribdif.__main__:main" 

Answer: When installing from github using pip I believe I am populating my site-packages with the content of the produced wheel and given that I need the extra files at run time I need to add to the wheel and not the source distribution. [tool.hatch.build.targets.wheel.force-include] "ribdif" = "ribdif" "docs/default.primers" = "ribdif/default.primers"  This directs the default.primers file to be in the ribdif/ directory ofthe site-packages and thus available at run time!
Question: I had some JSON data containing dot(.) in keys that are written to redis using redis-py like this: r = redis.Redis() r.json().set(_id, "$", {'First.Last': "John.Smith"})  It works if reading the whole JSON data like r.json().get(_id)  but error throws if directly getting the value with a path containing the dot: r.json().get(_id, "First\.Last") # ResponseError  Obviously, the dot is not correctly escaped. Also tried to quote it like "'First.Last'"but doesn't work either. What's the correct way to deal with this special character in a key? The redis instance is a redis-stack-server docker container running on linux. Thank you!

Answer: RedisJSON path supports both dot and bracket notation https://redis.io/docs/stack/json/path/, so you could use this r.json().get(_id, '["First.Last"]')
Question: I try my best to move from a setup.py managed lib to a pure pyproject.toml one. I have the following folder structure: tests └── <files> docs └── <files> sepal_ui └── <files> pyproject.toml  and in my pyproject.toml the following setup for file and packages discovery: [build-system] requires = ["setuptools>=61.2", "wheel"] [tool.setuptools] include-package-data = false [tool.setuptools.packages.find] include = ["sepal_ui*"] exclude = ["docs*", "tests*"]  and in the produce wheel, I get the following: tests └── <files> docs └── <files> sepal_ui └── <files> sepal_ui.egg-info └── top-level.txt  looking at the top-level.txt, I see that only sepal_ui is included so my question is simple why do the extra "docs" and "tests" folder are still included even if they are not used? how to get rid of them ? PS: I'm aware of the MANIFEST.in solution that I will accept if it's really the only one but I found it redundant to specify in 2 files.

Answer: Fun fact, it was working from the start.... Small debugging workflow for the next person that does not want to spend hours for nothing. configuration of the pyproject.toml the following configuration is the minimal to remove files from a docs/ and tests/ folders that are at the root of the repository. If you disseminated your tests in each modules, consider adding *.tests*: [build-system] requires = ["setuptools>=61.2", "wheel"] [tool.setuptools] include-package-data = false [tool.setuptools.packages.find] include = ["sepal_ui*"] exclude = ["docs*", "tests*"]  Clean environment That was my mistake. Python is cahing information in the build/ and egg-info folder. setuptools will use the lest of files stored in egg-info/SOURCE.txt so first step: get rid of previous build files. Then simply run: python -m build  check both wheel and tar.gz From the start I was checking only tar.gz thinking (naively) that .whl and .tar.gz were the same. It's not while the tests folder remains in the tar.gz file, it's absent from the wheel.
Question: Python supports Structural Pattern Matching since version 3.10. I came to notice that matching an empty dict doesn't work by simply matching {} as it does for lists. According to my naive approach, non-empty dicts are also matched (Python 3.10.4): def match_empty(m): match m: case []: print("empty list") case {}: print("empty dict") case _: print("not empty")  match_empty([]) # empty list match_empty([1, 2]) # not empty match_empty({}) # empty dict match_empty({'a': 1}) # empty dict  Matching the constructors even breaks the empty list matching: def match_empty(m): match m: case list(): print("empty list") case dict(): print("empty dict") case _: print("not empty")  match_empty([]) # empty list match_empty([1, 2]) # empty list match_empty({}) # empty dict match_empty({'a': 1}) # empty dict  Here is a solution, that works as I expect: def match_empty(m): match m: case []: print("empty list") case d: if isinstance(d, dict) and len(d) == 0: print("empty dict") return print("not empty")  match_empty([]) # empty list match_empty([1, 2]) # not empty match_empty({}) # empty dict match_empty({'a': 1}) # not empty  Now my questions are:  Why do my first 2 approaches not work (as expected)? Is there a way to use structural pattern matching to match only an empty dict (without checking the dict length explicitly)? 

Answer: Using a mapping (dict) as the match pattern works a bit differently than using a sequence (list). You can match the dict's structure by key-value pairs where the key is a literal and the value can be a capture pattern so it is used in the case. You can use **rest within a mapping pattern to capture additional keys in the subject. The main difference with lists is - "extra keys in the subject will be ignored while matching". So when you use {} as a case, what you're really telling Python is "match a dict, no constraints whatsoever", and not "match an empty dict". So one way that might be slightly more elegant than your last attempt is: def match_empty(m): match m: case []: print("empty list") case {**keys}: if keys: print("non-empty dict") else: print("empty dict") case _: print("not empty")   I think the main reason this feels awkward and doesn't work good is because the feature wasn't intended to work with mixed types like this. i.e. you're using the feature as a type-checker first and then as pattern matching. If you knew that m is a dict (and wanted to match its "insides"), this would work much nicer.
Question: using perl, $ perl -e 'print "\xca"' > out  now $ xxd out we have 00000000: ca  But with Python, I tried $ python3 -c 'print("\xca", end="")' > out $ xxd out  what I got is 00000000: c38a  I'm not sure what is going on.

Answer: So in Python, a str object is a series of unicode code points. How this is printed to the screen depends on the encoding of your sys.stdout. This is picked based on your locale (or possibly various environment variables can affect this, but by default, it is your locale). So yours must be set to UTF-8. That's my default too: (py311) Juans-MBP:~ juan$ locale LANG="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_CTYPE="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_ALL= (py311) Juans-MBP:~ juan$ python -c "print('\xca', end='')" | xxd 00000000: c38a  However, if I override my locale and tell it to use en_US.ISO8859-1 (latin-1), a single-byte encoding, we get what you expect: (py311) Juans-MBP:~ juan$ LC_ALL="en_US.ISO8859-1" python -c "print('\xca', end='')" | xxd 00000000: ca  The solution is to work with raw bytes if you want raw bytes. The way to do that in Python source code is to use a bytes literal (or a string literal and then .encode it). We can use the raw buffer at sys.stdout.buffer: (py311) Juans-MBP:~ juan$ python -c "import sys; sys.stdout.buffer.write(b'\xca')" | xxd 00000000: ca  Or by encoding a string to a bytes object: (py311) Juans-MBP:~ juan$ python -c "import sys; sys.stdout.buffer.write('\xca'.encode('latin'))" | xxd 00000000: ca
Question: How can I configure a Docker container to use a custom pip.conf file? This does not (seem to) work for me: from python:3.9 COPY pip.conf ~/.config/pip/pip.conf  where pip.conf is a copy of the pip configuration that points to a proprietary package repository.

Answer: The problem is the ~ expansion. This is a shell feature, and it's not working in a Dockerfile. Just define like this, using the dest path explicitly: from python:3.9 COPY pip.conf /root/.config/pip/pip.conf  If you want to use something other than /root/.config then consider to add a WORKDIR instruction and specify paths relative to that.
Question: I am using Polars in Python to try and add thirty days to a date I run the code, get no errors but also get no new dates Can anyone see my mistake? import polars as pl df = pl.DataFrame( {"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"]}) df = df.with_columns( pl.col("start_date").str.to_date() ) # Generate the days above and below df = df.with_columns( pl.col("start_date") + pl.duration(days=30).alias("date_plus_delta") ) df = df.with_columns( pl.col("start_date") + pl.duration(days=-30).alias("date_minus_delta") ) print(df)  shape: (3, 1) ┌────────────┐ │ start_date │ │ --- │ │ date │ ╞════════════╡ │ 2020-01-02 │ │ 2020-01-03 │ │ 2020-01-04 │ └────────────┘  Quick References The Manual: https://docs.pola.rs/user-guide/transformations/time-series/parsing/ strftime formats: https://docs.rs/chrono/latest/chrono/format/strftime/index.html SO Answer from a previous Post: How to add a duration to datetime in Python polars

Answer: You're supposed to call .alias on the entire operation pl.col('start_date') + pl.duration(days=30). Instead you're only alias-ing on pl.duration(days=30). So the correct way would be: import polars as pl df = pl.DataFrame({"start_date": ["2020-01-02", "2020-01-03", "2020-01-04"]}) df = df.with_columns(pl.col("start_date").str.to_date()) # Generate the days above and below df = df.with_columns((pl.col("start_date") + pl.duration(days=30)).alias("date_plus_delta")) df = df.with_columns((pl.col("start_date") - pl.duration(days=30)).alias("date_minus_delta")) print(df)  Output shape: (3, 3) ┌────────────┬─────────────────┬──────────────────┐ │ start_date ┆ date_plus_delta ┆ date_minus_delta │ │ --- ┆ --- ┆ --- │ │ date ┆ date ┆ date │ ╞════════════╪═════════════════╪══════════════════╡ │ 2020-01-02 ┆ 2020-02-01 ┆ 2019-12-03 │ │ 2020-01-03 ┆ 2020-02-02 ┆ 2019-12-04 │ │ 2020-01-04 ┆ 2020-02-03 ┆ 2019-12-05 │ └────────────┴─────────────────┴──────────────────┘
Question: I am just trying some simple functions in Python with OpenAI APIs but running into an error: I have a valid API secret key which I am using. Code: >>> import os >>> import openai >>> openai.api_key = os.getenv("I have placed the key here") >>> response = openai.Completion.create(model="text-davinci-003", prompt="Say this is a test", temperature=0, max_tokens=7)  

Answer: Option 1: OpenAI API key not set as an environment variable Change this... openai.api_key = os.getenv('sk-xxxxxxxxxxxxxxxxxxxx') ...to this. openai.api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'  Option 2: OpenAI API key set as an environment variable (recommended) There are two ways to set the OpenAI API key as an environment variable:  using an .env file (easier, but don't forget to create a .gitignore file) or using Windows Environment Variables.  Way 1: Using an .env file Change this... openai.api_key = os.getenv('sk-xxxxxxxxxxxxxxxxxxxx') ...to this... openai.api_key = os.getenv('OPENAI_API_KEY') Also, don't forget to use the python-dotenv package. Your final Python file should look as follows: # main.py import os from dotenv import load_dotenv from openai import OpenAI # Load environment variables from the .env file load_dotenv() # Initialize OpenAI client with the API key from environment variables client = OpenAI( api_key=os.getenv("OPENAI_API_KEY"), )  It's crucial that you create a .gitignore file not to push the .env file to your GitHub/GitLab and leak your OpenAI API key! # .gitignore .env  Way 2: Using Windows Environment Variables (source) STEP 1: Open System properties and select Advanced system settings  STEP 2: Select Environment Variables  STEP 3: Select New STEP 4: Add your name/key value pair Variable name: OPENAI_API_KEY Variable value: sk-xxxxxxxxxxxxxxxxxxxx  STEP 5: Restart your computer (IMPORTANT!) Your final Python file should look as follows: # main.py import os from dotenv import load_dotenv from openai import OpenAI # Initialize OpenAI client # It will automatically use your OpenAI API key set via Windows Environment Variables client = OpenAI()
Question: I create a fresh environment, install numpy, then install GDAL. GDAL imports successfully and I can open images using gdal.Open(, but I get the ImportError: cannot import name '_gdal_array' from 'osgeo' error when trying to use ReadAsRaster. pip list returns: GDAL 3.6.2 numpy 1.24.2 pip 23.0 setuptools 65.6.3 wheel 0.38.4  Completely stumped, has anyone come across this? Google tells me that installing numpy first is the solution (but that doesn't help). Help would be much appreciated.

Answer: Pip is most likely caching and reinstalling your bad version of GDAL over and over, even though you installed numpy. Here's what fixed it for me: pip3 install --no-cache-dir --force-reinstall 'GDAL[numpy]==3.6.2' Installing without --no-cache-dir causes pip to reuse the compiled wheel: % pip3 install --force-reinstall 'GDAL[numpy]==3.6.2' Collecting GDAL[numpy]==3.6.2 Using cached GDAL-3.6.2-cp311-cp311-macosx_13_0_x86_64.whl Collecting numpy>1.0.0 Using cached numpy-1.24.2-cp311-cp311-macosx_10_9_x86_64.whl (19.8 MB) Installing collected packages: numpy, GDAL Attempting uninstall: numpy Found existing installation: numpy 1.24.2 Uninstalling numpy-1.24.2: Successfully uninstalled numpy-1.24.2 Attempting uninstall: GDAL Found existing installation: GDAL 3.6.2 Uninstalling GDAL-3.6.2: Successfully uninstalled GDAL-3.6.2 Successfully installed GDAL-3.6.2 numpy-1.24.2   For others still encountering this issue, make sure wheel and setuptools are installed. Pip will think setuptools>=48 is enough, but it's wrong, as you'll need at least setuptools 67.` Pip will download wheel and/or setuptools as part of the building process, but it will not work (as of March 2024) and instead blame numpy: Building wheels for collected packages: GDAL Running command Building wheel for GDAL (pyproject.toml) WARNING: numpy not available! Array support will not be enabled  The only indication that it's a wheel or setuptools problem shows up when building without build isolation (--no-build-isolation):  error: invalid command 'bdist_wheel' error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> See above for output.  Preparing metadata (pyproject.toml) ... done ERROR: Exception: Traceback (most recent call last): ... ModuleNotFoundError: No module named 'setuptools'  To fix these errors, make sure wheel and setuptools are installed with pip3 install wheel 'setuptools>=67'
Question: I want to integrate OpenCV with YOLOv8 from ultralytics, so I want to obtain the bounding box coordinates from the model prediction. How do I do this? from ultralytics import YOLO import cv2 model = YOLO('yolov8n.pt') cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) while True: _, frame = cap.read() img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = model.predict(img) for r in results: for c in r.boxes.cls: print(model.names[int(c)]) cv2.imshow('YOLO V8 Detection', frame) if cv2.waitKey(1) & 0xFF == ord(' '): break cap.release() cv2.destroyAllWindows()  I want to display the YOLO annotated image in OpenCV. I know I can use the stream parameter in model.predict(source='0', show=True). But I want to continuously monitor the predicted class names for my program, at the same time displaying the image output.

Answer: This will:  Loop through each frame in the video Pass each frame to Yolov8 which will generate bounding boxes Draw the bounding boxes on the frame using the built in ultralytics' annotator:   from ultralytics import YOLO import cv2 from ultralytics.utils.plotting import Annotator # ultralytics.yolo.utils.plotting is deprecated model = YOLO('yolov8n.pt') cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480) while True: _, img = cap.read() # BGR to RGB conversion is performed under the hood # see: https://github.com/ultralytics/ultralytics/issues/2575 results = model.predict(img) for r in results: annotator = Annotator(img) boxes = r.boxes for box in boxes: b = box.xyxy[0] # get box coordinates in (left, top, right, bottom) format c = box.cls annotator.box_label(b, model.names[int(c)]) img = annotator.result() cv2.imshow('YOLO V8 Detection', img) if cv2.waitKey(1) & 0xFF == ord(' '): break cap.release() cv2.destroyAllWindows()
Question: I want to create BigInteger Identity column in SQLAlchemy ORM. Documentation does not have any example of either ORM Identity or BigInteger Identity.  Is this possible at all? I don't see any parameter for Identity type that would allow specifying inner integer type How to do this? Do I have to create custom type and pass it inside Mapping[] brackets? 

Answer: This seems to work: import sqlalchemy as sa from sqlalchemy.orm import mapped_column, Mapped, DeclarativeBase class Base(DeclarativeBase): pass class Test(Base): __tablename__ = 't75312537' id: Mapped[int] = mapped_column( sa.BigInteger, sa.Identity(), primary_key=True ) engine = sa.create_engine('postgresql+psycopg2:///test', echo=True) Base.metadata.drop_all(engine, checkfirst=True) Base.metadata.create_all(engine)  Output: CREATE TABLE t75312537 ( id BIGINT GENERATED BY DEFAULT AS IDENTITY, PRIMARY KEY (id) )  See the docs at Identity Columns (GENERATED {ALWAYS | BY DEFAULT} AS IDENTITY).
Question: Error:  I have already installed numpy module(pip show numpy):  this how it shows when i try to install numpy again  I tried to import numpy module which is already installed but it throws ModuleNotFoundError

Answer: if you are using vs code you need to select the Python interpreter explicitly:  press ctrl+shift+p to open the editor command  then Search Python: Select Interpreter then you can choose the appropriate Python interpreter
Question: I have a tests in my Django app. They're working well, but i want to disable showing console logging like .Bad Request: /api/v1/users/register/ One of my tests code  def test_user_register_username_error(self): data = { 'username': 'us', 'email': 'mail@mail.mail', 'password': 'pass123123', 'password_again': 'pass123123' } url = self.register_url response = client.post(url, data=data) self.assertEqual(response.status_code, 400) self.assertFalse(User.objects.filter(username='us').first())  Console output Found 1 test(s). Creating test database for alias 'default'... System check identified no issues (0 silenced). .Bad Request: /api/v1/users/register/ ---------------------------------------------------------------------- Ran 1 tests in 0.414s OK  Everything works nice, but i want to disable Bad Request: /api/v1/users/register/ output to console. I double checked, there's no print or logging functions, that can possibly log this to console. How can i disable messages like Bad Request: /api/v1/users/register/ logging while tests EDIT To make question more understandable here's current console output: Found 22 test(s). Creating test database for alias 'default'... System check identified no issues (0 silenced). ...........Bad Request: /api/v1/users/register/ .Bad Request: /api/v1/users/register/ .Bad Request: /api/v1/users/register/ .Bad Request: /api/v1/users/register/ .Bad Request: /api/v1/users/register/ ..Bad Request: /api/v1/users/register/ .Bad Request: /api/v1/users/activate/ .Bad Request: /api/v1/users/login/ .Bad Request: /api/v1/users/recovery/ Bad Request: /api/v1/users/recovery/ .Bad Request: /api/v1/users/register/ . ---------------------------------------------------------------------- Ran 22 tests in 6.350s OK  And what i expect: Found 22 test(s). Creating test database for alias 'default'... System check identified no issues (0 silenced). ...................... ---------------------------------------------------------------------- Ran 22 tests in 6.350s OK 

Answer: You can log only errors during the tests, and after they complete, return the normal logging level. class SampleTestCase(TestCase): def setUp(self) -> None: """Reduce the log level to avoid messages like 'bad request'""" logger = logging.getLogger("django.request") self.previous_level = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) def tearDown(self) -> None: """Reset the log level back to normal""" logger = logging.getLogger("django.request") logger.setLevel(self.previous_level)
Question: I want to know if there is any way to download the output dataframe of streamlit as an Excel file using the streamlit button?

Answer: I suggest you edit your question to include a minimal reproducible example so that it's easier for people to understand your question and to help you. Here is the answer if I understand you correctly. Basically it provides 2 ways to download your data df as either csv or xlsx. IMPORTANT: You need to install xlsxwriter package to make this work. import streamlit as st import pandas as pd import io # buffer to use for excel writer buffer = io.BytesIO() data = { "calories": [420, 380, 390], "duration": [50, 40, 45], "random1": [5, 12, 1], "random2": [230, 23, 1] } df = pd.DataFrame(data) @st.cache def convert_to_csv(df): # IMPORTANT: Cache the conversion to prevent computation on every rerun return df.to_csv(index=False).encode('utf-8') csv = convert_to_csv(df) # display the dataframe on streamlit app st.write(df) # download button 1 to download dataframe as csv download1 = st.download_button( label="Download data as CSV", data=csv, file_name='large_df.csv', mime='text/csv' ) # download button 2 to download dataframe as xlsx with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer: # Write each dataframe to a different worksheet. df.to_excel(writer, sheet_name='Sheet1', index=False) download2 = st.download_button( label="Download data as Excel", data=buffer, file_name='large_df.xlsx', mime='application/vnd.ms-excel' )
Question: I'm attempting to run a very simple tqdm script: from tqdm.notebook import tqdm for i in tqdm(range(10)): time.sleep(1)  but am met with: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html  My ipywidgets is v8.0.4 and jupyter v1.0.0... does tqdm not work anymore with VS Code Jupyter Notebook?

Answer: I had the same problem. Using this post I resolved it by running: %pip install --upgrade jupyter ipywidgets %jupyter nbextension enable --py widgetsnbextension # removed !pip on the recommendation of a comment. import time from tqdm.notebook import tqdm for i in tqdm(range(10)): time.sleep(0.1)