eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Is there a way to type hint a tuple of elements without having to define each inner element a bunch of times? Example: a = ((1, 2), (2, 3), (3, 4), (4, 5)) a: Tuple[Tuple[int, int], Tuple[int, int], Tuple[int, int], Tuple[int, int]]  I am looking for something that may look like this a: Tuple[5*Tuple[int, int]]  as otherwise my code would become very verbose in order to indicate something like that (tuple containing 5 tuples of 4 ints)

Answer: There's a few options here. (All examples here assume a = ((1, 2), (3, 4), (5, 6), (7, 8)), i.e., a tuple consisting of 4 (<int>, <int>) tuples.) You could, as has been suggested in the comments, just use a type alias for the inner type: from typing import Tuple X = Tuple[int, int] a: Tuple[X, X, X, X]  You could also use a type alias for the whole annotation if it's still too long, perhaps using typing.Annotated to clarify what's going on in your code: from typing import Tuple, Annotated X = Tuple[int, int] IntTupleSequence = Annotated[ Tuple[X, X, X, X], "Some useful metatadata here giving " "some documentation about this type alias" ] a: IntTupleSequence  You could also use typing.Sequence, which is useful for if you have a homogenous sequence (that may be mutable or immutable) that's either of indeterminate length or would be too long to feasibly annotate using typing.Tuple: from typing import Sequence, Tuple a: Sequence[Tuple[int, int]]  You can achieve the same thing as Sequence by using Tuple with a literal ellipsis. The ellipsis here signifies that the tuple will be of indeterminate length but homogenous type. from typing Sequence, Tuple a: Tuple[Tuple[int, int], ...]  I prefer using Sequence to using Tuple with an ellipsis, however. It's more concise, seems like a more intuitive syntax to me (... is used to mean something similar to Any in other parts of the typing module such as Callable), and I rarely find there's anything gained by telling the type-checker that my immutable Sequence is specifically of type tuple. That's just my opinion, however. A final option might be to use a NamedTuple instead of a tuple for variable a. However, that might be overkill if all items in your tuple are of a homogenous type. (NamedTuple is most useful for annotating heterogeneous tuples.)
Question: I am trying to do getter setter in this simple class, class Person: def __init__(self, n): self.name = n def get_name(self): return self.name def set_name(self, n): self.name = n name = property(get_name, set_name) p = Person('Lewis') p.name = 'Philo'  Looks pretty simple and straightforward, but somehow it's not working, what am I missing, help me find what is the understanding that I am going wrong. I get the following error. Traceback (most recent call last): File "/Users/napoleon/python-play/oops/person.py", line 15, in <module> p = Person('Lewis') File "/Users/napoleon/python-play/oops/person.py", line 4, in __init__ self.name = n File "/Users/napoleon/python-play/oops/person.py", line 10, in set_name self.name = n File "/Users/napoleon/python-play/oops/person.py", line 10, in set_name self.name = n File "/Users/napoleon/python-play/oops/person.py", line 10, in set_name self.name = n [Previous line repeated 994 more times] RecursionError: maximum recursion depth exceeded 

