eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to get the latest version of Python to run on my Linux Ubuntu 24.04 system, but the older version is still showing as current. There are two locations that Python is configured, '/usr/bin' and '/usr/local/bin'. How should I handle this to get the correct configuration on my system? I tried ~-> $ python --version and the response is Python 3.12.3 But, when I try ~-> $ python3 --version the response is Python 3.13.3 Python installation

Answer: Simply put one Python path in front of the other, for example if you want /usr/local/bin to be found before /usr/bin, then do this: export PATH=/usr/local/bin:${PATH}  in your shell. If you are satisfied with the result, put the above line in your ~/.bashrc.
Question: I've got my dockerfile FROM git.corp.com:4567/some/python:3.11-slim RUN apt update; \ apt install pipx -y; \ pipx install poetry; \ pipx ensurepath; \ chmod a +rx /root/.local/bin/poetry; \ ln -s /root/.local/bin/poetry /usr/bin/poetry; \  and my jenkins stage stage('Test') { agent { dockerfile{ filename 'Dockerfile.build' args "-v $WORKSPACE:/app" reuseNode true } } steps { sh """ ls -l poetry poetry install --no-root -E tests -E mypy -E lint PYTHONPATH="$PWD/src" pytest """ } }  Why do I get this message? script.sh.copy: 3: poetry: Permission denied  I've changed permessions using chmod a +rx ls -l poetry output looks like this lrwxrwxrwx 1 root root 23 Apr 11 10:02 /usr/bin/poetry -> /root/.local/bin/poetry  I know Jenkins pass -u 1000:1000 arguments to docker run command but shouldn't chmod fix this?

Answer: Try to install Poetry to the path which will be available to all users instead of installing it to /root/.local ENV PIPX_HOME=/opt/pipx \ PIPX_BIN_DIR=/usr/local/bin RUN apt update && \ apt install pipx -y && \ pipx install poetry
Question: I have a nested class that uses static vars to have class wide parameters and accumulators. If I do it as a standalone class it works. If I do a nested class and inherit the standalone class, it works. But I can't get a nested class to have static class variables, the interpreter gets confused. What am I doing wrong? Code snippet: class Cl_static_parameter_standalone: #static var common to all instances. Two uses: common settings, common accumulator c_n_counter : int = 0 @staticmethod def increment() -> int: Cl_static_parameter_standalone.c_n_counter += 1 return Cl_static_parameter_standalone.c_n_counter class Cl_some_class: class Cl_static_parameter_inherited(Cl_static_parameter_standalone): pass class Cl_static_parameter_nested: c_n_counter : int = 0 @staticmethod def increment() -> int: Cl_static_parameter_nested.c_n_counter += 1 return Cl_static_parameter_nested.c_n_counter def __init__(self): return def do_something(self): print(f"Execute Standalone: {Cl_static_parameter_standalone.increment()}") print(f"Execute Inherited: {self.Cl_static_parameter_inherited.increment()}") print(f"Execute Nested: {self.Cl_static_parameter_nested.increment()}") return my_instance = Cl_some_class() my_instance.do_something()  Output: Execute Standalone: 1 Execute Inherited: 2 Traceback (most recent call last): File "stack_overflow_class_static_parameter.py", line 52, in <module> my_instance.do_something() ~~~~~~~~~~~~~~~~~~~~~~~~^^ File "stack_overflow_class_static_parameter.py", line 48, in do_something print(f"Execute Nested:{self.Cl_static_parameter_nested.increment()}") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "stack_overflow_class_static_parameter.py", line 38, in increment Cl_static_parameter_nested.c_n_counter += 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^ NameError: name 'Cl_static_parameter_nested' is not defined. Did you mean: 'Cl_static_parameter_standalone'? 

