eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Say I have a standard matplotlib polar plot: import matplotlib.pyplot as plt import numpy as np r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax. Plot(theta, r) ax.set_rmax(2) ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line ax. Grid(True) ax.set_title("A line plot on a polar axis", va='bottom') plt.show()  Which yields (source):  In my case, the results are not trustful between certain angles (e.g. between 0º and 45º in the above figure), so I would like to change the plot background color, but only between those angles; i.e. apply ax.set_facecolor only between certain angles. Is it possible? I could not find online an example for that.

Answer: You can use axvspan(...) to color between two angles (in radians). import matplotlib.pyplot as plt import numpy as np r = np.arange(0, 2, 0.01) theta = 2 * np.pi * r fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ax.plot(theta, r) ax.set_rmax(2) ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line ax.grid(True) startangle = 0 endangle = 45 ax.axvspan(np.deg2rad(startangle), np.deg2rad(endangle), facecolor='red', alpha=0.3) plt.show()
Question: I would like to sort a polars dataframe based on absolute value of a column in either ascending or descending order. It is easy to do in Pandas, or using sorted function in python. Let's say I want to sort based on val column in the below dataframe. import numpy as np np.random.seed(42) import polars as pl df = pl.DataFrame({ "name": ["one", "one", "one", "two", "two", "two"], "id": ["C", "A", "B", "B", "C", "C"], "val": np.random.randint(-10, 10, 6) })  Returns: ┌──────┬─────┬─────┐ │ name ┆ id ┆ val │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ i32 │ ╞══════╪═════╪═════╡ │ one ┆ C ┆ -4 │ │ one ┆ A ┆ 9 │ │ one ┆ B ┆ 4 │ │ two ┆ B ┆ 0 │ │ two ┆ C ┆ -3 │ │ two ┆ C ┆ -4 │ └──────┴─────┴─────┘  Thanks!

