Sunday, 27 August 2017

Python Important Notes

Object Oriented:
---------------------
Every small component used in python is an OBJECT
Entities - world which exists
             - which can defined
             - which differ
Attributes -
Behaviours  -
Entities is divided into: (eg Employee) Array
Attributes - data on entity (Name, id, address, age) , (length, memory,name)
behaviour -  action on entity (createjob (), assignjob()), (add, insert,remove)

Data Structures:
---------------------
a=10      a - object ref
             10- object
b=a       shallow copy - incr the ref count
c=10     data pool
a=63     it points another location
if a value change b value does't change. it points to another location

Im-Mutable  - int/long/float/str/tuple  # bool/complex/Nonetype/bytes
Mutable     - bytearray/list/set/dict   # frozenset

Basic INPUT statement in python:
---------------------------------------------
a = input("string")
>>return value of input is a STRING

Basic OUTPUT statement in python:
-----------------------------------------------

print("statement")
1) file extension should be .py
2) #! - she-bang
3) shld have execute perm
$ which python3

Basic Py Program:.
Prog1.py:-
========
#!/usr/bin/python3
a = input("Enter the value of a : ")
b = input("Enter the value of b : ")
res = a + b
print("Final ans = ",res)

Prog2.py:-
========
#!/usr/bin/python3
a = input("Enter the value of a : ")
b = input("Enter the value of b : ")
#Type conversion
res = int(a) + int(b)
print("Final ans = ",res)

Prog3.py:-
========
#!/usr/bin/python3
a = int(input("Enter the value of a : "))
b = int(input("Enter the value of b : "))
res = a+b
print("Final ans = ",res)

String-class:-
==========
>>default is UNICODE strings
>>single quoted  a='hello'
>>double quoted  a="hello"
>>triple quoted  a='''hello'''

Convert the string from UNICODE to bytes
a="hello" # Str
res=a.encode("utf-8")   # converts unicode string to bytes
ALITER
a=b"Hello" # bytes
hardware representation is in bytes

a=b"hello"
res=a.decode("utf-8")    # convert bytes to UNICODE STRING

==============================================
String operations:-
a='hello world'
string length          : len(a)
first element          : a[0]     # indexing
last element           : a[-1]
first 4 elems          : a[:4]    # slicing
last 4 elems           : a[-4:]
except first 4         : a[4:]
except last 4          : a[:-4]
except first 4 & last 4: a[4:-4]
Alt elem               : a[::2]
Alt elem               : a[1::2]
reverse                : a[::-1]
concate                : "hello" + a
upper                  : a.upper()

accept the string from the user

sampling
sa-MPLI-ng

1234567890
12-345678-90

res = input("Enter the string : ")
new = res[:2]+"-"+res[2:-2].upper()+"-"+res[-2:]
print(new)

accept the string from the user

sampling

S-nilpma-G

sol:
a[0].upper() + "-" + a[1:-1][::-1].lower() + "-" + a[-1].upper()

5/2   - true division
5//2  - floor division
=================================================

Guess:-
======

a="12345678"

