eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: <nav class="flex h-full w-full flex-col p-2 gizmo:px-3 gizmo:pb-3.5 gizmo:pt-0" aria-label="Menu">  This is the nav although, its a-lot longer its full of divs I just want to know how to scroll till the end of the menu. Edit: loading element that has to be accounted for <svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="animate-spin text-center" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg">  and under it there are many line elements top of nav xpath: /html/body/div[1]/div[1]/div[1]/div/div/div/div/nav  top of svg xpath: /html/body/div[1]/div[1]/div[1]/div/div/div/div/nav/div[2]/div[2]/div[2]/svg  do you need scrollbar html?

Answer: There are lots of specialized scroll methods in SeleniumBase: self.scroll_to(selector) self.slow_scroll_to(selector) self.scroll_into_view(selector) self.scroll_to_top() self.scroll_to_bottom()  But you might not even need to scroll to the element. There's a self.js_click(selector) method, which lets you click on hidden elements that exist in the HTML. Here's an example test that uses that method to click a hidden logout menu item from the nav: from seleniumbase import BaseCase BaseCase.main(__name__, __file__) class SwagLabsLoginTests(BaseCase): def login_to_swag_labs(self): self.open("https://www.saucedemo.com") self.wait_for_element("div.login_logo") self.type("#user-name", "standard_user") self.type("#password", "secret_sauce") self.click('input[type="submit"]') def test_swag_labs_login(self): self.login_to_swag_labs() self.assert_element("div.inventory_list") self.assert_element('.inventory_item:contains("Backpack")') self.js_click("a#logout_sidebar_link") self.assert_element("div#login_button_container")
Question: After a test has been executed I need to collect the result of that test. But I don't find the result in the FixtureRequest object. I can find the test name and some additional data but nowhere I see something that shows if a test has been passed or failed. Nor if there were some exceptions example code: class TestSomething: @pytest.mark.test_case_id(99999) def test_example(self, api_interface) -> None: assert 5 == 5  and in someother file: @pytest.fixture def api_interface(request: FixtureRequest):  I can see that the request has the title name and a node object but nowhere I see something result or assert like data....

Answer: The way to achieve this is by using hooks. The hook will add the test result as an attribute of the test item, which afterwards you will be able to check and perform necessary actions. # example conftest.py import pytest # set up a hook to be able to check if a test has failed @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() setattr(item, "rep_" + rep.when, rep) # check if a test has failed @pytest.fixture(scope="function", autouse=True) def test_failed_check(request): yield if request.node.rep_call.failed: # Do something if tast has failed print('Test Failed')
Question: I'm trying to figure out how to make my code more readable and have less lines in my code. It contains a lot of if elif statements that seem like can be combined into a few. fl = input("file:").lower().strip() a = fl.endswith(".jpeg") b = fl.endswith(".jpg") c = fl.endswith(".txt") d = fl.endswith(".png") e = fl.endswith(".pdf") f = fl.endswith(".zip") g = fl.endswith(".gif") if a or b is True: print("image/jpeg") elif c is True: print("text/plain") elif d is True: print("image/png") elif e is True: print("application/pdf") elif f is True: print("application/zip") elif g is True: print("image/gif") else: print("application/octet-stream")  I tried getting rid of the variables on the top by putting fl == fl.endswith(".filetype") inside of the if statements, and instead of printing each type it only printed my else statement. I also tried looking up other ways to get the end of the str in python docs but couldn't find anything. Don't give me too direct of a solution to the problem, would like to avoid cs50 academic honesty issues. Also still quite new to python

Answer: Use a dictionary to map extensions to content-types. import os extension_content_types = { ".jpeg": "image/jpeg", ".jpg": "image/jpeg", ".txt": "text/plain", ".png": "image/png", ".pdf": "application/pdf", ".zip": "application/zip", ".gif": "image/gif", } fl = input("file:").lower().strip() filename, ext = os.path.splitext(fl) content_type = extension_content_types.get(ext, 'application/octet-stream') print(fl, content_type)
Question: The following code in a script works as expected, from sympy import * x = symbols('x') p = plot(x, x*(1-x), (x, 0, 1)) ax = p._backend.ax[0] ax.set_yticks((0, .05, .25)) p._backend.fig.savefig('Figure_1.png')   but when I copy the code above in a notebook cell, this is what I get  If it is possible to manipulate the (hidden) attributes of a Sympy's plot when one works in a Jupyter notebook, how can it be done?

