eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to plot a 3D superball in python matplotlib, where a superball is defined as a general mathematical shape that can be used to describe rounded cubes using a shape parameter p, where for p = 1 the shape is equal to that of a sphere. This paper claims that the superball is defined by using modified spherical coordinates with: x = r*cos(u)**1/p * sin(v)**1/p y = r*cos(u)**1/p * sin(v)**1/p z = r*cos(v)**1/p  with u = phi and v = theta. I managed to get the code running, at least for p = 1 which generates a sphere - exactly as it should do: import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(projection='3d') r, p = 1, 1 # Make data u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) u, v = np.meshgrid(u, v) x = r * np.cos(u)**(1/p) * np.sin(v)**(1/p) y = r * np.sin(u)**(1/p) * np.sin(v)**(1/p) z = r * np.cos(v)**(1/p) # Plot the surface ax.plot_surface(x, y, z) plt.show()  This is a 3D plot of the code above for p = 1.  However, as I put in any other value for p, e.g. 2, it's giving me only a partial shape, while it should actually give me a full superball. This is a 3D plot of the code above for p = 2.  I believe the fix is more of mathematical nature, but how can this be fixed?

Answer: When plotting a regular sphere, we transform positive and negative coordinates differently:  Positives: x**0.5 Negatives: -1 * abs(x)**0.5  For the superball variants, apply the same logic using np.sign and np.abs: power = lambda base, exp: np.sign(base) * np.abs(base)**exp x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p) y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p) z = r * power(np.cos(v), 1/p)   Full example for p = 4: import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots(subplot_kw={'projection': '3d'}) r, p = 1, 4 # Make the data u = np.linspace(0, 2 * np.pi) v = np.linspace(0, np.pi) u, v = np.meshgrid(u, v) # Transform the coordinates # Positives: base**exp # Negatives: -abs(base)**exp power = lambda base, exp: np.sign(base) * np.abs(base)**exp x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p) y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p) z = r * power(np.cos(v), 1/p) # Plot the surface ax.plot_surface(x, y, z) plt.show()
Question: I hope you're fine. I'm scraping the logos of some websites. I'm using the next code to localize them. I don't use a tag only the * because the class or attribute that contains the substring 'logo' there is not always in a <div> or <a> tags. driver.find_element(By.CSS_SELECTOR, "*[class*='logo']")  I have obtained some of them but in some cases the 'class' doesn't have the substring 'logo'. I've checked some websites and the logo has attributes like 'id', 'alt' or 'name' that contains the substring 'logo'. So I want to know if is there some condition like OR to applied it and if there is no match with 'class' then check in 'id', etc. I tried with these options but both launch an error: driver.find_element(By.CSS_SELECTOR, "*[class*='logo'] | *[id*='logo']") driver.find_element(By.CSS_SELECTOR, "*[class*='logo'] || *[id*='logo']")  In both cases the error is: selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified 

Answer: You can use , to group multiple CSS selectors. driver.find_element(By.CSS_SELECTOR, "[class*='logo'], [id*='logo']")
Question: What I'm trying to do: use environment variables in a Hydra config. I worked from the following links: OmegaConf: Environment variable interpolation and Hydra: Job Configuration. This is my config.yaml: hydra: job: env_copy: - EXPNAME # I also tried hydra:EXPNAME and EXPNAME, # which return None test: ${env:EXPNAME}  Then I set the environment variable (Ubuntu) with: export EXPNAME="123"  The error I get is omegaconf.errors.UnsupportedInterpolationType: Unsupported interpolation type env full_key: test object_type=dict 

Answer: Try this (env was removed in a long time ago in favor of oc.env). test: ${oc.env:EXPNAME}  I don't think the rest is needed if all you need is to access environment variables on your local machine.
Question: I'm trying to create a few array types for a scientific python project. So far, I have created generic types for 1D, 2D and ND numpy arrays: from typing import Any, Generic, Protocol, Tuple, TypeVar import numpy as np from numpy.typing import _DType, _GenericAlias Vector = _GenericAlias(np.ndarray, (Tuple[int], _DType)) Matrix = _GenericAlias(np.ndarray, (Tuple[int, int], _DType)) Tensor = _GenericAlias(np.ndarray, (Tuple[int, ...], _DType))  The first issue is that mypy says that Vector, Matrix and Tensor are not valid types (e.g. when I try myvar: Vector[int] = np.array([1, 2, 3])) The second issue is that I'd like to create a generic type Batch that I'd like to use like so: Batch[Vector[complex]] should be like Matrix[complex], Batch[Matrix[float]] should be like Tensor[float] and Batch[Tensor[int] should be like Tensor[int]. I am not sure what I mean by "should be like" I guess I mean that mypy should not complain. How to I get about this?

