Write a program with try-except to get two numbers as input from the user and do division operation. - Use try block to get input from the user and do the division of numbers. - Declare except handler for ZeroDivisionError and generate NameError with "ZERO DIVISON ERROR" message inside it. - Declare default exception handler and print the error message "ERROR CAUGHT !!!" and exit the program. - The program should always print "Program completed..." message at last irrespective of any errors. Scenario 1: Input: Enter the number1: 100 Enter the number2: 5 Output: The division of 100 and 5 is 20 Program completed... Scenario 2: Input: Enter the number1: 100 Enter the number2: 0 Output: Program completed... Traceback (most recent call last): File "C:/Users/Test/Desktop/exception.py", line 8, in raise NameError("ZERO DIVISION ERROR") NameError: ZERO DIVISION ERROR Scenario 3: Input: Enter the number1: welcome Output: ERROR CAUGHT !!! (, ValueError("invalid literal for int() with base 10: 'asdfg'",), ) program completed... Write a module named "calc.py" with below declaration and do below operations. Declare two variables: x=100, y=200 Declare two functions: addition(a,b), multiply(a,b,c) Print the declared variables and call functions[Ex: addition(50, 60) & multiply(2, 3, 5)] in main namespace Declare documentation for calc module - Import calc module and call addition() function with x and y values from module - Import x variable and multiply() function from calc module and call multiply() function with x,2,3 as arguments - Print the documentation for calc module Write a package named "Phone" Write a package named "Food" with below details Food/fruit.py Declare myfruits list with some fruit names such as apple, orange, etc. Declare showFruits() function which will display all the fruits in for loop. Food/vegetable.py Declare myvegetables list with some vegetable names such as carrot, tomato, etc. Declare showVegetables() function which will display all the vegetables in for loop. Food/snacks.py Declare mysnacks list with some snacks names such as biscuit, chocolate, etc. Declare showSnacks() function which will display all the snacks in for loop. Food/__init__.py Import snacks module Import showVegetables() function from vegetable module Import all variables and functions from fruits module Import the Food package and check the access level for variables and functions. (Direct & Sub-level) - Print all the variables (myfruits, myvegetables & mysnacks) - Call all the functions (showFruits, showVegetables & showSnacks) Write a class named "Employee" and "Company" with below details. Company: Declare constructor and store "company_name" and "company_location" details. (static) Declare a function named "showCompany()" which will display the company details. Employee: (Inherit 'Company' class) Declare constructor and store "empid", "empname" and "department" details. (Dynamic) Declare a function named "showEmployee()" which will display employee details. Create an object for 'Company' class and display company details. Create an object for first Employee and display employee and company details. (1001, Manoj, IT) Create an object for second Employee and display employee and company details. (1002, Suresh, Sales) Change the department & company_location for second employee and display the details again. Write a program to get a sentence as input from the user and display the number of digits, alphabets and special characters. Input: Enter the sentence: Welcome to PYTHON. My number is 1234567890. END!!! Output: Given sentence: Welcome to PYTHON. My number is 1234567890. END!!! Character Count --------- ----- DIGITS 10 UPPER 11 LOWER 17 SPECIAL 12 Write a program to find the below math formulas. (using functions) Area of square (a*a) Area of circle (PI * r*r) Area of Triangle (1/2 * b*h) Area of Rectangle (w * h) Write a program to calculate the Celsius to Fahrenheit & vice versa. (using functions) Input: Heat Calculator *************** 1. Celsius to Fahrenheit 2. Fahrenheit to Celsius Choose your option [1-2]: 1 Enter the celsius: 5 Output: Celsius: 5 Fahrenheit: 41 Write a program to print the ASCII value of given characters. Input: Enter the string: Welcome Python! Output: Given string: ASCII Characters: W -> 87 e -> 101 l -> 108 c -> 99 o -> 111 m -> 109 e -> 101 -> 32 P -> 80 y -> 121 t -> 116 h -> 104 o -> 111 n -> 110 ! -> 33 Write regex to match the given pattern?? ------------------------------- Python ------ http://www.diveintopython3.net/special-method-names.html https://jcalderone.livejournal.com/32837.html # 5a. Decrypt method import re def decs-trailhead.salesforce.com/users/00550000006GHTlAAO/trailmixes/VVVvbm-getting-started-with-salesforces-login.salesforce.com m.r@in.v.c -> msalesforce1rypt(s, mydict): l = list(mydict.keys()) result = '' for i in range(0, len(l)): mystr = l[i] if mystr == s: result += "'%s', " %(mydict[mystr]) else: if s.startswith(mystr): mystr1 = mystr mystr1_d = mydict[mystr] for i in range(0, len(l)): if mystr1+l[i] == s: mystr1_d += " %s" %(mydict[l[i]]) result += "'%s', " %(mystr1_d) elif s.startswith(mystr1+l[i]): mystr2 = mystr1 mystr2_d = "%s %s" %(mydict[mystr1], mydict[l[i]]) for k in range(i, len(l)): if mystr2+l[k] == s: mystr2_d += ' %s'%(mydict[l[k]]) result += "'%s', " %(mystr2_d) elif s.startswith(mystr2+l[k]): mystr2 += l[k] result = re.sub(', $', '', result) #print("RES: [%s]"%(result)) return '['+result+']' print(" decrypt() method ".center(50, '*')) print(decrypt('turing', {'turing': 'A', 'tur': 'B', 'ing': 'C', 'tu': 'D', 'ring': 'E', 'ri': 'F', 'ng': 'G'})) # 1. check anagram def check_anagram(): anagram_list = [] mydict = {} fp = open('C:/Users/v531997/Desktop/words.txt', 'r') for line in fp: line = line.rstrip() mydict[line] = False myresult = {} sorted_keys = sorted(mydict.keys()) for i in range(0, len(sorted_keys)): for j in range(i+1, len(sorted_keys)): if mydict[sorted_keys[j]] == False and (sorted(sorted_keys[i]) == sorted(sorted_keys[j])): mydict[sorted_keys[i]] = mydict[sorted_keys[j]] = True if sorted_keys[i] in myresult.keys(): myresult[sorted_keys[i]].append(sorted_keys[j]) else: myresult[sorted_keys[i]] = [sorted_keys[i], sorted_keys[j]] for k in mydict.keys(): if mydict[k] == False: myresult[k] = [k] for i in reversed(sorted(myresult.values(), key=len)): print(' '.join(i)) print(" Check Anagram ".center(50, '*')) check_anagram() # Skip Until def skip_until(p,iterable): iterable = iter(iterable) skip_flag = True for j in iterable: if skip_flag: if p(j): skip_flag = False yield(j) else: yield(j) print(" Skip Until ". center(50, '*')) for i in skip_until(lambda x: len(x) > 1, ['x', 'y', 'abc', 'a', 'xyz']): print(i) # Linked list reverse class LN: def __init__(self, value, next =None): self.value, self.next = value, next def str_ll(ll): answer = '' while ll != None: answer += str(ll.value)+'->' ll = ll.next return answer + 'None' def reverse(ll): if ll == None: return None prev = None curr = ll while (curr != None): next = curr.next curr.next = prev prev = curr curr = next ll = prev return ll print(" Reverse Linked List ".center(50, '*')) l1 = LN(1, LN(2, LN(3))) print(str_ll(l1)) print(str_ll(reverse(l1))) # Sum of Binary tree class TN: def __init__(self, value, left=None, right=None): self.value, self.left, self.right = value, left, right def sums(t): if t == None: return 0 return sums(t.left) + t.value + sums(t.right) tree = TN(6, left=TN(3, right=TN(2)), right=TN(8, left=TN(7), right=TN(9))) print(" Sum of Binary Tree ".center(50, '*')) print(sums(tree)) ================== CGI #!C:\Python27\python.exe # httpd.conf # Options +Indexes +FollowSymLinks +Multiviews +ExecCGI # AddHandler cgi-script .cgi .py import cgi import os def TestPage(): print """ Welcome to Python