Answer: You can use the classmethod decorator, which is more appropriate for this situation anyways: class C: class Nested: counter = 0 @classmethod def increment(cls) -> int: cls.counter += 1 return cls.counter print(C().Nested.increment()) # prints 1  If you are wondering why increment can't find Cl_static_parameter_nested in your example: you would have to write Cl_some_class.Cl_static_parameter_nested to access it from the global namespace.
Question: I need to use an iterable and a loop variable as a class property. But the flake8 checker produces B2020 warning: easy.py:11:13: B020 Found for loop that reassigns the iterable it is iterating with each iterable value.  If I use a variable for iterable there is OK. What is wrong? The warning example: #!/usr/bin/env python3 """Example of B020 error.""" class My_Template: """My example.""" def __init__(self, *template): """Obviously init.""" self.all_templates = (1, 2, 3) for self.tpl in self.all_templates: print(self.tpl)  The flake8 complains about loop variable: easy.py:11:13: B020 Found for loop that reassigns the iterable it is iterating with each iterable value.  The OK example: #!/usr/bin/env python3 """Example of B020 error.""" class My_Template: """My example.""" def __init__(self, *template): """Obviously init.""" all_templates = (1, 2, 3) for self.tpl in all_templates: print(self.tpl) 

Answer: It's a known issue: https://github.com/PyCQA/flake8-bugbear/issues/248 Understandably flake8-bugbear developers are a bit unwilling to fix this as it's not very common to use an instance attribute as the loop variable. It's also not really needed. You can simply use a normal loop variable: class My_Template: def __init__(self, *template): self.all_templates = (1, 2, 3) for tpl in self.all_templates: self.tpl = tpl print(self.tpl)
Question: I guess this must be rather simple, but I'm struggling to find the easy way of doing it. I have a pandas DataFrame with the columns A to D and need to copy some of the columns to new ones. The trick is that it not just involves renaming, I also need to duplicate the values to new columns as well. Here is an example of the input: import pandas as pd df = pd.DataFrame({ 'A': [1,2,3], 'B':['2025-10-01', '2025-10-02', '2025-10-01'], 'C': ['2025-02-10', '2025-02-15', '2025-02-20'], 'D': [0, 5, 4], 'values': [52.3, 60, 70.6] }) mapping_dict = { 'table_1': { 'id': 'A', 'dt_start': 'B', 'dt_end': 'B', }, 'table_2': { 'id': 'D', 'dt_start': 'C', 'dt_end': 'C', }, }  I'd like to have as output for table_1 a DataFrame as follows:    id dt_start dt_end values     1 2025-10-01 2025-10-01 52.3   2 2025-10-02 2025-10-02 60   3 2025-10-01 2025-10-01 80.6    And I guess it is possible to infer the expected output for table_2. Note that the column values, which is not included in the mapping logic, should remain in the dataframe. I was able to achieve this by using a for loop, but I feel that should be a natural way of doing this directly on pandas without manually looping over the mapping dict and then dropping the extra columns. Here is my solution so far: table_name = 'table_1' new_df = df.copy() for new_col, old_col in mapping_dict[table_name].items(): new_df[new_col] = df[old_col] new_df = new_df.drop(mapping_dict[table_name].values(), axis='columns')  Any help or suggestion will be appreciated!

Answer: IIUC, you can do this with this command, this is one of the reason I like to use the set_axis method in dataframes. table_name = 'table_1' df[list(mapping_dict[table_name].values())+['values']].set_axis(list(mapping_dict[table_name].keys())+['values'], axis=1)  Output:  id dt_start dt_end values 0 1 2025-10-01 2025-10-01 52.3 1 2 2025-10-02 2025-10-02 60.0 2 3 2025-10-01 2025-10-01 70.6  Or, table_name = 'table_2' df[list(mapping_dict[table_name].values())+['values']].set_axis(list(mapping_dict[table_name].keys())+['values'], axis=1)  Output:  id dt_start dt_end values 0 0 2025-02-10 2025-02-10 52.3 1 5 2025-02-15 2025-02-15 60.0 2 4 2025-02-20 2025-02-20 70.6  And, much like @khushalvaland you can create function for resuse: def gen_table(df, mapping, cols) -> pd.DataFrame: return (df[list(mapping.values())+cols] .set_axis(list(mapping.keys())+cols, axis=1)) gen_table(df, mapping = mapping_dict['table_1'], cols =['values']) gen_table(df, mapping = mapping_dict['table_2'], cols =['values'])  Output:  id dt_start dt_end values 0 1 2025-10-01 2025-10-01 52.3 1 2 2025-10-02 2025-10-02 60.0 2 3 2025-10-01 2025-10-01 70.6  and  id dt_start dt_end values 0 0 2025-02-10 2025-02-10 52.3 1 5 2025-02-15 2025-02-15 60.0 2 4 2025-02-20 2025-02-20 70.6
Question: plt.figure(figure=(6,8)) sns.barplot(data=csat_korean,x='grade',y='percentage').set_title('Korean'); plt.show() sns.barplot(data=csat_math,x='grade',y='percentage').set_title('Math'); plt.show() sns.barplot(data=csat_english,x='grade',y='percentage').set_title('English'); plt.show()  Hello, the above code is me trying to plot with sns(everything is imported). However, the plots I get are not constant in size. This is the result I get. How would I fix this code so that the plots are same in size, and the last graph is not annoyingly smaller than the other plots? Greatly appreciate it!

