eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: When using src layout rather than flat layout in a Python project, is anything other than the project module expected to live in the src folder? My understanding is that if I added mypkg2 under src in the layout below, and published the result to PyPI, anyone who did a pip install would be able to import mypkg and import mypkg2 (which might be surprising). Am I missing something? project_root_directory ├── pyproject.toml # AND/OR setup.cfg, setup.py ├── ... └── src/ └── mypkg/ ├── __init__.py ├── ... ├── module.py ├── subpkg1/ │ ├── __init__.py │ ├── ... │ └── module1.py └── subpkg2/ ├── __init__.py ├── ... └── module2.py  Sample layout from https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#src-layout I haven't been able to find an example of a project with anything else present, nor an explicit instruction not to put anything else in there. I am looking for a PEP or packaging document that answers this question.

Answer: In a correct src-layout project, the only things that should be present in the src directory are the top-level import packages and top-level import modules (sub-packages are in the sub-directories of the top-level packages, obviously). By convention each distribution package contains one and only one top-level import package (or top-level import module). And I guess that this convention is the "de facto standard" that is asked for in the question. In the case of the example shown in the question, if the project had both src/mypkg and src/mypkg2 directories, then the project would have 2 top-level import packages, which is unconventional (but possible). Famous example of project not following this convention is setuptools, it has 2 top-level imports: setuptools and pkg_resources. I do not know of any authoritative documentation that clarifies this specific point of the src-layout, the closest I can find is "src layout vs flat layout" on the Python Packaging User Guide.
Question: An "Agree with the terms" button appears on https://www.sreality.cz/hledani/prodej/domy I am trying to go through that with a .click() using Selenium and Python. The button element is: <button data-testid="button-agree" type="button" class="scmp-btn scmp-btn--default w-button--footer sm:scmp-ml-sm md:scmp-ml-md lg:scmp-ml-dialog">Souhlasím</button>  My approach is: driver = webdriver.Chrome() driver.implicitly_wait(20) driver.get("https://www.sreality.cz/hledani/prodej/domy") button = driver.find_element_by_css_selector("button[data-testid='button-agree']") button.click()  Any idea what to change to make it work? Thanks! :)

Answer: Check the below working workaround solution: driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get("https://www.sreality.cz/hledani/prodej/domy") driver.maximize_window() # Below line creates instance of ActionChains class action = ActionChains(driver) # Below line locates and stores an element which is outside the shadow-root element_outside_shadow = driver.find_element(By.XPATH, "//div[@class='szn-cmp-dialog-container']") # Below 2 lines clicks on the browser at an offset of co-ordinates x=5 and y=5 action.move_to_element_with_offset(element_outside_shadow, 5, 5) action.click() # Below 2 lines presses TAB key 9 times so that pointer moves to "Souhlasím" button and presses ENTER key once action.send_keys(Keys.TAB * 9).perform() action.send_keys(Keys.ENTER).perform()  Imports required: from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By  NOTE:  This is a workaround solution, as there is no direct selenium solution to handle it.
Question: I have a question, I know that it is possible to do global exception handling in python, but how to do it for all views, namely, I am interested in DetailView, but others will also come in handy.I understand that you need to give your own examples of solutions, but could you give an example of how this can be done, because I have no ideas at all how to do it. I want to do global error handling so that it can be done like this  try: current_profile = Profile.objects.get(user_connected_id=current_user_object) except ObjectDoesNotExist SomeERROR....: logger.info('Object user_connected_id not found....') raise ObjectDoesNotExist  And if I don't handle the error that appears, then I want it to work in the global handler. And if it's not difficult: what should I do correctly with errors in this handler, other than logging? I need the DetailView handler most of all

