eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I would like to send a message to a specific telegram-user - So I create a bot called Rapid1898Bot and get the api-key for it. I also send a message in the bot and get with https://api.telegram.org/bot<Bot\_token>/getUpdates  the chat-id With the following code it is now working that I send a message to the bot - what is fine:  import os from dotenv import load_dotenv, find_dotenv import requests load_dotenv(find_dotenv()) TELEGRAM_API = os.environ.get("TELEGRAM_API") # CHAT_ID = os.environ.get("CHAT_ID_Rapid1898Bot") CHAT_ID = os.environ.get("CHAT_ID_Rapid1898") print(CHAT_ID) botMessage = "This is a test message from python!" sendText = f"https://api.telegram.org/bot{TELEGRAM_API}/sendMessage?chat_id={CHAT_ID}&parse_mode=Markdown&text={botMessage}" response = requests.get(sendText) print(response.json())  But now I want to also send a message to a specific telegram-user. According to this explanation: How can I send a message to someone with my telegram bot using their Username I was expecting to a) send a message from e.g. my telegram account to the bot b) and then open https://api.telegram.org/bot<Bot\_token>/getUpdates  But unfortunately it seems that it is always the same chat-id, with which I can send the message to the rapid1898bot - but NOT to MY telegram account. Why is this not working and why I am getting always the same chat-id?

Answer: you already have a bot and its token after that you need to get the chat_id:  write message in the chat Visit https://api.telegram.org/bot<YourBOTToken>/getUpdates and get the chat_id under the key message['chat']['id']   import requests def telegram_bot_sendtext(bot_message): bot_token = '' bot_chatID = '' send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message response = requests.get(send_text) return response.json() test = telegram_bot_sendtext("Testing Telegram bot") print(test)  more info - https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with-python-4cf314d9fa3e
Question: My goal is to aggregate a pandas DataFrameGroupBy Object using the agg function. In order to do that, I am generating a dictionary that I'm going to unpack to kwargs using dict unpacking through **dict. This dictionary is required to contain the new column name as the key and a tuple as the value. The first value of the tuple is the column name that gets squeezed to a series and given to the second value as the input of lambda series: .... agg_dict = { f"{cat_name}_count": ('movement_state', lambda series: series.value_counts()[cat_name]) for cat_name in ml_data['category_column'].cat.categories } # Aggregating agg_ml_data = ml_data.groupby(['col1', 'col2']).agg(**agg_dict)  What actually happens now is kinda weird for me. Assuming: ml_data['category_column'].cat.categories  Index(['cat1', 'cat2', 'cat3'], dtype='object')  The correct value counts for one group are one_group['category_column'].value_counts()   | category_column cat1 | 2 cat2 | 9 cat3 | 6  Expected output for one group:     cat1_count cat2_count cat3_count     2 9 6    Actual output for one group     cat1_count cat2_count cat3_count     6 6 6     Somehow, python executes the dict comprehension for the lambda function not as expected and uses just the last category value cat3 when indexing series.value_counts()[cat_name]. I would expect, that the lambda functions are created as the dictionary itself is. Any idea on how to resolve that problem?

Answer: This is a classic Python trap. When you use a free variable (cat_name, in this case) in a lambda expression, the lambda captures which variable the name refers to, not the value of that variable. So in this case, the lambda "remembers" that cat_name was "the loop variable of that dict comprehension". When the lambda is called, it looks up the value of "the loop variable of that dict comprehension", which now, since the dict comprehension has finished, remains at the last value of the list. The usual way of working around this is to use a default argument to "freeze" the value, something like lambda series, cat=cat_name: series.blah[cat]  effectively using one trap (Python computing default arguments at function definition time) to climb out of another. :-)
Question: I have a problem which I simplified as below, I would love if anyone suggest me the code in seaborn like what I want to achieve. import matplotlib.pyplot as plt a = [2000, 4000, 3000, 8000, 6000, 3000, 3000, 4000, 2000, 4000, 3000, 8000, 6000, 3000, 3000, 4000, 2000, 4000, 3000, 8000, 6000, 3000, 3000, 4000] b = [0.8, 0.9, 0.83, 0.81, 0.86, 0.89, 0.89, 0.8, 0.8, 0.9, 0.83, 0.81, 0.86, 0.89, 0.89, 0.8, 0.8, 0.9, 0.83, 0.81, 0.86, 0.89, 0.89, 0.8] c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] fig1, ax1 = plt.subplots(figsize=(12, 6)) ax12 = ax1.twinx() ax1.bar(c, a) ax12.plot(c, b, 'o-', color="red", markersize=12, markerfacecolor='Yellow', markeredgewidth=2, linewidth=2) ax12.set_ylim(bottom=0, top=1, emit=True, auto=False) plt.grid() plt.show()  I am trying to achieve the labels in the center and vertical as shown in the following figure. 

