Category "Tutorials"

Automate the boring stuff with python

- - Python, Tutorials

Some while ago, I got myself enrolled in one of the best video lectures at Udemy. I have recently completed the lectures and would like to brief about it. The course is named Automate the boring stuff with python. Well, it is an excellent video lecture A.I Sweigart has brought up. It is good to go for people with any skill level. The video lectures gradually leaps upwards the ladder underlying the basics at the initial few videos. I would say it is a motivation to a new-comer in python.

Jumping straight onto the topics. Following is the list of topics covered in the course at Udemy which has at the time of writing this article, a 29, 500 students enrolled.

The lectures are chunked onto 16 sections.

Section 1(Installation and Introduction)

This section covers installation of python and basics including taking input from the user. More of an intro.

Section 2 (Flow Control)

The beginning of this section introduces to flowcharts, working with them, importance, etc. Basic if-else statements, looping structures- while loop and for loop. Includes topics like comparison operators, boolean operators and monkeying around them.

Section 3 (Functions)

Starts with built-in functions like print(), input(), len(). Intro to built-in modules and importing them like math which contains math related functions. Moves on to making calls to method the module has offered. Further towards the end of this section, A.I. Swiegart explains making functions and talks about local and global scoping.

Section 4 (Handling Error)

Error catching techniques in python using try/except block.

Section 5 (Writing a complete program using above learned things.)

A good point to start writing a complete program, hence the tutorial heads on to making the classic guess the number program.

Section 6 (Lists)

This section covers the lists definition, accessing items through index as well as slicing and deleting items in a list. Additionally, the lectures goes on to show the graphical representation on how the accessing of the items in the list is happening. Concatenating strings and lists are also covered. Using in operator to find the content in the list and string and passing strings on to list() method is talked about towards the end of this section. This section also covers looping over elements in a list, various built-in methods over lists and finally a comparison between list and string.

Section 7 (Dictionary)

Starts with the introduction to yet another powerful data-type in python, dictionary. Creating them, iterating over them an so on. Further, the lecture talks more about data structures and how they can model our problem set using an example program of tic-tac-toe.

Section 8 (Strings)

This section adds more knowledge about string methods and string manipulation as well as formatting strings. Great content in this section.

Section 9 (Running Programs from command line)

Sebang line is introduced in this section which I think is one of the most important thing to include in a lecture.

Section 10 (Regular Expressions)

Section 10 has 5 video lectures altogether. The lecture begins with the basics of regex advancing towards topics like greedy/nongreedy matching, findall() method, regex sub() method and verbose mode, etc. The section ends by creating a email and phone number scraper.

Section 11 (Files)

This section of the video course is designated for detailed talk on files. I think this is the most fundamental knowledge to have since it is glued in every application you build, be it a web-application or a small script. (On a long run, it helps in easily configuring paths in django and understanding exactly what is happening.) This sections covers essential things like absolute file path, relative file path, reading and writing to a plain text file, copying and moving files and folders, walking a directory tree and deleting files and folders.

Section 12 (Debugging)

Walk through debugging techniques like assert and logging, etc.

Section 13 (Web Scraping)

Intro to modules such as webbrowser, requests, BeautifulSoup, selenium. Each of the mentioned modules has dedicated video on showcasing their methods and usage. Parsing html using BeautifulSoup, controlling the browser with selenium, downloading files using requests modules and so on.

Section 14 (Working with Excel, Word and PDF files)

In this portion of the lecture, various libraries such as openpyxl, pypdf2, etc are introduced and their usage case are showcased as well. Reading and writing excel files, reading pdf files, merging them, etc are explained towards the end of this section.

Section 15 (Emails)

This section covers sending emails, checking emails, creating MIME objects and iterating over various folders in the email.

Section 16 (GUI Automation)

Introduction to pyautogui. You can read more about it’s usage on my article here. Controlling mouse, keyboard along with a delay in each click/keystroke, etc. Shows a game player designed with the use of pyautogui and assigns a task to create a bot to play 2048 game. Here is my assignment that plays a 2048 game on it’s own. https://github.com/bhishan/2048autoplay/

Concluding Words

It is an excellent video course. The name of the course however is misleading in a sense that it provides more content than it promises. Here’s is a link to the course if you’d like to enroll. https://www.udemy.com/automate

Thanks for reading guys. Share your thoughts on this post below in the comments section.

Integrating Google APIs using python – Slides API is fun

- - Python, Tutorials, Web

The Google slides API(currently in version 1) is very interesting in a sense that it provides most of the features for creating presentations. Things like setting transparency of images, creating shapes, text boxes, stretching pictures to fit the template, choosing layouts, text formatting, replacing text throughout the presentation, duplicating slide and a lot more.

