eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I wrote a OpenCV python script to identify the contour of an object in a microscope image. Selected contour image example:  I need to meassure the width of the object at N intervals, where width is the distance between the right side to the left side of the contour in that interval, the line meassured is always horisontal. this will later be repeated on about 30000 images. Thsi is my latest attempt but it obviously not working # Find contours in the inverse binary image contours, _ = cv2.findContours(255 - binary_opened, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Choose the largest contour (assumes the blob is the largest connected component) largest_contour = max(contours, key=cv2.contourArea) # Divide the contour into 10 equal horizontal intervals num_intervals = 10 interval_height = len(largest_contour) // num_intervals # Create evenly spaced intervals along the y-axis intervals_y = np.linspace(0, len(largest_contour), num_intervals + 1, dtype=int) # Draw and display each interval on the original image for i in range(num_intervals): start_idx = intervals_y[i] end_idx = intervals_y[i + 1] # Get points in the interval interval_points = largest_contour[start_idx:end_idx] # Draw the contour of the interval on the original image cv2.drawContours(image, [interval_points], -1, (0, 0, 255), 2)  I am new to openCV so any tip would be appreciated

Answer: I had a little attempt at this as follows: #!/usr/bin/env python3 import cv2 as cv import numpy as np # Load image and extract width and height im = cv.imread('osjKP.jpg') h, w = im.shape[:2] # Convert to HSV to find all the green pixels HSV = cv.cvtColor(im, cv.COLOR_BGR2HSV) lo = np.array([50,200, 100]) # lower limit for green in HSV space hi = np.array([70,255, 255]) # upper limit for green in HSV space greens = cv.inRange(HSV, lo, hi) cv.imwrite('DEBUG-greens.png', greens)   # Floodfill with white from the centre _, result, _, _ = cv.floodFill(greens, None, (int(w/2), int(h/2)), 255) cv.imwrite('DEBUG-floodfilled.png', result)   # Count the non-zero pixels in each row, i.e. widths[0] is the width of the top row widths = np.count_nonzero(result, axis=1) print(widths)   My assumption that it is safe to flood fill from the centre may not be correct for your other images (e.g. if the item you are measuring is narrow) so you may want to fill from the top-left and top-right corner and then invert, for example.  If you only want the widths of every 20th row, change the last couple of lines to: # Count the non-zero pixels in each row N = 20 widths = np.count_nonzero(result[::N,:], axis=1) print(widths)  Output [ 918 902 907 899 896 904 911 894 912 913 897 884 912 916 895 910 892 878 869 871 888 881 870 846 856 860 866 896 876 873 878 880 881 889 892 898 900 917 931 954 966 976 982 990 992 1030]
Question: I need to communicate with other services in my Python and FastAPI application, therefore I use the httpx library to be able to communicate asynchronously. So, I have the following code for POST requests: from typing import Any, Dict, Optional, Tuple from fastapi import File from httpx._client import AsyncClient async def post( *, url: str, files: Optional[Dict[str, File]] = None, json: Optional[Dict[str, Any]] = None, data: Optional[Dict[str, str]] = None, params: Optional[Dict[str, str]] = None, timeout: int = 10000 ) -> Tuple[bool, Any]: try: async with AsyncClient() as client: response = await client.post(url, files=files, json=json, data=data, timeout=timeout) response = response.json() if response.status_code == 200 else None if not response: return False, None return True, response except Exception as e: print(e) return False, None  I would like to implement a retry policy so that if a request fails, it is retried, for example, up to 3 times. Is this possible and makes sense with httpx and async? I was looking at some tutorials on the internet but they seem to be outdated since the information they contain does not work Update: I tried the following approach with HTTPTransport but it didn't work for me: from httpx import HTTPTransport # here try: async with AsyncClient(transport=transport) as client: # here response = await client.post(url, files=files, json=json, data=data, timeout=timeout) response = response.json() if response.status_code == 200 else None if not response: return False, None return True, response except Exception as e: print(e) return False, None transport = HTTPTransport(retries=3)  I get: 'HTTPTransport' object has no attribute 'aenter'

