analitics

Pages

Friday, January 22, 2016

wmi python module - part 001.

Named WMI from Windows Management Instrumentation, this python module allow you to use Microsoft’s implementation of Web-Based Enterprise Management ( WBEM ).
Is a set of extensions to the Windows Driver Model AND that provides an operating system interface.
allows you to scripting languages like VBScript to manage Microsoft Windows personal computers and servers, both locally and remotely.
You cand read about this python module here.

C:\Python34\Scripts>pip install  wmi
...
Installing collected packages: wmi
Running setup.py install for wmi
warning: install_data: setup script did not provide a directory for 'readme.
txt' -- installing right in 'C:\Python34'
...
Successfully installed wmi
Cleaning up...

Let try first example :

C:\Python34>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wmi
>>> remote_process = wmi.WMI (computer="home").new ("Win32_Process")
>>> for i in wmi.WMI ().Win32_OperatingSystem ():
...     print (i.Caption)
...
Microsoft Windows 10 Home

Now let's see another example can used by you with wmi python module.
This example let you see your processes.

import wmi
import datetime
c = wmi.WMI()
process_watcher = c.Win32_Process.watch_for("modification")
while True:
  new_process = process_watcher()
  print (new_process.Caption)

I used the python version 3.3.5 and Spyder ( Scientific PYthon Development EnviRonment ) to test the script.
You can change .watch_for method args with: creation, deletion, modification or operation.

Saturday, November 28, 2015

The svgwrite python module - part 001.

The svgwrite python module is a tool by Manfred Moitzi.
To install this python module use Python 3.4 version.
Then you need to install svgwrite with pip3.4:
C:\Python34\Scripts>pip3.4.exe install svgwrite
Collecting svgwrite
  Downloading svgwrite-1.1.6.tar.gz (109kB)
    100% |################################| 110kB 354kB/s
Also this svgwrite is the only module that needs to be imported and then you can deal with this module.
See this simple example:
import svgwrite
from svgwrite import *
#this will work with svgwrite
svg_doc = svgwrite.Drawing(filename = "test-svgwrite.svg",\
                                size = ("30px", "30px"))\
svg_doc.add(svg_doc.rect(insert = (0, 0),\
                                   size = ("20px", "20px"),\
                                   stroke_width = "1",\
                                   stroke = "green",\
                                   fill = "rgb(255,255,0)"))\
print(svg_doc.tostring())
svg_doc.save()
The result of this python script is this SVG file.

If you use the dir() then you can see more about this python module:
>>> dir(svgwrite)
['AUTHOR_EMAIL', 'AUTHOR_NAME', 'CYEAR', 'Drawing', 'Hz', 'Unit', 'VERSION', '__
builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__p
ackage__', '__path__', '__spec__', 'animate', 'base', 'cm', 'container', 'data',
 'deg', 'drawing', 'elementfactory', 'em', 'etree', 'ex', 'filters', 'grad', 'gr
adients', 'image', 'inch', 'kHz', 'masking', 'mixins', 'mm', 'params', 'path', '
pattern', 'pc', 'percent', 'pt', 'px', 'rad', 'rgb', 'shapes', 'text', 'utils',
'validator2', 'version']
For example you can add some text:
svg_doc.add(svg_document.text("This add new svg text",\
                                   insert = (10, 10)))