eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am debugging one of my programs where I am trying to assign a custom variable to a rectangle to update it's position. Here is my code: import os ; os.environ['PYGAME_HIDE_SUPPORT_PROMPT']='False' import pygame, random pygame.init() display = pygame.display.set_mode((401, 401)) display.fill("white") ; pygame.display.flip() class MyRect(pygame.Rect): def __setattr__(self, attr, value): # Sets a custom attribute to a rectangle super().__setattr__(attr, value) if attr == 'xValue': pygame.Rect.move(self, (value-self.centerx), 0) # Move the rectangle according to the xValue def contains(self, coords): return self.collidepoint(coords) square = MyRect(175, 175, 50, 50) pygame.draw.rect(display, 'steelBlue', square) # Draw the rectangle to the screen square.xValue = 200 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() ; exit() elif event.type == pygame.MOUSEBUTTONDOWN: if square.contains(pygame.mouse.get_pos()): square.xValue = random.randint(0, display.get_width()) # Update the square.xValue property pygame.display.flip() # Update the screen  When I execute the program, the square.xValue property is changing, but the position of the square on the screen is not. What did I miss?

Answer: You have to redraw the scene. You have to redraw the rectangle after it has been changed. Clear the display and draw the rectangle in the application loop: while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() ; exit() elif event.type == pygame.MOUSEBUTTONDOWN: if square.contains(pygame.mouse.get_pos()): square.xValue = random.randint(0, display.get_width()) # Update the square.xValue property display.fill("white"); pygame.draw.rect(display, 'steelBlue', square) pygame.display.flip() # Update the screen
Question: I want to deserialize some data into a union type like so: from pydantic import BaseModel, Field from typing import Annotated, Union, Literal class Foo(BaseModel): type: Literal["foo"] = "foo" x: int class Bar(BaseModel): type: Literal["bar"] = "bar" x: float Baz = Annotated[Union[Foo, Bar], Field(discriminator="type")] d = { "type": "bar", "x": 10.1 } # Deserialize Baz here  This could be done by making another base model and deserializing it there, but I was wondering if there was a way to use a function within Pydantic to do it without this? class Config(BaseModel): baz: Baz model = Config.model_validate({"baz": d}) print(model) # Outputs: baz=Bar(type='bar', x=10.1) 

Answer: You can use pydantic.TypeAdapter from pydantic import TypeAdapter model = TypeAdapter(Baz).validate_python(d)
Question: I'm creating a line chart using Altair. I have a DataFrame where my y-values move up and down around 0, and I'd like to add a phat line to mark y=0. Sounds easy enough, so I tried this: # Add a horizontal line at y=0 to clearly distinguish between positive and negative values. y_zero = alt.Chart().mark_rule().encode( y=alt.value(0), color=alt.value('black'), size=alt.value(10), )  This indeed draws a horizontal line, but it gets drawn at the top very top of my chart. It seems Altair uses a coordinate system where (0,0) is at the top-left corner. How do I move my line to my data's y=0 position? Thanks!

Answer: Using alt.datum instead of alt.value will draw the line at the data value 0, instead at a pixel value of 0. You can read more and see examples in the docs here https://altair-viz.github.io/user_guide/encodings/index.html#datum-and-value
Question: I've been reading and watching videos a lot about asyncio in python, but there's something I can't wrap my head around. I understand that asyncio is great when dealing with databases or http requests, because database management systems and http servers can handle multiple rapid requests, but... How can it be good for files, writing in particular.. Let's discuss this case with this code that is running on a server as fastAPI webApp.  @app.get('/end_point') async function () do_something() await write_to_a_file do_more_stuff_and_return  Now, as a server, it may get like 50, 100, or even more rapid requests that will trigger this function, yes await will pause the next instruction inside function() , but if there are multiple requests coming this function will start so many times and execute multiple write attempt at once to the file, which may corrupt the data inside. That's why I can't really understand how it is recommended to use asyncio for files. Am I getting something wrong here? Is there a way to instruct the event_loop to give this higher priority to that function event or some another way to avoid this? What I'm doing now is delaying writing files until I have X amount of data to write [write in bursts], but I still think that any function that does interaction with files shouldn't be async function. But then, I don't know how am I going to deal with tons of requests.... Your thoughts will be highly appreciated as I'm having a hard time with this. thanks, have a good day. I'm trying to write data to file, but the server will surly receive bursts of requests. So in a way I don't know how to handle this situation, and I fear the corruption of data.