Answer: With the Async HTTPTransport class of httpx you can configure retry: from typing import Any, Dict, Optional, Tuple from fastapi import File from httpx import AsyncHTTPTransport from httpx._client import AsyncClient transport = AsyncHTTPTransport(retries=3) async def post( *, url: str, files: Optional[Dict[str, File]] = None, json: Optional[Dict[str, Any]] = None, data: Optional[Dict[str, str]] = None, params: Optional[Dict[str, str]] = None, timeout: int = 100 ) -> Tuple[bool, Any]: if params: parameters = [key + "=" + parameter for key, parameter in params.items()] parameters = "&".join(parameters) url += "?" + parameters try: async with AsyncClient(transport=transport) as client: response = await client.post(url, files=files, json=json, data=data, timeout=timeout) response = response.json() if response.status_code == 200 else None if not response: return False, None return True, response except Exception as e: print(e) return False, None  I tried the above and it worked!
Question: I am new to polars library and the title says it all regarding what I am trying to do. Doing this with the pandas library I would use apply() and the build in eval() function of Python. since eval("[1,2,3]") returns [1,2,3]. This can be done in polars as well - below I have an expected output example - but polars strongly recommends to use its Expression API. I searched the Expr.str attribute but didn't find an expression that does this. Am I missing something or should go with apply()? data = {'col_string': ['[1,2,3]', '[4,5,6]']} df = pl.DataFrame(data) df = df.with_columns(pl.col('col_string').map_elements(eval).alias('col_list')) shape: (2, 2) ┌────────────┬───────────┐ │ col_string ┆ col_list │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞════════════╪═══════════╡ │ [1,2,3] ┆ [1, 2, 3] │ │ [4,5,6] ┆ [4, 5, 6] │ └────────────┴───────────┘ 

Answer: As long as your string column is valid JSON, you could use polars.Expr.str.json_decode as follows. df.with_columns( pl.col("col_string").str.json_decode().alias("col_list") )  Output. shape: (2, 2) ┌────────────┬───────────┐ │ col_string ┆ col_list │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞════════════╪═══════════╡ │ [1,2,3] ┆ [1, 2, 3] │ │ [4,5,6] ┆ [4, 5, 6] │ └────────────┴───────────┘
Question: The following code snippet shows a function named Literal being invoked with square brackets: Literal['r', 'rb', 'w', 'wb']  The example is from the CPython GitHub repo Though we can use square brackets to call instances by using __getitem__ method in Python classes, it doesn't seem to be the case here.

Answer: In the source code there are these decorators @_TypedCacheSpecialForm # this is a class @_tp_cache(typed=True) # not relevant here def Literal(self, *parameters): ...  _TypedCacheSpecialForm is a class, when using a class a decorator the function, here def Literal, is used to initialize an instance. Written differently you could write: Literal = _TypedCacheSpecialForm(lambda self, *parameters: ...) # or def _literal_function(self, *parameters): ... #contents of def Literal Literal = _TypedCacheSpecialForm(_literal_function)  In short: Literal is a instance of a class which has a __getitem__ method. With the call to __getitem__ Literal._getitem is invoked, which is the actual function declared as def Literal in the source code. Literal[str] -> __getitem__(str) -> _getitem(str)/def Literal(str) returns then an instance of the _LiteralGenericAlias class. Which you could say is the actual type hint.
Question: In this example, the entrypoint http://127.0.0.1:8000/ returns formatted text: "Hello \"World\"!" The quotes are masked by a slash, and quotes are added both at the beginning and at the end. How to return unformatted text, identical to my string Hello "World"!. import uvicorn from fastapi import FastAPI app = FastAPI() @app.get("/",) def read_root(): return 'Hello "World"!' uvicorn.run(app) 

Answer: When you return a string from a FastAPI endpoint, FastAPI will automatically convert it to a JSON response, which is why the quotes are being escaped in your example. JSON strings must escape double quotes with a backslash. If you want to return unformatted text (plain text) and not JSON, you can do so by using FastAPI's PlainTextResponse object (docs here). I'm using FastApi version 0.104 here: import uvicorn from fastapi import FastAPI from fastapi.responses import PlainTextResponse app = FastAPI() @app.get( "/", response_class=PlainTextResponse, ) def read_root(): return 'Hello "World"!' uvicorn.run(app)
Question: I am trying to run a Python program through a Bash script. When I execute the python file by shell it works with no problem, but when I try to execute the script, the python keyword is not recognized. I have a fresh Ubuntu 23.10 installation and I added, on top of the ~/.bashrc file, the following line: alias python=python3  How to solve this problem? Toy example For sake of simplicity I made a toy example that displays the same behavior. I have an hello-world.py file, whose content is simply print("Hello World!")  If I run it with the shell ($ python hello-world.py), it works smoothly:  Hello World!  Now I created a script start.sh whose content is: #!/bin/bash python hello-world.py  But if I execute it $ ./start.sh I get the following error:  ./start.sh: line 3: python: command not found 

Answer: So, the issue is that your shebang isn't running bash as a login shell (which is pretty normal). You are aliasing python to python3 in your .bashrc, but that will only get run in a login shell. So try adding: #!/bin/bash -l  to your shebang. Although, to be clear, I would consider this a hack. I would suggest that ideally you just use python3 in the shell script. Or, create a symlink. If you are on ubuntu, you can use python-is-python3: https://packages.ubuntu.com/focal/python-is-python3
Question: I am using Ruff, a formatter or linter tool for Python code. I want to ignore some specific rules, and to write that config in pyproject.toml. My package structure is as follows. . ├── LICENSE ├── pyproject.toml ├── README.md ├── src │ └── mypackage/mymodule.py └── tests ├── doc.md └── test_mymodule.py  And, I want to ignore rules pydocstyle (D) in the tests/ directory.

Answer: You can use Ruff's per-file-ignores and specify individual files, or select a directory tree with a wildcard (star). You can ignore a "letter class" by specifying only the letter. Example: Ignore specific rule in a specific file To ignore line-length violations in your tests, add this to pyproject.toml: [tool.ruff.lint.per-file-ignores] "foofile.py" = ["E501"]  Example: Ignore a class of rules in a whole directory tree To ignore all pydocstyle errors (starting with "D") in your tests, add this to pyproject.toml: [tool.ruff.lint.per-file-ignores] "tests/*" = ["D"]  The related Ruff docs are here.
Question: Is there a way to retain both the left and right join keys when performing a left join using the Python Polars library? Currently it seems that only the left join key is retained but the right join key gets dropped. Please see example code below: import polars as pl df = pl.DataFrame( { "foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"], } ) other_df = pl.DataFrame( { "apple": ["x", None, "z"], "ham": ["a", "b", "d"], } ) df.join(other_df, on="ham", how="left")  In other data manipulation libraries this is possible for example using the R dplyr library you can do the below or even in standard SQL we can access the right join key in the SELECT clause. library(dplyr) df <- data.frame( foo = c(1, 2, 3), bar = c(6.0, 7.0, 8.0), ham = c("a", "b", "c") ) other_df <- data.frame( apple = c("x", NA, "z"), ham = c("a", "b", "d") ) df %>% left_join(other_df, by = c("ham" = "ham"), suffix = c("", "_right"), keep = TRUE) 

Answer: join now has a coalesce= parameter to control this behaviour. df.join(other_df, on="ham", how="left", coalesce=False)  shape: (3, 5) ┌─────┬─────┬─────┬───────┬───────────┐ │ foo ┆ bar ┆ ham ┆ apple ┆ ham_right │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ f64 ┆ str ┆ str ┆ str │ ╞═════╪═════╪═════╪═══════╪═══════════╡ │ 1 ┆ 6.0 ┆ a ┆ x ┆ a │ │ 2 ┆ 7.0 ┆ b ┆ null ┆ b │ │ 3 ┆ 8.0 ┆ c ┆ null ┆ null │ └─────┴─────┴─────┴───────┴───────────┘
Question: I have tried to install any python packages on Ubuntu 24.04, but found I cannot do that as in 22.04, with --user PEP668 said it is for avoiding package conflict between system-wide package and user installed package. example: $ pip install setuptools --user error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. If you wish to install a non-Debian packaged Python application, it may be easiest to use pipx install xyz, which will manage a virtual environment for you. Make sure you have pipx installed. See /usr/share/doc/python3.11/README.venv for more information. note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification.  I am really confused with current rules and can not install any package to user local env. How can I manage my user local environment now? And how can I use latest pip (not linux-distro version) and other packages by default for current user? My Environment (dockerfile just for reproduce): FROM ubuntu:24.04 # add python RUN apt install -y python3-pip python3-venv python-is-python3 pipx USER ubuntu WORKDIR /app  I know I can use some env manage tools (pyenv) to do that, but is there any built-in method to bring my user local env back?

