eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Using Jupyter Notebook, I can create an animated plot (based on this sample code): %matplotlib notebook import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() x = np.arange(0, 2*np.pi, 0.01) line, = ax.plot(x, np.sin(x)) def init(): line.set_ydata([np.nan] * len(x)) return line, def animate(i): line.set_ydata(np.sin(x + i / 100)) # update the data. return line, ani = animation.FuncAnimation( fig, animate, init_func=init, interval=2, blit=True, save_count=50) plt.show()  Is it possible to do so in Visual Studio Code's notebook editor? I think it involves the magic %matplotlib notebook mode which VS Code does not seem to support, but I don't know if there is an alternative.

Answer: Looks as though vscode supports ipywidgets (https://github.com/microsoft/vscode-python/issues/3429). So you can use the ipympl backend to matplotlib. install with pip install ipympl (also available on conda-forge) To use it you can use the %matplotlib ipympl magic.  %matplotlib notebook does some javascript injection that is very specific to jupyter notebook, so it will not work in vscode or even jupyter lab.
Question: Hello I am having troubles with the pipreqs librairy in Python. It doesn't generate the correct requirements.txt file. I am using a Python Virtual Environment and the only packages I have installed are pipreqs and selenium with pip install pipreqs pip install selenium  Structure of the project: MyProject |- test.py  And test.py has only one line: from selenium import webdriver   First when I do pipreqs ./  I got the error UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3474: character maps to <undefined> which I managed to solve by using pipreqs ./ --encoding=utf-8   But now the requirements.txt generated doesn't match my expectations. In my opinion, it should be equal to: selenium==1.341.0  But it is equal to: brotli==1.0.9 cryptography==3.2.1 ipaddr==2.2.0 lxml==4.6.1 mock==4.0.2 ordereddict==1.1 protobuf==3.13.0 pyOpenSSL==19.1.0 simplejson==3.17.2   Now when I try to clone this code and do pip install -r requirements.txt it doesn't install selenium and the code doesn't run. What is happening here ?

Answer: So the issue I had was that my actual workspace was: MyProject |- .venv // <- My Python Virtual Environment |- test.py  My Python Virtual Environment was in my Project folder so when I run the command pipreqs ./  it is looking at all the dependencies of all the files in the folder (including my virtual environment) and that is why it was generating a weird requirements.txt file. To fix this, I used the option --ignore of pipreqs: pipreqs ./ --ignore .venv  And the generated requirements.txt is: selenium==3.141.0  You may also want to ignore other folders that are causing issues like this: pipreqs --ignore bin,etc,include,lib,lib64,.venv  The --force tag will also overwrite the existing requirements.txt
Question: I am using coverage.py to get the test coverage of the code. Suppose I have two functions with the same name in two different modules # foo/foo.py def get_something(): # fetch something # 10 line of branch code return "something foo/foo.py" # bar/foo.py def get_something(): # fetch something # 20 line of branch code return "something bar/foo.py"  How can I exclude the bar.foo.get_something(...) function "completely" ?

Answer: We can use pragma comment on the function definition level which tells the coveragepy to exclude the function completely. # bar/foo.py def get_something(): # pragma: no cover # fetch something # 20 line of branch code return "something bar/foo.py" Note If we have the coveragepy config file with an exclude_lines setting in it, make sure that pragma: no cover in that setting because it overrides the default.
Question: I have a Streamlit dashboard which lets me interactively explore a t-SNE embedding using an Altair plot. I am trying to figure out how to access the metadata of the selected datum so that I can visualize the corresponding image. In other words, given: selector = alt.selection_single() chart = ( alt.Chart(df) .mark_circle() .encode(x="tSNE_dim1", y="tSNE_dim2", color="predicted class", tooltip=["image url", "predicted class"]) .add_selection(selector) )  ...is there something akin to selected_metadata = selector.tooltip update_dashboard_img(img=selected_metadata["image url"], caption=selected_metadata["predicted class"])  I am aware about image marks but the images are on S3 and there are too many of them to fit in the plot.

Answer: I hate to disagree with the creator of Altair, but I was able to achieve this using streamlit-vega-lite package. This works by wrapping the call to the chart creation function with altair_component():  from streamlit_vega_lite import altair_component ... event_dict = altair_component(altair_chart=create_tsne_chart(tsne_df)) # note: works with selector = alt.selection_interval(), not selection_single() dim1_bounds, dim2_bounds = event_dict.get("dim1"), event_dict.get("dim2") if dim1_bounds: (dim1_min, dim1_max), (dim2_min, dim2_max) = dim1_bounds, dim2_bounds selected_images = tsne_df[ (tsne_df.dim1 >= dim1_min) & (tsne_df.dim1 <= dim1_max) & (tsne_df.dim2 >= dim2_min) & (tsne_df.dim2 <= dim2_max) ] st.write("Selected Images") st.write(selected_images) if len(selected_images) > 0: for _index, row in selected_images.iterrows(): img = get_img(row["image url"]) st.image(img, caption=f"{row['image url']} {row['predicted class']}", use_column_width=True)  The event_dict only contains information about the selector bounds. So, you have to use those values to reselect the data that was selected in the interactive chart. Note that this package is a POC and has various limitations. Please upvote the Streamlit feature request created by the author of streamlit_vega_lite.
Question: I want to create frames from the video named project.avi and save them to frameIn folder. But some type of errors are not let me done. How can I solve this problem. Here is the code: cap = cv2.VideoCapture('project.avi') currentFrame = 0 while(True): ret, frame = cap.read() name = 'frameIn/frame' + str(currentFrame) + '.jpg' print ("Creating file... " + name) cv2.imwrite(name, frame) frames.append(name) currentFrame += 1 cap.release() cv2.destroyAllWindows()  The error is: Traceback (most recent call last): File "videoreader.py", line 28, in <module> cv2.imwrite(name, frame) cv2.error: OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-2rx9f0ng/opencv/modules/imgcodecs/src/loadsave.cpp:738: error: (-215:Assertion failed) !_img.empty() in function 'imwrite' 

Answer: The cause may be that image is empty, so, You should check weather video is opened correctly before read frames by: cap.isOpened(). Then, after execute ret, frame = cap.read() check ret variable value if true to ensure that frame is grabbed correctly. The code to be Clear : cap = cv2.VideoCapture('project.avi') if cap.isOpened(): current_frame = 0 while True: ret, frame = cap.read() if ret: name = f'frameIn/frame{current_frame}.jpg' print(f"Creating file... {name}") cv2.imwrite(name, frame) frames.append(name) current_frame += 1 cap.release() cv2.destroyAllWindows()  I hope it helped you.
Question: I am trying to h5py on Windows10 64bit, Python 3.8.5, Pip 20.2.4. Used this command pip install h5py  But this throws an error ERROR: Could not build wheels for h5py which use PEP 517 and cannot be installed directly  Looks like it's quite known issue for pep 517 and other packages, so i try to check all of the solutions like pip install --no-use-pep517 h5py pip install --no-binary h5py  But nothing works. How can I install h5py?

Answer: Found a solution - I was trying to install on Python3.8.5 32bit. Switching to 64bit just solved the issue. I saw that the latest version doesn't support win 32, check this: github.com/h5py/h5py/issues/1753
Question: I have a plotly plot composed of subplots - fig = make_subplots( rows=3, cols=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1)  I want to add a title for every one the subplots but only after the figure is created and the traces are added i.e. not when I create the figure. fig = make_subplots( rows=3, cols=1, subplot_titles=['a', 'b', 'c']  Can I do it via fig.update_layout or something similar?

Answer: When make_subplots is used it's creating (correctly placed) annotations. So this can be mostly duplicated using annotation methods. In general, for the first: fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False, text="a", row=1, col=1)  The downside is you may need to adjust x and y to your conditions/tastes. Full example, using bold text: import plotly.graph_objects as go from plotly.subplots import make_subplots fig = make_subplots(rows=3, cols=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1) fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False, text="<b>Hey</b>", row=1, col=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=2, col=1) fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False, text="<b>Bee</b>", row=2, col=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=3, col=1) fig.add_annotation(xref="x domain",yref="y domain",x=0.5, y=1.2, showarrow=False, text="<b>See</b>", row=3, col=1) fig.show()
Question: I've got this message when I try to install numpy using Python 3.10. How to fix this?  Copying numpy.egg-info to build/bdist.linux-x86_64/wheel/numpy-1.19.3-py3.10.egg-info running install_scripts Traceback (most recent call last): File "/home/walenty/.local/lib/python3.10/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module> main() File "/home/walenty/.local/lib/python3.10/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/home/walenty/.local/lib/python3.10/site-packages/pip/_vendor/pep517/_in_process.py", line 204, in build_wheel return _build_backend().build_wheel(wheel_directory, config_settings, File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 211, in build_wheel return self._build_with_temp_dir(['bdist_wheel'], '.whl', File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 197, in _build_with_temp_dir self.run_setup() File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 248, in run_setup super(_BuildMetaLegacyBackend, File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/setuptools/build_meta.py", line 142, in run_setup exec(compile(code, __file__, 'exec'), locals()) File "setup.py", line 508, in <module> setup_package() File "setup.py", line 500, in setup_package setup(**metadata) File "/tmp/pip-install-p3yq92pw/numpy/numpy/distutils/core.py", line 169, in setup return old_setup(**new_attr) File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/setuptools/__init__.py", line 165, in setup return distutils.core.setup(**attrs) File "/usr/local/lib/python3.10/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/local/lib/python3.10/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/usr/local/lib/python3.10/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/wheel/bdist_wheel.py", line 328, in run impl_tag, abi_tag, plat_tag = self.get_tag() File "/tmp/pip-build-env-plb3t7s6/overlay/lib/python3.10/site-packages/wheel/bdist_wheel.py", line 278, in get_tag assert tag in supported_tags, "would build wheel with unsupported tag {}".format(tag) AssertionError: would build wheel with unsupported tag ('cp310', 'cp310', 'linux_x86_64') ---------------------------------------- ERROR: Failed building wheel for numpy Failed to build numpy ERROR: Could not build wheels for numpy which use PEP 517 and cannot be installed directly 

Answer: It's a bug in python 3.10, a workaround is installing numpy with the --no-use-pep517 flag. E.g.: pip3.10 install numpy --no-use-pep517 There's a fix for this on the way though, so just waiting is an option as well.
Question: ImportError: cannot import name 'docevents' from 'botocore.docs.bcdoc' (/python3.7/site-packages/botocore/docs/bcdoc/init.py) Traceback (most recent call last): File "/root/.pyenv/versions/3.7.6/bin/aws", line 19, in <module> import awscli.clidriver File "/root/.pyenv/versions/3.7.6/lib/python3.7/site-packages/awscli/clidriver.py", line 36, in <module> from awscli.help import ProviderHelpCommand File "/root/.pyenv/versions/3.7.6/lib/python3.7/site-packages/awscli/help.py", line 23, in <module> from botocore.docs.bcdoc import docevents ImportError: cannot import name 'docevents' from 'botocore.docs.bcdoc' (/root/.pyenv/versions/3.7.6/lib/python3.7/site-packages/botocore/docs/bcdoc/__init__.py) [Container] 2020/10/29 16:48:39 Command did not exit successfully aws --version exit status 1  The failure occurs in the PRE_BUILD. And this is my spec build file: buildspec-cd.yml pre_build: commands: - AWS_REGION=${AWS_DEFAULT_REGION} - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) - IMAGE_VERSION=${COMMIT_HASH} - REPOSITORY_URI=${CONTAINER_REGISTRY}/${APPLICATION_NAME} - aws --version - echo Logging in to Amazon ECR... - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)  The codebuild was working correctly and nothing has been changed. Only stopped working.

Answer: Reading this GitHub issue #2596. i fixed my error. Just before the PRE_BUILD section, I added this line to my buildspec-cd.yml file: pip3 install --upgrade awscli install: commands: - pip3 install awsebcli --upgrade - eb --version - pip3 install --upgrade awscli pre_build: commands: - AWS_REGION=${AWS_DEFAULT_REGION} - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) - IMAGE_VERSION=${COMMIT_HASH} ...
Question: This is the error I am getting and, as far as I can tell, there is nothing useful on the error link to fix this.  RuntimeError: The current Numpy installation ('...\\venv\\lib\\site-packages\\numpy\\__init__.py') fails to pass a sanity check due to a bug in the windows runtime.  See this issue for more information: https://developercommunity.visualstudio.com/content/problem/1207405/fmod-after-an-update-to-windows-2004-is-causing-a.html I have tried multiple versions of Python (3.8.6 and 3.9.0) and numpy and pandas. I am currently using PyCharm to do all this.

Answer: This error occurs when using python3.9 and numpy1.19.4 So uninstalling numpy1.19.4 and installing 1.19.3 will work.  Edit As of January 5th 2021 numpy version 1.19.5 is out and appears to solve the problem.
Question: I keep getting [Errno 98] Address already in use But the address is not in use. I tried to change the ip and port but It isn't budging.  from fastapi import FastAPI app = FastAPI() @app.get("/") async def main(): return {"message": "Helloworld，FastAPI"} if __name__ == '__main__': import uvicorn uvicorn.run(app, host="127.0.0.1", port=8000)  uvicorn main:app --reload also tried uvicorn main:app --host=172.0.0.2 --port=5000 then it gives  [Errno 99] error while attempting to bind on address ('172.0.0.2', 5000): cannot assign requested address I tried running a flask dev server and it was also running on 172.0.0.1 without a problem? using Arch-Manjaro-Linux I used nmap to see what the fuss was about. But only 2 ports in use on the 127.0.0.1 IP PORT STATE SERVICE 631/tcp open ipp 8000/tcp open http-alt  I would use another IP and port but it gives an error that it can't be assigned.

