Friday 11 April 2014

PyWinAtuto - Python Windows GUI Automation

'pywinauto' is a python module to automate windows GUI applications. 'pywinauto' in turn uses 'SendKeys' and 'ctypes'  python modules. In this post I'll discuss the installation of 'pywinauto' module and its dependencies. I'll also discuss automation of notepad  using pywinauto . So lets start..

  • Download and install Python 2.7
  • 'ctypes' module comes with Python 2.7 , so no need to install it.
  • Download appropriate version of SendKeys module installer from here.
  • All the dependencies are installed , now download 'pywinauto' bundle(zip file) from here.
  • Extract zip file to a folder
  • Copy pywinauto and sandbox folders from extracted location to "C:\Python27\Lib\site-packages" (default python external modules folder)
  • Installation of pywinauto is completed with previous step, test pywinauto using the below script. 
import pywinauto
app = pywinauto.application.Application()
app.start_("notepad")

above code will invoke notepad application.

As stage set for us, let us try to simple GUI automation of notepad application. Copy the below code and save it as python (.py) file and run the file. Most of the code is self explanatory if not comments will help you in understating the code.

from pywinauto import application

#start notepad application
window = application.Application().start_("notepad")

#Display/type hello world using TypeKeys method, Edit is the text editor control of notepad application
window.Notepad.Edit.TypeKeys("Hello World",with_spaces = True)

#Goto new line, its like pressing Enter key on keyboard
window.Notepad.Edit.TypeKeys("{ENTER}")

#with_spaces = fale by default
window.Notepad.Edit.TypeKeys("Text Without spaces will be like this")

window.Notepad.Edit.TypeKeys("{ENTER}")
window.Notepad.Edit.TypeKeys("Today Date is: ",with_spaces = True)
window.Notepad.Edit.TypeKeys("{F5}{ENTER}") #Pressing F5 in notepad will print date
window.Notepad.Edit.TypeKeys("Now let us save the file",with_spaces = True)

#we can access the menu bar using MenuSelect method
window.Notepad.MenuSelect("File->Save")

#top_window_() will return the currently focused child window
saveDlg = window.top_window_() 

#below 3 lines will save the file
saveDlg.TypeKeys("notepadAtumation")
saveDlg.print_control_identifiers()
saveDlg.Save.Click()

#close the notepad application
window.Notepad.MenuSelect("File->Exit")

In the next post I'll discuss automation controls and SendKeys mini syntax ( the strings we used in TypeKeys methods)

Friday 28 March 2014

Python - List Comprehensions

"List Comprehensions" in python is a concept used to construct lists in a easy way, like a mathematician does on a piece of paper. These listcomp (I'll be using listcomp  instead of List Comprehensions in the remaining of my description) are useful when there is a requirement of doing certain kind of operations on all the list elements. Say that you are having list 'a' as shown below:

  >>>a = [1,2,3,4,5,6]
  >>>
You want to create another list b which contains squares of elements in list a. We can do that using the below python script:
  a = [1,2,3,4,5,6]
  for x in a:
     b.append(x*x)  
  print(b)



Using listcomp we can do that easily.
  a = [1,2,3,4,5,6]
  b = [ x*x for x in a]
  print(b)
Listcomp conssts of an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression  in the context of the for and if clauses which follow it.

Let us take another example. Say we have 2 lists 'a' and 'b' and wish to build another list 'c' containing tuples of 'a' and 'b' i.e., 

  >>>a = [1,2,3]
  >>>b = [10,20,30]
  >>>c = [(1,10),(1,20),(1,30),(2,20),(2,30),(2,30),(3,10),(3,20),(3,30)]
This can be done using listcomps as below:
  a = [1,2,3]
  b = [10,20,30]
  c = [(x,y) for x in a for y in b if x!= y ]
  print(c)
Let us look into some complex listcomps. Observe the below piece of code
  nonprimes = [j for i in range(2, 8) for j in range(i*2,50,i)]
  #above listcomp creates a list with all nonprime numbers between 2 and 50

  primes = [x for x in range(2, 50) if x not in noprimes]
  #above listcomp creates a list all prime numbers between 2 and 50 by checking
  # into nonprimes list

  print(primes)
The comments in the above code snippet explains the liscomp generated. We can even make the above code simple by using nested listcomps:
  primes = [x for x in range(2, 50) if x not in [j for i in range(2, 8) for j in range(i*2, 50, i)]]

  print(primes)
As shown above we can build very complex lists using listcomps. In coming post I'll discuss how to convert the listcomp to iterator loops. Happy coding....:)

Note: I've used 3.3 python interpreter to run the code 

Saturday 22 March 2014

Configuring python for GIS operations

There are good open source APIs and libraries to work on Geospatial Information System(GIS) using Python. In this post I'll go through the installation of Geospatial Data Abstraction Library and its python bindings. I'll also go through PROJ.4 and pyProj APi which is used to convert geodetic data from one datum/projection to other. I'll be configuring this environment on windows-64 bit PC. Here you go


  • Install 2.7 (don't install 3.x) python from https://www.python.org/downloads/
  • Now go to this link
  • Download and install appropriate "Generic installer for the GDAL core components" MSI for the version of windows you are using.
  • Also download and install "Installer for the GDAL python bindingsMSI for the version of python you have installed.
  • Add a GDAL_DATA environmental variable with value “C:\Program Files\GDAL\gdal-data”
  • Also add the “C:\Program Files\GDAL” to PATH environmental variable.
  • Now go to your python prompt and test whether the installed GDAL modules are working by using the following command
  >>>import osgeo
  >>>
  • If the command prompt appears without any message then you are successful in setting the GDAL API for python
  • Now we need to install PROJ.4
  • Download PROJ.4
  • Extract and copy "proj" folder to C:\
  • Add "C:\proj\bin" PATH environmental variable.
  • Also add PROJ_LIB enviromental variable with value "C:\proj\nad".
  • Add PROJ_DIR environmental variable with value "C:\proj"
  • Now download and install appropriate pyproj exe from here (if you get any error related to registry during installation, just export python registry keys from LOCAL_MACHINE and change the key names from LOCAL_MACHINE to CURRENT_USER and try the installation again)
  • After the installation check the PyProj module is installed correctly or not by using the following command.
  >>>import pyproj
  >>>
  • If the command prompt appears without any message then you are successful in installing PYPROJ API.
Now everything is set work on Geospatial data. Happy coding... :)