Bite of Python

ALERT  I will not be dealing with the basic stuff of data types, loops etc. You can pick any book for these or can visit codecademy.com for hands on experience if that works for you.Here we will be drifting away from the conventional “Hello world” approach. I will go through a series of 7 TASKS to show some cool stuff to do with python.

Lets install the IDE first. You can use whatever you like – wind IDE, Ninja etc but here we are using Geany. It is a text editor – light and fast and has some basic features of an IDE.

You can get it from www.geany.org


TASK 1 – Open a page in a web browser

We are going to use webbrowser module for this.

task1-1.PNG
code to do the job

Save this file as task1.py and open it as below from cmd. The path of python may be different in your computer. Use cd .. and dir commands to find the python path in case you are not sure of it, it will be somewhere in program files (x86)

task1-2
Opening task1.py file from cmd

TASK 2 – Downloading files from the web

Here we will be downloading a text file and then printing its first 1000 characters. Here I have limited my scope to text files alone.

task2-1
Code to get the text file from the above address

The path mentioned in the code  will lead to the below text page. (just fro reference)

task2-2
This is the page from the internet which we are downloading

Below is the output when we run the file.

tast2-3
Output screen showing the first 1k characters

TASK 3 – HTML parsing

Lets use this wiki page about wiki itself a target.

scraping-1-3
Page being parsed

The below code will request for the page and will create an object out of it. Now we can search the object for elements.

scraping-1
Code to get the 10th div element
scraping-1-2
10th div element

Change the code to get the 11th div element

scraping2

scraping2-1
11th div element

TASK 4 – GOOGLING

If I have to search something I will search for the word and then open the top 5 google results in separate tabs so that i can refer them easily for which one is important and servers the purpose.

The below code uses requests module to open the browser, sys module to join or fit your search item to the URL for google search and bs4 module to parse the page.

The code flow is as below.

  • Opens a browser
  • Join the search string with the URL
  • Create a beautiful soup(bs4) object for the same
  • select the results with .r a element in that (this contains the destination location)
  • Loop through the elements(first five) and open the destination URL(href) one by one each will open in a separate tab.
task4-1
Code for the same
task4-2
Running the code and passing the search string

TASK 5 – Controlling the browser

An easier way to control the browser is by using selenium module. It can be used to interact with the sites , fill forms and much more. I have searched for saavn.com which is a site to search songs. Let’s inspect the search element, You can see that the element id of the search bar is search, take a note : we will use this info in the code.

task5-1
Inspect the search element and getting the element id in the code

First 3 lines are self explanatory, to open firefox browser with the desired website. Now we are finding the element with element id search. send_keys function will type the string in that element and .submit will send the request.

task5-2
Code for searching a song

TASK 6 – 1 WAY CHAT

We will use the socket module of python to get it done. this is one of the coolest module and integrates networking concepts with coding aspect of it.

The below code will set up a TCP connection on port 50000 and the socket object i.e. sock is listening, we are ready to accept the connection now.

owc-1
CMD screen 1 – Receiver screen

Open a second CMD screen and try to connect to the above receiver which is listening and is ready to accept the connection.(Use the same PORT)

owc-2
CDM screen 2 – Sender screen

As soon as you type the above code , screen 1 will change slightly , refer below  (highlighted >>>)

owc-3
Screen 1

Now from the sending screen (screen 2), we are sending a message

owc-4
sending a message

Now on the receiver we have received the connection with 1024 buffer size.

owc-5
Setting the buffer and receiving the message

TASK 7 – Hypnotizing square

pyautogui is the module to handle mouse and keyboard through python. time module is used to either speed up or delay the execution time.

Code flow :

  • Introduce a delay of 5 seconds ; I will explain later why we need it.
  • .click function will left click the mouse but will not release it.
  • now we are dragging(relational drag) the mouse to a distance of 200 in a time frame of 2 sec.
  • reduce the distance and loop it to get a square spiral.

Now 2 things to ponder

  1. We have introduced the time lag of 5 sec so that you can open paint in your laptop and select the pencil so that u are able to see the spiral, else the mouse will keep on moving in the normal screen and you will not be able to see the spiral.
  2. The coordinates of the screen are similar to the 4th quadrant of a maths graph.
    • Top left corner(0,0)
    • Top right corner (value,0)
    • Bottom left corner (0,-value)
    • Bottom right corner (value,-value)
spiral-1
Code to draw the spiral
spiral-2
Hypnotizing spiral

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s