eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have a decorator which wraps functions and produces classes (for a better reason than in the following example). It all works, except that I'd like to be able to set the name of the class given by type(). For example, >>> class Animal: ... pass ... >>> def linnean_name(genus, species): ... def _linnean_name(fn): ... class _Animal(Animal): ... binomial_name = (genus, species) ... def converse(self): ... fn() ... _Animal.__name__ = fn.__name__.title() ... _Animal.__module__ = fn.__module__ ... return _Animal ... return _linnean_name ... >>> @linnean_name('Vombatus', 'ursinus') ... def Wombat(): ... print("Hello, I am a wombat.") ... >>> sheila = Wombat() >>> sheila.binomial_name ('Vombatus', 'ursinus') >>> sheila.converse() Hello, I am a wombat.  All well and good, but >>> type(sheila) <class '__main__.linnean_name.<locals>._linnean_name.<locals>._Animal'>  where I was hoping to see <class '__main__.Wombat'>  This was the reason for ... _Animal.__name__ = fn.__name__.title() ... _Animal.__module__ = fn.__module__  which appear not to do anything particularly useful. How can I do this?

Answer: You are looking for __new__. It allows you to specify a new class name and share a common base class for pre-defined behavior. You could also make MixIn classes by define more bases/base classes etc. class Animal: def talk(self): print(f"Hello, I am {self}") def __str__(self): return f'{self.family} {self. spec}' def __new__(cls, family, spec, *args, **kwargs): cls_name = family.title() + spec.title() bases = (cls,) typ = type(cls_name, bases, {}) obj = object.__new__(typ) #from here it's like __init__ #except you return the object instead of None #obj is equal to self in the __init__ here obj.family = family obj.spec = spec return obj pass sheila = Animal('Vombatus', 'ursinus') sheila.talk()  As mentioned in the comments, you actually don't have to use a class and __new__ you can just have a factory that uses type. However, in my opinion __new__ is more readable. class Animal: def talk(self): print(f"Hello, I am {self}") def __str__(self): return f'{self.family} {self. spec}' pass def factory(family, spec, *args, **kwargs): cls_name = family.title() + spec.title() bases = (Animal,) typ = type(cls_name, bases, {}) obj = typ(*args, **kwargs) obj.family = family obj.spec = spec return obj sheila = factory('Vombatus', 'ursinus') print(type(sheila)) sheila.talk()
Question: I have two Python functions f() and g() which share one argument: a. def f(a, b): return a + b def g(a, c): return a * c  Sometimes, g() is called standalone, in which case both arguments are necessary: g(a = 1, c = 3)  Sometimes, g() is called as an argument inside a f() call: f(a = 1, b = g(a = 1, c = 3))  Notice that that the a argument is redundant, because it always has to have the same value in f() and g(). I would like to avoid this repetition. g() should figure out if it is called inside a f() call. If so, and if the a argument is not explicitly provided, then it should use the same a argument specified in the f() call. In other words, I want the same result as above with this simpler call: f(a = 1, g(c = 3)) 

Answer: What you want is not possible, g cannot know how it is called and it is evaluated before f. If you can modify the functions, g could return a partial if a is missing, then f can evaluate it if b is a function: from functools import partial def f(a, b): if callable(b): b = b(a) return a + b def g(a=None, c=None): if a is None: return partial(g, c=c) return a * c f(a = 1, b = g(a = 1, c = 3)) # 4 f(a = 1, b = g(c = 3)) # 4
Question: I have a web scraping script where, due to many reasons, some codes "break" if not finding the information as expected. I am handling it with multiple "try/except" blocks.  asin = item.get('data-asin') title = item.find_all('span',{'class' : 'a-size-base-plus a-color-base a-text-normal'})[0].get_text() try: label = item.find_all('span',{'aria-label' : 'Escolha da Amazon'})[0].get('aria-label') except IndexError : label = None try: current_whole_price = item.find_all('span', {'class' : 'a-price'})[0].find('span', {'class' : 'a-price-whole'}).get_text().replace(',','').replace('.','') except: current_whole_price = '0' try : current_fraction_price = item.find_all('span', {'class' : 'a-price'})[0].find('span', {'class' : 'a-price-fraction'}).get_text() except : current_fraction_price = '0' current_price = float(current_whole_price+'.'+current_fraction_price) try : rating_info = item.find('div', {'class':'a-row a-size-small'}).get_text() rating = float(rating_info[:3].replace(',','.')) rating_count = int(re.findall(r'\d+', rating_info)[-1]) except : rating = None rating_count = None try: ad = True if (item.find_all('span', {'class' : 'a-color-secondary'})[0].get_text() == 'Patrocinado') else False except IndexError: ad = False _ = {'productId' : itemId, 'asin' : asin, 'opt_label' : label, #"ad": True if (item.find_all('span', {'class' : 'a-color-secondary'})[0].get_text() == 'Patrocinado') else False , "ad": ad, 'title' : title, 'current_price' : current_price, 'url':f'https://www.amazon.com.br/dp/{asin}', 'rating' : rating, 'rating_count' : rating_count, }  But, looking at my code, you can see that many of the "try/except" are similar. I wonder if I could use some kind of function where I just pass the "item", the "desired selector" and the "failsafe value" to return if it goes wrong. I intend to make my code simpler and smaller. I take any tips! Regards!

Answer: Yes, you can create a function to handle the repetitive try/except blocks, but you have to find a common way to get your fields, then it might be something like: def get_element(item, selector, attribute, failsafe_value=None): try: element = item.find(selector).get(attribute) return element if element else failsafe_value except (AttributeError, IndexError): return failsafe_value
Question: How can I create a highlight mask for an image? I have attempted various methods, however, I have not been able to attain the desired outcome. How to detect colored sections without any adverse effects on the pink colored section, or any other color such as yellow? Input  Current output  Desired output  import cv2 import numpy as np def detect_highlighted_text(image_path): image = cv2.imread(image_path) hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) lower_pink = np.array([150, 50, 50]) upper_pink = np.array([180, 255, 255]) lower_red = np.array([0, 50, 50]) upper_red = np.array([10, 255, 255]) pink_mask = cv2.inRange(hsv_image, lower_pink, upper_pink) red_mask = cv2.inRange(hsv_image, lower_red, upper_red) combined_mask = cv2.bitwise_or(pink_mask, red_mask) highlighted_text = cv2.bitwise_and(image, image, mask=combined_mask) cv2.imshow("Highlighted Text", highlighted_text) cv2.waitKey(0) cv2.destroyAllWindows() detect_highlighted_text("path/to/your/image.jpg")  I tried to use create mask using inRange function, also searched about using findCountors.

Answer: You are almost there. You need a couple of Morphological filters and a little bit of tuning in your HSV thresholds. To obtain the final mask, you could also apply a Flood-fill at one corner using white color. Something like this: # Imports: import cv2 # Set image path directoryPath = "D://opencvImages//" # Set image path: imagePath = directoryPath + "41OzV.jpg" # Load image: inputImage = cv2.imread(imagePath) # Convert the BGR pixel to HSV: hsvImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV) # Threshold on red: rangeThreshold = 5 lowerValues = np.array([0, 34, 153]) upperValues = np.array([179, 255, 255]) # Create HSV mask: redMask = cv2.inRange(hsvImage, lowerValues, upperValues)  This gives you the following binary mask:  It’s alright, but it would benefit from some morphology. Apply a Closing first, to close the gaps and then an Opening to get rid of the small blobs of noise: # Use a little bit of morphology to clean the mask: # Set kernel (structuring element) size: kernelSize = 3 # Set morph operation iterations: opIterations = 3 # Get the structuring element: morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize)) # Perform closing: redMask = cv2.morphologyEx(redMask, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101) # Perform opening: redMask = cv2.morphologyEx(redMask, cv2.MORPH_OPEN, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)  This will yield the following cleaned mask:  Now, let’s use this mask and ANDit with the original image: # Mask original image: outputImage = cv2.bitwise_and(inputImage, inputImage, mask=redMask)  Which produces the following image:  Almost there, now let’s flood-fill at (0,0) (top left corner) to propagate white throughout the black color: # Flood-fill at seed point: seedPoint = (0, 0) # Set new color and tolerance: newColor = (255, 255, 255) tolerance = (5, 5, 5) cv2.floodFill(outputImage, None, seedPoint, newColor, loDiff=tolerance, upDiff=tolerance)  This is the final result:
Question: Method Not Allowed (GET): /users/logout/ Method Not Allowed: /users/logout/ [10/Dec/2023 12:46:21] "GET /users/logout/ HTTP/1.1" 405 0  This is happening when I went to url http://127.0.0.1:8000/users/logout/ urls.py: from django.contrib.auth import views as auth_views urlpatterns = [ ...other urls... path('users/logout/', auth_views.LogoutView.as_view(), name='logout'), ]  I am expecting user to logout

