eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm learning the basics of the pygame library and already struggling. The "game" at this point only has a player and walls. There are 2 main surfaces: "world" (the actual game map) and "screen" (which serves as a viewport for "view_src" w/ scaling & scrolling, "viewport" is the corresponding rect). Here's the problem: I want to implement at least basic optimisation and only render sprites that are actually visible, so I'm filtering the "all" group to whatever collides with the viewport. That acts as expected. But when I call the rendering functions on the "visible" ad hoc group I get artifacts whereas calling them on "all" works just fine. Here's the relevant snippet from the game loop:  # clear old sprites all.clear(world, background) # this should clear the OLD position of all sprites, right? # handle input and generic game logic here if player.move(key_state, walls) != (0,0): # moves the player's rect if possible scroll_view(world, player.last_move, view_src) # shifts view_src if applicable # this does very little and should be unrelated to the issue all.update() # draw the new scene visible = pg.sprite.Group([ spr for spr in all.sprites() if view_src.colliderect(spr.rect) ]) print(visible.sprites()) # confirms the visible sprites are chosen correctly visible.draw(world) # results in drawing each sprite in its new AND old position #all.draw(world) # acts as it should if used instead scaled = pg.transform.scale(world.subsurface(view_src), viewport.size) screen.blit(scaled, viewport.topleft) pg.display.flip()  (I do .empty() the "visible" group at the end of the loop) Even if I determine "visible" earlier and call visible.clear(world, background) and then go all.draw(world) I get the exact same issue, it only works if both .clear() and .draw() are called on "all". This is already after consulting an AI which told me this works just fine so hopefully a good old fashioned human can point me in the right direction.

Answer: Found the problem and the fix thanks to Kingsley's nudge. The issue: Group.clear() clears the sprites drawn by the last .draw() of that exact same group. So using a different group for .clear() and .draw() doesn't work, and the continuity it needs to function is also lost by re-assigning the "visible" group each time. The solution: Initialise "visible" before the loop, persist it between iterations and add/remove sprites as needed. Fixed code:  # clear old sprites visible.clear(world, background) # clears sprites from the last .draw() # handle input and generic game logic here if player.move(key_state, walls) != (0,0): scroll_view(world, player, view_src) # "step event", update positions etc here all.update() # draw the new scene visible.empty() visible.add([ spr for spr in all if view_src.colliderect(spr.rect) ]) visible.draw(world) render_view(screen, world, view_src, viewport) # this is still what it was before
Question: I have a FastAPI application, which, in general, works fine. My setup is Apache as a proxy and FastAPI server behind it. This is the apache config: ProxyPass /fs http://127.0.0.1:8000/fs retry=1 acquire=3000 timeout=600 Keepalive=On disablereuse=ON ProxyPassReverse /fs http://127.0.0.1:8000/fs  I have one endpoint that can return 409 HTTP response, if an object exists. FastAPI works correctly. I can see in logs: INFO: 172.**.0.25:0 - "PUT /fs/Automation/123.txt HTTP/1.1" 409 Conflict  But the final response to the client is "502 Bad Gateway". Apache error log has a record for this: [Tue Dec 31 04:45:54.545972 2024] [proxy:error] [pid 3019178:tid 140121168807680] (32)Broken pipe: [client 172.31.0.25:63759] AH01084: pass request body failed to 127.0.0.1:8000 (127.0.0.1), referer: https://10.100.21.13/fs/view/Automation [Tue Dec 31 04:45:54.545996 2024] [proxy_http:error] [pid 3019178:tid 140121168807680] [client 172.31.0.25:63759] AH01097: pass request body failed to 127.0.0.1:8000 (127.0.0.1) from 172.31.0.25 (), referer: https://10.100.21.13/fs/view/Automation  What can be the reason? Another interesting thing is that it doesn't happen for any PUT request. How can I debug this? Maybe FastAPI has to return something else, some header? Or it returns too much , some extra data? How to catch this?

