eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to install Tensorflow on my MacBook Pro with the M1 chip. The operating system of my MacBook is MacOS Big Sur Version 11.0.  In order to install Tensorflow to use it with Python, I have followed this tutorial, which says that I have to do the following:  Install Homebrew.  Download MiniForge3 for macOS arm64 chips (link provided in the webpage).  Install MiniForge3 using: chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh sh ~/Downloads/Miniforge3-MacOSX-arm64.sh source ~/miniforge3/bin/activate   Create a folder to set up an environment for Tensorflow. mkdir tensorflow-test cd tensorflow-test   Make and activate Conda environment. conda create --prefix ./env python=3.9.7 conda activate ./env   Install Tensorflow dependencies. conda install -c apple tensorflow-deps python -m pip install tensorflow-macos python -m pip install tensorflow-metal    After this, I open a Jupyter Notebook and I try to import tensorflow, but this error shows up: OSError: dlopen(/Users/blancoarnau/tensorflow-test/env/lib/python3.9/site-packages/tensorflow/python/platform/../../core/platform/_cpu_feature_guard.so, 6): Symbol not found: __ZNKSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEE3strEv Referenced from: /Users/blancoarnau/tensorflow-test/env/lib/python3.9/site-packages/tensorflow/python/platform/../../core/platform/_cpu_feature_guard.so (which was built for Mac OS X 12.3) Expected in: /usr/lib/libc++.1.dylib  As you can see in this screenshot:  Do you have an idea why this is happening?

Answer: check the message details: (which was built for Mac OS X 12.3) you need to upgrade macOS to 12.3
Question: How can zoneinfo be installed on a Linux Debian 10 machine? our script is working just fine on Mac. When pushed to Linux Debian and run, the script returns the error: myemail@repo-name:~/path-to/mainfolder$ python3 main_cbb_v2.py Traceback (most recent call last): File "main_cbb_v2.py", line 3, in <module> from utils import * File "/home/pathto-utils/utils.py", line 16, in <module> from zoneinfo import ZoneInfo ModuleNotFoundError: No module named 'zoneinfo'  and when we attempt to install the library, we get the error: pip3 install zoneinfo Collecting zoneinfo Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple /zoneinfo/  and we get this same exact error even if sudo su is run beforehand. And if backports prefix is used: pip3 install backports.zoneinfo Requirement already satisfied: backports.zoneinfo in /usr/local/lib/python3.7/dist-packages (0.2.1)  How can this be troubleshooted further?

Answer: zoneinfo is new in python 3.9, so the undelying issue is probably that you have different python versions on different systems. You can either upgrade your python version or you use the backports module which you already have installed, but then your code needs to be: from backports.zoneinfo import ZoneInfo
Question: I am following this mini-tutorial/blog on pytest-mock. I can not understand how the mocker is working since there is no import for it - in particular the function declaration def test_mocking_constant_a(mocker): import mock_examples.functions from mock_examples.functions import double def test_mocking_constant_a(mocker): mocker.patch.object(mock_examples.functions, 'CONSTANT_A', 2) expected = 4 actual = double() # now it returns 4, not 2 assert expected == actual  Somehow the mocker has the attributes/functions of pytest-mocker.mocker: in particular mocker.patch.object . But how can that be without the import statement?

Answer: The mocker variable is a Pytest fixture. Rather than using imports, fixtures are supplied using dependency injection - that is, Pytest takes care of creating the mocker object for you and supplies it to the test function when it runs the test. Pytest-mock defines the "mocker" fixture here, using the Pytest fixture decorator. Here, the fixture decorator is used as a regular function, which is a slightly unusual way of doing it. A more typical way of using the fixture decorator would look something like this: @pytest.fixture() def mocker(pytestconfig: Any) -> Generator[MockerFixture, None, None]: """ Return an object that has the same interface to the `mock` module, but takes care of automatically undoing all patches after each test method. """ result = MockerFixture(pytestconfig) yield result result.stopall()  The fixture decorator registers the "mocker" function with Pytest, and when Pytest runs a test with a parameter called "mocker", it inserts the result of the "mocker" function for you. Pytest can do this because it uses Python's introspection features to view the list of arguments, complete with names, before calling the test function. It compares the names of the arguments with names of fixtures that have been registered, and if the names match, it supplies the corresponding object to that parameter of the test function.
Question: I'm building a python Instagram bot, and I'm trying to get it to click on the DMs icon, but I'm not sure how to select it. I tried selecting by Xpath, but I can't seem to be able to navigate it to the icon. Here's Instagram's html code for the DMs icon: <svg aria-label="Messenger" class="_8-yf5 " color="#262626" fill="#262626" height="24" role="img" viewBox="0 0 24 24" width="24">  Any help is appreciated.

Answer: You have to apply a slightly different locator strategy to find svg. Here is what works: driver.find_element(By.XPATH, "//*[name()='svg']")  assuming that this is the only svg element (as provided in your query) Combination of more than one attribute from the same DOM line: //*[name()='svg' and @aria-label='Messenger']
Question: Can someone explain me how this function of the library fuzzywuzzy in Python works? I know how the Levenshtein distance works but I don't understand how the ratio is computed. b = fuzz.token_sort_ratio('controlled', 'comparative')  The result is 38

Answer: Levenshtein distance As you probably already know the Levenshtein distance is the minimum amount of insertions / deletions / substitutions to convert one sequence into another sequence. It can be normalized as dist / max_dist, where max_dist is the maximum distance possible given the two sequence lengths. In the case of the Levenshtein distance this results in the normalization dist / max(len(s1), len(s2)). In addition a normalized similarity can be calculated by inverting this: 1 - normalized distance. >>> from rapidfuzz.distance import Levenshtein >>> Levenshtein.distance('controlled', 'comparative') 8 >>> Levenshtein.similarity('controlled', 'comparative') 3 >>> Levenshtein.normalized_distance('controlled', 'comparative') 0.7272727272727273 >>> Levenshtein.normalized_similarity('controlled', 'comparative') 0.2727272727272727  Indel distance The Indel distance is the minimum amount of insertions / deletions to convert one sequence into another sequence. So it behaves similar to the Levenshtein distance, but does not allow substitutions. Since any substitution can be replaced by a insertion + deletion this can be calculated e.g. by modifying the cost of substitutions in the Levenshtein distance to 2. The Indel distance can be normalized in a similar way to the Levenshtein distance, but uses a different max_dist: dist / (len(s1) + len(s2)). >>> from rapidfuzz.distance import Indel >>> Indel.distance('controlled', 'comparative') 13 >>> Indel.similarity('controlled', 'comparative') 8 >>> Indel.normalized_distance('controlled', 'comparative') 0.6190476190476191 >>> Indel.normalized_similarity('controlled', 'comparative') 0.38095238095238093  ratio The ratio in fuzzywuzzy/thefuzz/rapidfuzz is the normalized indel similarity scaled to 100. >>> from rapidfuzz.distance import Indel >>> from rapidfuzz import fuzz >>> Indel.normalized_similarity('controlled', 'comparative') * 100 38.095238095238095 >>> fuzz.ratio('controlled', 'comparative') 38.095238095238095  The only difference in fuzzywuzzy/thefuzz is, that results are rounded: >>> from fuzzywuzzy import fuzz >>> fuzz.ratio('controlled', 'comparative') 38  token_sort_ratio token_sort_ratio is a variant of ratio, which sorts the words in both sequences before comparing them: s1 = " ".join(sorted(s1.split())) s2 = " ".join(sorted(s2.split())) fuzz.ratio(s1, s2)  In your example token_sort_ratio will have the same result as ratio, since both sequences are already sorted.
Question: "I was doing the follow exercise: A gymnast can earn a score between 1 and 10 from each judge. Print out a series of sentences, "A judge can give a gymnast _ points." Don't worry if your first sentence reads "A judge can give a gymnast 1 points." However, you get 1000 bonus internet points if you can use a for loop, and have correct grammar. My code is this: scores = (tuple(range(1,11))) for score in scores: print (f'A judge can give a gymnast {score} points.')  And my output of course was:  A judge can give a gymnast 1 points.  A judge can give a gymnast 2 points.  A judge can give a gymnast 3 points.  A judge can give a gymnast 4 points.  A judge can give a gymnast 5 points.  A judge can give a gymnast 6 points.  A judge can give a gymnast 7 points.  A judge can give a gymnast 8 points.  A judge can give a gymnast 9 points.  A judge can give a gymnast 10 points.   The question is: How can I correct the grammar in my loop? I mean, when is "1 point" how can I make my loop to say "1 point" and not "1 points" and for the others scores maintain the "points"?

