eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to write a program to time sailing races, and to simplify adding the boats to a race by selecting from a list. I have used a Treeview linked to a tk.StringVar() to filter the boats by typing a part of the person or boats name to filter the list of boats to select. This works very well. I am writing this program in Debian 12 Linux Plasma I wanted to put a Treeview on the second tab to show the list of boats selected as entries, but when I pack this Treeview the Notebook doesn't expand to fill the window. Uncommenting the following line produces this behaviour. #tree2.pack(fill='both', expand=True)  The following is the code import tkinter as tk from tkinter import ttk import csv # root window root = tk.Tk() root.geometry(str('1600x900')) # create a notebook notebook = ttk.Notebook(root) notebook.pack(expand=True) # create frames for tabs EntriesFrame = ttk.Frame(notebook, width=1600, height=880) # 20 seems to be the right amount RaceFrame = ttk.Frame(notebook, width=1600, height=880) # add frames to notebook as tabs notebook.add(EntriesFrame, text='Entries') notebook.add(RaceFrame, text='Race') # Set up a treeview in first tab (EntriesFrame) ColNames = ['SailNo', 'Boat', 'HelmName', 'CrewName', 'Class', 'Fleet', 'Yardstick'] tree = ttk.Treeview(EntriesFrame, columns=ColNames, show='headings') for ColName in ColNames: tree.heading(ColName, text=ColName) tree.pack(fill='both', expand=True) # a Treeview for the entries on the next tab. tree2 = ttk.Treeview(RaceFrame, columns=ColNames, show='headings') for ColName in ColNames: tree2.heading(ColName, text=ColName) #tree2.pack(fill='both', expand=True) root.mainloop()  Hope someone can help.

Answer: Without calling tree2.pack(...), the size of RaceFrame is still around 1600x880, so the frames inside the notebook will all have the size of the biggest frame, that is RaceFrame. However, when tree2.pack(...) is called, the size of RaceFrame will be shrink to the size of tree2. So the size of the notebook will also be shrink to the size that can hold the biggest frame inside it because no fill option is used in notebook.pack(...). Therefore adding fill='both' to notebook.pack(...) will keep the notebook to fill the size of the window. And those frames inside the notebook will also be expanded.
Question: Question: I'm looking for guidance on using @property on a Django model, particularly when the property returns an Enum value and needs to be exposed in a Django REST Framework (DRF) serializer. Here’s my setup: I’ve defined an Enum, AccountingType, to represent the possible accounting types: from enum import Enum class AccountingType(Enum): ASSET = "Asset" LIABILITY = "Liability" UNKNOWN = "Unknown"  On my Account model, I use a @property method to determine the accounting_type based on existing fields: # Account fields ... @property def accounting_type(self) -> AccountingType: """Return the accounting type for this account based on the account sub type.""" if self.account_sub_type in constants.LIABILITY_SUB_TYPES: return AccountingType.LIABILITY if self.account_sub_type in constants.ASSET_SUB_TYPES: return AccountingType.ASSET return AccountingType.UNKNOWN  In Django views, I can use this property directly without issues. For example: account = Account.objects.get(id=some_id) if account.accounting_type == AccountingType.LIABILITY: print("This account is a liability.")  Problem: When trying to expose accounting_type in DRF, using serializers.ReadOnlyField() does not include the property in the serialized output: class AccountDetailSerializer(serializers.ModelSerializer): accounting_type = serializers.ReadOnlyField() class Meta: model = Account fields = ['accounting_type', 'account_id', ...]  I found that switching to serializers.SerializerMethodField() resolves the issue, allowing me to return the Enum value as a string: class AccountDetailSerializer(serializers.ModelSerializer): accounting_type = serializers.SerializerMethodField() class Meta: model = Account fields = ['accounting_type', 'account_id', ...] def get_accounting_type(self, obj): return obj.accounting_type.value # Return the Enum value as a string  Questions:  Is there a reason serializers.ReadOnlyField() doesn’t work with @property when it returns an Enum? Does DRF handle @property fields differently based on the return type? Is SerializerMethodField the recommended approach when a property returns a complex type, like an Enum, that needs specific serialization? Are there best practices for exposing Enum values via model properties in DRF?  Any insights would be appreciated.

