eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to verify a webhook sent from Plaid's API. Every webhook request is sent with a 'plaid-verification' header which is a JSON Web Token. The steps required to validate are:  Extract JWT from request header  signed_jwt = eyJhbGciOiJFUzI1NiIsImtpZCI6IjZjNTUxNmUxLTkyZGMtNDc5ZS1hOGZmLTVhNTE5OTJlMDAwMSIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1OTA4ODcwMDEsInJlcXVlc3RfYm9keV9zaGEyNTYiOiJiNjNhMDdiNTQ3YjAwZjk5MjU0N2Y2YmJjOGQ5YWNjNjFhOGNjZWUxMzhiYzgyZjQ0YTZiYWEwOTY4M2E1ZDBmIn0.OOKvIihgqCj7Qrb2bmz7T3t7uK-0JyjiEqL2s1kWeJBM4MMmjaHKK8GmU_z91QolBWMzvPgs718EElY-rE3cwQ  Extract JWT header value without validating the signature, which looks like this:   { "alg": "ES256", "kid": "6c5516e1-92dc-479e-a8ff-5a51992e0001", "typ": "JWT" }   Extract the kid and POST to /webhook_verification_key/get   POST /webhook_verification_key/get { "client_id": "MY_CLIENT_ID" "secret": "MY_SECRET_ID" "key_id": "6c5516e1-92dc-479e-a8ff-5a51992e0001" }  The response is: { "key": { "alg": "ES256", "created_at": 1560466143, "crv": "P-256", "expired_at": null, "kid": "6c5516e1-92dc-479e-a8ff-5a51992e0001", "kty": "EC", "use": "sig", "x": "35lvC8uz2QrWpQJ3TUH8t9o9DURMp7ydU518RKDl20k", "y": "I8BuXB2bvxelzJAd7OKhd-ZwjCst05Fx47Mb_0ugros" }, "request_id": "HvfCtrDLG1ihcp7" }   Interpret key as a JSON Web Key, validate that the signature of the JSON Web Key is valid, and extract the payload (using jose python library)  claims = jwt.decode(signed_jwt, key, algorithms=['ES256']) claims = { "iat": 1590887001, "request_body_sha256": "b63a07b547b00f992547f6bbc8d9acc61a8ccee138bc82f44a6baa09683a5d0f" }   Compute the SHA-256 of the request body and ensure that it matches claims['request_body_sha256']:  Body is in a file body.json { "error": null, "item_id": "yxQbxDjnD8hr69pKbQpbcKeVn3GL9QuyA7NV3", "new_transactions": 390, "webhook_code": "HISTORICAL_UPDATE", "webhook_type": "TRANSACTIONS" }  Compute SHA-256 of body.json f = open('body.json') body = json.load(f) f.close() m = hashlib.sha256() m.update(json.dumps(body).encode()) body_hash = m.hexdigest() print(body_hash) body_hash = 'efbb5274864518f7eb3834125d9bcdb95fb03066d3d1bed3ebcc6163d8dc3579'  The body hash in the example above does not equal the body hash received from Plaid. There's 2 possible problems here:  Plaid isn't sending the correct body hash (unlikely) The hash method I'm using to hash the body is not the same as Plaid's method  Is there something I'm missing here? Perhaps the request body is encoded differently on my end? I'm using Node.js and Express in production but I made a Python script to follow the method Plaid outlined here, but I'm still not getting the correct hash. I'm honestly out of ideas.

Answer: It seems to be a problem with whitespace. If you modify body.json to have 2 spaces per ‘tab’ on each new line, it will generate the right hash.
Question: As explained in pip's documentation a user can install packages in his personal account using pip install --user <pkg>.  How can I programmatically determine the user install location for scripts installed like this? I am talking about the directory that should be added to the PATH so that installed packages can be invoked from command line. For example, in Windows when installing pip install -U pylint --user I get the following warning because I don't have 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' in my PATH: ... Installing collected packages: wrapt, six, typed-ast, lazy-object-proxy, astroid, mccabe, isort, colorama, toml, pylint Running setup.py install for wrapt ... done WARNING: The script isort.exe is installed in 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts epylint.exe, pylint.exe, pyreverse.exe and symilar.exe are installed in 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' which is not on PATH.  Is there some python code I can use to determine that location programmatically (that will work on Windows/Linux/Darwin/etc)? Something like: def get_user_install_scripts_dir(): ... # would return 'C:\Users\myusername\AppData\Roaming\Python\Python37\Scripts' # on Windows with Python 3.7.x, '/home/myusername/.local/bin' in Linux, etc return platform_scripts_dir  As a fallback, is there some command I can run to obtain this location? Something like (but for the script location not the site's base directory): PS C:\Users\myusername\> python -m site --user-base C:\Users\myusername\AppData\Roaming\Python $ python -m site --user-base /home/myusername/.local 