Answer: So, i have found the reason. When there is file upload you need to read the input buffer in any case, even if you want to return the error. In my case i had to add try: except: to empty the buffer when exception happens. Something like try: ... my original code except Exception as e: # Empty input buffer here to avoid proxy problems await request.body() raise e
Question: I am trying to install mlagents. I got to the part in python but after creating a virtual enviorment with pyenv and setting the local version to 3.10, 3.9, and 3.8 it works on none of them. I upgraded pip, installed mlagents, then torch,torchvision, and torchaudio. Then I tested mlagents-learn --help and then because of a error installed protobuf 3.20.3. I then tested again to get the following error (venv) D:\Unity\AI Ecosystem>mlagents-learn --help Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "D:\Unity\AI Ecosystem\venv\Scripts\mlagents-learn.exe\__main__.py", line 4, in <module> File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\mlagents\trainers\learn.py", line 2, in <module> from mlagents import torch_utils File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\mlagents\torch_utils\__init__.py", line 1, in <module> from mlagents.torch_utils.torch import torch as torch # noqa ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\mlagents\torch_utils\torch.py", line 6, in <module> from mlagents.trainers.settings import TorchSettings File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\mlagents\trainers\settings.py", line 644, in <module> class TrainerSettings(ExportableSettings): File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\mlagents\trainers\settings.py", line 667, in TrainerSettings cattr.register_structure_hook( File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\cattr\converters.py", line 207, in register_structure_hook self._structure_func.register_cls_list([(cl, func)]) File "D:\Unity\AI Ecosystem\venv\Lib\site-packages\cattr\dispatch.py", line 55, in register_cls_list self._single_dispatch.register(cls, handler) File "C:\Users\Ebrah\AppData\Local\Programs\Python\Python311\Lib\functools.py", line 864, in register raise TypeError( TypeError: Invalid first argument to `register()`. typing.Dict[mlagents.trainers.settings.RewardSignalType, mlagents.trainers.settings.RewardSignalSettings] is not a class or union type.  I tried installing cattrs 1.5.0 but the error remains. As I said before I also tried in 3.11, 3.10, 3.9 and 3.8 and got the same error in all of them. My unity version is 2022.3.5f1 but I don't see how that would make a difference. My pyenv version is 3.1.1. I am on windows 11 and am using pyenv-win.

Answer: Try deleting your unity project and making a new one. Unity says to use conda so try that too. Use python 3.9.
Question: All, I am trying to use the Geopandas dissolve function to aggregate a few countries; the function countries.dissolve keeps running forever. Here is a minimal script. import geopandas as gpd shape='/Volumes/TwoGb/shape/fwdshapfileoftheworld/' countries=gpd.read_file(shape+'TM_WORLD_BORDERS-0.3.shp') # Add columns countries['wmosubregion'] = '' countries['dummy'] = '' country_count = len(countries) # If the country list is empty then use all countries. country_list=['SO','SD','DJ','KM'] default = 'Null' for i in range(country_count): countries.at[i, 'wmosubregion'] = default if countries.ISO2[i] in country_list: countries.at[i, 'wmosubregion'] = "EAST_AFRICA" print(countries.ISO2[i]) region_shapes = countries.dissolve(by='wmosubregion')  I am using the TM_WORLD_BORDERS-0.3 shape files, which is freely accessible. You can get the shape files (TM_WORLD_BORDERS-0.3.shp, TM_WORLD_BORDERS-0.3.dbf, TM_WORLD_BORDERS-0.3.shx, TM_WORLD_BORDERS-0.3.shp ) from the following GitHub https://github.com/rmichnovicz/Sick-Slopes/tree/master Thanks

