eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I like to use __getattr__ to give objects optional properties while avoiding issues with None. This is a simple example: from typing import Any class CelestialBody: def __init__(self, name: str, mass: float | None = None) -> None: self.name = name self._mass = mass return None def __getattr__(self, name: str) -> Any: if f"_{name}" not in self.__dict__: raise AttributeError(f"Attribute {name} not found") out = getattr(self, f"_{name}") if out is None: raise AttributeError(f"Attribute {name} not set for {self.name}") return out  My problem is that Python tries to be nice when I raise my custom AttributeError and exposes the "private" attribute for which I am creating the interface: AttributeError: Attribute mass not set for Earth. Did you mean: '_mass'?  Is there a standard way to suppress these recommendations?

Answer: The name recommendation logic is triggered only if the name attribute of the AttributeError is not None: elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ getattr(exc_value, "name", None) is not None: wrong_name = getattr(exc_value, "name", None) suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name) if suggestion: self._str += f". Did you mean: '{suggestion}'?"  So you can easily disable the suggestion by setting name to None when instantiating an AttributeError: if out is None: raise AttributeError(f"Attribute {name} not set for {self.name}", name=None)  Demo here
Question: Problem While solving the Longest Repeating Subsequence problem using bottom-up dynamic programming, I started running into an edge case whenever a letter was repeated an odd number of times. The goal is to find the longest subsequence that occurs twice in the string using elements at different indices. The ranges can overlap, but the indices should be disjoint (i.e., str[1], str[4] and str[2], str[6] can be a solution, but not str[1], str[2] and str[2], str[3]). EDIT: Misunderstood example. (See the comment) Minimum Reproducible Example s = 'AXXXA' n = len(s) dp = [['' for i in range(n + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, n + 1): if (i != j and s[i - 1] == s[j - 1]): dp[i][j] = dp[i - 1][j - 1] + s[i - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) print(dp[n][n])  Question Any pointers on how to avoid this? With input s = 'AXXXA', the answer should be either A or X, but the final result returns XX, apparently pairing up the third X with both the first X and the second X. False Start I don't want to add a check on a match (s[i - 1] == s[j - 1]) to see if s[i - 1] in dp[i - 1][j - 1] because another input might be something like AAJDDAJJTATA, which must add the A twice.

Answer: Actually, your initial algorithm and its answer are correct (... but this is a good question because others might confuse what an LRS means). Given your input (in), the subsequences (s1, s2) are: in: AXXXA s1: XX s2: XX  So XX (length 2) is indeed the correct answer here. X would be the correct answer for the problem's non-overlapping version, where the ranges - not just individual indices - must be disjoint.
Question: I'm trying to train a model with Yolov8. Everything was good but today I suddenly notice getting this warning apparently related to PyTorch and cuDNN. In spite the warning, the training seems to be progressing though. I'm not sure if it has any negative effects on the training progress. site-packages/torch/autograd/graph.py:744: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at ../aten/src/ATen/native/cudnn/Conv_v8.cpp:919.) return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass  What is the problem and how to address this? Here is the output of collect_env: Collecting environment information... PyTorch version: 2.3.0+cu118 Is debug build: False CUDA used to build PyTorch: 11.8 ROCM used to build PyTorch: N/A OS: Ubuntu 20.04.6 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Clang version: Could not collect CMake version: version 3.29.3 Libc version: glibc-2.31 Python version: 3.9.7 | packaged by conda-forge | (default, Sep 2 2021, 17:58:34) [GCC 9.4.0] (64-bit runtime) Python platform: Linux-5.15.0-69-generic-x86_64-with-glibc2.31 Is CUDA available: True CUDA runtime version: 11.8.89 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA A100 80GB PCIe Nvidia driver version: 515.105.01 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.8.0 /usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.8.0 HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True CPU: Architecture: x86_64 Versions of relevant libraries: [pip3] numpy==1.26.4 [pip3] onnx==1.16.0 [pip3] onnxruntime==1.17.3 [pip3] onnxruntime-gpu==1.17.1 [pip3] onnxsim==0.4.36 [pip3] optree==0.11.0 [pip3] torch==2.3.0+cu118 [pip3] torchaudio==2.3.0+cu118 [pip3] torchvision==0.18.0+cu118 [pip3] triton==2.3.0 [conda] numpy 1.24.4 pypi_0 pypi [conda] pytorch-quantization 2.2.1 pypi_0 pypi [conda] torch 2.1.1+cu118 pypi_0 pypi [conda] torchaudio 2.1.1+cu118 pypi_0 pypi [conda] torchmetrics 0.8.0 pypi_0 pypi [conda] torchvision 0.16.1+cu118 pypi_0 pypi [conda] triton 2.1.0 pypi_0 pypi 

Answer: June 2024 Solution: Upgrade torch version to 2.3.1 to fix it: pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Question: I am trying to install Python packages, like lietorch or pytorch-scatter with AMD support. I download the repository, git clone --recursive https://github.com/repo  If I just try to install the package, I'll get an error, stating  clang: error: cannot find HIP runtime; provide its path via '--rocm-path', or pass '-nogpuinc' to build without HIP runtime  Therefore, I change the extra_compile_args inside setup.py, adding '-DCMAKE_CXX_FLAGS=--rocm-path=/opt/rocm'  to the nvcc flags. The flags get passed and is executed by hipcc: /opt/rocm-6.0.2/bin/hipcc _various_compile_flags_ -DCMAKE_CXX_FLAGS=--rocm-path=/opt/rocm-6.0.2 _even_more_flags_  But it terminates with the error sh: 1: --rocm-path=/opt/rocm-6.0.2/llvm/bin/clang: not found sh: 1: --rocm-path=/opt/rocm-6.0.2/llvm/bin/clang: not found error: command '/opt/rocm-6.0.2/bin/hipcc' failed with exit code 127  Is there anything I can do to get the packages built? Do these packages have insufficient support for AMD and there is nothing I can do? Or is there a grave oversight on my part and an obvious solution? Some System Information: GPU: AMD Radeon RX 6800 XT (gfx1030)  Environment Variables: ROCM_PATH=/opt/rocm ROCM_ROOT=/opt/rocm ROCM_HOME=/opt/rocm-6.0.2 CPATH=/opt/rocm-6.0.2/include:/opt/rocm-6.0.2/include/hip:/opt/rocm-6.0.2/include/rocrand:/opt/rocm-6.0.2/include/hiprand:/opt/rocm-6.0.2/include/roctracer:/opt/rocm-6.0.2/include/hipblas:/opt/rocm-6.0.2/include/hipsparse:/opt/rocm-6.0.2/include/hipfft:/opt/rocm-6.0.2/include/rocsolver HIP_PATH=/opt/rocm/hip HCC_HOME=/opt/rocm/hcc HIP_PLATFORM="amd" PYTORCH_ROCM_ARCH="gfx1030" HSA_OVERRIDE_GFX_VERSION=10.3.0 HIP_VISIBLE_DEVICES=0  I tried setting the rocm-path flag as described above and looked for help in github issues and Stack Overflow posts without success. I expect the packages to compile successfully and run on my hardware.

Answer: While searching for an answer I stumbled upon this Clang documentation. It mentions the Order of Precedence for HIP Path:  Order of Precedence for HIP Path  --hip-path compiler option  HIP_PATH environment variable (use with caution)  --rocm-path compiler option  ROCM_PATH environment variable (use with caution)  Default automatic detection (relative to Clang or at the default ROCm installation location)    Including --hip-path=/opt/rocm inside the extra_compile_args let me compile the packages.
Question: I'm trying to run some Python unit tests on a remote build server using Teamcity. They fail when attempting to execute some matplotlib code. I get the following output in the Teamcity build logs, which seems to point towards the matplotlib backend as the culprit.  XXXXX\stats.py:144: in PerformHypothesisTest fig, ax = plt.subplots(1, 1, figsize=(10, 6)) .venv\lib\site-packages\matplotlib\pyplot.py:1702: in subplots fig = figure(**fig_kw) .venv\lib\site-packages\matplotlib\pyplot.py:1022: in figure manager = new_figure_manager( .venv\lib\site-packages\matplotlib\pyplot.py:545: in new_figure_manager return _get_backend_mod().new_figure_manager(*args, **kwargs) .venv\lib\site-packages\matplotlib\backend_bases.py:3521: in new_figure_manager return cls.new_figure_manager_given_figure(num, fig) .venv\lib\site-packages\matplotlib\backend_bases.py:3526: in new_figure_manager_given_figure return cls.FigureCanvas.new_manager(figure, num) .venv\lib\site-packages\matplotlib\backend_bases.py:1811: in new_manager return cls.manager_class.create_with_canvas(cls, figure, num) .venv\lib\site-packages\matplotlib\backends\_backend_tk.py:479: in create_with_canvas with _restore_foreground_window_at_end(): C:\Python310\lib\contextlib.py:135: in __enter__ return next(self.gen) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ @contextmanager def _restore_foreground_window_at_end(): > foreground = _c_internal_utils.Win32_GetForegroundWindow() E ValueError: PyCapsule_New called with null pointer .venv\lib\site-packages\matplotlib\backends\_backend_tk.py:43: ValueError  The tests run fine both:  Locally on my PC from Pycharm On the build server when executed from the command line, i.e. by running python -m pytest  I'm not super familiar with how Teamcity works and how to debug it, so I would appreciate any ideas as to what might be going wrong here. The build server is running the following versions:  Python 3.10.0 Matplotlib 3.9.0 Pytest 8.2.1  If it is useful, the build server is using the 'tkagg' backend (from matplotlib.get_backend()). Update: Thanks for the responses. As snark says, the issue seems to be due to a bug in the most recent Matplotlib release (3.9.0). Until this is fixed, I've dealt with this by explicitly setting the Matplotlib backend to 'Agg' as suggested by BadCaffe and Lemmy. I did this programmatically via a pytest conftest.py file.

Answer: I had the same Issue, it has something to do with the missing physical video out (on a server with a nightly build, you usually have no monitor connected). This is why it works on your private machine. matplotlib.use('Agg')  should work, but if you have completely automatic nightly builds, that pull down some kind of repository and you can not alter the code, look here: https://matplotlib.org/stable/users/explain/customizing.html#the-matplotlibrc-file You can place a "matplotlibrc" file in the user folder (depending on your os) and simply put "backend : Agg" in there. This does the same thing as matplotlib.use('Agg') in changing the used backend, but you can do it without changing the code.
Question: I used the following code to load my custom-trained tokenizer: from transformers import AutoTokenizer test_tokenizer = AutoTokenizer.from_pretrained('raptorkwok/cantonese-tokenizer-test')  It took forever to load. Even if I replace the AutoTokenizer with PreTrainedTokenizerFast, it still loads forever. How to debug or fix this issue?

Answer: The problem is resolved when downgrading transformers version to 4.28.1 from 4.41.0. Both pipeline() and from_pretrained() load the tokenizer successfully in seconds.
Question: Current Style  Target Style  When I generate a percentage stacked bar chart by openpyxl, I would like it as the target style in the image attached. But what I got is the current one. The code is as below. I tried change the chart.style number. It doesn't work. Environment: Window 10 + Python 3.10 (VS 2017) + Excel 365 def chart_add_A(filename): workbook = load_workbook(filename) worksheet = workbook['A'] chart_data = Reference(worksheet, min_row = 2, max_row = worksheet.max_row, min_col = 2, max_col = 3) chart_series = Reference(worksheet, min_row = 2, max_row = worksheet.max_row, min_col = 1, max_col = 1) chart_A = BarChart() chart_A.type = 'col' chart_A.style = 3 chart_A.grouping = "percentStacked" chart_A.title = 'A' chart_A.x_axis.title = 'Month' chart_A.y_axis.title = 'Count' chart_A.legend = None chart_A.dLbls=label.DataLabelList() chart_A.dLbls.showVal=True chart_A.showVal = True chart_A.width = 24 chart_A.height = 12 chart_A.add_data(chart_data) chart_A.set_categories(chart_series) worksheet.add_chart(chart_A, 'A0') workbook.save(filename)  How can I get the target style to save the chart in excel? Thanks

Answer: Referenced in the Openpyxl documentation, Stacked Bar Charts should have their overlap set to 100. Need to add the following setting; chart_A.overlap = 100
Question: I'm trying to read an opc ua extension object in python with the following code: from opcua import Client from opcua import ua connessione= 'opc.tcp://10.1.17.21:4840/' client = Client(connessione) try: client.connect() var = client.get_node('ns=1;s=VARIABLE_OPC_first_coiling_machine_production_list') before = var.get_value() client.load_type_definitions() after = var.get_value() print(before, '\n', after) client.disconnect() except Exception as e: print(e.__str__())  But in 'after' I get a non-decipherable arraybyte. Example:  ExtensionObject(TypeId:StringNodeId(ns=1;s=ENC_DATATYPE_OPC_coiling_machine_production_list), Encoding:1, 2311 bytes)  I also try with opcua-asyncio library, but nothing change.

Answer: Try opcua-asyncio library and call the function load_data_type_definitions.
Question: I tried to install the requirements for Grok on my server(I don’t have much disk space). dm_haiku==0.0.12 jax[cuda12-pip]==0.4.25 -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html numpy==1.26.4 sentencepiece==0.2.0  But I found they are so big that the disk quota exceeded. Thus, I wanted to uninstall them. I used pip uninstall -r requirements.txt to uninstall these packages and deleted all the cache files by using rm -rf ~/.cache/*. However, the disk usage was still very high. How can I clean up these packages?

Answer: pip uninstall will remove only the requirements you pass to it, not all other things they depend on. List the full set of installed packages in your environment, like this: pip freeze  Choose the subset you want to uninstall, and pass them to pip uninstall. pip uninstall --yes -r requirements-to-remove.txt  This could possibly result in some packages being broken... use pip check to check for that (so you don't have to wait until a runtime error happens to discover that). pip check  For example: # run a Python 3.11 environment in Docker docker run --rm -it python:3.11 bash # install 'lightgbm' and 'pandas' (which both depend on 'numpy') pip install lightgbm pandas # that pulls in other dependencies, like 'numpy' and 'pytz' pip freeze # lightgbm==4.3.0 # numpy==1.26.4 # pandas==2.2.2 # ... # if you just uninstall 'pandas', 'numpy' will still be in the environment pip uninstall --yes pandas pip freeze | grep numpy # numpy==1.26.4 # if you then uninstall 'numpy' too, it'll break 'lightgbm' pip uninstall --yes numpy python -c "import lightgbm" # ModuleNotFoundError: No module named 'numpy' # you could detect this with 'pip check' pip check # lightgbm 4.3.0 requires numpy, which is not installed. # scipy 1.13.0 requires numpy, which is not installed. # that could be fixed by reinstalling 'numpy' pip install numpy
Question: Suppose I have a Pandas DataFrame, I take some easy example: import pandas as pd df = pd.DataFrame(columns=["A", "B"], data = [(1, 2), (4, 5), (7, 8), (10, 11)])  I have a set of indices, let's make it simple and random: inds = [(0, 1, 3), (0, 1, 2), (1, 2, 3)]  I want to aggregate the data according to those indices, in the following way, for instance if the aggregation operation is the mean I would obtain:    A B     df.loc[inds[0], "A"].mean() df.loc[inds[0], "B"].mean()   df.loc[inds[1], "A"].mean() df.loc[inds[1], "B"].mean()   df.loc[inds[2], "A"].mean() df.loc[inds[2], "B"].mean()    Is there a way to perform this in pure pandas without writing a loop? This is very similar to a df.groupby and then .agg type of operation, but I did not find a way to create a GroupBy object from a custom set of indices.

Answer: Edit: showing how to achieve this with groupby, but surely "significantly simpler to think of this as a selection by index problem"; see the answer by @HenryEcker.  Option 1 (reindex + groupby) s = pd.Series(inds).explode() out = df.reindex(s).groupby(s.index).mean() out A B 0 5.0 6.0 # i.e. A: (1+4+10)/3, B: (2+5+11)/3, etc. 1 4.0 5.0 2 7.0 8.0  Explanation  Use inds to create a pd.Series (here: s), and apply series.explode. The index values function as group identifiers:  # intermediate series ('group 0, 1, 2') 0 0 0 1 0 3 1 0 1 1 1 2 2 1 2 2 2 3 dtype: object   Apply df.reindex with values from s, use df.groupby with s.index, and get groupby.mean.  Option 2 (merge + groupby) out = ( df.merge( pd.Series(inds, name='g').explode(), left_index=True, right_on='g', how='right' ) .drop(columns=['g']) .groupby(level=0) .mean() ) # same result  Explanation  As with option 1, we create a pd.Series and explode it, but this time we add a name, which we need for the merge in the next step. Now, use df.merge with how=right to add the values from df using g values from our series and index from df as the keys. Finally, drop column 'g' (df.drop), apply df.groupby on the index (level=0), and get groupby.mean.
Question: I'm a new user of FastAPI. I'm writing a small web application and I'm wondering if it's good practice to validate additional information, which is not directly related to the object itself, using the Pydantic model itself? For example, checking if a user with such a name exists in the database. For example: class CreateUser(BaseModel): model_config = ConfigDict(strict=True) username: str = Field(pattern=r"[0-9a-zA-Z!@#$%&*_.-]{3,}") password: str secret: str @field_validator("username") def validate_username(cls, value: str): # check if user is exist in DB... # if no, return the username # if yes, raise error 

Answer: In my opinion you shouldn't do that. In FastAPI, to me, Pydantic acts as a gateway to the path operation function(whether it is input or output). It deserializes, cleans(and/or converts) and validates the data so that you can count on that the data you receive is in the good shape. The rest is going to be passed to the path operation function or service layer if you have. I'll decouple the job of checking the availability of the user in database from Pydantic models.
Question: I have a Django project configured with Stripe for payments. I have created promocodes for discounts, which can only be redeemed once per customer. The problem is that I want to validate whether a given customer has already redeemed a specific promocode. If they haven't redeemed it, I want to proceed to the next step; otherwise, I want to stop the customer at that point. I have retrieve the promocode but it returns the following values: { "active": true, "code": "test10", "coupon": { "amount_off": null, "created": 1716289929, "currency": "usd", "duration": "once", "duration_in_months": null, "id": "test10", "livemode": false, "max_redemptions": null, "metadata": {}, "name": null, "object": "coupon", "percent_off": 10.0, "redeem_by": null, "times_redeemed": 1, "valid": true }, "created": 1716289930, "customer": null, "expires_at": 1716595200, "id": "promo_1PIqc2B67hjAZnF4umv6pZCP", "livemode": false, "max_redemptions": null, "metadata": {}, "object": "promotion_code", "restrictions": { "first_time_transaction": true, "minimum_amount": null, "minimum_amount_currency": null }, "times_redeemed": 1 }  I am not able to find an API that validates whether a customer has already redeemed a given promocode. I'm expecting a method or API endpoint in Stripe that allows me to check if a specific customer (customer ID) has redeemed a specific promocode. I have created The promocode which is only be redeemed once per customer.

Answer: You would need to either:  create individual one-time Promotion Code objects for each customer, and then list them by customer and code (in case you want to use the same code) to check if it was used, or update Customer's metadata field, to record if a given promo code was used.
Question: I have the input dataframe in the following format: id city state date param value unit 1 Phoenix AZ 4-21-2024 temp 100 F 2 Phoenix AZ 4-21-2024 prec 0 mm 3 Phoenix AZ 4-21-2024 wind 2 mph 4 Phoenix AZ 4-20-2024 temp 101 F 5 Phoenix AZ 4-20-2024 prec 0 NaN 6 Phoenix AZ 4-20-2024 wind 4 mph 7 Seattle WA 4-20-2024 temp 82 F 8 Seattle WA 4-20-2024 prec 3 mm 9 Seattle WA 4-20-2024 wind 5 mph  I want this data pivoted into this format: id city state date temp prec wind temp_unit prec_unit wind_unit 1 Phoenix AZ 4-21-2024 100 0 2 F mm mph 2 Phoenix AZ 4-20-2024 101 0 4 F mph 3 Seattle WA 4-20-2024 82 3 5 F mm mph  How do I pivot the dataframe on param and get the value and its corresponding unit?

Answer: You can pivot, flatten the MultiIndex, then reset_index: out = df.pivot(index=['city', 'state', 'date'], columns='param', values=['value', 'unit']) out.columns = out.columns.map(lambda x: f'{x[0]}_{x[1]}' if x[0]!='value' else x[1]) out.reset_index(inplace=True)  Output:  city state date prec temp wind unit_prec unit_temp unit_wind 0 Phoenix AZ 4-20-2024 0 101 4 NaN F mph 1 Phoenix AZ 4-21-2024 0 100 2 mm F mph 2 Seattle WA 4-20-2024 3 82 5 mm F mph
Question: So I have this simple code to run. import streamlit as st st.title("hellobored")  I created a docker image of this code with a simple earthfile with the command earthly +all VERSION 0.8 streamlit: FROM python:3.11-slim WORKDIR /src/app RUN apt-get update && apt-get install -y curl RUN python -m pip install --upgrade pip RUN pip install streamlit COPY ./hellobored.py /src/app/hellobored.py RUN ls EXPOSE 8501 ENTRYPOINT ["streamlit", "run", "/src/app/hellobored.py", "--server.port=8501"] SAVE IMAGE hellobored all: BUILD +streamlit  I have a Docker Compose file that looks like this: version: '3.8' services: streamlit: image: hellobored ports: - "8501:8501" develop: watch: - path: ./ action: rebuild - path: ./ target: /src/app/ action: sync  I already tried using rebuild or sync separately, but both don't seem to work. I used docker compose up -d and docker compose watch to see the watch-related problem and received this error for the rebuild:  [+] Running 1/0manualy ✔ Container hellobored-streamlit-1 Running 0.0s can't watch service "streamlit" with action rebuild without a build context  And this error for the sync:  [+] Running 1/0 ✔ Container hellobored-streamlit-1 Running 0.0s none of the selected services is configured for watch, consider setting an 'develop' section  I can't find a way to give Docker Compose a build context for Earthly on the net, so I'm asking here. For note, I need the build to be done by Earthly because the images I create are 10GB, and Earthly can compile them in seconds. I need to watch because it would be really long to rebuild and reload manually after every change.

Answer: In your initial setup, the docker-compose-watch command failed because there was no build context specified. Docker Compose requires a build context to track changes and trigger rebuilds. Without a build context, the watch command cannot determine which files to monitor for changes. build: context: .