Answer: I believe the following should give the expected result import os import sysconfig user_scripts_path = sysconfig.get_path('scripts', f'{os.name}_user') print(user_scripts_path)  Command-line: python -c 'import os,sysconfig;print(sysconfig.get_path("scripts",f"{os.name}_user"))'  Since pip 21.3 released on 2021-10-11, pip itself uses sysconfig to compute paths. References:  https://docs.python.org/3/library/sysconfig.html#installation-paths https://docs.python.org/3/library/os.html#os.name https://discuss.python.org/t/proper-way-to-determine-install-location-of-console-scripts/7188 pip 21.3 "Deprecations and removals" github.com/pypa/pip/pull/10358 "Switch install scheme backend to sysconfig"
Question: I am running a python script in a termux environment on an Android device and I would like to be able to detect that the OS is Android. The traditional approaches don't work: >>> import platform >>> import sys >>> print(platform.system()) 'Linux' >>> print(sys.platform) 'linux' >>> print(platform.release()) '4.14.117-perf+' >>> print(platform.platform()) 'Linux-4.14.117-perf+-aarch64-with-libc'  What other ootb options are available? An apparently useful option is platform.machine() which returns armv8 — this is more than just 'Linux' yet it's just the architecture, and not the OS, and it might return a false positive for example on a raspberry pi or other arm-based systems.

