eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm still reaseaching about Loguru, but I can't find an easy way to do this. I want to use the default options from Loguru, I believe they are great, but I want to add information to it, I want to add the IP of a request that will be logged. If I try this: import sys from loguru import logger logger.info("This is log info!") # This is directle from Loguru page logger.add(sys.stderr, format="{extra[ip]} {extra[user]} {message}") context_logger = logger.bind(ip="192.168.0.1", user="someone") context_logger.info("Contextualize your logger easily") context_logger.bind(user="someone_else").info("Inline binding of extra attribute") context_logger.info("Use kwargs to add context during formatting: {user}", user="anybody")  That logs this:  I know that with logger.remove(0) I will remove the default logs, but I want to use it to obtain something like this: 2022-02-03 15:16:54.920 | INFO | __main__:<module>:79 - XXX.XXX.XX.X - Use kwargs to add context during formatting: anybody, with XXX.XXX.XX.X being the IP. Using the default config (for color and the rest of thing) and adding a little thing to the format. I'm trying to access the default configs, but I haven't been able to import them and use them with logger.add. I think I will have to configure everything from scratch. Hope someone can help me, thanks.

Answer: I made the same question in the Github Repository and this was the answer by Delgan (Loguru maintainer): I think you simply need to add() your handler using a custom format containing the extra information. Here is an example: logger_format = ( "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | " "<level>{level: <8}</level> | " "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | " "{extra[ip]} {extra[user]} - <level>{message}</level>" ) logger.configure(extra={"ip": "", "user": ""}) # Default values logger.remove() logger.add(sys.stderr, format=logger_format)  Extra: if you want to use TRACE level use this when adding the configurations: logger.add(sys.stderr, format=logger_format, level="TRACE")
Question: I need to install playwright inside of docker. This is my dockerfile. FROM python:3.9 EXPOSE 8000 WORKDIR /fastanalytics COPY /requirements.txt /fastanalytics/requirements.txt RUN pip install --no-cache-dir --upgrade -r /fastanalytics/requirements.txt RUN playwright install RUN playwright install-deps RUN apt-get update && apt-get upgrade -y  But when I am installing I got the below error. I tried installing everything in the error message but it didn't help. E: Package 'ttf-ubuntu-font-family' has no installation candidate E: Unable to locate package libenchant1c2a E: Unable to locate package libicu66 E: Package 'libjpeg-turbo8' has no installation candidate  

Answer: Microsoft released a python docker image for Playwright Dockerfile # Build Environment: Playwright FROM mcr.microsoft.com/playwright/python:v1.21.0-focal # Add python script to Docker COPY index.py / # Run Python script CMD [ "python", "index.py" ]  Check Playwright - Docker docs for the latest playwright version.
Question: With Pandas groupby, I can do things like this: >>> df = pd.DataFrame( ... { ... "A": ["foo", "bar", "bar", "foo", "bar"], ... "B": ["one", "two", "three", "four", "five"], ... } ... ) >>> print(df) A B 0 foo one 1 bar two 2 bar three 3 foo four 4 bar five >>> print(df.groupby('A')['B'].unique()) A bar [two, three, five] foo [one, four] Name: B, dtype: object  What I am looking for is output that produces a list of indices instead of a list of column B: A bar [1, 2, 4] foo [0, 3]  However, groupby('A').index.unique() doesn't work. What syntax would provide me the output I'm after? I'd be more than happy to do this in some other way than with groupby, although I do need to group by two columns in my real application.

Answer: You do not necessarily need to have a label in groupby, you can use a grouping object. This enables things like: df.index.to_series().groupby(df['A']).unique()  output: A bar [1, 2, 4] foo [0, 3] dtype: object  getting the indices of the unique B values: df[~df[['A', 'B']].duplicated()].index.to_series().groupby(df['A']).unique()
Question: I can't understand the following two examples of behaviour of list.append() in Python: list_1 = ['A', 'B'] list_2 = ['C', 'D'] copy_l1 = list_1 copy_l1.append(list_2)   Example print(copy_l1)  result: ['A', 'B', ['C', 'D']] expected: ['A', 'B', 'C', 'D']. I kind of understand this, but how to get the expected result?  Example print(list_1)  result: ['A', 'B', ['C', 'D']] expected: ['A', 'B'].   This is the most puzzling for me. Why does copy_l1.append(list_2) also affect list_1? Due to my backgound in C, this looks to me like I'm working on pointers, but I gather that should not be the case. What means?

