eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to add a parameter type to the readinto() method declared in a custom class that derives from RawIOBase, like this: from io import RawIOBase class Reader(RawIOBase): def readinto(self, buf: bytearray) -> int: pass # actual implementation omitted  But pyright complains: io.py:6:9 - error: Method "readinto" overrides class "_RawIOBase" in an incompatible manner Parameter 2 type mismatch: base parameter is type "WriteableBuffer", override parameter is type "bytearray" "Buffer" is not assignable to "bytearray" (reportIncompatibleMethodOverride) 1 error, 0 warnings, 0 informations  How do I fix this? Note: I know I can remove the type hint entirely. I want to assign it the correct type instead. I'm using Python 3.13.3 and pyright 1.1.400.

Answer: You need to use the same type definition as your base. You can use the same type alias here by importing it from _typeshed package, provided you put it under a TYPE_CHECKING guard: from io import RawIOBase from typing import TYPE_CHECKING if TYPE_CHECKING: from _typeshed import MaybeNone, WriteableBuffer class Reader(RawIOBase): def readinto(self, buf: WriteableBuffer) -> int | MaybeNone: ...  You can count on the _typeshed package existing when running under a type checker, because the type checker brings along that package. This is how pyright knows what the signature for RawIOBase.readinto() is, for example. I added MaybeNone to the above for the same reason, because that's also part of the documented signature. Alternatively, because WriteableBuffer is just a type alias for collections.abc.Buffer, the following also works (replacing MaybeNone with a redefinition): from collections.abc import Buffer from io import RawIOBase from typing import Any, TypeAlias type MaybeNone = Any class Reader(RawIOBase): def readinto(self, buf: Buffer) -> int | MaybeNone: ...  See The Any Trick for an explanation as to why MaybeNone is an alias for Any.
Question: I use the following code to create subclasses of class G and pass them the type of model they should produce. from typing import TypeVar, Type class A: @classmethod def model_validate(cls): print('Done') T = TypeVar('T', bound=A) class G[T]: def __init__(self, model: Type[T]): self.model = model def func(self) -> None: print(self.model.model_validate()) G[A](A).func()  This works fine, but mypy gives this error: error: "type[T]" has no attribute "model_validate" [attr-defined]  What am i doing wrong?

Answer: You are mixing up two type variables. T = TypeVar... defines a typevar with bound, then G[T] completely disregards that, creates a new typevar (with no bound) and uses it. Either use old-style generics (playground): from typing import Generic, TypeVar class A: @classmethod def model_validate(cls) -> None: print('Done') T = TypeVar('T', bound=A) class G(Generic[T]): def __init__(self, model: type[T]) -> None: self.model = model def func(self) -> None: print(self.model.model_validate()) G[A](A).func()  or new-style PEP695 generics (playground): class A: @classmethod def model_validate(cls) -> None: print('Done') class G[T: A]: def __init__(self, model: type[T]) -> None: self.model = model def func(self) -> None: print(self.model.model_validate()) G[A](A).func()  Both are accepted by mypy. Also note that you don't need typing.Type if you're using a non-EOL python version (built-in type should be used instead since 3.9 as shown above).
Question: I am making a simple Python GUI to allow the user to choose a color using Tkinter colorchooser. The user will click a button to open the colorchooser, and I'd like that button to have an image instead of text. However, after you use the colorchooser, the image is deleted from the button and the button is disabled. Any ideas what is causing this failure? If I use text for the button instead of an image, there is no problem using colorchooser. import tkinter as tk from tkinter import colorchooser class MainGUI(tk.Tk): def __init__(self): super().__init__() addImage = r'C:\Documents\Scripting\VisualStudio\ClispiPy\ClispiPy\Images\Add.png' addPhoto = tk.PhotoImage(file = addImage) tk.Button(self, image=addPhoto, command=self.open_sub_gui).pack(pady=20) def open_sub_gui(self): col = colorchooser.askcolor(title='Select Color') return if __name__ == "__main__": main_gui = MainGUI() main_gui.mainloop()  Before using colorchooser:  After using colorchooser: 

