eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm a beginner in python and currently studying QR code detection and decoding. I'm having a hard time rotating the detected QR code to the right position. I already used minAreaRect() to rotate my QR code but it doesn't work. Is there any workaround or a right way to do this? thanks! ROI2 = cv2.imread('ROI.png') gray2 = cv2.cvtColor(ROI2, cv2.COLOR_BGR2GRAY) blur2 = cv2.GaussianBlur(gray2, (9, 9), 0) thresh2 = cv2.threshold(blur2, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Morph close # kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # close2 = cv2.morphologyEx(thresh2, cv2.MORPH_CLOSE, kernel2, iterations=10) # Find contours and filter for QR code cnts2 = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts2 = cnts2[0] if len(cnts2) == 2 else cnts2[1] c = sorted(cnts2, key=cv2.contourArea, reverse=True)[0] draw = cv2.cvtColor(thresh2, cv2.COLOR_GRAY2BGR) cv2.drawContours(draw, [c], 0, (0, 255, 0), 2) rotrect = cv2.minAreaRect(c) box = cv2.boxPoints(rotrect) box = numpy.int0(box) cv2.drawContours(draw, [box], 0, (0, 0, 255), 2) cv2.imshow('thresh', thresh2) cv2.imshow('ROI', ROI2) cv2.imshow('minarearect', draw)    

Answer: From my understanding, you're trying to deskew an image. To do this, we need to first compute the rotated bounding box angle then perform a linear transformation. The idea is to use cv2.minAreaRect + cv2.warpAffine. According to the documentation, cv2.minAreaRect returns (center(x, y), (width, height), angle of rotation) = cv2.minAreaRect(...)  The third parameter gives us the angle we need to deskew the image.  Input image -> Output result   Skew angle: -39.99416732788086  Code import cv2 import numpy as np # Load image, grayscale, Otsu's threshold image = cv2.imread('2.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = 255 - gray thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] # Compute rotated bounding box coords = np.column_stack(np.where(thresh > 0)) angle = cv2.minAreaRect(coords)[-1] if angle < -45: angle = -(90 + angle) else: angle = -angle print("Skew angle: ", angle) # Rotate image to deskew (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) cv2.imshow('rotated', rotated) cv2.waitKey()  Note: See Python OpenCV skew correction for another approach using the Projection Profile Method to correct skew.
Question: I have a dataframe that looks something like this:  col1 col2 col3 0 1 True abc 1 2 False def 2 3 True ghi  When I run df.mean(), it shows a warning: >>> df.mean() <stdin>:1: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError. Select only valid columns before calling the reduction. col1 2.000000 col2 0.666667 dtype: float64  How do I solve this warning?

Answer: Numeric functions such as mean, median, sem, skew, etc., only support dealing with numeric values. If you look at the data types of your columns... >>> df.dtypes col1 int64 col2 bool col3 object dtype: object  ...you can see that the dtype of col1 is int64, which mean can handle, because it's numeric. Likewise, the dtype of col2 is bool, which Python, pandas, and numpy essentially treat as ints, so mean treats col2 as if it only contains 1 (for True) and 0 for False. The dtype of col3, however, is object, the default dtype for strings, which is basically a generic type to encapsulate any type of data that pandas can't understand. Since it's not numeric, mean has no idea how to deal with it. (After all, how would you compute the mean of abc and def?) There are a few ways to solve this problem, but "ignoring it" isn't one of them, because, as the warning indicates, in a future version of pandas, this warning will become an error that will stop your code from running.  Use numeric_only=True. This will cause mean to skip columns that aren't numeric — col3 in this case: >>> df.mean(numeric_only=True) col1 2.000000 col2 0.666667 dtype: float64  (Notice how col3 is omitted).  Select only the columns you need to operate on: >>> df[['col1', 'col2']].mean() col1 2.000000 col2 0.666667 dtype: float64
Question: I would like to extract 10.00ML in following byte: b'\x0200S10.00ML\x03' So I've tried extracting the 10.00ML between 200S and \x03: result = re.search(b'200S(.*)x03', b'\x0200S10.00ML\x03')  which didn't work, no element was found: AttributeError: 'NoneType' object has no attribute 'group'  Using only strings I have a minimum working example: test_string = 'a3223b' result = re.search('a(.*)b', test_string) print(result.group(1)) 

Answer: You can use import re text = b'\x0200S10.00ML\x03' m = re.search(rb'\x0200S(.*?)\x03', text, re.S) if m: print( m.group(1).decode('utf-8') ) # => 10.00ML  Note that \x02 and \x03 are START OF HEADING and START OF TEXT control chars, so you cannot match them as literal text.
Question: It's not a huge issue, but as a matter of style, I was wondering about the best way to indicate optional function parameters... Before type hints, it was like this for parameter b: def my_func(a, b = None)  Before Python 3.10, using type hints: def my_func(a, b: Optional[str])  With Python 3.10's lovely pipe-type notation (see PEP 604): def my_func(a, b: str | None)  The latter seems the obvious choice of the three options, but I was wondering if this completely eliminates the need to specify the default None value, which would be: def my_func(a, b: str | None = None)  EDIT: Thanks to @deceze and @jonrsharpe for pointing out that def my_func(a, b: str | None) would still require you to pass a value to b: you would explicitly have to pass None if you wanted that. So, the most concise one that will work to ensure that b is optional (i.e. the caller does not have to pass a value at all) is: def my_func(a, b: str = None)  Stylistically, incorporating explicit typing, is def my_func(a, b: str | None = None), i.e. explicit optional typing plus default None value, ever an option?

Answer: Per PEP-484:  A past version of this PEP allowed type checkers to assume an optional type when the default value is None, as in this code: def handle_employee(e: Employee = None): ... This is no longer the recommended behavior. Type checkers should move towards requiring the optional type to be made explicit.  So the official answer is no. You should specify ... | None = None for optional arguments.
Question: I have a dataframe like as shown below  ID raw_val var_name constant s_value 1 388 Qty 0.36 -0.032 2 120 Qty 0.36 -0.007 3 34 Qty 0.36 0.16 4 45 Qty 0.36 0.31 1 110 F1 0.36 -0.232 2 1000 F1 0.36 -0.17 3 318 F1 0.36 0.26 4 419 F1 0.36 0.31  My objective is to a) Find the upper and lower limits (of raw_val) for each value of var_name for s_value >=0 b) Find the upper and lower limits (of raw_val) for each value of var_name for s_value <0 I tried the below df['sign'] = np.where[df['s_value']<0, 'neg', 'pos'] s = df.groupby(['var_name','sign'])['raw_val'].series df['buckets'] = pd.IntervalIndex.from_arrays(s)  Please note that my real data is big data and has more than 200 unique values for var_name column. The distribution of positive and negative values (s_value) may be uneven for each value of the var_name columns. In sample df, I have shown even distribution of pos and neg values but it may not be the case in real life. I expect my output to be like as below var_name sign low_limit upp_limit Qty neg 120 388 F1 neg 110 1000 Qty pos 34 45 Qty pos 318 419 