Answer: With seaborn's catplot(), you can generate a grid of bar plots, starting from a combined dataframe. The size of the subplots is set via the height= and aspect= parameters (width = height * aspect). By default, the x and y axis are shared between the subplots, so they look very similar. from matplotlib import pyplot as plt import seaborn as sns import pandas as pd import numpy as np # generate some dummy test data courses = ['Math', 'English', 'Korean'] csat_data = pd.DataFrame({'course': np.repeat(courses, 9), 'grade': np.tile(np.arange(1, 10), 3), 'count': np.random.randint(10, 1000, size=27)}) csat_data['percentage'] = csat_data.groupby('course')['count'].transform(lambda x: (x / x.sum()) * 100) g = sns.catplot(csat_data, kind='bar', x='grade', y='percentage', row='course', height=8, aspect=6/8) plt.show()   PS: For your original dataframes, you can combine them like: csat_math['course'] = 'Math' csat_english['course'] = 'English' csat_korean['course'] = 'Korean' csat_data = pd.concat([csat_math, csat_english, csat_korean])  You can also let Seaborn calculate the histograms directly from the original data. courses = ['Math', 'English', 'Korean'] csat_data = pd.DataFrame({'course': np.repeat(courses, 50), 'grade': np.clip(np.random.normal(loc=6, scale=1.8, size=150).round().astype(int), 1, 9)}) g = sns.displot(csat_data, kind='hist', stat='percent', x='grade', discrete=True, row='course', height=8, aspect=6/8) g.set(xticks=range(1, 10)) plt.show(block=True)
Question: Morning, I'm not sure if this can be achieved.. Let's say i have a polars dataframe with cols a, b (whatever). df = pl.DataFrame({"a":[1,2,3,4,5],"b":['x','y','z','p','f']})  And a list.. l = [1,3,5,2,4]; is it possible to sort the dataframe (using column "a") using the list l as the sorting order? Thanks in advance!

Answer: You can use an Enum to sort with a custom order, however as Enum only works with strings, you first need to temporarily convert to string: df.sort(by=pl.col('a').cast(pl.String).cast(pl.Enum(list(map(str, l)))))  Output: ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ str │ ╞═════╪═════╡ │ 1 ┆ x │ │ 3 ┆ z │ │ 5 ┆ f │ │ 2 ┆ y │ │ 4 ┆ p │ └─────┴─────┘
Question: I would like to use the decorator deco to reshape the __enter__ and __exit__ instance methods. The code runs but the wrapper is not executed in the with section. Find the actual output below, followed by expected output and finally the code. Current output: In __init__ ------------------- In __enter__ ------------------- In wrapper Blah, blah, blah ------------------- In __exit__  Expected output: In __init__ ------------------- In wrapper In __enter__ ------------------- In wrapper Blah, blah, blah ------------------- In wrapper In __exit__  class contMgr(): def __init__(self): print("In __init__") pass def __enter__(self): print("In __enter__") return self def __exit__(self, exc_type, exc_val, exc_tb): print("In __exit__") pass def __call__(self, *args, **kwargs): print("In __call_") def brol(): print("brol") def deco(func): def wrapper(*args,**kwargs): print("In wrapper") result = func(*args, **kwargs) return result return wrapper @deco def test(): print("Blah, blah, blah") mgr = contMgr() mgr.__enter__ = deco(mgr.__enter__) mgr.__exit__ = deco(mgr.__exit__) print("------------------------") with mgr: print("------------------------") test() print("------------------------") 

