eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I noticed that when wrapping a function or method that have some type hinting then the wrapped method loses the type hinting informations when I am coding using Visual studio code. For example with this code: from typing import Callable import functools def decorate(function: Callable): @functools.wraps(function) def wrapper(object: "A", *args, **kwargs): return function(object, *args, **kwargs) return wrapper class A: @decorate def g(self, count: int) -> str: return f"hello {count}" a = A() print(a.g(2))  When I am hovering within visual studio code over the name g then I lose the type hinting informations. Would you know a way to prevent this? Sincerely

Answer: The best you can do with Python 3.8 (and 3.9) is the following: from __future__ import annotations from functools import wraps from typing import Any, Callable, TypeVar T = TypeVar("T") def decorate(function: Callable[..., T]) -> Callable[..., T]: @wraps(function) def wrapper(obj: A, *args: Any, **kwargs: Any) -> T: return function(obj, *args, **kwargs) return wrapper class A: @decorate def g(self, count: int) -> str: return f"hello {count}"  This will preserve the return type information, but no details about the parameter types. The @wraps decorator should at least keep the signature intact. If the decorator is supposed to be universal for methods of A, this is as good as it gets IMO. If you want it to be more specific, you can always restrict the function type to Callable[[A, B, C], T], but then the decorator will not be as universal anymore.  If you upgrade to Python 3.10, you will have access to ParamSpec. Then you can do the following: from __future__ import annotations from functools import wraps from typing import Callable, ParamSpec, TypeVar P = ParamSpec("P") T = TypeVar("T") def decorate(function: Callable[P, T]) -> Callable[P, T]: @wraps(function) def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: return function(*args, **kwargs) return wrapper class A: @decorate def g(self, count: int) -> str: return f"hello {count}"  This actually preserves (as the name implies) all the parameter specifications. Hope this helps.
Question: I have a fraction and I want to print the latex form of it. The fraction is like this for n == 3:  How can I print the latex for this fraction using divide and conquer: 1+\frac{2+\frac{4}{5}}{3+\frac{6}{7}} And for n == 4 the fraction is:  And the result is: 1+\frac{2+\frac{4+\frac{8}{9}}{5+\frac{10}{11}}}{3+\frac{6+\frac{12}{13}}{7+\frac{14}{15}}}

Answer: I found the solution myself. I've tried to solve the problem by using divide and conquer. My solution has two parameters, start and step. Start is the value that is printed at the first step. and step is the value that shows the number of fractions. If step = 1, then I print just the value of start. Otherwise for each step the numerator of the fraction is 2start and for denominator the value is 2start+1. Here is the code of my solution. def generate_fraction(start : int, step : int): result = "" if step == 1: result = str(start) else: result = str(start) + '+\\frac{'+ generate_fraction(2*start, step-1) + '}{' + generate_fraction(2*start + 1, step-1) + '}' return result # Main Program ... step = int(input()) start = 1 print(generate_fraction(start, step))
Question: I am in the process of building a Python package that can be used by Data Scientists to manage their MLOps lifecycle. Now, this package can be used either locally (usually on PyCharm) or on Databricks. I want a certain functionality of the package to be dependent on where it is running, i.e. I want it to do something different if it is called by a Databricks notebook and something else entirely if it is running locally. Is there any way I can determine where it is being called from? I am a little doubtful as to whether we can use something like the following that checks if your code is running on a notebook or otherwise since this will be a package that is going to be stored in your Databricks environment,  How can I check if code is executed in the IPython notebook?

