eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: which lib is better to integrate with a new django project? i red the docs and still doesnt know how performatic or easier to integrate each one can be in prod environment. i used graphene before to integrate with some pipefy code that i did at work but im pretty new in graphql and dont know at this point what way i should go.  Strawberry docs - https://strawberry.rocks/docs Graphene - https://docs.graphene-python.org/en/latest/ 

Answer: I'm the maintainer of Strawberry, so there might be some bias in my answer 😊 Both Strawberry and Graphene are based on GraphQL-core which is the library that provides the GraphQL execution, so in terms of performance they are comparable. For Strawberry we have a performance dashboard here: https://speed.strawberry.rocks/ and you can see we've been working to make it as fast as we can, but GraphQL-core will always be the deciding factor for the speed[1] For Django, I personally don't tend to use the integrations from models as I think it is a bad practise, but both Graphene and Strawberry have integration in that sense. Graphene integration's probably more mature, but Strawberry's is getting better every day (the maintainer works on both strawberry-django and strawberry-django-plus, and he's doing amazing work). Graphene has also probably more extensions at the moment and maybe more guides online, though most might not be up-to-date any more. Strawberry is well maintained and makes releases quite often, and we are trying to not have big breaking changes, even if we are at version 0.x. Graphene has been unmaintained for a bit but luckily now there are more maintainers. I'd definitely encourage you to do a small prototype with both libraries and see which one resonates the most with you as they have different DX, with Strawberry leveraging Python Type Hints and Graphene having a syntax very similar to Django models. [1] I do have some ideas on how we can make the library faster, but I don't know when I'll be able to implement them :)
Question: I am trying to make a Bar plot in python so that the color of each bar is set based on the magnitude of that value. For example the list below is our numbers: List = [2,10,18,50, 100, ... 500]  In this situation 2 should have the lightest color and 500 should have the darkest color values. The picture below which is from excel shows what i mean:  I appreciate answers which helps me. Thank you. Update: import matplotlib.pyplot as plt fig, ax = plt.subplots() names= ['A', 'B', 'C', 'D'] counts = [30, 40, 55, 100] bar_colors = ['blue'] ax.bar(names, counts, color=bar_colors) ax.set_ylabel('Frequency') ax.set_title('Names') plt.show() 

Answer: Then you can use a Colormap (extra docs) from matplotlib In this example the bar_colors list is being creating with a scale between 0 and 100 but you can choose the scale as you want. import matplotlib.pyplot as plt import matplotlib.colors fig, ax = plt.subplots() names= ['A', 'B', 'C', 'D', 'E'] counts = [10, 30, 40, 55, 100] cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","orange", "green"]) bar_colors = [cmap(c/100) for c in counts] ax.bar(names, counts, color=bar_colors) ax.set_ylabel('Frequency') ax.set_title('Names') plt.show()  Please, notice you can instantiate the cmap with a list of colors as you desire. Also, be sure that when invoking cmap() you just can use a float between [0,1].
Question: Is there a way to index a dict using an enum? e.g. I have the following Enum and dict: class STATUS(Enum): ACTIVE = "active" d = { "active": 1 }  I'd like to add the appropriate logic to the class STATUS in order to get the following result: d[STATUS.ACTIVE] # returns 1  I understand that the type of STATUS.ACTIVE is not a string.. But is there a way around other than declaring the dict using STATUS.ACTIVE instead of "active"? Also: looking for a solution other than adding a class property or .value

Answer: @ElmovanKielmo solution will work, however you can also achieve your desired result by making the STATUS Enum derive from str as well from enum import Enum class STATUS(str, Enum): ACTIVE = "active" d = {"active": 1} print(d[STATUS.ACTIVE]) # prints 1  Please keep in mind, that when inheriting from str, or any other type, the resulting enum members are also that type.
Question: I am trying to add an image to the "whitespace" behind the various subplots of a matplotlib figure. Most discussions similar to this topic are to add images to the plots themselves, however I have not yet come across a means to change the background of the overall "canvas". The most similar function I have found is set_facecolor(), however this only allows a single color to be set as the background. fig, ax = plt.subplots(2,2) fig.patch.set_facecolor('xkcd:mint green') plt.show()   However, I am seeking a solution to import an image behind the plots, similar to this (manually made):  I have googled, searched SO, and looked through the matplotlib docs but I only get results for either plt.imshow(image) or set_facecolor() or similar.

