eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: So I'm trying to install the PyQt package so I just did this on my Anaconda Prompt: C:\Users\USER>pip install PyQt5 Collecting PyQt5 Using cached PyQt5-5.15.0-5.15.0-cp35.cp36.cp37.cp38-none-win_amd64.whl (64.5 MB) Collecting PyQt5-sip<13,>=12.8 Using cached PyQt5_sip-12.8.0-cp37-cp37m-win_amd64.whl (62 kB) ERROR: spyder 4.1.4 requires pyqtwebengine<5.13; python_version >= "3", which is not installed. ERROR: spyder 4.1.4 has requirement pyqt5<5.13; python_version >= "3", but you'll have pyqt5 5.15.0 which is incompatible.  So I tried a different version with: pip install --upgrade --user pyqt5==5.12  And then this happened: Collecting pyqt5==5.12 Downloading PyQt5-5.12-5.12.1_a-cp35.cp36.cp37.cp38-none-win_amd64.whl (49.4 MB) |████████████████████████████████| 49.4 MB 43 kB/s Collecting PyQt5_sip<4.20,>=4.19.14 Downloading PyQt5_sip-4.19.19-cp37-none-win_amd64.whl (52 kB) |████████████████████████████████| 52 kB 3.8 MB/s ERROR: spyder 4.1.4 requires pyqtwebengine<5.13; python_version >= "3", which is not installed. 

Answer: To install PyQt5 without errors try this. First install pyqtwebengine version 5.12 and then install pyqt5 version 5.12, using the following commands: pip install --upgrade --user pyqtwebengine==5.12 pip install --upgrade --user pyqt5==5.12  By this, I have successfully installed pyqt5
Question: Have a local package ABC-0.0.2-py3-none-any.whl. I want to install it in the different project through requrements.txt. e.g. requirements.txt ABC==0.0.2 Flask==1.1.2 flask-restplus==0.13.0 gunicorn==20.0.4  Is it possible to install the ABC package this way. ABC-0.0.2-py3-none-any.whl is included in source code. I had to pip install ABC-0.0.2-py3-none-any.whl separately.

Answer: This is called a direct reference. Since version 19.3, pip support this in both command line and requirement files. Check out an example from the official documentation. As to OP's question, simply put the local wheel's relative path, i.e., ./<my_wheel_dir>/<my_wheel.whl>, in requirement.txt, e.g., ./local_wheels/ABC-0.0.2-py3-none-any.whl Flask==1.1.2 flask-restplus==0.13.0 gunicorn==20.0.4
Question: I am using Typeguard in a couple if projects for type checking at run time in Python. It works pretty well. I have encountered a situation where the type of a function parameter is a typing.Union made up of a few dynamically collected data types. E.g. def find_datatypes(): # some stuff ... return (str, int) # dynamically generated list / tuple datatypes = find_datatypes()  Now I want to generate a typing.Union from datatypes for eventual use in a function. I expected unpacking syntax to work: my_union = typing.Union[*datatypes] @typeguard.typechecked def some_function(param: my_union): pass  However, it did not:  my_union = typing.Union[*datatypes] ^ SyntaxError: invalid syntax  How would I achieve what I want?