p1  = a[0:len(a)//2]

p2  = a[len(a)//2:]

print(p1)  # 1234
print(p2)  # 5678

Others functions:-
-------------------
search for a substring in a string ---->  "hello" in mystr
                                   ---->  "hello" not in mystr
split the string                   -----> flst = a.split(delimiter)
a="192.168.1.125"

>> is "a" is string var - YES
>> what is delimiter    - "."
flst = a.split(".")
print(flst)
print(len(flst))
print(flst[0])
print(flst[-1])

Find:-
--------

a.replace()
a.count()
a.index()
a.rindex()
a.find()
a.rfind()
a.rstrip()
a.lstrip()
a.strip()


a='sample data was added'

display the first word    a.split()[0]
display the last word     a.split()[-1]

display the first words last char  a.split()[0][-1]
display the last words first char  a.split()[-1][0]

a=10 20 30 40 50 60 70"
display first 4 fields

a.split()[0:4]

a="north 10,20,30,40"
display the first & last fields i.e 10 & 40

flst = a.split()[1].split(",")
print(flst[0],flst[-1])

import re
a="north 10,20,30,40"
flst = re.split("[\s,]",a)
ALITER
import re
a="sample-----hello-data----using-done"
b=re.sub("-+","-",a)
print(a)

a="north 10,20,30,40"

Branching:-
=========

>> and
>> or
>> not
>> elsif

if conidition:
  statement1
  statement2
else:
  statement1
  statement2

Ex1:
 if a>=0 and a<=100:
   print("in range")
 else:
   print("out of range")

Ex2:
 if a[0] in "aeiou":
   print("Vowel")
 else:
   print("Conso")

Accept string & check the given string is a palindrome

madam
nitin
nayan
gadag

name = "nayan"
print(name[::])
print(name[::-1])
if name[::] == name[::-1]:
    print("palindrome")
else:
    print("no palindrome")

Generate natural nos:-
----------------------------

>> what is the diff b/w range & xrange  of python 2.x
>> What is the diff b/w zip  & izip
>> GENERATOR pattern function - range(start,stop-1,step)
>> range(1,11)
>> list(range(1,1))

range will not allocate memory for all elements. It will assign memory based on demand.
range returns generator.generator has to be iterated then only we can extract the values

Loop:-
=====

i=1
while i<=10:
 print(i)
 i+=1

Iterator:-
=======

>> special classes designed to work on CONTAINERs
>> automatically starts
>> automatically ends  - StopIteration
>> automatically advance

name='harish'

for alpha in name:
  print(alpha,end=" ")

Guess:-
--------
for i in range(1,6):
  print(i," = ",i*i)

Guess:-
--------

numlst = [10,20,30,40,50,60,70,80]

for i in range(len(numlst)):
  print(i,numlst[i])
ALITER
for num in numlst:
  print(num)

data is divided into
sequences = tuples, bytes, list
and
non-sequences = set and dict


Tuple-class:-
==========

>>constant collection
>>Im-mutable
>>months=("jan","feb","mar","apr".....)
>>weeks=()
>>numbers=("zero".....)

1) a = (10,20,30,40)
   ALITER
   a = 10,20,30,40

2) for num  in  a:
     print(num)

3) a,b,c = 10,20,30   # equal distribution - Tuple Unpacking

   a,b,*c = 10,20,30,40,50
   a,*b,c = 10,20,30,40,50
   *a,b,c = 10,20,30,40,50

Bytearray-class:-
=============

>>collection of bytes
>>collection of unsigned integers
>>mutable

a = bytearray([1,2,3,4])


List-class:-
========

>> collection
>> Mutable

defined        - alst = [1,2,3,4,5]
length          - len(alst)

Add             - alst.append(60)
add more     - alst.extend([70,80,90])
insert           - alst.insert(index,value)

delete          - alst.pop(index)
delete          - alst.remove(VALUE)  # first occurance

sort              - a.sort()
reverse        - a.reverse()
stat fns        - sum(alst)
                      max(alst)
                      min(alst)

Guess:-
======

a = (10,20,[30,40,50],60,70)
a[0]
a[1]
a[2]
a[2][0] = 55
a[2].append(66)

numlst = [1,2,3,4,5,6,7,8,9]

oddlst = []  # compulsory
evenlst = []  #

for num in numlst:
   if num % 2 ==0:
     evenlst.append(num)
   else:
     oddlst.append(num)

grplst = ["team1-55",
          "team2-34",
          "team3-26",
          "team4-63",
          "team5-23"
         ]

#total vals
numlst=[]
for grp in grplst:
  flst = grp.split("-")
  numlst.append(int(flst[1]))

print(sum(numlst))

Ex1:-
===

#outputlst = list(map(FUNCTNAME, inputlst))

datalst = ["10","20","30","40","50"]

templst=[]
for data in datalst:
  templst.append(int(data))
ALITER
for i in range(len(datalst)):
  datalst[i] = int(datalst[i])
ALITER
datalst = list(map(int, datalst))
print(sum(datalst))