Answer: The workaround I've found to work is to check for databricks specific environment variables. import os def is_running_in_databricks() -> bool: return "DATABRICKS_RUNTIME_VERSION" in os.environ
Question: I have been trying to implement the Apriori Algorithm in python. There are several examples online, they all use similar methods and mostly the same example dataset. The reference link: https://www.kaggle.com/code/rockystats/apriori-algorithm-or-market-basket-analysis/notebook (starting from the line [26]) I have a different dataset that has the same structure as the example datasets online. I keep getting the  "DeprecationWarning: DataFrames with non-bool types result in worse computationalperformance and their support might be discontinued in the future.Please use a DataFrame with bool type"  error. Here is my code:  import pandas as pd import numpy as np from mlxtend.frequent_patterns import apriori, association_rules df1 = pd.read_csv(r'C:\Users\USER\dataset', sep=';') df=df1.fillna(0) basket = pd.pivot_table(data=df, index='cust_id', columns='Product', values='quantity', aggfunc='count',fill_value=0.0) def convert_into_binary(x): if x > 0: return 1 else: return 0 basket_sets = basket.applymap(convert_into_binary) frequent_itemsets = apriori(basket_sets, min_support=0.07, use_colnames=True) print(frequent_itemsets) # association rule rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1) print(rules)  In addition, in the last step of my code, I get an empty dataframe; I can see the column headings of the dataset but the output is empty.  Empty DataFrame Columns: [antecedents, consequents, antecedent support, consequent support, support, confidence, lift, leverage, conviction] Index: []  I am not sure if this issue is related to this error that I am having. I am new to python and I would really appreciate assistance and support on this issue.

Answer: I ran into the same issue even after converting my dataframe fields to 0 and 1. The fix was just making sure the apriori module knows the dataframe is of boolean type, so in your case you should run this :  frequent_itemsets = apriori(basket_sets.astype('bool'), min_support=0.07, use_colnames=True)   In addition, in the last step of my code, I get an empty dataframe; I can see the column headings of the dataset but the output is empty.  Try using a smaller min_support
Question: In pandas v.1.5.0 a new warning has been added, which is shown, when a column is set from an array of different dtype. The FutureWarning informs about a planned semantic change, when using iloc: the change will be done in-place in future versions. The changelog instructs what to do to get the old behavior, but there is no hint how to handle the situation, when in-place operation is in fact the right choice. The example from the changelog: df = pd.DataFrame({'price': [11.1, 12.2]}, index=['book1', 'book2']) original_prices = df['price'] new_prices = np.array([98, 99]) df.iloc[:, 0] = new_prices df.iloc[:, 0]  This is the warning, which is printed in pandas 1.5.0:  FutureWarning: In a future version, df.iloc[:, i] = newvals will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either df[df.columns[i]] = newvals or, if columns are non-unique, df.isetitem(i, newvals)  How to get rid of the warning, if I don't care about in-place or not, but want to get rid of the warning? Am I supposed to change dtype explicitly? Do I really need to catch the warning every single time I need to use this feature? Isn't there a better way?

Answer: I haven't found any better way than suppressing the warning using the warnings module: import numpy as np import pandas as pd import warnings df = pd.DataFrame({"price": [11.1, 12.2]}, index=["book1", "book2"]) original_prices = df["price"] new_prices = np.array([98, 99]) with warnings.catch_warnings(): # Setting values in-place is fine, ignore the warning in Pandas >= 1.5.0 # This can be removed, if Pandas 1.5.0 does not need to be supported any longer. # See also: https://stackoverflow.com/q/74057367/859591 warnings.filterwarnings( "ignore", category=FutureWarning, message=( ".*will attempt to set the values inplace instead of always setting a new array. " "To retain the old behavior, use either.*" ), ) df.iloc[:, 0] = new_prices df.iloc[:, 0]
Question: Update: Which Python should I use to install poetry?  System Python: That is an excellent idea. Once, however, poetry self update was trying to update a system package without the necessary permissions. Pyenv: A good solution. Nonetheless, if Python is updated and the old installation is deleted, poetry will stop working because it is not aware of the new python version.    Set global python with pyenv  pyenv global 3.10.7  Install poetry  $ curl -sSL https://install.python-poetry.org | python3 -  Change global python  pyenv global 3.10.8 Now, poetry still runs on Python-3.10.7. If I uninstall this python version, poetry crashes. How can I instruct the virtual environment of poetry to use the new python version? A solution is to uninstall and reinstall it: $ curl -sSL https://install.python-poetry.org | python3 - --uninstall $ curl -sSL https://install.python-poetry.org | python3 -  Is there any other way?