Answer:  I'm trying to write data to file, but the server will surly receive bursts of requests. So in a way I don't know how to handle this situation, and I fear the corruption of data.  Yes, corruption could happen from time to time especially if you use aiofiles. If I'm not wrong it uses threads to achieve this. I think you have two options here:  Using lock and background tasks: in FastApi(since you mentioned) you can do the writing in the background (after sending the respond to the user), and there, acquire a lock object for writing to the file.  Queue the messages for writing: instead of directly writing to the file, put them on a queue and let another task/process get do the writing part.   In both cases user won't wait for writing to be finished and get the response immediately and the writing part will happen in sync.
Question: My DataFrame is: import pandas as pd df = pd.DataFrame( { 'a': [20, 100], 'b': [2, 3], 'dir': ['long', 'short'] } )  Expected output: Creating column x:  a b dir x 0 20 2 long [22, 24, 26] 1 100 3 short [97, 94, 91]  Steps: x is a list with length of 3. a is starting point of x and b is step that a increases/decreases depending on dir. If df.dir == long x ascends otherwise it descends. My Attempt based on this answer: df['x'] = np.arange(0, 3) * df.b + df.a  Which does not produce the expected output.

Answer: If you want a vectorial approach with numpy # create 1 for long, else -1 d = np.where(df['dir'].eq('long'), 1, -1)[:,None] # convert a and b to numpy as a column vector a = df['a'].to_numpy()[:,None] b = df['b'].to_numpy()[:,None] # combine N = 3 x = np.arange(1, N+1) df['x'] = list(a + d*b*x)  Output:  a b dir x 0 20 2 long [22, 24, 26] 1 100 3 short [97, 94, 91]
Question: I have comfortably been using @pytest.mark.parametrize() in many tests now. I have not, however, succeeded in combining it with @pytest.fixture() and I cannot find a question answering this issue. This example applies @pytest.fixture() succesfully (copied from another question that I cannot find anymore): import pytest def foo(data, key): return data[key] @pytest.fixture() def dict_used_by_many_tests(): return {"name": "Dionysia", "age": 28, "location": "Athens"} def test_foo_one(dict_used_by_many_tests): actual = foo(dict_used_by_many_tests, "name") expected = "Dionysia" assert actual == expected  Now, in practice I want to use @pytest.mark.parametrize(). @pytest.fixture() def dict_used_by_many_tests(): return {"name": "Dionysia", "age": 28, "location": "Athens"} @pytest.mark.parametrize( "dict_used_by_many_tests", [ (dict_used_by_many_tests), ], ) def test_foo_one(dict_used_by_many_tests): actual = foo(dict_used_by_many_tests, "name") expected = "Dionysia" assert actual == expected  This results in the error: TypeError: 'function' object is not subscriptable. I tried calling dict_used_by_many_tests() to work with its return value instead of the function object. This resulted in a Fixtures are not meant to be called directly error, however.

Answer: One method is request.getfixturevalue: import pytest @pytest.fixture() def dict_used_by_many_tests(): return { "name": "Dionysia", "age": 28, "location": "Athens", } @pytest.fixture() def dict_used_by_some_tests(): return { "name": "Medusa, maybe?", "age": 2 << 32, "location": "Underworld?", } @pytest.mark.parametrize( "fixture_name", [ "dict_used_by_many_tests", "dict_used_by_some_tests", ], ) def test_foo_one(request, fixture_name): d = request.getfixturevalue(fixture_name) # ...  Another is indirect parametrization: import pytest @pytest.fixture() def character_dict(request): if request.param == 1: return {"name": "Dionysia", "age": 28, "location": "Athens"} elif request.param == 2: return {"name": "Medusa", "age": 2 << 32, "location": "Underworld"} raise NotImplementedError(f"No such dict: {request.param!r}") @pytest.mark.parametrize( "character_dict", [1, 2], indirect=True, ) def test_foo_one(character_dict): assert isinstance(character_dict, dict)
Question: I have 2 dataframes called df and df2 ,a small example of both are shown below. I want to join the two into a combined dataframe by matching the entries of the dataFrame by the 'formula' column df2 with the 'filename' column of df1 since the order of entries in the 2 dataFrames are not the same. An example of the final ideal dataframe is also provided below. df = pd.DataFrame({'filename': ['Fe12Co4BN.cif', 'Fe16N2.cif', 'Fe15CoBN.cif'], 'jml_bp_mult_atom_rad': [170.11, 154.73, 172.43], 'jml_hfus_add_bp': [45.13, 41.90, 47.23]}) df2 = pd.DataFrame({'formula': ['Fe12Co4BN','Fe15CoBN','Fe16N2'], 'nsites': [8, 8, 8], 'number': [139, 139, 140]})  Combined dataFrame should look like this: df3 = pd.DataFrame({'formula': ['Fe12Co4BN','Fe15CoBN','Fe16N2'], 'jml_bp_mult_atom_rad': [170.11, 172.43, 154.73], 'jml_hfus_add_bp': [45.13, 47.23, 41.90], 'nsites': [8, 8, 8], 'number': [139, 139, 140]})  The order of of the columns matters to me, so the 'formula' column should be first followed by the columns in df1 and then df2. thanks in advance.

Answer: Create a temporary column in first dataframe df and use merge: out = df2.merge( df.assign(formula=df["filename"].str.split(".").str[0]), on="formula" ).drop(columns="filename") print(out)  Prints:  formula nsites number jml_bp_mult_atom_rad jml_hfus_add_bp 0 Fe12Co4BN 8 139 170.11 45.13 1 Fe15CoBN 8 139 172.43 47.23 2 Fe16N2 8 140 154.73 41.90
Question: I have an n by n matrix of integers and I want to find the circular area, with origin at the top left corner, with maximum sum. Consider the following grid with a circle imposed on it.  This is made with: import matplotlib.pyplot as plt from matplotlib.patches import Circle import numpy as np plt.yticks(np.arange(0, 10.01, 1)) plt.xticks(np.arange(0, 10.01, 1)) plt.xlim(0,9) plt.ylim(0,9) plt.gca().invert_yaxis() # Set aspect ratio to be equal plt.gca().set_aspect('equal', adjustable='box') plt.grid() np.random.seed(40) square = np.empty((10, 10), dtype=np.int_) for x in np.arange(0, 10, 1): for y in np.arange(0, 10, 1): plt.scatter(x, y, color='blue', s=2, zorder=2, clip_on=False) for x in np.arange(0, 10, 1): for y in np.arange(0, 10, 1): value = np.random.randint(-3, 4) square[int(x), int(y)] = value plt.text(x-0.2, y-0.2, str(value), ha='center', va='center', fontsize=8, color='black') r1 = 3 circle1 = Circle((0, 0), r1, color="blue", alpha=0.5, ec='k', lw=1) plt.gca().add_patch(circle1)  In this case the matrix is: [[ 3 0 2 -3 -3 -1 -2 1 -1 0] [-1 0 0 0 -2 -3 -2 2 -2 -3] [ 1 3 3 1 1 -3 -1 -1 3 0] [ 0 0 -2 0 2 1 2 2 -1 -1] [-1 0 3 1 1 3 -2 0 0 -1] [-1 -1 1 2 -3 -2 1 -2 0 0] [-3 2 2 3 -2 0 -1 -1 3 -2] [-2 0 2 1 2 2 1 -1 -3 -3] [-2 -2 1 -3 -2 -1 3 2 3 -3] [ 2 3 1 -1 0 1 -1 3 -2 -1]]  When the circle has radius 3, there are 11 points in the grid within the circle. As the radius increases, more and more points fall into the circle. I am looking for a fast way to find a radius which maximizes the sum of the integers of grid points within it. The radius will not be unique so any one that maximizes the sum is ok. I will ultimately want to do this with much larger matrices. This question is related but I am not sure how to extend it to my new question.

