eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I was given a problem that takes in a table of information (grid coordinates and characters) and asked to place them into a table to make a message. I've spent time working on some Python to work it out but my answer is coming out upside down and I can't figure out why. import requests from bs4 import BeautifulSoup webpage_response = requests.get('https://docs.google.com/document/d/e/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq/pub') webpage = webpage_response.content #print(webpage) soup = BeautifulSoup(webpage, 'html.parser') table = soup.find('table') #print(doc_table) rows = table.find_all('tr') data = [] for row in rows[1:]: cells = row.find_all('td') character = cells[1].text.strip() x = cells[0].text.strip() y = cells[2].text.strip() data.append((character,x,y)) #print(data) max_x = max(x for _,x,_ in data) max_y = max(y for _, _,y in data) #print(max_x) #print(max_y) grid = [[' ' for _ in range(int(max_x) + 1)] for _ in range(int(max_y) + 1)] for character, x, y in data: grid[int(y)][int(x)] = character for row in grid: print(''.join(row))  This is my code. It should print out a grid of characters that look like a capital F but its upside down. I think the problem arises when I populate the grid but I'm not sure how to fix it.

Answer: The likely cause of the upside-down output is how you are indexing grid[int(y)]. In typical grid representations for display:  grid[0] often represents the top row.  grid[len(grid)-1] often represents the bottom row.   However, if your data's y-coordinates are such that y=0 is the bottom of your intended image (like a standard Cartesian coordinate system where y increases upwards), then directly using grid[int(y)] will place y=0 at grid[0], which is the top. This effectively flips the image vertically. To fix this, you need to adjust the y-coordinate when placing the character into the grid. I tried to improve your program a bit. Please check the Google Drive link I shared and feel free to give your feedback. ->> Kindly take a look Explanation of the fix (adjusted_y = max_y - y): Let's assume your y-coordinates in the data range from 0 to max_y.  When y = max_y (the "top" of your desired "F" in the original coordinate system), adjusted_y will be max_y - max_y = 0. This places the character in grid[0], which is the very first (top) row of your printed grid.  When y = 0 (the "bottom" of your desired "F" in the original coordinate system), adjusted_y will be max_y - 0 = max_y. This places the character in grid[max_y], which is the very last (bottom) row of your printed grid.   This transformation effectively reverses the order of the y-coordinates, causing the image to be displayed correctly (right-side up).
Question: Plotly express line plots number of data points instead of actual data on the y axis. Now whats confusing me is that it only does this on one machine, its works perfectly fine on 3 others. I made sure python and the packages i use are of the same version. I tried changing the data type to int and float, which only does what i described above. However if i changed the data type to string it no longer showed the number of data point, but it scaled all the values that aren't 0 to 1(other devices did the same on datatype str). df['Machine_Status'] = df['Machine_Status'].astype(int) df = df.dropna(subset=['Machine_Status']) df =df.reset_index(drop=True) df[['Timestamp', 'Machine_Status']].to_csv('machine_status_timeframe_output.csv', index=False) fig = px.line( df, x='Timestamp', y='Machine_Status', title=f'{machine_id} Activity on {selected_date} (6 AM to 10 PM)', labels={'Timestamp': 'Time', 'Machine_Status': 'Activity'}, line_shape='hv', )     Timestamp Machine_Status     2025-06-16 06:00:04 0   2025-06-16 06:00:09 0   2025-06-16 06:00:14 3   2025-06-16 06:00:18 0   2025-06-16 06:00:23 0   2025-06-16 06:00:28 3   2025-06-16 06:00:33 0   2025-06-16 06:00:38 0   2025-06-16 06:00:43 3   2025-06-16 06:00:48 0   2025-06-16 06:00:53 0   2025-06-16 06:00:58 3   2025-06-16 06:01:03 0   2025-06-16 06:01:08 0   2025-06-16 06:01:13 0   2025-06-16 06:01:18 0   2025-06-16 06:01:23 0   2025-06-16 06:01:28 0   2025-06-16 06:01:33 0   2025-06-16 06:01:38 0   2025-06-16 06:01:43 0   2025-06-16 06:01:48 0   2025-06-16 06:01:53 0    Current behavior:  Expected behavior: 