Answer: There is more simple way that doesn't depend using external utilities and just uses sys module. Here is code: import sys is_android: bool = hasattr(sys, 'getandroidapilevel')  Here are it's pros and cons: @@Pros@@ + Does not depend on environment values + Does not depend on third-party modules + Simple one-liner (2 technically) @@Cons@@ - Version restriction (Supports CPython 3.7+ or equivalent) - Implementation-dependent (while CPython implements this I don't know about others)
Question: I have opened a public access to S3 bucket and I need to download files / folders with files from the bucket using python. The trick is that I do not want to supply credentials (which boto3 apparently requires). Is it even possible?

Answer: You can use GetObject from the S3 REST API, together with the Requests library in Python. If you grant READ access to the anonymous user, you can return the object without using an authorization header. Example of such an S3 REST call:: > GET /example-object HTTP/1.1 > Host: example-bucket.s3.<Region>.amazonaws.com  Python(rough example): import requests url = '/example-object' headers = {'Host': 'example-bucket.s3.<Region>.amazonaws.com'} r = requests.get(url, headers=headers)  Requests GetObject
Question: All search results on "coroutine was never awaited" are for people who were either trying to fire-and-forget or actually did forget to await. This is not my case. I want to use a coroutine the same way I often use generators: I'm creating it here while I have all the variables handy, but I'm not sure yet whether I'll ever need that to be run. Something like: options = { 'a': async_func_1(..., ...), 'b': async_func_2(), 'c': async_func_3(...), }  and elsewhere: appropriate_option = figure_the_option_out(...) result = await options[appropriate_option] 

Answer: deceze's comment that you should not create the coroutine object until you are ready to await it is probably the most ideal solution. But if that isn't practical, you can use weakref.finalize() to call the coroutine object's close() method just before it is garbage-collected. >python -m asyncio asyncio REPL 3.9.5 (default, May 18 2021, 14:42:02) [MSC v.1916 64 bit (AMD64)] on win32 Use "await" directly instead of "asyncio.run()". Type "help", "copyright", "credits" or "license" for more information. >>> import asyncio >>> import weakref >>> async def coro(x): ... print(x) ... >>> coro_obj = coro('Hello') >>> del coro_obj <console>:1: RuntimeWarning: coroutine 'coro' was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback >>> coro_obj = coro('Hello') >>> _ = weakref.finalize(coro_obj, coro_obj.close) >>> del coro_obj >>>
Question: I created a python project "foo" with Poetry. This is the content of pyproject.toml: [tool.poetry] name = "bar" version = "0.1.0" description = "" [tool.poetry.dependencies] python = ">=3.5" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"  This package is compatible with Python3.5. I want to black formatter, which is not compatible with Python3.5. I think there is no problem if I use Python>=3.6 for development, but I cannot install black formatter: $ poetry add black --dev [SolverProblemError] The current project's Python requirement (>=3.5) is not compatible with some of the required packages Python requirement: - black requires Python >=3.6 Because no versions of black match >19.10b0,<20.0 and black (19.10b0) requires Python >=3.6, black is forbidden. So, because bar depends on black (^19.10b0), version solving failed.  So I installed black directly with pip: $ poetry run pip install black  This way doesn't sit well with me. I want to install black by poetry. How should I do? (I don't want to modify the dependency to python>=3.6)

Answer: Seems a bit late but actually you can do what you want even if black supports only Python >=3.6.2 In your pyproject.toml you can define a restricted dependcy as documented in https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies [tool.poetry.dependencies] python = ">=3.5" [tool.poetry.dev-dependencies] black = {version = "^21.7b0", python = ">=3.6.2"}  Poetry won't complain and you won't have any problems since it is a dev dependency.
Question: Suppose, we have a tensor  t = torch.tensor([True, False, True, False])  How do we convert it to an integer tensor with values [1, 0, 1, 0]?

Answer: The solution is just a single line of code. To convert a tensor t with values [True, False, True, False] to an integer tensor, just do the following. t = torch.tensor([True, False, True, False]) t_integer = t.long() print(t_integer) [1, 0, 1, 0]
Question: I would like to align vertically all options of a dash_core_components.RadioItems. According to the dash documentation, the default behavior should include a vertical alignment of the RadioItems options. If you wanted to align the options horizontally, you would have to specify: labelStyle={'display': 'inline-block'}  On the contrary, as default behavior I get a horizontal alignment and I don't know what to specify as the display item to get a vertical alignment of the RadioItems options. Here my attempt until now: import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output app = dash.Dash() app.layout = html.Div([dcc.RadioItems(id = 'input-radio-button', options = [dict(label = 'A', value = 'A'), dict(label = 'B', value = 'B')], value = 'A'), html.P(id = 'output-text')]) @app.callback(Output('output-text', 'children'), [Input('input-radio-button', 'value')]) def update_graph(value): return f'The selected value is {value}' if __name__ == "__main__": app.run_server()  What I get:  I would like to get a results like this (image manually edited):  I found this reference where this problem is mentioned. There, it is proposed to solve it by referring to an external stylesheet. I would like, if possible, to avoid this turnaround and solve it by specifying the correct option within the definition of the RadioItems element. Version info: Python 3.7.0 dash 1.12.0 plotly 4.7.0 

Answer: You can pass the labelStyle={'display': 'block'} property to dcc.RadioItems() in order to vertically align the different options, but I suggest that you follow the recommendation in the Dash Community Forum, which is to always link the Dash CSS file bWLwgP.css.
Question: I'm trying to display the tuples of a postgreSQL table neatly in my Jupyter Notebook, but the newline \n escape character doesn't seem to work here (it works for my python scripts w/ same code outside of jupyter).  I'm trying to run: cur.execute('SELECT * FROM Cars') '\n '.join(str(x) for x in cur.fetchall())  But my output still contains the '\n' characters themselves: "(1, 'Toyota', 'Supra', 2020, 'Sport', 'Gas', 49995.0, 7, 280.0, datetime.date(2020, 5, 27), 'Loan', 50150.0, 300.0, 987654321, 333356789)\n (4, 'Chevrolet', 'Corvette', 2020, 'Sport', 'Gas', 55999.0, 4, 280.0, datetime.date(2020, 5, 27), 'Loan', 58999.0, 300.0, 987444321, 333356789)\n (2, 'Toyota', '4Runner', 2018, 'Sport', 'Gas', 40599.0, 13266, 280.0, datetime.date(2020, 5, 27), 'Loan', 58999.0, 300.0, 987334321, 333356789)"  Any ideas as to what I need to do or add?

Answer: When you don't put the output in print statement. "\n" would be printed as "\n" instead of newline in jupyter notebook and python shell. in: 'A\nB' out: 'A\nB' in: print('A\nB') out: A B  The solution you need is: print('\n '.join(str(x) for x in cur.fetchall()))
Question: I have a page with the following Code Structure: Python Code: from flask import Flask,render_template app=Flask(__name__) @app.route('/') def home(): return render_template("first.html") @app.route('/second') def next(): return render_template("second.html") if __name__=="__main__": app.run(debug=True)  HTML Code for second.html: {% extends first.html %} <dosomething> </dosomething>  Now I want to set the Environment of Jinja as follows: Environment: import jinja2 JINJA_ENV=jinja2.Environment(block_start_string='#%',block_end_string="#%",variable_start_string='{',variable_end_string='}')  I want this changes to be reflected in Flask. Should this variable be initialized with Flask in some way or left as such? 

Answer: It's not publically documented, but a Flask() object has a .jinja_options dict that will be used to build the Jinja Environment. Just make sure to set it ASAP. Source: https://github.com/pallets/flask/blob/bbb273bb761461ab329f03ff2d9002f6cb81e2a4/src/flask/app.py#L272 https://github.com/pallets/flask/blob/bbb273bb761461ab329f03ff2d9002f6cb81e2a4/src/flask/app.py#L652 Example: from flask import Flask from jinja2 import ChainableUndefined app = Flask(__name__) app.jinja_options["undefined"] = ChainableUndefined
Question: I have always used np.arange. I recently came across np.linspace. I am wondering what exactly is the difference between them... Looking at their documentation: np.arange:  Return evenly spaced values within a given interval.  np.linspace:  Return evenly spaced numbers over a specified interval.  The only difference I can see is linspace having more options... Like choosing to include the last element. Which one of these two would you recommend and why? And in which cases is np.linspace superior?

Answer: np.linspace allows you to define how many values you get including the specified min and max value. It infers the stepsize: >>> np.linspace(0,1,11) array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])  np.arange allows you to define the stepsize and infers the number of steps(the number of values you get). >>> np.arange(0,1,.1) array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])   contributions from user2357112: np.arange excludes the maximum value unless rounding error makes it do otherwise. For example, the following results occur due to rounding error: >>> numpy.arange(1, 1.3, 0.1) array([1. , 1.1, 1.2, 1.3])  You can exclude the stop value (in our case 1.3) using endpoint=False: >>> numpy.linspace(1, 1.3, 3, endpoint=False) array([1. , 1.1, 1.2])
Question: We're applying Black code style to a django project. In all the tutorials / examples I find (such as in django cookiecutter and this blog), I keep seeing django's migrations files excluded from the linter. But to my mind, these are still python files. Sure, django may not autogenerate them to meet the Black spec. But it's not like developers always write their code to meet Black spec... that's what linting is for! Why would migration files be considered different to any other python files?! NB I'm aware of the possibility of changing an already-applied migration if you've got pre-existing migrations - this requires care on first application (as does first application to the rest of the codebase, frankly) but surely isn't a reason not to do it? EDIT - @torxed asked for an example of a django migration file I'm not sure how helpful this'll be tbh, but a typical django migration file might look like this (in this case adding a char field to a table): # Generated by Django 2.2.3 on 2019-10-28 09:45 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('api', '0009_figures_notes_tables'), ] operations = [ migrations.AlterField( model_name='project', name='processed_kmz_sha', field=models.CharField(max_length=255), ), ] 

