eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to replace all the values of the border of my n-dimensional array by False. So far, I have seen that numpy provides np.pad that allows me to grow an array in all dimensions with an arbitrary array. Is there an equivalent to do the opposite and "shrink" the array by cutting the borders? Here is an example in 2D that I would like to extend to arbitrary dimension: import numpy as np nd_array = np.random.randn(100,100)>0 # Just to have a random bool array, but the same would apply with floats, for example cut_array = nd_array[1:-1, 1:-1] # This is what I would like to generalize to arbitrary dimension padded_array = np.pad(cut_array, pad_width=1, mode='constant', constant_values=False)  Of course, if there is an easier way to change the values of the borders in arbitrary dimension, that would also be appreciated.

Answer: I would not use the approach of first cropping then padding, because like this you move a lot of memory around. Instead I would explicitly set the border indexes to the desired value: import numpy as np border_value = False nd_array = np.random.randn(100,100) > 0 # Iterate over all dimensions of `nd_array` for dim in range(nd_array.ndim): # Make current dimension the first dimension array_moved = np.moveaxis(nd_array, dim, 0) # Set border values in the current dimension array_moved[0] = border_value array_moved[-1] = border_value # We do not even need to move the current dimension # back to its original position, as `np.moveaxis()` # provides a view into the original data, thus by # altering the values of `array_moved`, we also # alter the values of `nd_array`. So we are done.  Note that np.moveaxis() is a pretty cheap operation, as it only adjusts the strides of the array (to produce array_moved, in our case), so no actual array data gets moved around.
Question: I have a route / which started an endless loop (technically until the websocket is disconnected but in this simplified example it is truly endless). How do I stop this loop on shutdown: from fastapi import FastAPI import asyncio app = FastAPI() running = True @app.on_event("shutdown") def shutdown_event(): global running running = False @app.get("/") async def index(): while running: await asyncio.sleep(0.1)  According to the docs @app.on_event("shutdown") should be called during the shutdown, but is suspect it is called similar to the lifetime event which is called after everything is finished which is a deadlock in this situation. To test:  i run it as uvicorn module.filename:app --host 0.0.0.0 curl http://ip:port/ then stop the server (pressing CTRL+C)  and you see that it hangs forever since running is never set to false because shutdown_event is not called. (Yes you can force shutdown by pressing CTRL+C)

Answer: import signal import asyncio from fastapi import FastAPI app = FastAPI() running = True def stop_server(*args): global running running = False @app.on_event("startup") def startup_event(): signal.signal(signal.SIGINT, stop_server) @app.get("/") async def index(): while running: await asyncio.sleep(0.1)  Source: https://github.com/tiangolo/fastapi/discussions/9373#discussioncomment-5573492 Setting up and catching the SIGINT signal allows to catch the first CNTR+C. This will set running to False which ends the loop in index(). Terminating the running request allowing the server to shutdown.
Question: I'm trying to list files using webdab but I'm having issues. I can create directories and put files just fine but not list a directory or pull a file. I'm seeing the error, "Method not supported". from webdav3.client import Client options = { 'webdav_hostname': "https://___________.com/remote.php/dav/files/", 'webdav_login': "user_name", 'webdav_password': "password" } client = Client(options) print(client.list('/')) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 67, in _wrapper res = fn(self, *args, **kw) File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 264, in list response = self.execute_request(action='list', path=directory_urn.quote()) File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 228, in execute_request raise MethodNotSupported(name=action, server=self.webdav.hostname) webdav3.exceptions.MethodNotSupported: Method 'list' not supported for https://________.com/remote.php/dav/files 