Answer: The decision is to use Pyenv to install Poetry. If the older python version should be deleted:  Uninstall Poetry. Delete old Python version. Install Poetry.
Question: I am facing multiple issues using an async ODM inside my celery worker First i wasn't able to init my database models using celery worker signal i am using beanie for the db connection. First Implementation from asyncer import syncify from asgiref.sync import async_to_sync client = AsyncIOMotorClient( DATABASE_URL, uuidRepresentation="standard" ) db = client[DB_NAME] async def db_session(): await init_beanie( database=db, document_models=[Project, User], ) @worker_ready.connect def startup_celery_ecosystem(**kwargs): logger.info('Startup celery worker process') async_to_sync(db_session)() logger.info('FINISHED : Startup celery worker process') async def get_users(): users = User.find() users_list = await users.to_list() return users_list @celery_app.task def pool_db(): async_to_sync(get_users)() #syncify(get_users)() same error User class is not initialized yet (init_beanie should have already initialized all the models )  With this implementation i could not access my database using the User and Project class and it raises an error as if User and Project haven't been instantiated yet The workaround is to call db_session() at the module level which solve the problem with database models instantiation, But now when querying the database i get the following error from my celery task  RuntimeError: Event loop is closed  Second Implementation from asyncer import syncify from asgiref.sync import async_to_sync client = AsyncIOMotorClient( DATABASE_URL, uuidRepresentation="standard" ) db = client[DB_NAME] async def db_session(): await init_beanie( database=db, document_models=[Project, User], ) # now init_beanie at module level async_to_sync(db_session)() async def get_users(): users = User.find() users_list = await users.to_list() return users_list @celery_app.task def pool_db(): # this raises the following Runtime error RuntimeError('Event loop is closed') async_to_sync(get_users)() #syncify(get_users)() same error  i am not very familiar with how asyncio is implemented and how asyncer and asgiref allows to run async code inside a sync thread which left me confused, any help would be appriciated

