eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have this exercise to do: Let M be a positive integer, and V = ⟨v1, . . . , vn⟩ an ordered vector where the value of item vi is 5×i. Present an O(log(n)) algorithm that returns the maximum number of items from V that can be selected given that the sum of the selected items is less than or equal to M (repeated selection of items is not allowed). First I did a naive solution where:  I know the sum of elements on the array will be always less than the M/5 index on the array. So a did for i=0..i<=M/5 and found the sum. Moreover this is not O(log(n)) because given a big M, bigger than the sum of all elements on the array, it will be O(n).  Therefore I tried divide and conquer, I thought a binary search should be the way. But actually no because if I do that I will sum the minimum elements that can be chosen to arrive in M, not the maximum. My code is below  def max_items_selected_recursive2(M, V, left, right, max): if len(V[left:right]) == 1: return max mid = math.floor((left+right)/2) if V[mid] >= M: return max_items_selected_recursive2(M - V[mid], V, mid + 1, right, max+1) else: if M - V[mid] >= 0: return max_items_selected_recursive2(M - V[mid], V, left, mid - 1, max+1) else: return max_items_selected_recursive2(M, V, left, mid - 1, max)  example of call M = 20 V = [0, 5, 10, 15, 20] max_items_selected_recursive2(M, V, 0, len(V) - 1, 0) +1 # +1 since all have the O element  Any ideas on how to do this on O(log n)?

Answer: The sum 1 + 2 + ... + n = n * (n+1) / 2. The sum 5 + 10 + ... + 5n = 5 * (1 + 2 + ... + n) = 5 * n * (n+1) / 2. So given an M, we want to solve for n so that 5 * n * (n+1) / 2 <= M. Then, add 1 to this to account for zero. You can use the quadratic formula for an O(1) (which is also O(log n)) solution, or binary search on n for an O(log n) solution.
Question: I have the following code: @app.route("/", methods=["GET", "POST"]) def login(): if request.method == "GET": return render_template("login.html") elif request.method == "POST": username, password = request.form.get("username"), request.form.get("password") if validate_login(username, password): return redirect("/home.html") else: return render_template("login.html", login_message="Invalid username or password!")  with the following home.html file: {% extends "layout.html" %} {% block body %} Hello {{ username }}, your password is {{ password }}! {% endblock %}  The point here is that I am finding some ways to pass in variables to home.html using the redirect function (just like the last line of the code render_template), but after some intense google searches, I still can't find a way to do so. Do anyone know how to do this?

Answer: I'm assuming you're using flask as your routing engine. If so, instead of redirecting to a bare html url, you can redirect to a new route. Something like: if validate_login(username, password): return redirect(f"/home/{username}")  Then have that route render the home.html template using the variable as normal: @app.route("/home/<username>") def user_home(username): return render_template("home.html", username=username)  For additional variables you need, you could either have the user_home function retrieve them and pass them along to the template, or use some middleware function to add additional data to the user's request.
Question: Using Odoo 16, using Odoo.sh I am trying to set a date in a model that should be the furthest in the future of the dates among the related objects from a one2many field. So I have a computed field with a @api.depends computation to iterate through the one2many and find + assign this data to a field. Everything in my below code compiles and upgrades without problem, but I get an error when I trigger the @api.depends function by adding a order_line_ids: if order_line.delivery > record.full_delivery_date: TypeError: '>' not supported between instances of 'datetime.date' and 'bool' Relevant code in primary model operations.purchaseorder: full_delivery_date = fields.Date(string="Full Delivery Date (calculated)", compute='_compute_deliver_date', default=lambda self: fields.Date.today()) order_line_ids = fields.One2many('operations.purchaseorderline','purchase_order_id',string="Order Lines") @api.depends('order_line_ids') def _compute_deliver_date(self): for record in self: if record.order_line_ids: for order_line in record.order_line_ids: if order_line.delivery > record.full_delivery_date: record.full_delivery_date = order_line.delivery  Relevant code in co-model operations.purchaseorderline: purchase_order_id = fields.Many2one('operations.purchaseorder',string='Purchase Order') delivery = fields.Date("Promised Delivery Date") 