Answer: The symptom you’re seeing is exactly the bug that crept into Plotly v6.0.0: in Jupyter the first release of 6.x sometimes mistakes a perfectly-normal numeric series for “categorical” and silently switches the underlying trace to the default aggregation “count”. Instead of plotting the values it therefore shows how many rows share each x-value – the bar/line height becomes “number of points”, not the data you passed in. Why you only see it on one PC  The machine that mis-behaves almost certainly has plotly == 6.0.0 installed.  The other three computers are still on the stable 5.x line (or have been upgraded to ≥ 6.0.1), so the bug never triggers.   You can confirm this in one line: import plotly, sys print(plotly.__version__, sys.executable)  How to fix it 1. Upgrade to a patched 6.x release (recommended) pip install -U plotly # installs the latest 6.1.2 at the time of writing # or pin to the first patched build: # pip install plotly==6.0.1  2. …or temporarily roll back to the last 5.x version pip install "plotly<6" # e.g. 5.24.1 or 5.23.0  Either option removes the aggregation glitch; your original code works unchanged.
Question: I have two arrays a1 and a2 of shape M1xM2x...xMPx3 and N1xN2x...xNQx3, and a mask m (boolean array) of shape M1xM2x...xMPxN1xN2x...xNQ (you can assume that a1 and a2 are at least 2D). import numpy as np np.random.seed(0) a1 = np.random.rand(4, 5, 3) a2 = np.random.rand(6, 3) m = np.random.rand(4, 5, 6) >= 0.7  I would like to two arrays b1 and b2 of shape Mx3 that contains the values of a1 and a2 where the mask m is true. My current way is to repeat a1 and a2 to obtain arrays of size M1xM2x...xMPxN1xN2x...xNQx3 and then index them using m: a1_shape, a2_shape = a1.shape[:-1], a2.shape[:-1] t_shape = a1_shape + a2_shape + (3,) a1 = a1[..., None, :].repeat(np.prod(a2_shape), axis=-2).reshape(t_shape) a2 = a2[None, ..., :].repeat(np.prod(a1_shape), axis=0).reshape(t_shape) b1, b2 = a1[m, :], a2[m, :]  This requires creating two huge array of size M1xM2x...xMPxN1xN2x...xNQ which can be problematic. Is there a way to obtain the same b1 and b2 without having to create such huge intermediate array?