Answer: The methodology here involves a couple things. First, within those views, you want to make sure you are getting those errors within an except block as your code does. Then you should return an HttpResponseRedirect object redirecting the user to the error page or another page where you would like for the user to return. The next thing you can do is return a particular view for all errors of a type on the site when they hit urls.py. For example, to redirect all 404 errors, add the following code to the base site urls.py handler404 = 'App.views.error_404_view'  Then, in the view simply return the custom error page you'd like or perform a new HttpResponseRedirect def error_404_view(request, exception): return render(request, 'error_404.html')
Question: Good morning, everyone. I'm trying to try to find the differences between two rows. I'm trying to put a formula together, but I feel like I'm making things too complicated when an easier answer may be available, and my code's not perfect. Here's my example dataset. cols = ['Name', 'Math', 'Science', 'English', "History"] data = [['Tom', 100, 93, 95, 92], ['Nick', 89, 75, 82, 57], ['Julie', 99, 89, 76, 88], ['Sarah', 79, 78, 94, 88]] df = pd.DataFrame(data, columns=cols) df  The output is this:  My current (and non-working) formula is: students = ['Tom', 'Nick', 'Julie', 'Sarah'] differences = [] def student_diff(student): for col in df.columns[1:]: for classmate in students: differences.append(abs(student[col] - classmate[col])) print (student, differences.mean()) student_diff('Tom')  The error is: TypeError: string indices must be integers  All in all, I was hoping the output would be something like (for example with Tom): Nick 19.25 

Answer: # student to find difference student = 'Tom' # create your mask where the name is the student name mask = df['Name'].eq(student) # concat you masks together and set the index data = pd.concat([df[mask], df[~mask]]).set_index('Name') # get the mean from the differnce abs(data.iloc[0, :] - data.iloc[1:, :]).mean(axis=1) Name Nick 19.25 Julie 7.00 Sarah 10.25 dtype: float64  or if you did want a function def student_diff(df: pd.DataFrame, student: str) -> pd.Series: # create your mask where the name is the student name mask = df['Name'].eq(student) # concat you masks together and set the index data = pd.concat([df[mask], df[~mask]]).set_index('Name') # get the mean from the differnce return abs(data.iloc[0, :] - data.iloc[1:, :]).mean(axis=1) student_diff(df=df, student='Nick') Name Tom 19.25 Julie 15.25 Sarah 14.00 dtype: float64
Question: I am using VS Code for developing in Python. I have been able to debug single test cases from the test module, which is very practical. Since recently, it no longer works. After a short waiting time, a dialog pops up: "Invalid message: Found duplicate in "env": PATH." with the buttons "Open launch.json" and "Cancel". Opening launch.json does not help, since I don't manipulate PATH there. I also did not edit PATH recently (and the message seems to point not to a wrong value in PATH, but to PATH being set multiple times). I tried to see the duplicate PATH myself, but failed: Both echo ${env:PATH} at the console and os.environ in Python will only give a single PATH variable - as already implied by the used data structure. I tried applying this SO answer. It does neither relate directly to VS Code, nor to duplicate PATH variables, but to duplicates in PATH, but it was the closest I found on here. It didn't work.

Answer: You can install different versions of the Python plugin (to whom it may concern: if this works unsatisfactorily, it may be because you have multiple instances of VS Code running). You will note:  2023.4.1 is the latest known stable version that works 2023.6.0 does not work v2023.7.11011538 (latest) works  It seems it's an issue in the latest stable version the VS Code team have solved in the meanwhile.
Question: For example, I might have an abstract base class with some abstract method that takes some mutable type as a parameter: from abc import * class AbstractClass(metaclass=ABCMeta): @abstractmethod def abstract_method(self, mutable_parameter: list | set): raise NotImplementedError  Is there some way of hinting to the function implementer that this parameter should not be modified in any implementation of this method? I imagine maybe something like ReadOnly could exist: def abstract_method(self, mutable_parameter: ReadOnly[list]):  but I can't seem to find anything suggesting such a thing does exist. From looking at the typing module docs I would have assumed that Final was what I am looking for but PyCharm tells me 'Final' could not be used in annotations for function parameters.

