Saturday, March 12, 2016

マックのストレージが一杯だったのでストレージ調査のスクリプト作ってみた

Skip to content
This repository
Pull requests
Issues
Gist
@keitaroemotion
1
0
0
keitaroemotion/stan
Code
Issues 0
Pull requests 0
Wiki
Pulse
Graphs
Settings
stan/stan
66eff34 3 minutes ago
@keitaroemotion keitaroemotion anonymous func
executable file 41 lines (29 sloc) 914 Bytes
#!/usr/bin/env python
import os
import sys
def get_size(f):
return os.path.isfile(f) and os.path.getsize(f) or get_dir_size(f)
def ishidden(f):
f.startswith('.')
def get_dir_size(file):
return addup([get_size("%s/%s" % (file,f)) for f in listdir(file)])[0]
def addup(arg):
return (len(arg) == 0) and [0] or [reduce(lambda x,y:x+y, arg)]
def listdir(d):
try:
return os.listdir(d)
except:
return []
def get_directory_input():
return len(sys.argv) == 1 and "." or sys.argv[1]
def getValue(x, denom, box=()):
return denom > 999 and getValue(x%denom,denom/1000, box+(x/denom,)) or box+(x,)
def unitmap(key):
return {
1000000000:"GB",
1000000 :"MB",
1000 :"KB"
}[key]
def b2gb(m):
return (lambda t: "%sG %sM %sK" % (t[0], t[1], t[2]))(getValue(m, 1000000000))
print b2gb(get_dir_size(get_directory_input()))
Status API Training Shop Blog About Pricing
© 2016 GitHub, Inc. Terms Privacy Security Contact Help
view raw stan hosted with ❤ by GitHub
数学的な観点でみるとやっぱり再帰ってこういうところで力発揮すると思う。
ここでG、M、KBの解析を割り算とか でべたべた書いて行くと、汚いプログラムの温床になる。汚いコードってことは、後始末が大変ってことだ。



上と下、やってることはほぼ同じなんだけど、上の作り方は負の遺産になる。
まずエントロピーが高すぎるし、そもそも数学的に美しい作り方ではない。

つまりバグも出やすく、壊れやすく、メンテナンスもしにくい(工数があとあと雪だるま式にふくれあがる)。

再帰関数ってのはもっとも基礎的であって、もっともSEの力量が問われるトピックだと思う。

https://github.com/keitaroemotion/stan/blob/master/stan

No comments:

Post a Comment