eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: is there away to make this code not spam in the second file without interfering with any other functions? "Anomaly.log" is being spammed although "Bandit.log" is not being spammed, what am I doing wrong? import pyautogui import schedule import time import logging # Initialize a counter call_count = 0 # Example calls def logger(): logging.basicConfig(level=logging.INFO, filename="bandit.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) logger.propagate = False handler = logging.FileHandler('anomaly.log') formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) try: location = pyautogui.locateOnScreen('1.png', confidence=0.9) if pyautogui.locateOnScreen('1.png', confidence=0.9): global call_count call_count += 1 logging.info(call_count) except pyautogui.ImageNotFoundException: pass def bandit_cycle(): try: location = pyautogui.locateOnScreen('1.png', confidence=0.9) logging.info('x1 Data found') time.sleep(1.5) except: pass # schedule for main working code schedule.every(1).seconds.do(bandit_main) # schedule for main working code schedule.every(1).seconds.do(logger) while True: schedule.run_pending() time.sleep(1)  Anomaly.log  2025-02-03 14:51:19,773 - main - INFO - 1 2025-02-03 14:51:19,773 - main - INFO - 1 2025-02-03 14:51:19,773 - main - INFO - 1 2025-02-03 14:51:19,773 - main - INFO - 1 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2 2025-02-03 14:51:57,076 - main - INFO - 2  Bandit.log  2025-02-03 14:51:15,548 - INFO - x1 Data found 2025-02-03 14:51:19,773 - INFO - 1 2025-02-03 14:51:52,877 - INFO - x1 Data found 2025-02-03 14:51:57,076 - INFO - 2 2025-02-03 14:52:30,495 - INFO - x5 Data found 

Answer: When you call this function the first time: logger = logging.getLogger(__name__)  It creates a new logger object. But if you call it again within the same program execution, it remembers the logger object you already created and just fetches it, instead of creating a new one. And then each time you call this: logger.addHandler(handler)  It adds another handler to the logger object. So you end up with a logger that has dozens, or even hundreds, of handlers. And messages are logged by each handler on the logger. You should rearrange your logging setup/initialization code so it isn't called multiple times.
Question: I have a simple chatbot that generates SQL queries and uses them on a database. I want to store the output in a .csv file and then download that file. This is usually possible when I take my output from SQL (a list of dicts), create a pd.Dataframe after evaluating that output, and finally download it as a csv file using st.download_button. However, when the result is a Decimal() format from SQL, pd.Dataframe fails and both eval and literal_eval would not work on it (I got invalid object error using literal_eval). I also tried to convert the data using Python's Decimal datatype. from decimal import Decimal But this recognized the output object as string and not a Decimal. So I did some research and found that writerows would work with Decimal type data, but I am still not able to download the file. out = db.run_no_throw(query, include_columns=True) #This is the output returned by the database. According to the [docs][1], this returns a string with the result. print(out, type(out)) # Prints [{A: Decimal(1,2)}] and str (literal_eval(out), literal_eval(str(out)) and literal_eval('"' + out + '"') all gave an invalid object error here)  This is how I am currently trying to download the output data: with open(filename, mode='w+', newline='') as file_to_output: writer = csv.writer(file_to_output, delimiter=",") writer.writerows(out) downloaded = st.download_button( label="Download data as CSV", data=file_to_output, file_name="filename.csv" )  The above code creates a file locally with the expected data, but it prints 1 line per row in the .csv file, like so - [ { A : D e ... However, on the server, the file I download does not even consist of that data (It is a blank file). So the streamlit download button is not getting the data from the file, even though it says here that it should because it is one of str, bytes, or a file. What am I missing here? I would really appreciate any help. Thanks! EDIT - Running eval() on out variable gave the error "Could not recognize Decimal() object"