Answer: Since django-5, you need to do this through a POST request, since it has side-effects. The fact that it worked with a GET request was (likely) a violation of the HTTP protocol: it made it possible for certain scripts to log out users, without the user wanting to. So a POST request also protects against cross-site request forgery (CSRF) [wiki]. So in the template, work with a mini-form: <form method="post" action="{% url 'logout' %}"> {% csrf_token %} <button type="submit">logout</button> </form>  Also note that Django's LogoutView [Django-doc] does not render a page: it only works with a POST request that logs out the user that is making the request, and redirect to the page provided by the ?next_page=… parameter, or if such parameter is absent with LOGOUT_REDIRECT_URL [Django-doc]. Visiting the page will thus not work and return a 405 Method Not Allowed. If you thus want to make a page to logout, you add an extra view: from django.views.generic import TemplateView urlpatterns = [ # …, path( 'do-logout/', TemplateView.as_view(template_name='my_template.html'), name='do-logout', ) ] where the my_template.html then thus contains such miniform. You can thus visit /do-logout/ (or use another path) to render a page with a miniform to log out.
Question: I have the following Python code which works fine using the Web Socket mode. When I type a slash command (/hello-socket-mode) on my Slash application then it very well invokes my method, handle_some_command():- import os from slack_bolt import App from slack_bolt.adapter.socket_mode import SocketModeHandler # Install the Slack app and get xoxb- token in advance app = App(token="<bot token>") # Add functionality here @app.command("/hello-socket-mode") def handle_some_command(ack, body, logger): ack() print('testing slash command') logger.info(body) if __name__ == "__main__": # Create an app-level token with connections:write scope handler = SocketModeHandler(app,"<app token>") handler.start()  Since I'm in the development phase of my Slack app, I want to add reload functionality to this backend code of the web socket such that it reloads automatically whenever there is a change in the code. I tried to add uvicorn here but with that, I stopped getting invocation to my backend method, handle_some_command()whenever I tried to enter the slash command in the my slack application. I created a separate Python script called run.py with following code:- from uvicorn import run if __name__ == "__main__": run("main:app", host="0.0.0.0", port=3000, reload=True, log_level="info")  And then executed, python run.py to run my app using uvicorn to reload whenever there is a code change. It is not working at all, I'm not getting any slash command invocation to my backend code now. I just need some way of reload and not necessarily uvicorn here. I would appreciate it if someone could please guide me on this to make it work.

Answer: Code like this will work; you may delete fastapi dependency if not needed when development finished import os from fastapi import FastAPI from slack_bolt.adapter.socket_mode import SocketModeHandler from slack_bolt.app import App # Set enviornment vars BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"] APP_TOKEN = os.environ["SLACK_APP_TOKEN"] SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"] # Load Slack Bolt app = App(token=BOT_TOKEN, signing_secret=SIGNING_SECRET) # Load FastAPI api = FastAPI() # Load Slack bolt handlers # Action().add_to(bolt) SocketModeHandler(app, APP_TOKEN).connect() @app.message("hello") def message_hello(message, say): say(f"Hey there <@{message['user']}>!") # Load FastAPI endpoints @api.get("/") async def root(): return {"OK"}  And call this code like this: bash -c 'uvicorn app.main:api --reload --host 0.0.0.0 --port 4000 --log-level warning'
Question: My drone pipeline suddenly started failing on Windows only (the Linux pipeline, executing the same tests, works fine). error:  assert hasattr(request, 'session'), "The session-based temporary "\ AssertionError: The session-based temporary message storage  requires session middleware to be installed, and come before the message middleware in the MIDDLEWARE list  Looking at RequestFactory, this does indeed not give a session, but somehow it works on Linux and it used to work on Windows too. What has changed, why? And how should I add a dummy session to the request? test.py class TestDynamicAlertSubscriptionAdminModule(TestCase): def setUp(self): request = RequestFactory().post(path="dummy") request.user = self.user request._messages = messages.storage.default_storage(request)  settings.py MIDDLEWARE = [ "django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.messages.middleware.MessageMiddleware", ] INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", ] 