Answer: The key fix is simply adding self. to store the PhotoImage as an instance variable instead of a local variable. The full code is provided below: import tkinter as tk from tkinter import colorchooser class MainGUI(tk.Tk): def __init__(self): super().__init__() addImage = r'add.png' self.addPhoto = tk.PhotoImage(file=addImage) # Store as instance variable tk.Button(self, image=self.addPhoto, command=self.open_sub_gui).pack(pady=20) def open_sub_gui(self): col = colorchooser.askcolor(title='Select Color') return if __name__ == "__main__": main_gui = MainGUI() main_gui.mainloop()  Output: I used my own plus sign image.
Question: I want my leafmap map to occupy ALL the backgroud or main, without any margin or something to move it up, down, left or right. However, I still have margins at the top and bottom. Also, if I increase the height this creates a Y offset and I don't want that either but a static window fitted to the main or background space. I attach a screenshot of what I mean. On the other hand, I need to add layers from a netcdf, so it's better folium or leafmap? Please help me. Thanks in advance. import streamlit as st import leafmap.foliumap as leafmap st.set_page_config( page_title=None, page_icon=None, layout="wide", initial_sidebar_state="auto", ) st.markdown( """ <style> header {visibility: hidden;} footer {visibility: hidden;} .block-container { padding: 0 !important; margin: 0 !important; max-width: 100% !important; width: 100% !important; } .main { padding: 0 !important; margin: 0 !important; } .css-1aumxhk { margin-top: 0 !important; } </style> """, unsafe_allow_html=True, ) m = leafmap.Map(center=[-0.1807, -78.4678], zoom=7) m.to_streamlit(height=830)  Margins

Answer: Top margin is created by st.markdown() :) If I use st.html() (without unsafe_allow_html=True,) instead of st.markdown() then top margin disappears. (It seems st.html() was added in version 1.33.0 in Apr 2024 - so it may not be used in older tutorials) As for bottom margin - it needs height: 100vh (as suggested @CamillaGiuliani) but inside <style> and for iframe. It also needs to change iframe into block-object because there is still small gap in iframe like for inline-text. <style> iframe { height: 100vh; display: block; } </style>   Full working code: Streamlit version: 1.44.1 import streamlit as st import leafmap.foliumap as leafmap print('Streamlit version:', st.__version__) st.set_page_config( page_title=None, page_icon=None, layout="wide", initial_sidebar_state="auto", ) st.html( """ <style> header {visibility: hidden;} footer {visibility: hidden;} .block-container { padding: 0 !important; margin: 0 !important; max-width: 100% !important; width: 100% !important; } .main { padding: 0 !important; margin: 0 !important; } .css-1aumxhk { margin-top: 0 !important; } iframe { height: 100vh; display: block; } </style> """ ) m = leafmap.Map(center=[-0.1807, -78.4678], zoom=7) m.to_streamlit() # height=830)
Question: I built and populated a tkinter.Listbox. Now I have events that will select the item at index index. Like so: listbox.selection_clear(0, tk.END) listbox.select_set(index)  And it works in that the entry with index index is in fact selected. However, when using 'tab' keys to move to other widgets that also have the power to select items in that listbox, and then returning to the listbox, there is a shadow selection, that appears not to be the anchor (at least, listbox.selection_anchor(index) did not solve this issue for me) on the selection that was active, when I last left focus on the listbox. Using 'up' and 'down' keys will take control of the active selection. However, they will start not at the proper selection (010_adf in below example), but on that shadow (007_adf) that I can only specify closer by providing this screenshot:  Fig: The "shadow" in question is around entry 007_adf. The proper selection is 010.adf. How to sync the shadow to the proper selection?