Answer: As of matplotlib 3.4.0, use Axes.bar_label:  label_type='center' places the labels at the center of the bars rotation=90 rotates them 90 deg  Since this is a regular bar chart, we only need to label one bar container ax1.containers[0]: ax1.bar_label(ax1.containers[0], label_type='center', rotation=90, color='white')  But if this were a grouped/stacked bar chart, we should iterate all ax1.containers: for container in ax1.containers: ax1.bar_label(container, label_type='center', rotation=90, color='white')    seaborn version I just noticed the question text asks about seaborn, in which case we can use sns.barplot and sns.pointplot. We can still use bar_label with seaborn via the underlying axes. import pandas as pd import seaborn as sns # put the lists into a DataFrame df = pd.DataFrame({'a': a, 'b': b, 'c': c}) # create the barplot and vertically centered labels ax1 = sns.barplot(data=df, x='c', y='a', color='green') ax1.bar_label(ax1.containers[0], label_type='center', rotation=90, color='white') ax12 = ax1.twinx() ax12.set_ylim(bottom=0, top=1, emit=True, auto=False) # create the pointplot with x=[0, 1, 2, ...] # this is because that's where the bars are located (due to being categorical) sns.pointplot(ax=ax12, data=df.reset_index(), x='index', y='b', color='red')
Question: How do you make Optional attr's of a dataclass? from dataclasses import dataclass @dataclass class CampingEquipment: knife: bool fork: bool missing_flask_size: # what to write here? kennys_stuff = { 'knife': True, 'fork': True } print(CampingEquipment(**kennys_stuff))  I tried field(init=False), but it gave me: TypeError: CampingEquipment.__init__() missing 1 required positional argument: 'missing_flask_size'  By Optional I mean __dict__ may contain the key "missing_flask_size" or not. If I set a default value then the key will be there and it shouldn't be in some cases. I want to check its type if it is there. I tried moving the field(init=False) to the type location (after the colon) so I could make it more explicit as to the thing I wanted optional would be the key and not the value. So I want this test to pass: with pytest.raises(AttributeError): ce = CampingEquipment(**kennys_stuff) print(ce.missing_flask_size) 

