Thursday, February 23, 2012

Wolfram Alpha

View this post here.

PS: I am taking my blog to wordpress. Sorry for inconvenience.

Pinterest

View this post here.

PS: I am taking my blog to wordpress. Sorry for inconvenience.

Saturday, February 11, 2012

Socket Programming: Python

We will be understanding the basics of socket programming using Python.



You may be thinking why socket programming? Well, its because if you need to send data over a network you need to know about socket. The HTTP websites runs on port 80. Each website has a IP adress. So when you request for a website actually your browser is trying to get data from someipaddress:80. 80 is default port that browser uses, otherwise specified.
Some of you might have used Apache. What it does it creates server(we call apache server) and binds it to 127.0.0.1 and port 80. 127.0.0.1 is called  loopback address as it tells the browser to look in the consumer's computer only instead of searching web.

Lets start, first of all socket is a connection point like your phone. You connect to some socket (someone on other side of phone) and send and receive message or data (chat).

Requires: Python 2.6 +

Using:
Lets try to create a server first.

  1. #! /usr/bin/python

  2. # Import all from module socket
  3. from socket import *

  4. # Defining server address and port 
  5. host = ''  #'localhost' or '127.0.0.1' or '' are all same
  6. port = 52000 #Use port > 1024, below it all are reserved

  7. #Creating socket object
  8. sock = socket()
  9. #Binding socket to a address. bind() takes tuple of host and port.
  10. sock.bind((host, port))
  11. #Listening at the address
  12. sock.listen(5) #5 denotes the number of clients can queue

  13. #Accepting incoming connections 
  14. conn, addr = sock.accept()

  15. #Sending message to connected client 
  16. conn.send('Hi! I am server') #send only takes string
  17. #Receiving from client
  18. data = conn.recv(1024) # 1024 stands for bytes of data to be received
  19. print data
  20.  
  21. #Closing connections
  22. conn.close()
  23. sock.close()
Now a client.
  1. #! usr/bin/python
  2. from socket import *
  3.  
  4. host = 'localhost' # '127.0.0.1' can also be used
  5. port = 52000
  6.  
  7. sock = socket()
  8. #Connecting to socket
  9. sock.connect((host, port)) #connect takes tuple of host and port
  10.  
  11. data = sock.recv(1024)
  12. print data
  13. sock.send('HI! I am client.')
  14.  
  15. sock.close()
Understanding:
Everything is explained in comments.

Note:
  • The program above is waiting for type of programming. Look at the send and recv part in both programming, if server is sending something client must always know when server will send and vice versa or it should wait for that. Of course if you don't want to drop anything you are sending.
  • # is used for comments except the first line it is for telling where python program is. Use path to python.exe on Windows.
  • Always use sock.close() to close the socket otherwise socket is in use error will be thrown by python. You may also see it if the program terminates in between.
  • 5 in sock.listen is of no use right now as the server.py will terminate as soon as it is done with first client.
  • sock.recv() waits till it does not receive something.
  • print data will not work with python 3.0+. Use print(data).
There will be more on it. Till if you want to read more about it, see here.

Monday, February 6, 2012

Blogger almost killed my blog!

Blogger has changed the domains of free blog from .com to .in. This may not be a big issue as .com redirects to .in domain.

I have withdrawn the post Creating a custom HTML tags. My reason for that is as I use syntax highlighter for the writing codes and it do not support .in domain. This thing had completely eliminated the css of my blog. Though you can see it my wordpress blog. Still you might not be able to see some codes written in other blog posts.

Worst part hits will get reduce drastically because search engines search for results first according to locale then common to world(like .com, .edu) and finally other country domains. Probably they wont even show the blogs  in results even if it finds the blog as perfect match. Like we don't get results from France or Mexico.

Friday, January 20, 2012

SOPA sacked!

The sponsor of the SOPA bill, Lamar Smith, pulled the bill from consideration in the House. Its a win situation against the government intentions of taking our freedom. Congratulation to all the internet users, freedom is still yours.
Remember PIPA is still in the house. We respect the Intellectual Property right of the owner, but laws should not interfere in our freedom.

Thursday, January 19, 2012

Against SOPA and PIPA

Thank you for your support against SOPA and PIPA.

