Skip to content

How to help tqdm figure out the total in a custom iterator

I’m implementing my own iterator. tqdm does not show a progressbar, as it does not know the total amount of elements in the list. I don’t want to use “total=” as it looks ugly. Rather I would prefer to add something to my iterator that tqdm can use to figure out the total. Is this even…

Error handling for list comprehension in Python

Is there a way to handle errors in a python list comprehension. Preferably I would have something like this where the last two values are represented by None: This throws a syntax error and the only way I can do it is: I hope there is a more ‘neat’ way of doing this because the current solution is…

How can mypy ignore a single line in a source file?

I’m using mypy in my python project for type checking. I’m also using PyYAML for reading and writing the project configuration files. Unfortunately, when using the recommended import mechanism from the PyYAML documentation this generates a spurious error in a try/except clause that attempts to imp…

Is there an efficient way to create a random bit mask in Pytorch?

I want to have a random bit mask that has some specified percent of 0s. The function I devised is: To illustrate: The main issue I have with this method is it requires the rate to divide the shape. I want a function that accepts an arbitrary decimal and gives approximately rate percent of 0s in the bitmask. F…

Counting specific words in a sentence

I am currently trying to solve this homework question. My task is to implement a function that returns a vector of word counts in a given text. I am required to split the text into words then use NLTK’s tokeniser to tokenise each sentence. This is the code I have so far: There are two doctests that shou…