eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm migrating an API that was originally written in Python. The Python API allows you to send your request as camelCase or snake_case like this: This is allowed { "someInput": "nice" }  This is allowed { "some_input": "nice" }  This is done using a great Python library: Pydantic from pydantic import BaseModel def to_camel(string): words = string.split('_') return words[0] + ''.join(word.capitalize() for word in words[1:]) class InputModel(BaseModel): some_input: str class Config: alias_generator = to_camel allow_population_by_field_name = True  This allows to create an InputModel by alias (someInput) or by field name (some_input). I want to do the same or equivalent in Go. I'm using Gin: func Routes(router *gin.Engine) { v1 := router.Group("/v1") { v1.POST("/shipments", controllers.ShipmentCreator) } } func ShipmentCreator(ctx *gin.Context) { ResponseController := new(internal.OutputModel) var body domain.ShipmentsInputModel if err := ctx.BindJSON(&body); err != nil { fmt.Println(err) } validate := validator.New() err := validate.Struct(body) if err != nil { var validationErrors validator.ValidationErrors errors.As(err, &validationErrors) for _, validationError := range validationErrors { ResponseController.AddError(internal.ErrorsModel{ Parameter: validationError.Field(), Message: validationError.Error(), }) } ctx.JSON(http.StatusBadRequest, ResponseController) return }  My input struct looks something like this: type ShipmentsInputModel struct { LotId string `json:"lotId" tag:"lot_id" alias:"lot_id" validate:"required"` }  This does not work when my input is: { "lot_id": "someLotId" }  It returns: "message": "Key: 'ShipmentsInputModel.LotId' Error:Field validation for 'LotId' failed on the 'required' tag",  How can I accept both camelCase and snake_case?

Answer: In Go, you cannot provide two JSON tags together for a single struct field. JSON tags are specified using a single string, and they are used to define how a field should be marshaled (serialized to JSON) or unmarshaled (deserialized from JSON). You cannot specify multiple tags for a single field within a struct directly. If you need to support both CamelCase and SnakeCase in your JSON output, you would typically have to choose one consistent naming convention for your struct fields and then use appropriate JSON tags for all the fields. There's a neat way of doing this. I hope this helps.  package main import ( "encoding/json" "fmt" ) type ShipmentsInputModel struct { LotID } type LotID struct { LotId string `json:"lotId,omitempty"` Lot_ID string `json:"lot_id,omitempty"` } func (s *ShipmentsInputModel) setLodID(id string) { s.LotId = id s.Lot_ID = id } func main() { shipment := ShipmentsInputModel{} shipment.setLodID("someLotID") // Convert struct to JSON jsonData, err := json.Marshal(shipment) if err != nil { fmt.Println("Error:", err) return } // prints: {"lotId":"someLotID","lot_id":"someLotID"} fmt.Println(string(jsonData)) }
Question: The Coverage tool supports generating code coverage data from Pytest tests with coverage run -m pytest .... However, there is also the Pytest-Cov plugin, which invokes Coverage and generates coverage data by adding the --cov= option to Pytest. However the Pytest-Cov documentation doesn't seem to explain anywhere how this differs from just using coverage run. Is there any practical difference, or is it just a matter of the options/configuration choices that are available?

Answer: The differences/advantages are mentioned on their Github README and docs  Compared to just using coverage run this plugin does some extras:  Subprocess support: you can fork or run stuff in a subprocess and will get covered without any fuss. Xdist support: you can use all of pytest-xdist's features and still get coverage. Consistent pytest behavior. If you run coverage run -m pytest you will have slightly different sys.path (CWD will be in it, unlike when running pytest).  All features offered by the coverage package should work, either through pytest-cov's command line options or through coverage's config file.
Question: I have this dataframe : df = pd.DataFrame([list("ABCDEFGHIJ")]) ​ 0 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J  I got an error when trying to reshape the dataframe/array : np.reshape(df, (-1, 3))  ValueError: cannot reshape array of size 10 into shape (3) I'm expecting this array (or a dataframe with the same shape) : array([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', nan, nan]], dtype=object)  Why NumPy can't guess the expected shape by completing the missing values with nan?