Answer: You should not be using protected members (names starting with an underscore) from the outside. They are typically marked this way to indicated implementation details that may change in the future, which is exactly what happened here between versions of numpy. For example in 1.24 your import line from numpy.typing fails at runtime because the members you try to import are no longer there.  There is no need to use internal alias constructors because numpy.ndarray is already generic in terms of the array shape and its dtype. You can construct your own type aliases fairly easily. You just need to ensure you parameterize the dtype correctly. Here is a working example: from typing import Tuple, TypeVar import numpy as np T = TypeVar("T", bound=np.generic, covariant=True) Vector = np.ndarray[Tuple[int], np.dtype[T]] Matrix = np.ndarray[Tuple[int, int], np.dtype[T]] Tensor = np.ndarray[Tuple[int, ...], np.dtype[T]]  Usage: def f(v: Vector[np.complex64]) -> None: print(v[0]) def g(m: Matrix[np.float_]) -> None: print(m[0]) def h(t: Tensor[np.int32]) -> None: print(t.reshape((1, 4))) f(np.array([0j+1])) # prints (1+0j) g(np.array([[3.14, 0.], [1., -1.]])) # prints [3.14 0. ] h(np.array([[3.14, 0.], [1., -1.]])) # prints [[ 3.14 0. 1. -1. ]]  The issue currently is that shapes have almost no typing support, but work is underway to implement that using the new TypeVarTuple capabilities provided by PEP 646. Until then, there is little practical use in discriminating the types by shape.  The batch issue should be a separate question. Try and ask one question at a time.
Question: This is the correct way to import Gtk3 into python: import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GObject  When I save such code in VSCode with "editor.formatOnSave": true, it gets reordered to: from gi.repository import Gtk, Gdk import gi gi.require_version('Gtk', '3.0')  which makes Gtk to be loaded before I have the chance to specify the version I am using, which at very least leads to the following warning being displayed: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded.  or worse, get me an exception like: ValueError: Namespace Gtk is already loaded with version 4.0  Now, I like VSCode code formatting, but I don't want it to reorder my imports, specially not across statements (being that imports in python have side effects). How to properly use VSCode's Python code formatter with Gtk?

Answer: To prevent VSCode from reordering Python imports across statements, you can configure the editor to use a specific Python code formatter that maintains the order of the imports as they are in the original code. Here's how you can do it:  Install the Python extension for VSCode if you haven't already done so. Install a Python code formatter that maintains the order of imports, such as isort or yapf. You can do this by running the command pip install isort or pip install yapf in your terminal. In VSCode, open the settings by clicking on the gear icon in the bottom left corner and selecting "Settings". Search for "Python › Formatting: Provider" in the search bar. Select "Edit in settings.json" to edit the settings.json file. Add the following line to the file, depending on which code formatter you installed: For isort: "python.formatting.provider": "isort" For yapf: "python.formatting.provider": "yapf" Save the file and close the settings.
Question: ((gamma-(gamma**2-omega**2)**0.5)*(gamma+(gamma**2-omega**2)**0.5)).simplify()  The output is: gamma^2 - (gamma^2 -omega^2)^{1.0} $ However, I expected the result to be omega^2. I know in the sympy docs, it warns about being careful with floating point numbers, but I was under the impression that integers and also fractional powers of 2 (which can be represented exactly) were fine. The following code correctly reproduces omega^2: ((gamma-(gamma**2-omega**2)**sym.Rational(1,2))*(gamma+(gamma**2-omega**2)**sym.Rational(1,2))).simplify()  Why does the first code not produce the expected result?

