eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I would like to apply vocal_start to so many different variables and i can't handle them all and i wouldn't always repeat startswith. So I don't want to write variable1.startswith(("a", "e", "i", "o", "u")), but i would like to apply startswith(("a", "e", "i", " o", "u")) directly to all variables without specifying variable1.startswith (something similar to my code). I wouldn't always repeat startswith I guess using startswith is only like this (variable1.startswith), so is there anything else I can use? I need something like my code, but not with startswith (or I'd like to use startswith differently if possible) vocal_start = startswith(("a", "e", "i", "o", "u")) variable1 = "open" #Only for Test if variable1 == vocal_start: print("correct")  In the code, of course, I get the NameError: name 'startswith' is not defined because I didn't specify variable1.startswith(("a", "e", "i", "o", "u")), but as I said, I don't want to go this route

Answer: You can get your desired form to work if you don't mind writing really obscure code. Write a class whose __eq__ does what you want and then use an instance of that class for your compare. >>> class Startswith: ... def __eq__(self, rhs): ... return rhs.startswith(('a', 'e', 'i', 'o', 'u')) ... >>> startswith = Startswith() >>> "abcd" == startswith True >>> "xxx" == startswith False
Question: When I run the following test script for tensorflow import tensorflow as tf cifar = tf.keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar.load_data() model = tf.keras.applications.ResNet50( include_top=True, weights=None, input_shape=(32, 32, 3), classes=100,) loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"]) model.fit(x_train, y_train, epochs=5, batch_size=4)  I obtain the following terminal output: Metal device set to: Apple M2 Pro systemMemory: 16.00 GB maxCacheSize: 5.33 GB 2023-03-23 00:26:32.203361: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support. 2023-03-23 00:26:32.203521: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>) zsh: bus error python3 app/model/tf_verify.py 

Answer: While unclear from the official Apple documentation, it looks like the tensorflow-macos version should match the tensorflow-metal plugin version from the "Releases" section. Since you are using tensorflow-macos==2.9, you should use tensorflow-metal==0.5.0 and not tensorflow-metal==0.6.0. I was able to reproduce and solve the issue on a MacBook Pro M1 Pro and the training works fine.
Question: I have multiple Boolean variables in my code and now they are defined like this: self.paused, self.show_difficulty, self.resizable, self.high_score_saved, \ self.show_high_scores, self.show_game_modes = \ False, False, True, False, False, False  And I thought of refactoring the code like this to improve readability. ui_options = { "paused": False, "show_difficulty": False, "resizable": False, "high_score_saved": False, "show_high_scores": False, "show_game_modes": False }  Is there a better way to do it?

Answer: To group values like this, I'd recommend using a dataclass over a dictionary. This means you can still have named attributes rather than needing to do string lookups. This will allow auto-completes and static analyzers to work with your code. from dataclasses import dataclass @dataclass class UIOptions: paused: bool = False show_difficulty: bool = False resizable: bool = False high_score_saved: bool = False show_high_scores: bool = False show_game_modes: bool = False ui_options = UIOptions()
Question: I have the following dataframe in pandas: data = { 'idx': [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10], 'hue_val': ["A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C",], 'value': np.random.rand(30), } df = pd.DataFrame(data)  Now I want to have a line plot with the cumulative sum over the value by following the "idx" for each "hue_val". So in the end it would be three curves going strictly up (since they are positive numbers), one for "A", "B" and "C". I found this code in several sources: sns.lineplot(x="idx", y="value", hue="hue_val", data=df, estimator="cumsum")  That is not doing the trick, since both the curve and the x-axis are false: 

Answer: Given OPs dataframe import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt data = { 'idx': [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10], 'hue_val': ["A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C",], 'value': np.random.rand(30) } df = pd.DataFrame(data)  There are two things one needs to do:  Calculate the cum sum for each hue_val  Plot it    1. Calculate the cum sum for each hue_val In order to calculate the cumulative sum, one can use pandas.DataFrame.groupby and pandas.Series.cumsum. As per OP's request, use a variable column as a way to select the column one wants to consider as follows column = 'value' df['cum_sum'] = df.groupby('hue_val')[column].cumsum()  As one is using Numpy to generate some dataframe values, one can also use it to calculate the cum sum with pandas.DataFrame.apply and numpy.cumsum as follows df['cum_sum'] = df.groupby('hue_val')[column].apply(lambda x: np.cumsum(x))   2. Plot it Then one can plot it with seaborn.lineplot as follows sns.lineplot(data=df, x='idx', y='cum_sum', hue='hue_val')    Notes:  Having a variable to specify the column, makes it more user friendly if the dataframe has more columns, as the one below, (one of OP's concern), as one will simply change the variable to, let's say value3 data = { 'idx': [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10], 'hue_val': ["A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C",], 'value': np.random.rand(30), 'value1': np.random.rand(30), 'value2': np.random.rand(30), 'value3': np.random.rand(30), } df = pd.DataFrame(data) column = 'value3' df['cum_sum'] = df.groupby('hue_val')[column].cumsum() sns.lineplot(data=df, x='idx', y='cum_sum', hue='hue_val')    There are strong opinions on using .apply(). Would recommend reading this: When should I (not) want to use pandas apply() in my code?
Question: In my project, all pydantic models inherit from a custom "base model" called GeneralModel. This enables to configure the same behavior for the entire project in one place. Let's assume the following implementation: from pydantic import BaseModel class GeneralModel(BaseModel): class Config: use_enum_values = True exclude_none = True  One particularly desired behavior is to perform a validation on all fields of a specific type. Any ideas how to achieve this using my GeneralModel? Different approaches are blessed as well.

