Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, May 23, 2015

Python split with examples

"""
Rsplit. Usually rsplit() is the same as split.
The only difference occurs when the second argument is specified. 
This limits the number of times a string is separated.

So:
When we specify 3, 
we split off only three times from the right. 
This is the maximum number of splits that occur.
Source:#http://www.dotnetperls.com/split-python
"""
In [1]: s="Obama,President,USA,Ajay,cam,cyber"

In [2]: s.rsplit(",",3)
Out[2]: ['Obama,President,USA', 'Ajay', 'cam', 'cyber']

In [4]: s.split(",",3)
Out[4]: ['Obama', 'President', 'USA', 'Ajay,cam,cyber']

In [5]: s = "foo    bar   foobar foo"

In [7]: s.split(None,2)
Out[7]: ['foo', 'bar', 'foobar foo']

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Wednesday, May 6, 2015

Understanding list comprehensions with if else

"""
[dosomething if else for ]

Today list comprehensions alone killed most of time.
http://stackoverflow.com/questions/30080047/replacing-letters-in-python-with-a-specific-condition/30080898#30080898

In [35]: meds
Out[35]: ['tuberculin Cap(s)', 'tylenol Cap(s)', 'tramadol 2 Cap(s)']

In [36]: new_meds=[ i.replace(" Cap(s)", " 1 Cap(s)") if any(char.isdigit() for char in i) == False  else i for i in meds]

In [37]: new_meds
Out[37]: ['tuberculin 1 Cap(s)', 'tylenol 1 Cap(s)', 'tramadol 2 Cap(s)']
"""



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Tuesday, May 5, 2015

Understading Scope of a function in python

Fire up your terminal or IDLE and type all these.

 In python functions create it's own name space or scope.
In the example below we are using two functions to illustrate that .
Each namespace is a different world.The variables defined in one function don't know about the variables defined in another function.
Python has builtins called globals() --> to display global variables.
locals() --> to display local variables.




>>> myvar="Hi i'm global"
>>> def foo():
...     a=1
...     print(locals())
... 

>>> def bar():
...     b=2
...     print(locals())
... 
>>> def my_func():
...     print("hi")
...     name="ajay"
...     place="India"
...     age='you cannot ask'
...     print(locals())
... 
>>> my_func()
hi
{'name': 'ajay', 'age': 'you cannot ask', 'place': 'India'}
>>> foo()
{'a': 1}
>>> bar()
{'b': 2}
>>> globals() #this may slightly vary for you
{'bar': , '__builtins__': , '__name__': '__main__', 'my_func': , '__package__': None, '__doc__': None, 'foo': , 'myvar': "Hi i'm global", '__spec__': None, '__loader__': }



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Sunday, May 3, 2015

Basic usage of heapq in python

"""
Understang heapq
"""

nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]

# How to find max and min in the list??

In [2]: max(nums)
Out[2]: 42

In [3]: min(nums)
Out[3]: -4
#How to find n maximum  numbers?
#this is not a well written .I'm lazy so is there a pythonic way?
def n_max(n,nums):
 max_list=[]

 if len(nums) == 0:
  return (0)
 elif len(nums) == 1:
  return nums
 elif len(nums) >1:
  for i in range(n):

   max_list.append(max(nums))
   nums.remove(max(nums))

 return max_list,len(max_list)

print n_max(11,nums)

#Using heapq

#The heapq module has two functions— nlargest() and nsmallest() —that do exactly and more efficeint way

In [1]: nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]

In [2]: import heapq

In [3]: print (heapq.nlargest(3,nums))
[42, 37, 23]

In [4]: print (heapq.nsmallest(3,nums))
[-4, 1, 2]

In [5]: portfolio =[{'name':"Ajay",'age':24,'sex':'Male'},
   ...:              {'name':"Cam",'age':23,'sex':'Male'},
   ...:              {'name':"Cyber",'age':16,'sex':'Female'}]

In [6]: 

In [6]: youngest=heapq.nsmallest(1,portfolio,key=lambda s: s['age'])

In [7]: youngest
Out[7]: [{'age': 16, 'name': 'Cyber', 'sex': 'Female'}]

In [8]: youngest=heapq.nsmallest(2,portfolio,key=lambda s: s['age'])

In [9]: youngest
Out[9]: 
[{'age': 16, 'name': 'Cyber', 'sex': 'Female'},
 {'age': 23, 'name': 'Cam', 'sex': 'Male'}]

In [10]: eldest=heapq.nlargest(1,portfolio,key=lambda s: s['age'])

In [11]: eldest
Out[11]: [{'age': 24, 'name': 'Ajay', 'sex': 'Male'}]

"""
A heap is a tree-like data structure where the child nodes have a sort-order relationship
with the parents. Binary heaps can be represented using a list or an array organized
so that the children of element N are at positions 2*N+1 and 2*N+2 (for zero-based
indexes). This layout makes it possible to rearrange heaps in place, so it is not necessary
to reallocate as much memory when adding or removing items.
A max-heap ensures that the parent is larger than or equal to both of its children.
A min-heap requires that the parent be less than or equal to its children. Python’s heapq
module implements a min-heap.
Check http://visualgo.net/heap.html(max heap implementation)
minheap --> https://www.cs.usfca.edu/~galles/visualization/Heap.html
http://kanaka.github.io/rbt_cfs/trees.html

check heapify,heappush,heapop from the standard library

Practical use of heapq

http://stackoverflow.com/questions/8627109/what-would-you-use-the-heapq-python-module-for-in-real-life
https://docs.python.org/3/library/heapq.html

"""

#To understand heap start with an empty list

>>> import heapq
>>> l=[1,9,2,4,8,5,6] # (L is llooking like 1)
>>> l=[]
>>> heapq.heappush(l,1)
>>> heapq.heappush(l,10)
>>> heapq.heappush(l,4)
>>> heapq.heappush(l,6)
>>> heapq.heappush(l,8)
>>> l
[1, 6, 4, 10, 8]

>>> heapq.heappop(l)
1
>>> l
[4, 6, 8, 10]
>>> heapq.heappushpop(l,83)
4
>>> l
[6, 10, 8, 83]


Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Friday, May 1, 2015

Convert Strings,list of strings into a python dict or dictionary

"""
Strings ==> Dicts

"""

#problem1
#Input: s = "{'muffin' : 'lolz', 'foo' : 'kitty'}" --> a string
#output:  {'muffin' : 'lolz', 'foo' : 'kitty'} --> dictionary

>>> s
"{'muffin' : 'lolz', 'foo' : 'kitty'}"
>>> from ast import literal_eval
>>> literal_eval(s)
{'muffin': 'lolz', 'foo': 'kitty'}

>>> s
"{'muffin' : 'lolz', 'foo' : 'kitty'}"
>>> import json

>>> json_acceptable_string = s.replace("'", "\"")
>>> d = json.loads(json_acceptable_string)
>>> d
{'muffin': 'lolz', 'foo': 'kitty'}

