eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Issue My slash commands return 404 Not Found (error code: 10062): Unknown interaction when I run them. I do have them deferred as: @bot.tree.command(name="evaluate") async def evaluate(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) await asyncio.sleep(10) await interaction.followup.send("Command works")  for example. However, when I run my commands, it raises the following error: File "/home/container/.local/lib/python3.11/site-packages/discord/app_commands/commands.py", line 862, in _do_call return await self._callback(interaction, **params) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/container/bot.py", line 282, in evaluate await interaction.response.defer(ephemeral=False, thinking=True) File "/home/container/.local/lib/python3.11/site-packages/discord/interactions.py", line 636, in defer await adapter.create_interaction_response( File "/home/container/.local/lib/python3.11/site-packages/discord/webhook/async_.py", line 218, in request raise NotFound(response, data) discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction  This happens mostly in one command, but at the very end. The entire command works fine but the bot doesn't send the final confirmation response await interaction.followup.send() and instead throws the above error. (Edited paragraph) Reason for posting: I don't really think I'm making an error with the code itself, which is why I'd like some insight as to why this is or may be happening. I also posted this in case someone else in the future encounters the same issue, since I couldn't find similar or same questions asked here on SO.

Answer: Posted this on the discord.py official Discord Server and got an answer. If a 404 Not Found error appears even after deferring, it means that it took too long for the defer() to execute. Or in other words, your await interaction.response.defer() is running after 3 seconds have passed and the API request has already been terminated. Credit: SolsticeShard on the discord.py Discord. The solution was to run defer() much earlier up the code block. That ensures that the interaction is deferred before the 3s time limit has passed.
Question: I'm writing a small script to learn Python. The script prints a chess tournament table for N players. It has a simple CLI with a single argument N. Now I'm trying the following approach: import argparse def parse_args(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description="Tournament tables") parser.add_argument('N', help="number of players (2 at least)", type=int) args = parser.parse_args(argv) if args.N < 2: parser.error("N must be 2 at least") return args.N def main(n: int) -> None: print(F"Here will be the table for {n} players") if __name__ == '__main__': main(parse_args())  But this seems to have a flaw. The function main doesn't check n for invalid input (as it's the job of CLI parser). So if somebody calls main directly from another module (a tester for example), he may call it with lets say 0, and the program most likely crashes. How should I properly handle this issue? I'm considering several possible ways, but not sure what is the best.  Add a proper value checking and error handling to main. This option looks ugly to me, as it violates the DRY principle and forces main to double the job of CLI.  Just document that main must take only n >= 2, and its behaviour is unpredicted otherwise. Possibly to combine with adding an assertion check to main, like this: assert n >= 2, "n must be 2 or more"  Perhaps such a function should not be external at all? So the whole chosen idiom is wrong and the script's entry point should be rewritten another way.  ???  

Answer: You could have main do all the checking aind raise ArgumentError if something is amiss. Then catch that exception and forward it to the parser for display. Something along these lines: import argparse def run_with_args(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description="Tournament tables") parser.add_argument('N', help="number of players (2 at least)", type=int) args = parser.parse_args(argv) try: main(args.N) except argparse.ArgumentError as ex: parser.error(str(ex)) def main(n: int) -> None: if N < 2: raise argparse.ArgumentError("N must be 2 at least") print(F"Here will be the table for {n} players") if __name__ == '__main__': run_with_args()  If you don't want to expose argparse.ArgumentError to library users of main, you can also create a custom exception type instead of it.
Question: Can someone explain why printing float16 minimal produces different results below? Is it by design or a bug?  In [87]: x=np.finfo(np.float16).min In [88]: x_array_single=np.array([x]) In [89]: x Out[89]: -65500.0 In [90]: x_array_single Out[90]: array([-65504.], dtype=float16) 

