Skip to content

Instantly share code, notes, and snippets.

@gcr
Created July 6, 2009 21:54
Show Gist options
  • Select an option

  • Save gcr/141715 to your computer and use it in GitHub Desktop.

Select an option

Save gcr/141715 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import pprint
r = re.compile(r'^([\d.]+[KGM]?) +(\w+)')
homes = []
# How much is a K or an M or a G worth?
sizemap = {"K":"*1024", "M": "*1024"*2, "G": "*1024"*3, "T": "*1024"*4}
for line in file('/tmp/homes'):
size, name = r.match( line ).groups()
# Calculate a math expression:
# 24.5M turns into 24 * 1024 * 1024
sizexpr = size
for key, value in sizemap.items():
sizexpr = sizexpr.replace(key, value)
# Convert that back into a number
realsize = eval(sizexpr)
homes.append((realsize, size, name))
# Sort the entries in reverse by the number we computed
homes.sort(key= lambda i: - i[0])
# Print them
for entry in homes:
print "%s %s" % (entry[1], entry[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment