eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Someone reverse-sorted by 1/i instead of the usual -i and it made me wonder: What is the smallest positive integer case where that fails*? I think it must be where two consecutive integers i and i+1 have the same reciprocal float. The smallest I found is i = 6369051721119404: i = 6369051721119404 print(1/i == 1/(i+1)) import math print(math.log2(i))  Output (Try it online!): True 52.50000001100726  Note that it's only a bit larger than 252.5 (which I only noticed after finding the number with other ways) and 52 is the mantissa bits stored by float, so maybe that is meaningful. So: What is the smallest where it fails? Is it the one I found? And is there a meaningful explanation for why? * Meaning it fails to sort correctly, for example sorted([6369051721119404, 6369051721119405], key=lambda i: 1/i) doesn't reverse that list, as both key values are the same.

Answer: Suppose i is between 2^n and 2^(n+1) for some n. Then 1/i is between 2^(-n-1) and 2^-n. Its representation as double-precision floating point is 1.xxx...xxx * 2^(-n-1), where there are 52 x's. The smallest difference that can be expressed at that magnitude is 2^-52 * 2^(-n-1) = 2^(-n-53). 1/i and 1/(i+1) may get rounded to the same number if the difference between them is at most 2^(-n-53). Solving for i: 1/i - 1/(i+1) = 2^(-n-53) ==> i(i+1) = 2^(n+53). The solution is approximately i = 2^((n+53) / 2). This matches our range for i if n = 52. Then i = 2^52.5. Starting at this value of i there is a possibility to get the same value for 1/i and 1/(i+1). But it won't happen for every i as it depends on how the numbers are rounded. We can start searching at that point, and as you discovered shortly after that we'll find the first occurrence. Note: We also need to rule out n=51 as it seems to have solutions that fall in range. However, the only integer i that falls in the range 2^51 to 2^52 and is at least the minimal value calculated above is 2^52 itself, which can be ruled out. For larger values we need to switch to n=52 as above.
Question: _ score can be used as a variable name anywhere in Python, such as: _ = 10 print(_)  However, it is not accepted here: d = dict(john = 10, owen=12, jenny=13) match d: case {'john' : 10, 'jenny': _}: print('does not work', _)  ERROR: print('does not work', _) NameError: name '_' is not defined  Yet, perfectly fine to use as follows: d = dict(john = 10, owen=12, jenny=13) match d: case {'john' : 10, 'jenny': a}: print('does not work', a)  Why is _ not a valid variable name in new match in Python 3.10?

Answer: In a match statement, _ is a wildcard pattern. It matches anything without binding any names, so you can use it multiple times in the same case without having to come up with a bunch of different names for multiple values you don't care about.
Question: So, hi. Everything works with SQLite, but when I try to add PostgreSQL according to the user's guide on FastAPI, nothing works and I get: sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) invalid dsn: invalid connection option "check_same_thread" My database.py is: from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker #SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db" SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db" engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} ) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() 

Answer: check_same_thread is an argument specific to sqlite. As you've specified a Postgres URL, you can remove that argument and you should have no issue creating an engine. i.e: from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db" engine = create_engine(SQLALCHEMY_DATABASE_URL) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base()
Question: I'm trying to write a function that returns list of lists with all possible combination. It's supposed to return this: [['Moscow', 'Oslo', 'Boston', 'Berlin'], ['Moscow', 'Oslo', 'Sydney', 'Berlin'], ['Moscow', 'Paris', 'Boston', 'Berlin'], ['Moscow', 'Paris', 'Sydney', 'Berlin']]  and when I call the function pathway('Moscow', [['Oslo', 'Paris'], ['Boston', 'Sydney']], 'Berlin')  I get this: [['Moscow', 'Oslo, Boston', 'Berlin'], ['Moscow', 'Oslo, Sydney', 'Berlin'], ['Moscow', 'Paris, Boston', 'Berlin'], ['Moscow', 'Paris, Sydney', 'Berlin']]  Here is my function: def pathway(city_from, city_array, city_to): paths = [] cities = itertools.product(*city_array) cities = [', '.join(map(str, x)) for x in cities] for i in cities: i = str(i) path = city_from, i, city_to paths.append(list(path)) return paths  How can it be fixed?