Now this is not a how to article and just a regular blog, I am not going to go into details on using the APIs and explaining the codes. Comment below and let know if you’d be interested for a video tutorial on this very idea. If we have many interested for the video tutorial, I will cover the entire codewalk along with how to on enabling APIs.
In this blog, I will talk about one of the smaller projects I took on at fiverr. If you are a regular reader, you might have noticed that I had been away for quite a long time from writing blogs. In the meantime, I started selling services on fiverr.

GOOGLE APIs and Automation

Google APIs are always interesting and allows developers with it’s superior APIs to build products and services around it. Even better when you integrate multiple APIs into a single product/service. I had used Google sheets API and drive API in the past. While slides API is essentially a subset of drive API, I hadn’t yet used it. Since presentations actually reside in the drive itself, I like to call slides as being a subset of drive.

The task was to read a specific spreadsheet populated with contents and later take these data to add into slides using a template stored in the drive itself. Each of the rows in the spreadsheet corresponded to a specific entertainment keyword with columns defining statistics such as mobile impressions, video impressions, audience type, overall impressions, an image file name, etc.

The images, again were hosted in the drive and were to be used as background image for the slide corresponding to the row in the spreadsheet.


I made use of a library : python client for google apis to complete the task. Installation is as such

pip install --upgrade google-api-python-client

In order to make use of google apis, it is required to create a project on google console and activate the APIs required(in our case, Drive API, Sheets API, Slides API). Once the project is created, you can download the oauth2.0 credentials as a JSON file and take it from there.

Sneak Peek

Integrating Google APIs

I am going wrap up this blog here. If you are interested for a video tutorial comment down below. Thanks for reading. I appreciate your time. Follow me on github. If you are looking for automation scripts, you can message me at fiverr.

Implementing Stack using List in Python – Python Programming Essentials

- - Python, Tutorials, Web

Intro

Stack is a collection of objects inserted and removed in a last-in first-out fashion (LIFO). Objects can be inserted onto stack at any time but only the object inserted last can be accessed or removed which coins the object to be top of the stack.

Realization of Stack Operations using List

 

Methods Realization using List Running Time
S.push(e) L.append(e) O(1)*
S.pop() L.pop() O(1)*
S.top() L[-1] O(1)
S.isempty() len(L) == 0 O(1)
len(S) len(L) O(1)

What is O(1)* ?

The running time for push and pop operations are given O(1)* in the above table. This is known as amortization. It is a principle used in complexity analysis of data structures and algorithms. It should be used carefully and for special cases only.

Why did we use amortized analysis for push/pop?

The list(our stack’s underlying data structure) is a series of objects which eventually are realized by arrays. The objects are stored in a continuous block of memory which offers indexing property for lists. As such, a list cannot occupy the entire memory but restricts to some specific size. When there is no more space for the objects to be added to the end of the list, a new memory series is allocated with the increased size, all the objects are copied to the new allocation and new object is added next to the last object of the current series. The previously held memory is then released free. Here, on every append, resizing of list is not required but true once in a while. Hence the running time of append in list (push on stack) for most elements is O(1) but as a whole in an amortized sense, it is O(1)* which accounts for the timely resizing and copying of elements.

Similarly for pop operations, shrinking of the underlying list is done once in a while therefore accounting for an amortized complexity of O(1)*

Implementation of Stack using List

 

class ListStack:
    def __init__(self):
        self._data = []

    def __len__(self):
        return len(self._data)


    def isempty(self):
        return len(self._data) == 0

    def top(self):
        return self._data[-1]

    def push(self, e):
        self._data.append(e)

    def pop(self):
        return self._data.pop()

Conclusion

Stack is an important data structure for realizing solutions to various programming problems. As such, it is even more essential to understand the running time evaluations and working mechanism of these data structures.

Follow me on github Github

Hire me for a project Fiverr

Raising and Handling Exceptions in Python – Python Programming Essentials

- - Tutorials

Brief Introduction

Any unexpected events that occur during the execution of a program is known to be an exception. Like everything, exceptions are also objects in python that is either an instance of Exception class or an instance of underlying class derived from the base class Exception. Exceptions may occur due to logical errors in the program, running out of memory, etc..

Common Exception Types