Answer: Dissolve is working when I try it, it finishes in a few seconds. My Geopandas version is 1.0.1. import geopandas as gpd df = gpd.read_file(r"C:\Users\bera\Downloads\TM_WORLD_BORDERS-0.3.shp") df.plot(column="NAME")   df2 = df.dissolve() df2.plot()   There are some invalid geometries that might cause problems for you? Try fixing them: #df.geometry.is_valid.all() #np.False_ #Four geometries are invalid df.loc[~df.geometry.is_valid] # FIPS ISO2 ... LAT geometry # 23 CA CA ... 59.081 MULTIPOLYGON (((-65.61362 43.42027, -65.61972 ... # 32 CI CL ... -23.389 MULTIPOLYGON (((-67.21278 -55.89362, -67.24695... # 154 NO NO ... 61.152 MULTIPOLYGON (((8.74361 58.40972, 8.73194 58.4... # 174 RS RU ... 61.988 MULTIPOLYGON (((131.87329 42.95694, 131.82413 ... # [4 rows x 12 columns] df.geometry = df.geometry.make_valid() #df.geometry.is_valid.all() #np.True_
Question: I am currently developing and testing a RNN that relies upon a large amount of data for training, and so have attempted to separate my training and testing files. I have one file where I create, train, and save a tensorflow.keras model to a file 'model.keras' I then load this model in another file and predict some values, but get the following error: Failed to convert elements of {'class_name': '__tensor__', 'config': {'dtype': 'float64', 'value': [0.0, 0.0, 0.0, 0.0]}} to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes By the way, I have tried running model.predict with this exact same data in the file where I train the model, and it works smoothly. The model loading must be the problem, not the data used to predict. This mysterious float64 tensor is the value I passed into the masking layer. I don't understand why keras is unable to recognize this JSON object as a Tensor and apply the masking operation as such. I have included snippets of my code below, edited for clarity and brevity: model_generation.py: # Create model model = tf.keras.Sequential([ tf.keras.layers.Input((352, 4)), tf.keras.layers.Masking(mask_value=tf.convert_to_tensor(np.array([0.0, 0.0, 0.0, 0.0]))), tf.keras.layers.GRU(50, return_sequences=True, activation='tanh'), tf.keras.layers.Dropout(0.2), tf.keras.layers.GRU(50,activation='tanh'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(units=1, activation='sigmoid')]) # Compile Model... # Train Model... model.save('model.keras') model.predict(data) # Line works here  model_testing.py model = tf.keras.models.load_model('model.keras') model.predict(data) # this line generates the error  EDIT: Moved the load command into the same file as the training, still receiving the exact same error message.

Answer: That error is due to the mask_value that you pass into tf.keras.layers.Masking not getting serialized compatibly for deserialization. But because you masking layer is a tensor containing all 0s anyway, you can instead just pass a scalar value like this and it will eliminate the need to serialize a tensor while storing the model tf.keras.layers.Masking(mask_value=0.0)  and it broadcasts it to effectively make it equivalent to comparing it against the tensor containing all 0s. Here is the source where the mask is applied like this ops.any(ops.not_equal(inputs, self.mask_value), axis=-1, keepdims=True)  and ops.not_equal supports broadcasting.
Question: Can you explain why this happened? import numpy as np a = np.array([[1,2], [3,4], [5,6] ]) b = np.array([[2,2,2], [2,2,2]]) print(np.einsum("xy,zx -> yx",a,b))  and output of the code is:[[ 4 12 20] [ 8 16 24]]  Which means the answer is calculated like this : ‍‍[1*2+1*2 , 3*2+3*2 , ...]  But I expected it to be calculated like this: [[1*2 , 3*2 , 5*2],[2*2 , 4*2 , 6*2]] Where did I make a mistake?

Answer: Your code is equivalent to: (a[None] * b[..., None]).sum(axis=0).T  You start with a (x, y) and b (z, x). First let's align the arrays: # a[None] shape: (1, x, y) array([[[1, 2], [3, 4], [5, 6]]]) # b[..., None] shape: (z, x, 1) array([[[2], [2], [2]], [[2], [2], [2]]])  and multiply: # a[None] * b[..., None] shape: (z, x, y) array([[[ 2, 4], [ 6, 8], [10, 12]], [[ 2, 4], [ 6, 8], [10, 12]]])  sum over axis = 0 (z): # (a[None] * b[..., None]).sum(axis=0) shape: (x, y) array([[ 4, 8], [12, 16], [20, 24]])  Swap x and y: # (a[None] * b[..., None]).sum(axis=0).T shape: (y, x) array([[ 4, 12, 20], [ 8, 16, 24]])  What you want is np.einsum('yx,xy->xy', a, b): array([[ 2, 6, 10], [ 4, 8, 12]])
Question: I'm developing a Telegram bot using python-telegram-bot to handle and reply to posts in a specific channel. The bot starts successfully and shows "Bot is running...", but it never replies to posts in the channel. Here's the relevant code for handling channel posts: async def handle_channel_post(self, update: Update, context: ContextTypes.DEFAULT_TYPE): """Handle new channel posts by adding the message link as a reply.""" try: # Get the message and channel info message = update.channel_post or update.message if not message: return # Verify this is from our target channel if message.chat.username != self.channel_username: return channel_id = message.chat.id message_id = message.message_id # Construct the message link if str(channel_id).startswith("-100"): # Private channels (or supergroups) link = f"https://t.me/c/{str(channel_id)[4:]}/{message_id}" else: # Public channels link = f"https://t.me/{self.channel_username.replace('@', '')}/{message_id}" # Create the reply text reply_text = f"View message: [Click here]({link})" # Reply to the channel post await context.bot.send_message( chat_id=channel_id, text=reply_text, reply_to_message_id=message_id, parse_mode="Markdown" ) except Exception as e: print(f"Error handling channel post: {e}")  This is the main method: async def main(): BOT_TOKEN = "<MT_BOT_TOKEN>" CHANNEL_USERNAME = "@TestTGBot123" bot = ChannelBot(BOT_TOKEN, CHANNEL_USERNAME) await bot.start()  I tried with another channel and different channel types but still not working. The bot is admin and also has privileges to post in the channel.

