eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want to configure rate limiting with SlowAPI (in-memory, without Redis Cache etc.), but I don't want to add the @limiter.limit() decorator seperately for every endpoint. So, something I don't want to do manually would be: @limiter.limit("5/minute") async def myendpoint(request: Request) pass  So, essentially, I want to include it in a middleware: from slowapi import Limiter limiter = Limiter(key_func=get_remote_address) @app.middleware("http") async def check_request(request: Request, call_next): client_ip = request.client.host prefix = "request_rate_limiter." + client_ip #... (logic from slowapi to get allowed flag) if allowed: response = await call_next(request) return response  But I didn't find a solution on how to receive a boolean from the limiter. How would I proceed from this and would this work? It would be great if I could configure it for different routes and also for example depending on a user subscription (e.g. free/premium). Thanks!

Answer: To apply a global (default) limit to all routes, you could use the SlowAPIMiddleware, as shown in the example below. The relevant documentation could be found here. Related answers could also be found here and here. Example from fastapi import FastAPI from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.middleware import SlowAPIMiddleware from slowapi.errors import RateLimitExceeded limiter = Limiter(key_func=get_remote_address, default_limits=["1/minute"]) app = FastAPI() app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) app.add_middleware(SlowAPIMiddleware) @app.get("/") async def main(): return "Only once per minute"  To exempt a route from the global limit, you could use the @limiter.exempt decorator on a given route, as shown in the following example. However, it seems that, currently, the decorator would only work for endpoints defined with normal def instead of async def (it could be a bug in the relevant implementation of SlowAPI—UPDATE: The issue has been fixed, so please make sure to upgrade to the latest version of SlowAPI). @app.get("/someroute") @limiter.exempt async def someroute(): return "I'm unlimited"
Question: I'm using OpenCV's HoughLinesP function to detect straight lines in an image. When the image contains thin lines, the detection works perfectly. However, when the image contains thick lines, the algorithm detects them as two parallel lines instead of a single line. Here's my current code: import cv2 import numpy as np image_path = "thickLines.png" image = cv2.imread(image_path) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Thresholding to create a binary image _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) # Edge Detection edges = cv2.Canny(binary, 50, 150, apertureSize=3) # Hough Line Transform to Detect Walls lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=5) # Draw Detected Walls if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw thick lines in green # Show Final Processed Image cv2.imshow("Detected Image", image) cv2.waitKey(0) cv2.destroyAllWindows()  I have tried adjusting the Canny edge thresholds and modifying minLineLength and maxLineGap, but the issue persists. My goal is to detect thick lines as a single line instead of two parallel lines. Questions:  How can I modify my approach to merge or simplify detected thick lines into a single line? Are there any parameters in HoughLinesP that can be tuned to achieve this?  Screenshots Here are screenshots of the issue: The green lines represents the detected lines from the image   Here are sample images of thick & thin lines  