Answer: You want to sort based on the absolute value of 'val'? Expressions are your friend, no need for temporary columns: In [33]: df.sort(pl.col('val').abs()) Out[33]: shape: (6, 3) ┌──────┬─────┬─────┐ │ name ┆ id ┆ val │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ i64 │ ╞══════╪═════╪═════╡ │ two ┆ B ┆ 0 │ │ two ┆ C ┆ -3 │ │ one ┆ C ┆ -4 │ │ one ┆ B ┆ 4 │ │ two ┆ C ┆ -4 │ │ one ┆ A ┆ 9 │ └──────┴─────┴─────┘
Question: I've got two dataframes, one of which is a timestamp, and the other also has timestamps, but has gaps in it. The two dataframes have some overlap in timestamps, and some not. MWE: This code creates the first dataframe: daterange = pd.date_range(start='1/1/2023 09:30:00', end='1/3/2023 09:35:00', freq = 'min') daterange_keep = (pd.DatetimeIndex(pd.to_datetime(daterange)) .indexer_between_time('09:30', '09:35') ) firstdf= pd.DataFrame(daterange[daterange_keep]) firstdf.columns = ['timestamp'] firstdf  This creates the following dataframe, with times from 9:30 to 9:33, 1 Jan to 3 Jan:  The second dataframe looks like this: seconddf = pd.DataFrame({'timestamp': ['2023-01-01 09:30:00', '2023-01-01 09:32:00', '2023-01-01 09:34:00', '2023-02-01 09:30:00'], 'value': [3,5,7,9]}) seconddf   I want to merge the two dataframes, keeping all the timestamps in the first dataframe and inserting NaNs for the missing data from the second dataframe, and dropping all the data in the second frame that isn't in the first frame. The desired output is:  What is the best way to do this? (Ideally I'll also be able to rename the 'value' column but I assume I can do that independently of the merge.) The obvious way appears to be firstdf.merge(seconddf, how = 'inner'), but this yields an error that says I should use concat instead, and I can't figure out how concat can achieve this merge.

Answer: Do you missed to convert timestamp of your second dataframe to datetime64? seconddf['timestamp'] = pd.to_datetime(seconddf['timestamp']) out = firstdf.merge(seconddf, on='timestamp', how='left')  Output: >>> out timestamp value 0 2023-01-01 09:30:00 3.0 1 2023-01-01 09:31:00 NaN 2 2023-01-01 09:32:00 5.0 3 2023-01-01 09:33:00 NaN 4 2023-01-01 09:34:00 7.0 5 2023-01-01 09:35:00 NaN 6 2023-01-02 09:30:00 NaN 7 2023-01-02 09:31:00 NaN 8 2023-01-02 09:32:00 NaN 9 2023-01-02 09:33:00 NaN 10 2023-01-02 09:34:00 NaN 11 2023-01-02 09:35:00 NaN 12 2023-01-03 09:30:00 NaN 13 2023-01-03 09:31:00 NaN 14 2023-01-03 09:32:00 NaN 15 2023-01-03 09:33:00 NaN 16 2023-01-03 09:34:00 NaN 17 2023-01-03 09:35:00 NaN  You can also use: dmap = seconddf.set_index('timestamp')['value'] firstdf['value'] = firstdf['timestamp'].map(dmap)  Using indexing: out = (seconddf.set_index('timestamp') .reindex(firstdf['timestamp']) .reset_index())
Question: I want to display an svg file using PySide. But I would prefer not having to create a file.svg and then load it using QSvgWidget Rather I just have the file contents of the file.svg contents already stored in a variable as a string. How would I go about directly loading the SVG from the variable rather than form the file. Best

Answer: You need to convert the string to a QByteArray. From the QSvgWidget.load documentation:  PySide6.QtSvgWidgets.QSvgWidget.load(contents) PARAMETERS: contents – PySide6.QtCore.QByteArray  Here's a full demo: from PySide6.QtWidgets import QApplication from PySide6.QtSvgWidgets import QSvgWidget from PySide6.QtCore import QByteArray import sys svg_string="""<svg width="50" height="50"> <circle cx="25" cy="25" r="20"/> </svg>""" svg_bytes = QByteArray(svg_string) app = QApplication(sys.argv) svgWidget = QSvgWidget() svgWidget.renderer().load(svg_bytes) svgWidget.show() sys.exit(app.exec())
Question: I’ve read through VS Code’s documentation on creating a virtual environment and I’ve used the venv command to do so, but how do I know if my current Python files are working using that Virtual Environment? In the folder I’m saving my .py files in, I see the .venv folder which was created. Is that it or is there more to it? I’ve used VS Code’s terminal commands and followed the documentation instructions, but there is no documentation to tell me exactly what I’m supposed to see when it works, so I don’t know if I’m done.

Answer: You want to check if you are in the venv or not: Type which python into VS Code's integrated terminal. If you are using the venv, it will show the path to the python executable inside the venv. For example, for me, the .venv folder is located in /Users/lion/example_folder, but for you the path would be different.  If the path does not include the .venv folder inside your Python Projects folder, then you are not inside the venv. For example, the path shown by the command is /usr/bin/python, but the .venv is at /user/Python Projects. You are not inside the venv and you want to activate it Inside VS Code's terminal, type source .venv/bin/activate (or specify the absolute path source ~/example_folder/.venv/bin/activate).
Question: I have a Django application running on Kubernetes, using an API for health checks. The issue I'm facing is that every time the IP associated with Django in Kubernetes changes, I have to manually update ALLOWED_HOSTS. django code: class HealthViewSet(ViewSet): @action(methods=['GET'], detail=False) def health(self, request): try: return Response('OK', status=status.HTTP_200_OK) except Exception as e: print(e) return Response({'response': 'Internal server error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)  deployment code : livenessProbe: httpGet: path: /health/ port: 8000 initialDelaySeconds: 15 timeoutSeconds: 5  Error: Invalid HTTP_HOST header: '192.168.186.79:8000'. You may need to add '192.168.186.79' to ALLOWED_HOSTS. Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/django/utils/deprecation.py", line 135, in __call__ response = self.process_request(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/django/middleware/common.py", line 48, in process_request host = request.get_host() ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/django/http/request.py", line 148, in get_host raise DisallowedHost(msg) django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '192.168.186.79:8000'. You may need to add '192.168.186.79' to ALLOWED_HOSTS. Bad Request: /health/  Is there a way to dynamically use ALLOWED_HOSTS and avoid manual updates? (Every deployment IP changed.) ALLOWED_HOSTS ALLOWED_HOSTS = [localhost', '127.0.0.1']  Any guidance or suggestions for the best solution in this regard would be appreciated.