https://public.etherpad-mozilla.org/p/python

studlst=[
         "arun-blr-CSE-15,20,18",
         "hari-chn-CIV-23,24,21",
         "tanu-hyd-MEC-18,19,11",
         "ravi-tvm-ECE-11,19,20"
       ]
avg of best 2

Sol:
for stud in studlst:
  name,loc,dept,*mlst = stud.replace("-",",").split(",")
  mlst=list(map(int,mlst))
  mlst.sort(reverse=True)
  avg=(mlst[0] + mlst[1])/2
  print(name,loc,dept,avg)

team1= ["anil","ravi","guru","jagu","john"]
team2= ["john","anil","hari","amith","jaya"]
for mem in team1:
  if mem in team2:
    print(mem)

Set-class:-
=======

>> collection of unique values
>> duplicates are deleted
>> non-sequence data structure
>> un ordered datastructure

define    :  a={10,20,30,40,50}
                 b={10,40,80,20,70}

union     :  a|b   # a.union(b) : {70, 40, 10, 80, 50, 20, 30}
intersect :  a&b   # a.intersection(b)  : {40, 10, 20}
minus     :  a-b   # a.minus(b) : {50, 30}

common b/w two sets   : a&b
uncommon b/w two sets : (a|b) - a&b
                      : (a-b) | (b-a)

Dict-class:-
========

>> collection of key-value
>> mutable
>> lookup table
>> non-sequence / un ordered data structure
>> key-based lookup
define      : colors={
                      "blue" : [10,20,30],
                      "red"  : [40,50,60],
                      "green": [70,80,90]
                     }
get         : colors["red"]
            : colors.get("red","UNKNOWN COLOR")
set         : colors["red"]=[11,22,33]
add         : colors["white"] = [5,6,7]
            : colors.update({"white" : [5,6,7]})
search      : if "black" in colors
delete      : colors.pop("red")
ALL KEYS    : colors.keys()
ALL VALS    : colors.values()
traverse    : for i in colors:
                 print(i,colors[i])

colors={
        "blue" : [10,20,30],
        "red"  : [40,50,60],
        "green": [70,80,90]
    }

user = input("Enter any color : ").lower()

if user in colors:
    print("yes")
    print("Selected color = ",user)
    print("Value          = ",colors[user])
else:
    print("no")
    print(colors.keys())



studs = {"arun"  : "blr-cse-VIT",
         "john"  : "chn-ece-BIT",
         ..
        }
prompt the user to enter the name
         name
         loc
         dept
         coll name


emps={ 101  :  "arun-blr-500",
       102  :  "jack-chn-503",
       103  :  "ravi-hyd-501",
       104  :  "tina-tvm-500"
     }
depts={500 : "sales",
       501 : "purch",
       502 : "accts",
       503 : "hrd"
      }

emp-id : 101

name     =
loc      =
deptid   =
deptname =

code = int(input("Enter the emp code : "))

if code in emps:
  name,loc,deptid = emps[code].split("-")
  print("NAme = ",name)
  print("loc  = ",loc)
  print("Depid = ",deptid)
  print("dname = ",depts[int(deptid)])
else:
  print("Not Found")



===========================
a=10                       #int
b="hello"              #str
c=2.5                      #float
e=bytearray()       #bytearray
f=(1,2,3)                #tuple
g=[1,2,3]               #list
h={1,2,3}              #set
i={}                       #dict
j=b"sample"       #bytes
k=None               #empty

==========================

Composition:-
===========

a = [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ]
type(a)

print(a[0][0])
1

How to install a 3rd party modules in Python:-
===================================

$ sudo pip3 install numpy
ALITER
$ sudo easy_install3 numpy
pip3 install pandas
pip3 install matplotlib

numpy:-
=======
>> written in "C" - cython
>> fast to python

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[1,2,3],[4,5,6],[7,8,9]])

c1=a+b

c2=a.dot(b)

print(c1)
print(c2)

print(a[:,0])
print(a.sum())

print(np.arange(1.0,2.0,.1))

