Skip to content

Instantly share code, notes, and snippets.

@vitiral
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitiral/616b229619512ca6c99a to your computer and use it in GitHub Desktop.
Save vitiral/616b229619512ca6c99a to your computer and use it in GitHub Desktop.
python code to get all the information about the platform into a dictionary
# python 3
import os
import platform
import ctypes
import re
import subprocess
import psutil
def processor():
'''Get type of processor
http://stackoverflow.com/questions/4842448/getting-processor-information-in-python
'''
out = None
if platform.system() == "Windows":
out = platform.processor()
elif platform.system() == "Darwin":
path = os.environ['PATH']
os.environ['PATH'] = os.environ['PATH'] + os.pathsep + '/usr/sbin'
try:
command = "sysctl -n machdep.cpu.brand_string"
out = subprocess.check_output(command, shell=True).strip().decode()
finally:
os.environ['PATH'] = path
elif platform.system() == "Linux":
command = "cat /proc/cpuinfo"
all_info = subprocess.check_output(command, shell=True).strip().decode()
for line in all_info.split("\n"):
if "model name" in line:
out = re.sub(".*model name.*:", "", line, 1)
if out is None:
return platform.processor()
else:
return out
def platform_dict(self):
'''Returns platform data
'''
out = {key: getattr(platform, key)() for key in
('machine', 'version', 'platform', 'dist', 'system')}
out['python'] = {key: getattr(platform, "python_" + key)() for key in
('implementation', 'compiler', 'version',
'version_tuple')
}
out['python']['architecture'] = int(ctypes.sizeof(ctypes.c_voidp) * 8)
out['processor'] = processor()
out['cores'] = len(psutil.cpu_percent(percpu=True))
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment