eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am learning web development, I have some experience in python scripting. This time, I wanted to create an api in python, so review fast api docs. I have the following set up (example contrived for the purpose of this post). Based on the examples in fast api site, I created following python code, which has two methods. Also, I have defined classes that represent the data I want to read in these methods. My understanding is that pydantic library validates data. So, for testing, I used curl command as such, which works. Pydantic maps the data {'name': 'foo'} I sent in curl command, to the class I have defined Item. Is there any documentation on how it maps data? For my next method, I want to post contents of a file, like video/audio data, which I understand I can send as raw binary data. I have defined FileContent class with a field that can store bytes. but when I try to post file contents, curl -X POST -F "file=@somefile.wav" http://127.0.0.1:5000/upload_file_content  I get json serialization error. how can I map the binary content of the file, I send via curl command to the FileContent class? curl -X "POST" \ "http://127.0.0.1:5000/items" \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d "{\"name\": \"foo\"}"  from fastapi import FastAPI from pydantic import BaseModel, bytes app = FastAPI() class FileContent(BaseModel): data: bytes class Item(BaseModel): name: str @app.post("/items/") async def create_item(item: Item): return item @app.post("/upload_file_content/") async def upload_file_content(file_content: FileContent): # do something with file content here 

Answer: You're on the right track with FastAPI and Pydantic, but binary file uploads via curl -F (i.e., multipart/form-data) don't get automatically mapped to a Pydantic model like FileContent. When dealing with file uploads, FastAPI provides a special way to handle binary data using UploadFile, not bytes directly in a Pydantic model. Here’s how to rewrite your /upload_file_content/ endpoint: from fastapi import FastAPI, File, UploadFile from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str @app.post("/items/") async def create_item(item: Item): return item @app.post("/upload_file_content/") async def upload_file_content(file: UploadFile = File(...)): contents = await file.read() # Get file contents as bytes return {"filename": file.filename, "size": len(contents)}
Question: I have 2 dataframes, dfA & dfB. dfA contains purchases of certain products & dfB contains info on said products. For instance, dfA:    purchaseID productID quantity     1 432 1   2 432 4   3 567 7    and dfB:    productID name     432 'mower'   567 'cat'    I wish to merge the two datasets on productID to produce something like:    purchaseID productID quantity name     1 432 1 'mower'   2 432 4 'mower'   3 567 7 'cat'    In actual fact, dfA & dfB are much larger. How can I do this? I understand how to do normal one-one merges, but struggling to see how to do one-many.

