1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/psutil/examples/meminfo.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,31 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. 1.7 +# Use of this source code is governed by a BSD-style license that can be 1.8 +# found in the LICENSE file. 1.9 + 1.10 +""" 1.11 +Print system memory information. 1.12 +""" 1.13 + 1.14 +import psutil 1.15 +from psutil._compat import print_ 1.16 + 1.17 +def to_meg(n): 1.18 + return str(int(n / 1024 / 1024)) + "M" 1.19 + 1.20 +def pprint_ntuple(nt): 1.21 + for name in nt._fields: 1.22 + value = getattr(nt, name) 1.23 + if name != 'percent': 1.24 + value = to_meg(value) 1.25 + print_('%-10s : %7s' % (name.capitalize(), value)) 1.26 + 1.27 +def main(): 1.28 + print_('MEMORY\n------') 1.29 + pprint_ntuple(psutil.virtual_memory()) 1.30 + print_('\nSWAP\n----') 1.31 + pprint_ntuple(psutil.swap_memory()) 1.32 + 1.33 +if __name__ == '__main__': 1.34 + main()