eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: According to https://fastapi.tiangolo.com/tutorial/middleware/, we could apply a FastAPI Middleware on async def endpoints. Currently I have several non-async def endpoints, how to apply FastAPI Middleware on non-async def endpoint? If I still register an async Middleware, will it work for the non-async def endpoint ? For example:  @app.middleware("http") async def add_process_time_header(request: Request, call_next): start_time = time.time() response = await call_next(request) process_time = time.time() - start_time response.headers["X-Process-Time"] = str(process_time) return response  Will the Middleware work properly if call_next is a non-async def method ? Thank you.

Answer: A coroutine is created based on any synchronous function. So yes, this will work fine for you. You can read more about this here.
Question: I am trying to list all products with numbers = [1,2,3,4,5,6,7,8] string length of 4 with some constraints.  Position 0 must be < 8 Positions 2 and 3 must be < 6  With the current code it is printing every possible combination so I was wondering how do I go about filtering it? import itertools number = [1,2,3,4,5,6,7,8] result = itertools.product(number, repeat=4) for item in result: print(item)  I've tried using if product[0] < 8 or product[2] < 6 or product[3] < 6: but I don't know where to fit in or how to format it.

Answer: I think you can simplify this and avoid wasting a lot of cycles by looking at the inputs carefully. repeat=4 means that you want to iterate over the following: [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]  However, what your question is asking is how to iterate through [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]  Since the conditions on the inputs are independent (the value of one element is not what restricts the others), you can adjust the inputs instead of filtering elements after the fact. I suggest you just iterate over the sequences you want directly instead of filtering the output. You don't need to make actual lists: a range object will be much more efficient: itertools.product(range(1, 8), range(1, 9), range(1, 6), range(1, 6))
Question: I'm using FastAPI and OpenAPI/Swagger UI to see and test my endpoints. Each time I use an endpoint for the first time, in order to test it, I have to first click the Try it out button, which is getting tedious. Is there a way to make it disappear and be able to test the endpoint instantly?

Answer: Yes, you can configure the OpenAPI/swagger page by passing a dictionary to the kwarg "swagger_ui_parameters" when creating your FastAPI instance (docs). The full list of all settings you can update that way can be found here. For your example, it would look like this: from fastapi import FastAPI app = FastAPI(swagger_ui_parameters={"tryItOutEnabled": True})
Question: This question relates to resample .agg/.apply which behaves differently than groupby .agg/.apply. Here is an example df: df = pd.DataFrame({'A':range(0,100),'B':range(0,200,2)},index=pd.date_range('1/1/2022',periods=100,freq='D'))  Output:  A B 2022-01-01 0 0 2022-01-02 1 2 2022-01-03 2 4 2022-01-04 3 6 2022-01-05 4 8 ... .. ... 2022-04-06 95 190 2022-04-07 96 192 2022-04-08 97 194 2022-04-09 98 196 2022-04-10 99 198  My question is, what does x represent in the apply function below. There are times where it behaves as a series and other times it behaves as a df. By calling type(x) it returns df. However, the below returns an error saying "No axis named 1 for object type Series" df.resample('M').apply(lambda x: x.sum(axis=1))  But this does not. There is no stack for a series, so this would imply x represents a df. df.resample('M').apply(lambda x: x.stack())  Also, when you run df.resample('M').apply(lambda x: print(type(x))) the outputs are series, but df.resample('M').apply(lambda x: type(x)) outputs dataframe type. So my main question is, what gets passed into apply for resample. a series or a dataframe?