Answer: EDIT: Note that you would also get this issue if you printed the first value of the array: >>> x_array[0] -65500.0  In the NumPy 1.14.0 Release Notes, it has been written that:  Floating-point arrays and scalars use a new algorithm for decimal representations, giving the shortest unique representation. This will usually shorten float16 fractional output, and sometimes float32 and float128 output. float64 should be unaffected. See the new floatmode option to np.set_printoptions.  That's why the outputs are different. When you print it as a float32 or as a float64 (or just using the built-in float format which is 64 bits), you get the more precised output: >>> float(x) -65504.0 >>> np.float32('-65504') -65504.0  and: >>> float(x_array[0]) -65504.0  You can also see the change in the precision here: >>> np.float16('65500') == np.float16('65504')` True >>> np.float32('65500') == np.float32('65504') False
Question: How to correctly transform a Polars DataFrame to a pySpark DataFrame? More specifically, the conversion methods which I've tried all seem to have problems parsing columns containing arrays / lists. create spark dataframe data = [{"id": 1, "strings": ['A', 'C'], "floats": [0.12, 0.43]}, {"id": 2, "strings": ['B', 'B'], "floats": [0.01]}, {"id": 3, "strings": ['C'], "floats": [0.09, 0.01]} ] sparkdf = spark.createDataFrame(data)  convert it to polars import pyarrow as pa import polars as pl pldf = pl.from_arrow(pa.Table.from_batches(sparkdf._collect_as_arrow()))  try to convert back to spark dataframe (attempt 1) spark.createDataFrame(pldf.to_pandas()) TypeError: Can not infer schema for type: <class 'numpy.ndarray'> TypeError: Unable to infer the type of the field floats.  try to convert back to spark dataframe (attempt 2) schema = sparkdf.schema spark.createDataFrame(pldf.to_pandas(), schema) TypeError: field floats: ArrayType(DoubleType(), True) can not accept object array([0.12, 0.43]) in type <class 'numpy.ndarray'>  relevant: How to transform Spark dataframe to Polars dataframe?

Answer: What about spark.createDataFrame(pldf.to_dicts())  Alternatively you could do: spark.createDataFrame({x:y.to_list() for x,y in pldf.to_dict().items()})  Since the to_dict method returns polars Series instead of lists, I'm using a comprehension to convert the Series into regular lists which spark comprehends.
Question: Has anyone used a Polars dataframe with Seaborn to graph something? I've been working through a notebook on Kaggle that used Pandas, and I wanted to refactor it to Polars. The dataframe I'm working with looks like this:     PassengerID (i64) Survived (i64) Pclass (i64) Name (str) ... Ticket (str) Fare (f64) Cabin (str) Embarked (str) Age (f64)     1 0 3 your name here ... A/5 21171 7.25 null S 24   ... ... ... ... ... ... ... ... ... ...     Kaggle has me making a histogram with the following code: g = sns.FacetGrid(train_df, col='Survived') g.map(plt.hist, 'Age', bins=20)  When I run these two lines I get the following error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/axisgrid.py", line 678, in map for (row_i, col_j, hue_k), data_ijk in self.facet_data(): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/axisgrid.py", line 632, in facet_data data_ijk = data[row & col & hue & self._not_na] File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/series/series.py", line 906, in __array_ufunc__ args.append(arg.view(ignore_nulls=True)) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/internals/series/series.py", line 2680, in view ptr_type = dtype_to_ctype(self.dtype) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/polars/datatypes.py", line 550, in dtype_to_ctype raise NotImplementedError( NotImplementedError: Conversion of polars data type <class 'polars.datatypes.Boolean'> to C-type not implemented.  I don't have any boolean datatypes in my dataframe, so I'm not sure what to do about this error. Any ideas?

Answer: seaborn doesn't accept a polars dataframe as an input. You just have to use to_pandas() so change g = sns.FacetGrid(train_df, col='Survived') to g = sns.FacetGrid(train_df.to_pandas(), col='Survived')
Question: During an installation, pip is throwing an error due to version conflicts ERROR: Could not find a version that satisfies the requirement XXX==1.2.1  This is due to a package made by the company for witch I work ( not open source work ). The reason for witch it is blocking is because it is locked in python version 3.6 and I am attempting to use python 3.9 Is it possible or not to ask pip to force install a package even though it was not built / tested for this specific version of python ? To be clear, I am fully aware that this is normally not a good idea. I however have little alternatives as the team that manages that specific package no longer exists. It is a dependency we are attempting to remove but until we can, we need to use it Can I ask pip to use the latest version of the package even though it may break ? I'll add that there are no other package conflicts, just this one with python version so I should - in theory - be the only issue