Answer: pd.merge(dfA, dfB, on='productID', how='inner') you can use merge along with the how type
Question: How could I split a column of string into list of list? Minimum example: df = pl.DataFrame({'test': "A,B,C,1\nD,E,F,2\nG,H,I,3\nJ,K,L,4"})  I try the following, somehow I stop after the first split df = df.with_columns(pl.col('test').str.split('\n'))  My desired result would be it return a list of list inside the dataframe, so that the list of list is readily to be read by other columns result = pl.DataFrame({'test': [[["A","B","C",1], ["D","E","F",2], ["G","H","I",3], ["J","K","L",4]]]}, strict=False) result = result.with_columns( get_data = pl.col('test').list[2].list[3].cast(pl.Int64) # Answer = 3 ) result.glimpse()  Rows: 1 Columns: 2 $ test <list[list[str]]> [['A', 'B', 'C', '1'], ['D', 'E', 'F', '2'], ['G', 'H', 'I', '3'], ['J', 'K', 'L', '4']] $ get_data <i64> 3 

Answer: df.with_columns( pl.col('test') .str.split('\n') .list.eval( pl.element() .str.split(",") ) )  In your example you have a list of mixed strings and numbers which polars doesn't support so your output has to have the numbers as strings. You say you want to use these lists from other columns readily so you might want to convert to a struct column and unnest it so that you have new flat columns.
Question: This script: import numpy as np, cv2 as cv contour = np.array([[0, 0], [1, 0], [1, 1], [0.5, 0.2], [0, 0]], dtype='f4') hull = cv.convexHull(contour, returnPoints=False) defects = cv.convexityDefects(contour, hull)  fails and produces this error message:  File "/home/paul/upwork/pickleball/code/so-65-ocv-convexity-defects.py", line 5, in <module> defects = cv.convexityDefects(contour, hull) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cv2.error: OpenCV(4.11.0) /io/opencv/modules/imgproc/src/convhull.cpp:319: error: (-215:Assertion failed) npoints >= 0 in function 'convexityDefects'  What is the reason? Here is a plot of contour:  And hull is: [[0] [1] [2]] 

Answer: I couldn't find confirmation for that in the documentation, but convexityDefects expects the points coordinates in contour to be int32 rather than floating points. The following code works as expected: import cv2 as cv import numpy as np contour = np.array([[0, 0], [10, 0], [10, 10], [5, 2], [0, 0]], dtype=np.int32) hull = cv.convexHull(contour, returnPoints=False) defects = cv.convexityDefects(contour, hull) print(defects)  Output: [[[ 2 0 3 543]]]  Note that the result is a 4 elements vector: (start_index, end_index, farthest_pt_index, fixpt_depth) and the last element in the result vector (that seem relatively large) is fixpt_depth:  fixed-point approximation (with 8 fractional bits) of the distance between the farthest contour point and the hull. That is, to get the floating-point value of the depth will be fixpt_depth/256.0.
Question: When i double click on this python script, it automatically closes turtle window after receiving the input. import math import turtle def arc(t, radius, angle): arc_length = 2 * math.pi * radius * angle / 360 n = int(arc_length / 3) + 1 step_length = arc_length / n step_angle = float(angle) / n polyline(t, n, step_length, step_angle) def polyline(t, n, length, angle): for i in range(n): t.fd(length) t.lt(angle) def circle(t, r): arc(t, r, 360) turtle.mainloop() radius=input("Enter the radius of the circle to be printed: ") bob=turtle.Turtle() circle(bob,radius)  I have tried running the script from Python IDLE. It's working fine from IDLE. But it closes the turtle window when I double click on the python script file.

Answer: radius is set by a string, producing an error in arc when computing arc_length , so the execution stops and the window is closed For instance replace radius=input("Enter the radius of the circle to be printed: ")  by radius=float(input("Enter the radius of the circle to be printed: "))  Launching the execution by hand in a shell allows you to see the error message
Question: I'm working with Pyright in strict mode and want to check if a function parameter value of type object is an Iterable[str]. I tried using: if isinstance(value, Iterable) and all(isinstance(v, str) for v in value): # Pyright complains: 'Type of "v" is unknown'  However, looking at the elements, Pyright complains that the type of v is still Unknown, even after the isinstance check on each element. Why doesn't all(isinstance(...)) refine the type of value to Iterable[str]? For context: I'm implementing __contains__ in a class that inherits from collections.abc.MutableSequence. Therefore, the method signature must remain def __contains__(self, value: object) -> bool, I can't change the type annotation of value to Iterable[str]. Here's my current implementation: def __contains__(self, value: object) -> bool: if isinstance(value, Iterable) and all(isinstance(v, str) for v in value): value = "".join(value).upper() return value in "".join(self.sequence) # self.sequence is an Iterable[str] return False  Is there a way to get Pyright to properly infer the type of value here without using an explicit cast() or defining a separate TypeGuard function? It seems to me that a separate TypeGuard should make no difference. Alternatively: am I looking at this the wrong way? Should I try to avoid implementing __contains__ myself, because of the object type?

Answer: No you cannot do it with pyright without additional structures. Plain all(isinstance(...)) is not a type guard that is supported by pyright. Different to filter can an all type guard not be written in the stubs and needs needs special handling; see some discussion here or here.  The type checker would not only need to hard code knowledge of all but also make assumptions about the semantics of the iterable expression it is acting upon.   Support would need to be added specifically for all(isinstance(a, b) for a in [x, y]). This specific expression form is rarely used, so it wouldn't make sense to add the custom logic to support it.  The official recommendation is to use manual type guards. The following is a nicely reusable function that lets you also specify the type. Alternatively use a cast after the if. from typing import Iterable, TypeIs from typing_extensions import TypeForm def is_iterable[T](obj, typ: TypeForm[T]=object) -> TypeIs[Iterable[T]]: return isinstance(obj, Iterable) and all(isinstance(v, str) for v in obj) def __contains__(self, value: object) -> bool: if is_iterable(value, str): reveal_type(value) # Iterable[str] value = "".join(value).upper() return value in "".join(self.sequence) # self.sequence is an Iterable[str] reveal_type(value) # object return False  For the strict mode you can add a # pyright: ignore[reportUnknownArgumentType] to not complain about v in the iterator or use these alternative guards: def is_iterable_of_type[T](obj: object, typ: type[T]=object) -> TypeIs[Iterable[T]]: return is_iterable(obj) and all(isinstance(v, typ) for v in obj) def is_iterable(obj: object) -> TypeIs[Iterable[Any]]: return isinstance(obj, Iterable)
Question: I have a class with two threads  MainThread - solving tasks one by one(getting self.cur_task_id, and changing self.cur_task_status) Thread self.report_status_thread - read self.cur_task_id, self.cur_task_status and send values via http  I am using mutex in my class to synchronize these threads. Is that redundant because of GIL (Global Interpreter Lock)? class DeepAllocationService(): def __init__(self,): self.mutex = threading.Lock() # ensure cur_task_id and cur_task_status changed in the same thread self.cur_task_id = None self.cur_task_status = None self.report_status_thread = threading.Thread(target=self.periodic_send_status) def periodic_solve_tasks(self,): self.report_status_thread.start() while True: try: new_task, task_file = self.get_new_task() if new_task: self.update_task_info(new_task["id"], new_task["status"]) self.solve_task(new_task, task_file) except Exception as e: self.cur_task_status = TaskStatus.ERROR self.logger.error(f"Exception in periodic_solve_tasks: {e}") finally: time.sleep(1) def update_task_info(self, task_id=None, task_status=None): self.mutex.acquire() self.cur_task_id = task_id self.cur_task_status = task_status self.mutex.release() def periodic_send_status(self,): while True: # read-only function - read and send self.cur_task_id and self.cur_task_status requests.post() # sends elf.cur_task_id and self.cur_task_status time.sleep(2) 

Answer: If you had a single field, which you either read or wrote, relying on the GIL would be sufficient (it does not allow torn reads / torn writes). However, here that is not the case: you have two fields, and the GIL can be released at any bytecode operation, so a separate thread could observe inconsistent values for cur_task_id and cur_task_status. Incidentally:  you should use the lock as a context manager, as that avoids deadlocking on error rather than use attributes and a lock, you could use a queue.Queue to communicate between the solver and the reporter  I would strongly recommend the latter, as currently the reporter is not guaranteed to see every task or status, and has to sleep in order to avoid busy-looping. With a queue, the reporter could just wait on the queue waiting it up. Your solver task is also inconsistent: while True: try: new_task, task_file = self.get_new_task() if new_task: self.update_task_info(new_task["id"], new_task["status"]) self.solve_task(new_task, task_file) except Exception as e: self.cur_task_status = TaskStatus.ERROR self.logger.error(f"Exception in periodic_solve_tasks: {e}") finally: time.sleep(1)  If get_new_task raises, an error status will be assigned to the previous task.
Question: I have a dataframe where each column starts and finished with certain number of nan values. Somewhere in the middle of a column there is a continuous list of values. It can happen that a nan value "interrupts" the data. I want to iterate over each column, find such values and then remove the whole row. For example, I want to find the np.nan between 9 and 13 and remove it: [np.nan, np.nan, np.nan, 1, 4, 6, 6, 9, np.nan, 13, np.nan, np.nan]  Conditions for removal:  if value has at least one data point before if value has at least one data point after if value is nan  I wrote code that does this already, but it's slow and kind of wordy. import pandas as pd import numpy as np data = {'A': [np.nan, np.nan, np.nan, 1, 4, 6, 6, 9, np.nan, 13, np.nan, np.nan], 'B': [np.nan, np.nan, np.nan, 11, 3, 16, 13, np.nan, np.nan, 12, np.nan, np.nan]} df = pd.DataFrame(data) def get_nans(column): output = [] for index_to_check, value in column.items(): has_value_before = not column[:index_to_check].isnull().all() has_value_after = not column[index_to_check + 1:].isnull().all() is_nan = np.isnan(value) output.append(not( has_value_before and has_value_after and is_nan)) return output for column in df.columns: df = df[get_nans(df[column])] print(df)  How can I improve my code, vectorize it etc?

Answer: You could use a vectorial approach with isna and cummin to perform boolean indexing. First let's use one column as example: # identify NaNs m1 = df['A'].isna() # Identify external NaNs m2 = (m1.cummin()|m1[::-1].cummin()) out= df.loc[m2 | ~m1, 'A']  Output: 0 NaN 1 NaN 2 NaN 3 1.0 4 4.0 5 6.0 6 6.0 7 9.0 9 13.0 10 NaN 11 NaN Name: A, dtype: float64  Then you can vectorize to the whole DataFrame and aggregate with all: m1 = df.isna() m2 = (m1.cummin()|m1[::-1].cummin()) out= df.loc[(m2 | ~m1).all(axis=1)]  Output:  A B 0 NaN NaN 1 NaN NaN 2 NaN NaN 3 1.0 11.0 4 4.0 3.0 5 6.0 16.0 6 6.0 13.0 9 13.0 12.0 10 NaN NaN 11 NaN NaN  Another option would be to leverage interpolate with limit_area='inside': # is the cell not NaN? m1 = df.notna() # is the cell an external NaN? m2 = df.interpolate(limit_area='inside').isna() out = df[(m1|m2).all(axis=1)]
Question: This script: import numpy as np from numpy.linalg import norm a = np.array([(1, 2, 3), (1, 4, 9), (2, 4, 4)]) b = np.array([(1, 3, 3), (1, 5, 9)]) r = sum([min(norm(a-e, ord=1, axis=1)) for e in b])  computes a similarity measure r between different size NumPy arrays a and b. Is there a way to express it entirely with NumPy API for greater efficiency?

Answer: You can do this: r = np.linalg.norm(a[:,None,:]-b[None], ord=1, axis=2).min(0).sum()  Here, a[:,None,:] - b[None] will add extra dimensions to a and b for the proper subtraction broadcasting. The norm is unchanged, the .min(0) takes the row-wise minimum, and then .sum() gets the final result.
Question: I have a GeoDataFrame I want to plot. This works fine, however somehow I cannot easily plot its legend. I have tried a number of alternatives and checked solutions from googling and LLM, but I do not understand why this does not work. Code: import geopandas as gpd from shapely.geometry import box, Polygon, LineString import matplotlib.pyplot as plt fig, ax = plt.subplots() bounding_box = [9.454, 80.4, 12, 80.88] polygon = box(*bounding_box) gdf = gpd.GeoDataFrame(geometry=[polygon]) plot_obj = gdf.plot(ax=ax, edgecolor='red', facecolor='none', linewidth=2, label="user bbox query") # plt.legend() # does not work # ax.legend(handles=[plot_obj], labels=["test"]) # does not work ax.legend(handles=[plot_obj]) # does not work plt.xlabel('Longitude') plt.ylabel('Latitude') plt.show()  Result:  I get a warning: <python-input-0>:16: UserWarning: Legend does not support handles for Axes instances. A proxy artist may be used instead. See: https://matplotlib.org/stable/users/explain/axes/legend_guide.html#controlling-the-legend-entries ax.legend(handles=[plot_obj]) # does not wor  But somehow I am not able to take advantage of it to make things work (I tried several ways to plot the legend from "handles", see the different attempts, but none work). I am certainly missing something - any pointer to how this can be done simply? :)

Answer: The issue is that plot_obj = gdf.plot(...) returns an Axes object, not a plot "handle" that can be passed to legend(). To display a legend, you need to create a proxy artist (e.g., a matplotlib.patches.Patch) that mimics the appearance of your GeoDataFrame's geometry (in your case, a red-bordered polygon with no fill), and then use that in ax.legend(). Here’s how to fix your code to show the legend correctly: import geopandas as gpd from shapely.geometry import box import matplotlib.pyplot as plt from matplotlib.patches import Patch # For the legend proxy fig, ax = plt.subplots() bounding_box = [9.454, 80.4, 12, 80.88] polygon = box(*bounding_box) gdf = gpd.GeoDataFrame(geometry=[polygon]) # Plot the GeoDataFrame gdf.plot(ax=ax, edgecolor='red', facecolor='none', linewidth=2) # Create a legend proxy legend_patch = Patch(facecolor='none', edgecolor='red', linewidth=2, label='user bbox query') ax.legend(handles=[legend_patch]) # Use the proxy artist for legend plt.xlabel('Longitude') plt.ylabel('Latitude') plt.show()  Result:
Question: I'm trying to make it impossible to instantiate a class directly, without it having any unimplemented abstract methods. Based on other solutions online, a class should have something along the lines of: class Example: def __new__(cls,*args,**kwargs): if cls is Example: raise TypeError("...") return super().__new__(cls,*args,**kwargs)  I'm trying to move this snippet to a separate place such that each such class does not have to repeat this code. C = TypeVar("C") def abstract(cls:Type[C])->Type[C]: class Abstract(cls): def __new__(cls, *args:Any, **kwargs:Any)->"Abstract": if cls is Abstract: msg = "Abstract class {} cannot be instantiated".format(cls.__name__) raise TypeError(msg) return cast( Abstract, super().__new__(*args,**kwargs) ) return Abstract  This is my attempt and might be incorrect But mypy complains:  error: Invalid base class "cls"  How can I have some reusable way (such as a decorator) to achieve what I want whilst passing mypy --strict? Context: This is in the context of a pyside6 application where I'm subclassing QEvent, to have some additional extra properties. The base class defining these properties (getters) has a default implementation, yet I would like to prevent it from being initialized directly as it is not (and should not) be registered to the Qt event system. (I have a couple more such classes with different default values for convenience)

Answer: May I suggest a mixin instead of a decorator? You basically want to check if cls.mro()[1] is the abstract class (i.e., in the current class is a direct subclass of Abstract) from typing import Self, final class Abstract: @final def __new__(cls, *args, **kwargs) -> Self: if cls.mro()[1] is Abstract: raise TypeError("...") return super().__new__(cls,*args,**kwargs) class AbstractFoo(Abstract): def frobnicate(self, x: int) -> int: return x // 42 class ConcreteFoo(AbstractFoo): def __init__(self, value: int) -> None: self.value = value class AbstractBaz(Abstract): def __new__(cls, *args, **kwargs) -> Self: return object.__new__(cls) foo1 = ConcreteFoo(1) foo2 = AbstractFoo() # TypeError: ...  Note, due to the @final decorator on Abstract.__new__, mypy complains about trying to override __new__ in AbstractBaz: main.py:20: error: Cannot override final attribute "__new__" (previously declared in base class "Abstract") [misc]  You may or may not want to go with this, depending on how much you want to control, but keep in mind, if someone really wants to instantiate your class, they can and you cannot really stop them because any user can simply use object.__new__ directly. Note, static analysis tools will not catch: foo2 = AbstractFoo()  I don't think there is any way to express abstractness in the type system itself, abc is special cased.
Question: I'm trying to make a graph using plotly library and I want to make some texts in bold here's the code used : import plotly.express as px import pandas as pd data = { "lib_acte":["test 98lop1", "test9665 opp1", "test QSDFR1", "test ABBE1", "testtest21","test23"], "x":[12.6, 10.8, -1, -15.2, -10.4, 1.6], "y":[15, 5, 44, -11, -35, -19], "circle_size":[375, 112.5, 60,210, 202.5, 195], "color":["green", "green", "green", "red", "red", "red"], "textfont":["normal", "normal", "normal", "bold", "bold", "bold"], } #load data into a DataFrame object: df = pd.DataFrame(data) fig = px.scatter( df, x="x", y="y", color="color", size='circle_size', text="lib_acte", hover_name="lib_acte", color_discrete_map={"red": "red", "green": "green"}, title="chart" ) fig.update_traces(textposition='middle right', textfont_size=14, textfont_color='black', textfont_family="Inter", hoverinfo="skip") newnames = {'red':'red title', 'green': 'green title'} fig.update_layout( { 'yaxis': { "range": [-200, 200], 'zerolinewidth': 2, "zerolinecolor": "red", "tick0": -200, "dtick":45, }, 'xaxis': { "range": [-200, 200], 'zerolinewidth': 2, "zerolinecolor": "gray", "tick0": -200, "dtick": 45, # "scaleanchor": 'y' }, "height": 800, } ) fig.add_scatter( x=[0, 0, -200, -200], y=[0, 200, 200, 0], fill="toself", fillcolor="gray", zorder=-1, mode="markers", marker_color="rgba(0,0,0,0)", showlegend=False, hoverinfo="skip" ) fig.add_scatter( x=[0, 0, 200, 200], y=[0, -200, -200, 0], fill="toself", fillcolor="yellow", zorder=-1, mode="markers", marker_color="rgba(0,0,0,0)", showlegend=False, hoverinfo="skip" ) fig.update_layout( paper_bgcolor="#F1F2F6", ) fig.show()  and here's the output of above code:  What I'm trying to do is to make "test ABBE1", "testtest21","test23" in bold on the graph, could anyone please help how to do that ?

Answer: Not sure if there is a better solution, but, as mentioned in furas's comment you can use the HTML tag <b>…</b> for the elements that should be bold-faced. You can achieve this, for example, by adding the following line: # Create your data dict (as before) data = { ... } # Add HTML tag for boldface data["lib_acte"] = [(f"<b>{el}</b>" if ft == "bold" else el) for el, ft in zip(data["lib_acte"], data["textfont"])] # Create dataframe (as before) df = pd.DataFrame(data)  The documentation says:  Chart Studio uses a subset of HTML tags to do things like newline (<br>), bold (<b></b>), italics (<i></i>), and hyperlinks (<a href=’…’></a>). Tags <em>, <sup>, and <sub> are also supported.  Here, Chart Studio is the online service built on top of and provided by the makers of plotly. While not explicitly stated, I am quite sure that the same subset of HTML tags also applies to plotly (stand-alone) – I just tried successfully with <b>, <sup>, and <a href=…>.