Answer: pandas .read_sql_query() method can be used to directly create a DataFrame from an SQL query. Then the DataFrame can be written to a CSV file using the .to_csv() method. import pandas as pd import sqlalchemy as sa engine = sa.create_engine("postgresql://scott:tiger@192.168.0.199/test") sql = """\ SELECT 'widget' AS item, CAST(2.99 AS Decimal(18, 4)) AS price UNION ALL SELECT 'gadget' AS item, CAST(9.99 AS Decimal(18, 4)) AS price """ df = pd.read_sql_query(sql, engine, coerce_float=False) print(df) """ item price 0 widget 2.9900 1 gadget 9.9900 """ print(repr(df.loc[0, "price"])) # Decimal('2.9900') df.to_csv("products.csv", header=True, index=False) with open("products.csv", "r") as csv: print(csv.read()) """ item,price widget,2.9900 gadget,9.9900 """
Question: Good morning all I am trying to process a lot of data, and I need to group data, look at the group, then set a value based on the other entries in the group, but I want to set the value in a column in the full dataset. What I can't figure out is how I can use the group to write back to the main dataframe. So as an example, I created this data frame import pandas as pd data = [{ "class": "cat", "name": "Fluffy", "age": 3, "child": "Whiskers", "parents_in_group": "" }, { "class": "dog", "name": "Spot", "age": 5 }, { "class": "cat", "name": "Whiskers", "age": 7 }, { "class": "dog", "name": "Rover", "age": 2, "child": "Spot" }] df = pd.DataFrame(data) df  So as an example, lets say that I want to set the parrents_in_group to a list of all the parrents in the group, easy to do for name, group in group_by_class: mask = group["child"].notna() print("This is the parrent in group") print(group[mask]) parent_name = group[mask]["name"].values[0] print(f"This is the parent name: {parent_name}") group["parents_in_group"] = parent_name print("And now we have the name set in group") print(group)  That updates the group, but not the actual data frame. So how would I go about writing this information back to the main data frame Using the name and search This works, but seems a bit untidy for name, group in group_by_class: mask = group["child"].notna() parent_name = group[mask]["name"].values[0] df.loc[df['class'] == name, 'parents_in_group'] = parent_name df  Using group How would I go about using group to set the values, rather than searching for the name that the group was created by. Or are there better ways to going about it. The real challenge I'm having is that I need to get the group, find some specific values in the group, then set some fields based on the data found. Any help of course welcome.

Answer: A loop-less approach would be to compute a groupby.first after dropna, then to map the output: df['parents_in_group'] = df['class'].map( df.dropna(subset='child').groupby('class')['name'].first() ) # variant df['parents_in_group'] = df['class'].map( df['name'].where(df['child'].notna()).groupby(df['class']).first() )  Or, with drop_duplicates in place of groupby (for efficiency): df['parents_in_group'] = df['class'].map( df.dropna(subset='child') .drop_duplicates(subset='class') .set_index('class')['name'] )  Output:  class name age child parents_in_group 0 cat Fluffy 3 Whiskers Fluffy 1 dog Spot 5 NaN Rover 2 cat Whiskers 7 NaN Fluffy 3 dog Rover 2 Spot Rover  Or, if efficiency doesn't really matter with a groupy.apply: out = (df.groupby('class', sort=False, group_keys=False) .apply(lambda x: x.assign(parents_in_group=x.loc[x['child'].notna(), 'name'] .iloc[:1].squeeze()), include_groups=False) )
Question: I have a dataframe and a series, as follows: import pandas as pd from itertools import permutations df = pd.DataFrame({'a': [['a', 'b', 'c'], ['a', 'c', 'b'], ['c', 'a', 'b']]}) prob = list(permutations(['a', 'b', 'c'])) prob = [list(ele) for ele in prob] ps = pd.Series(prob) >>> df a 0 [a, b, c] 1 [a, c, b] 2 [c, a, b] >>> ps 0 [a, b, c] 1 [a, c, b] 2 [b, a, c] 3 [b, c, a] 4 [c, a, b] 5 [c, b, a] dtype: object  My question is how to add a column 'idx' in df, which contains the index of the value in column 'a' in series 'ps'? The desire result is: a idx [a,b,c] 0 [a,c,b] 1 [c,a,b] 4  The chatgpt gave me a answer, but it works very very slowly when my real data is huge. df['idx'] = df['a'].apply(lambda x: ps[ps.apply(lambda y: y == x)].index[0])  Is there a more efficient way?