Answer: As per comment from @CZoellner the issue was because I had not put handling into the code for when order_line.delivery was empty. Correct code below: @api.depends('order_line_ids') def _compute_full_delivery_date(self): for record in self: if record.order_line_ids: for order_line in record.order_line_ids: if order_line.delivery: if record.full_delivery_date: if order_line.delivery > record.full_delivery_date: record.full_delivery_date = order_line.delivery else: record.full_delivery_date = order_line.delivery
Question: i have a use case in spacy where i want to find phone numbers in German sentences. Unfortunately the tokenizer is not doing the tokenization as expected. When the number is at the end of a sentence the number and the dot is not split into two tokens. English and German Version differ here, see the following code: import spacy nlp_en = spacy.blank("en") nlp_de = spacy.blank("de") text = "Die Nummer lautet 1234 123444." doc_en = nlp_en(text) doc_de = nlp_de(text) print(doc_en[-1]) #output is: . print(doc_de[-1]) #output is: 123444.  expected output is: 123444. is split into two tokens. But i also want to use the "de" version as it has other meaningful defaults for German sentences... my spaCy version: 3.7.4 in a similar case i was able to solve the problem with nlp_de.tokenizer.add_special_case but here i need to match a number that i don't know. and i couldn't find a way to use regex with add_special_case I also had a look at: Is it possible to change the token split rules for a Spacy tokenizer? which seems promising. but i wasn't able to figure out how to adjust the tokenizer. I guess I should use a custom tokenizer and the information from https://github.com/explosion/spaCy/blob/master/spacy/lang/de/punctuation.py !?

Answer: You may use 'suffixes' to fix issues with punctuation. Here is an example: import spacy nlp_en = spacy.blank("en") nlp_de = spacy.blank("de") text = "Die Nummer lautet 1234 123448." suffixes = nlp_de.Defaults.suffixes + [r'\.',] suffix_regex = spacy.util.compile_suffix_regex(suffixes) nlp_de.tokenizer.suffix_search = suffix_regex.search doc_en = nlp_en(text) doc_de = nlp_de(text) print(doc_en[-1]) #output is: . print(doc_de[-1]) #output is: .
Question: In pyhon3.10, I can run this code snippet without raising any errors: {}[0]:1  which creates an empty dictionary, and then accesses the 0 key. However, the :1 that follows is what I would consider an invalid syntax. And indeed, if I try to determine the type of the result: type({}[0]:1)  a syntax error gets raised. A similar behavior occurs whenever I try to work with the result, such as print({}[0]:1). Why does this happen? I am assuming that the interpreter recognizes the expression as not being assigned and does not compile it. And therefore you can run your code with the {}[0]:1 line being present. However, this is not consistent with other syntax errors being raised by different syntactically invalid code (such as 1:1 which raises an error).

Answer: {}[0]:1 as a statement is not a syntax error. It's an annotation where {}[0] is the thing being annotated and 1 is the annotation. Normally, you would write something like x: int. {}[0] is not an error because it is executed as if on the left-hand side of an assignment. We can see this by using dis.dis (this is Python 3.11.2): >>> dis.dis("{}[0]:1") 0 0 RESUME 0 1 2 SETUP_ANNOTATIONS 4 BUILD_MAP 0 6 POP_TOP 8 LOAD_CONST 0 (0) 10 POP_TOP 12 LOAD_CONST 1 (1) 14 POP_TOP 16 LOAD_CONST 2 (None) 18 RETURN_VALUE  Only when using {}[0]:1 as an expression, it is invalid syntax. It's a coincidence that you chose {}[0] on the left-hand side of :, this does not always work: >>> print(x):1 File "<stdin>", line 1 print(x):1 ^^^^^^^^ SyntaxError: illegal target for annotation  (You should have gotten the same error for 1:1.) The corresponding section in the language reference is here: https://docs.python.org/3/reference/simple_stmts.html#annotated-assignment-statements
Question: Context I am using google gemini-api.  Using their Python SDK Goal I am trying to generate multiple possible responses for a single prompt according to the docs and API-reference Expected result - multiple response for a single prompt  Actual result - single response Code I have tried # ... more code above model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest", system_instruction=system_instruction) response = model.generate_content("What is the meaning of life?") resps = response.candidates   resps is a list which should contain more than 1 response. But there is only 1 response inside it. The prompt used here is a demo prompt. But the outcome is same for any input string.  If any more information is needed please ask in the comments.

Answer: When I saw the official document GenerationConfig of Method: models.generateContent, it says as follows.  candidateCount: integer Optional. Number of generated responses to return. Currently, this value can only be set to 1. If unset, this will default to 1.  It seems that in the current stage, candidateCount is only 1. This is the same with "v1". Ref But, when I saw google.ai.generativelanguage.GenerationConfig of the official document, it says as follows.  candidate_count: int Optional. Number of generated responses to return. This value must be between [1, 8], inclusive. If unset, this will default to 1.  From this, it seems that it is possible to try to change the value of candidate_count by the script. From this document, when your script is modified, it becomes as follows. model = genai.GenerativeModel( model_name="gemini-1.5-pro-latest", system_instruction=system_instruction, generation_config=glm.GenerationConfig(candidate_count=2), ) response = model.generate_content("What is the meaning of life?") resps = response.candidates   In this case, please add import google.ai.generativelanguage as glm. When I tested the above script with candidate_count=2, an error like Only one candidate can be specified occurred. I guess that this value might be able to be changed in the future update. When a value of more than 1 got to be able to be used, please test the above-modified script.  References:  GenerationConfig google.ai.generativelanguage.GenerationConfig
Question: Imagine having an array of matrices (nxkxk), how would I return the diagonal entries while keeping the original shape without using a for-loop. For example, without keeping the original shape we could do np.diagonal(array_of_matrices, axis1=1, axis2=2) Obviously I could just do this and then reconstruct the original shape, but I'm wondering if there is a cleaner way. I have tried nothing and I'm all out of ideas. np.diag does not take axis arguments.

Answer: You can use broadcasting to multiply an eye matrix by your k x n x n shaped array to return only the diagonal elements. import numpy as np n = 4 k = 3 arr = np.arange(k * n * n).reshape(k, n, n) diags = arr * np.eye(n)  Here are the elements: arr # returns: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]], [[16, 17, 18, 19], [20, 21, 22, 23], [24, 25, 26, 27], [28, 29, 30, 31]], [[32, 33, 34, 35], [36, 37, 38, 39], [40, 41, 42, 43], [44, 45, 46, 47]]]) diags # returns: array([[[ 0., 0., 0., 0.], [ 0., 5., 0., 0.], [ 0., 0., 10., 0.], [ 0., 0., 0., 15.]], [[16., 0., 0., 0.], [ 0., 21., 0., 0.], [ 0., 0., 26., 0.], [ 0., 0., 0., 31.]], [[32., 0., 0., 0.], [ 0., 37., 0., 0.], [ 0., 0., 42., 0.], [ 0., 0., 0., 47.]]])
Question: I'm an experienced Python programmer trying to learn C++ to speed up some projects. Passing a py::tuple to a function, how do I access the elements? Here's the constructor for a class I'm making to hold images from a video. Buffer::Buffer(int size, py::tuple hw_args_) { unsigned int frame_height = get<0>(hw_args_); unsigned int frame_width = get<1>(hw_args_); Buffer::time_codes = new int[size]; Buffer::time_stamps = new string[size]; Buffer::frames = new unsigned char[size][hw_args[0]][hw_args[1]]; if ((Buffer::time_codes && Buffer::time_stamps) == 0) { cout << "Error allocating memory\n"; exit(1); }  } This gives me an error "No instance of the overloaded function 'get' matches the argument list, argument types are pybind11::tuple" I've also tried setting the frame height and widths this way. unsigned int frame_height = hw_args_[0]; unsigned int frame_width = hw_args_[1];  This gives an error "No suitable conversion function from 'pybind11::detail::tuple_accessor' to 'unsigned int' exists" I'm at a loss, I can only seem to find info on making tuples in C++, not accessing them from Pybind11 / Python.