Answer: The short answer As others have pointed out, the problem is that it's the __enter__ and __exit__ methods of the class that will get called, not those of the instance. But you said this is for testing purposes, and you don't want the decorators to be there permanently, so the solution is to alter the class methods temporarily. You can do it manually, of course: change the function on the class, run the test, and change it back, but unittest.mock.patch can automate that for you. Using patch Here's how you can use patch as a context manager which will restore the original function once you leave the context manager's scope: with (patch("__main__.contMgr.__enter__", deco(contMgr.__enter__)), patch("__main__.contMgr.__exit__", deco(contMgr.__exit__))): with mgr: test()  will call the decorated enter and exit only inside this with statement. Here's a longer example with output: print("Using patch") with (patch("__main__.contMgr.__enter__", deco(contMgr.__enter__)), patch("__main__.contMgr.__exit__", deco(contMgr.__exit__))): with mgr: test() print("\nNot using patch") with mgr: test()  Output: Using patch In wrapper In __enter__ In wrapper Blah, blah, blah In wrapper In __exit__ Not using patch In __enter__ In wrapper Blah, blah, blah In __exit__  Documentation https://docs.python.org/3/library/unittest.mock.html#patch
Question: I am currently creating an application in python which uses the Youtube Data API and includes the following code block: import os from subprocess import run os.chdir(os.path.dirname(__file__)) command = 'python3.10 uploadYoutube.py --file="blank.mp4" --title="Blank" --description="THIS IS YOUR BLANK VIDEO" --keywords="blank" --category="20" --privacyStatus="private"' terminal_output = run(command, capture_output=True).stdout print(terminal_output)  This produces the error:  terminal_output = run(command, capture_output=True).stdout File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 503, in run with Popen(*popenargs, **kwargs) as process: File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 971, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1456, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified  Does anybody know how I could fix this error?

Answer: as Adon Bilivit stated, this is due to the first argument you pass to the command subprocess.run. As stated in the subprocess documentation, when you provide a string there is a platform dependent interpretation. On POSIX, providing a string  is interpreted as the name or path of the program to execute.  Solution The first solution is to pass a sequence to the first subprocess.run argument subprocess.run(command.split(), capture_output=True)  You can also, as the documentation suggests, set shell to True.  If shell is True, it is recommended to pass args as a string rather than as a sequence.  subprocess.run(command, capture_output=True, shell=True)
Question: I have this code in a CLI: @click.option( "--username", default=lambda: os.environ.get("USER", None), show_default=True, help="User name for SSH configuration.", )  When I invoke the CLI with --help option, I get this:  --username TEXT User name for SSH configuration. [default: (dynamic)]  Is there a way to make click invoke the lambda function and show the actual username instead of (dynamic)? I know I can call that function before invoking the click decorator and pass that retrieved value as the default instead of lambda. I am trying to do better than that.

Answer: The Option.get_default method has a call option to call the default value when it's a callable. The option is True by default but is passed False only when generating help, so you can make the help generator call the the callable simply by overriding Option.get_default with a wrapper that forces the call option to be True: import click class DefaultCallingOption(click.Option): def get_default(self, ctx, call=True): return super().get_default(ctx, True)  so that: @click.command() @click.option( "--username", default=lambda: 'foo', show_default=True, help="User name to say hello to.", cls=DefaultCallingOption # use the custom Option class ) def hello(username): print(f'hello {username}') hello()  produces the following help when given the --help option in the command line: Usage: test.py [OPTIONS] Options: --username TEXT User name to say hello to. [default: foo] --help Show this message and exit.
Question: I have two Rust crates lib1 and lib2, for which I use pyo3 to generate bindings. I import the crate lib1 in lib2. In a separate crape lib1-py, I create a python class MyClass, and in lib2, I try to use MyClass as a parameter for a Python function, using : fn foo(param: PyRef<MyClass>)  I separately use maturin to build both lib1-py and lib2 in the same environment. When I try to run the python code: import lib1 import lib2 param = lib1.MyClass() lib2.foo(param)  I obtain the error TypeError: argument 'param': 'MyClass' object cannot be converted to ‘MyClass’. I suppose that Rust's crate system tries to be safe by treating types from different crates as separate entities, even if they are identical. How could I solve such an issue?