Answer: I think numpy.where with aggregate minimal and maximal values is way: df['sign'] = np.where(df['s_value']<0, 'neg', 'pos') df1 = (df.groupby(['var_name','sign'], sort=False, as_index=False) .agg(low_limit=('raw_val','min'), upp_limit=('raw_val','max'))) print (df1) var_name sign low_limit upp_limit 0 Qty neg 120 388 1 Qty pos 34 45 2 F1 neg 110 1000 3 F1 pos 318 419
Question: I'm trying to adjust the text size according to country size, so the text will be inside the boarders of the country. Here's my code: # imports import pandas as pd import plotly.express as px # uploading file df=pd.read_csv('regional-be-daily-latest.csv', header = 1) # creating figure fig = px.choropleth(df, locations='Code', color='Track Name') fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.add_scattergeo( locations = df['Code'], text = df['Track Name'], mode = 'text', ) fig.show()  The output visualization that my code gives me is:  The text label for the orange country is inside the boarders of the country but the text to label the blue country is bigger than the country itself. What I'm looking for would be to adjust the size so it will not exceed the boarders of the country. How can I do this?

Answer: You can set the font size using the update_layout function and specifying the font's size by passing the dictionary in the font parameter. import pandas as pd import plotly.express as px df=pd.read_csv('regional-be-daily-latest.csv', header = 1) fig = px.choropleth(df, locations='Code', color='Track Name') fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.add_scattergeo( locations = df['Code'], text = df['Track Name'], mode = 'text', ) fig.update_layout( font=dict( family="Courier New, monospace", size=18, # Set the font size here color="RebeccaPurple" ) ) fig.show()
Question: I'm Downloading a Playlist which has some hidden Videos so python gives me DownloadError, I want to Download the Whole Playlist at once. Is there a fix for that. I'm trying to see if I can make it ignore those hidden videos My Code: from yt_dlp import YoutubeDL url = 'https://www.youtube.com/playlist?list=PLzMXToX8KzqhKrURIhVTJMb0v-HeDM3gs' ydl_opts = {'format': 'mp4'} with YoutubeDL(ydl_opts) as ydl: ydl.download(url)  Error Given in Terminal: Enter your URL: https://youtube.com/playlist?list=PLzMXToX8KzqhKrURIhVTJMb0v-HeDM3gs [youtube:tab] PLzMXToX8KzqhKrURIhVTJMb0v-HeDM3gs: Downloading webpage WARNING: [youtube:tab] YouTube said: INFO - 8 unavailable videos are hidden [youtube:tab] PLzMXToX8KzqhKrURIhVTJMb0v-HeDM3gs: Downloading API JSON with unavailable videos WARNING: [youtube:tab] YouTube said: INFO - Unavailable videos will be hidden during playback [download] Downloading playlist: English Grammar [youtube:tab] playlist English Grammar: Downloading 52 videos [download] Downloading video 1 of 52 [youtube] JGXK_99nc5s: Downloading webpage [youtube] JGXK_99nc5s: Downloading android player API JSON ERROR: [youtube] JGXK_99nc5s: Private video. Sign in if you've been granted access to this video 

