eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Calling Python static methods using the class name is more common, but can be a real eye sore for long class names. Sometimes I use self within the same class to call the static methods, because I find it looks cleaner. class ASomewhatLongButDescriptiveClassName: def __init__(self): # This works, but it's an eyesore ASomewhatLongButDescriptiveClassName.do_something_static() # This works too and looks cleaner. self.do_something_static() @staticmethod def do_something_static(): print('Static method called.')  My understanding is that calling a static method with self gets interpreted as ClassName.static_method(self), where self would be ignored by the static method. (EDIT: The above statement is only true for instance methods, not static methods) Are there any concrete reasons why I should not use self to call static methods within the same class?  FWIW This is a sister question to Difference between calling method with self and with class name?, which deals with non-static methods. 

Answer: You make a few statements that aren't entirely correct:  Calling Python static methods using the class name is more common  It's not more common, it's the only way to do so from outside the class. i.e.: class MyClass: @staticmethod def a_method(): pass MyClass.a_method()  In this example, self.a_method() would not work, as self would not refer to an instance of MyClass.  calling a static method with self is the same as ClassName.static_method(self), where self would be ignored by the static method  That's not actually the case, for example: class MyClass: @staticmethod def a_method(): pass def another_method(self): # this is fine self.a_method() # this causes an error, as .a_method expects no arguments MyClass.a_method(self)  self simply refers to the instance of the class that called an instance method (which has the self argument, which doesn't even have to be called self - it's just whatever the first parameter is called, self is the convention. You can call static methods on self, because self is an instance of the class that has the static method, and thus has the method. You can also call static methods on classes directly, because a static method doesn't require an object instance as a first argument - which is the point of the static method. You're fine using self.a_method() where you like, just keep in mind that self will refer to an object of the class the object was instanced as, not the specific class you mention. For example: class ClassA: @staticmethod def a_method(): print('a') def another_method(self): # prints whatever a_method for the class of self prints self.a_method() # always prints 'a', as a_method for ClassA prints 'a' ClassA.a_method() class ClassB(ClassA): @staticmethod def a_method(): print('b') a = ClassA() a.another_method() b = ClassB() b.another_method()  The output: a a b a  So, you see, there is a difference between calling from self. and from Class.
Question: I have two data frames df1 and df2 as shown below: df1 Date BillNo. Amount 10/08/2020 ABBCSQ1ZA 878 10/09/2020 AADC9C1Z5 11 10/12/2020 AC928Q1ZS 3998 10/14/2020 AC9268RE3 198 10/16/2020 AA171E1Z0 5490 10/19/2020 BU073C1ZW 3432 df2 Date BillNo. Amount 10/08/2020 ABBCSQ1ZA 876 10/11/2020 ATRC95REW 115 10/14/2020 AC9268RE3 212 10/16/2020 AA171E1Z0 5491 10/25/2020 BPO66W2LO 344  My final answer should be: final Date BillNo. Amount 10/08/2020 ABBCSQ1ZA 876 10/16/2020 AA171E1Z0 5491  How do I find common rows from both the data frame using Date BillNo. Amount when the difference in value range is between [-5,5]? I know how to find common rows by using: df_all = df1.merge(df2.drop_duplicates(), on=['Date', 'BillNo.', 'Amount'], how='outer', indicator=True)  However, this doesn't give the rows which are in range. Anyone who could help? Edit: We can see in df1: 10/14/2020,AC9268RE3,198 and df2: 10/14/2020,AC9268RE3,212 the difference is 14, hence this should not be included in common rows

Answer: We can merge, then perform a query to drop rows not within the range: (df1.merge(df2, on=['Date', 'BillNo.']) .query('abs(Amount_x - Amount_y) <= 5') .drop('Amount_x', axis=1)) Date BillNo. Amount_y 0 10/08/2020 ABBCSQ1ZA 876 1 10/16/2020 AA171E1Z0 5491  This works well as long as there is only one row that corresponds to a specific (Date, BillNo) combination in each frame.
Question: I have a problem while running python on linux, I have python3 already installed. When type python3 on the terminal i got: python 3.9.0  When I run any program I made with for example python I got this error bash: python: command not found  And this happen to every python program I try to install on my machine.