Answer: Basically, you can do this. This will kill the process that listens TCP connections on port 8000 sudo lsof -t -i tcp:8000 | xargs kill -9
Question: I am given a list of dates in UTC, all hours cast to 00:00. I'd like to determine if a (lunar) eclipse occurred in a given day (ie past 24 hours) Considering the python snippet from sykfield.api import load eph = load('de421.bsp') def eclipticangle(t): moon, earth = eph['moon'], eph['earth'] e = earth.at(t) x, y, _ = e.observe(moon).apparent().ecliptic_latlon() return x.degrees  I am assuming one is able to determine if an eclipse occurred within 24hrs of a time t by  Checking that the first angle is close enough to 180 (easy) Checking if the second degree is close enough to 0 (not so essy?)  Now as far as the answer in the comment suggests it is not so trivial to solve the second problem simply by testing if the angle is close to 0. Therefore, my question is  Can someone provide a function to determine if a lunar eclipse occurred on a given day t?  Edit. This question was edited to reflect the feedback from Brandon Rhodes left in the comments below.

Answer: I just went through section 11.2.3 of the Explanatory Supplement to the Astronomical Almanac and tried turning it into Skyfield Python code. Here is what I came up with: import numpy as np from skyfield.api import load from skyfield.constants import ERAD from skyfield.functions import angle_between, length_of from skyfield.searchlib import find_maxima eph = load('de421.bsp') earth = eph['earth'] moon = eph['moon'] sun = eph['sun'] def f(t): e = earth.at(t).position.au s = sun.at(t).position.au m = moon.at(t).position.au return angle_between(s - e, m - e) f.step_days = 5.0 ts = load.timescale() start_time = ts.utc(2019, 1, 1) end_time = ts.utc(2020, 1, 1) t, y = find_maxima(start_time, end_time, f) e = earth.at(t).position.m m = moon.at(t).position.m s = sun.at(t).position.m solar_radius_m = 696340e3 moon_radius_m = 1.7371e6 pi_m = np.arcsin(ERAD / length_of(m - e)) pi_s = np.arcsin(ERAD / length_of(s - e)) s_s = np.arcsin(solar_radius_m / length_of(s - e)) pi_1 = 0.998340 * pi_m sigma = angle_between(s - e, e - m) s_m = np.arcsin(moon_radius_m / length_of(e - m)) penumbral = sigma < 1.02 * (pi_1 + pi_s + s_s) + s_m partial = sigma < 1.02 * (pi_1 + pi_s - s_s) + s_m total = sigma < 1.02 * (pi_1 + pi_s - s_s) - s_m mask = penumbral | partial | total t = t[mask] penumbral = penumbral[mask] partial = partial[mask] total = total[mask] print(t.utc_strftime()) print(0 + penumbral + partial + total)  It produces a vector of times at which lunar eclipses occurred, and then a rating of how total the eclipse is: ['2019-01-21 05:12:51 UTC', '2019-07-16 21:31:27 UTC'] [3 2]  Its eclipse times are within 3 seconds of the times given in the huge table of lunar ephemerides at NASA: https://eclipse.gsfc.nasa.gov/5MCLE/5MKLEcatalog.txt
Question: Can I implement a generic descriptor in Python in a way it will support/respect/understand inheritance hierarchy of his owners? It should be more clear in the code: from typing import ( Generic, Optional, TYPE_CHECKING, Type, TypeVar, Union, overload, ) T = TypeVar("T", bound="A") # noqa class Descr(Generic[T]): @overload def __get__(self: "Descr[T]", instance: None, owner: Type[T]) -> "Descr[T]": ... @overload def __get__(self: "Descr[T]", instance: T, owner: Type[T]) -> T: ... def __get__(self: "Descr[T]", instance: Optional[T], owner: Type[T]) -> Union["Descr[T]", T]: if instance is None: return self return instance class A: attr: int = 123 descr = Descr[T]() # I want to bind T here, but don't know how class B(A): new_attr: int = 123 qwerty: str = "qwe" if __name__ == "__main__": a = A() if TYPE_CHECKING: reveal_type(a.descr) # mypy guess it is T? but I want A* print("a.attr =", a.descr.attr) # mypy error: T? has no attribute "attr" # no runtime error b = B() if TYPE_CHECKING: reveal_type(b.descr) # mypy said it's T? but I want B* print("b.new_attr =", b.descr.new_attr) # mypy error: T? has no attribute "new_attr" # no runtime error print("b.qwerty =", b.descr.qwerty) # mypy error: T? has no attribute "qwerty" # (no runtime error)  gist - almost the same code snippet on gist