Answer: Heres another way of doing it. The answer above may(I don't know) be more efficient, in my opinion this is more readable for future developers. scores = (tuple(range(1,11))) for score in scores: if score == 1: print ('A judge can give a gymnast 1 point.') else: print (f'A judge can give a gymnast {score} points.')
Question: I am writing a program and python and want to use the match-case statement allowed in python 3.10 to be able to switch on 'enum' values i.e: match token.type: case TOK_INT: #0 # do stuff case TOK_STRING: #1 # do stuff etc...  however trying to do this causes python to throw a SyntaxError: name capture 'TOK_INT' makes remaining patterns unreachable error. I know I can fix it by manually typing the number associated with each 'enum' variable but I am wondering if there is a better way of going about this?

Answer: This will work with any dotted name (like math.pi). However an unqualified name (i.e. a bare name with no dots) will be always interpreted as a capture pattern, so avoid that ambiguity by always using qualified constants in patterns. you can refer here
Question: I followed this documentation to setup up a single user: https://fastapi.tiangolo.com/advanced/security/http-basic-auth/ But I only get prompted for user/pass for that one end point, "/users/me". How do I ensure that all endpoints are behind auth?

Answer: You can configure FastAPI with a set of dependencies that needs to be resolved for any endpoint by giving the paramter directly when creating the FastAPI application (i.e. global dependencies): security = HTTPBasic() app = FastAPI(dependencies=[Depends(security)])  If you want some endpoints to be authenticated and some to be unauthenticated, you can create separate instances of APIRouter, then assign required dependencies to the one that require authentication: unauthenticated_router = APIRouter() authenticated_router = APIRouter(dependencies=[Depends(security)])  .. and then either include other routers under each (using .include_router) or register endpoints as you'd do with the app object - but instead use your two routers.
Question: In python, if I am writing a function, is this the best way to type hint a list of strings: def sample_def(var:list[str]):

Answer: I would use the typing module from typing import List def foo(bar: List[str]): pass  The reason is typing contains so many type hints and the ability to create your own, specify callables, etc. Definitely check it out. Edit: I guess as of Python 3.9 typing is deprecated (RIP). Instead it looks like you can use collections.abc.*. So you can do this if you're on Python 3.9+: from collections.abc import Iterable def foo(bar: Iterable[str]): pass  You can take a look at https://docs.python.org/3/library/collections.abc.html for a list of ABCs that might fit your need. For instance, it might make more sense to specify Sequence[str] depending on your needs of that function.
Question: Looping over a list of bigrams to search for, I need to create a boolean field for each bigram according to whether or not it is present in a tokenized pandas series. And I'd appreciate an upvote if you think this is a good question! List of bigrams: bigrams = ['data science', 'computer science', 'bachelors degree']  Dataframe: df = pd.DataFrame(data={'job_description': [['data', 'science', 'degree', 'expert'], ['computer', 'science', 'degree', 'masters'], ['bachelors', 'degree', 'computer', 'vision'], ['data', 'processing', 'science']]})  Desired Output:  job_description data science computer science bachelors degree 0 [data, science, degree, expert] True False False 1 [computer, science, degree, masters] False True False 2 [bachelors, degree, computer, vision] False False True 3 [data, bachelors, science] False False False  Criteria:  Only exact matches should be replaced (for example, flagging for 'data science' should return True for 'data science' but False for 'science data' or 'data bachelors science') Each search term should get it's own field and be concatenated to the original df  What I've tried: Failed: df = [x for x in df['job_description'] if x in bigrams] Failed: df[bigrams] = [[any(w==term for w in lst) for term in bigrams] for lst in df['job_description']] Failed: Could not adapt the approach here -> Match trigrams, bigrams, and unigrams to a text; if unigram or bigram a substring of already matched trigram, pass; python Failed: Could not get this one to adapt, either -> Compare two bigrams lists and return the matching bigram Failed: This method is very close, but couldn't adapt it to bigrams -> Create new boolean fields based on specific terms appearing in a tokenized pandas dataframe Thanks for any help you can provide!

Answer: You could also try using numpy and nltk, which should be quite fast: import pandas as pd import numpy as np import nltk bigrams = ['data science', 'computer science', 'bachelors degree'] df = pd.DataFrame(data={'job_description': [['data', 'science', 'degree', 'expert'], ['computer', 'science', 'degree', 'masters'], ['bachelors', 'degree', 'computer', 'vision'], ['data', 'processing', 'science']]}) def find_bigrams(data): output = np.zeros((data.shape[0], len(bigrams)), dtype=bool) for i, d in enumerate(data): possible_bigrams = [' '.join(x) for x in list(nltk.bigrams(d)) + list(nltk.bigrams(d[::-1]))] indices = np.where(np.isin(bigrams, list(set(bigrams).intersection(set(possible_bigrams))))) output[i, indices] = True return list(output.T) output = find_bigrams(df['job_description'].to_numpy()) df = df.assign(**dict(zip(bigrams, output)))  | | job_description | data science | computer science | bachelors degree | |---:|:----------------------------------------------|:---------------|:-------------------|:-------------------| | 0 | ['data', 'science', 'degree', 'expert'] | True | False | False | | 1 | ['computer', 'science', 'degree', 'masters'] | False | True | False | | 2 | ['bachelors', 'degree', 'computer', 'vision'] | False | False | True | | 3 | ['data', 'processing', 'science'] | False | False | False |
Question: I am working on a script to walk over a directory, and convert all the python2 files to python3. There is a utitliy (2to3.py) to acheive that. ( I am using python2.7 interpreter) I have the following code: import os import subprocess import pathlib APP_FOLDER = 'C:/Users/XXXX/Test/' for dirpath, dirnames, filenames in os.walk(APP_FOLDER): for inputFile in filenames: if pathlib.Path(inputFile).suffix == ".py": file_path = os.path.join(dirpath, inputFile) with open(file_path) as f: num_lines = len(f.readlines()) print dirpath print inputFile print "lines of code: ", num_lines print "converting to python 3" cmd ="py C:\Python27\Tools\Scripts\\2to3.py "+inputFile+" -w" p1=subprocess.Popen(cmd,shell=True) p1.wait() if p1.returncode ==0: print "Success" else: print "failure"  At the path mentioned, I have a subdirectory named "FolderA" and within that there is a simple python file having the syntax of python2 and a division operation. I receive an error as below: C:/Users/XXXX/Test/FolderA Sample.py lines of code: 7 converting to python 3 RefactoringTool: Skipping optional fixer: buffer RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: set_literal RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: Can't open Sample.py: [Errno 2] No such file or directory: 'Sample.py' RefactoringTool: No files need to be modified. RefactoringTool: There was 1 error: RefactoringTool: Can't open Sample.py: [Errno 2] No such file or directory: 'Sample.py' failure -------------------*************--------------------  What am I doing wrong here?

Answer: Try using, cmd ="py C:\Python27\Tools\Scripts\\2to3.py "+file_path+" -w"
Question: I have a TensorFlow Keras deep learning model in the form of an h5 file. How can I upload an image and return a NumPy array in FastAPI? import numpy as np import cv2 from fastapi import FastAPI, File, UploadFile import numpy as np from tensorflow.keras.models import load_model import tensorflow as tf model=load_model("complete_model.h5") app = FastAPI() def prepare(image): IMG_SIZE = 224 new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE)) return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3) @app.post("/") async def root(file: UploadFile = File(...)): global model content = await file.read() nparr = np.fromstring(content, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.float32) prediction = model.predict(prepare(img)) return prediction  When uploading the image using Swagger UI, I get the following error: line 137, in jsonable_encoder data = dict(obj) TypeError: 'numpy.float32' object is not iterable  Working code without FastAPI: import numpy as np import numpy as np from tensorflow.keras.models import load_model import tensorflow as tf import cv2 model=load_model("complete_model.h5") def prepare(image): IMG_SIZE = 224 new_array = cv2.resize(image, (IMG_SIZE, IMG_SIZE)) return new_array.reshape(-1, IMG_SIZE,IMG_SIZE,3) img = cv2.imread("./test.jpeg").astype(np.float32) prediction = model.predict(prepare(img)) print(prediction)  Result in the terminal: [[0.25442022 0.74557984]]  How can I get the same result while using FastAPI?