Answer: Based on my understanding of the documentation, I think this will do what you want - unfortunately I cannot test it at the moment, so let me know if it doesn't work: import yt_dlp ydl_opts = { 'ignoreerrors': True } url = 'https://www.youtube.com/playlist?list=PLzMXToX8KzqhKrURIhVTJMb0v-HeDM3gs' with yt_dlp.YoutubeDL(ydl_opts) as ydl: error_code = ydl.download(url)
Question: I'm making a test class for pytest, I want to set a class attribute a that will be used for several test methods. To do so, I used a fixture set_a, which is launched automatically autouse=True, and invoked only once for the class (scope='class'), because setting a is costly. Here is my code: import pytest import time class Test: @pytest.fixture(scope='class', autouse=True) def set_a(self): print('Setting a...') time.sleep(5) self.a = 1 def test_1(self): print('TEST 1') assert self.a == 1  But the test fails with the following error: ========================================================================= FAILURES ========================================================================== ________________________________________________________________________ Test.test_1 ________________________________________________________________________ self = <tests.test_file.Test object at 0x116d953a0> def test_1(self): print('TEST 1') > assert self.a == 1 E AttributeError: 'Test' object has no attribute 'a' tests/test_file.py:15: AttributeError ------------------------------------------------------------------- Captured stdout setup ------------------------------------------------------------------- Setting a... ------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------- TEST 1  It looks like a wasn't set even if set_a was invoked, like if a new instance of the class was created when the test is executed. It works well if I change the fixture scope to function, but I don't wan't to set a for each test. Any idea what's the problem here ?

Answer: You shouldn’t set the scope since you are already in the class. class Test: @pytest.fixture(autouse=True) def set_a(self): print("Setting a...") time.sleep(5) self.a = 1 def test_1(self): print("TEST 1") assert self.a == 1  This is how you should use the scope=class, meaning it will work for any class in your module: @pytest.fixture(scope="class", autouse=True) def a(request): print("Setting a...") time.sleep(5) request.cls.a = 1 class Test: def test_1(self): print("TEST 1") assert self.a == 1
Question: In VS Code, I'm writing python code. I was wondering if there is a key shortcut to run the file instead of pressing the run button in the right top corner of the screen constantly.