Answer:  Is there a reason serializers.ReadOnlyField() doesn’t work with @property when it returns an Enum?  An Enum can not be JSON serialized. Make the AccountingType json serializable by making it a subclass of str as well: class AccountingType(str, Enum): ASSET = 'Asset' LIABILITY = 'Liability' UNKNOWN = 'Unknown' then it is sufficient to work with a ReadOnlyField: class AccountDetailSerializer(serializers.ModelSerializer): accounting_type = serializers.ReadOnlyField() # … Alternatively, we can just obtain the .value from the AccountingType: class AccountDetailSerializer(serializers.ModelSerializer): accounting_type = serializers.ReadOnlyField(source='accountingtype.value') # …
Question: In Python v3.10, the following code generates a Pylance error stating (Expression of type "EllipsisType" cannot be assigned to parameter of type "int") from typing import Any from PySide6.QtGui import QStandardItem class A(QStandardItem): def data(self, role: int = ...) -> Any: return super().data(role) pass  In QtGui.pyi, The data method of QStandardItem is defined as follows def data(self, role: int = ...) -> Any: ...  What is the correct way to subclass by specifying the typing accurately

Answer: ... as the default value in a .pyi file does not mean a literal Ellipsis object. Rather, role: int = ... means that the parameter role is of type int and has a default value of that same type at runtime, but that value is omitted in the stub file. That said, you need to provide a default value of your own: class A(QStandardItem): def data(self, role: int = 42) -> Any: return super().data(role)  If you don't care about LSP, just throw it away entirely: class A(QStandardItem): def data(self, role: int) -> Any: return super().data(role)  Using None or a similar sentinel value is another choice: class A(QStandardItem): def data(self, role: int | None = None) -> Any: if role is None: return super().data() return super().data(role)  I don't know PySide6, so take this with a grain of salt.
Question: I have a dataframe like this:    C1 C2 C3 C4     1 foo asd 23   foo foo asd 43   3 foo asd 1   4 foo asd bar    I'm trying to filter (and discard) all rows that have strings in C1 or C4 columns, my final dataframe must be:    C1 C2 C3 C4     1 foo asd 23   3 foo asd 1    I'm trying to do this using "isNaN" but I'm not sure how I should use it. This is my code: df = pd.read_csv( path_file, sep=",", usecols=columns, skiprows=0, skipfooter=0, engine="python", encoding="utf-8", skipinitialspace=True, on_bad_lines='warn', names=columns) df_new = df[df["C1"].notna()] df_new_2 = df[df["C4"].notna()]  Any idea about how I can achieve this?

Answer: You can try something like this: df.loc[df[['C1', 'C4']].apply(pd.to_numeric, errors='coerce').dropna(how='any').index]  Output:  C1 C2 C3 C4 0 1 foo asd 23 2 3 foo asd 1  Use pd.to_numeric with errors='coerce' to give NaN that can be used to with dropna.
Question: I've been trying to create a scrollable frame in with Tkinter / ttk using the many answers on here and guides scattered around. The frame is to hold a table which can have rows added and deleted. One issue I'm having that I can't find elsewhere is that if there is space for more content within the frame, I can scroll the contents of the table to the bottom of the frame. When the frame is full, it behaves as expected. I can't convert to a gif at the moment, so I've added some screenshots to try and illustrate. Here is my scrollable frame class, it's pretty standard from examples on the web; class ScrollableFrame(ttk.Frame): def __init__(self, parent): super().__init__(parent) self.canvas = tk.Canvas(self) self.scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview) self.scrollableFrame = ttk.Frame(self.canvas) self.canvas.configure(yscrollcommand=self.scrollbar.set) self.scrollableFrame.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))) self.bind("<Enter>", self.bind_to_mousewheel) self.bind("<Leave>", self.unbind_from_mousewheel) self.scrollbar.pack(side='right', fill='y') self.canvas.pack(side='top', expand=0, fill='x') self.canvas.create_window((0, 0), window=self.scrollableFrame) def bind_to_mousewheel(self, event): self.canvas.bind_all("<MouseWheel>", self.on_mousewheel) def unbind_from_mousewheel(self, event): self.canvas.unbind_all("<MouseWheel>") def on_mousewheel(self, event): self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")  The table rows are added to the ScrollableFrame.scrollableFrame widget by the parent GUI, which I figured was necessary to trigger the <Configure> bound to the ScrollableFrame.canvas. Does anyone have any suggestions? No scroll required as frame not full  Table still scrolls  Scroll stops with row widgets at bottom of frame 

