eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to build a golang cli tool for my company and as part of that build login and some other features into the tool. For the life of me I can't figure out how AWS is able to open a browser window and wait for a few button clicks before proceeding from the CLI. https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_StartDeviceAuthorization.html Here's the CLI command I input aws sso login --profile login Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-east-1.amazonaws.com/ Then enter the code: abcd-efgh Successfully logged into Start URL: https://d-1421421423.awsapps.com/start  Here's the Python docs as well for start device auth and create token https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sso-oidc/client/start_device_authorization.html https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sso-oidc/client/create_token.html

Answer: One option that I just threw together that seems to be working is a loop that just checks every second  for attempts <= 30 { fmt.Println(attempts) token, err := idc.CreateToken(context.TODO(), &createTokenInput) if err != nil { // if debug is enabled show error log.Debug(err.Error()) attempts++ // wait 1 second time.Sleep(1 * time.Second) } else { response = *token break } }  Edit: After running AWS sso login —debug I noticed that the logs are actually looping and running the createToken query over and over, so AWS is doing something similar to the above.
Question: I was writing the most simplest piece of code to run some small app. I got the next warning message: ~\PycharmProjects\LoggerTest\main.py:10: DeprecationWarning: sipPyTypeDict() is deprecated, the extension module should use sipPyTypeDictRef() instead class MainWindow(QMainWindow):  My code: # Import libraries import sys # from PyQt5 import QtGui # from PyQt5.QtCore import QEvent from PyQt5.QtWidgets import QApplication, QMainWindow # from PyQt5.QtCore import pyqtSignal #, pyqtSlot from gui_ui import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self, parent=None, **kwargs): super(MainWindow, self).__init__(parent=parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.show() if __name__ == '__main__': app = QApplication(sys.argv) g = MainWindow() app.exec_()  What does that warning mean?

Answer: It is solved in python-3.12.0 After upgrading, the warning should be away. Ref: https://github.com/python/cpython/pull/105747
Question: How can you filter a DataFrame in pandas by selecting rows that fall within the 10th and 90th percentiles based on a specific column and then remove these filtered rows from the original DataFrame? Code Example: # Create a sample DataFrame data = { 'A': [0, 0, 0, 0, 0, 172, 183, 92, 105, 120], 'B': [3, 14, 27, 33, 41, 55, 66, 75, 88, 92] } df = pd.DataFrame(data) # Filter the DataFrame based on the 10th and 90th percentiles of column 'A' q10 = df['A'].quantile(0.1) q90 = df['A'].quantile(0.9) filtered_df = df[(df['A'] > q10) & (df['A'] < q90)]  Issue: filtered_df doesn't really remove entry below 10th percentile and above 90th percentile. EDIT: Output should have 8 rows i.e. 80 percentile of the data.

Answer: Using percentiles won't guarantee having a fixed number of rows. It rather looks like you want to take 80% of the ranked values. Using rank with 'first' and between: m = (df['A'] .rank(method='first').sub(1) .between(0.1*len(df), 0.9*len(df), inclusive='left') ) out = df[m]  Output:  A B 1 0 14 2 0 27 3 0 33 4 0 41 5 172 55 7 92 75 8 105 88 9 120 92  Intermediates:  A B rank m 0 0 3 1.0 False 1 0 14 2.0 True 2 0 27 3.0 True 3 0 33 4.0 True 4 0 41 5.0 True 5 172 55 9.0 True 6 183 66 10.0 False 7 92 75 6.0 True 8 105 88 7.0 True 9 120 92 8.0 True
Question: I'm new to Django and wanted to ask how can I solve a problem that I have class InventoryId(models.Model): inventory_id = models.AutoField(primary_key=True) class Laptop(models.Model): name = models.CharField(max_length=30) inventory_id = models.ForeignKey(InventoryId, on_delete=models.CASCADE) class Desktop(models.Model): name = models.CharField(max_length=30) inventory_id = models.ForeignKey(InventoryId, on_delete=models.CASCADE)  In the example above I would like models Laptop and Desktop to use inventory_id's that are stored in the same table and are incremental to avoid collisions. example: laptop1 --> inventory_id = 1 desktop1 --> inventory_id = 2 laptop2 --> inventory_id = 3  what's the correct way of doing that in django? I found something that recommends using signals to do that. but i was discouraged by documentation that it may be not the best idea

Answer: Use model inheritance [Django-doc] instead: class InventoryItem(models.Model): inventory_id = models.AutoField(primary_key=True) class Laptop(InventoryItem): name = models.CharField(max_length=30) class Desktop(InventoryItem): name = models.CharField(max_length=30) This will make OneToOneFields from Laptop and Desktop to the table of the InventoryItem. It also guarantees that two Desktops for example can not refer to the same `InventoryItem. Since you here, perhaps as an example, have two name fields with the same specifications, it makes sense to "pull up" this field to the InventoryItem model.
Question: I have a dataframe with 2 columns (ID, Contents) which I am saving in pickle format. Say for example, I have 10 rows(ie 0 to 9 ID). I am trying to load a single row by passing ID (valid ID between 0-9) from the pickle file without loading the entire file. import pandas as pd import pickle id = 2 path = "contents.pickle" # dummy data df = pd.DataFrame({ "ID": [0,1,2,3,4,5,6,7,8,9], "Contents": [0,1,2,3,4,5,6,7,8,9] }) print(f"{df=}") df.to_pickle(path=path) df = pd.read_pickle(filepath_or_buffer=path) with open(path, "rb") as handle: df = pickle.load(handle)  The above both are reading all the data in one go. I am not getting where I should pass the ID to load that row only. If not possible with dataframe, I tried with simple list containing data saved in pickle file using reference here but failed. data = [0,1,2,3,4,5,6,7,8,9] with open(path, "wb") as handle: pickle.dump(data, handle) file = open(file=path) file.seek(id) row = pickle.load(file) print(f"{row=}") 

Answer: Unfortunately I think pickle is not the right tool to do that (but maybe I'm wrong). Try a column-oriented data file format like parquet and pd.read_parquet: df.to_parquet('results.parquet') row = pd.read_parquet('results.parquet', filters=[('ID', '=', 9)])  Output: >>> row ID Contents 0 9 9  Update With pyarrow and ParquetDataset import pyarrow.parquet as pq ds = pq.ParquetDataset('results.parquet', filters=[('ID', '=', 9)])  Output: >>> ds.read() # read to get the row without load the entire file pyarrow.Table ID: int64 Contents: int64 ---- ID: [[9]] Contents: [[9]]
Question: Here's a minimal example I've written: from __future__ import annotations from typing import Protocol from typing_extensions import Self class Cat(Protocol): def add(self, other: Self) -> Self: ... def sub(self, other: Self) -> Self: ... class FancyCat(Cat): def __init__(self, value: int): self._value = value def add(self, other: Self) -> Self: return FancyCat(self._value + other._value) def sub(self, other: Self) -> Self: return FancyCat(self._value - other._value) fc = FancyCat(3) fc2 = FancyCat(4) fc.add(fc2)  If I try to type check it, I get $ mypy t.py t.py:19: error: Incompatible return value type (got "FancyCat", expected "Self") [return-value] t.py:22: error: Incompatible return value type (got "FancyCat", expected "Self") [return-value] Found 2 errors in 1 file (checked 1 source file)  I'm so confused - isn't Self FancyCat in this case? How can I satisfy mypy here?

Answer: Your code does not guarantee that Self will be returned. For example, child classes will still return FancyCat, therefore Self annotation is invalid. To get rid of errors, you can either: a) make sure it's always self.__class__ returned, which IS actual Self def add(self, other: Self) -> Self: return self.__class__(self._value + other._value)  b) type with what you are actually returning, FancyCat def sub(self, other: Self) -> FancyCat: return FancyCat(self._value - other._value)
Question: I know that to activate a virtual environment with pyenv, we can run pyenv activate [virtualenv], but here, i also saw that we can "select" a virtual environment by running pyenv local [virtualenv]. So what is the difference between the two ways?

Answer: The base command is pyenv local <python_version>. It selects what Python version to use in the current directory or its subdirectories. pyenv activate <name> activates a Python virtual environment. pyenv local <venv-name> works after pyenv virtualenv venv-name and eval "$(pyenv virtualenv-init -)" in shell configuration. After the command the <venv-name> (a virtual environment known to pyenv) is activated when entering (with cd) the directory where pyenv local <venv-name> had been run.
Question: I am using a Python library that has one function that I would need to customize. That is not a problem, since I can make my own edited version like this: from coolLibrary import originalclass class myclass(originalclass): whatever...  The problem that I am facing now, is that the library uses this originalclass in multiple locations. Is there a way to tell the library to use myclass everywhere instead of originalclass? My goal would be to have a custom code without editing the original code so the library can be updated.

Answer: If coolLibrary.originalclass has explicit references to itself: # coolLibrary.py class originalclass: def foo(self): print('original foo') def bar(self): originalclass.foo(self)  such that: # main.py import coolLibrary class myclass(coolLibrary.originalclass): def foo(self): print('custom foo') myclass().bar()  outputs: original foo  You can patch coolLibrary.originalclass with your custom class after the custom class is defined: coolLibrary.originalclass = myclass  so that: myclass().bar()  would then output: custom foo  Demo: https://replit.com/@blhsing1/JollyHumiliatingTabs Note that patching the original class makes the behavior change global. If another module that also uses coolLibrary.originalclass depends on its original behavior in order to work, you may want to apply the patch to only the block of code that needs such a custom behavior. This can be done with the unittest.mock.patch context manager: from unittest.mock import patch with patch('coolLibrary.originalclass', myclass): myclass().bar()
Question: I have the following dataframe and I want to transpose it so that the elements in column "Trans_Type" become new columns to represent the values from column "Trans_amount". import pandas as pd import numpy as np df = pd.DataFrame([['21/10/2023','1','CR',2323], ['21/10/2023','1','CR',23], ['21/10/2023','1','DR',65], ['21/10/2023','2','CR',3.3], ['21/10/2023','3','CR',56], ['21/10/2023','3','DR',23.66], ['21/10/2023','4','CR',54.34], ['22/10/2023','4','CR',23.34], ['22/10/2023','4','DR',5.5]], columns = ['Date','Account_Number','Trans_Type','Trns_Amount']) df  DF Output:  Date Account_Number Trans_Type Trns_Amount 0 21/10/2023 1 CR 2323.00 1 21/10/2023 1 CR 23.00 2 21/10/2023 1 DR 65.00 3 21/10/2023 2 CR 3.30 4 21/10/2023 3 CR 56.00 5 21/10/2023 3 DR 23.66 6 21/10/2023 4 CR 54.34 7 22/10/2023 4 CR 23.34 8 22/10/2023 4 DR 5.5  Expected Output:  Date Account_Number CR DR 0 21/10/2023 1 2323.00 NaN 1 21/10/2023 1 23.00 NaN 2 21/10/2023 1 NaN 65.00 3 21/10/2023 2 3.30 NaN 4 21/10/2023 3 56.00 NaN 5 21/10/2023 3 NaN 23.66 6 21/10/2023 4 54.34 NaN 7 22/10/2023 4 23.34 NaN 8 22/10/2023 4 NaN 5.50  Any assistance would be great. Thanks Alan

Answer: You can use DataFrame.reset_index for helper column with unique values, so possible use DataFrame.pivot for reshape: out = (df.reset_index() .pivot(index=['index','Date','Account_Number'], columns='Trans_Type', values='Trns_Amount') .reset_index(level=[1,2])) print (out) Trans_Type Date Account_Number CR DR index 0 21/10/2023 1 2323.00 NaN 1 21/10/2023 1 23.00 NaN 2 21/10/2023 1 NaN 65.00 3 21/10/2023 2 3.30 NaN 4 21/10/2023 3 56.00 NaN 5 21/10/2023 3 NaN 23.66 6 21/10/2023 4 54.34 NaN 7 22/10/2023 4 23.34 NaN 8 22/10/2023 4 NaN 5.50
Question: I am trying to write a program similar to a blind auction, but I am having issues with the while loop. I don't understand why the loop is not ending. I used Thonny and even after the player enters 'no', it seems like the loop is breaking since it prints what is outside the loop and then goes to the bid function inside the while loop, with the player value set to 'yes'. I don't understand why this is happening. I have just started learning Python, so I would appreciate any help in explaining why this is occurring. Thank you! def bid(player_name,player_bid): player_name = input("What's your name?\n") player_bid = input("What's your bid? PLN") bid_list[player_name] = player_bid while True: player = input("Are they any other users? Insert yes or no?") if player == "no": player = "no" break bid(player_name,player_bid) max_value = max(bid_list.values()) print(f"{player_name} has the highest bid - {max_value} ") print("Thank you for playing") player_name = "" player_bid = 0 bid_list = {} bid(player_name,player_bid)  I did try to change the code many times and used ChatGPT but still I don't understand why my code is not working. After I switched some part in while loop the code is working but I want to understand why my code is not working and why the loop is not ending.

Answer: Don't use recursion as a substitute for looping. Put your while loop around all the code that asks for player names and bids. Your recursion is outside the while loop, so it occurs every time and you never get out of the loop. There's also no need for player_name and player_bid to be function parameters, since the function assigns those variables itself from user input. You could instead pass in bid_list as a parameter. Your max() call gets the maximum bid, but not the player that made that bid. Get the maximum of bid_list.items() which returns both the keys and values. def bid(bid_list): while True: player_name = input("What's your name?\n") player_bid = input("What's your bid? PLN") bid_list[player_name] = player_bid player = input("Are they any other users? Insert yes or no?") if player == "no": break max_player, max_value = max(bid_list.items(), key=lambda x: x[1]) print(f"{max_player} has the highest bid - {max_value} ") print("Thank you for playing") bid_list = {} bid(bid_list)
Question: I need to convert the following CRC computation algorithm to Python: #include <stdio.h> unsigned int Crc32Table[256]; unsigned int crc32jam(const unsigned char *Block, unsigned int uSize) { unsigned int x = -1; //initial value unsigned int c = 0; while (c < uSize) { x = ((x >> 8) ^ Crc32Table[((x ^ Block[c]) & 255)]); c++; } return x; } void crc32tab() { unsigned int x, c, b; c = 0; while (c <= 255) { x = c; b = 0; while (b <= 7) { if ((x & 1) != 0) x = ((x >> 1) ^ 0xEDB88320); //polynomial else x = (x >> 1); b++; } Crc32Table[c] = x; c++; } } int main() { unsigned char buff[] = "whatever buffer content"; unsigned int l = sizeof(buff) -1; unsigned int hash; crc32tab(); hash = crc32jam(buff, l); printf("%d\n", hash); }  two (failed) attempts to rewrite this in python follow: def crc32_1(buf): crc = 0xffffffff for b in buf: crc ^= b for _ in range(8): crc = (crc >> 1) ^ 0xedb88320 if crc & 1 else crc >> 1 return crc ^ 0xffffffff def crc32_2(block): table = [0] * 256 for c in range(256): x = c b = 0 for _ in range(8): if x & 1: x = ((x >> 1) ^ 0xEDB88320) else: x >>= 1 table[c] = x x = -1 for c in block: x = ((x >> 8) ^ table[((x ^ c) & 255)]) return x & 0xffffffff data = b'whatever buffer content' print(crc32_1(data), crc32_2(data))  Using the three routines on thee exact same data yield three different results: mcon@cinderella:~/Desktop/3xDAsav/DDDAedit$ ./test5 2022541416 mcon@cinderella:~/Desktop/3xDAsav/DDDAedit$ python3 test5.py 2272425879 2096952735  As said: C code is "Golden Standard", how do I fix this in Python? Note: I know I can call C routines from Python, but I consider that as "last resort".

Answer: Instead of porting your own CRC32 implementation, you can use one from the Python standard library. For historic reasons, the standard library includes two identical1 CRC32 implementations:  binascii.crc32 zlib.crc32  Both implementations match the behavior of your crc32_1 function: import binascii import zlib >>> print(binascii.crc32(b'whatever buffer content')) 2272425879 >>> print(zlib.crc32(b'whatever buffer content')) 2272425879  To get a result matching the C implementation from the question, you just need to apply a constant offset: >>> 0xffff_ffff - zlib.crc32(b'whatever buffer content') 2022541416  As a bonus, these CRC32 functions are implemented in efficient C code, and will be much faster than any equivalent pure-Python port.  1Note that the zlib module is only available when CPython is compiled with zlib support (which is almost always true). In the off chance that you're using a CPythion build without zlib, you won't be able to use the zlib module. Instead, you can use the binascii implementation, which uses zlib when available and defaults to an "in-house" implementation when its not.
Question: import requests import json url = "https://betkarma.com/api/propsComparison?startDate=2023-10-17&endDate=2023-10-23&league=nfl" response = requests.get(url) data = response.json() print(response)  for player_name in data["games"][0]["offers"][0]["player"]: if offers[0] == "player": value = player["value"] break print(player_name)  The above code appears to be correct; however, something certainly is missing in the code below. I'm expecting the first and last name of each participant, but I'm just getting the first letter of the first person's first name returned for some reason. Any assistance is highly appreciated. I'm not sure whether I'm not outputting the correct thing, if some code is missing, or what I need to do to get a list of names. I'm I'm a noob in python!

Answer: try this: import requests url = "https://betkarma.com/api/propsComparison?startDate=2023-10-17&endDate=2023-10-23&league=nfl" response = requests.get(url) data = response.json() for p in data.get("games")[0].get("offers"): print(p["player"])  output: Foye Oluokun Foye Oluokun Michael (Saints) Thomas Michael (Saints) Thomas Pete Werner Pete Werner Demario Davis Demario Davis Marshon Lattimore Demario Davis Marshon Lattimore Pete Werner Marshon Lattimore Alontae Taylor Alontae Taylor Alontae Taylor Carl Granderson Carl Granderson Carl Granderson Jaguars DST Saints DST Blake Grupe Brandon McManus Blake Grupe Blake Grupe Brandon McManus Brandon McManus Brandon McManus Blake Grupe Devin Lloyd Devin Lloyd Devin Lloyd Foyesade Oluokun Derek Carr Trevor Lawrence Trevor Lawrence Derek Carr Derek Carr Derek Carr Trevor Lawrence Derek Carr Trevor Lawrence Trevor Lawrence Derek Carr Derek Carr Derek Carr Trevor Lawrence Derek Carr Derek Carr Taysom Hill Trevor Lawrence Trevor Lawrence Alvin Kamara Travis Etienne Jr. Travis Etienne Jr. Travis Etienne Jr. Alvin Kamara Tank Bigsby Alvin Kamara Alvin Kamara Travis Etienne Jr. Travis Etienne Jr. Alvin Kamara Travis Etienne Jr. Travis Etienne Jr. Alvin Kamara Alvin Kamara Travis Etienne Jr. Alvin Kamara Travis Etienne Travis Etienne Travis Etienne Travis Etienne Travis Etienne Travis Etienne Travis Etienne Travis Etienne Andre Cisco Andre Cisco Rayshawn Jenkins Marcus Maye Marcus Maye Rayshawn Jenkins Rayshawn Jenkins Andre Cisco Marcus Maye Evan Engram Foster Moreau Foster Moreau Foster Moreau Evan Engram Evan Engram Evan Engram Taysom Hill Rashid Shaheed Michael Thomas Chris Olave Calvin Ridley Christian Kirk Michael Thomas Chris Olave Rashid Shaheed Rashid Shaheed Calvin Ridley Christian Kirk Calvin Ridley
Question: I am writing a script that is supposed to read a lot of csv files and transform data in a specific column, when the condition is being met. To do this I have a for-loop that looks like this: for index, transformed_row in transformed_data.iterrows(): new_rows = [] if transformed_row['Code'] in ['14/9', '14+9', '149']: transformed_row['Code'] = '18' elif transformed_row['Code'] == "3+4+1": transformed_row['Code'] = '17'  However I read in the Pandas documentation that when using .iterrows() one should not modify the DataFrame. Even though this code works and my output DF is exactly the way I want, I fear that over 50+ csv files this will not always work as stated in the documentation. So my idea was to make a new list new_rows = []  and adding the changed rows there with something like this new_rows.append(transformed_row.copy())  however, when I do this, I get duplicates of the rows I want to transform. I cannot find a solution on how to do this properly...

Answer: I think the simpler and better performing option is to use Series.replace (link): replace_dict = {"14/9": "18", "14+9": "18", "149": "18", "3+4+1": "17"} transformed_data["Code"] = transformed_data["Code"].replace(replace_dict)
Question: I was able to easily deploy to GitHub Actions for 1st generation Google Cloud Functions, but now with 2nd generation, I get authentication errors. How can I set up a GitHub workflow to deploy my function when I merge or push to my main branch? Here is the workflow I was using before  - id: "deploy" uses: "google-github-actions/deploy-cloud-functions@v0" with: name: "<cloud-function-name>" runtime: "python310" region: "us-east1" entry_point: "<function-in-main-file>" timeout: 540 service_account_email: PROJECT@appspot.gserviceaccount.com ingress_settings: ALLOW_ALL max_instances: 1 

Answer: I got this working from a comment on this GitHub issue. Their comment is for a Node function. The code I offer below is for Python, but it's the same for all of them. You'll need a service account json key and the yaml that uses it - and that's it! Service Account  Create a service account in the project via the GCP console  Give it Cloud Functions Developer and Service Account User permission (source)   Click into the service account -> Keys -> Add Key -> Create JSON key Copy the JSON Now go to your GitHub repo settings -> "Secrets and variables" (Sidebar) -> Actions Click "New repository secret"  Name: GCP_SA_DEPLOY_KEY (or whatever you want, it goes in the yaml below) Secret: Paste the JSON service account key    GitHub Action Create .github/workflows/deploy-python-src.yml in your repo (name it whatever you want). Update the deploy command in the code below (ask ChatGPT if you need help figuring it out). Make sure it works by running it locally before testing the action. name: Deploy Cloud Functions run-name: 🚀 ${{ github.actor }} is deploying all cloud functions on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: ls - id: 'auth' name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@v1' with: credentials_json: '${{ secrets.GCP_SA_DEPLOY_KEY }}' - name: 'Set up Cloud SDK' uses: 'google-github-actions/setup-gcloud@v1' with: version: '>= 363.0.0' - name: 'Use gcloud CLI' run: 'gcloud info' - name: 'Deploy to gen2 cloud function' run: | gcloud functions deploy <cloud-function-name> \ --gen2 \ --region=us-east1 \ --runtime=python310 \ --source=<source-folder-in-repo> \ --entry-point=<function-in-main-file> \ --trigger-topic=<topic-if-pub-sub>  ⚠️Notes⚠️  If you get errors related to Cloud Run, you can see the specific issue in the Cloud Run GCP console. The package that handles the auth advises against Service Accounts and provides another option (Workload Identity Federation). Read their README for specifics to know if it's secure in your scenario.