Answer: A possible solution: idx = np.where(m) P = len(a1.shape) - 1 Q = len(a2.shape) - 1 a1_idx = idx[:P] a2_idx = idx[P:P+Q] b1 = a1[a1_idx] if P > 0 else np.tile(a1, (np.sum(m), 1)) b2 = a2[a2_idx] if Q > 0 else np.tile(a2, (np.sum(m), 1))  This solution begins by using np.where(m) to obtain a tuple of index arrays corresponding to all True locations in m. The number of spatial dimensions for a1 is determined using P = len(a1.shape) - 1, where .shape provides the dimensions and len counts them; Q is determined similarly for a2. These index arrays are then partitioned: the first P arrays (idx[:P]) are assigned to a1_idx and the subsequent Q arrays (idx[P:P+Q]) are assigned to a2_idx. For edge cases where an array might lack spatial dimensions (P=0 or Q=0), it handles this by using np.tile, which repeats the original vector np.sum(m) times (counting True values), ensuring the output shape matches the expected (K, 3) format.
Question: I'm currently working on removing all the type errors from my Python project in VS Code. Assume you have a Python class that has an asymmetric property. It takes any kind of iterable and converts it into a custom list subclass with additional methods. class ObservableList(list): """list with events for change, insert, remove, ...""" # ... class MyFrame: @property def my_list(self) -> ObservableList: return self._my_list @my_list.setter def my_list(self, val: typing.Iterable): self._my_list = ObservableList(val) # ... my_frame = MyFrame()  VS Code (i.e. Pyright) will correctly deduce that:  you can set my_frame.my_list using any iterable, and my_frame.my_list will always be an ObservableList.  Now, let's assume that there is no actual @property code. Instead, the property is implemented dynamically using __setattr__ and __getattr__. (Context: We're talking about a GUI generator which provides automatic bindings.) I want to use a declaration on class level to tell the typechecker that this property exists, without actually spelling it out: class MyFrame(AutoFrame): my_list: ??? # ...  (AutoFrame provides the __getattr__/ __setattr__ implementation.) What can I put in place of the ??? to make this work?  When using ObservableList, Pyright complains when I assign a plain list to the property. When using Iterable or list, Pyright complains when I access ObservableList-specific methods. Same when using list | ObservableList: Pyright assumes that the property could return both, and list misses the additional methods.  Re: close vote: the linked question's answer basically boils down to going back to square one (implementing the property explicitly). The point of using AutoFrame is specifically to get rid of that repetitive boilerplate code. Just imagine doing this for a GUI frame with a dozen bound controls. I can live with a single added declaration line but not much more.

Answer: You can use the Any-Trick, see my answer on how the four different cases work, but in short, if you type it as: from typing import Any, reveal_type class ObservableList(list): """list with events for change, insert, remove, ...""" def foo(self) -> str: ... class MyFrame: my_list: ObservableList | Any MyFrame().my_list = [2, 3] # OK value: str = MyFrame().my_list.foo() # OK, add :str to avoid value being str | Any  Of course that will have the downside that any assignment, will be okay as well: MyFrame().my_list = "no error" so you need to be aware of that.  Alternatively, you can implement the property behind a if TYPE_CHECKING block OR in a .pyi file: no-runtime influence, correct-typing, but boilerplate: class MyFrame: if TYPE_CHECKING: @property def my_list(self) -> ObservableList: ... @my_list.setter def my_list(self, val: typing.Iterable): ...
Question: I get a division by zero error when I run the following python script. import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(10, 10)) freq = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 1.8, 2.0] amp = [0.0, 0.0, 0.0, 0.0, 0.0, 0.012, 0.031, 0.074, 0.082, 0.084, 0.080, 0.078, 0.072, 0.059, 0.039, 0.019, 0.010] ax.semilogx(freq, amp, marker='s', color='purple') plt.xlim(0.1, 10) plt.xlabel('Frequency (Hz)') plt.ylabel('Crest Loss (ft)') ax.set(title='Fundamental Frequency') ax.grid() ax.grid(which="minor", color="0.9") def forward(x): return 1 / x def inverse(x): return 1 / x secax = ax.secondary_xaxis('top', functions=(forward, inverse)) secax.set_xlabel('Period (s)') plt.show()  The plot seems to output correctly, but I don't know why I get a division by zero error when all the data plotted on the x axis is greater than zero.

Answer: I think it is just a "feature" of the secondary axis. In the third example in the docs here, they explicitly have a check to avoid divide by zero warnings: fig, ax = plt.subplots(layout='constrained') x = np.arange(0.02, 1, 0.02) np.random.seed(19680801) y = np.random.randn(len(x)) ** 2 ax.loglog(x, y) ax.set_xlabel('f [Hz]') ax.set_ylabel('PSD') ax.set_title('Random spectrum') def one_over(x): """Vectorized 1/x, treating x==0 manually""" x = np.array(x, float) near_zero = np.isclose(x, 0) x[near_zero] = np.inf x[~near_zero] = 1 / x[~near_zero] return x # the function "1/x" is its own inverse inverse = one_over secax = ax.secondary_xaxis('top', functions=(one_over, inverse)) secax.set_xlabel('period [s]') plt.show()  Also, see the code example on the secondary_axis docs page here where the same thing is done.
Question: I'm trying to dynamically assign class attributes to an existing custom class. These attributes have individual getter and setter methods defined in a loop across all attributes to be created and are assigned with setattr() within the loop. Please see code below. If I use setattr(), the newly created attributes point towards the last attribute that was created in the list. Whereas if I use setattr() within an eval() statement, it works correctly, i.e. each attribute pointing towards its own value. I think I kinda understand why - something about the scope of the functions and setattr() in the loop - but I can't really formulate it into words. Can someone smarter than me please give a formal explanation about what is going on and if there is a cleaner solution than using eval()? for attr in attr_list: def getter(self): return getattr(self, attr) def setter(self, value): setattr(self, attr, value) # DOES NOT WORK setattr(self.__class__, attr, property(getter, setter)) # WORKS eval(f'setattr(self.__class__, \'{attr}\', property(lambda self: getattr(self, \'{attr}\'), lambda self, value: setattr(self, {attr}, value)))') 