Welcome to Python CGI

This is my test page.

""" def EnvPage(): print "" if os.environ['QUERY_STRING']: for myvar in os.environ['QUERY_STRING'].split('&'): (name, value) = myvar.split('=') print "{0} --> {1}
" .format(name, value) print "

Environment Variables


" for k in sorted(os.environ.keys()): print "%20s: %s
" % (k, os.environ[k]) print " " def FormPage(): print "" form = cgi.FieldStorage() first_name = form.getvalue('first_name') last_name = form.getvalue('last_name') if first_name: print "

Hello %s %s

" % (first_name, last_name) print """
First Name:
Last Name :
""" print "Content-type: text/html\n" #TestPage() #EnvPage() FormPage() ================== Django Setup ------------ > python -m django --version > django-admin --version Sqlite3: ------- > sqlite3 [] .databases .tables .schema select * from [List entries without header] .show [To display all default settings] .header on [Try above query, will print header also] .mode column [To display output in column space separated, instead of "|" separator] .timer on [To dislay the time taken to execute the query] .exit To take dump, > sqlite3 MyTestDb.db .dump > MyTestDB.sql To restore from dump, > sqlite3 MyTestDb.db < MyTestDB.sql To list all tables in database, > SELECT tbl_name FROM sqlite_master WHERE type = 'table'; > select sql from sqlite_master WHERE type = 'table' and tbl_name = 'sample'; > select current_timestamp as "CURRENT TIME"; > select strftime("%d/%m/%Y %H:%M:%S %s", 'now') > alter table sample add mydate text; > update sample set mydate = datetime('now', 'localtime') > select datetime(1518599385, 'unixepoch', 'localtime') ------- > django-admin startproject MySite > cd MySite > python manage.py runserver 8080 To Create an application, > python manage.py startapp myfirstapp * Change file myfirstapp/views.py, from django.http import HttpResponse def sample(request): return HttpResponse("

Hello, world!

Welcome to Django Application.

") * Create a file myfirstapp/urls.py, from django.urls import path from . import views urlpatterns = [ path('', views.sample, name='sample'), ] * Change file MySite/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('testapp/', include('myfirstapp.urls')) ] * Start the service, > python manage.py runserver 8080 Try URL, http://localhost:8080/testapp Database Setup, Goto MySite/settings.py - Change TIME_ZONE entry and database details. {Default SQLite] # python manage.py migrate To create super user, > python manage.py createsuperuser ============= # pip install pillow https://pillow.readthedocs.io/en/3.1.x/reference/Image.html =========== class MyTestClass(): a=100 def __init__(self, funcname): exec("self.myval = funcname" .format(funcname=funcname)) exec("self.{name} = 100; self.get_{name} = lambda : 12345 " .format(name=funcname)) #exec("""def mytest(self): print("MYTEST FUNCTION")""" .format(self=self)) #exec("self.{name} = 100; get_{name} = lambda : self.{name}" .format(name=funcname)) self.keywords = ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] print("Class object created.") #def __str__(self): # print("String method: ") # return "STRING: " def checkKeyword(self, keyword): match = 'not' if keyword in self.keywords: match = '' print("Given word [%s] is %s a keyword." %(keyword, match)) def checkIdentifier(self, name): ## Use try / except to check NameError or SyntaxError isValid = False if str(name).isidentifier() and not str(name).startswith("_") and name not in self.keywords: print("Valid identifier [%s]" %(name)) else: print("Invalid identifier [%s]" %(name)) def myfunc(self): print("This if myfunc() function.") def __del__(self): print("Destructor called.") def get_a(self): return self.a def __getitem__(self): print("Calling getitem() method") def __repr__(self): print("MyClass(1,2,3)") return("rep method") myobj = MyTestClass('myval') myobj.myfunc() #print(myobj) #str(myobj, "Welcome", "welcome") myobj.checkIdentifier('if') myobj.checkIdentifier('abcd') myobj.checkIdentifier('10') myobj.checkIdentifier('_abcd') print(myobj.a) print(myobj.get_a()) print(dir(myobj)) print(myobj.get_myval()) repr(myobj) #-->Triple1(a=1,b=2,c=3) myobj.a #print(myobj[a]) #print(myobj.mytest1()) #print(myobj.get_mynum()) #print(getattr(myobj, a)) #print(myobj.getattr(a)) del(myobj) https://docs.python.org/3/contents.html String -> length, List -> length, sort, reverse, replace, min, max, count, index, data structure Tuple -> type conversion Dictionary -> Set -> | & - ^ List Comprehension -> range Looping techniques -> enumerate, zip Lambda expressions Files and directories Errors and Exceptions Iterators, Generators, Decorators Regular Expressions Internet Access Sending Email XML and JSON Processing Databases Networking (Threads) GUI Programming Performance Monitoring (pylint, inspect) Testing tools (unittest) Command line argument and options (-cmvh, sys.argv, sys.path) Python standard library Debugging & Misc functions Modules and Packages Classes and OOPS Django Framework Static Method & Implicit properties, This methods are visible inside classes. It can't be accessed via objects. class MyClass(): def __init__(self, myvar): self.a=myvar self.__b=2000 @property def a(self): return(self.__a) @a.setter def a(self, x): self.__a = x #self.a=200 """ def myStaticMethod(a): print("This is static method. ", a) def myNormalMethod(self, a): print("This is static method. ", a) myobj = MyClass() MyClass.myStaticMethod(200) myobj.myNormalMethod(100) """ myobj=MyClass(100) print(myobj.a) #print(myobj.b) myobj.a=1000 print(myobj.a) # Implicit properties class MyClass(): def __init__(self,x): self.__my_attribute = x def getAttribute(self): return self.__my_attribute def setAttribute(self, x): self.__my_attribute = x @property def my_attribute(self): return self.__my_attribute @my_attribute.setter def my_attribute(self, value): self.__my_attribute = value myobj = MyClass(100) #print(myobj.__my_attribute) print(myobj.getAttribute()) myobj.setAttribute(1000) print(myobj.getAttribute()) myobj1 = MyClass(200) print(myobj1.my_attribute) myobj1.my_attribute=2000 print(myobj1.my_attribute) REF: https://www.python-course.eu/python3_properties.php REF: http://www.bogotobogo.com/python/python_differences_between_static_method_and_class_method_instance_method.php Profiling, cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module. import cProfile import os cProfile.run('os.getcwd()') > python -m cProfile myscript.py REF: https://julien.danjou.info/blog/2015/guide-to-python-profiling-cprofile-concrete-case-carbonara Struct, This module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources. It uses Format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values. mypack = struct.pack('ifiii', 10, 20.5,30,40,50) struct.unpack('ifiii', mypack) Queue, import queue myqueue = queue.Queue() # myqueue = queue.Lifoqueue(5) for i in range(1,10): myqueue.put(i) print(myqueue) print(myqueue.size()) while not myqueue.empty(): print(myqueue.get()) http://structure.usc.edu/python/dist/simple-example.html Distributing modules, MyModule/ mymath.py # Math module def add(x,y): return x + y def sub(x,y): return x - y def mul(x,y): return x * y setup.py from distutils.core import setup setup(name="mymath", version="1.0", description="This is mymath module.", author='Manojkumar R', author_email='manoj.hispeed@gmail.com', url='http://www.mywebsite.com/manoj', py_modules=["mymath"]) > python setup.py sdist Copy the tar file and extract it to some path, mymath-1.0.tar\dist\mymath-1.0\mymath-1.0> python setup.py install --prefix="C:\\Desktop\MySetup" >>> sys.path.append('C:\\\\Desktop\\MySetup\\lib\\site-packages') >>> import mymath Regex match, search, sub Patterns -> ^ $ . [abcA-E], {}, | (), +, *, ?, \wsd, backref [phone, date, time] Modifiers -> re.I, re.S Internet Access from urllib.request import urlopen with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response: #with urlopen('https://www.unixtimestamp.com/index.php') as response: for line in response: line = line.decode('utf-8') # Decoding the binary data to text. if 'EST' in line or 'EDT' in line or ' UTC' in line: # look for Eastern Time print(line) Sending Mail, import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText ''' server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(username, pass) msg = "Hi,\n\nThis is test email from python program.\n\nRegards,\nManoj" server.sendmail("from", "to", msg) server.quit() ''' from_addr = 'from' to_addr = 'to' text = "Hi,\n\nThis is test email from python program.\n\nRegards,\nManoj" msg = MIMEMultipart() msg['From'] = 'from' msg['To'] = 'to' msg['Subject'] = 'Test Mail [Python]' msg.attach(MIMEText(text)) server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.ehlo() server.login('user', 'pass') server.sendmail(from_addr,to_addr,msg.as_string()) server.quit() print "Mail Sent successfully." Module, """ This is my Sample module """ MYVAL=10 def myfunc(): print("This is my function") print("Namespace: ", __name__) if __name__ == '__main__': print("Main Function") print "The value of MYVAL is ", MYVAL myfunc() ------------ import sample #from sample import myfunc MYVAL=100 sample.myfunc() print("MYVAL : ", MYVAL) print("Module MYVAL : ", sample.MYVAL) print(sample.__doc__) XML, 1001 Ramesh CSC 1002 Suresh Mech 1003 Aakash CSC import xml.etree.ElementTree as ETree tree = ETree.parse(r"Sample.xml") root=tree.getroot() #print("Collection: ", root.tag, "\nName: ", root.attrib) print(root.tag.center(50, '*')) print(root.attrib['shelf']) for child in root: print("\n", child.tag, child.attrib['title']) print("\tRegno: ", child[0].text) print("\tName : ", child[1].text) print("\tDept : ", child[2].text) JSON, { "id": "0001", "dept": "CSC", "name": "abc", "marks": { "Maths": 98, "Science": 90, "English": 95 } } import json json_data = json.load(open('Sample.json')) data = { 'name' : 'ABC', 'id' : 1001, 'dept': 'CSC' } json_str=json.dumps(data) print(type(json_str), json_str) json_data=json.loads(json_str) print(type(json_data), json_data) Networking, Server: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Connection received from from', addr c.send('Welcome to the Server.\nThank you for connecting...') c.close() # Close the connection Client: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done Threading, import thread import time, os # Define a function for the thread def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s [%d]" % ( threadName, time.ctime(time.time()), os.getpid() ) # Create two threads as follows try: print("Program started [%d]", os.getpid()) thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) ) except: print "Error: unable to start thread" #while 1: time.sleep(30) print("Processing Completed") Forking, import os def child(): print('\nA new child ', os.getpid()) os._exit(0) while True: newpid = os.fork() if newpid == 0: child() else: pids = (os.getpid(), newpid) print("parent: %d, child: %d\n" % pids) reply = input("q for quit / c for new fork") if reply == 'c': continue else: break GUI Program, import Tkinter import tkMessageBox top = Tkinter.Tk() """ ## Message box with info message def helloCallBack(): tkMessageBox.showinfo( "Hello Python", "Hello World") B = Tkinter.Button(top, text ="Hello")#, command = helloCallBack) B.pack() """ label = Tkinter.Message(top, text = "Welcome\n\t\tto\n python") label.pack() top.mainloop() Databases, import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "password", "testdb" ) # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. sql_query="create table student(id int, name varchar(20), dept varchar(20))" #sql_query="insert into student(id, name, dept) values(1001, 'abc', 'csc')" #sql_query="insert into student(id, name, dept) values(1002, 'def', 'mech')" #sql_query="update student set dept='eee' where id=1002" #sql_query="delete from student where id=1002" cursor.execute(sql_query) db.commit() # Fetch a single row using fetchone() method. """ sql="select * from student" #data = cursor.fetchone() data_set = cursor.fetchall() for data in data_set: print(data) """ db.close() Performance, pylint -> To check the coding standard practices in code(PEP8) and detect errors. > pip install pylint > pylint program.py """welcome to python""" import os MYVAR = 10 print "welcome to python" print "the value of a is: %d " %(MYVAR) print "Name: ", os.getcwd() Inspect -> Provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. import os def myfunc(): print("welcome") inspect.getdoc(os) inspect.getfile(os) inspect.getmodule(os) # sys inspect.isfunction(myfunc) inspect.isfunction(os.getcwd) inspect.isbuiltin(os.getcwd) Unittest -> Framework to test/check the behaviour of functions, methods and classes import unittest myflag=False myvar=30 def add(x,y): return x + y class SimpleTest(unittest.TestCase): def testadd1(self): self.assertEqual(add(4,5),9) def testadd_test2(self): self.assertNotEqual(add(5,5), 12) self.assertEqual(add(5,5), 10) def testadd_test3(self): self.assertTrue(myvar) self.assertFalse(myflag) self.assertIn('p', 'python', "Match pattern not found.") if __name__ == '__main__': unittest.main() Python standard library, system, os, Date, Time, Math import calendar calendar.calendar(2017) calendar.month(2017, 5) import time time.time(); time.gmtime(); time.sleep(5) time.localtime(time.time()) -> [0] time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) Debugging & Misc functions alarm, sleep and exit hostname and login user Getting hostname and ip address socket.gethostname() | socket.gethostbyaddr(socket.gethostname()) socket.gethostbyname(socket.gethostname()) getpass.getuser() platform.platform(), platform.architecture(), platform.processor() Socket functions Executing system commands (dir|date|time \T) Debugger > python -m pdb sample.py Errors and Exceptions import sys try: a= 10 + 20 import asdfadf #raise NameError("This is raised Name Error") except NameError as err: print("Caught NameError Exception: ", err) except TypeError: print("Caught TypeError Exception: ", sys.exc_info()[1]) except: print("Caught Other Exception: ", sys.exc_info()[1]) #sys.exit(0) else: print("This is else part") finally: print("Addition success") print("Program completed") Monkey patching -> http://burnignorance.com/python-tips-and-tricks/monkey-patching-in-python/ https://filippo.io/instance-monkey-patching-in-python/ Monkey-patching is the technique of swapping functions or methods with others in order to change a module, library or class behavior. class MyClass(): def add(self, x, y): return x + y obj = MyClass() obj.add(10,20) def multiple(self, x, y): return x * y MyClass.add = multiple obj.add(10,20) Function -> variable length args def myfunc(a,b, c, d, e ): print(a,b,c,d, e) myfunc(10,20, [30, 40, 50], **{'d':100, 'e': 200}) myfunc(10,20, *[30, 40, 50] Executing module as script, sample.py def myfunc(a,b, c, d, e ): print(a,b,c,d, e) myfunc(*range(50,260, 50)) if __name__ == "__main__": myfunc(*range(10, 60, 10)) import sample # sys.path sample.myfunc(*range(100,600,100)) Iterators: Iterator method is used to iterate elements from object as one by one. mylist=[10,20,30] for i in mylist: print i it = iter(mylist) it.next() ## In 3.x, next(it) Generators: Generators are used to generate iterators. We can use yield method to generate iterators. Normal function return value and yield return. It internally maintain refernces. def mygenerator(): print "Welcome" for i in (10,20): yield i it=iter(mygenerator()) it.next() def mygenerator(): a= yield yield a * 2 it=iter(mygenerator()) it.next() it.send(20) it.next 3.0, def myGenerator(): for i in range(10,50, 10): yield(i) it=myGenerator() next(it) Decorators: Decorator provide a very useful method to add functionality to existing functions and classes. Decorators are functions that wrap other functions or classes. Ex: def math(func, x, y): print "Math Function" func(x,y) def add(a,b): print "This is addition function" print a + b def mul(a,b): print "This is subtraction function" print a * b math(add, 10,20) math(mul,10,20) import time def my_decorator(myfunc): def my_wrapper(): t1 = time.time() print("Function started ", myfunc.__name__) myfunc() print("Function ended ", myfunc.__name__) t2 = time.time() print("Time taken to execution function: ", int((t2 - t1))) return my_wrapper @my_decorator def myfunc(): print("This is my function") time.sleep(2) myfunc() def my_decorator(func): def wrapper(*args, **kwargs): print("Before call") result = func(*args, **kwargs) print("After call") return result return wrapper @my_decorator def add(x, y, z): print(x, y, z) add(100, 200, 300) """ def our_decorator(func): def function_wrapper(x): print("Before calling " + func.__name__) res = func(x) print(res) print("After calling " + func.__name__) return function_wrapper @our_decorator def succ(n): return n + 10 print(succ(100)) """ Classes, PDF, IMAGE Date, Time, Cal Data Compression -> zlib, gzip Performance Measurement -> from timeit import Timer Testing -> unittest Pretty print -> pprint, textwrap Logging -> logging Pickling and Shelving Config Parse -> configparser Threading & Multiprocessing IPC and Networking -> socket, signal GUI with Tk Debugger -> pdb https://docs.python.org/3/reference/index.html#reference-index https://scipy.org/ https://www.python-course.eu/python3_namespaces.php Exercies: * Write a program to find the given number is odd or even number ? Input: Enter the number: 15 Output: The given number(15) is odd number. * Write a program to check whether the given string is a palindrome ? Input: Enter the string: racecar Output: The given string 'racecar' is a palindrome. * Write a program to print the number histogram. Input: Enter the number for histogram: 5826 Output: Result, ***** ******** ** ****** * Write a program to count the number of characters(occurences) in a string. Input: Enter the string: welcome to python program Output: a -> 1 -> 3 c -> 1 e -> 2 g -> 1 h -> 1 m -> 2 l -> 1 o -> 4 n -> 1 p -> 2 r -> 2 t -> 2 w -> 1 y -> 1 ============================= Write a program to find the longest line in the given string ? Input: Enter the lines: welcome to python this is the longest line this is my first line this is second line short line here this is the end line Output: Given lines: [above lines here] Longest line: this is the longest line [Length: 24] Enhance this program to print both longest and shortest lines. Write a program to calculate the sum of given numbers? Input: Enter the limit/count for numbers: 3 Enter the number 1: 10 Enter the number 2: 20 Enter the number 3: 30 Output: The sum of given numbers [10 20 30] is: 60 Write a program to print number histogram and get the sum of all digits in a number ? Input: Enter the number for histogram: 5826 Output: The given number: 5826 Result, ***** ******** ** ****** The sum of digits in number: 21 >>> def getNumber2(n): global result if n > 0: getNumber2(n/10) print n%10, "*" * int(n%10) result += n%10 >>> getNumber2(mynum) 4 **** 2 ** 6 ****** 7 ******* def factorial(n): if n <= 1: return n else: return n * factorial(n-1) ============================= NOTES Joining 2 lines, print "welcome \ to \ python" Multiple statements in single line, print "welcome to python"; print "second string" Multiple assignment, x,y = 100, 200 a=b=123 .format method, >>> "{:15}".format(mystr) 'python ' >>> "{:<15}".format(mystr) 'python ' >>> "{:*<15}".format(mystr) 'python*********' >>> >>> "{:>15}".format(mystr) ' python' >>> "{:*>15}".format(mystr) '*********python' >>> >>> "{:^15}".format(mystr) ' python ' >>> "{:*^15}".format(mystr) '****python*****' >>> "{:.3}".format(mystr) 'pyt' >>> "{:d}".format(123) '123' >>> "{:5d}".format(123) ' 123' >>> "{:f}".format(123.4567) '123.456700' >>> "{:.2f}".format(123.4567) '123.46' >>> "{:15.2f}".format(123.4567) ' 123.46' Finding the max and minimum value from dictionary, >>> mydict = {'x':500, 'y':5874, 'z': 560} >>> max(mydict.values()) 5874 >>> min(mydict.values()) 500 >>> max_key = max(mydict.keys(), key=(lambda k: mydict[k])) >>> print max_key, mydict[max_key] y 5874 >>> >>> min_key = min(mydict.keys(), key=(lambda k: mydict[k])) >>> print min_key, mydict[min_key] x 500 Built-in methods, all() any(), etc... To get the content from web page, import requests response = requests.get("https://en.wikipedia.org/robots.txt") print response.text To check whether the webpage is exist, from urllib.request import urlopen from urllib.error import HTTPError from urllib.error import URLError try: html = urlopen("http://www.example.com/") except HTTPError as e: print "HTTP error" except URLError as e: print "Server not found!" else: print "HTML Details" print html.read() ============================= Project: Bank ATM machine Project Design a program to do the functionalities of Bank ATM machine. Requirements, - Display "Welcome Screen" with menu options 1. Account Summary 2. Withdraw Amount3. Deposit Amount 4. Mini Statement [optional] 5. Transfer Amount [optional] 6. Exit - For any option, get the card number and pin details and do the authentication [first time authentication] Additional, - Send SMS to registered mobile number - Send mails to registered email-id if any transaction amount is above Rs. 10000 - For 5 invalid login attempt, block the card/account type() - get the data type for a variable id() - get the memory address of an object with >>> mynum = input('Enter the number: ') Enter the number: 10 + 20 >>> mynum 30 >>> type(mynum) >>> >>> mystr = input('Enter the string: ') Enter the string: 'welcome ' + 'to Python' >>> mystr 'welcome to Python' >>> type(mystr) >>> mynum = raw_input('Enter the number: ') Enter the number: 10 + 20 >>> mynum '10 + 20' >>> type(mynum) >>> >>> >>> mynum = eval(raw_input('Enter the number: ')) Enter the number: 10 + 20 >>> mynum 30 >>> type(mynum) .py - Regular scripts .py3 - (rarely used) Python3 script. Python3 scripts usually end with ".py" not ".py3", but I have seen that a few times .pyc - compiled script (Bytecode) .pyo - optimized pyc file (As of Python3.5, Python will only use pyc rather than pyo and pyc) .pyw - Python script for Windows. It is executed with pythonw.exe .pyx - Cython src to be converted to C/C++ .pyd - Python script made as a Windows DLL .pxd - Cython script which is equivalent to a C/C++ header .pxi - MyPy stub .pyi - Stub file (PEP 484) .pyz - Python script archive (PEP 441); this is a script containing compressed Python scripts (ZIP) in binary form after the standard Python script header .pywz - Python script archive for MS-Windows (PEP 441); this is a script containing compressed Python scripts (ZIP) in binary form after the standard Python script header .py[cod] - wildcard notation in ".gitignore" that means the file may be ".pyc", ".pyo", or ".pyd". Simple syntax Easy/Quick to develop Fewer lines of code Reduce development time Dynamic data type allocation Ask about them - Introduction - Collegue, Any doubt stop, OS and Intro - As of 03/10/2017, (Rent Rs. 8700/2 -> 4350) EB bill: Rs. 265 Total: Rs. 265 => Rs. 132 For FSaravana, Remaining + 2 months rent + others Rs. 34538 + Rs. 4350 + Rs. 132 => Rs. 39020 As of 03/11/2017, (Rent Rs. 4300/2 -> 2150) For Saravana, Remaining + 1 month rent + others Rs. 34020 + Rs. 2150 + Rs. 0 => Rs. 36170 Rent/Owner Account, Bank Name: INDIAN OVERSEAS BANK IFSC Code: IOBA0000098 Account Number: 009801000046497 Electricity Bill, Consumer No: 092370211213 Name: A.MOHAMEDALI KAMARUNISSA Region: 09-Chennai-South Circle: 400-South1 We need to get the remaining EB amount (Rs. 170) from neighbor for previous bills. EB Bill, 07/07, Total: 380 Units -> Rs. 770 (Per Unit Rs. 2.02) Ours : 180 Units -> Rs. 365 Rem : 200 Units -> Rs. 405 08/09, Total: 300 Units -> Rs. 530 (Per Unit Rs. 1.76) Ours : 150 Units -> Rs. 265 Rem : 150 Units -> Rs. 265 For them, Rs. 670 (last 4 months) https://tamil.boldsky.com/home-garden/how-to/2017/did-you-know-what-should-not-kept-fridge-017611.html https://curl.trillworks.com/ #### import sys, os, time import logging import psutil pid_file = 'test.PID' block_file = 'test.BLOCKED' def createPidFile(filename): mypid = os.getpid() print("Current process ID: {0}".format(mypid)) fp = open(pid_file, 'w') fp.write(str(mypid)) fp.close() def checkFileExist(filename): if os.path.isfile(filename): print("File [{0}] is exist." .format(filename)) return True print("File [{0}] does not exist." .format(filename)) return False def readPidFile(filename): fp = open(filename, 'r') data = fp.readline() print('PID: {0}' .format(int(data))) return int(data) def checkRunning(): if checkFileExist(pid_file): pid = readPidFile(pid_file) if checkPidExist(pid): print("Process is already running...") sys.exit(0) else: createPidFile(pid_file) print("PID file created successfully...") else: print("Process is not running.") def checkPidExist(pid): if psutil.pid_exists(pid): print("PID [{0}] is running." .format(pid)) return True print("PID [{0}]does not exist." .format(pid)) checkRunning() skip_day = '17,18,20' run_week = 'Mon,Tue,Wed,Thu,Fri' start_time = '17:30' end_time = '19:20' def checkRunTime(): (week_day, day, hour, min) = time.strftime('%a:%d:%H:%M', time.localtime()).split(':') (hour, min) = (int(hour), int(min)) print("Current Day and Time: {0} {1} - {2}:{3}" .format(day, week_day, hour, min)) if skip_day: skip_days = skip_day.split(',') print('Skip Days: ',skip_days) if day in skip_days: return False run_weeks = run_week.split(',') (start_hr, start_min) = [int(x) for x in start_time.split(':')] (end_hr, end_min) = [int(x) for x in end_time.split(':')] print('Run Days : ',run_weeks) print('Run Time : {0}:{1} - {2}:{3}' .format(start_hr, start_min, end_hr, end_min)) #print(day, run_weeks, hour, start_hr, end_hr, min, start_min, end_min) if (week_day in run_weeks) and (hour >= start_hr and hour <= end_hr): if ((hour == start_hr and min < start_min) or (hour == end_hr and min > end_min)): return False return True return False if not checkRunTime(): print("Runtime does not match for current run...") sys.exit(0) print("Runtime matched for current run...") while True: if checkFileExist(block_file): print('BLOCK file exist.') time.sleep(2) continue print("Inside Loop [{0}]" .format(os.getpid())) time.sleep(5) #### a=500 # Simple function def myfunc(): #global a #a=100 print "This is myfunc() function." #print "The value of a is ", a # Function with args def add(a,b): print "Going to add two numbers: ", a, b return a + b # Function with default args def add1(a, b=50): print "Going to add two numbers: ", a, b return a + b # Function with variable length args def myfunc1(a,b,c): print "Calling myfunc1()" print a,b,c # Function with variable length args def myfunc2(**a): print "Calling myfunc2()" print a #myfunc() #add(10,20) #add(b=200, a=100) #add1(100) #add1(1000,2000) mylist=[10,20,30] #myfunc1(*mylist) mydict={'a': 100, 'b': 200, 'c': 300} #myfunc1(**mydict) #myfunc2(mylist) ===================================================== OS, Interface between hardware and user. Basic need is to do read/write operation on HW. (storage processing) Tasks, Memory management - Main memory / virtual memory Process management - processor, processes, threads, cpu scheduling, process synchronize, dead locks, etc. File management - file/folder directory structure. Device management - attach/detach device, plug-play Security - User / Group permission, patches Disk Management System performance Layers, User1, User2 Software -> OS -> System software, Application software HW -> CPU, RAM, IO Diff types of OS: Windows Unix / Linux (RHEL, Solaris, Debian, Ubuntu, HP-UX, AIX) Mac / ios Andriod Diagram, (unix) HW -> Kernel -> Shell -> Apps, Compiler, Commands, etc Boot Process BIOS MBR (boot loader) Boot loader (GRUB / LILO) Kernel Operating System Process (init) Diff Languages ??? Why Python ? 1985 - 1990 -> Guido van Rossum Open source -> GPL License (high level programming language) Latest version -> 2.7.13, 3.6.2 Interpreted language (Compiled vs Interpreted), PVM Who Uses?? Google -> web search Youtue -> Video sharing Dropox bittorrent NSA -> National Search Agency NASA -> Scienttific program irobot Company -> intel, cisco, HP, Ibm, seagate and etc What can we do? System programming -> automation, report generation, monitoring, etc GUI Web programming Network / Internet Programming Component Integration Database Programming Numeric & Scientific Program Gaming & Robotic Data Mining / Analytics & Data science ----------------------------------------------- import sys, os, time, re import xlrd import logging import MySQLdb filename = '/home/manoj/TechTrend/Dashboard.xls' (db_connect, db_cursor) = ('', '') dashboardDetail = [ {'name': 'Higher Volumes', 'table': 'higher_volumes', 'range': [(2, 1), (22, 5)], 'columns': ('ttq_daily', 'symbol', 'ltp', 'perc_chg', 'mn_ttq_perc')} #{'name': '15 Curr Volume Breakers', 'table': 'curr_volume_breakers', 'range': [(26, 1), (46, 6)]}, #{'name': 'SIGNALS IN MOMENTUM STOCKS', 'table': 'signals_in_momentum_stocks', 'range': [(51, 1), (71, 6)]}, #{'name': 'NEAR MONTH HIGH', 'table': 'near_month_high', 'range': [(76, 1), (90, 3)]}, #{'name': 'NEAR MONTH LOW', 'table': 'near_month_low', 'range': [(76, 5), (90, 7)]}, #{'name': 'GAINERS OF THE DAY', 'table': 'gainers_of_day', 'range': [(96, 0), (110, 4)]}, #{'name': 'LOOSERS OF THE DAY', 'table': 'losers_of_day', 'range': [(96, 6), (110, 9)]}, #{'name': 'MONTHLY VOLUMES', 'table': 'monthly_volumes', 'range': [(116, 1), (164, 4)]}, #{'name': 'MONTHLY GAINERS', 'table': 'monthly_gainers', 'range': [(116, 6), (141, 9)]}, #{'name': 'MONTHLY LOOSERS', 'table': 'monthly_losers', 'range': [(145, 6), (164, 9)]} ] def main(): checkFileExist(filename) openDbConnection() processExcelData() sys.exit(0) def processExcelData(): global db_connect, db_cursor try: workbook = xlrd.open_workbook(filename) print "No of sheets in workbook", workbook.nsheets worksheet = workbook.sheet_by_index(0) print "Total No. of rows: ", worksheet.nrows for dashboard in dashboardDetail: table = dashboard['table'] print "\nName: ", dashboard['name'] print "Table: ", table print "Range: ", dashboard['range'] (srow, scol) = (dashboard['range'][0][0], dashboard['range'][0][1]) (erow, ecol) = (dashboard['range'][1][0], dashboard['range'][1][1]) query="insert into %s(%s) values " %(table, ', '.join(dashboard['columns'])) print "QUERY: ", query for i in range(srow, erow): #print "\nROW [%d] : " %(i), k=0 query += '(' for j in range(scol-1, ecol): value = worksheet.cell(i, j).value query += "'{0}'," .format(value) query = re.sub(',$', '), ', query) query = re.sub(', $', '', query) print "QUERY: ", query last_updated_time = executeQuery("select max(created_time) from %s where active='Y'" %(table), True) update_query = "update %s set active='N' where active = 'Y' and created_time <= '%s'" %(table, last_updated_time) print "QUERY: ", update_query executeQuery(update_query) executeQuery(query) #print "[HEADER] -> ", worksheet.row_values(0) #for row in range(1, worksheet.nrows): # print "[DATA] -> ", worksheet.row_values(row) except: print "Exception caught", sys.exc_info()[1] db_connect.commit() db_connect.close() print "Database connection closed successfully." def executeQuery(query, Return=False): global db_connect, db_cursor print "Query: ", query #return try: db_cursor.execute(query) if Return: data = db_cursor.fetchone() print "DATA: ", data return data[0] except: print "Exception caught.", sys.exc_info()[1] def openDbConnection(): global db_connect, db_cursor try: db_connect = MySQLdb.connect("trendz.com","trend_d","dat17","trend_d" ) ## CHANGE MYSQL ## print "Database connection established successfully." db_cursor = db_connect.cursor() print "Cursor object created successfully." except: print "Unable to establish connection with database. Error: ", sys.exc_info()[1] sys.exit(0) ## To check the existence of file def checkFileExist(filename): if not os.path.isfile(filename): print "File does not exist. [%s]" %(filename) sys.exit(0) main() --------------------------------- def running_count(iterable,p): match_count = 0 it = iter(iterable) while True: try: if p(next(it)): match_count += 1 yield(match_count) except StopIteration: return def stop_when(iterable,p): it = iter(iterable) while True: try: myval = next(it) if p(myval): return yield(myval) except StopIteration: return def yield_and_skip(iterable): it = iter(iterable) while True: try: myval = next(it) if str(myval).isdigit(): [next(it) for i in range(myval)] yield(myval) except StopIteration: return def windows(iterable,m,n=1): it = iter(iterable) tmplist = [] while True: try: if len(tmplist) > 0: tmplist = tmplist[n:m] tmplist.extend([next(it) for i in range(len(tmplist), m)]) yield(tmplist) except StopIteration: return def alternate(*iterables): iterables = [iter(it) for it in iterables] while True: for it in iterables: try: yield(next(it)) except StopIteration: return def myzip(*iterables): iterables = [iter(it) for it in iterables] while True: tmplist = [] for it in iterables: try: tmplist.append(next(it)) except StopIteration: tmplist.append(None) if all(v is None for v in tmplist): return yield(tmplist) class Backwardable: def __init__(self,iterable): self._iterable = iterable def __iter__(self): class B_iter: def __init__(self,iterable): self._all = [] self._iterator = iter(iterable) self._index = -1 # index of just returned value from __next__ or __prev__ def __str__(self): return '_all={}, _index={}'.format(self._all,self._index) def __next__(self): if len(self._all) == 0: while True: try: self._all.append(next(self._iterator)) except: break ''' if len(self._all) == self._index +1: try: self._index +=1 self._all.append(next(self._iterator)) except : self._index-=1 raise else: self._index+=1 return self._all[self._index] ''' if self._index < len(self._all) -1 : self._index += 1 return self._all[self._index] raise(StopIteration("Stop the iteration. No more next values !!")) def __prev__(self): #print(self._index) if self._index <= 0: raise(AssertionError("Assertion error caught. No more previous values !!")) self._index -= 1 return self._all[self._index] ''' self._index -=1 try: assert self._index> -1, 'No more previous value.' except AssertionError: self._index+=1 raise return self._all[self._index] ''' return B_iter(self._iterable) def prev(x): return x.__prev__() if __name__ == '__main__': # Test running_count; you can add your own test cases print('\nTesting running_count') for i in running_count('bananastand',lambda x : x in 'aeiou'): # is vowel print(i,end=' ') print() for i in running_count(hide('bananastand'),lambda x : x in 'aeiou'): # is vowel print(i,end=' ') print() print(nth(running_count(primes(),lambda x : x%10 == 3),1000)) # Test stop_when; you can add your own test cases print('\nTesting stop_when') for c in stop_when('abcdefghijk', lambda x : x >='d'): print(c,end='') print() for c in stop_when(hide('abcdefghijk'), lambda x : x >='d'): print(c,end='') print('\n') print(nth(stop_when(primes(),lambda x : x > 100000),100)) # Test group_when; you can add your own test cases print('\nTesting yield_and_skip') for i in yield_and_skip([1, 2, 1, 3, 'a', 'b', 2, 5, 'c', 1, 2, 3, 8, 'x', 'y', 'z', 2]): print(i,end=' ') print() for i in yield_and_skip(hide([1, 2, 1, 3, 'a', 'b', 2, 5, 'c', 1, 2, 3, 8, 'x', 'y', 'z', 2])): print(i,end=' ') print() print(nth(yield_and_skip(primes()),5)) # Test windows; you can add your own test cases print('\nTesting windows') for i in windows('abcdefghijk',4,2): print(i,end=' ') print() print('\nTesting windows on hidden') for i in windows(hide('abcdefghijk'),3,2): print(i,end=' ') print() print(nth(windows(primes(),10,5),20)) # Test alternate; add your own test cases print('\nTesting alternate') for i in alternate('abcde','fg','hijk'): print(i,end='') print() for i in alternate(hide('abcde'), hide('fg'),hide('hijk')): print(i,end='') print() for i in alternate(primes(20), hide('fghi'),hide('jk')): print(i,end='') print() print(nth(alternate(primes(),primes()),50)) # Test myzip; add your own test cases print('\nTesting myzip') for i in myzip('abcde','fg','hijk'): print(i,end='') print() for i in myzip(hide('abcde'), hide('fg'),hide('hijk')): print(i,end='') print() for i in myzip(primes(20), hide('fghi'),hide('jk')): print(i,end='') print('\n') print(nth(myzip(primes(),primes()),50)) # Test Backwardable; add your own test cases print('\nTesting Backwardable') s = 'abcde' i = iter(Backwardable(s)) print(i) print(next(i),i) #a print(next(i),i) #b print(next(i),i) #c print(prev(i),i) #b print(prev(i),i) #a try: print(prev(i),i) except AssertionError: print('Tried to prev before first value') print(next(i),i) #b print(next(i),i) #c print(next(i),i) #d print(next(i),i) #d try: print(next(i),i) except StopIteration: print('Correctly raised StopIteration') ------------------------------------ Python Pandas and Numpy, https://www.hackerearth.com/practice/machine-learning/data-manipulation-visualisation-r-python/tutorial-data-manipulation-numpy-pandas-python/tutorial/ IOT, https://www.element14.com/community/groups/internet-of-things/blog/2017/02/17/iot-with-python-essential-packages sdcard, http://www.toontricks.com/2017/07/ubuntu-changes-to-sd-card-undone-after.html https://superuser.com/questions/795034/format-a-protected-corrupted-sd-card https://ubuntuforums.org/showthread.php?t=1985599 https://askubuntu.com/questions/262892/changes-to-sd-card-undone-after-re-mount-e-g-deleted-files-re-appear