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... :)

Friday, 29 November 2013

Creating your own Forum using PunBB

In this article we create a forum on local machine using xampp.

Prerequisites:


  • xampp is installed on your machine. 


Installing the forum:

There are many ready made, easy to install forums are available on the internet. I have chosen "punbb" (which is a php powered discussion board or forum). You can find more info on punbb.informer.com official site.
  • Go to download page.
  • Download recent version zip file.
  • Extract the zip file.
  • Go to "C:\xampp\htdocs" (default xammp installation folder).
  • Create a new folder as "forum".
  • Copy all the contents of the extracted punbb folder to this newly created "C:\xampp\htdocs\forum" folder.
  • Start Apache and MySQL server from xampp control panel and check whether they are running or not by typing http://localhost in your favorite browser.
  • Click on phpmyAdmin 
  • Click on Databases tab
  • Create a new database with the name "myforum".
  • You will see a configuration screen as shown below.
  • Fill the details as shown below.
  • After filling the details click on "Start Install"
  • You will be redirected to "successful installation page". Now click on "go to the forum index" link
  • We will redirected to the forum home page

That's it we are done with the installation of a forum.

Happy Coding..:)






Sunday, 24 November 2013

OpenGL for Dummies #1:Introduction

What is OpenGL?

OpenGL is an Application Programming Interface (API) for  rendering 2D and 3D computer graphics using simple primitives like points, lines, polygons, triangles, images and bitmaps.

Setting up OpenGL Environment on Windows 7

OpenGL more or less comes with the system. However, you will need to ensure that you have downloaded and installed a recent driver for your graphics hardware. You can find the updated driver and more details here.
Now we know that our operating system with updated graphics hardware drivers has the openGL API that we what to use. But we are not sure of the openGL version installed on the system. Next section will help you in finding out the OpenGL version currently present on your system.

Finding OpenGL version installed on your System?

To know about the OpenGL version installex on the system, we need GLEW (OpenGL Extension Wrangler Library). GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform. Follow the below steps to get started with GLEW:



  • Download the zip file and extract it.
  • Copy the glew-x.xx.xx folder to "C:\GL" (if not present create GL folder in C:\ drive).
  • Go to "C:\GL\glew-x.xx.x\bin\Release\Win32" path
  • Run "glewinfo.exe"
  • New "glewinfo.txt" file will be created which contains all the information about  supported OpenGL features, commands and different versions installed on your machine.

Now we know the version of openGl. But this is not enough to render the graphics on to the screen. We need utility toolkit to handle the window operations, keyboard operations. Next section will describe installation of Graphic Library Utility Toolkit (GLUT) on your system.

Installing GLUT (freeglut)



  • Download recent version of freeglut.
  • Extract the zip file and copy "freeglut-x.x.x" folder to "C:\GL".
  • Open "C:\GL\freeglut-x.x.x\VisualStudio\2010\freeglut.sln"\ in Visual Studio 2010
  • Rebuild the project.

  • The built libraries and dlls can be found at "C:\GL\freeglut-2.8.1\lib\x86\Debug" (we require these binaries for creating our own OpenGL projects) 

We are all set to run OpenGL programs. We will write our first graphics program in the next tutorial.

Happy Coding.. :)