eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm new to python/jmespath and am trying to sort the below students by their final grade in 2024. I'm sure I can come up with something in python to do it but was just wondering if there is any jmespath magic I can use. I have seen sort_by and know how to search/sort by simple attributes, but haven't yet found anything that can do this. { "school": "lame", "students": [ { "id": 1, "name": "Joe", "grades": [ {"year": 2024, "final": 85}, {"year": 2023, "final": 73} ] }, { "id": 2, "name": "Pedro", "grades": [ {"year": 2024, "final": 92}, {"year": 2023, "final": 90} ] }, { "id": 3, "name": "Mary", "grades": [ {"year": 2024, "final": 88}, {"year": 2023, "final": 70} ] } ] } 

Answer: Assuming that you want to sort by the latest year (not specifically the value 2024), you could achieve it with this: jmespath.search("students | sort_by(@,&grades|max_by(@,&year).final)", data)  That would result in: [{'id': 1, 'name': 'Joe', 'grades': [{'year': 2024, 'final': 85}, {'year': 2023, 'final': 73}]}, {'id': 3, 'name': 'Mary', 'grades': [{'year': 2024, 'final': 88}, {'year': 2023, 'final': 70}]}, {'id': 2, 'name': 'Pedro', 'grades': [{'year': 2024, 'final': 92}, {'year': 2023, 'final': 90}]}]   If you actually wanted to sort by the year 2024 specifically, you could use this: jmespath.search("students | sort_by(@,&(grades[?year==`2024`].final)[0])", data)  But you would gen an error if you have any student record without any grade in 2024, as the index selector [0] would return null. In that case you have two options:  If you want to ignore students without 2024 grade, you could use this: jmespath.search("students[?grades[?year==`2024`]] | sort_by(@,&(grades[?year==`2024`].final)[0])", data)   If you want to sort them at the end or beginning: jmespath.search("students | sort_by(@,&(grades[?year==`2024`].final)[0]||`0`)", data) jmespath.search("students | sort_by(@,&(grades[?year==`2024`].final)[0]||`999`)", data)
Question: I am learning Object Oriented Programming for Python using Tkinter. I want to create a Class for each type of widgets (right now considering only Buttons). I am passing the Button class to my main App class. Below is the code I have so far: import tkinter as tk from tkinter import ttk class Buttons: def __init__(self): pass def buttons(self,text,width,cmd,col,row): self.text = text self.width = width self.cmd = cmd self.col = col self.row = row ttk.Button(text=self.text, width=self.width, command=self.cmd).grid(column=self.col,row=self.row) class App(tk.Tk, Buttons): def __init__(self): super().__init__() self.geometry('100x100') self.resizable(0,0) b1 = self.buttons('Btn1',10,lambda: changeButton(),0,0) b2 = self.buttons('Btn2',10,lambda: changeButton(),0,1) self.mainloop() # where should I place this function? How can I change button attributes? def changeButton(self): b1['text'] = 'Done' # If button 1 pressed b2['text'] = 'OKAY' # If button 2 pressed if __name__=="__main__": app = App()  I have two questions -  Where should I place the function 'changeButton()' and why? If I have 2 buttons, and when pressed, if they should change their texts to something else; is it possible to do that via a single function? Or should there be a 2 functions?  Thank you When I try the above code, I get the error: line 20, in <lambda> b1 = self.buttons('Btn1',10,lambda: changeButton(),0,0) ^^^^^^^^^^^^^^ TypeError: changeButton() missing 1 required positional argument: 'self' 