Answer: You can kind of do it: my_union = typing.Union[datatypes]  At runtime, thing[x, y] is already equivalent to thing[(x, y)]. That said, there are limitations to keep in mind. Particularly, when using string annotations, my_union will have to be available in some_function's global namespace for typeguard or anything else to be able to resolve the annotation at runtime. That restricts a lot of closure use cases, and a lot of attempts to add annotations dynamically. (String annotations may become the default eventually, but the devs are considering other options, and the plans are currently unclear.) Also, as you might expect, mypy will not consider any of this valid.
Question: Good afternoon. I'm sorry if my question may seem dumb or if it has already been posted (I looked for it but didn't seem to find anything. If I'm wrong, please let me know: I'm new here and I may not be the best at searching for the correct questions). I was wondering if it was possible to remove (pop) a generic item from a dictionary in python. The idea came from the following exercise:  Write a function to find the sum of the VALUES in a given dictionary.  Obviously there are many ways to do it: summing dictionary.values(), creating a variable for the sum and iterate through the dict and updating it, etc.. But I was trying to solve it with recursion, with something like: def total_sum(dictionary): if dictionary == {}: return 0 return dictionary.pop() + total_sum(dictionary)  The problem with this idea is that we don't know a priori which could be the "first" key of a dict since it's unordered: if it was a list, the index 0 would have been used and it all would have worked. Since I don't care about the order in which the items are popped, it would be enough to have a way to delete any of the items (a "generic" item). Do you think something like this is possible or should I necessarily make use of some auxiliary variable, losing the whole point of the use of recursion, whose advantage would be a very concise and simple code? I actually found the following solution, which though, as you can see, makes the code more complex and harder to read: I reckon it could still be interesting and useful if there was some built-in, simple and direct solution to that particular problem of removing the "first" item of a dict, although many "artificious", alternative solutions could be found. def total_sum(dictionary): if dictionary == {}: return 0 return dictionary.pop(list(dictionary.keys())[0]) + total_sum(dictionary)  I will let you here a simple example dictionary on which the function could be applied, if you want to make some simple tests. ex_dict = {"milk":5, "eggs":2, "flour": 3} 

Answer: ex_dict.popitem() it removes the last (most recently added) element from the dictionary
Question: How can i redirect a request with FastAPI if there is a HTTPException? In Flask we can achieve that like this: @app.errorhandler(404) def handle_404(e): if request.path.startswith('/api'): return render_template('my_api_404.html'), 404 else: return redirect(url_for('index'))  Or in Django we can use django.shortcuts: from django.shortcuts import redirect def view_404(request, exception=None): return redirect('/')  How we can achieve that with FastAPI?

Answer: I know it's too late but this is the shortest approach to handle 404 exceptions in your personal way. Redirect from fastapi.responses import RedirectResponse @app.exception_handler(404) async def custom_404_handler(_, __): return RedirectResponse("/")  Custom Jinja Template from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles templates = Jinja2Templates(directory="templates") app.mount("/static", StaticFiles(directory="static"), name="static") @app.exception_handler(404) async def custom_404_handler(request, __): return templates.TemplateResponse("404.html", {"request": request})  Serve HTML from file @app.exception_handler(404) async def custom_404_handler(_, __): return FileResponse('./path/to/404.html')  Serve HTML directly from fastapi.responses import HTMLResponse response_404 = """ <!DOCTYPE html> <html lang="en"> <head> <title>Not Found</title> </head> <body> <p>The file you requested was not found.</p> </body> </html> """ @app.exception_handler(404) async def custom_404_handler(_, __): return HTMLResponse(response_404)  Note: exception_handler decorator passes the current request and exception as arguments to the function. I've used _ and __ where the variables are unnecessary.
Question: I just started working with keras and noticed that there are two layers with very similar names for max-pooling: MaxPool and MaxPooling. I was surprised that I couldn't find the difference between these two on Google; so I am wondering what the difference is between the two if any.

Answer: They are the same... You can test it on your own import numpy as np import tensorflow as tf from tensorflow.keras.layers import * # create dummy data X = np.random.uniform(0,1, (32,5,3)).astype(np.float32) pool1 = MaxPool1D()(X) pool2 = MaxPooling1D()(X) tf.reduce_all(pool1 == pool2) # True  I used 1D max-pooling but the same is valid for all the pooling operations (2D, 3D, avg, global pooling)
Question: Do you know if there is a way to collect all pytest node ids (as presented here) using the pytest python API ? I have found the --collect-only parameter of pytest, but I can't figure out how to get the output using python ? Thanks in advance !

