Querying Info¶
Information about the computer can be queried on a per-component basis, or all components at once.
Complete Info Retrieval¶
In the following example, we query all available information from the system.
import pysysinfo
from pysysinfo.models.info_models import HardwareInfo
hm = pysysinfo.HardwareManager()
info = hm.fetch_hardware_info()
print(type(info))
print(isinstance(info, HardwareInfo))
Output on a macOS machine:
<class 'pysysinfo.models.info_models.MacHardwareInfo'>
True
fetch_hardware_info() returns an instance of an OS-specific HardwareInfo class, which has the following structure.
- class HardwareInfo¶
-
- memory: MemoryInfo | None¶
- storage: StorageInfo | None¶
- graphics: GraphicsInfo | None¶
- network: NetworkInfo | None¶
- display: DisplayInfo | None¶
- audio: AudioInfo | None¶
- baseboard: BaseboardInfo | None¶
Depending on the OS, one of the following classes is returned. Do note that the structure and usage remain the same.
- class WindowsHardwareInfo
Bases:
HardwareInfo
- class MacHardwareInfo
Bases:
HardwareInfo
- class LinuxHardwareInfo
Bases:
HardwareInfo
Single Component Retrieval¶
In the following example, we query data for just the CPU. The same structure can be followed for GPU, Memory, etc.
import pysysinfo
hm = pysysinfo.HardwareManager()
cpu_info = hm.fetch_cpu_info()
print(type(cpu_info))
print(cpu_info.name)
Output:
<class 'pysysinfo.models.cpu_models.CPUInfo'>
Apple M3
Accessing Retrieved Data¶
There are two ways to access the data retrieved from PySysInfo.
The first is to assign the output of fetch_hardware_info()
or the other single-component methods into a variable.
import pysysinfo
hm = pysysinfo.HardwareManager()
info = hm.fetch_hardware_info()
print(info.cpu.name)
print(info.cpu.architecture)
print(info.cpu.vendor)
Output:
Apple M3
ARM
Apple
Here’s another example:
import pysysinfo
hm = pysysinfo.HardwareManager()
storage = hm.fetch_storage_info()
print("Found", len(storage.disks), "storage devices")
print("==========")
for disk in storage.disks:
print("Name:", disk.model)
print("Size:", disk.size.capacity, disk.size.unit)
print("==========")
Output:
Found 1 storage devices
==========
Name: APPLE SSD AP0512Z
Size: 477102 MB
==========
The second way is to use the info attribute of the
HardwareManager.
Querying any data automatically populates the info attribute,
meaning it can be accessed directly from the HardwareManager instance.
import pysysinfo
hm = pysysinfo.HardwareManager()
hm.fetch_cpu_info()
print("CPU Name:", hm.info.cpu.name)
hm.fetch_storage_info()
print("Found", len(hm.info.storage.disks), "disks")
# CPU data is still available at this point.
print("CPU Manufacturer:", hm.info.cpu.vendor)
Output:
CPU Name: Apple M3
Found 1 disks
CPU Manufacturer: Apple
This is possible because hm.info is an instance of the HardwareInfo class.
The data returned from fetch_hardware_info() and hm.info are the exact same.
import pysysinfo
hm = pysysinfo.HardwareManager()
info = hm.fetch_hardware_info()
print(hm.info == info)
cpu_info = hm.fetch_cpu_info()
print(hm.info.cpu == cpu_info)
Output:
True
True
Errors during Hardware Discovery¶
Sometimes, errors may be encountered during hardware discovery that partially or fatally affect the process.
When querying data, each component will have a status property.
this property contains info about whether any errors were encountered when fetching data.
When querying per-component information:
cpu_info = hm.fetch_cpu_info()
print(cpu_info.status.type)
Output:
StatusType.SUCCESS
When querying complete information:
import pysysinfo
hm = pysysinfo.HardwareManager()
hm.fetch_hardware_info()
print("CPU:", hm.info.cpu.status.type)
print("Graphics:", hm.info.graphics.status.type)
print("Storage:", hm.info.storage.status.type)
Output:
CPU: StatusType.SUCCESS
Graphics: StatusType.SUCCESS
Storage: StatusType.SUCCESS
the status property follows the following structure:
- class Status
Describes the status of an individual component. If the status is
PARTIALorFAILED, there may be messages that describe the error(s).- type: StatusType
- messages: List[str]
The type attribute is an Enum.
Depending on the errors encountered, it can be one of the following three values.
- class StatusType
Types of statuses possible.
- SUCCESS = 'success'
There were no errors encountered.
- PARTIAL = 'partial'
There were errors encountered, but only some parts of the data could not be retrieved.
- FAILED = 'failed'
Fatal error occurred, no data could be retrieved.
When using this library, the following example may be of use, to handle partial and fatal errors.
import pysysinfo
from pysysinfo.models.status_models import StatusType
hm = pysysinfo.HardwareManager()
cpu = hm.fetch_cpu_info()
if cpu.status.type == StatusType.FAILED:
print("Failed - Fatal issue(s) occurred:")
for message in cpu.status.messages:
print(message)
exit(1) # Don't continue executing
elif cpu.status.type == StatusType.PARTIAL:
print("Partial Error - Issue(s) occurred:")
for message in cpu.status.messages:
print(message)
# Continue executing
print(cpu.name)
else:
# It is StatusType.SUCCESS
print("Successfully retrieved info!")
print(cpu.name)
Output:
Successfully retrieved info!
Apple M3