Class Description
Exception A base class for most error types
AttributeError Raised by syntax obj.foo, if obj has no member named foo
EOFError Raised if “end of file” reached for console or file input
IOError Raised upon failure of I/O operation (e.g., opening file)
IndexError Raised if index to sequence is out of bounds
KeyError Raised if nonexistent key requested for set or dictionary
KeyboardInterrupt Raised if user types ctrl-C while program is executing
NameError Raised if nonexistent identifier used
StopIteration Raised by next(iterator) if no element
TypeError Raised when wrong type of parameter is sent to a function
ValueError Raised when parameter has invalid value (e.g., sqrt(−5))
ZeroDivisionError Raised when any division operator used with 0 as divisor
For an example, following produces a TypeError exception
abs(‘hello world’) #expects numeric parameter but string given
Example of ValueError

Although the type of the passed parameter is correct, the value is illegitimate.

int(‘hello world’)
int(‘3.14’)

Raising an Exception

An exception can be raised from anywhere within the program though the keyword raise followed by an instance of any of the exception classes.

For example, when your program is expecting a positive integer to process but the I/O stream sent a negative integer, you could raise an Exception as such:

raise ValueError(‘Expecting a positive integer, got negative’) #instance of ValueError exception class

Handling an Exception

Now that we have talked on raising an exception, we should program such that the exception is dealt as required, else the execution of the program terminates. It is advisible to catch each exception types separately although python allows a more generic exception handling for any type of exceptions that may occur.

Examples of Common Usage:

try: 
    result = x/y
except ZeroDivisionError:
    #do as per required

Other common exception handling:

try:
    fp = open(‘sample.txt’ )
except IOError as e:
    print( Unable to open the file: , e)

Conclusion

Exceptions are an important principles of programming for any languages. It should be used wisely. On a concluding note, a try-except block can have a finally block as well. An example of use of finally can be to close a connection regardless of the successful or failed transmission of messages. Additionally, a try-except combination can have a single try block with multiple except blocks catching various classes of exception.

Follow me on github https://github.com/bhishan

Hire me for a project https://fiverr.com/bhishan

Automation With Python Python Codes To Create Dropbox Apps

- - Python, Tutorials
As promised in the article earlier on Automate DropBox Signups using python, I have come up with an article along with the codes to create an app and fetch the API keys for it which then allows us to access the files in dropbox. Well, again we stick to selenium module for an ease. In the last article, I’ve explained a python script to automate the signups for dropbox. Now that we have enough cloud space in different accounts. We now need to access the files in those spaces so we can use it as a file server. DropBox provides a feature to create apps on dropbox and gives API keys to hence access the files in the account. Since we’ve got multiple dropbox accounts we would stick towards automating the procedure to get the api key for accessing the files.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get("https://dropbox.com/login")
list_of_inputs = browser.find_elements_by_xpath("//div/input[starts-with(@id,'pyxl')]")
list_of_inputs[0].send_keys("email@domain.com")
list_of_inputs[1].send_keys("password")
sign_in = browser.find_elements_by_xpath("//*[contains(text(),'Sign in')]")
sign_in[len(sign_in)-1].click()
time.sleep(10)
browser.get("https://dropbox.com/developers/apps/create")
time.sleep(3)
type_of_app = browser.find_elements_by_xpath("//*[contains(text(),'Dropbox API app')]")
type_of_app[0].click()
file_access = browser.find_elements_by_xpath("//*[contains(text(),'My app needs access to files already on Dropbox.')]")
file_access[0].click()
type_of_file_access = browser.find_elements_by_xpath("//*[contains(text(),'My app needs access to a user')]")
type_of_file_access[0].click()
app_name = browser.find_element_by_name("name")
app_name.send_keys("appnamewhichisuniquelolo")
create_app = browser.find_elements_by_xpath("//*[contains(text(),'Create app')]")
create_app[1].click()
time.sleep(7)
app_key_item = browser.find_element_by_class_name("app-key")
app_key = str(app_key_item.get_attribute('innerHTML'))
app_secret_item = browser.find_element_by_class_name("app-secret")
app_secret = app_secret_item.get_attribute('data-app-secret')
print app_key, app_secret

General Idea of Automation

The general idea for automation is to mimic the manual workflow and put it in a loop or assign a cron job(it’s kind of same thing but not really). For creating apps on dropbox, I did the same thing. The codes are self-explanatory. We’ve used selenium and time module throughout our program. We use selenium for initiating as well as interacting with the browser. You can see, we’ve used time.time(time_in_seconds) method from time module. Depending on the speed of the internet, we need to set this up. Failing to do so will lead the program to misbehave since it will start looking for some element even when the page hasn’t been completely loaded. We fuel our program with the varieties of methods selenium provides. The above codes however shows only the procedure to create an app for a single account and print the api keys. You should loop over some file containing email id’s and password and save the api keys to some file in real usage. Hint: Place a loop over the codes and once done with getting api keys, logout from the current account.

Do comment below how you felt the article was. Any queries, please mention below.

Announcement

I’ve joined twitter @bbhishan

