eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Im unsure about the new doc on packaging with hatch and wonder if someone worked out how to define a script in a pip installable package. So in short I need to be able to direct python -m build to make a package with open_foo_bar.py as in the example, install into the (virtual env)/bin dir. my package looks like this (after a python -m build step that generated dist dir) pypi_package/ ├── bin │ └── open_foo_bar.py ├── dist │ ├── foo-0.1.0-py3-none-any.whl │ └── foo-0.1.0.tar.gz ├── pyproject.toml ├── README.md └── test_pkg ├── foolib.py └── __init__.py  Im trying to get bin/open_foo_bar.py installed into the $(virtual env)/bin instead it installs it into the site-packages/bin ./lib/python3.10/site-packages/bin/open_foo_bar.py  myproject.toml is [build-system] requires = ["hatchling"] build-backend = "hatchling.build" [project] name = "FOO" version = "0.1.0" authors = [ { name="Mr Foo", email="foo@bar.com" }, ] description = "a Foo bar without drinks" readme = "README.md" requires-python = ">=3.8" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] dependencies = [ 'requests' ] [project.urls] "Homepage" = "http://footopia.s/howto_foo"  This used to be easy by defining the scripts section in setup.py setuptools.setup( ... scripts ['bin/script1'], ... ) 

Answer: In theory the Python spec defines how entrypoints and scripts are supposed to be handled. Hatch has chosen to do it a bit differently, and I'm not sure if this is even considered best-practice. This approach combines Forced Inclusion with Metadata Entrypoints/CLI. Here's how you might achieve your desired outcome:  Add a main or run function to your script open_foo_bar.py Force hatch to make your script appear as part of the package once it is installed. You would add the lines:  [tool.hatch.build.targets.sdist.force-include] "bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py" [tool.hatch.build.targets.wheel.force-include] "bin/open_foo_bar.py" = "test_pkg/open_foo_bar.py"   Create an entrypoint using the scripts section:  [project.scripts] open_foo_bar = "test_pkg.open_foo_bar:main"  When you install the package, open_foo_bar should be in your virtualenv bin/ directory, and it will call your main function within open_foo_bar.py, which will have been moved to reside within your package in site-packages. It's a hacky solution, but there doesn't appear to be a 1:1 feature for supporting arbitrary scripts. Hatch actually does mention the usage of an Entrypoints section, but it's catered towards plugin management.
Question: When I search about the installation of the BeautifulSoup lib, sometimes I see pip install bs4, and sometimes pip install BeautifulSoup4. What's the difference between these 2 methods of installation?

Answer: bs4 is technically a different package; however, it is a dummy package designed to install the correct package: beautifulsoup4. https://pypi.org/project/bs4/ TLDR: You can use either the short name or long name
Question: I've got a Flask app with some routes protected by authlib's ResourceProtector. I want to test that a route I've set up is indeed protected by a authlib.integrations.flask_oauth2.ResourceProtector but I don't want to go to the trouble of creating a valid jwt token or anything like that. I want to avoid feeling like I'm testing authlib, rather than testing my code. All I really need to do is check that when I hit my endpoint, the ResourceProtector is called. This should be possible with mocking of some kind, but I wonder if there's a supported way to do this. from authlib.integrations.flask_oauth2 import ResourceProtector require_auth = ResourceProtector() require_auth.register_token_validator(validator) APP = Flask(__name__) @APP.route("/") @require_auth(None) def home(): return "Authorized!"  The test should look something like this: from my_module import APP class TestApi: @property def client(self): return APP.test_client() def test_response_without_oauth(self): response = self.client.get("/") assert response.status_code == 401 def test_response_with_auth0(self): # Do some mocking or some magic response = self.client.get("/") assert response.text == "Authorized!" 