Answer: I bit the bullet and applied Black to my migrations files, progressively across half a dozen django projects. No problems at all, everything deployed in production for months now. So the answer is: No reason at all why not to do this, and I think migrations files should be included, so that reading them is a consistent experience with the rest of the project.
Question: I appending a new row to an existing pandas dataframe as follows: df= df.append(pd.Series(), ignore_index=True)  This is resulting in the subject DeprecationWarning. The existing df has a mix of string, float and dateime.date datatypes (8 columns totals). Is there a way to explicitly specify the columns types in the df.append?  I have looked here and here but I still have no solution. Please advise if there is a better way to append a row to the end of an existing dataframe without triggering this warning.

Answer: You can try this Type_new = pd.Series([],dtype=pd.StringDtype())  This will create a blank data frame for us.
Question: I have already install anaconda on my Windows 10 laptop. I'm trying to activate the Python environment named pyenv. First, I check the conda env list in my laptop, this is the output on the power shell: PS C:\Users\User> conda env list # conda environments: # base * C:\Users\User\Anaconda3 pyenv C:\Users\User\Anaconda3\envs\pyenv  Then I activate pyenv: PS C:\Users\User> conda activate pyenv  But I check again, it still activates base environment: PS C:\Users\User> conda env list # conda environments: # base * C:\Users\User\Anaconda3 pyenv C:\Users\User\Anaconda3\envs\pyenv  When I use the Anaconda prompt, it works normally: (base) C:\Users\User>conda activate pyenv (pyenv) C:\Users\User>  Does anyone know why it causes this problem and how to fix this? Update: Running conda init powershell: PS C:\Users\User> conda init powershell no change C:\Users\User\Anaconda3\Scripts\conda.exe no change C:\Users\User\Anaconda3\Scripts\conda-script.py no change C:\Users\User\Anaconda3\Scripts\conda-env-script.py no change C:\Users\User\Anaconda3\condabin\conda.bat no change C:\Users\User\Anaconda3\Library\bin\conda.bat no change C:\Users\User\Anaconda3\condabin\_conda_activate.bat no change C:\Users\User\Anaconda3\condabin\rename_tmp.bat no change C:\Users\User\Anaconda3\condabin\conda_hook.bat no change C:\Users\User\Anaconda3\Scripts\activate.bat no change C:\Users\User\Anaconda3\condabin\activate.bat no change C:\Users\User\Anaconda3\condabin\deactivate.bat modified C:\Users\User\Anaconda3\etc\profile.d\conda.sh modified C:\Users\User\Anaconda3\etc\fish\conf.d\conda.fish no change C:\Users\User\Anaconda3\shell\condabin\Conda.psm1 modified C:\Users\User\Anaconda3\shell\condabin\conda-hook.ps1 no change C:\Users\User\Anaconda3\Lib\site-packages\xontrib\conda.xsh modified C:\Users\User\Anaconda3\etc\profile.d\conda.csh modified C:\Users\User\Documents\WindowsPowerShell\profile.ps1  Update 2: It works when using CMD: C:\Users\User>conda activate pyenv (pyenv) C:\Users\User> 