Google Search Using Selenium And Python – Selenium Python Basics

- - Applications, Python, Tutorials

The intentions of this blog is to show through examples some of the most common methods of selenium. Selenium is a library used for automated browser testing. However, in this post we will discuss about using selenium module in python to make a google search. The post breaks down into various blocks explaining on how to open a url in the browser via selenium python, search presence of a url in a page, click links present in a page. These are the necessities to get started with selenium.

Prerequisites
  1. Python
  2. selenium module in Python
  3. Chrome driver (http://chromedriver.chromium.org/downloads)
Installation of selenium through pip in both Linux and Windows

pip install selenium

Google search using selenium python
from selenium import webdriver

search_query = input("Enter the search query")
search_query = search_query.replace(' ', '+') #structuring our search query for search url.
executable_path = "/path/to/chromedriver"
browser = webdriver.Chrome(executable_path=executable_path)


for i in range(20):
    browser.get("https://www.google.com/search?q=" + search_query + "&start=" + str(10 * i))
    matched_elements = browser.find_elements_by_xpath('//a[starts-with(@href, "https://www.thetaranights.com")]')
    if matched_elements:
        matched_elements[0].click()
        break
1. Import statements (Line 1)

It is the import statements that is required for initiating a browser later in our program and passing url parameters to the address bar in the browser. It can be thought of as a driver for the browser. We use various methods on an instance from webdriver.Chrome() instance to control interaction with the browser.

2. Get query for google search (Line 3 and 4)

Here, we are taking a query for the google search via input() in Python3(raw_input() for Python2). Here is an example url for a google search which requires the spaces between the words to be replaced by “+” , an additional parameter start=0 is seen which specifies the search result of page 1. Similarly start=10 gives the search result of page 2.
https://www.google.com/search?q=bhishan+bhandari&start=0“
Hence, after taking the input from the user, we replaces the spaces with +.

3. Instantiate a browser (Line 5)

The statement browser = webdriver.Chrome() opens up a new browser window. We can also customize the browser capabilities such as download location, etc.

4. Opening a url in the browser (Line 9)

For opening a url in the browser, all you need to do is pass the url as an argument to the browser.get method. Remember I’ve given browser.get because we instantiated the browser earlier with browser = webdriver.Chrome(executable_path=executable_path).

5. Searching for a presence of certain url/text in the search result (Line 10 to 15)

The following methods returns the browser elements which match the criteria that the href attribute of the anchor element starts with https://www.thetaranights.com

browser.find_elements_by_xpath('//a[starts-with(@href, "http://www.thetaranights.com"]')

There is also an alternative method find_element_by_xpath for getting the first element that matches the given xpath construct. Then we make a check whether or not any there was at least an element returned from the above statement, which if true we click using click() method on the first element that matched the criteria. This will open the link on the browser. Since the result we are looking for is found and clicked, we exit loop. Else continue searching for the link with the above criteria until 20 pages if not found. You can quit the browser using browser.quit() method.

We generally covered how to open a browser, search for link in the body of the page and click the link. You may also like to read my article on how to login to a website using selenium python.

Website Mobile Friendly Tester Automation Script Python Codes For Mobile Friendly Test

- - Python, Tutorials

Hey Guys, I am back again with another script that may pronounce useful to website owners, search engine optimization experts as well as normal people like me. Through the codes we write and discuss in this article, you will be able to check if a website is mobile friendly or not. Well, here I offer a bonus. Through the codes you will be able to issue a number of websites for a mobile friendly test instantly at a time. Why is it necessary? Here’s the answer. As of the latest update in google’s search algorithm, the search engine lord now considers mobile friendliness as a major ranking factor for a website.

Python script to automate mobile friendly test

Before we begin

Before we begin our coding, let me make few things clear. We will be writing 2 files although one will be a simple text file and another will be a python file. In this text file we will write the names of the domain we want to issue for a mobile friendly test, one in each line in the format domain.com i.e without www

from json import loads
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]    

with open('websitesformobilefriendlytest.txt') as f:
    for line in f:
        google_results = br.open("https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://" + str(line)).read()
        json_obj = loads(google_results)
        if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
            print "Congrats " + str(line)  + " is mobile friendly"
        else:
            print str(line) + " is not mobile friendly"

1. Line 1 and 2

These are the import statements as we will be using mechanize module to query the mobile friendly test via a browser instantiated by the module and the response is a JSON hence we import loads from json.

2. Line 3 to 5

On line 3 we use the Browser() method of mechanize to instantiate a browser. Line 4 is a statement that tells to ignore the robots.txt file. On line 5, we specify a user agent.

3. Line 7 to 14

