eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: For approaches to retrieving partial matches in a numeric list, go to:  How to return a subset of a list that matches a condition?  Python: Find in list    But if you're looking for how to retrieve partial matches for a list of strings, you'll find the best approaches concisely explained in the answer below. SO: Python list lookup with partial match shows how to return a bool, if a list contains an element that partially matches (e.g. begins, ends, or contains) a certain string. But how can you return the element itself, instead of True or False Example: l = ['ones', 'twos', 'threes'] wanted = 'three'  Here, the approach in the linked question will return True using: any(s.startswith(wanted) for s in l)  So how can you return the element 'threes' instead?

Answer:  startswith and in, return a Boolean. The in operator is a test of membership. This can be performed with a list-comprehension or filter. Using a list-comprehension, with in, is the fastest implementation tested. If case is not an issue, consider mapping all the words to lowercase.  l = list(map(str.lower, l)).   Tested with python 3.11.0  filter:  Using filter creates a filter object, so list() is used to show all the matching values in a list.  l = ['ones', 'twos', 'threes'] wanted = 'three' # using startswith result = list(filter(lambda x: x.startswith(wanted), l)) # using in result = list(filter(lambda x: wanted in x, l)) print(result) [out]: ['threes']  list-comprehension l = ['ones', 'twos', 'threes'] wanted = 'three' # using startswith result = [v for v in l if v.startswith(wanted)] # using in result = [v for v in l if wanted in v] print(result) [out]: ['threes']  Which implementation is faster?  Tested in Jupyter Lab using the words corpus from nltk v3.7, which has 236736 words Words with 'three'  ['three', 'threefold', 'threefolded', 'threefoldedness', 'threefoldly', 'threefoldness', 'threeling', 'threeness', 'threepence', 'threepenny', 'threepennyworth', 'threescore', 'threesome']    from nltk.corpus import words %timeit list(filter(lambda x: x.startswith(wanted), words.words())) %timeit list(filter(lambda x: wanted in x, words.words())) %timeit [v for v in words.words() if v.startswith(wanted)] %timeit [v for v in words.words() if wanted in v]  %timeit results 62.8 ms ± 816 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 53.8 ms ± 982 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 56.9 ms ± 1.33 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 47.5 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Question: Is there any difference in the return type hint amongst these three functions? def my_func1(): print("Hello World") return None def my_func2(): print("Hello World") return def my_func3(): print("Hello World")  Should they all have -> None return type hint, since that's what they in fact return, explicitly or implicitly? Or should my_func2 or my_func3 have literally no return type hint? Motivations for asking are this question, and this nice answer, and the fact that I'm learning Type Hints.

Answer: They all should have the -> None return type, since they all clearly return None. Note that there also exists the typing.NoReturn type for functions that actually never return anything, e.g. from typing import NoReturn def raise_err() -> NoReturn: raise AssertionError("oops an error")  Other types of functions (pointed out by @chepner) that actually never return and thus should be type hinted with -> NoReturn would be for example an event loop that only ends using sys.exit or any of the os.exec* functions  Or should my_func2 or my_func3 have literally no return type hint?  In my opinion, they should always have a type hint, since as @yedpodtrzitko said in their answer, functions with no type hints are by default not type checked by Mypy at all, and their return values are basically treated as if they would've been typed to be Any. This greatly reduces the benefits of type checking, and that's one of the reasons I always use the Mypy setting disallow_untyped_defs = True for new projects.
Question: My Python test script causes our product to raise Windows notifications ("Toasts"). How can my python script verify that the notifications are indeed raised? I see it's possible to make a notification listener in C# using Windows.UI.Notifications.Management.UserNotificationListener (ref), And I see I can make my own notifications in Python using win10toast - but how do I listen to othe apps' notifications?