Answer: You can use a dummy subplot, with the same size as the figure, and plot the background onto that subplot. import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np image = plt.imread('test.jpg') # make ticks white, for readability on colored background mpl.rcParams.update({'xtick.color': "white", 'ytick.color': "white", 'axes.labelcolor': "white"}) # create a figure with 4 subplots, with the same aspect ratio as the image width = 8 fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(width, width * image.shape[0] / image.shape[1])) for ax in np.ravel(axs): ax.patch.set_alpha(0.7) # make subplots semi-transparent background_ax = plt.axes([0, 0, 1, 1]) # create a dummy subplot for the background background_ax.set_zorder(-1) # set the background subplot behind the others background_ax.imshow(image, aspect='auto') # show the backgroud image # plot something onto the subplots t = np.linspace(0, 8 * np.pi, 2000) for i in range(2): for j in range(2): axs[i, j].plot(np.sin(t * (i + 2)), np.sin(t * (j + 4))) # plt.tight_layout() gives a warning, as the background ax won't be taken into account, # but normally the other subplots will be rearranged to nicely fill the figure plt.tight_layout() plt.show()
Question: I am on an Ubuntu machine, where Python 3.10 is automatically installed. To do a given task in a shared codebase I need to use Python 3.9 for some issues with new versions. I would like to have both of the Python installed on my machine and be able to use both and switching if I need to. So, I am trying to install old Python 3.9 with the command sudo apt-get install python3.9 and it succeeded in installation, but I can't find it anywhere even with which python3.9 and similar. Even the python interpreter option in VSCode does not show it. I think I am missing something. Can please someone help me? Thank you

Answer: Python should be installed under the /usr/bin/ folder. If it's not there, you might have not actually installed the package. Check out this guide for installing specific versions (Scroll down to the "Use Deadsnakes PPA to Install Python 3 on Ubuntu" section.) This will allow you to install specific version of python like python3.9
Question: I'm building a machine learning model and I need to populate a test dataframe with synthetic data. I have time series data that currently looks like this: Date DayOfWeek Unit 2022-10-01 7 A 2022-10-02 1 A 2022-10-03 2 A  What I need is to duplicate all the date rows, but I need a row for each 'Unit' (A,B,C,D) like this: Date DayOfWeek Unit 2022-10-01 7 A 2022-10-01 7 B 2022-10-01 7 C 2022-10-01 7 D 2022-10-02 1 A 2022-10-02 1 B 2022-10-02 1 C 2022-10-02 1 D 2022-10-03 2 A 2022-10-03 2 B 2022-10-03 2 C 2022-10-03 2 D  I found a previous answer that showed me how to repeat: df.reindex(df.index.repeat(4)).reset_index(drop=True)  What's the best way to take that, but instead of repeating everything, only repeating 'Date' and "DayOfWeek' but populating A through D on 'Unit'?

Answer: Suggest using itertools.product for the purpose: from itertools import product df = pd.DataFrame( data=product( pd.Series(pd.date_range('2022-10-01', '2022-10-03', freq='D')), "ABCD" ), columns=("Date", "Unit"), ) df["DayOfWeek"] = df["Date"].dt.dayofweek.add(1) # To Have Day of Week Starting with 1 df = df[["Date", "DayOfWeek", "Unit"]] print(df)  Output:  Date DayOfWeek Unit 0 2022-10-01 6 A 1 2022-10-01 6 B 2 2022-10-01 6 C 3 2022-10-01 6 D 4 2022-10-02 7 A 5 2022-10-02 7 B 6 2022-10-02 7 C 7 2022-10-02 7 D 8 2022-10-03 1 A 9 2022-10-03 1 B 10 2022-10-03 1 C 11 2022-10-03 1 D
Question: i need somebody's help, i have a column with words, i want to remove the duplicated words inside each cell what i want to get is something like this     words expected     car apple car good car apple good   good bad well good good bad well   car apple bus food car apple bus food     i've tried this but is not working from collections import OrderedDict df['expected'] = (df['words'].str.split().apply(lambda x: OrderedDict.fromkeys(x).keys()).str.join(' '))  I'll be very grateful if somebody can help me

