eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to check if a single element is in a list using match case. I'm not very familiar with these new keywords so 90% sure I'm using them wrong. Regardless, is there a way to do this? This is my code. I'm expecting for this to print "hi detected in list. Hi!" and "hello detected in list. Hello!", but the match statement doesn't seem to work this way.  mylist= ["hello", "hi", 123, True] match mylist: case ['hi']: print("hi detected in list. Hi!") case ['hello']: print("hello detected in list. Hello!")  Is there a way to check if a list includes an element using match case?

Answer: Using match/case is not the most appropriate way to determine if a list contains some particular value. However, to answer the question then: mylist= ["hello", "hi", 123, True] for element in mylist: match element: case 'hello': print('hello detected') case 'hi': print('hi detected')
Question: When annotating a function parameter with a bound TypeVar, giving it a default value results in the parameter having a union type between the TypeVar and the default value type, even though the default value is of the TypeVar type. Example: class A: pass class B(A): pass Instance = TypeVar("Instance", bound=A) def get_instance(cls: type[Instance] = A) -> Instance: return cls()  Running mypy yields the following error: error: Incompatible default for argument "cls" (default has type "Type[A]", argument has type "Type[Instance]"). reveal_type is correct in both cases: instance_a = get_instance(cls=A) reveal_type(instance_a) # note: Revealed type is "A" instance_b = get_instance(cls=B) reveal_type(instance_b) # note: Revealed type is "B"  How do I correctly annotate get_instance so that I can keep the default argument?

Answer: It is a very old mypy issue. The general solution is using overload's, it can be applied in your case: from typing import TypeVar, overload class A: pass class B(A): pass _Instance = TypeVar("_Instance", bound=A) @overload def get_instance() -> A: ... @overload def get_instance(cls: type[_Instance]) -> _Instance: ... def get_instance(cls: type[A] = A) -> A: return cls() instance_a = get_instance(cls=A) reveal_type(instance_a) # note: Revealed type is "A" instance_b = get_instance(cls=B) reveal_type(instance_b) # note: Revealed type is "B"  For type checking purposes, we define two overloads. Implementation signature is checked only within the function, external callers see only the overloads. Here we just ignore specific relationship between cls type and return type while checking body, however, you can consider other solutions from the linked issue. Try me in playground
Question: About a week ago I set up an application on google. Now when I tri and run: SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'] creds = None if os.path.exists('token.pickle'): with open(self.CREDENTIALS_PATH+self.conjoiner+'token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) ##error here  I get the following error: Exception has occurred: RefreshError ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})  What could be the problem?

Answer: token.pickle contains the access token and refresh token for your application.  Token has been expired or revoked.'  Means that the refersh token in this file is no longer working this can be caused by servral reasons.  the user revoked your access The user has authorized your access token more then 50 times and this is the oldest token and was there for expired the refresh token hasn't been used in six months The project is still in the testing phase, and there for the refresh token will be expired after seven days.  All of the above information can be found in Oauth2 expirationdocumentation the solution for options 1- 3 is to simply delete the token.pickle file and request authorization of the user again. For number four you should go to google developer console under the Oauth2 consent screen and set your application to production. Then your refresh token will stop expiring. Then you can delete the token.pickle and you wont have this issue again.
Question: I have a (2x2) NumPy array: ar = np.array([[2, 0],[3, 0]])  and the same one in the form of xarray.DataArray: da = xr.DataArray(ar, dims=['x', 'y'], coords=[[0, 1], [0, 1]])  I am trying to downsample the 2d array spatially using a custom function to find the mode (i.e., the most frequently occurring value): def find_mode(window): # find the mode over all axes uniq = np.unique(window, return_counts=True) return uniq[0][np.argmax(uniq[1])]  The find_mode() works well for ar as find_mode(ar) gives 0. However, it doesn't work for da (i.e., da.coarsen(x=2, y=2).reduce(find_mode)), with an error: TypeError: find_mode() got an unexpected keyword argument 'axis'  Thank you so much for your attention and participation.