Answer: The Flask docs subtly refer to Application Factories as having a use case involving testing:  So why would you want to do this?  Testing. You can have instances of the application with different settings to test every case.  ...  We can use an application factory to keep code out of the top-level which makes life hard for mocking (because by the time you've imported the module, the thing you're hoping to mock has already been defined.) from authlib.integrations.flask_oauth2 import ResourceProtector def create_app(): """The name `create_app` is magic: you can't rename it""" require_auth = ResourceProtector() require_auth.register_token_validator(validator) app = Flask(__name__) @app.route("/") @require_auth(None) def home(): return "Authorized!"  This means we can then test the route as normal like this: from my_module import create_app from authlib.integrations.flask_oauth2 import ResourceProtector class TestApi: def test_endpoint_is_protected(self): client = create_app().test_client() with patch("my_module.ResourceProtector", wraps=ResourceProtector) as mock: client().get(self.ENDPOINT) mock.assert_called_once()  To then test the route without having to worry about auth at all, you could tweak the server a little bit to use the optional argument: def create_app(auth_is_optional = False): ... @app.route("/") @require_auth(None, optional=auth_is_optional) def home(): return "Authorized!"  We can now ignore auth in testing by calling create_app with auth_is_optional=True. This approach, of having keyword arguments to create_app for config, is recommended by the docs:  Make it possible to pass in configuration values for unit tests so that you don’t have to create config files on the filesystem.
Question: I wrote pytest fixture in fixtures.py file and using it in my main_test.py . But I am getting this error in flake8: F401 'utils_test.backup_path' imported but unused for this code: @pytest.fixture def backup_path(): ...  from fixtures import backup_path def test_filename(backup_path): ...  How can I resolve this?

Answer: generally you should not do this making fixtures available via import side-effects is an unintentional implementation detail of how fixtures work and may break in the future of pytest if you want to continue doing so, you can use # noqa: F403 on the import, telling flake8 to ignore the unused imports (though the linter is telling you the right thing here!) the supported way to make reusable fixtures is to place them in a conftest.py which is in a directory above all the tests that need it. tests will have these fixtures "in scope" automatically if that file is getting too large, you can write your fixtures in a plugin module and add that plugin module via pytest_plugins = ['module.name'] in your conftest.py  disclaimer: I'm the current flake8 maintainer, I'm also a pytest core dev
Question: Lets assume we have below given dataframe. Now for each row I need to create dictionary and pass it to UDF for some logic processing.Is there a way to achieve this using either polars or pyspark dataframe ? 

Answer: With Polars, you can use: # Dict of lists >>> df.transpose().to_dict(as_series=False) {'column_0': [1.0, 100.0, 1000.0], 'column_1': [2.0, 200.0, None]} # List of dicts >>> df.to_dicts() [{'Account number': 1, 'V1': 100, 'V2': 1000.0}, {'Account number': 2, 'V1': 200, 'V2': None}]  Input dataframe: >>> df shape: (2, 3) ┌────────────────┬─────┬────────┐ │ Account number ┆ V1 ┆ V2 │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ f64 │ ╞════════════════╪═════╪════════╡ │ 1 ┆ 100 ┆ 1000.0 │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤ │ 2 ┆ 200 ┆ null │ └────────────────┴─────┴────────┘
Question: I recently updated Pandas to the latest version, 1.5.3. I was using a version pre 1.4 previously. With the newest update I am getting a bunch of depreciation notices on index.get_loc and I am to use index.get_indexer. I updated my code to get_indexer, but now I am receiving a bunch of errors. My code uses timestamps as the index, and with get_loc I could simply find the index number by passing in a datetime. Now I get an error: 'datetime.datetime' object is not iterable. I have some simple sample code below: from datetime import datetime, timedelta import pandas as pd import numpy as np expiration_date = datetime(2016,3,24,16,00,0) num_minutes = 50000 date_list = [expiration_date - timedelta(minutes=x) for x in range(num_minutes)] df = pd.DataFrame(index = date_list) searchDate = date_list[10] idx_get_loc = df.index.get_loc(searchDate, method='nearest') print(f'Closes index using get_loc: {idx_get_loc}') idx_get_indexer = df.index.get_indexer(searchDate, method='nearest') print(f'Closes index using get_indexer: {idx_get_indexer}')  get_loc will report out the proper index location. get_indexer fails. What am I missing?