Answer: That "shadow" designates the active item in the listbox. Think of it like a cursor in a text widget. You can set it with the activate method. If you want to hide it altogether you can set activestyle='none'. Or, you can set it when you set the selection: ... lb = tk.Listbox(...) ... lb.selection_set(4,6) lb.activate(4) ...
Question: (I know that there are similar questions already answered, but my question focuses more on the reason behind the solution instead of the solution itself). I have been in need of something like a "class property" in Python, and I have searched through existing questions. Some answers provide a workaround, but I cannot understand why python disabled chaining @classmethod and @property. Is there any explanation for this? Also, I've found that all the currently available solutions have limitations, which are listed below. The posts I have read include:  An answer which points out that chaining @classmethod and @property has been disabled since Python 3.13 Another solution which defines a customized classproperty descriptor. But this workaround fails to prevent modification. For example, the following code derived from the original answer will not raise an exception when modification over x is attempted. class classproperty(property): def __get__(self, owner_self, owner_cls): return self.fget(owner_cls) def __set__(self, instance, value): raise AttributeError("can't set attribute") class C(object): @classproperty def x(cls): return 1 print(C.x) C.x = 2 print(C.x) # Output: 2 # no exception raised # cannot prevent modification   A solution by writing the class property into metaclass. This method successfully prevents attempted modifications, but with this method, access to class variables will only be possible via class, not via instance. class CMeta(type): @property def x(cls): return 1 class C(object, metaclass=CMeta): ... print(C.x) # C.x = 2 # AttributeError: property 'x' of 'CMeta' object has no setter # print(C().x) # AttributeError: 'C' object has no attribute 'x'    So is there an ultimate way to resolve all the above mentioned problems and allow for a class property implementation satisfying the following two conditions?  Can prevent attempted modifications Can be accessed from both class and instance 

Answer: Class property was deprecated in Python 3.11 (with link to the original issues) because it was found to be impossible for a chain of @classmethod and @property-decorated attribute to be seen by inspection code as an instance of property. If you need a class property to work on an instance then a custom classproperty is the way to go, but to prevent modifications you can override the __setattr__ method of the metaclass so that it raises an exception if the named attribute is found to be an instance of classproperty: class classproperty(property): def __get__(self, owner_self, owner_cls): return self.fget(owner_cls) class CMeta(type): def __setattr__(cls, name, value): if isinstance(vars(cls).get(name), classproperty): raise AttributeError("can't set attribute") super().__setattr__(name, value) class C(metaclass=CMeta): @classproperty def x(cls): return 1 print(C.x) # 1 print(C().x) # 1 C.x = 2 # AttributeError: can't set attribute  Demo: https://ideone.com/4Ys77N
Question: I am currently down the rabbit hole trying to understand metaclasses and as such went back to refresh my understanding of super() and type. While refreshing I came across a geeksforgeeks super() article and it had a rather weird example. here is the example class Animals: # Initializing constructor def __init__(self): self.legs = 4 self.domestic = True self.tail = True self.mammals = True def isMammal(self): if self.mammals: print("It is a mammal.") def isDomestic(self): if self.domestic: print("It is a domestic animal.") class Dogs(Animals): def __init__(self): super().__init__() def isMammal(self): super().isMammal() <- Why do this?  I understand that when inheriting from a parent class, the functions like __init__ must be called to be initialized, hence the super().__init__() but why do this also in the function? I thought that functions were inherited. Is this so that we can run the parent function along with modifications? Why not call a parent function, within a new function? def mammal_can_dance(self): self.isMammal() print("and can dance") 