Answer: This will do precisely what you specified: def pathway(city_from, city_array, city_to): return [[city_from, *c, city_to] for c in itertools.product(*city_array)]
Question: I need to get all the info about some writers in wikidata For example - https://www.wikidata.org/wiki/Q39829 My code from wikidata.client import Client client = Client() entity = client.get('Q39829', load=True) # Spouse spouse_prop = client.get('P26') spouse = entity[spouse_prop] print('Mother: ', spouse.label) # Children child_prop = client.get('P40') child = entity[child_prop] print('Children: ', dir(child)) print(child.label)  Problem The problem is with the properties with the list of values For example, he has 3 children And in the result I have only one name Question How can I get the list of all children?

Answer: From the docs for Wikidata:  Although it implements Mapping[EntityId, object], it [entity] actually is multidict. See also getlist() method.  Which means you can do: >>> [c.label for c in entity.getlist(child_prop)] [m'Joe Hill', m'Owen King', m'Naomi King']
Question: Using the python inspect module, in a function, I would like to get the source code of the line that called that function. So in the following situation: def fct1(): # Retrieve the line that called me and extract 'a' return an object containing name='a' a = fct1()  I would like to retrieve the string "a = fct1()" in fct1 All I can do so far is to retrieve the code of the whole module with : code = inspect.getsource(sys._getframe().f_back)  Please note that fct1() can be called many times in the main module. Eventually, what I want is to retrieve the variable name "a" which is easy if I can get s = "a = fct1()" in fct1() : a_name = s.split("=")[0].strip() 

Answer: A really dumb solution would be to capture a stack trace and take the 2nd line: import traceback def fct1(): stack = traceback.extract_stack(limit=2) print(traceback.format_list(stack)[0].split('\n')[1].strip()) # prints "a = fct1()" return None a = fct1()  @jtlz2 asked for it in a decorator import traceback def add_caller(func): def wrapper(*args, **kwargs): stack = traceback.extract_stack(limit=2) func(*args, caller=traceback.format_list(stack)[0].split('\n')[1].strip(), **kwargs) return wrapper @add_caller def fct1(caller): print(caller) fct1()  And it does work. UPDATE Here's a functional version (which needed limit=3 for the caller of the caller): import traceback def source_line_of_caller(): """Return the Python source code line that called your function.""" stack = traceback.extract_stack(limit=3) return traceback.format_list(stack)[0].split('\n')[1].strip() def _tester(): assert "_tester() # whoa, comments too" == source_line_of_caller() _tester() # whoa, comments too
Question: How can I calculate the elementwise maximum of two columns in Polars inside an expression? Polars version = 0.13.31 Problem statement as code: import polars as pl import numpy as np df = pl.DataFrame({ "a": np.arange(5), "b": np.arange(5)[::-1] }) # Produce a column with the values [4, 3, 2, 3, 4] using df.select([ ... ]).alias("max(a, b)")  Things I've tried Polars claims to support numpy universal functions (docs), which includes np.maximum which does what I'm asking for. However when I try that I get an error. df.select([ np.maximum(pl.col("a"), pl.col("b")).alias("max(a, b)") ]) # TypeError: maximum() takes from 2 to 3 positional arguments but 1 were given  There appears to be no Polars builtin for this, there is pl.max but this returns only the single maximum element in an array. Using .map() my_df.select([ pl.col(["a", "b"]).map(np.maximum) ]) # PanicException  Current workaround I'm able to do this using the following snippet however I want to be able to do this inside an expresion as it's much more convenient. df["max(a, b)"] = np.maximum(df["a"], df["b"]) 