Answer: You create different getters and setters at each iteration but they all use the attr variable from the outer scope; this means that when you access a getter/setter then it uses the current value of the attr variable, which will be the final value from the loop. You need to have attr in a different scope for each getter and setter so that each refers to the corresponding attr value. Note: you also want the getter/setter to refer to a different attribute or you will enter an infinite loop. I.e. _attr. def create_property(attr): private_attr = f"_{attr}" def getter(self): return getattr(self, private_attr) def setter(self, value): setattr(self, private_attr, value) return property(getter, setter) attr_list = ["a", "b", "c"] class Test: pass for attr in attr_list: setattr(Test, attr, create_property(attr)) test1 = Test() test2 = Test() test1.a = 42 test1.b = 17 test2.a = 96 test2.b = 123 print(test1.a, test1.b, test1._a, test1._b) print(test2.a, test2.b, test2._a, test2._b)  Outputs: 42 17 42 17 96 123 96 123  fiddle
Question: I'm trying to plot some data with a non-linear fit using the function:  kB and Bv being a constant while J'' is the independent variable and T is a parameter. I tried to do this in Python: #Constants kb = 1.380649e-23 B = 0.3808 #Fit function def func(J, T): return (B*(2*J+1) / kb*T * np.exp(-B*J*(J+1)/kb*T)) popt, pcov = optimize.curve_fit(func, x, y) print(popt) plt.plot(x, y, "o") plt.plot(x, func(j, popt[0]))  But this results in the warning "OptimizeWarning: Covariance of the parameters could not be estimated warnings.warn('Covariance of the parameters could not be estimated'" and the parameter turns out to be 1. This is the data: x = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48] y = [0.185,0.375,0.548,0.695,0.849,0.931,0.996,0.992,1.0,0.977,0.927,0.834,0.691,0.68,0.575,0.479,0.421,0.351,0.259,0.208,0.162,0.135,0.093,0.066]  