Answer: Why is the session attribute not present on the request? You're using RequestFactory to create your request and directly passing it to the view, this means the request doesn't go through any of the middleware and since SessionMiddleware is responsible for setting up the session it doesn't work. This is described more in detail here. Why does this work on one environment and not the other? The messages can be stored in different backends depending on the MESSAGE_STORAGE setting. Given you have two different environments there's a possibility that this setting is configured differently for the environments where one is using the SessionStorage and another is using one of FallbackStorage or CookieStorage. The environment using SessionStorage will fail and error out while the other will succeed since they don't rely on the session.
Question: I'm trying to convert a Spark data frame in Python 3.10 into a dynamic frame using Glue's fromDF method from awsglue.dynamicframe import DynamicFrame DynamicFrame.fromDF(frame, glue_context, "node") But this throws an error on CloudWatch saying com.mongodb.spark.sql.connector.exceptions.MongoSparkException: Partitioning failed. Inspecting the logs further, I found that under the hood Glue seems to be using $collStats somewhere, which is not supported by AWS DocumentDB. Note that this function worked when the job was Glue 2.0, but updating it to 4.0 has started causing this issue. com.mongodb.MongoCommandException: Command failed with error 304: 'Aggregation stage not supported: '$collStats'' I haven't really tried fixing this issue, because this seems to be happening under the hood, and I don't have access to the source code of Glue 4.0. The only thing is that this did not fail in Glue 2.0.