Answer: You can use pywinrt to access the bindings in python. A basic example would look something like this: from winrt.windows.ui.notifications.management import UserNotificationListener, UserNotificationListenerAccessStatus from winrt.windows.ui.notifications import NotificationKinds, KnownNotificationBindings if not ApiInformation.is_type_present("Windows.UI.Notifications.Management.UserNotificationListener"): print("UserNotificationListener is not supported on this device.") exit() listener = UserNotificationListener.get_current() accessStatus = await listener.request_access_async() if accessStatus != UserNotificationListenerAccessStatus.ALLOWED: print("Access to UserNotificationListener is not allowed.") exit() def handler(listener, event): notification = listener.get_notification(event.user_notification_id) # get some app info if available if hasattr(notification, "app_info"): print("App Name: ", notification.app_info.display_info.display_name) listener.add_notification_changed(handler)
Question: How do I type hint an optional output parameter: def myfunc( x: float, return_y: bool = False ) -> float, Optional[float] : # !!! WRONG !!! # ... code here # # y and z are floats if return_y: return z, y return z  --- edit  This seem to work: -> Tuple[float, Union[None, float]] :  but that is so ugly and seems to overwhelm the fact that typically it will only return a simple float. Is that the correct way to do this?  See answer below for correct way. --- edit 2 This question was flagged as duplicate of that question. However, that question is about Union return type, while this question is about Optional return type.  Note: this design is not a good practice and should be avoided in favour of a consistent return type. Still, in case it has to be done, by optional output parameter it is meant a parameter that may not be returned depending on an input argument's flag.

Answer: Since Python 3.10 and PEP 604 you now can use | instead of Union. The return type would be float | Tuple[float, float]  The right type hint would be: from typing import Tuple, Union def myfunc(x: float, return_y: bool = False) -> Union[float, Tuple[float, float]]: z = 1.5 if return_y: y = 2.0 return z, y return z  However, it is usually not a good practice to have these kinds of return. Either return something like Tuple[float, Optional[float]] or write multiple functions, it will be much easier to handle later on. More about return statement consistency:  PEP8 - Programming Recommendations   Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).   Why should functions return values of a consistent type?
Question: There is np.stack in NumPy, but is there an opposite np.unstack same as tf.unstack?

Answer: Coming across this late, here is a much simpler answer: def unstack(a, axis=0): return np.moveaxis(a, axis, 0) # return list(np.moveaxis(a, axis, 0))  As a bonus, the result is still a numpy array. The unwrapping happens if you just python-unwrap it: A, B, = unstack([[1, 2], [3, 4]], axis=1) assert list(A) == [1, 3] assert list(B) == [2, 4]  Unsurprisingly, it is also the fastest: # np.squeeze ❯ python -m timeit -s "import numpy as np; a=np.array(np.meshgrid(np.arange(1000), np.arange(1000)));" "C = [np.squeeze(e, 1) for e in np.split(a, a.shape[1], axis = 1)]" 100 loops, best of 5: 2.64 msec per loop # np.take ❯ python -m timeit -s "import numpy as np; a=np.array(np.meshgrid(np.arange(1000), np.arange(1000)));" "C = [np.take(a, i, axis = 1) for i in range(a.shape[1])]" 50 loops, best of 5: 5.08 msec per loop # np.moveaxis ❯ python -m timeit -s "import numpy as np; a=np.array(np.meshgrid(np.arange(1000), np.arange(1000)));" "C = np.moveaxis(a, 1, 0)" 100000 loops, best of 5: 3.89 usec per loop # list(np.moveaxis) ❯ python -m timeit -s "import numpy as np; a=np.array(np.meshgrid(np.arange(1000), np.arange(1000)));" "C = list(np.moveaxis(a, 1, 0))" 1000 loops, best of 5: 205 usec per loop
Question: I am trying to call a C++ function from a Python script. I have seen different solutions on Stackoverflow from 2010-2015 but they are all using complicated packages and was hoping for something easier/newer and more sophisticated. The C++ function I am trying to call takes in a double variable and returns a double. double foo(double var1){ double result = ... return result; } 