Answer: I haven't tried it myself yet, but according to the documentation, pip install has the following argument that may help to bypass it: --ignore-requires-python Ignore the Requires-Python information.  https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-ignore-requires-python
Question: I am running this code using the healpy package. I am not using multiprocessing and I need it to run on a single core. It worked for a certain amount of time, but, when I run it now, the function healpy.projector.GnomonicProj.projmap takes all the available cores. This is the incriminated code block: def Stacking () : f = lambda x,y,z: pixelfunc.vec2pix(xsize,x,y,z,nest=False) map_array = pixelfunc.ma_to_array(data) im = np.zeros((xsize, xsize)) plt.figure() for i in range (nvoids) : sys.stdout.write("\r" + str(i+1) + "/" + str(nvoids)) sys.stdout.flush() proj = hp.projector.GnomonicProj(rot=[rav[i],decv[i]], xsize=xsize, reso=2*nRad*rad_deg[i]*60/(xsize)) im += proj.projmap(map_array, f) im/=nvoids plt.imshow(im) plt.colorbar() plt.title(title + " (Map)") plt.savefig("../Plots/stackedMap_"+name+".png") return im  Does someone know why this function is running in parallel? And most important, does someone know a way to run it in a single core? Thank you!

Answer: In this thread they recommend to set the environment variable OMP_NUM_THREADS accordingly:  Worked with: import os os.environ['OMP_NUM_THREADS'] = '1' import healpy as hp import numpy as np  os.environ['OMP_NUM_THREADS'] = '1' have to be done before import numpy and healpy libraries.  As to the why: probably they use some parallelization techniques wrapped within their implementation of the functions you use. According to the name of the variable, I would guess OpenMP it is.
Question: I'm trying to save data returned from an API as JSON in a SUPER type column in a table on Redshift. My request: data = requests.get(url=f'https://economia.awesomeapi.com.br/json/last/USD-BRL'  I have a function to insert the data that runs like this:  QUERY = f"""INSERT INTO {schema}.{table}(data) VALUES (%s)""" conn = self.__get_conn() cursor = conn.cursor() print('INSERT DATA') cursor.execute(query=QUERY, vars=([data, ])) conn.commit()  When try to insert the data using data.json() I get the following error: psycopg2.ProgrammingError: can't adapt type 'dict'. So I used json.dumps(data.json()) to serialize and input it. But when I look at the database the data has escape characters like this: "{\"code\": \"USD\", \"codein\": \"BRL\", \"name\": \"Dólar Americano/Real Brasileiro\", \"high\": \"5.2768\", \"low\": \"5.1848\", \"varBid\": \"-0.0264\", \"pctChange\": \"-0.5\", \"bid\": I want to use DBT to structure this dataset using JSON_PARSE() and CTEs on Redshift, but these escaping characters are in the way. What am I missing? Is there a different way to do it? The table DDL: CREATE TABLE IF NOT EXISTS public.raw_currency ( id BIGINT DEFAULT "identity"(105800, 0, '1,1'::text) ENCODE az64 ,"data" SUPER ENCODE zstd ,stored_at TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,error_log VARCHAR(65535) ENCODE lzo ) 

Answer: INSERT is an anti-pattern with Redshift; basically, inserts are very slow. And in this case, it's the conversion from JSON to string and back that's getting you. You want to use COPY when bulk-inserting data. Redshift's COPY takes a format parameter, and JSON is one of many supported formats. Docs. Note that to be copied, the data needs to be in S3, and your Redshift cluster needs access to the key in S3 where you are copying from. Lots of tutorials online for this.
Question: I have pyarrow table which have column order ['A', 'B', 'C', 'D'] I want to change the order of this pyarrow table to ['B', 'D', 'C', 'A'] can we reorder pyarrows table like pandas dataframe ?

Answer: You can use pyarrow.Table.select import pyarrow as pa table_a_b = pa.table({ "A": [1,2,3], "B": [4,5,6] }) table_b_a = table_a_b.select(['B', 'A'])      B A     4 1   5 2   6 3
Question: I individually installed (and posterior uninstalled):  python-xlib python3-xlib pyxlib xlib  via pip (un)install and could execute from Xlib import X, display, Xutil from Xlib.ext import randr d = display.Display()  with all of them with Python 3.8.10. – What is the difference between them? Pip definitively downloads and installs different packages with different sizes.