Answer: The error is thrown when returning the response (i.e., prediction in your case) from your endpoint. It looks like FastAPI is trying to convert the NumPy array into a dict, using the jsonable_encoder, which is used internally by FastAPI when returning a value from an endpoint, and which seems to call Python's vars() method, as shown in the error you provided here (have a look at the discussion here, as well as the documentation). Thus, what you could do is to convert the NumPy array into a Python list, then serialize it into a JSON string and return it: return json.dumps(prediction.tolist())  Note that instead of returning the JSON string in the way it is shown above, which would casue FastAPI to serialize it again behind the scenes, you might consider returning a custom Response directly, as demonstrated here, as well as here and here. On Swagger UI /docs, you should still be able to see the expected result. However, if you needed to convert it back to a NumPy array, you could parse the JSON string in Python, as shown below. arr = np.asarray(json.loads(resp.json())) # resp.json() if using Python requests  If you would like to return the NumPy array as raw bytes and display the image in the browser or download it, have a look at this answer.
Question: In Polars, how can one specify a single dtype for all columns in read_csv? According to the docs, the schema_overrides argument to read_csv can take either a mapping (dict) in the form of {'column_name': dtype}, or a list of dtypes, one for each column. However, it is not clear how to specify "I want all columns to be a single dtype". If you wanted all columns to be String for example and you knew the total number of columns, you could do: pl.read_csv('sample.csv', schema_overrides=[pl.String]*number_of_columns)  However, this doesn't work if you don't know the total number of columns. In Pandas, you could do something like: pd.read_csv('sample.csv', dtype=str)  But this doesn't work in Polars.