"""
NOTE that if you have single quotes as a part of your keys or values this will fail due to improper character replacement

"""

#problem:2
#Input: mystring="a=0 b=1 c=3"
#output: {'a': 0, 'b': 1, 'c': 3}

"""
It's easy to convert list into a dict.

"""

In [1]: mystring = "a=0 b=1 c=3"

In [2]: mylist1=mystring.split() #using split for a string generates a list 

In [3]: mylist1
Out[3]: ['a=0', 'b=1', 'c=3']

In [4]: mylist2=[]

In [5]: for i in mylist1: #for every element in list1 i'm caling split at '='
   ...:     mylist2.append(i.split('='))
   ...:     

In [6]: mylist2
Out[6]: [['a', '0'], ['b', '1'], ['c', '3']]

In [7]: dict(mylist2)
Out[7]: {'a': '0', 'b': '1', 'c': '3'} #it worked but values in dictonary are strings not ints

In [8]: mylist2
Out[8]: [['a', '0'], ['b', '1'], ['c', '3']]

#convert the value item into an int  i.e '0'->0, '1'->1,'3'->3  ; mylist2 has 3 lists 
#So for every list in mylist2 i want to change the first element into a int
#mylist2[0][1] is '0'
#mylist2[1][1] is '1'

In [9]: for lists in mylist2: 
   ...:     lists[1]=int(lists[1])
   ...:     

In [10]: mylist2
Out[10]: [['a', 0], ['b', 1], ['c', 3]]


#we can use a single line answer for this

In [1]: mystring = "a=0 b=1 c=3"

In [2]: dict( (n,int(v)) for n,v in (i.split('=') for i in mystring.split() ) )
Out[2]: {'a': 0, 'b': 1, 'c': 3}


#using eval to solve 
#try to avoid using eval.
#eval() interprets a string as code.
>>> a='2*3'
>>> eval(a)
6
mystring = "a=0 b=1 c=3"


In [3]: mydict=eval('dict(%s)'%mystring.replace(' ',','))

In [4]: mydict
Out[4]: {'a': 0, 'b': 1, 'c': 3}

"""
This one took  me a while to understand.

try this 
"""
In [27]: dict(a=0,b=2,c=3)
Out[27]: {'a': 0, 'b': 2, 'c': 3}
#After trying this i came to understand

In [25]: 'dict(%s)' % mystring.replace(' ',',')
Out[25]: 'dict(a=0,b=1,c=3)'

# Invoking eval on the above line gives us desired dictionary





#problem 3
#Input:list_with_strings=["name","Ajay Kumar","age",25,"place","India"]

#Output:{'age': 25, 'name': 'Ajay Kumar', 'place': 'India'} 

In [1]: list_with_strings=["name","Ajay Kumar","age",25,"place","India"]

In [2]: dict(zip(*[iter(list_with_strings)]*2))
Out[2]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'}

"""
func(*a) is the same as func(a[0], a[1], a[2], a[3] ... a[n]) if ahad n arguments
* is an argument unpacking
More @ http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean/287582#287582
"""
In [37]: list_with_strings=["name","Ajay Kumar","age",25,"place","India"] 

In [38]: l = [iter(list_with_strings)]*2

In [39]: l
Out[39]: [, ]

In [40]: dict(zip(l[0], l[1]))
Out[40]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'}


In [41]: def foo(a,b,c,d):
   ....:     print a,b,c,d
   ....:     

In [42]: l=[0,1] 

In [43]: d={"d":3,"c":2}

In [44]: foo(*l,**d) #for arguments we use * and keyword arguments we use **
0 1 2 3

#Easy way to understand this

n [3]: my_iterable=iter(list_with_strings) #iter keyword makes it iterable 

In [4]: dict(zip(my_iterable,my_iterable))
Out[4]: {'age': 25, 'name': 'Ajay Kumar', 'place': 'India'}






Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Thursday, April 30, 2015

Convert strings to tuple in python

#Strings to tuple


>>> myname='cam'
>>> tuple(myname)
('c', 'a', 'm')
>>> (myname,) #did you see the trailing comma after myname, ??
('cam',)

#Problem 1
mylist=["('good', 'buono')", "('afternoon', 'pomeriggo')"]

#my list contains two elements and  they are strings not tuples.

>>> from ast import literal_eval

>>> [literal_eval(i) for i in mylist]

[('good', 'buono'), ('afternoon', 'pomeriggo')]


#problem2

#Input : ['aaa','bbb','ccc']
#output:[('aaa',),('bbb',),('ccc',)]

lst = ['aaa','bbb','ccc']
tpl_lst = [(i,) for i in lst]
>>> tpl_lst
[('aaa',), ('bbb',), ('ccc',)]


#problem 3

# Input : '1/2' ==> it's  a string
#output: (1,2)
>>> my_frac='1/2'
>>> my_frac.split('/')
['1', '2']

>>> my_frac = tuple(map(int, my_frac.split('/')))
>>> my_frac
(1, 2)

#problem 4

#Input: s = "a b c d"
#output1: [(a,a),(b,b),(c,c),(d,d)]
#output2:[(a, b), (b, c), (c, d)]

#Solution for output1

>>> s = "a b c d"
>>> w = s.split()
>>> w
['a', 'b', 'c', 'd']
#python 3.x
>>> list(zip(w,w))
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd')]

#python 2.7

>>> zip(w,w)
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd')]

#Solution 2:

#python 3.x
>>> list(zip(w,w[1:]))
[('a', 'b'), ('b', 'c'), ('c', 'd')]

#python 2.7

>>> zip(w,w[1:])
[('a', 'b'), ('b', 'c'), ('c', 'd')] 
"Check one more example from SO" Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Convert string or strings into a list in python

I always wanted a python cheatsheet where by looking could get an idea.Since i'm a novice programmer,i need to look at these snippets so that it will get into longterm memory
>> myname="cam"
>>> list(myname)
['c', 'a', 'm']
>>> myname.split()
['cam']

>>> "cam".split('c') # split at c and eliminate c
['', 'am']
>>> "cam".split('cam')
['', '']

>>> "cacacacacacccaaaa".split('c')  #split or break at c and remove c 
['', 'a', 'a', 'a', 'a', 'a', '', '', 'aaaa']
#observe the fist character it's empty string
# ""+"cacacacacacccaaa" will give us same string "cacacacacacccaaa"


>>> "cacacacacacccaaaa".split('a')
['c', 'c', 'c', 'c', 'c', 'ccc', '', '', '', '']


"""
Problem: I've a text which have a | seperators.
I need to make a list of numbers 

text = '2,4,6,8|10,12,14,16|18,20,22,24' 

Output:

[[2,4,6,8],[10,12,14,16,18],[18,20,22,24]]

"""

>>> list(text)
['2', ',', '4', ',', '6', ',', '8', '|', '1', '0', ',', '1', '2', ',', '1', '4', ',', '1', '6', '|', '1', '8', ',', '2', '0', ',', '2', '2', ',', '2', '4']