Answer: That's a really good question and I think I have not the right answer but.  resample a timeseries returns a DatetimeIndexResampler instance. apply is an alias of aggregate function.  Now check the source code:  @doc( _shared_docs["aggregate"], see_also=_agg_see_also_doc, examples=_agg_examples_doc, klass="DataFrame", axis="", ) def aggregate(self, func=None, *args, **kwargs): result = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg() if result is None: how = func result = self._groupby_and_aggregate(how, *args, **kwargs) result = self._apply_loffset(result) return result agg = aggregate apply = aggregate  What I understand: I think if something goes wrong with ResamplerWindowApply, aggregate function have a fallback mechanism to reevaluate the function with _groupby_and_aggregate. The docstring of the last one is : """ Re-evaluate the obj with a groupby aggregation. """  Let's debug with a named function: import inspect def f(x): print(inspect.stack()[2].function) print(f'begin: {type(x)}') x.stack() print(f'end: {type(x)}') return x.sum(axis=1) df.resample('M').apply(f)  Output: _aggregate_series_pure_python begin: <class 'pandas.core.series.Series'> # something goes wrong _python_apply_general # the caller has changed begin: <class 'pandas.core.frame.DataFrame'> # now x is a DataFrame end: <class 'pandas.core.frame.DataFrame'> _python_apply_general begin: <class 'pandas.core.frame.DataFrame'> end: <class 'pandas.core.frame.DataFrame'> _python_apply_general begin: <class 'pandas.core.frame.DataFrame'> end: <class 'pandas.core.frame.DataFrame'> _python_apply_general begin: <class 'pandas.core.frame.DataFrame'> end: <class 'pandas.core.frame.DataFrame'>  After a failure with a Series, aggregate calls the function with a DataFrame. Unfortunately, this behavior is not documented.
Question: I have a python dictionary. a = {'1':'saturn', '2':'venus', '3':'mars', '4':'jupiter', '5':'rahu', '6':'ketu'} planet = input('Enter planet : ') print(planet)  If user enteres 'rahu', dictionary to be sorted like the following a = {'1':'rahu', '2':'ketu', '3':'saturn', '4':'venus', '5':'mars', '6':'jupiter' } print('4th entry is : ')  It should sort dictionary based on next values in the dictionary. If dictionary ends, it should start from initial values of dictionary. It should print 4th entry of the dictionary, it should return venus  How to sort python dictionary based on user input value?

Answer: Your use of a dictionary is probably not ideal. Dictionaries are useful when the key has a significance and the matching value needs to be accessed quickly. A list might be better suited. Anyway, you could do: l = list(a.values()) idx = l.index(planet) a = dict(enumerate(l[idx:]+l[:idx], start=1))  NB. the above code requires the input string to be a valid dictionary value, if not you'll have to handle the ValueError as you see fit. Output: {1: 'rahu', 2: 'ketu', 3: 'saturn', 4: 'venus', 5: 'mars', 6: 'jupiter'}
Question: I have created a dataframe in databricks as a combination of multiple dataframes. I am now trying to upload that df to a table in my database and I have used this code many times before with no problem, but now it is not working. My code is df.write.saveAsTable("dashboardco.AccountList")  getting the error: AttributeError: 'DataFrame' object has no attribute 'write' Thanks for any help!

Answer: Most probably your DataFrame is the Pandas DataFrame object, not Spark DataFrame object. try: spark.createDataFrame(df).write.saveAsTable("dashboardco.AccountList")
Question: When I run this: import pygame import cairo WIDTH, HEIGHT = 640, 480 pygame.display.init() screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) screen.fill((255, 255, 255)) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surface) ctx.set_source_rgb(0, 0, 1) ctx.rectangle(0, 0, 100, 100) ctx.fill() buf = surface.get_data() img = pygame.image.frombuffer(buf, (WIDTH, HEIGHT), "ARGB") screen.blit(img, (0, 0)) pygame.display.flip() clock = pygame.time.Clock() while not pygame.QUIT in [e.type for e in pygame.event.get()]: clock.tick(30)  the result is a window with a blue rectangle, as I would expect. However, if I change ctx.set_source_rgb(0, 0, 1) with ctx.set_source_rgb(0, 0, 0), i.e. change the colour from (0, 0, 1) to (0, 0, 0), I get nothing at all. The expected behaviour would be that the rectangle is black. When I modify the blue value between 0 and 1, the opacity seems to change while the actual blue value seems to stay constant. The r and g values work as expected when b = 1. The same happens with set_source_rgba.