studs= {"arun" : [1,2,3],
        "ravi" : [4,5,6]
       }

studs["arun"]
studs["arun"][0]
studs = {
          "arun" : {"dept" : "CSE", "marks" : [1,2,3]  },
          "ravi" : {"dept" : "ECE", "marks" : [4,5,6]  }
        }

studs["ravi"]["dept"]
studs["arun"]["marks"][0]

HTML - XML/JSON
JSON -
Perl -
Ruby -


Files :-
=====

>> to store the data permanently

>> Text Files   - human readable
>> Binary Files - m/c readable

>> 3 ref points   BOF - 0
                           CUR - 1
                           EOF - 2

>> Diff modes
    r - rb  - readonly     - BOF - retained
    w - wb  - writeonly    - BOF - lost
    a - ab  - appendonly   - EOF - retained
    r+ - rb+ - read-write  - BOF - retained
    w+ - wb+ - write-read  - BOF - lost
    a+ - ab+ - append-read - EOF - retained

>> Random Access

   filehandler.seek(No_of_bytes, REF_POINT)

   filehandler.tell() - return nos bytes advanced from BOF to CURR Loc

fob.seek(0,0)    # to move to BOF
fob.seek(25,0)   # from BOF move 25 bytes ahead
fob.seek(100,1)  # from CURR loc move 100 bytes ahead
fob.seek(0,2)    # move to EOF

f1 = open("one.txt","w")

f1.write("Hello world\n")
f1.write("sample data was added\n")
f1.write("Thatz the end")

f1.close()

f2 = open("one.txt","r")
for line in f2:
  print(line)

f2.close()
--------------------------------------------------------------
fob = open("data.txt","w+")

fob.write("kar-blr\n")
fob.write("ap-ama\n")
fob.write("ts-hyd\n")
fob.write("tn-chn\n")
fob.write("ker-tvm")

fob.seek(0,0)
for line in fob:
  line=line.strip()
  print(line.split("-")[0])

