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.