eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to check if the checkbox is selected using is_selected() method. I understand that since the element I am trying to validate is not a legit checkbox, it's not right to validate it this way. It's pretty obvious because it's wrapped in an anchor(<a>) and italic tag(<i>). I've tried different variants of XPaths such as: //input[@name='non_stop']/preceding-sibling::i //input[@name='non_stop']/parent::a //a[@title='Non Stop Flights']/i[contains(@class, 'ico ico-checkbox')] (possibly the most wrong one here)  But is there a workaround to verify this? Here's my code: driver = webdriver.Chrome() driver.get("https://www.yatra.com/") non_stop_flights = driver.find_element( By.XPATH, "//input[@name='non_stop']/preceding-sibling::i") print("Before clicking") print(non_stop_flights.is_selected()) # Expecting False here non_stop_flights.click() sleep(3) print("After clicking") print(non_stop_flights.is_selected()) # Expecting True here  But I am still getting False as the output in the last line. How to make it recognize that the checkbox has been checked?

Answer: The problem is that the non-stop INPUT isn't actually storing checked state. It's being stored in the I tag right above it but inside the A tag. <a for="BE_flight_non_stop" title="Non Stop Flights" class="custom-check"> -> <i class="ico ico-checkbox"></i> <input data-trackcategory="Home Page" data-trackaction="Booking Engine" data-trackvalue="Non Stop Flight - Checked/Unchecked" type="checkbox" name="non_stop" id="BE_flight_non_stop" class="eventTrackable js-prodSpecEvtCat"> Non Stop Flights </a>  The checked state is tracked using a class on the I tag... Not checked <i class="ico ico-checkbox"></i>  Checked <i class="ico ico-checkbox ico-checkbox-checked"></i> ^^^^^^^^^^^^^^^^^^^^ checked state  With this info, we can make a quick method to determine the checked state of these fields def is_checked(e): """ Returns a True if the checkbox is checked, False otherwise. Parameters: e (WebElement): The I tag for the checkbox """ return "ico-checkbox-checked" in e.get_attribute("class")  and then call it from your original (modified) script driver = webdriver.Chrome() driver.get("https://www.yatra.com/") non_stop_flights = driver.find_element(By.CSS_SELECTOR, "a[title='Non Stop Flights'] i") print("Before clicking") print(is_checked(non_stop_flights)) # Expecting False here non_stop_flights.click() print("After clicking") print(is_checked(non_stop_flights)) # Expecting True here  and it works as expected.
Question: There is no documentation for do this, I found it unacceptable. from msal import ConfidentialClientApplication from msgraph import GraphServiceClient client_id = '' client_secret = '' tenant_id = '' authority = f'https://login.microsoftonline.com/{tenant_id}' scopes = ['https://graph.microsoft.com/.default'] app = ConfidentialClientApplication( client_id, authority=authority, client_credential=client_secret, ) response = app.acquire_token_for_client(scopes) graph_client = GraphServiceClient( credentials=response, scopes=scopes ) await graph_client.users.get()  /usr/local/lib/python3.10/dist-packages/kiota_authentication_azure/azure_identity_access_token_provider.py in get_authorization_token(self, uri, additional_authentication_context) 101 ) 102 else: --> 103 result = self._credentials.get_token(*self._scopes, claims=decoded_claim) 104 105 if inspect.isawaitable(result): > AttributeError: 'dict' object has no attribute 'get_token' Analyzing the stack, you can see that the object passed as credentials in the `msgraph` client is not what it expects; `acquire_token_for_client` returns a dictionary, but `GraphServiceClient` expects it to have a function called "get_token."  How can this be resolved?