Answer: You're looking to skeletonize the image - reduce the thickness of all features to 1 pixel.  scikit-image.morphology has a neat method, skeletonize, just for this. import cv2 import numpy as np from skimage.morphology import skeletonize image_path = "thickLines.png" image = cv2.imread(image_path) # binarize image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) # convert to skeleton, then to mat skeleton = skeletonize(binary) skeletonImg = (skeleton * 255).astype(np.uint8) # Hough Line Transform to Detect Walls lines = cv2.HoughLinesP(skeletonImg, 1, np.pi / 180, 80, minLineLength=50, maxLineGap=5) # Draw Detected Walls if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw thick lines in green # Show Final Processed Image cv2.imshow("Detected Image", image) cv2.waitKey(0) cv2.destroyAllWindows()  Result:    thin thick
Question: I have a django app, and I use django-solo for SingletonModel. I do have a singleton settings model: class GeneralSettings(SingletonModel): allow_signup = models.BooleanField(default=True)  I want to create an API endpoint to be able to retrieve and update the settings. I currently use DRF. Using RetrieveModelMixin and UpdateModelMixin I can easily do it but then my route has to be: .../api/settings/1 < I need to add the id. How can I retrieve / update my settings without having to use the id (since it doesn't make sens for a SingletonModel)? DRF view: class GeneralSettingsViewSet( RetrieveModelMixin, UpdateModelMixin, GenericViewSet, ): queryset = GeneralSettings.objects.all() serializer_class = GeneralSettingsSerializer http_method_names = ["get", "put"] def get_object(self) -> GeneralSettings: return GeneralSettings.get_solo()  Router: router.register(r"settings", GeneralSettingsViewSet, "api-settings")

Answer: You can use custom router to solve your problem. Below is an example of what this might look like: from rest_framework.routers import Route, SimpleRouter class SingletonRouter(SimpleRouter): routes = [ Route( url=r'^{prefix}{trailing_slash}$', mapping={ 'get': 'retrieve', 'put': 'update', }, name='{basename}-detail', detail=True, initkwargs={'suffix': 'Instance'} ), ] router = SingletonRouter() router.register(r'settings', GeneralSettingsViewSet, 'api-settings')  It should work now: GET .../settings/ PUT .../settings/
Question: I created a custom wraps like this def addLogger(fn): from functools import wraps @wraps(fn) async def add_logger(*args, **kwargs): print(f"addLogger: About to run {fn.__name__}") log = logger startTime = time.time() log.info('About to run %s' % fn.__name__) result = await fn(*args, **kwargs) log.info('Done running %s Execution time: %s' % (fn.__name__, time.time() - startTime)) print(f"addLogger: Done running {fn.__name__}") return result return add_logger  and it's working great on this function @bot.event @addLogger async def on_ready(): logger.info(f'{bot.user.name} has connected to Discord!') try : synced = await bot.tree.sync() print(f"Synced {len(synced)} command") except Exception as e: print(e)  but not on list_cogs its just does nothing @addLogger @bot.tree.command(name="list_cogs", description="list all cogs") async def list_cogs(interaction): await interaction.response.send_message(f"{cogList}")  I already tried to return commands.check(add_logger) instead of add_logger but it ends up to not work at all

Answer: You need to have @addLogger after the @bot.tree.command() line. So the correct code would be @bot.tree.command(name="list_cogs", description="list all cogs") @addLogger async def list_cogs(interaction): await interaction.response.send_message(f"{cogList}")  Think of it like @addLogger changing the code of list_cogs() so the new list_cogs() function will do what it did before, but also time itself. Then, once you have that new function, that's what you add to the bot tree as a command.
Question: I've written this code: from typing import overload, TYPE_CHECKING, Protocol, Any import pyarrow as pa # type: ignore[import-not-found] class PyArrowArray(Protocol): @property def buffers(self) -> Any: ... @overload def func(a: PyArrowArray) -> int: ... @overload def func(a: str) -> str: ... @overload def func(a: Any) -> str | int: ... def func(a) -> str | int: if isinstance(a, pa.Array): return 0 return '0' reveal_type(func(pa.array([1,2,3])))  PyArrow is a Python library which does not have type hints. However, there is a package pyarrow-stubs which provides types for it. I have a function can accept either a pyarrow.Array or a str:  if it receives a pyarrow.Array, it returns an int if it receives a str, it returns a str  I would like to annotate it such that:  if a user has pyarrow-stubs installed, then func(pa.array([1,2,3])) is revealed to be int if a user doesn't have pyarrow-stubs installed, then func(pa.array([1,2,3])) should be revealed to be int | str, because pa is not known statically  I was hoping that the code above would accomplish that, but it doesn't. If pyarrow-stubs is not installed, I get Revealed type is "Any"  I was expecting that the @overload def func(a: Any) -> str | int: ...  overload would be matched and that I'd get Revealed type is int | str

Answer: I am confident to say that this is very likely not possible. The Any Type per PEP 484 matches everything, hence an Any input will match all overloads. What happens in such cases is defined here in the Mypy docs:  [...] if multiple variants match due to an argument being of type Any, mypy will make the inferred type also be Any:  pyright docs also describes it similarly, interestingly with pyright you can choose one, but not both overloads and avoid Unknown, this is explained here. # NOTE: pyright only @overload def func(a: PyArrowArray) -> int: ... @overload def func(a: Any) -> str | int: ... def func(a): if isinstance(a, pa.Array): return 0 return "0" bla = cast(PyArrowArray, ...) reveal_type(func(bla)) # int reveal_type(func("fooo")) # str | int :( reveal_type(func(pa.array([1, 2, 3]))) # str | int # Not Unknown   The only solution I somewhat see is that in turn pa.array is not allowed to be Any. Currently I see no good way to satisfy that without destroying compatibility when stubs are present. Best make it a requirement. Or somehow make everything a no-op class that is secondary to the stubs. I assume you are looking for a mypy solution, with pyright you can solve it like this: from typing import overload, TYPE_CHECKING, Callable, Protocol, Any, reveal_type, T import pyarrow as pa if TYPE_CHECKING: if pa.array is None: # path only taken when stubs not present class unknown: def __call__(self, *args, **kwargs) -> unknown: ... pa.array = unknown() # ... your original code # type of pa.array without stubs will be: array: unknown | Unknown reveal_type(func(bla)) # int reveal_type(func("fooo")) # str reveal_type(func(pa.array([1, 2, 3]))) # int | str
Question: I'm slowly migrating to polars from pandas and I have found that in some cases the polars syntax is tricky. I'm seeking help to do a group_by followed by a describe using less (or more readable) code. See this example: from io import BytesIO import pandas as pd import polars as pl S = b'''group,value\n3,245\n3,28\n3,48\n1,113\n1,288\n1,165\n2,90\n2,21\n2,109''' pl_df = pl.read_csv(BytesIO(S)) pd_df = pd.read_csv(BytesIO(S)) # Polars' way pl_df.group_by('group').map_groups( lambda df: ( df['value'] .describe() .with_columns( group=pl.lit(df['group'][0]) ) ) ).pivot(index='group', on='statistic')  Something similar in pandas would be: # pandas' pd_df.groupby('group').value.describe() 

Answer: You can write a quick function that returns a mapping of expressions that you can unpack right into your DataFrame.group_by(...).agg. This avoids any slow-ness of using map_groups and enables Polars to easily scan the query for any optimizations (provided you are working with a LazyFrame). from io import BytesIO import pandas as pd import polars as pl def describe(column, percentiles=[.25, .5, .75]): return { 'count': column.count(), 'null_count': column.null_count(), 'mean': column.mean(), 'std': column.std(), 'min': column.min(), **{ f'{pct*100:g}%': column.quantile(pct) for pct in percentiles }, 'max': column.max(), } S = b'''group,value\n3,245\n3,28\n3,48\n1,113\n1,288\n1,165\n2,90\n2,21\n2,109''' pl_df = pl.read_csv(BytesIO(S)) print( pl_df.group_by('group').agg(**describe(pl.col('value'))) )  Produces shape: (3, 10) ┌───────┬───────┬────────────┬────────────┬───┬───────┬───────┬───────┬─────┐ │ group ┆ count ┆ null_count ┆ mean ┆ … ┆ 25% ┆ 50% ┆ 75% ┆ max │ │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ u32 ┆ u32 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ i64 │ ╞═══════╪═══════╪════════════╪════════════╪═══╪═══════╪═══════╪═══════╪═════╡ │ 1 ┆ 3 ┆ 0 ┆ 188.666667 ┆ … ┆ 165.0 ┆ 165.0 ┆ 288.0 ┆ 288 │ │ 3 ┆ 3 ┆ 0 ┆ 107.0 ┆ … ┆ 48.0 ┆ 48.0 ┆ 245.0 ┆ 245 │ │ 2 ┆ 3 ┆ 0 ┆ 73.333333 ┆ … ┆ 90.0 ┆ 90.0 ┆ 109.0 ┆ 109 │ └───────┴───────┴────────────┴────────────┴───┴───────┴───────┴───────┴─────┘
Question: I'm trying to organize text alignment in ttk.Label using style in order to reduce the amount of code: import tkinter as tk from tkinter import ttk from tkinter import font app = tk.Tk() width = 605 height = 200 x = int((app.winfo_screenwidth() / 2) - (width / 2)) y = int((app.winfo_screenheight() / 2) - (height / 2)) app.geometry(f'{width}x{height}+{x}+{y}') app.resizable(width=False, height=False) ttk.Style().configure('question.TLabel', justify=tk.CENTER, background='#ffffff', border=0) question = font.Font(family='Tahoma', size=14, weight='bold') ttk.Label(app, style='question.TLabel', text='When the view in the widget’s window change,\n' 'the widget will generate a Tcl command' '\nbased on the scrollcommand.', font=question).place(x=90, y=35) app.mainloop()  RESULT screenshot But it doesn't work that way. How to fix it? The ttk.Label documentation says:  If the text you provide contains newline ('\n') characters, this option specifies how each line will be positioned horizontally: tk.LEFT to left-justify; tk.CENTER to center; or tk.RIGHT to right-justify each line. You may also specify this option using a style.  The rest of the parameters specified in the style work. justify=tk.CENTER starts working if moved directly to ttk.Label: RESULT screenshot

Answer: According to the official documentation, justify is not supported in the style. I think the documentation you're using is incorrect. ----  TLabel styling options configurable with ttk::style are: -background color -compound compound -foreground color -font font
Question: The code below shows a solution I have found in order to expand a dataframe to include the cartesian product of columns A and B, filling in the other columns with null values. I'm wondering if there is a better and more efficient way of solving this? >>> df = pl.DataFrame({'A': [0, 1, 1], ... 'B': [1, 1, 2], ... 'C': [6, 7, 8]}) >>> df shape: (3, 3) ┌─────┬─────┬─────┐ │ A ┆ B ┆ C │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 0 ┆ 1 ┆ 6 │ │ 1 ┆ 1 ┆ 7 │ │ 1 ┆ 2 ┆ 8 │ └─────┴─────┴─────┘ >>> df.join(df.select('A').unique().join(df.select('B').unique(), how='cross'), on=['A','B'], how='right') shape: (4, 3) ┌──────┬─────┬─────┐ │ C ┆ A ┆ B │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞══════╪═════╪═════╡ │ 6 ┆ 0 ┆ 1 │ │ null ┆ 0 ┆ 2 │ │ 7 ┆ 1 ┆ 1 │ │ 8 ┆ 1 ┆ 2 │ └──────┴─────┴─────┘ 

Answer: This is a requested feature (this is available in R or pandas's janitor as complete). An alternative approach mentioned in the feature request would be: (df.select(pl.col(['A', 'B']).unique().sort().implode()) .explode('A') .explode('B') .join(df, how='left', on=['A', 'B']) )  Which makes it easy to generalize to a greater number of columns. Output: ┌─────┬─────┬──────┐ │ A ┆ B ┆ C │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪══════╡ │ 0 ┆ 1 ┆ 6 │ │ 0 ┆ 2 ┆ null │ │ 1 ┆ 1 ┆ 7 │ │ 1 ┆ 2 ┆ 8 │ └─────┴─────┴──────┘
Question: SETUP I have a list days and a value N days = ['Monday','Tuesday','Wednesday','Thursday','Friday'] N = 52  WHAT I AM TRYING TO DO I am trying to create a list selections with length N where I uniformly in frequency sample values from days (remainders are fine). I would like the order of this list to then be shuffled. EXAMPLE OUTPUT NOTE HOW THE ORDER IS SHUFFLED, BUT THE DISTRIBUTION OF VALUES IS UNIFORM selections ['Wednesday','Friday','Monday',...'Tuesday','Thursday','Monday'] import collections counter = collections.Counter(selections) counter Counter({'Monday': 11, 'Tuesday': 10, 'Wednesday': 11, 'Thursday': 10, 'Friday': 10})  WHAT I HAVE TRIED I have code to randomly select N values from days from random import choice, seed seed(1) days = ['Monday','Tuesday','Wednesday','Thursday','Friday'] N = 52 selections = [choice(days) for x in range(N)]  But they aren't selected uniformly import collections counter = collections.Counter(selections) counter Counter({'Tuesday': 9, 'Friday': 8, 'Monday': 14, 'Wednesday': 7, 'Thursday': 14})  How can I adjust this code or what different method will create a list of length N with a uniform distribution of values from days in a random order?  EDIT: I obviously seemed to have phrased this question poorly. I am looking for list with length N with a uniform distribution of values from days but in a shuffled order (what I meant by random.) So I suppose what I am looking for is how to uniformly sample values from days N times, then just shuffle that list. Again, I want an equal amount of each value from days making up a list with length N. I need a uniform distribution for a list of exactly length 52, just as the example output shows.

Answer: The code you have is correct. You are seeing expected noise around the mean. Note that for higher N, the relative noise decreases, as expected. For example, this is what you get for N = 10000000: Counter({'Tuesday': 2000695, 'Thursday': 2000615, 'Wednesday': 2000096, 'Monday': 1999526, 'Friday': 1999068})  If you need equal or approximately equal (deterministic, rather than random) numbers of each element in random order, try a combination of itertools.cycle, itertools.islice and random.shuffle like so:  import random import collections import itertools random.seed(1) days = ['Monday','Tuesday','Wednesday','Thursday','Friday'] N = 52 # If `N` is not divisible by `len(days)`, this line ensures that the last # `N % len(days)` elements of `selections` also stay random: random.shuffle(days) selections = list(itertools.islice(itertools.cycle(days), N)) random.shuffle(selections) print(selections) counter = collections.Counter(selections) print(counter)  Output: ['Friday', 'Friday', 'Wednesday', ..., 'Thursday'] Counter({'Tuesday': 11, 'Monday': 11, 'Friday': 10, 'Wednesday': 10, 'Thursday': 10})
Question: I have a Protocol subclass that defines objects with attributes from an external library: class P(Protocol): val: int  For testing purposes, I want to turn this protocol class into something I can instantiate easily. However, when I try to turn it into a dataclass, an error pops up: import dataclasses from typing import Protocol class P(Protocol): val: int PInst = dataclasses.dataclass(P) PInst(val=4) # TypeError: Protocols cannot be instantiated  Is there an easy solution to use P to create a class that satifies its protocol and is instantiable, without redeclaring its attributes?

Answer: You are asking for a non-protocol class derived from the attributes in your protocol class. They are stored in the annotations, which are accessible as typing.get_type_hints(P). In my first try, I dynamically created that class with builtin type, passing it the protocol's type hints to define an equivalent non-protocol class, and pass that to dataclass. But it needed me to manually set dunder annotations before it could correctly create the init method. But messing with internal attributes is a red flag telling me there should be an easier way. So I looked at the dataclasses public interface and found make_dataclass. You can just pass the annotations directly to its fields parameter. from dataclasses import make_dataclass from typing import Protocol, get_type_hints class P(Protocol): val: int DP = make_dataclass('DP', get_type_hints(P)) instance = DP(val=4) print(instance)  output: DP(val=4)
Question: I have two endpoints below, one that accepts two integers and another that accepts tow lists of integers. I also have two post requests that I am making using Python requests. The request for the list of ints endpoint works fine, but the one that just accepts the two ints does not. I know that if I pass the two ints in the URL (i.e, the query string), the endpoint will work. However, could you tell me why it doesn't work when passed as JSON? endpoints.py from fastapi import FastAPI app = FastAPI() @app.post('/ints') def post_int(x: int, y: int): return x, y @app.post('/lists') def post_list(x: list[int], y: list[int]): return x, y  requests.py import requests r = requests.post('http://127.0.0.1:8000/ints', json={'x': 1, 'y': 2}) print(r.json()) # returns an error saying that x and y are missing r = requests.post('http://127.0.0.1:8000/ints?x=1&y=2') print(r.json()) # returns the two ints r = requests.post('http://127.0.0.1:8000/lists', json={'x': [1, 2, 5], 'y': [1, 2, 3]}) print(r.json()) # returns the two lists  To start server, run "fastapi dev /path/to/endpoints.py" in terminal. Run requests.py in another terminal for output.

Answer:  could you tell me why it doesn't work when passed as JSON?  When you register a route with @app.post('/ints') def post_int(x: int, y: int): return x, y  in FastApi, the route is analyzed and added in decorator (routing.py) add_api_route (routing.py) __init__ (routing.py) get_dependant (dependencies/utils.py) analyze_param (dependencies/utils.py)  Near the end of analyze_param, you can find elif field_info is None and depends is None: default_value = value if value is not inspect.Signature.empty else RequiredParam if is_path_param: # We might check here that `default_value is RequiredParam`, but the fact is that the same # parameter might sometimes be a path parameter and sometimes not. See # `tests/test_infer_param_optionality.py` for an example. field_info = params.Path(annotation=use_annotation) elif is_uploadfile_or_nonable_uploadfile_annotation( type_annotation ) or is_uploadfile_sequence_annotation(type_annotation): field_info = params.File(annotation=use_annotation, default=default_value) elif not field_annotation_is_scalar(annotation=type_annotation): field_info = params.Body(annotation=use_annotation, default=default_value) else: field_info = params.Query(annotation=use_annotation, default=default_value)  https://github.com/fastapi/fastapi/blob/master/fastapi/dependencies/utils.py#L460 There you can see that scalar parameters have field info type query. Lists aren't scalar values and have field info type body.