Answer: The signature for functions passed to DatasetCoarsen.reduce must include axis and kwargs. A good example is np.sum. So your function would need to look something like: def find_mode(window, axis=None, **kwargs): # find the mode over all axes uniq = np.unique(window, return_counts=True) ret = uniq[0][np.argmax(uniq[1])] ret = np.atleast_2d(ret) return ret  Depending on your application, you may want to use the axis argument (tuple of integers) in place of your [0] and [1] indexing steps. Note: I've added np.atleast_2d here to make sure the return array is 2D. This is a bit ugly so I recommend thinking a bit more about this part in your application. The key thing to understand is that the return array needs to have the same number of dimensions as the input array.
Question: I need to perform an action without changing the global working directory. My case is I have a few folders, and in each, there are a few files. I need to do some computations using those files. Initially, I tried the following: with os.chdir('/directory'): ...some code needing execution inside  but got AttributeError: __enter__. After reading up online using with seems not to be an option. Therefore I'm looking to find another elegant way of doing so. I also tried just using os statements like so: cwd = os.getcwd() os.chdir('/directory') ..run code inside directory os.chdir(cwd)  but this is a pain during debugging and seems like a bad practice.

Answer: You can write your own context manager to temporarily change the working directory. import contextlib @contextlib.contextmanager def new_cd(x): d = os.getcwd() # This could raise an exception, but it's probably # best to let it propagate and let the caller # deal with it, since they requested x os.chdir(x) try: yield finally: # This could also raise an exception, but you *really* # aren't equipped to figure out what went wrong if the # old working directory can't be restored. os.chdir(d) with new_cd('/directory'): ...
Question: As mentioned in Python documentation, sys.intern() only accepts string objects. I understand why mutable types are not supported by sys.intern. But there's at least one more immutable type for which interning would make sense: bytes. So here's my question: is there any particular reason why Python interning doesn't support bytes?

Answer: This was suggested a decade ago on the Python-Dev mailing list. The answer is:  The main difference is that sys.intern() will remove the interned strings when every external reference vanishes. It requires either weakref'ability (which both str and bytes lack) or special cooperation from the object destructor (which is why sys.intern() is restricted to str instead of working with arbitrary objects).  Clearly it is possible to add support for bytes, but it seems very niche, not something standard Python is likely to add. That doesn't stop you from making your own equivalent, unless the whole reason you want it is for dictionary key lookup speed. I've never seen anyone use bytes as dictionary keys, but I'm sure some people do.
Question: I'm making a crud in fastapiI have a user model and I created another one called showuser to only show some specific fields in the query, but when I execute the request I get an error. I just want my request to show the fields I have in showuser.  my schemas from pydantic import BaseModel from typing import Optional from datetime import datetime # Create a User model # Create a class for the user class User(BaseModel): username: str password: str name: str lastname: str address: Optional[str] = None telephone: Optional[int] = None email: str creation_user: datetime = datetime.now() # Create UserId model # Create a class for the UserId class UserId(BaseModel): id: int # Create a ShowUser model # Create a class for the ShowUser class ShowUser(BaseModel): username: str name: str lastname: str email: str class Config(): orm_mode = True  and this is the code from user where I implement the api @router.get('/{user_id}', response_model=ShowUser) def get_user(user_id: int, db: Session = Depends(get_db)): user = db.query(models.User).filter(models.User.id == user_id).first() if not user: return {"Error": "User not found"} return {"User": user}  Terminal Message pydantic.error_wrappers.ValidationError: 4 validation errors for ShowUser response -> username field required (type-value_error.missing) response -> name field required (type=value_error.missing) response -> lastname field required (type=value_error.missing) response -> email field required (type=value_error.missing) 