Answer: The first result is expected as you are adding the list itself, not its values, to copy_l1. To get the desired result, use either of the following:  copy_l1 += list2 copy_l1.extend(list2)   The second result is harder to understand, but it has to do with the fact that lists are mutable in Python. To understand this, you must first understand what actually happens when you assign one variable to another. Variable Assignment When you use var_a = var_b, both names point to the same variable - that is, the same space in memory; they are just two different ways of accessing it. This can be tested using is, which checks for identity, not just value: a = 1 b = a print(a is b) # True  So you would expect that changing the value of one name would also affect the other. However, this is not usually the case. Changing the Value of an Immutable Variable Most basic data types in Python are immutable. This means that their value can't actually be changed once they are created. Some examples of immutable data types are strs, ints, floats and bools. If the data type of a variable is immutable, then its value can't be changed directly. Instead, when you alter the value of a name pointing it, this is what actually happens:  A new variable is created Its value is set to the new value It is given the same name as the name which you are trying to alter - it effectively replaces it  During this process, only one name has had its value changed (or rather, replaced) and any other names pointing to the same variable stay unchanged. You can test that the actual variable the name points to has changed using is: a = 5 b = a print(a is b) # True a += 4 print(a is b) # False  Because of this, the fact that variable assignment works the way it does can almost always be ignored. But this changes when you use mutable variables, such as lists. Changing the Value of a Mutable Variable Because lists are mutable, their value can actually be changed so this three-step process is not necessary. Therefore, all names pointing to the list get changed. You can see this by using is once again: a = [1, 2] b = a print(a is b) # True a += [3, 4] print(a is b, "again") # True again  How to Stop this Happening To stop this from happening, use .copy() to get a shallow copy of the list: copy_l1 = list1.copy()  Now both names point to different variables, which happen to have the same value. Operations on one will not affect the other.
Question: Whenever I install a pip library in Python, I get a series of warnings. For example :  WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution - (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution - (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution -ip (c:\python310\lib\site-packages) WARNING: Ignoring invalid distribution - (c:\python310\lib\site-packages)   How can I avoid getting these warnings ?

Answer: the warning below:  can fix as follows. go to the lib\site-packages folder, then look for folders starting with ~ like what you see in the picture below  and mentioned in that warning, then remove them this can be fixed this warning and no longer appears
Question: fig.write_image("images/fig1.png",format='png',engine='kaleido') This makes my VSCode go bananas, the terminal hangs and the program stops then and there. Everything works fine if I remove just that line. I want to save the plots as pngs, but it is not working. I have kaleido installed.

Answer: Try this version of kaleido. pip install kaleido==0.1.0post1  It works for me
Question: I have seen typing.Callable, but I didn't find any useful docs about it. What exactly is typing.Callable?

Answer: typing.Callable is the type you use to indicate a callable. Most python types that support the () operator are of the type collections.abc.Callable. Examples include functions, classmethods, staticmethods, bound methods and lambdas. In summary, anything with a __call__ method (which is how () is implemented), is a callable. PEP 677 attempted to introduce implicit tuple-with-arrow syntax, so that something like Callable[[int, str], list[float]] could be expressed much more intuitively as (int, str) -> list[float]. The PEP was rejected because the benefits of the new syntax were not deemed sufficient given the added maintenance burden and possible room for confusion.
Question: I want a circular convolution function where I can set the number N as I like. All examples I looked at like here and here assume that full padding is required but that not what I want. I want to have the result for different values of N  so input would N and and two different arrays of values the output should be the N point convolved signal   Here is the formula for circular convolution. Sub N can be seen as the modulo operation.   taken from this basic introduction   update for possible solution This answer is a suitable solution when the array a is piled accordingly to the different cases of N. When I find time I will post a complete answer, meanwhile feel free to do so. Thanks to @André pointing this out in the comments!   examples for input/output from here N = 4  N = 7 with zero padding 

Answer: I think that this should work: def conv(x1, x2, N): n, m = np.ogrid[:N, :N] return (x1[:N] * x2[(n - m) % N]).sum(axis=1)  This is a direct translation of the formula posted in the question:  To implement this formula, first we compute an array of indices used by x₂. This is done using the code n, m = np.ogrid[:N, :N] indices = (n - m) % N  For example, for N=5, the array indices is: [[0 4 3 2 1] [1 0 4 3 2] [2 1 0 4 3] [3 2 1 0 4] [4 3 2 1 0]]  The entry in the i-th row and j-th column is (i-j) % N. Then, x2[indices] creates an array consisting of elements of x2 corresponding to these indices. It remains to multiply each row of this array by the first N elements of x1 and take the sum of each row: (x1[:N] * x2[indices]).sum(axis=1)
Question: When attempting to install "kaleido" via Poetry, I receive the following error message: ~ poetry add kaleido Using version ^0.2.1 for kaleido Updating dependencies Resolving dependencies... (3.1s) Package operations: 1 install, 0 updates, 0 removals • Installing kaleido (0.2.1.post1): Failed RuntimeError Unable to find installation candidates for kaleido (0.2.1.post1) at ~/.poetry/lib/poetry/installation/chooser.py:72 in choose_for 68│ 69│ links.append(link) 70│ 71│ if not links: → 72│ raise RuntimeError( 73│ "Unable to find installation candidates for {}".format(package) 74│ ) 75│ 76│ # Get the best link  However, the "kaleido" appears in the poetry.lock file: [[package]] name = "kaleido" version = "0.2.1.post1" description = "Static image export for web-based visualization libraries with zero dependencies" category = "main" optional = false python-versions = "*"  If I try to export an image, I unsurprisingly receive the following error message: ValueError: Image export using the "kaleido" engine requires the kaleido package, which can be installed using pip: $ pip install -U kaleido  Does anyone know how to install this package via poetry (or amend the .lock file to do it manually)?

Answer: Firstly try to use a master version of poetry as advised in Github issue or upgrade it to the latest version pip3 install --upgrade poetry  Then try to install with kaleido with locked version: poetry add kaleido==0.2.1  That worked in my case.
Question: I know it's possible to create array of string in postgres but I want to create a model in sqlalchemy that contains a list or array column but I don't know how Please view the code below class Question(Base): __tablename__= 'questions' id = Column(Integer, nullable=False, primary_key=True) question = Column(String,nullable=False) options = Column([String],nullable=False) answer = Column(String,nullable=False) created_at = Column(TIMESTAMP(timezone=true),server_default=text('now()') 

Answer: You need to use: from sqlalchemy.dialects.postgresql import ARRAY  Here: from datetime import datetime from sqlalchemy import * from sqlalchemy.dialects.postgresql import ARRAY from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Question1(Base): __tablename__= 'questions' id = Column(Integer, nullable=False, primary_key=True) question = Column(String,nullable=False) options = Column(ARRAY(String),nullable=False) answer = Column(String,nullable=False)  Example: Python 3.8.2 (default, Dec 21 2020, 15:06:04) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from datetime import datetime >>> from sqlalchemy import * >>> from sqlalchemy.dialects.postgresql import ARRAY >>> from sqlalchemy.ext.declarative import declarative_base >>> Base = declarative_base() >>> class Question1(Base): ... __tablename__= 'questions' ... id = Column(Integer, nullable=False, primary_key=True) ... question = Column(String,nullable=False) ... options = Column(ARRAY(String),nullable=False) ... answer = Column(String,nullable=False) ... >>>
Question: I’m trying to get the function below to run. However I’m getting an error saying TypeError: ‘type’ object is not subscriptable def dist(loc1: tuple[float], loc2: tuple[float]) -> float: dx = loc1[0] - loc2[0] dy = loc1[1] - loc2[1] return (dx**2 + dy**2)**0.5 

Answer: You need to use typing.Tuple, not the tuple class. from typing import Tuple def dist(loc1: Tuple[float], loc2: Tuple[float]) -> float: dx = loc1[0] - loc2[0] dy = loc1[1] - loc2[1] return (dx**2 + dy**2)**0.5 dist((1,2),(2,1)) # output 1.4142135623730951
Question: I have installed Python 3.9 in the Ubuntu 20.04 LTS. Now the system has both Python 3.8 and Python 3.9. # which python # which python3 /usr/bin/python3 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8  But the pip3 command will still install everything into the Python 3.8 directory. # pip3 install --upgrade --find-links file:///path/to/directory <...>  I want to change that default pip3 behavior by updating the symbolic link /usr/bin/python3 to /usr/bin/python3.9. How to do that? # update-alternatives --set python3 /usr/bin/python3.9 This command will not work as expected.  Here is the pip3 info: # which pip3 /usr/bin/pip3 # ls -alith /usr/bin/pip3 12589712 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8) #  The alias command will not work: # alias python3=python3.9 # ls -alith /usr/bin/python3 12583916 lrwxrwxrwx 1 root root 9 Jul 19 2021 /usr/bin/python3 -> python3.8 

Answer: You should be able to use python3.9 -m pip install <package> to run pip with a specific python version, in this case 3.9. The full docs on this are here: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/ If you want python3 to point to python3.9 you could use the quick and dirty. alias python3=python3.9  EDIT: Tried to recreate your problem, # which python3 /usr/bin/python3 # python3 --version Python 3.8.10 # which python3.8 /usr/bin/python3.8 # which python3.9 /usr/bin/python3.9  Then update the alternatives, and set new priority: # sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 # sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 # sudo update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.9 2 auto mode 1 /usr/bin/python3.8 2 manual mode * 2 /usr/bin/python3.9 2 manual mode Press <enter> to keep the current choice[*], or type selection number: 0  Check new version: # ls -alith /usr/bin/python3 3338 lrwxrwxrwx 1 root root 25 Feb 8 14:33 /usr/bin/python3 -> /etc/alternatives/python3 # python3 -V Python 3.9.5 # ls -alith /usr/bin/pip3 48482 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3 # pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)  Hope this helps (tried it in wsl2 Ubuntu 20.04 LTS)
Question: def function(a: int, b: str) -> None: pass def wrapper(extra: int, *args, **kwargs) -> None: do_something_with_extra(extra) function(*args, **kwargs)  Is there an easy way for wrapper to inherit function()'s type hints without retyping all of them? Normally I'd write def wrapper(extra: int, a: int, b: str) -> None:  But it becomes very verbose with a lot of arguments, and I have to update wrapper() every time I update function()'s arguments, and using *args, **kwargs means there won't be proper autocomplete in vscode and other editors.

Answer: The best that you can do is to use a decorator for this purpose, and you can use Concatenate and ParamSpec. I use python 3.8 and I had to install typing_extensions for them. In the code below, if you type function in VSCode, it will show the int argument but without the "extra" name in front of it. from typing import Callable, TypeVar from typing_extensions import Concatenate, ParamSpec P = ParamSpec('P') R = TypeVar('R') def wrapper(func: Callable[P, R]) -> Callable[Concatenate[int, P], R]: def inner(extra, *args, **kwargs): print(extra) func(*args, **kwargs) return inner @wrapper def function(a: int, b: str) -> None: print(a, b) pass # typing function will show 'function: (int, a: int, b: str) -> None'
Question: I'm unable to import pipeline function of transformers class as my jupyter kernel keeps dying. Tried on transformers-4.15.0 and 4.16.2. Anyone faced this issue? I tried importing the class in a new notebook as you can see in the image and it keeps killing the kernel. 

Answer: It works fine for me.  You could try creating a fresh conda environment and reinstalling the app. You could also try using jupyterlab instead of jupyter-notebook. Are you on Mac OS? I couldn't get it to run at first using conda install transformers my jupyterlab kept hanging as well. Then I did this, conda install -c huggingface transformers and here is the result.  It works fine for me on linux and Mac now.
Question: I updated Pandas to 1.4.0 with yfinance 0.1.70. Previously, I had to stay with Pandas 1.3.5 as Pandas and yfinance did't play well together. These latest versions of Pandas and yfinance now work together, BUT Pandas now gives me this warning: Future Warning: Passing method to DatetimeIndex.get_loc is deprecated... Use index.get_indexer([item], method=...) instead  I had enough trouble as a novice Python person getting the original get_loc statement to work: last_week = format((df.index[df.index.get_loc(last_week, method='nearest')]).strftime('%Y-%m-%d'))  This statement allowed me to get a date from the dataframe that I could use further in determining the value associated with that date: week_value = df.loc[last_week, ans]  Truth be known, I am intimidated in trying to change this statement to be compliant with the new and improved get_indexer function. Can someone help me out please?

Answer: Should be pretty simple. Just change get_loc(XXX, ...) to get_indexer([XXX], ...)[0]: last_week = format((df.index[df.index.get_indexer([last_week], method='nearest')[0]]).strftime('%Y-%m-%d'))
Question: Tox: https://tox.wiki/en/latest/ pre-commit: https://pre-commit.com/ I would like to understand the borders for both choices. I know that pre-commit creates a py environment - same as tox. To me, their architecture looks a bit the same. Some people use them in combination... what pre-commit can't do, that tox can? I saw examples where with pre-commit during CI pipeline you can run unit tests, etc. Which one is the best to integrate within the CI build?

Answer: In short, pre-commit is a linter/formatter runner, tox is a generic virtual env management and test command line tool. While tox could run linters too, it is tedious to manage the versions of the linters. In pre-commit you can just run pre-commit autoupdate, and all linters get updated. On the other hand tox can run e.g. a test suite or a coverage report for many different Python versions. This is not only helpful for a library, but also for an app - so you can already test the upcoming Python versions. tox is also used to create documentation, and sometimes also to make a release - you can't (shouldn't) do this with pre-commit. And tox is certainly not obsolete, although GitHub actions can test against different Python versions, as you can run tox both local and in CI. I gave a lightning talk on this topic: https://www.youtube.com/watch?v=OnM3KuE7MQM  Which one is the best to integrate within the CI build?  I like to run pre-commit via tox, both in CI and locally. Example tox configuration https://github.com/jugmac00/flask-reuploaded/blob/6496f8427a06be4a9f6a5699757ca7f9aba78ef6/tox.ini#L24-L26 Example pre-commit configuration https://github.com/jugmac00/flask-reuploaded/blob/6496f8427a06be4a9f6a5699757ca7f9aba78ef6/.pre-commit-config.yaml
Question: I am trying to measure code coverage with a code that uses threads created by the python threading module. I am using coverage to measure coverage. However I can not get the code that is run within a thread to get measured. I tried following the suggestion on the coverage docs to measure coverage in subprocesses but no luck. Here is a minimal example file untitled.py: import time, threading import numpy as np def thread(): print(f'{threading.get_ident()}: started thread') time.sleep(np.random.uniform(1, 10)) print(f'{threading.get_ident()}: done') def run_threads(): threads = [threading.Thread(target=thread)] for t in threads: t.start() print('started all threads') for t in threads: t.join() print('all threads done') if __name__ == '__main__': run_threads()  > coverage run untitled.py 139952541644544: started thread started all threads 139952541644544: done all threads done > coverage combine Combined data file .coverage.248973.677959 > coverage report -m Name Stmts Miss Cover Missing ------------------------------------------- untitled.py 14 3 79% 6-8 ------------------------------------------- TOTAL 14 3 79% >  As you can see the lines 6-8 (the thread() function) is executed but not measured. For context I am running on a linux machine, Python 3.9.0, coverage 6.2. The directory contains this .coveragerc file: # .coveragerc to control coverage.py [run] source = ./ parallel = True concurrency = multiprocessing [report] # Regexes for lines to exclude from consideration exclude_lines = # Have to re-enable the standard pragma pragma: no cover # Don't complain about missing debug-only code: def __repr__ if self\.debug # Don't complain if tests don't hit defensive assertion code: raise AssertionError raise NotImplementedError # Don't complain if non-runnable code isn't run: if 0: if __name__ == .__main__.:  I am very thankful for any suggestion!!

Answer: You need to specify "thread" as part of your concurrency setting: concurrency = multiprocessing,thread  I'm not sure you need multiprocessing. Your sample program doesn't use it, maybe your real program doesn't either.