Line 7 opens the text file where we previously stored the names of the domain. We now can reference the content of the file via variable f.

Line 8 is the start of the for loop which stores the name of the domain in the variable line on each iteration.

On line 9, we query a domain name/ website for a mobile friendly test. The specified url will return a response of the test result which we store in a variable google_results

On line 10, we read the response and load it as a json object to a variable json_obj.

Now on line 11, we have a conditional statement to check if the website passed the mobile friendly test. The test result is a boolean value which is a value for the key “pass” which is again a value for the key “USABILITY” which in turn is a value for the key “ruleGroups” in the json_obj. Below is the example of how it may look.

{“ruleGroups” : {“USABILITY” : {“pass” : Ture/False}}}

If the website passed the mobile friendly test, the value will be True else False. Based on the result, we then print whether a website is mobile compatible or not.

Mobile friendly tester which writes result to google spreadsheet

Well, here is the bonus code. Let me know if you have any questions regarding the codes in the comment section below. Also, here’s a similar program (Is it a wordpress website checker script)with explanation on the codes which can help you understand and implement these codes. Thanks for reading :)

from json import loads
import mechanize
import gdata.spreadsheet.service
import datetime
rowdict = {}
rowdict['date'] = str(datetime.date.today())
spread_sheet_id = '13mX6ALRRtGlfCzyDNCqY-G_AqYV4TpE7rq1ZNNOcD_Q'
worksheet_id = 'od6'
client = gdata.spreadsheet.service.SpreadsheetsService()
client.debug = True
client.email = 'email@domain.com'
client.password = 'password'
client.source = 'mobilefriendlytest'
client.ProgrammaticLogin()

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]

with open('websitesformobilefriendlytest.txt') as f:
for line in f:
    google_results = br.open("https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=http://" + str(line)).read()
    json_obj = loads(google_results)
    rowdict['website'] = str(line)
    if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
        #print "Congrats " + str(line) + " is mobile friendly"
        rowdict['ismobilefriendly'] = "yes"
    else:
        #print str(line) + " is not mobile friendly"
        rowdict['ismobilefriendly'] = "no"
    client.InsertRow(rowdict,spread_sheet_id, worksheet_id)

Object Oriented Programming With C Constructors Getter Setter

- - C++, Tutorials
Object Oriented Programming C++

Here through this article, we will discuss about the basics of Object Oriented Programming. Our codes will be based on C++ programming language while the concept is the same for other OOP languages too. We will write 3 files amongst which one is the header file, the second one is the implementation of the header template. Finally we will have one main program. By the end of this read, you will be able to write codes in Object Oriented Programming languages. We will cover constructor, destructor, setter and getters.

Class definition file Computer.h

#include
using namespace std;

class Computer{
    private:
        string deviceType;
        string nameofBrand;
    public:
        Computer(string brandName="lenovo",string typeofDevice="laptop");
        ~Computer();
        void setBrandName(string brandName);
        void setDeviceType(string typeofDevice);
        string getBrandName();
        string getDeviceType();
        void displayDeviceInfo();
};

The above program shows the structure of our class Computer. The file Computer.h is our class template file.

1. Line 1 and 2 are the include statements of our input/output header file i.e iostream

2. Line 4: Our class for this example is Computer in which the starting alphabet is capital which is the convention of OOP.

3. Line 5 to 7: In C++ we place the private variables after the keyword private followed by a colon. For our example we have two private variables deviceType and nameofBrand. The private variables cannot be accessed by the object.variableName while it is possible to access it via member functions i.e the methods that are public. Basically on a general sense, private variables can be accessed only within the class.

4. Line 8 to 15 are the member functions of class Computer. Here the functions/methods are placed after the keyword public: . This means the object of class Computer can access these member functions directly via object.memberFunction().

5. Line 9 and 10 are different than the other member functions. Line 9 is the definition of the constructor for our class Computer. The constructor contains the name same to the class name. This is the convention for all the OOP. A constructor has no return type as it is basically used for the initialization of the private variables. The code inside the constructor runs at the time of object creation. In our header file, we have two parameters in the constructor and each of the parameter is initialized by the default value. Line 10 is the definition of destructor. In C++ destructor has same name as the class name except it contains “~” sign before the name. Destructor are basically used to destroy other classes initialized in the current class.

6. Line 11 to 14 are the setter and getter methods for the private variables deviceType and nameofBrand. The setter methods have no return type and takes values through parameters which are to be set to the private variables. The getter methods are used to access the private variables and takes no parameter as it’s function is to return the value and not accept any parameters. Therefore getters have return type which is based on the type of private variables.

7. Line 15 is the member function like all others which has return type void and takes no parameter/argument.