Internet is the place in the world where we are actually have freedom and are democratic. Please be there always to keep it that way. The day this freedom is gone, we will never say words like freedom and democracy again.

Share the story.

I will be posting a blog against the action being taken by Delhi High Court, India against 21 companies including Google, Facebook and Yahoo. Please support the cause.

Sunday, January 8, 2012

Tag hinting or label hinting javascript library

Tag hinting or label hinting is a very useful thing for forums and blogs, where it is used to tag post.
 
tag-hinting.js is a Javascript library for embedding tah hinting to a webpage, it is very useful on blogs and forums where you tag post. I have created it in such a way that it is easy to use and understand. All you have to do is include the tag-hinting.js file in your webpage and write the following lines:
arr = ['your','tag','values'];
and a div to show the hints.

The function which is to be called is:
hint(this, event)
Note:
Other style values can be added to above div.
id of the div has to be hintwords.
arr has to be declared and should be after file inclusion.
hint take only the specified arguments.
hint can work on keypress, keyup and keydown events.

There is a example file on this in repository on git. Pretty soon i'll be sharing a library which interacts with database and can be helpful in searches.

Friday, January 6, 2012

Javascript Library for dynamic element creation

d-element.js is a javascript library I have created. With the help of it you can easily create dynamic elements, in just one line. Try it!

Required: d-element.js

Using:

$('div').addelement('p',{'background-color':'blue'},'hi');


Explanation:
     This has identical syntax as of jQuery. 'div' here is selector which selects all the div tags.
     addelement method takes three arguments of which first is required and null can be given in other two.

Note:
    Only element(ex: div), id(ex: .id), class(ex: #class) can be used as selector for now, element under which element has to be created.
    First argument of addelement is required which is the element to be created.
    Second argument if given should be associative array, it is for the attributes of the element to be created.
    Third argument is data, if any string to be inserted. It can be a string having other html elements.
    Check console for error logs.

Wednesday, January 4, 2012

jQuery plugin - cycle: Slideshow of your own in a sec

The jQuery Cycle Plugin is a slideshow plugin that supports many different types of transition effects. It supports pause-on-hover, auto-stop, auto-fit, before/after callbacks, click triggers and much more.


Requires: Javascript, jquery


Using:
 


Explanation:
          Line 12: it means check if document is ready to apply javascript over it.
          Line 13: $('#classObject') selects the object having class name classObject. # is called selector. We apply a method cycle over this class.
          Line 14: fx(function/effect) is required parameter which can be defined in many ways here it is shuffle.
          Line 15: shuffle is optional, if its not defined there is default value top:15px and left: width of container. Here both are defined in line 16 and 17.
         Line 19: easing can only be used with easing plugin of jquery.
         Line 20: delay is also required parameter, which takes time for effect for single transition in milliseconds.

Note:
         Take care of the brackets people usually get it wrong. 
         "id" can also be used instead of class (instead of #(hash) .(dot) is used as selector). 
         jquery.js is required.
         jquery easing plugin will make it great.
         Give 3-4 div same class name, by this way there will equal number of div having slideshow of there own. Like if you need multiple slides at a time slideshow.
         There are many other cool fx. You can also create custom.
         There are also some other parameter apart from fx, delay etc. Take a look into the coding of jquery.cycle for other parameters.


There some great demos on the site of jQuery cycle. You can also find easing plugin on this site.

Tuesday, January 3, 2012

MySQL: Alternate to INNER JOIN

I came across a good way to write queries in mysql. Instead of using INNER JOIN there is a easy way to do it, sort of renaming the tables while writing query. Below is the example:

Requires: MySQL

Using:
table1(
   id int,
   name varchar(30)
   age int)
table2(
   id int,
   address varchar(300))

SELECT a.name, a.age, b.address FROM table1 a, table2 b WHERE a.id = b.id;


Explanation:
Look at the query renaming of table is done before retrieving the information we want. WHERE clause is very important in the query because there should be something which is common in both the tables in order to fetch the data.

Note: You can also write queries instead of table name in FROM clause i.e., sub-queries.
          If you are not getting results as you expect, there is something wrong with the way query has been written. Try writing it some other way. And this will happen a lot with complex queries. :P

So, this method will make your queries really powerful and easy. Time to replace INNER JOIN.