my net house

WAHEGURU….!

Monthly Archives: January 2016

Cython in Easy Way, Be cool

Ok Be cool there is lot of buzzing supposed to be happened in my mind because there have to be,

Why I am going to learn about Cython?

1 . Sometimes people say my Favourite language is Damn! Slow! (which is not true if you know about Generators and futures and multiprocessing modules, Let’s just save this for some other day)

2. If I got to use it at any job any day in the field of Data-Analysis or Number-crunching computations.

3. Cyhton is Sexy, I love sexy things, ๐Ÿ™‚ ๐Ÿ˜‰

4. Portability is good for you if you are Server administrator in day and Android-Developer at night, Or even Web-developer in Weekends.[So cython can help you everywhere]

5. You have lots of C++/C code but you can read it

 

Slide1> C+Python = Cython

You will not get it’s need in first place unless you are from NASA ๐Ÿ˜‰

 

Slide2> Why we should/must love Python

{import antigravity}

Slide3> C+P….. = Cython

Do I need to learn C if I have to go with Cython?

No

Slide4> Cython is made for Python Programmers to achieve the Speed of C and Productivity of Python,

Aha! I feel Like I can create Whole universe now(If you understood above statement)

 

Slide5> Where to start if you want to write programs in Cython?

Rember (C+Python = Cython) or (Python+C = Cython) ๐Ÿ˜‰

So let’s Start With Python…! yay!!

 

A very Simple Python Function(Don’t try at home ๐Ÿ˜‰ )

def sum_text(double number_range):

“This function explains lot about my lazyness”
return sum([i*i for i in xrange(number_range)])

When you will run it in ipython uisng %timeit sum_text(100000000) function your system Can crash/hang if you don’t have good RAM like 10 GB

It will take around 15.6 seconds.

So now we need Cython to optimise it.

Again Using Ipython

%%cython
cpdef double sum_text_cython(int number_range):
“This is cython(!100% but…), It works ๐Ÿ˜€ ”

cdef int i
return sum([i*i for i in xrange(number_range)])

Image of above thing: 
https://dl.dropboxusercontent.com/u/32435266/cython.png

 

%timeit sum_text_cython(100000000)

1 loops, best of 3: 3.43 s per loop

So it is about "2.5 times" faster, Yeah!!! (That's what I mean!)

For the information this Example is Just for information, It can produce lots of other errors






Continued.... (Going for dinner) Come with monte-CarloSimulation
Now really going for dinner...
http://hplgit.github.io/teamods/MC_cython/sphinx/main_MC_cython.html

Ok Now really thing isย  we need to take things in real life, Like how to use Cython when You want to do real code in Python as well.

So Writeย  a .pyx file with simple Python code.

sum_text(number_range):
p = [i*i for i in xrange(number_range)]
return sum(p)

So this is simple Python thing, save it hellocython.pyx

NEVER SAVE ITย  “cython.pyx” file.

$ easycython hellocython.pyx

It will generate hellocython.c file and then also compile that into .so file.

Nextslide> So we only have Python code but Cython does understand it well.

nxtSlide> Let’s make code more faster:

def sum_text(int number_range):
cdef int i
p = [i*i for i in xrange(number_range)]
return sum(p)

This above is not even 100% pure Cython function butย  it is 2.5 times faster. ๐Ÿ˜€ with .so file.

nxtslide> Now we can easily import this into our regular Python call.

create normal a.py file.

import hellocython

print hellocython.sum_text(100000)

Now we can call Our results both Cython = C+Python

[Next is Cython using Multi-threaded] AND [Convert it into presentation] Add more Cython examples and make GitHub repository…

A reusable function

def fetch_uids(address,password,folder,d):
mail = IMAP4_SSL(‘imap.gmail.com’)
mail.login(address, password)
mail.select(folder)
interval = (date.today()-timedelta(days)).strftime(“%d-%b-%Y”)
result, data = mail.uid(‘search’, None,'(SENTSINCE {date})’.format(date=interval))
return data

 

 

Now I can define variables according to my project and using the function call I can make my function more reusable:

for daily analysis inbox I can call:

fetch_uids(address,password,folder=’INBOX’,days=0)

 

for daily analysis sentbox I can call:

fetch_uids(address,password,folder='[Gmail]/Sent’,days=0)

Same as I can change the value of ‘days’ which make more sense.

 

 

%d bloggers like this: