eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: i implemented a system of complex valued functions and i solved it with sagemath's "solve" method. In my solutions array i find entries like: x1 == r5 or for different cases x1 == c15403. (and so on with diffent numbers) Does the r5 stand for an arbitary rational number and c15403 for arbitary complex number? Maybe someone can give me a hint where to find such stuff in the documentation. Ty and have nice day.

Answer: Yes, 'c' stands for complex. I think that 'r' stands for real, 'z' stands for integer. I don't know if there is a symbol for rationals. I don't know where it's documented, but I believe this comes from Maxima, which Sage uses as a solver. See the documentation for new_variable at https://maxima.sourceforge.io/docs/manual/maxima_346.html#Functions-and-Variables-for-to_005fpoly_005fsolve, and in particular look at the first code example. The Sage reference manual mentions the r and z prefixes in the documentation of the solve method of symbolic relations. Slightly amended excerpt:  If there is a parameter in the answer, that will show up as a new variable. In the following example, r1 is an arbitrary real (hence the r). sage: forget() sage: x, y = var('x, y') sage: solve([x + y == 3, 2*x + 2*y == 6], x, y) [[x == -r1 + 3, y == r1]] sage: b, c = var('b, c') sage: solve((b - 1)*(c - 1), [b, c]) [[b == 1, c == r...], [b == r..., c == 1]]  Especially with trigonometric functions, the dummy variable may be implicitly an integer (hence the z). sage: solve(sin(x) == cos(x), x, to_poly_solve=True) [x == 1/4*pi + pi*z...] sage: solve([cos(x)*sin(x) == 1/2, x + y == 0], x, y) [[x == 1/4*pi + pi*z..., y == -1/4*pi - pi*z...]]
Question: I am trying to compute the area and the center coordinates of objects in a binary mask using opencv. However, I noticed that in some cases I get the wrong result. For example, if I get the contours like this: import numpy as np import cv2 binary_mask = np.array([ [0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1]]) contours, _ = cv2.findContours( binary_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) >>> contours (array([[[0, 3]], [[5, 3]]], dtype=int32), array([[[2, 0]], [[2, 1]], [[3, 1]], [[3, 0]]], dtype=int32))  Then I get ZeroDivisionError for the center calculation: def get_centroid_from_contour(contour): M = cv2.moments(contour) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) return (cX, cY) >>> get_centroid_from_contour(contour) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in get_centroid_from_contour_ ZeroDivisionError: float division by zero  This I think is related to the fact that somehow opencv thinks the 2x2 squared object has zero area: >>> cv2.contourArea(contours[0]) 0.0  It seems something related to "open objects". Indeed the first contour only contains two points that do not close the polygon, but I have no idea how to fix this. I also tried closing the contour as suggested here but it doesn't work.

Answer: Looks like a bug in the implementation of moments(). It appears to not consider how contours are defined in OpenCV. The issue isn't with your 2x2 connected component, but with the 6x1 one. That one is returned first. Its contour consists of two points. For the contour of a 1-pixel line, moments() should have still given a non-zero m00 because that is how contours work in OpenCV. Since you say you got a ZeroDivisionError, it must have been zero. That is incorrect for OpenCV's notion of a contour, but correct for every other notion of a polygon/contour. In OpenCV, a contour describes the polygon that needs to be drawn to reproduce the picture. The line goes ON the outer edge pixels that are still inside the connected component. That is why the contour of your line CC becomes a 2-corner polygon of, strictly speaking, zero area. OpenCV could have defined contours to be zero-width lines that circumscribe the outer pixels of a connected component. That would have required:  the drawing calls in OpenCV to not be as broken and neglected as they are (I'm being blunt but it's true) the conventions for a contour to allow non-integer coordinates  Feel free to submit an issue on OpenCV's github page. The moments() implementation needs a fix. Addendum: the fix should probably be another flag that distinguishes "contours" from proper polygons, so the mathematically correct behavior isn't lost, but instead made optional. Everyone calls that function on contours, so that should be the default.
Question: I wrote a function that creates a table of periods given the comparison of a value from a series as well as a specific minimum duration. Now I recently cleaned up my environment and somehow the same function doesn't assign values anymore as it did before. However, it still creates a dataframe of the right shape, it just doesn't assign values and I can't figure out why. Previous output:  Current output:  from datetime import datetime import pandas as pd pd.options.mode.chained_assignment = None import pandas_datareader.data as web import dataframe_image as dfi # getting the last periods of increasing interest rates (Federal Funds Effective MONTHLY Rate) from 1965 onwards fed_rates = web.DataReader("FEDFUNDS", "fred", 1965) fed_rates.set_index(pd.to_datetime(fed_rates.index.date), inplace=True) # test for empty values print(fed_rates.index.isna().sum()) def period_df(start, duration, fed_rates=fed_rates): fed_rates = fed_rates[fed_rates.index >= start] df = pd.DataFrame(columns=["Name", "Start", "Last"]) df.loc[df.shape[0]] = [None, None, None] period = 0 j = 0 for i in range(0, len(fed_rates) - 1): if (fed_rates.iloc[i + 1]["FEDFUNDS"] <= fed_rates.iloc[i]["FEDFUNDS"]) and ( i - j >= duration ): df.loc[period]["Last"] = datetime.strftime(fed_rates.index[i], "%Y-%m-%d") period += 1 if (fed_rates.index[-1] - fed_rates.index[i]).days >= 365: df.loc[len(df)] = [None, None, None] j = i elif (fed_rates.iloc[i + 1]["FEDFUNDS"] <= fed_rates.iloc[i]["FEDFUNDS"]) and ( i - j < duration ): df.iloc[period]["Name"] = "Period " + str(period + 1) df.loc[period]["Start"] = datetime.strftime(fed_rates.index[i], "%Y-%m-%d") j = i # add last date df.loc[period]["Last"] = datetime.strftime(fed_rates.index[-1], "%Y-%m-%d") # add duration column df["Duration"] = ( pd.to_datetime(df["Last"]) - pd.to_datetime(df["Start"]) ) / np.timedelta64(1, "M") # export df as image dfi.export( df.style.set_properties( **{"background-color": "white", "color": "black", "border-color": "#948b8b"} ), "periods" + start + ".png", ) return df period_df(start="1995", duration=9) 

Answer: In pandas the loc/iloc operations, when they are not setting anything, just return a copy of the data. When you do something along the lines of df.loc[row][col] = value, it may look like the loc operation setting something, but this "assignment" happen in two stages:  First, df.loc[row] retrieves a copy of the relevant row Then, the copy's col is set to value  You can avoid this by passing row and col to loc/iloc or even use at/iat: df.loc[row, col] = value df.at[row, col] = value
Question: I need to use python's pandas to tabulate the test result in a csv format. The result could be "passsed" or sometime "failed". After I  import python as pd,my code is: df = pd.read_csv('myfile.csv') pass_res =df['Status'].value_counts()['passed'] fail_res =df['Status'].value_counts()['failed']  this code will work if there IS a case of fail. However, when there is no failure, the last line of code will cause an error. How do check, if there is a failure, then I will execute my last line.

Answer: You can also add a CategoricalDType as value_counts returns all observed: # sample df = pd.DataFrame({'Status': ['passed']*5 + ['other']*3}) status = pd.CategoricalDtype(['passed', 'failed'], ordered=True) passed, failed = df['Status'].astype(status).value_counts().sort_index()  Output: >>> passed 5 >>> failed 0 >>> df['Status'].astype(status).value_counts().sort_index() Status passed 5 failed 0 Name: count, dtype: int64 >>> df Status 0 passed 1 passed 2 passed 3 passed 4 passed 5 other 6 other 7 other
Question: I have a large file that I'm trying to reduce using dataframe.drop Here's my code: probe_df = pd.read_csv(csv_file,header = 9233, nrows = 4608) # Get rid of stuff c_to_drop = 'Unnamed: ' + str(count_tp+1) probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1)  When I ran this, I got this error message: probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1) TypeError: DataFrame.drop() takes from 1 to 2 positional arguments but 3 were given  I don't understand why I got the error and how to fix it. Please help! I'm a newbie and I tried looking online for a solution but I'm still quite new and didn't see anything that matched my issue exactly.

Answer: According to the pandas documentation, the source for drop would be  DataFrame.drop(labels=None, *, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')  The * represents the end of allowed positional arguments. This indicates that the positional arguments would be labels, and for python's error message this would likely be from the self positional argument that is not directly shown as it is implicitly supplied. Therefore, doing probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1) would be feeding 2 positional arguments into a function which only takes 1 (not including self). By changing it to probe_df.set_index("Chamber ID").drop(c_to_drop, axis=1), we convert the 1 from a positional argument to a keyword argument as required by the function.
Question: I am using xarray for combining multiple netcdf files using xarray.open_mfdataset. But I get the error while running the command, below are the commands and error. nc_all = xarray.open_mfdataset(files,combine = 'nested', concat_dim="time") files = glob.glob("/filepath/*")  I get the following error- Traceback (most recent call last): File "/home/lsrathore/GLEAM/GLEAM_HPC.py", line 85, in <module> nc_1980_90 = xarray.open_mfdataset(files[1:11],combine = 'nested', concat_dim="time") File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 1038, in open_mfdataset datasets = [open_(p, **open_kwargs) for p in paths] File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 1038, in <listcomp> datasets = [open_(p, **open_kwargs) for p in paths] File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 572, in open_dataset ds = _dataset_from_backend_dataset( File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 367, in _dataset_from_backend_dataset ds = _chunk_ds( File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 315, in _chunk_ds chunkmanager = guess_chunkmanager(chunked_array_type) File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/core/parallelcompat.py", line 87, in guess_chunkmanager raise ValueError( ValueError: unrecognized chunk manager dask - must be one of: []  What is causing the problem?

Answer: The issue was resolved when I downgraded the xarray version to 0.21.1 from 2023.5.0
Question: I am trying to create a TypedDict for better code completion and am running into an issue. I want to have a fixed set of keys (an Enum) and the values to match a specific list of objects depending on the key. For example: from enum import Enum class OneObject: pass class TwoObject: pass class MyEnum(Enum): ONE: 1 TWO: 2  I am looking to have something like this: from typing import TypedDict class CustomDict(TypedDict): MyEnum.ONE: list[OneObject] MyEnum.TWO: list[TwoObject]  However, I am getting Non-self attribute could not be type hinted and it doesn't really work. What are my options?

Answer: This is not compatible with the TypedDict specification as laid out in PEP 589. Let me quote: (emphasis mine)  A TypedDict type represents dictionary objects with a specific set of string keys, and with specific value types for each valid key.  So using arbitrary enum members for defining TypedDict keys is invalid. While TypedDict does also support an alternative, functional definition syntax and you could theoretically make your enum have the str data type by doing class MyEnum(str, Enum): ..., you would still probably not be able to define a TypedDict with those enum members in a way that your type checker understands. That is because only actual string literals are officially accepted as keys as mentioned in the section on the Use of Final Values and Literal Types. Quote: (again, emphasis mine)  Type checkers are only expected to support actual string literals, not final names or literal types, for specifying keys in a TypedDict type definition. [...] The motivation for this is to make type declarations self-contained, and to simplify the implementation of type checkers.  In other words, whether something like the following is supported depends entirely on any given type checker: from enum import Enum from typing import TypedDict class OneObject: pass class TwoObject: pass class MyEnum(str, Enum): ONE = "1" TWO = "2" CustomDict = TypedDict( "CustomDict", {MyEnum.ONE: list[OneObject], MyEnum.TWO: list[TwoObject]} )  Mypy (currently) does not and gives the output: error: Invalid TypedDict() field name. (By the way, I tested it with Final variables as keys and those are also rejected.) So depending on what your use case is, you will probably have to bite the bullet and explicitly type out the enum/key names again or just not use an enum for that in the first place, as suggested by @artem in his answer.
Question: I have this small fastapi application import uvicorn from fastapi import FastAPI, APIRouter from fastapi import Path from pydantic import BaseModel from starlette import status app = FastAPI() def test(): print("creating the resource") return "Hello world" router = APIRouter() class MessageResponse(BaseModel): detail: str router.add_api_route( path="/test", endpoint=test, methods=["POST"], responses={ status.HTTP_201_CREATED: {"model": MessageResponse} } ) app.include_router(router) def main(): uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001) if __name__ == "__main__": main()  when I check the docs on http://127.0.0.1:8001/docs#/default/test_test_post, in the list of responses in the docs, I see two responses: 200 and 201 I don't have any 200 responses here. I don't want 200 to be shown for me in the docs. Here is the fast api auto-generated openapi.json file { "openapi": "3.0.2", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": {"/test": { "post": {"summary": "Test", "operationId": "test_test_post", "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}} }, "201": { "description": "Created", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/MessageResponse"}}}}}}} }, "components": {"schemas": { "MessageResponse": {"title": "MessageResponse", "required": ["detail"], "type": "object", "properties": {"detail": {"title": "Detail", "type": "string"}}}}}}  I should not be seeing  "description": "Successful Response", "content": {"application/json": {"schema": {}}} },  What should I do?  UPDATE: this one also did not work import uvicorn from fastapi import FastAPI, APIRouter from pydantic import BaseModel from starlette import status from starlette.responses import Response app = FastAPI() def test(response: Response): print("creating the resource") response.status_code = 201 return "Hello world" router = APIRouter() class MessageResponse(BaseModel): detail: str router.add_api_route( path="/test", endpoint=test, methods=["POST"], response_model=None, responses={ 200: {}, status.HTTP_201_CREATED: {"model": MessageResponse} } ) app.include_router(router) def main(): uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001) if __name__ == "__main__": main() 

Answer: The default response can be set with the status_code parameter, and the default response model can also be directly controlled by the return type. This example shows you how to do it with the decorator paradigm which is recommended over manually adding API routes. class MessageResponse(BaseModel): detail: str @router.post('/test', status_code=201) def test() -> MessageResponse: print("creating the resource") return "Hello world"  If you really need to do it with your current structure, you can simply define status_code in the add_api_route function. router.add_api_route( path="/test", endpoint=test, methods=["POST"], status_code=201, responses={ status.HTTP_201_CREATED: {"model": MessageResponse} } )
Question: I have create a small robot framework test suit which will communicate with trace32 Lauterbach. My idea is to run different functions name using a loop. Every loop, it will make a breakpoint in the Trace32 later batch. I have written a simple python script as library in the robot framework. test.robot file import os *** Settings *** Documentation simple test script to control Trace32 Library Collections Library can.Trace32 Suite Setup Suite Teardown *** Variables *** ${temp} 1 *** Test Cases *** Check Input and Output [Documentation] test script [Setup] #Retrive Data . This list has 5 values ${MainList} Create List #start debugger start Debugger #connect debugger Connect Debugger #Iterate 5 times FOR ${UserAttribute} IN @{MainList} #sleep 1 sec Sleep 1 seconds #call for breakpoint break Debugger ${temp} +=1 END Disconnect Debugger [Teardown]  and the trace 32 script file: import time import ctypes from ctypes import c_void_p import enum T32_DEV = 1 class Trace32: def start_Debugger(self): self.t32api = ctypes.cdll.LoadLibrary('D:/test/api/python/t32api64.dll') self.t32api.T32_Config(b"NODE=",b"localhost") self.t32api.T32_Config(b"PORT=",b"20000") self.t32api.T32_Config(b"PACKLEN=",b"1024") rc = self.t32api.T32_GetChannelSize() ch1 = ctypes.create_string_buffer(rc) self.t32api.T32_GetChannelDefaults(ctypes.cast(ch1,ctypes.c_void_p)) ch2 = ctypes.create_string_buffer(rc) self.t32api.T32_GetChannelDefaults(ctypes.cast(ch2,ctypes.c_void_p)) self.t32api.T32_SetChannel(ctypes.cast(ch2,c_void_p)) def Connect_Debugger(self): rc = self.t32api.T32_Init() rc = self.t32api.T32_Attach(T32_DEV) def breakpoint_Debugger(self): rc = self.t32api.T32_Ping() time.sleep(2) rc = self.t32api.T32_Cmd(b"InterCom M7_0 Break") time.sleep(3) rc = self.t32api.T32_Cmd(b"InterCom M7_0 Go") time.sleep(2) rc = self.t32api.T32_Cmd(b"InterCom M7_0 break.Set My_func") time.sleep(2) def Disconnect_Debugger(self): rc = self.t32api.T32_Exit()  In the robot file, I am calling start Debugger and Connect Debugger function to start and connect the debugger. I want self.t32api to be global. So that I can call break_Debugger many times to put a breakpoint. But Unfortunately, I can only put breakpoint in the first iteration. In second iteration, the breakpoint is not working. How can I make self.t32api global until the robot file executed completely?

Answer: Just initialize it in the constructor of the Trace32 class, so it will persist as long as Trace32 object exist, we can then also remove start_Debugger() class Trace32: def __init__(self): self.t32api = ctypes.cdll.LoadLibrary('D:/test/api/python/t32api64.dll') def start_Debugger(self): self.t32api.T32_Config(b"NODE=",b"localhost") self.t32api.T32_Config(b"PORT=",b"20000") self.t32api.T32_Config(b"PACKLEN=",b"1024") rc = self.t32api.T32_GetChannelSize() ch1 = ctypes.create_string_buffer(rc) self.t32api.T32_GetChannelDefaults(ctypes.cast(ch1,ctypes.c_void_p)) ch2 = ctypes.create_string_buffer(rc) self.t32api.T32_GetChannelDefaults(ctypes.cast(ch2,ctypes.c_void_p)) self.t32api.T32_SetChannel(ctypes.cast(ch2,c_void_p))
Question: I'm trying to parse a To email header with a regex. If there are no <> characters then I want the whole string otherwise I want what is inside the <> pair. import re re_destinatario = re.compile(r'^.*?<?(?P<to>.*)>?') addresses = [ 'XKYDF/ABC (Caixa Corporativa)', 'Fulano de Tal | Atlantica Beans <fulano.tal@atlanticabeans.com>' ] for address in addresses: m = re_destinatario.search(address) print(m.groups()) print(m.group('to'))  But the regex is wrong: ('XKYDF/ABC (Caixa Corporativa)',) XKYDF/ABC (Caixa Corporativa) ('Fulano de Tal | Atlantica Beans <fulano.tal@atlanticabeans.com>',) Fulano de Tal | Atlantica Beans <fulano.tal@atlanticabeans.com>  What am I missing?

Answer: You may use this regex: <?(?P<to>[^<>]+)>?$  RegEx Demo RegEx Demo:  <?: Match an optional < (?P<to>[^<>]+): Named capture group to to match 1+ of any characters that are not < and > >?: Match an optional > $: End  Code Demo Code: import re re_destinatario = re.compile(r'<?(?P<to>[^<>]+)>?$') addresses = [ 'XKYDF/ABC (Caixa Corporativa)', 'Fulano de Tal | Atlantica Beans <fulano.tal@atlanticabeans.com>' ] for address in addresses: m = re_destinatario.search(address) print(m.group('to'))  Output: XKYDF/ABC (Caixa Corporativa) fulano.tal@atlanticabeans.com
Question: As a Python beginner, I was downloading the OpenAI's Whisper with the following command: pip install -U openai-whisper, and noticed that you can use Whisper in both Python and the Command-line. To my knowledge, pip install installs Python packages, so should only be available within Python, but it seems like you can use Whisper in the command line? In summary, why does pip install-ing Python packages let you use the package in the command line?

Answer: When you install something with pip, if the package defines an entry point, it creates a command-line wrapper, and adds it to your Python installation's bin. Your PATH environment variable should include the folder Python's bin, so you can run the package from the command-line. https://packaging.python.org/en/latest/specifications/entry-points/
Question: Trying to add, subtract, two Series that contains datatype List[i64]. The operation seems to be not supported. a = pl.Series("a",[[1,2],[2,3]]) b = pl.Series("b",[[4,5],[6,7]]) c = a+b  this gives the error: PanicException: `add` operation not supported for dtype `list[i64]`  I would expect a element-wise sum, like would happen with numpy array for example: c = [[5,7],[8,10]]  What's the correct syntax to add two series of lists?

Answer: you can do the following:  c = (a.explode() + b.explode()).reshape((2,-1)).alias('c') shape: (2,) Series: 'a' [list[i64]] [ [5, 7] [8, 10] ]  Final thoughts: if your list has a fixed size, then you might consider using the new Polars Array datatype.