Answer: If you don't need to retain the original order of the words, you can create an intermediate set which will remove duplicates. df["expected"] = df["words"].str.split().apply(set).str.join(" ")
Question: I have python strings like below "1234_4534_41247612_2462184_2131_GHI.xlsx" "1234_4534__sfhaksj_DHJKhd_hJD_41247612_2462184_2131_PQRST.GHI.xlsx" "12JSAF34_45aAF34__sfhaksj_DHJKhd_hJD_41247612_2f462184_2131_JKLMN.OPQ.xlsx" "1234_4534__sfhaksj_DHJKhd_hJD_41FA247612_2462184_2131_WXY.TUV.xlsx"  I would like to do the below a) extract characters that appear before and after 1st dot b) The keywords that I want are always found after the last _ symbol For ex: If you look at 2nd input string, I would like to get only PQRST.GHI as output. It is after last _ and before 1st . and we also get keyword after 1st . So, I tried the below for s in strings: after_part = (s.split('.')[1]) before_part = (s.split('.')[0]) before_part = qnd_part.split('_')[-1] expected_keyword = before_part + "." + after_part print(expected_keyword)  Though this works, this is definitely not nice and elegant way to write a regex. Is there any other better way to write this? I expect my output to be like as below. As you can see that we get keywords before and after 1st dot character GHI PQRST.GHI JKLMN.OPQ WXY.TUV 

Answer: Try (regex101): import re strings = [ "1234_4534_41247612_2462184_2131_ABCDEF.GHI.xlsx", "1234_4534__sfhaksj_DHJKhd_hJD_41247612_2462184_2131_PQRST.GHI.xlsx", "12JSAF34_45aAF34__sfhaksj_DHJKhd_hJD_41247612_2f462184_2131_JKLMN.OPQ.xlsx", "1234_4534__sfhaksj_DHJKhd_hJD_41FA247612_2462184_2131_WXY.TUV.xlsx", ] pat = re.compile(r"[^.]+_([^.]+\.[^.]+)") for s in strings: print(pat.search(s).group(1))  Prints: ABCDEF.GHI PQRST.GHI JKLMN.OPQ WXY.TUV
Question: I have a dataframe looks like this: import pandas as pd df = pd.DataFrame({'type_a': [1,0,0,0,0,1,0,0,0,1], 'type_b': [0,1,0,0,0,0,0,0,1,1], 'type_c': [0,0,1,1,1,1,0,0,0,0], 'type_d': [1,0,0,0,0,1,1,0,1,0], })  I wanna create a new column based on those 4 columns, it will return the column names whenever the value in those 4 columns equals to 1, if there are multiple columns equal to 1 at the same time then it will return the list of those columns names, otherwise it will be nan. The output dataframe will look like this: df = pd.DataFrame({'type_a': [1,0,0,0,0,1,0,0,0,1], 'type_b': [0,1,0,0,0,0,0,0,1,1], 'type_c': [0,0,1,1,1,1,0,0,0,0], 'type_d': [1,0,0,0,0,1,1,0,1,0], 'type':[['type_a','type_d'], 'type_b', 'type_c', 'type_c','type_c', ['type_a','type_c','type_d'], 'type_d', 'nan', ['type_b','type_d'],['type_a','type_b']] })  Any help will be really appreciated. Thanks!

Answer: This is also another way: import pandas as pd df['type'] = (pd.melt(df.reset_index(), id_vars='index') .query('value == 1') .groupby('index')['variable'] .apply(list)) type_a type_b type_c type_d type 0 1 0 0 1 [type_a, type_d] 1 0 1 0 0 [type_b] 2 0 0 1 0 [type_c] 3 0 0 1 0 [type_c] 4 0 0 1 0 [type_c] 5 1 0 1 1 [type_a, type_c, type_d] 6 0 0 0 1 [type_d] 7 0 0 0 0 NaN 8 0 1 0 1 [type_b, type_d] 9 1 1 0 0 [type_a, type_b]
Question: Is there a way to do a Timedelta for one month? Applying pd.Timedelta('1M') yields Timedelta('0 days 00:01:00'). Is there a code word for month?

Answer: Timedelta is for absolute offsets. A month "offset", by definition, might have different lengths depending on the value it's applied to. For those cases, you should use DateOffset: pandas.DateOffset(months=1) Functional example: import pandas as pd pd.Timestamp('2022-09-29 00:27:00') + pd.DateOffset(months=1) >>> Timestamp('2022-10-29 00:27:00')
Question: I used to unpack a long iterable expression like this: In python 3.8.7: >>> _, a, (*_), c = [1,2,3,4,5,6] >>> a 2 >>> c 6  In python 3.10.7: >>> _, a, (*_), c = [1,2,3,4,5,6] File "<stdin>", line 1 _, a, (*_), c = [1,2,3,4,5,6] ^^ SyntaxError: cannot use starred expression here  I'm not sure which version of python between 3.8.7 and 3.10.7 introduced this backwards breaking behavior. What's the justification for this?

Answer: There's an official discussion here. The most relevant quote I can find is:   Also the current behavior allows (*x), y = 1 assignment. If (*x) is to be totally disallowed, (*x), y = 1 should also be rejected.    I agree.  The final "I agree" is from Guido van Rossum. The rationale for rejecting (*x) was:  Honestly this seems like a bug in 3.8 to me (if it indeed behaves like this): >>> (*x), y (1, 2, 3)  Every time I mistakenly tried (*x) I really meant (*x,), so it's surprising that (*x), y would be interpreted as (*x, y) rather than flagging (*x) as an error. Please don't "fix" this even if it is a regression.  Also by Guido van Rossum. So it seems like (*x) was rejected because it looks too similar to unpacking into a singlet tuple.
Question: I was wondering if the Path.write_text(data) function from pathlib was atomic or not. If not, are there scenarios where we could end up with a file created in the filesystem but not containing the intended content? To be more specific, as the comment from @ShadowRanger suggested what I care about is to know if the file contains either the original data or the new data, but never something in between. Which is actually less as full atomicity.

Answer: On the specific case of the file containing the original data or the new data, and nothing in between: No, it does not do any tricks with opening a temp file in the same directory, populating it, and finishing with an atomic rename to replace the original file. The current implementation is guaranteed to be at least two unique operations:  Opening the file in write mode (which implicitly truncates it), and Writing out the provided data (which may take multiple system calls depending on the size of the data, OS API limitations, and interference by signals that might interrupt the write part-way and require the remainder to be written in a separate system call)  If nothing else, your code could die after step 1 and before step 2 (a badly timed Ctrl-C or power loss), and the original data would be gone, and no new data would be written. Old answer in terms of general atomicity: The question is kinda nonsensical on its face. It doesn't really matter if it's atomic; even if it was atomic, a nanosecond after the write occurs, some other process could open the file, truncate it, rewrite it, move it, etc. Heck, in between write_text opening the file and when it writes the data, some other process could swoop in and move/rename the newly opened file or delete it; the open handle write_text holds would still work when it writes a nanosecond later, but the data would never be seen in a file at the provided path (and might disappear the instant write_text closes it, if some other process swooped in and deleted it). Beyond that, it can't be atomic even while writing, in any portable sense. Two processes could have the same file open at once, and their writes can interleave (there are locks around the standard handles within a process to prevent this, but no such locks exist to coordinate with an arbitrary other process). Concurrent file I/O is hard; avoid it if at all possible.
Question: Is there a way in Pydatic to perform the full validation of my classes? And return all the possible errors? It seems that the standard behaviour blocks the validation at the first encountered error. As an example: from pydantic import BaseModel class Salary(BaseModel): gross: int net: int tax: int class Employee(BaseModel): name: str age: int salary: Salary salary = Salary(gross = "hello", net = 1000, tax = 10) employee= Employee(name = "Mattia", age = "hello", Salary=salary)  This code works fine and returns the validation error: pydantic.error_wrappers.ValidationError: 1 validation error for Salary gross value is not a valid integer (type=type_error.integer)  However, it is not catching the second validation error on the age field. In a real bugfix scenario, I would need to fix the first validation error, re-run everything again, and only at that point I would discover the second error on age. Is there a way to perform the full validation in pydantic? So validate everything and return ALL the validation errors? (so basically, do not stop at the first error met)