Answer: There are many ways to perform this task, I've added comments inside the code for convenience reasons. import tkinter as tk from tkinter import ttk class MyButton(ttk.Button): def __init__(self, text, width, cmd, col, row): super().__init__() self.textvar = tk.StringVar() # create a StringVar object self.textvar.set(text) # set the value of the StringVar object self.config(textvariable=self.textvar, width=width) self.grid(column=col,row=row) self.bind('<Button-1>', cmd) # binding the cmd function with the Mouse '<Button-1>' event to MyButton object class App(tk.Tk, MyButton): def __init__(self): super().__init__() self.geometry('100x100') self.resizable(0,0) MyButton('Btn1', 10, changeButton, 0, 0) MyButton('Btn2', 10, changeButton, 0, 1) self.mainloop() # where should I place this function? How can I change button attributes? def changeButton(event): """ event is the Mouse '<Button-1>' event object containing information about the event event.widget is the MyButton object that generated the event event.widget.textvar is the StringVar object of the MyButton object event.widget.textvar.get() returns the value of the StringVar object event.widget.textvar.set() sets the value of the StringVar object print(dir(event)) to see all the attributes of the event object """ text = ( 'Done' if event.widget.textvar.get() == 'Btn1' else 'OKAY' if event.widget.textvar.get() == 'Btn2' else 'Btn1' if event.widget.textvar.get() == 'Done' else 'Btn2' ) event.widget.textvar.set(text) if __name__=="__main__": app = App()
Question: Suppose I have the following sample code: from fastapi import APIRouter, FastAPI router_one = APIRouter(tags=["users"]) router_two = APIRouter(tags=["products"]) app = FastAPI(description="## Description for whole application") @router_one.get("/users") async def fn1(): pass @router_one.post("/users") async def fn2(): pass @router_two.get("/products") async def fn3(): pass @router_two.post("/products") async def fn4(): pass app.include_router(router_one) app.include_router(router_two)  It is rendered as below in swagger:  I know I can pass description argument for individual path operation functions but what I really need is to pass description argument to the APIRouter itself(I showed in the picture). I have some common information which is shared among the path operations below a certain tag like users. I noticed that there is no api available for that in FastAPI like this: router_one = APIRouter(tags=["users"], description=...) # or app.include_router(router_one, description=...)  Is there any other way to achieve that?

Answer: You can use the openapi_tags parameter of FastAPI class as below - from fastapi import APIRouter, FastAPI router_one = APIRouter(tags=["users"]) router_two = APIRouter(tags=["products"]) app = FastAPI( description="## Description for whole application", openapi_tags=[ {"name": "users", "description": "Operations with users"}, { "name": "products", "description": "Operations with products", "externalDocs": { "description": "Items external docs", "url": "https://fastapi.tiangolo.com/", }, }, ], ) @router_one.get("/users") async def fn1(): pass @router_one.post("/users") async def fn2(): pass @router_two.get("/products") async def fn3(): pass @router_two.post("/products") async def fn4(): pass app.include_router(router_one) app.include_router(router_two) Result
Question: I have a polars dataframe containing some rows with infinite values (np.inf and -np.inf) which I'd like to drop. I am aware ofdrop_nans and drop_null but I don't see a similar drop_inf. As I want to handle nan values separately, I cannot just replace inf with nan and then call drop_nans. What's an idiomatic way of dropping rows with infinite values?