Answer: This is a problem of endianness, or byte order. Cairo's pixel formats are endian-dependent, and pygame's pixel formats are independent of endianness. See https://github.com/pygame/pygame/issues/2972 for more of a discussion of interoperability. So if the bytes are flipped, and you say blue (0, 0, 1), you fill out the ARGB like [1, 0, 0, 1], because the alpha is also 1 by default. Flip [1, 0, 0, 1] and you still have the same thing. However if you fill in ARGB for black, it goes to [1, 0, 0, 0]. Interpreting that as BGRA yields R=0, G=0, B=1, A=0. Because alpha is 0, it's completely transparent and nothing is drawn. On a little endian system (like yours), Cairo's ARGB is equivalent to pygame's BGRA. This was just added to pygame, so you'll have to update to pygame 2.1.3.dev8or higher. (Right now that's the highest release of pygame, and it's a pre release, so you'd need pip install pygame --upgrade --pre Then you can do img = pygame.image.frombuffer(buf, (WIDTH, HEIGHT), "BGRA")
Question: I am testing a local setup of dbt-postgres. I have a simple model, but for some reason, any table created is being placed in a schema with the prefix public appended to it. Desired output table: public.test Current output table: public_public.test As you can see, the public schema is being duplicated here. Using another schema in the model also creates a new schema with public_ prefix. Simple model test.sql file: {{ config(materialized='table', schema='public') }} select a from table x  dbt_profile.yml name: 'abc' version: '0.1' config-version: 2 profile: 'abc' model-paths: ["models"] analysis-paths: ["analyses"] test-paths: ["tests"] seed-paths: ["seeds"] macro-paths: ["macros"] snapshot-paths: ["snapshots"] target-path: "target" clean-targets: - "target" - "dbt_packages" - "dbt_modules" - "logs" models: abc: materialized: table  profiles.yml abc: outputs: dev: type: postgres threads: 1 host: "localhost" port: 5432 user: "admin" pass: "admin" dbname: database schema: "public" target: dev 

Answer: See the docs on custom schemas. You are defining public as the "target schema" in your profiles.yml file. You do not need to add {{ config(schema='public') }} to your model file; that config sets a "custom schema" for that model, and by default, dbt will land your model at <target_schema>_<custom_schema>. You can change this behavior if you want, by overriding the built-in macro called generate_schema_name, but in this case you can just remove the config from your model, and let the profile determine the schema.
Question: For iinstance I have a dataframe as below import pandas as pd df = pd.DataFrame({"col":['AM RLC, F C', 'AM/F C', 'DM','D C']}) |col -------------------| 0 |"AM RLC, F C" | 1 |"AM/F C" | 2 |"DM" | 3 |"D C" |  My expected output is as following  |col ----|-----------------------| 0 |["AM", "RLC", "F", "C"]| 1 |["AM", "F", "C"] | 2 |["DM" ] | 3 |["D", "C"] |  ",", "/" and "space" should be treated as delimiter, The answers in this question do not answer my queries

Answer: I would use str.split or str.findall: df['col'] = df['col'].str.split('[\s,/]+') # or df['col'] = df['col'].str.findall('\w+')  Output:  col 0 [AM, RLC, F, C] 1 [AM, F, C] 2 [DM] 3 [D, C]  Regex: [\s,/]+ # at least one of space/comma/slash with optional repeats \w+ # one or more word characters
Question: I am trying to learn while loops. To practice, I created a simple while loop with an If statement inside a function to check and see if a word is a palindrome. For some reason, even if the word is not a palindrome, it keeps returning True. I expected the output of the print function on the last line to be False: from collections import deque word = "tacrocat" def check_palindrome(word): d = deque(word) while len(d) > 1: if d.pop() == d.popleft(): return True return False print(check_palindrome(word))  NOTE: When I change the if statement evaluation to "!=", change the return statement inside the if statement to False, and change the return statement in the while loop to True, it seems to accurately detect palindromes - but I have no idea why.

Answer: If the program is written like this: from collections import deque word = "tacrocat" def check_palindrome(word): d = deque(word) while len(d) > 1: if d.pop() == d.popleft(): return True return False print(check_palindrome(word))  At iteration-1: Since both the first character('t') and last character('t') in the deque are equal it will enter the if condition and return True. When a return statement is executed in a function, then the control comes out of the function. In other words, as soon as return True statement is executed, the control comes back to print statement without executing remaining iterations of while loop and since we returned True, so True will be printed as output. Let's analyze second program: from collections import deque word = "tacrocat" def check_palindrome(word): d = deque(word) while len(d) > 1: if d.pop() != d.popleft(): return False return True print(check_palindrome(word))  At iteration-1: Current deque: [t,a,c,r,o,c,a,t]  We are popping both first element and last element and checking whether they are not equal in if condition. Since 't' and 't' are equal, if condition will not execute and while loop will continue. At iteration-2: Current deque: [a,c,r,o,c,a]  Since 'a' and 'a' are equal, if condition will not execute and while loop will continue. Both first and last elements are popped (pop and popleft) At iteration-3: Current deque: [c,r,o,c]  Since 'c' and 'c' are equal, if condition will not execute and while loop will continue. Both first and last elements are popped (pop and popleft) At iteration-4: Current deque: [r,o]  Since 'r' and 'o' are not equal, if condition will execute and return False statement is executed. So, the function check_palindrome will terminate with return value as False and so we get output as False. The second program is correct because to check if a word is palindrome or not, we need to check all letters whether they are satisfying the condition or not. Just checking only the first and last character of a word and if they are equal, then it doesn't mean the remaining letters are same. We need to check them too.
Question: Is there any difference between the typing.cast function and the built-in cast function? x = 123 y = str(x)  from typing import cast x = 123 y = cast(str, x)  I expected that mypy might not like the first case and would prefer the typing.cast but this was not the case.