Answer: In general, this is impossible. Rust does not even guarantee MyClass will have the same memory layouts in both builds. It is possible to design lib1-py to allow this, though. For example, if the entirety of MyClass is #[repr(C)] (that is, it and all of its fields are, recursively), you can take a Bound<PyAny> and convert it to Bound<MyClass> with into_ptr() and from_owned_ptr(). Although that is a dangerous method; if something will someday become not #[repr(C)] your code will become unsound. A probably better method (and what e.g. Polars does) is to have a method to serialize MyClass to some format, accessible from Python, and a corresponding method to deserialize (which doesn't need to be accessible from Python). Then you take Bound<PyAny> and call this method on it, and deserialize the result.
Question: I have a fixed table with (name,value) pairs that are known. I.e.: Bob.age: 47 Bill.age: 44 Jane.age: 36 Steve.age: 22  I'd like a user to be able to write in a json file, a statement to dynamically generate a conditional statement to evaluate to true/false for a report: (Bob.age == Jane.age) || (Bob.age < Bill.age)  I can do this manually via regex but I was wondering if there was already a tried and tested library for this kind of thing.

Answer: There are many template languages implemented in Python. There are whole list of "tried and tested libraries": https://www.fullstackpython.com/template-engines.html , https://wiki.python.org/moin/Templating . Additionally, you can execute arbitrary strings as python with eval. Sky is the limit. An example solution would be to use jinja2 template language. You json could look like the following: { "key": {% if (Bob.age == Jane.age) or (Bob.age < Bill.age) %}true{% else %}false{% endif %} }
Question: I'm building a flask app to process images uploaded from a mobile device, before sending results back to the mobile app. I've successfully deployed the flask app on Azure, and can confirm it works with certain paths, /test for one. The path my mobile app uses to upload an image, is /upload-data. When I run my flask app locally on my own PC and connect to it, this path works fine, accepting the image uploaded and returning a response. However, when using the flask app deployed on Azure, the response I get for /upload-data is just 405 (Method not allowed). This is the top part of my code in my python flask backend: @app.route("/upload-data", methods=['POST']) def store_data(): #Image processing code etc...  And this is how I make the post to the above path in my java android app with OkHTTP: private void uploadData(String imagePath, String pointCloudPath) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(responseTimeOut, TimeUnit.SECONDS) .build(); File imageFile = new File(imagePath); //Create request body RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("image", imageFile.getName(), RequestBody.create(imageFile, MediaType.parse("image/jpeg"))) .build(); //Create request itself String url = "http://" + backendUrl; Request request = new Request.Builder() .url(url+"/upload-data") .post(requestBody) .build(); //Execute request in background Log.d("OkHTTP Image Upload", "Creating image upload request to: " + request.url()); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { e.printStackTrace(); Log.e("OkHTTP Image Upload", "Failed: " + e.getMessage()); runOnUiThread(() -> textStatus.setText("Request Failed: " + e.getMessage())); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { Log.d("OkHTTP Upload", "Success"); InputStream responseZipStream = response.body().byteStream(); runOnUiThread(() -> processAnalysisResponse(responseZipStream)); } else { Log.e("OkHTTP Image Upload", "Error: "+response.code()); runOnUiThread(() -> textStatus.setText("Request Error: "+response.code())); } } }); } 

Answer: I notice you're making your requests over HTTP not HTTPS, I imagine Azure is stricter on the POST requests rather than GET due to encrypting the body of the request. Make sure you have HTTPS only turned off in Azure Portal.
Question: I have a string 'manual__2025-04-08T11:37:13.757109+00:00' and I want 'manual__2025-04-08T11_37_13_00_00' I know how to substitute the : and + using 'manual__2025-04-08T11:37:13.757109+00:00'.replace(':','_').replace('+','_')  but I also want to get rid of the numbers between the two characters '.' and '+'. I'm using python.

Answer: You could do a regex substitution using re.sub: >>> import re >>> s = 'manual__2025-04-08T11:37:13.757109+00:00' >>> new_s = re.sub(r'\.\d+(?=\+)', '', s).replace(':', '_').replace('+', '_') >>> new_s 'manual__2025-04-08T11_37_13_00_00'