fob.close()
--------------------------------------------------------------
fob = open("data.txt","w+")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30\n")
fob.write("arun-maths-10-sci-20-soc-30)

csvfile = open("out.csv","w")

fob.seek(0,0)

for line in fob:
  line = line.strip()
  if line:
    flst = line.split("-")
    tot  = sum(map(int,flst[2::2]))
    print("%s,%.2f\n" %(flst[0],tot))
    csvfile.write("%s,%.2f\n" %(flst[0],tot))
fob.close()
csvfile.close()

----------------------------------------------------------------
strbuffer = fob.read()    # read complete file as string
strbuffer = fob.read(50)  # read only 50 bytes
strbuffer = fob.readline()  # read upto \n or EOF
lstbuffer = fob.readlines() # read complete file
fob.write(strbuffer)
fob.writelines(lstbuffer)
-----------------------------------------------------------------
studs.csv
----------
name,dept,avg
arun,CSE,80
ravi,ECE,85
john,ELE,78
hari,CSE,67

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("studs.csv")

print(df)

df.plot()

plt.show()
------------------------------------------------------------------------------

import os
import sys

if not os.path.isfile("one.txt"):  #check whether file exists
  print("File not found")
  sys.exit()
------------------------------------------------------------------------------
with open("one.txt","r") as fob:  # file autoclosed
  print("hello")
  print("hai")

print("Some")
------------------------------------------------------------------------------
xlrd - excel reader
xlwt - excel writer

ex:

import xlrd

b = xlrd.open_workbook("hello.xlsx")

sheet = b.sheet_by_index(0)

for i in range(sheet.nrows):
 for j in range(sheet.ncols):
  print(sheet.cell(i,j).value)
 
numpy           OR   custom built Framework  or PYATS
pandas
matplotlib
xlrd
xlwt
--------------------------------------------------

Functions:-
========
f(x) = x2
f(5) = 25
def f(x):
  return x*x
ALITER
f1 = lambda x : x*x
>>> f1(5)
25

Ex:
f(x,y) = x+y
f = lambda x,y :  x+y
>>> f(2,3)
5
def fun(x,y):
  return x+y

>> var defined within the fn - LOCAL
>> var defined outside the fn - GLOBAL
>> fns has to be defined before its call

def add(n1,n2):
  res = n1 + n2
  retunr res

a=10
b=20
c = add(a,b)
print(c)

--------------------------------------------------------
namelst = ["arun","ravi","john","manu","eshu"]
reslst=[]
for name in namelst:
  reslst.append(name[0])
ALITER
reslst = [name[0] for name in namelst  ]   # list comprehension
ALITER
reslst = list(map(lambda x : x[0], namelst))  # map-iterator
--------------------------------------------------------
numlst = [1,2,3,4,5,6,7,8]
reslst=list(map(lambda x: x*x , numlst))
zonelst=['north-10','south-20','east-30','west-40']
numlst = list(map(lambda x: int(x.split("-")[1]), zonelst))
print(sum(numlst))
--------------------------------------------------------

Proof the that all the call in python are CALL BY REFERENCE

def add(n1,n2):
 print("n1 = ",id(n1))
 print("n2 = ",id(n2))
 res = n1 + n2
 return res

a=10
b=20
print("a = ",id(a))
print("b = ",id(b))
c = add(a,b)
print(c)


Guess:-
=====

def fun(alst):
  alst[0:4] = "0" * 4
  print("ALST = ",alst)
numlst = [10,20,30,40,50]
fun(numlst)
print("NUMLST = ",numlst)


Positional args:-
============

def addRecord(name,dept,loc,sal):
  pass

addRecord()                       # error
addRecord("arun")                 # Error
addRecord("arun","sales")         # error
addRecord("arun","sales","blr")   # error
addRecord("arun","sales","blr",15000)  # right-way
addRecord("sales","blr","arun",15000)  # right-way  # programmers

Default args:-
==========

def addRecord(name=None,dept="finan",loc="hyd",sal=0):
  pass

addRecord()                       # work
addRecord("arun")                 # works
addRecord("arun","sales")         # works
addRecord("arun","sales","blr")   # works

addRecord("arun","sales","blr",15000)  # works
addRecord("sales","blr","arun",15000)  # works  # programmers resp
                                                # to pass in correct order
Keyword args:-
============

def addRecord(name=None,dept="finan",loc="hyd",sal=0):
  pass

addRecord(loc="chn")
addRecord(loc="blr",dept="IMS")


Variable args:-
===========

>> *args
>> args is a TUPLE

def fun(*args):
  print(args)

fun()
fun(10)
fun(10,20,30,40)
fun(10,20)
fun(10,20,30)


Variable keyword args:-
================

>> **kwargs
>> dictionary

def fun(**kwargs):
  print(kwargs)

fun(a=10,b=20,c=30)
fun()
fun(old="new.txt", new="that.txt")

Guess:-
---------
def fun(*args,**kwargs):
  pass

>> non-keyword first and then keyword args

others:-
--------
>>global
>>globals()
>>locals()

example for special fn - globals()
def fun():
  num=55  # LOCAL
  print("FUN = ",num)
  print("GLOBL = ",globals()["num"])

num=10    # GLOBAL
print("MAIN = ",num)
fun()
print("MAIN = ",num)

ex: for keyword global

def fun():
  global num
  num=55  # LOCAL
  print("FUN = ",num)

num=10    # GLOBAL
print("MAIN = ",num)
fun()
print("MAIN = ",num)


Modules:-
=======

>> collection of fns/classes
>> file extension shd be .py
>> can be include   import modulename
                    from modulename/package import *
>> once a module is import - BYTE CODE .pyc
>> module search path  import sys
                       sys.path

mathlib.py   - open source
mathlib.pyc  - closed source
mathlib.exe  - standalone / non-portable - py2exe

Packages:-
========

>> is a FOLDER
>> collection of modules/subpackages
>> every pack shld have a compulsory file - __init__.py
             
                            mylibs
                              |
     -------------------------------------------------------------------
    |                                                   |                                   |
mathlib.py another.py __init__.py
---------- ----------              -------------
def add(a,b): def add():              __all__=['mathlib',
  print("PACk = ",__package__)      print("HEllo")                 'another']
  print("SYM  = ",__name__)
  print("ANS = ",a+b)

def minus(a,b):
  print(a-b)

def divide(a,b):
  print(a/b)

def mult(a,b):
  print(a*b)

Sample1.py:-
==========

import mylibs.mathlib
mylib.mathlibs.add(10,20)

Sample2.py:-
==========

from mylibs import *    # __init__.py
mathlib.add(10,20)
another.add()

Sample3.py:-
==========
from mylibs.mathlib import *
add(10,20)

=============================
Mathlib.py
--------------

add()/minus()/divide()/mult()

Method1:- Method2:
========= =========
import mathlib import mathlib as m

mathlib.add() m.add()

Method3:- method4:-
========== ==========
from mathlib import add from mathlib import *
add() add()

Method5:-
==========
from mathlib import add as libadd
def add():
  print("HELLO WORLD")

add()
libadd()

===========================
using import - call f1/f2/f3

import vendors.intel
import vendors.sony
import vendors.subvendor.nokia

vendors.intel.f1()
vendors.sony.f2()
vendors.subvendor.nokia.f3()

===========================
using from - call f1/f2/f3
from vendors import *
intel.f1()
sony.f2()
subvendors.nokia.f3()

===============================
from vendors.subvendors.nokia import *
f3()

===============================
import modulename - fully qualified names
from package import * - relative names
from package.module import * - relavtive name

===============================
Classes:-
======


>> local var - private will be accessible only within class
     global var - public will be accessible in/outside class


>> special fns
                 constructor - fn same name as the CLASSNAME
                 is automatically called when a OBJECT IS CREATED

   destructor  - fn same name as the ~CLASSNAME
                 is automatically called when a OBJECT goes out of scope


>> Compile Time Class Development   - C++/JAVA/C#

>> Run Time Class Development       - PERL/PYTHON

There are two types of classes in Python:
     i.         Immutable Classes
Ones whose objects cannot be modified after instantiating. int, long, string, float, bool, tuple, complex, None are immutable ones.
   ii.         Mutable Classes
Ones whose objects can be modified aftestar instantiation. list, dict, bytearray, set are examples of mutable classes in Python.


Python Class Development:-
======================

>> Run Time Classes - Monkey patching - Metaclasses
>> Class heirarchy - base class for all the classes - "object"
>> OLd class style  - python 2.x
>> New class style  - python 2.x / python 3.x
>> this pointer is renamed as "self"
>> self pointer is a compulsory arg for all the MEMBER FNS - RUN TIME CLASSES
>> constructor is def __init__
>> destructor is  def __del__  # optional - garbage collector
>> default access specifier within class - PUBLIC

mylib.py:-
========
class Circle:

   def __init__(self,a):
      self.radius = a
      self.area = 0

   def find(self):
      self.area = 3.14 * (self.radius**2)

   def show(self):
      print(" %.2f = %.2f " %(self.radius,self.area))

Sample.py:-
========

import mylib
c1 = mylib.Circle(2.5)
c1.find()
c1.show()

=============================
class student:
 def __init__(self,name,mlst):
  self.name = name
  self.mlst = mlst

 def find(self):
  self.avg = sum(self.mlst)/3

 def show(self):
  print("%s %s %s"  %(self.name,self.mlst,self.avg)

 def compare(self,other):
  if self.avg > other.avg:
    return True
  else:
    return False


from mylib import student

s1 = student("Arun",[10,20,30])
s2 = student("RAvi",[40,30,10])

s1.find()
s2.find()

if s1.compareAvg(s2):
  s1.show()
else:
  s2.show()


Magic Methods:-
=============

>> Operator overloading

+    def __add__(self,other)
-    def __sub__(self,other)
*    def __mul__(self,other)
/    def __div__(self,other)

==   def __eq__(self,other)
!=   def __ne__(self,other)
>    def __gt__(self,other)
>=   def __ge__(self,other)
<    def __lt__(self,other)
<=   def __le__(self,other)

---------------------------------------------------------------------------
how to write a class                - keyword class
object im memory                  - dictionary
this pointers                            - self   # is not a keyword
acess data members in class   - self.datamember
acess data mem out class        - object.datamember
access methods                       - object.methodname(args)
ctor                                         - def __init__()
dtor                                         - def __del__()
private data members             - self.__datamember
semi private                            - self._datamember
public data members              - self.datamember
private method                       - def __method(self):
operator overloading              - magicmethods def __eq__(self,other)

static data members                - class properties
                                                  classname.staticdatamembers
static methods                         - @staticmethod
inheritance                              - ISA-relationship
method over-riding                 - super().methodname()
                                                 OR
                                                 classname.METHODNAME(self)

---------------------------------------------------------------------------


Ex of static data members:-
=====================


a=10

class Sample:
  total
  def __init__(self,val):
    b=20
    self.value = val

what is a ? - GLOBAL VARIABLE print(a)
what is b ? - LOCAL VAR  print(b)
what is self.value ? - data member print(self.value)
what is total      ? - STATIC DATA MEMBER print(Sample.total)


Ex:
class stock(object):  new style classes in python 2.x
class stock:          old style classes in python 2.x

class stock:          only style in python 3.x

Ex:
class stock:

   __total=0
   def __init__(self,name,cat,qty):
     self.name = name
     self.cat  = cat
     self.qty  = qty
     stock.__total = stock.__total + self.qty

   @staticmethod           # inbuilt decorator
   def showtotal():
     print("Total = ",stock.__total)


if __name__ == '__main__':
 stock.showtotal()
 s1 = stock("a","b",10)
 s2 = stock("a","b",10)
 s3 = stock("a","b",10)
 s4 = stock("a","b",10)
 stock.showtotal()

---------------------------------------------------------------
Example for Inheritance:-
===================

>> there is CTOR CASCADING in python, which we have to do

class Account:
  def __init__(self,num,name=None,bal=0):
    self.num = num
    self.name = name
    self.bal  = bal
  def show(self):
    print("%s %s %s" %(self.num,self.name,self.bal))

class Savings(Account):                  # ***** Deriving class
  def __init__(self,num,name=None,bal=0,intrate=4,cap=0):
    Account.__init__(self,num,name,bal)  # **** CTOR CASCADING
    self.intrate = intrate
    self.cap     = cap
  def show1(self):
    print("%s %s" %(self.intrate,self.cap))


s1 = Savings(12345,"arun",15000,5.5,100000)
s1.show()
s1.show1()

---------------------------------------------------------------
Example for method over-riding:-
==========================

class Account:
  def __init__(self,num,name=None,bal=0):
    self.num = num
    self.name = name
    self.bal  = bal
  def show(self):
    print("%s %s %s" %(self.num,self.name,self.bal))

class Savings(Account):                  # ***** Deriving class
  def __init__(self,num,name=None,bal=0,intrate=4,cap=0):
    Account.__init__(self,num,name,bal)  # **** CTOR CASCADING
    self.intrate = intrate
    self.cap     = cap
  def show(self):                      # over ridden method
    super().show()                     # how to invoke a OVER RIDDEN METHOD
    print("%s %s" %(self.intrate,self.cap))


s1 = Savings(12345,"arun",15000,5.5,100000)
s1.show()

---------------------------------------------------------
Exception handling:-
===============

>> Software interrupts in a program - Exception

>> Implicit handlers
     done by the compilers it self
     abrupt exit
     there is no communication to the programmers


>> Explicit handlers
     designed by programmers
     safe exit
     program resumes back to the SAID POINT
     keywords try/except/finally/raise

>> asynch exceptions - automatic
>> synch exceptions  - mannual  - raise

ex:
# user defined exceptions
class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

n1 = input("Enter the value of n1 : ")
n2 = input("Enter the value of n2 : ")

n1 = int(n1)
n2 = int(n2)

if n1==0 or n2==0:
  raise ZeroError("Value cannot be ZERO")

res = n1 + n2
print("FINAL = ",res)

ex:
class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

try:
    n1 = input("Enter the value of n1 : ")
    n2 = input("Enter the value of n2 : ")
    n1 = int(n1)
    n2 = int(n2)

    if n1==0 or n2==0:
       raise ZeroError("Value cannot be ZERO")
    res = n1 + n2
    print("FINAL = ",res)
except:
    print("Oh problem")

ex:
import traceback

class ZeroError(Exception):
  def __init__(self,mesg):
    Exception.__init__(self,mesg)

try:
    n1 = input("Enter the value of n1 : ")
    n2 = input("Enter the value of n2 : ")
    n1 = int(n1)
    n2 = int(n2)

    if n1==0 or n2==0:
       raise ZeroError("Value cannot be ZERO")
    res = n1 + n2
    print("FINAL = ",res)
except ZeroError as e1:
  print("Action-1",e1)
except ValueError as e2:
  print("Action-2",e2)
except EOFError as e3:
  print("ACtion-3",e3)
except:                            # Generic CATCH BLOCK
    print("Oh problem")
    traceback.print_exc()
finally:
    print("Cleanup")

==============================

Inbuilt libs:-
=========

CORE LIBs - which are installed with the compiler

1) Data persistance modules

pickle   - import pickle
json     - imprt json

shelve   - import shelve
db       - import sqlite3

import pickle

fob = open("info.pickle","wb")

info = { "A" : 10,
         "B" : "hello",
         "C" : 2.5
         "D" : [10,20,30,40],
         "E" : (1,2,3)
         "F" : {"a":1,"b":2}
       }
pickle.dump(info, fob)
fob.close()
------------------------------------------------------------------------
import pickle

fob = open("info.pickle","rb")

res = pickle.load(fob)

fob.close()

print(res)

--------------------------------------------------------------------

2) File Related