Answer: After a while, my Powershell appear this error when I open it. . : File C:\Users\User\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3 + . 'C:\Users\BinoyGhosh\Documents\WindowsPowerShell\profile.ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess  Then I found this solution.  Run Powershell as Administrator Run this line set-executionpolicy remotesigned Close the terminal  Then it works.
Question: I've been moving from bottle to flask. I'm the type of person that prefers writing my own code instead of downloading packages from the internet if I the code needed is 20 lines or less. Take for example support for Basic authentication protocol. In bottle I could write: def allow_anonymous(): """assign a _allow_anonymous flag to functions not requiring authentication""" def wrapper(fn): fn._allow_anonymous = True return fn return wrapper def auth_middleware(fn): """perform authentication (pre-req)""" def wrapper(*a, **ka): # if the allow_anonymous annotation is set then bypass this auth if hasattr(fn, '_allow_anonymous') and fn._allow_anonymous: return fn(*a, **ka) user, password = request.auth or (None, None) if user is None or not check(user, password): err = HTTPError(401, text) err.add_header('WWW-Authenticate', 'Basic realm="%s"' % realm) return err return fn(*a, **ka) return wrapper ... app = Bottle() app.install(middleware.auth_middleware)  The above code gave me full support for basic auth protocol for all methods unless explicitly decorated with the @allow_anonymous wrapper. I'm just a beginner with flask. I'm having a hard time accomplishing the bottle-compatible code above in flask without adding dependencies on more python packages or excessive boiler-plate. How is this handled directly and clearly in flask?

Answer: You can definitely some of the functionality of flask-httpauth yourself, if you wish :-P I would think you will need to play some before_request games (not very beautiful), or alternatively call flask's add_url_rule with a decorated method for each api endpoint (or have a route decorator of your own that will do this). The add_url_rule gets a view function that is usually your api endpoint handler, but in your case, will be a wrapped method in a manner very much like the one you gave in the post (auth_middleware). The gist of it: from flask import Flask, make_response, request app = Flask(__name__) def view_wrapper(fn): """ Create a wrapped view function that checks user authorization """ def protected_view(*a, **ka): # if the allow_anonymous annotation is set then bypass this auth if hasattr(fn, '_allow_anonymous') and fn._allow_anonymous: return fn(*a, **ka) # consult werkzeug's authorization mixin user, password = (request.authorization.username, request.authorization.password) if request.authorization else (None, None) if user is None or not check(user, password): err_response = make_response(text, 401) err_response.headers['WWW-Authenticate'] = 'Basic realm="%s"' % realm return err_response return fn(*a, **ka) return protected_view # An endpoint def hello(): return 'hello there' app.add_url_rule('/', 'hello', view_wrapper(hello))  Of course, this can (and should) be further enhanced with Blueprints, etc.