Answer: Why? You have three problems in your model:  Priority of operations are not respected, as pointed by @jared, you must add parenthesis around the term kb * T; Target dataset is normalized to unity where it is its area which is unitary (it is a distribution). therefore such normalization prevents the model to converge. Adding the missing parameter N to the model allows convergence; Scale issue and/or units issue, your Boltzmann constant is expressed in J/K but we don't have any clue about the units of B. Recall the term inside the exponential must be dimensionless. Scaling constant to eV/K seems to provide credible temperature.  MCVE Bellow a complete MCVE fitting your data: import numpy as np import matplotlib.pyplot as plt from scipy import optimize, integrate from sklearn import metrics #kb = 1.380649e-23 # J/K kb = 8.617333262e-5 # eV/K B = 0.3808 # ? def model(J, T, N): return N * (B * (2 * J + 1) / (kb * T) * np.exp(- B * J * (J + 1)/ (kb * T))) J = np.array([2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48]) N = np.array([0.185,0.375,0.548,0.695,0.849,0.931,0.996,0.992,1.0,0.977,0.927,0.834,0.691,0.68,0.575,0.479,0.421,0.351,0.259,0.208,0.162,0.135,0.093,0.066]) popt, pcov = optimize.curve_fit(model, J, N, p0=[1e5, 1]) #(array([2.51661517e+06, 2.75770698e+01]), # array([[1.17518427e+09, 3.25544771e+03], # [3.25544771e+03, 6.04463335e-02]])) np.sqrt(np.diag(pcov)) # array([3.42809607e+04, 2.45858361e-01]) Nhat = model(J, *popt) metrics.r2_score(N, Nhat) # 0.9940231921241076 Jlin = np.linspace(J.min(), J.max(), 200) Nlin = model(Jlin, *popt) fig, axe = plt.subplots() axe.scatter(J, N) axe.plot(Jlin, Nlin) axe.set_xlabel("$J''$") axe.set_ylabel("$N(J'')$") axe.grid()  Regressed values are about T=2.51e6 ± 0.03e6 and N=2.76e1 ± 0.02e1, both have two significant figures. The fit is reasonable (R^2 = 0.994) and looks like:  If your Boltzmann constant is expressed in J/K, temperature T is about 1.6e+25 which seems a bit too much for such a variable. Check We can check the area under the curve is indeed unitary: Jlin = np.linspace(0, 100, 2000) Nlin = model(Jlin, *popt) I = integrate.trapezoid(Nlin / popt[-1], Jlin) # 0.9999992484190978
Question: This is my code: import sqlite3 conn = sqlite3.connect(':memory:') cursor = conn.cursor() cursor.execute(''' CREATE TABLE schools ( county TEXT, statustype TEXT ) ''') cursor.execute(''' INSERT INTO schools VALUES ('some_county', 'some_status') ''') cursor.execute(''' SELECT COUNT(*) FROM schools WHERE county = 'Alpine' AND statustype IN ('Active', 'Closed') AND "School Type" = 'District Community Day Schools' ''') result = cursor.fetchone()[0] print(f"Count result: {result}") conn.close()  Note that there is intentionally no 'School Type' column in the database schema in this example. The result is 0. Is possible to change some settings of SQLite3 or of the database in order to get an error about non-existing column instead?

Answer: SQLite has a quirk when it comes to delimiting identifiers. In ANSI SQL double quoting an column name does what you expect i.e. delimits the column name. However in SQLite you get the following behaviour (from the docs)  ... in an effort to be compatible with MySQL 3.x (which was one of the most widely used RDBMSes when SQLite was first being designed) SQLite will also interpret a double-quotes string as string literal if it does not match any valid identifier. This misfeature means that a misspelled double-quoted identifier will be interpreted as a string literal, rather than generating an error. It also lures developers who are new to the SQL language into the bad habit of using double-quoted string literals when they really need to learn to use the correct single-quoted string literal form.  Therefore, because the column doesn't exist, you end up comparing strings to strings e.g. "School Type" = 'District Community Day Schools' which is always false. Therefore you need to delimit the column name using either square brackets ([]) or backticks (``). e.g. you should use [column name here] or `column name here` after which you'll get an error as such:  sqlite3.OperationalError: no such column:   Of course in general its actually better to not use spaces in the first place (eg. school_type). Note: The same documentation goes on to say:  As of SQLite 3.29.0 (2019-07-10) the use of double-quoted string literals can be disabled at run-time...  Which would seem to be a good practice in order to avoid this sort of confusion.
Question: I'm wondering if it is possible to type-hint a Callable with positional- / keyword-only argument without using typing.Protocol. For example in this snippet - how would I type-hint x correctly so that add is assignable to it: from typing import Callable def add(arg: int, /, *, other: str) -> str: return f"{arg}{other}" x: Callable[[int, str], str] = add # Type Error: # `(arg: int, /, *, other: str) -> str` is not assignable to `(int, str) -> str` 