Answer: I think the return value of your get_user function is the issue. Rather than returning {"User": user}, try returning just the user object as shown below: @router.get('/{user_id}', response_model=ShowUser) def get_user(user_id: int, db: Session = Depends(get_db)): user = db.query(models.User).filter(models.User.id == user_id).first() if not user: return {"Error": "User not found"} return user  EDIT: The same error will occur if the database does not contain a User object matching the value of user_id. Rather than returning {"Error": "User not found"}, the best way to handle this very common scenario is to raise an HTTPException with a 404 status code and error message: @router.get('/{user_id}', response_model=ShowUser) def get_user(user_id: int, db: Session = Depends(get_db)): user = db.query(models.User).filter(models.User.id == user_id).first() if not user: raise HTTPException( status_code=int(HTTPStatus.NOT_FOUND), detail=f"No user exists with user.id = {user_id}" ) return user
Question: I have the following example dataframe: d = {'col1': [4, 2, 8, 4, 3, 7, 6, 9, 3, 5]} df = pd.DataFrame(data=d) df col1 0 4 1 2 2 8 3 4 4 3 5 7 6 6 7 9 8 3 9 5  I need to add col2 to this dataframe, and values of this new column will be set by comparing col1 values (from different rows) as described below. Each row of col2 will be set as following: df.loc[0, "col2"] will say how many of df.loc[1, "col1"], df.loc[2, "col1"] and df.loc[3, "col1"] are bigger than df.loc[0, "col1"]. df.loc[1, "col2"] will say how many of df.loc[2, "col1"], df.loc[3, "col1"] and df.loc[4, "col1"] are bigger than df.loc[1, "col1"]. df.loc[2, "col2"] will say how many of df.loc[3, "col1"], df.loc[4, "col1"] and df.loc[5, "col1"] are bigger than df.loc[2, "col1"]. And so on... If there are not 3 rows left after the index N, col2 value will be set to -1. The end result will look like the following:  col1 col2 0 4 1 1 2 3 2 8 0 3 4 2 4 3 3 5 7 1 6 6 1 7 9 -1 8 3 -1 9 5 -1  I need a function that will take a dataframe as input and will return the dataframe by adding the new column as described above. In the example above, next 3 rows are considered. But this needs to be configurable and should be an input to the function that will do the work. Speed is important here so it is not desired to use for loops. How can this be done in the most efficient way in Python?

Answer: You need a reversed rolling to compare the values to the next ones: N = 3 df['col2'] = (df.loc[::-1, 'col1'] .rolling(N+1) .apply(lambda s: s.iloc[:-1].gt(s.iloc[-1]).sum()) .fillna(-1, downcast='infer') )  Alternatively, using numpy.lib.stride_tricks.sliding_window_view: import numpy as np from numpy.lib.stride_tricks import sliding_window_view as swv N = 3 df['col2'] = np.r_[(df['col1'].to_numpy()[:-N, None] < swv(df['col1'], N)[1:] # broadcasted comparison ).sum(axis=1), # count True per row -np.ones(N, dtype=int)] # add missing -1  Output:  col1 col2 0 4 1 1 2 3 2 8 0 3 4 2 4 3 3 5 7 1 6 6 1 7 9 -1 8 3 -1 9 5 -1
Question: Lately I was doing some ML stuff with Python using scikit-learn package. I wanted to use make_blobs() function so I began writing code for example: X, y = make_blobs(n_samples=m, centers=2, n_features=2, center_box=(80, 100))  and of course this is fine. However while coding next lines my Intellisense within Visual Studio Code (I have only Microsoft addons for Python installed just to be clear) started to showing weird error on that line I mentioned before. Here's full error message:  Expression with type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any] | tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" cannot be assigned to target tuple Type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any]" is incompatible with target tuple Element size mismatch; expected 2 but received 3  Please notice the last sentence. Element size mismatch where make_blobs() function returned 3 elements. What??? I've checked scikit-learn documentation for make_blobs() function and I've read that on default make_blobs() returns only 2 elements not 3. 3 elements can be returned when return_centers is set to True, where I have not set that to true as you can see in my example. Ok, maybe I'll try to expect those 3 elements, so I modified that line X, y, _ = make_blobs(n_samples=m, centers=2, n_features=2, center_box=(80, 100))  and well... this is the error message...  Expression with type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any], ndarray[Any, dtype[float64]] | Any] | tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" cannot be assigned to target tuple Type "tuple[Unknown | list[Unknown] | NDArray[float64], Unknown | list[Unknown] | NDArray[Any]]" is incompatible with target tuple Element size mismatch; expected 3 but received 2  Now it returns 2 elements?! What I have tried next is:  reinstall scikit-learn package. Same effect purging Python with all it files. Same effetc reinstalling Microsoft python extension for vscode. Same effect  Clearly it is some kind of intellisense issue, because running the code works fine, but what cause this behaviour? Python I used was 3.10.9 and 3.11.1. Running on Windows 10 22H2 19045.2364. VSCode up-to-date. For completeness scikit-learn version is 1.2.0