Answer: Use DataFrame.merge with DataFrame constructor: #if possible duplicates in ps remove them ps = ps.drop_duplicates() df = df.merge(pd.DataFrame({'idx': ps.index, 'a':ps.values}), on='a') print (df) a idx 0 [a, b, c] 0 1 [a, c, b] 1 2 [c, a, b] 4  Solution for oldier pandas versions - converting lists to tuples before merge: df1 = ps.apply(tuple).reset_index().drop_duplicates(0) print (df1) index 0 0 0 (a, b, c) 1 1 (a, c, b) 2 2 (b, a, c) 3 3 (b, c, a) 4 4 (c, a, b) 5 5 (c, b, a) df = (df.merge(df1, left_on=df['a'].apply(tuple),right_on=df1[0]) .drop(['key_0',0], axis=1)) print (df) a index 0 [a, b, c] 0 1 [a, c, b] 1 2 [c, a, b] 4
Question: Only started learning Python a few days ago. I followed freecodecamp's while loop word guessing game tutorial and wanted to add the statement, "Wrong answer!" after every wrong guess. This was what I initially wrote: secret_word = "giraffe" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = False while guess != secret_word and not (out_of_guesses): if guess_count < guess_limit: guess = input("Enter guess: ") guess_count += 1 print("Wrong answer!") else: out_of_guesses = True if out_of_guesses: print("Out of Guesses, YOU LOSE!") else: print("Correct!")  While it did print after every wrong answer, it ALSO printed after the correct input, which was then followed by the "Correct!" statement. Initially I tried moving print("Wrong answer!") to other lines within the while function and tried adding a break after the if function but nothing seemed to work. It was only when I found this answer where "Wrong answer!" stopped printing after the correct input: secret_word = "giraffe" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = False while guess != secret_word and not (out_of_guesses): if guess_count < guess_limit: guess = input("Enter guess: ") guess_count += 1 if guess == secret_word: print("Correct!") if guess != secret_word: print("Wrong answer!") else: out_of_guesses = True print("Out of Guesses, YOU LOSE!")  So my questions are:  Why do I need to define if guess != secret_word: AGAIN if I already had while guess != secret_word and not (out_of_guesses): in the while loop function?  Why did it keep printing "Wrong answer!" after the correct input in the first code even after adding a break?  