Answer: It's not possible to use a dataclass to make an attribute that sometimes exists and sometimes doesn't because the generated __init__, __eq__, __repr__, etc hard-code which attributes they check. However, it is possible to make a dataclass with an optional argument that uses a default value for an attribute (when it's not provided). from dataclasses import dataclass from typing import Optional @dataclass class CampingEquipment: knife: bool fork: bool missing_flask_size: Optional[int] = None kennys_stuff = { 'knife':True, 'fork': True } print(CampingEquipment(**kennys_stuff))  And it's possible to make a dataclass with an argument that's accepted to __init__ but isn't an actual field. So you could do something like this: from dataclasses import dataclass, InitVar from typing import Optional @dataclass class CampingEquipment: knife: bool fork: bool missing_flask_size: InitVar[Optional[int]] = None def __post_init__(self, missing_flask_size): if missing_flask_size is not None: self.missing_flask_size = missing_flask_size  If you really want classes to either to have that attribute present or not have it at all, you could subclass your dataclass and make a factory function that creates one class or the other based on whether that missing_flask_size attribute is present: from dataclasses import dataclass @dataclass class CampingEquipment: knife: bool fork: bool @dataclass class CampingEquipmentWithFlask(CampingEquipment): missing_flask_size: int def equipment(**fields): if 'missing_flask_size' in fields: return CampingEquipmentWithFlask(**fields) return CampingEquipment(**fields) kennys_stuff = { 'knife':True, 'fork': True } print(equipment(**kennys_stuff))  If you really wanted to (I wouldn't recommend it though), you could even customize the __new__ of CampingEquipment to return an instance of that special subclass when that missing_flask_size argument is given (though then you'd need to set init=False and make your own __init__ as well on that class).
Question: I have a file called main.py in which I put a POST call with only one input parameter (integer). Simplified code is given below: from fastapi import FastAPI app = FastAPI() @app.post("/do_something/") async def do_something(process_id: int): # some code return {"process_id": process_id}  Now, if I run the code for the test, saved in the file test_main.py, that is: from fastapi.testclient import TestClient from main import app client = TestClient(app) def test_do_something(): response = client.post( "/do_something/", json={ "process_id": 16 } ) return response.json() print(test_do_something())  I get: {'detail': [{'loc': ['query', 'process_id'], 'msg': 'field required', 'type': 'value_error.missing'}]}  I can't figure out what the mistake is. It is necessary that it remains a POST call.

Answer: The error, basically, says that the required query parameter process_id is missing. The reason for that error is that you send a POST request with request body, i.e., JSON payload; however, your endpoint expects a query parameter. To receive the data in JSON format instead, one needs to create a Pydantic BaseModel—as shown below—and send the data from the client in the same way you already do. from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): process_id: int @app.post("/do_something") async def do_something(item: Item): return item  Test the above as shown in your question: def test_do_something(): response = client.post("/do_something", json={"process_id": 16}) return response.json()  If, however, you had to pass a query parameter, then you would create an endpoint in the same way you did, that is: @app.post("/do_something") async def do_something(process_id: int): return {"process_id": process_id}  but on client side, you would need to add the parameter to the URL itself, as described in the documentation (e.g., "/do_something?process_id=16"), or use the params attribute and as shown below: def test_do_something(): response = client.post("/do_something", params={"process_id": 16}) return response.json()  Update Alternatively, another way to pass JSON data when having a single body parameter is to use Body(..., embed=True), as shown below: @app.post("/do_something") def do_something(process_id: int = Body(..., embed=True)): return process_id  For more details and options on how to post JSON data in FastAPI, please have a look at this answer and this answer.
Question: I've seen that I can repeat a function with python every x seconds by using a event loop library in this post: import sched, time s = sched.scheduler(time.time, time.sleep) def do_something(sc): print("Doing stuff...") # do your stuff s.enter(60, 1, do_something, (sc,)) s.enter(60, 1, do_something, (s,)) s.run()  But I need something slightly different: I need that the function will be called at every system clock minute: at 11:44:00PM, 11:45:00PM and so on. How can I achieve this result?

Answer: Use schedule. import schedule import time schedule.every().minute.at(':00').do(do_something, sc) while True: schedule.run_pending() time.sleep(.1)  If do_something takes more than a minute, turn it into a thread before passing it to do. import threading def do_something_threaded(sc): threading.Thread(target=do_something, args=(sc,)).start()
Question: I need to check if object is descendant of typing.Literal, I have annotation like this: GameState: Literal['start', 'stop']  And I need to check GameState annotation type: def parse_values(ann) if isinstance(ann, str): # do sth if isinstance(ann, int): # do sth if isinstance(ann, Literal): # do sth  But it causes error, so I swapped the last one to: if type(ann) == Literal: # do sth  But it never returns True, so anyone knows a workaround for this?

Answer: typing.get_origin(tp) is the proper way It was implemented in Python 3.8 (Same as typing.Literal) The docstring is thoroughly instructive: def get_origin(tp): """Get the unsubscripted version of a type. This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar and Annotated. Return None for unsupported types. Examples:: get_origin(Literal[42]) is Literal get_origin(int) is None get_origin(ClassVar[int]) is ClassVar get_origin(Generic) is Generic get_origin(Generic[T]) is Generic get_origin(Union[T, int]) is Union get_origin(List[Tuple[T, T]][int]) == list """  In your use case it would be: from typing import Literal, get_origin def parse_values(ann): if isinstance(ann, str): return "str" elif isinstance(ann, int): return "int" elif get_origin(ann) is Literal: return "Literal" assert parse_values("foo") == "str" assert parse_values(5) == "int" assert parse_values(Literal["bar", 6]) == "Literal"
Question: I am trying to match a type in Python 3.10 using the console: t = 12.0 match type(t): case int: print("int") case float: print("float")  And I get this error:  File "<stdin>", line 2 SyntaxError: name capture 'int' makes remaining patterns unreachable  How can I fix this issue?