>>> text.split()
['2,4,6,8|10,12,14,16|18,20,22,24']

# but it's just one single string and i still see the seperators '|'

 >>> text.split('|')
['2,4,6,8', '10,12,14,16', '18,20,22,24']

"""wow we got rid of '|'
well this is okay but  it is a list of 3 strings 
'2,4,6,8'==>together one string"""


>>> len(text.split('|'))
3



 #we can use a for loop iterate over the list and add each element to a new list

>>> mynewlist=[]
>>> for i in text.split('|'):
...     mynewlist.append(i.split(','))
... 
>>> mynewlist
[['2', '4', '6', '8'], ['10', '12', '14', '16'], ['18', '20', '22', '24']]


 
#There's more cleaner way to do it.
#So let's use list comprehension to acheive this.


>>> text = '2,4,6,8|10,12,14,16|18,20,22,24' 
>>> my_data = [x.split(',') for x in text.split('|')]
>>> my_data
[['2', '4', '6', '8'], ['10', '12', '14', '16'], ['18', '20', '22', '24']] #but they are still strings

>>> [[int(y) for y in x.split(',')] for x in text.split('|')]
[[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]]


#or alternatively we can use map function
#http://pythonnotesbyajay.blogspot.in/2013/03/imap-and-map-in-python.html
#Simple usage of map (it takes 3 arguments 1.function,2.iterable,3.iterable )

>>> x=[1,2,3]
>>> y=[4,5,6]
>>> map(pow,x,y)

>>> output=list(map(pow,x,y))
>>> output
[1, 32, 729]


>>> text = '2,4,6,8|10,12,14,16|18,20,22,24'
>>> strs1=text.split('|')

>>> [map(int,x.split(',')) for x in strs1] 
[[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]]


#How do you change this string into list 'QH QD JC KD JS' ?

>>> 'QH QD JC KD JS'.split()
['QH', 'QD', 'JC', 'KD', 'JS']



More about Split
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Palindrome of first characters of strings in a list

I've scored 70/100 for this problem because i've missed few cases like empty list,list with one word(although this is implemented in actual algorthm).There might be some error crept
"""
Problem: Take first letters of a words in a list and check if it is palindrome or not,you can remove as many as letters and see if the word is
palindrome or not

Ex:  

inputlist=["Ajay","IS","ALIEN"]
Solution: 
First letters of words ==> AIA
Check if the word is palindrome or not ;
if word is not a palindrome drop any letter and check if it is a palindrome.
do this till you find a palindrome.

"""

from itertools import *
def PalindromeLengthPuzzle(input1): #==> ['ADAM','EVE','ABRAHAM','ELVIS']
  myList=[]
  for i in range(len(input1)):
   myList.append(input1[i][0]) #==> adding first letters to myList( myList = ['A','E','A','E']

  """
 Concept 1:

 1. How to join elements in a list to a single string? (Most frequently you will come across )
 Ex:1 ===> no space between ('') so you'll get an output :abc
 >>> myList=['a','b','c']
 >>> mystring=''.join(myList)
 >>> mystring
 'abc'
 Ex:2 ==> Space (' ') so you'll get an output : abc
 >>> mystring1=' '.join(myList)
 >>> mystring1
 'a b c'


  """
  mystring=''.join(myList) #==> mystring='AEAE'
  print mystring

  if len(input1) == 0:
   return 0,''
  elif len(input1) ==1:
   return mystring,1
  elif len(input1) == 2:
   if mystring[0:] == mystring[::-1] :
    return mystring,len(mystring)


  elif len(input1)>2:
   for i in reversed(range(len(mystring)+1)):
    #print i

    my_list=[''.join(item) for item in combinations(mystring,i)] # generate different combinations of 'AEAE' into a list
    #print my_list

    for item in my_list:
     #print my_list

     if item[0:] == item[::-1]:

      return len(item),item








input1=["Bharati","Akash","Bharat","BUMM","CAT","BAT","CUT","ATE","BY"]
input2=["Bharati"]
input3=['BOB','BAAB','BAABS']

print PalindromeLengthPuzzle(input1)
print PalindromeLengthPuzzle(input2)
print PalindromeLengthPuzzle([])
print PalindromeLengthPuzzle(input3)
"""
Output:

BABBCBCAB
(7, 'BABBBAB')
B
('B', 1)

(0, '')
BBB
(3, 'BBB')
[Finished in 0.0s]

"""





Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Wednesday, April 29, 2015

Understanding staticmethod classmethod Decorators


I've been using python from a while but and i've came across these terms but didn't find them where to use.I've been learning Java for a while,now suddenly all these python terms started to make a sense.




class Hello(object):
 """Understanding @staticmethod, @classmethod decorators"""

 @staticmethod
 def main(i):
  """ Using @staticmethod decorator.There is no self argument"""
  print(i)
  print("I'm inside main method which has @staticmethod decorator")

 @classmethod
 def func2(self,i):
  """ using @classmethod You need to provide a self argument """
  print(i)
  print("I'm inside func2 method which has @classmethod decorator")

 def func3(self,i):
  print(i)
  print("I'm inside func3 method which has no decorator  so to acess func3 I need to create an instance of Hello class to access the methods of Hello class")
  
Hello.main(1) #didn't create any instanc of Hello class,yet able to access the methods 
Hello.func2(1)
#Hello.func3(1) ==> This will be an error. General way to acess methods is to create an instance of object 
hello_object = Hello() # Created an instance of Hello class
hello_object.func3(100)
hello_object.func2(100)
hello_object.main(100)


Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Tuesday, July 16, 2013

PyManga--A python based gui to download manga

I often used to see people sitting infront of the big screens and clicking the mouse to check their favourate manga.I got plenty of time so decided to make the script.
This is the dumbest program ...with lots of bugs in it.What do you expect in one day ??
How to run this program
1.Download this program from sourceforge
2.Extract ==> one shortcut and one zip file
extract the zip file and run the file manga.exe
3.you'll see a popup and follow the instructions









usage:
Ex:1   naruto 100   
Ex2: bleach 245
  **** Spell properly else program will not run  
Ex:<anime name> number



Enter and click ok...wait for some time to download(as soon as black window aka command prompt disappears download is complete) check the manga downloader folder you'll find a new directory/folder check the images for more info..
or you can got to mangareader.net and copy the link at the top and paste it.Make sure you're on the first page(first image should be there on that page)3,4 images.
If you have python then install easygui and beautifulsoup4 modules using pip.Then copy this script and run...



import os
import urllib2
import urllib
from urlparse import urlparse
from bs4 import BeautifulSoup
import easygui as eg


eg.msgbox("Search/Enter proper url"+'\n'+"Ex:naruto 100 or bleach 400"+'\n'+" www.mangareader.net/naruto/100",title="Manga Downloader", ok_button="ok")
q=eg.enterbox(msg='Search or Enter the Link.',title='Manga Downloader')
print q

def user_input(q):
    if 'www' in q:
        site1(q)
    else:
        search(q)

def search(query):
    """Name episod
    Ex: 1.Bleach 544
         2.Naruto 100"""

    s=query.lower().strip(' ').split(' ')
        
    link='http://www.mangareader.net/'+s[0]+'/'+s[1]
    if len(s) > 2:
        link='http://www.mangareader.net/'+'-'.join(s[0:-1])+'/'+s[-1]

    site1(link)


def site1(link):
    link=link.strip('http://')
    link=link.strip('.html')
    if (link.count('/')>2):
        print 'link.count:',link.count('/')

        two=link.find('/',20)
        three=link.find('/',two+1)
        #link=http://www.mangareader.net/440-45521-1/watashi-ni-xx-shinasai/chapter-8.html
        link='http://www.mangareader.net/'+link[two+1:three]+'/'+link[three+9:]
        print link

    if 'http://' not in link:
        
        link1="http://"+link
    else:
        link1=link
    
    try:
        html=urllib2.urlopen(link1).read()

    except urllib2.HTTPError:
        print('Enter proper url')
    
    soup = BeautifulSoup(html)
    link_image=soup.img['src']
    link_next=soup.img.parent['href']
    """ Creates folder at specified location"""
    link_properties = urlparse(link)
    start=link_properties.path.find('/')
    end=link_properties.path.find('/',start+1)
    folder_name=link_properties.path[start+1:end]+'_'+link_properties.path[end+1:]
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
    """Completed creating Directory"""
        
    
    
    if 'www.' in link_next:
        pass
    link_next='http://www.mangareader.net'+link_next
    i=0
    while ('/'+link_properties.path[end+1:]+'/')in link_image:
        
            f = open(folder_name+'/'+str(i+1)+'.jpg','wb')
            f.write(urllib.urlopen(link_image).read())
            f.close()
            html=urllib2.urlopen(link_next).read()
            soup = BeautifulSoup(html)
            link_image=soup.img['src']
            link_next=soup.img.parent['href']
            link_next='http://www.mangareader.net'+link_next
            print link_next,link_image
            i=i+1

#search("one piece 100")
user_input(q)



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Saturday, July 6, 2013

How to send messages to billions of users on facebook with one simple script

 Although title looks good,but due to facebook's spam preventing algorithm this message will be sent to other folder[rarely people will see them],but if the user is a friend of your's then you can send message directly



#usr/bin/env/python
"""
This script can get the user data from facebook graph api.
This is written for better understanding of python 
Modules required:BeautifulSoup
Author:Ajay Kumar Medepalli
Blog:http://pythonnotesbyajay.blogspot.in/
"""
import smtplib
import email
from email.MIMEMultipart import MIMEMultipart
from email.parser import Parser
from email.MIMEText import MIMEText
import urllib2
#from BeautifulSoup import BeautifulSoup
"""There are two versions of BeautifulSoup.This script is updated with the latest one"""
from bs4 import Beautifulsoup
import time
import random


"""
Algorithm Explaination:
Aim: To send messages to fb users 
To send messages we need to have their usernames. (FB has introduced this messaging system like if i've my username i can receive message from 
    email clients and fb email id would be username@facebook.com)

FB Graphi API provides us to acess usernames if users have created one.
get_fb_username ==> This function will get you all usernames 
send_mail ==> This functions will send messages to users (Note :  The message will be sent to other folder if you are not a friend )

"""
 
user_name_array=[]  #List or array to store usernames of facebook
def get_fb_username(id):
    try:
        """
        In fb graph api every url doesn't point to a user or json object from which username can be extracted
        There are some urls which we don't need,since we are using range function there will be errors.To avoid errors we are using
        try 
        """
        url=urllib2.urlopen('https://graph.facebook.com/'+str(id)).read()
        """
        id ==> it's the number.So i'm converting it into string.
        Our urls look like
        https://graph.facebook.com/1
        open your browser or install RESTFUL api addon and check the url
        https://graph.facebook.com/4 
        The above url leads you to the Mark Zuckerberg's page which has his details in json 

        {
   "id": "101",
   "first_name": "xx",
   "gender": "male",
   "last_name": "yyy",
   "link": "https://www.facebook.com/zz",
   "locale": "en_US",
   "name": "Xxx YYy",
   "username": "cam"
}
    
        """

        soup = BeautifulSoup(url)
        all_attr=soup.prettify()
        print all_attr
        gend=all_attr.find("gender")
        if(all_attr[gend+9] == 'm'): # just a check to see if user is a male 
            gender='male'
        elif (all_attr[gend+9] == 'f'): #checking if the user is a female
            gender = 'female'
        else:
            gender="The user didn't specify any gender"
        if all_attr.find('username') != -1: #if there is a username then proceed
            start_quote=all_attr.find('username')+10 #find the first occurence of username
            end_quote=all_attr.find('"',start_quote+1) 
#find the '"' after the username
 
            user_name=all_attr[start_quote:end_quote+1].strip('"')+'@facebook.com' 
#generated username and adding @facebook.com to the username
 
            user_name_array.append(str(user_name)) # adding username to list or array
            print "username ==>"+'\t'+user_name +'\t'+ "gender ==>"+"\t"+gender
            print "\n"
         
    except urllib2.HTTPError:
        pass
 
 
     
 """
The range function in python is so useful . We can generate 1000s of username .I've called the function inside range function.
 """
for i in range(124896015,124896016,1):
#for i in range(startvalue,stopvalue,stepvalue):
    get_fb_username(i+1)
print user_name_array
 
def send_mail():
    random_text=["hi","hello","Nice to meet you","How are you","wassup","hi!!!",'just wanted to say hi']
    server = smtplib.SMTP()
    server.connect('smtp.gmail.com', 587) # for eg. host = 'smtp.gmail.com', port = 587
    server.ehlo()
    server.starttls()
    server.login('username@gmail.com', 'password')
    #replace this with u r gmail id
    #password ==> ur gmail password
    fromaddr ='username@gmail.com'
 
    for i in range(len(user_name_array)-1):
 
        msg = email.MIMEMultipart.MIMEMultipart()
        msg['From'] = fromaddr
        msg['To'] = user_name_array[i]
        msg['Subject'] = 'hi'
         
        msg.attach(MIMEText(random_text[random.randint(0,len(random_text)-1)]))
        #msg.attach(MIMEText('put some custom message.', 'plain')) 
        server.sendmail(fromaddr,user_name_array[i],msg.as_string())
    server.quit()  
send_mail()

But you need to make few changes
1.change the numbers in the range function,if you've a fast computer then you can start all the way from 4 to 5000000 (Mark zuckerberg has id =4)
2.change the user@gmail.com,password with your details
3.Edit the message you want to send aswell
How to use this??
pip install BeautifulSoup ==>Download this module
a.copy and paste this script somewhere on u rcomputer
b.cd somewhere
c.python scriptname.py



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Tuesday, March 19, 2013