Answer: This is a known behaviour of pyright (which is a Python type checker used in Intellisense). It raises a return type mismatch warning if there is at least one return statement within the function that's incompatible with what you're expecting. See a similar issue in their repo for more details and an explanation from one of the maintainers. You can suppress type checking for this particular line with a comment though: X, y = make_blobs(n_samples=m, centers=2, n_features=2, center_box=(80, 100)) # pyright: ignore
Question: I'm new to JAX and writing code that JIT compiles is proving to be quite hard for me. I am trying to achieve the following: Given an (n,n) array mat in JAX, I would like to add a (1,n) or an (n,1) array to an arbitrary row or column, respectively, of the original array mat. If I wanted to add a row array, r, to the third row, the numpy equivalent would be, # if mat is a numpy array mat[2,:] = mat[2,:] + r  The only way I know how to update an element of an array in JAX is using array.at[i].set(). I am not sure how one can use this to update a row or a column without explicitly using a for-loop.

Answer: JAX arrays are immutable, so you cannot do in-place modifications of array entries. But you can accomplish similar results with the np.ndarray.at syntax. For example, the equivalent of mat[2,:] = mat[2,:] + r  would be mat = mat.at[2,:].set(mat[2,:] + r)  But you can use the add method to be more efficient in this case: mat = mat.at[2:].add(r)  Here is an example of adding a row and column array to a 2D array: import jax.numpy as jnp mat = jnp.zeros((5, 5)) # Create 2D row & col arrays, as in question row = jnp.ones(5).reshape(1, 5) col = jnp.ones(5).reshape(5, 1) mat = mat.at[1:2, :].add(row) mat = mat.at[:, 2:3].add(col) print(mat) # [[0. 0. 1. 0. 0.] # [1. 1. 2. 1. 1.] # [0. 0. 1. 0. 0.] # [0. 0. 1. 0. 0.] # [0. 0. 1. 0. 0.]]  See JAX Sharp Bits: In-Place Updates for more discussion of this.
Question: I got a long single column DataFrame as following table:     Column A     Cell 1   Cell 2   Cell 3   Cell 4   Cell 5   Cell 6   Cell 7   Cell 8     I want to split column A in order with specify row quantity and add to others new columns If I give 2 row quantity for each column     Column A Column B Column C Column D     Cell 1 Cell 3 Cell 5 Cell 7   Cell 2 Cell 4 Cell 6 Cell 8     split long a single column to new adding column by given row quantity.

Answer: You can use the underlying numpy array to reshape in Fortran order (rows, then columns): from string import ascii_uppercase N = 2 out = (pd.DataFrame(df['Column A'].to_numpy().reshape(N, -1, order='F')) # the line below is optional, just to have the column names .rename(columns=dict(enumerate(ascii_uppercase))).add_prefix('Column ') )  Output:  Column A Column B Column C Column D 0 Cell 1 Cell 3 Cell 5 Cell 7 1 Cell 2 Cell 4 Cell 6 Cell 8  If you want to handle N that are non multiples of len(df), you can add a reindex step to pad the DataFrame with NaNs: N = 3 out = (pd.DataFrame(df['Column A'].reindex(range(int(np.ceil(len(df)/N)*N))) .to_numpy().reshape(N, -1, order='F')) .rename(columns=dict(enumerate(ascii_uppercase))).add_prefix('Column ') )  Output:  Column A Column B Column C 0 Cell 1 Cell 4 Cell 7 1 Cell 2 Cell 5 Cell 8 2 Cell 3 Cell 6 NaN
Question: I have a pandas DataFrame with three columns:  X Y Z 0 1 4 True 1 2 5 True 2 3 6 False  How do I make it so that I have two columns X and Z with values:  X Z 0 1 True 1 2 True 2 3 False 3 4 True 4 5 True 5 6 False 

Answer: you can melt: In [41]: df.melt(id_vars="Z", value_vars=["X", "Y"], value_name="XY")[["XY", "Z"]] Out[41]: XY Z 0 1 True 1 2 True 2 3 False 3 4 True 4 5 True 5 6 False   identifier variable is "Z": it will be repeated as necessary against value variables... ...which are X and Y name X and Y's together column to "XY", and select that and "Z" at the end  (you can chain .rename(columns={"XY": "X"}) if you want that column to be named X again.)
Question: print(sgrades_flat) ['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45'] print(s_grades) ['F', 'A', 'B', 'D', 'C']  I want to combine sgrades_flat and s_grades to look like ... ['Barrett', 'Edan', '70', '45', '59', 'F', 'Bradshaw', 'Reagan', '96', '97', '88', 'A' 'Charlton', 'Caius', '73', '94', '80', 'B' 'Mayo', 'Tyrese', '88', '61', '36', 'D' 'Stern', 'Brenda', '90', '86', '45', 'C']  My current strategy is to use this code: z=[] for i, x in zip(sgrades_flat[::5], s_grades): z.append(i+x) print(z)  but that output is: ['BarrettF', 'BradshawA', 'CharltonB', 'MayoD', 'SternC'] 

Answer: I would combine the list by iterating manually on them: sgrades_flat=['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45'] s_grades=['F', 'A', 'B', 'D', 'C'] it1 = iter(sgrades_flat) it2 = iter(s_grades) result = [] try: while True: for _ in range(5): result.append(next(it1)) result.append(next(it2)) except StopIteration: pass print(result)  prints ['Barrett', 'Edan', '70', '45', '59', 'F', 'Bradshaw', 'Reagan', '96', '97', '88', 'A', 'Charlton', 'Caius', '73', '94', '80', 'B', 'Mayo', 'Tyrese', '88', '61', '36', 'D', 'Stern', 'Brenda', '90', '86', '45', 'C']  (this still looks like a bad idea as a flat list is sub-optimal for such a data structure) Note that a one-liner without any manual iteration also does the same: import itertools grouped_sgrades = list(itertools.chain.from_iterable( sgrades_flat[i:i+5]+[s_grades[i//5]] for i in range(0,len(sgrades_flat),5)))  however, why flatten the lists? grouped_sgrades = [sgrades_flat[i:i+5]+[s_grades[i//5]] for i in range(0,len(sgrades_flat),5)]  result is a nice list of lists, which is approaching some structured data: [['Barrett', 'Edan', '70', '45', '59', 'F'], ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], ['Charlton', 'Caius', '73', '94', '80', 'B'], ['Mayo', 'Tyrese', '88', '61', '36', 'D'], ['Stern', 'Brenda', '90', '86', '45', 'C']]
Question: I have 3 simple classes like: class Animal(abc.ABC): ... class Cat(Animal): ... class Dog(Animal): ...  Then I have a function which is annotated as such: def speak(animals: List[Animal]) -> List[str]: ...  My problem is that I want to constrain the List[Animal] to only include one type of animal, so: speak([Dog(), Dog()]) # OK speak([Cat(), Cat()]) # OK speak([Cat(), Dog()]) # typing error  How would I annotate the speak function to allow for this? Is it even possible to do using typing or am I forced to check this at runtime? I have tried to use the List[Animal] as above but that doesn't give me an error when calling speak like speak([Cat(), Dog()]). I have also tried messing around with generics like TypeVar('T', bound=Animal) but this still allows me to pass in a List of any combination of subclasses.

Answer: I would consider your issue to be not yet well defined. Once you start filling in a more concrete implementation of Animal, you're possibly going to arrive at a convincing solution. Here, I'll reword your criteria for speak as it currently stands: You want it to accept a list of any individual subclass of Animal, but not Animal itself. Hopefully we can see why this doesn't make sense - there's nothing in your given code that suggests Animal can be distinguished from any subclass of Animal, at least from the point of view to what speak will do to the list of animals. Let's provide some distinguishing features instead: Python 3.10 import typing as t from typing_extensions import LiteralString from collections.abc import Sequence Sound = t.TypeVar("Sound", bound=LiteralString) class Animal(t.Generic[Sound]): def speak(self) -> Sound: ... class Cat(Animal[t.Literal["meow"]]): ... class Dog(Animal[t.Literal["bark"]]): ... def speak(animals: Sequence[Animal[Sound]]) -> list[str]: return [animal.speak() for animal in animals] >>> speak([Dog(), Dog()]) # OK >>> speak([Cat(), Cat()]) # OK >>> >>> # mypy: Argument 1 to "speak" has incompatible type "List[object]"; expected "Sequence[Animal[<nothing>]]" [arg-type] >>> # pyright: Argument of type "list[Cat | Dog]" cannot be assigned to parameter "animals" of type "Sequence[Animal[Sound@speak]]" in function "speak" >>> # pyre: Incompatible parameter type [6]: In call `speak`, for 1st positional only parameter expected `Sequence[Animal[Variable[Sound (bound to typing_extensions.LiteralString)]]]` but got `List[Union[Cat, Dog]]` >>> speak([Cat(), Dog()])  Note that although mypy doesn't complain about the signature squeak(animals: list[Animal[Sound]]), this is technically not type-safe; you may decide to append Cat() to list[Dog]. This is why Sequence is used (it is non-mutable and covariant in its element types).