Answer: The client.list() method assumes the remote root directory by default. As you supply https://___________.com/remote.php/dav/files/ as your webdav_hostname the root directory it tries to access when you call client.list('/') is the top level files directory. As a Nextcloud user you don't have access to that level, so listing that is impossible. However, you do have access to the files/<username> directory, so listing client.list('/<username>/') works. To prevent that you have prepend the username to every list command you can set the webdav_hostname to .../remote.php/dav/files/<username>. Then a call to client.list() should work straight away.
Question: this is my first question here :) When i try to install Anaconda on my MacBook (M1) with Ventura 13.3.1 i receive the following error: "This package is incompatible with this version of macOS." I tried the arm64 installer and the x86 installer, both lead to the same error message. I used Anaconda on the same MacBook just a few days ago, but after the update from Ventura 13.2.1 to 13.3 I couldn't open Jupyter Notebooks from within the Anaconda Navigator. First I thought that the problem might be caused by Anaconda, so I uninstalled it. However, now here I am, unable to install it again. I also did a complete reset of my MacBook, nothing changed. Does anyone have the same issue or know how I can fix this? Thanks a lot

Answer: if you have homebrew installed you should be able to run "brew install anaconda"
Question: I'm training YOLOv8 in Colab on a custom dataset. How can I save the model after some epochs and continue the training later. I did the first epoch like this: import torch model = YOLO("yolov8x.pt") model.train(data="/image_datasets/Website_Screenshots.v1-raw.yolov8/data.yaml", epochs=1)  While looking for the options it seems that with YOLOv5 it would be possible to save the model or the weights dict. I tried these but either the save or load doesn't seem to work in this case: torch.save(model, 'yolov8_model.pt') torch.save(model.state_dict(), 'yolov8x_model_state.pt') 

Answer: "I am currently working on a project using YOLOv8. After training on a custom dataset, the best weight is automatically stored in the runs/detect/train/weights directory as best.pt. When I retrain the model, I use the best.pt weight instead of yolov8x.pt to train the model."
Question: I'm looking for a nice way to sequentially combine two itertools operators. As an example, suppose we want to select numbers from a generator sequence greater than a threshold, after having gotten past that threshold. For a threshold of 12000, these would correspond to it.takewhile(lambda x: x<12000) and it.takewhile(lambda x: x>=12000): # Set up an example generator: def lcg(a=75,c=74,m=2**16+1,x0 = 1): xn = x0 yield xn while True: xn = (a*xn+c) % m yield xn  # First 20 elements: list(it.islice(lcg(), 20)) [1, # <- start sequence, start it.takewhile(lambda x: x<12000) 149, 11249, # <- last element of it.takewhile(lambda x: x<12000) 57305, # <- start it.takewhile(lambda x: x>=12000) here 38044, 35283, 24819, 26463, 18689, 25472, # <- last element of it.takewhile(lambda x: x>=12000); end of sequence 9901, 21742, 57836, 12332, 7456, 34978, 1944, 14800, 61482, 23634]  Is there a way to select the sequence of greater than 12000, including the initial values less than 12000, i.e. the desired output is: [1, 149, 11249, 57305, 38044, 35283, 24819, 26463, 18689, 25472]  This is trivial to do with two for-loops, but I'm looking for an itertools-type way (maybe a one-liner?) of combining the two operators without reseting the lcg generator.