Answer: Here, the get_indexer() method expects an iterable object such as a list or an array of timestamps, not a single timestamp as an argument. To use the get_indexer() method with a single timestamp, you need to put the timestamp in a list or an array like below. idx_get_indexer = df.index.get_indexer([searchDate], method='nearest')
Question: I have a dictionary of items with multiple properties. from django.utils.translation import ( gettext_lazy as _, ) {"item1": { "labels": [ _("label1"), "this is" + _("translatethis") + " label2", ]  These items are then serialized in DRF. The problem is that _("label1") is being translated but "this is" + _("translatethis") + " label2" is not translated I tried also string interpolation, fstring and .format but nothing worked. When serializer fetches labels, _("translatethis") is not a proxy object. Is the only way to make this work surrounding whole strings in the gettext_lazy ?

Answer: The main problem is that _('translatethis') is not a string, but something that promises, when necessary, to be a string. When you however concatenate it with a string, it is time to keep its promise, and it thus presents a string, and it no longer can thus, when needed, check the active language. An option might be to work with a lazy object, like: from django.utils.functional import lazy def text_join(*items): return ''.join(items) text_join_lazy = lazy(text_join, str) { 'item1': { 'labels': [ _('label1'), text_join_lazy('this is ', _("translatethis"), ' label2'), ] } }
Question: I have two tensors: # losses_q tensor(0.0870, device='cuda:0', grad_fn=<SumBackward0>) # this_loss_q tensor([0.0874], device='cuda:0', grad_fn=<AddBackward0>)  When I am trying to concat them, PyTorch raises an error: losses_q = torch.cat((losses_q, this_loss_q), dim=0) RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated  How to resolve this error?

Answer: losses_q is zero dimensional so can't be concatenated with anything. You can reshape it into a one dimensional tensor before concatenation. losses_q = torch.cat((losses_q.reshape(1), this_loss_q), dim=0)
Question: I have a function f that takes in a boolean static argument flag and performs some computation based on it's value. Below is a rough outline of this function. @partial(jax.jit, static_argnames=['flag']) def f(x, flag): # Preprocessing if flag: ... else: ... # Postprocessing  Each time f is called with a different value of flag, a recompilation of this function should be triggered. However, because flag is a boolean and can take on at most two values, it would be preferable if JAX would cache the compiled version of f for each of the possible values of flag and avoid recompilations. In short, I would like JAX to compile f only two times when running following piece of code: flag = True for i in range(100): f(x, flag) flag = not flag  Is there a way to tell JAX not to throw away old compiled versions of f, each time it's called with a new value of flag? And in general, are there any caching mechanisms implemented in JAX for such scenarios? (For instance if flag is an integer, but we know beforehand that it would only ever take k distinct values, and we would like to save the compiled version of f for each of these k values) I know that I can use jax.lax.cond or jax.lax.switch to control the flow inside f and treat flag as a regular argument rather than a static one. But this would make the code much more bloated (and difficult to read) as there are several places within the body of f where I access flag. It would be much cleaner if I declared flag to be a static argument and then controled the caching behavior of jax.jit to avoid recompilations.

Answer: If I understand your question correctly, then JAX by default behaves the way you would like it to behave. Each JIT-compiled function has an LRU cache of compilations based on the shape and dtype of dynamic arguments and the hash of static arguments. You can inspect the size of this cache using the _cache_size method of the compiled function. For example: import jax import jax.numpy as jnp from functools import partial @partial(jax.jit, static_argnames=['flag']) def f(x, flag): if flag: return jnp.sin(x) else: return jnp.cos(x) print(f._cache_size()) # 0 x = jnp.arange(10) f(x, True) print(f._cache_size()) # 1 f(x, False) print(f._cache_size()) # 2 # Subsequent calls with the same flag value hit the cache: flag = True for i in range(100): f(x, flag) flag = not flag print(f._cache_size()) # 2  Since the size of the x argument hasn't changed, we get one cache entry for each value of flag, and the cached compilations are used in subsequent calls. Note however if you change the shape or dtype of the dynamic argument, you get new cache entries: x = jnp.arange(100) for i in range(100): f(x, flag) flag = not flag print(f._cache_size()) # 4  The reason this is necessary is that, in general, functions may change its behavior based on these static quantities.
Question: import re input_string = "Sus cosas deben ser llevadas allí, ella tomo a sí, Lucy la hermana menor, esta muy entusiasmada. por verte hoy por la tarde\n sdsdsd" #result_list = re.split(r"(?:.\s*\n|.|\n|;|,\s*[A-Z])", input_string) result_list = re.split(r"(?=[.,;]|(?<=\s)[A-Z])", input_string) print(result_list)  Separate the string input_string using these separators r"(?:.\s*\n|.|\n|;|,\s*[A-Z])" , but without removing them from the substrings of the resulting list. When I use a positive lookahead assertion instead of a non-capturing group. This will split the input string at the positions immediately before the separators, while keeping the separators in the substrings. But I get this wrong output list ['Sus cosas deben ser llevadas allí', ', ella tomo a sí', ', ', 'Lucy la hermana menor', ', esta muy entusiasmada', '. por verte hoy por la tarde\n sdsdsd']  In order to obtain this correct list of output, when printing ["Sus cosas deben ser llevadas allí, ella tomo a sí,", " Lucy la hermana menor, esta muy entusiasmada.", " por verte hoy por la tarde\n", " sdsdsd"] 

Answer: Condense your delimiter list pattern to the following "([.;\n]|,(?=\s*[A-Z]))" and use itertools.zip_longest to combine resulting substrings with followed delimiters: import re from itertools import zip_longest input_string = "Sus cosas deben ser llevadas allí, ella tomo a sí, Lucy la hermana menor, esta muy entusiasmada. por verte hoy por la tarde\n sdsdsd" res = re.split(r"([.;\n]|,(?=\s*[A-Z]))", input_string) res = list(map(''.join, zip_longest(res[::2], res[1::2], fillvalue=''))) print(res)   ['Sus cosas deben ser llevadas allí, ella tomo a sí,', ' Lucy la hermana menor, esta muy entusiasmada.', ' por verte hoy por la tarde\n', ' sdsdsd']
Question: I am having a function safe_cast which casts a value to a given type, but raises if the value fails to comply with the type at runtime: from typing import TypeVar T = TypeVar('T') def safe_cast(t: type[T], value: Any) -> T: if isinstance(value, t): return cast(T, value) raise TypeError()  This works nicely with primitive types. But I run into problems if I want to safe_cast against a UnionType: string = "string" casted: str | int = safe_cast(str | int, string)  The instance check works with a union type. But my solution does not work, because mypy gives me error: Argument 1 to "safe_cast" has incompatible type "UnionType"; expected "Type[<nothing>]" [arg-type]  I figure that <nothing> refers to the unspecified type variable T here. I also figure that apparently mypy cannot resolve Union[str, int] to Type[T]. My question is: How can I solve this? I looked into creating an overload for the UnionType. IIUC, in order to write the overload, I would need to create a Generic Union Type with a variadic number of arguments. I failed to get this done. Is this the right direction? If yes, how do I get it done? If no, how can I solve my problem with safe_casting Union types?

Answer: typing.cast is likely to be special-cased in a type-checking implementation, because while this works in mypy, import typing as t if t.TYPE_CHECKING: reveal_type(t.cast(str | int, "string")) # mypy: Revealed type is "Union[builtins.str, builtins.int]"  the type annotations for typing.cast don't actually do anything meaningful with union types: @overload def cast(typ: Type[_T], val: Any) -> _T: ... @overload def cast(typ: str, val: Any) -> Any: ... @overload def cast(typ: object, val: Any) -> Any: ...  What you'd actually want to do is to exploit the magic that your type-checker-implementation already offers. This is very easily done by making typing.cast your typing API, while making your safe_cast the runtime implementation. import typing as t T = t.TypeVar("T") if t.TYPE_CHECKING: from typing import cast as safe_cast else: # Type annotations don't actually matter here, but # are nice to provide for readability purposes. def safe_cast(t: type[T], value: t.Any) -> T: if isinstance(value, t): return value raise TypeError  >>> reveal_type(safe_cast(int | str, "string")) # mypy: Revealed type is "Union[builtins.int, builtins.str]"
Question: I have two models Product and Cart. Product model has maximum_order_quantity. While updating quantity in cart, I'll have to check whether quantity is greater than maximum_order_quantityat database level. For that am comparing quantity with maximum_order_quantity in Cart Model But it throws an error when I try to migrate cart.CartItems: (models.E041) 'constraints' refers to the joined field 'product__maximum_order_quantity'. Below are my models class Products(models.Model): category = models.ForeignKey( Category, on_delete=models.CASCADE, related_name="products" ) product_name = models.CharField(max_length=50, unique=True) base_price = models.IntegerField() product_image = models.ImageField( upload_to="photos/products", null=True, blank=True ) stock = models.IntegerField(validators=[MinValueValidator(0)]) maximum_order_quantity = models.IntegerField(null=True, blank=True) )  class CartItems(models.Model): cart = models.ForeignKey(Cart, on_delete=models.CASCADE) product = models.ForeignKey(Products, on_delete=models.CASCADE) quantity = models.IntegerField() class Meta: verbose_name_plural = "Cart Items" constraints = [ models.CheckConstraint( check=models.Q(quantity__gt=models.F("product__maximum_order_quantity")), name="Quantity cannot be more than maximum order quantity" ) ]  Error SystemCheckError: System check identified some issues: ERRORS: cart.CartItems: (models.E041) 'constraints' refers to the joined field 'product__maximum_order_quantity'. 

Answer: You cannot reference fields on related models in database-level constraints like CheckConstraint. The error message you received is indicating that you cannot reference the maximum_order_quantity field of the Products model in the constraints attribute of the CartItems model. The reason for this is that the maximum_order_quantity field is a property of the Products model, and in the CartItems model, you are referencing it through a foreign key relationship. Specifically, you are using the syntax product__maximum_order_quantity to access the maximum_order_quantity field of the related Products model. However, the constraints attribute can only reference fields that belong to the same model. It cannot reference fields of related models. Currently, you can override the clean() method as: from django.core.exceptions import ValidationError class CartItems(models.Model): cart = models.ForeignKey(Cart, on_delete=models.CASCADE) product = models.ForeignKey(Products, on_delete=models.CASCADE) quantity = models.IntegerField() def clean(self): if self.quantity > self.product.maximum_order_quantity: raise ValidationError('Quantity cannot be more than maximum order quantity')   Note: Models in Django don't require s to be added as suffix, since it is added by default. So it is better to change them as CartItem and Product from CartItems and Products respectively.
Question: I am trying to solve a system of 4 exponential equations with two variables. However If I use fsolve python will only allow me two use as many equations as I have variables. But as I have infinitely many pairs of solutions (if only two equations are used) and I need to find the pair of variables that fits not only two but all four equations, fsolve does not seem to work. So right know my code look something like this: from scipy.optimize import fsolve c_1 = w+(y/(x*R)) c_2 = offset - (c_1/(x*R)) #offset(1.05), w(10) and R(0.35) are constants def equations(p): x,y = p eq1 = (c_1*sp.exp(-y*R*1.017))/(y*R)+c_2-(x*1.017)/(y*R)-(5.1138*2*np.pi) eq2 = (c_1*sp.exp(-y*R*2.35))/(y*R)+c_2-(x*2.35)/(y*R)-(2.02457*4*np.pi) eq3 = (c_1*sp.exp(-y*R*2.683))/(y*R)+c_2-(x*2.683)/(y*R)-(6.0842178*4*np.pi) return (eq1,eq2,eq3) x, y = fsolve(equations, (1,1)) print (equations((x, y)))  However this will give me wildly different result depending on the initial guesses I give. I now want to so edit this code so that I can put additional equations to guarantee that I get the correct pair of x,y as a solution. Simply adding a third equation here in the into the function will return a TypeError of course so I was wondering how this can be done.

Answer: To add an arbitrary number of equations, I don't think you can use fsolve at all. Just run a least-squares minimisation, and make sure that you vectorise properly instead of rote repetition. The following does run and generate a result, though I have not assessed its accuracy. import numpy as np from scipy.optimize import minimize offset = 1.05 w = 10 R = 0.35 a = np.array((-1.017, -2.350, -2.683)) b = np.array(( -5.1138000*2, -2.0245700*4, -6.0842178*4, ))*np.pi def equations(x: float, y: float) -> np.ndarray: c_1 = w + y/x/R c_2 = offset - c_1/x/R return (c_1*np.exp(a*y*R) + a*x)/y/R + c_2 + b def error(xy: np.ndarray) -> np.ndarray: eqs = equations(*xy) return eqs.dot(eqs) result = minimize( fun=error, x0=(1, 1), method='nelder-mead', ) print(result.message) print('x, y =', result.x) print('equations:', equations(*result.x))  Optimization terminated successfully. x, y = [0.19082078 0.13493941] equations: [ 27.4082168 13.91087443 -42.00388728]