Answer: py::tuple has an operator[] that returns a detail::tuple_accessor which has a .cast as any py::object uint32_t frame_height = hw_args_[0].cast<uint32_t>();  you could check its use from pybind11 tests [](const py::tuple &tup) { if (py::len(tup) != 4) { throw py::cast_error("Invalid size"); } return SimpleStruct{tup[0].cast<bool>(), tup[1].cast<uint32_t>(), tup[2].cast<float>(), tup[3].cast<long double>()}; }
Question: During my recent work on a mathematical problem in Python, I encountered some confusion regarding the behavior of the bisect.bisect_left function from the bisect library. This confusion arose due to its similarity with another function in the library, namely bisect.insort_left. In my specific use case, I was utilizing a custom key function, such as bisect.insort_left(my_list, my_item, key=my_key), which behaved as expected. This function determined the appropriate index for my_item in my_list using the specified key and inserted it accordingly. However, when attempting a similar operation with bisect.bisect_left(my_list, my_item, key=my_key), I encountered an unexpected TypeError: "other argument must be K instance". This error message lacked clarity regarding the underlying issue. Upon investigating the source code of bisect, I discovered the correct usage pattern, as indicated by line 71 of the source code. It became apparent that the correct usage involves calling the key function with the item as an argument, like so: bisect.bisect_left(my_list, my_key(my_item), key=my_key). I am curious about the design decision behind this requirement. Why is it necessary to call my_key(my_item) when using bisect.bisect_left compared to the more straightforward usage in bisect.insort_left?

Answer: Let's say the list items contain a lot of data. Each item can be e. g. a dict { 'id': 12, 'firstname': 'foo', 'lastname': 'bar', 'address': ..., 'phone': ... ... }  These items are sorted in the list by id with an appropriate key function like lambda item: item['id']. If you want to find an item with bisect_left it shouldn't be necessary to provide the whole item with all of its data but the id should be enough. In the docs this is mentioned a bit cryptically:  To support searching complex records, the key function is not applied to the x value.  If you want to insert an item with insort_left of course you have to provide the complete item with all data.
Question: I'm working on a Python project with a setup.py that has something like this1: setup( cmdclass={"build_ext": my_build_ext}, ext_modules=[ Extension("A", ["a.c", "common.c"]), Extension("B", ["b.c", "common.c"]) ] )  I'm running into a problem when building the modules in parallel where it seems like one module tries to read common.o/common.obj while another is compiling it, and it fails. Is there some way to get setuptools to compile the C files for each module into their own build directories so that they aren't overwriting each other?    The actual project is more complicated with more modules and source files. 

Answer: I found a potential solution by overriding build_extension() in a custom build_ext class: import copy, os from setuptools import Extension, setup from setuptools.command.build_ext import build_ext class my_build_ext(build_ext): def build_extension(self, ext): # Append the extension name to the temp build directory # so that each module builds to its own directory. # We need to make a (shallow) copy of 'self' here # so that we don't overwrite this value when running in parallel. self_copy = copy.copy(self) self_copy.build_temp = os.path.join(self.build_temp, ext.name) build_ext.build_extension(self_copy, ext) setup( cmdclass={"build_ext": my_build_ext}, ext_modules=[ Extension("A", ["a.c", "common.c"]), Extension("B", ["b.c", "common.c"]) ] )  I've also since been told of the (currently undocumented) libraries parameter for setup(): from setuptools import Extension, setup setup( libraries=[ ("common", {"sources": ["common.c"]}), ], ext_modules=[ Extension("A", sources=["a.c"], libraries=["common"]), Extension("B", sources=["b.c"], libraries=["common"]), ], )  Both solutions worked for me, but in slightly different ways. The first solution recompiles the code for each module, which allows you to specify different parameters to use for each module (ex. different defs). The second solution only has to compile to code once and it will reuse that for every module.
Question: I have a Polars dataframe like df = pl.DataFrame({ "tags": ['{"ref":"@1", "area": "livingroom", "type": "elec"}', '{"ref":"@2", "area": "kitchen"}', '{"ref":"@3", "type": "elec"}'], "name": ["a", "b", "c"], })  ┌────────────────────────────────────────────────────┬──────┐ │ tags ┆ name │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════════════════════════════════════════════╪══════╡ │ {"ref":"@1", "area": "livingroom", "type": "elec"} ┆ a │ │ {"ref":"@2", "area": "kitchen"} ┆ b │ │ {"ref":"@3", "type": "elec"} ┆ c │ └────────────────────────────────────────────────────┴──────┘  What I would to do is create a filter function that filters dataframe based on the tags column. Particularly, I would like to only be left with rows where the tags column has an area key and a type key that has a value "elec". How can I achieve this (ideally using the native expressions API)?

