eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I tried typing.IO as suggested in Type hint for a file or file-like object?, but it doesn't work: from __future__ import annotations from tempfile import NamedTemporaryFile from typing import IO def example(tmp: IO) -> str: print(tmp.file) return tmp.name print(example(NamedTemporaryFile()))  for this, mypy tells me: test.py:6: error: "IO[Any]" has no attribute "file"; maybe "fileno"?  and Python runs fine. So the code is ok.

Answer: I don't think this can be easily type hinted. If you check the definition of NamedTemporaryFile, you'll see that it's a function that ends in: return _TemporaryFileWrapper(file, name, delete)  And _TemporaryFileWrapper is defined as: class _TemporaryFileWrapper:  Which means there isn't a super-class that can be indicated, and _TemporaryFileWrapper is "module-private". It also doesn't look like it has any members that make it a part of an existing Protocol * (except for Iterable and ContextManager; but you aren't using those methods here). I think you'll need to use _TemporaryFileWrapper and ignore the warnings: from tempfile import _TemporaryFileWrapper # Weak error def example(tmp: _TemporaryFileWrapper) -> str: print(tmp.file) return tmp.name   If you really want a clean solution, you could implement your own Protocol that includes the attributes you need, and have it also inherit from Iterable and ContextManager. Then you can type-hint using your custom Protocol.  * It was later pointed out that it does fulfil IO, but the OP requires attributes that aren't in IO, so that can't be used.
Question: I have tons of .xlsm files that I have to load. Each Excel file has 6 sheets. Because of that, I'm opening each Excel file like this, using pandas: for excel_file in files_list: with pd.ExcelFile(excel_file, engine = "openpyxl") as f: df1 = pd.read_excel(f, "Sheet1") df2 = pd.read_excel(f, "Sheet2") df3 = pd.read_excel(f, "Sheet3") ...  After each iteration I am passing the df to other function and do some stuff with it. I am using pd.ExcelFile to load the file into memory just once and then separate it on DataFrames. However, when doing this, I am getting the following warning: /opt/anaconda3/lib/python3.8/site-packages/openpyxl/worksheet/_reader.py:300: UserWarning: Data Validation extension is not supported and will be removed warn(msg)  No matter the warning, the information is loaded correctly from the Excel file and no data is missing. It takes about 0.8s to load each Excel file and all of its sheets into df. If I use the default engine on pandas to load each Excel file, the warning goes away, but the time it takes for each file goes up to 5 or even 6 seconds. I saw this post, but there wasn't an answer on how to remove the warning, which is what I need, as everything's working correctly. How can I disable said UserWarning?

Answer: You can do this using warnings core module: import warnings warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')  You can also specify the particular module you'd like to silence warnings for by adding an argument module="openpyxl".
Question: There's a code I found in internet that says it gives my machines local network IP address: hostname = socket.gethostname() local_ip = socket.gethostbyname(hostname)  but the IP it returns is 192.168.94.2 but my IP address in WIFI network is actually 192.168.1.107 How can I only get wifi network local IP address with only python? I want it to work for windows,linux and macos.

Answer: You can use this code: import socket hostname = socket.getfqdn() print("IP Address:",socket.gethostbyname_ex(hostname)[2][1])  or this to get public ip: import requests import json print(json.loads(requests.get("https://ip.seeip.org/jsonip?").text)["ip"])
Question: I am trying to write a test for logging out a user in Django. Here is the code: urls.py from django.conf.urls import url from django.contrib import admin from accounts.views import LoginView, LogoutView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', LoginView.as_view(), name='login'), url(r'^logout/', LogoutView.as_view(), name='logout'), ]  views.py from django.http import HttpResponseRedirect from django.contrib.auth import login, logout from django.views.generic import View class LogoutView(View): def get(self, request): logout(request) return HttpResponseRedirect('/')  tests.py from django.test import TestCase, Client from django.contrib.auth.models import User class LogoutTest(TestCase): def setUp(self): self.client = Client() self.user = User.objects.create_user( username='user1', email='user1_123@gmail.com', password='top_secret123' ) def test_user_logs_out(self): self.client.login(email=self.user.email, password=self.user.password) self.assertTrue(self.user.is_authenticated) response = self.client.get('/logout/') self.assertFalse(self.user.is_authenticated) self.assertRedirects(response, '/', 302)  The assertion self.assertFalse(self.user.is_authenticated) is failing. Testing through the browser seems to work fine. It seems like the user would not be authenticated after calling logout(). Am I missing something?

Answer:  It seems like the user would not be authenticated after calling logout(). Am I missing something?  .is_authenticated [Django-doc] does not check if a user is logged in. Every real User returns always True for is_authenticated. An AnonymousUser [Django-doc] will return False for example. If you thus log out, then request.user will be the AnonymousUser, and thus not be authenticated. In other words, if you use request.user.is_authenticated, you will call this on the logged-in user if the session is bounded to a user (you logged in with the browser), and you call this on the AnonymousUser in case the browser did not log in a user/the browser logged out.
Question: In model.state_dict(), model.parameters() and model.named_parameters() weights and biases of nn.Linear() modules are contained separately, e.q. fc1.weight and fc1.bias. Is there a simple pythonic way to get both of them? Expected example looks similar to this: layer = model['fc1'] print(layer.weight) print(layer.bias) 

Answer: You can recover the named parameters for each linear layer in your model like so: from torch import nn for layer in model.children(): if isinstance(layer, nn.Linear): print(layer.state_dict()['weight']) print(layer.state_dict()['bias'])
Question: I have Python 3.8 and 3.9 installed via Homebrew: ~ brew list | grep python python@3.8 python@3.9  I want to use Python 3.9 as my default one with python3 command. I tried the following: ~ brew switch python 3.9 Error: python does not have a version "3.9" in the Cellar. python's installed versions: 3.8.6  I tried to uninstall Python and reinstall it, but it's used by other packages: ~ brew uninstall python Error: Refusing to uninstall /usr/local/Cellar/python@3.8/3.8.6 because it is required by glib and php, which are currently installed. You can override this and force removal with: brew uninstall --ignore-dependencies python  How can I use Python 3.9?

Answer: There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9. To workaround, following commands should work for you: brew unlink python@3.9 brew unlink python@3.8 brew link --force python@3.9  Re-opening your terminal or execute command rehash can be required to take account the change.
Question: I am currently on a new mac with python 3.8.2 installed. I have a bitbucket repo I cloned down. When I modify a file and git add that works fine. But when I make a git commit I get this error message env: python3.8: No such file or directory  My path env variable looks like this PATH=/Users/rach/bin:/Users/rach/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin  When I type whereis python3 I get /usr/bin/python3  I cannot figure out why I can't make a git commit with python3 already installed and the location in the path

Answer: Since you're using pre-commit, you can uninstall hooks by: pre-commit uninstall  To install them again, run: pre-commit install
Question: Say I have an flag --debug/--no-debug defined for the base command. This flag will affect the behavior of many operations in my program. Right now I find myself passing this flag as function parameters all over the place, which doesn't seem elegant. Especially when I need to access this flag in a deep call stack, I'll have to add this parameter to every single function on the stack. I can instead create a global variable is_debug and set its value at the beginning of the command function that receives the value of this flag. But this doesn't seem elegant to me either. Is there a better way to make some option values globally accessible using the Click library?

Answer: There are two ways to do so, depending on your needs. Both of them end up using the click Context. Personally, I'm a fan of Option 2 because then I don't have to modify function signatures (and I rarely write multi-threaded programs). It also sounds more like what you're looking for. Option 1: Pass the Context to the function Use the click.pass_context decorator to pass the click context to the function. Docs:  Usage: https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts API: https://click.palletsprojects.com/en/7.x/api/#click.pass_context  # test1.py import click @click.pass_context def some_func(ctx, bar): foo = ctx.params["foo"] print(f"The value of foo is: {foo}") @click.command() @click.option("--foo") @click.option("--bar") def main(foo, bar): some_func(bar) if __name__ == "__main__": main()  $ python test1.py --foo 1 --bar "bbb" The value of foo is: 1  Option 2: click.get_current_context() Pull the context directly from the current thread via click.get_current_context(). Available starting in Click 5.0. Docs:  Usage: https://click.palletsprojects.com/en/7.x/advanced/#global-context-access API: https://click.palletsprojects.com/en/7.x/api/#click.get_current_context  Note: This only works if you're in the current thread (the same thread as what was used to set up the click commands originally). # test2.py import click def some_func(bar): c = click.get_current_context() foo = c.params["foo"] print(f"The value of foo is: {foo}") @click.command() @click.option("--foo") @click.option("--bar") def main(foo, bar): some_func(bar) if __name__ == "__main__": main()  $ python test2.py --foo 1 --bar "bbb" The value of foo is: 1
Question: I'am trying to import pyodbc library into google colab, but i'am getting this error. Just in case, I have Anaconda installed in my notebook, and I never had problem with pyodbc in there. Can you help me please? Tks! Collecting pyodbc Using cached https://files.pythonhosted.org/packages/81/0d/bb08bb16c97765244791c73e49de9fd4c24bb3ef00313aed82e5640dee5d/pyodbc-4.0.30.tar.gz Building wheels for collected packages: pyodbc Building wheel for pyodbc (setup.py) ... error ERROR: Failed building wheel for pyodbc Running setup.py clean for pyodbc Failed to build pyodbc Installing collected packages: pyodbc Running setup.py install for pyodbc ... error ERROR: Command errored out with exit status 1: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-u5dmb223/pyodbc/setup.py'"'"'; __file__='"'"'/tmp/pip-install-u5dmb223/pyodbc/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-9jmhckrt/install-record.txt --single-version-externally-managed --compile Check the logs for full command output. 

Answer: You can try the following: !apt install unixodbc-dev !pip install pyodbc
Question: I'm not aware of anything on my system having changed, but the aws CLI tool has stopped working. $ aws-bash: /Users/user_name/Library/Python/3.7/bin/aws:/usr/local/opt/python/bin/ python3.7: bad interpreter: No such file or directory  I've tried brew reinstall awscli which is suggested elsewhere, but with no luck.

Answer: Option 1 Type brew uninstall awscli  Then brew install awscli  update python to 3.9. look in the following post. If this approach does not work for you, then try : Option 2  Go to https://www.python.org/ and use the GUI installer for your OS  pip3 install awscli
Question: I'm trying to share memory between a python process and a nodejs process started from the python process using an anonymous mmap. Essentially, the python process begins, initializes the mmap and starts a subprocess using either call or Popen to launch a child that runs some node code. This nodejs code uses mmap to try to access the same area in memory. However I get two different mappings and no data is shared between them. Why is this? import mmap, math, os from subprocess import call mm = mmap.mmap( -1, 1024, flags=mmap.MAP_SHARED | mmap.MAP_ANONYMOUS, prot= mmap.PROT_READ | mmap.PROT_WRITE ) mm.seek(0) mm.write('hello world!\n'.encode('utf-8')) call([ 'node', '-e', """ const mmap = require('mmap.js'); const fileBuf = mmap.alloc( 1024, mmap.PROT_READ | mmap.PROT_WRITE, mmap.MAP_SHARED| mmap.MAP_ANONYMOUS, -1, 0 ) console.log(fileBuf.toString('utf-8')); """ ])  The mmap.js that I am using is a NAPI of the original mmap c function. This is the github for this library. EDIT: Thanks to 'that other guy' for his answer. It was the correct one. Here's some sample code that works out of the box!: test_mmap.py import os, ctypes, posix_ipc, sys, mmap from subprocess import call SHARED_MEMORY_NAME = "/shared_memory" memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREX, size=1024) mapFile = mmap.mmap(memory.fd, memory.size) memory.close_fd() mapFile.seek(0) mapFile.write("Hello world!\n".encode('utf-8')) mapFile.seek(0) print("FROM PYTHON MAIN PROCESS: ", mapFile.readline().decode('utf-8')) mapFile.seek(0) call([ "node", "./test_mmap.js", SHARED_MEMORY_NAME ]) mapFile.close() posix_ipc.unlink_shared_memory(SHARED_MEMORY_NAME)  test_mmap.js const args = process.argv; const mmap = require('mmap.js'); const shm = require('nodeshm'); const SHM_FILE_NAME=args[args.length-1]; let fd = shm.shm_open(SHM_FILE_NAME, shm.O_RDWR, 0600); if (fd == -1){ console.log("FD COULD NOT BE OPENED!"); throw "here"; } let mm = mmap.alloc(1024, mmap.PROT_READ | mmap.PROT_WRITE, mmap.MAP_SHARED, fd, 0); console.log("FROM NODE: ", mm.slice(0, mm.indexOf('\n')).toString('utf-8'));  Sample output: FROM PYTHON MAIN PROCESS: Hello world! FROM NODE: Hello world! 