Answer: You can use .max_horizontal() df = pl.select( a = pl.int_range(0, 5), b = pl.int_range(0, 5).reverse(), )  shape: (5, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 0 ┆ 4 │ │ 1 ┆ 3 │ │ 2 ┆ 2 │ │ 3 ┆ 1 │ │ 4 ┆ 0 │ └─────┴─────┘  df.with_columns( pl.max_horizontal('a', 'b').alias('max(a, b)') )  shape: (5, 3) ┌─────┬─────┬───────────┐ │ a ┆ b ┆ max(a, b) │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞═════╪═════╪═══════════╡ │ 0 ┆ 4 ┆ 4 │ │ 1 ┆ 3 ┆ 3 │ │ 2 ┆ 2 ┆ 2 │ │ 3 ┆ 1 ┆ 3 │ │ 4 ┆ 0 ┆ 4 │ └─────┴─────┴───────────┘  The API also contains other horizontal functions.
Question: So consider this snippet import polars as pl df = pl.DataFrame({'class': ['a', 'a', 'b', 'b'], 'name': ['Ron', 'Jon', 'Don', 'Von'], 'score': [0.2, 0.5, 0.3, 0.4]}) df.group_by('class').agg(pl.col('score').max())  This gives me: shape: (2, 2) ┌───────┬───────┐ │ class ┆ score │ │ --- ┆ --- │ │ str ┆ f64 │ ╞═══════╪═══════╡ │ a ┆ 0.5 │ │ b ┆ 0.4 │ └───────┴───────┘  But I want the entire row of the group that corresponded to the maximum score. I can do a join with the original dataframe like sdf = df.group_by('class').agg(pl.col('score').max()) sdf.join(df, on=['class', 'score'])  To get shape: (2, 3) ┌───────┬───────┬──────┐ │ class ┆ score ┆ name │ │ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ str │ ╞═══════╪═══════╪══════╡ │ a ┆ 0.5 ┆ Jon │ │ b ┆ 0.4 ┆ Von │ └───────┴───────┴──────┘  Is there any way to avoid the join and include the name column as part of the groupby aggregation?

Answer: You can use a sort_by expression to sort your observations in each group by score, and then use the last expression to take the last observation. For example, to take all columns: df.group_by('class').agg( pl.all().sort_by('score').last(), )  shape: (2, 3) ┌───────┬──────┬───────┐ │ class ┆ name ┆ score │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ f64 │ ╞═══════╪══════╪═══════╡ │ a ┆ Jon ┆ 0.5 │ │ b ┆ Von ┆ 0.4 │ └───────┴──────┴───────┘  Edit: using over If you have more than one observation that is the max, another easy way to get all rows is to use over. For example, if your data has two students in class b ('Von' and 'Yvonne') who tied for highest score: df = pl.DataFrame( { "class": ["a", "a", "b", "b", "b"], "name": ["Ron", "Jon", "Don", "Von", "Yvonne"], "score": [0.2, 0.5, 0.3, 0.4, 0.4], } ) df  shape: (5, 3) ┌───────┬────────┬───────┐ │ class ┆ name ┆ score │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ f64 │ ╞═══════╪════════╪═══════╡ │ a ┆ Ron ┆ 0.2 │ │ a ┆ Jon ┆ 0.5 │ │ b ┆ Don ┆ 0.3 │ │ b ┆ Von ┆ 0.4 │ │ b ┆ Yvonne ┆ 0.4 │ └───────┴────────┴───────┘  df.filter(pl.col('score') == pl.col('score').max().over('class'))  shape: (3, 3) ┌───────┬────────┬───────┐ │ class ┆ name ┆ score │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ f64 │ ╞═══════╪════════╪═══════╡ │ a ┆ Jon ┆ 0.5 │ │ b ┆ Von ┆ 0.4 │ │ b ┆ Yvonne ┆ 0.4 │ └───────┴────────┴───────┘
Question: I tried code below in TEST.py:32 print("ChromeDriverManager().install() :", ChromeDriverManager().install())  [WDM] - ====== WebDriver manager ====== 2022-07-05 19:49:04,445 INFO ====== WebDriver manager ====== Traceback (most recent call last): File "d:\Python\PYTHONWORKSPACE\repo\Auto-booking-master\src\TEST.py", line 32, in <module> print("ChromeDriverManager().install() :", ChromeDriverManager().install()) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\chrome.py", line 38, in install driver_path = self._get_driver_path(self.driver) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\core\manager.py", line 29, in _get_driver_path binary_path = self.driver_cache.find_driver(driver) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\core\driver_cache.py", line 95, in find_driver driver_version = driver.get_version() File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\core\driver.py", line 43, in get_version self.get_latest_release_version() File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\drivers\chrome.py", line 37, in get_latest_release_version self.browser_version = get_browser_version_from_os(self.chrome_type) File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\webdriver_manager\core\utils.py", line 152, in get_browser_version_from_os cmd_mapping = { KeyError: 'google-chrome'  Please help me.

Answer: May be your web driver manager is outdated.  Uninstall your web driver manager by... pip uninstall webdriver_manager  ...then again install... pip install webdriver_manager  Install webdriver-manager: pip install webdriver-manager, pip install selenium  And then: # selenium 4 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager option = webdriver.ChromeOptions() option.add_argument("start-maximized") driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option) driver.get('https://www.google.com/')   WebDriverManager
Question: Question What is the correct or best way to query a pandas DataFrame? Is it depending on the use case or can you say "always use .query()" or "never use .query()"? My primary concern is robustness or error-proof-ness of the code, but of course performance is also relevant. In this post the query method is stated to be robust and preferred over the other methods, do you agree? Should I always use .query()?  DataFrame.query() function in pandas is one of the robust methods to filter the rows of a pandas DataFrame object. And it is preferable to use the DataFrame.query() function to select or filter the rows of the pandas DataFrame object instead of the traditional and the commonly used indexing method.  Background I recently came across the .query() method and started to use it quite frequently for convenience and because I thought this was the way to do it properly. Then I read these two posts (the content is not essential for this question, I just want to show what made me think about it): apply, the Convenience Function you Never Needed and How to deal with SettingWithCopyWarning in Pandas? In the post about SettingWithCopyWarning different methods like .loc and .at are mentioned, but not .query(). This made me wonder whether .query() is really used. (I thought I start a new question rather than posting this in the comments). It might also not have been relevant for that specific problem, but it made me wonder none the less. The post about "apply - the convenience function..." made me wonder whether .query() is also a convenience function you never need. The documentation mentions the following use case:  query() Use Cases A use case for query() is when you have a collection of DataFrame objects that have a subset of column names (or index levels/names) in common. You can pass the same query to both frames without having to specify which frame you’re interested in querying  Edit: fixed the link to .apply() question.

Answer: I don't think there is a hard answer for this question. To answer your question regarding should you always use query, the simple answer is no. The query method uses eval behind the scenes, which makes it less performant. So when should you use query? You should use query if the condition you're trying to filter is incredibly specific and involves multiple columns. While the most panda-esque way of filtering is using loc, there are times when chaining loc after loc gets out of hand. The decision to use the query method should be based on readbility and performance. If you're using loc over and over again, you may wish to revise it using a simple query string. However, if the switch makes your code less performant, and you are working with mission-critical data, you should sacrifice a little bit of readability over performance.
Question: I just stumbled accross this surprising behaviour with Python datetimes while creating datetimes accross DST shift. Adding a timedelta to a local datetime might not add the amount of time we expect. import datetime as dt from zoneinfo import ZoneInfo # Midnight d0 = dt.datetime(2020, 3, 29, 0, 0, tzinfo=ZoneInfo("Europe/Paris")) # datetime.datetime(2020, 3, 29, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Paris')) d0.isoformat() # '2020-03-29T00:00:00+01:00' # Before DST shift d1 = d0 + dt.timedelta(hours=2) # datetime.datetime(2020, 3, 29, 2, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Paris')) d1.isoformat() # '2020-03-29T02:00:00+01:00' # After DST shift d2 = d0 + dt.timedelta(hours=3) # datetime.datetime(2020, 3, 29, 3, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Paris')) d2.isoformat() # '2020-03-29T03:00:00+02:00' # Convert to UCT d1u = d1.astimezone(dt.timezone.utc) # datetime.datetime(2020, 3, 29, 1, 0, tzinfo=datetime.timezone.utc) d2u = d2.astimezone(dt.timezone.utc) # datetime.datetime(2020, 3, 29, 1, 0, tzinfo=datetime.timezone.utc) # Compute timedeltas d2 - d1 # datetime.timedelta(seconds=3600) d2u - d1u # datetime.timedelta(0)  I agree d1 and d2 are the same, but shouldn't d2 be '2020-03-29T04:00:00+02:00', then? d3 = d0 + dt.timedelta(hours=4) # datetime.datetime(2020, 3, 29, 4, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Paris'))  Apparently, when adding a timedelta (ex. 3 hours) to a local datetime, it is added regardless of the timezone and the delta between the two datetimes (in real time / UTC) is not guaranteed to be that timedelta (i.e. it may be 2 hours due to DST). This is a bit of a pitfall. What is the rationale? Is this documented somewhere?

Answer: The rationale is : timedelta arithmetic is wall time arithmetic. That is, it includes the DST transition hours (or excludes, depending on the change). See also P. Ganssle's blog post on the topic . An illustration: import datetime as dt from zoneinfo import ZoneInfo # Midnight d0 = dt.datetime(2020, 3, 29, 0, 0, tzinfo=ZoneInfo("Europe/Paris")) for h in range(1, 4): print(h) print(d0 + dt.timedelta(hours=h)) print((d0 + dt.timedelta(hours=h)).astimezone(ZoneInfo("UTC")), end="\n\n")  1 2020-03-29 01:00:00+01:00 2020-03-29 00:00:00+00:00 # as expected, 1 hour added 2 2020-03-29 02:00:00+01:00 # that's a non-existing datetime... 2020-03-29 01:00:00+00:00 # looks normal 3 2020-03-29 03:00:00+02:00 2020-03-29 01:00:00+00:00 # oops, 3 hours timedelta is only 2 hours actually!   Need more confusion? Use naive datetime. Given that the tz of my machine (Europe/Berlin) has the same DST transitions as the tz used above: d0 = dt.datetime(2020, 3, 29, 0, 0) for h in range(1, 4): print(h) print(d0 + dt.timedelta(hours=h)) print((d0 + dt.timedelta(hours=h)).astimezone(ZoneInfo("UTC")), end="\n\n")  1 2020-03-29 01:00:00 # 1 hour as expected 2020-03-29 00:00:00+00:00 # we're on UTC+1 2 2020-03-29 02:00:00 # ok 2 hours... 2020-03-29 00:00:00+00:00 # wait, what?! 3 2020-03-29 03:00:00 2020-03-29 01:00:00+00:00
Question: I made a simple server using python flask in mac. Please find below the code. from flask import Flask app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def hello(): print("request received") return "Hello world!" if __name__ == "__main__": app.run(debug=True)  I run it using python3 main.py command. While calling above API on url http://localhost:5000/ from Postman using GET / POST method, it always returns http 403 error.  Python version : 3.8.9 OS : Mac OS 12.4 Flask : 2.1.2 

Answer: Mac OSX Monterey (12.x) currently uses ports 5000 and 7000 for its Control centre hence the issue. Try running your app from port other than 5000 and 7000 use this: if __name__ == "__main__": app.run(port=8000, debug=True)  and then run your flask file, eg: app.py python app.py You can also run using the flask command line interface using this command provided you have set the environment variable necessary for flask CLI. flask run --port 8000 You can also turn off AirPlay Receiver in the Sharing via System Preference. Related discussion here: https://developer.apple.com/forums/thread/682332 Update(November 2022): Mac OSX Ventura(13.x) still has this problem and is fixed with the change in default port as mentioned above.
Question: While importing googletrans I am getting this error:  AttributeError: module 'httpcore' has no attribute 'SyncHTTPTransport 

Answer: googletrans==3.0.0 use very old httpx (0.13.3) and httpcore version. You just need to update httpx and httpcore to latest version and go to googletrans source directory in Python310/Lib/site-packages. In the file client.py, fix 'httpcore.SyncHTTPTransport' to 'httpcore.AsyncHTTPProxy'. And done, perfect. Even, Async, a concurrency model that is far more efficient than multi-threading, and can provide significant performance benefits and enable the use of long-lived network connections such as WebSockets. if you got error 'Nonetype'...group. Try: pip install googletrans==4.0.0-rc1
Question: I want to use "./manage.py shell" to run some Python commands with a specific tenant, but the code to do so is quite cumbersome because I first have to look up the tenant and then use with tenant_context(tenant)): and then write my code into this block. I thought that there should be a command for that provided by django-tenants, but there isn't.

Answer: I've just looked at this myself and this will work, where tenant1 is your chosen tenant: python3 manage.py tenant_command shell --schema=tenant1