Answer: Use only  python-xlib  The other three  python3-xlib pyxlib xlib  are (seemingly) from two individuals (one holds pyxlib and xlib the other holds python3-xlib) with either broken homepage links or pointing to python-xlib. Nothing in python-xlib points to pyxlib or python3-xlib. In the best case these are just outdated snapshots of python-xlib with questionable changes turned into packages.
Question: When I try to run the server when I'm using PyCharm brings me this error   Jupyter server process exited with code 1 usage: jupyter.py [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir] [--paths] [--json] [--debug] [subcommand] Jupyter: Interactive Computing positional arguments: subcommand the subcommand to launch optional arguments: -h, --help show this help message and exit --version show the versions of core jupyter packages and exit --config-dir show Jupyter config dir --data-dir show Jupyter data dir --runtime-dir show Jupyter runtime dir --paths show all Jupyter paths. Add --json for machine-readable format. --json output paths as machine-readable json --debug output debug information about paths Available subcommands: 1.0.0 Jupyter command jupyter-notebook not found.  When running Jupyter using VSCode it works properly. I also tried reinstalling it from PyCharm packages and terminal but still doesn't work.

Answer: I didn't manage to solve this, but I found a workaround  Go to PyCharm Settings and search for Jupyter Servers  Open a Terminal, and start Jupyter notebook, typically: python3 -m notebook  Copy the URL with the token to the Configured Server field in Pycharm, click OK   You should now be able to run and debug Jupyter cells in Pycharm!
Question: I am trying to get Latex symbols in titles and labels of a Plotly figure. I am using VSCode and I run the code in Interactive Window. Latex usage looks really simple in Jupyter Notebook, from what I saw in other posts, but I can't get it to work within this environment. My env: python 3.10.4 plotly 5.9.0 vscode 1.62.3 What I tried:  use r"$$" formatting, change the font family change plotly.io.renderers.default install mathjax in my conda env and try to adapt plotly.offline mode (see https://github.com/plotly/plotly.py/issues/515)  This basic code snippet should work according to most posts I have seen but does not do the Latexrendering in the Interactive Window. It has been taken from https://plotly.com/python/LaTeX/, where everything looks so easy. That's why I am guessing the issue is related to VSCode. import plotly.graph_objs as go fig = go.Figure() fig.add_trace(go.Scatter( x=[1, 2, 3, 4], y=[1, 4, 9, 16], name=r'$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$' )) fig.add_trace(go.Scatter( x=[1, 2, 3, 4], y=[0.5, 2, 4.5, 8], name=r'$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$' )) fig.update_layout( xaxis_title=r'$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$', yaxis_title=r'$d, r \text{ (solar radius)}$' ) fig.show()  What I have  What I should have 