Answer: pl.Expr.str.json_path_match can be used to extract the first match of the JSON string with the a suitable path expression. ( df .filter( pl.col("tags").str.json_path_match("$.area").is_not_null(), pl.col("tags").str.json_path_match("$.type") == "elec", ) )  shape: (1, 2) ┌────────────────────────────────────────────────────┬──────┐ │ tags ┆ name │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════════════════════════════════════════════╪══════╡ │ {"ref":"@1", "area": "livingroom", "type": "elec"} ┆ a │ └────────────────────────────────────────────────────┴──────┘   More generally, pl.Expr.str.json_decode ca be used to obtain a struct column with the information of the JSON. This struct can be unnested and used for any downstream filtering operation. ( df .with_columns( pl.col("tags").str.json_decode() ) .unnest("tags") )  shape: (3, 4) ┌─────┬────────────┬──────┬──────┐ │ ref ┆ area ┆ type ┆ name │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str ┆ str │ ╞═════╪════════════╪══════╪══════╡ │ @1 ┆ livingroom ┆ elec ┆ a │ │ @2 ┆ kitchen ┆ null ┆ b │ │ @3 ┆ null ┆ elec ┆ c │ └─────┴────────────┴──────┴──────┘
Question: I have a frame like this: import polars as pl from polars import Boolean, List, col src = pl.DataFrame( { "c1": ["a", "b", "c", "d"], "c2": [ [0, 0], [0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 0], ], }, schema_overrides={"c2": List(Boolean)}, )  For each of the inner lists in "c2", I am trying to calculate the maximum sum in a sliding window of length 3: [1, 0, 0, 1, 1, 1] ⇒ [(1, 0, 0), (0, 0, 1), (0, 1, 1),(1, 1, 1)] ⇒ [1, 1, 2, 3] ⇒ 3  What is the most efficient way to do it?  I cannot find the method to roll over inner lists. Several answers on StackOverflow advise me to  explode the inner lists, roll over the exploded series, restore the structure somehow.  … which leads to monstrosities like the following: ( src.with_row_index("outer_index") .explode("c2") .with_row_index("inner_index") .rolling("inner_index", period="3i", offset="0i", closed="left") .agg("outer_index", "c1", col("c2").sum()) .drop("inner_index") )  First of all, this is ugly. And this is wrong: rolling windows must respect borders of the original internal lists, but here they do not. For readability, here is the dump I get with df.to_dicts() [{'outer_index': [0, 0, 1], 'c1': ['a', 'a', 'b'], 'c2': 0}, {'outer_index': [0, 1, 1], 'c1': ['a', 'b', 'b'], 'c2': 1}, {'outer_index': [1, 1, 1], 'c1': ['b', 'b', 'b'], 'c2': 1}, {'outer_index': [1, 1, 1], 'c1': ['b', 'b', 'b'], 'c2': 1}, {'outer_index': [1, 1, 2], 'c1': ['b', 'b', 'c'], 'c2': 1}, {'outer_index': [1, 2, 2], 'c1': ['b', 'c', 'c'], 'c2': 1}, {'outer_index': [2, 2, 2], 'c1': ['c', 'c', 'c'], 'c2': 2}, {'outer_index': [2, 2, 2], 'c1': ['c', 'c', 'c'], 'c2': 2}, {'outer_index': [2, 2, 2], 'c1': ['c', 'c', 'c'], 'c2': 3}, {'outer_index': [2, 2, 3], 'c1': ['c', 'c', 'd'], 'c2': 3}, {'outer_index': [2, 3, 3], 'c1': ['c', 'd', 'd'], 'c2': 3}, {'outer_index': [3, 3, 3], 'c1': ['d', 'd', 'd'], 'c2': 2}, {'outer_index': [3, 3], 'c1': ['d', 'd'], 'c2': 1}, {'outer_index': [3], 'c1': ['d'], 'c2': 0}] 

