eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to port some code from Python to C++. The Python code has a function foo that can take nested lists of ints, with variable list depth. For example, these are legitimate function calls to foo: foo([ [], [[]], [ [], [[]] ] ]) foo([1]) foo([ [1], [2, 3, [4, 5]], [ [6], [7, [8, 9], 10] ] ])  What should the method signature(s) be for a C++ method that can accept this kind of argument?

Answer: Here's a way that's pretty simple to define and use: #include <variant> #include <vector> struct VariableDepthList : std::variant<std::vector<VariableDepthList>, int> { private: using base = std::variant<std::vector<VariableDepthList>, int>; public: using base::base; VariableDepthList(std::initializer_list<VariableDepthList> v) : base(v) {} };  This is based on the fact that your type is either an int or a list of (the same type), adding an initializer_list constructor just for ease of use. You might want to add some helper function like is_vector()/is_value() too. Here is an example using it: #include <iostream> void foo(const VariableDepthList& v) { // Use like a variant. This is a print function if (auto* as_vector = std::get_if<std::vector<VariableDepthList>>(&v)) { if (as_vector->empty()) { std::cout << "[]"; return; } std::cout << "[ "; bool first = true; for (const auto& el : *as_vector) { if (!first) { std::cout << ", "; } first = false; foo(el); } std::cout << " ]"; } else { auto* as_int = std::get_if<int>(&v); std::cout << *as_int; } } int main() { foo({}); std::cout << '\n'; foo({ 1 }); std::cout << '\n'; foo({ {}, {{}}, { {}, {{}} } }); foo( {{1},{2,3,{4,5}},{{6},{7,{8,9},10}}} ); std::cout << '\n'; }
Question: Given the following dataframe: +------------+--------+ | Date | Amount | +------------+--------+ | 01/05/2019 | 15 | | 27/05/2019 | 20 | | 27/05/2019 | 15 | | 25/06/2019 | 10 | | 29/06/2019 | 25 | | 01/07/2019 | 50 | +------------+--------+  I need to get the rolling sum of all previous dates as follows: +------------+--------+ | Date | Amount | +------------+--------+ | 01/05/2019 | NaN | | 27/05/2019 | 15 | | 27/05/2019 | 15 | | 15/06/2019 | 35 | | 29/06/2019 | 10 | | 01/07/2019 | 35 | +------------+--------+  Using: df = pd.DataFrame( { 'Date': { 0: datetime.datetime(2019, 5, 1), 1: datetime.datetime(2019, 5, 27), 2: datetime.datetime(2019, 5, 27), 3: datetime.datetime(2019, 6, 15), 4: datetime.datetime(2019, 6, 29), 5: datetime.datetime(2019, 7, 1), }, 'Amount': {0: 15, 1: 20, 2: 15, 3: 10, 4: 25, 5: 50} } ) df.sort_values("Date", inplace=True) df_roll = df.rolling("28d", on="Date", closed="left").sum()  Gets me: +------------+--------+ | Date | Amount | +------------+--------+ | 01/05/2019 | NaN | | 27/05/2019 | 15 | | 27/05/2019 | 35 | <-- Should be 15 | 15/06/2019 | 35 | | 29/06/2019 | 10 | | 01/07/2019 | 35 | +------------+--------+  Which isn't quite correct. How would I get the sum of all previous dates rather than all previous rows?

Answer: You can do df['new'] = df.Date.map(df.groupby('Date').Amount.sum().rolling("28d", closed="left").sum()) df Date Amount new 0 2019-05-01 15 NaN 1 2019-05-27 20 15.0 2 2019-05-27 15 15.0 3 2019-06-15 10 35.0 4 2019-06-29 25 10.0 5 2019-07-01 50 35.0
Question: I have a for loop with several different deep learning models in it that generates this warning: WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001B0A8CC90D0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. WARNING:tensorflow:6 out of the last 6 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001B0A6C01940> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.  I have tried many different things in the for loop to stop it from popping up with no success. Is there simply a way to disable all warnings?

Answer: Use this: tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
Question: I'm trying to use a function with a parameter of namedTuple which has default values. I tried this. Is that somehow possible? from typing import Optional, NamedTuple Stats = NamedTuple("Stats", [("min", Optional[int]), ("max", Optional[int])]) def print(value1: Stats=None, value2: Stats=None): print("min: ", value1.min) print("max: ", value1.max) print() 