import glob        - list all the files
import fileinput   - read the file
import filecmp     - compare 2 files/2 dirs
import shutil      - copy/move file/dirs
import tarfile     - compress folder
import zipfile     - compress file
import stat        - file stats
ex:
import glob
res = glob.glob("*.txt")
print(res)

ex:
import fileinput
res = fileinput.input("one.txt")
print(list(res))

3) Date & Time

from datetime import date,timedelta

print(date.today())

d1 = date(2010,5,1)
d2 = date.today()

print(d2-d1)
print(d1==d2)
print(d2+timedelta(15))


from datetime import time
from datetime import datetime

4) Others

import math
import random
import hashlib   - md5/sha1/sha256
import optparse/argparse/shopts
import logging

import os
import sys
ex:
from optparse import OptionParser

p = OptionParser()
p.add_option("--host1",type="str",action="store",dest="a")
p.add_option("--host2",type="str",action="store",dest="b")
p.add_option("--out",type="int",action="store",dest="c")
p.add_option("--in",type="int",action="store",dest="d")

args, extraargs = p.parse_args()

print(args.a)
print(args.b)
print(args.c)
print(args.d)

=============================

debug   - 10
info    - 20
warning - 30  - default level
error   - 40
critical- 50


import logging

logging.debug("Hello-1")

logging.info("Hello-2")

logging.warning("Hello-3")

logging.error("Hello-4")

logging.crtical("Hello-5")

ex:

import logging
logging.basicConfig(level=logging.DEBUG)

logging.debug("Hello-1")

logging.info("Hello-2")

logging.warning("Hello-3")

logging.error("Hello-4")

logging.critical("Hello-5")

============================
import sys   -  Interface b/w programmer & Python Inter

sys.version
sys.implementation
sys.path

sys.argv
sys.stdin
sys.stdout
sys.stderr

sys.platform
sys.getsizeof(a)
sys.getrefcount(a)


import os    - Interface b/w programmer & underlying OS

os.name
os.pid()
os.getcwd()
os.system("commands")  #    os.system("echo $$")
os.remove("one.txt")
os.environ
os.environ["PATH"]




No comments:

Post a Comment

Note: only a member of this blog may post a comment.