Well, the question pretty much summarises it. My db activity is very update intensive, and I want to programmatically issue a Vacuum Analyze. However I get an error that says that the query cannot be executed within a transaction. Is there some other way to do it? Answer This is a flaw in the Python DB-API: i…
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 “tr…
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. …
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, …
How to get numbers after decimal point?
How do I get the numbers after a decimal point? For example, if I have 5.55, how do i get .55? Answer An easy approach for 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: &…
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 varia…
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 the…
How come I can not activate my Virtual Python Environment with ‘source env/bin/activate’ command?
I am trying to activate my Virtual Python Environment to use with Pylons but I think I am executing the commands wrong. What am I doing wrong? How should I do it right? Answer I realize I had to do And here we see virtualenv.py. From here I just had to And then Solved :)
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