Answer: The issue is with this part of the code: if message.chat.username != self.channel_username: return  The message.chat.username returns the channel username without the '@' and your self.channel.username includes '@' Try this: if message.chat.username != self.channel_username.replace("@", ""): return  It removes '@' from self.channel.username and your bot should work as expected.
Question: I need to pass a parameter (some_url) from the main app to the blueprint using Flask This is my (oversimplified) app app = Flask(__name__) app.register_blueprint(my_bp, url_prefix='/mybp', some_url ="http....")  This is my (oversimplified) blueprint my_bp = Blueprint('mybp', __name__, url_prefix='/mybp') @repositories_bp.route('/entrypoint', methods=['GET', 'POST']) def entrypoint(): some_url = ????  Not sure this is the way to go, but I parsed countless threads, I just cannot find any Info about this Thanks for your help

Answer: you can use g object for the current request which stores temporary data, or you can use session to maintain data between multiple requests which usually stores this data in the client browser as a cookie, or you can store the data in the app.config to maintain a constant value.
Question: Say I have a dataframe with 2 columns, how would I create all possible combinations for a specific combination size? Each row of the df should be treated as 1 item in the combination rather than 2 unique separate items. I want the columns of the combinations to be appended to the right. The solution should ideally be efficient since it takes long to generate all the combinations with a large list. For example, I want to create all possible combinations with a combination size of 3. import pandas as pd df = pd.DataFrame({'A':['a','b','c','d'], 'B':['1','2','3','4']})  How would I get my dataframe to look like this?  A B A B A B 0 a 1 b 2 c 3 1 a 1 b 2 d 4 2 a 1 c 3 d 4 3 b 2 c 3 d 4 

Answer: An approach is itertools to generate the combinations.  Define the combination size and generate all possible combinations of rows using itertools.combinations Flatten each combination into a single list of values using itertools.chain. combination_df is created from the flattened combinations and the columns are dynamically generated to repeat 'A' and 'B' for each combination  Sample import itertools combination_size = 3 combinations = list(itertools.combinations(df.values, combination_size)) combination_df = pd.DataFrame( [list(itertools.chain(*comb)) for comb in combinations], columns=[col for i in range(combination_size) for col in df.columns] ) )  EDIT : Optimisation as suggested by @ouroboros1 combination_df = pd.DataFrame( (chain.from_iterable(c) for c in combinations), columns=np.tile(df.columns, combination_size) )  Output  A B A B A B 0 a 1 b 2 c 3 1 a 1 b 2 d 4 2 a 1 c 3 d 4 3 b 2 c 3 d 4
Question: Per the Python documentation of globals():  For code within functions, this is set when the function is defined and remains the same regardless of where the function is called.  I understood this as calling globals() from within a function returns an identical dict to the one that represented the global namespace when the function was defined, even if there have been modifications to the global namespace since then. However, my experiment below showed that my understanding is apparently incorrect. What does the documentation mean, then? (In the example below I expected the second call of foo() to give the same result as the first. Of course, if that was the case I would question the utility of globals(), but that seems to be what the documentation means.) def foo(): if 'x' in globals(): print(f"Found x: {globals()['x']}") else: global x x = 1 print(f"Not found. Set x = {x}.") foo() # Not found. Set x = 1. foo() # Found x: 1 