Answer: Another possible solution, based on numpy.pad, which inserts the needed np.nan into the array: n = 3 s = df.shape[1] m = s // n + 1*(s % n != 0) np.pad(df.values.flatten(), (0, m*n - s), mode='constant', constant_values=np.nan).reshape(m,n)  Explanation:  s // n is the integer division of the length of the original array and the number of columns (after reshape).  s % n gives the remainder of the division s // n. For instance, if s = 9, then s // n is equal to 3 and s % n equal to 0.  However, if s = 10, s // n is equal to 3 and s % n equal to 1. Thus, s % n != 0 is True. Consequently, 1*(s % n != 0) is equal to 1, which makes m = 3 + 1.  (0, m*n - s) means the number of np.nan to insert at the left of the array (0, in this case) and the number of np.nan to insert at the right of the array (m*n - s).   Output: array([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', nan, nan]], dtype=object)
Question: It is not clear from the docs how to save a checkpoint for every epoch, and have it actually saved and not instantly deleted, with no followed metric. How to do it?

Answer: max_epochs = 100 val_every_n_epochs = 1 checkpoint_callback = ModelCheckpoint( # dirpath=checkpoints_path, # <--- specify this on the trainer itself for version control filename="fa_classifier_{epoch:02d}", every_n_epochs=val_every_n_epochs, save_top_k=-1, # <--- this is important! ) trainer = Trainer( callbacks=[checkpoint_callback], default_root_dir=checkpoints_path, check_val_every_n_epoch=val_every_n_epochs, max_epochs=max_epochs, gpus=1 )  this will not delete saved checkpoints.
Question: I was making a POST request which sends an image file from my UI to the backend server. Backend Server: from fastapi import FastAPI, UploadFile from fastapi.middleware.cors import CORSMiddleware import numpy as np import tensorflow as tf from io import BytesIO from PIL import Image app = FastAPI() origins = ["http://localhost:5173/"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) model = tf.keras.models.load_model("../models/1") class_names = ["Normal", "Tuberculosis"] def convert_image_to_np_array(bytes): # Read bytes as PIL image and convert it to np array np_array = np.array(Image.open(BytesIO(bytes))) return np_array @app.post("/predict") async def predict_tuberculosis(file: UploadFile): bytes = await file.read() np_array = convert_image_to_np_array(bytes) batch_image = np.expand_dims(np_array, axis=0) resized_batch_image = np.resize(batch_image, (1,256,256,3)) prediction = model.predict(resized_batch_image) label = class_names[np.argmax(prediction)] accuracy = np.max(prediction) print(accuracy) return label  Frontend: import React from "react" import { useState } from "react" import axios from "axios" import "./App.css" function App() { function callPrediction(file) { const formData = new FormData() formData.append("file", file) axios.post("http://localhost:8000/predict", formData) .then(res => setResult(res.data)) .catch(err => console.log(err)) } ...  Note: The file input from callPrediction has the format like this  When I call the function callPrediction to send an image file as an input to the function predict_tuberculosis , I got this error popping up  I did go searching for this but all the solutions I got was just adding CORS to my backend (which I have already done). I really appreciate any helps! Thank you

Answer: You should remove the trailing slash from the origin URL in: http://localhost:5173/ ^  The origin URL should instead look like this http://localhost:5173. Hence: origins = ["http://localhost:5173"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], )  For further details, please have a look at the following answers here, here, as well as here, here and here.
Question: I hope you are doing well. <ul> <li> 
Title:
 De Aardappeleters </li> <li> 
Dimensions:
 82 x 114 cm </li> <li> 
Media:
 canvas </li> <li> 
Style:
 Realism </li> <li> 
Date:
 1885 </li> ______ <li> | 
Genre:
 | It is located on a page of the website here Modern | </li> ______| </ul>  I have an HTML block☝ that I want to receive a text from li. But unfortunately, this li has no class or ID that I can select.This block is for a site.  <li> 
Genre:
 Modern </li>  I want to select the genre list and get the text.👇 Modern  The main problem here is that this block is different on another page.👇 <ul> <li> 
Title:
 De Aardappeleters </li> <li> 
Dimensions:
 82 x 114 cm </li> <li> 
Media:
 canvas </li> ______ <li> | 
Genre:
 |And it is located here on another page. Modern | </li> ______| <li> 
Style:
 Realism </li> <li> 
Date:
 1885 </li> </ul>  OriginalTagFind = layout.css('article ul li s::text').getall() TitleOriginal = [tag.strip() for tag in OriginalTagFind if tag.startswith('Genre:')]  In my opinion, if I come to the place I have selected and print the text of the mother's list with Next Sibiling. is it possible؟

Answer: With a css selector you can use: 'li:has(s):contains("Genre:")::text' With an xpath selector you can use: "//li[s[contains(text(), 'Genre')]]/text()" I have demonstrated using both with your example below: In [1]: html = """<ul> ...: <li> ...: 
Title:
 ...: De Aardappeleters ...: </li> ...: <li> ...: 
Dimensions:
 ...: 82 x 114 cm ...: </li> ...: <li> ...: 
Media:
 ...: canvas ...: </li> ...: <li> ...: 
Style:
 ...: Realism ...: </li> ...: <li> ...: 
Date:
 ...: 188 ...: </li> ...: <li> ...: 
Genre:
 ...: Modern ...: </li> ...: </ul> """ In [2]: selector = scrapy.Selector(text=html) In [3]: ''.join(selector.xpath("//li[s[contains(text(), 'Genre')]]/text()").getall()).strip() Out[3]: 'Modern' In [4]: ''.join(selector.css('li:has(s):contains("Genre:")::text').getall()).strip() Out[4]: 'Modern'
Question: is it a good idea to run the asyncio eventloop inside a thread? import asyncio import time from sample_threading import parallel loop = asyncio.new_event_loop() async def fn(p): for i in range(5): print(i) time.sleep(5) print("done") @parallel def th(p): loop.run_until_complete(fn(p)) th(1) th(2) th(3)  above code giving error raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running  any suggestion ?

Answer: error message you are haveing, This event loop is already running, is beacuse when you attempt to run an asyncio event loop that is already running. In your code, you are creating a new event loop using asyncio.new_event_loop(), but you are not explicitly setting it as the current event loop. import asyncio import time from sample_threading import parallel async def fn(p): for i in range(5): print(i) # instead of time.sleep, you ensure that the sleep operation is non- # blocking and allows other tasks to run concurrently. await asyncio.sleep(5) print("done") @parallel def th(p): asyncio.run(fn(p)) th(1) th(2) th(3)  Running the asyncio event loop inside a thread can be a valid approach in certain scenarios, especially when you need to perform asynchronous tasks concurrently with other operations.
Question: Consider the following function: from datetime import date def days_between(start_date: date, end_date: date) -> int: if start_date == "initial": start_date = date(2023, 9, 1) delta = end_date - start_date return delta.days  The type hint of start date is mostly fine, but it does not cover the case where "initial" is passed. This is the only string allowed, how would you express it? The options I thought about are:  start_date: date | str which seems a bit of an overkill start_date: date | "initial" maybe fine but I have not come across anything similar no change and simply add a description in the docstring  For completeness, I am more interested in how to convey the use of the function to a user compared to formal correctness for static type checking.

Answer: You should use typing.Literal from typing import Literal def days_between(start_date: date | Literal['initial'], end_date: date) -> int: ...  This is included starting from python 3.8. Literal can be used to indicate to type checkers that the annotated object has a value equivalent to one of the provided literals.
Question: Is there anyway to generate doc string using Github Copilot I have code and I want to generate doc string for it. def make_chat_content(self,chat_uuid,text,db_session): import uuid all_content = ChatContent.query.filter_by(chat_uuid=chat_uuid).all() chat_json = dump(all_content) chat_json.append({"role":"user","content":text}) response, total_words_generated = self.chat.get_response(chat_json)  for example doc string that describes function """ Add the user's text to the chat content and generate a response. This function takes the chat UUID, user's text, and a database session as input. It appends the user's content to the existing chat content, generates a response using a chat model, and returns the response along with the total words generated. Args: chat_uuid (str): The UUID of the chat session. text (str): The text content provided by the user. db_session: The database session for querying chat content. Returns: tuple: A tuple containing the response generated by the chat model and the total number of words generated in the response. """ 

Answer: Looks like it can be done:  Only test the sample code in the post.
Question: I could swear this should be returning three rows that meet the logic, and I cannot for the life of me figure out why only one is coming back.. Can someone please explain this? import numpy as np data = np.array([[1, 2, 3], [2, 5, 6], [7, 8, 9], [4, 3, 4]]) target_sum = 7 x = (((data[:, 0] + data[:, 1])==target_sum) | data[:, 0] ==7) matching_rows = data[x] print(matching_rows)  My terminal says: [[7 8 9]]  but surely it should say: [[2 5 6] [4 3 4] [7 8 9]]  No?

Answer: I think you mean: x = ((data[:, 0] + data[:, 1])==target_sum) | (data[:, 0] ==7)  Notice the parenthesis position. As stated in the doc: Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic. That means a | b == c is evaluated as (a | b) == c, not a | (b==c).
Question: I am trying to order facets in a plot produced by the seaborn objects interface. import numpy as np import seaborn as sns import seaborn.objects as so import matplotlib.pyplot as plt df = sns.load_dataset("iris") df["species"] = df["species"].astype("category") df["species"] = df["species"].cat.codes rng = np.random.default_rng(seed=0) df["subset"] = rng.choice(['A','B','C'], len(df), replace=True) fig = plt.figure(figsize=(6.4 * 2.0, 4.8 * 2.0)) _ = ( so.Plot(df, x="sepal_length", y="sepal_width") .facet(row="species", col="subset") .add(so.Dot()) .on(fig) .plot() )   However, if col_order or row_order are passed as parameters to the .facet() line an "unexpected keyword argument" TypeError is raised. _ = ( so.Plot(df, x="sepal_length", y="sepal_width") .facet( row="species", col="subset", row_order=['A','C','B'], col_order=[0,2,1] ) .add(so.Dot()) .on(fig) .plot() )  TypeError: Plot.facet() got an unexpected keyword argument 'row_order'  How should facets be ordered when using the seaborn.objects interface? Note that this question is very similar to "Seaborn ordering of facets" which is the same question when the plot is generated using seaborn but not the seaborn.objects module. Ideally, an answer should also work when using the wrap parameter of facet() in the seaborn.objects interface.

Answer: Plot.facet has a single order parameter. When only col or row are used a single list can be passed and it will be used by the appropriate variable. When both col and row are used, order can be a dictionary with col/row keys: ( so.Plot(df, x="sepal_length", y="sepal_width") .facet(row="species", col="subset", order={"row": [2, 1, 0], "col": ["A", "B", "C"]}) .add(so.Dot()) .layout(size=(6.4 * 2, 4.8 * 2)) )
Question: I was earlier able to filter duplicates based on multiple columns using df.filter(pl.col(['A','C']).is_duplicated()) but after the latest version update this is not working. import polars as pl df = pl.DataFrame( { "A": [1,4,4,7,7,10,10,13,16], "B": [2,5,5,8,18,11,11,14,17], "C": [3,6,6,9,9,12,12,15,18] } )  df.filter(pl.col(['A','C']).is_duplicated())  giving error df.filter(df.select( pl.col(['A','C']).is_duplicated() ) )  giving error

Answer: This behavior was noted as ambiguous in 0.16.10 and would return this error: exceptions.ComputeError: The predicate passed to 'LazyFrame.filter' expanded to multiple expressions: col("A").is_duplicated(), col("C").is_duplicated(), This is ambiguous. Try to combine the predicates with the 'all' or `any' expression.  However 0.19.0 removed the deprecated behavior of all/any replaced by all_horizontal and any_horizontal. To get the same behavior as the pre-0.16.10, use df.filter(pl.all_horizontal(pl.col(['A','C']).is_duplicated())) I've modified the input slightly to reflect the differences between any_horizontal and all_horizontal import polars as pl df = pl.DataFrame( { "A": [1,3,4,7,7,10,10,13,16], "B": [2,5,5,8,18,11,11,14,17], "C": [3,6,6,9,9,12,12,15,18] } ) # print("legacy run in 0.16.9: ", df.filter(pl.col(['A','C']).is_duplicated())) print("all_horizontal: ", df.filter(pl.all_horizontal(pl.col(['A','C']).is_duplicated()))) print("any_horizontal: ", df.filter(pl.any_horizontal(pl.col(['A','C']).is_duplicated())))  legacy run in 0.16.9: shape: (4, 3) ┌─────┬─────┬─────┐ │ A ┆ B ┆ C │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 7 ┆ 8 ┆ 9 │ │ 7 ┆ 18 ┆ 9 │ │ 10 ┆ 11 ┆ 12 │ │ 10 ┆ 11 ┆ 12 │ └─────┴─────┴─────┘ all_horizontal: shape: (4, 3) ┌─────┬─────┬─────┐ │ A ┆ B ┆ C │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 7 ┆ 8 ┆ 9 │ │ 7 ┆ 18 ┆ 9 │ │ 10 ┆ 11 ┆ 12 │ │ 10 ┆ 11 ┆ 12 │ └─────┴─────┴─────┘ any_horizontal: shape: (6, 3) ┌─────┬─────┬─────┐ │ A ┆ B ┆ C │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═════╡ │ 3 ┆ 5 ┆ 6 │ │ 4 ┆ 5 ┆ 6 │ │ 7 ┆ 8 ┆ 9 │ │ 7 ┆ 18 ┆ 9 │ │ 10 ┆ 11 ┆ 12 │ │ 10 ┆ 11 ┆ 12 │ └─────┴─────┴─────┘
Question: I have a dataframe of predictions on text data which gives a column named "prediction". here is the code to recreate it. import pandas as pd # initialize list elements data = ['others', 'others', 'prediction1', 'others', 'others', 'others', 'others', 'others', 'others', 'others', 'prediction2', 'others', 'prediction2', 'others', 'others', 'prediction1', 'others', 'others', 'others', 'others', 'others', 'others', 'others', 'prediction2', 'others', 'others', 'others', 'others', 'prediction1', 'others'] score = [0.75, 0.75, 0.9, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.88, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.9, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75] df = pd.DataFrame( {'prediction': data, 'score': score })  I want to find a list of index pairs of [(index_prediction1,index_prediction2),(index_prediction1,index_prediction2)] in this column where when prediction1 is found, find next prediction2 which has score higher than 0.85 and similarly find more pairs like this. prediction1 has no restriction of score. Currently I'm using iterrows which I came to know are very slow. This is my current code for finding all pairs. pred_pairs = [] for i, row in df.iterrows(): if (row['prediction'] == 'prediction1'): for index, row in df.iloc[i+1:].iterrows(): if row['prediction'] == 'prediction2' and row['score']>=0.85: pred_pairs.append((i,index)) break pred_pairs  I would like to know a better approach to do this.

Answer: original question You can use zip: pred_pairs = list(zip(df.index[df['prediction'].eq('prediction1')], df.index[df['prediction'].eq('prediction2')]))  Output: [(8, 20), (28, 36)]  Or, if the pairs always alternate, with numpy: out = (df.index[df['prediction'].isin(['prediction1', 'prediction2'])] .to_numpy().reshape(-1, 2).tolist() )  Output: [[8, 20], [28, 36]] updated question: more complex logic  only keep the prediction2 > 0.85 keep the first prediction2 after each prediction1  s = df.query('(prediction == "prediction1") | ((prediction == "prediction2") & (score > 0.85))')['prediction'] out = [list(x.index[:2]) for _, x in s.groupby(s.eq('prediction1').cumsum())]  Output: [[2, 12], [15, 23], [28]] If you only want the pairs in which there is a prediction2: s = df.query('(prediction == "prediction1") | ((prediction == "prediction2") & (score > 0.85))')['prediction'] out = [l for _, x in s.groupby(s.eq('prediction1').cumsum()) if len(l:=list(x.index[:2]))>1]  Output: [[2, 12], [15, 23]]
Question: I try to sort list of tuples first by 1st element, than by 2nd (if it exists), than by 3rd (if it exists). Since the tuples are of different length, some of them does not have 2nd or/and 3rd element, so the error appears: IndexError: list index out of range on line undefined.  list_to_sort = [(1, 11), (2, 0, 0), (1, 2), (2), (0, 1), (1, 2, 1), (1, 1, 1), (2, 0)]  expected output: [(0, 1), (1, 1, 1), (1, 2), (1, 2, 1), (1, 11), (2), (2, 0), (2, 0, 0)]  What I tried to do already, but the same error appears: IndexError: list index out of range on line undefined.  1. print(sorted(list_to_sort,key=lambda x:(x[0], x[1], x[2])))     print(sorted(list_to_sort,key=lambda x:(x[0],(x[1] is not None, x[1]), (x[2] is not None, x[2])))     def check(x): if x: return x else: return "" print(sorted(list_to_sort,key=lambda x:(x[0], check(x[1]), check(x[2])))  I want the code to check if the 2nd/3rd element is there and if yes sort with it's value, if not - suppose that it is empty or zero for sorting purposes. It seems easy, but I'm running out of options here. Any suggestions are welcome :) Thanks in advance!

Answer: I think you are looking for the basic, default sorting for a list a tuples however one of the items in your list might look like a tuple (2) but is an int. For it to be a tuple it would look more like (2,). If that item was a tuple then you could just do: print(sorted(list_to_sort))  So, that gives us a clue as to how to proceed. Let's assume for a moment that you want that item to be an int but would like to sort it as if it was a tuple. We can try: print(sorted(list_to_sort, key=lambda x: x if isinstance(x, tuple) else (x,)))  That will give you: [(0, 1), (1, 1, 1), (1, 2), (1, 2, 1), (1, 11), 2, (2, 0), (2, 0, 0)]