Answer: Same O(n^2 log n) method as others: accumulate weight sums by distance, then compute cumulative sums. But in pure python which should be trivial to translate to C++. Also PyPy may run surprisingly fast. from collections import Counter g = [[-3, 2, 2], [ 2, 0, 3], [-1, -2, 0]] n = 3 sum_dist = Counter() for i in range(n): for j in range(n): dist = i**2 + j**2 sum_dist[dist] += g[i][j] sorted_dists = sorted(sum_dist.keys()) for i in range(1, len(sorted_dists)): sum_dist[sorted_dists[i]] += sum_dist[sorted_dists[i-1]] print(sum_dist) print(max(sum_dist, key=sum_dist.get))
Question: I have created a type alias for defining member variables in a dataclass through type annotations: >>> from typing import TypeAlias >>> Number: TypeAlias = int | float >>> n, x = 1, 2.5 >>> isinstance(n, Number) True >>> isinstance(x, Number) True  According to the docs, this syntax has been deprecated:  Deprecated since version 3.12: TypeAlias is deprecated in favor of the type statement, which creates instances of TypeAliasType and which natively supports forward references. Note that while TypeAlias and TypeAliasType serve similar purposes and have similar names, they are distinct and the latter is not the type of the former. Removal of TypeAlias is not currently planned, but users are encouraged to migrate to type statements.  I tried to use the new syntax, but I got an error: >>> type Number = int | float >>> isinstance(n, Number) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union  How should I go about this?

Answer: If you need to use isinstance with the new syntax, you need to access the type alias's value (and force its evaluation) explicitly, through the __value__ attribute: isinstance(n, Number.__value__)  Of course, this will only work if the value of the alias is compatible with isinstance, which rules out stuff like NewType or generics, or... most of the things you'd want to create a type alias for, but int | float should be fine. The old syntax had these limits too.
Question: I have a big csv file of data delimited by ";" stored in one only huge row. The first 9 fields separated by a semicolon are the columns name of the entire set. With Python, how could I reformat this csv file rewriting it and adding a newline every 9 fields for a correct import in Excel or Calc? Should I import csv or pandas? Thanks a lot in advance.

Answer: Here's a quick regex hack that could do the job. This assumes that there are no quoted semicolons in the values: import re csv = "a;b;c;d;e;f;g;h;i;1;2;3;4;5;6;7;8;9;1;2;3;4;5;6;7;8;9;" wrapped = re.sub(r"((?:[^;]*;){9})", r"\1\n", csv) print(wrapped)  This outputs: a;b;c;d;e;f;g;h;i; 1;2;3;4;5;6;7;8;9; 1;2;3;4;5;6;7;8;9;
Question: I want to create an array in which the values depends on one variables and two coefficients. The coefficient values depend on the variable value as well. A simple example: x_intervals = [2, 10] C1_values = [0.1, 0.5, -0.2] C2_values = [0.4,0.6, -0.8]  Here I want C1 and C2:  to be equal to the first array element if x<2 (so C1 = 0.1 and C2 = 0.4) to be equal to the second array element if x>2 and x>10 (so C1 = 0.5 and C2 = 0.6) to be equal to the third element if x>10 (so C1 = -0.2 and C2 = -0.8)  Then I want to use these coefficients to find the values of y=C1*x^C2 for any number of x contained in a list. Currently I do this using subsequent if functions as: x = [0.1,1,1.5,2.2,5,9,12,20,30,60,70,100] y = [] for x_ in x: if x_<=x_intervals[0]: C1 = C1_values[0] C2 = C2_values[0] elif x_<=x_intervals[1]: C1 = C1_values[1] C2 = C2_values[1] else: C1 = C1_values[2] C2 = C2_values[2] y.append(C1*x_ **C2)  This works but it is extremely dirty and manual, and if I increase the number of intervals it becomes extremely long to code. I am sure that there is a smarter and cleaner way to do this. Any idea? Thanks in advance (Using Python, but this is more of a logic/algorithm question) #################################################################### EDIT: Thank you all for your solutions. They all worked and are useful I found the numpy solution using searchsorted the most appropriate in my case. The code looks like this now (where Coeff_1 and Coeff_2 are two pandas dataframe where I store my coefficients C1 and C2): def calculate_curve_value(Re=10, df=pd.DataFrame()): MaxRe = df.loc["MaxRe"].values C1 = df.loc["C1"].values C2 = df.loc["C2"].values idx = np.searchsorted(MaxRe, Re) value = C1[idx]*Re**(C2[idx]) return value Re = [0.1,1,1.5,2.2,5,9,12,20,30,60,70,100] N = [] f = [] for i in Re: N.append(calculate_curve_value(Re=i, df=Coeff_1)) f.append(calculate_curve_value(Re=i, df=Coeff_2)) 