Answer: Yes. In this case, since neither the overloading __init__ nor isMammal do anything (besides calling their parent implementations), they could just be omitted for the same result: class Dogs(Animals): pass  Here the parent methods would simply be inherited and do exactly the same thing. But if you want to use the parent's implementation and add something to it, you'd use super and do something in addition: class Foo(Bar): def baz(self): some_list = super().baz() return some_list + ['quux']
Question: When loading a csv file in pandas I've encountered the bellow error message: DtypeWarning: Columns have mixed types. Specify dtype option on import or set low_memory=False  Reading online I found few solutions. One, to set low_memory=False, but I understand that this is not a good practice and it doesn't really resolve the problem. Second solution is to set a data type for each column (or each column with mixed data types): pd.read_csv(csv_path_name, dtype={'first_column': 'str', 'second_column': 'str'})  Again, from what I read, not the ideal solution if we have a big dataset. Third solution - create a converter function. To my understanding this might be the most appropriate solution. I found code which works for me, but I am trying to better understand what is this function exactly doing: def convert_dtype(x): if not x: return '' try: return str(x) except: return '' df = pd.read_csv(csv_path_name, converters={'first_col':convert_dtype, 'second_col':convert_dtype, etc.... } )  Can someone please explain the function code to me? Thanks

Answer: if not x checks if x is an empty string. if it is empty it returns '', which is an empty string without any content. def convert_dtype(x): if not x: return ''  try: return str(x) tries to convert and return x as a string.  try: return str(x)  if converting and returning x as a string doesn't work, it returns ''.  except: return ''  Basically, if the content of the column is empty from the start or can't be converted to string it's discarded and replaced with a string not having any content. I can't judge however if this is a good approach, it depends on what you are trying to accomplish with your application. Your column will only contain strings afterwards nonetheless.
Question: I need to find out the Zscore pertaining to 1 specific point, that is, for 1 value of X using Scipy. Below is the manual code: data = [25, 37, 15, 36, 92, 28, 33, 40] mean = sum(data)/len(data) summation = 0 for i in range(0, len(data)): summation += (data[i]-mean)**2 std = ((1/len(data))*summation))**(1/2) Z = (x-mean)/std  Next, when I try to do the same with Scipy: Z = stats.zscore(data)  I get the output: [-0.61219538 -0.05775428 -1.07422964 -0.10395771 2.48343411 -0.47358511 -0.24256798 0.08085599]  Maybe because I am passing only one parameter and that is the data itself. How to get the Z-Score for only one value of X?

Answer: This solution might be suitable for you. This solution might be better than others. from scipy import stats import numpy as np data = [25, 37, 15, 36, 92, 28, 33, 40] x = 40 # Please assign a specific value mean = np.mean(data) std = np.std(data, ddof=0) Z = (x - mean) / std print(Z)  You might like this method also (scipy.stats.zscore): from scipy import stats data = [25, 37, 15, 36, 92, 28, 33, 40] x = 40 all_zscores = stats.zscore(data, ddof=0) x_index = data.index(x) Z = all_zscores[x_index] print(Z)  Ouput: 0.08085599417810461
Question: I have a Pandas Dataframe that I derive from a process like this: df1 = pd.DataFrame({'c1':['A','B','C','D','E'],'c2':[1,2,3,4,5]}) df2 = pd.DataFrame({'c1':['A','B','C'],'c2':[1,2,3],'c3': [np.array((1,2,3,4,5,6)),np.array((6,7,8,9,10,11)),np.full((6,),np.nan)]}) df3 = df1.merge(df2,how='left',on=['c1','c2'])  This looks like this:    c1 c2 c3     A 1 [1,2,3,4,5,6]   B 2 [6,7,8,9,10,11]   C 3 [nan,nan,nan,nan,nan,nan]   D 4 NaN   E 5 NaN    In order to run the next step of my code, I need all of the arrays in c3 to have a consistent length. For the inputs coming in that were present in the join (i.e. row 1 through 3) this was already taken care of. However, for the rows that were missing from df2 where I now have only a single NaN value (rows 4 and 5) I need to replace those NaN's with an array of NaN values like in row 3. The problem is that I can't figure out how to do that. I've tried a number of things, starting with the obvious: df3.loc[pd.isnull(df3.c3),'c3'] = np.full((6,),np.nan)  Which gave me a ValueError: Must have equal len keys and value when setting with an iterable  Fair enough; I understand this error and why python is confused about what I'm trying to do. How about this? for i in df3.index: df3.at[i,'c3'] = np.full((6,),np.nan) if all(pd.isnull(df3.c3)) else df3.c3  That code runs without error but then when I go to print out df3 (or use it) I get this error: RecursionError: maximum recursion depth exceeded  That one I don't understand, but moving on, what if I preassign a column full of my NaN arrays and then I can do some logic after the join: for i in df1.index: df1.at[i,'c4'] = np.full((6,),np.nan)  This gives me the understandable error: ValueError: setting an array element with a sequence  How about another variation of the same idea: df1['c4'] = np.full((6,),np.nan)  This one gives a different, also understandable error: ValueError: Length of values (6) does not match length of index (5)  Hence, the question: How do I replace values in my dataframe (in this case null values) with an empty numpy array of a given length? For clarity, the desired final result is this:    c1 c2 c3     A 1 [1,2,3,4,5,6]   B 2 [6,7,8,9,10,11]   C 3 [nan,nan,nan,nan,nan,nan]   D 4 [nan,nan,nan,nan,nan,nan]   E 5 [nan,nan,nan,nan,nan,nan]   