Answer: SymPy considers that there is a distinction between exact and inexact numbers. In this context floats like 0.5 and 1.0 are considered to be inexact and therefore it is not clear that x**1.0 is really equal to x or equal to something slightly different like say x**1.00000000000000000000001. That is because floats usually arise from floating point calculations which can have rounding errors. In your example the result is: In [5]: from sympy import * In [6]: gamma, omega = symbols('gamma, omega') In [7]: e = ((gamma-(gamma**2-omega**2)**0.5)*(gamma+(gamma**2-omega**2)**0.5)).simplify() In [8]: e Out[8]: 1.0 2 ⎛ 2 2⎞ γ - ⎝γ - ω ⎠  If you want to tell SymPy that the 1.0 should be treated as an exact 1 then you can use SymPy's nsimplify function: In [9]: nsimplify(e) Out[9]: 2 ω
Question: Good day. Today I'm trying to send a document generated on the server to the user on the click of a button using Flask. My task is this: Create a document (without saving it on the server). And send it to the user. However, using a java script, I track the button click on the form and use fetch to make a request to the server. The server retrieves the necessary data and creates a Word document based on it. How can I form a response to a request so that the file starts downloading? Code since the creation of the document. (The text of the Word document has been replaced) python Falsk: document = Document() document.add_heading("Some head-title") document.add_paragraph('Some text') f = BytesIO() document.save(f) f.seek(0) return send_file(f, as_attachment=True, download_name='some.docx')  However, the file does not start downloading. How can I send a file from the server to the user? Edits This is my js request. fetch('/getData', { method : 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ someData: someData, }) }) .then(response => response.text() ) .then(response =>{ console.log(response); });  This is my html <form action="" name="getData" method="post" enctype="multipart/form-data"> <button type = "submit" name = "Download">Download</button> </form> 

Answer: You need to specify the mimetype, It tries to detect the mimetype from the filename but since we are not saving it we need to specify the mimetype. return send_file(f, mimetype='application/msword', as_attachment=True, download_name='output.doc')
Question: I have this file structure in my python project |__src |__main.py |__gen.py |__app |__ __init__.py |__ app.py |__ lang.py  Intention I want to use the Language class from sibling module lang. So I tried with this import statement in app.py: from app.lang import Language  Issue But when I run app.py I get a ModuleNotFoundError error saying 'app' is not a package:  Which doesn't make sense since app has __init__.py. How can I solve this?

Answer: Because both app.py and lang.py are in the same directory try to import like this : from .lang import Language  or you can use from app.lang import Language from another file located outside app folder
Question: With the move to the new pyproject.toml system, I was wondering whether there was a way to install packages in editable mode while compiling extensions (which pip install -e . does not do). So I want pip to:  run the build_ext I configured for Cython and generate my .so files put them in the local folder do the rest of the normal editable install  I found some mentions of build_wheel_for_editable on the pip documentation but I could not find any actual example of where this hook should be implemented and what it should look like. (to be honest, I'm not even completely sure this is what I'm looking for) So would anyone know how to do that? I'd also happy about any additional explanation as to why pip install . runs build_ext but the editable command does not.  Details: I don't have a setup.py file anymore; the pyproject.toml uses setuptools and contains [build-system] requires = ["setuptools>=61.0", "numpy>=1.17", "cython>=0.18"] build-backend = "setuptools.build_meta" [tool.setuptools] package-dir = {"" = "."} [tool.setuptools.packages] find = {} [tool.setuptools.cmdclass] build_ext = "_custom_build.build_ext"  The custom build_ext looks like from setuptools import Extension from setuptools.command.build_ext import build_ext as _build_ext from Cython.Build import cythonize class build_ext(_build_ext): def initialize_options(self): super().initialize_options() if self.distribution.ext_modules is None: self.distribution.ext_modules = [] extensions = Extension(...) self.distribution.ext_modules.extend(cythonize(extensions)) def build_extensions(self): ... super().build_extensions()  It builds a .pyx into .cpp, then adds it with another cpp into a .so.