Answer: You can press Ctrl + F5 to run the file. If you want to debug the file, use F5 instead.
Question: With a nested list like: ex_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]  I need to be able to slice this list for: [[1, 2], [4, 5]]  I've been trying: list(ex_list[:2][:2])  but this isn't working. I'm obviously doing something very wrong but haven't been able to find a solution as using commas doesn't work either for some reason.

Answer: You should try using comprehension: Try: [i[:2] for i in ex_list[:2]]  Code: ex_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print([i[:2] for i in ex_list[:2]])  Output: [[1, 2], [4, 5]]
Question: images_per_class = 80 fixed_size = tuple((500, 500)) train_path = "dataset/train" train_labels = os.listdir(train_path) for training_name in train_labels: dir = os.path.join(train_path, training_name) current_label = training_name for x in range(1,images_per_class+1): # get the image file name file = dir + "/" + str(x) + ".jpg" # read the image and resize it to a fixed-size image = cv2.imread(file) image = cv2.resize(image, fixed_size)  when i run this code it appeare this error error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'  and this warning [ WARN:0@19.045] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('dataset/train\Apple/1.jpg'): can't open/read file: check file path/integrity i dont have probleme with installation of opencv because i use it before andwith another code it fonctionne any help please

Answer:  file = dir + "/" + str(x) + ".jpg"  try to replace this line with :  file = dir + "\" + str(x) + ".jpg"   the / not correct the correct is \
Question: When I have the following minimum reproducing code: start.py from __future__ import annotations import a  a.py from __future__ import annotations from typing import Text import b Foo = Text  b.py from __future__ import annotations import a FooType = a.Foo  I get the following error: soot@soot:~/code/soot/experimental/amol/typeddict-circular-import$ python3 start.py Traceback (most recent call last): File "start.py", line 3, in <module> import a File "/home/soot/code/soot/experimental/amol/typeddict-circular-import/a.py", line 5, in <module> import b File "/home/soot/code/soot/experimental/amol/typeddict-circular-import/b.py", line 5, in <module> FooType = a.Foo AttributeError: partially initialized module 'a' has no attribute 'Foo' (most likely due to a circular import)  I included __future__.annotations because most qa of this sort is resolved by simply including the future import at the top of the file. However, the annotations import does not improve the situation here because simply converting the types to text (as the annotations import does) doesn't actually resolve the import order dependency. More broadly, this seems like an issue whenever you want to create composite types from multiple (potentially circular) sources, e.g. CompositeType = Union[a.Foo, b.Bar, c.Baz]  What are the available options to resolve this issue? Is there any other way to 'lift' the type annotations so they are all evaluated after everything is imported?

Answer: In most cases using typing.TYPE_CHECKING should be enough to resolve circular import issues related to use in annotations. Note annotations future-import (details), alternatively you can enclose all names not available at runtime (imported under if TYPE_CHECKING) in quotes. # a.py from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from b import B class A: pass def foo(b: B) -> None: pass  # b.py from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from a import A class B: pass def bar(a: A) -> None: pass  # __main__.py from a import A from b import B  However, for exactly your MRE it won't work. If the circular dependency is introduced not only by type annotations (e.g. your type aliases), the resolving may become really tricky. If you don't need Foo available at runtime in your example, it can be declared in if TYPE_CHECKING: block too, mypy will interpret that properly. If it is for runtime too, then everything depends on exact code structure (in your MRE dropping import b is enough). Union type can be declared in separate file that imports a, b and c and creates Union. If you need this union in a, b or c, then things are a bit more complicated, probably some functionality needs to be extracted into separate file d that creates union and uses it (also the code will be a bit cleaner this way, because every file will contain only common functionality).
Question: I installed Python 3 on Mac and installed some packages as well. But then I see AWS lamda does not support Python 3 so I decided to downgrade. I removed Python3 folder in Applications and cleared the trash. But still I see a folder named 3 in /Library/Frameworks/Python.framework/Versions which is causing problems, such as this:  $ python3 -m pip install virtualenv Requirement already satisfied: virtualenv in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (20.14.1) Requirement already satisfied: platformdirs<3,>=2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from virtualenv) (2.5.2)  So my question is how do I completely uninstall python 3 from my Mac?