Answer: It would make a lot of sense to use numpy here. What you want is a searchsorted on x_intervals, then a vectorial operation: import numpy as np x_intervals = np.array([2, 10]) C1_values = np.array([0.1, 0.5, -0.2]) C2_values = np.array([0.4,0.6, -0.8]) x = [0.1,1,1.5,2.2,5,9,12,20,30,60,70,100] idx = np.searchsorted(x_intervals, x) y = C1_values[idx]*x**C2_values[idx]  Output: array([ 0.03981072, 0.1 , 0.1176079 , 0.80246041, 1.3132639 , 1.86859641, -0.02739586, -0.01820564, -0.01316234, -0.00755978, -0.00668269, -0.00502377])  If you really want to use pure python, go with bisect: from bisect import bisect_left x_intervals = [2, 10] C1_values = [0.1, 0.5, -0.2] C2_values = [0.4,0.6, -0.8] x = [0.1,1,1.5,2.2,5,9,12,20,30,60,70,100] y = [] for x_ in x: idx = bisect_left(x_intervals, x_) y.append(C1_values[idx]*x_**C2_values[idx])  Output: [0.03981071705534973, 0.1, 0.11760790225246737, 0.8024604053351734, 1.3132639022018835, 1.8685964094232759, -0.027395863825287095, -0.0182056420302608, -0.013162336572232132, -0.007559777184220181, -0.006682693821222914, -0.005023772863019159]
Question: I have data with datetime index using minute resolution. I want to see what is the average 'profile' of one day in a month using minute resolution. The dataset format is like this:  Power 2019-01-01 11:43:01+02:00 9.223261 2019-01-01 11:44:01+02:00 14.304057 2019-01-01 11:45:01+02:00 28.678970 2019-01-01 11:46:01+02:00 35.143512 2019-01-01 11:47:01+02:00 24.431278 ... ... 2019-12-31 15:05:14+02:00 -0.075000 2019-12-31 15:06:14+02:00 -0.075000 2019-12-31 15:07:14+02:00 -0.075000 2019-12-31 15:08:14+02:00 -0.075000 2019-12-31 15:09:14+02:00 -0.075000  To plot the average day of a month power profile on hourly basis I did the following plt.plot(df_jul.groupby(df_jul.index.hour)[['Power']].mean(), label=('July'))  where df_jul is a subset of the data above, including only data from July.  Power 2019-07-01 05:28:15+03:00 2.561204 2019-07-01 05:29:15+03:00 2.749837 2019-07-01 05:30:15+03:00 2.963823 2019-07-01 05:31:15+03:00 3.190177 2019-07-01 05:32:15+03:00 3.374277 ... ... 2019-07-31 21:12:02+03:00 2.311575 2019-07-31 21:13:02+03:00 2.310808 2019-07-31 21:14:02+03:00 2.415743 2019-07-31 21:15:02+03:00 2.485820 2019-07-31 21:16:02+03:00 1.874091  The resulting figure is like this:  So, what is the best method to get the same profile plot as in the figure above, but using minute resolution? I tried to group it by minutes but that results in an average hour of the month. I also think I could just iterate through the dataframe and do the average calculations, but I feel like there is an easier way that I am missing. Here is the correct result plot 