Answer: Place the below line in ~/.bashrc file: alias python=python3  After inserting run the below command: source ~/.bashrc  .bashrc is the configuration file for bash, a linux shell/command interpreter. An alias is a substitute for a (complete) command. It can be thought of as a shortcut. By adding the above line, an alias is created for python3 in the name of python. Now the programs using python instead of python3 should work fine without any error.
Question: In a previous project, I used the first version of the following two lines. Now that I found getcwd() I thought this would be the shorter alternative. print(os.path.dirname(__file__)) # D:/Personal_Software/my_project print(os.getcwd()) # D:\Personal_Software\my_project  I already read this post, but the thing I'm curious about is the use of "/" vs. "\". I'm using Windows 10 if that's relevant.

Answer: There is a difference, though you wouldn't be able to tell from a single script. __file__ is the full filename of a loaded module or script, so getting the parent directory of it with os.path.dirname(__file__) gets you the directory that script is in. Note: on Linux (and similar OSes), such a filename can be a symbolic link to the actual file which may reside somewhere else. You can use os.path.realpath() to resolve through any such links, if needed, although you can typically use the symlink equivalently. On Windows these are less common, but similarly, you can resolve symbolic links through realpath(). os.getcwd() gets you the current working directory. If you start a script from the directory the script is in (which is common), the working directory will be the same as the result from the call from os.path.dirname(__file__). But if you start the script from another directory (i.e. python d:\some\path\script.py), or if you change the working directory during the script (e.g. with os.chdir()), the current working directory has changed, but the directory part of the script filename has not. So, it depends on what you need:  Need the directory your script file is in? Use os.path.dirname(__file__) Need the directory your script is currently running in? use os.getcwd()  You'll see / in some results and \ in others. Sadly, MS Windows uses \ to separate parts of a path (e.g. C:\Program Files\App\), while pretty much all other operating systems use / (e.g. /home/user/script.py) Python will often convert those automatically, so you can use paths like C:/Program Files/App in Python on Windows as well, but it tends to be a good idea to be safe and use os.path.sep. Note: if you're on Python 3, you may be better off just using pathlib's Path instead of os.path. It automatically resolves symbolic links (although you can still resolve to the link if you prefer) and has other nice conveniences as well.
Question: import pandas as pd   Reproducible setup I have two dataframes: df=\ pd.DataFrame.from_dict({'A':['xy','yx','zy','zz'], 'B':[[1, 3],[4, 3, 5],[3],[2, 6]]}) df2=\ pd.DataFrame.from_dict({'B':[1,3,4,5,6], 'C':['pq','rs','pr','qs','sp']})  df looks like:  A B 0 xy [1, 3] 1 yx [4, 3, 5] 2 zy [3] 3 zz [2, 6]  df2 looks like:  B C 0 1 pq 1 3 rs 2 4 pr 3 5 qs 4 6 sp   Aim I would like to combine these two to form res: res=\ pd.DataFrame.from_dict({'A':['xy','yx','zy','zz'], 'C':['pq','pr','rs','sp']})  ie  A C 0 xy pq 1 yx pr 2 zy rs 3 zz sp  The row with xy in df has the lsit [1,3]. There is a row with value 1 in column B in df2. The C column has value pq in that row, so I combine xy with pq. Same for the next two rows. Last row: there is no value with 2 in column B in df2, so I go for the value 6 (the last row in df has the list [2,6]).  Question How can I achieve this without iterating through the dataframe?   A very similar post in Spanish SO, which inspired this post. 