Answer: You can access .rolling_sum() via the .list.eval() API. df.with_columns( pl.col("c2").list.eval( pl.element().rolling_sum(3) ) )  shape: (4, 2) ┌─────┬───────────────────────┐ │ c1 ┆ c2 │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞═════╪═══════════════════════╡ │ a ┆ [null, null] │ │ b ┆ [null, null, 1, 1] │ │ c ┆ [null, null, 2, 2, 3] │ │ d ┆ [null, null, 2] │ └─────┴───────────────────────┘  And take the .max() df.with_columns( pl.col("c2").list.eval( pl.element().rolling_sum(3).max() ) )  shape: (4, 2) ┌─────┬───────────┐ │ c1 ┆ c2 │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞═════╪═══════════╡ │ a ┆ [null] │ │ b ┆ [1] │ │ c ┆ [3] │ │ d ┆ [2] │ └─────┴───────────┘
Question: I have a frame like this: src = pl.DataFrame( { "c1": ["a", "b", "c", "d"], "c2": [[0], [2, 3, 4], [3, 4, 7, 9], [3, 9]], } )  ... and a list of targets: targets = pl.Series([3, 7, 9])  ... and I want to count the number of targets in "c2": dst = pl.DataFrame( { "c1": ["a", "b", "c", "d"], "c2": [[0], [2, 3, 4], [3, 4, 7, 9], [3, 9]], "match_count": [0, 1, 3, 2], } )  What is the most efficient way to do it?  I see count_matches, but it does not work with multiple options: df["c"].list.count_matches(3) # OK. df["c"].list.count_matches([3, 7, 9]) # No way. 

Answer: You could use pl.Expr.list.eval to evaluate for each element in the list column, whether it is contained in target. Then, the sum of the boolean list gives the number of matches. dst.with_columns( pl.col("c2").list.eval(pl.element().is_in(targets)).list.sum().alias("res") )  shape: (4, 4) ┌─────┬─────────────┬─────────────┬─────┐ │ c1 ┆ c2 ┆ match_count ┆ res │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ list[i64] ┆ i64 ┆ u32 │ ╞═════╪═════════════╪═════════════╪═════╡ │ a ┆ [0] ┆ 0 ┆ 0 │ │ b ┆ [2, 3, 4] ┆ 1 ┆ 1 │ │ c ┆ [3, 4, … 9] ┆ 3 ┆ 3 │ │ d ┆ [3, 9] ┆ 2 ┆ 2 │ └─────┴─────────────┴─────────────┴─────┘  Similarly, you could use pl.Expr.list.eval to filter column c2 and, then, use pl.Expr.list.len to see how many values remain. If this is performance critical, I'd suggest benchmarking both approaches.  Alternatively, you are really interested in the number of elements in the set intersection (as mentioned by @jqurious) or the elements in at least one list are unique, you can use pl.Expr.list.set_intersection. dst.with_columns( pl.col("c2") .list.set_intersection(targets.to_list()) .list.len() .alias("res_intersection") )  shape: (4, 4) ┌─────┬─────────────┬─────────────┬──────────────────┐ │ c1 ┆ c2 ┆ match_count ┆ res_intersection │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ list[i64] ┆ i64 ┆ u32 │ ╞═════╪═════════════╪═════════════╪══════════════════╡ │ a ┆ [0] ┆ 0 ┆ 0 │ │ b ┆ [2, 3, 4] ┆ 1 ┆ 1 │ │ c ┆ [3, 4, … 9] ┆ 3 ┆ 3 │ │ d ┆ [3, 9] ┆ 2 ┆ 2 │ └─────┴─────────────┴─────────────┴──────────────────┘