Answer: Removing the app does not completely uninstall that version of Python. You will need to remove the framework directories and their symbolic links. Deleting the frameworks sudo rm -rf /Library/Frameworks/Python.framework/Versions/[version number]  replacing [version number] with 3.10 in your case. Removing symbolic links To list the broken symbolic links. ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]'  And to remove these links: cd /usr/local/bin ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]' | awk '{print $9}' | tr -d @ | xargs rm  As always, please be wary of copying these commands. Please make sure the directories in the inputs are actual working directories before you execute anything. The general idea in the end is to remove the folders and symlinks, and you're good to go. Here is another response addressing this process: How to uninstall Python 2.7 on a Mac OS X 10.6.4?
Question: I am using black to format my python code. I observed the following behavior. I admit that it is a very specific case but it goes on my nerves. Let's suppose I have the following code: @pytest.mark.parametrize( "first", ["second"], )  black does not change that. But, if I remove the trailing comma after ["second"]: @pytest.mark.parametrize( "first", ["second"] )  black makes a nice reordering: @pytest.mark.parametrize("first", ["second"])  On the other hand, let us see the following: @pytest.mark.parametrize( "This is a very long line. Black will not be able to to a linebreak here.", ["second"], )  In this case, again, black does not change anything. So far, so good. Now, I remove the trailing comma after ["second"] again: @pytest.mark.parametrize( "This is a very long line and black will not be able to do a line break here.", ["second"] )  As the line is too long, black does not do a nice reordering into one line as before. But instead, black adds a trailing comma after the ["second"]: @pytest.mark.parametrize( "This is a very long line and black will not be able to do a line break here.", ["second"], )  I do not like that. When I am confronted with such lists in my code, I always tend to remove the trailing comma to enforce one line reordering. But then, in most of the cases, black adds it again ...

Answer: The black behavior is expected from my understanding, because one of the purpose is to limit the number of changes inside a git diff. If the line is too long, you already are in a multiline writing. And if you are in a multiline writing, then it should end with a comma. That way if you add elements in the tuple, list, … or arguments in method or function, it will not generate a diff for the line where you add the comma. If a contrario you are in a mono-line writing that is not too long, black will consider if you have added a trailing comma. If trailing comma, it will go to multi-line, if not it will encourage mono-line. a = [1, 2,] # goes to a = [ 1, 2, ] # and a = [1, 2] # remains: a = [1, 2]
Question: When I publish my azure cloud functions I get the message: Local python version '3.9.7' is different from the version expected for your deployed Function App. This may result in 'ModuleNotFound' errors in Azure Functions. Please create a Python Function App for version 3.9 or change the virtual environment on your local machine to match 'Python|3.8'. How can I change the version to 3.9?

Answer:  You can view and set the linuxFxVersion from the Azure CLI. With the az functionapp config set command, you can change the linuxFxVersion setting in the function app.  az functionapp config set --name <FUNCTION_APP> \ --resource-group <RESOURCE_GROUP> \ --linux-fx-version "PYTHON|3.9"  Please refer Changing Python version for more information.
Question: I was trying to make a quick bot for discord and I used this code: import discord from discord.ui import button, view from discord.ext import commands client = discord.Client() @client.event async def on_ready(): print('Autenticazione riuscita. {0.user} è online!'.format(client))  But this error pops up: Client.__init__() missing 1 required keyword-only argument: 'intents'  I tried providing an argument by putting something between the brackets, like this: import discord from discord.ui import button, view from discord.ext import commands client = discord.Client(0) @client.event async def on_ready(): print('Autenticazione riuscita. {0.user} è online!'.format(client))  But instead I get this error: Client.__init__() takes 1 positional argument but 2 were given  On another PC the exact same code, with exact same modules and same Python version works just fine. What am I missing?

Answer: You could use the default Intents unless you have a particular one to specify. client = discord.Client(intents=discord.Intents.default())  As the first error message says, it is a keyword-only argument, so you cannot write discord.Client(discord.Intents.default()) without intents=. See Intents for more details.