Answer: In short no. A combination is (currently) not possible, as keyword only parameters are not possible with Callable, as it describes positional-only parameters - you need a Protocol for more specific typing. To quote the specs:  Parameters specified using Callable are assumed to be positional-only. The Callable form provides no way to specify keyword-only parameters, variadic parameters, or default argument values. For these use cases, see the section on Callback protocols.   A bit more on assignability; a function with standard parameters (keyword or positional) can be assigned to a function with any parameter type. The reverse, if you have a function that is keyword-only / positional-only it can only be assigned to a matching type, i.e. in your case you need a Protocol that reflects these parameter types exactly. from typing import Callable, Protocol class Standard(Protocol): def __call__(self, a: int) -> None: ... class KwOnly(Protocol): def __call__(self, *, a: int) -> None: ... class PosOnly(Protocol): def __call__(self, a: int, /) -> None: ... CallableType = Callable[[int], None] def func(standard: Standard, kw_only: KwOnly, callable: CallableType, pos_only: PosOnly): # Standard assignable to all f1a: KwOnly = standard # OK f1b: CallableType = standard # OK f1c: PosOnly = standard # OK # Keyword-Only assignable to keyword-only f2a: Standard = kw_only # error f2b: CallableType = kw_only # error f2c: PosOnly = kw_only # error # CallableType and PosOnly are equivalent; only assignable to position-only/Callable f3a: Standard = callable # error f3b: KwOnly = callable # error f3c: PosOnly = callable # OK - as equivalent f4a: Standard = pos_only # error f4b: KwOnly = pos_only # error f4c: CallableType = pos_only # OK - as equivalent  I am not aware that there are any plans to change extend Callable in its behavior, e.g. accept keyword-only via TypedDicts.
Question: I'm working with a function in Python that constructs a 4×4 matrix based on inputs (x1, y1, x2, y2), and computes its eigenvalues and eigenvectors using np.linalg.eigh. Here is a simplified version of my code: import numpy as np def f(kx, ky): return kx + 1j * ky def fs(kx, ky): return np.conj(f(kx, ky)) def eig(x1, y1, x2, y2): a = 10 x = x1 + x2 y = y1 + y2 H = np.array([ [a, f(x, y), f(x, y), fs(x, y)], [fs(x, y), a, 0, f(x, y)], [fs(x, y), 0, -a, f(x, y)], [f(x, y), fs(x, y), fs(x, y), -a] ]) Eval, Evec = np.linalg.eigh(H) sorted_indices = np.argsort(Eval) return Eval[sorted_indices], Evec[:, sorted_indices]  Now, I have 1-d arrays of input values: x1_array, y1_array, x2_array, y2_array # all same shape  I want to efficiently vectorize this function across those arrays — i.e., compute all eigenvalues/eigenvectors in a batched way without writing an explicit Python loop if possible.

Answer: It is a bit of stacking, but you can create a matrix that is your batch size times 4x4 and pass it to np.linalg.eigh. Also note the slight optimization avoiding multiple evaluations of f(x, y) and fs(x, y). def eig_vectorized(x1_array, y1_array, x2_array, y2_array): a = 10 x_array = x1_array + x2_array y_array = y1_array + y2_array f_xy = f(x_array, y_array) # Optimization, you don't want to recompute fs_xy = fs(x_array, y_array) # these two again and again # Create H as an array-sizex4x4 matrix H = np.stack([ np.stack([np.full_like(f_xy, a), f_xy, f_xy, fs_xy], axis=-1), np.stack([fs_xy, np.full_like(f_xy, a), np.zeros_like(f_xy), f_xy], axis=-1), np.stack([fs_xy, np.zeros_like(f_xy), -np.full_like(f_xy, a), f_xy], axis=-1), np.stack([f_xy, fs_xy, fs_xy, -np.full_like(f_xy, a)], axis=-1) ], axis=-2) Evals, Evecs = np.linalg.eigh(H) # Compute eigenvalues and -vectors # Sort eigenvalues and eigenvectors sorted_indices = np.argsort(Evals, axis=-1) Evals = np.take_along_axis(Evals, sorted_indices, axis=-1) Evecs = np.take_along_axis(Evecs, sorted_indices[..., None], axis=-1) return Evals, Evecs