Answer: One approach to writing a one-liner for the problem with the existing itertools library would be to use a flag variable with {0} as a default value to indicate which predicate to use. At first the flag evaluates to a truthy value so that the first predicate (x < 12000) is made effective, and if the first predicate fails, pop the set so the flag becomes falsey to make the second predicate (x >= 12000) effective. By popping 0 from the set, it also allows the expression to fall back to the second predicate on the same iteration when the first predicate fails: takewhile(lambda x, f={0}: f and (x < 12000 or f.pop()) or x >= 12000, lcg())  Note that it's safe to use a mutable object as the default value of an argument in this case because lambda creates a new function object along with a new instance of the mutable object every time it's evaluated. Demo: https://replit.com/@blhsing/VirtualGuiltyRatio
Question: I'm trying to add support for zooming in and out inside a Canvas widget, containing both text element (created using create_text) and non-text elements, such as rectangles (created with create_rectangle), etc. So far, I made the following MRE using both part of this answer and this one: import tkinter as tk from tkinter.font import Font root = tk.Tk() canvas = tk.Canvas(root, width=400, height=400) canvas.pack() font = Font(family="Arial", size=10) fontsize = 10 # Add elements to canvas rectangle = canvas.create_rectangle(100, 100, 300, 200, fill='red') oval = canvas.create_oval(150, 150, 250, 250, fill='blue') text = canvas.create_text(200, 175, text="Hello", font=font) def do_zoom(event): global fontsize x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) factor = 1.001 ** event.delta if (event.delta > 0): fontsize *= 1.1 font.configure(size=int(fontsize)) elif (event.delta < 0): fontsize *= 0.9 font.configure(size=int(fontsize)) canvas.scale("all", x, y, factor, factor) canvas.bind("<MouseWheel>", do_zoom) canvas.bind('<ButtonPress-1>', lambda event: canvas.scan_mark(event.x, event.y)) canvas.bind("<B1-Motion>", lambda event: canvas.scan_dragto(event.x, event.y, gain=1)) root.mainloop()  This seems to work, but has one or two issues:  Once the font size get to 0 or 0.0??? in floats (happen when zooming out), the font size doesn't match with the actual visual size of the font, as it seems to be fixed in a higher size (can be seen on this gif here) When zooming in and out fast enough, repeatedly, a discrepancy can be seen on the font size on previous mousewheel scrolling, and the next one (can be seen by printing the font size).  In short, I'm wondering if there is a way, either to fix the above (and the reasons for them happening, aside from my own conjecture) or if there is a better way to handle this.

Answer: That's because the zoom factor for the coordinates of the canvas items and the zoom factor for the font size are different in your implementation. Keep in mind the XSCALE and YSCALE parameters of the scale command of the canvas accumulate for repeated calls. Because the original coordinates of the items are transformed with roundoff errors, I don't recommend using the scale() to implement zooming. (See the implementation of the polygon, for example.) Anyway, if you don't mind the roundoff errors, do like this. ... factor = 1.0 def do_zoom(event): global factor x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) if (event.delta > 0): ratio = 1.1 elif (event.delta < 0): ratio = 0.9 factor *= ratio font.configure(size=int(fontsize*factor)) canvas.scale("all", x, y, ratio, ratio) ...
Question: I was trying to create a simple video player using PyQt6. I found this example on the internet: import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QSlider from PyQt6.QtMultimedia import QMediaPlayer, QMediaContent from PyQt6.QtMultimediaWidgets import QVideoWidget from PyQt6.QtCore import QUrl, Qt class VideoPlayer(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Video Player") self.setGeometry(100, 100, 1024, 768) self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface) self.video_widget = QVideoWidget() self.start_button = QPushButton("Start") self.start_button.clicked.connect(self.start_video) self.pause_button = QPushButton("Pause") self.pause_button.clicked.connect(self.pause_video) self.stop_button = QPushButton("Stop") self.stop_button.clicked.connect(self.stop_video) self.slider = QSlider(Qt.Orientation.Horizontal) self.slider.sliderMoved.connect(self.set_position) self.media_player.setVideoOutput(self.video_widget) self.media_player.setMedia(QMediaContent(QUrl.fromLocalFile("Video.mp4"))) self.media_player.positionChanged.connect(self.position_changed) self.media_player.durationChanged.connect(self.duration_changed) layout = QVBoxLayout() layout.addWidget(self.video_widget) layout.addWidget(self.start_button) layout.addWidget(self.pause_button) layout.addWidget(self.stop_button) layout.addWidget(self.slider) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) def start_video(self): self.media_player.play() def pause_video(self): self.media_player.pause() def stop_video(self): self.media_player.stop() def set_position(self, position): self.media_player.setPosition(position) def position_changed(self, position): self.slider.setValue(position) def duration_changed(self, duration): self.slider.setRange(0, duration) if __name__ == "__main__": app = QApplication(sys.argv) video_player = VideoPlayer() video_player.show() sys.exit(app.exec())  But it doesn't work. As far as I understand, the "QMediaContent" and "QMediaPlayer.VideoSurface" classes are no longer supported. How can I change this code to make the player work? I've been trying to find what these classes were replaced with, but there's very little information on the internet about playing video in PyQt6 at the moment.

Answer: PyQt5: QMediaPlayer(parent=None, flags=QMediaPlayer.Flags()) QMediaPlayer.media # property of type QMediaContent QMediaPlayer.setMedia(media, stream=None)  PyQt6: The QMediaPlayer.Flags enum was removed, the QMediaContent class was removed, and the media property was replaced with source. QMediaPlayer(parent=None) QMediaPlayer.source # property of type QUrl QMediaPlayer.setSource(source)  The example can be fixed by changing those two lines accordingly. class VideoPlayer(QMainWindow): def __init__(self): super().__init__() ... self.media_player = QMediaPlayer() self.media_player.setSource(QUrl.fromLocalFile("Video.mp4")) self.video_widget = QVideoWidget() self.media_player.setVideoOutput(self.video_widget) ...  See Changes to Qt Multimedia for the complete list of changes.
Question: xml2js <=0.4.23 Severity: high xml2js is vulnerable to prototype pollution - https://github.com/advisories/GHSA-776f-qx25-q3cc No fix available node_modules/xml2js aws-sdk * Depends on vulnerable versions of xml2js node_modules/aws-sdk 2 high severity vulnerabilities Upgraded aws-sdk npm package to latest version. But vulnerability still exists.

Answer:  delete your package-lock.json, add this to your package.json:   "overrides": { "xml2js": "^0.5.0" }   reinstall the deps : npm i
Question: Question When I try to set the .device attribute to torch.device('cpu'), I get an error. How am I supposed to set device then? Python Code from transformers import TrainingArguments from transformers import Trainer import torch training_args = TrainingArguments( output_dir="./some_local_dir", overwrite_output_dir=True, per_device_train_batch_size=4, dataloader_num_workers=2, max_steps=500, logging_steps=1, evaluation_strategy="steps", eval_steps=5 ) trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=test_dataset, compute_metrics=compute_metrics, ) training_args.device = torch.device('cpu')  Python Error AttributeError Traceback (most recent call last) <ipython-input-11-30a92c0570b8> in <cell line: 28>() 26 ) 27 ---> 28 training_args.device = torch.device('cpu') AttributeError: can't set attribute 

