eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: When I click on the button it should print the id of the label inside that card only. But it always prints the id of last label no matter which button I click on. In the code below my_grid is the name of the grid in which I'm trying to populate the cards and listcards is the list of cards labels that I'm trying to put inside the card Code with my_grid: for i in range(0,len(listcards)): with ui.card() as mycard: label_card = ui.label(text=f"{listcards[i]}") bt = ui.button("ID", on_click=lambda:print(label_card.id))  When I click on the button I want to print the label id of that card only but it always prints the id of last label. I really want to fix this issue. Any suggestions?

Answer: This is a very frequently asked question. We're actually thinking about introducing a FAQ section because of this question. But it is indeed a tricky Python behavior that keeps confusing developers. In the following code example, a button click will call the lambda function, which in turn prints the value of label_card.id. Since the for loop has terminated long before the button click, label_card is the label with text "9". Therefore, regardless of which button is clicked, the ID of this last label is printed. for i in range(10): with ui.card(): label_card = ui.label(text=i) ui.button("ID", on_click=lambda: print(label_card.id))  The trick is to add label_card=label_card to the parameter list of the lambda function. The lambda function is unique for every loop cycle. And so are its parameters. The current value of the label_card variable is written into the parameter list of the unique lambda function. This way a button click prints the ID of the corresponding label_card. for i in range(10): with ui.card(): label_card = ui.label(text=i) ui.button("ID", on_click=lambda label_card=label_card: print(label_card.id))
Question: I am learning about Python. I am trying to store project data to a .CSV file using Pandas library. I know csv is comma separated values, the data is separated by comma(,). I am wondering why I would use a .CSV instead of the other Excel file types?

