Sep 25, 2019

Python Prime number

import math

def is_prime(n):
    if n<2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(2, sqrt_n + 1): 
        if n % i == 0:
            return False
    return True

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print("All list :", list(foo))
print("Prime list :", list(filter(is_prime, foo)))

Output:
All list : [2, 18, 9, 22, 17, 24, 8, 12, 27]
Prime list : [2, 17]


Sep 24, 2019

Python concurrent.futures ProcessPoolExecutor

"""
The ProcessPoolExecutor class is an Executor subclass that uses a pool of processes to execute calls asynchronously. 

ProcessPoolExecutor uses the multiprocessing module
"""

from concurrent.futures import ProcessPoolExecutor
import math
import multiprocessing
import os
import sys
import time

PRIMES = [
    112272535095293,
    112582705942171,
    112272535095293,
    115280095190773,
    115797848077099,
    109972689928541]

def is_prime(n):
    if n == 2:

        return True

    if n % 2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(3, sqrt_n + 1, 2):
        if n % i == 0:
            return False
    return True

def main():
    print('No of CPUs/Processors: {}' . format(multiprocessing.cpu_count()))
    a = time.time()
    #default max_workers is number of processors on the machine
    with ProcessPoolExecutor() as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print('%d is prime: %s' % (number, prime))
    b = time.time()
    print('Time taken: {:.2f} secs'.format(b-a))

if __name__ == '__main__':
    main()


Output:
No of CPUs/Processors: 4
112272535095293 is prime: True
112582705942171 is prime: True
112272535095293 is prime: True
115280095190773 is prime: True
115797848077099 is prime: True
109972689928541 is prime: False
Time taken: 12.67 secs


Python concurrent.futures ThreadPoolExecutor as_completed

import urllib.request 
from concurrent.futures import ThreadPoolExecutor, as_completed

URLS = ['https://www.google.com', 
               'http://www.cnn.com/',
               'http://europe.wsj.com/', 
               'http://www.bbc.co.uk/', 
               'http://abc.abc.com'   #invalid
             ]

def load_url(url, timeout):
  with urllib.request.urlopen(url, timeout=timeout) as conn:
    txt = conn.read()
    return txt

with ThreadPoolExecutor(max_workers = 5) as executor:
  #Forming Key-Value pairs
  future_to_url = {executor.submit(load_url, url, 50): url for url in URLS}
  print(future_to_url)
  print('----')
  for future in as_completed(future_to_url):
    url = future_to_url[future]
    try:
      data = future.result()
      print('%s length is %d' % (url, len(data)))
    except Exception as e:
      print('Error in URL: %s is %s' % (url, e))


Output:
{<Future at 0x7f40d262d2d0 state=running>: 'https://www.google.com', <Future at 0x7f40cadc4ad0 state=running>: 'http://www.cnn.com/', <Future at 0x7f40cadcd710 state=running>: 'http://europe.wsj.com/', <Future at 0x7f40cadcd410 state=running>: 'http://www.bbc.co.uk/', <Future at 0x7f40cade0a10 state=running>: 'http://abc.abc.com'}
----
Error in URL: http://abc.abc.com is <urlopen error [Errno -2] Name or servicenot known>
https://www.google.com length is 12571
http://www.cnn.com/ length is 1134562
http://europe.wsj.com/ length is 1006417
http://www.bbc.co.uk/ length is 311008

Python sort by val

myd = { "Peter": 40, "John": 2, "Bob": 1, "Danny": 3, } #sort by val s = sorted(myd.items(), key= lambda x:x[1]) print(s) Output: [('Bob', 1), ('John', 2), ('Danny', 3), ('Peter', 40)]

Python Sorted 2 Vs 3 versions

employees = {1000: {'name': 'Sahasra','country': 'India', 'age': 25}, \
   1001: {'name': 'Peter','country': 'US', 'age': 21}, \
   1002: {'name': 'John','country': 'US', 'age': 36}, \
   1003: {'name': 'Sarayu','country': 'India', 'age': 30},\
   1004: {'name': 'Akio','country': 'Japan', 'age': 60}, \
   1005: {'name': 'Anand','country': 'India', 'age': 50}, \
   1006: {'name': 'Vidya','country': 'India', 'age': 32}, \
   1007: {'name': 'Salma','country': 'Bangladesh', 'age': 23},}

# Works in Python 2.7 only
ss = sorted(employees.items(), key=lambda(x, y): y['age'])
print(ss)

# Works in Python 2.7 and 3.7
# Using parentheses to unpack the arguments in a lambda is not allowed in ss ss = sorted(employees.items(), key=lambda x: x[1]['age'])
print(ss)


Output:

[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]

[(1001, {'country': 'US', 'age': 21, 'name': 'Peter'}), (1007, {'country': 'Bangladesh', 'age': 23, 'name': 'Salma'}), (1000, {'country': 'India', 'age': 25, 'name': 'Sahasra'}), (1003, {'country': 'India', 'age': 30, 'name': 'Sarayu'}), (1006, {'country': 'India', 'age': 32, 'name': 'Vidya'}), (1002, {'country': 'US', 'age': 36, 'name': 'John'}), (1005, {'country': 'India', 'age': 50, 'name': 'Anand'}), (1004, {'country': 'Japan', 'age': 60, 'name': 'Akio'})]

Sep 19, 2019

Python ThreadPoolExecutor submit

from concurrent.futures import ThreadPoolExecutor import threading def task(n): print("Processing {} - {}".format(n, threading.current_thread())) def main(): print("Starting ThreadPoolExecutor") with ThreadPoolExecutor(max_workers=3) as executor: future = executor.submit(task, (2)) future = executor.submit(task, (3)) future = executor.submit(task, (4)) print("All tasks complete") if __name__ == '__main__': main()

Output:
Starting ThreadPoolExecutor Processing 2 - <Thread(ThreadPoolExecutor-0_0, started daemon 140052642395904)> Processing 3 - <Thread(ThreadPoolExecutor-0_1, started daemon 140052634003200)> Processing 4 - <Thread(ThreadPoolExecutor-0_2, started daemon 140052625610496)> All tasks complete


Python ThreadPoolExecutor, map

import urllib.request 
from concurrent.futures import ThreadPoolExecutor
import threading

urls = [
  'http://www.python.org', 
  'http://www.python.org/about/',
  'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
  'http://www.python.org/doc/',
  'http://www.python.org/download/',
  'http://www.python.org/getit/',
  'http://www.python.org/community/',
  'https://wiki.python.org/moin/',
]

def fun(url):
  print(url, threading.current_thread())
  r = urllib.request.urlopen(url)
  return r

# make the Pool of workers
pool = ThreadPoolExecutor(4) 

results = pool.map(fun, urls)
real_results = list(results)
print('----')
print(real_results)


Output:
http://www.python.org <Thread(ThreadPoolExecutor-0_0, started daemon 139989036611328)>
http://www.python.org/about/ <Thread(ThreadPoolExecutor-0_1, started daemon 139988956083968)>
http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html <Thread(ThreadPoolExecutor-0_2, started daemon 139988947691264)>
http://www.python.org/doc/ <Thread(ThreadPoolExecutor-0_3, started daemon 139988939298560)>
http://www.python.org/download/ <Thread(ThreadPoolExecutor-0_1, started daemon 139988956083968)>
http://www.python.org/getit/ <Thread(ThreadPoolExecutor-0_3, started daemon 139988939298560)>
http://www.python.org/community/ <Thread(ThreadPoolExecutor-0_0, started daemon 139989036611328)>
https://wiki.python.org/moin/ <Thread(ThreadPoolExecutor-0_3, started daemon 139988939298560)>
----
[<http.client.HTTPResponse object at 0x7f51be3d7910>, <http.client.HTTPResponse object at 0x7f51be3c5f90>, <http.client.HTTPResponse object at 0x7f51be3d73d0>, <http.client.HTTPResponse object at 0x7f51be3d77d0>, <http.client.HTTPResponse object at 0x7f51be3b3b90>, <http.client.HTTPResponse object at 0x7f51be3c5610>, <http.client.HTTPResponse object at 0x7f51be3e1c90>, <http.client.HTTPResponse object at 0x7f51be3d7250>]

Python threads using Queue

#Ref:
#https://stackoverflow.com/questions/47900922/split-list-into-n-lists-and-assign-each-list-to-a-worker-in-multithreading
#https://pymotw.com/2/Queue/