Answer:  As per this answer of Display two Sympy plots as two Matplotlib subplots, with inline mode in Jupyter, p.backend(p) must be used. The problem is that sympy.plotting.plot.plot(*args, show=True, **kwargs) creates its own figure and axes and plots.show() displays the plot immediately.  Because of the way inline mode works, the plot is shown before the changes are implemented by ax.set_yticks((0, .05, .25)).   Tested in python v3.12.0, matplotlib v3.8.1, sympy v1.11.1.  from sympy import symbols, plot import matplotlib.pyplot as plt x = symbols('x') # note show=False, the default is True p = plot(x, x*(1-x), (x, 0, 1), show=False) fig, ax = plt.subplots() backend = p.backend(p) backend.ax = ax backend._process_series(backend.parent._series, ax, backend.parent) backend.ax.set_yticks((0, .05, .25)) plt.close(backend.fig) plt.show()    In interactive mode %matplotlib qt, the code in the OP works fine.  from sympy import symbols, plot import matplotlib.pyplot as plt %matplotlib qt # %matplotlib inline - to revert to inline x = symbols('x') p = plot(x, x*(1-x), (x, 0, 1)) ax = p._backend.ax[0] ax.set_yticks((0, .05, .25))   With a custom backend instance. This has as the advantage that matplotlib should not be explicitly imported. One should be aware of what you are doing: sympy provides a ready-to-use interface to plot functions on the fly, without the need to worry about (boring) mathematical technicality such as domain/range. If instead you try to act against such defaults you need to go back to the source code and do some reverse engineering. In the OP, custom ticks are required, but by default the MatplotlibBackend sets a linear scale, for both x & y, from the sympy.plotting.plot.Plot object, xscale='linear', yscale='linear', which interfere with the matplotlib.axes.Axes.set_yticks method. From the source code 1467 if parent.yscale and not isinstance(ax, Axes3D): 1468 ax.set_yscale(parent.yscale)  By setting parent.yscale to False (or None, '') the condition will never be meet. super is built-in python method used for accessing inherited methods that have been overridden in a class. from sympy.plotting.plot import MatplotlibBackend from sympy import symbols class JupyterPlotter(MatplotlibBackend): def _process_series(self, series, ax, parent): parent.yscale = False ax.set_yticks((0, .05, .25)) # custom yticks super()._process_series(series, ax, parent) x = symbols('x') p = plot(x, x*(1-x), (x, 0, 1), backend=JupyterPlotter)   Plots with special needs may require a special implementation, here is a different example.
Question: With plotly in jupyter I am creating a Scatter3D plot as follows: # Configure the trace. trace = go.Scatter3d( x=x, y=y, z=z, mode='markers', marker=dict(color=colors, size=1) ) # Configure the layout. layout = go.Layout( margin={'l': 0, 'r': 0, 'b': 0, 't': 0}, height = 1000, width = 1000 ) data = [trace] plot_figure = go.Figure(data=data, layout=layout) # Render the plot. plotly.offline.iplot(plot_figure)  How can I rotate a plot generated like this in order to create a gif video out of it i.e. stored as a gif file like rotate.gif which shows an animation of the plot rotated? Based on the comments given I created this code (complete, working example): import plotly.graph_objects as go import numpy as np import plotly.io as pio # Helix equation t = np.linspace(0, 10, 50) x, y, z = np.cos(t), np.sin(t), t fig= go.Figure(go.Scatter3d(x=x, y=y, z=z, mode='markers')) x_eye = -1.25 y_eye = 2 z_eye = 0.5 fig.update_layout( title='Animation Test', width=600, height=600, scene_camera_eye=dict(x=x_eye, y=y_eye, z=z_eye), updatemenus=[dict(type='buttons', showactive=False, y=1, x=0.8, xanchor='left', yanchor='bottom', pad=dict(t=45, r=10), buttons=[dict(label='Play', method='animate', args=[None, dict(frame=dict(duration=5, redraw=True), transition=dict(duration=1), fromcurrent=True, mode='immediate' )] ) ] ) ] ) def rotate_z(x, y, z, theta): w = x+1j*y return np.real(np.exp(1j*theta)*w), np.imag(np.exp(1j*theta)*w), z frames=[] for k, t in enumerate(np.arange(0, 6.26, 0.1)): xe, ye, ze = rotate_z(x_eye, y_eye, z_eye, -t) newframe = go.Frame(layout=dict(scene_camera_eye=dict(x=xe, y=ye, z=ze))) frames.append(newframe) pio.write_image(newframe, f"images/images_{k+1:03d}.png", width=400, height=400, scale=1) fig.frames=frames fig.show()  which runs without an error and does rotate the scenery when I press on Play, however the image that is saved just shows an empty 2D coordinate system:  but not what I actually see rotating. It also seems those image are created when I execute the cell in the jupyter notebook and not after I press "Play". Seems that there are two figures, one that I can see rotating, and the image of an empty 2D coordinate system that gets saved to a file ...