Answer: If you want to access nodeids programmatically, best is to write a small plugin that will store them on test collection. Example: import pytest class NodeidsCollector: def pytest_collection_modifyitems(self, items): self.nodeids = [item.nodeid for item in items] def main(): collector = NodeidsCollector() pytest.main(['--collect-only'], plugins=[collector]) # use collector.nodeids now  If you want to avoid pytest output to the terminal, disable the terminal plugin in addition: pytest.main(['--collect-only', '-pno:terminal'], plugins=[collector])
Question: this piece of code was working before, however, after creating a new environment , it stopped working for the line plt.xticks(x, months, rotation=25,fontsize=8)  if i comment this line then no error, after putting this line error is thrown ValueError: The number of FixedLocator locations (5), usually from a call to set_ticks, does not match the number of ticklabels (12).   import numpy as np import matplotlib.pyplot as plt dataset = df dfsize = dataset[df.columns[0]].size x = [] for i in range(dfsize): x.append(i) dataset.shape # dataset.dropna(inplace=True) dataset.columns.values var = "" for i in range(dataset.shape[1]): ## 1 is for column, dataset.shape[1] calculate length of col y = dataset[dataset.columns[i]].values y = y.astype(float) y = y.reshape(-1, 1) y.shape from sklearn.impute import SimpleImputer missingvalues = SimpleImputer(missing_values=np.nan, strategy='mean', verbose=0) missingvalues = missingvalues.fit(y) y = missingvalues.transform(y[:, :]) from sklearn.preprocessing import LabelEncoder, OneHotEncoder from sklearn.compose import ColumnTransformer labelencoder_x = LabelEncoder() x = labelencoder_x.fit_transform(x) from scipy.interpolate import * p1 = np.polyfit(x, y, 1) # from matplotlib.pyplot import * import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.figure() plt.xticks(x, months, rotation=25,fontsize=8) #print("-->"+dataset.columns[i]) plt.suptitle(dataset.columns[i] + ' (xyz)', fontsize=10) plt.xlabel('month', fontsize=8) plt.ylabel('Age', fontsize=10) plt.plot(x, y, y, 'r-', linestyle='-', marker='o') plt.plot(x, np.polyval(p1, x), 'b-') y = y.round(decimals=2) for a, b in zip(x, y): plt.text(a, b, str(b), bbox=dict(facecolor='yellow', alpha=0.9)) plt.grid() # plt.pause(2) # plt.grid() var = var + "," + dataset.columns[i] plt.savefig(path3 + dataset.columns[i] + '_1.png') plt.close(path3 + dataset.columns[i] + '_1.png') plt.close('all') 

Answer: I also stumbled across the error and found that making both your xtick_labels and xticks a list of equal length works. So in your case something like : def month(num): # returns month name based on month number num_elements = len(x) X_Tick_List = [] X_Tick_Label_List=[] for item in range (0,num_elements): X_Tick_List.append(x[item]) X_Tick_Label_List.append(month(item+1)) plt.xticks(ticks=X_Tick_List,labels=X_Tick_LabeL_List, rotation=25,fontsize=8)
Question: I managed to read data from a Google Sheet file using this method: # ACCES GOOGLE SHEET googleSheetId = 'myGoogleSheetId' workSheetName = 'mySheetName' URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format( googleSheetId, workSheetName ) df = pd.read_csv(URL)  However, after generating a pd.DataFrame that fetches info from the web using selenium, I need to append that data to the Google Sheet. Question: Do you know a way to export that DataFrame to Google Sheets?

Answer: Yes, there is a module called "gspread". Just install it with pip and import it into your script. Here you can find the documentation: https://gspread.readthedocs.io/en/latest/ In particular their section on Examples of gspread with pandas. worksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())
Question: I'm trying to use functools.lru_cache to cache the result of an inner function, but the cache doesn't seem to work as expected. I have a function that performs some logic and then calls a rather expensive function. I'd like to cache the result of the expensive function call and though I'd just apply the lru_cache to an inner function. Unfortunately the behavior isn't as expected - the expensive function gets called every time even though the arguments to the inner function are identical. I've created a (simplified) test case to show the behavior: import unittest from functools import lru_cache from unittest.mock import patch def expensive_function(parameter: str) -> str: return parameter def partially_cached(some_parameter: str) -> str: @lru_cache def inner_function(parameter: str): return expensive_function(parameter) result = inner_function(some_parameter) print(inner_function.cache_info()) return result class CacheTestCase(unittest.TestCase): def test_partially_cached(self): with patch(self.__module__ + ".expensive_function") as expensive_mock: expensive_mock.return_value = "a" self.assertEqual(partially_cached("a"), "a") self.assertEqual(partially_cached("a"), "a") # If the cache works, I expect the expensive function # to be called just once for the same parameter expensive_mock.assert_called_once() if __name__ == "__main__": unittest.main()  (In this case I wouldn't need the inner function, but as I said - it's simplified) Unfortunately the test fails python3 /scratch/test_cache.py CacheInfo(hits=0, misses=1, maxsize=128, currsize=1) CacheInfo(hits=0, misses=1, maxsize=128, currsize=1) F ====================================================================== FAIL: test_partially_cached (__main__.CacheTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): [...] AssertionError: Expected 'expensive_function' to have been called once. Called 2 times. Calls: [call('a'), call('a')]. ---------------------------------------------------------------------- Ran 1 test in 0.004s FAILED (failures=1)  I probably have a misunderstanding concerning either inner functions or the lru_cache, but I'm not sure which it is - I appreciate all the help I can get.