Answer: Fortunately this doesn't work: imagine how confusing if all of the system's MAP_ANONYMOUS mappings were against the same area and kept overwriting each other. Instead, use shm_open to create a new handle you can mmap in both processes. This is a portable wrapper around the equally valid but less portable strategy of creating and mmap'ing a file in /dev/shm/.
Question: How can I embed code into a docstring to tell Sphinx to format the code similar as it will be done in Markdown (different background colour, monospaced sans-serif font)? For example to document a code usage example. """ This is a module documentation Use this module like this: res = aFunction(something, goes, in) print(res.avalue) """ 

Answer: There are a few ways to do it. I think the most sensible in your case would be .. code-block:: """ This is a module documentation Use this module like this: .. code-block:: python res = aFunction(something, goes, in) print(res.avalue) """  Notice the blank line between the directive and the code block - it must be there in order for the block to render properly.
Question: I have dataframe: time_to_rent = {'rentId': {0: 43.0, 1: 87.0, 2: 140.0, 3: 454.0, 4: 1458.0}, 'creditCardId': {0: 40, 1: 40, 2: 40, 3: 40, 4: 40}, 'createdAt': {0: Timestamp('2020-08-24 16:13:11.850216'), 1: Timestamp('2020-09-10 10:47:31.748628'), 2: Timestamp('2020-09-13 15:29:06.077622'), 3: Timestamp('2020-09-24 08:08:39.852348'), 4: Timestamp('2020-10-19 08:54:09.891518')}, 'updatedAt': {0: Timestamp('2020-08-24 20:26:31.805939'), 1: Timestamp('2020-09-10 20:05:18.759421'), 2: Timestamp('2020-09-13 18:38:10.044112'), 3: Timestamp('2020-09-24 08:53:22.512533'), 4: Timestamp('2020-10-19 17:10:09.110038')}, 'rent_time': {0: Timedelta('0 days 04:13:19.955723'), 1: Timedelta('0 days 09:17:47.010793'), 2: Timedelta('0 days 03:09:03.966490'), 3: Timedelta('0 days 00:44:42.660185'), 4: Timedelta('0 days 08:15:59.218520')}}  The idea to aggregate dataframe by column 'creditCardId' and have mean value of 'rent_time'. Ideal output should be: creditCardId rent_time mean 40 0 days 05:08:10.562342  if I run code: print (time_to_rent['rent_time'].mean())  it works fine and i have "0 days 05:08:10.562342" as output. But when i am trying to get grouping by: time_to_rent.groupby('creditCardId', as_index=False)[['rent_time']].mean()  I got error back: ~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _cython_agg_blocks(self, how, alt, numeric_only, min_count) 1093 1094 if not (agg_blocks or split_frames): -> 1095 raise DataError("No numeric types to aggregate") 1096 1097 if split_items: DataError: No numeric types to aggregate  if I use the command: time_to_rent = time_to_rent.groupby('creditCardId', as_index=False)[['rent_time']]  it returns only "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000000000B5F2EE0>" May you please help me understand where my mistake is?

Answer: It's not your mistake, possibly a bug in Pandas since Timedelta can be averaged. A work-around is apply: time_to_rent.groupby('creditCardId')['rent_time'].apply(lambda x: x.mean())  Output: creditCardId 40 0 days 05:08:10.562342200 Name: rent_time, dtype: timedelta64[ns]
Question: Given a string s representing characters typed into an editor, with "->" representing a delete, return the current state of the editor. For every one "->" it should delete one char. If there are two "->" i.e "->->" it should delete 2 char post the symbol. Example 1 Input s = "a->bcz" Output "acz"  Explanation The "b" got deleted by the delete. Example 2 Input s = "->x->z" Output empty string  Explanation All characters are deleted. Also note you can type delete when the editor is empty as well. """ I Have tried following function but id didnt work def delete_forward(text): """ return the current state of the editor after deletion of characters """ f = "->" for i in text: if (i==f): del(text[i+1])  How can i complete this without using regular expressions?

Answer: Here's a simple recursive solution- # Constant storing the length of the arrow ARROW_LEN = len('->') def delete_forward(s: str): try: first_occurence = s.index('->') except ValueError: # No more arrows in string return s if s[first_occurence + ARROW_LEN:first_occurence + ARROW_LEN + ARROW_LEN] == '->': # Don't delete part of the next arrow next_s = s[first_occurence + ARROW_LEN:] else: # Delete the character immediately following the arrow next_s = s[first_occurence + ARROW_LEN + 1:] return delete_forward(s[:first_occurence] + s[first_occurence + ARROW_LEN + 1:])  Remember, python strings are immutable so you should instead rely on string slicing to create new strings as you go. In each recursion step, the first index of -> is located and everything before this is extracted out. Then, check if there's another -> immediately following the current location - if there is, don't delete the next character and call delete_forward with everything after the first occurrence. If what is immediately followed is not an arrow, delete the immediately next character after the current arrow, and feed it into delete_forward. This will turn x->zb into xb. The base case for the recursion is when .index finds no matches, in which case the result string is returned. Output >>> delete_forward('ab->cz') 'abz' >>> delete_forward('abcz') 'abcz' >>> delete_forward('->abc->z') 'bc' >>> delete_forward('abc->z->') 'abc' >>> delete_forward('a-->b>x-->c>de->f->->g->->->->->') 'a->x->de'
Question: The usage of *args and **kwargs in python is clear to me and there are many questions out there in SO (eg Use of *args and **kwargs and What does ** (double star/asterisk) and * (star/asterisk) do for parameters?). But one thing I would like to understand is: why is it not possible to simultaneously define mandatory positional args, mandatory kwarg arguments and eventually still allow catching other args and kwargs as in cant_do_that below? def one_kwarg_is_mandatory(*, b, **kwargs): print(b) for key, value in kwargs.items(): print(key, value) def one_pos_arg_and_one_kwarg_are_mandatory(a, *, b, **kwargs): print(a, b) for key, value in kwargs.items(): print(key, value) # I wanted a mandatory arg (a) and possibly parse other args (*args), # then a mandatory kwarg (b) and eventually other kwargs (**kwargs) def cant_do_that(a, *args, *, b, **kwargs): print(a, b) print(args) for key, value in kwargs.items(): print(key, value) # not really interested on this because "b" must be a kwarg and hiding # it under **kwargs would not be explicit enough for my customer (sometimes myself ;)) def could_do_this_but(a, b, *args, **kwargs): print(a, b) print(args) print(kwargs)  Yes one could get rid of b in the could_do_this_but function's signature, perform (for instance) a kwargs.get("b", None) at the top of the function and raise some appropriate error if found None... but having "b" directly on the function signature would allow faster and more explicit code development employing the function down the road.

Answer: The correct syntax is def cant_do_that(a, *args, b, **kwargs):. Note that * is used only once, both to mark the end of positional arguments and to set the name for variadic positional arguments.  The * in a function definition is syntactically unique at the separation between positional-or-keyword and keyword-only arguments: parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]] | "**" parameter [","]  In short, the grammar "*" [parameter] means * and *args are syntactically the same thing – a literal * and optional name – which may occur only once. Use a bare * to start keyword-only arguments without taking variadic positional arguments, and use a named *args to start keyword-only arguments with taking variadic positional arguments.  If the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. [...] Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.