Answer: You should use resample with the desired frequency. If you want to plot an average day of the month, you can transform all days to the end of month (with pd.offsets.MonthEnd), then resample: (df_jul.set_axis(df_jul.index + pd.offsets.MonthEnd(0)) .resample('2min')['Power'].mean() .plot(marker='o') )  Or with a variant of your original groupby: df_jul.groupby(df_jul.index.floor('1min').time)['Power'].mean().plot()   If you want to plot the full month: df_jul.resample('1min')['Power'].mean().plot(marker='o')  Output:
Question: When pip installing the jupyter server package, it seems that pip install jupyter_server  and pip install jupyter-server  do the same thing. Is that right? Why are the package names with underscore and hyphen both OK? The same goes to jupyter_client and jupyter-client.

Answer: Pip replaces underscore with dash by default, so you always install the same package (jupyter-server).
Question: My DataFrame is: import pandas as pd df = pd.DataFrame( { 'a': [20, 9, 31, 40], 'b': [1, 10, 17, 30], } )  Expected output: Creating column c  a b c 0 20 1 20 1 9 10 20 2 31 17 17 3 40 30 31  Steps: c is the maximum value between df.b and df.a.shift(1).bfill().  My attempt: df['temp'] = df.a.shift(1).bfill() df['c'] = df[['temp', 'b']].max(axis=1)  Is it the cleanest way / best approach?

Answer: If you don't want the temporary column, then you can replace values on the shifted column using where() in a one-liner. df['c'] = df['a'].shift(1).bfill().where(lambda x: x>df['b'], df['b'])  This is similar to the combine() method posted in the other answer, but this one does a vectorized comparison while, combine() does it element-wise so this should be much faster as the length of the dataframe increase.
Question: Recently upgraded from connexion version 2 to version 3. Based on official documents in https://connexion.readthedocs.io/en/latest/index.html major change in architecture happend. my problem: when using zappa to deploy python lambda function in aws, this general error Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 500 response code. happens. in clouldwatch log review i see also general comment AbstractApp.__call__() missing 1 required positional argument: 'send'. Any idea?

Answer: Connexion 3 migrated from the WSGI to the ASGI interface, which does not seem to be supported by Zappa. You can either use an adapter middleware to run Connexion through WSGI (docs) or switch to a Zappa alternative like Magnum which supports ASGI.
Question: I need to show a plot created by the Wolfram Mathematica application used from within a Python script. So far I have written the following code, but I do not know how I should show the information stored in the wlplot variable. Is it even possible in Python? The plot.txt file contains the command I need. I checked that the command is correct and that it is properly executed in the Wolfram Alpha itself. session = WolframLanguageSession() plot_file = open("plot.txt") wl_comm = plot_file.read() plot_file.close() wlplot = session.evaluate(wlexpr(wl_plot)) #??? session.terminate()  So the question is: If it is possible, what should I write within the Python script in order to show the plot created by the Wolfram Engine?

Answer: The working code follows. from PIL import Image png_export = wl.Export(path,wlplot,"PNG") session.evaluate(png_export) img = Image.open(path) img.show()
Question: In VScode, pytests are discovered. Using a conda env and it is the selected Python:Interpreter. However, when I try to run one or all the tests, it just says "Finished running tests!". Can't get into debug. Doesn't give green checkmark or red x's. If I run pytest in the terminal, it works just fine. I have reinstalled everything and started from scratch. I have no idea what is going on. Please help! :) Added Python Test Log. This is the output when I discover and then try to run a test.    

Answer: having the following in my .vscode/setting.json fixed the problem: { "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false, "python.testing.pytestArgs": [ "${workspaceFolder}", "--rootdir=${workspaceFolder}" ], }