Answer: It does not work as intended because the inner_function gets redefined each time partially_cached is called, and so is the cached version. So each cached version gets called only once. See memoizing-decorator-keeping-stored-values Additionally, if you mock a decorated function you need to apply the decorator again. See how-to-mock-a-decorated-function If you decorate at the outer level, or decorate another extracted to outer level function, it will work, if you don't break it with mocking.
Question: Is there a way to automatically add contour (iso-z) lines to a heatmap with concrete x and y values? Please consider the official seaborn flights dataset: import seaborn as sns flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") sns.heatmap(flights, annot=True, fmt='d')  I imagine the step-like lines to look something like shown below (lhs), indicating thresholds (here 200 and 400). They do not need to be interpolated or smoothed in any way, although that would do as well, if easier to realize. If the horizontal lines complicate the solution further, they too could be omitted (rhs).  So far, I have tried to add hlines and vlines manually, to overlay a kdeplot etc. without the desired result. Could somebody hint me into the right direction?

Answer: You can use aLineCollection: import seaborn as sns import numpy as np from matplotlib.collections import LineCollection flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") ax = sns.heatmap(flights, annot=True, fmt='d') def add_iso_line(ax, value, color): v = flights.gt(value).diff(axis=1).fillna(False).to_numpy() h = flights.gt(value).diff(axis=0).fillna(False).to_numpy() try: l = np.argwhere(v.T) vlines = np.array(list(zip(l, np.stack((l[:,0], l[:,1]+1)).T))) l = np.argwhere(h.T) hlines = np.array(list(zip(l, np.stack((l[:,0]+1, l[:,1])).T))) lines = np.vstack((vlines, hlines)) ax.add_collection(LineCollection(lines, lw=3, colors=color )) except: pass add_iso_line(ax, 200, 'b') add_iso_line(ax, 400, 'y')
Question: When I load the BERT pretrained model online I get this error OSError: Error no file named ['pytorch_model.bin', 'tf_model.h5', 'model.ckpt.index'] found in directory uncased_L-12_H-768_A-12 or 'from_tf' set to False what should I do?

Answer: Here is what I found. Go to the following link, and click the circled to download, rename it to pytorch_model.bin, and drop it to the directory of biobert-nli, then the issue is resolved. Didn't figure out how to clone from the link. https://huggingface.co/gsarti/biobert-nli/tree/main
Question: Suppose I'm writing code using pathlib and I want to iter over all the files in the same level of a directory. I can do this in two ways: p = pathlib.Path('/some/path') for f in p.iterdir(): print(f)  p = pathlib.Path('/some/path') for f in p.glob('*'): print(f)  Is one of the options better in any way?