Answer: You might be able to find a structural superclass that provides the behaviors you want. There's a good summary of the available collections classes at collections.abc, but as a quick, non-exhaustive summary.  If you're just planning to iterate over the collection in some order, you're looking for Iterable. If all you're doing is using the infix in operator to check for membership, you can use Container. Collection is Iterable plus Container plus the len function. Sequence gives you Collection, __getitem__ (i.e. the square brackets operator), and several other nice list-y things. This is a good general-purpose candidate for list-like structures.  None of these protocols have any mutating methods, so if you adhere strictly to your types (and don't downcast), then you can be assured that a function taking these types will not mutate the argument. Note that, prior to PEP 585 (released in Python 3.9), the collections.abc classes were non-generic. If you need to be compatible with Python versions prior to that, the typing module contains the same class names as collections.abc, and those were generic since day one. And, finally, the glory of structural superclasses is that you can define them anytime with typing.Protocol. If you can't find a protocol that suits your needs, then just make one. Are you planning to call __getitem__ but want to support structures that don't have a len? Here's your protocol! from typing import Protocol, TypeVar T = TypeVar("T", covariant=True) class Gettable(Protocol[T]): def __getitem__(self, index: int, /) -> T: ...  Since this class inherits from Protocol, when used in type hints it will behave as a structural subtype, meaning any object which has a __getitem__ that takes an int and returns a T counts as a Gettable[T], even without an explicit runtime subtyping relationship.
Question: I have been trying to make a Python program that connects to a Planetscale MySQL DB, I have used 2 libraries that are mysql-connector-python and mysqlclient. I think I have entered the correct details every time with both but it hasn't worked. I tried Planetscale's recommended way which is the following (it didn't work for me btw) : import MySQLdb # pip install mysqlclient # This is planetscale's copy/paste method to connect to the DB conn = MySQLdb.connect( host=[HOST], user=[USER], passwd=[PASSWORD], db=[DATABASE], ssl_mode = "VERIFY_IDENTITY", ssl = { "ca": "/etc/ssl/cert.pem" } )  This is the current code I'm using import mysql.connector # pip install mysql-connector-python config = { "user": hidden for security purposes, "password": hidden for security purposes, "host": hidden for security purposes, "database": hidden for security purposes, "ssl_verify_identity": True, "ssl_ca": "/etc/ssl/cert.pem", } conn = mysql.connector.connect(**config) cursor = conn.cursor() cursor.execute("""CREATE TABLE IF NOT EXISTS qr_table ( type VARCHAR(255), date VARCHAR(255), path VARCHAR(255), unique_id VARCHAR(255) )""") conn.commit() cursor.close() conn.close()  This is the error I'm getting Traceback (most recent call last): File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 268, in _open_connection self._cmysql.connect(**cnx_kwargs) _mysql_connector.MySQLInterfaceError: SSL connection error: SSL_CTX_set_default_verify_paths failed The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\Axelr\PycharmProjects\PC01\main\Special Main\ARC QR Generator\SQL Script.py", line 26, in <module> conn = mysql.connector.connect(**config) File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\pooling.py", line 286, in connect return CMySQLConnection(*args, **kwargs) File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 101, in __init__ self.connect(**kwargs) File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\abstracts.py", line 1108, in connect self._open_connection() File "C:\Users\Axelr\Python coding\lib\site-packages\mysql\connector\connection_cext.py", line 273, in _open_connection raise get_mysql_exception( mysql.connector.errors.InterfaceError: 2026 (HY000): SSL connection error: SSL_CTX_set_default_verify_paths failed Process finished with exit code 1  Can someone please help me make a connection that works? Also, it wouldn't be a problem to use another library to connect. [Python 3.10]

Answer: I have fixed the issue. The problem was the SSL and that I used a dictionary to store the details, then used **config to use the dictionary in the connection. Here is the correct and working code for me: import mysql.connector as sql conn = sql.connect(host="*Hidden For Security Purposes*", database="*Hidden For Security Purposes*", user="*Hidden For Security Purposes*", password="*Hidden For Security Purposes*", )  Thanks to everyone helping me with this problem and i hope this StackOverflow question will help many with the same problem.
Question: In TypeScript, we have the ability to create a "literal" type based on the keys of an object: const tastyFoods = { pizza: '🍕', burger: '🍔', iceCream: '🍦', fries: '🍟', taco: '🌮', sushi: '🍣', spaghetti: '🍝', donut: '🍩', cookie: '🍪', chicken: '🍗', } as const; type TastyFoodsKeys = keyof typeof tastyFoods; // gives us: // type TastyFoodsKeys = "pizza" | "burger" | "iceCream" | "fries" | "taco" | "sushi" | "spaghetti" | "donut" | "cookie" | "chicken"  Is there an equivalent in Python (3.10+ is fine) for creating type hints based on a dict? E.g., from typing import Literal tasty_foods = { "pizza": '🍕', "burger": '🍔', "iceCream": '🍦', "fries": '🍟', "taco": '🌮', "sushi": '🍣', "spaghetti": '🍝', "donut": '🍩', "cookie": '🍪', "chicken": '🍗', } TastyFoodsKeys = Literal[list(tasty_foods.keys())] # except that doesn't work, obviously 

Answer: There is not. You need to define the Literal type statically: TastyFoodsKeys = Literal["pizza", "burger"]  You can, however, use TastyFoodsKeys.__args__ to then define your dict. tasty_foods = dict(zip(TastyFoodsKeys.__args__, ['🍕', '🍔']))  The __args__ attribute is not documented, so use at your own risk. As pointed out in a comment, you can use typing.get_args (which is documented) instead of accessing __args__ directly: tasty_foods = dict(zip(typing.get_args(TastyFoodsKeys), ['🍕', '🍔']))   You might, however, not want to use a dict at all. Perhaps what you really want is an enumerated type. from enum import StrEnum class TastyFood(StrEnum): PIZZA = '🍕' BURGER = '🍔' # etc
Question: I have installed Python 3.11 from Windows Store. I used to have Python 3.10 also installed from Windows store, but I changed the environment variables and could not use it from the terminal anymore. Therefore I decided to uninstall it and install the latest version, hoping that it would be added to PATH automatically, just like it happened the first time I installed the previous version. The reason why I am installing it from Windows store is because that is the only interpreter that VSCode is able to find (I have tried accessing another Python interpreter and VSCode would just not allow me). At the moment, this Python interpreter has the following route C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1008.0_x64__qbz5n2kfra8p0\python3.11.exe I have copy-pasted it to my Path and still can't run python on a terminal. What I mean by this is that if I type "python", the command is not recognized. This is the complete directory in which the .exe file is, in case it is useful  EDIT I also have the following route C:\Users\Usuario\AppData\Local\Programs\Python\Python311 That contains the following  I have a vague memory of the Scripts folder being in the Path before, but adding it does not solve the issue either. I have also added the above route (where the Scripts folder is) and still does not work. What can I do to be able to run python from terminal?

Answer: I finally solved it by looking at another device where I had a similar configuration. I needed to add C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps and C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps\python3.11.exe to the path. That worked. Still don't know why Windows Store didn't add them automatically this time tho.
Question: I want to use vectorization to create a column in a pandas data frame that retrieve the second/last part of a string, from each row in a column, that is split on '_'. I tried this code: df = pd.DataFrame() df['Var1'] = ["test1_test2","test3_test4"] df['Var2'] = [[df['Var1'].str.split('_')][0]][0] df Var1 Var2 0 test1_test2 test3 1 test3_test4 test4  Which is obviously incorrect as I should get test2 and test 4 in row 0 and 1 of column Var2 respectively.

Answer: Use the .str.split('_') method along with .str[-1] to retrieve the second/last part of each string in the column. Following is the updated code: import pandas as pd df = pd.DataFrame() df['Var1'] = ["test1_test2", "test3_test4"] df['Var2'] = df['Var1'].str.split('_').str[-1] print(df)  Output:  Var1 Var2 0 test1_test2 test2 1 test3_test4 test4  In the above code, df['Var1'].str.split('_') splits each string in the 'Var1' column by the '_' delimiter, and .str[-1] selects the last part of the split string for each row.
Question: I am writing a custom Action Plugin for Ansible which I use in my playbook and I am trying to set a variable that will be used in the next task, in the playbook, by a (custom) module. Effectively, the playbook equivalent of what I am trying to mimic is a set_fact task like so: - name: set_fact task set_fact: ansible_python_interpreter: /path/to/python  In my custom Action Plugin, I have used self._execute_module before to execute other modules (such as slurp) within the plugin code. However, with the set_fact module it doesn't seem to be updating the ansible_python_interpreter variable, as expected. I have tried the following: self._execute_module(module_name='ansible.builtin.set_fact', module_args=dict(ansible_python_interpreter=/path/to/python), task_vars=task_vars)  And I have also tried different variations of module_args: module_args=dict(key_value={ansible_python_interpreter=/path/to/python})  module_args=dict(key_value='ansible_python_interpreter:/path/to/python')  However, my ansible_python_interpreter does not seem to be changing. Any help, please?

Answer: The closest I can get is to return a dict containing the Ansible facts that I want to be set for the playbook simply by following the dev guide for Action Plugins on the Ansible docs. So, as I am returning an _execute_model() call in my plugin as well, my run() function in my plugin would look something like this: def run(self, tmp=None, task_vars=None): # Plugin code here facts = dict() facts['ansible_python_interpreter'] = '/path/to/python' return dict(self._execute_module(module_name='my_custom_module', module_args=module_args, task_vars=task_vars), ansible_facts=dict(facts))  However, unfortunately this throws another warning/error of: [WARNING]: Removed restricted key from module data: ansible_python_interpreter And this seems to be due to a safety mechanism for overriding connection details, and so I have gone down a different route for my plugin. In another use case, returning dict(ansible_facts=dict(facts)) (like in the docs) would work if it wasn't a connection var I was trying to override, I believe.
Question: I am trying to run a python script to run all cells in all notebooks found a directory. It runs fine and I am getting the desired results in the notebook files. However, I want to disable the warnings that are printed to the VSCode cmd terminal when running the script. My code below: import nbformat from glob import glob from nbconvert.preprocessors import ExecutePreprocessor if __name__ == "__main__": nb_list = glob("./*.ipynb") ep = ExecutePreprocessor() for nb in nb_list: with open(nb) as f: nb_r = nbformat.read(f, as_version=4) ep.preprocess(nb_r)  The console output:  0.00s - Debugger warning: It seems that frozen modules are being used, which may 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off 0.00s - to python to disable frozen modules. 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.   Tried setting "env": {"PYDEVD_DISABLE_FILE_VALIDATION":"1"} in the launch.json file. Didn't change anything.  Tried setting "pythonArgs": ["-Xfrozen_modules=off"] in the launch.json file. Didn't change anything.  Tried setting warnings.filterwarnings('ignore', module='ExecutePreprocessor'). Didn't change anything.  Tried setting os.environ['PYTHONWARNINGS'] = ''. Didn't change anything.  Tried setting os.environ['PYDEVD_USE_CYTHON'] = '1'. Didn't change anything.  What I haven't tried is setting PYDEVD_DISABLE_FILE_VALIDATION=1. I don't know where to set this, how to set it, and the implications.  

Answer: Figured out how to "Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation". Adding a user or system environment variable called 'PYDEVD_DISABLE_FILE_VALIDATION' and setting the value to '1' did the job. Didn't know this is what it meant (newbie alert).
Question: I have a polars dataframe, like: import polars as pl df = pl.DataFrame({"foo": [1.0, 2.0, 3.0], "bar": [11, 5, 8]})  How do I convert the first column to int64 type? I was trying something like: df.select(pl.col('foo')) = df.select(pl.col('foo')).cast(pl.Int64)  but it is not working. In Pandas it was super easy: df['foo'] = df['foo'].astype('int64')  Thanks.

Answer: Select your col, cast it to (int64) and add it back to the original DataFrame with_columns. df = df.with_columns(pl.col("foo").cast(pl.Int64))  Output : print(df) shape: (3, 2) ┌─────┬─────┐ │ foo ┆ bar │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ 11 │ │ 2 ┆ 5 │ │ 3 ┆ 8 │ └─────┴─────┘