Answer: You can explode "B" into separate rows, then merge on "B" and drop duplicates. Big thanks to Asish M. in the comments for pointing out a potential bug with the ordering. (df.explode('B') .merge(df2, on='B', how='left') .dropna(subset=['C']) .drop_duplicates('A')) A B C 0 xy 1 pq 2 yx 4 pr 5 zy 3 rs 7 zz 6 sp   Ideally, the following should have worked: df.explode('B').merge(df2).drop_duplicates('A')  However, pandas (as of writing, version 1.2dev) does not preserve the ordering of the left keys on a merge which is a bug, see GH18776. In the meantime, we can use the workaround of a left merge as shown above.
Question: Facing the above error when running the django app. What exactly needs to be changed though? The comment_body = models.TextField() aspect most probably is the culprit since it stores reddit comments which can be of varying lengths. When i do a git clone and run it on my local, strangely it works.  Models.py from django.db import models # Create your models here. class Subreddit(models.Model): # Field for storing the name of a subreddit subreddit_name = models.CharField(max_length=200, unique=True) # Field for storing the time the model object was last saved last_updated = models.DateTimeField(auto_now=True) class Submission(models.Model): subreddit = models.ForeignKey(Subreddit, on_delete=models.CASCADE) # The Reddit submission id of the object submission_id = models.CharField(max_length=200, unique=True) # Reddit Submission URL url = models.URLField(max_length=200) # Reddit Submission Title title = models.CharField(max_length=200) class SubmissionComment(models.Model): # Parent submission object submission = models.ForeignKey(Submission, on_delete=models.CASCADE) # Text of the comment comment_body = models.TextField() class Meme(models.Model): memeurl = models.URLField(max_length=200)  EDIT: New error post char changed to 300, and migrations run locally and on heroku. 

Answer: Given the error: value too long for type character varying(200) you should look for model fields that have a max_length of 200. Since you have multiple fields with a max_length set to 200, you need to determine which model and field are throwing the error. Check the stacktrace, run a debugger and/or insert some debugging print(instance.__dict__)s. Once you find the culprit, extend that field's max_length to something larger or turn it into a TextField.
Question: import pandas as pd training_data = pd.DataFrame() training_data['a'] = [401,401.2,410,420,425,426, 426.1] training_data['b'] = [1,1,2,2,2,3,3] training_data['condition'] = [True, False, True, True, True,False, False]  My training data: a b condition 401 1 True 401.2 1 False 410 2 True 420 2 True 425 2 True 426 3 False 426.1 3 False  Desired output: a b condition 401 2 True (1+1) 410 2 True 420 2 True 425 8 True (2+3+3)  All False conditions have been deleted and column 'b' has been added with the amended values. How can I get this desired output? I am aware of using .cumsum() with training_data.query('condition').groupby('grp').agg() 

Answer: Here we go with cumsum out = training_data.groupby(training_data['condition'].cumsum()).agg({'a':'first','b':'sum','condition':'first'}) Out[271]: a b condition condition 1 401.0 2 True 2 410.0 2 True 3 420.0 2 True 4 425.0 8 True
Question: I'm currently studying regular expressions and have come across an inquiry. So the title of the question is what I'm trying to find out. I thought since \s represents a white space, re.split(" ", string) and re.split("\s+", string) would give out same values, as shown next: >>> import re >>> a = re.split(" ", "Why is this wrong") >>> a ["Why", "is", "this", "wrong"]  >>> import re >>> a = re.split("\s+", "Why is this wrong") >>> a ["Why", "is", "this", "wrong"]  These two give out the same answers so I thought that they were the same thing. However, it turns out that these are different. In what case would it be different? And what am I missing here that is blinding me?

Answer: This only look similar based on your example. A split on ' ' (a single space) does exactly that - it splits on a single space. Consecutive spaces will lead to empty "matches" when you split. A split on '\s+' will also split on multiple occurences of those characters and it includes other whitespaces then "pure spaces": import re a = re.split(" ", "Why is this \t \t wrong") b = re.split("\s+", "Why is this \t \t wrong") print(a) print(b)  Output: # re.split(" ",data) ['Why', '', '', '', 'is', 'this', '', '\t', '\t', '', 'wrong'] # re.split("\s+",data) ['Why', 'is', 'this', 'wrong']  Documentation:  \s Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v]. (https://docs.python.org/3/howto/regex.html#matching-characters)
Question: Random.seed() Is less secure than secrets, but I can't find any documentation on using a seed with secrets? or is random.seed just as fine?

Answer: No, there isn't. secrets uses random's SystemRandom class, which reads from the operating system's random device, such as /dev/urandom on Linux. This OS randomness is based off hardware entropy, which is what gives it its security, and there is no way to seed it.
Question: I'm following the transformer's pretrained model xlm-roberta-large-xnli example from transformers import pipeline classifier = pipeline("zero-shot-classification", model="joeddav/xlm-roberta-large-xnli")  and I get the following error ValueError: Couldn't instantiate the backend tokenizer from one of: (1) a `tokenizers` library serialization file, (2) a slow tokenizer instance to convert or (3) an equivalent slow tokenizer class to instantiate and convert. You need to have sentencepiece installed to convert a slow tokenizer to a fast one.  I'm using Transformers version '4.1.1'

Answer: According to Transformers v4.0.0 release, sentencepiece was removed as a required dependency. This means that  "The tokenizers that depend on the SentencePiece library will not be available with a standard transformers installation"  including the XLMRobertaTokenizer. However, sentencepiece can be installed as an extra dependency pip install transformers[sentencepiece]  or pip install sentencepiece  if you have transformers already installed.
Question: I'm following this tutorial about dynamic programming and I'm struggling to implement memoization in the following problem: *Write a function called canSum(targetSum, numbers) that returns True only if the numbers in the array can sum to the target sum. All the numbers in the array are positive integers and you can use them more than once for the solution. Example: canSum(7, [2, 4]) -> False because you can't form 7 by adding 2 and 4. * My brute force solution was the following one: def canSum(targetSum, numbers): if targetSum == 0: return True if targetSum < 0: return False for n in numbers: remainder = targetSum - n if canSum(remainder, numbers): return True return False print(canSum(7, [2, 3])) # True print(canSum(7, [5, 3, 4, 7])) # True print(canSum(7, [2, 4])) # False print(canSum(8, [2, 3, 5])) # True  Works well, but it'd be faster if we memoized the solutions of the remainders (this is explained at minute 1:28:03 in the video). I did the following with Python, which is exactly what the instructor is doing, but it only returns True and I can't figure out why... def canSum(targetSum, numbers, memo={}): if targetSum in memo: return memo[targetSum] if targetSum == 0: return True if targetSum < 0: return False for n in numbers: remainder = targetSum - n if canSum(remainder, numbers, memo): memo[targetSum] = True return True memo[targetSum] = False return False print(canSum(7, [2, 3])) print(canSum(7, [5, 3, 4, 7])) print(canSum(7, [2, 4])) print(canSum(8, [2, 3, 5])) # All of them return True 

Answer: Thanks to the article shared by @Jared Smith I was able to figure it out. The problem is caused by how python handles default arguments. From the article:  In Python, when passing a mutable value as a default argument in a function, the default argument is mutated anytime that value is mutated.  My memo dictionary was being mutated every call. So I simply changed memo=None and added a check to see if it was the first call of the function: def canSum(targetSum, numbers, memo=None): if memo == None: memo = {} if targetSum in memo: return memo[targetSum] if targetSum == 0: return True if targetSum < 0: return False for n in numbers: remainder = targetSum - n if canSum(remainder, numbers, memo): memo[targetSum] = True return True memo[targetSum] = False return False print(canSum(7, [2, 3])) # True print(canSum(7, [5, 3, 4, 7])) # True print(canSum(7, [2, 4])) # False print(canSum(8, [2, 3, 5])) # True print(canSum(3000, [7, 14])) # False -> Works fast with large inputs!
Question: I have recently (i.e., yesterday) discovered matplotlib as a much better alternative to Matlab for plots. Unfortunately, my knowledge of python is close to zero. I would like to use \mathbb{} in the legend and/or axes (for example, to denote expected value or variance) and it seems that this requires the additional STIX fonts (see, e.g., here and here). However, I haven't been able to include these fonts in my code so far. In the following example, I would like to replace \mathrm{E} --> \mathbb{E} and \mathrm{V} --> \mathbb{V}. Is there a simple way to do it? import numpy as np import scipy.io from matplotlib import pyplot as plt plt.figure(figsize=[3.3, 3.3]) plt.rcParams.update({'font.size': 8, 'text.usetex': True}) plt.plot([1,2,3,4], [1,2,3,4], label=r'$\mathrm{E}[x]$') plt.plot([1,2,3,4], [1,4,9,16], label=r'$\mathrm{V}[x]$') plt.grid() plt.xlabel('x') plt.legend(loc='upper left') plt.savefig('filename.pdf', format='pdf') plt.show() 