Answer: I am not sure if you need to have the descriptor class as generic; it will probably just suffice to have __get__ on an instance of Type[T] to return T: T = TypeVar("T") # noqa class Descr: @overload def __get__(self, instance: None, owner: Type[T]) -> "Descr": ... @overload def __get__(self, instance: T, owner: Type[T]) -> T: ... def __get__(self, instance: Optional[T], owner: Type[T]) -> Union["Descr", T]: if instance is None: return self return instance class A: attr: int = 123 descr = Descr() class B(A): new_attr: int = 123 qwerty: str = "qwe"  And every example of yours works as you wanted, and you will get an error for print("b.spam =", b.descr.spam)  which produces error: "B" has no attribute "spam"
Question: I am creating a ec2 instance with boto3 and I want print the ip address of that new instance. ec2 = boto3.resource('ec2') # create the instance new_instance = ec2.create_instances( ImageId='###', MinCount = 1, MaxCount = 1, InstanceType = 't2.nano', KeyName = "key", SecurityGroupIds = ["###"] ) ... wait until running ... ip = new_instance[0].ipv4 # something like this  Is there a way to do something like this after it is running?

Answer: ec2.create_instances returns a list of ec2.Instance objects. ec2.Instance objects have an attribute named private_ip_address. You can use that to get the private IP address. A side note (based on the comments in your code example) you can also use the wait_until_running waiter to have your code halt until the instance is running. # create the instance new_instance = ec2.create_instances(...) # use a waiter on the instance to wait until running new_instance[0].wait_until_running() ip = new_instance[0].private_ip_address public_ip = new_instance[0].public_ip_address
Question: I have a few functions in my code that are randomly causing SegmentationFault error. I've identified them by enabling the faulthandler. I'm a bit stuck and have no idea how to reliably eliminate this problem. I'm thinking about some workaround. Since the functions are crashing randomly, I could potentially retry them after a failure. The problem is that there's no way to recover from SegmentationFault crash. The best idea I have for now is to rewrite these functions a bit and run them via subprocess. This solution will help me, that a crashed function won't crash the whole application, and can be retried. Some of the functions are quite small and often executed, so it will significantly slow down my app. Is there any method to execute function in a separate context, faster than a subprocess that won't crash whole program in case of segfault?