Answer: The ALLOWED_HOSTS setting checks the Host header sent on an HTTP request, so you can simply configure the headers sent by your liveliness probe: livenessProbe: httpGet: path: /health/ port: 8000 httpHeaders: - name: host value: your.hostname.here # Configure the appropriate host here initialDelaySeconds: 15 timeoutSeconds: 5
Question: I have a preexisting database with around 15 PDFs stored. I want to be able to search the database so that I'm getting the X most relevant results back given a certain threshold using cosine similarity. Currently, I've defined a collection using this code: chroma_client = chromadb.PersistentClient(path="TEST_EMBEDDINGS/CHUNK_EMBEDDINGS") collection = chroma_client.get_or_create_collection(name="CHUNK_EMBEDDINGS")  I've done a bit of research and it seems to me that while ChromaDB does not have a similarity search, FAISS does. However, the existing solutions online describe to do something along the lines of this: from langchain.vectorstores import Chroma db = Chroma.from_documents(texts, embeddings) docs_score = db.similarity_search_with_score(query=query, distance_metric="cos", k = 6)  I am unsure how I can integrate this code or if there are better solutions.

Answer: ChromaDB does have similarity search. The default is L2, but you can change it as documented here. collection = client.create_collection( name="collection_name", metadata={"hnsw:space": "cosine"} # l2 is the default )
Question: Edit: MacOS Sonoma Version 14.2.1 I am running a python script via crontab, and the script runs, but I get an error when trying to iterate the ~/.Trash directory: PermissionError: [Errno 1] Operation not permitted: '/Users/me/.Trash' I have enabled full disk access for: /usr/sbin/cron, /usr/bin/crontab, and terminal.app, but still have the same problem. If I run the command directly, it works fine, but when cron runs it, I get the error above. I have tried a few different crontab entries, but get the same result from all of them (I've ran each version directly and each works fine when not ran via cron).  */5 * * * * /Users/me/miniforge3/envs/dev/bin/fclean >> /dev/null 2>&1  */5 * * * * /Users/me/miniforge3/envs/dev/bin/python /Users/me/miniforge3/envs/dev/bin/fclean >> /dev/null 2>&1  */5 * * * * /Users/me/miniforge3/envs/dev/bin/python /Users/me/path/to/file.py >> /dev/null 2>&1   if it's helpful the python function that's raising the permission issue is: def clean_folder(folder: Path, _time: int = days(30)) -> None: """ If a file in the specified path hasn't been accessed in the specified days; remove it. Args: folder (Path): Path to folder to iterate through _time (int): optional time parameter to pass as expiration time. Returns: None """ for file in folder.iterdir(): if expired(file, _time): try: rm_files(file) except PermissionError as permission: logging.exception(permission) continue except Exception as _err: logging.exception(_err) continue 

Answer: I cross posted this issue in the apple developer forums: Here In one of the responses I was linked a really great thread that helps explain some of what is going on: Here Here's the snippet that's most applicable to the situation.  Scripting   MAC presents some serious challenges for scripting because scripts are run by interpreters and the system can’t distinguish file system operations done by the interpreter from those done by the script. For example, if you have a script that needs to manipulate files on your desktop, you wouldn’t want to give the interpreter that privilege because then any script could do that.   The easiest solution to this problem is to package your script as a standalone program that MAC can use for its tracking. This may be easy or hard depending on the specific scripting environment. For example, AppleScript makes it easy to export a script as a signed app, but that’s not true for shell scripts.  I was able to provide Full Disk Access to the python interpreter and that allows cron to run the python script and access the ~/.Trash directory. As @eskimo1 points out in the article - that means any script running in that environment has Full Disk Access. So I will be looking at creating a package for my script in the future.
Question: In Python, how do I correctly define a classmethod of a parent class that references an attribute of a child class? from enum import Enum class LabelledEnum(Enum): @classmethod def list_labels(cls): return list(l for c, l in cls.__labels.items()) class Test(LabelledEnum): A = 1 B = 2 C = 3 __labels = { 1: "Label A", 2: "Custom B", 3: "Custom label for value C + another string", } print(Test.list_labels()) # expected output # ["Label A", "Custom B", "Custom label for value C + another string"]  In the code above I expect that Test.list_labels() will correctly print out the labels, however because the __labels dictionary is defined with the double underscore, I cannot access it correctly. The reason I wanted to have double underscore is to make sure that the labels would not show up when iterating over the enumerator, e.g. list(Test) should not show the dictionary containing labels.

Answer:  Note: This answer was originally a comment to the question. I strongly advise taking a different approach, like:  Ethan Furman's answer (author of Python's Enum)  Or juanpa.arrivillaga's answer     Python 3.11+ I do not suggest using private names. That being said, if for some reason you must use private names and you can't use the @enum.nonmember decorator, which is a much better approach. Then the following will work in Python 3.11+. The _Private__names section in Enum HOWTO states:  Private names are not converted to enum members, but remain normal attributes.  You could do something really ugly like: getattr(cls, f"_{cls.__name__}__labels", {})  from enum import Enum class LabelledEnum(Enum): @classmethod def list_labels(cls): # account for private name mangling labels = getattr(cls, f"_{cls.__name__}__labels", {}) return list(l for c, l in labels.items()) class Test(LabelledEnum): A = 1 __labels = { 1: "Label A" } print(Test.list_labels()) # ['Label A']   Python < 3.11 In Python versions less than 3.11, __labels will become the _Test__labels enum member of Test. And the above code will raise an error, due to getattr returning the enum rather than a dict. print(Test.__members__) #{'A': <Test.A: 1>, '_Test__labels': <Test._Test__labels: {1: 'Label A'}>} print(type(Test._Test__labels)) #<enum 'Test'>  Also, in Python 3.9 and 3.10, using private names in an enum class will cause a DeprecationWarning, similar to the following: DeprecationWarning: private variables, such as '_Test__labels', will be normal attributes in 3.10
Question: My system:  Windows 10 x64 Python version 3.9.4 SQLite3 module integrated in the standard library  A DB connection and a cursor are both considered resources, so that I can use a with clause. I know that if I open a resource in a with block, it will be automatically closed outside it (files work in this way). If these assumptions are correct, why can I access a connection or a cursor even from outside the with block? Try this: import sqlite3 with sqlite3.connect('test.db') as conn: cur = conn.cursor() # some code here.... # Now we are outside with block but I can still use conn and cur cur.execute('''CREATE TABLE IF NOT EXISTS users (name TEST, surname TEXT) ''') cur2 = conn.cursor() 

Answer: The context manager does not close the connection on exit; it either commits the last transaction (if no exception was raised) or rolls it back. From the documentation:  Note The context manager neither implicitly opens a new transaction nor closes the connection. If you need a closing context manager, consider using contextlib.closing().  Related, there is no new scope associated with the with statement, so both conn and cur are still in scope following the statement. If you want the with statement to close the connection, do as the documentation suggests: from contextlib import closing with closing(sqlite3.connect('test.db')) as conn: ...
Question: I have a list of track information from Spotify: track_list = [[track_id1, track_title1, track_popularity1],[track_id2, track_title2, track_popularity2] Each track_id is unique. Is there a way to find the entry that matches the track_id without iterating through the entire list every time? def find_track(track_id, track_list): for track in track_list: if track[0] = track_id: return track break return('Not Found')  I know I could get the item I need by using a for loop, but I suspect there's a more efficient way that I've yet to discover.

Answer: The most efficient method for this purpose would be using a dictionary, where the keys are the track_ids and the values are the corresponding track information. This allows for constant-time lookups (O(1) complexity), which is significantly faster than iterating over the entire list (O(n) complexity). Here's an example: def list_to_dict(track_list): return {track[0]: track for track in track_list} def find_track(track_id, track_dict): return track_dict.get(track_id, 'Not Found') # Example usage: track_list = [ ['track_id1', 'track_title1', 'track_popularity1'], ['track_id2', 'track_title2', 'track_popularity2'] ] track_dict = list_to_dict(track_list) track = find_track('track_id1', track_dict) print(track)
Question: I'm trying to install faiss-cpu via pip (pip install faiss-cpu) and get the following error:  × Building wheel for faiss-cpu (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [12 lines of output] running bdist_wheel running build running build_py running build_ext building 'faiss._swigfaiss' extension swigging faiss/faiss/python/swigfaiss.i to faiss/faiss/python/swigfaiss_wrap.cpp swig -python -c++ -Doverride= -I/usr/local/include -Ifaiss -doxygen -module swigfaiss -o faiss/faiss/python/swigfaiss_wrap.cpp faiss/faiss/python/swigfaiss.i Traceback (most recent call last): File "/Users/me/langchain/venv/bin/swig", line 5, in <module> from swig import swig ModuleNotFoundError: No module named 'swig' error: command '/Users/me/langchain/venv/bin/swig' failed with exit code 1 [end of output]  I've searched around and tried various solutions but nothing has worked. I have the following setup: Python 3.12.1 pip 23.3.2 SWIG Version 4.1.1 Python swig package (via pip): 4.1.1.post1 

Answer: faiss-cpu is not available in python-3.12. According to their page on pypi it's available from python-3.7 to python-3.11. You need to downgrade your python in order to install faiss-cpu in your system. Now you can either remove python-3.12 and install python-3.10 on your system. Or use conda to create a virtual env with python-3.10 like following: conda create -n myenv python=3.10  then to activate: conda activate myenv  now install faiss-cpu: pip install faiss-cpu
Question: I am using django, django-ninja framework to replace some of my apis ( written in drf, as it is becoming more like a boilerplate codebase ). Now while transforming some legacy api, I need to follow the old structure, so the client side doesn't face any issue. This is just the backstory. I have two separate models. class Author(models.Model): username = models.CharField(...) email = models.CharField(...) ... # Other fields class Blog(models.Model): title = models.CharField(...) text = models.CharField(...) tags = models.CharField(...) author = models.ForeignKey(...) ... # Other fields  The structure written in django rest framework serializer class BlogBaseSerializer(serializers.Serializer): class Meta: model = Blog exclude = ["author"] class AuthorSerializer(serializers.Serializer): class Meta: model = Author fields = "__all__" class BlogSerializer(serializers.Serializer): blog = BlogBaseSerializer(source="*") author = AuthorSerializer()  In viewset the following queryset will be passed class BlogViewSet(viewsets.GenericViewSet, ListViewMixin): queryset = Blog.objects.all() serializer_class = BlogSerializer ... # Other config  So, as I am switching to django-ninja which uses pydantic for schema generation. I have the following code for pydantic schema AuthorSchema = create_schema(Author, exclude=["updated", "date_joined"]) class BlogBaseSchema(ModelSchema): class Meta: model = Blog exclude = ["author", ] class BlogSchema(Schema): blog: BlogBaseSchema author: AuthorSchema  But as you can see, drf serializer has a parameter called source, where source="*" means to pass the entire original dataset to the nested field serializer. Is there any option to do the exact same with pydantic? Except for creating a list of dictionaries [{author: blog.author, "blog": blog} for blog in queryset]

Answer: Resolved the problem with the following code class AuthorSchema(ModelSchema): class Meta: model = Author exclude=["updated", "date_joined"] class BlogBaseSchema(ModelSchema): class Meta: model = Blog exclude = ["author", ] class BlogSchema(Schema): blog: BlogBaseSchema author: AuthorSchema @staticmethod def resolve_blog(self, obj): return obj