Answer: Expansion of my comment: Why put the API to extra work parsing and testing against a filter pattern when you could just... not? glob is better when you need to make use of the filtering feature and the filter is simple and string-based, as it simplifies the work. Sure, hand-writing simple matches (filtering iterdir via if path.endswith('.txt'): instead of glob('*.txt')) might be more efficient than the regex based pattern matching glob hides, but it's generally not worth the trouble of reinventing the wheel given that disk I/O is orders of magnitude slower. But if you don't need the filtering functionality at all, don't use it. glob is gaining you nothing in terms of code simplicity or functionality, and hurting performance, so just use iterdir.
Question: I have generated multiple point clouds using a RGB+depth video, and would like to visualize the multiple point clouds as a video or animation. Currently I am using Python, part of my code is as follows: for i in range(1,10) pcd = Track.create_pcd(i) o3d.visualization.draw_geometries([pcd]) pcd_list.append(pcd)  When I use draw_geometries or draw_geometries_with_animation_callback, it seems they could not display a list of point clouds: o3d.visualization.draw_geometries([pcd_list])  or def rotate_view(vis): ctr = vis.get_view_control() ctr.rotate(10.0, 0.0) return False o3d.visualization.draw_geometries_with_animation_callback([pcd_list],rotate_view)  It gave the following error:  TypeError: draw_geometries(): incompatible function arguments. The following argument types are supported:  (geometry_list: List[open3d.open3d_pybind.geometry.Geometry], window_name: str = ‘Open3D’, width: int = 1920, height: int = 1080, left: int = 50, top: int = 50, point_show_normal: bool = False, mesh_show_wireframe: bool = False, mesh_show_back_face: bool = False) -> None   Is there any example of how to export list of point cloud into a video, like setting a viewer, and displaying each point cloud with a waitkey of 0.5 seconds, and then save as a video file (.mp4/.avi)? And also to get and then set a fixed viewpoint of the point clouds in the video? Thank you very much!

Answer: You can use Open3D Non-blocking visualization. It'll be like this vis = o3d.visualization.Visualizer() vis.create_window() # geometry is the point cloud used in your animaiton geometry = o3d.geometry.PointCloud() vis.add_geometry(geometry) for i in range(icp_iteration): # now modify the points of your geometry # you can use whatever method suits you best, this is just an example geometry.points = pcd_list[i].points vis.update_geometry(geometry) vis.poll_events() vis.update_renderer()
Question: When I run my FastAPI server using uvicorn: uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info  The log I get after running the server: INFO: Started server process [405098] INFO: Waiting for application startup. INFO: Connect to database... INFO: Successfully connected to the database! INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: 122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK  How do I get the time stamp along with the request logging? Like: INFO: "2020-07-16:23:34:78" - 122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK 

Answer: You can use Uvicorn's LOGGING_CONFIG import uvicorn from uvicorn.config import LOGGING_CONFIG from fastapi import FastAPI app = FastAPI() def run(): LOGGING_CONFIG["formatters"]["default"]["fmt"] = "%(asctime)s [%(name)s] %(levelprefix)s %(message)s" uvicorn.run(app) if __name__ == '__main__': run()  Which will return uvicorn log with the timestamp 2020-08-20 02:33:53,765 [uvicorn.error] INFO: Started server process [107131] 2020-08-20 02:33:53,765 [uvicorn.error] INFO: Waiting for application startup. 2020-08-20 02:33:53,765 [uvicorn.error] INFO: Application startup complete. 2020-08-20 02:33:53,767 [uvicorn.error] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Question: I'm writing a Python project which is published as a package to a pypi-like repository (using setuptools and twine). I use type hints in my code. The issue is, when importing the package from a different project and running mypy, I get the following error: error: Skipping analyzing 'XXX': found module but no type hints or library stubs As I understand, I got this error because my package was not compliant with https://www.python.org/dev/peps/pep-0561/ . After some searching online, I didn't find a way that was not manual to add the required files to the package. I resorted to writing my own code to:  Run stubgen to create stub files. Create py.typed files in every directory. Collect all the created files in a dict in package_data field in the setup.py file.  This code solved the issue and mypy runs without errors. But this feels very wrong to me. Is there a standard tool for making a package PEP-561 compliant? Am I missing something else?

Answer: As mentioned before, You need to add the py.typed in the package folder of the module. You also need to add that file to the setup.py package_data - otherwise the file would not be part of the package when You deploy it. I personally put the type annotations in the code and dont create extra stub files - but that is only possible from python 3.4 upwards. If You want to make python2.7 compatible code, You can not use inline type annotation - in that case You can use stub files. If You want to type annotate a third party library, You can write a *.pyi file for the functions You use for that library. That can be a bit tricky, because MYPY must only find that *.pyi file ONCE in the MYPY Path. So I handle it that way : for local testing, the MYPY path is set to a directory were I collect all the 3rd party stubs, for testing on travis, I have a subdirectory in the package with the stubs needed for that module to test it on travis, and set the mypy path accordingly.