Answer: First, let's explain the code in the question: t = 12.0 match type(t): case int: print("int") case float: print("float")  In Python, the match statement operates by trying to fit the subject (the value of type(t), i.e. float) into one of the patterns. The above code has two patterns (case int: and case float:). These patterns, by nature of their syntax, are capture patterns, meaning that they attempt to capture the subject (the value of type(t), i.e. float) into the variable name specified in each pattern (the names int and float, respectively). The patterns case int: and case float: are identical to each other. Moreover, they will both match any subject. Hence, the error: SyntaxError: name capture 'int' makes remaining patterns unreachable. Additionally, note that these two patterns are functionally identical to the conventional default pattern of case _:. case _: is also simply a capture pattern, nothing more. (The only difference between case int:, case float: and case _: is the name of the variable that will capture the subject.) Now let's explain the code in @mmohaveri's answer. t = 12.0 match t: case int(): print("int") case float(): print("float")  The above match statement will attempt to fit the value 12.0 first into the pattern case int():, and then into the pattern case float():. These patterns, by nature of their syntax, are class patterns. The parentheses can be used to specify positional arguments and keyword arguments for the pattern. (The parentheses are not function calls.) For the first pattern (case int():), 12.0 is not an instance of int, so the pattern match fails. For the second pattern, (case float():), 12.0 is an instance of float, and no positional or keyword arguments are specified inside the parentheses, so the pattern match succeeds. Here is more information about the matching process.
Question: I am adding path that are higher up or in a sibling directories using following code. And I am not getting IntelliSense for modules inside these folders. Any idea how to get this IntelliSense?    The function colorPrint is defined inside LoggingHelper module in Utility folder.

Answer: I solved it as following. I am adding parent folder and resolving all modules inside the parent folder. This way, I get IntelliSense HERE = Path(__file__).parent sys.path.append(str(HERE / '..')) from Utility.LoggingKotakHelper import (colorPrint, logKotakInfo, logKotakWarning)
Question: As of matplotlib 3.4.0, Axes.bar_label method allows for labelling bar charts. However, the labelling format option works with old style formatting, e.g. fmt='%g' How can I make it work with new style formatting that would allow me to do things like percentages, thousands separators, etc: '{:,.2f}', '{:.2%}', ... The first thing that comes to my mind is somehow taking the initial labels from ax.containers and then reformatting them but it also needs to work for different bar structures, grouped bars with different formats and so on.

Answer:  How can I make bar_label work with new style formatting like percentages, thousands separators, etc?   As of matplotlib 3.7 The fmt param now directly supports {}-based format strings, e.g.: # >= 3.7 plt.bar_label(bars, fmt='{:,.2f}') # ^no f here (not an actual f-string)   Prior to matplotlib 3.7 The fmt param does not support {}-based format strings, so use the labels param. Format the bar container's datavalues with an f-string and set those as the labels, e.g.: # < 3.7 plt.bar_label(bars, labels=[f'{x:,.2f}' for x in bars.datavalues])     Examples:  Thousands separator labels bars = plt.bar(list('ABC'), [12344.56, 23456.78, 34567.89]) # >= v3.7 plt.bar_label(bars, fmt='${:,.2f}')  # < v3.7 plt.bar_label(bars, labels=[f'${x:,.2f}' for x in bars.datavalues])    Percentage labels bars = plt.bar(list('ABC'), [0.123456, 0.567890, 0.789012]) # >= 3.7 plt.bar_label(bars, fmt='{:.2%}') # >= 3.7  # < 3.7 plt.bar_label(bars, labels=[f'{x:.2%}' for x in bars.datavalues])    Stacked percentage labels x = list('ABC') y = [0.7654, 0.6543, 0.5432] fig, ax = plt.subplots() ax.bar(x, y) ax.bar(x, 1 - np.array(y), bottom=y) # now 2 bar containers: white labels for blue bars, black labels for orange bars colors = list('wk') # >= 3.7 for bars, color in zip(ax.containers, colors): ax.bar_label(bars, fmt='{:.1%}', color=color, label_type='center')  # < 3.7 for bars, color in zip(ax.containers, colors): labels = [f'{x:.1%}' for x in bars.datavalues] ax.bar_label(bars, labels=labels, color=color, label_type='center')
Question: I have a FastAPI app in which routes are dynamically generated based on an DB config. However, once the routes are defined and the app running, if the config changes, there seems to be no way to reload the config so that the routes could reflect the config. The only solution I have for now is manually restart the asgi app by restarting uvicorn. Is there any way to fully regenerate routes without stopping the app, that could ideally be called from an URL ?