Answer: I found an answer thanks to the hint from @acw1668; I adjusted the <Configure> command to check the size of the scrollregion and compare it to the parent ScrollableFrame class window. If it is smaller, I change the scroll region to be the size of the ScrollableFrame. Here are the adjustments I made to my class, including changing the window anchor to sit at the top left of the frame; class ScrollableFrame(ttk.Frame): """ """ def __init__(self, parent): """ """ ... self.scrollableFrame.bind("<Configure>", self.update_scroll_region) ... self.canvas.create_window((0, 0), window=self.scrollableFrame, anchor='nw') def update_scroll_region(self, event): bbox = self.canvas.bbox('all') sfHeight = self.winfo_height() if (bbox[3] - bbox[1]) < sfHeight: newBbox = (bbox[0], 0, bbox[2], sfHeight) self.canvas.configure(scrollregion=newBbox) else: self.canvas.configure(scrollregion=self.canvas.bbox("all"))  As pointed out, it would be possible and more Pythonic to use bbox[-1] = max(bbox[-1], sfHeight), then call self.canvas.configure(scrollregion=bbox) however I found due to my canvas' placement I had to set the first Y coordinate to 0.
Question: Completely stumped! I'm using the console as my email backend. I end up with False in token_generator.check_token as a result "Invalid or expired token." is displayed in my homepage when I navigate to say "http://localhost:8000/user/verify-email/?token=cgegv3-ec1fe9eb2cebc34e240791d72fb10d7d&email=test16@example.com" Here's my code from django.contrib.auth.tokens import PasswordResetTokenGenerator class CustomPasswordResetTokenGenerator(PasswordResetTokenGenerator): pass # Define a single instance of the token generator token_generator = CustomPasswordResetTokenGenerator()  def verify_email(request): email = request.GET.get("email") token = request.GET.get("token") try: user = CustomUser.objects.get(email=email) except CustomUser.DoesNotExist: messages.error(request, "Invalid verification link.") return redirect("home") if token_generator.check_token(user, token): user.is_active = True user.save() messages.success(request, "Your email has been verified!") return redirect("sign_in") else: messages.error(request, "Invalid or expired token.") return redirect("home")  from django.core.mail import send_mail from django.urls import reverse from user_management.utils import token_generator def send_verification_email(user, request): token = token_generator.make_token(user) verification_url = request.build_absolute_uri( reverse("verify_email") + f"?token={token}&email={user.email}" ) send_mail( "Verify your email", f"Click the link to verify your email: {verification_url}", "no-reply@example.com", [user.email], fail_silently=False, ) 