Answer: Reading all data in a csv to any other type than pl.String likely fails with a lot of null values. We can use expressions to declare how we want to deal with those null values. If you read a csv with infer_schema_length=0, polars does not know the schema and will read all columns as pl.String as that is a super type of all polars types. When read as String we can use expressions to cast all columns. (pl.read_csv("test.csv", infer_schema_length=0) .with_columns(pl.all().cast(pl.Int32, strict=False))  Update: infer_schema=False was added in 1.2.0 as a more user-friendly name for this feature. pl.read_csv("test.csv", infer_schema=False) # read all as pl.String
Question: I'm using Python 3.8.9 with IPython 8.0.1 on macOS. When I type anything whatsoever, it displays a predicted suggestion based on past commands. Cool. However, how do I actually accept that suggestion? I tried the obvious: tab, which does not accept the suggestion, but rather opens up a menu with different suggestions, while the original suggestion is still there (see screenshot). I also tried space, and return, but both of those act as if the suggestion was never made. How the heck do I actually use the ipython autosuggestion? Or is tab supposed to work and something is wrong with my ipython build or something? 

Answer: CTRL-E, CTRL-F, or Right Arrow Key https://ipython.readthedocs.io/en/8.13.2/config/shortcuts/index.html Alternatively, the End key, as suggested by Richard Berg (below).
Question: I have two different lists and I would like to know how I can get each element of one list print with each element of another list. I know I could use two for loops (each for one of the lists), however I want to use the zip() function because there's more that I will be doing in this for loop for which I will require parallel iteration. I therefore attempted the following but the output is as shown below. lasts = ['x', 'y', 'z'] firsts = ['a', 'b', 'c'] for last, first in zip(lasts, firsts): print (last, first, "\n")  Output: x a y b z c  Expected Output: x a x b x c y a y b y c z a z b z c 

Answer: I believe the function you are looking for is itertools.product: lasts = ['x', 'y', 'z'] firsts = ['a', 'b', 'c'] from itertools import product for last, first in product(lasts, firsts): print (last, first) x a x b x c y a y b y c z a z b z c  Another alternative, that also produces an iterator is to use a nested comprehension: iPairs=( (l,f) for l in lasts for f in firsts) for last, first in iPairs: print (last, first)  If you must use zip(), both inputs must be extended to the total length (product of lengths) with the first one repeating items and the second one repeating the list: iPairs = zip( (l for l in lasts for _ in firsts), (f for _ in lasts for f in firsts) )
Question: I have defined an empty data frame with df = pd.DataFrame(columns=['Name', 'Weight', 'Sample'])  and want to append rows in a for loop like this: for key in my_dict: ... row = {'Name':key, 'Weight':wg, 'Sample':sm} df = pd.concat(row, axis=1, ignore_index=True)  But I get this error cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid  If I use df = df.append(row, ignore_index=True), it works but it seems that append is deprecated. So, I want to use concat(). How can I fix that?

Answer: You can transform your dict in pandas DataFrame import pandas as pd df = pd.DataFrame(columns=['Name', 'Weight', 'Sample']) for key in my_dict: ... #transform your dic in DataFrame new_df = pd.DataFrame([row]) df = pd.concat([df, new_df], axis=0, ignore_index=True)