The following file is Computer.cpp file which contains the implementation of the class definition Computer.h

Class implementation file Computer.cpp

#include
#include "Computer.h"
using namespace std;

Computer::Computer(string brandName,string typeofDevice){
    setBrandName(brandName);
    setDeviceType(typeofDevice);
}

Computer::~Computer(){
    cout<<"Object Destroyed!!"<<endl;
}

void Computer::setBrandName(string brandName){
    nameofBrand = brandName;
}

void Computer::setDeviceType(string typeofDevice){
    deviceType = typeofDevice;
}

string Computer::getBrandName(){
    return nameofBrand;
}

string Computer::getDeviceType(){
    return deviceType;
}

void Computer::displayDeviceInfo(){
    cout<< "It is a   " << getDeviceType() << "and belongs to  "<< getBrandName()<<endl;
}

1. Line 1 to 3 contains the include statements. We have to include the header file Computer.h in our implementation file. The standard header files are included via statement #include<header> while the header files created by the user are included via statement #include “Header.h”

2. Line 5 to 8 is the implementation of the constructor of the class Computer. It takes two arguments namely brandName and typeofDevice. Inside the function setBrandName and setDeviceType methods are called with the parameters brandName and typeofDevice respectively. Whenever an object of class Computer is created, the codes inside the constructor is run immediately.

3. Line 10 to 12 is the implementation of the Destructor of the class Computer. The destructor is basically used to terminate/kill the objects of the other classes initialized in the current class. In our example, we have done nothing but printed that the object has been destroyed.

4. Line 14 to 16 is the implementation of the method setBrandName. It is a setter method. Conventionally setter method begins with “set” followed by the variable name. Our setBrandName takes one argument and is of return type void. Inside the method, nameofBrand is set to the value passed in as an argument. nameofBrand is our private variable hence a public method is used to access and alter it’s value i.e setBrandName.

5. Line 18 to 20 is the implementation of the setDeviceType. Similar to the setBrandName method, it is also a setter method. This method is used to set the value of the private variable typeofDevice. This method also takes one argument and is of return type void.

6. Line 22 to 24 is the implementation of the method getBrandName. Unlike setBrandName, getBrandName is a getter method that is used to return the value of a private variable which in this case is nameofBrand. The return type of a getter method is same as the type of variable it returns. In our example, getBrandName is of string return type which takes no parameter/argument.

7. Line 26 to 28 is also a getter method that is used to return the value of the variable deviceType. It is of string return type because it is used to access the value of the variable deviceType which is of type string.

8. Finally we have our last method in the class computer which in this case, we are using to print out the information of the device based on the entries entered at the type of object creation. Method displayDeviceInfo is a void return type method that takes no parameter. Here we are using the standard of method of accessing the private variables i.e using getter methods. The method when invoked on an object prints the deviceType and nameofBrand.

Let us take a look at our main program where we create objects of class Computer and invoke various methods of the class. Below is the main program.

Main program testprogram.cpp

#include
#include "Computer.h"
using namespace std;

int main(){
    string deviceBrand;
    string typeofDevice;

    Computer computers[5];

    for(int i = 0; i < 5; i++){


        cout<< "Enter the brand of your computer for position "<< i+1<<endl;
        getline(cin,deviceBrand);

        cout<< "Enter the type of computer for position "<< i+1<<endl;
        getline(cin,typeofDevice);

        Computer objectHolder(deviceBrand, typeofDevice);

        computers[i] = objectHolder;
    }

    for(int i = 0; i < 5; i++){

        Computer objectHolder = computers[i];
        objectHolder.displayDeviceInfo();
        //computers[i].displayDeviceInfo();
    }
}

1. Line 1 to 3 are the statements to include the iostream and our Computer class that we coded earlier. As discussed earlier, we include the non-standard class (Computer.h in this case) in the format #include “Header.h”. One thing to note is that we include the class definition file and not the implementation file.

2. Line 6 to 7, we declare two variables of type string.

3. Line 9 begins the OOP portion. Here we are declaring an array of type Computer of size 5. This means each index of the array computers can hold an object of Computer class.

4. Line 11 to 23 is a for loop where we iterate for the number of times equal to the size of our array I.e five. We then take input from the user for the variables deviceBrand and typeofDevice declared earlier. Next, we create an object named objectHolder of class Computer. You will notice we have passed in two arguments at the time of creation of the object. Now this invokes the constructor of Computer class. Everything that’s inside of the constructor gets run at this instance. Finally, we are assigning the objectHolder to the array’s current index. Summing up we will have five objects assigned to the array at the end of our loop.