Answer: The code you posted seems fine. The check_token method performs several checks and figuring out which exact one fails should lead you to a solution. You can add breakpoints or print statements in place where the Django package is installed or bring the code into your project. Since you're already subclassing PasswordResetTokenGenerator you can: from django.conf import settings from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.utils.crypto import constant_time_compare from django.utils.http import int_to_base36 class CustomPasswordResetTokenGenerator(PasswordResetTokenGenerator): def check_token(self, user, token): """ Check that a password reset token is correct for a given user. """ # Use breakpoints / print statements to figure out # which of the conditions fails here and where to go next if not (user and token): return False # Parse the token try: ts_b36, _ = token.split("-") except ValueError: return False try: ts = base36_to_int(ts_b36) except ValueError: return False # Check that the timestamp/uid has not been tampered with for secret in [self.secret, *self.secret_fallbacks]: if constant_time_compare( self._make_token_with_timestamp(user, ts, secret), token, ): break else: return False # Check the timestamp is within limit. if (self._num_seconds(self._now()) - ts) > settings.PASSWORD_RESET_TIMEOUT: return False return True # Define a single instance of the token generator token_generator = CustomPasswordResetTokenGenerator()
Question: I'm doing some datetime math in python with the pytz library (although I'm open to using other libraries if necessary). I have an iterator that needs to increase by one day for each iteration of the loop. The problem comes when transitioning from November 3rd to November 4th in the Eastern timezone, which crosses the daylight saving boundary (there are 25 hours between the start of November 3rd and the start of November 5th, instead of the usual 24). Whenever I add a "day" that crosses the boundary, I get a time that is 24 hours in the future, instead of the expected 25. This is what I've tried: import datetime import pytz ET = pytz.timezone("US/Eastern") first_day = ET.localize(datetime.datetime(2024, 11, 3)) next_day = first_day + datetime.timedelta(days=1) first_day.isoformat() # '2024-11-03T00:00:00-04:00' next_day.isoformat() # '2024-11-04T00:00:00-04:00' assert next_day == ET.localize(datetime.datetime(2024, 11, 4)) # This fails!! # I want next_day to be '2024-11-04T00:00:00-05:00' or '2024-11-04T01:00:00-04:00'  I also tried throwing a normalize() in there, but that didn't produce the right result either: ET.normalize(next_day).isoformat() # '2024-11-03T23:00:00-05:00'  (That's one hour earlier than my desired output) I suppose I could make a copy of my start_day that increments the day field, but then I'd have to be aware of month and year boundaries, which doesn't seem ideal to me.

Answer: It looks like you want "wall time", which is the same "wall clock" time the next day, regardless of daylight savings time transitions. I would use the built-in zoneinfo module. You may need to install the "1st party" tzdata module if using Windows to have up-to-date time zone information (pip install tzdata): import datetime as dt import zoneinfo as zi ET = zi.ZoneInfo('US/Eastern') first_day = dt.datetime(2024, 11, 3, tzinfo=ET) # time zone aware next_day = first_day + dt.timedelta(days=1) # "wall clock" math in time zone print(first_day) print(next_day) assert next_day == dt.datetime(2024, 11, 4, tzinfo=ET)  Output: 2024-11-03 00:00:00-04:00 2024-11-04 00:00:00-05:00  Note that if instead you want exactly 24 hours later regardless of DST transitions, do the time delta math in UTC: import datetime as dt import zoneinfo as zi ET = zi.ZoneInfo("US/Eastern") first_day = dt.datetime(2024, 11, 3, tzinfo=ET) # time zone aware # Convert to UTC, do math, convert back to desired time zone next_day = (first_day.astimezone(dt.UTC) + dt.timedelta(days=1)).astimezone(ET) print(first_day) print(next_day)  Output: 2024-11-03 00:00:00-04:00 2024-11-03 23:00:00-05:00
Question: (also posted on r/dagster) Dagster N00b here. I have a very specific use-case. My ETL executes the following steps:  Query a DB to get a list of CSV files Go to a filesystem and for each CSV file:   load it into DuckDB transform some columns to date transform some numeric codes to text categories export clean table to a .parquet file run a profile report for the clean data  The DuckDB tables are named just the same as the CSV files for convenience. 2a through 2e can be done in parallel FOR EACH CSV FILE. Within the context of a single CSV file, they need to run SERIALLY. My current code is: @op def get_csv_filenames(context) -> List[str]: @op(out=DynamicOut()) def generate_subtasks(context, csv_list:List[str]): for csv_filename in csv_list: yield DynamicOutput(csv_filename, mapping_key=csv_filename) def load_csv_into_duckdb(context, csv_filename) def transform_dates(context, csv_filename) def from_code_2_categories(context, csv_filename) def export_2_parqu 

Answer: If I understand correctly, you want depth-first processing, instead of breadth first? I think you might be able to trigger depth-first processing using a nested graph after the dynamic output step. You're also conceptually missing how to set dependencies between ops in Dagster. Something like this should work: @op def get_csv_filenames(context) -> List[str]: @op(out=DynamicOut()) def generate_subtasks(context, csv_list:List[str]): for csv_filename in csv_list: yield DynamicOutput(csv_filename, mapping_key=csv_filename) @op def load_csv_into_duckdb(context, csv_filename) ... return csv_filename @op def transform_dates(context, csv_filename) ... return csv_filename @op def from_code_2_categories(context, csv_filename) ... return csv_filename @op def export_2_parquet(context, csv_filename) ... return csv_filename @op def profile_dataset(context, csv_filename) ... return csv_filename @graph def process(context, csv_filename:str): profile_dataset(export_2_parquet(from_code_2_categories(transform_dates(load_csv_into_duckdb(csv_filename))))) @job def pipeline(): csv_filename_list = get_csv_filenames() generate_subtasks(csv_filename_list).map(process)
Question: Is it possible to read from string with pl.read_csv() ? Something like this, which would work : content = """c1, c2 A,1 B,3 C,2""" pl.read_csv(content)  I know of course about this : pl.DataFrame({"c1":["A", "B", "C"],"c2" :[1,3,2]})  But it is error-prone with long tables and you have to count numbers to know which value to modify. I also know about dictionaries but I have more than 2 columns in my real life example. Context: I used to fread() content with R data.table and it was very useful, especially when you want to convert a column with the help of a join, instead of complicated ifelse() statements Thanks !

Answer: pl.read_csv() accepts IO as source parameter.  source: str | Path | IO[str] | IO[bytes] | bytes  So you can use io.StringIO: from io import StringIO content = """ c1,c2 A,1 B,3 C,2 """ data = StringIO(content) pl.read_csv(data)  shape: (3, 2) ┌─────┬─────┐ │ c1 ┆ c2 │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════╪═════╡ │ A ┆ 1 │ │ B ┆ 3 │ │ C ┆ 2 │ └─────┴─────┘  As you can see above, you can also pass bytes as source parameter. You can use str.encode() method for that: content = """ c1,c2 A,1 B,3 C,2 """ pl.read_csv(content.encode())  shape: (3, 2) ┌─────┬─────┐ │ c1 ┆ c2 │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════╪═════╡ │ A ┆ 1 │ │ B ┆ 3 │ │ C ┆ 2 │ └─────┴─────┘
Question: I am doing a project that involves lattices. A point of coordinates (x0, y0) is chosen randomly and I need to color blue all the points that are in the circle of center (x0, y0) and radius R and red all the other points and then draw a circle around. The tricky part is that there is periodic boundary conditions, meaning that if my circle is near the left border then I need to draw the rest of it on the right side, the same goes for up and down. Here is my code that plots the lattice, I have managed to color the points depending on whether or not they are in the circle but I am yet to draw the circle. from matplotlib import pyplot as plt import numpy as np class lattice: def __init__(self, L): self.L = L self.positions = np.array([[[i, j] for i in range(L)] for j in range(L)]) def draw_lattice(self, filename): X = self.positions[:, :, 0].flatten() Y = self.positions[:, :, 1].flatten() plt.scatter(X, Y, s=10) plt.xticks([]) plt.yticks([]) plt.title("Lattice") plt.savefig(filename) def dist_centre(self): x0, y0 = np.random.randint(0, self.L), np.random.randint(0, self.L) self.c0 = (x0, y0) self.distance = np.zeros((self.L, self.L)) for i in range(self.L): for j in range(self.L): x = self.positions[i, j, 0] y = self.positions[i, j, 1] # Distance with periodic boundary conditions. Dx = -self.L/2 + ((x0-x)+self.L/2)%self.L Dy = -self.L/2 + ((y0-y)+self.L/2)%self.L dist = np.sqrt(Dx**2 + Dy**2) self.distance[i, j] = dist def draw_zone(self, filename, R): colormap = np.where(self.distance <= R, "blue", "red").flatten() X = self.positions[:, :, 0].flatten() Y = self.positions[:, :, 1].flatten() plt.clf() plt.scatter(X, Y, s=10, color=colormap) plt.xticks([]) plt.yticks([]) plt.title("Lattice") plt.savefig(filename) if __name__ == "__main__": L = 10 R = 3 filename = "test.pdf" latt = lattice(L) latt.draw_lattice(filename) latt.dist_centre() latt.draw_zone(filename, R)  The formula for the distance is modified because of the periodic boundary conditions. 