#The Queue module provides a FIFO implementation suitable for multi-threaded programming. 
#It can be used to pass messages or other data between producer and consumer threads safely. 
#Locking is handled for the caller, so it is simple to have as many threads as you want working with the same Queue instance. 
#A Queue’s size (number of elements) may be restricted to throttle memory usage or processing.

#### 3 types of queues
# Basic FIFO Queue
# LIFO Queue
# Priority Queue

from queue import Queue, LifoQueue
from threading import Thread, current_thread
from time import sleep
first_names = ['Steve','Jane','Sara','Mary','Jack','tara','bobby']

q = Queue() #FIFO
lq = LifoQueue() #LifoQueue

num_threads = 3

def do_stuff(q):
    while True:
        print(q.get(), current_thread())
        sleep(1)
        q.task_done()

if __name__ == '__main__':
    print('------ FIFO - Basic ------')

    for x in first_names:
        q.put(x)
    
    for i in range(num_threads):
        worker = Thread(target=do_stuff, args=(q,))
        worker.start()

    q.join()
    
    print('------ LIFO - reverse order -----')

    for x in first_names:
        lq.put(x)
    
    for i in range(num_threads):
        worker = Thread(target=do_stuff, args=(lq,))
        worker.start()

    lq.join()



Output:

------ FIFO - Basic ------
Steve <Thread(Thread-1, started 140202362271488)>
Jane <Thread(Thread-2, started 140202353878784)>
Sara <Thread(Thread-3, started 140202345486080)>
Mary <Thread(Thread-1, started 140202362271488)>
Jack <Thread(Thread-2, started 140202353878784)>
tara <Thread(Thread-3, started 140202345486080)>
bobby <Thread(Thread-1, started 140202362271488)>
------ LIFO - reverse order -----
bobby <Thread(Thread-4, started 140202337093376)>
tara <Thread(Thread-5, started 140202328700672)>
Jack <Thread(Thread-6, started 140202320307968)>
Mary <Thread(Thread-4, started 140202337093376)>
Sara <Thread(Thread-5, started 140202328700672)>
Jane <Thread(Thread-6, started 140202320307968)>
Steve <Thread(Thread-4, started 140202337093376)>


Python multiple threads - how to join

from threading import Thread, active_count, current_thread
import time

def fun(val):
  for _ in range(5):
    print(val, current_thread())
    time.sleep(3)

threads = []
for i in range(1, 11):
   t = Thread(target=fun, args=(i*i,))
   threads.append(t)
   t.start()
   print("Current Threads count: %i." % active_count())

#Join threads
for t in threads:
    t.join()

print('bye')


Output:
######
1 <Thread(Thread-1, started 140645849839360)>
Current Threads count: 2.
4 <Thread(Thread-2, started 140645841446656)>
Current Threads count: 3.
9 <Thread(Thread-3, started 140645833053952)>
Current Threads count: 4.
16 <Thread(Thread-4, started 140645616318208)>
Current Threads count: 5.
25 <Thread(Thread-5, started 140645607925504)>
Current Threads count: 6.
36 <Thread(Thread-6, started 140645599532800)>
Current Threads count: 7.
49 <Thread(Thread-7, started 140645591140096)>
Current Threads count: 8.
64 <Thread(Thread-8, started 140645582747392)>
Current Threads count: 9.
81 <Thread(Thread-9, started 140645574354688)>
Current Threads count: 10.
100 <Thread(Thread-10, started 140645565961984)>
Current Threads count: 11.
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
49 <Thread(Thread-7, started 140645591140096)>
81 <Thread(Thread-9, started 140645574354688)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
9 <Thread(Thread-3, started 140645833053952)>
16 <Thread(Thread-4, started 140645616318208)>
100 <Thread(Thread-10, started 140645565961984)>
1 <Thread(Thread-1, started 140645849839360)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
64 <Thread(Thread-8, started 140645582747392)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
4 <Thread(Thread-2, started 140645841446656)>
16 <Thread(Thread-4, started 140645616318208)>
9 <Thread(Thread-3, started 140645833053952)>
1 <Thread(Thread-1, started 140645849839360)>
100 <Thread(Thread-10, started 140645565961984)>
64 <Thread(Thread-8, started 140645582747392)>
25 <Thread(Thread-5, started 140645607925504)>
36 <Thread(Thread-6, started 140645599532800)>
81 <Thread(Thread-9, started 140645574354688)>
49 <Thread(Thread-7, started 140645591140096)>
bye

Python glob vs glob recursive - loop directory

import glob

p = glob.glob('*.py')
print(p)
print(len(p)) #17

#single star - all files in current dir
p = glob.glob('*', recursive=True)
print(p)
print(len(p)) #20

#double star - all folders and files recursively in current dir
p = glob.glob('**', recursive=True)
print(p)
print(len(p)) #22


"""
Output:
['timeit_test.py', 'args_kwargs.py', 'fibonacci.py', 'shallow_vs_deep_copy.py', 'inheritance_example.py', 'python_closure.py', 'super_test.py', 'date_example.py', 'contextlib_example.py', 're_compile_vs_match.py', 'iterator_example.py', 'str_repr_eval.py', 'generator_example.py', 'init_vs_call.py', 'main.py', 'filter_map_reduce.py', '_test_runner.py']
17

['timeit_test.py', 'args_kwargs.py', 'fibonacci.py', 'shallow_vs_deep_copy.py', 'inheritance_example.py', 'python_closure.py', 'super_test.py', 'utils', 'date_example.py', 'contextlib_example.py', 'test1.txt', 're_compile_vs_match.py', 'test.txt', 'iterator_example.py', 'str_repr_eval.py', 'generator_example.py','init_vs_call.py', 'main.py', 'filter_map_reduce.py', '_test_runner.py']
20

['timeit_test.py', 'args_kwargs.py', 'fibonacci.py', 'shallow_vs_deep_copy.py', 'inheritance_example.py', 'python_closure.py', 'super_test.py', 'utils', 'utils/__init__.py', 'utils/utils.py', 'utils/utils1', 'date_example.py', 'contextlib_example.py','test1.txt', 're_compile_vs_match.py', 'test.txt', 'iterator_example.py', 'str_repr_eval.py', 'generator_example.py', 'init_vs_call.py', 'main.py', 'filter_map_reduce.py', '_test_runner.py']
22
"""

python reverse vs reversed

"""
reverse() modifies the list itself, whereas
reversed() returns an iterator ready to traverse the list in reversed order.
"""

#string reverse (best way for string reverse using slicing)
s = 'string'
print(s[::-1]) #gnirts
print(s) #string

#string reversed
rs = reversed(s)
print(''.join(rs)) #gnirts
print(s) #string

#reverse list
l = [1,2,3]
l.reverse()
print(l) #[3,2,1]

#reversed list
ll = reversed(l)
print(ll) #<list_reverseiterator object at 0x7fa572312790>
print(list(ll)) #[1,2,3]


Sep 18, 2019

python timeit re vs compiled re

import re
import timeit

s = 'strings are strings'
compiled_regex = re.compile(r'(str)in(gs)')

def not_compiled_func():
   r = re.match(r'(str)in(gs)', s) 
   #print(r.group())

def compiled_func():
  r = compiled_regex.match(s)
  #print(r.group()) 

t1 = timeit.timeit(stmt=not_compiled_func, number=1000000)
print('%0.2f' % t1)  #5.65 secs
t2 = timeit.timeit(stmt=compiled_func, number=1000000)
print('%0.2f' % t2)  #1.73 secs



Sep 17, 2019

Python remove all occurrences a given element from the list

#Method1
#Use the same list (don't use another list)
lista = [10, 10, 20, 10, 30, 10, 40, 10, 50]
print(lista)
#o/p: [20, 30, 40, 50]

#Note:
#Every time when you remove item, list index changes

n = 10 #element to remove

print('------Method1-----')
i = 0
listlen = len(lista)
while(i < listlen):
  if lista[i] == n:
    lista.remove(n)
    listlen -= 1
    continue
  i += 1

print(lista)   #[20, 30, 40, 50]


#Method2
print('-----Method2-----')
#Remove element and copy to another list
lista = [10, 10, 20, 10, 30, 10, 40, 10, 50]
listb = list(filter(lambda x: x != 10, lista))
print(listb)   #[20, 30, 40, 50]
listb = [i for i in lista if i != 10]
print(listb)   #[20, 30, 40, 50]

Python Super method

class A:
  def __init__(self):
    print('A')

class AA(A):
  def __init__(self):
    print('AA')
    super().__init__() #No need to pass self, only args if required

a = A()
aa = AA()


class B:
  def __init__(self):
    print('B')