Answer: The major differences between Excel XLSX and CSV file format are the file size and the formatting. In a *.CSV file, the file size is smaller, and the data looks like this: (there is no formatting, just raw data)  And if you open using a text editor, you'd get this: idx,col1,col2 123,aaa,xxx 456,bbb,yyy 789,ccc,zzz  And in a *.XLSX file, the file size is larger, and the data looks like this: (this format allows formatting such as tables, borders, background color, bold, etc)  And if you open with a text editor, you'd get this: PK ! A7傁n [Content_Types].xml ?(? 琓蒼?絎?D綱墶嚜??[$?榵扻$跺(鼄'fQU??Ql蟍&?&YB@鉲.鶼O$`璻?鼿烢偆琕嵆悑5? 镲拥 L岗b.j""%5?3缌騈锽珗?C%?妾?陕YK)ub8x僐-J轜技Q23V$瘺sU.旝?盤勾?I晔?燷県:C@i?╩23???g€/#莺矢2 泌x|`隚簼惝秛_?傃悓U燨詹w筳鋸髾s箪4去瓑-蔤e霳?e|鮫,ん佅??愸y絼s?i? 藓??s??耵V7?麛幵88彍? 梬a懏:??霤rh伥??轁鄸??  PK ! 礥0#? L _rels/.rels ?(? <truncated>  Generally, I use CSV format to store raw data, and use XLSX format to present the data.
Question: I am getting weird results from numpy sqrt method when applying it on an array of integers with a where condition. See below. With integers: a = np.array([1, 4, 9]) np.sqrt(a, where=(a>5)) Out[3]: array([0. , 0.5, 3. ])  With floats: a = np.array([1., 4., 9.]) np.sqrt(a, where=(a>5)) Out[25]: array([1., 4., 3.])  Is it a bug or a misunderstanding of how this method works?

Answer: I think there might be a bug inconsistency*, when repeating the command several times I don't get consistent results. *this is actually due to the probable use of numpy.empty to initialize the output. This function reuses memory without setting the default values and thus will have non-predictable values in the cells that are not manually overwritten. Here is an example: import numpy as np print(np.__version__) a = np.array([1, 4, 9]) # integers print(np.sqrt(a, where=(a>5)).round(3)) a = np.array([1., 4., 9.]) # floats print(np.sqrt(a, where=(a>5)).round(3)) a = np.array([1, 4, 9]) # integers again print(np.sqrt(a, where=(a>5)).round(3))  Output: 1.24.2 [0. 0. 3.] [0. 0. 3.] [1. 4. 3.] # this is now different  From the remark of @hpaulj, it seems that providing out is required. Indeed, this prevents the inconsistent behavior with the above example. out should be initialized with a predictable function, such as numpy.zeros. import numpy as np print(np.__version__) out = np.zeros(3) a = np.array([1, 4, 9]) # integers print(np.sqrt(a, where=(a>5), out=out)) print(out) out = np.zeros(3) a = np.array([1., 4., 9.]) # floats print(np.sqrt(a, where=(a>5), out=out)) print(out) a = np.array([1, 4, 9]) # integers again print(np.sqrt(a, where=(a>5), out=out)) print(out)  Output: 1.24.2 [0. 0. 3.] [0. 0. 3.] [0. 0. 3.] [0. 0. 3.] [0. 0. 3.] [0. 0. 3.]  Nevertheless, this seems to be an inconsistent behavior. The doc specifies that it out is not provided, a new array should be allocated:  out ndarray, None, or tuple of ndarray and None, optional  A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.  where array_like, optional  This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.   In your case you rather want to use numpy.where: out = np.where((a>5), np.sqrt(a), a) # or, to avoid potential errors if you have forbidden values out = np.where(a>5, np.sqrt(a, where=a>5), a) # or out = np.sqrt(a, where=a>5, out=a.astype(float))  Output: array([1., 4., 3.])
Question: If I install some python pacakges, like opencv-python, it will install the cv2 pacakge. But before I look at opencv's document, how can I inspector the opencv-python pacakge, and find out what it installed. Like pip info opencv-python, but it will not print the installed packages. Update: I find the pip installed place /usr/local/lib/python3.11/site-packages/opencv_python-4.7.0.72.dist-info/top_level.txt contains the top_level package(like installed packages). So I can write some script to parse this file, or python has builltin util to print this info?

Answer: The answers demonstrating pip show usage are fine if you just wanted to discover this information from the command-line interactively. If you want access to the top-level package names programmatically, however, pip is not much help because it has no API. Since Python 3.10 the information is easily available using stdlib importlib.metadata: >>> from importlib.metadata import packages_distributions >>> dists = packages_distributions() >>> [name for name, pkgs in dists.items() if "opencv-python" in pkgs] ['cv2']  If you want to know all the files owned by an installed package, not just the top-level import names, you can get that too like this: from importlib.metadata import distribution dist = distribution("opencv-python") for file in dist.files: print(file) ...
Question: So, Im trying to setup Alembic with FastAPI and Im having a problem with Pydantic's BaseSettings, I get a validation error (variables not found) because it doesnt find the .env file (?) It can be solved by changing env_file = ".env" to env_file = "../.env" in the BaseSettings class Config but that makes the error happen when running main.py, I tried setting it as an absolute path with env_file = os.path.abspath("../../.env") but that didnt work. What should I do? config.py: import os from functools import lru_cache from pydantic_settings import BaseSettings abs_path_env = os.path.abspath("../../.env") class Settings(BaseSettings): APP_NAME: str = "AppName" SQLALCHEMY_URL: str ENVIRONMENT: str class Config: env_file = ".env" # Works with uvicorn run command from my-app/project/ # env_file = "../.env" Works with alembic command from my-app/alembic # env_file = abs_path_env @lru_cache() def get_settings(): return Settings()  Project folders: my-app ├── alembic │ ├── versions │ ├── alembic.ini │ ├── env.py │ ├── README │ └── script.py.mako ├── project │ ├── core │ │ ├── __init__.py │ │ └── config.py │ └── __init__.py ├── __init__.py ├── .env └── main.py 

Answer: For me it was the fact that .env was not in the same directory as the running script. I had to manually anchor the .env file to an absolute path. Here is my settings.py file with .env residing alongside in the same directory: import os from pydantic_settings import BaseSettings, SettingsConfigDict DOTENV = os.path.join(os.path.dirname(__file__), ".env") class Settings(BaseSettings): pg_dsn: str pg_pool_min_size: int pg_pool_max_size: int pg_pool_max_queries: int pg_pool_max_intactive_conn_lifetime: int model_config = SettingsConfigDict(env_file=DOTENV)  So with the os.path.join(os.path.dirname(__file__), ".env") line I basically instruct to search for .env file inside the same directory as the settings.py file. This way you can run any of your scripts, and the Settings object will always point to your .dotenv file.
Question: I have got an db like: class NewsBase(BaseModel): title: str topic: str class NewsCreate(NewsBase): datetime: datetime class News(NewsBase): id: int datetime: datetime class Config: orm_mode = True  When i try to make this request, it returns with 500: @app.get("/news/find_by_topic/{topic}", response_model=schemas.News) def find_news_by_topic(topic: str, db: Session = Depends(get_db)): db_news = crud.get_news_by_topic(db, topic=topic) if db_news is None: raise HTTPException(status_code=404, detail="This title is not found") return db_news  Crud.py: def get_news_by_topic(db: Session, topic: str): return db.query(models.News).filter(models.News.topic == topic).all()  It's like error:  File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\middleware\errors.py", line 184, in __call__ raise exc File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\middleware\errors.py", line 162, in __call__ await self.app(scope, receive, _send) File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__ raise exc File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__ await self.app(scope, receive, sender) File "C:\Projects\RestAPI\venv\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 20, in __call__ raise e File "C:\Projects\RestAPI\venv\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 17, in __call__ await self.app(scope, receive, send) File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\routing.py", line 718, in __call__ await route.handle(scope, receive, send) File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\routing.py", line 276, in handle await self.app(scope, receive, send) File "C:\Projects\RestAPI\venv\Lib\site-packages\starlette\routing.py", line 66, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "C:\Projects\RestAPI\venv\Lib\site-packages\fastapi\routing.py", line 291, in app content = await serialize_response( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Projects\RestAPI\venv\Lib\site-packages\fastapi\routing.py", line 154, in serialize_response raise ResponseValidationError( fastapi.exceptions.ResponseValidationError  How can I fix it?

Answer: It seems like you need to set response_model properly. Read more here. If you are using Python 3.9: response_model=list[schemas.News] If you are using older Python: from typing import List response_model=List[schemas.News]
Question: I am just using huggingface example to use their LLM model, but it stuck at the: downloading shards: 0%| | 0/5 [00:00<?, ?it/s]  (I am using Jupiter notebook, python 3.11, and all requirements were installed) from transformers import AutoTokenizer, AutoModelForCausalLM import transformers import torch model = "tiiuae/falcon-40b-instruct" tokenizer = AutoTokenizer.from_pretrained(model) pipeline = transformers.pipeline( "text-generation", model=model, tokenizer=tokenizer, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto", ) sequences = pipeline( "Girafatron is obsessed with giraffes, the most glorious animal on the face of this Earth. Giraftron believes all other animals are irrelevant when compared to the glorious majesty of the giraffe.\nDaniel: Hello, Girafatron!\nGirafatron:", max_length=200, do_sample=True, top_k=10, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id, ) for seq in sequences: print(f"Result: {seq['generated_text']}")  how can I fix it?

Answer: I think it's not stuck. These are just very large models that take a while to download. tqdm only estimates after the first iteration, so it just looks like nothing is happening. I'm currently downloading the smallest version of LLama2 (7B parameters) and it's downloading two shards. The first took over 17 minutes to complete and I have reasonably fast internet connection.
Question: I can't figure out why my code it's always getting an error This is my code: from selenium import webdriver url = "https://google.com/" path = "C:/Users/thefo/OneDrive/Desktop/summer 2023/chromedriver_win32" driver = webdriver.Chrome(path) driver.get(url)  Path of chromedriver:  and this is the error that always appears: Traceback (most recent call last): File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path path = SeleniumManager().driver_location(options) if path is None else path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 74, in driver_location browser = options.capabilities["browserName"] ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'str' object has no attribute 'capabilities' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\thefo\OneDrive\Desktop\summer 2023\Projeto Bot Discord - BUFF SELL CHECKER\teste2.py", line 6, in <module> driver = webdriver.Chrome(path) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__ self.service.path = DriverFinder.get_path(self.service, self.options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 44, in get_path raise NoSuchDriverException(f"Unable to obtain {service.path} using Selenium Manager; {err}") selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain chromedriver using Selenium Manager; 'str' object has no attribute 'capabilities'; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location 

Answer: This error message... Traceback (most recent call last): File "C:\Users\thefo\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 42, in get_path path = SeleniumManager().driver_location(options) if path is None else path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ...implies that the version of Selenium which you are using is v4.6 or above.  Selenium Manager In such cases Selenium Manager can silently download the matching ChromeDriver and you don't have to explicitly mention the chromedriver path anymore.  Solution Your minimal code block can be: from selenium import webdriver url = "https://google.com/" driver = webdriver.Chrome() driver.get(url)
Question: I've spent several hours on research and no solution seems to work anymore. My function will take an estimated 30-50 minutes per process. If I spot flaws during the first few minutes, I don't want to wait for the processes to finish completely or have to close the terminal without killing the processes cleanly. For simplicity, each process is assigned a function with an infinite loop. With Ctrl+c the complete main program should now be interrupted without getting stuck. The reason for the infinite loop is that every previous solution has worked with lists that are actually looped through. The keyboard interrupt is recognized, but only afterwards, when all processes have finished. I don't know if this has anything to do with the Python version, but that's not the requirement I would like. I would like a simple and clean solution to this problem. That would probably help many others as well. Example: from multiprocessing import Pool def f(x): while True: pass if __name__ == '__main__': with Pool(2) as p: p.map(f, [1, 2]) 

Answer: Problem Solved In another forum this problem was discussed and solved in a simple and clean way. Here is the link for everyone else: https://www.reddit.com/r/learnpython/comments/152sfp8/how_to_stop_multiprocessingpool_with_ctrlc_python/ Solution 1 In multiprocessing module there is an AsyncResult object. This includes various functions such as get(), wait() and ready(). While get() and wait() block, you can use ready() to wait for the result and still catch KeyboardInterrupts. Unfortunately, only map_async() and apply_async() return an AsyncResult object. from multiprocessing import Pool def f(x): while True: pass if __name__ == "__main__": with Pool(2) as p: result = p.map_async(f, [1, 2]) while not result.ready(): time.sleep(1)  Solution 2 This way you can also use functions that block:  Playing around with signals it seems that from inside child processes, exceptions other than KeyboardInterrupt or SystemExit propagate to the main process. When ctrl+c is pressed, all processes (children and main process) run their SIGINT handler which by default just raises KeyboardInterrupt. To get control out of the children, the child interrupt handler must raise some other exception. The exception is not seen by the way because the main process has its hands full with its own KeyboardInterrupt.  Basically, the SIGINT handler can also return None. from multiprocessing import Pool from signal import signal, SIGINT def initializer(): signal(SIGINT, lambda: None) def f(x): while True: pass if __name__ == '__main__': with Pool(n, initializer) as p: p.map(f, [1, 2])
Question: While downloading a video using the PyTube library using this code: yt.streams.get_highest_resolution().download("PATH", f"PATH.mp4")  I get an error: raise RegexMatchError(caller="get_transform_object", pattern=pattern) pytube.exceptions.RegexMatchError: get_transform_object: could not find match for var for={(.*?)};  I've seen a lot of fixes on Stack Overflow and in the Git repository of PyTube, but they seem to go into different parts of cypher.py. I would like to know how I could alternate get_transform_object class in cypher.py to match the RegEx check.

Answer: Here is a quick fix in the meantime as the library makes an update. -> In file .venv/lib/python3.10/site-packages/pytube/cipher.py I am using python 3.10 and my virtual environment is called .venv You just have to find the library pytube and go to the file cipher.py and edit its source code for now. -> Find the method get_transform_object and replace it as below def get_transform_object(js: str, var: str) -> List[str]: pattern = r"var %s={(.*?)};" % re.escape(var) logger.debug("getting transform object") regex = re.compile(pattern, flags=re.DOTALL) transform_match = regex.search(js) if not transform_match: # i commented out the line raising the error # raise RegexMatchError(caller="get_transform_object", pattern=pattern) return [] # Return an empty list if no match is found return transform_match.group(1).replace("\n", " ").split(", ")
Question: Below is a complete script I am trying to automate the process of pinging multiple routers and to do that every 2 hrs but I also want the ability to terminate it at any given time. def start(): for file_name in file_list: unrechable = [] rechable = [] print("Processing:"+file_name,end="\n") open_file(file_name, rechable, unrechable) if len(unrechable) > 0: print("These IP from " + file_name + " are Unrechable:") for i in unrechable: print(i,end="\n") print("") else: print("All IP's are Rechable from " + file_name) return ''' ''' def open_file(file_name, rechable, unrechable): df = pd.read_excel("D:/Network/"+file_name+".xlsx") col_IP = df.loc[:, "IP"].tolist() col_name = df.loc[:, "Location"].tolist() check(col_IP, col_name, rechable, unrechable) return ''' ''' def check(col_IP, col_name, rechable, unrechable): for ip in range(len(col_IP)): response = os.popen(f"ping {col_IP[ip]} ").read() if("Request timed out." or "unreachable") in response: print(response) unrechable.append(str(col_IP[ip] + " at " + col_name[ip])) else: print(response) rechable.append(str(col_IP[ip] + " at " + col_name[ip])) return ''' ''' def main1(): while(True): start() print("Goint to Sleep for 2Hrs") time.sleep(60) def qu(): while(True): if(keyboard.is_pressed('q')): print("exit") os._exit ''' ''' file_list = ["ISP" , "NVR"] if __name__ == '__main__': p2 = Thread(target=main1) p3 = Thread(target=qu) p2.start() p3.start()  I have created 2 threads here one runs the main script the other looks for keyboard interrupt. But once q is pressed only one of the threads terminates. I later found out it is impossible to terminate both threads at one and I am completely lost at this point

Answer: I added a queue to a thread that listens to any keyboard interrupts and once the condition is met adds it to the queue. def qu1(): while(True): if keyboard.is_pressed("q"): qu_main.put("q") print("added q") break else: pass return  Next I put my sleep timer in a loop. I wanted my script to execute in 2hr intervals therefore I added a 1s timer and looped it 7200 times. while(x < 7200): if(not qu_main.empty()): if(qu_main.get() == 'q'): print("ending the Cycle") return else: print("sleep") x += 1 time.sleep(1) else: print("sleep") x += 1 time.sleep(1)
Question: This StackOverflow answer says python dicts keep insertion order of keys as of python 3.7. The comments to that answer discuss the implementation details of what happens when a key is deleted. I'd like to know: what does the language spec guarantee about key order in the face of deletes (preferably with a link)? Based on the discussion, I bet it guarantees insertion order of the undeleted elements, but I've been unable to find confirmation.

Answer: The language guarantees that undeleted elements will remain in the same order after a key is deleted. The Python language reference states (emphasis mine):  Dictionaries preserve insertion order, meaning that keys will be produced in the same order they were added sequentially over the dictionary. Replacing an existing key does not change the order, however removing a key and re-inserting it will add it to the end instead of keeping its old place.  The bolded text would not be true if the order of keys changed after a deletion.