Answer: \mathbb is provided by the LaTeX package amsfonts, so you have to load this package for the figure to compile properly. You can load packages using the text.latex.preamble setting, as follows: import numpy as np import scipy.io from matplotlib import pyplot as plt plt.figure(figsize=[3.3, 3.3]) plt.rcParams.update({ 'font.size': 8, 'text.usetex': True, 'text.latex.preamble': r'\usepackage{amsfonts}' }) plt.plot([1,2,3,4], [1,2,3,4], label=r'$\mathbb{E}[x]$') plt.plot([1,2,3,4], [1,4,9,16], label=r'$\mathbb{V}[x]$') plt.grid() plt.xlabel('x') plt.legend(loc='upper left') plt.savefig('filename.pdf', format='pdf') plt.show()   Alternatively, to use the STIX fonts you refer to in your question, you can use matplotlib's built in TeX parser, not using LaTeX at all for your text handling (test.usetex can be False). In this case, however, note that \mathbb{} produces italic text by default, and you need to combine it with \mathrm{} as \mathrm{\mathbb{}} to get upright text (as in the example you linked). A possible version of your code is then: import numpy as np import scipy.io from matplotlib import pyplot as plt plt.figure(figsize=[3.3, 3.3]) plt.rcParams.update({'font.size': 8}) plt.plot([1,2,3,4], [1,2,3,4], label=r'$\mathrm{\mathbb{E}}[x]$') plt.plot([1,2,3,4], [1,4,9,16], label=r'$\mathrm{\mathbb{V}}[x]$') plt.grid() plt.xlabel('x') plt.legend(loc='upper left') plt.savefig('filename.pdf', format='pdf') plt.show()  In this case, this is the result. (posted as a link to limit the height of this answer)
Question: I have successfully used Playwright in python to get elements from a page. I now ran into to challenge of getting elements from a document embedded within an iframe. As an example, I used the w3schools page explaining the <option> element, which displays the result in an iframe. I am trying to retrieve a handle for this <option> element from the iframe. The 'normal' way of getting the an element on the page with page.querySelector() fails to get an elementHandle, this just prints <class 'NoneType'>: with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch(headless=False) page = browser.newPage() page.goto('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_option') element = page.querySelector('select') print(type(element)) browser.close()  I tried explicitly getting a handle for the iframe first, but this yields the same result (<class 'NoneType'>): with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch(headless=False) page = browser.newPage() page.goto('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_option') iframe = page.querySelector('iframe') element = iframe.querySelector('select') print(type(element)) browser.close()  How can I get content from within the iframe?

Answer: Turns out I was close, but to get the iframe correctly, I needed to call the contentFrame() method.  Returns the content frame for element handles referencing iframe nodes, or null otherwise  Then, querySelector() will return the respective elementHandle just fine: with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch(headless=False) page = browser.newPage() page.goto('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_option') iframe = page.querySelector('iframe').contentFrame() element = iframe.querySelector('select') print(type(element)) print(element.innerHTML()) browser.close()  successfully prints <class 'playwright.sync_api.ElementHandle'> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option>  Note: if there are multiple iframes, you can just use an attribute when retrieving the handle. To get the iframe by its id in the above example, e.g. use iframe = page.querySelector('iframe[id=\"iframeResult\"]').contentFrame()
Question: I have a CSV file for example like this :     id name email physics chemistry maths     1 Sta sta@example.com 67 78 90   2 Danny dany@example.com 77 98 89   3 Elle elle@example.com 77 67 90     Now I want to output a new CSV file using pandas which has new columns too for example like this :     id name grade address physics chemistry attendance maths total     I want to create new columns in random places and I want to place the value as blank in the new columns. I have tried using : import pandas as pd df = pd.read_csv("sample.csv") final_df = df.loc[['id','name','grade','address','physics','chemistry','attendance','maths','total']]  When I did this I got an error : KeyError(f“None of [{key}] are in the [{axis_name}]”)  Any ideas or suggestions to arrange this.

Answer: If you want to add new columns you should try reindex with axis=1: import pandas as pd df = pd.read_csv("sample.csv") final_df = df.reindex(['id','name','grade','address','physics','chemistry','attendance','maths','total'], axis=1)