Answer: After many investigation using flower for monitoring workers and logging the workers Id ( processes ids) it turns out that Celery worker itself does not process any tasks, it spawns other child processes ( this is my case because i am using the default executor pool which is prefork), while the signal ( worker_ready.connect ) is only run on the supervisor process Celery worker and not the childs, and since processes are isoleted memory wise, this means that you can't have access to db connection or any initialized ressources from the child processes. Now i am using celery with gevent which only spawn 1 process, because initially my project doesn't require CPU heavy tasks which means i don't need all the cpu power provided by the prefork pool
Question: I am finding a difficulty with quitting FastAPI. Ctr+c does not work. Here is my pyproject.toml [tool.pyright] exclude = ["app/worker"] ignore = ["app/worker"] [tool.poetry] name = "api" version = "0.1.0" description = "" authors = ["SamiAlsubhi <sami@alsubhi.me>"] [tool.poetry.dependencies] python = ">=3.8,<3.9" fastapi = "^0.65.2" tortoise-orm = "^0.17.4" asyncpg = "^0.23.0" aerich = "^0.5.3" networkx = "^2.5.1" numpy = "^1.21.0" ldap3 = "^2.9.1" fastapi-jwt-auth = "^0.5.0" python-multipart = "^0.0.5" torch = "1.7.1" pyts = "0.11.0" Pint = "^0.17" Cython = "^0.29.24" python-dotenv = "^0.19.0" arq = "^0.22" uvicorn = {extras = ["standard"], version = "^0.15.0"} [tool.poetry.dev-dependencies] pytest = "^6.2.4" requests = "^2.25.1" asynctest = "^0.13.0" coverage = "^5.5" pytest-html = "^3.1.1" pytest-sugar = "^0.9.4" pytest-json-report = "^1.4.0" pytest-cov = "^2.12.1" pylint = "^2.11.1" autopep8 = "^1.5.7" black = "^22.3.0" aiosqlite = "^0.17.0" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"  here is my entry point """running API in a local dev environment""" import os import uvicorn from dotenv import load_dotenv # laoding env values load_dotenv("../.env") if __name__ == "__main__": port = os.getenv("FASTAPI_PORT") port = int(port) if port else None uvicorn.run("app.main:app", host=os.getenv("FASTAPI_HOST"), port=port, reload=True)  This what I get when I run it and then try to quit, the process hangs and does not go back to terminal: (trendr) sami@Samis-MBP backend % python run.py INFO: Will watch for changes in these directories: ['/Users/name/Desktop/etc'] INFO: Uvicorn running on http://0.0.0.0:1000 (Press CTRL+C to quit) INFO: Started reloader process [70087] using watchgod INFO: Started server process [70089] INFO: Waiting for application startup. INFO: Application startup complete. ^CINFO: Shutting down INFO: Finished server process [70089] INFO: ASGI 'lifespan' protocol appears unsupported. 

Answer: It looks like there was a compatibility issue between unvicorn, starlette and FastAPI around those versions. I updated them to the latest versions and that solved the issue.
Question: I'm using a monorepo with multiple packages in separate directories. Poetry is in charge of package management including creating the virtual environment. I wish VS Code would use the right Python interpreter for each package individually. Is that possible? My workaround is to open a separate window with the directory containing a package, so all of its files would use the right interpreter.

Answer: You can open multiple folders in vscode and select the interpreter for each folder individually.  Select Add Folder to Workspace in the menu. As a simple example I have added three folders    Then click on the python interpreter version in the lower right corner (or Ctrl+Shift+P --> Python:Select Interpreter)   Choose a folder, here I choose folder1   Now select interpreter for folder1 folder    The same operation continues to select interpreters for folder2 and folder3. Of course this is just the basics, you can also create virtual environments for each folder and then choose the interpreter for the virtual environment for each folder. For example, I created a virtual environment .venv2 under the folder2, the same step 2 and 3, and then I selected the interpreter under the virtual environment .venv2.  There is also a way to specify the default interpreter path in the settings.json, but after setting it also requires step 2 and 3 and then select it in the selection panel.  When you modify the folder settings, the .vscode folder and the settings.json file will be automatically generated under the folder. The contents of the settings.json file are the settings you just modified  And this step is reversible. You can manually create the .vscode folder and add the settings.json file in it. And enter the corresponding settings in the settings.json file.
Question: I am trying to vizualize a large network with pyvis and facing two problems:  super long rendering network instability, i.e. nodes move too fast and it is hard to interact with such a network.  Disabling physics with toggle_physics(False) helps to speed up rendering and makes the network static, but eliminates the layout settings. This is how it looks agter disabling physics: link. As you see, the graph is messy and has no structure. What I want to do is to disable physics but keep the layout settings, i.e. I want my graph to look like a normal network (e.g. similar to spring layout in networkX) with weights taken into account for every edge. Is there a way to do so? So far I found out that pyvis only has hierarchial layouts, which is not what I need. I think integrating a networkX layout might help but I have no idea how to do this, since networkX allows to set layout as a keyword argument in nx.draw() function, which is not compatible with my case. This is my code in case it helps to understand my problem: g = nx.Graph() edges_cards = cards_weights_df.values.tolist() g.add_weighted_edges_from(edges_cards) net = Network("1000px", "1800px") net.from_nx(g) net.toggle_physics(False) net.show("graph.html")  Your help is appreciated!

Answer: It is possible to pass x and y coordinates to your pyvis nodes (see doc here). You can then create a graph layout with networkx and pass the resulting positions to your pyvis graph. See example below with nx.circular_layout() applied to the karate club network: import networkx as nx from pyvis.network import Network G = nx.karate_club_graph() pos=nx.circular_layout(G,scale=500) net = Network() net.from_nx(G) for node in net.get_nodes(): net.get_node(node)['x']=pos[node][0] net.get_node(node)['y']=-pos[node][1] #the minus is needed here to respect networkx y-axis convention net.get_node(node)['physics']=False net.get_node(node)['label']=str(node) #set the node label as a string so that it can be displayed net.toggle_physics(False) net.show('net.html')  Here is the result with circular layout:  and without any specific layout: import networkx as nx from pyvis.network import Network G = nx.karate_club_graph() net = Network() net.from_nx(G) for node in net.get_nodes(): net.get_node(node)['physics']=False net.get_node(node)['label']=str(node) net.toggle_physics(False) net.show('net.html')
Question: The July 2022 release of the Python extension for Visual Studio Code introduced "Inlay Type Hints", which automatically suggests return types of functions that don't have an explicit annotation. To enable it, you can set "python.analysis.inlayHints.functionReturnTypes": true to your IDE user settings (Preferences: Open Settings (JSON) command). While testing this feature, I noticed the following kind of suggestion, inside a class:  ... where the highlighted text in yellow is the return type suggested by the Python extension, which is based on Pylance, which itself relies on Pyright. My question is: what is the @ sign in this suggestion supposed to mean? Is there a PEP that refers to this kind of type annotations (with Self@...) or is that way of type hinting specific to Pyright, differing from the standard convention? Where can I find more information about it? I've found a similar Stackoverflow question here but it did not get any answer.

Answer: That @ indicates that Self is a TypeVar, and Self@HereIsMyClassName refers to the Self in the context of the class HereIsMyClassName (it could also be a function). This is not valid Python. (Technically, it is valid, as the @ operator is matrix multiplication, so you are matrix-multiplying Self and HereIsMyClassName. However, that's not what is meant and really doesn't make any sense.) Don't write this in your code, but know that this is how Pylance shows you TypeVars when it shows you type definitions. (Possibly other editors and extensions as well.)
Question: I am working on a project in Python, and while my below code works I am getting this hideous error:  UserWarning: Boolean Series key will be reindexed to match DataFrame index.  dummy_types = pd.get_dummies(df_pokemon_co, columns=['Type 1', 'Type 2']) df_pokemon_co['Rock'] = dummy_types['Type 1_Rock'] + dummy_types['Type 2_Rock'] df_pokemon_co['Ground'] = dummy_types['Type 1_Ground'] + dummy_types['Type 2_Ground'] df_pokemon_co['Water'] = dummy_types['Type 1_Water'] + dummy_types['Type 2_Water'] df_pokemon_co['Sum'] = df_pokemon_co['Rock'] + df_pokemon_co['Ground'] + df_pokemon_co['Water'] print('Total rock:', np.sum(np.any(df_pokemon_co[df_pokemon_co['Sum']==2][df_pokemon_co['Rock']==1], axis=1))) print('Total ground:', np.sum(np.any(df_pokemon_co[df_pokemon_co['Sum']==2][df_pokemon_co['Ground']==1], axis=1))) print('Total water:', np.sum(np.any(df_pokemon_co[df_pokemon_co['Sum']==2][df_pokemon_co['Water']==1], axis=1)))  I have worked out that if I remove the following from the print lines the message goes away, but I am not quite sure how to remedy. [df_pokemon_co['Sum']==2]  Does anyone have any idea on how to fix this? I have seen some other posts related to this error however the error seems to be getting issued for different reasons in those cases. Thanks in advance :)

Answer: If need count matched values by 2 conditions chain them by & for bitwise AND and use sum for Trues values: print('Total rock:', ((df_pokemon_co['Sum']==2) & (df_pokemon_co['Rock']==1)).sum())  For median use DataFrame.loc for select by condition and column name: med = (df_pokemon_co.loc[(df_pokemon_co['Rock']==1) & (df_pokemon_co['Sum']==2), 'Defense'].median())  For avoid call multiple times same mask assign it to variable: mask1 = (df_pokemon_co['Sum']==2) & (df_pokemon_co['Rock']==1) print('Total rock:', mask1.sum()) print('Median rock:', df_pokemon_co.loc[mask1, 'Defense'].sum())
Question: I followed the tutorial in the channels documentation but when I start the server python3 manage.py runserver it gives me this : Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 17, 2022 - 00:13:21 Django version 4.1.2, using settings 'config.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.  when I expected for it to give me this : Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). October 17, 2022 - 00:13:21 Django version 4.1.2, using settings 'config.settings' Starting ASGI/Channels version 3.0.5 development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.  settings.py INSTALLED_APPS = [ 'channels', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ... ] ASGI_APPLICATION = 'config.asgi.application'  asgi.py import os from django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') application = ProtocolTypeRouter({ 'http': get_asgi_application(), })  It doesn't give any errors even when I change the ASGI_APPLICATION = 'config.asgi.application to ASGI_APPLICATION = ''.

Answer: This could be due to the fact that the Django and channels versions you have used are not compatible Try : channels==3.0.4 and django==4.0.0