Answer: I registered one Entra ID application and granted User.Read.All permission of Application type as below:  Initially, I too got same error when I ran your code to fetch list of users like this: from msal import ConfidentialClientApplication from msgraph import GraphServiceClient client_id = '' client_secret = '' tenant_id = '' authority = f'https://login.microsoftonline.com/{tenant_id}' scopes = ['https://graph.microsoft.com/.default'] app = ConfidentialClientApplication( client_id, authority=authority, client_credential=client_secret, ) response = app.acquire_token_for_client(scopes) graph_client = GraphServiceClient( credentials=response, scopes=scopes ) result = await graph_client.users.get() print(result)  Response:  To resolve the error, make use of below modified code that uses client credentials flow to authenticate with MS Graph and listed users successfully: import asyncio from azure.identity import ClientSecretCredential from msgraph import GraphServiceClient tenant_id = "tenantID" client_id = "appID" client_secret = "secret" credential = ClientSecretCredential( tenant_id=tenant_id, client_id=client_id, client_secret=client_secret ) client = GraphServiceClient(credential) async def main(): result = await client.users.get() users = result.value for user in users: print("User ID:", user.id) print("User Display Name:", user.display_name) print("-" * 50) # Separating each user with a line asyncio.run(main())  Response:  References: List users - Microsoft Graph  GitHub - microsoftgraph/msgraph-sdk-python
Question: I have 2 python lists to compare. list1 = ['13.3. Risk', '13.3.1. Process', 'Change'] list2 = ['Change', '13.3. Risk', '13.3.1. Process']  I want to know how exact the order of elements is. If I go item by item, the coincidence it´s 0 since the first one fails. But if you look carefully, just fails the first element. And the rest are in order. So the coincidence, or better explained: accuracy/precision is 66.66% I have tried 3 things: Element by element coincidences= [i == j for i, j in zip(list1, list2)] percentaje= 100 * sum(coincidences) / len(list1)  This results on 0% in this example. Levenstein distance I convert list to string with join and calculate levenstein distance from Levenshtein import distance str1 = ','.join(list1) str2 = ','.join(list2) lev_dist = distance(str1, str2) percentaje= 100 * (1 - lev_dist / max(len(str1), len(str2)))  This results on 39.80582524271845% Spearman Coef from scipy.stats import spearmanr pos_list1 = {elem: i for i, elem in enumerate(list1)} range_list2 = [pos_list1 [elem] for elem in list2] coef, p_valor = spearmanr(list(range(len(list1))), rango_lista2) print(f'Spearman coef is: {coef}')  This results on -0.5 So as you see, I dont get the expected 66.66% Is there another way of doing this?

Answer: May calculate Levenshtein distance between lists itself, not their concatenations: lev_dist = distance(list1, list2) percentage = 100 * (1 - lev_dist / (len(list1) + len(list2)))  shows 66.66666666666666
Question: I got the following dataframe:  index user default_shipping_cost category shipping_cost shipping_coalesce estimated_shipping_cost 0 0 1 1 clothes NaN 1.0 6.0 1 1 1 1 electronics 2.0 2.0 6.0 2 2 1 15 furniture NaN 15.0 6.0 3 3 2 15 furniture NaN 15.0 15.0 4 4 2 15 furniture NaN 15.0 15.0  Per user, combine shipping_cost with default_shipping_cost and calculate the mean of the combined shipping_costs but only if there is at least one shipping_cost given. Explanation:  user_1 shipping_cost is given (at least once) so we can calculate the mean user_2 there are no shipping_cost, so I would like to go with Nan  Code: import pandas as pd pd.set_option("display.max_columns", None) pd.set_option("display.max_rows", None) pd.set_option('display.width', 1000) df = pd.DataFrame( { 'user': [1, 1, 1, 2, 2], 'default_shipping_cost': [1, 1, 15, 15, 15], 'category': ['clothes', 'electronics', 'furniture', 'furniture', 'furniture'], 'shipping_cost': [None, 2, None, None, None] } ) df.reset_index(inplace=True) df['shipping_coalesce'] = df.shipping_cost.combine_first(df.default_shipping_cost) dfg_user = df.groupby(['user']) df['estimated_shipping_cost'] = dfg_user['shipping_coalesce'].transform("mean") print(df)  Expected output:  index user default_shipping_cost category shipping_cost shipping_coalesce estimated_shipping_cost 0 0 1 1 clothes NaN 1.0 6.0 1 1 1 1 electronics 2.0 2.0 6.0 2 2 1 15 furniture NaN 15.0 6.0 3 3 2 15 furniture NaN 15.0 NaN 4 4 2 15 furniture NaN 15.0 NaN 