Answer: This is a known issue with Plotly in Jupyter notebooks in VSCode (e.g. issues #7801 and #8131). Tomas Mazak shared a workaround in #8131:  import plotly import plotly.graph_objs as go from IPython.display import display, HTML ## Tomas Mazak's workaround plotly.offline.init_notebook_mode() display(HTML( '<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_SVG"></script>' )) ## fig = go.Figure() fig.add_trace(go.Scatter( x=[1, 2, 3, 4], y=[1, 4, 9, 16], name=r'$\alpha_{1c} = 352 \pm 11 \text{ km s}^{-1}$' )) fig.add_trace(go.Scatter( x=[1, 2, 3, 4], y=[0.5, 2, 4.5, 8], name=r'$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$' )) fig.update_layout( xaxis_title=r'$\sqrt{(n_\text{c}(t|{T_\text{early}}))}$', yaxis_title=r'$d, r \text{ (solar radius)}$' ) fig.show()  Output:
Question: On my WSL im getting the error AttributeError: module 'lib' has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK' whenever I try to use pip e.g. pip list, python3 -m pip, etc. Is there a way to reinstall pip or uninstall packages without using pip? I tried following the solutions in related Questions but none of them work because they either use pip or the problem persists after.

Answer: The solution that worked for me was mentioned here. you have to remove the line:  CB_ISSUER_CHECK = _lib.X509_V_FLAG_CB_ISSUER_CHECK  from this file:  /usr/lib/python3/dist-packages/OpenSSL/crypto.py  and then you can use pip again: $ pip uninstall cryptography $ pip install --upgrade cryptography==36.0.2
Question: Is there a possibility of saving the results of keras-tuner as Dataframe? All I can find are printing functions like result_summary(), but I cannot access the printed content. The example below both prints print None, while the result_summary() still prints the best results. It looks like I will have to access the trial results by traversing through the saved files. I hoped to get a prepared table as printed by result_summary().  optimizer = keras_tuner.BayesianOptimization( hypermodel=build_model, objective='val_loss', max_trials=5, # num_initial_points=2, # alpha=0.0001, # beta=2.6, seed=1, hyperparameters=None, tune_new_entries=True, allow_new_entries=True #,min_trials=3 ) search = optimizer.search(x=train_X, y=train_Y, epochs=epochs, validation_data=(test_X, test_Y)) results = optimizer.results_summary(num_trials=10) print(results) print(search) 

Answer: If you don't need to store "score" with the hyperparameters, this should do what you want. You need to get the hyperparameters (HPs). The HPs are stored in hp.get_config() under ["values"] key. You collect a list of dicts with the HPs and convert them into DataFrame and to csv file. best_hps = optimizer.get_best_hyperparameters(num_trials=max_trials) HP_list = [] for hp in best_hps: HP_list.append(hp.get_config()["values"]) HP_df = pd.DataFrame(HP_list) HP_df.to_csv("name.csv", index=False, na_rep='NaN')  If you wish to save the scores too, you need to go through trials and concatenate the hyperparameter dict with its score as this trials = optimizer.oracle.get_best_trials(num_trials=max_trials) HP_list = [] for trial in trials: HP_list.append(trial.hyperparameters.get_config()["values"] | {"Score": trial.score}) HP_df = pd.DataFrame(HP_list) HP_df.to_csv("name.csv", index=False, na_rep='NaN')
Question: I am currently running python 3.6 on my Mac, and installed the latest version of Python (3.11) by downloading and installing through the official python releases. Running python3.11 opens the interpreter in 3.11, and python3.11 --version returns Python 3.11.0, but python -V in terminal returns Python 3.6.1 :: Continuum Analytics, Inc.. I tried to install again via homebrew using brew install python@3.11 but got the same results. More frustrating, when I try to open a virtual environment using python3 -m venv env I get Error: Command '['/Users/User/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.  I altered .bash_profile with # Setting PATH for Python 3.11 # The original version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}" export PATH . "$HOME/.cargo/env"  And created a .zprofile based on this post with export PYTHONPATH=$HOME/Users/User  and a .zshrc based on this post, but --version still throws python3.6. I'm running Big Sur OS. Pip and homebrew are up to date and upgraded. Acknowledging that I'm totally foolish, what do I need to do to get python >3.7 running in terminal?

Answer: What you want to do is overwrite a python symlink. After installing python via homebrew, you can see that python3.11 is just symlink. cd /usr/local/bin; ls -l | grep python3.11  The result is: lrwxr-xr-x 1 user admin 43 Nov 7 15:43 python3.11@ -> ../Cellar/python@3.11/3.11.0/bin/python3.11  So let's just overwrite it. ln -s -f $(which python3.11) $(which python) ln -s -f $(which python3.11) $(which python3) ln -s -f $(which pip3.11) $(which pip) ln -s -f $(which pip3.11) $(which pip3)  After these commands, pip, pip3, python3, python will invoke the version 3.11. This command makes soft symlink. ln -s  This command with -f option overwrite an existing soft symlink. A soft symlink is similar to a shortcut. In man page,which command is described as  which - shows the full path of (shell) commands.
Question: I got this error after installing Tensorflow.js. Previously this program was working. Could it be a problem with the versions? I'm really curious as to what's causing it. Thanks in advance. File ~\OneDrive\Masaüstü\Bitirme Proje\neural_network(sinir_ağları).py:61 model = build_model() File ~\OneDrive\Masaüstü\Bitirme Proje\neural_network(sinir_ağları).py:29 in build_model model = keras.Sequential([ File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\trackable\base.py:205 in _method_wrapper result = method(self, *args, **kwargs) File C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67 in error_handler raise e.with_traceback(filtered_tb) from None File C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py:3331 in saver_with_op_caching return tf.__internal__.tracking.TrackableSaver( AttributeError: module 'tensorflow.compat.v2.__internal__.tracking' has no attribute 'TrackableSaver'  I was planning to convert my model with Tensorflow.js and run it over the web. But when I installed Tensorflow.js I got this error in the program.

Answer: Update keras with pip install 'keras>=2.9.0' for keras-team/keras@af70910.  - self._trackable_saver = saver_with_op_caching(self) + self._checkpoint = tf.train.Checkpoint(root=weakref.ref(self))