Answer: I had some unreliable C extensions throw segfaults every once in a while and, since there was no way I was going to be able to fix that, what I did was create a decorator that would run the wrapped function in a separate process. That way you can stop segfaults from killing the main process. Something like this: https://gist.github.com/joezuntz/e7e7764e5b591ed519cfd488e20311f1 Mine was a bit simpler, and it did the job for me. Additionally it lets you choose a timeout and a default return value in case there was a problem: #! /usr/bin/env python3 # std imports import multiprocessing as mp def parametrized(dec): """This decorator can be used to create other decorators that accept arguments""" def layer(*args, **kwargs): def repl(f): return dec(f, *args, **kwargs) return repl return layer @parametrized def sigsev_guard(fcn, default_value=None, timeout=None): """Used as a decorator with arguments. The decorated function will be called with its input arguments in another process. If the execution lasts longer than *timeout* seconds, it will be considered failed. If the execution fails, *default_value* will be returned. """ def _fcn_wrapper(*args, **kwargs): q = mp.Queue() p = mp.Process(target=lambda q: q.put(fcn(*args, **kwargs)), args=(q,)) p.start() p.join(timeout=timeout) exit_code = p.exitcode if exit_code == 0: return q.get() logging.warning('Process did not exit correctly. Exit code: {}'.format(exit_code)) return default_value return _fcn_wrapper  So you would use it like:  @sigsev_guard(default_value=-1, timeout=60) def your_risky_function(a,b,c,d): ...