eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have this code that works properly on windows 10 that I use to send text and enter keys to a specific window using pywinauto, but once i tried to use it in another system using win 11 it does not work. Here is the code: import pywinauto import time boolBisCheckbox = False def parar(eachFfox): global boolBisCheckbox if boolBisCheckbox == False: appname = r"\[FIREFOX " + eachFfox + r"\]" else: appname = r"\[FIREFOX " + eachFfox + r"2\]" handle = pywinauto.findwindows.find_window(title_re=appname) app = pywinauto.application.Application(backend="win32").connect(handle=handle) Wizard = app[appname] Wizard.send_keystrokes("parar") time.sleep(0.5) Wizard.send_keystrokes("{ENTER}") time.sleep(6) Wizard.send_keystrokes("{ENTER}") time.sleep(10) Wizard.send_keystrokes("{ENTER}") input() parar("A")  And here is the error: Traceback (most recent call last): File "C:\pypy\testParar.py", line 23, in <module> parar("A") File "C:\pypy\testParar.py", line 12, in parar handle = pywinauto.findwindows.find_window(title_re=appname) File "C:\Python\lib\site-packages\pywinauto\findwindows.py", line 113, in find_window element = find_element(**kwargs) File "C:\Python\lib\site-packages\pywinauto\findwindows.py", line 84, in find_element elements = find_elements(**kwargs) File "C:\Python\lib\site-packages\pywinauto\findwindows.py", line 283, in find_elements elements = [elem for elem in elements if _title_match(elem)] File "C:\Python\lib\site-packages\pywinauto\findwindows.py", line 283, in <listcomp> elements = [elem for elem in elements if _title_match(elem)] File "C:\Python\lib\site-packages\pywinauto\findwindows.py", line 279, in _title_match t = w.rich_text File "C:\Python\lib\site-packages\pywinauto\win32_element_info.py", line 83, in rich_text return handleprops.text(self.handle) File "C:\Python\lib\site-packages\pywinauto\handleprops.py", line 92, in text buffer_ = create_unicode_buffer(length) File "C:\Python\lib\ctypes\_init_.py", line 297, in create_unicode_buffer buf = buftype() MemoryError  I took the same code used in another PC running win 10 where it was working properly, and used it in win 11. The systems both have 32 Gb of Ram. I google to try to find similar questions but only found one and there was no answer to the problem.

Answer: I just identified the culprit, thank you so much for your insights Vasily Ryabov ! It was SteelSeriesGG.exe app for the keyboard (bloatware **** ...), even in the tray it was somehow messing with it.
Question: I'm making a python script that requests the user for their cookie through playwright webdriver This is my dummy code import asyncio from playwright.async_api import async_playwright async def main(): async with async_playwright() as playwright: browser = await playwright.chromium.launch(headless=False) context = await browser.new_context() page = await context.new_page() await page.goto('https://example.com') print('Please log-in') while True: for i in (await context.cookies()): if i['name'] == 'account_cookie': return i['value'] asyncio.run(main()) 

Answer: You could use wait_for_function: await page.wait_for_function("document.cookie.includes('account_cookie')")
Question: I have an NxM array, as well as an arbitrary list of sets of column indices I'd like to use to slice the array. For example, the 3x3 array my_arr = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])  and index sets my_idxs = [[0, 1], [2]]  I would like to use the pairs of indices to select the corresponding columns from the array and obtain the length of the (row-wise) vectors using np.linalg.norm(). I would like to do this for all index pairs. Given the aforementioned array and list of index sets, this should give: [[2.23606797749979, 3], [2.23606797749979, 3], [2.23606797749979, 3]]  When all sets have the same number of indices (for example, using my_idxs = [[0, 1], [1, 2]] I can simply use np.linalg.norm(my_arr[:, my_idxs], axis=1): [[2.23606797749979, 3.605551275463989], [2.23606797749979, 3.605551275463989], [2.23606797749979, 3.605551275463989]]  However, when they are not (as is the case with my_idxs = [[0, 1], [2]], the varying index list lengths yield an error when slicing as the array of index sets would be irregular in shape. Is there any way to implement the single-line option, without resorting to looping over the list of index sets and handling each of them separately?