Answer: The while loop (it's not a function!) head is (here) independent from what you do in the loop's body. When the loop condition is true, the whole code in the body runs. If that code prints something unconditionally (i.e., without using an if to check whether it should print), then you get that output. After the body is finished, the loop's condition is evaluated again, and the loop may or may not start over. Stating it differently: the loop head determines if the body should run again, and that check happens when the body is finished. The whole body runs, unless you use break or continue to exit the loop early.
Question: Python 3.13.1 results in unexpected indentation errors when I copy and paste Python code into its windows terminal (as shown in the pic below). This did not happen in Python 3.12.x or earlier. In windows terminal open python 3.13.1, and 3.12.8 separately, and paste the below code for example def fun1(): if 5 > 3: return 'a' return 0  and hit enter.. You should get the below for 3.13.1  and below for 3.12.8 (and versions before)...  (I pasted using a simple right click in windows terminal, and check multiple times the steps are same) has anyone faced this issue, and how did you fix this

Answer: Press F3 before pasting (new in Python 3.13). Press F3 again when done pasting. See What's New in Python 3.13: New Features - A better interactive interpreter.
Question: I have a netCDF4 dataset for the following datatime which is stored in _date_times variable:- <xarray.DataArray 'Time' (Time: 21)> Size: 168B array(['2025-01-30T00:00:00.000000000', '2025-01-30T06:00:00.000000000', '2025-01-30T12:00:00.000000000', '2025-01-30T18:00:00.000000000', '2025-01-31T00:00:00.000000000', '2025-01-31T06:00:00.000000000', '2025-01-31T12:00:00.000000000', '2025-01-31T18:00:00.000000000', '2025-02-01T00:00:00.000000000', '2025-02-01T06:00:00.000000000', '2025-02-01T12:00:00.000000000', '2025-02-01T18:00:00.000000000', '2025-02-02T00:00:00.000000000', '2025-02-02T06:00:00.000000000', '2025-02-02T12:00:00.000000000', '2025-02-02T18:00:00.000000000', '2025-02-03T00:00:00.000000000', '2025-02-03T06:00:00.000000000', '2025-02-03T12:00:00.000000000', '2025-02-03T18:00:00.000000000', '2025-02-04T00:00:00.000000000'], dtype='datetime64[ns]')  The above data is of six hour interval. However, I need to convert the dataset to twelve hourly dataset. The filtered dataset should look like this:- <xarray.DataArray 'Time' (Time: 21)> Size: 168B array(['2025-01-30T00:00:00.000000000', '2025-01-30T12:00:00.000000000', '2025-01-31T00:00:00.000000000', '2025-01-31T12:00:00.000000000', '2025-02-01T00:00:00.000000000', '2025-02-01T12:00:00.000000000', '2025-02-02T00:00:00.000000000', '2025-02-02T12:00:00.000000000', '2025-02-03T00:00:00.000000000', '2025-02-03T12:00:00.000000000', '2025-02-04T00:00:00.000000000'], dtype='datetime64[ns]')  What I tried was:- xr_ds.sel(Time=slice(_date_times[0], _date_times[-1]), freq='12 h')  Off course, it won't work as there is no option to specify freq. How do I slice dataset containing only on specific time interval?

Answer: You don't have to use a slice() to select the times, you can also specify a list or array of times. Here, I used Pandas date_range() for simplicity: import xarray as xr import pandas as pd import numpy as np ds = xr.open_dataset('202001.nc') times = pd.date_range(ds.time[0].values, ds.time[-1].values, freq='12h') dst = ds.sel(time=times)  This results in: In [10]: dst.time Out[10]: <xarray.DataArray 'time' (time: 62)> Size: 496B array(['2020-01-01T00:00:00.000000000', '2020-01-01T12:00:00.000000000', '2020-01-02T00:00:00.000000000', '2020-01-02T12:00:00.000000000',  An alternative is to use ds.isel() with an array of indexes. dst = ds.isel(time=np.arange(0, ds.time.size, 12))  Or you can simply slice the time array from the original dataset, if you really want to avoid Pandas/Numpy: dst = ds.sel(time=ds.time[::12])
Question: I want to read the config file if the value is empty Example: To convert a video file to audio file # config.ini [settings] videofile = video.avi codesplit = -vn outputfile = audio.mp3  Output ['ffmpeg.exe', '-i', 'video.avi', '-vn', 'audio3.mp3']  If you make the value "codesplit" empty, the code will not work. example : To convert a video avi file to video mp4 [settings] videofile = video.avi codesplit = outputfile = videoaudio.mp4  Output ['ffmpeg.exe', '-i', 'video.avi', '', 'videoaudio.mp4']  I want to remove this quote so that the code to works  '',  full code import subprocess import configparser config = configparser.ConfigParser(allow_no_value=True) config.read(r'config.ini') videofile = config.get('settings', 'videofile') outputfile = config.get('settings', 'outputfile') codesplit = config.get('settings', 'codesplit', fallback=None) ffmpeg_path = r"ffmpeg.exe" command = [ f"{ffmpeg_path}", "-i", (videofile), (codesplit), (outputfile),] process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1, universal_newlines=True) process.communicate() print(command) 

Answer: You can try using an if-else statement to check if there is a codesplit option passed from the config file or not. import subprocess import configparser config = configparser.ConfigParser(allow_no_value=True) config.read(r"config.ini") videofile = config.get("settings", "videofile") outputfile = config.get("settings", "outputfile") codesplit = config.get("settings", "codesplit", fallback=None) ffmpeg_path = r"ffmpeg.exe" if codesplit: command = [ f"{ffmpeg_path}", "-i", (videofile), (codesplit), (outputfile), ] else: command = [ f"{ffmpeg_path}", "-i", (videofile), (outputfile), ] process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1, universal_newlines=True, ) process.communicate() print(command)  This code checks if codesplit is there in the config. If so, it adds it to the command list. If not, it doesn't. The output looks like ['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']  where the empty string is absent.
Question: I have a class called "IterQueue which is an iter queue: IterQueue.py from multiprocessing import Process, Queue, Pool import queue class IterQueue(queue.Queue): def __init__(self): self.current = 0 self.end = 10000 def __iter__(self): self.current = 0 self.end = 10000 while True: yield self.get() def __next__(self): if self.current >= self.end: raise StopIteration current = self.current self.current += 1 return current  I put items inside it in a different processes: modulexxx.py ... self.q1= IterQueue() def function(self): x = 1 while True: x = x + 1 self.q1.put(x)  Everything works fine, but python gives me an error: 'IterQueue' object has no attribute 'not_full  I searched for this function so I implement it in my custom queue, but found nothing, What's the solution to this ?

Answer: You inherited from Queue and overrode __init__, but never called its __init__, so that was never run. That means that not_full was never assigned, thus the error. Unless you want to override its maxsize default argument of 0, you just need to change your __init__ to: def __init__(self): super().__init__() self.current = 0 self.end = 10000
Question: I'm trying to make a heating cable layout for floor heating and i got stuck with this problem. This i what i got from my code  So i made a small program asking for width,lenght of the room and calculating the are in m2. And i need a graphical representation of how cable should be on the floor. This is my code for arch: # Add C-shaped curves at the end of each section for i in range(int(width // spacing)): theta = np.linspace(0, np.pi, 100) x_curve = length + (spacing / 2) * np.cos(theta) y_curve = (i + 1) * spacing + (spacing / 2) * np.sin(theta) color = 'blue' if total_length < 5 else 'red' ax.plot(x_curve, y_curve, color=color) total_length += np.pi * (spacing / 2)  I'm a beginner so i tried what i knew so far. If someone can guide me or help me in anyway possible.