Answer: A possible solution: # the array with the 6 nan values arr_nan = np.full( df3['c3'].map( lambda x: np.size(x) if isinstance(x, np.ndarray) else 0).max(), np.nan) df3.assign(c3 = df3['c3'].map( lambda y: arr_nan if not isinstance(y, np.ndarray) else y))  This solution first determines the length of the arrays in c3, and then replaces all non-array entries in c3 by the array of 6 np.nan. Output:  c1 c2 c3 0 A 1 [1, 2, 3, 4, 5, 6] 1 B 2 [6, 7, 8, 9, 10, 11] 2 C 3 [nan, nan, nan, nan, nan, nan] 3 D 4 [nan, nan, nan, nan, nan, nan] 4 E 5 [nan, nan, nan, nan, nan, nan]
Question: I have the app at the bottom. Now, I have this preset field where I can select from 3 options (+ the option changed). What I want to be able to set the input_option with the preset field. But I also want to be able to change it manually. If I change the input_option manually the preset field should switch to changed. The problem is, if I set the option with the preset field, this automatically triggers the second function and sets input_preset back to changed. But this should only happen, if I manually change it, not if it is changed by the first reactive function. is that somehow possible? I tried a little with reactive.isolate(), but this does not seem to have any effect. from shiny import App, ui, reactive app_ui = ui.page_fillable( ui.layout_sidebar( ui.sidebar( ui.input_select("input_preset", "input_preset", choices=["A", "B", "C", "changed"]), ui.input_text("input_option", "input_option", value=''), ) ) ) def server(input, output, session): @reactive.effect @reactive.event(input.input_preset) def _(): if input.input_preset() != 'changed': # with reactive.isolate(): ui.update_text("input_option", value=str(input.input_preset())) @reactive.effect @reactive.event(input.input_option) def _(): ui.update_select("input_preset", selected='changed') app = App(app_ui, server) 

Answer: Require (req()) input.input_option() != input.input_preset() for doing the update on the preset input: from shiny import App, ui, reactive, req app_ui = ui.page_fillable( ui.layout_sidebar( ui.sidebar( ui.input_select("input_preset", "input_preset", choices=["A", "B", "C", "changed"]), ui.input_text("input_option", "input_option", value=''), ) ) ) def server(input, output, session): @reactive.effect @reactive.event(input.input_preset) def _(): if input.input_preset() != 'changed': ui.update_text("input_option", value=str(input.input_preset())) @reactive.effect @reactive.event(input.input_option) def _(): req(input.input_option() != input.input_preset()) ui.update_select("input_preset", selected='changed') app = App(app_ui, server)
Question: I have been trying to make a leapfrog integration to document the variation of the hamiltonian over time for the 3BP, but I never really grasped how to implement it using the normal half-step method so I tried using a variation but I'm not sure if it's correct. This is the functions I'm using where the variables p, v, m p = [array([x,y]), array([x,y]), array([x,y])] v = [array([x,y]), array([x,y]), array([x,y])] m = [1, 1, 1]  are numpy arrays: from numpy import sum from numpy.linalg import norm from copy import deepcopy def H(p, v, m): #hamiltonian function #sum of kinetic energy for all bodies T = sum([m[i]*norm(v[i])**2/2 for i in range(3)]) #sum of potential energy between all bodies V = -sum([m[0-i]*m[1-i]/norm(p[0-i]-p[1-i]) for i in range(3)]) return T + V def a(p, n, m): #sum of the acceleration arrays for body n and the two other bodies return m[n-1]*(p[n-1]-p[n])/(norm(p[n-1]-p[n])**3) + m[n-2]*(p[n-2]-p[n])/(norm(p[n-2]-p[n])**3) def collision(p): #checks for collisions for i in range(3): if norm(p[0-i] - p[1-i]) < 0.1: return True #leapfrog def Leapfrog(P, V, dt, steps, m): p,v=deepcopy(P),deepcopy(V) H_L = [H(p, v, m)] for t in range(steps): atemp = [a(p, i, m) for i in range(3)] #acceleration at time step i #calc new values for i in range(3): p[i] = p[i] + v[i]*dt + 0.5*atemp[i]*dt**2 for i in range(3): v[i] = v[i] + 0.5*(atemp[i] + a(p, i, m))*dt #acceleration at timestep i+1 if collision(p): return H_L H_L.append(H(p, v, m)) return H_L 

Answer: Corrected implementation import numpy as np from numpy.linalg import norm from copy import deepcopy def H(p, v, m): # hamiltonian function # sum of kinetic energy for all bodies T = sum([m[i] * norm(v[i])**2 / 2 for i in range(3)]) # sum of potential energy between all unique pairs V = -sum([m[i] * m[j] / norm(p[i] - p[j]) for i in range(3) for j in range(i+1, 3)]) return T + V def acceleration(p, m): """Compute acceleration for all bodies""" a = [np.zeros(2) for _ in range(3)] for i in range(3): for j in range(3): if i != j: r = p[i] - p[j] a[i] -= m[j] * r / (norm(r)**3) return a def collision(p): for i in range(3): for j in range(i+1, 3): if norm(p[i] - p[j]) < 0.1: return True return False def Leapfrog(P, V, dt, steps, m): p, v = deepcopy(P), deepcopy(V) H_L = [H(p, v, m)] # Initial acceleration a_prev = acceleration(p, m) for _ in range(steps): # Half-step velocity update v_half = [v[i] + 0.5 * a_prev[i] * dt for i in range(3)] # Full-step position update p = [p[i] + v_half[i] * dt for i in range(3)] # Compute new acceleration a_new = acceleration(p, m) # Second half-step velocity update v = [v_half[i] + 0.5 * a_new[i] * dt for i in range(3)] if collision(p): return H_L H_L.append(H(p, v, m)) a_prev = a_new return H_L
Question: I have two numpy arrays a and b such that a.shape[:-1] and b.shape are broadcastable. With this constraint only, I want to calculate an array c according to the following: c = numpy.empty(numpy.broadcast_shapes(a.shape[:-1],b.shape),a.dtype) for i in range(a.shape[-1]): c[...,i] = a[...,i] * b  The above code certainly works, but I would like to know if there is a more elegant (and idiomatic) way of doing it.

Answer: Use np.newaxis with ... to add a new axis after your last axis. c = a * b[..., np.newaxis] Which is the same as c = a * b[np.newaxis, :] You don't need to allocate space for c in advance btw.