Answer: You can try: my_arr = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) my_idxs = [[0, 1], [2]] out = np.c_[*[np.linalg.norm(my_arr[:, i], axis=1) for i in my_idxs]] print(out)  Prints: [[2.23606798 3. ] [2.23606798 3. ] [2.23606798 3. ]]
Question: My understanding is MultiIndex.dropna() removes index entries for which at least one level is NaN, there are no conditions. However it seems if a previous groupby was used with dropna=False, it's no longer possible to use MultiIndex.dropna().  What are the reasons for the different behavior? How to drop the NaN entries after using groupby?  (I'm aware the NaN groups would be dropped by groupby without the dropna parameter, but I'm looking for a solution working in the case the parameter has been used at some point earlier).  import pandas as pd import numpy as np d = {(8.0, 8.0): {'A': -1.10, 'B': -1.0}, (7.0, 8.0): {'A': -0.10, 'B': 0.1}, (5.0, 8.0): {'A': 1.15, 'B': -1.2}, (7.0, 7.0): {'A': 1.10, 'B': 1.6}, (7.0, np.NaN): {'A': 0.70, 'B': -0.7}, (8.0, np.NaN): {'A': -1.00, 'B': 0.9}, (np.NaN, 5.0): {'A': -2.20, 'B': 1.1}} # This works as expected index = pd.MultiIndex.from_tuples(d.keys(), names=['L1', 'L2']) df = pd.DataFrame(d.values(), index=index) print(df.index.dropna()) # This doesn't work as expected df = df.groupby(['L1', 'L2'], dropna=False).mean() print(df.index.dropna())   MultiIndex([(8.0, 8.0), (7.0, 8.0), (5.0, 8.0), (7.0, 7.0)], names=['L1', 'L2']) MultiIndex([(5.0, 8.0), (7.0, 7.0), (7.0, 8.0), (7.0, nan), (8.0, 8.0), (8.0, nan), (nan, 5.0)], names=['L1', 'L2']) 

Answer: Looking at the sources of pd.MultiIndex.dropna() reveals that there are codes for each value of index. Pandas expects code value -1 for NaN, which apparently is not the case when doing .groupby() (a bug?). You can avoid this issue by reconstructing the index and then drop NaN values, e.g.: df = df.groupby(["L1", "L2"], dropna=False).mean() # reconstruct the index (this will assign code -1 to NaN): df.index = pd.MultiIndex.from_tuples(df.index.to_list(), names=df.index.names) print(df.index.dropna())  Prints: MultiIndex([(5.0, 8.0), (7.0, 7.0), (7.0, 8.0), (8.0, 8.0)], names=['L1', 'L2'])
Question: Suppose I want to write a recursive binary search function in Python. The recursive function needs to get the start and end of the current search interval as parameters: def binary_search(myarray, start, end): ...  But, when I call the actual function from outside, I always start at 0 and finish at the end of the array. It is easy to make 0 a default value for start, but how can I write a default value for end? Is it possible to write something like: def binary_search(myarray, start=0, end=len(myarray)): ...  ?