class BB(B):
  def __init__(self):
    print('BB')
    B.__init__(self) #need to pass self and args if required

b = B()
bb = BB()


Output:
A
AA
A
B
BB
B


python str vs repr vs eval

import datetime 

foo = 100
var = 'foo'

print(var) #foo

#with quotes
print(repr(var)) #'foo'

#evaluates any variables
print(eval(var)) #100

today = datetime.datetime.now()

# Prints readable format for date-time object
print (str(today)) #2019-09-17 11:54:13.675979

# prints the official format of date-time object
print (repr(today)) #datetime.datetime(2019, 9, 17, 11, 54, 13, 675979)

Python Shallow vs Deep Copy

"""
1. Copy by Reference
2. Shallow Copy
3. Deep Copy
"""

import copy

#Copy Ref
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9

print('----------')
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))
print('Copy Ref - Old List:', old_list)
print('Copy Ref - New List:', new_list)
print('----------')

#Output
#ID of Old List: 140672073880512
#ID of New List: 140672073880512
#Copy Ref - Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Copy Ref - New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


#Shallow Copy
##############
#A shallow copy creates a new object which stores the reference of the original elements.

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)

print("Shallow Copy Old list:", old_list)
print("Shallow Copy New list:", new_list)
print('----------')

#Output:
#Shallow Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#Shallow Copy - append
######################
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list.append([10,11,12])
print("Shallow Copy add Old list:", old_list)
print("Shallow Copy add New list:", new_list)
print('----------')

#Output:
#Shallow Copy add Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Shallow Copy add New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]


#Shallow Copy - nested update
###########################
#Existing elements will get updated - since it has reference to original elements

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
new_list[1][1] = 400
print("Shallow Copy nested Old list:", old_list)
print("Shallow Copy nested New list:", new_list)
print('----------')

#Output:
#Shallow Copy nested Old list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]
#Shallow Copy nested New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]


#Deep Copy###########
#It makes complete copy of elements
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.deepcopy(old_list)
new_list[1][1] = 400
print('ID of Old List:', id(old_list))
print('ID of New List:', id(new_list))

print("Deep Copy Old list:", old_list)
print("Deep Copy New list:", new_list)

#Output:
#ID of Old List: 140672073879792
#ID of New List: 140672073880992
#Deep Copy Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Deep Copy New list: [[1, 2, 3], [4, 400, 6], [7, 8, 9]]



Python args kwargs

def argsTest(*args, **kwargs):
     print(args)
     print(kwargs)

print('-----------')
argsTest(10)
print('-----------')
argsTest(10, 20)
print('-----------')
argsTest(10, 20, 30, a=10, b=20)
print('-----------')

Output:
-----------
(10,)
{}
-----------
(10, 20)
{}
-----------
(10, 20, 30)
{'a': 10, 'b': 20}
-----------

Sep 16, 2019

python fibonacci


FibList = [0,1] 
  
def fibonacci_func(n): 
    if n<0: 
        print("Entered Incorrect input") 
    elif n<=len(FibList): 
        return FibList[n-1] 
    else: 
        temp_fib = fibonacci_func(n-1)+fibonacci_func(n-2) 
        FibList.append(temp_fib) 
        return temp_fib 


print('#######')
print(fibonacci_func(14))  #233


Sep 13, 2019

python contextlib vs context manager

import contextlib

class contextManagerExample:
  def __init__(self):
    print('inside __init__')
  def __enter__(self):
    print('inside __enter__')
    return 'returning from contextManagerExample enter'
  def __exit__(self, exc_type, exc_val, exc_tb):
    print('inside __exit__')

#No need to write __enter__, __exit__ separately
#yield instead of return
@contextlib.contextmanager
def context_lib_test():
  try:
    yield 'returning from context_lib_test enter'
  except Exception as e:
    raise

if __name__ == '__main__':
  print('-'*20)
  with contextManagerExample() as cm:
    print('inside ContextManagerExample scope')
    print(cm)
  print('-'*20)
  with context_lib_test() as cm:
    print('inside context_lib_test scope')
    print(cm)
  print('-'*20)



Output:
--------------------
inside __init__
inside __enter__
inside ContextManagerExample scope
returning from contextManagerExample enter
inside __exit__
--------------------
inside context_lib_test scope
returning from context_lib_test enter
--------------------