5. Line 25 to 30 is another loop. Here we invoke the displayDeviceInfo method of the class Computer on each object stored in the array computers. On invoking the method, we get the information of the device we’ve entered at the time of creation of the object.

Following is the output of our program. You will see Object destroyed being printed several time. This is because we have a destructor method in our computer class.

Grab Whois Information And Write To Google Spreadsheet

Hello Guys, Here I am with yet another program that can benefit you and many search engine optimizers. By the end of this read you will be able to write a program to extract the whois information of a number of domains stored in a text file and write the information about the domain in a google spreadsheet which has now been a medium to share data and findings online. As a Search Engine Optimizer, you need to keep track a number of websites including your competitions. Here I offer you a simple python program to keep track of. On the other hand if you are not a SEO expert like myself, you can still use this script to track various websites you are used to.

Prerequisites before beginning to code

We are going to have two files one of which is a .py file where we code our program. The other is a text file with .txt extention where we store the domain names we want to find whois information for. The text file must contain a domian name in a format www.domain.com one per each line.

Next, we need to create a google spreadsheet where we intend to write the whois information so we can share with others. Direct your browser to https://docs.google.com/spreadsheets/ and create a new spreadsheet named “Whois Info”. Once done, create three rows namely “website”, “whoisinformation” and “date”. The name of the domain name will be under the row website, the whois information will be under the row whoisinformation and the date we queried the whois information will remain under the row date.

Python code to extract whois information and write to google spreadsheet

from bs4 import BeautifulSoup
from urllib2 import urlopen
import gdata.spreadsheet.service
import datetime
rowdict = {}
rowdict['date'] = str(datetime.date.today())
spread_sheet_id = '1zE8Qe8wmC271hG2uW4XE68btUks79xX0OG-O4KDl_Mo'
worksheet_id = 'od6'
client = gdata.spreadsheet.service.SpreadsheetsService()
client.debug = True
client.email = "email@domain.com"
client.password = 'password'
client.source = 'whoisinfo'
client.ProgrammaticLogin()
with open('websitesforwhois.txt') as f:
    for line in f:
        soup = BeautifulSoup(urlopen("http://www.checkdomain.com/cgi-bin/checkdomain.pl?domain=" + str(line)).read())
        for pre in soup.find_all("pre"):
            whois_info = str(pre.string)
        #print whois_info
        rowdict['website'] = str(line)
        rowdict['whoisinformation'] = whois_info
        client.InsertRow(rowdict,spread_sheet_id, worksheet_id)

1. Line 1 to 4

These are the import statements. We use BeautifulSoup to make a soup object out of a url response. Urlopen to get the response of a url. Gdata to access the google spreadsheet. Datetime to get the current system time.

2. Line 5 and 6

In our program, we require to access the google spreadsheet and write to it hence we are using gdata module. Now in order to write to spreadsheet, we need to pass the data as a dictionary or generally known as json which has data as a key:value pair. Rowdict is a variable storing the data to pass to google spreadsheet. On line 6, we store the current date to the key “date” which if you remember is a row at our spreadsheet.

3. Line 7 to 14

Line 7 to 14 is a procedure to connect/access a specific google spreadsheet. We require spread_sheet_id and worksheet_id. Take a look to the url of your spreadsheet. The url looks something like this one

https://docs.google.com/spreadsheets/d/1VbNph0TfFetKLU8hphrEyuNXlJ-7m628p8Sbu82o8lU/edit#gid=0

The spreadsheet id(mentioned earlier) is present in the url. “1VbNph0TfFetKLU8hphrEyuNXlJ-7m628p8Sbu82o8lU” in the above url is the spreadsheet id we need. By default the worksheet id is ‘od6‘.

On line 13 is the client.source assigned to string ‘whoisinfo’. This is the file name or the spreadsheet name. Remember we named our spreadsheet “Whois Info”. The client.source is the spreadsheet name which is written in small alphabets excluding white spaces.

4. Line 15 to 16

Line 15 opens the text file where we’ve stored the names of the domain. Line 16 helps iterate through each lines in the file. At each iteration, the domain name at each line is stored to variable line.

5 Line 17

On line 17, we query the page giving the whois information for us and make a soup object out of it by invoking the BeautifulSoup method over the url response. The reason we are making a soup object is that we can access required data via tags and the data we need is inside a <pre></pre> tag.

6 Line 18 to 19

Now we know that there is only one “pre” tag in the soup element. We therefore iterate to find a pre tag and store the information inside of the pre tag to a variable whois_info.

7 Line 21 to 23

On line 21, we are assigning the domain name to the key “website” of the dictionary rowdict. On line 22, we are assigning the whois information stored in the variable whois_info to the key “whoisinformation” of the dictionary rowdict. Note that the key of the dictionary must match to the row name in our spreadsheet. Line 23 pushes the dictionary to the google spreadsheet and writes to it. The iteration goes until the domain names at a text file is finished.

If you have any questions/confusions regarding the article or code, please mention below in comments so we can discuss. Thanks for reading

Gui Automation With Python

- - Applications, Python, Tutorials, Web
Hello Readers. It has been a bit longer delay in publishing my article. However today I will present to my awesome readers, an introduction to a GUI automation module in python (I.e pyautogui). Pyautogui is a GUI automation module for python2 and python3 which provides methods for controlling mouse and keystrokes. This decent module can be used to create bots to automate the repetitive tasks while you can enjoy your coffee. Pyautogui can do anything a human user sitting at the computer can do, except spill coffee on the keyboard” says the geek responsible for this cool module.

Follow the link below to have pyautogui installed on your machine.

https://pyautogui.readthedocs.org/en/latest/install.html

With no further iteration about the introduction, I would like to present few basics about the module.

1. Locating coordinates of the mouse cursor.

>>> import pyautogui

>>> pyautogui.position()

(850, 504)

>>>

It returns the current x and y coordinate of the mouse cursor position. In a computer screen the left top point is the origin or (0,0)

2. Moving the mouse cursor

>>> pyautogui.moveTo(10,10)

>>> pyautogui.moveTo(10,10,duration=1)

The moveTo function takes x-coordinate and y-coordinate as parameters while duration can be passed as the third parameter which is optional used to specify the amount of time in seconds to reach to the specified coordinate. The second one is humanly approach while the first is an instant movement of cursor.

3. Clicking

>>> pyautogui.click(80,80)

>>> pyautogui.doubleClick(80,80)

>>> pyautogui.rightClick(80,80)

Clicking on a certain coordinate on the screen is possible via click method while it also provides doubleClick, rightClick methods taking parameter x-coordinate and y-coordinate in all cases.

4. Keystrokes

For typing, we will first need to locate an appropriate type area. Therefore, you might want to use this method after click on some coordinate which is writable. You can use two or more statements to run simultaneously one after another by separating each statement by semicolon. For instance, I’ve specified the coordinates of the url bar on my browser and then typed my name on it via following commands/statements

>>> pyautogui.click(50,80);pyautogui.typewrite(“Bhishan”)

>>> pyautogui.click(50,80);pyautogui.typewrite(“Bhishan”, interval=0.2)

We can pass an optional parameter interval in seconds to specify the time in seconds between each letter or keystroke.

5. Hot Key

The hotkey method can be used in cases we need to press two or more keys at the same time. A handy example is Ctrl + S to save a file or Ctrl + Shift + q to quit

>>> pyautogui.hotkey(‘Ctrl’,’Shift’,’q’)

You can see all the possible mapping keys of the keystrokes via this method

>>> pyautogui.KEYBOARD_KEYS

Well that’s enough to get you started and good at GUI automation via pyautogui. Below is a bot I have made using the module to automate a boring task for myself. Iterating the story behind the need for the bot. I am a fourth semester CS undergrad student(I mean lazy student). I never take notes in any of the classes I attend. At the time of exams, I rely on the photos of my friend’s notes which they send me. As always I got the photos but this time all the pictures were at landscape mode by some chance(near about 100-110 images). It would be kind of distracting to rotate each image to read it. So I wrote some 7-8 lines of code to make a bot that would open each image file rotate it and save it while I have my dinner. I’ve used time module along with pyautogui to keep some time gap between the statements.

import pyautogui
import time
pyautogui.click(450,450);pyautogui.typewrite('graphicsnotes');pyautogui.press('enter')
time.sleep(2)
for i in range(107):
  pyautogui.press('right');pyautogui.press('enter')
  pyautogui.hotkey('ctrl','r');pyautogui.hotkey('ctrl','s')
  time.sleep(2)
  pyautogui.press('esc')
  time.sleep(2)
  time.sleep(2)


The concept is to click anywhere on the desktop screen. I choose some random coordinate (450,450). Then typing folder name to locate the folder followed by enter to open it. Then iteratively clicking right to select the image file, opening the image file by pressing enter, followed by hotkey ‘Ctrl’ + ‘r’ to rotate the image clockwise then ‘Ctrl’ + ‘s’ to save it. Finally pressing esc to close the file and repeating the process to go to the next image file. I had total of 107 images so I’ve iterated 107 times in my program to reach up to all the image files. Tell me how you felt the article was in the comments section below so I can come up with a cool set of articles for the next week. Till then, happy automation with pyautogui 🙂

Here read the docs https://pyautogui.readthedocs.org