Answer: you can use DataFrame.filter() and Expr.is_infinite() to filter out rows you don't need: import numpy as np import polars as pl df = pl.DataFrame({ "a": [1, 2, 3, np.inf], "b": [1, 2, 3, 4] }) df.filter(~pl.col('a').is_infinite()) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ f64 ┆ i64 │ ╞═════╪═════╡ │ 1.0 ┆ 1 │ │ 2.0 ┆ 2 │ │ 3.0 ┆ 3 │ └─────┴─────┘  Or, if you want to check all columns for infinite values, you can use .any_horizontal(): import numpy as np import polars as pl df = pl.DataFrame({ "a": [1, 2, 3, np.inf], "b": [1, 2, -np.inf, 4] }) df.filter( ~pl.any_horizontal(pl.all().is_infinite()) ) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ f64 ┆ f64 │ ╞═════╪═════╡ │ 1.0 ┆ 1.0 │ │ 2.0 ┆ 2.0 │ └─────┴─────┘  If you have non-numeric columns you can also use selectors: import numpy as np import polars as pl import polars.selectors as cs df = pl.DataFrame({ "a": [1, 2, 3, np.inf], "b": [1, 2, -np.inf, 4], "c": list("abcd") }) df.filter( ~pl.any_horizontal(cs.numeric().is_infinite()) ) ┌─────┬─────┬─────┐ │ a ┆ b ┆ c │ │ --- ┆ --- ┆ --- │ │ f64 ┆ f64 ┆ str │ ╞═════╪═════╪═════╡ │ 1.0 ┆ 1.0 ┆ a │ │ 2.0 ┆ 2.0 ┆ b │ └─────┴─────┴─────┘
Question: This is the continuation of my previous question. Now, with both channel_priority: flexible and channel_priority: strict, every conda update -n base --all flips between The following packages will be UPDATED: libarchive 3.7.2-h313118b_1 --> 3.7.4-haf234dc_0 zstd 1.5.5-h12be248_0 --> 1.5.6-h0ea2cb4_0 The following packages will be DOWNGRADED: zstandard 0.22.0-py311he5d195f_0 --> 0.19.0-py311ha68e1ae_0  and The following packages will be UPDATED: zstandard 0.19.0-py311ha68e1ae_0 --> 0.22.0-py311he5d195f_0 The following packages will be DOWNGRADED: libarchive 3.7.4-haf234dc_0 --> 3.7.2-h313118b_1 zstd 1.5.6-h0ea2cb4_0 --> 1.5.5-h12be248_0  and the corresponding conda list -n base --show-channel-urls are libarchive 3.7.2 h313118b_1 XXX zstandard 0.22.0 py311he5d195f_0 XXX zstd 1.5.5 h12be248_0 XXX  and libarchive 3.7.4 haf234dc_0 XXX zstandard 0.19.0 py311ha68e1ae_0 XXX zstd 1.5.6 h0ea2cb4_0 XXX  where XXX is the exact same (internal/corp) channel (which I do not cotrol). What am I doing wrong? How do I avoid this? Is this a bug in conda 24.5.0 or in the configuration of the XXX channel? PS. Reported

Answer: I cannot fully replicate the behavior that you are experiencing, so your mileage may vary with this solution. Conda has a config feature that allows you to pin package specs. This feature is still in beta according to the conda docs, so it may change in the future. First add the package pinning to your conda config: conda config --append pinned_packages "zstandard>=0.22.0"  Then install the newest version of zstandard and downgrade the other two packages. conda install zstandard==0.22.0 libarchive==3.7.2 zstd==1.5.5  Finally check the upgrade again: conda update -n base --all
Question: How can I log all request headers of an httpx request? I use log level DEBUG nad response headers log fine but there are no request headers in the log. If it matters I'm using async api of httpx lib.