Answer: What worked for me is setting the partitioner in the batch read configuration for mongo-spark to be set to SinglePartitionPartitioner. Seems like the default was updated to use SamplePartitioner in Glue 4.0 and the newer mongo-spark connector versions which was causing the problem. Refer the documentation
Question: I am trying to run sage in the Ubuntu 23.10 terminal and it throws this exact error: ImportError: libsingular-Singular-4.3.1.so: cannot open shared object file: No such file or directory I am not sure which package to install and (considering the new ubuntu 23 (https://askubuntu.com/questions/1465218/pip-error-on-ubuntu-externally-managed-environment-%C3%97-this-environment-is-extern) thing) how. if it matters, I installed sage from apt.

Answer: This is a known bug: A quick workaround is downgrading singular-related packages. Here is a one-line-solution: curl -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/libsingular4-dev-common_4.3.1-p3+ds-1_all.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/libsingular4-dev_4.3.1-p3+ds-1_amd64.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/libsingular4m3n0_4.3.1-p3+ds-1_amd64.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/singular-data_4.3.1-p3+ds-1_all.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/singular-modules_4.3.1-p3+ds-1_amd64.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/singular-ui_4.3.1-p3+ds-1_amd64.deb -O https://mirror.enzu.com/ubuntu/pool/universe/s/singular/singular_4.3.1-p3+ds-1_amd64.deb && sudo dpkg -i libsingular4-dev-common_4.3.1-p3+ds-1_all.deb libsingular4-dev_4.3.1-p3+ds-1_amd64.deb libsingular4m3n0_4.3.1-p3+ds-1_amd64.deb singular-data_4.3.1-p3+ds-1_all.deb singular-modules_4.3.1-p3+ds-1_amd64.deb singular-ui_4.3.1-p3+ds-1_amd64.deb singular_4.3.1-p3+ds-1_amd64.deb && sudo apt-mark hold libsingular4-dev-common libsingular4-dev libsingular4m3n0 singular-data singular-modules singular-ui singular && rm -f libsingular4-dev-common_4.3.1-p3+ds-1_all.deb libsingular4-dev_4.3.1-p3+ds-1_amd64.deb libsingular4m3n0_4.3.1-p3+ds-1_amd64.deb singular-data_4.3.1-p3+ds-1_all.deb singular-modules_4.3.1-p3+ds-1_amd64.deb singular-ui_4.3.1-p3+ds-1_amd64.deb singular_4.3.1-p3+ds-1_amd64.deb  Complete answer on git: https://github.com/sagemath/sage/issues/36688
Question: I have created a Python function (see below code) that when called, plots a grouped bar chart which is working ok. I have also attached an image of the created graph. Is there a way instead of printing 1,2,3,4,5,6,7,8,9,10 on the x-axis, 6 times for each indexed list inside data_list. And then separate each of the 10 grouped bars apart from each other so it is more obvious and easier to interpret? import matplotlib.pyplot as plt def plot_graph(list, title_str): x_plot = [] y_plot = [] legend_labels = ['a', 'b', 'c', 'd', 'e', 'f'] x_labels = [1,2,3,4,5,6,7,8,9,10] x_labels_text = ['red', 'blue', 'green', 'purple', 'olive', 'brown'] x_colors = ['tab:red', 'tab:blue', 'tab:green', 'tab:purple', 'tab:olive', 'tab:brown'] fig, ax = plt.subplots() ax.set_xlabel('\nFault Type', fontsize=15) ax.set_ylabel('Number of Errors (%)', fontsize=15) ax.set_title('Total Number of Errors (%)', fontsize=15) for i in range(len(list)): for j in range(len(list[i])): x_plot.append(x_labels[i]) y_plot.append(list[i][j]) ax.bar(range(len(x_plot)), y_plot, label=legend_labels, color=x_colors, width=0.5) ax.set_xticks(range(len(x_plot)), x_plot) ax.set_ylim(ymax=100) #ax.legend(['a', 'b', 'c', 'd', 'e', 'f']) patches, _ = ax.get_legend_handles_labels() labels = [*'abcdef'] ax.legend(*patches, labels, loc='best') fig.tight_layout() plt.setp(ax.get_xticklabels(), fontsize=10) plt.savefig("C:/CoolTermWin64Bit/CoolTermWin64Bit/uart_data/Gathered Data/Code Generated Data Files/" + title_str + ".pdf") data_list = [ [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60], [10, 20, 30, 40, 50, 60] ] plot_graph(data_list, "data grouped bar graph")  

Answer: Some notes before I answer your questions: Make sure your indentation is correct; consider using numpy for your arrays (faster and easier if you know what you're doing); don't use list as a variable name as it has a specific use in Python. Also, please see answer number 3 if you want a more complete answer for number 1.  Not an exactly elegant solution, but it works:   for i in range(len(data)): for j in range(len(data[i])): if j == len(x_labels) // 2: x_plot.append(i + 1) else: x_plot.append('') y_plot.append(data[i][j])   First of all, you need to fix your labels, there's something wrong with your code there. Here's how to add a legend:  categories = ['example1', 'example2',...'example6'] plt.legend(categories, title='Legend')   Instead of writing out how to do a whole group bar chart (which is what you're looking for), I'm going to link a guide. This should help solve problem number 1 as well: https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html
Question: I'm trying to interactively run pdb on a remote system I don't own (I'm not sure if that's relevant), and it's behaving a bit differently than I expect. Listing the source shows there is clearly a line 22, but when I try to set the breakpoint, it tells me the line is blank. Fiddling a bit, I can get it to break on line 23, which is the line AFTER the one I want to break on. See terminal output below. I'm sure it's something dumb, but I'm at a loss. EDIT: I just noticed something new.... I'm using conda, and if I use the base environment, I don't see this issue, but if I use an environment I created, it does. (ecg) [tmhedr3@hmrihpcp03 ecg_analysis]$ python -m pdb rnn.py > /home/tmhedr3/ecg_analysis/rnn.py(1)<module>() -> import os (Pdb) ll 0 import os 1 -> import glob 2 import numpy as np 3 import pandas as pd 4 # import tensorflow as tf 5 # import keras 6 # from keras import layers 7 8 ages = open('ages.csv', 'r') 9 lines = ages.readlines() 10 n = len(lines) 11 12 data = np.ndarray((n, 5000, 12)) 13 14 for i in range(n): 15 li = lines[i].strip().split(sep=',') 16 name = li[0] 17 age = li[1] 18 19 leadData = np.loadtxt('./data/csv/' + name, skiprows=1, delimiter=',', usecols=(0,1,2,3,4,5,6,7,8,9,10,11), dtype='int') 20 data[i] = leadData 21 22 print(data.shape) 23 (Pdb) b 22 *** Blank or comment (Pdb) b 23 Breakpoint 1 at /home/tmhedr3/ecg_analysis/rnn.py:23 (Pdb) c > /home/tmhedr3/ecg_analysis/rnn.py(23)<module>() -> print(data.shape) (Pdb) 

Answer: Here it is, you've been hit by this issue - https://github.com/python/cpython/issues/103319 - that is whyit only affects a few Python versions. All it took for me to find was to open the Lib/pdb.py on cpython's source tree, look for "ll", and inspect visually the code from there - the issue itself is described as comments in the source code. cPython's Lib/pdb.py ... class Pdb(...): ... # [~ line 1992] def _getsourcelines(self, obj): # GH-103319 # inspect.getsourcelines() returns lineno = 0 for # module-level frame which breaks our code print line number # This method should be replaced by inspect.getsourcelines(obj) # once this bug is fixed in inspect lines, lineno = inspect.getsourcelines(obj) lineno = max(1, lineno) return lines, lineno  Of course, changing the behavior of inspect.getsourcelines now to do as written would sure break a lot of code depending on its count-from-0 behavior. We are likely either stuck with this, unless someone adds an optional, defaulting to False, flag parameter for getsourcelines to start counting from 1.