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: …
Tag: python
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 …
Open document with default OS application in Python, both in Windows and Mac OS
I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double-click on the document icon in Explorer or Finder. What is the best way to do this in Python? Answer open and start are command-interpreter things for Ma…
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 ̶…
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
How to serialize Python objects in a human-readable format? [closed]
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago. The community reviewed whether to reopen this question 1 year ago and left it closed: Origi…
Determining application path in a Python EXE generated by pyInstaller
I have an application that resides in a single .py file. I’ve been able to get pyInstaller to bundle it successfully into an EXE for Windows. The problem is, the application requires a .cfg file that always sits directly beside the application in the same directory. Normally, I build the path using the …