Answer: Use custom event hooks: import httpx import logging logging.basicConfig(level=logging.DEBUG) async def log_request(request): logging.debug(f"Request headers: {request.headers}") async with httpx.AsyncClient(event_hooks={'request': [log_request]}) as client: response = await client.get('https://httpbin.org/get')
Question: I have the following code that plots ydata vs xdata which is supposed to be a circle. The plot has two subplots -- a lineplot with markers and a scatter plot. import matplotlib.pyplot as plt xdata = [-1.9987069285852805, -1.955030386765729, -1.955030386765729, -1.8259096357678795, -1.8259096357678795, -1.6169878720004491, -1.6169878720004491, -1.3373959790579202, -1.3373959790579202, -0.9993534642926399, -0.9993534642926399, -0.6176344077078071, -0.6176344077078071, -0.20892176376743077, -0.20892176376743077, 0.20892176376743032, 0.20892176376743032, 0.6176344077078065, 0.6176344077078065, 0.999353464292642, 0.999353464292642, 1.3373959790579217, 1.3373959790579217, 1.6169878720004487, 1.6169878720004487, 1.8259096357678786, 1.8259096357678786, 1.9550303867657255, 1.9550303867657255, 1.9987069285852832] ydata = (0.0, -0.038801795445724575, 0.038801795445724575, -0.07590776623879933, 0.07590776623879933, -0.10969620340136318, 0.10969620340136318, -0.13869039009450249, 0.13869039009450249, -0.16162314123018345, 0.16162314123018345, -0.1774921855402276, 0.1774921855402276, -0.18560396964016201, 0.18560396964016201, -0.185603969640162, 0.185603969640162, -0.17749218554022747, 0.17749218554022747, -0.16162314123018337, 0.16162314123018337, -0.13869039009450224, 0.13869039009450224, -0.10969620340136294, 0.10969620340136294, -0.0759077662387991, 0.0759077662387991, -0.038801795445725006, 0.038801795445725006, 0.0) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16,6)) fig.suptitle('Plot comparison: line vs scatter'+ 3*'\n') fig.subplots_adjust(wspace=1, hspace=3) fig.supxlabel('x') fig.supylabel('y') ax1.plot(xdata, ydata, 'o-', c='blue') ax1.set_title('Line-point plot', c='blue') for i in range(len(xdata)): ax2.scatter(xdata, ydata, c='orange') ax2.set_title('Scatter plot', c='orange') plt.savefig('line_vs_scatter_plot.png') plt.show()  Output:  From the output, it can be seen that the lineplot does not connect the dots (or points). Can we rearrange the x or y data in someway that fixes the issue? Or do something else?