Answer: I created a module that looks like this: $ tree . . ├── pyproject.toml ├── setup.py └── test └── helloworld.pyx 1 directory, 3 files  My pyproject.toml looks like: [build-system] requires = ["setuptools>=61.0", "numpy>=1.17", "cython>=0.18"] build-backend = "setuptools.build_meta" [tool.setuptools] py-modules = ["test"] [project] name = "test" version = "0.0.1"%  My setup.py: from setuptools import setup from Cython.Build import cythonize setup(ext_modules=cythonize("test/helloworld.pyx"))  And helloworld.pyx just contains print("Hello world"). When I do pip install -e ., it builds the cython file as expected. If you really don't want to have a setup.py at all, I think you'll need to override build_py instead of build_ext, but IMO just having the simple setup.py file isn't a big deal.
Question: I am building a rest api with fastapi. I implemented the data layer separately from the fastapi application meaning I do not have direct access to the database session in my fastapi application. I have access to the storage object which have method like close_session which allow me to close the current session. Is there a equivalent of flask teardown_request in fastapi? Flask Implementation from models import storage ..... ..... @app.teardown_request def close_session(exception=None): storage.close_session()  I have looked at fastapi on_event('shutdown') and on_event('startup'). These two only runs when the application is shutting down or starting up.

Answer: We can do this by using dependency. credit to williamjemir: Click here to read the github discussion from fastapi import FastAPI, Depends from models import storage async def close_session() -> None: """Close current after every request.""" print('Closing current session') yield storage.close() print('db session closed.') app = FastAPI(dependencies=[Depends(close_session)]) @app.get('/') def home(): return "Hello World" if __name__ == '__main__': import uvicorn uvicorn.run(app)
Question: I have created a basic ChatBot using OpenAI with the following code: import openai openai.api_key = "sk-xxx" while True: prompt = input("User:") response = openai.Completion.create( model="text-davinci-003", prompt=prompt, max_tokens=50, temperature=0, ) print(response.choices[0].text)  This is the input and output:  And in text form: User:What is python Python is a high-level, interpreted, general-purpose programming language. It is a powerful and versatile language that is used for a wide range of applications, from web development and software development to data science and machine learning.` User:What is the Latest Version of it? The latest version of Microsoft Office is Microsoft Office 2019.  As you can see i am asking questions related to python and asking version of it gives answer related to Microsoft Office, whereas when asking the same question to ChatGpt it remembers the previous conservation and acts according to it. Is there any solution for remembering the conversation?

Answer: One possibility would be to store the inputs and outputs somewhere, and then include them in subsequent inputs. This is very rudimentary but you could do something like the following: inputs, outputs = [], [] while True: prompt = input("Enter input (or 'quit' to exit):") if prompt == 'quit': break if len(inputs) > 0: inputs.append(prompt) last_input, last_output = inputs[-1], outputs[-1] prompt = f"{prompt} (based on my previous question: {last_input}, and your previous answer {last_output}" else: inputs.append(prompt) response = openai.Completion.create( model="text-davinci-003", prompt=prompt, max_tokens=200, temperature=0, ) output = response.choices[0].text outputs.append(output) print(output)  This program would be able to recall the last input and output, and provide that information along with the current prompt. You could include more lines of input and output depending on how much "memory" you want your program to have, and there is also a text limit (max_tokens), so you may need to adjust the wording so that the entire prompt makes sense. And to avoid an infinite loop, we can have a condition to exit the while loop.
Question: I have a series of scatterplots (one example below), but I want to modify it so that the colors of the points in the plot become more red (or "hot") when they are clustered more closely with other points, while points that are spread out further are colored more blue (or "cold"). Is it possible to do this?  Currently, my code is pretty basic in its set up. import plotly.express as px fig = px.scatter(data, x='A', y='B', trendline='ols') 

Answer: Using scipy.stats.gaussian_kde you can calculate the density and then use this to color the plot: import pandas as pd import plotly.express as px from scipy import stats df = pd.DataFrame({ 'x':[0,0,1,1,2,2,2.25,2.5,2.5,3,3,4,2,4,8,2,2.75,3.5,2.5], 'y':[0,2,3,2,1,2,2.75,2.5,3,3,4,1,5,4,8,4,2.75,1.5,3.25] }) kernel = stats.gaussian_kde([df.x, df.y]) df['z'] = kernel([df.x, df.y]) fig = px.scatter(df, x='x', y='y', color='z', trendline='ols', color_continuous_scale=px.colors.sequential.Bluered)  output:
Question: I am encountering the following error when attempting to install matplotlib in an alpine Docker image:  error: Failed to download any of the following: ['http://www.qhull.org/download/qhull-2020-src-8.0.2.tgz']. Please download one of these urls and extract it into 'build/' at the top-level of the source repository. [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for matplotlib Failed to build matplotlib ERROR: Could not build wheels for matplotlib, which is required to install pyproject.toml-based projects  My Python version is 3.9.12. How can I resolve this error?

Answer: This solved my problem: pip install matplotlib==3.2.1
Question: I want to open a file that may be gzipped or not. To open the file, I use either with open(myfile, 'r') as f: some_func(f) # arbitrary function  or import gzip with gzip.open(myfile, 'r') as f: some_func(f)  I want to check if myfile has a gz extension or not, and then from there decide which with statement to use. Here's what I have: # myfile_gzipped is a Boolean variable that tells me whether it's gzipped or not if myfile_gzipped: with gzip.open(myfile, 'rb') as f: some_func(f) else: with open(myfile, 'r') as f: some_func(f)  How should I go about it, without having to repeat some_func(f)?

Answer: if myfile_gzipped: f = gzip.open(myfile, 'rb') else: f = open(myfile, 'r') with f: some_func(f)  The result of open and gzip.open is a context manager. with invokes the entry and exit methods on context managers. There is nothing special in calling those functions inside the with statement itself.
Question: Apparently this is a more complicated problem than I thought. All I want to do is fill the zeros with ++1 increments until the next 1 My dataset is 1m+ rows, so I'm trying to vectorize this operation if possible. Here's a sample column: # Define the input dataframe df = pd.DataFrame({'col': [1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0]}) 0 1 1 0 2 1 3 0 4 1 5 1 6 0 7 0 8 0 9 0 10 1 11 0 12 1 13 1 14 0  Goal Result: 0 1 1 2 2 1 3 2 4 1 5 1 6 2 7 3 8 4 9 5 10 1 11 2 12 1 13 1 14 2  I've tried a number of different methods with ffill() and cumsum(), but the issue with cumsum() tends to be that it doesn't reset the increment.

Answer: Group by cumulative sums of column col and apply cumcount: df['col'] = df.groupby(df['col'].cumsum())['col'].cumcount() + 1    col 0 1 1 2 2 1 3 2 4 1 5 1 6 2 7 3 8 4 9 5 10 1 11 2 12 1 13 1 14 2
Question: I have the following pandas dataframe: d2 = {'col1': [0, 0, 1, 1, 2], 'col2': [10, 11, 12, 13, 14]} df2 = pd.DataFrame(data=d2) df2  Output:  col1 col2 0 0 10 1 0 11 2 1 12 3 1 13 4 2 14  And I need to run the following: for i, g in df2.groupby(['col1']): col1_val = g["col1"].iloc[0] print(col1_val)  The original code is more complex but writing so for the purpose of illustration. And the part for i, g in df2.groupby(['col1']): gives the following warning: FutureWarning: In a future version of pandas, a length 1 tuple will be returned when iterating over a groupby with a grouper equal to a list of length 1. Don't supply a list with a single grouper to avoid this warning.  How am I supposed to run the for loop to get rid of this warning?

Answer: This means that you should use a string instead of the list with a unique string: for i, g in df2.groupby('col1'): col1_val = g["col1"].iloc[0] print(col1_val)  If you keep the original code, in the future i will have the value (0,)/(1,)/(2,) instead of 0/1/2
Question: I have a python function for which I want to use type hinting. There are two arguments. The first is any Enum class, the second optional arg is an element of that Enum. For example, say I have: class Foo(Enum): ALPHA = 1 BETA = 2 GAMMA = 3  The first arg would be, e.g. Foo, the second would be e.g. Foo.ALPHA What would be the correct way of type hinting this? What I have so far is: def switch(options: Enum, selected: Optional[Enum] = None) -> Enum: # Rest of fn...  but that doesn't seem right.

Answer: Define a TypeVar with Enum as a bound, and then specify that your function takes the Type of that typevar and returns an instance of it: from enum import Enum from typing import Optional, Type, TypeVar _E = TypeVar('_E', bound=Enum) def switch( options: Type[_E], selected: Optional[_E] = None ) -> _E: ...  Testing it in mypy with an actual Enum subclass: class Foo(Enum): ALPHA = 1 BETA = 2 GAMMA = 3 reveal_type(switch(Foo, Foo.ALPHA)) # Revealed type is "Foo"