analitics

Pages

Thursday, May 30, 2019

Python 3.7.3 : Using the win32com - part 007.

Today I show you a new script I tested a few days ago with win32com and Win32_LogonSession class, see thedocumentation.
The Win32_LogonSession WMI class describes the logon session or sessions associated with a user logged on to a computer system running Windows.
Let's see the script:
import win32com.client
from win32com.client import gencache
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Session")
for objItem in colItems:
    print("Caption" , objItem.Caption)
    print("Description" , objItem.Description)
    print("InstallDate" , objItem.InstallDate)
    print("Name" , objItem.Name)
    print("Status" , objItem.Status)
    print("StartTime" , objItem.StartTime)
    print("AuthenticationPackage" , objItem.AuthenticationPackage)
    print("LogonId" , objItem.LogonId)
    print("LogonType" , objItem.LogonType)
The result of this python script:
Caption None
Description None
InstallDate None
Name None
Status None
StartTime 20190530105325.022532+180
AuthenticationPackage LiveSSP
LogonId 8291403
LogonType 2
Caption None
Description None
InstallDate None
Name None
Status None
StartTime 20190530105325.022532+180
AuthenticationPackage LiveSSP
LogonId 8291220
LogonType 2
You can read info with this python module.
For example, the output tells me aboutLogonType.
You’ll see type 2 logons when a user attempts to log on at the local keyboard and screen whether with a domain account or a local account from the computer.

Python 3.7.3 : How fast are Data Classes.

This simple tutorial follows the PEP 0557 subject of Data Classes.
About the python classes then you can define into three ways:
  • standard_class;
  • slot_class ( standard_class with __slots__);
  • the new dataclass
Today I will show you how this works and how fast are these classes.
First, let's import some python modules: dataclasses and timeit.
The dataclasses python module lets us use the new dataclass.
This dataclass is a class decorator as defined in PEP 526.
For testing, I define a function to show us the output:
def  _str(n): return f'({n.x})'
This will get value and return it.
I define three classes for each type of these with one value named x and set to 1.0.
Each of these classes can have values and be used:
sc = standard_class(1.0)
sl = slot_class(1.0)
dc = new_dataclass(1.0)
This will set for each class the value 1.0 to x parameter.
The next step will start with the measure execution time of the time object creation for each of these classes.
Let's see the source code:
from dataclasses import dataclass
from timeit import timeit

def  _str(n): return f'({n.x})'

class standard_class:
 def __init__(self, x=0.0):
  self.x = x
 def __str__(self): return _str(self)

class slot_class:
 __slots__ = 'x'
 def __init__(self, x=0):
  self.x =x
 def __str__(self): return _str(self)

@dataclass
class new_dataclass:
 x: float = 0.0
 def __str__(self): return _str(self)

sc = standard_class(1.0)
sl = slot_class(1.0)
dc = new_dataclass(1.0)
print(sc,sl,dc)

time_sc=timeit('standard_class()', setup = 'from __main__ import standard_class')
print(f'standard class: {time_sc:.5f}')
time_sc=timeit('slot_class()', setup = 'from __main__ import slot_class')
print(f'standard class: {time_sc:.5f}')
time_sc=timeit('new_dataclass()', setup = 'from __main__ import new_dataclass')
print(f'standard class: {time_sc:.5f}')
The output is:
C:\Python373>python.exe dataclasses_001.py
(1.0) (1.0) (1.0)
standard class: 0.48912
standard class: 0.41349
standard class: 0.48514
As you can see, the new types of the class definition are not very fast but allow for other advantages and disadvantages.
You can read more about this at PEP 0557.


Monday, May 27, 2019

Python 3.7.3 : Using the win32com - part 006.

Today I will show you how to see the system environment setting:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Environment")
for objItem in colItems:
    print("Caption:", objItem.Caption)
    print("Description:", objItem.Description)
    print("Install Date:", objItem.InstallDate)
    print("Name:", objItem.Name)
    print("Status:", objItem.Status)
    print("System Variable:", objItem.SystemVariable)
    print("User Name:", objItem.UserName)
    print("Variable Value:", objItem.VariableValue)
The result is :
...
Install Date: None
Name: PATH
Status: OK
System Variable: False...

Sunday, May 26, 2019

Python 3.7.3 : Using the win32com - part 005.