Answer: I finally found the best solution is use third-party tools (pyenv, conda, mini-forge). Pyenv use it's own python and pip independent with system's and use it by default. see $ where pip /home/john/.pyenv/shims/pip /home/john/.local/bin/pip /usr/local/bin/pip /usr/bin/pip /bin/pip  So, system python and daily used python are separated. User can use pip install xxx like in previous days.
Question: How can I add row numbers within windows in a DataFrame? This example depicts what I want (in the counter column): >>> df = pl.DataFrame([{'groupings': 'a', 'target_count_over_windows': 1}, {'groupings': 'a', 'target_count_over_windows': 2}, {'groupings': 'a', 'target_count_over_windows': 3}, {'groupings': 'b', 'target_count_over_windows': 1}, {'groupings': 'c', 'target_count_over_windows': 1}, {'groupings': 'c', 'target_count_over_windows': 2}, {'groupings': 'd', 'target_count_over_windows': 1}, {'groupings': 'd', 'target_count_over_windows': 2}, {'groupings': 'd', 'target_count_over_windows': 3}]) >>> df shape: (9, 2) ┌───────────┬───────────────────────────┐ │ groupings ┆ target_count_over_windows │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═══════════╪═══════════════════════════╡ │ a ┆ 1 │ │ a ┆ 2 │ │ a ┆ 3 │ │ b ┆ 1 │ │ c ┆ 1 │ │ c ┆ 2 │ │ d ┆ 1 │ │ d ┆ 2 │ │ d ┆ 3 │ └───────────┴───────────────────────────┘  Obviously the df.with_row_numbers() method exists, but it adds row numbers for the whole dataframe.

Answer: You can generate an .int_range() over each group with a window function. df.with_columns(count = 1 + pl.int_range(pl.len()).over("groupings"))  shape: (9, 3) ┌───────────┬───────────────────────────┬───────┐ │ groupings ┆ target_count_over_windows ┆ count │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ i64 │ ╞═══════════╪═══════════════════════════╪═══════╡ │ a ┆ 1 ┆ 1 │ │ a ┆ 2 ┆ 2 │ │ a ┆ 3 ┆ 3 │ │ b ┆ 1 ┆ 1 │ │ c ┆ 1 ┆ 1 │ │ c ┆ 1 ┆ 2 │ │ d ┆ 1 ┆ 1 │ │ d ┆ 2 ┆ 2 │ │ d ┆ 3 ┆ 3 │ └───────────┴───────────────────────────┴───────┘
Question: I'm new in DRF and now I have an Error in logout in DRF API browsable when I want to logout in API browsable it shows me Error 405 (method not Allowed) this is my setting.py: REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', ), }  this is my urls.py: urlpatterns = [ path('admin/', admin.site.urls), path('api-auth/', include('rest_framework.urls')) ]  please tell what can I do to fix this Error as I said I'm new in DRF so I just Wrote this codes nothing else I think this Error its because BasicAuthentication

Answer: It depends on Django version you are using. If you are using Version 5, try downgrading to 4.2.7.
Question: I have an application in Python with a microservices architecture and pipelines for which I use Celery along with RabbitMQ and Redis. In an application flow (machine learning training) 8 methodologies are needed, therefore a first worker called "Worker Training" sends 8 tasks to another worker called "Worker Training Model". This second one has 3 replicas to be able to finish the training faster. At first it works well, each worker consumes a methodology and processes it until finishing and consuming the next one. However I am seeing that for example at this moment, 5 of the 8 tasks have already been completed, there are 2 workers (of the 3 replicas) processing a task, but there is 1 worker doing nothing! It should be processing the last missing methodology! Any idea why this happens? I think that in the end it will end up being processed in one of the other 2 workers when they finish with the one they have at the moment, but I need to be more efficient and not have workers doing nothing that can consume tasks. My RabbitMQ dashboard looks like this right now:  (That task is the missing methodology and should be done by the free worker...)