Answer: From the docs of the TrainingArguments object doesn't have a settable device attribute. But interestingly device is initialized but non mutable: import torch from transformers import TrainingArguments args = TrainingArguments('./') args.device # [out]: device(type='cpu') args.device = torch.device(type='cpu')  [out]: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-12-dcb5ef23be68> in <cell line: 8>() 6 7 ----> 8 args.device = torch.device(type='cpu') AttributeError: can't set attribute  From the code, it looks like the device is set after the initialization: https://github.com/huggingface/transformers/blob/main/src/transformers/training_args.py#L1113 And the device is only setup when the TrainingArguments.device is called  https://github.com/huggingface/transformers/blob/main/src/transformers/training_args.py#L1678 https://github.com/huggingface/transformers/blob/main/src/transformers/training_args.py#L1524  Perhaps you are referring to the huggingface model and you want to try training it on the CPU. To do that, usually the Trainer will automatically detect it if you accelerate https://huggingface.co/docs/accelerate/index By default, the TrainingArguments is already set to CPU if _n_gpu = -1  But if you want to explicitly set the model to be used on CPU, try: model = model.to('cpu') trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=test_dataset, compute_metrics=compute_metrics, )  Or: trainer = Trainer( model=model.to('cpu'), args=training_args, train_dataset=train_dataset, eval_dataset=test_dataset, compute_metrics=compute_metrics, place_model_on_device=True )
Question: If I have the following code: submit = gr.Button(...) submit.click( fn=my_func, inputs=[ some_slider_1, # gr.Slider some_slider_2, # gr.Slider some_slider_3, # gr.Slider ], outputs=[ some_text_field ] )  How can I substitute let's say some_slider_2 for a constant value like 2? If I simply write 2 in there, I get an error: AttributeError: 'int' object has no attribute '_id'

Answer: If you want a constant number input which is not visible on the UI you can pass gr.Number(value=2, visible=False)
Question: I'm using Django 4.1.7 with django-stubs 1.16.0, and mypy 1.1.1. I have code that looks like this: class ProductQuerySet(QuerySet): ... class ProductManager(Manager): def create_red_product(self, **kwargs) -> "Product": return self.model(color=Product.Color.RED, **kwargs) _product_manager = ProductManager.from_queryset(ProductQuerySet) class Product(Model): ... objects = _product_manager()  When mypy looks at this, it says: models/product.py:46: error: Incompatible return value type (got "_T", expected "Product") [return-value]  It seems like the type of self.model in a model manager method is _T, which from what I understand is a generic type bound to the model, which in my case should be "Product". Why isn't this working? How can I fix it?

Answer: I can't reproduce one to one your case, but please try to specify what exactly your manager is for by: class ProductManager(Manager["Product"]):
Question: Is there a way to check if a Python dataclass has been set to frozen? If not, would it be valuable to have a method like is_frozen in the dataclasses module to perform this check? e.g. from dataclasses import dataclass, is_frozen @dataclass(frozen=True) class Person: name: str age: int person = Person('Alice', 25) if not is_frozen(person): person.name = 'Bob'  One way to check if a dataclass has been set to frozen is to try to modify one of its attributes and catch the FrozenInstanceError exception that will be raised if it's frozen. e.g. from dataclasses import FrozenInstanceError is_frozen = False try: person.name = 'check_if_frozen' except FrozenInstanceError: is_frozen = True  However, if the dataclass is not frozen, the attribute will be modified, which may be unwanted just for the sake of performing the check.

Answer: Yes looks like you can retrieve parameters' information from __dataclass_params__. It returns an instance of _DataclassParams type which is nothing but an object for holding the values of init, repr, eq, order, unsafe_hash, frozen attributes: from dataclasses import dataclass @dataclass(frozen=True) class Person: name: str age: int print(Person.__dataclass_params__) print(Person.__dataclass_params__.frozen)  output: _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=True) True
Question: I validate data using Pydantic schema in my FastAPI project and if it is not ok it returns 422. Can I change it to 400?

Answer: Yes, you can. For example, you can apply the next exception handler: from fastapi import FastAPI, Request, status from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse app = FastAPI() @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content={"detail": exc.errors()}, ) # your routes and endpoints here  In this case any validation errors raised by Pydantic will result in a 400 Bad Request status code being returned by your API. The exc.errors() contains the validation errors.
Question: I want to transform this json to a pyspark dataframe I have added my current code. json = { "key1": 0.75, "values":[ { "id": 2313, "val1": 350, "val2": 6000 }, { "id": 2477, "val1": 340, "val2": 6500 } ]  } my code: I can get the expected output using my code. Hope someone improve this. import json from pyspark.sql import SparkSession spark = SparkSession.builder.appName("CreateDataFrame").getOrCreate() json_string = json.dumps({ "key1": 0.75, "values":[ { "id": 2313, "val1": 350, "val2": 6000 }, { "id": 2477, "val1": 340, "val2": 6500 } ] }) df = spark.read.json(spark.sparkContext.parallelize([json_string])) df = df.select("key1", "values.id", "values.val1", "values.val2") df.show()  output +----+-------------+-------------+-------------+ |key1| id| val1| val2| +----+-------------+-------------+-------------+ |0.75| [2313, 2477]| [350, 340]| [6000, 6500]| +----+-------------+-------------+-------------+  Help appreciate to get the expecting output. Expecting output: +----+----+----+----+ |key1| id|val1|val2| +----+----+----+----+ |0.75|2313| 350|6000| |0.75|2477| 340|6500| +----+----+----+----+ 

Answer: You can try the spark inline function. df = df.selectExpr("key1", "inline(values)")