Answer: seems like since name is a property, that you defined with get_name and set_name when you call self.name = n what it does is actually calls the setter of the property, which is set_name. when you initialize the Person object, it calls __init__ which then calls set_name since it has the line self.name = n, and the same happens in set_name itself (since it has the same line). a solution may be creating a _name member and using it in the getter and setter. in more complex cases you may check if it's not holding the value you sent to the setter, and only then change the member's value in the setter. class Person: _name = None def __init__(self, n): self._name = n def get_name(self): return self._name def set_name(self, n): self._name = n name = property(get_name, set_name)
Question: The following is a test. How to make this test fail if the subprocess.run results in an error? import pytest import subprocess @pytest.mark.parametrize("input_json", ["input.json"]) def test_main(input_json): subprocess.run(['python', 'main.py', input_json] 

Answer: subprocess.run returns a CompletedProcess instance, which you can inspect. I'm not sure what exactly you mean by "results in an error"—if it's the return code of the process being non-zero, you can check returncode. If it's a specific error message being output, check stdout or stderr instead. For example: import pytest import subprocess @pytest.mark.parametrize("input_json", ["input.json"]) def test_main(input_json): completed_process = subprocess.run(['python', 'main.py', 'input_json']) assert completed_process.returncode == 0  NB. You don't seem to use the parametrized argument input_json within your test function. Just an observation.
Question: I have a simple question. I used to create a poetry project with my package at root. project.toml mypackage +- __init__.py +- mypackage.py +- test_mypackage.py  I recently moved my tests in another directory, so that the folder now looks like. project.toml src +- mypackage +- __init__.py +- mypackage.py tests +- test_mypackage.py  Nothing changed for poetry which still work fine when I make poetry build. How does it search for the package root folder? Does it serach a folder with the package name in project.toml? Thanks for your help.

Answer: Having a folder called src to contain the package code is just a pre-defined standard that poetry recognizes without being told. It works via the packages section in your project file, which by default scans for mypackage and src/mypackage. If you provide your own value, it will stop auto-detecting those two.
Question: I have a python program that runs an infinite loop, however, every once in a while the code freezes. No errors are raised or any other message that would alert me something's wrong. I was wondering if Kubernetes has any liveness probe that could possibly help catch when the code freezes so it can kill and restart that container. I have an idea of having the python code make a periodic log every time it completes the loop. This way I can have a liveness probe check the log file every 30 seconds or so to see if the file has been updated. If the file has not been updated after the allotted time, then its is assumed the program froze and the container is killed and restarted. I am currently using the following python code to test with: #Libraries import logging import random as r from time import sleep #Global Veriables FREEZE_TIME = 60 '''Starts an infinate loop that has a 10% chance of freezing...........................................''' def main(): #Create .log file to hold logged info. logging.basicConfig(filename="freeze.log", level=logging.INFO) #Start infinate loop while True: freeze = r.randint(1, 10) #10% chance of freezing. sleep(2) logging.info('Running infinate loop...') print("Running infinate loop...") #Simulate a freeze. if freeze == 1: print(f"Simulating freeze for {FREEZE_TIME} sec.") sleep(FREEZE_TIME) #Start code with main() if __name__ == "__main__": main()  If anyone could tell me how to implement this log idea or if there is a better way to do this I would be most grateful! I am currently using Kubernetes on Docker-Desktop for windows 10 if this makes a difference. Also, I am fairly new to this so if you could keep your answers to a "Kubernetes for dummies" level I would appreciate it.

Answer: A common approach to liveness probes in Kubernetes is to access an HTTP endpoint (if the application has it). Kubernetes checks whether response status code falls into 200-399 range (=success) or not (=failure). Running a HTTP server is not mandatory as you can run a command or sequence of commands instead. In this case health status is based on the exit code (0 - ok, anything else - failure). Given the nature of your script and the idea with the log, I would wrote another python script to read the last line of that log and parse the timestamp. Then, if the difference between current time and the timestamp is greater than [insert reasonable amount] then exit(1), else exit(0). If you have prepared the health-check script, you can enable it in this way: spec: containers: - name: my_app image: my_image livenessProbe: exec: command: # the command to run - python3 - check_health.py initialDelaySeconds: 5 # wait 5 sec after start for the log to appear periodSeconds: 5 # run every 5 seconds  The documentation has detailed explanation with some great examples.
Question: Is there any way for a FastAPI "dependency" to interpret Path parameters? I have a lot of functions of the form: @app.post("/item/{item_id}/process", response_class=ProcessResponse) async def process_item(item_id: UUID, session: UserSession = Depends(security.user_session)) -> ProcessResponse: item = await get_item(client_id=session.client_id, item_id=item_id) await item.process()  Over and over, I need to pass in [multiple] arguments to fetch the required item before doing something with it. This is very repetitive and makes the code very verbose. What I'd really like to do is pass the item in as an argument to the method. Ideally I'd like to make get_item a dependency or embed it somehow in the router. This would dramatically reduce the repetitive logic and excessively verbose function arguments. The problem is that some critical arguments are passed by the client in the Path. Is it possible to pass Path arguments into a dependency or perhaps execute the dependency in the router and pass the result?

Answer: A FastAPI dependency function can take any of the arguments that a normal endpoint function can take. So in a normal endpoint you might define a path parameter like so: from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id): return {"item_id": item_id}  Now if you want to use that parameter in a dependency, you can simply do: from fastapi import Depends, FastAPI app = FastAPI() async def my_dependency_function(item_id: int): return {"item_id": item_id} @app.get("/items/{item_id}") async def read_item(item_id: int, my_dependency: dict = Depends(my_dependency_function)): return my_dependency  The parameters will simply be passed on through to the dependency function if they are present there. You can also use things like Path and Query within the dependency function to define where these are coming from. It will just analyze the request object to pull these values. Here is an example using the Path function from FastAPI: from fastapi import Depends, FastAPI, Path app = FastAPI() async def my_dependency_function(item_id: int = Path(...)): return {"item_id": item_id} @app.get("/items/{item_id}") async def read_item(my_dependency: dict = Depends(my_dependency_function)): return my_dependency  As for your concern of implementing it as a dependency in the router, you can do something like this when creating the router: items_router = APIRouter( prefix="/items", tags=["items"], dependencies=[Depends(my_dependency_function)], )  Or you can do it when you run include_router on the app like: app.include_router( items_router, prefix="/items", dependencies=[Depends(my_dependency_function)], )  For more on dependencies and more examples like this see https://fastapi.tiangolo.com/tutorial/dependencies/
Question: I'm using PyCharm 2021.2 Professional edition and I have installed opencv-python with: pip install opencv-python  However, the IDE keeps giving me the following warning when I try to use cv2 package: Cannot find reference 'resize' in '__init__.py'  Here I gave the example of the resize function, but it's happening for every function in cv2 package. Although the code runs with no errors, I can't use the auto complete feature, which is a bit annoying. I found an answer here that might help. The guy says to use: import cv2.cv2 as cv2  However this is not working for me. Im getting the following error: ERROR: No matching distribution found for cv2  That's because there is no package named cv2 inside opencv. Does anyone know how to solve this problem? Is it a PyCharm's issue? UPDATE Here is the output of the command pip show opencv-python: Name: opencv-python Version: 4.5.3.56 Summary: Wrapper package for OpenCV python bindings. Home-page: https://github.com/skvark/opencv-python Author: None Author-email: None License: MIT Location: z:\appdata\python\lib\site-packages Requires: numpy Required-by: 

Answer: This solution worked for me.  In preferences, Select Python Interpreter Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All A list of all your configured Interpreters is show with your current interpreter already hi-lighted. With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter. Click the + button and add the following path: /lib/python3.9/site-packages/cv2 The .../python3.9... will be different if you are using a different Python Version. Click Ok until you are back to the main IDE window  Tested on Mac OS 12.4, PyCharm 2022.1
Question: First, I'm using hotstick.minimal theme in oh my posh. And it looks like this.  As you can see, a current venv doesn't look good. And I made some changes in JSON file. Then it looks like this.  I don't want to display the name of venv on the left. How can I do that? This is my JSON file: { "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json", "final_space": true, "osc99": true, "console_title": true, "console_title_style": "template", "console_title_template": "{{.Folder}}{{if .Root}} :: root{{end}} :: {{.Shell}}", "blocks": [ { "type": "prompt", "alignment": "left", "segments": [ { "type": "root", "style": "plain", "foreground": "yellow", "properties": { "root_icon": "" } }, { "type": "path", "style": "powerline", "foreground": "black", "background": "#68D6D6", "powerline_symbol": "", "leading_diamond": "", "trailing_diamond": "", "properties": { "prefix": " \uF07C ", "style": "folder" } }, { "type": "python", "style": "powerline", "powerline_symbol": "\uE0B0", "foreground": "#100e23", "background": "#906cff", "properties": { "prefix": " \uE235 " } }, { "type": "git", "style": "powerline", "powerline_symbol": "", "foreground": "black", "background": "green", "properties": { "display_stash_count": true, "display_upstream_icon": true, "display_status": true, "display_status_detail": true, "branch_icon": "  ", "branch_identical_icon": "≡", "branch_ahead_icon": "↑", "branch_behind_icon": "↓", "branch_gone": "≢", "local_working_icon": "", "local_staged_icon": "", "stash_count_icon": "", "commit_icon": "▷ ", "tag_icon": "▶ ", "rebase_icon": "Ɫ ", "cherry_pick_icon": "✓ ", "merge_icon": "◴ ", "no_commits_icon": "[no commits]", "status_separator_icon": " │", "status_colors_enabled": true, "color_background": true, "local_changes_color": "yellow" } } ] } ] }  NOTE: Some symbols may not appear due to the font.

Answer: You have to include the following in your $PROFILE (profile.ps1): $env:VIRTUAL_ENV_DISABLE_PROMPT = 1  Two notes:  Deactivate the venv first Close and re-open the terminal for it to work.  See a fuller discussion here: https://github.com/JanDeDobbeleer/oh-my-posh/discussions/390
Question: I have this output :  [Pandas-profiling] ImportError: cannot import name 'ABCIndexClass' from 'pandas.core.dtypes.generic'  when trying to import pandas-profiling in this fashion : from pandas_profiling import ProfileReport  It seems to import pandas-profiling correctly but struggles when it comes to interfacing with pandas itself. Both libraries are currently up to date through conda. It doesn't seem to match any of the common problems associated with pandas-profiling as per their documentation, and I can't seem to locate a more general solution of importing the name ABCIndexClass. Thanks

Answer: Thanks to the @aflyingtoaster's answer, the following workaround has worked fine for me:  Edit the file "~/[your_conda_env_path]/lib/site-packages/visions/dtypes/boolean.py" Find the row "from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries" and just replace ABCIndexClass for ABCIndex. Save the boolean.py file and enjoy your report!
Question: I am using plotly.graph_object for 3D scatter plot. I'd like to define marker color based on category string value. The category values are A2, A3, A4. How to modify below code? Thanks Here is what I did:  import plotly.graph_objects as go x=df_merged_pc['PC1'] y=df_merged_pc['PC2'] z=df_merged_pc['PC3'] color=df_merged_pc['AREA'] fig=go.Figure(data=[go.Scatter3d(x=x,y=y,z=z,mode='markers', marker=dict(size=12, color=df_merged_pc['AREA'], colorscale ='Viridis'))]) fig.show()  The error I got is: ValueError: Invalid element(s) received for the 'color' property of scatter3d.marker Invalid elements include: ['A3', 'A3', 'A3', 'A3', 'A3', 'A3', 'A3', 'A2', 'A2', 'A2'] 

Answer: I might be wrong here, but it sounds to me like you're actually asking for a widely used built-in feature of plotly.express where you can assign a color to subgroups of labeled data. Take the dataset px.data.iris as an example with: fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')  Here, the colors are assigned to different species of which you have three unique values ['setosa', 'versicolor', 'virginica']:  sepal_length sepal_width petal_length petal_width species species_id 0 5.1 3.5 1.4 0.2 setosa 1 1 4.9 3.0 1.4 0.2 setosa 1 2 4.7 3.2 1.3 0.2 setosa 1 3 4.6 3.1 1.5 0.2 setosa 1 4 5.0 3.6 1.4 0.2 setosa 1   This example can be expanded upon by changing the color scheme like above, in which case your color scheme can be defined by a dictionary: colors = {"flower": 'green', 'not a flower': 'rgba(50,50,50,0.6)'}  Or you can specify a discrete color sequence with: color_discrete_sequence = plotly.colors.sequential.Viridis  You can also add a new column like random.choice(['flower', 'not a flower']) to change the category you would like your colors associated with.  Plotly.graph_objects If you would like to use go.Scatter3d instead I would build one trace per unique subgroup, and set up the colors using itertools.cycle like this: colors = cycle(plotly.colors.sequential.Viridis) fig = go.Figure() for s in dfi.species.unique(): df = dfi[dfi.species == s] fig.add_trace(go.Scatter3d(x=df['sepal_length'], y = df['sepal_width'], z = df['petal_width'], mode = 'markers', name = s, marker_color = next(colors)))  Complete code for plotly express import plotly.express as px import random df = px.data.iris() colors = {"flower": 'green', 'not a flower': 'rgba(50,50,50,0.6)'} df['plant'] = [random.choice(['flower', 'not a flower']) for obs in range(0, len(df))] fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color = 'plant', color_discrete_map=colors ) fig.show()  Complete code for plotly graph objects import plotly.graph_objects as go import plotly from itertools import cycle dfi = px.data.iris() colors = cycle(plotly.colors.sequential.Viridis) fig = go.Figure() for s in dfi.species.unique(): df = dfi[dfi.species == s] fig.add_trace(go.Scatter3d(x=df['sepal_length'], y = df['sepal_width'], z = df['petal_width'], mode = 'markers', name = s, marker_color = next(colors))) fig.show()
Question: I am making simple image of my python Django app in Docker. But at the end of the building container it throws next warning (I am building it on Ubuntu 20.04): WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead Why does it throw this warning if I am installing Python requirements inside my image? I am building my image using: sudo docker build -t my_app:1 .  Should I be worried about warning that pip throws, because I know it can break my system? Here is my Dockerfile: FROM python:3.8-slim-buster WORKDIR /app COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] 

Answer: The way your container is built doesn't add a user, so everything is done as root. You could create a user and install to that users's home directory by doing something like this; FROM python:3.8.3-alpine RUN pip install --upgrade pip RUN adduser -D myuser USER myuser WORKDIR /home/myuser COPY --chown=myuser:myuser requirements.txt requirements.txt RUN pip install --user -r requirements.txt ENV PATH="/home/myuser/.local/bin:${PATH}" COPY --chown=myuser:myuser . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Question: I have a class FileSet with a method _process_series, which contains a bunch of if-elif blocks doing filetag-specific formatting of different pandas.Series:  elif filetag == "EntityA": ps[filetag+"_Id"] = str(ps[filetag+"_Id"]).strip() ps[filetag+"_DateOfBirth"] = str(pd.to_datetime(ps[filetag+"_DateOfBirth"]).strftime('%Y-%m-%d')).strip() ps[filetag+"_FirstName"] = str(ps[filetag+"_FirstName"]).strip().capitalize() ps[filetag+"_LastName"] = str(ps[filetag+"_LastName"]).strip().capitalize() ps[filetag+"_Age"] = relativedelta(datetime.today(), datetime.strptime(ps[filetag+"_DateOfBirth"], "%Y-%m-%d")).years return ps  I'd like to define an abstract format method in the class and keep these blocks of formatting in separate modules that are imported when _process_series is called for a given filetag. Forgive the pseudo-code, but something like: for tag in filetag: from my_formatters import tag+'_formatter' as fmt ps = self.format(pandas_series, fmt) return ps  And the module would contain the formatting block: # my_formatters.EntityA_formatter ps[filetag+"_Id"] = str(ps[filetag+"_Id"]).strip() ps[filetag+"_DateOfBirth"] = str(pd.to_datetime(ps[filetag+"_DateOfBirth"]).strftime('%Y-%m-%d')).strip() ps[filetag+"_FirstName"] = str(ps[filetag+"_FirstName"]).strip().capitalize() ps[filetag+"_LastName"] = str(ps[filetag+"_LastName"]).strip().capitalize() ps[filetag+"_Age"] = relativedelta(datetime.today(), datetime.strptime(ps[filetag+"_DateOfBirth"], "%Y-%m-%d")).years return ps 

Answer: Why not use globals with asterisk: from my_formatters import * for tag in filetag: fmt = globals()[tag + '_formatter'] ps = self.format(pandas_series, fmt) return ps  I converted your pseudocode to real code. globals documentation:  Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).