Answer: If the xy coordinates have a consistent zig-zag pattern (odd/even), you could do : def unzigzag(data): data = list(data) # just in case return data[::2] + data[::-2] + [data[0]] ax1.plot(*map(unzigzag, [xdata, ydata]), "bo-")  NB: This plot is annotated with the physical position (starting from 1) of x/y in their lists.  If not, one option would be to use shapely : from shapely import MultiPoint from shapely.geometry.polygon import orient def unzigzag(x, y, ori=-1): # -1: clock-wise p = MultiPoint(list(zip(x, y))).convex_hull return list(orient(p, ori).boundary.coords) ax2.plot(*zip(*unzigzag(xdata, ydata)), "-bo") # the ori has no effect  Animation to highlight the orientation :
Question: I'm trying to deepen my understanding of how Python's dataclasses module works, particularly the role of type hints in defining class attributes. When I define a dataclass like this: from dataclasses import dataclass @dataclass class Person: name: str age: int  I notice that the type hints (str for name and int for age) are used by the dataclass decorator to generate various methods like __init__, __repr__, and __eq__ (I'm only interested in __init__). This automatic generation makes it very convenient, but I'm curious about the underlying mechanisms that enable this functionality. Specifically, I have a question: How does the dataclass decorator utilize type hints to generate these methods? I’ve copied and pasted the same code from the original dataclasses file into my working directory with the name custom_dataclass (for testing purposes), but my linter did not recognize the __init__ attributes.  In picture1, you can see that when I try to instantiate the Person class, I correctly visualize all attributes mentioned in the Person class. This class uses the original @dataclass decorator. However, when I try the same with PersonCustom using the @custom_dataclass decorator, no attributes appear at all, as shown in picture2.  If anyone has any answers or insights, I’d like to hear them. Thanks in advance for your help! I've also tried to see declarative_base method from sqlalchemy like from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User(Base): __tablename__ = "user" id = Column(Integer, primary_key=True) name = Column(String) age = Column(Integer) User()  but there no attributes appear either 

Answer:  [sqlalchemy] no attributes appear either  Well, there aren't any type hints involved. Rather we're assigning Column objects to those names, and each such object is storing a certain sql type (not a hint, not an annotation).  the type hints ... are used by the dataclass decorator  Yes. It accesses them via an API call. Details are in the documentation. It appears your custom decorator is not making use of dataclass_transform() and is not doing work similar to what the @dataclass decorator does. Which is unsurprising, as there's a lot of work involved, at least when addressing the general case, about 1.5 KLOC of source code.
Question: I am getting an import Error when trying to import imblearn.over_sampling for RandomOverSampler. I believe the issue is not with my code but with the libraries clashing, I'm not sure though. import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn.preprocessing import StandardScaler #actually scikit-learn from imblearn.over_sampling import RandomOverSampler  code that's using StandardScaler and RandomOverSampler: def scale_dataset(dataframe, oversample=False): X = dataframe[dataframe.columns[:-1]].values Y = dataframe[dataframe.columns[-1]].values scaler = StandardScaler() X = scaler.fit_transform(X) if oversample: ros = RandomOverSampler() X, Y = ros.fit_resample(X,Y) data = np.hstack((X, np.reshape(Y, (-1, 1)))) return data, X, Y print(len(train[train["class"]==1])) print(len(train[train["class"]==0])) train, X_train, Y_train = scale_dataset(train, True)  I tried fully importing sklearn, uninstalled and reinstalled scipi and sklearn (as scikit-learn), installing Tensorflow. I do have numpy, scipy, pandas and other dependent libraries installed.

Answer: This is a known issue (https://github.com/scikit-learn-contrib/imbalanced-learn/issues/1081#issuecomment-2127245933). You can either pip install git+https://github.com/scikit-learn-contrib/imbalanced-learn.git@master  or downgrade scikit-learn to 1.5.
Question: The below code uses Python lists to create a Plotly graph. The timestamps are Epoch milliseconds. How do I format the x-axis to readable datetime? I tried fig.layout['xaxis_tickformat'] = '%HH-%MM-%SS' but it didn't work. import plotly.graph_objects as go time_series = [1716693661000, 1716693662000, 1716693663000, 1716693664000] prices = [20, 45, 32, 19] fig = go.Figure() fig.add_trace(go.Scatter(x=time_series, y=prices, yaxis='y')) fig.update_layout(xaxis=dict(rangeslider=dict(visible=True),type="linear")) fig.layout['xaxis_tickformat'] = '%Y-%m-%d' fig.show() 

Answer: You can use fromtimestamp().strftime() from datetime: tdt = [datetime.fromtimestamp(ts / 1000).strftime('%Y-%m-%d %H:%M:%S') for ts in time_series]  Code  time_series = [1716693661000, 1716693662000, 1716693663000, 1716693664000] prices = [20, 45, 32, 19] tdt = [datetime.fromtimestamp(ts / 1000).strftime('%Y-%m-%d %H:%M:%S') for ts in time_series] fig = go.Figure() fig.add_trace(go.Scatter(x=tdt, y=prices, yaxis='y')) fig.update_layout( xaxis=dict( rangeslider=dict(visible=True), type="date", tickformat='%Y-%m-%d %H:%M:%S' ) ) fig.show()
Question: In the following code after 5 seconds sleep I expect the second part of date_updated to be changed, but only the millisecond part is changed. If I use database_url = 'sqlite:///:memory:' it works as expected. Why? class Base(MappedAsDataclass, DeclarativeBase): pass class Test(Base): __tablename__ = 'test' test_id: Mapped[int] = mapped_column(primary_key=True, init=False) name: Mapped[str] date_created: Mapped[datetime] = mapped_column( TIMESTAMP(timezone=True), insert_default=func.now(), init=False ) date_updated: Mapped[datetime] = mapped_column( TIMESTAMP(timezone=True), nullable=True, insert_default=None, onupdate=func.now(), init=False ) database_url: URL = URL.create( drivername='postgresql+psycopg', username='my_username', password='my_password', host='localhost', port=5432, database='my_db' ) engine = create_engine(database_url, echo=True) Base.metadata.drop_all(engine) Base.metadata.create_all(engine) with Session(engine) as session: test = Test(name='foo') session.add(test) session.commit() print(test) time.sleep(5) test.name = 'bar' session.commit() print(test.date_created.time()) # prints: 08:07:45.413737 print(test.date_updated.time()) # prints: 08:07:45.426483 

Answer: PostgreSQL's now() function returns the time of the start of the current transaction. In the code in the question, the second transaction is triggered by the print statement, since a new transaction must be opened to retrieve the values expired by the previous commit. Thus the difference in the observed timestamps is only a few milliseconds. To obtain the desired behaviour, any of these approaches should work:  Don't expire on commit (but carefully consider the consequences in production code): with orm.Session(engine, expire_on_commit=False) as session:   remove the print() call Call session.comit() again after the call to time.sleep() to start a new transaction  SQLite doesn't have a now() function; SQLAlchemy converts the function call to SELECT CURRENT_TIMESTAMP AS now  SQLite does not seem to restrict CURRENT_TIMESTAMP's value to the start of the current transaction: sqlite> begin; sqlite> select current_timestamp as 'now'; 2024-05-25 13:21:22 sqlite> select current_timestamp as 'now'; 2024-05-25 13:21:24 sqlite> select current_timestamp as 'now'; 2024-05-25 13:21:26 sqlite> rollback;
Question: f-string allows a very compact expression for printing str objects with spacing like so: a = "Hello" print(f'{a=:>20}') a= Hello  Is there a way to do the same for other objects like so: from pathlib import Path b=Path.cwd() print(f'{b=:>20}') Traceback (most recent call last): File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode exec(code, self.locals) File "<pyshell#11>", line 1, in <module> TypeError: unsupported format string passed to PosixPath.__format__  An alternative is: print(f'b={str(b):>20}') b= /home/user  But this method loses the object info that is shown when I do: print(f'{b=}') b=PosixPath('/home/user')  The desired outcome is to print b= PosixPath('/home/user')  Multiple printed statements should show something like: self.project= PosixPath('/home/user/project') self.project_a= PosixPath('/home/user/project/project') self.longer= PosixPath('/home/user/project/project/icons/project/pic.png') self.PIPFILELOCK= PosixPath('/home/user/project/Pipfile.lock') self.VIRTUALENV= PosixPath('/home/user/.local/share/virtualenvs/project-mKDFEK') 

Answer: You can use !s and !r: >>> b = Path("/var") >>> f"{b=!s:>20}" 'b= /var' >>> f"{b=!r:>20}" "b= PosixPath('/var')"  These are explicit conversion flags.  To tabulate a namespace of keys and values: >>> d {'project': PosixPath('/home/user/project'), 'project_a': PosixPath('/home/user/project/project'), 'longer': PosixPath('/home/user/project/project/icons/project/pic.png'), 'PIPFILELOCK': PosixPath('/home/user/project/Pipfile.lock'), 'VIRTUALENV': PosixPath('/home/user/.local/share/virtualenvs/project-mKDFEK')} >>> for k, v in d.items(): ... print(f"{k+'=':<14}{v!r}") ... project= PosixPath('/home/user/project') project_a= PosixPath('/home/user/project/project') longer= PosixPath('/home/user/project/project/icons/project/pic.png') PIPFILELOCK= PosixPath('/home/user/project/Pipfile.lock') VIRTUALENV= PosixPath('/home/user/.local/share/virtualenvs/project-mKDFEK')  In your example on printing the attributes of a class, just do: for k, v in self.__dict__.items(): print(f"self.{k + '=':<14}{v!r}")  Class attributes are already stored in a dictionary that can be accessed from self.__dict__ so you don't have to create d. Or using 3rd-party tabulate: >>> from tabulate import tabulate # pip install tabulate >>> d = {k+'=': repr(v) for k,v in d.items()} >>> print(tabulate(d.items(), tablefmt="plain")) project= PosixPath('/home/user/project') project_a= PosixPath('/home/user/project/project') longer= PosixPath('/home/user/project/project/icons/project/pic.png') PIPFILELOCK= PosixPath('/home/user/project/Pipfile.lock') VIRTUALENV= PosixPath('/home/user/.local/share/virtualenvs/project-mKDFEK')