I’m developing/testing a package in my local directory. I want to import it in the interpreter (v2.5), but sys.path does not include the current directory. Right now I type in sys.path.insert(0,’.’). Is there a better way? Also, fails with this error: Answer You can use relative imports only…
Tag: python
How do I capture SIGINT in Python?
I’m working on a python script that starts several processes and database connections. Every now and then I want to kill the script with a Ctrl+C signal, and I’d like to do some cleanup. In Perl I’d do this: How do I do the analogue of this in Python? Answer Register your handler with signal…
Parse annotations from a pdf
I want a python function that takes a pdf and returns a list of the text of the note annotations in the document. I have looked at python-poppler (https://code.launchpad.net/~poppler-python/poppler-python/trunk) but I can not figure out how to get it to give me anything useful. I found the get_annot_mapping m…
How can I get a flat result from a list comprehension instead of a nested list?
I have a list A, and a function f which takes an item of A and returns a list. I can use a list comprehension to convert everything in A like [f(a) for a in A], but this returns a list of lists. Suppose my input is [a1,a2,a3], resulting in [[b11,b12],[b21,b22],[b31,b32]]. How can I get the flattened list [b11…
How do you get Python documentation in Texinfo Info format?
Since Python 2.6, it seems the documentation is in the new reStructuredText format, and it doesn’t seem very easy to build a Texinfo Info file out of the box anymore. I’m an Emacs addict and prefer my documentation installed in Info. Does anyone have Python 2.6 or later docs in Texinfo format? How…
How to import a Python class that is in a directory above?
I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file? Answer from ..subpkg2 import mod Per the Python docs: When inside a package hierarchy, use two dots, as the import statement doc says: When specifying what module to import y…
What’s the difference between “2*2” and “2**2” in Python?
What is the difference between the following statements? Statement 1: Statement 2: I see no difference. This raises the following question. Why is Statement 1 used if we can use Statement 2? Answer Try: and to see the difference. ** is the operator for “power of”. In your particular operation, 2 t…
Get the index of an element in a queryset
I have a QuerySet, let’s call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let’s call it obj. Now I’d like to know at what index obj has in qs, as efficiently as possible. I know that I could use .index() from Python or possibly loop t…
How to replace whitespaces with underscore?
I want to replace whitespace with underscore in a string to create nice URLs. So that for example: Should become I am using Python with Django. Can this be solved using regular expressions? Answer You don’t need regular expressions. Python has a built-in string method that does what you need:
How do I pass a variable by reference?
Are parameters passed by reference or by value? How do I pass by reference so that the code below outputs ‘Changed’ instead of ‘Original’? See also: Why can a function modify some arguments as perceived by the caller, but not others? Answer Arguments are passed by assignment. The ratio…