Answer: This is most likely caused by the prefetching. Read the Prefetch Limits section, and the section after to find out how to make worker reserve as many tasks as they have (free) worker processes. In short (TLDR): task_acks_late = True worker_prefetch_multiplier = 1
Question: I have a dataframe with 3 kind of data types - int64, float64 and object (string). How to apply format for two different data types in one function? Sample: def abc_style_format(cell_val): default = "" style_a = "background-color: green" style_b = "background-color: gold" style_c = "background-color: salmon" style_float = "precision=2" match cell_val: case "A": return style_a case "B": return style_b case "C": return style_c if type(cell_val) == "float64": return style_float return default df_abc.style.map(abc_style_format)  Block match/case works properly, but part precision=2 doesn't work. The result: 

Answer: Here is one way to do it by defining as many helper functions as needed: import pandas as pd def _abc_style_format(s: pd.Series) -> list[str]: """Helper function to format 'abc' columns. Args: s: target column. Returns: list of styles to apply to each target column cell. """ background_colors = [] for v in s: match v: case "A": background_colors.append("background-color: green") case "B": background_colors.append("background-color: gold") case "C": background_colors.append("background-color: salmon") return background_colors  def _qty_format(s: pd.Series) -> list[str]: """Helper function to format 'qty' column. Args: s: target column. Returns: list of styles to apply to each target column cell. """ font_colors = [] for v in s: if v > 150: font_colors.append("color: red") else: font_colors.append("color: green") return font_colors  And one main function: from pandas.io.formats.style import Styler def style(df: pd.DataFrame) -> Styler: """Function to style whole dataframe. Args: df: target dataframe. Returns: styled dataframe. """ styler = df.style for col in df.columns: match df[col].dtype: case "object": styler = styler.apply(_abc_style_format, subset=col) case "float64": styler = styler.format(precision=2, subset=col) case "int64": styler = styler.apply(_qty_format, subset=col) case _: pass return styler  Then, with the following toy dataframe: df = pd.DataFrame( { "description": ["product1", "product2", "product3"], "qty": [130, 160, 110], "gm": [43.20000, 40.70000, 35.20000], "abc_gm_category": ["A", "B", "C"], "abc_amount_category": ["C", "A", "B"], } )  You can simply do: styled_df = style(df) styled_df  Which gets you the expected output:
Question: Hey i am trying to make a application working in windows. It's basically adjusting your monitor brightness. There is a function provides minimize application. So i have issue about minimize. When clicking second time to minimize button getting error: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "C:\Users\user\OneDrive\Desktop\Python_simple_projects\Desktop_brightness_app\test.py", line 249, in minimize_app self.icon.run() File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\pystray\_base.py", line 212, in run self._run() File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\pystray\_win32.py", line 120, in _run self._hwnd = self._create_window(self._atom) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\pystray\_win32.py", line 244, in _create_window hwnd = win32.CreateWindowEx( File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\pystray\_util\win32.py", line 204, in _err raise ctypes.WinError() OSError: [WinError 6] The handle is invalid.  There is a few part of code below: import ttkbootstrap as tb import ttkbootstrap as ttk import PIL.Image import pystray class DesktopBrightnessApp: def __init__(self): self.minimize_button = tb.Button(self.root, text="Minimize", command=self.minimize_app) self.minimize_button.place(x=215, y=200) self.icon = pystray.Icon('icon', img, menu=pystray.Menu( pystray.MenuItem("Open GUI", self.on_move_clicked), pystray.MenuItem("Exit", self.on_move_clicked) )) def on_move_clicked(self, icon, item): if str(item) == "Open GUI": icon.stop() self.root.deiconify() elif str(item) == "Exit": icon.stop() self.root.destroy() def minimize_app(self): self.root.withdraw() self.icon.run() def run(self): self.root.mainloop() if __name__ == "__main__": app = DesktopBrightnessApp() app.run() 

Answer: When icon.stop() is executed, you need to create a new instance of pystray.Icon in order to run icon.run(): class DesktopBrightnessApp: def __init__(self): ... # convert img to instance variable self.img self.img = ... ... # create the icon menu once self.menu = pystray.Menu( pystray.MenuItem("Open GUI", self.on_move_clicked), pystray.MenuItem("Exit", self.on_move_clicked) ) # don't create instance of pystray.Icon here ... def minimize_app(self): self.root.withdraw() # create instance of pystray.Icon here self.icon = pystray.Icon('icon', self.img, menu=self.menu) self.icon.run() ...