Answer: Add an extra condition with transform('any') and where: df['estimated_shipping_cost'] = (dfg_user['shipping_coalesce'].transform('mean') .where(dfg_user['shipping_cost'].transform('any')) )  Output:  index user default_shipping_cost category shipping_cost shipping_coalesce estimated_shipping_cost 0 0 1 1 clothes NaN 1.0 6.0 1 1 1 1 electronics 2.0 2.0 6.0 2 2 1 15 furniture NaN 15.0 6.0 3 3 2 15 furniture NaN 15.0 NaN 4 4 2 15 furniture NaN 15.0 NaN  Intermediate: dfg_user['shipping_cost'].transform('any') 0 True 1 True 2 True 3 False 4 False Name: shipping_cost, dtype: bool
Question: I'm trying to use Llama 2 chat (via hugging face) with 7B parameters in Google Colab (Python 3.10.12). I've already obtain my access token via Meta. I simply use the code in hugging face on how to implement the model along with my access token. Here is my code: !pip install transformers from transformers import AutoModelForCausalLM, AutoTokenizer import torch token = "---Token copied from Hugging Face and pasted here---" tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf", token=token) model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf", token=token)  It starts downloading the model but when it reaches Loading checkpoint shards: it just stops running and there is no error: 

Answer: The issue is with Colab instance running out of RAM. Based on your comments you are using basic Colab instance with 12.7 Gb CPU RAM. For LLama model you'll need:  for the float32 model about 25 Gb (but you'll need both cpu RAM and same 25 gb GPU ram); for the bfloat16 model around 13 Gb (and still not enough to fit basic Colab Cpu instance, given that you'll also need to store intermediate calculations from the model);  Check this link for the details on the required resources: huggingface.co/NousResearch/Llama-2-7b-chat-hf/discussions/3 Also if you want only to do inference (predictions) on the model I would recommend to use it's quantized 4bit or 8bit versions. Both can be ran on CPU and don't need a lot of memory.
Question: I have an imported .csv of number values- I want to sort the dataframe so I end up a list showing how many times each value occurs across the whole dataframe. E.g. 1: 5 2: 0 3: 23 4: 8  I have found how to count the values of a specified column, but I can't find a way to do the same thing for the entire dataframe- I could count the values of each column and then combine them afterward but it is a bit clunky and I was looking for a more elegant solution. This is an example of what I was trying: sort_dataframe = df.select(pl.col("1", "2", "3", "4", "5", "6", "7").value_counts())  Which results in: polars.exceptions.ComputeError: Series length 16 doesn't match the DataFrame height of 26 

Answer: TLDR. You can use value_counts after unpivoting the dataframe into a long format. df.unpivot().get_column("value").value_counts()  Explanation Let us consider the following example dataframe. import polars as pl df = pl.DataFrame({ "col_1": [1, 2, 3], "col_2": [2, 3, 7], "col_3": [1, 1, 9], })  shape: (3, 3) ┌───────┬───────┬───────┐ │ col_1 ┆ col_2 ┆ col_3 │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═══════╪═══════╪═══════╡ │ 1 ┆ 2 ┆ 1 │ │ 2 ┆ 3 ┆ 1 │ │ 3 ┆ 7 ┆ 9 │ └───────┴───────┴───────┘  First, we can unpivot the dataframe using pl.DataFrame.unpivot to obtain a single column containing all values. df.unpivot()  shape: (9, 2) ┌──────────┬───────┐ │ variable ┆ value │ │ --- ┆ --- │ │ str ┆ i64 │ ╞══════════╪═══════╡ │ col_1 ┆ 1 │ │ col_1 ┆ 2 │ │ col_1 ┆ 3 │ │ col_2 ┆ 2 │ │ col_2 ┆ 3 │ │ col_2 ┆ 7 │ │ col_3 ┆ 1 │ │ col_3 ┆ 1 │ │ col_3 ┆ 9 │ └──────────┴───────┘  Finally, we can get the value column as a pl.Series and use pl.Series.value_counts to count the number of occurrences of each value. counts = df.melt().get_column("value").value_counts()  shape: (5, 2) ┌───────┬───────┐ │ value ┆ count │ │ --- ┆ --- │ │ i64 ┆ u32 │ ╞═══════╪═══════╡ │ 7 ┆ 1 │ │ 3 ┆ 2 │ │ 9 ┆ 1 │ │ 2 ┆ 2 │ │ 1 ┆ 3 │ └───────┴───────┘  This can also be simply converted to a python dictionary. dict(counts.iter_rows())  {3: 2, 7: 1, 1: 3, 2: 2, 9: 1}
Question: I am adapting my pip commands to newer versions of Ubuntu (that support PEP 668). Out of the options, the only one worked so far (in my specific use case) is to  Use --break-system-packages at the end of pip  as indicated in this answer. That is, change sudo pip install xyz  to sudo pip install xyz --break-system-packages  . This worked for the newer versions of Ubuntu but causes an error in older versions of Ubuntu (22.04 LTS) that do not recognize the --break-system-packages option. The error message from pip is:  no such option: --break-system-packages  pip --version shows:  pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)  How can one add conditions around pip commands so that it uses the --break-system-packages option only when the pip/python version is high enough to recognize it?

Answer: You can set an environment variable instead of using --break-system-packages. PIP_BREAK_SYSTEM_PACKAGES=1 pip install xyz should work in both new and older versions of pip
Question: I try to authenticate the GEE, the kernel keeps running, but authenticating box does not show to paste the authentication code in (picture below, but no authentication box showed). Anyone has similar experience? 

Answer: You should add the authentication code to the box appearing on top of VSCode. This is the same box displayed under "the authorization workflow will generate a code which you should paste in the box below" in Jupyter.
Question: I'm using polars.testing assert_frame_equal method to compare two sorted dataframes containing same columns and below is my code: assert_frame_equal(src_df, tgt_df, check_dtype=False, check_row_order=False)  For a dataframe containing 5 million records, it takes long time to report a failure as it compares all the rows between two dataframes. Is there a way that we can make polars to fail immediately and report on first mismatch/failure and stop the execution as we just need to know the first failure. I tried searching through and i'm unable to find any documentation for this requirement. Can someone please help me on this?

Answer: The polars.testing.* methods call .to_list() when reporting differences.  https://github.com/pola-rs/polars/blob/main/py-polars/polars/testing/asserts/frame.py#L128-L130  I've found this to be a source of significant slowdown when larger data is involved. If you also want error reporting, it seems you need to resort to doing it manually. .arg_true() can be used as part of getting the index of the first mismatch. a = pl.Series(["a", "b", "c", "d"]) b = pl.Series(["a", "b", "e", "f"]) a.ne_missing(b).arg_true()  shape: (2,) Series: '' [u32] [ 2 3 ]  You can refer to the implementation for the other pre-checks performed, but you could do something similar to: import polars as pl N = 5 src_df = pl.DataFrame({ "a": range(N), "b": list(range(N - 2)) + [42, 42] }).sort(pl.all()) tgt_df = pl.DataFrame({ "a": range(N), "b": range(N) }).sort(pl.all()) """ Insert other equality pre-checks here """ for col in src_df: try: idx = col.ne_missing(tgt_df[col.name]).arg_true().head(1).item() print("LEFT:", src_df[idx]) print("RIGHT:", tgt_df[idx]) break except ValueError: pass  LEFT: shape: (1, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 3 ┆ 42 │ └─────┴─────┘ RIGHT: shape: (1, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 3 ┆ 3 │ └─────┴─────┘
Question: I have the code: loader = PyPDFLoader(“https://arxiv.org/pdf/2303.08774.pdf”) data = loader.load() docs = text_splitter1.split_documents(data) vector_search_index = “vector_index” vector_search = MongoDBAtlasVectorSearch.from_documents( documents=docs, embedding=OpenAIEmbeddings(disallowed_special=()), collection=atlas_collection, index_name=vector_search_index, ) query = "What were the compute requirements for training GPT 4" results = vector_search1.similarity_search(query) print("result: ", results)  And in results I have every time only empty array. I don't understand what I do wrong. This is the link  on the langchain documentation with examples. Information is saved normally in database, but I cannot search info in this collection.

Answer: So I was able to get this to work in MongoDB with the following code: text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150) loader = PyPDFLoader("https://arxiv.org/pdf/2303.08774.pdf") data = loader.load() docs = text_splitter.split_documents(data) DB_NAME = "langchain_db" COLLECTION_NAME = "atlas_collection" ATLAS_VECTOR_SEARCH_INDEX_NAME = "vector_index" MONGODB_ATLAS_CLUSTER_URI = uri = os.environ.get("MONGO_DB_ENDPOINT") client = MongoClient(MONGODB_ATLAS_CLUSTER_URI) MONGODB_COLLECTION = client[DB_NAME][COLLECTION_NAME] vector_search = MongoDBAtlasVectorSearch.from_documents( documents=docs, embedding=OpenAIEmbeddings(disallowed_special=()), collection=MONGODB_COLLECTION, index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME, ) query = "What were the compute requirements for training GPT 4" results = vector_search.similarity_search(query) print("result: ", results)  At this point, I did get the same results that you did. Before it would work, I had to create the vector search index and I made sure it was named the same as what is specified in ATLAS_VECTOR_SEARCH_INDEX_NAME:  FWIW - It was easier for me to do in Astra DB (I tried this first, because I am a DataStax employee): text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150) loader = PyPDFLoader("https://arxiv.org/pdf/2303.08774.pdf") data = loader.load() docs = text_splitter.split_documents(data) atlas_collection = "atlas_collection" ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT") ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN") vector_search = AstraDBVectorStore.from_documents( documents=docs, embedding=OpenAIEmbeddings(disallowed_special=()), collection_name=atlas_collection, api_endpoint=ASTRA_DB_API_ENDPOINT, token=ASTRA_DB_APPLICATION_TOKEN, ) query = "What were the compute requirements for training GPT 4" results = vector_search.similarity_search(query) print("result: ", results)  Worth noting, that Astra DB will create your vector index automatically based on the dimensions of the embedding model.
Question: I have a problem with the hr.employee.public model. If I try to add a new field Char, Integer, Many2one, etc. I have no issues. However, if I try to insert a Binary field like this employee_signature = fields.Binary(string='Employee signature', attachment=True, store=True) I always get the following error: psycopg2.errors.UndefinedColumn: column emp.employee_signature does not exist LINE 3: ...l,emp.priv_email,emp.employee_file_name_signature,emp.employ...  Where am I going wrong? What could be the problem?

Answer: If you inherit hr.employee.public to add a stored field (not x2many fields), you should see the same error message because of the init function which tries to get fields from hr.employee You don't need to set the store attribute to True because it's the default value (if you set store to False, you will not be able to save the attachment). When the attachment attribute is set to True (the default value), Odoo will set the column type of the field to None and ignore updating the database schema To fix this issue, you can use the same logic as image_1920 field (which is an extended Binary field) and add the field to the hr.employee model Example: class HrEmployee(models.Model): _inherit = 'hr.employee' employee_signature = fields.Binary(string='Employee signature') class HREmployeePublic(models.Model): _inherit = "hr.employee.public" employee_signature = fields.Binary(compute="_compute_employee_signature", compute_sudo=True) def _compute_employee_signature(self): for employee in self: employee_id = self.sudo().env['hr.employee'].browse(employee.id) employee.employee_signature = employee_id.employee_signature  In the example above the employee signature attachment attribute is set to True and Odoo will not create a column in the employee table, this is not a problem because the same field in the public profile is a non-stored computed field so Odoo will not try to get the field from employee table For more details, check  [IMP] hr: Introduce the public employee profile  commit
Question: How to store a 2D numpy ndarray in raw binary format? It should become a raw array of float32 values, in row-major order, no padding, without any headers. According to the documentations, ndarray.tofile() can store it as binary or textual, but the format argument is a string to textual formatting. And np.save() saves it in .npy format.

Answer: with open('out', 'wb') as f: f.write(arr.tobytes())  should do. You may have to add a astype(np.float32) if arr is not already a float32 array. Likewise, if the array is not already in "row major order", you may have to add a .T somewhere. And of course (but I take you know that very well, if you want to dump binary representation) you need to be aware of little/big endian order. EDIT: Since you've mentioned tofile', I looked at it. Indeed, that the same thing. Just don't pass any format (otherwise, indeed, it is a text file) with open('out', 'wb') as f: arr.tofile(f)  Not that it is significantly shorter. But it also works. And the doc says explicitly that it does exactly (when no sep, no format is passed) what my 1st solution does.
Question: I'm attempting to solve a very simple IVP in Python in order to do error analysis: import numpy as np from scipy.integrate import solve_ivp def dh_dt_zerodriver(t, h): return -2 / h t = 50 steps = 10 dt = t / steps t_span = [0, t] t_eval = np.arange(0, t + dt, dt) h0 = 5 sol = solve_ivp(dh_dt_zerodriver, t_span=t_span, y0=[h0], t_eval=t_eval)  However, the solution will not compute as it runs for an indefinite amount of time. I have used solve_ivp to solve more complex IVPs without analytical solutions, but this one seems to have me stumped despite it seemingly being a fairly simply problem. My paper is based on using the RK45 method, but if I must use some different numerical method then it is okay.

Answer: The problem is the domain you want your solution. Your ODE is h'(t) = -2/h(t) where h(0) = 5. The analytical solution is h(t) = sqrt(25 - 4t). For t < 6.25, the solution is real, but for t >= 6.25, the solution becomes complex. If you were to instead integrate over t = [0, 6], you would quickly get the correct result.