Skip to main content
  1. Logbooks/

Python Logbook

Pretty printing a Python object’s functions and attributes #

In everyday Python usage, you might use dir() to see an object’s functions and attributes, but it’s littered with dunder methods and the distinction between functions and attributes is not clear. Here’s a function that will print out the functions with “()” appended, and the attributes that aren’t dunder methods:

def ddir(obj):
    return [a + '()' if callable(getattr(obj, a)) else a
            for a in dir(obj) if not (a[:2] == '__' == a[-2:])]

Originally discovered here.

Listing files in a directory in numerical order #

Normally, if you have a bunch of files like 1-1000.png, glob.glob will sort them alphabetically, instead of from 1-1000. The way to have a correctly ordered list is to do this:

import glob
import os
files = sorted(glob.glob('output/*'), key=len)

where output is the directory with all the files.

Simple progress bar #

def progress(purpose, currentcount, maxcount):
   sys.stdout.write('\r')
   sys.stdout.write("{}: {} of {}, {.2f}%".format(purpose, currentcount+1, maxcount, (currentcount+1)/maxcount*100))
   sys.stdout.flush()
Kevin Lin
Author
Kevin Lin