Answer: Note pio.write_image() expects a Figure object or dict representing a figure as the 1st argument (we can't just pass an update object or a frame). The idea is precisely to apply the changes from each animation frame to the figure and export it at each point sequentially : frames=[] for k, t in enumerate(np.arange(0, 6.26, 0.1)): xe, ye, ze = rotate_z(x_eye, y_eye, z_eye, -t) newframe = go.Frame(layout=dict(scene_camera_eye=dict(x=xe, y=ye, z=ze))) frames.append(newframe) fig.update_layout(scene_camera_eye=dict(x=xe, y=ye, z=ze)) pio.write_image(fig, f"images/images_{k+1:03d}.png", width=400, height=400, scale=1)  The animation frames describe only the changes to be applied at some point to the figure, asynchronously, using Plotly.js animate method (js runtime), but the image export is done synchronously (python runtime), before the figure is eventually rendered (because before fig.show()).
Question: I used a SLURM manager to submit a bunch of scripts that all run in one Conda environment. I would like to install a new Python package to this environment. Do I need to wait until all of my scripts are done running? Or can I install the package now without messing anything up?

Answer: Can you? Sure. Should you? No. It could lead to changing existing packages, which could potentially lead to problems (e.g., missing references, API changes) - really depends on how the scripts are written and the dynamics of library loading throughout the scripts. However, there is a larger issue of working reproducibly. May not apply here, but most SLURM users are doing scientific computing and scientific users should never mutate environments after using them to produce results. Intact environments are the scientific record and essential for reproducibility. If software requirements change, then create a new environment. Conda uses hardlinks to minimize disk usage, so one should be extremely liberal about creating new environments.
Question: I'm trying to open HDF files using a python code provided in this website (https://hdfeos.org/software/pyhdf.php). However, I get an error while importing the pyhdf package import os from pyhdf.SD import SD, SDC import numpy as np import rasterio  The error : --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf . Check the section C-API incompatibility at the Troubleshooting ImportError section at https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility for indications on how to solve this problem . --------------------------------------------------------------------------- ImportError Traceback (most recent call last) Input In [14], in <cell line: 2>() 1 import os ----> 2 from pyhdf.SD import SD, SDC 3 import rasterio File ~\Miniconda3\lib\site-packages\pyhdf\SD.py:1003, in <module> 38 """ 39 SD (scientific dataset) API (:mod:`pyhdf.SD`) 40 ============================================= (...) 999 1000 """ 1001 import os, sys, types -> 1003 from . import hdfext as _C 1004 from .six.moves import xrange 1005 from .error import _checkErr, HDF4Error File ~\Miniconda3\lib\site-packages\pyhdf\hdfext.py:10, in <module> 8 # Import the low-level C/C++ module 9 if __package__ or "." in __name__: ---> 10 from . import _hdfext 11 else: 12 import _hdfext ImportError: numpy.core.multiarray failed to import  I tried upgrading numpy and pyhdf versions but it didn't work

Answer: As commented by @Cow, I am posting this as an answer: The following packages are required to build and install pyhdf: Python: Python 2.6 or newer for Python 2, or Python 3.2 or newer for Python 3. NumPy HDF4 libraries (to use their HDF4 binaries, you will also need szip, available from the same page) Compiler suite e.g. GCC. On Windows, you need to use a compatible Visual C++ compiler. zlib libjpeg  https://fhs.github.io/pyhdf/install.html#requirements  Pay special attention to Swig-generated interface files  Interface files hdfext.py and hdfext_wrap.c (located under the pyhdf subdirectory) have been generated using the SWIG tool. Those two files should be usable as is on most environments. It could happen however that, for reasons related to your environment, your C compiler does not accept the ‘.c’ file and raises a compilation error. If so, the interface needs to be regenerated. To do so, install SWIG, then run:  $ cd pyhdf $ swig -python hdfext.i  SWIG should silently regenerate the two interface files, after which installation should proceed correctly. https://fhs.github.io/pyhdf/install.html#swig-generated-interface-files
Question: I am working with the Google calendar API, the python quickstart in particular, but the language does not matter. The example from https://developers.google.com/calendar/api/quickstart/python has:  if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( "credentials.json", SCOPES ) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open("token.json", "w") as token: token.write(creds.to_json())  I am working on a website, that is mostly server side. That people will log in, and be able to create a calendar, that the server will allow them to create a calendar, and automatically add events depending on events that occur. Question 1: My question is about token.json, is that file shared between all users, or should a separator file be created for each person? Question 2: Should it be backed up, cause if I lost the file then will everyone be logged out?

Answer:  Question 1: My question is about token.json, is that file shared between all users, or should a separator file be created for each person?  Token Json is single user. In fact as your code is written it is single user. The first thing that sample does is check if the file exists if os.path.exists("token.json"):  If it does it will load the credentials within that file.  Question 2: Should it be backed up, cause if I lost the file then will everyone be logged out?  Yes you should probably back it up as the user who authorized the application will be prompted to authorize your application again. Note this is authorization not authencation, there is no log-out Notes: The code you are following Authorize credentials for a desktop application it is designed for a desk top application, as written it is single user. It is also not going to work on a hosted web page as. flow = InstalledAppFlow.from_client_secrets_file( "credentials.json", SCOPES ) creds = flow.run_local_server(port=0)  Will run the the authorization request on the machine its running on unless the user can login to the webserver its not going to work.
Question: In below screenshot when hovering over the grayed out variables Pylance (correctly!) says they are not accessed, e.g.; "_baz" is not accessed Pylance. My question is about waz, which is clearly not accessed in either tabs, still not grayed out. Why isn't it grayed out?  I thought maybe it was related to waz not being a "private" (underscore) variable, but it just doesn't make sense...

Answer: Global variables without an underscore are considered public, so it might be used when imported from somewhere else.
Question: Sorry guys for the title but it is really what I'm trying to do. Here is a table to explain that more. The bold lines makes the years and the the thin ones makes the weeks.  For the expected output. It really doesn't matter its format. All I need is that if I ask for the dates of a pair YEAR/WEEK, I get the corresponding window of dates. For example, if I do some_window_function(2022, 5) I should have the result below (it correspond to the RED WINDOW)  DATE YEAR WEEK 2020 30 Friday, July 24, 2020 2022 5 Wednesday, February 2, 2022 5 Thursday, February 3, 2022 5 Friday, February 4, 2022 7 Tuesday, February 15, 2022  And for example, if I do some_window_function(2022, 7) I should have the result below (it correspond to the BLUE WINDOW)  DATE YEAR WEEK 2022 5 Friday, February 4, 2022 2022 7 Tuesday, February 15, 2022 7 Wednesday, February 16, 2022 7 Thursday, February 17, 2022 2023 44 Tuesday, October 31, 2023  The dataframe used is this : df = pd.DataFrame({'YEAR': [2020, 2020, 2020, 2020, 2020, 2020, 2020, 2022, 2022, 2022, 2022, 2022, 2022, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023], 'WEEK': [29, 29, 29, 30, 30, 30, 30, 5, 5, 5, 7, 7, 7, 44, 44, 44, 44, 45, 45, 45, 46, 46, 46, 46], 'DATE': ['Monday, July 13, 2020', 'Thursday, July 16, 2020', 'Friday, July 17, 2020', 'Monday, July 20, 2020', 'Tuesday, July 21, 2020', 'Thursday, July 23, 2020', 'Friday, July 24, 2020', 'Wednesday, February 2, 2022', 'Thursday, February 3, 2022', 'Friday, February 4, 2022', 'Tuesday, February 15, 2022', 'Wednesday, February 16, 2022', 'Thursday, February 17, 2022', 'Tuesday, October 31, 2023', 'Wednesday, November 02, 2023', 'Friday, November 03, 2023', 'Sunday, November 05, 2023', 'Monday, November 06, 2023', 'Tuesday, November 07, 2023', 'Wednesday, November 08, 2023', 'Monday, November 13, 2023', 'Tuesday, November 14, 2023', 'Wednesday, November 15, 2023', 'Thursday, November 16, 2023']})  I made the code below but it gives a similar dataframe to my input : def make_windows(group): if group.name == df.loc[df['YEAR'] == group.name, 'WEEK'].min(): group.at[group.index[-1]+1, 'DATE'] = df.at[group.index[-1]+1, 'DATE'] return group.ffill() elif group.name < df.loc[df['YEAR']== group.name, 'WEEK'].max(): group.at[group.index[-1]+1, 'DATE'] = df.at[group.index[-1]+1, 'DATE'] return group.iloc[1:].ffill() else: return group.iloc[1:].ffill() results = df.groupby('YEAR').apply(make_windows) 

Answer: Looks like you could use a simple mask for the YEAR/WEEK and expand it one row above/below (assuming sorted dates): df = df.sort_values(by=['YEAR', 'WEEK']) def some_window_function(year, week): mask = df['YEAR'].eq(year) & df['WEEK'].eq(week) return df[mask|mask.shift()|mask.shift(-1)] some_window_function(2022, 5)  Output:  YEAR WEEK DATE 6 2020 30 Friday, July 24, 2020 7 2022 5 Wednesday, February 2, 2022 8 2022 5 Thursday, February 3, 2022 9 2022 5 Friday, February 4, 2022 10 2022 7 Tuesday, February 15, 2022
Question: I have a simple data frame as below: import pandas as pd import numpy as np df = pd.DataFrame({'CUS_NO': ['900636229', '900636229', '900636080', '900636080', '900636052', '900636052', '900636053', '900636054', '900636055', '900636056'], 'indicator': ['both', 'left_only', 'both', 'left_only', 'both', 'left_only', 'both', 'left_only', 'both', 'left_only'], 'Nationality': ['VN', 'VN', 'KR', 'KR', 'VN', 'VN', 'KR', 'VN', 'KR', 'VN']}) CUS_NO indicator Nationality 0 900636229 both VN 1 900636229 left_only VN 2 900636080 both KR 3 900636080 left_only KR 4 900636052 both VN 5 900636052 left_only VN 6 900636053 both KR 7 900636054 left_only VN 8 900636055 both KR 9 900636056 left_only VN  I want to count unique value of CUS_NO so I used pd.Series.nunique by below code: df2 = pd.pivot_table(df, values='CUS_NO', index='Nationality', columns='indicator', aggfunc=pd.Series.nunique, margins=True).reset_index() df2  And here is the result: indicator Nationality both left_only All 0 KR 3 1 3 1 VN 2 4 4 2 All 5 5 7  But I my expectation is if CUS_NO was same and indicator was different, I just need to count both indicator. So below is my expected Output: indicator Nationality both left_only All 0 KR 3 0 3 1 VN 2 2 4 2 All 5 2 7  Thank you.

Answer: You can sort_values to have "both" on top (if more categories, use a Categorical to define a custom order), then drop_duplicates: tmp = (df .sort_values(by='indicator') .drop_duplicates(subset=['CUS_NO', 'Nationality'], keep='first') ) df2 = pd.pivot_table(tmp, values='CUS_NO', index='Nationality', columns='indicator', aggfunc=pd.Series.nunique, margins=True, fill_value=0).reset_index()  Output: indicator Nationality both left_only All 0 KR 3 0 3 1 VN 2 2 4 2 All 5 2 7  Intermediate tmp:  CUS_NO indicator Nationality 0 900636229 both VN 2 900636080 both KR 4 900636052 both VN 6 900636053 both KR 8 900636055 both KR 7 900636054 left_only VN 9 900636056 left_only VN
Question: I get confusing results from pandas.rolling() when the dtype is datetime64[us]. Pandas version is 2.1.1. Let df be the dataframe  day x 0 2021-01-01 3 1 2021-01-02 2 2 2021-01-03 1 3 2021-01-05 4 4 2021-01-08 2 5 2021-01-14 5 6 2021-01-15 6 7 2021-01-16 1 8 2021-01-19 5 9 2021-01-20 2  Its dtypes are: day datetime64[ns] x int64 dtype: object  We specify a rolling window of length 3 days: df.rolling("3d", on="day", center=True)["x"].sum()  Output is as expected: 0 5.0 1 6.0 2 3.0 3 4.0 4 2.0 5 11.0 6 12.0 7 7.0 8 7.0 9 7.0 Name: x, dtype: float64  Let us repeat this after casting the dtype datetime64[ns] to datetime64[us]: df["day"] = df["day"].astype("datetime64[us]")  Using the exact same code as above for the rolling window now gives: 0 31.0 1 31.0 2 31.0 3 31.0 4 31.0 5 31.0 6 31.0 7 31.0 8 31.0 9 31.0 Name: x, dtype: float64  Why?

Answer: I can't reproduce it on pandas 2.1.2, make sure to use a recent version: print(pd.__version__) 2.1.2 df["day"] = df["day"].astype("datetime64[us]") print(df.dtypes) day datetime64[us] x int64 dtype: object df.rolling("3d", on="day", center=True)["x"].sum() 0 5.0 1 6.0 2 3.0 3 4.0 4 2.0 5 11.0 6 12.0 7 7.0 8 7.0 9 7.0 Name: x, dtype: float64