Sets in python


A set is an unordered collection of zero or more immutable Python data objects. Sets do not allow duplicates and are written as comma-delimited values enclosed in curly braces. The empty set is represented by set(). Sets are heterogeneous, and the collection can be assigned to a variable as below.
1
2
3
4
5
6
>>> {3,6,"cat",4.5,False}
{False, 4.5, 3, 6, 'cat'}
>>> mySet = {3,6,"cat",4.5,False}
>>> mySet
{False, 4.5, 3, 6, 'cat'}
>>>
Even though sets are not considered to be sequential, they do support a few of the familiar operations presented earlier. Table  reviews these operations and the following session gives examples of their use.
Operation Name Operator Explanation
membership in Set membership
length len Returns the cardinality of the set
| aset | otherset Returns a new set with all elements from both sets
& aset & otherset Returns a new set with only those elements common to both sets
- aset - otherset Returns a new set with all items from the first set not in second
<= aset <= otherset Asks whether all elements of the first set are in the second
Operations on a Set in Python
1
2
3
4
5
6
7
8
9
>>> mySet
{False, 4.5, 3, 6, 'cat'}
>>> len(mySet)
5
>>> False in mySet
True
>>> "dog" in mySet
False
>>>
Sets support a number of methods that should be familiar to those who have worked with them in a mathematics setting. Table  provides a summary. Examples of their use follow. Note that union, intersection, issubset, and difference all have operators that can be used as well.
Method Name Use Explanation
union aset.union(otherset) Returns a new set with all elements from both sets
intersection aset.intersection(otherset) Returns a new set with only those elements common to both sets
difference aset.difference(otherset) Returns a new set with all items from first set not in second
issubset aset.issubset(otherset) Asks whether all elements of one set are in the other
add aset.add(item) Adds item to the set
remove aset.remove(item) Removes item from the set
pop aset.pop() Removes an arbitrary element from the set
clear aset.clear() Removes all elements from the set


aset.clear() Removes all elements from the set
Methods Provided by Sets in Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> mySet
{False, 4.5, 3, 6, 'cat'}
>>> yourSet = {99,3,100}
>>> mySet.union(yourSet)
{False, 4.5, 3, 100, 6, 'cat', 99}
>>> mySet | yourSet
{False, 4.5, 3, 100, 6, 'cat', 99}
>>> mySet.intersection(yourSet)
{3}
>>> mySet & yourSet
{3}
>>> mySet.difference(yourSet)
{False, 4.5, 6, 'cat'}
>>> mySet - yourSet
{False, 4.5, 6, 'cat'}
>>> {3,100}.issubset(yourSet)
True
>>> {3,100}<=yourSet
True
>>> mySet.add("house")
>>> mySet
{False, 4.5, 3, 6, 'house', 'cat'}
>>> mySet.remove(4.5)
>>> mySet
{False, 3, 6, 'house', 'cat'}
>>> mySet.pop()
False
>>> mySet
{3, 6, 'house', 'cat'}
>>> mySet.clear()
>>> mySet
set()
>>>
 From:http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Monday, March 18, 2013

Strings in Python

Before you read my stuff have a look at official documentation.Get some vague idea and come back here.
Check the pleac project...I love that site,last but not least tutorials point,even then you don't understand you're in right place. :)).

1.Introduction to strings.



#!usr/bin/env/python
###############################################################################
mystr = "\n"        #a new line character.

mystr_raw=r"\n"     #two characters,\ and n.
print mystr_raw 
print "Length of mystr_raw:",len(mystr_raw),'\n'

###############################################################################
mystr_dq="Matthew 'Mark' Luke" #Single quote inside double quote.
print mystr_dq+"\n"            #'+' is used for adding(concatenating the strings.)

mystr_sq='Matthew "Mark" Luke' #dq(double quote) inside sq(single quote).
print mystr_sq+"\n"           

################################################################################
mystr_1_multiplelines="""These examples are
taken from pleac project.Really inspiring"""    #used triple double quotes.
print mystr_1_multiplelines+'\n'

mystr_2_multiplelines='''These examples are     
taken from pleac project.Really inspiring'''    #used triple single quotes.
print mystr_2_multiplelines+'\n'
################################################################################


Output:

\n
Length of mystr_raw: 2
Matthew 'Mark' Luke
Matthew "Mark" Luke
These examples are
taken from pleac project.Really inspiring
These examples are
taken from pleac project.Really inspiring



2.String Slicing.


#!usr/bin/env/python
"""Python strings are immutable once created they cann't be modified"""
mystr="Python is awesome!!!!"#P(0 or -21) y(1 or-20) t(2 or 19)....awesome(10,11,12,13,14,15,16 or -11,-10...-5 )
#     +012345678901234567890  Indexing forwards  (left to right)
#      109876543210987654321- Indexing backwards (right to left)
print'Printing..the full string ===>',mystr +'\n'

print mystr[0] +'\n'  #using forward indexing to print 'P'.
#Output:P
print mystr[-21]+'\n' #using backward indexing to print 'P'
#Output:P
print mystr[-1],mystr[len(mystr)-1]+'\n' #length=21,Since indexing is started at 0 we'll have one less 
#len(mystr)=lenght of the string.
""" 
mystr[a:b]
Case 1:Forward Indexing 
0<=a<b<=(len(mystr)-1)
mystr[a:b]==>Start from a till b-1
mystr[:b]==>assume a=0,start from a till b-1
mystr[a:]==>start from a till end.
Case2:Backward Indexing===>here we are using negative numbers,So do look
at couple of examples.
-len(mystr)<=a<b<=-1
"""
print mystr[0:5]+'\n' #start from 0 to 5-1==>(Pytho)
print mystr[1:]+'\n'  #start from one till end==>(ython is awesome!!!!)
print mystr[:18]+'\n' #start from 0 to 18-1==>(Python is awesome!)
print mystr[-20:-5]+'\n'#start from -20 till -5==>(ython is awesom)
#mystr[-5:-20] will return an empty string.
print mystr[-5:]+'\n' #start from -5(e) till end(!)===>(e!!!!)
print mystr[:-18]     #start from -21 till (-18-1=-19)===>(Pyt)

mystr = mystr[:9] + " easier than C" + mystr[17:]
print mystr
 

Output: 

Printing..the full string ===> Python is awesome!!!!
P
P
! !
Pytho
ython is awesome!!!!
Python is awesome!
ython is awesom
e!!!!
Pyt
Python is easier than C!!!!

3.String Methods.

 
#!usr/bin/env/python
"""String Methods """

mystr="Try 'import this' in your python interpreter"
print mystr.count('in')
#Counts number of occurences of 'in'.
#Output:2 
print mystr.find('i') # index of first occurence of i==&gt;5('i' in import)
print mystr.find('in')#index of first occurence of in==&gt;18(returns position of i in in i.e 18)
print mystr.find('in',19)#start from 19 and find first occurence of in
#There's an 'in' in interpreter.
print "Ajay".find('a')
#Output is 2

