eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: In assert_called_once_with, how can I specify a parameter is "any instance of class Foo"? For example: class Foo(): pass def f(x): pass def g(): f(Foo()) import __main__ from unittest import mock  mock.ANY of course passes: with mock.patch.object(__main__, 'f') as mock_f: g() mock_f.assert_called_once_with(mock.ANY)  and of course, another instance of Foo doesn't pass. with mock.patch.object(__main__, 'f') as mock_f: g() mock_f.assert_called_once_with(Foo()) AssertionError: Expected call: f(<__main__.Foo object at 0x7fd38411d0b8>) Actual call: f(<__main__.Foo object at 0x7fd384111f98>)  What can I put as my expected parameter such that any instance of Foo will make the assertion pass?

Answer: One simple solution is to do this in two steps: with mock.patch.object(__main__, 'f') as mock_f: g() mock_f.assert_called_once() self.assertIsInstance(mock_f.mock_calls[0].args[0], Foo)   However, if you look at the implementation of ANY: class _ANY(object): "A helper object that compares equal to everything." def __eq__(self, other): return True def __ne__(self, other): return False def __repr__(self): return '<ANY>' ANY = _ANY()  you can see it's just an object that's equal to anything. So you could define your own equivalent that's equal to any instance of Foo: class AnyFoo: "A helper object that compares equal to every instance of Foo." def __eq__(self, other): return isinstance(other, Foo) def __ne__(self, other): return not isinstance(other, Foo) def __repr__(self): return '<ANY Foo>' ANY_FOO = AnyFoo()  Or more generically: class AnyInstanceOf: "A helper object that compares equal to every instance of the specified class." def __init__(self, cls): self.cls = cls def __eq__(self, other): return isinstance(other, self.cls) def __ne__(self, other): return not isinstance(other, self.cls) def __repr__(self): return f"<ANY {self.cls.__name__}>" ANY_FOO = AnyInstanceOf(Foo)  Either way, you can use it as you would ANY: with mock.patch.object(__main__, 'f') as mock_f: g() mock_f.assert_called_once_with(ANY_FOO)
Question: I have a dataframe that contains one hot encoded columns of 0s and 1s which is of dtype int32. a b h1 h2 h3 xy za 0 0 1 ab cd 1 0 0 pq rs 0 1 0  I want to convert the columns h1,h2 and h3 to boolean so here is what I did.. df[df.columns[2:]].astype(bool)  But this changed all values of h1-h3 as TRUE. I also tried df[df.columns[2:]].map({0:False, 1:True})  but that does not work either. (AttributeError: 'DataFrame' object has no attribute 'map') What is the best way to convert specific columns of the dataframe from int32 0s and 1s to boolean (True/False)?

Answer: You can select all columns by positions after first 2 with DataFrame.iloc, convert to boolean and assign back: df.iloc[:, 2:] = df.iloc[:, 2:].astype(bool) print (df) a b h1 h2 h3 0 xy za False False True 1 ab cd True False False 2 pq rs False True False  Or create dictionary for convert columns names without first 2: df = df.astype(dict.fromkeys(df.columns[2:], bool)) print (df) a b h1 h2 h3 0 xy za False False True 1 ab cd True False False 2 pq rs False True False
Question: Using Plotly for a bar plot preserves the dataset's order when not using color: import pandas as pd import plotly.express as px df = pd.DataFrame({'val': [1, 2, 3], 'type': ['b', 'a', 'b']}, index=['obs1', 'obs2', 'obs3']) px.bar(df, 'val')   But color reorders the data: px.bar(df, 'val', color='type')   How can I preserve the original ordering while using the color arg? This is similar to How can I retain desired order in R Plotly bar chart with color variable, but I'm using Python rather than R.

Answer: You could use the category_orders parameter: import pandas as pd import plotly.express as px df = pd.DataFrame({'val': [1, 2, 3], 'type': ['b', 'a', 'b']}, index=['obs1', 'obs2', 'obs3']) fig = px.bar(df, 'val', color='type', category_orders={'index': df.index[::-1]}) fig.show()  Output  From the documentation:  This parameter is used to force a specific ordering of values per column. The keys of this dict should correspond to column names, and the values should be lists of strings corresponding to the specific display order desired.  By looking at the code it seems that color is being used as a grouping attribute, so that's probably why the reordering is happening.
Question: I can't use the different proxy in Python. My code: import requests proxies = { "https":'https://154.16.202.22:3128', "http":'http://154.16.202.22:3128' } r=requests.get('https://httpbin.org/ip', proxies=proxies) print(r.json())  The error I'm getting is: . . . raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1122)'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): . . . requests.exceptions.SSLError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1122)')))  I executed pip install requests. I executed pip uninstall pyopenssl, then tried to pip install an old version of of pyopenssl but it didn't work. Why is this not working?

Answer: The proxy you use simply does not support proxying https:// URLs: $ https_proxy=http://154.16.202.22:3128 curl -v https://httpbin.org/ip * Trying 154.16.202.22... * TCP_NODELAY set * Connected to (nil) (154.16.202.22) port 3128 (#0) * Establish HTTP proxy tunnel to httpbin.org:443 > CONNECT httpbin.org:443 HTTP/1.1 > Host: httpbin.org:443 > User-Agent: curl/7.52.1 > Proxy-Connection: Keep-Alive > < HTTP/1.1 400 Bad Request  Apart from that the URL for the proxy itself is wrong - it should be http://.. and not https://.. even if you proxy HTTPS traffic. But requests actually ignores the given protocol completely, so this error is not the reason for the problem. But just to demonstrate that it would not work either if the proxy itself got accessed with HTTPS (as the URL suggests): $ https_proxy=https://154.16.202.22:3128 curl -v https://httpbin.org/ip * Trying 154.16.202.22... * TCP_NODELAY set * Connected to (nil) (154.16.202.22) port 3128 (#0) * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH * TLSv1.2 (OUT), TLS header, Certificate Status (22): * TLSv1.2 (OUT), TLS handshake, Client hello (1): * error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol * Curl_http_done: called premature == 0 * Closing connection 0 curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol  So the fix here would be to use a different proxy, one which actually supports proxying https:// URLs.
Question: In Python, what is the actual difference between awaiting a coroutine and using asyncio.run()? They both seem to run a coroutine, the only difference that I can see being that await can only be used in a coroutine.

Answer: That is the exact difference. There should be exactly one call to asyncio.run() in your code, which will block until all coroutines have finished. Inside any coroutine, you can use await to suspend the current function, and asyncio will resume the function at some future time. All of this happens inside the asyncio.run() function, which schedules what functions can run when.
Question: When doing python setup.py sdist bdist_wheel  it creates build, dist, packagename.egg-info directories. I'd like to have them out of the current folder. I tried:  --dist-dir=../dist: works with sdist but packagename.egg-info is still there  --bdist-dir=../dist: for example:  python setup.py sdist bdist_wheel --dist-dir=../dist --bdist-dir=../dist2  works and the final bdist package is in ../dist. But the current folder still gets new directories build, dist, packagename.egg-info, which I don't want.   Question: how to have everything (the output of sdist and bdist_wheel) outside of the current folder? Of course I can write a script with mv, rm -r, etc. but I wanted to know if there exists a built-in solution.

Answer: I tried some time again with -d, --dist-dir, --bdist-dir but I found no way to do it in one-line. I'm afraid the shortest we could find (on Windows) is: python setup.py sdist bdist_wheel rmdir /s /q packagename.egg-info build ..\dist move dist ..
Question: I am trying to plot 3 dendrograms, 2 on top and one on the bottom. But the only way I figured out in doing this: fig, axes = plt.subplots(2, 2, figsize=(22, 14)) dn1 = hc.dendrogram(wardLink, ax=axes[0, 0]) dn2 = hc.dendrogram(singleLink, ax=axes[0, 1]) dn3 = hc.dendrogram(completeLink, ax=axes[1, 0])  Gives me a fourth blank graph on the bottom right. Is there a way to plot only 3 graphs?

Answer: You can redivide the canvas area as you desire and use the 3rd argument to subplot to tell it which cell to plot to: plt.subplot(2, 2, 1) # divide as 2x2, plot top left plt.plt(...) plt.subplot(2, 2, 2) # divide as 2x2, plot top right plt.plt(...) plt.subplot(2, 1, 2) # divide as 2x1, plot bottom plt.plt(...)  You can also use a gridspec as follows: gs = fig.add_gridspec(2, 2) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) ax3 = fig.add_subplot(gs[1, :]) ...
Question: I am trying to print emojis in both Windows Powershell and WSL Linux Terminal using Python3. I have tried using unicode, CLDR names and also installed the emoji library. print("\U0001F44D") print(emoji.emojize(':thumbs_up:'))  But in the terminal it is showing only a question mark within a box. No emoji is showing. What is the issue here? But if I copy the output here it is showing properly. Like this 👍

Answer: I don't think any of the Windows Shells has proper support for emoji/unicode characters, or it may not support emojis. If your using Windows, you may want to try Windows Terminal. It has complete Emoji support and should work with Powershell and WSL.
Question: I am trying to import a module from a sibling package in Python; following the instructions in this answer. My problem is that the import works... but mypy is saying that it's a bad import. I'm seeking to understand why mypy is reporting an error, and how to fix it. Directory structure/Code This is a module that I have installed successfully with python -m pip install -e .. I know it is installed because it is listed when I run pip freeze, and the project root is listed in sys.path when I print that out. mypackage ├── mypackage │ ├── foo │ │ ├── __init__.py │ │ └── db.py │ ├── bar │ │ ├── __init__.py │ │ └── model.py │ └── py.typed └── setup.py  In db.py: from mypackage.bar import model  In model.py: class MyClass: # implementation irrelevant  Error Message When I run mypy (mypy mypackage from the project base directory), I get the following error: mypackage/foo/db.py:7: error: Module 'mypackage.bar' has no attribute 'model'  What confuses me is that, when I open IDLE, the following imports/runs just fine: >>> from mypackage.bar import model >>> model.MyClass <class 'mypackage.bar.model.MyClass'>  My Question Why is mypy showing an error here when the import actually works? How can I get mypy to recognize that the import works?

Answer: Running mypy with the --namespace-packages flag made the check run without error, which pointed me to the actual problem: ./mypackage/mypackage/__init__.py did not exist, causing mypy to not pursue the import correctly. Python was working because in 3.3+, namespace packages are supported, but mypy requires a flag to check for those specifically. Thus, my overall solution was to add the needed __init__.py file.
Question: I want to check torch version in my device using Jupyter Notebook. I'm used this import torch print(torch.__version__)  but it didn't work and Jupyter notebook raised an error as below AttributeError Traceback (most recent call last) <ipython-input-8-beb55f24d5ec> in <module> 1 import torch ----> 2 print(torch.__version__) AttributeError: module 'torch' has no attribute '__version__'  Is there any command to check torch version using Jupyter notebook?

Answer:  I have tried to install new Pytorch version. But, it didn't work and then I deleted the Pytorch files manually suggested on my command line. Finally, I installed new Pytorch version using conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch and everything works fine. This code works well after that. import torch print(torch.__version__)
Question: Can I indicate a specific dictionary shape/form for an argument to a function in python? Like in typescript I'd indicate that the info argument should be an object with a string name and a number age: function parseInfo(info: {name: string, age: number}) { /* ... */ }  Is there a way to do this with a python function that's otherwise: def parseInfo(info: dict): # function body  Or is that perhaps not Pythonic and I should use named keywords or something like that?

Answer: In Python 3.8+ you could use the alternative syntax to create a TypedDict: from typing import TypedDict Info = TypedDict('Info', {'name': str, 'age': int}) def parse_info(info: Info): pass  From the documentation on TypedDict:  TypedDict declares a dictionary type that expects all of its instances to have a certain set of keys, where each key is associated with a value of a consistent type. This expectation is not checked at runtime but is only enforced by type checkers.
Question: Why should we use -> in def __init__(self, n) -> None:? I read the following excerpt from PEP 484, but I am unable to understand what it means.  (Note that the return type of __init__ ought to be annotated with -> None. The reason for this is subtle. If __init__ assumed a return annotation of -> None, would that mean that an argument-less, un-annotated __init__ method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other methods.)  What's the subtle difference between using def __init__(self, n) -> None: and def __init__(self, n):? Can someone explain the quoted excerpt in simple words?

Answer: The main reason is to allow static type checking. By default, mypy will ignore unannotated functions and methods. Consider the following definition: class Foo: def __init__(self): return 3 f = Foo()  mypy, a static type analysis tool, sees nothing wrong with this by default: $ mypy tmp.py Success: no issues found in 1 source file  but it produces a runtime TypeError (note that python here is Python 3.8.6): $ python tmp.py Traceback (most recent call last): File "tmp.py", line 5, in <module> f = Foo() TypeError: __init__() should return None, not 'int'  If you add the annotation -> None, then mypy will type-check the method and raise an error: $ mypy tmp.py tmp.py:3: error: No return value expected Found 1 error in 1 file (checked 1 source file)  mypy will even complain if you try to circumvent the the check by declaring def __init__(self) -> int: instead: $ mypy tmp.py tmp.py:2: error: The return type of "__init__" must be None Found 1 error in 1 file (checked 1 source file)  It's also worth noting that any annotation will make mypy pay attention; the lack of a return type is the same as -> None if you have at least one annotated argument: def __init__(self, x: int): return x  will produce the same "No return value expected" error as an explicit -> None. The explicit return type, though, is often easier to provide than any artificial argument type hints, and is arguably clearer than trying to type self.
Question: I am trying to setup an app using FastAPI but keep getting this error which I can't make sense of. My main.py file is as follows: from fastapi import FastAPI from app.routers import test app = FastAPI() app.include_router(test, prefix="/api/v1/test")  And in my routers/test.py file I have: from fastapi import APIRouter, File, UploadFile import app.schemas.myschema as my_schema router = APIRouter() Response = my_schema.Response @router.get("/", response_model=Response) def process(file: UploadFile = File(...)): # Do work  But I keep getting the following error:  File "/Users/Desktop/test-service/venv/lib/python3.8/site-packages/fastapi/routing.py", line 566, in include_router for route in router.routes: AttributeError: module 'app.routers.test' has no attribute 'routes' python-BaseException  I cant make sense of this as I can see something similar being done in the sample app here.

Answer: I think you want: app.include_router(test.router, prefix="/api/v1/test")  rather than: app.include_router(test, prefix="/api/v1/test")
Question: I use in Python the package pydantic and the linker Flake8. I want to use constr from pydantic with a regular Experssion. Only certain Characters should be passed. (a-z, A-Z, 0-9 and _) The regular Experssion "^[a-zA-Z0-9_]*$" works, but flake8 shows me the following error:  syntax error in forward annotation '^[a-zA-Z0-9_]*$' flake8(F722)  class RedisSettings(BaseModel): keyInput: constr(regex="^[a-zA-Z0-9_]*$") = "" keyOutput: constr(regex="^[a-zA-Z0-9_]*$") = ""  Can you help me to avoid the Error Message?

Answer: the error here comes from pyflakes which attempts to interpret type annotations as type annotations according to PEP 484 the annotations used by pydantic are incompatible with PEP 484 and result in that error. you can read more about this in this pyflakes issue I'd suggest either (1) finding a way to use pydantic which doesn't involve violating PEP 484 or (2) ignoring the errors from pyflakes using flake8's extend-ignore / # noqa: ... / per-file-ignores  disclaimer: I am one of the pyflakes maintainers and I am the current flake8 maintainer
Question: Is there any way to make SharedMemory object created in Python persist between processes? If the following code is invoked in interactive python session: >>> from multiprocessing import shared_memory >>> shm = shared_memory.SharedMemory(name='test_smm', size=1000000, create=True)  it creates a file in /dev/shm/ on a Linux machine. ls /dev/shm/test_smm /dev/shm/test_smm  But when the python session ends I get the following: /usr/lib/python3.8/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked shared_memory objects to clean up at shutdown warnings.warn('resource_tracker: There appear to be %d  and the test_smm is gone: ls /dev/shm/test_smm ls: cannot access '/dev/shm/test_smm': No such file or directory  So is there any way to make the shared memory object created in python persist across process runs? Running with Python 3.8

Answer: You can unregister a shared memory object from the resource cleanup process without unlinking it: $ python3 Python 3.8.6 (default, Sep 25 2020, 09:36:53) [GCC 10.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from multiprocessing import shared_memory, resource_tracker >>> shm = shared_memory.SharedMemory(name='test_smm', size=1000000, create=True) >>> resource_tracker.unregister(shm._name, 'shared_memory') >>> $ ls /dev/shm/test_smm /dev/shm/test_smm  I don't know whether this is portable, and it doesn't look like a supported way of using the multiprocessing module, but it works on Linux at least.
Question: I have a dataframe where I want to replace values in a column, but the dict describing the replacement is based on values in another column. A sample dataframe would look like this:  Map me strings date 0 1 test1 2020-01-01 1 2 test2 2020-02-10 2 3 test3 2020-01-01 3 4 test2 2020-03-15  I have a dictionary that looks like this: map_dict = {'2020-01-01': {1: 4, 2: 3, 3: 1, 4: 2}, '2020-02-10': {1: 3, 2: 4, 3: 1, 4: 2}, '2020-03-15': {1: 3, 2: 2, 3: 1, 4: 4}}  Where I want the mapping logic to be different based on the date. In this example, the expected output would be:  Map me strings date 0 4 test1 2020-01-01 1 4 test2 2020-02-10 2 1 test3 2020-01-01 3 4 test2 2020-03-15  I have a massive dataframe (100M+ rows) so I really want to avoid any looping solutions if at all possible. I have tried to think of a way to use either map or replace but have been unsuccessful

Answer: Use DataFrame.join with MultiIndex Series created by DataFrame cosntructor and DataFrame.stack: df = df.join(pd.DataFrame(map_dict).stack().rename('new'), on=['Map me','date']) print (df) Map me strings date new 0 1 test1 2020-01-01 4 1 2 test2 2020-02-10 4 2 3 test3 2020-01-01 1 3 4 test2 2020-03-15 4
Question: I want to update my SQLite DB with data provided by an external API and I don't want to check every time for any conflicts by myself, instead, I want to take advantage of UPSERT statement. SQLAlchemy documentation for version 1.4 (still in beta but that's ok) shows that this is possible. But I don't know how to get this to work with flask_sqlalchemy. I've also tried to include sqlite_on_conflict='IGNORE' in constraint definitions (as mentioned in SQLAlchemy docs for version 1.3 here): class SomeModel(db.Model): __table_args__ = ( db.ForeignKeyConstraint( ('id', 'other_id'), ('other_table.id', 'other_table.other_id') sqlite_on_conflict='IGNORE' ), ) # ...  I then verified SQL output with SQLALCHEMY_ECHO set to True and it didn't work at all... I tried with both 1.3 and 1.4 SQLAlchemy versions.

Answer: I found the solution thanks to this gist (credits for droustchev) which I slightly edited. It's a bit messy but it works: from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql import Insert @compiles(Insert, 'sqlite') def suffix_insert(insert, compiler, **kwargs): stmt = compiler.visit_insert(insert, **kwargs) if insert.dialect_kwargs.get('sqlite_on_conflict_do_nothing'): stmt += ' ON CONFLICT DO NOTHING' return stmt Insert.argument_for('sqlite', 'on_conflict_do_nothing', False)  Just include this code in your models.py file (or whatever filename you have) and then you can write: some_random_values = [ {'id': 1, 'some_column': 'asadsadad'}, {'id': 2, 'some_column': 'dsawaefds'} ] stmt = db.insert(MyModel, sqlite_on_conflict_do_nothing=True).values(some_random_values) db.session.execute(stmt)  This should do the trick.
Question: I'm now studying recursion and try to build some codes to generate all binary strings of the given length 'n'. I found a code to use for loop: n = 5 for i in range(2**n, 2**(n+1)): print(bin(i)[3:])  But is there any other way to solve this problem using recursion? Thank you!

Answer: It's hard to determine what way is "the best" ;) We have to add zero or one to current string and go to the next recursion level. Stop-condition is reaching of needed length (here n-1 because we have to provide leading one corresponding to your example) def genbin(n, bs = ''): if n-1: genbin(n-1, bs + '0') genbin(n-1, bs + '1') else: print('1' + bs) genbin(4) 1000 1001 1010 1011 1100 1101 1110 1111