Skip to content
Advertisement

False or None vs. None or False

This behaviour confuses me. Could someone explain to me why is this happening like this? I expected them to both behave the same. Answer The expression x or y evaluates to x if x is true, or y if x is false. Note that “true” and “false” in the above sentence are talking about “truthiness”, not the fixed values True

How can I loop through blocks of lines in a file?

I have a text file that looks like this, with blocks of lines separated by blank lines: How can I loop through the blocks and process the data in each block? eventually I want to gather the name, family name and age values into three columns, like so: Answer Here’s another way, using itertools.groupby. The function groupy iterates through lines

How to change the font size on a matplotlib plot

How does one change the font size for all elements (ticks, labels, title) on a matplotlib plot? I know how to change the tick label sizes, this is done with: But how does one change the rest? Answer From the matplotlib documentation, This sets the font of all items to the font specified by the kwargs object, font. Alternatively, you

How to check that a regular expression has matched a string completely, i.e. – the string did not contain any extra character?

I have two questions: 1) I have a regular expression ([A-Z][a-z]{0,2})(d*) and I am using Python’s re.finditer() to match appropriate strings. My problem is, that I want to match only strings that contain no extra characters, otherwise I want to raise an exception. I want to catch a following pattern: – capital letter, followed by 0, 1 or 2 small

Can you have variables within triple quotes? If so, how?

This is probably a very simple question for some, but it has me stumped. Can you use variables within python’s triple-quotes? In the following example, how do use variables in the text: I would like it to result in: If not what is the best way to handle large chunks of text where you need a couple variables, and there

Call Class Method From Another Class

Is there a way to call the method of a class from another class? I am looking for something like PHP’s call_user_func_array(). Here is what I want to happen: Answer update: Just saw the reference to call_user_func_array in your post. that’s different. use getattr to get the function object and then call it with your arguments method is now an

Efficiently detect sign-changes in python

I want to do exactly what this guy did: Python – count sign changes However I need to optimize it to run super fast. In brief I want to take a time series and tell every time it crosses crosses zero (changes sign). I want to record the time in between zero crossings. Since this is real data (32 bit

Advertisement