Answer: str(x) returns a new str object, independent of the original int. It's only an example of "casting" in a very loose sense (and one I don't think is useful, at least in the context of Python code). cast(str, x) simply returns x, but tells a type checker to pretend that the return value has type str, no matter what type x may actually have. Because Python variables have no type (type is an attribute of a value), there's no need for casting in the sense that languages like C use it (where you can change how the contents of a variable are viewed based on the type you cast the variable to).
Question: I always though that Callable is equivalent to having the dunder __call__ but apparently there is also __name__, because the following code is correct for mypy --strict: def print_name(f: Callable[..., Any]) -> None: print(f.__name__) def foo() -> None: pass print_name(foo) print_name(lambda x: x)  What is actual interface of python Callable? I dug out what functools.wraps does. AFAIU it sets ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__') - is that the same what the Callable is expected to have?

Answer: So the mypy position up until now seems to have been that most of the time, when a variable is annotated with Callable, the user expects it to stand for a user-defined function (i.e. def something(...): ...). Even though user-defined functions are technically a subtype of the callable and even though they are the ones that define a number of those attributes you mentioned, some users are not aware of this distinction and would be surprised, if mypy raised an error with code like this: from collections.abc import Callable from typing import Any def f(cal: Callable[..., Any]) -> None: print(cal.__name__) print(cal.__globals__) print(cal.__kwdefaults__) print(cal.foo)  Each of those print-lines should be an error, yet only the last actually triggers one. Moreover, if we define a minimal callable class that doesn't have those attributes, it is treated as a subtype of Callable by both Python and mypy, creating a logical contradication: class Bar: def __call__(self) -> None: print(f"hi mom") f(Bar()) # this is valid print(Bar().__name__) # this is an error  Their argument so far amounts to maintaining convenience for users that have so far failed to see the distinction between callable subtypes, and by extension avoiding confused issues being opened by those users, asking why callables shouldn't have __name__ or those other attributes. (I hope I am being charitable enough with my interpretation.) I find this to be a very odd position (to put it mildly) and I expressed as much in the issue I opened for this. I'll keep this answer updated, if any new insights are reached in the discussion around the issue.  Bottom line is: You are right, callables must have the __call__ method and do not require anything else.
Question: I had learned that n = n + v and n += v are the same. Until this; def assign_value(n, v): n += v print(n) l1 = [1, 2, 3] l2 = [4, 5, 6] assign_value(l1, l2) print(l1)  The output will be: [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]  Now when I use the expanded version: def assign_value(n, v): n = n + v print(n) l1 = [1, 2, 3] l2 = [4, 5, 6] assign_value(l1, l2) print(l1)  The output will be: [1, 2, 3, 4, 5, 6] [1, 2, 3]  Using the += has a different result with the fully expanded operation. What is causing this?

Answer: It may seem counter-intuitive, but they are not always the same. In fact,  a = a + b means a = a.__add__(b), creating a new object a += b means a = a.__iadd__(b), mutating the object  __iadd__, if absent, defaults to the __add__, but it also can (and it does, in the case of lists) mutate the original object in-place.
Question: I would sum the values of each tuple enclosed in two lists. The output i would like to get is: 125, 200.0, 100.0. The problem is that they don't sum, but they add like this [(87.5, 37.5), (125.0, 75.0), (50.0, 50.0)]. I need first and second to stay the same as mine, without changing any parentheses. I've searched for many similar answers on stackoverflow, but haven't found any answers for my case. How can i edit calc and fix it? Thank you! Code: first = [(87.5,), (125.0,), (50.0,)] second = [(37.5,), (75.0,), (50.0,)] calc = [x + y for x, y in zip(first, second)] print(calc) 