Answer: Python has ctypes package which allows calling functions in DLLs or shared libraries. Compile your C++ project into a shared library (.so) on Linux, or DLL on Windows. Export the functions you wish to expose outside. C++ supports function overloading, to avoid ambiguity in the binary code, additional information is added to function names, known as name mangling. To ensure no name is changed, place inside an extern "C" block. More on importance of extern "C" at the end!  Demo: In this dummy demo, our library has a single function, taking an int and printing it. lib.cpp #include <iostream> int Function(int num) { std::cout << "Num = " << num << std::endl; return 0; } extern "C" { int My_Function(int a) { return Function(a); } }  We will compile this into a shared object first g++ -fPIC -shared -o libTest.so lib.cpp   Now we will utilized ctypes, to load the shared object/dll and functions. myLib.py import ctypes import sys import os dir_path = os.path.dirname(os.path.realpath(__file__)) handle = ctypes.CDLL(dir_path + "/libTest.so") handle.My_Function.argtypes = [ctypes.c_int] def My_Function(num): return handle.My_Function(num)   For our test, we will call the function with num = 16 test.py from myLib import * My_Function(16)   The expected out as well.   EDIT: Comment section does not understand the importance of extern "C". As already explained above, C++ supports function overloading and additional information is added to function names known as name mangling. Consider the following library #include <iostream> int My_Function(int num) { std::cout << "Num = " << num << std::endl; return 0; }  Compiled the same: g++ -fPIC -shared -o libTest.so lib.cpp. Listing the exported symbols with nm -gD libTest.so results in:  Notice how the function name in the exported symbols is changed to _Z11My_Functioni. Running test.py now fails as it can not find the symbol.  You'd have to change myLib.py to reflect the change. However, you do not compile your library, take a look at the resulting symbol and build your module extension because there's no guarantee that re-compiling in the future with different version and additional code will result in the same symbol names. This is why one uses extern "C". Notice how in the first code the function name is unchanged.
Question: I have the following structure of my AWS lambda project: module app.py b.py  app.py is my default aws lambda function with lambda_handler, it works fine. I decided to pull all the heavy calculations out of it to function calc of b.py. Then, I imported it to app.py: from module.b import calc  Now, when I run it locally with sam local invoke Function --event events/event.json, it raises an error:  {"errorType":"Runtime.ImportModuleError","errorMessage":"Unable to import module 'app': No module named 'module'"}  It seems to me that when it prepares the code to run, it moves the files to some other directory, so the imports break. To fix this, I tried to use relative import: from .b import calc  But it also raised an error:  {"errorType":"Runtime.ImportModuleError","errorMessage":"Unable to import module 'app': attempted relative import with no known parent package"}  How do I setup a multi-file python application on aws lambda?

Answer: This is how me resolve that problem. First your root folder need to seem like this: lambda_folder lambda_function.py // Or your main.py.... this file have the method lambda_handler  Now... When I use multiple files... I always use a lib folder. Like this: lambda_folder lib lib1.py lib2.py lib3.py lambda_function.py  IMPORTANT Inside your lib folder you always need an __init__.py or you can't see the files inside. lambda_folder lib lib1.py lib2.py lib3.py __init__.py lambda_function.py  NOTE: the __init__.py needs to have the two underscores before and after init. EXAMPLE lib1.py def sum(a,b): return a+b  lambda_function.py from lib import lib1 import json def lambda_handler(event, context): result = lib.sum(5,4) return { "statusCode": 200, "body": "hi " + result }  And that's all.
Question: I got the following error installing a dependency with pip:  pip9.exceptions.InstallationError Command "python setup.py egg_info" failed with error code 1 in /tmp/tmpoons7qgkbuild/opencv-python/  Below is the result of running the command pipenv install opencv-python on a recent linux (5.4.0 x64) system. Locking [packages] dependencies… self.repository.get_dependencies(ireq): File "/usr/lib/python3/dist-packages/pipenv/patched/piptools/repositories/pypi.py", line 174, in get_dependencies legacy_results = self.get_legacy_dependencies(ireq) File "/usr/lib/python3/dist-packages/pipenv/patched/piptools/repositories/pypi.py", line 222, in get_legacy_dependencies result = reqset._prepare_file(self.finder, ireq, ignore_requires_python=True) File "/usr/lib/python3/dist-packages/pipenv/patched/notpip/req/req_set.py", line 644, in _prepare_file abstract_dist.prep_for_dist() File "/usr/lib/python3/dist-packages/pipenv/patched/notpip/req/req_set.py", line 134, in prep_for_dist self.req_to_install.run_egg_info() File "/usr/lib/python3/dist-packages/pipenv/vendor/pip9/req/req_install.py", line 435, in run_egg_info call_subprocess( File "/usr/lib/python3/dist-packages/pipenv/vendor/pip9/utils/__init__.py", line 705, in call_subprocess raise InstallationError( pip9.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in /tmp/tmpoons7qgkbuild/opencv-python/ 

Answer: How to fix the pip9.exceptions.InstallationError Make sure the version of your pip and setuptools is sufficient for manylinux2014 wheels. A) System Install sudo python3 -m pip install -U pip sudo python3 -m pip install -U setuptools  B) Virtual Env / Pipenv # Within the venv pip3 install -U pip pip3 install -U setuptools  Explanation For me, python setup.py egg_info probably failed because of a recent change in python wheels, as manylinux1 wheels were replaced by manylinux2014 wheels according to open-cv faq.
Question: Is there a way to get the most recent message of a specific channel using discord.py? I looked at the official docs and didn't find a way to.