Answer: In fact this problem is only loosely related to the globals() builtin function but more closely related to the behaviour of mutable objects. Long story made short, your observation is correct, and the documentation is absolutely correct and accurate. The underlying cause, is that Python variables are only references to the actual objects. Let us look at an example: a = {'a': 1, 'b': 2} b = a # ok, we take a "copy" print(b) {'a': 1, 'b': 2} # no surprise here a['c'] = 3 # let us MUTATE the original object print(b) {'a': 1, 'b': 2, 'c': 3}  What happens here is that both variable are references to the very same object, what can be confirmed with print(id(a), id(b)) But if we use a different object: a = {'a': 1, 'b': 2, 'c': 3} # a is now a new and distinct object # even if it has the same value a['d'] = 4 print(a, b) {'a': 1, 'b': 2, 'c': 3, 'd': 4} {'a': 1, 'b': 2, 'c': 3}  b is still a reference to the original object, so the new changes to a are not accessible through b. You can confirm that they are now distinct objects with print(id(a), id(b)). The documentation is just a warning that if for any reason the global directory is changed to a new and different object(*), the function will still keep a reference of the object that existed when the function was defined.  (*) AFAIK, the specification of the language has no guarantee that the global directory will be the very same object during all the program lifetime
Question: I have this example dataframe in polars: df_example = pl.DataFrame( { "DATE": ["2024-11-11", "2024-11-11", "2024-11-12", "2024-11-12", "2024-11-13"], "A": [None, None, "option1", "option2", None], "B": [None, None, "YES", "YES", "NO"], } )  Which looks like this:     DATE A B     0 2024-11-11     1 2024-11-11     2 2024-11-12 option1 YES   3 2024-11-12 option2 YES   4 2024-11-13  NO    As you can see this is a long format dataframe. I want to have it in a wide format, meaning that I want the DATE to be unique per row and for each other column several columns have to be created. What I want to achieve is:     DATE A_option1 A_option2 B_YES B_NO      2024-11-11 Null Null Null Null    2024-11-12 True True True Null    2024-11-13 Null Null Null True    I have tried doing the following: df_example.pivot( index="DATE", on=["A", "B"], values=["A", "B"], aggregate_function="first" )  However, I get this error: DuplicateError: column with name 'null' has more than one occurrence  Which is logical, as it tries to create a column for the Null values in columns A, and a column for the Null values in column B. I am looking for a clean solution to this problem. I know I can impute the nulls per column with something unique and then do the pivot. Or by pivoting per column and then dropping the Null columns. However, this will create unnecessary columns. I want something more elegant.

Answer: I ended up with: ( df_example.pipe( lambda df: df.group_by("DATE").agg( [ pl.col(col).eq(val).any().alias(f"{col}_{val}") for col in df.select(pl.exclude("DATE")).columns for val in df.get_column(col).unique().drop_nulls() ] ) ).sort("DATE") )
Question: I'm trying add type annotations to an ElementList object that inherits from list and can contain either Element objects or other ElementGroup objects. When I run the following code through mypy: from typing import Self class Element: pass class ElementList(list[Element | Self]): pass elements = ElementList( [ Element(), Element(), ElementList( [ Element(), Element(), ] ), ] )  I get the following error: element.py:8: error: Self type is only allowed in annotations within class definition [misc] Found 1 error in 1 file (checked 1 source file)  What's the recommended way to add typing annotations to this so that mypy doesn't throw an error?

Answer: Your sample list argument to the ElementList constructor contains not just Elements and ElementLists but also actual lists, so a workaround of class ElementList(list["Element | ElementList"]): ... would not have worked, as @dROOOze pointed out in the comment, because list is not a subtype of ElementList. You can work around this limitation with a type alias, which can refer to itself without creating a subtype: class Element: pass type ElementListType[T] = Element | T | list[ElementListType[T]] class ElementList(list[ElementListType["ElementList"]]): pass elements = ElementList( [ Element(), Element(), [ Element(), ElementList( [ Element(), Element(), ] ) ], ] )  Demo with mypy here Demo with pyright here
Question: I tried to plot Jakarta's map based on the district. fig, ax = plt.subplots(1, figsize=(4.5,10)) jakarta_mandiri_planar.plot(ax=ax, column='Kecamatan', legend=True, legend_kwds={'loc':'center left'}) leg= ax.get_legend() leg.set_bbox_to_anchor((1.04, 0.5))   I plotted the legend on the right of the map, but I think it's too long. Can I make the legend into two or three columns? If so, how?

Answer: Use the ncols keyword: df.plot(column="NAME", cmap="tab20", legend=True, figsize=(8,8))   df.plot(column="NAME", cmap="tab20", legend=True, figsize=(10,10), legend_kwds={"ncols":2, "loc":"lower left"})