print "AJAY".lower()
#Return a copy of AJAY, but with upper case letters converted to lower case.
print "ajay".upper()

print mystr.replace("import this","import string")
#Dude strings do not change in python....but what happened right now?? It just return a copy of mystr.

print mystr
#mystr is same unmodified.

import string  #All string methods can be done like this.Try lower,upper and find in the simlar way.
print string.replace(mystr,"import this","import string")

"""Below are some useful oneline methods.
Replace str with a variable mystr or strings like "Ajay is dumb"."""

# str.capitalize()
print mystr.capitalize()
"""Return a copy of the string with its first character capitalized and the rest lowercased."""

"""----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------"""

#str.split()==&gt;Creates a list.

print "Ajay is dumb,idiot,mining engineer".split()
#Split after a whitespace(space)
#Output:['Ajay', 'is', 'dumb,idiot,mining', 'engineer']

print "Ajay is dumb,idiot,mining engineer".split(',')
#splits after a coma ',' and forms a list of words.
#Output:['Ajay is dumb', 'idiot', 'mining engineer']

print "Ajay is dumb,idiot,mining engineer".split(None,1)
#None is nothing but your telling to split after whitespace.but number of splits should be one.
#In place of None you can use ',' or anything.In place of 1 you can use any number
"""----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------"""
#str.strip()
#Return a copy of the string with the leading and trailing characters removed.
print "    Ajay loves space(white space)   ".strip()
#Output:Ajay loves space(white space)
print 'www.example.com'.strip('cmowz.')
#Output:example
#??? What happened let's try some more examples.
print 'www.example.com'.strip('cmoz.')
#Output:www.example
#'cmoz.'==&gt;'.comz'===&gt;'.com' can be removed because it's in a sequence.z is ignored.
print 'www.example.com'.strip('cmz.')
#Output:www.example.co
#'.cmz'==&gt;'m'==&gt;So m is removed.
"""----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------"""
#str.join
seq = ['1','2','3','4','5']   #'1','2'....are strings.
print '+'.join(seq)
#Output:1+2+3+4+5

print '*_*'.join("Ajay's blog is the dumbest".split())
#Output:Ajay's*_*blog*_*is*_*the*_*dumbest
#Just combined the split and join.To understand this first we did a split on the string.
# 1)"Ajay's blog is the dumbest".split() 
#2)later i joined the list elements using str.join

"""----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------"""
#str.partition
print("Python programmers are awesome :) ".partition('are'))
#Output:('Python programmers ', 'are', ' awesome :) ')
#It's gives a tuple of 3 elements.
#1)Everything before the partition element==&gt;here it's "Python programmers"
#2)Partition element itself.===&gt;'are'
#3)Everything after Partition element.==&gt;'awesome'

#Check out the original documentation for more examples and methods. 

Output:
2
5
18
33
2
ajay AJAY
Try 'import string' in your python interpreter
Try 'import this' in your python interpreter
Try 'import string' in your python interpreter
Try 'import this' in your python interpreter
['Ajay', 'is', 'dumb,idiot,mining', 'engineer']
['Ajay is dumb', 'idiot', 'mining engineer']
['Ajay', 'is dumb,idiot,mining engineer']
Ajay loves space(white space)
example
www.example
www.example.co
1+2+3+4+5
Ajay's*_*blog*_*is*_*the*_*dumbest
('Python programmers ', 'are', ' awesome :) ')

Check out the questions from SO,some day you gonna be same place so practice them.One more i missed here is string formatting,translate method which are important.I'll be posting when i'm free :)).

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Saturday, March 16, 2013

imap and map in python.

  •  The imap() function returns an "iterator" that calls a function on the values in the input
    iterators and returns the results.
  • Like map() but stops when the shortest iterable is exhausted instead
of filling in None for shorter iterables.The sentence is bit odd but let's go through it.