Answer: Quote from the Pydantic validators documentation:  a single validator can also be called on all fields by passing the special value '*'  and:  you can also add any subset of the following arguments to the signature (the names must match): [...]  field: the field being validated. Type of object is pydantic.fields.ModelField.   So you can write a catch-all validator and pass it the ModleField instance as an argument. Then check the type of the field and apply whatever validation you want. Very simple example: from typing import Any from pydantic import BaseModel, validator from pydantic.fields import ModelField class GeneralModel(BaseModel): @validator("*") def ensure_non_negative(cls, v: Any, field: ModelField) -> Any: if field.type_ is float and v < 0: raise ValueError(f"`{field.name}` value must not be negative") return v class Config: ...  Usage: from pydantic import ValidationError # ... import GeneralModel class ConcreteModel(GeneralModel): x: float y: str = "foo" print(ConcreteModel(x=3.14)) try: ConcreteModel(x=-1) except ValidationError as err: print(err.json(indent=4))  Output: x=3.14 y='foo'  [ { "loc": [ "x" ], "msg": "`x` value must not be negative", "type": "value_error" } ]  Note: When defining a field as for example list[str], Pydantic internally considers the field type to be str, but its shape to be pydantic.fields.SHAPE_LIST. Luckily, shape is also a public attribute of ModelField. See this answer for an example, where this is important.
Question: I am studying the FFT algorithm for fast polynomial multiplication. We went over the algorithm and I decided to try and implement it in Python. from typing import List import numpy as np def fft(p: List[int]) -> List[int]: n = len(p) if n == 1: return p unity_root = np.exp(2j * np.pi / n) p_even = p[::2] p_odd = p[1::2] y_even = fft(p_even) y_odd = fft(p_odd) y = [0] * n for j in range(n // 2): omega = np.power(unity_root, j) y[j] = y_even[j] + omega * y_odd[j] y[n // 2 + j] = y_even[j] - omega * y_odd[j] return y def ifft(p: List[int]) -> List[int]: n = len(p) if n == 1: return p unity_root = (1 / n) * np.exp(-2j * np.pi / n) p_even = p[::2] p_odd = p[1::2] y_even = ifft(p_even) y_odd = ifft(p_odd) y = [0] * n for j in range(n // 2): omega = np.power(unity_root, j) y[j] = y_even[j] + omega * y_odd[j] y[n // 2 + j] = y_even[j] - omega * y_odd[j] return y  I tried running the following code to make sure it works print(ifft(fft([1, 2, 3, 4])))  I expected the output to be the original list I started with as that list represents the coefficients, yet I am getting (ignoring precision issues with floating point arithmetic): [(4+0j), (11-0j), (12+0j), (13+0j)]  My question is: Shouldn't I be getting the original list? If I should be getting the original list, where is the mistake in the code? as I went over the code several times and I am having issues finding it. If I shouldn't be getting the original list back and my code is correct, what am I actually getting?

Answer: The problem is that, in ifft, you're dividing the root of unity by n. You need to divide the final result instead.
Question: I am plotting data in a seaborn barplot. I want to label something from my pandas dataframe into the bar. I have gotten the labeling part figured out (see code to replicate below), but I still want to convert it to scientific notation. import pandas as pd d = {'name': ['experiment1','experiment2'], 'reads': [15000,12000], 'positiveEvents': [40,60]} df = pd.DataFrame(d) df['proportianPositive'] = df['positiveEvents']/df['reads'] p = sns.barplot(data=df,x='name',y='positiveEvents', palette = 'colorblind', alpha =0.8) p.bar_label(p.containers[0],labels=df.proportianPositive, padding = -50, rotation=90) plt.show()  Result:  How do I convert df.proportianPositive to scientific notation so that it will show up on my barplot?

Answer:  As already stated, labels= supersedes fmt=, so they may not be used together. p.containers[0] → <BarContainer object of 2 artists>, which are the properties describing the bars.  If using the default height (vertical bars), or width (horizontal bars), then use the fmt='%.3E' If using custom labels, the formatting must be applied to the object passed to labels=  In the case of a pandas.Series: labels = df['proportianPositive'].map(lambda v: f'{v:.3E}'). See Difference between map, applymap and apply. For a list-comprehension: labels = [f'{v:.3E}' for v in df.proportianPositive] Both options use modern f-string formatting     See How to add value labels on a bar chart for additional details, and examples, using matplotlib.pyplot.bar_label.  fmt= ax = sns.barplot(data=df, x='name', y='positiveEvents', palette='colorblind', alpha=0.8) xa.bar_label(ax.containers[0], fmt='%.3E')   labels= ax = sns.barplot(data=df, x='name', y='positiveEvents', palette='colorblind', alpha=0.8) labels = df['proportianPositive'].map(lambda v: f'{v:.3E}') # pandas.Series # labels = [f'{v:.3E}' for v in df.proportianPositive] # list comprehension ax.bar_label(ax.containers[0], labels=labels)
Question: I'm trying to use 1inch oracle methods from here. This is python wrapper around 1inch apis. I want to know token price from some oracle so i'm using oracle method "get_rate_to_ETH". But in a result I have this exception: Traceback (most recent call last): File "D:\projects\1inchArb\1inch_api.py", line 20, in <module> price_usdc = oracle.get_rate_to_ETH("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", src_token_decimal=6) File "D:\projects\1inchArb\venv\lib\site-packages\oneinch_py\main.py", line 376, in get_rate_to_ETH rate = self.oracle_contract.functions.getRateToEth(self.w3.toChecksumAddress(src_token), wrap).call() AttributeError: 'Web3' object has no attribute 'toChecksumAddress'  My code: from oneinch_py import OneInchOracle public_key = "my_wallet_addr" rpc_url = "https://cloudflare-eth.com" oracle = OneInchOracle(rpc_url, chain='ethereum') price_usdc = oracle.get_rate_to_ETH("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", src_token_decimal=6)  Also I tried to use different RPC urls such as "https://mainnet.infura.io/v3/{}".format(my_infura_key) but it gives same error. If anyone knows how to help me it would be very grateful...

Answer: Update to latests version оf 1inch.py - 1.9, released 20 March 2023 and also web3 to use version 6.0.0. 1inch.py is using web3 as dependency. web3 removed CamelCase naming in latest version 6.0.0 1inch.py released new version to change their code and migrate to using web3, version 6.0.0.
Question: Consider the following model: from pydantic import BaseModel class Cirle(BaseModel): radius: int pi = 3.14  If I run the following code, I can see the fields of this model: print(Circle.__fields__) # Output: { 'radius': ModelField(name='radius', type=int, required=True), 'pi': ModelField(name='pi', type=float, required=False, default=3.14) }  The question is: how can I get the type (or inferred type hint) from the ModelField type? These all give errors: Circle.__fields__['pi'].type # AttributeError: 'ModelField' object has no attribute 'type' Circle.__fields__['pi'].__dict__ # AttributeError: 'ModelField' object has no attribute '__dict__' type(Circle.__fields__['pi']) # <class 'pydantic.fields.ModelField'> import typing typing.get_type_hints(Circle.__fields__['pi']) # TypeError: ModelField(name='pi', type=float, required=False, default=3.14) # is not a module, class, method, or function. typing.get_type_hints(Circle) # This does not include the "pi" field because it has no type hints # {'radius': <class 'int'>}  I don't even see where the ModelField type is defined in github. How can I iterate over the fields of a pydantic model and find the types?

Answer: You can use the type_ variable of the pydantic fields. The variable is masked with an underscore to prevent collision with the Python internal type keyword. from pydantic import BaseModel class Cirle(BaseModel): radius: int pi = 3.14 for key, value in Cirle.__fields__.items(): print(key, value.type_) # Output: # radius <class 'int'> # pi <class 'float'>
Question: I have a data set with a column filled with 1s and 0s as such: column 1: 1 1 1 1 0 0 0 1 1 0 1 1  and I am currently using np.where() to create a new column that segments the data from column 1. The issue is, whenever it comes across a new segments of 1s I want the values in column 2 to increase by 1. n = 1 df['column2'] = np.where(df['column1'] == 1 , n + 1 , 0)  The results I am getting: column 1: column 2: 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1  what I am trying to achieve column 1: column 2: 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 2 1 2 0 0 1 3 1 3 

Answer: Cheeky one-liner: df["column2"] = df['column1'].diff().ne(0).cumsum().add(1).floordiv(2).where(df['column1'].astype(bool), other=0)  df:  column1 column2 0 1 1 1 1 1 2 1 1 3 1 1 4 0 0 5 0 0 6 0 0 7 1 2 8 1 2 9 0 0 10 1 3 11 1 3
Question: This is the code example: class ToplevelWindow(customtkinter.CTkToplevel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.geometry("400x300") self.title("New Window") self.resizable(False, False) self.iconbitmap('icon.ico') self.after(100, self.lift) class App(customtkinter.CTk): def __init__(self): super().__init__() self.geometry("550x150") self.title("Title") self.resizable(False, False) logo_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_images") self.logo = customtkinter.CTkImage( Image.open(os.path.join(logo_path, "MCB.png")), size=(90, 54)) self.labelimage = customtkinter.CTkLabel(master=self, text=None, image=self.logo) self.labelimage.place(relx=0, rely=0, anchor=tkinter.NW) self.iconbitmap('icon.ico') self.button = customtkinter.CTkButton(master=self, text="Close", command=self.close) self.button.place(relx=0.675, rely=0.8, anchor=tkinter.CENTER) self.button2 = customtkinter.CTkButton(master=self, text="New Window", command=self.open_toplevel) self.button2.place(relx=0.325, rely=0.80, anchor=tkinter.CENTER) self.toplevel_window = None def close(self): "nothing here yet" def open_toplevel(self): if self.toplevel_window is None or not self.toplevel_window.winfo_exists(): self.toplevel_window = ToplevelWindow(self) # create window if its None or destroyed else: self.toplevel_window.focus() # if window exists focus it  When i use the iconbitmap on the main window it works fine. But when is used at the toplevel class, it does nothing. The icon still appears as the default version. What i'm missing ? How can i make this right or is it even possible ?

Answer: This is a bug of customtkinter. If you look into the source code of CTkToplevel, there is below code inside __init__(): class CTkToplevel(tkinter.Toplevel, ...): def __init__(self, ...): ... try: # Set Windows titlebar icon if sys.platform.startswith("win"): customtkinter_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) self.after(200, lambda: self.iconbitmap(os.path.join(customtkinter_directory, "assets", "icons", "CustomTkinter_icon_Windows.ico"))) except Exception: pass ...  So an after job is scheduled to change the window icon after 200ms which overrides your call of self.iconbitmap('icon.ico'). The above code block does not exist in the source code of CTk class, so the main window shows the icon you want. To overcome it, you can delay your call of self.iconbitmap(...) using .after() as well: class ToplevelWindow(...): def __init__(...): ... self.after(250, lambda: self.iconbitmap('icon.ico')) ...
Question: I have the following dataframe import pandas as pd df = pd.DataFrame({'Original': [92,93,94,95,100,101,102], 'Sub_90': [99,98,99,100,102,101,np.nan], 'Sub_80': [99,98,99,100,102,np.nan,np.nan], 'Gen_90': [99,98,99,100,102,101,101], 'Gen_80': [99,98,99,100,102,101,100]})  I would like to create the following dictionary { 'Gen_90': 'Original', 'Sub_90': 'Gen_90', 'Gen_80': 'Original', 'Sub_80': 'Gen_80', }  using regex (because at my original data I also have Gen_70, Gen_60, ... , Gen_10 and Sub_70, Sub_60, ... , Sub_10) So I would like to create pairs of Sub and Gen for the same _number and also pairs or the Original with the Gens How could I do that ?

Answer: You can do: gen_cols = df.filter(like='Gen_').columns sub_cols = df.filter(like='Sub_').columns d = dict(zip(sorted(sub_cols), sorted(gen_cols))) d.update({g : 'Original' for g in gen_cols}) print(d) {'Sub_80': 'Gen_80', 'Sub_90': 'Gen_90', 'Gen_90': 'Original', 'Gen_80': 'Original'}
Question: PEP-562 introduced __getattr__ for modules. While testing I noticed this magic method is called twice when called in this form: from X import Y. file_b.py: def __getattr__(name): print("__getattr__ called:", name)  file_a.py: from file_b import foo, bar  output: __getattr__ called: __path__ __getattr__ called: foo __getattr__ called: bar __getattr__ called: foo __getattr__ called: bar  I run it with: python file_a.py. The interpreter version is: 3.10.6 Could you please let me know the reason behind this?

Answer: Because your __getattr__ returns None for __path__ instead of raising an AttributeError, the import machinery thinks your file_b is a package. None isn't actually a valid __path__, but all __import__ checks here is hasattr(module, '__path__'). It doesn't check the value: elif hasattr(module, '__path__'): return _handle_fromlist(module, fromlist, _gcd_import)  Because the import machinery thinks your file_b is a package, it calls _handle_fromlist, the function responsible for loading package submodules specified in a from import. _handle_fromlist will go through all the names you specified, and for each one, it will call hasattr to check whether an attribute with that name exists on file_b. If an attribute is not found, _handle_fromlist will attempt to import a submodule with the given name: for x in fromlist: if not isinstance(x, str): if recursive: where = module.__name__ + '.__all__' else: where = "``from list''" raise TypeError(f"Item in {where} must be str, " f"not {type(x).__name__}") elif x == '*': if not recursive and hasattr(module, '__all__'): _handle_fromlist(module, module.__all__, import_, recursive=True) elif not hasattr(module, x): from_name = '{}.{}'.format(module.__name__, x) try: _call_with_frames_removed(import_, from_name) except ModuleNotFoundError as exc: # Backwards-compatibility dictates we ignore failed # imports triggered by fromlist for modules that don't # exist. if (exc.name == from_name and sys.modules.get(from_name, _NEEDS_LOADING) is not None): continue raise  This is where the first __getattr__ calls come from: hasattr attempts to get the foo and bar attributes on file_b, triggering your __getattr__. The second __getattr__ calls are just the calls you would expect, retrieving the foo and bar attributes you said to import so the import statement can assign them to names in the namespace the import was executed in.