eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I tried to install psycopg2 with the command line : pip install psycopg2 and this is what I get  Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [25 lines of output] /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/setuptools/config/setupcfg.py:463: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead. warnings.warn(msg, warning_class) running egg_info creating /private/var/folders/lx/rssnrrbj15jcz8pj88rr89f00000gn/T/pip-pip-egg-info-lpapm88u/psycopg2.egg-info writing /private/var/folders/lx/rssnrrbj15jcz8pj88rr89f00000gn/T/pip-pip-egg-info-lpapm88u/psycopg2.egg-info/PKG-INFO writing dependency_links to /private/var/folders/lx/rssnrrbj15jcz8pj88rr89f00000gn/T/pip-pip-egg-info-lpapm88u/psycopg2.egg-info/dependency_links.txt writing top-level names to /private/var/folders/lx/rssnrrbj15jcz8pj88rr89f00000gn/T/pip-pip-egg-info-lpapm88u/psycopg2.egg-info/top_level.txt writing manifest file '/private/var/folders/lx/rssnrrbj15jcz8pj88rr89f00000gn/T/pip-pip-egg-info-lpapm88u/psycopg2.egg-info/SOURCES.txt' Error: pg_config executable not found. pg_config is required to build psycopg2 from source. Please add the directory containing pg_config to the $PATH or specify the full executable path with the option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. If you prefer to avoid building psycopg2 from source, please install the PyPI 'psycopg2-binary' package instead. For further information please check the 'doc/src/install.rst' file (also at <https://www.psycopg.org/docs/install.html>). [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.  then I tried to download the package from the official web site and put in the PATH: /Users/t/PycharmProjects/API/venv/lib/python3.10/site-packages (Im using a virtual environement) but I get No module named 'psycopg2'

Answer: After reading some similar questions on Stackoverflow here is the solution that worked for me. First, install Homebrew in case you don't already have it installed /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"  then install postgresql : brew install postgresql  finally use the pip command to install psycpg2 : pip install psycopg2
Question: I'm new to python and I'm trying to make a GUI window with Tkinter that executes a command. I wrote the code underneath but it wont work. What is wrong with it? The required imports are imported like tk and ttk. This is the tkinter window code: root = Tk() root.geometry("600x450") root.title("Points") Label(root, text=PlayerPoints,font=('Ubuntu')).pack(pady=30) btn= ttk.Button(root, text="Add more points",command=AddPoints) btn.pack()  Here is the AddPoints commands code (not the full thing but enough to make the error show up): PointsToAdd = int(input("How many points do you want to add?")) print(PointsToAdd, "points will be added.")  And here is the error i get: Exception has occurred: RuntimeError (note: full exception trace is shown but execution is paused at: AddPoints) can't re-enter readline  An example code to paste in a code editor to see the error yourself: from tkinter import * from tkinter import ttk PlayerPoints = 100 def AddPoints(): PointsToAdd = int(input("How many points do you want to add?")) print(PointsToAdd, "points will be added.") root = Tk() root.geometry("600x450") root.title("Points") Label(root, text=PlayerPoints,font=('Ubuntu')).pack(pady=30) btn= ttk.Button(root, text="Add more points",command=AddPoints) btn.pack() n = input("this is just to make Python wait for an input instead of killing the program") 

Answer: From running the code you share, where the tk window will not run, the first clear error is that you haven't included a root.mainloop() statement below is the code as I have tweaked it to make it run: from tkinter import * from tkinter import ttk PlayerPoints = 100 def AddPoints(): PointsToAdd = int(input("How many points do you want to add?")) print(PointsToAdd, "points will be added.") root = Tk() root.geometry("600x450") Label(root, text=PlayerPoints, font=('Ubuntu')).pack(pady=30) btn = ttk.Button(root, text="Add more points", command=AddPoints) btn.pack() root.mainloop() n = input("this is just to make Python wait for an input instead of killing the program")  from what I have changed, there aren't any errors. Not too sure if this is what you meant or not though. If you're unfamiliar with how the mainloop feature of tk works I'd suggest either doing some research on it or looking at the following article https://www.educba.com/tkinter-mainloop/ One final thing is that you don't have any code to update the points on the label in the tk GUI (not sure whether you wanted this to happen or not) the following screenshot is the output when running the code I attached above console_output
Question: Now I have a dataframe: df = pd.DataFrame({ "a":[1,2,3,4,5], "b":[2,3,4,5,6], "c":[3,4,5,6,7] })  The function: def fun(a,b,shift_len): return a+b*shift_len,b-shift_len  Using Pandas, I can get the result by: df[["d","e"]] = df.apply(lambda row:fun(row["a"],row["b"],3),axis=1,result_type="expand")  I want to know how can I use polars to get the same result?

Answer: Passing arguments with args import pandas as pd df1 = pd.DataFrame({"a":[1,2,3,4,5],"b":[2,3,4,5,6],"c":[3,4,5,6,7]}) def t(df, row1, row2, shift_len): return df[row1] + df[row2] * shift_len, df[row2] - shift_len df1[["d", "e"]] = df1.apply(t, args=("a", "b", 3), axis=1, result_type="expand") print(df1) OUTPUT: a b c d e 0 1 2 3 7 -1 1 2 3 4 11 0 2 3 4 5 15 1 3 4 5 6 19 2 4 5 6 7 23 3
Question: How to index a specific number of Characters on Django Charfield? For example, this is how we index fields in Django, but I guess it applies to entire field or all characters. class Meta: indexes = [ models.Index(fields=['last_name', 'first_name',]), models.Index(fields=['-date_of_birth',]), ]  So, how to apply index to specific portion of field as shown in mysql below. CREATE INDEX part_of_name ON customer (name(10)); 

Answer: You can create a functional index with the Substr function [Django-doc]: from django.db.models.functions import Substr # … class Meta: indexes = [ models.Index(fields=['last_name', 'first_name']), models.Index(fields=['-date_of_birth']), models.Index(Substr('pub_date', 0, 10), name='part_of_name') ]
Question: I have the following pandas dataframe. There are many NaN but there are lots of NaN value (I skipped the NaN value to make it look shorter). 0 NaN ... 26 NaN 27 357.0 28 357.0 29 357.0 30 NaN ... 246 NaN 247 357.0 248 357.0 249 357.0 250 NaN ... 303 NaN 304 58.0 305 58.0 306 58.0 307 58.0 308 58.0 309 58.0 310 58.0 311 58.0 312 58.0 313 58.0 314 58.0 315 58.0 316 NaN ... 333 NaN 334 237.0  I would like to filter all the NaN value and also only keep the first value out of the NaN (e.g. from index 27-29 there are three values, I would like to keep the value indexed 27 and skip the 28 and 29 value). The targeted array should be as follows: 27 357.0 247 357.0 304 58.0 334 237.0  I am not sure how could I keep only the first value. Thanks in advance.

Answer: Take only values that aren't nan, but the value before them is nan: df = df[df.col1.notna() & df.col1.shift().isna()]  Output:  col1 27 357.0 247 357.0 304 58.0 334 237.0   Assuming all values are greater than 0, we could also do: df = df.fillna(0).diff() df = df[df.col1.gt(0)]
Question: I've noticed, to my surprise, that in a function call, I could unpack a dict with strings that weren't even valid python identifiers. It's surprising to me since argument names must be identifiers, so allowing a function call to unpack a **kwargs that has non-identifiers, with no run time error, doesn't seem healthy (since it could bury problems deeper that where they actually occur). Unless there's an actual use to being able to do this, in which case my question becomes "what would that use be?". Example code Consider this function: def foo(**kwargs): first_key, first_val = next(iter(kwargs.items())) print(f"{first_key=}, {first_val=}") return kwargs  This shows that, within a function call, you can't unpack a dict that has has integer keys, which is EXPECTED. >>> t = foo(**{1: 2, 3: 4}) TypeError Traceback (most recent call last) ... TypeError: foo() keywords must be strings  What is really not expected, and surprising, is that you can, on the other hand, unpack a dict with string keys, even if these are not valid python identifiers: >>> t = foo(**{'not an identifier': 1, '12': 12, ',(*&$)': 100}) first_key='not an identifier', first_val=1 >>> t {'not an identifier': 1, '12': 12, ',(*&$)': 100} 

Answer: Looks like this is more of a kwargs issue than an unpacking issue. For example, one wouldn't run into the same issue with foo: def foo(a, b): print(a + b) foo(**{"a": 3, "b": 2}) # 5 foo(**{"a": 3, "b": 2, "c": 4}) # TypeError: foo() got an unexpected keyword argument 'c' foo(**{"a": 3, "b": 2, "not valid": 4}) # TypeError: foo() got an unexpected keyword argument 'not valid'  But when kwargs is used, that flexibility comes with a price. It looks like the function first attempts to pop out and map all the named arguments and then passes the remaining items in a dict called kwargs. Since all keywords are strings (but all strings are not valid keywords), the first check is easy - keywords must be strings. Beyond that, it's up to the author to figure out what to do with remaining items in kwargs. def bar(a, **kwargs): print(locals()) bar(a=2) # {'a': 2, 'kwargs': {}} bar(**{"a": 3, "b": 2}) # {'a': 3, 'kwargs': {'b': 2}} bar(**{"a": 3, "b": 2, "c": 4}) # {'a': 3, 'kwargs': {'b': 2, 'c': 4}} bar(**{1: 3, 3: 4}) # TypeError: keywords must be strings  Having said all that, there definitely is inconsistency but not a flaw. Some related discussions:  Supporting (or not) invalid identifiers in **kwargs  feature: **kwargs allowing improperly named variables
Question: IDK why my player.move() is not working here's my main class: import pygame from player import * pygame.init() WIDTH, HEIGHT = 900, 600 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("MyGame!") FPS = 60 player_x = 500 player_y = 500 PLAYER_WIDTH = 60 PLAYER_HEIGHT = 60 PLAYER_VEL = 5 WHITE = (255, 255, 255) BLACK = (0, 0, 0) keys_pressed = pygame.key.get_pressed() player_rect = pygame.Rect(player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT) player = Player(player_rect, BLACK, WIN, keys_pressed, PLAYER_VEL) def draw_window(): WIN.fill(WHITE) pygame.display.update() def main(): run = True clock = pygame.time.Clock() while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False draw_window() player.move() player.draw_player() pygame.display.update() pygame.quit() if __name__ == '__main__': main()  and here's my other class called player.py that the move function is not working: import pygame class Player(object): def __init__(self, player, color, surface, keys_pressed, vel): self.player = player self.color = color self.surface = surface self.keys_pressed = keys_pressed self.vel = vel def draw_player(self): pygame.draw.rect(self.surface, self.color, self.player) pygame.display.update() def move(self): if self.keys_pressed[pygame.K_a]: self.player.x -= self.vel if self.keys_pressed[pygame.K_d]: self.player.x += self.vel  I tried putting the player = Player(...) before the main function. But whatever I tried, it doesn't seem to work and this is my first time posting a question on stackoverflow. This problem also happened alot in the past so thanks if you helped me out.

Answer: pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is 1, otherwise 0. The contents of the keys_pressed list don't magically change when the state of the keys changes. keys_pressed is not tied to the keys. You need to get the new state of the keys in every frame: class Player(object): # [...] def move(self): # get current state of the keys self.keys_pressed = pygame.key.get_pressed() if self.keys_pressed[pygame.K_a]: self.player.x -= self.vel if self.keys_pressed[pygame.K_d]: self.player.x += self.vel
Question: Issue: I am using VSCode version 1.69.1 on Mac (Version details at the bottom). From Mac, I connect to a remote repo using SSH FS When I click on 'run' > 'Start Debugging' or 'Run Without Debugging' on a remote python file, the "Run and Debug pane opens" but the file is not run [![Pane is blank][1]][1] The debugger works for local repos (hosted on Mac). I have tried reinstalling the Python extension, removed the ~/.vscode-server on the remote server, re-install VSCode on the Mac but nothing seem to have helped. This was earlier working (tried a week back) but unsure what changed. From the terminal (connected to remote host, rendered by SSH FS, I am able to run python test.py and that works Launch.json file for python  // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true } ] }  Version details Commit: b06ae3b2d2dbfe28bca3134cc6be65935cdfea6a Date: 2022-07-12T08:21:51.333Z (1 day ago) Electron: 18.3.5 Chromium: 100.0.4896.160 Node.js: 16.13.2 V8: 10.0.139.17-electron.0 OS: Darwin x64 21.5.0  Id: Kelvin.vscode-sshfs Description: File system, terminal and task provider using SSH Version: 1.25.0 Publisher: Kelvin Schoofs VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=Kelvin.vscode-sshfs``` [1]: https://i.sstatic.net/elruD.png 

Answer: Python Debugger version v2022.10.0 seems to be broken for SSH-FS. Using the previous version of Python extension addressed it. To install an older version of an extension, click on gear icon> select "Install another version" and select version to install.  I used version v2022.8.1, and that works
Question: I'm trying to add the timestamp of a video frame to its own frame name like "frame2 00:00:01:05", using CAP_PROP_POS_MSEC I'm getting the current position of the video file in milliseconds, what I want is to change those milliseconds to the time format "00:00:00:00:". My code currently assigns the name like: "frame2 1.05" but ideally I would have the name like: "frame2 00:00:01:05". Is there a library that can help me achieve this milliseconds to HH:MM:SS conversion? import cv2 vidcap = cv2.VideoCapture('video.mp4') success,frame = vidcap.read() cont = 0 while success: frame_timestamp = (vidcap.get(cv2.CAP_PROP_POS_MSEC))/(1000) cv2.imwrite(f'frame{cont} '+str((format(frame_timestamp,'.2f')))+'.jpg',frame) success,frame = vidcap.read() cont+=1  Thanks!

Answer: Just use divmod. It does a division and modulo at the same time. It's more convenient than doing that separately. seconds = 1.05 # or whatever (hours, seconds) = divmod(seconds, 3600) (minutes, seconds) = divmod(seconds, 60) formatted = f"{hours:02.0f}:{minutes:02.0f}:{seconds:05.2f}" # >>> formatted # '00:00:01.05'  And if you need the fraction of seconds to be separated by :, which nobody will understand, you can use (seconds, fraction) = divmod(seconds, 1) and multiply fraction * 100 to get hundredth of a second.
Question: I want to write a service that launches multiple workers that work infinitely and then quit when main process is Ctrl+C'd. However, I do not understand how to handle Ctrl+C correctly. I have a following testing code: import os import multiprocessing as mp def g(): print(os.getpid()) while True: pass def main(): with mp.Pool(1) as pool: try: s = pool.starmap(g, [[]] * 1) except KeyboardInterrupt: print('Done') if __name__ == "__main__": print(os.getpid()) main()  When I try to Ctrl+C it, I expect process(es) running g to just receive SIGTERM and silently terminate, however, I receive something like that instead: Process ForkPoolWorker-1: Done Traceback (most recent call last): File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap self.run() File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File "/usr/lib/python3.8/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, **kwds)) File "/usr/lib/python3.8/multiprocessing/pool.py", line 51, in starmapstar return list(itertools.starmap(args[0], args[1])) File "test.py", line 8, in g pass KeyboardInterrupt  This obviously means that parent and children processes both raise KeyboardInterrupt from Ctrl+C, further suggested by tests with kill -2. Why does this happen and how to deal with it to achieve what I want?

Answer: The signal that triggers KeyboardInterrupt is delivered to the whole pool. The child worker processes treat it the same as the parent, raising KeyboardInterrupt. The easiest solution here is:  Disable the SIGINT handling in each worker on creation Ensure the parent terminates the workers when it catches KeyboardInterrupt  You can do this easily by passing an initializer function that the Pool runs in each worker before the worker begins doing work: import signal import multiprocessing as mp # Use initializer to ignore SIGINT in child processes with mp.Pool(1, initializer=signal.signal, initargs=(signal.SIGINT, signal.SIG_IGN)) as pool: try: s = pool.starmap(g, [[]] * 1) except KeyboardInterrupt: print('Done')  The initializer replaces the default SIGINT handler with one that ignores SIGINT in the children, leaving it up to the parent process to kill them. The with statement in the parent handles this automatically (exiting the with block implicitly calls pool.terminate()), so all you're responsible for is catching the KeyboardInterrupt in the parent and converting from ugly traceback to simple message.
Question: The PyCharm debugger has the feature to set breakpoints at raised exceptions. However if an exception is handled inside a try except block it is not raised. How to deal with this if I want to debug within the try block? I could comment out the try except parts but this seems too cumbersome. Is there a better solution?

Answer: In the breakpoints settings (Either the  icon in the debug toolbar, or ctrl+shft+F8), you can set exception breakpoints. The "Activation Policy" is usually set by default to "On termination". But since you handle the error, there is no termination. To activate the breakpoint immediately, even if the error is handled, you need to set the activation policy to "On raise":  Note: that warning sign which says: "This option may slow down the debugger"
Question: Is there any way to replace multiple characters in a string at once, so that instead of doing: "foo_faa:fee,fii".replace("_", "").replace(":", "").replace(",", "")  just something like (with str.replace()) "foo_faa:fee,fii".replace(["_", ":", ","], "") 

Answer: An option that requires no looping or regular expressions is translate: >>> "foo_faa:fee,fii".translate(str.maketrans('', '', "_:,")) "foofaafeefii"  Note that for Python 2, the API is slightly different.
Question: I have a polars dataframe like so: df = pl.from_repr(""" ┌─────────────────────┬─────────┬─────────┐ │ time ┆ 1 ┆ 2 │ │ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ f64 ┆ f64 │ ╞═════════════════════╪═════════╪═════════╡ │ 2021-10-02 00:05:00 ┆ 2.9048 ┆ 2.8849 │ │ 2021-10-02 00:10:00 ┆ 48224.0 ┆ 48068.0 │ └─────────────────────┴─────────┴─────────┘ """)  and a masking dataframe with similar columns and time value like so: df_mask = pl.from_repr(""" ┌─────────────────────┬───────┬───────┐ │ time ┆ 1 ┆ 2 │ │ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ bool ┆ bool │ ╞═════════════════════╪═══════╪═══════╡ │ 2021-10-02 00:05:00 ┆ false ┆ false │ │ 2021-10-02 00:10:00 ┆ true ┆ true │ └─────────────────────┴───────┴───────┘ """)  I am looking for this result: shape: (2, 3) ┌─────────────────────┬────────┬─────────┐ │ time ┆ 1 ┆ 2 │ │ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ f64 ┆ f64 │ ╞═════════════════════╪════════╪═════════╡ │ 2021-10-02 00:05:00 ┆ null ┆ null │ │ 2021-10-02 00:10:00 ┆ 2.8849 ┆ 48068.0 │ └─────────────────────┴────────┴─────────┘  Here I only show with 2 columns '1' and '2' but there could any number of them. Any help would be appreciated!

Answer: Having columns in a single DataFrame has guarantees you don't have when you have data in separate tables. Masking out values by columns in another DataFrame is a potential for errors caused by different lengths. For this reason polars does not encourage such operations and therefore you must first create a single DataFrame from the two, and then select the columns/computations you need. So let's do that. The first thing you can do is join the two tables. This guaranteed to work on DataFrames of different sizes. df_a.join(df_mask, on="time", suffix="_mask")  This comes however with a price, as joining is not free. If you are 100% certain your dataframes have the same height, you can use a horizontal concat. ( pl.concat( [df_a, df_mask.select(pl.all().name.suffix("_mask"))], how="horizontal" ).select( [pl.col("time")] + [ pl.when(pl.col(f"{name}_mask")).then(pl.col(name)).otherwise(None) for name in ["1", "2"] ] ) )  In the final select query we take the columns we want. And compute the masked values with a when -> then -> otherwise branch. This outputs: shape: (2, 3) ┌─────────────────────┬─────────┬─────────┐ │ time ┆ 1 ┆ 2 │ │ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ f64 ┆ f64 │ ╞═════════════════════╪═════════╪═════════╡ │ 2021-10-02 00:05:00 ┆ null ┆ null │ │ 2021-10-02 00:10:00 ┆ 48224.0 ┆ 48068.0 │ └─────────────────────┴─────────┴─────────┘
Question: Given the following dataframe, is there some way to select only columns starting with a given prefix? I know I could do e.g. pl.col(column) for column in df.columns if column.startswith("prefix_"), but I'm wondering if I can do it as part of a single expression. df = pl.DataFrame( {"prefix_a": [1, 2, 3], "prefix_b": [1, 2, 3], "some_column": [3, 2, 1]} ) df.select(pl.all().<column_name_starts_with>("prefix_"))  Would this be possible to do lazily?

Answer: Starting from Polars 0.18.1 you can use Selectors(polars.selectors.starts_with) which provides more intuitive selection of columns from DataFrame or LazyFrame objects based on their name, dtype or other properties. >>> import polars as pl >>> import polars.selectors as cs >>> >>> df = pl.DataFrame( ... {"prefix_a": [1, 2, 3], "prefix_b": [1, 2, 3], "some_column": [3, 2, 1]} ... ) >>> df shape: (3, 3) ┌──────────┬──────────┬─────────────┐ │ prefix_a ┆ prefix_b ┆ some_column │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞══════════╪══════════╪═════════════╡ │ 1 ┆ 1 ┆ 3 │ │ 2 ┆ 2 ┆ 2 │ │ 3 ┆ 3 ┆ 1 │ └──────────┴──────────┴─────────────┘ >>> # print(df.lazy().select(cs.starts_with("prefix_")).collect()) # for LazyFrame >>> print(df.select(cs.starts_with("prefix_"))) # For DataFrame shape: (3, 2) ┌──────────┬──────────┐ │ prefix_a ┆ prefix_b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞══════════╪══════════╡ │ 1 ┆ 1 │ │ 2 ┆ 2 │ │ 3 ┆ 3 │ └──────────┴──────────┘