Answer: The common idiom* is: def binary_search(myarray, start=0, end=None): if end is None: end = len(myarray)  *(usually used to avoid a mutable default argument value, like here: https://stackoverflow.com/a/41686973/5052365)
Question: Python: 3.11 Saxonche: 12.4.2 My website keeps consuming more and more memory until the server runs out of memory and crashes. I isolated the problematic code to the following script: import gc from time import sleep from saxonche import PySaxonProcessor xml_str = """ <root> <stuff>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac auctor ex. Nunc in tincidunt urna. Sed tincidunt eros lacus, sed pulvinar sem venenatis et. Donec euismod orci quis pellentesque sagittis. Donec at tortor in dui mattis facilisis. Pellentesque vel varius lectus. Nunc sed gravida risus, ac finibus elit. Etiam sollicitudin nunc a velit efficitur molestie in ac lectus. Donec vulputate orci odio, sit amet hendrerit odio rhoncus commodo.</stuff> <stuff>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac auctor ex. Nunc in tincidunt urna. Sed tincidunt eros lacus, sed pulvinar sem venenatis et. Donec euismod orci quis pellentesque sagittis. Donec at tortor in dui mattis facilisis. Pellentesque vel varius lectus. Nunc sed gravida risus, ac finibus elit. Etiam sollicitudin nunc a velit efficitur molestie in ac lectus. Donec vulputate orci odio, sit amet hendrerit odio rhoncus commodo.</stuff> <stuff>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac auctor ex. Nunc in tincidunt urna. Sed tincidunt eros lacus, sed pulvinar sem venenatis et. Donec euismod orci quis pellentesque sagittis. Donec at tortor in dui mattis facilisis. Pellentesque vel varius lectus. Nunc sed gravida risus, ac finibus elit. Etiam sollicitudin nunc a velit efficitur molestie in ac lectus. Donec vulputate orci odio, sit amet hendrerit odio rhoncus commodo.</stuff> </root> """ while True: print('Running once...') with PySaxonProcessor(license=False) as proc: proc.parse_xml(xml_text=xml_str) gc.collect() sleep(1)  This script consumes memory at a rate of about 0.5 MB per second. The memory usage does not plateau after a while. I have logs showing that memory usage continues to grow for hours until the server runs out of memory and crashes. Other things I tried that aren't shown above:  Using a PyDocumentBuilder to parse the XML instead of a PySaxonProcessor. It didn't appear to change anything. Deleting the Saxon processor and the return value of parse_xml() using the del Python keyword. No change.  I have to use Saxon instead of lxml because I need XPath 3.0 support. What am I doing wrong? How do I parse XML using Saxon in a way that doesn't leak?  A few folks have suggested that instantiating the PySaxonProcessor once before the loop will fix the leak. It doesn't. This still leaks: with PySaxonProcessor(license=False) as proc: while True: print('Running once...') proc.parse_xml(xml_text=xml_str) gc.collect() sleep(1) 

Answer: It looks like a memory leak. I created a bug to track it: https://saxonica.plan.io/issues/6391 And the issue is now fixed in the released SaxonC 12.5.
Question: I Am trying to set up the debug environment for Pycharm and Python(Fastapi) in Docker (with docker-compose). But I stuck into the problem that I cannot launch both: the debug server and the docker image. My setup of the entry point of the app: # import debugpy # debugpy.listen(('0.0.0.0', 5678)) # debugpy.wait_for_client() # print("Debugger is attached!") import pydevd_pycharm pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)  In the Pycharm I set up port 5678 for Python Debug Server. So, if I start debug in Pycharm first, then I get error during docker-compose up: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5678 -> 0.0.0.0:0: listen tcp 0.0.0.0:5678: bind: address already in use  If I start docker-compose first, then I get the error in Pycharm while starting debug: Address already in use  It looks they both want to listen for the same port on my local machine and who listens the first, gets all access. I tried to google but nothing. At the same time, working with VSC does have any problems. It connects to the docker container and debugs as intended. Please advise.

Answer:  Replace 'localhost' with 'host.docker.internal' (at least on a Mac) Remove any port forward for port 5678 (so Docker won't try to allocate it) Use this in the code:  import pydevd_pycharm pydevd_pycharm.settrace('host.docker.internal', port=5678, stdoutToServer=True, stderrToServer=True)  You might also want to take a look at the last answer here.
Question: before ckeditor worked for django but now it is not working and expired. django by itself suggest non-free ckeditor 4 LTS or ckeditor 5 but I don't know how to use it please if there is give me another editor for django or guide me for this ckeditor. it is the warning message: WARNINGS: ?: (ckeditor.W001) django-ckeditor bundles CKEditor 4.22.1 which isn't supported anmyore and which does have unfixed security issues, see for example https://ckeditor.com/cke4/release/CKEditor-4.24.0-LTS . You should consider strongly switching to a different editor (maybe CKEditor 5 respectively django-ckeditor-5 after checking whether the CKEditor 5 license terms work for you) or switch to the non-free CKEditor 4 LTS package. See https://ckeditor.com/ckeditor-4-support/ for more on this. (Note! This notice has been added by the django-ckeditor developers and we are not affiliated with CKSource and were not involved in the licensing change, so please refrain from complaining to us. Thanks.)

Answer: You can use github.com/hvlads/django-ckeditor You can install it with pip install django-ckeditor-5  further instructions can be found on the github page.
Question: I have a shapefile that I read as a geopandas dataframe import geopandas as gpd gdf = gpd.read_file('myfile.shp') gdf.plot()   where gdf.crs <Projected CRS: ESRI:54009> Name: World_Mollweide Axis Info [cartesian]: - E[east]: Easting (metre) - N[north]: Northing (metre) Area of Use: - name: World. - bounds: (-180.0, -90.0, 180.0, 90.0) Coordinate Operation: - name: World_Mollweide - method: Mollweide Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich  and gdf.total_bounds array([-17561329.90352868, -6732161.66088735, 17840887.22672861, 8750122.26961274])  I would like to use basemap to plot the lat/lon grid on top of it. This is what I am doing from mpl_toolkits.basemap import Basemap # Create a Basemap instance with the same projection as the GeoDataFrame map = Basemap(projection='moll', lon_0=-0, lat_0=-0, resolution='c') # Create a figure and axis fig, ax = plt.subplots(figsize=(10, 6)) # Plot the basemap map.drawcoastlines() map.drawcountries() map.drawparallels(range(-90, 91, 30), labels=[1,0,0,0], fontsize=10) map.drawmeridians(range(-180, 181, 60), labels=[0,0,0,1], fontsize=10) # Plot the GeoDataFrame on top of the basemap gdf.plot(ax=ax, color='red', markersize=5)  but this is what I get 

Answer: That's because the Mollweide projection's parameters (i.e, the proj-string) used by the Basemap are different from the ones of your GeoDataFrame (that is ESRI:54009) : >>> gdf.crs.srs 'esri:54009' >>> map.srs '+proj=moll +R=6370997.0 +units=m +lat_0=0.0 +lon_0=0.0 +x_0=18019900...'  A simple fix would be to call to_crs (with the basemap's srs) before making the plot : gdf.plot(ax=ax, color='red', markersize=5) gdf.to_crs(map.srs).plot(ax=ax, color='red') SN: Using map as a variable name isn't recommended since it's a built-in.
Question: I have the following code: import time def run_indefinitely(): while True: # code to run indefinitely goes here print("Running indefinitely...") time.sleep(1) try: # Run the code indefinitely until Enter key is pressed run_indefinitely() except KeyboardInterrupt: print("Loop interrupted by user")  Is there a way to break out of the while loop by hitting 'enter' instead of ctrl+C ?

Answer: This is surprisingly tricky to do in Python. The technique involves spawning a worker thread to interrupt us later. Using python-readchar: import os import signal import sys import time from threading import Timer from readchar import readkey # pip install readchar def wait_for(key, timeout): """wait `timeout` seconds for user to press `key`""" pid = os.getpid() sig = signal.CTRL_C_EVENT if os.name == "nt" else signal.SIGINT timer = Timer(timeout, lambda: os.kill(pid, sig)) timer.start() while True: k = readkey() print(f"received {k!r}") if k == key: timer.cancel() # cancel the timer print("breaking") break def run_indefinitely(): while True: print("Running indefinitely...") try: wait_for(key="\n", timeout=1) except KeyboardInterrupt: continue else: break run_indefinitely() print("Loop interrupted by user")  Using pynput: import os import signal import time from pynput import keyboard # pip install pynput pid = os.getpid() def on_press(key): if key is keyboard.Key["enter"]: sig = signal.CTRL_C_EVENT if os.name == "nt" else signal.SIGINT os.kill(pid, sig) with keyboard.Listener(on_press=on_press) as k: try: for i in range(10): print("Running indefinitely...") time.sleep(1) except KeyboardInterrupt: pass print("Loop interrupted by user")
Question: I'm having trouble manipulating a dataset in Python which has non-UTF-8 characters. The strings are imported as a binary. But I am having issues converting the binary columns to strings where a cell has non UTF-8 characters. A minimal working example of my issue is import polars as pl import pandas as pd pd_df = pd.DataFrame([[b"bob", b"value 2", 3], [b"jane", b"\xc4", 6]], columns=["a", "b", "c"]) df = pl.from_pandas(pd_df) column_names = df.columns # Loop through the column names for col_name in column_names: # Check if the column has binary values if df[col_name].dtype ==pl.Binary: # Convert the binary column to string format print(col_name) df = df.with_columns(pl.col(col_name).cast(pl.String))  This throws an error when converting column b. For a solution, I'm fine converting any non-utf 8 characters to blanks. Have tried many other suggestions for conversion in online suggestions, but I can't get any of them to work.

Answer: This solution also relies on applying python's native bytes.decode to all elements in the columns of type pl.Binary. Unfortunately, we cannot yet use polars' native expression API for this, but need to call pl.Expr.map_elements instead. df.with_columns( pl.col(pl.Binary).map_elements( lambda bytes: bytes.decode(errors='ignore'), return_dtype=pl.String ) )  shape: (2, 3) ┌──────┬─────────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ i64 │ ╞══════╪═════════╪═════╡ │ bob ┆ value 2 ┆ 3 │ │ jane ┆ ┆ 6 │ └──────┴─────────┴─────┘
Question: I'm currently responsible to write library code that is both compatible with pydantic v1 and v2. Getting the code functional is more or less straightforward since you can make version sensitive choices in your code to satisfy your test suite, e.g. using patterns like this: import pydantic PYDANTIC_VERSION = packaging.version.parse(pydantic.__version__) if PYDANTIC_VERSION.major == 1: # do some v1 things else: # do some v2 things  However, getting this code compatible with mypy type checks seems difficult since mypy will not resolve the PYDANTIC_VERSION variable and always run through both sides of the if blocks. This will constantly trigger mypy errors in both worlds inside our CI (where mypy is supposed to run against both versions). This makes total sense since mypy is not a runtime type checker, but I wonder if there any best practices to achieve such a goal. The issue isn't really unique to pydantic, that's just an example. I know that such version switches do work on the interpreter version (sys.version_info) but I cannot find a way to have mypy skip over an entire block of code if the version of pydantic is equal to either 1.x.x or 2.x.x I've searched issues on mypy and Stack Overflow, but given the thousands of issues and articles it's very hard to find existing (eventually solved or fixed) details on the topic.

Answer: --always-true and --always-false exist, so you can make a boolean flag named PYDANTIC_V1 and run Mypy twice on the same code: $ pip install "pydantic==1.*" $ mypy file.py --always-true=PYDANTIC_V1  $ pip install -U pydantic $ mypy file.py --always-false=PYDANTIC_V1
Question: Example is below, need to make sure IDE type checker or reveal_type would identify k, j and i types correctly. Perhaps there is some way to suggest the typing that args is an empty tuple and kwargs empty dict and the return value then would be tuple[int]? from typing import Union, overload def test(*args: int, **kwargs: str) -> Union[int, str, tuple[int]]: if args: return 5 if kwargs: return "5" return (5,) # now all are Union[int, str, tuple[int]] k = test(1) j = test(i="1") i = test() reveal_type(k) # should be int reveal_type(j) # should be str reveal_type(i) # should be tuple[int] 

Answer: Here's a possible solution that allows mypy to infer the correct types for k, j, and i. This approach does result in mypy complaining about the overload signatures overlapping. I'm not sure if it's possible to address that besides simply suppressing the errors. from typing import Union, overload @overload def test() -> tuple[int]: # type: ignore[overload-overlap] ... @overload def test(*args: int) -> int: # type: ignore[overload-overlap] ... @overload def test(**kwargs: str) -> str: ... def test(*args: int, **kwargs: str) -> Union[int, str, tuple[int]]: if args: return 5 if kwargs: return "5" return (5,) # now all are Union[int, str, tuple[int]] k = test(1) j = test(i="1") i = test() reveal_type(k) # should be int reveal_type(j) # should be str reveal_type(i) # should be tuple[int]  Output from mypy --strict v1.9.0 (try it online!): main.py:29: note: Revealed type is "builtins.int" main.py:30: note: Revealed type is "builtins.str" main.py:31: note: Revealed type is "tuple[builtins.int]" Success: no issues found in 1 source file
Question: In my python app I am using Shapely. Invoking the function below: def get_t_start(t_line: geometry.LineString): print('get_t_start', t_line.boundary) p1, p2 = t_line.boundary t_start = p1 if p1.y < p2.y else p2 return t_start  produces the following output:  get_t_start MULTIPOINT (965 80, 1565 1074) Traceback (most recent call last): ... File "/sites/mpapp.py", line 13, in get_t_start p1, p2 = t_line.boundary TypeError: cannot unpack non-iterable MultiPoint object  From the print of t_line.boundary I guess the object is ok. I am sure I have used this object (MULTIPOINT) like this in other apps to get the boundary points. I really can't figure out why now this is not working.

Answer: That's most likely because your apps are running different versions of shapely. Starting with Shapely 1.8, iteration over multi-part geometries (like a MultiPoint) is deprecated and was removed in Shapely 2.0 (read more). So, you just need to access the boundary's geoms : from shapely import LineString, Point def get_t_start(t_line: LineString) -> Point: print("get_t_start", t_line.boundary) p1, p2 = t_line.boundary.geoms # << here t_start = p1 if p1.y < p2.y else p2 return t_start  Output : from shapely import from_wkt line = from_wkt("LINESTRING (965 80, 1565 1074)") >>> get_t_start(line) # get_t_start MULTIPOINT (965 80, 1565 1074) # + a display of the point
Question: I'm trying to run my Cython project. And one of the header is gmpxx.h. Even though I already installed the gmp library using brew install gmp. I could not run my cython file with  python3 setup.py build_ext --inplace. fatal error: 'gmpxx.h' file not found #include <gmpxx.h> ^~~~~~~~~ 1 error generated. error: command '/usr/bin/clang' failed with exit code 1  So I use brew list gmp to check the location of the  gmpxx.h header. So it is actually inside /opt/homebrew/Cellar/gmp/6.3.0/include/ folder. With Xcode, I can just add the location into the header search paths. But I'm trying to do the same thing with Pycharm. How do I add the location of my gmpxx.h header to pycharm? I need a little help. Please kindly give me your take. Thank you.

Answer: So I added the library location directly into setup.py file. from setuptools import setup, Extension, find_packages from Cython.Build import cythonize extension = Extension( "Class Name", sources=["Something" ], include_dirs=[ "/opt/homebrew/Cellar/gmp/6.3.0/include", # Include directory for GMP headers "/opt/homebrew/Cellar/gmp/6.3.0/include/gmp", # Additional include directory for GMP headers ], libraries=["gmpxx", "gmp"], library_dirs=[ "/opt/homebrew/Cellar/gmp/6.3.0/lib", # Directory containing libgmpxx.dylib ], extra_compile_args=["-std=c++17", "-O3"], language="c++" ) setup( name="Class Name", version="0.1", packages=find_packages(where="src"), package_dir={"": "src"}, package_data={"Class Name": ["*.pyi"]}, ext_modules=cythonize(extension, compiler_directives={"language_level":3}, annotate=True), zip_safe=False )