>>>from itertools import *
>>> list(imap(pow, xrange(10), count()))
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]


  1. pow is a builtin function which caluclates power of two numbers(pow(2,5==>2**5==>32)
  2. In the above example we passed 3 arguments,1 pow function,xrange(10) ,count.
  3. imap maps the power function over those other two arguments.This is equivalent to pow(xrange(10),count())
  4. xrange(10) will exhaust after 10 values,despite count() being infinite iteration it stops because xrange(10) has exhausted.
  5. we make a list of all the powers by calling list()
  6.  
Input:
#!usr/bin/env/python
from itertools import *
#Using imap from itertools.
print 'Triples:'
for i in imap(lambda x:3*x,xrange(5)):
    print i
print "\nMultiples:"
for i in imap(lambda x,y:(x, y, x*y), xrange(5), xrange(5,10)):
    print '%d * %d = %d' % i
print "\n Zip"
print list(map(pow, xrange(10), count()))
Output:
Triples:
0
3
6
9
12

Multiples:
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36

 Zip
Traceback (most recent call last):
  File "/home/ubuntu/Desktop/map_imap.py", line 11, in 
    print list(map(pow, xrange(10), count()))
TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'
[Finished in 0.2s with exit code 1]
The error is expected because count() is infinite iteration,but xrange() is exhausted so it can't perform operations which have "None" argument.
     











Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Python's itertools.tee() explained with examples

Input:
 
#!usr/bin/env/python
from itertools import *
r=islice(count(),5)
iterator_1,iterator_2=tee(r)
print "iterator_1,iterator_2:",list(iterator_1),list(iterator_2)
#you can create n number of iterators,but default is set to tw0 iterators.
print"\nExample 2:"
r_2=islice(count(),5)
i1,i2=tee(r_2)
print 'r_2:',
for i in r_2:
 print i,
 if i>1:
  break
print
print'i1,i2:',list(i1),list(i2)
Output:
 
iterator_1,iterator_2: [0, 1, 2, 3, 4] [0, 1, 2, 3, 4]

Example 2:
r_2: 0 1 2
i1,i2: [3, 4] [3, 4]
 

 




 Itertools is a great library, but some methods definitely receive more attention than others – for instance, I’d wager that chain is a lot more well-known than tee. Python’s documentation describes tee as follows:

Return n independent iterators from a single iterable.
It also adds this caveat:
Once tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being informed.(Example-2 is Illustrated).
Caution:Don't read the rest....you may feel boring.Learn about Iterators,Generators and come back.

More on this is covered in this url: http://jezng.com/2012/06/inside-python-tee/



However, the documentation doesn’t really explain why you might want to use tee, instead of copying the iterable n times. The short answer is that tee() uses some heuristics and strategies to make the generation of these n iterators more memory efficient. This article will peek inside Python’s internals and explore these strategies in more detail.

Iterables, Iterators, and the Iterator Protocol

To understand the basis for the optimizations, it is essential to know how Python’s iterator protocol works. (More experienced Pythonistas might want to skim this section – it’s here for completeness.)
Iterables are Python objects that define an __iter__() method, which, when called, returns an iterator. Iterators define the __iter__() method as well, but implement it by simply returning the iterator itself. Iterators also implement a next() method, which returns the next element in a sequence. To signal the end of a sequence, next() raises a StopIteration exception. Any object that implements both __iter__ and next in the aforementioned manner is said to implement the iterator protocol.
To actually use these iterators and iterables, we turn to Python’s classic for item in sequence loop. Here, sequence can be either an iterator or an iterable – it doesn’t matter, because the loop will begin by implicitly invoking seq’s __iter__ method, which will return an iterator. Now, on each round through the loop, the Python interpreter will invoke the iterator’s next method and assign it to item, and will continue to do so until next raises StopIteration.
Perhaps you are wondering why Python makes the distinction between iterables and iterators, and why iterators don’t have prev or rewind methods. These two questions are in fact related. It can be expensive or complicated to implement these other methods – for instance, reading sequentially forwards from a file corresponds simply to fread() in C, but rewinding to the start is a more expensive fseek call, and there is no efficient way to read sequentially in reverse.
More importantly, prev and rewind are not essential for iteration – they can be built upon the primitive next method. For instance, if we really wish to access the earlier contents of the file in Python, we could buffer the earlier results ourselves. However, buffering all the earlier values is generally inefficient and wasteful: many programs have no need to access earlier values of an iterator, and those that do tend to limit themselves to one or two elements before the current one. Rewinding an iterable is a more common use case, but the same thing can be achieved by obtaining an entirely new iterator that points to the start of the sequence. To do that, we need a factory that produces iterators – which is exactly what an iterable is.
In sum, Python’s iterator protocol is simple to implement, and it works well for the common use case of for .. in loops.

Sometimes Buffering is Useful

The starkness of Python’s iterator design means that more complicated use cases will need to build their own abstractions on top of it. Fortunately, its simplicity also means that it is easy to build upon.
tee() is one such abstraction, built to efficiently create n independent iterators. Consider file I/O again: we might not want to create n file iterations by calling fopen() over and over, especially if n is large. Nor would we want to copy iterators that do heavy computation each time we called next(). In both cases buffering is a better solution, and this is what tee does, collating in one central list the values returned by the original iterator. Its return value consists of n tee iterator objects,1 each of which stores a single integer index that indicates the next element it should return from the list. The list itself is populated lazily – whenever any of the n iterators asks for a value at an index that is not yet in the buffer, tee() calls next on the underlying iterator, then caches and returns the new value.
With this implementation detail in mind, the reasoning behind the documentation’s caveat becomes clearer – using the iterator outside of tee() would cause some (or all) values to be lost from the cache.

Sometimes Buffering is Not Useful

Buffering is not always the most efficient way to create n independent iterators. For example, next could be a computationally cheap function, and it would be more efficient for us to just copy it n times. We can indicate this fact by defining a __copy__ method on our iterator.2 If tee() detects the presence of __copy__, it will copy the iterator instead of doing buffering. Moreover, it will only do this copying n - 1 times – the last iterator will simply be the original one that was passed in! Since the original iterator should never be used after being passed to tee(), this makes perfect sense; the end result will appear the same to the library consumer.
In fact, tee objects themselves implement __copy__! Since any tee object is backed by a central buffer, the most efficient way to duplicate one is to have the copy point to the same buffer. So not only does tee duplicate any iterator efficiently, it also ensures that future duplications are efficient.
Notably, as a consequence of this optimization, passing in a non-tee object that implements __copy__ will return a tuple of copies of that object, not tee objects. In practice, the type of the object should not matter as long as one sticks to using it solely as an iterator.

Efficient Buffering: The gritty details

If the total number of items generated by the iterator is very large, storing all of the generated values might take up a lot of memory. If we can keep track of all the indices of the tee objects generated by a tee() function call, we can delete old values once all of the indices have passed it. (Since iterators can only progress forwards, we know that these values will never be used again.) With CPython’s reference counting, this is actually quite simple: the buffer can be implemented as a singly linked list, with each tee object holding a pointer to the next element that it will return. As a natural consequence, once the last tee iterator is done with a buffer element, the element’s reference count goes to zero and it gets garbage collected.

Tee linked list buffer with 3 tee iterators using it. The grey cell has no pointers to it, so the reference counting mechanism will free it up.
However, linked lists perform worse than arrays in terms of memory fragmentation. Moreover, they incur lots of overhead in memory allocations and deallocations. As a compromise, CPython uses linked arrays – a linked list with arrays as individual elements. Moreover, these arrays are sized such that the entire array element is 64 bits in size, fitting nicely into cache lines.

A Python Implementation

Python’s documentation gives pure-Python implementation of tee(), but it simplifies things and leaves out the buffering and copy semantics. I’ve written up a slightly more involved one that reflects the optimizations described above. Unlike the version in the docs, this implementation passes the standard library’s test suite. It still makes some concessions to simplicity: for instance, buffering uses a single list, instead of the linked arrays described above.
    class TeeIterator(object):

        def __new__(cls, tee_data):
            if isinstance(tee_data, TeeData):
                self = super(TeeIterator, cls).__new__(cls)
                self.tee_data = tee_data
            else:
                self = TeeIterator.from_iterable(tee_data)
            self.index = 0
            return self

        def __copy__(self):
            return TeeIterator(self.tee_data)

        def __iter__(self):
            return self

        def next(self):
            rv = self.tee_data[self.index]
            self.index += 1
            return rv

        @classmethod
        def from_iterable(cls, iterable):
            if isinstance(iterable, cls):
                return iterable.__copy__()
            tee_data = TeeData(iter(iterable))
            return TeeIterator(tee_data)

    class TeeData(object):

        def __init__(self, iterator):
            self.iterator = iterator
            self.buffer = []

        def __getitem__(self, index):
            if index == len(self.buffer):
                self.buffer.append(next(self.iterator))
            return self.buffer[index]

    def tee(iterable, n=2):

        if n < 0:
            raise ValueError("n must be >= 0")
        elif n == 0:
            return ()

        if hasattr(iterable, '__copy__'):
            copyable = iterable
        else:
            copyable = TeeIterator.from_iterable(iterable)

        result = [ copyable ]

        for i in xrange(1, n):
            result.append(copyable.__copy__())

        return tuple(result)

  1. Yes, the returned objects are called tee as well, but they are completely distinct from the itertools.tee() function.
  2. copy.copy() looks for this method too.
http://code.activestate.com/recipes/305588-simple-example-to-show-off-itertoolstee/








Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

islice python implementation with examples

Pure Pyhton Implemntation:
def islice(iterable, *args):
    # islice('ABCDEFG', 2) --> A B
    # islice('ABCDEFG', 2, 4) --> C D
    # islice('ABCDEFG', 2, None) --> C D E F G
    # islice('ABCDEFG', 0, None, 2) --> A C E G
    s = slice(*args)
    it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
    nexti = next(it)
    for i, element in enumerate(iterable):
        if i == nexti:
            yield element
            nexti = next(it)

Give a look at this pdf.It's wonderful for all itertools examples
http://www-igm.univ-mlv.fr/~vialette/teaching/2009-2010/Python/itertools.pdf


#!usr/bin/env/python
"""
islice==>It performs slicing operation on iterable(sequence)and returns an iterator.
negative indexing is not possible.
It can have optional start,step values.If start is not given,it defaults to 0.If step is not given 
it takes default value 1.
"""
from itertools import *
print 'Example-1[Give a string(iterable) and u can get a selected strings into a list]'
print'\n',list(islice('Ajay kumar',2))
#start=0,stop at 2nd element but don't include it.step=1
print'\n',list(islice('Ajay kumar',2,4))
#start=2,stop=4-1,step=1
print'\n',list(islice('Ajay kumar',2,None))
#start=2,stop=till the end,step=1
print'\n',list(islice('Ajay kumar',0,None,2))
#start=0,stop=till the end,step=2
print '\nExample-2 ==>using count with islice(it does same thing as range.)'
for i in islice(count(),5):
 print i,


Output:

Example-1[Give a string(iterable) and u can get a selected strings into a list]

['A', 'j']

['a', 'y']

['a', 'y', ' ', 'k', 'u', 'm', 'a', 'r']

['A', 'a', ' ', 'u', 'a']

Example-2 ==>using count with islice(it does same thing as range.)
0 1 2 3 4
 
 

The above  Examples can be seen in the image attached.














Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

ZIP in python with examples

#!usr/bin/env/python
"""This(zip) builtin function returns a list of tuples"""
x=(1,2,3)
y=(4,5,6)
z=zip(x,y)
print "\nz will be a list:",z

print "\nExample 1:zip(string,list)"
for i in zip("Ajay kumar",[0,1,2,3,4,5,6,7,8,9]):
 print i,
print "\nExample 2:zip(list,list)"
for i in zip([1,2,3],[4,5,6]):
 print i,
print "\n Example 3:zip(tuple,tuple)"
for i in zip((1,2,3),('a','b','c')):
 print i,

Output:

z will be a list: [(1, 4), (2, 5), (3, 6)]

Example 1:zip(string,list)
('A', 0) ('j', 1) ('a', 2) ('y', 3) (' ', 4) ('k', 5) ('u', 6) ('m', 7) ('a', 8) ('r', 9) 
Example 2:zip(list,list)
(1, 4) (2, 5) (3, 6) 
 Example 3:zip(tuple,tuple)
(1, 'a') (2, 'b') (3, 'c')

Output can be seen in the image

 
 
 
One More version from some online book:
 
z = zip(list1, list2)
newlist1, newlist2 = zip(*z)

This works becauses the * syntax unpacks a list of values. The above code zips and unzips two lists, which is pointless, but the same syntax can be used to convert from a list of columns of data to a list of rows of data. For example, the following list comprehension reads in a file of tab-delimited data as a list of rows, where each row is a tuple of values:
rows = [line.rstrip().split('\t') for line in file(filename)]
If you want to flip the data through 90 degrees (i.e. convert from rows or data to columns of data), then you use:
columns = zip(*rows)
For example, if the data was originally (a, 1), (b, 2), (c, 3), it becomes (a, b, c), (1, 2, 3).
 Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Friday, March 15, 2013

Itertools chain function

Few examples are from the book Python standard library by example.

From the
Documentation====>The chain() function takes several iterators as arguments and returns a single iterator that produces the contents of all of them as though they came from a single iterator.


In Python programming language, an iterator is an object which implements the iterator protocol. The iterator protocol consists of two methods. The __iter__() method, which must return the iterator object and the next() method, which returns the next element from a sequence.



#!usr/bin/env/python 
from itertools import * #chain function  
"""
From the
Documentation====>The chain() function takes several iterators 
as arguments and returns a single iterator that produces the contents of all of them as though they came from a single iterator.

"""
from itertools import *
#Example 1(with equal sized lists)
print "Equal Sized lists:"
for i in chain([1,2,3],['a','b','c']):
    print i,
    # ',' is for horizontal printing of output.

#Example 2:(with inequal size of lists)
print "\n inequal size of lists:"
for i in chain([1,2,3],['a','b']):
    print i,

#Example 3:Tuples and strings
print "\n For tuples and strings"
for i in chain((1,2,3),"Ajay kumar Medepalli"):
    #Ajay kumar Medepalli==>It's my name :P
    print i,



The output is shown in the screenshot

 Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Python: Nicest way to pad zeroes to string

Python: Nicest way to pad zeroes to string

Strings:

>>> n = '4'
>>> print n.zfill(3)
>>> '004'
 
And for numbers:
>>> n = 4
>>> print '%03d' % n
>>> 004
>>> print "{0:03d}".format(4)  # python >= 2.6
>>> 004
>>> print("{0:03d}".format(4))  # python 3
>>> 004
String formatting documentation.

 

 








Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Python Random String Generation

Answer in one line:
''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
In details, with a clean function for further reuse:
>>> import string
>>> import random
>>> def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
...    return ''.join(random.choice(chars) for x in range(size))
...
>>> id_generator()
'G5G74W'
>>> id_generator(3, "6793YUIO")
'Y3U'
How does it work ?
We import string, a module that contains sequences of common ASCII characters, and random, a module that deals with random generation.
string.ascii_uppercase + string.digits just concatenates the list of characters representing uppercase ASCII chars and digits:
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.ascii_uppercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
Then we use a generator expression to create a list of 'n' elements:
>>> range(4) # range create a list of 'n' numbers
[0, 1, 2, 3]
>>> ['elem' for x in range(4)] # we use range to create 4 times 'elem'
['elem', 'elem', 'elem', 'elem']
In the example above, we use [ to create the list, but we don't in the id_generator function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).
Instead of asking to create 'n' times the string elem, we will ask Python to create 'n' times a random character, picked from a sequence of characters:
>>> random.choice("abcde")
'a'
>>> random.choice("abcde")
'd'
>>> random.choice("abcde")
'b'
Therefore random.choice(chars) for x in range(size) really is creating a sequence of size characters. Characters that are randomly picked from chars:
>>> [random.choice('abcde') for x in range(3)]
['a', 'b', 'b']
>>> [random.choice('abcde') for x in range(3)]
['e', 'b', 'e']
>>> [random.choice('abcde') for x in range(3)]
['d', 'a', 'c']
Then we just join them with an empty string so the sequence becomes a string:
>>> ''.join(['a', 'b', 'b'])
'abb'
>>> [random.choice('abcde') for x in range(3)]
['d', 'c', 'b']
>>> ''.join(random.choice('abcde') for x in range(3))
'dac'





Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.