Answer: It is possible to modify routes at runtime. FastAPI apps have the method add_api_route which allows you to dynamically define new endpoints. To remove an endpoint you will need to fiddle directly with the routes of the underlying Router. The following code shows how to dynamically add and remove routes. import fastapi app = fastapi.FastAPI() @app.get("/add") async def add(name: str): async def dynamic_controller(): return f"dynamic: {name}" app.add_api_route(f"/dyn/{name}", dynamic_controller, methods=["GET"]) return "ok" def route_matches(route, name): return route.path_format == f"/dyn/{name}" @app.get("/remove") async def remove(name: str): for i, r in enumerate(app.router.routes): if route_matches(r, name): del app.router.routes[i] return "ok" return "not found"  And below is shown how to use it $ curl 127.0.0.1:8000/dyn/test {"detail":"Not Found"} $ curl 127.0.0.1:8000/add?name=test "ok" $ curl 127.0.0.1:8000/dyn/test "dynamic: test" $ curl 127.0.0.1:8000/add?name=test2 "ok" $ curl 127.0.0.1:8000/dyn/test2 "dynamic: test2" $ curl 127.0.0.1:8000/remove?name=test "ok" $ curl 127.0.0.1:8000/dyn/test {"detail":"Not Found"} $ curl 127.0.0.1:8000/dyn/test2 "dynamic: test2"  Note though, that if you change the routes dynamically you will need to invalidate the cache of the OpenAPI endpoint.
Question: I am using pydantic python package in FastAPI for a web app, and I noticed there is some inconsistent float-int conversions with different typing checks. For example: class model(BaseModel): data: Optional[Union[int, float]] = None m = model(data=3.33) m.data --> 3.33 class model(BaseModel): data: Optional[Union[int, float, str]] = None m = model(data=3.33) m.data --> 3 class model(BaseModel): data: Union[int, float, str] = None m = model(data=3.33) m.data --> 3 class model(BaseModel): data: Union[str, int, float] = None m = model(data=3.33) m.data --> '3.33'  As shown here, different orders/combinations of typings have different behaviors. I checked out thread https://github.com/samuelcolvin/pydantic/issues/360, and https://github.com/samuelcolvin/pydantic/issues/284, but they seem not to be the exact same problem. What causes such behavior under the hood? Is there a specific reason for this? Or did I do anything wrong/inappropriate here? I'm using python 3.8, pydantic 1.8.2 Thank you for helping! ------ Update ------ In pydantic==1.9.1 this seems has been fixed -> refer to @JacekK's answer.

Answer: To understand what happened there we have to know how the Union works and how pydantic uses typing to validate values. Union According to the documentation:  Union type; Union[X, Y] is equivalent to X | Y and means either X or Y.  OR means that at least one element has to be true to make the whole sentence true. So, if the first element is true there's no need to check the second element. Because the whole sentence is true no matter the value of the second element. Classical disjunction Pydantic Pydantic gets the given value and tries to map it to the type declared in the class attribute definition. If it succeeds then pydantic assigns value to the field. Otherwise it raises a ValidationError. pseudo code: x.data: int x.data = "3" # string x.data = int("3") #convertion, x.data → 3  how it works in your case: x.data = Union[int, float, str] x.data = 3.33 x.data = int(3.33) #convertion to int, which is first in your Union # because previous was success, then: x.data → 3  This behavior is known and well documented:  As such, it is recommended that when defining Union annotations, the most specific type is included first and followed by less specific types.  class MyModel(BaseModel): data: Union[float, int, str] m = MyModel(data=3.33) print(m.data) # output is: 3.33 m = MyModel(data="3.33") print(m.data) # output is: 3.33 m = MyModel(data=3) print(m.data) # output is: 3.0