Answer: I suspect you want to lay your cable like this. The end arcs are essentially the same (so don't keep re-calculating them). Just shift them up at each crossing and alternate left and right sides.  import numpy as np import matplotlib.pyplot as plt width = 4 spacing = 0.25 N = 17 length = 4 total_length = 0 theta = np.linspace(0, np.pi, 100) xarc = (spacing / 2) * np.sin(theta) yarc = (spacing / 2) * ( 1 - np.cos(theta) ) base = 0 for i in range( N ): color = 'blue' if total_length < 5 else 'red' plt.plot( [0,length], [base,base], color=color) # draw line if i == N - 1: break # no connector at end if i % 2 == 0: x_curve = length + xarc # arc on right else: x_curve = -xarc # arc on left y_curve = base + yarc plt.plot(x_curve, y_curve, color=color) # draw connector base += spacing # update base and length total_length += length + np.pi * (spacing / 2) plt.show()
Question: I'm trying to implement a simple MD simulation in Python (I'm new to this),I'm using LJ potential and force equations along with Verlet method: def LJ_VF(r): #r = distance in Å #Returns V in (eV) and F in (eV/Å) V = 4 * epsilon * ( (sigma/r)**(12) - (sigma/r)**6 ) F = 24 * epsilon * ( 2 * ((sigma**12)/(r**(13))) - ( (sigma**6)/(r**7) )) return V , F def velocity_verlet(x, v, f_old, f_new): #setting m=1 so that a=f x_new = x + v * dt + 0.5 * f_old * dt**2 v_new = v + 0.5 * (f_old + f_new) * dt return x_new, v_new  Now to make sure it works I'm trying to use this on the simplest system, i.e 2 atoms separated by the distance 4 Å: #Constants epsilon = 0.0103 sigma = 3.4 m = 1.0 t0 = 0.0 v0 = 0.0 dt = 0.1 N = 1000 def simulate_two_atoms(p1_x0, p1_v0, p2_x0, p2_v0): p1_x, p2_x = [p1_x0], [p2_x0] p1_v, p2_v = [p1_v0], [p2_v0] p1_F, p1_V, p2_F, p2_V = [], [], [], [] r = abs(p2_x0 - p1_x0) V, F = LJ_VF(r) p1_F.append(F) p1_V.append(V) p2_F.append(-F) p2_V.append(V) for i in range(N - 1): r_new = abs(p2_x[-1] - p1_x[-1]) V_new, F_new = LJ_VF(r_new) x1_new, v1_new = velocity_verlet(p1_x[-1], p1_v[-1], p1_F[-1], F_new) x2_new, v2_new = velocity_verlet(p2_x[-1], p2_v[-1], p2_F[-1], -F_new) p1_x.append(x1_new) p1_v.append(v1_new) p2_x.append(x2_new) p2_v.append(v2_new) p1_F.append(F_new) p2_F.append(-F_new) p1_V.append(V_new) p2_V.append(V_new) return np.array(p1_x), np.array(p1_v), np.array(p2_x), np.array(p2_v) #Initial conditions p1_x0 = 0.0 p1_v0 = 0.0 p2_x0 = 4.0 p2_v0 = 0.0 #Plot p1_x, p1_v, p2_x, p2_v = simulate_two_atoms(p1_x0, p1_v0, p2_x0, p2_v0) time = np.arange(N) plt.plot(time, p1_x, label="Particle 1", color="blue") plt.plot(time, p2_x, label="Particle 2", color="green") plt.xlabel("Time (t)") plt.ylabel("x (Å)") plt.title("Particle Positions Over Time (Bouncing Test)") plt.legend() plt.grid(True) plt.show()  But I'm getting incorrect values and the plot shows that the atoms are not bouncing at all, in contra they are drifting away from each other!  I have been trying to find where I went wrong for a very long time now but not progressing in any level! Thought that a second eye might help! Any tips/advice?

Answer: Your initial force F is negative. You assign that to P1, making it move down and -F (which is positive) to P2, so making P2 move up. That is contrary to the physics. You have just assigned F and -F to the wrong particles (F should go to the one with positive r). Switch them round, both where you update x_new, v_new and the particle forces p1_F and p2_F. With your given potential I think the equilibrium displacement should be 2^(1/6).sigma, or about 3.816 in the units you are using here. Specifically: x1_new, v1_new = velocity_verlet(p1_x[-1], p1_v[-1], p1_F[-1], -F_new) x2_new, v2_new = velocity_verlet(p2_x[-1], p2_v[-1], p2_F[-1], F_new) p1_x.append(x1_new) p1_v.append(v1_new) p2_x.append(x2_new) p2_v.append(v2_new) p1_F.append(-F_new) p2_F.append( F_new)  Gives