Answer: Rename your print() function, first you're using the built-in function name print which is bad style, secondly then you make a recursive call to print() inside print() (and I'm sure you meant to call the actual built-in print() inside function's body). Second, use collection.namedtuple class to implement actual type of tuple, like following: Also type annotations are not needed. Try it online! from collections import namedtuple StatsTup = namedtuple('Stats', ['min', 'max'], defaults = [3, 7]) def printf(value1 = StatsTup(), value2 = StatsTup(max = 10)): print("min1: ", value1.min) print("max1: ", value1.max) print("min2: ", value2.min) print("max2: ", value2.max) printf() printf(StatsTup(12, 14), StatsTup(16, 18))  Output: min1: 3 max1: 7 min2: 3 max2: 10 min1: 12 max1: 14 min2: 16 max2: 18  As you can see in code and output above I'm passing named tuple as default parameters to funcion. You can omit tuple's fields values if you provide defaults = [...] like I did. If you provide such defaults then you may provide no values to tuple like StatsTup() or some values like StatsTup(max = 123) or all values like StatsTup(min = 20, max = 35).  Solution above works only starting with Python 3.7, for older versions of Python do following: Try it online! from collections import namedtuple StatsTup = namedtuple('Stats', 'min max') StatsTup.__new__.__defaults__ = (3, 7) def printf(value1 = StatsTup(), value2 = StatsTup(max = 10)): print("min1: ", value1.min) print("max1: ", value1.max) print("min2: ", value2.min) print("max2: ", value2.max) printf() printf(StatsTup(12, 14), StatsTup(16, 18))
Question: I have the following f-string: f"Something{function(parameter)}"  I want to hardcode that parameter, which is a string: f"Something{function("foobar")}"  It gives me this error: SyntaxError: f-string: unmatched '('  How do I do this?