Answer: What you are describing is not Pydantic-specific behavior. This is how exceptions in Python work. As soon as one is raised (and is not caught somewhere up the stack), execution stops. Validation is triggered, when you attempt to initialize a Salary instance. Failed validation triggers the ValidationError. The Python interpreter doesn't even begin executing the line, where you want to initialize an Employee. Pydantic is actually way nicer in this regard than it could be. If you pass more than one invalid value in the same initialization, the ValidationError will contain info about about all of them. Like this examle: ... salary = Salary(gross="hello", net="foo", tax=10)  The error message will look like this: ValidationError: 2 validation errors for Salary gross value is not a valid integer (type=type_error.integer) net value is not a valid integer (type=type_error.integer)  What you'll have to do, if you want to postpone raising errors, is wrap the initialization in a try-block and upon catching an error, you could for example add it to a list to be processed later. In your example, this will not work because you want to use salary later on. In that case you could just initialize the Employee like this: ... employee = Employee( name="Mattia", age="hello", salary={"gross": "hello", "net": 100, "tax": 10} )  Which would also give you: ValidationError: 2 validation errors for Employee age value is not a valid integer (type=type_error.integer) salary -> gross value is not a valid integer (type=type_error.integer)  Hope this helps.
Question: Let's that we have geotiff of 0 and 1. import rasterio src = rasterio.open('myData.tif') data = src.read(1) data array([[0, 1, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0], [1, 0, 1, 1]])  I would like to have for each pixel 1 the sum of all adjacent pixels forming a cluster of ones and to have something like the following: array([[0, 2, 2, 0], [1, 0, 0, 1], [0, 0, 3, 0], [1, 0, 3, 3]]) 

Answer: You can use scipy.ndimage.label: from scipy.ndimage import label out = np.zeros_like(data) labels, N = label(data) for i in range(N): mask = labels==i+1 out[mask] = mask.sum()  output: array([[0, 2, 2, 0], [1, 0, 0, 1], [0, 0, 3, 0], [1, 0, 3, 3]])
Question: Is there any significant difference between the two Python keywords (...) and (pass) like in the examples def tempFunction(): pass  and def tempFunction(): ...  I should be aware of?

Answer: The ... is the shorthand for the Ellipsis global object in python. Similar to None and NotImplemented it can be used as a marker value to indicate the absence of something. For example: print(...) # Prints "Ellipsis"  In this case, it has no effect. You could put any constant there and it would do the same. This is valid: def function(): 1  Or def function(): 'this function does nothing'  Note both do nothing and return None. Since there is no return keyword the value won't be returned. pass explicitly does nothing, so it will have the same effect in this case too.
Question: Recently, I faced this issue with Poetry. All my commands using poetry were failing with the following error. RuntimeError The Poetry configuration is invalid: - Additional properties are not allowed ('group' was unexpected) 

Answer: I figured out the following issue.  The code owners had updated the poetry core requirement to requires = ["poetry-core>=1.2.0"] My current poetry version was 1.1.12  I did the following to fix my issue. # remove the current poetry installation rm -rf /Users/myusername/.poetry # upgrade poetry version pip install poetry -U  This should solve the problem. I verified the same by running my other poetry commands.  It should be noted that your current poetry configurations will be lost while doing this, and would need to be recreated and reinstalled.  # reinstall poetry for my project poetry install
Question: I am attempting to select a button within an iframe utilizing Python & Playwright... in Selenium I know you can do this by using indexes. Is this possible in playwright? I've been digging through the documentation and can't seem to figure it out. The button contained within the iframe that I am trying to select is: "button:has-text(\"Add New User\")"  The html code for the iframe I am using looks similar to this: <iframe src="https://www.urlthatcannotbereturnedinpagehtml.com/veryparticularparameters" width="100%" style="height: 590px;"></iframe>  Does anyone have any thoughts? I've attempted to find the URL by parsing the code for the webpage, but this portion can't be selected like that. I may just be at a loss with the documentation in Playwright, I've spent so much time in selenium that this seems like an entirely new language.

Answer: From what I understand, you have a page that has content within an iframe. You want to use Playwright to access elements within that frame. The official docs to handle frames: Official docs: https://playwright.dev/python/docs/frames You could then try something like this: // Locate element inside frame const iframeButton = await page.frameLocator('iFrame').locator("button:has-text(\"Add New User\")"); await iframeButton.click();  Notice that the example has the iFrame tag as locator, but you could and should use something more accurate like and id, name or url.