Another example with win32com python module:
This script show info about the audio or video codec, see this link:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_CodecFile")
for objItem in colItems:
    print("Access Mask: " , objItem.AccessMask)
    print("Archive: " , objItem.Archive)
    print("Caption: " , objItem.Caption)
    print("Drive: " , objItem.Drive)
    print("Extension: " , objItem.Extension)
    print("File Name: " , objItem.FileName)
    print("File Size: " , objItem.FileSize)
    print("File Type: " , objItem.FileType)
    print("File System Name: " , objItem.FSName)
    print("Group: " , objItem.Group)
    print("Hidden: " , objItem.Hidden)
    print("Manufacturer: " , objItem.Manufacturer)
    print("Name: " , objItem.Name)
    print("Path: " , objItem.Path)
    print("Version: " , objItem.Version)
The output is this:
File Type:  Application Extension
File System Name:  NTFS
Group:  Video
Hidden:  False
Manufacturer:  Microsoft Corporation
Name:  C:\Windows\system32\MSVIDC32.DLL
Path:  \windows\system32\
Version:  6.3.9600.17415
Access Mask:  1179817
Archive:  True
Caption:  c:\windows\system32\l3codeca.acm
Drive:  c:
Extension:  acm
File Name:  l3codeca
File Size:  82432
File Type:  acm File
File System Name:  NTFS
Group:  Audio
Hidden:  False
Manufacturer:  Fraunhofer Institut Integrierte Schaltungen IIS
Name:  C:\Windows\system32\L3CODECA.ACM
Path:  \windows\system32\
Version:  1.9.0.401

Saturday, May 25, 2019

Python 3.7.3 : Using the win32com - part 004.

This is a source code I created a few days ago.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_ProgIDSpecification")
for objItem in colItems:
    print ("Caption: ", objItem.Caption)
    print ("Check ID: ", objItem.CheckID)
    print ("Check Mode: ", objItem.CheckMode)
    print ("Description: ", objItem.Description)
    print ("Name: ", objItem.Name)
    print ("Parent: ", objItem.Parent)
    print ("ProgID: ", objItem.ProgID)
    print ("Software Element ID: ", objItem.SoftwareElementID)
    print ("Software Element State: ", objItem.SoftwareElementState)
    print ("Target Operating System: ", objItem.TargetOperatingSystem)
    print ("Version: ", objItem.Version)
This script uses the win32com python module to show us info about Win32_ProgIDSpecification.
The result is strange and shows like this:
...
Version:  None
Caption:  Addin Class
...
See the full documentation at this link.

Friday, May 24, 2019

Python 3.7.3 : Using the win32com - part 003.