Answer: I've now figured it out by myself: For a discord.Client class you just need these lines of code for the last message:  (await self.get_channel(CHANNEL_ID).history(limit=1).flatten())[0]  If you use a discord.ext.commands.Bot @thegamecracks' answer is correct.
Question: Is it somehow possible to exclude certain part of the code when reloading the scrip with --reload flag? uvicorn main:app --reload  Use case: I have a model which takes a lot of time loading so I was wondering if there is a way to ignore that line of code when reloading. Or is it just impossible?

Answer: Update Uvicorn now supports including/excluding certain directories/files to/from watchlist.  --reload-include TEXT Set glob patterns to include while watching for files. Includes '*.py' by default, which can be overridden in reload-excludes. --reload-exclude TEXT Set glob patterns to exclude while watching for files. Includes '.*, .py[cod], .sw.*, ~*' by default, which can be overridden in reload-excludes.   No there is no way to exclude something, however you can be explicit in what you want to be looked at with the --reload-dir flag:  --reload-dir TEXT Set reload directories explicitly, instead of using the current working directory.  in https://www.uvicorn.org/#command-line-options
Question: hi im having error with this code but it runs in python shell could any body help me from machine import Pin import time import network import urequests p0 = Pin(0,Pin.OUT) wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect('ssid', 'pass') response = urequests.get('http://jsonplaceholder.typicode.com/albums/1') while True: ans = response.json()['userId'] p0.value(1) time.sleep(1) p0.off() time.sleep(1) print('ok')  and this is the error: Traceback (most recent call last): File "<stdin>", line 9, in <module> File "urequests.py", line 108, in get File "urequests.py", line 53, in request OSError: -202 

Answer: Your issue (my guess) is that you begin to urequest.get() without connected to WiFi. Create function that do wifi connection and call it def do_connect(): import network wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') wlan.connect('essid', 'password') while not wlan.isconnected(): pass print('network config:', wlan.ifconfig())  Explain: wlan.connect() is asynchronous function and you have to wait, while it connects to wifi and only then continue with urequest.get()
Question: For some reason, PyJTW doesn't seem to work on my virtualenv on Ubuntu 16.04, but it worked fine on my local Windows machine (inside a venv too). I'm clueless, I've tried different versions, copied the exact same versions as I had on my Windows machine, and yet I still couldn't get this to work. Installed packages: Package Version -------------------------- --------- aiohttp 3.6.2 async-timeout 3.0.1 attrs 20.2.0 cachetools 4.1.1 certifi 2020.6.20 cffi 1.14.3 chardet 3.0.4 click 7.1.2 cryptography 2.9.2 DateTime 4.3 discord.py 1.5.0 Flask 1.1.2 Flask-Discord 0.1.61 flask-oidc 1.4.0 flask-oidc2 1.5.0 httplib2 0.18.1 idna 2.10 itsdangerous 1.1.0 Jinja2 2.11.2 jwt 1.0.0 MarkupSafe 1.1.1 multidict 4.7.6 mysql-connector-python 8.0.21 mysql-connector-repackaged 0.3.1 oauth2client 4.1.3 oauthlib 3.1.0 pip 20.2.3 protobuf 3.13.0 pyasn1 0.4.8 pyasn1-modules 0.2.8 pycparser 2.20 PyJWT 1.7.1 pytz 2020.1 requests 2.24.0 requests-oauthlib 1.3.0 rsa 4.6 schedule 0.6.0 setuptools 50.3.0 six 1.15.0 typing-extensions 3.7.4.3 urllib3 1.25.10 Werkzeug 1.0.1 wheel 0.35.1 yarl 1.6.0 zope.interface 5.1.0  The error: [2020-09-29 21:58:44 +0000] [2036] [ERROR] Exception in worker process Traceback (most recent call last): File "/usr/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spaw n_worker worker.init_process() File "/usr/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process self.load_wsgi() File "/usr/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/usr/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in l oad return self.load_wsgiapp() File "/usr/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in l oad_wsgiapp return util.import_app(self.app_uri) File "/usr/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_ app mod = importlib.import_module(module) File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/soro/soros-dashboard/wsgi.py", line 1, in <module> from app import app File "/home/soro/soros-dashboard/app.py", line 9, in <module> import keycloak File "/home/soro/soros-dashboard/keycloak.py", line 4, in <module> from jwt.algorithms import RSAAlgorithm ModuleNotFoundError: No module named 'jwt.algorithms'  I'm running Python 3.7.7.

Answer: I had the same issue. The error seems to be a conflict between the pyjwt and jwt modules (as mentioned by @vimalloc above). What worked for me was to run the following command (NOTE: I am using Python 3.6.10). pip3 install -U pyjwt
Question: So I have the following Python3 script to list all virtual machines. import os, json from azure.mgmt.compute import ComputeManagementClient from azure.mgmt.network import NetworkManagementClient from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient from azure.common.credentials import ServicePrincipalCredentials credentials = ServicePrincipalCredentials( client_id="xxx", secret="xxx", tenant="xxx" ) resource_client = ResourceManagementClient(credentials, "my-subscription") compute_client = ComputeManagementClient(credentials, "my-subscription") network_client = NetworkManagementClient(credentials, "my-subscription") for vm in compute_client.virtual_machines.list_all(): print("\tVM: {}".format(vm.name))  but for some reason, I get the following error: Traceback (most recent call last): File "/Users/me/a/azure-test.py", line 17, in <module> for vm in compute_client.virtual_machines.list_all(): ... File "/usr/local/lib/python3.8/site-packages/azure/core/pipeline/policies/_authentication.py", line 93, in on_request self._token = self._credential.get_token(*self._scopes) AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'  Am I doing something wrong?

Answer: The Azure libraries for Python are currently being updated to share common cloud patterns such as authentication protocols, logging, tracing, transport protocols, buffered responses, and retries. This would change the Authentication mechanism a bit as well. In the older version, ServicePrincipalCredentials in azure.common was used for authenticating to Azure and creating a service client. In the newer version, the authentication mechanism has been re-designed and replaced by azure-identity library in order to provide unified authentication based on Azure Identity for all Azure SDKs. Run pip install azure-identity to get the package. In terms of code, what then was: from azure.common.credentials import ServicePrincipalCredentials from azure.mgmt.compute import ComputeManagementClient credentials = ServicePrincipalCredentials( client_id='xxxxx', secret='xxxxx', tenant='xxxxx' ) compute_client = ComputeManagementClient( credentials=credentials, subscription_id=SUBSCRIPTION_ID )  is now: from azure.identity import ClientSecretCredential from azure.mgmt.compute import ComputeManagementClient credential = ClientSecretCredential( tenant_id='xxxxx', client_id='xxxxx', client_secret='xxxxx' ) compute_client = ComputeManagementClient( credential=credential, subscription_id=SUBSCRIPTION_ID )  You can then use the list_all method with compute_client to list all VMs as usual: # List all Virtual Machines in the specified subscription def list_virtual_machines(): for vm in compute_client.virtual_machines.list_all(): print(vm.name) list_virtual_machines()  References:  Azure SDK for Python on GitHub Migration Guide - Resource Management How to authenticate and authorize Python apps on Azure Example: Use the Azure libraries to provision a virtual machine