Skip to content

Tag: python

Python string join performance

There are a lot of articles around the web concerning Python performance. The first thing you read is concatenating strings should not be done using ‘+’; avoid s1 + s2 + s3, and instead use str.join I tried the following: concatenating two strings as part of a directory path: three approaches: &#8…

Cropping pages of a .pdf file

I was wondering if anyone had any experience in working programmatically with .pdf files. I have a .pdf file and I need to crop every page down to a certain size. After a quick Google search I found the pyPdf library for python but my experiments with it failed. When I changed the cropBox and trimBox attribut…

What is __init__.py for?

What is __init__.py for in a Python source directory? Answer It used to be a required part of a package (old, pre-3.3 “regular package”, not newer 3.3+ “namespace package”). Here’s the documentation. Python defines two types of packages, regular packages and namespace packages. R…

Cleanest way to run/debug python programs in windows

Python for Windows by default comes with IDLE, which is the barest-bones IDE I’ve ever encountered. For editing files, I’ll stick to emacs, thank you very much. However, I want to run programs in some other shell than the crappy windows command prompt, which can’t be widened to more than 80 …

How do I unload (reload) a Python module?

I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What’s the best way do do this? Answer You can reload a module when it has already been imported by using importlib.reload(): In Python 2, reload was a builtin. In Python 3, it was moved to …

How to iterate over a list in chunks

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don’t have control of the input, or I’d have it passed in as a list of four-element tuples. Currently, I’m iterating over it this way: It looks a lot like &#822…

Sorting and Grouping Nested Lists in Python

I have the following data structure (a list of lists) I would like to be able to Use a function to reorder the list so that I can group by each item in the list. For example I’d like to be able to group by the second column (so that all the 21’s are together) Use a function to only