This is another python script with win32com python module and python version 3.7.3 .
The script shows us info about the drive:
import win32com
from win32com import client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
out_items = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive")
for object_item in out_items:
    if object_item.Availability != None:
        print("Availability:" +  str(object_item.Availability))
    if object_item.BytesPerSector != None:
        print("BytesPerSector:" +  str(object_item.BytesPerSector))
    print("Capabilities:")
    strList = " "
    try :
        for obj_element in object_item.Capabilities :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    print("CapabilityDescriptions:")
    strList = " "
    try :
        for obj_element in object_item.CapabilityDescriptions :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    if object_item.Caption != None:
        print("Caption:" +  str(object_item.Caption))
    if object_item.CompressionMethod != None:
        print("CompressionMethod:" + str(object_item.CompressionMethod))
    if object_item.ConfigManagerErrorCode != None:
        print("ConfigManagerErrorCode:" + str(object_item.ConfigManagerErrorCode))
    if object_item.ConfigManagerUserConfig != None:
        print("ConfigManagerUserConfig:" + str(object_item.ConfigManagerUserConfig))
    if object_item.CreationClassName != None:
        print("CreationClassName:" + str(object_item.CreationClassName))
    if object_item.DefaultBlockSize != None:
        print("DefaultBlockSize:" + str(object_item.DefaultBlockSize))
    if object_item.Description != None:
        print("Description:" + str(object_item.Description))
    if object_item.DeviceID != None:
        print("DeviceID:" + str(object_item.DeviceID))
    if object_item.ErrorCleared != None:
        print("ErrorCleared:" + str(object_item.ErrorCleared))
    if object_item.ErrorDescription != None:
        print("ErrorDescription:" + str(object_item.ErrorDescription))
    if object_item.ErrorMethodology != None:
        print("ErrorMethodology:" + str(object_item.ErrorMethodology))
    if object_item.Index != None:
        print("Index:" + str(object_item.Index))
    #object_item.InstallDate - this is not show
    if object_item.InterfaceType != None:
        print("InterfaceType:" + str(object_item.InterfaceType))
    if object_item.LastErrorCode != None:
        print("LastErrorCode:" + str(object_item.LastErrorCode))
    if object_item.Manufacturer != None:
        print("Manufacturer:" + str(object_item.Manufacturer))
    if object_item.MaxBlockSize != None:
        print("MaxBlockSize:" + str(object_item.MaxBlockSize))
    if object_item.MaxMediaSize != None:
        print("MaxMediaSize:" + str(object_item.MaxMediaSize))
    if object_item.MediaLoaded != None:
        print("MediaLoaded:" + str(object_item.MediaLoaded))
    if object_item.MediaType != None:
        print("MediaType:" + str(object_item.MediaType))
    if object_item.MinBlockSize != None:
        print("MinBlockSize:" + str(object_item.MinBlockSize))
    if object_item.Model != None:
        print("Model:" + str(object_item.Model))
    if object_item.Name != None:
        print("Name:" + str(object_item.Name))
    if object_item.NeedsCleaning != None:
        print("NeedsCleaning:" + str(object_item.NeedsCleaning))
    if object_item.NumberOfMediaSupported != None:
        print("NumberOfMediaSupported:" + str(object_item.NumberOfMediaSupported))
    if object_item.Partitions != None:
        print("Partitions:" + str(object_item.Partitions))
    if object_item.PNPDeviceID != None:
        print("PNPDeviceID:" + str(object_item.PNPDeviceID))
    print("PowerManagementCapabilities:")
    strList = " "
    try :
        for obj_element in object_item.PowerManagementCapabilities :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    if object_item.PowerManagementSupported != None:
        print("PowerManagementSupported:" + str(object_item.PowerManagementSupported))
    if object_item.SCSIBus != None:
        print("SCSIBus:" + str(object_item.SCSIBus))
    if object_item.SCSILogicalUnit != None:
        print("SCSILogicalUnit:" + str(object_item.SCSILogicalUnit))
    if object_item.SCSIPort != None:
        print("SCSIPort:" + str(object_item.SCSIPort))
    if object_item.SCSITargetId != None:
        print("SCSITargetId:" + str(object_item.SCSITargetId))
    if object_item.SectorsPerTrack != None:
        print("SectorsPerTrack:" + str(object_item.SectorsPerTrack))
    if object_item.Signature != None:
        print("Signature:" + str(object_item.Signature))
    if object_item.Size != None:
        print("Size:" + str(object_item.Size))
    if object_item.Status != None:
        print("Status:" + str(object_item.Status))
    if object_item.StatusInfo != None:
        print("StatusInfo:" + str(object_item.StatusInfo))
    if object_item.SystemCreationClassName != None:
        print("SystemCreationClassName:" + str(object_item.SystemCreationClassName))
    if object_item.SystemName != None:
        print("SystemName:" + str(object_item.SystemName))
    if object_item.TotalCylinders != None:
        print("TotalCylinders:" + str(object_item.TotalCylinders))
    if object_item.TotalHeads != None:
        print("TotalHeads:" + str(object_item.TotalHeads))
    if object_item.TotalSectors != None:
        print("TotalSectors:" + str(object_item.TotalSectors))
    if object_item.TotalTracks != None:
        print("TotalTracks:" + str(object_item.TotalTracks))
    if object_item.TracksPerCylinder != None:
        print("TracksPerCylinder:" + str(object_item.TracksPerCylinder))
The result looks something like this:
MediaLoaded:True
...
MediaType:Fixed hard disk media
Model:TOSHIBA MQ01ABF050
Name:\\.\PHYSICALDRIVE0
Partitions:2
PNPDeviceID:SCSI\DISK&VEN_TOSHIBA&PROD_MQ01ABF050\4&AD866E5&0&000000
PowerManagementCapabilities:
 null
SCSIBus:0
SCSILogicalUnit:0
SCSIPort:0
SCSITargetId:0
SectorsPerTrack:63
Signature:-1846003871
Size:500105249280
Status:OK
SystemCreationClassName:Win32_ComputerSystem
SystemName:CATAFEST
TotalCylinders:60801
TotalHeads:255
TotalSectors:976768065
TotalTracks:15504255
TracksPerCylinder:255