I've a class which returns the health statistics of the machine.
class HealthMonitor(object):
"""Various HealthMonitor methods."""
@classmethod
def get_uptime(cls):
"""Get the uptime of the system."""
return uptime()
@classmethod
def detect_platform(cls):
"""Platform detection."""
return platform.system()
@classmethod
def get_cpu_usage(cls):
"""Return CPU percentage of each core."""
return psutil.cpu_percent(interval=1, percpu=True)
@classmethod
def get_memory_usage(cls):
"""Return current memory usage of a machine."""
memory = psutil.virtual_memory()
return {
'used': memory.used,
'total': memory.total,
'available': memory.available,
'free': memory.free,
'percentage': memory.percentage
}
@classmethod
def get_stats(cls):
return {
'memory_usage': cls.get_memory_usage(),
'uptime': cls.uptime(),
'cpu_usage': cls.get_cpu_usage(),
'security_logs': cls.get_windows_security_logs()
}
Method get_stats
will be called from outside the class. Which is the correct way to defining the related functions. Using classmethods
or staticmethods
or make an object of the class and then call the get_stats
.
I've read enough on the differences, but still want to clear my understanding with an example. Which is the more pythonic approach?