Answer: The comment from Tino_D gave me the answer. I imagined a bigged lattice, my lattice and 8 lattices surrounding it and drew a total of 9 circles with a centers that were translated to another sub lattice and then I restricted my plot to the original lattice.  def draw_zone(self, filename, R): colormap = np.where(self.distance <= R, "blue", "red").flatten() X = self.positions[:, :, 0].flatten() Y = self.positions[:, :, 1].flatten() x0, y0 = self.c0 centers = [(x0-L, y0-L), (x0-L, y0), (x0-L, y0+L), (x0, y0-L), (x0, y0), (x0, y0+L), (x0+L, y0-L), (x0+L, y0), (x0+L, y0+L)] plt.clf() for (x,y) in centers: circle = plt.Circle((x, y), R, alpha=0.2, color="grey") plt.gca().add_patch(circle) plt.scatter(X, Y, s=10, color=colormap) plt.xticks([]) plt.yticks([]) plt.xlim(0, self.L-1) plt.ylim(0, self.L-1) plt.title("Lattice") plt.savefig(filename)
Question: I am trying to extend the functionality of polars to manipulate categories of Enum. I am following this guide and this section of documentation orig_df = pl.DataFrame({ 'idx': pl.int_range(5, eager=True), 'orig_series': pl.Series(['Alpha', 'Omega', 'Alpha', 'Beta', 'Gamma'], dtype=pl.Enum(['Alpha', 'Beta', 'Gamma', 'Omega']))}) @pl.api.register_expr_namespace('fct') class CustomEnumMethodsCollection: def __init__(self, expr: pl.Expr): self._expr = expr def rev(self) -> pl.Expr: cats = self._expr.cat.get_categories() tmp_sr = self._expr.cast(pl.Categorical) return tmp_sr.cast(dtype=pl.Enum(cats.str.reverse())) (orig_df .with_columns(rev_series=pl.col("orig_series").fct.rev()) )  This errors with TypeError: Series constructor called with unsupported type 'Expr' for the values parameter because cats is an unevaluated expression, not a list or a series, as pl.Enum(dtype=) expects it. How do I evaluate the cats into the actual list/series to provide the new categories for my cast(pl.Enum) method?

Answer: You can use .map_batches() @pl.api.register_expr_namespace('fct') class CustomEnumMethodsCollection: def __init__(self, expr: pl.Expr): self._expr = expr def rev(self) -> pl.Expr: return self._expr.map_batches(lambda s: s.cast(pl.Enum(s.cat.get_categories().reverse())) )  df = pl.DataFrame({ 'idx': pl.int_range(5, eager=True), 'orig_series': pl.Series(['Alpha', 'Omega', 'Alpha', 'Beta', 'Gamma'], dtype=pl.Enum(['Alpha', 'Beta', 'Gamma', 'Omega']))}) df.with_columns(rev_series=pl.col('orig_series').fct.rev()).schema  Schema([('idx', Int64), ('orig_series', Enum(categories=['Alpha', 'Beta', 'Gamma', 'Omega'])), ('rev_series', Enum(categories=['Omega', 'Gamma', 'Beta', 'Alpha']))])