Answer: Because f-strings are recognized by the lexer, not the parser, you cannot nest quotes of the same type in a string. The lexer is just looking for the next ", regardless of its context. Use single quotes inside f"..." or double quotes inside f'...'. f"Something{function('foobar')}" f'Something{function("foobar")}'  Escaping quotes is not an option (for reasons that escape me at the moment), which means arbitrarily nested expressions are not an option. You only have 4 types of quotes to work with:  "..." '...' """...""" '''...'''
Question: I have a DataFrame and want to get divisions of pairs of columns like below: df = pd.DataFrame({ 'a1': np.random.randint(1, 1000, 1000), 'a2': np.random.randint(1, 1000, 1000), 'b1': np.random.randint(1, 1000, 1000), 'b2': np.random.randint(1, 1000, 1000), 'c1': np.random.randint(1, 1000, 1000), 'c2': np.random.randint(1, 1000, 1000), }) df['a'] = df['a2'] / df['a1'] df['b'] = df['b2'] / df['b1'] df['c'] = df['c2'] / df['c1']  I want to combine the last three lines into one like: df[['a', 'b', 'c']] = df[['a2', 'b2', 'c2']] / df[['a1', 'b1', 'c1']]  but I only get an error of ValueError: Columns must be same length as key. If I just simply print(df[['a2', 'b2', 'c2']] / df[['a1', 'b1', 'c1']]), I will only get a DataFrame with NaNs of shape (1000, 6). ==== Edit Now I know why my original one-line code doesn't work. Actually, the arithmetic operations of two DataFrames will be conducted between the columns with same labels, while those columns without same label in another DataFrame will generate NaNs. The result DataFrame will have the union() of the columns of the two operating DataFrames. That's why my original solution will give an ValueError and the div will generate NaNs. Following example will be helpful to explain: df1 = pd.DataFrame(data={'A':[1,2], 'B':[3,4], 'C':[5,6], 'D':[8,9]}) df2 = pd.DataFrame(data={'A':[11,12], 'B':[13,14], 'C':[15,16], 'D':[18,19]}) df1[['A', 'B']] / df2[['A', 'B']] Out[130]: A B 0 0.090909 0.230769 1 0.166667 0.285714 df1[['A', 'B']] / df2[['C', 'D']] Out[131]: A B C D 0 NaN NaN NaN NaN 1 NaN NaN NaN NaN df1[['A', 'B']] + df2[['A', 'C']] Out[132]: A B C 0 12 NaN NaN 1 14 NaN NaN 

Answer: You can use: df[['a', 'b', 'c']] = df[['a2', 'b2', 'c2']].values / df[['a1', 'b1', 'c1']].values  OUTPUT  a1 a2 b1 b2 c1 c2 a b c 0 864 214 551 761 174 111 0.247685 1.381125 0.637931 1 820 971 379 79 190 587 1.184146 0.208443 3.089474 2 305 154 519 378 567 186 0.504918 0.728324 0.328042 3 51 505 303 417 959 326 9.901961 1.376238 0.339937 4 84 531 625 899 248 905 6.321429 1.438400 3.649194 .. ... ... ... ... ... ... ... ... ... 995 302 695 790 777 896 975 2.301325 0.983544 1.088170 996 24 308 462 316 388 784 12.833333 0.683983 2.020619 997 135 286 359 752 282 283 2.118519 2.094708 1.003546 998 695 45 832 936 811 404 0.064748 1.125000 0.498150 999 809 454 971 335 366 884 0.561187 0.345005 2.415301
Question: Objects A, B ... have attribute namespace and I have a function that filters a list of such objects by a certain set of values of namespace attribute: T = TypeVar('T') def filter(seq: list[T], namespace_values: set[str]) -> list[T]: # Returns a smaller list containing only the items from # `seq` whose `namespace` are in `namespace_values` ...  This works well, but it allows passing an object of type X that does not have the attribute namespace without any check error. Then I created a protocol and changed the function in order to use the protocol:  class Namespaced(Protocol): namespace: str def filter(seq: list[Namespaced], namespace_values: set[str]) -> list[Namespaced]: # Returns a smaller list containing only the items from # `seq` whose `namespace` are in `namespace_values` ...  Now I get a check error if I pass a list of X (which is what I wanted), but I lost the generics:  list_of_a: list[A] = [a1, a2, a3] output = filter(list_of_a, ['ns1', 'ns2']) # output is list[Namespaced] instead of list[A]  How can I combine the generics and protocol so my function returns a list of type T and also checks that the seq's items implement Namespaced protocol? I tried the below approach but the T is lost.  def filter(seq: list[Namespaced[T]], namespace_values: set[str]) -> list[T]: # Returns a smaller list containing only the items from # `seq` whose `namespace` are in `namespace_values` ...  Cheers!

Answer: Use a bound type variable with the protocol as the bound. Consider the following module: (py39) Juans-MacBook-Pro:~ juan$ cat test.py  Which has: from typing import TypeVar, Protocol from dataclasses import dataclass class Namespaced(Protocol): namespace: str T = TypeVar("T", bound="Namespaced") @dataclass class Foo: namespace: str @dataclass class Bar: namespace: str id: int def frobnicate(namespaced: list[T]) -> list[T]: for x in namespaced: print(x.namespace) return namespaced result1 = frobnicate([Foo('foo')]) result2 = frobnicate([Bar('bar', 1)]) reveal_type(result1) reveal_type(result2)  Then mypy gives: (py39) Juans-MacBook-Pro:~ juan$ mypy --strict test.py test.py:27: note: Revealed type is "builtins.list[test.Foo*]" test.py:28: note: Revealed type is "builtins.list[test.Bar*]"
Question: I have 2 data frames df1 and df2. import pandas as pd df1 = pd.DataFrame({ 'id':['1','1','1','2','2','2', '3', '4','4', '5', '6', '7'], 'group':['A','A','B', 'A', 'A', 'C', 'A', 'A', 'B', 'B', 'A', 'C'] }) df2 = pd.DataFrame({ 'id':['1','2','3','4','5','6','7'] })  I want to add 3 columns to df2 named group_A, group_B, and group_C, where each counts the number of repetitions of each group in df1 according to the id column. so the result of df2 should be likes this: 

Answer: Use crosstab with DataFrame.join, type of both id has to by same, like here strings: print (pd.crosstab(df1['id'], df1['group']).add_prefix('group_')) group group_A group_B group_C id 1 2 1 0 2 2 0 1 3 1 0 0 4 1 1 0 5 0 1 0 6 1 0 0 7 0 0 1 df = df2.join(pd.crosstab(df1['id'], df1['group']).add_prefix('group_'), on='id') print (df) id group_A group_B group_C 0 1 2 1 0 1 2 2 0 1 2 3 1 0 0 3 4 1 1 0 4 5 0 1 0 5 6 1 0 0 6 7 0 0 1  Solution without join is possible, if same ids in both DataFrames: print (pd.crosstab(df1['id'], df1['group']).add_prefix('group_').reset_index().rename_axis(None, axis=1)) id group_A group_B group_C 0 1 2 1 0 1 2 2 0 1 2 3 1 0 0 3 4 1 1 0 4 5 0 1 0 5 6 1 0 0 6 7 0 0 1
Question: I've been trying to get tesseract OCR to extract some digits from a pre-cropped image and it's not working well at all even though the images are fairly clear. I've tried looking around for solutions but all the other questions I've seen on here involve a problem with cropping or skewed text. Here's an example of my code which tries to read the image and output to the command line.  #convert image to greyscale for OCR im_g = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #create threshold image to simplify things. im_t = cv2.threshold(im_g, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)[1] #define kernel size rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20,20)) #Apply dilation to threshold image im_d = cv2.dilate(im_t, rect_kernel, iterations = 1) #Find countours contours = cv2.findContours(im_t, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0] for cnt in contours: x,y,w,h = cv2.boundingRect(cnt) #crop im_c = im[y:y+h, x:x+w] speed = pytesseract.image_to_string(im_c) print(im_path +" : " + speed)  Here's an example of an image The output for it is: frame10008.jpg : VAeVAs}  I've gotten a tiny improvement in some images by adding the following config to the tesseract image to string function: config="--psm 7"  Without the new config, it would detect nothing for this image. Now it outputs frame100.jpg : | U |  Any ideas as to what I'm doing wrong? Is there a different approach I could be taking to solve this problem? I'm open to not using Tesseract at all.

Answer: I've found a decent workaround. First off I've made the image larger. More area for tesseract to work with helped it a lot. Second, to get rid of non-digit outputs, I've used the following config on the image to string function: config = "--psm 7 outputbase digits"  That line now looks like this: speed = pytesseract.image_to_string(im_c, config = "--psm 7 outputbase digits")  The data coming back is far from perfect but the success rate is high enough that I should be able to clean up the garbage data and interpolate where tesseract returns no digits.
Question: I have django 3.2 and an IntegerChoices class  class Type(models.IntegerChoices): GENERAL = 2, _("general address") DELIVERY = 4, _("delivery address") BILLING = 6, _("billing address")  I can get the Value name and label easily by doing Type.GENERAL , Type.GENERAL.name and Type.GENERAL.label. But how can I get these values if I only have the value, e.g. I want to get the Name DELIVERY from the value 4. Type[4].name ist not working as the list values does not correspond to the int values of the enum. Thanks Matt

Answer: IntegerChoices.choices returns you a list of tuples with all content. In your case you'll have something like: [(2, 'general address'), (4, 'delivery address'), (6, 'billing address')]  Thus it can be done this way: class Type(models.IntegerChoices): GENERAL = 2, _("general address") DELIVERY = 4, _("delivery address") BILLING = 6, _("billing address") def label_by_type_value(value): choices = [c[1] for c in Type.choices if c[0] == value] return choices[0] if len(choices) else None
Question: I am trying to find if a particular element (int/string type), exists in my list or not. But I am using assert to evaluate my condition, meaning if the assert condition states True (element is present inside the list), False for element not being there in the list. Here is what I am trying- def test(x): try: for i in x: assert i==210410 return True except AssertionError as msg: print('Error') x=[210410,'ABC',21228,'YMCA',31334,'KJHG']  The output results to Error, even if the element is in the list. Can you please help me to sort this issue out?

Answer: Try using this: assert 210410 in x
Question: I am trying to integrate my Medium profile on a streamlit app using the below code snippet (generate through https://medium-widget.pixelpoint.io/) import streamlit as st st.markdown(''' <div id="medium-widget"></div> <script src="https://medium-widget.pixelpoint.io/widget.js"></script> <script>MediumWidget.Init({renderTo: '#medium-widget', params: {"resource":"https://medium.com/@mehulgupta_7991","postsPerLine":3,"limit":9,"picture":"small","fields":["description","author","claps","publishAt"],"ratio":"landscape"}})</script>''')  But looks like the tag is getting ignored. Any idea where I am going wrong?

Answer: You can use Streamlit component html. Code: import streamlit as st import streamlit.components.v1 as components components.html(''' <div id="medium-widget"></div> <script src="https://medium-widget.pixelpoint.io/widget.js"></script> <script>MediumWidget.Init({renderTo: '#medium-widget', params: {"resource":"https://medium.com/@mehulgupta_7991","postsPerLine":3,"limit":9,"picture":"small","fields":["description","author","claps","publishAt"],"ratio":"landscape"}})</script>''')  Output:
Question: I want to add many annotations with an arrow to my line plot. However, I don't want to add them all manually (like I did in the code below). This will be a hell of a job and I would rather add the annotations directly from the column df['text'] (or a list from this column). import plotly.express as px import pandas as pd # assign data of lists. data = {'x': [0, 1, 2, 3, 4, 5, 6, 7, 8], 'y': [0, 1, 3, 2, 4, 3, 4, 6, 5], 'text':["","","Annotation1","","Annotation2","","","",""]} # Create DataFrame df = pd.DataFrame(data) fig = px.line(df, x='x', y='y', title='I want to add annotation with the tekst column in my dataframe (instead of manually)') fig.add_annotation(x=2, y=3, text="Annotation1 (added manual)", showarrow=True, arrowhead= 2) fig.add_annotation(x=4, y=4, text="Annotation2 (added manual)", showarrow=True, arrowhead= 2) fig.update_layout(showlegend=False) fig.show()  The expected outcome looks like this (but then I want to add the annotations all at once with a list or something like that):  Many thanks in advance for your help.

Answer: I figured it out myself. See below the answer: import plotly.express as px import pandas as pd # assign data of lists. data = {'x': [0, 1, 2, 3, 4, 5, 6, 7, 8], 'y': [0, 1, 3, 2, 4, 3, 4, 6, 5], 'text':["","","Annotation1","","Annotation2","","","",""]} # Create DataFrame df = pd.DataFrame(data) fig = px.line(df, x='x', y='y', title='I want to add annotation with the tekst column in my dataframe (instead of manually)') arrow_list=[] counter=0 for i in df['text'].tolist(): if i != "": arrow=dict(x=df['x'].values[counter],y=df['y'].values[counter],xref="x",yref="y",text=i,arrowhead = 2, arrowwidth=1.5, arrowcolor='rgb(255,51,0)',) arrow_list.append(arrow) counter+=1 else: counter+=1 fig.update_layout(annotations=arrow_list) fig.show()
Question: I have a list which can have one or more items: a_list = ["apple"]  But it can also be empty: a_list = []  In this case, which of List[str] and List[Optional[str]] is the appropriate type-hint for this variable and why?

Answer: List[str] includes all lists of strings, including the empty list. (From a typing perspective, an empty list of type List[str] is distinct from an empty list of type List[int]). Optional[str] is shorthand for Union[None, str], so List[Optional[str]] is the type of lists that can contain str values and Nones, not the type of list that may or may not have a str value.
Question: I have declared multiple orm classes in a.py: Base = declarative_base() class Message(Base): __tablename__ = "message" __table_args__ = {"schema": "stocktwits"} id = Column(BigInteger, primary_key=True) user_id = Column(BigInteger, nullable=False) body = Column(String, nullable=False) created_at = Column(DateTime, nullable=False) __table_args__ = ( Index("created_at_idx", created_at), ) ...  I am initializing the database in another file b.py: def init_db(self): engine = create_engine(db_url) meta.create_all(engine) session = sessionmaker(bind=engine)()  I have two questions about such structure: First, how can I refer to my orm objects declared in a.py when I call the method meta.create_all(engine) in b.py? Should I use Base.metadata.create_all(bind=engine)? In that case I have to figure out a way to share the Base object across files. Second, I have read posts about importing objects before calling the meta.create_all() , since python does not allowed wildcard import within a function, does it mean I have to import all my orm objects individually, like from a.py import a, b, c, d, ....

Answer:  Should I use Base.metadata.create_all(bind=engine)?  Yes - all model classes that inherit from Base are registered in its metadata, so calling Base.metadata.create_all(engine) will create all the associated tables.  In that case I have to figure out a way to share the Base object across files  from a import Base  in b.py should be sufficient. Obviously you should only define Base once.
Question: I just got a new PC and downloaded visual studio code. I'm trying to run the exact same plots as the code I had on my other PC (just plt.plot(losses)) but now matplotlib seems to have a dark background instead of white:  I found this and this that had opposite problems. To clarify, I'm asking how to change the matplotlib background plots to white (note that in my other machine I didn't have to hard code any matplotlib background information so I think it's a visual studio problem, but couldn't figure it out)

Answer: Difficult to be sure since I cannot reproduce your problem. Two things to try (both presume that you import matplotlib using import matplotlib.pyplot as plt):  if you use plt.figure, add facecolor='white' parameter. Or try to run fig.set_facecolor('white') (fig here is the variable that stored the figure which facecolor you are changing. If you don't have any, use plt.gcf().set_facecolor('white') once the figure is created; gcf() returns current figure, see this doc). Try to change plt.style.context as in this matplotlib example.