Answer: The problem is that you are trying to add the tuples (if you do type(x) or type(y) you see that those are tuple values and not the specific floats that you have) if you want to add the values inside of the tuples then you have to access the elements you can do it like so:  first = [(87.5,), (125.0,), (50.0,)] second = [(37.5,), (75.0,), (50.0,)] calc = [x[0] + y[0] for x, y in zip(first, second)] # accessing the first element of x and y -- as there is only 1 element in each of the tuples. # if you had more elements in each tuple you could do the following: calc = [sum(x) + sum(y) for x, y in zip(first, second)] print(calc) print(first, second)
Question: Is it possible to stringize a Polars expression and vice-versa? For example, convert df.filter(pl.col('a')<10) to a string of "df.filter(pl.col('a')<10)". Is roundtripping possible e.g. eval("df.filter(pl.col('a')<10)") for user input or tool automation? I know this can be done with a SQL expression but I'm interested in native. I want to show the specified filter in the title of plots.

Answer: Expressions >>> expr = pl.col("foo") > 2 >>> print(str(expr)) [(col("foo")) > (2i32)]  LazyFrames >>> import io >>> df = pl.DataFrame({ ... "foo": [1, 2, 3] ... }) >>> json_state = df.lazy().filter(expr).serialize(format="json") >>> query_plan = pl.LazyFrame.deserialize(io.StringIO(json_state), format="json") >>> query_plan.collect() shape: (1, 1) ┌─────┐ │ foo │ │ --- │ │ i64 │ ╞═════╡ │ 3 │ └─────┘
Question: I tried to run my code in another computer, while it successfully compiled in the original environment, this error can outta nowhere: File "c:\vision_hw\hw_3\cv2IP.py", line 91, in SECOND_ORDER_LOG original = np.zeros((5,5),dtype=np.int) File "C:\Users\brian2lee\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\numpy\__init__.py", line 284, in __getattr__ raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'int'  I have tried reinstallin numpy but it did not work. Down below is my code: def SECOND_ORDER_LOG (self,img): original = np.zeros((5,5),dtype=np.int) original[2,2] = 1 kernel = np.array([[ 0, 0, -1, 0, 0], [ 0, -1, -2, -1, 0], [-1, -2, 16, -2, -1], [ 0, -1, -2, -1, 0], [ 0, 0, -1, 0, 0]]) result = original + 1 * kernel sharpened = cv2.filter2D(img, -1, result) return sharpened 

Answer: numpy.int was deprecated in NumPy 1.20 and was removed in NumPy 1.24. You can change it to numpy.int_, or just int. Several other aliases for standard types were also removed from NumPy's namespace on the same schedule:    Deprecated name Identical to NumPy scalar type names     numpy.bool bool numpy.bool_   numpy.int int numpy.int_ (default), numpy.int64, or numpy.int32   numpy.float float numpy.float64, numpy.float_, numpy.double (equivalent)   numpy.complex complex numpy.complex128, numpy.complex_, numpy.cdouble (equivalent)   numpy.object object numpy.object_   numpy.str str numpy.str_   numpy.long int numpy.int_ (C long), numpy.longlong (largest integer type)   numpy.unicode str numpy.unicode_    There are similar AttributeError messages for these removals (listing them helps people find this Q&A in the search results):  AttributeError: module 'numpy' has no attribute 'bool'. AttributeError: module 'numpy' has no attribute 'int'. AttributeError: module 'numpy' has no attribute 'float'. AttributeError: module 'numpy' has no attribute 'complex'. AttributeError: module 'numpy' has no attribute 'object'. AttributeError: module 'numpy' has no attribute 'str'. AttributeError: module 'numpy' has no attribute 'long'. AttributeError: module 'numpy' has no attribute 'unicode'.
Question: I am trying to migrate my package from setup.py to pyproject.toml and I am not sure how to do the dynamic versioning in the same way as before. Currently I can pass the development version using environment variables when the build is for development. The setup.py file looks similar to this: import os from setuptools import setup import my_package if __name__ == "__main__": dev_version = os.environ.get("DEV_VERSION") version = dev_version if dev_version else f"{my_package.__version__}" setup( name="my_package", version=version, ... )  Is there a way to do similar thing when using pyproject.toml file?

Answer: Another alternative that might be worth considering for your use case, if you use Git, is to use setuptools_scm. It uses your git tags to perform dynamic versioning. Your pyproject.toml would look something like this: [build-system] requires = ["setuptools", "setuptools-scm"] build-backend = "setuptools.build_meta" [tool.setuptools_scm] "version_scheme" = "post-release" "local_scheme" = "no-local-version" "write_to" = "mypackage/version.py" [tool.setuptools.package-dir] mypackage = "mypackage" [project] name = "mypackage" ... dynamic = ["version"]