types

How to type annotate reduce function?

How to type annotate reduce function? Question: I’m writing a very thin wrapper on top of list and I want to define a method called reduce, but I’m struggling to annotate it properly such that pylance, mypy & pylint cut their complaints whenever I use the method, or even define it. I was perturbed to …

Total answers: 1

How to alias a type used in a class before it is declared?

How to alias a type used in a class before it is declared? Question: I have code similar to this example import typing class C(): def __init__(self, callback: typing.Callable[[C], int]): self._callback = callback def getCallback() -> typing.Callable[[C], int]: return self._callback class C2(): def __init__(self, cInstance: C): self._cInstance = cInstance def f() -> typing.NoReturn: self._cInstance.getCallback()(self.cInstance) and …

Total answers: 2

Checking if elements in my dataframe columns have the same type

Checking if elements in my dataframe columns have the same type Question: I’m using Python and work with a dataframe df. When trying to check if for all columns, each row has the same type I wrote the following lines : a=0 first_object = df.loc[df.index[0]] for column in df: for i in range(0,len(df)): if type(df[column][i]) …

Total answers: 1

Changing data types twice in one conditional statement (Python)

Changing data types twice in one conditional statement (Python) Question: Pretty basic question, but I am trying to write a simple while loop in which I first need an input as int. But in order to break out of said loop, I want to be able to write ‘quit’ which is a string. How do …

Total answers: 2

Why is median blur not working? – OpenCV – Python

Why is median blur not working? – OpenCV – Python Question: I have a function to add gaussian noise to an image read by OpenCV with imread that returns an image (matrix). I am trying to use median blur on that image but terminal returns this error: median = cv2.medianBlur(image, 5) ^^^^^^^^^^^^^^^^^^^^^^^^ cv2.error: OpenCV(4.7.0) D:/a/opencv-python/opencv-python/opencv/modules/imgproc/src/median_blur.simd.hpp:870: …

Total answers: 1

how to loop through a list of integers

how to loop through a list of integers Question: I’m trying to loop through a list of integers by using a for loop. However, we know that in python, int is not iterable. So I’m wondering if there’s any ways I can store integers in another way so that I can loop through the integers? …

Total answers: 5

Mutable NamedTuple in python

Mutable NamedTuple in python Question: I’m looking for a good struct pattern in python. The NamedTuple class constructor is almost what I want. Consider the following: class MyStruct(NamedTuple): prop1: str prop2: str prop3: Optional[str] = "prop3" # should allow default values prop4: Optional[str] = "prop4" … # allow this struct = MyStruct(prop1="prop1", prop2="prop2") # allow …

Total answers: 2

Annotation with custom types derived from numerical types in Python

Annotation with custom types derived from numerical types in Python Question: I am trying to create a custom type/class in Python that plays well with annotations and also can be checked in runtime. The only way I was able to achieve this is by using TypeAlias. from __future__ import annotations import typing FloatAlias: typing.TypeAlias = …

Total answers: 1

Structured numpy array how to assign type correctly

Structured numpy array how to assign type correctly Question: My attempts to create structured arrays are failing: I create an array shape (4,5) sample = np.array([[0.01627555, 1.55885081, 1.99043222, 0.00898849, 1.43987417], [0.01875182, 0.97853587, 2.09924081, 0.00474326, 1.31002428], [0.01905054, 1.74849054, 1.78033106, 0.01303594, 1.28518933], [0.01753927, 1.22486495, 1.88287677, 0.01823483, 1.36472148]]) assign dtype for each of the five columns: sample.dtype = …

Total answers: 2

Typehinting function with two possible call signatures

Typehinting function with two possible call signatures Question: I have a class which takes a function as a parameter, and I want that function to either have signature int -> int or list[int] -> int. I’ve type hinted it using a Union as follows. from typing import Callable, Union class Foo: def __init__(self, func: Callable[[Union[int, …

Total answers: 1