python programming by example

210

Upload: others

Post on 11-Sep-2021

44 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Programming by Example
Page 2: Python Programming by Example

Copyright

PythonProgrammingbyExample

AgusKurniawan

1stEdition,2015

Copyright©2015AgusKurniawan

*CoverphotoiscredittoFajarRamadhany,Bataviasoft,http://bataviasoft.com/.

Page 3: Python Programming by Example

TableofContents

CopyrightPreface1.DevelopmentEnvironment1.1Installation1.2DevelopmentTools1.3PythonShell1.4RunningPythonApplicationfromFiles2.PythonProgrammingLanguage2.1CommonRule2.2Variables2.3Comment2.4ArithmeticOperations2.5MathematicalFunctions2.6IncrementandDecrement2.7GettingInputfromKeyboard2.8PythonOperators2.8.1ComparisonOperators2.8.2LogicalOperators2.8.3BitwiseOpeators2.8.4TestingAll2.9DecisionControl2.10Iteration-forandwhile2.11break,continueandpass2.12Date&Time

Page 4: Python Programming by Example

3.Lists,TuplesandDictionary3.1Lists3.2Tuples3.3Dictionary4.Functions4.1CreatingASimpleFunction4.2FunctionwithParametersandReturningValue4.3FunctionwithMultipleReturningValues4.4RecursionFunction4.5Testing5.PythonObjectOriented5.1CreatingClasses5.2ClassAttributes5.3Built-InClassAttributes5.4DestroyingClassObject5.5WritethemAll5.6Inheritance5.7OverridingMethods5.8OverloadingOperators6.PythonModulesandPackages6.1PythonModules6.2importStatement6.3from...import*Statement6.4InstallingExternalPythonPackage7.StringOperations7.1GettingStarted7.2ConcatenatingStrings

Page 5: Python Programming by Example

7.3StringToNumeric7.4NumerictoString7.5StringParser7.6CheckStringDataLength7.7CopyData7.8UpperandLowerCaseCharacters7.9TestingAProgram8.FileOperations8.1GettingStarted8.2WritingDataIntoAFile8.2.1CreatingaFile8.2.2WritingData8.2.3ClosingaFile8.2.4Demo8.3ReadingDataFromAFile9.ErrorHandling9.1ErrorHandling9.2CatchingAllErrors9.3RaisingExceptions9.4CustomException10.BuildingOwnPythonModule10.1CreatingSimpleModule10.2BuildingOwnPythonPackage11.Concurrency11.1GettingStarted11.2Threading11.3SynchronizingThreads

Page 6: Python Programming by Example

11.3.1MutexLocks11.3.2Event11.4Queue11.5Multiprocessing11.5.1Process11.5.2SynchronizingProcesses11.6ParallelTasks11.6.1ThreadPoolExecutor11.6.2ProcessPoolExecutor12.Encoding12.1GettingStarted12.2EncodingBase6412.3Hexadecimal12.4JSON12.5XML12.6CSV13.HashingandCryptography13.1GettingStarted13.2Hashing13.2.1HashingwithMD513.2.2HashingwithSHA1andSHA25613.2.3HashingwithKeyUsingHMAC13.2.4WritethemAll13.3Cryptography13.3.1SymmetricCryptography13.3.2AsymmetricCryptography14.DatabaseProgramming

Page 7: Python Programming by Example

14.1DatabaseforPython14.2MySQLDriverforPython14.3TestingConnection14.4CRUD(Create,Read,UpdateandDelete)Operations14.4.1CreateData14.4.2ReadData14.4.3UpdateData14.4.4DeleteData14.4.5WritethemAll15.SocketProgramming15.1SocketModule15.2HelloWorld15.3Client/ServerSocket15.3.1ServerSocket15.3.2ClientSocket15.3.3Testing16.PythonRegularExpressions16.1GettingStarted16.2Demo17.PythonGUIProgramming17.1GettingStarted17.2HelloPythonGUI17.3WorkingwithInputForm17.4WorkingwithCommonDialogs18.PythonUnitTesting18.1GettingStarted

Page 8: Python Programming by Example

18.2DemoSourceCodeContact

Page 9: Python Programming by Example

Preface

ThisbookisabriefreferencetothePythonprogramminglanguage.Itdescribesalltheelementsofthelanguageandillustratestheirusewithcodeexamples.

AgusKurniawan

Depok,November2015

Page 10: Python Programming by Example

1.DevelopmentEnvironment

Page 11: Python Programming by Example

1.1Installation

Pythonisawidelyusedgeneral-purpose,high-levelprogramminglanguage.InstallationofPythonapplicationiseasy.ForWindows,LinuxandMacPlatform,youdownloadsetupfilefromPythonwebsite,https://www.python.org/downloads/.Downloadandrunit.Followinstallationcommands.

Ifyou'reworkingonWindowsplatform,youcanrunsetupfileandfollowinstruction.

Page 12: Python Programming by Example

1.2DevelopmentTools

Basically,youcanuseanytexteditortowritePythoncode.Thefollowingisalistoftexteditor:

vimnanoPyCharm,https://www.jetbrains.com/pycharm/IntellijIDEA,https://www.jetbrains.com/idea/Sublimetext,http://www.sublimetext.com/VisualStudio,https://www.visualstudio.com

Inthisbook,IusePyCharmfordevelopmenttool.JetbrainsprovidescommunityandEducationlicensesforPyCharm.

Page 13: Python Programming by Example

1.3PythonShell

AfterinstalledPython,youobtainPythonshellonyourplatform.YoucantypethiscommandonTerminalorCommandPromptforWindowsPlatform.

python

ThisisPython2.x.Then,yougetPythonshell,showninFigurebelow.

IfyouinstalledPython3.x,youcanPythonshellbytypingthiscommand.

python3

ThisisPython3.x.Then,yougetPythonshell,showninFigurebelow.

Page 14: Python Programming by Example

OutputprogramonWindowsplatform.

AfteryoucallPythonshell,youobtaintheshell.Itshows>>>onTerminal.

Trytodothefollowingcommand.

>>>a=3

>>>b=5

>>>printa

Page 15: Python Programming by Example

>>>c=a*b

>>>printc

InPython3.x,printaisreplacedbyprint(a).

Thefollowingisasampleoutputofprogram.

Page 16: Python Programming by Example

AsampleoutputforWindowsplatform.

Page 17: Python Programming by Example

1.4RunningPythonApplicationfromFiles

Youcanrunyourprogrambywritingthemonafile.Forinstance,youcreateafile,calledch01_01.py,andwritethisscript.

print('helloworldfrompython')

Toruntheprogram,youcantypethiscommandonTerminal.

pythonch01_01.py

IfyouwanttoruntheprogramunderPython3.x,typethiscommandonTerminal.

python3ch01_01.py

Programoutput:

Page 18: Python Programming by Example

2.PythonProgrammingLanguage

ThischapterexplainsthebasicofPythonprogramminglanguage.

Page 19: Python Programming by Example

2.1CommonRule

Pythonlanguagedoesn'twrite";"attheendofsyntaxlikeyoudoitonC/C++languages.HereisthesyntaxruleofPython:

syntax_code1

syntax_code2

syntax_code3

Page 20: Python Programming by Example

2.2Variables

Inthissection,weexplorehowtodefineavariableandassignitsvalue.Bydefault,wedefinevariablesonPythonwithassigningvalue.

#declarevariables

num=2

area=58.7

city='Berlin'

country="Germany"

z=10+5j#complexnumber

Ifyouwanttodeclarevariableswithoutassigningvalues,youcansetitusingNone.

#declarevariablewithoutinitializingvalue

counter=None

index=None

Writethesecodesfortesting.

#declarevariables

num=2

area=58.7

city='Berlin'

country="Germany"

z=10+5j#complexnumber

#declarevariablewithoutinitializingvalue

counter=None

index=None

globalvar_global

var_global=10

print(num)

Page 21: Python Programming by Example

print(area)

print(city)

print(country)

print(z)

print(z.real)

print(z.imag)

print(var_global)

Savethesescriptsintoafile,calledch02_01.py.

NowyoucantypethisfileusingPython3.x.

$python3ch02_01.py

AsampleofprogramoutputcanseeninFigurebelow.

Page 22: Python Programming by Example

2.3Comment

Youmayexplainhowtoworkonyourcodewithwritingcomments.Todoit,youcanuse#and"""syntax.Hereissamplecode:

#thisacomment

"""

Thesearelongcomments

Thesearelongcomments

Thesearelongcomments

"""

Page 23: Python Programming by Example

2.4ArithmeticOperations

Pythonsupportsthesamefourbasicarithmeticoperationssuchasaddition,subtraction,multiplication,anddivision.Fortesting,createafile,calledch02_02.py.

Thefollowingisthecodeillustrationforbasicarithmeticinch02_02.py:

a=2.3

b=8

c=a+b

print(c)

c=a-b

print(c)

c=a*b

print(c)

c=a/b

print(c)

Saveandrunthisprogram.

python3ch02_02.py

Asampleofprogramoutput:

Page 24: Python Programming by Example
Page 25: Python Programming by Example

2.5MathematicalFunctions

Pythonprovidesmathlibrary.Ifyou’reworkingwithPython2.x,youcanreadthislibraryonhttps://docs.python.org/2/library/math.html.ForPython3.x,youcanreadmathlibraryonhttps://docs.python.org/3/library/math.html.

Createafile,calledch02_03.py.Writethefollowingcode.

frommathimport*

a=1.8

b=2.5

c=pow(a,b)

print(c)

c=sqrt(b)

print(c)

c=sin(a)

print(c)

print(pi)

Saveandruntheprogram.

python3ch02_03.py

Asampleofprogramoutput:

Page 26: Python Programming by Example
Page 27: Python Programming by Example

2.6IncrementandDecrement

Pythondoesn'thasspecialsyntaxforincrementanddecrement.Wecandefineincrementanddecrementasfollows.

++syntaxforincrement.a++canbedefinedasa=a+1--syntaxfordecrement.a--canbedefinedasa=a-1

Fortesting,createafile,calledch02_04.py.Writethefollowingscript.

a=4

print(a)

#increment

a=a+1

print(a)

a+=10

print(a)

#decrement

a=a-2

print(a)

a-=7

print(a)

Then,saveandruntheprogram.

python3ch02_04.py

Asampleofprogramoutput:

Page 28: Python Programming by Example
Page 29: Python Programming by Example

2.7GettingInputfromKeyboard

Togetinputfromkeyboard,wecanuseinput()forPython3.xandraw_input()forPython2.x.

Fortesting,createafile,calledch02_05.py,andwritethisscript.

#gettinginputfromkeyboardusinginput()

name=input('Whatisyourname?')

print('Hello,'+name+'!!')

user_id=input('WhatisyourID?')

print('Idis'+str(user_id))

#gettinginputfromkeyboardusingraw_input()

product=raw_input('Productname?')

print('Product='+product)

product_id=raw_input('ProductID?')

print('Productid='+str(product_id))

Saveandruntheprogram.

python3ch02_05.py

Asampleofprogramoutput:

Page 30: Python Programming by Example
Page 31: Python Programming by Example

2.8PythonOperators

Inthissection,welearnseveralPythonoperatorssuchascomparison,logicalandbitwiseoperators.

2.8.1ComparisonOperators

Youmaydetermineequalityordifferenceamongvariablesorvalues.Hereisthelistofcomparisonoperators:

==isequalto

!=isnotequal

>isgreaterthan

<islessthan

>=isgreaterthanorequalto

<=islessthanorequalto

2.8.2LogicalOperators

Theseoperatorscanbeusedtodeterminethelogicbetweenvariablesorvalues.

&&and

||or

!not

2.8.3BitwiseOpeators

BitwiseoperatorsinPythoncanbedefinedasfollows

&AND

|OR

^ExclusiveOR

Page 32: Python Programming by Example

>>Shiftright

<<Shiftleft

~Not(Inversion)

2.8.4TestingAll

Nowwetesthowtousecomparison,logicalandbitwiseoperatorsincode.Createafile,calledch02_06.py,andwritethesescripts.

#comparisonoperators

a=3

b=8

print(a==b)

print(a!=b)

print(a>b)

print(a>=b)

print(a<b)

print(a<=b)

#logicaloperators

print((a==b)and(a!=b))

print((a<=b)or(a>b))

print(not(a>=b))

#bitwiseoperators

#declarebinaryvariables

m=0b01010011

n=0b11111001

print(m)

print(n)

print(bin(m&n))

print(bin(m|n))

print(bin(m^n))

print(bin(~m))

print(bin(b<<3))

print(bin(b>>2))

Page 33: Python Programming by Example

Savethisfileandruntheprogram.

pythonch02_06.py

Asampleofprogramoutput:

Page 34: Python Programming by Example

2.9DecisionControl

Syntaxmodelforif..elsecanbeformulatedasbelow:

if(conditional):

#dosomething

else:

#dosomething

#########

if(conditional):

#dosomething

elif(conditional):

#dosomething

else:

#dosomething

Fortesting,createafile,calledch02_07.pyandwritethesescripts.

#if-else

a=10

b=30

print('demoif-elif-else')

if(a>10)or(b>10):

#dosomething

print('(a>10)or(b>10)')

elif(a!=5)and(b<=7):

#dosomething

print('(a!=5)and(b<=7)')

else:

#dosomething

print('else')

#nestedif

if(a==0)or(b>20):

ifb<50:

Page 35: Python Programming by Example

print('nested-if')

else:

print('else-nested-if')

else:

print('if-else')

Saveandruntheprogram.

python3ch02_07.py

Asampleofprogramoutput:

Page 36: Python Programming by Example

2.10Iteration-forandwhile

Iterationoperationisusefulwhenwedorepetitiveactivities.Thefollowingisforsyntax.

for(iteration):

#dosomething

while(conditional):

#dosomething

Fortesting,createafile,calledch02_08.pyandwritethesescripts.

#iteration-for

print('demo-iterationfor')

foriinrange(1,5):

print(i)

#nested-for

print('demo-nestedfor')

foriinrange(1,3):

forjinrange(5,10):

print(str(i)+'-'+str(j))

#iteration-while

print('demo-iterationwhile')

i=0

whilei<10:

print(i)

i+=1

Saveandruntheprogram

python3ch02_08.py

Page 37: Python Programming by Example

Asampleofprogramoutput:

Page 38: Python Programming by Example

2.11break,continueandpass

breakcanbeusedtostoponthecodepoint.Otherwise,continuecanbeusedtoskipsomescripts.Forillustration,wehavealooping.Theloopingwillbestoppedusingbreakifvalue=7.Anothersample,wecanskiptheiterationwithvalue=4usingcontinuesyntax.

Writethesescripts.

print('demo-break,continueandpass')

foriinrange(1,10):

ifi==4:

continue

ifi==7:

break

print(i)

pass#donothing

print('Thisistheendofprogram')

Savethesescriptsintoafile,calledch02_09.py.

Runtheprogram.

python3ch02_09.py

Asampleofprogramoutput:

Page 39: Python Programming by Example
Page 40: Python Programming by Example

2.12Date&Time

WecanworkwithDataandtimeinPythonusingtimelibrary.Wemustimportthislibrary.

Fortesting,writethesescripts.

importtime

#getcurrenttime

now=time.time()#utc

print(now)

#displayreadablecurrenttime

print(time.strftime("%b%d%Y%H:%M:%S",time.gmtime(now)))

print(time.timezone)

Savetheprogramintoafile,calledch02_10.py.

Runtheprogram.

python3ch02_10.py

Asampleofprogramoutput:

Page 41: Python Programming by Example
Page 42: Python Programming by Example

3.Lists,TuplesandDictionary

ThischapterexplainshowtoworkwithPythoncollection.

Page 43: Python Programming by Example

3.1Lists

Pythonprovidesalistforcollectionmanipulation.Wedefinealistas[].

Forillustration,weshowyouhowtousealistinPythonprogram.Theprogramimplements

declaringprintinggettingalistlengthaddinggettingaspecificitemfromalistsortingremoving

Writethesescripts.

#declarelists

print('----declarelists')

numbers=[]

a=[2,7,10,8]

cities=['Berlin','Seattle','Tokyo','Moscow']

b=[10,3,'Apple',6,'Strawberry']

c=range(1,10,2)

#print(lists

print('----print(lists')

print(a)

forcityincities:

print(city)

print(b)

print(c)

#getlengthoflists

print('----getlengthoflists')

print(len(a))

print(len(cities))

Page 44: Python Programming by Example

#additemintolist

print('----additem')

numbers.append(10)

numbers.append(5)

cities.append('London')

foriinnumbers:

print(i)

forcityincities:

print(city)

#getspecificitem

print('----getitem')

print(cities[2])

print(a[3])

#sorting

print(a.sort())

#edititem

print('----edititem')

cities[2]='newcity'

forcityincities:

print(city)

#removeitem

print('----removeitem')

a.remove(8)#byvalue

delcities[2]#byindex

forcityincities:

print(city)

Savethsesscriptsintoafile,calledch03_01.py.

Runtheprogram.

python3ch03_01.py

Page 45: Python Programming by Example

Asampleofprogramoutput:

Page 46: Python Programming by Example

3.2Tuples

Wecandefineatupleusing()inPython.Atuplecanbeappendanewitem.

Fortesting,webuildaprogramintoafile,calledch03_02.py.Writethesescripts.

#declaretuples

a=()

b=(3,5,7)

c=('Ford','BMW','Toyota')

d=(3,(5,'London'),12)

#print

print(a)

print(b)

print(c)

print(d)

#getlengthoftuples

print(len(a))

print(len(b))

print(len(c))

print(len(d))

#getitem

print(b[2])

print(c[1])

#getindex

print(b.index(7))

print(c.index('Toyota'))

Saveandruntheprogram.

python3ch03_02.py

Asampleofprogramoutput:

Page 47: Python Programming by Example

Asampleofprogramoutput:

Page 48: Python Programming by Example

3.3Dictionary

Wecancreateanarraywithkey-valueordictionary.Pythonuses{}toimplementkey-valuearray.

Forillustration,wecreateaprogram,ch03_03.py.Writethesescripts.

#declare

a={}

b={2:'Sea',3:'River',8:'Mountain'}

c={2:{4:'abcd',5:'hjkl'},3:'vbnm'}

d=dict(name='elena',age=30,roles=('manager',

'consultant'))

#print

print(a)

print(b)

print(c)

print(d)

#keysvalues

print(b.keys())

print(b.values())

print(b.items())

#additem

a.setdefault(2,'car')

a.setdefault(5,'train')

a.setdefault(7,'plane')

print(a)

#checkkey

print(3inb)

print(5inb)

Savethesescriptsandruntheprogram.

python3ch03_03.py

Page 49: Python Programming by Example

Asampleofprogramoutput:

Page 50: Python Programming by Example

4.Functions

ThischapterexplainshowtocreatefunctionusingPython.

Page 51: Python Programming by Example

4.1CreatingASimpleFunction

DeclaringfunctioninPythonhasformatasfollows.

deffoo():

print('foo()')

Youcancallthisfunctionfoo()onyourprogram.

Page 52: Python Programming by Example

4.2FunctionwithParametersandReturningValue

Sometimesyouwanttocreateafunctionwithaparameter.Youcanimplementitasfollows.

defcalculate(val_a,val_b):

val=val_a*val_b

returnval

Page 53: Python Programming by Example

4.3FunctionwithMultipleReturningValues

AfunctioncanreturnmultiplereturningvaluesinPython.Forinstance,wereturnthreevaluesinPythonfunction.Asamplecodecanbewrittenasbelow.

defperform(num):

d=num*5

returnd,d+5,d-2

Page 54: Python Programming by Example

4.4RecursionFunction

Recursionfunctionisafunctionwherethesolutiontoaproblemdependsonsolutionstosmallerinstancesofthesameproblem(asopposedtoiteration).Forillustration,wecanimplementFibonacciproblemusingPython.

ThefollowingisasamplecodeforFibonaccisolutioninPython.

deffibonacci(n):

ifn==0:

return0

elifn==1:

return1

else:

returnfibonacci(n-1)+fibonacci(n-2)

Page 55: Python Programming by Example

4.5Testing

Wecanwritecodeagainfromsection4.1to4.4andusetheminonch04_01.pyfile.

deffoo():

print('foo()')

defcalculate(val_a,val_b):

val=val_a*val_b

returnval

defperform(num):

d=num*5

returnd,d+5,d-2

deffibonacci(n):

ifn==0:

return0

elifn==1:

return1

else:

returnfibonacci(n-1)+fibonacci(n-2)

foo()

m=calculate(10,5)

print(m)

a,b,c=perform(5)

print(a)

print(b)

print(c)

res=fibonacci(10)

print(res)

Saveandruntheprogram.

python3ch04_01.py

Page 56: Python Programming by Example

Asampleofprogramoutput:

Page 57: Python Programming by Example

5.PythonObjectOriented

ThischapterweexplorehowtoworkwithObject-OrientedprogramminginPython.

Page 58: Python Programming by Example

5.1CreatingClasses

Object-orientedprogramming(OOP)isaprogramminglanguagemodelorganizedaroundobjects.Inthischapter,Idon’texplainindetailaboutOOP.IrecommendyoutoreadtextbooksrelatedtoOOP.

InOOP,aclassisatemplatedefinitionofthemethodsandvariablesinaparticularkindofobject.YoucandeclareaclassinPythonasfollows.

frommathimport*

classCity:

#classdata

city_count=0

city_id=0

###dosomething

Note:YoucanworkwithOOPusingPython3.x.

Afterdeclaredaclass,wecanuseit.

a=City()

b=City()

Page 59: Python Programming by Example

5.2ClassAttributes

Wecandeclareclassattributestostorethedataortocommunicatetootherobject.

Forinstance,wedeclarecity_countandcity_id.Wealsodefinemethods:move_to()anddistance().

classCity:

#classdata

city_count=0

city_id=0

#classattributes

defmove_to(self,x=0,y=0):

self.x+=x

self.y+=y

defdistance(self,other_city):

xi=pow(other_city.x-self.x,2)

yi=pow(other_city.y-self.y,2)

returnsqrt(xi+yi)

Page 60: Python Programming by Example

5.3Built-InClassAttributes

Basically,Pythonhasbuilt-inclassattributes,suchas__init__()isusedasclasscontructorand__str__()togenerateinformationabouttheclass.

classCity:

#classdata

city_count=0

city_id=0

#constructor

def__init__(self,name='',x=0,y=0):

self.name=name

self.x=x

self.y=y

City.city_count+=1#accessallCityclasses

self.city_id=City.city_count

def__str__(self):

return'City:'+self.name+',id='+

str(self.city_id)+',x='+str(self.x)+',y='+str(self.y)

Page 61: Python Programming by Example
Page 62: Python Programming by Example

5.4DestroyingClassObject

Inaclass,wecandefinedestructortoclearallusageresouces.Wecanuse__del__()inPythontodothat.

classCity:

def__del__(self):

#getclassname

class_name=self.__class__.__name__

print('class',class_name,'destroyed')

Page 63: Python Programming by Example

5.5WritethemAll

Let'strytowritethecodeinimplementation.Createafile,calledch05_01.py,andwritethesescripts.

frommathimport*

classCity:

#classdata

city_count=0

city_id=0

#constructor

def__init__(self,name='',x=0,y=0):

self.name=name

self.x=x

self.y=y

City.city_count+=1#accessallCityclasses

self.city_id=City.city_count

def__str__(self):

return'City:'+self.name+',id='+

str(self.city_id)+',x='+str(self.x)+',y='+str(self.y)

#classattributes

defmove_to(self,x=0,y=0):

self.x+=x

self.y+=y

defdistance(self,other_city):

xi=pow(other_city.x-self.x,2)

yi=pow(other_city.y-self.y,2)

returnsqrt(xi+yi)

def__del__(self):

#getclassname

class_name=self.__class__.__name__

print('class',class_name,'destroyed')

a=City('Hamburg',10,5)

b=City('Berlin',3,10)

Page 64: Python Programming by Example

print(a)

print(b)

print(City.city_count)

a.move_to(4,3)

b.move_to(7,12)

print(a)

print(b)

distance=a.distance(b)

print(distance)

Nowyoucanruntheprogram.

$python3ch05_01.py

Asampleofprogramoutput:

Page 65: Python Programming by Example

5.6Inheritance

Inheritance,encapsulation,abstraction,andpolymorphismarefourfundamentalconceptsofobject-orientedprogramming.Inthissection,weimplementinheritanceinPython.

Inheritanceenablesnewobjectstotakeonthepropertiesofexistingobjects.Aclassthatisusedasthebasisforinheritanceiscalledasuperclassorbaseclass.InPython,wecandeclareinheritanceasfollows.

importmath

classshape:

def__init__(self):

print('call__init__fromshapeclass')

deffoo(self):

print('callingfoo()fromshapeclass')

classcircle(shape):

def__init__(self,r):

print('call__init__fromcircleclass')

self.r=r

defcalculate_area_circle(self):

returnmath.pi*self.r*self.r

classrectangle(shape):

def__init__(self,l,w):

print('call__init__fromrectangleclass')

self.l=l

self.w=w

defcalculate_area_rectangle(self):

returnself.l*self.w

a=shape()

a.foo()

Page 66: Python Programming by Example

b=circle(5)

b.foo()

area=b.calculate_area_circle()

print('area:',area)

c=rectangle(2,3)

c.foo()

area=c.calculate_area_rectangle()

print('area:',area)

Savethesescriptsintoafile,calledch05_02.py.

Nowrunthisfile.

$python3ch05_02.py

Programoutput:

Page 67: Python Programming by Example

5.7OverridingMethods

WecanoverrideclassmethodsinOOP.Forinstance,wehaveaclass,shape,whichhascalculate_area().Weoverridethismethodfromderivedclassbyourownimplementation.

Writethesescriptsforsample.

importmath

classshape:

def__init__(self):

print('call__init__fromshapeclass')

defcalculate_area(self):

print('callingcalculate_area()fromshapeclass')

return0

classcircle(shape):

def__init__(self,r):

print('call__init__fromcircleclass')

self.r=r

defcalculate_area(self):

print('callingcalculate_area()fromcircleclass')

returnmath.pi*self.r*self.r

classrectangle(shape):

def__init__(self,l,w):

print('call__init__fromrectangleclass')

self.l=l

self.w=w

defcalculate_area(self):

print('callingcalculate_area()fromrectangle

class')

returnself.l*self.w

a=shape()

Page 68: Python Programming by Example

area=a.calculate_area()

print('area:',area)

b=circle(5)

area=b.calculate_area()

print('area:',area)

c=rectangle(2,3)

area=c.calculate_area()

print('area:',area)

Saveandruntheprogram.

$python3ch05_03.py

Asampleofprogramoutput:

Page 69: Python Programming by Example

5.8OverloadingOperators

WecandefineouroverloadingoperatorsinPythonusing__add__()forinstance.

Forcompleted,writethesescripts.

classPoint:

def__init__(self,x,y):

self.x=x

self.y=y

def__add__(self,other):

returnPoint(self.x+other.x,self.y+other.y)

def__sub__(self,other):

returnPoint(self.x-other.x,self.y-other.y)

def__mul__(self,other):

returnPoint(self.x*other.x,self.y*other.y)

def__str__(self):

return'x'+str(self.x)+',y:'+str(self.y)

a=Point(10,3)

b=Point(2,7)

c=Point(8,1)

print(a)

print(a+b)

print(c-b)

print(a*c)

Savethesescriptsintoafile,calledch05_04.py.

$python3ch05_04.py

Page 70: Python Programming by Example

Aprogramoutput:

Page 71: Python Programming by Example

6.PythonModulesandPackages

ThischapterexplainshowtoworkPythonmodulesandpackages.

Page 72: Python Programming by Example

6.1PythonModules

Inthischapter,welearnhowtoaccessPythonmodules.AlistofPythonpackagecanbefoundthiswebsite,https://pypi.python.org/pypi?%3Aaction=index.

Page 73: Python Programming by Example

6.2importStatement

YoucanaccessPythonmoduleusingimport.Trytowritethesescripts.

importmath

a=math.sin(0.3)

print(a)

b=math.sqrt(math.sin(0.5)*math.pow(5,3))

print(b)

Saveintoafile,calledch06_01.py.Runtheprogram.

$python3ch06_01.py

Programoutput:

Page 74: Python Programming by Example

6.3from...import*Statement

Onprevioussection,weuseimporttousePythonmodule.Touseit,youshouldmodulename.Youcanignoreitusingfrom...importstatement.

Writethesescripts.

frommathimport*

a=sin(0.3)

print(a)

b=sqrt(sin(0.5)*pow(5,3))

print(b)

Youcanseewedon'tneedtocallmath.sin().Wejustcallsin().

Saveandryntheprogram.

$python3ch06_02.py

Programoutput:

Page 75: Python Programming by Example
Page 76: Python Programming by Example

6.4InstallingExternalPythonPackage

IfyouwanttouseexternalPythonPackage,forinstance,Colorama,https://pypi.python.org/pypi/colorama.Wecaninstallituviapip.

TypethiscommandonTerminal.

pipinstallcolorama

IfyouwanttoinstallitforPython3.x,youshouldpip3.

pip3installcolorama

Nowlet'swritethedemo.

importcolorama

fromcoloramaimportFore,Back,Style

colorama.init()

message="helloworldfrompython"

print(message)

print(Fore.RED+message)

print(Fore.GREEN+message)

print(Fore.BLUE+message)

print(Fore.RED+Back.YELLOW+message+Style.RESET_ALL)

Saveintoafile,calledch06_03.py.

$python3ch06_03.py

Programoutput:

Page 77: Python Programming by Example

Programoutput:

Page 78: Python Programming by Example

7.StringOperations

ThischapterexplainshowtoworkwithStringoperationinPython.

Page 79: Python Programming by Example

7.1GettingStarted

Wealreadyusestringasdatatype,https://docs.python.org/3/library/string.html.Inthissection,weexploresomeoperationsinstring.

Thenextstepistoexplorehowtoworkwithstring.

Page 80: Python Programming by Example

7.2ConcatenatingStrings

Ifyouhavealistofstring,youcanconcatenateintoonestring.Youcanuse+operatorandformat()function.Hereisasamplecode

#Concatenating

print(str1+""+str2)

print(str1,str2)

print("%s%s"%(str1,str2))

print("{}{}".format(str1,str2))

Page 81: Python Programming by Example

7.3StringToNumeric

Sometimeyouwanttodomathoperationsbutinputdatahasstringtype.Toconvertstringtypeintonumeric,youcanuseint()forStringtoIntegerandfloat()forstringtoFloat.

Thefollowingisasamplecodetoimplementstringtonumericconversion.

#stringtonumeric

a="2"

b="6.8"

num1=int(a)

num2=float(b)

print(num1)

print(num2)

Page 82: Python Programming by Example

7.4NumerictoString

ItiseasytoconvertnumerictoStringtype,youcanusestr().Youcangetstringtypeautomatically.

#numerictostring

a=6

b=8.56

str1=str(a)

str2=str(b)

print(str1)

print(str2)

Page 83: Python Programming by Example

7.5StringParser

ThesimplesolutiontoparsingStringusessplit()withdelimiterparameter.Forexample,youhaveStringdatawith;delimiterandwanttoparseit.Hereissamplecode

#parsing

msg='Berlin;Amsterdam;London;Tokyo'

cities=msg.split(';')

forcityincities:

print(city)

Page 84: Python Programming by Example

7.6CheckStringDataLength

Youcanuselen()togetthelengthofdata.

msg='Helloworld,Python!'

#getalengthofstring

length=len(msg)

print(length)

Page 85: Python Programming by Example

7.7CopyData

YoumaycopysomecharactersfromStringdata.Todoit,youcanuse[start:end]syntax.Hereissyntaxformat:

msg='Helloworld,Python!'

#copy

print(msg[5:])

print(msg[:5])

print(msg[-3:])

print(msg[:-3])

print(msg[2:6])

print(msg[5:8])

Page 86: Python Programming by Example

7.8UpperandLowerCaseCharacters

Insomesituation,youwanttogetallstringdatainupperorlowercasecharacters.ThisfeatureisbuiltinStringobject.upper()functionisusedtomakewholestringinuppercaseandlower()isusedtomakewholestringinlowercase.

Thefollowingisasamplecodetogetupperandlowercasecharacters.

msg='Helloworld,Python!'

#upper&lower

print(msg.upper())

print(msg.lower())

Page 87: Python Programming by Example

7.9TestingAProgram

Wecanwriteourcodeinch17_01.pycompletelyasfollows.

str1="helloworld"

str2="python"

#Concatenating

print(str1+""+str2)

print(str1,str2)

print("%s%s"%(str1,str2))

print("{}{}".format(str1,str2))

#stringtonumeric

a="2"

b="6.8"

num1=int(a)

num2=float(b)

print(num1)

print(num2)

#numerictostring

a=6

b=8.56

str1=str(a)

str2=str(b)

print(str1)

print(str2)

#parsing

msg='Berlin;Amsterdam;London;Tokyo'

cities=msg.split(';')

forcityincities:

print(city)

#stringoperations

msg='Helloworld,Python!'

Page 88: Python Programming by Example

#upper&lower

print(msg.upper())

print(msg.lower())

#copy

print(msg[5:])

print(msg[:5])

print(msg[-3:])

print(msg[:-3])

print(msg[2:6])

print(msg[5:8])

#getalengthofstring

length=len(msg)

print(length)

Savethescriptandruntheprogram.

$python3ch07_01.py

Programoutput:

Page 89: Python Programming by Example
Page 90: Python Programming by Example

8.FileOperations

ThischapterexplainshowtoworkwithfileoperationsusingPython.

Page 91: Python Programming by Example

8.1GettingStarted

WecanworkwithI/Ofileusingiopackage,https://docs.python.org/3/library/io.html.

ThenextstepistobuildPythonapplicationtowriteandreadafile.

Page 92: Python Programming by Example

8.2WritingDataIntoAFile

Towriteandreadafile,wecanuseiopackage.Inthissection,wetrytowritedataintoafile.

8.2.1CreatingaFile

Wecancreateafileusingopen()functionwithparameter"w".Iffileisexist,itwillrecreateafile.

Ifyouwanttousetheexistingfile,youcanpass"a".Parameter"b"isusedforbinaryfile.

#createafile.

#Iffileisexisting,iterasesandcreatesanewone

f1=open('mydoc1','w')

#createafile.

#Iffileisexisting,itappends.Otherwise,itcreates

f2=open('mydoc2','a')

#binaryfiles

bf1=open('mydoc3','wb')

bf2=open('mydoc4','ab')

8.2.2WritingData

Writedataintoafile,wecanusewrite()function.

forindexinrange(1,12):

data=''

name='user'+str(index-1)

Page 93: Python Programming by Example

email='user'+str(index-1)+'@email.com'

ifindex==1:

data='{0:3s}{1:10s}{2:15s}\n'.format('No',

'Name','Email')

else:

data='{0:3s}{1:10s}{2:15s}\n'.format(str(index-

1),name,email)

f1.write(data)

f2.write(data)

bf1.write(data)

bf2.write(data)

8.2.3ClosingaFile

Iffileoperationsdone,youshouldcallclose()toclosefile.

f1.close()

f2.close()

bf1.close()

bf2.close()

8.2.4Demo

Let'swritethesescriptsfordemo.

#####################################

print('creatingfiles...')

#createafile.

#Iffileisexisting,iterasesandcreatesanewone

f1=open('mydoc1','w')

#createafile.

#Iffileisexisting,itappends.Otherwise,itcreates

f2=open('mydoc2','a')

Page 94: Python Programming by Example

#binaryfiles

bf1=open('mydoc3','wb')

bf2=open('mydoc4','ab')

#####################################

#writingdata

print('writingdataintofiles...')

forindexinrange(1,12):

data=''

name='user'+str(index-1)

email='user'+str(index-1)+'@email.com'

ifindex==1:

data='{0:3s}{1:10s}{2:15s}\n'.format('No',

'Name','Email')

else:

data='{0:3s}{1:10s}{2:15s}\n'.format(str(index-

1),name,email)

f1.write(data)

f2.write(data)

bf1.write(data)

bf2.write(data)

#####################################

#closeall

print('closefiles...')

f1.close()

f2.close()

bf1.close()

bf2.close()

Saveintoafile,calledch08_01.py.Then,runtheprogram.

$python3ch08_01.py

Page 95: Python Programming by Example

Programoutput:

Ifsuccess,youcanopenallfilestoseethecontent.

Asampleofcontentfrommydoc1filecanbeseeninFigurebelow.

Page 96: Python Programming by Example

Asampleofcontentfrommydoc3filecanbeseeninFigurebelow.

Page 97: Python Programming by Example

8.3ReadingDataFromAFile

Toreaddataperlinefromafile,weusereadline()function.

Writethesescriptsfordemo.

importsys

#####################################

print('openingfiles...')

f1=open('mydoc1','r')

f2=open('mydoc2','r')

bf1=open('mydoc3','rb')

bf2=open('mydoc4','rb')

#####################################

#readingdata

defreading_data(f):

whileTrue:

data=f.readline()

if(data=='')or(data==None):

break

sys.stdout.write(data)

print('formydoc1>>>>>')

reading_data(f1)

print('>>>>>>>>>>>>>>>')

print('formydoc2>>>>>')

reading_data(f2)

print('>>>>>>>>>>>>>>>')

print('formydoc3>>>>>')

reading_data(bf1)

print('>>>>>>>>>>>>>>>')

print('formydoc4>>>>>')

reading_data(bf1)

print('>>>>>>>>>>>>>>>')

Page 98: Python Programming by Example

#####################################

#closeall

print('closefiles...')

f1.close()

f2.close()

bf1.close()

bf2.close()

Saveintoafile,calledch08_02.py.Then,runtheprogram.

$python3ch08_02.py

Programoutput:

Page 99: Python Programming by Example

9.ErrorHandling

ThischapterexplainshowtohandleerrorsandexceptionsthatoccurinPythonapplication.

Page 100: Python Programming by Example

9.1ErrorHandling

Basicallywhenwewriteaprogramandoccurerroronrunning,Pythonwillcatchprogramerror.

Trytowritethesescriptsandrunthisprogram.

a=18

b=0

c=a/b

$python3ch09_01.py

Youshouldgeterror,showninFigurebelow.

Nowwecancatcherrorusingtry..except.Youcanreadhowtouseitonhttps://docs.python.org/3/tutorial/errors.html.

Writethesescripts.

Page 101: Python Programming by Example

Writethesescripts.

try:

a=18

b=0

c=a/b

print('result:',str(c))

exceptZeroDivisionErrorase:

print('Error:divisionbyzero')

print(e)

finally:

print('Done')

print('exitfromprogram')

Saveintoafile,ch09_02.py.Then,runthefile.

$python3ch09_02.py

Youcanseetheprogramcanhandletheerror.

Page 102: Python Programming by Example

9.2CatchingAllErrors

Onprevioussection,wecatcherrorfor"divisionbyerror".WecancatchallerrorsusingExceptionobject.Writethesescriptsfordemo.

try:

a=18

b=0

c=a/b

print('result:',str(c))

exceptExceptionase:

print(e)

finally:

print('Done')

print('exitfromprogram')

Saveintoafile,calledch09_03.py.Runtheprogram.

$python3ch09_03.py

Programoutput:

Page 103: Python Programming by Example
Page 104: Python Programming by Example

9.3RaisingExceptions

Wecanraiseerrorfromourprogramusingraise.

Fordemo,writethesescripts.

try:

a=18

b=0

c=a/b

print('result:',str(c))

exceptExceptionase:

raise

finally:

print('Done')

#thiscodeisnevercalled

print('exitfromprogram')

Saveintoafile,calledch09_04.py.Then,runtheprogram.

$python3ch09_04.py

Youshouldtheprogramraisetheerrorsowedon'twords"exitfromprogram".

Page 105: Python Programming by Example
Page 106: Python Programming by Example

9.4CustomException

WecanbuildownerrorwithimplementinginheritanceException.

Forinstance,wecreateaclass,MySimpleError,withinheritancefromException.

classMySimpleError(Exception):

def__init__(self,code,message):

self.code=code

self.message=message

def__str__(self):

returnrepr(str(self.code)+":"+self.message)

defsave_to_database(self):

print('savethiserrorintodatabase..')

#howtousecustomerror

try:

print('democustomerror')

print('raiseerrornow')

raiseMySimpleError(100,'Thisiscustomerror')

exceptMySimpleErrorase:

print(e)

e.save_to_database()

Savetheprogramintoafile,calledch09_05.py.

Runthisprogram.

$python3ch09_05.py

Programoutput:

Page 107: Python Programming by Example
Page 108: Python Programming by Example

10.BuildingOwnPythonModule

ThischapterexplainshowtobuildownPythonmodule.

Page 109: Python Programming by Example

10.1CreatingSimpleModule

Inthissection,wecreateasimplemodule.Wewillcallfunctionsfromexternalfile(*.py)inthesamepackage,mainpackage.

Firstly,createafile,simplemodule.py,andwritethesescripts.

defperform(a,b):

returna*2.5+b

defcalculate(a,b):

returna+b*5

YoualsocancreateaclassonPythonfile,forinstancesimpleadvmodule.pyandwritethesescripts.

frommathimport*

classCity:

#classdata

city_count=0

city_id=0

#constructor

def__init__(self,name='',x=0,y=0):

self.name=name

self.x=x

self.y=y

City.city_count+=1#accessallCityclasses

self.city_id=City.city_count

def__str__(self):

return'City:'+self.name+',id='+

str(self.city_id)+',x='+str(self.x)+',y='+str(self.y)

#classattributes

defmove_to(self,x=0,y=0):

self.x+=x

Page 110: Python Programming by Example

self.y+=y

defdistance(self,other_city):

xi=pow(other_city.x-self.x,2)

yi=pow(other_city.y-self.y,2)

returnsqrt(xi+yi)

def__del__(self):

#getclassname

class_name=self.__class__.__name__

print('class',class_name,'destroyed')

Nowyouaccessfunctionsfromsimplemodule.pyandsimpleadvmodule.pyfilesusingimportstatement.

Writethesescripts.

#accessourmodules

importsimplemodule

importsimpleadvmodule

#usesimplemodule

num1=simplemodule.perform(10,5)

print(num1)

num1=simplemodule.calculate(4,3)

print(num1)

#usesimpleadvmodule

city_a=simpleadvmodule.City('Hamburg',8,12)

city_b=simpleadvmodule.City('Berlin',5,7)

print(city_a)

print(city_b)

city_a.move_to(4,3)

city_b.move_to(7,12)

print(city_a)

print(city_b)

Saveintoafile,calledch10_01.py.Runtheprogram.

Page 111: Python Programming by Example

Saveintoafile,calledch10_01.py.Runtheprogram.

$python3ch10_01.py

Programoutput:

Page 112: Python Programming by Example

10.2BuildingOwnPythonPackage

Inpreviousprogram,wecreateamoduleinPythonpackage.Wecreateapackageandthenuseitinourprogram.

Youcancreateapackage,calledLearning,bycreatingfolder.InsideLearningfolder,youcreatefolders:AlgebraandArithmetic.

Thefollowingisourpackagestructure.

Nowweaddseveralfilesineachfolder

Learningfolder:__init__.py,common.py,Computer.pyandfoo.pyLearning/Algebrafolder:__init__.pyandadd.pyLearning/Arithmeticfolder:__init__.pyandcalculate.py

Thefollowingisscriptimplementationforeachfile.

common.py

defdo_something():

print('calldo_something()')

Page 113: Python Programming by Example

Computer.py

classComputer:

#constructor

def__init__(self,name=''):

self.name=name

def__str__(self):

return'Computer:'+self.name

defsay_hello(self):

print("I'mcomputer,called",

self.name)

foo.py

deffoo():

print('callfoo()')

__init__.pyfromLearningfolder.

fromcommonimportdo_something

fromComputerimportComputer

fromfooimportfoo

add.py

defadd(a,b):

Page 114: Python Programming by Example

returna+b

__init__.pyfromLearning/Algebrafolder.

fromLearning.Algebra.addimportadd

calculate.py

defcalculate(a,b):

returna+b*2.8

__init__.pyfromLearning/Arithmeticfolder.

fromLearning.Arithmetic.calculateimportcalculate

Nowwecanaccessourpackage.Createafile,calledch10_02.py,andwritethesescripts.

importsys

sys.path.append('./Learning')

importLearning

Learning.foo()

Learning.do_something()

a=Learning.Computer('myPC')

a.say_hello()

importLearning.Algebraasalgebra

b=algebra.add(10,5)

print(b)

Page 115: Python Programming by Example

importLearning.Arithmeticasarith

c=arith.calculate(5,8)

print(c)

Saveandruntheprogram.

$python3ch10_02.py

Programoutput:

Page 116: Python Programming by Example

11.Concurrency

ThischapterexplainshowtocreateconcurrencyinPython

Page 117: Python Programming by Example

11.1GettingStarted

WecanrunaprogramorafunctioninbackgroundusingPython.Inthischapter,weexploreseveralscenariostobuildconcurrencyapplication.InPython,youcanreadconcurrencyonthiswebsite,https://docs.python.org/3/library/concurrency.html.

Page 118: Python Programming by Example

11.2Threading

Basically,wecanimplementthreadingusingThreadwithpassingthefunction.Youcancallstart()torunathread.

importtime

importthreading

globalrunning

defperform():

globalrunning

counter=0

running=True

whilerunning:

print('counter:',str(counter))

time.sleep(2)

counter+=1

my_thread=threading.Thread(target=perform)

my_thread.setDaemon(True)

my_thread.start()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

running=False

my_thread.join(2)

Toexitfromathread,wecancalljoin()withtimeoutvalue.

Saveintoafile,calledch11_01.py.

Runtheprogram.

Page 119: Python Programming by Example

$python3ch11_01.py

WhenThread.start()iscalled,itexecutesperform()function.

AsampleofprogramoutputcanbeseeninFigurebelow.

Threadobjectcanbeimplementedbyaderivedclassfromthreading.Thread.Forinstance,wecreateaclass,MyThread.Ithasinheritancefromthreading.Threadandimplementrun()function.

Writethesescripts.

importtime

importthreading

classMyThread(threading.Thread):

def__init__(self):

threading.Thread.__init__(self)

self.running=False

defrun(self):

Page 120: Python Programming by Example

counter=0

self.running=True

whileself.running:

print('counter:',str(counter))

time.sleep(2)

counter+=1

defstop(self):

print('stoppingthread...')

self.running=False

self.join(2)

my_thread=MyThread()

my_thread.setDaemon(True)

my_thread.start()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

my_thread.stop()

Savethesescriptsintoafile,calledch11_02.py.Youcanseeourobject,my_thread,callstart()then,itcallrun().Wecallstop()functiontostopourthread.Basically,MyThreadwillcalljoin()whilecalledstop()function.

Nowyoucanruntheprogram.

$python3ch11_02.py

Asampleofprogramoutput:

Page 121: Python Programming by Example
Page 122: Python Programming by Example

11.3SynchronizingThreads

WecansynchronizeamongbackgroundcodesinPython.Inthissection,weusemutexlockandeventforthreadsynchronization.

Let'sstart.

11.3.1MutexLocks

Theideaisasimple.Whenweaccessaresource,wecallacquire().Ifdone,youcallrelease()fromLockobject.

Fortesting,wedefineasharedresource,suchasavariablecalledvalue.Thisvariablewillbeaccessbytwothreads.Onlyonethreadcanaccessthisvariable.

Ok,writethesescripts.

importtime

importthreading

classMyThread(threading.Thread):

def__init__(self,name,o_lock):

threading.Thread.__init__(self)

self.name=name

self.running=False

self.value_lock=o_lock

defrun(self):

globalvalue

self.running=True

whileself.running:

self.value_lock.acquire()

value+=1

print('value:',str(value),'from',self.name)

self.value_lock.release()

time.sleep(2)

Page 123: Python Programming by Example

defstop(self):

print('stopping',self.name)

self.running=False

self.join(2)

globalvalue

value=0

value_lock=threading.Lock()

my_thread1=MyThread('Thread1',value_lock)

my_thread1.setDaemon(True)

my_thread2=MyThread('Thread2',value_lock)

my_thread2.setDaemon(True)

my_thread1.start()

my_thread2.start()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

my_thread1.stop()

my_thread2.stop()

Saveintoafile,calledch11_03.py.

Nowyoucanruntheprogram.

$python3ch11_03.py

Programoutput:

Page 124: Python Programming by Example

11.3.2Event

Anotheroptiontosynchthreading,wecanuseEventobject.Callwait()toblockoperation.Itmeanstheprogramcan'texecutecodesafterwait().Then,callset()toreleasetheblockingprocess.

Forillustration,wecreatethreeworkerthreads.Thesethreadswillperformsomethingaftercallingset()fromevent.Thisisusefulforinitializationstateprocess.

Writethesescripts.

importtime

importthreading

classWorker(threading.Thread):

def__init__(self,name,signal):

threading.Thread.__init__(self)

self.name=name

self.signal=signal

defrun(self):

Page 125: Python Programming by Example

print('waitingfrom',self.name)

self.signal.wait()

print('processingfrom',self.name)

time.sleep(2)

print('donefrom',self.name)

signal_event=threading.Event()

my_thread1=Worker('Thread1',signal_event)

my_thread1.setDaemon(True)

my_thread2=Worker('Thread2',signal_event)

my_thread2.setDaemon(True)

my_thread3=Worker('Thread3',signal_event)

my_thread3.setDaemon(True)

my_thread1.start()

my_thread2.start()

my_thread3.start()

#waitingfor10seconds

time.sleep(10)

#startprocess

print('Sendasignaltostartprocessing')

signal_event.set()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

print('Doneall')

Saveintoafile,calledch11_04.py.Then,runtheprogram.

$python3ch11_04.py

Programoutput:

Page 126: Python Programming by Example
Page 127: Python Programming by Example

11.4Queue

Inthissection,welearnaboutQueueobjectfromPython,https://docs.python.org/3/library/queue.html.

Fortesting,weaddsomejobsintoQueue.Then,someworkerthreadswillpeakthejobandrunit.

Let'swritethesescripts.

importtime

importthreading

importqueue

classWorker(threading.Thread):

def__init__(self,name,q):

threading.Thread.__init__(self)

self.name=name

self.q=q

defrun(self):

whileTrue:

ifself.q.empty():

print('threadstopped')

break

job=self.q.get()

print('runjob',str(job),'from',self.name)

time.sleep(1)

self.q.task_done()

q=queue.Queue()

#generatejobs

print('populatejobs')

foriinrange(15):

q.put(i)

my_thread1=Worker('Thread1',q)

my_thread1.setDaemon(True)

my_thread2=Worker('Thread2',q)

my_thread2.setDaemon(True)

Page 128: Python Programming by Example

my_thread3=Worker('Thread3',q)

my_thread3.setDaemon(True)

my_thread1.start()

my_thread2.start()

my_thread3.start()

my_thread1.join()

my_thread2.join()

my_thread3.join()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

print('Doneall')

Saveintoafile,calledch11_05.py.

Nowyoucanruntheprogram.

$python3ch11_05.py

Programoutput:

Page 129: Python Programming by Example
Page 130: Python Programming by Example

11.5Multiprocessing

WecanimplementconcurrencyinPythonusingmultiprocessing.Youcanreadtheinformationaboutthisonthissite,https://docs.python.org/3/library/multiprocessing.html.

11.5.1Process

WecanuseProcessobjecttoimplementmultiprocessinginPython.Forinstance,webuildacounterfromaprocess.

Writethesescripts.

importtime

importmultiprocessing

classMyProcess(multiprocessing.Process):

def__init__(self):

multiprocessing.Process.__init__(self)

self.running=False

defrun(self):

counter=0

self.running=True

whileself.running:

print('counter:',str(counter))

time.sleep(2)

counter+=1

defstop(self):

print('stoppingprocess...')

self.running=False

self.join(1)

my_process=MyProcess()

my_process.daemon=True

my_process.start()

Page 131: Python Programming by Example

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

my_process.stop()

Saveintoafile,calledch11_06.py,andrunit.

$python3ch11_06.py

Programoutput:

11.5.2SynchronizingProcesses

Wecansynchamongprocessesusingmultiprocessing.Value.Thisobjectimplementsynchronizingprocess.

Fortesting,wedefineasharedresourceviamultiprocessing.Value.Writethese

Page 132: Python Programming by Example

Fortesting,wedefineasharedresourceviamultiprocessing.Value.Writethesescripts.

importtime

importmultiprocessing

classMyProcess(multiprocessing.Process):

def__init__(self,name,shared_dt):

multiprocessing.Process.__init__(self)

self.name=name

self.running=False

self.shared_data=shared_dt

defrun(self):

self.running=True

whileself.running:

time.sleep(1)

withself.shared_data.get_lock():

self.shared_data.value+=1

print('value:',str(self.shared_data.value),

'from',self.name)

defstop(self):

print('stopping',self.name)

self.running=False

self.join(1)

shared_data=multiprocessing.Value('i',0,lock=True)

my_process1=MyProcess('Process1',shared_data)

my_process1.daemon=True

my_process2=MyProcess('Process2',shared_data)

my_process2.daemon=True

my_process1.start()

my_process2.start()

#python3

input("PressEntertostop...")

#python2

#raw_input("PressEntertostop...")

my_process1.stop()

Page 133: Python Programming by Example

my_process2.stop()

Savetheprogramintoafile,calledch11_07.py.

Nowyoucanruntheprogram.

$python3ch11_07.py

Programoutput:

Page 134: Python Programming by Example

11.6ParallelTasks

Thelastsectionistoimplementparalleltasksusingconcurrent.futures.Therearetwooptionstoimplementthis:ThreadPoolExecutorandProcessPoolExecutor.

11.6.1ThreadPoolExecutor

ThreadPoolExecutorusesthreadtodoparalleltasks.

Asampleofscriptforparalleltaskscanbewrittenthefollowingscript.

importqueue

importconcurrent.futures

importrandom

importtime

importdatetime

defperform(q,a,b,c):

rand_val=random.uniform(0,2)

res=a*b*10-c*2

time.sleep(rand_val)

q.put(res)

t1=datetime.datetime.now()

q=queue.Queue()

withconcurrent.futures.ThreadPoolExecutor(max_workers=3)as

executor:

foriinrange(1,15):

val_a=random.randint(1,10)

val_b=random.randint(1,10)

val_c=random.randint(1,10)

executor.submit(perform,q,val_a,val_b,val_c)

print('Printresults')

t2=datetime.datetime.now()

Page 135: Python Programming by Example

whilenotq.empty():

print(q.get())

t=t2-t1

print('totaltime:',str(t.total_seconds()),'seconds')

Saveintoafile,calledch11_08.py.

$python3ch11_08.py

Programoutput:

11.6.2ProcessPoolExecutor

ProcessPoolExecutorusesprocesstodoparalleltasks.

Page 136: Python Programming by Example

ProcessPoolExecutorusesprocesstodoparalleltasks.

Weimplementthesamescenariofromprevioussection.

importmultiprocessing

importconcurrent.futures

importrandom

importtime

importdatetime

defperform(q,a,b,c):

rand_val=random.uniform(0,2)

res=a*b*10-c*2

time.sleep(rand_val)

q.put(res)

t1=datetime.datetime.now()

m=multiprocessing.Manager()

q=m.Queue()

withconcurrent.futures.ProcessPoolExecutor(max_workers=3)as

executor:

foriinrange(1,15):

val_a=random.randint(1,10)

val_b=random.randint(1,10)

val_c=random.randint(1,10)

executor.submit(perform,q,val_a,val_b,val_c)

print('Printresults')

t2=datetime.datetime.now()

whilenotq.empty():

print(q.get())

t=t2-t1

print('totaltime:',str(t.total_seconds()),'seconds')

Savethesescriptsintoafile,calledch11_09.py.

$python3ch11_09.py

Page 137: Python Programming by Example

Programoutput:

Page 138: Python Programming by Example

12.Encoding

ThischapterexplainshowtoworkwithencodinginPython.

Page 139: Python Programming by Example

12.1GettingStarted

Inthischapter,weexploreencodingpackagefromPython.Thefollowingisalistofourdemotoillustratehowtouseencodingpackage:

Base64HexadecimalJSONXMLCSV

Let'sstarttoimplementtheseencoding.

Page 140: Python Programming by Example

12.2EncodingBase64

Thefirstdemoistoworkwithbase64encoding.Wecanusebase64package,https://docs.python.org/3/library/base64.html.Toencodestringtobase64string,wecanuseb64encode().Otherwise,wecandecodeitusingb64decode()function.

Fortesting,weencodeastringmessagetobase64.Then,wedecodebase64messagetooriginalmessage.Writethesescripts.

importbase64

plaintext='HelloworldfromPython'

s_bytes=plaintext.encode()

enc1=base64.b64encode(s_bytes)

dec1=base64.b64decode(enc1)

s_dec1=dec1.decode()

print('Plaintext:',plaintext)

print('Base64:',enc1)

print('Decoded:',s_dec1)

Saveintoafile,calledch12_01.py.

Nowyoucantesttobuildandruntheprogram.

$python3ch12_01.py

AsampleoutputcanbeseeninFigurebelow.

Page 141: Python Programming by Example
Page 142: Python Programming by Example

12.3Hexadecimal

TheseconddemoistoencodeanddecodestringtoHexadecimal.WecanuseencodeanddecodefromPython3.x,toimplementourdemo.

Createafile,calledch12_02.py.Thefollowingisimplementationofencoding/decodingHexadecimal.

fromcodecsimportencode,decode

#declarehexdata

num=0x64

print(num,'-->',chr(num))

num_s="\x64"

print(num_s)

#displayhexfromstringdata

s='HelloworldfromPython'

s_bytes=s.encode()

s_hex=encode(s_bytes,'hex')

s_decoded=decode(s_hex,'hex')

s_plaintext=s_decoded.decode()

print('plaintext:',s)

print('hex:',s_hex)

print('decoded:',s_plaintext)

#samplesfordisplayinghexdata

print('displayhexformat')

forcins:

print(c,'-->',encode(c.encode(),'hex'))

hex2=":".join("{:02x}".format(c)forcins_bytes)

print(hex2)

Savethiscode.Nowyoucanbuildandrunthisprogram.

Page 143: Python Programming by Example

$python3ch12_02.py

AsampleoutputcanbeseeninFigurebelow.

Page 144: Python Programming by Example

12.4JSON

ThethirddemoistoconstructandparseJSONdata.InPython,wecanusejsonpackage,https://docs.python.org/3/library/json.html.

Fordemo,createafile,calledch12_02.pyandwritethiscode.

importjson

importtime

#constructjson

data={

'name':'anna',

'sex':'woman',

'age':20,

'country':'germany'

}

blog={

'title':'myblog',

'created':time.time(),

'comments':[

{'name':'user1','comment':'thisiscomment1'},

{'name':'user2','comment':'thisiscomment2'},

{'name':'user3','comment':'thisiscomment3'}

]

}

#jsonobjecttojsonstring

json_data=json.dumps(data)

json_data2=json.dumps(blog)

print(json_data)

print(json_data2)

#decodejsonstringtojsonobject

#youdefinejsonstringorloadjsonstringfromfile

json_o1=json.loads(json_data)

json_o2=json.loads(json_data2)

#iterationjsonvalues

print('----json_o1---')

print(json_o1['name'])

print(json_o1['sex'])

print(json_o1['age'])

Page 145: Python Programming by Example

print(json_o1['country'])

print('----json_o2---')

print(json_o2['title'])

created_s=time.strftime("%b%d%Y%H:%M:%S",

time.gmtime(json_o2['created']))

print(created_s)

print('comments:')

forcommentinjson_o2['comments']:

print('---',comment['name'],':',comment['comment'])

Savethiscode.Nowyoucanbuildandrunthisprogram.

$python3ch12_03.py

AsampleoutputcanbeseeninFigurebelow.

Page 146: Python Programming by Example

12.5XML

ThefourthdemoistoreadandwriteXMLdata.Wecanusexmlpackage,https://docs.python.org/3/library/xml.html.Inthisdemo,weusexml.etree.ElementTreetoprocessXMLdata.Inthisdemo,wereadxmlfileandcreateanewxmlfile.

Firstly,wecreatexmlfilefortesting,calledproducts.xmlandwritethisdata.

<?xmlversion="1.0"?>

<products>

<productname="product1">

<code>1001</code>

<year>2015</year>

<modelcolor="red"category="food">12E</model>

</product>

<productname="product2">

<code>1002</code>

<year>2015</year>

<modelcolor="green"category="beverage">15C</model>

</product>

<productname="product3">

<code>1003</code>

<year>2015</year>

<modelcolor="blue"

category="electronics">19A</model>

</product>

</products>

NowwereadxmlfileanddisplayitintoTerminal.

importxml.etree.ElementTreeasEt

#loadxmlfileanditerate

xml_tree=Et.parse('products.xml')

products=xml_tree.getroot()

print(products.tag)

forproductinproducts:

print('',product.tag,'name=',product.get('name'))

forproduct_iteminproduct:

Page 147: Python Programming by Example

print('',product_item.tag,'=',product_item.text)

#findingspecificdata

print('----------')

forcodeinproducts.iter('code'):

print(code.text)

#constructxmlandsaveintoafile

print('constructxmlfile')

users=Et.Element('users')

foriinrange(1,5):

user=Et.SubElement(users,'user')

user.set('name',"User"+str(i))

user_item=Et.SubElement(user,'age')

user_item.text=str(i*3)

user_item2=Et.SubElement(user,'id')

user_item2.text="1203"+str(i)

print('writeintoxmlfile')

tree=Et.ElementTree(users)

tree.write("users.xml")

Savethiscodeintoafiile,calledch12_04.py.Nowyoucanrunthisprogram.

$python3ch12_04.py

Ifsuccess,youseeusers.xmlfileandyoushouldseethecontentasfollows.

<users>

<username="User1"><age>3</age><id>12031</id></user>

<username="User2"><age>6</age><id>12032</id></user>

<username="User3"><age>9</age><id>12033</id></user>

<username="User4"><age>12</age><id>12034</id></user>

</users>

Page 148: Python Programming by Example

AsampleofprogramoutputcanbeseeninFigurebelow.

Page 149: Python Programming by Example
Page 150: Python Programming by Example

12.6CSV

ThelastdemoistoreadandwritedataCSVwhichisacollectionofcomma-separateddata.WecanaccessCSVfileusingcsvpackage,https://docs.python.org/3/library/csv.html.Nowwebuildaprogramtoreadcsvfileandwritedataintocsvfile.

Fortesting,wecreateaCSVfile,customers.csv,withthefollowingcontent.

id,full_name,age,country

12,JamesButt,23,US

13,JosephineDarakjy,40,UK

14,ArtVenere,35,US

15,LennaPaprocki,34,DE

16,DonetteFoller,27,NL

Thefollowingisimplementationofreading/writingCSVfile.

importcsv

#readingcsvfile

withopen('customers.csv',newline='')ascsv_file:

customers=csv.reader(csv_file,delimiter=',')

forrowincustomers:

print(','.join(row))

csv_file.close()

print('------------')

#readingcsvfilewithhandlingheader

withopen('customers.csv')ascsv_file:

reader=csv.DictReader(csv_file)

forrowinreader:

print(row['id'],row['full_name'],row['age'],

row['country'])

csv_file.close()

#writingcsvfile

print('----------------')

print('writingcsvfile')

Page 151: Python Programming by Example

withopen('cities.csv','w')ascsv_file:

fieldnames=['id','name','country']

writer=csv.DictWriter(csv_file,fieldnames=fieldnames,

delimiter=';')

writer.writeheader()

foriinrange(1,10):

writer.writerow({'id':i,'name':"city"+str(i),

'country':"country"+str(i)})

csv_file.close()

print('done')

Note:YoucanchangeCSVfilepath.

Savethiscodeintoafile,calledch12_05.py.Nowyoucanrunthisprogram.

$python3ch12_05.py

AsampleoutputcanbeseeninFigurebelow.

Page 152: Python Programming by Example

Theprogramalsogeneratecities.csvfile.

Ifyouopencities.csv,yougetacontentofcitiesdatalikeacontentofcustomers.csvfile.

Page 153: Python Programming by Example
Page 154: Python Programming by Example

13.HashingandCryptography

ThischapterexplainshowtoworkwithhashingandcryptographyinPython.

Page 155: Python Programming by Example

13.1GettingStarted

Hashingisgeneratingavalueorvaluesfromastringoftextusingamathematicalfunction.Cryptographyisthepracticeandstudyoftechniquesforsecurecommunicationinthepresenceofthirdparties(calledadversaries),http://en.wikipedia.org/wiki/Cryptography.Inthischapter,Idon'texplainmathematicalhashingandCryptography.Youcanreadthosematerialsontextbooks.

Inthischapter,weexplorehowtoworkwithhashingimplementationusingPython.Thefollowingishashingalgorithmswhichweuseinthisbook:

MD5SHA1andSHA256HashingwithKey(HMAC)

ThenexttopicistoimplementCryptographyusingPython.WeexploresymmetricandasymmetricCryptography.

Page 156: Python Programming by Example

13.2Hashing

Basically,youcanexplorehowtoimplementhashingorhashfunctionusingPythonviahttps://docs.python.org/3/library/hashlib.html.Wealsouseopensourcelibrary,calledpycrypto,https://pypi.python.org/pypi/pycrypto.Weimplementbothinourcase.

Inthissection,weexploreseveralhashingalgorithms,forinstance,MD5,SHA1,SHA256andHMAC.

13.2.1HashingwithMD5

WecanuseMD5usingmd5package,https://docs.python.org/3/library/hashlib.html.Tocalculateahashvaluefromatext,wecancalldigest()function.

Forillustration,wedohashingaplaintext.

importhashlib

importbinascii

plaintext='helloworldfrompython'

#md5

md5=hashlib.md5()

md5.update(plaintext.encode())

hash_md5=md5.digest()

hex_hash_md5=md5.hexdigest()

print('hashmd5:',hash_md5)

print('hexhashmd5:',hex_hash_md5)

digest()isusedtocalculateahashvalue.hexdigest()istocalculatehexhash.

Page 157: Python Programming by Example

13.2.2HashingwithSHA1andSHA256

Theseconddemoistoimplementhashfunctionusingsha1andsha256.

Forillustration,writethesescriptforhashingSHA1andSHA256.

importhashlib

importbinascii

plaintext='helloworldfrompython'

#sha1

sha1=hashlib.sha1()

sha1.update(plaintext.encode())

hash_sha1=sha1.digest()

hex_hash_sha1=sha1.hexdigest()

print('hashsha1:',hash_sha1)

print('hexhashsha1:',hex_hash_sha1)

#sha256

sha256=hashlib.sha256()

sha256.update(plaintext.encode())

hash_sha256=sha256.digest()

hex_hash_sha256=sha256.hexdigest()

print('hashsha256:',hash_sha256)

print('hexhashsha256:',hex_hash_sha256)

13.2.3HashingwithKeyUsingHMAC

Akeyed-hashmessageauthenticationcode(HMAC)isaspecificconstructionforcalculatingamessageauthenticationcode(MAC)involvingacryptographichashfunctionincombinationwithasecretcryptographickey,http://en.wikipedia.org/wiki/Hash-based_message_authentication_code.InPython,weusepbkdf2_hmac()object.

Forillustration,wedohashingaplaintextwithkey.

importhashlib

Page 158: Python Programming by Example

importbinascii

plaintext='helloworldfrompython'

#hashwithkey

#hmac

key='p4ssw0rd'

hmac=hashlib.pbkdf2_hmac('sha256',key.encode(),

plaintext.encode(),100000)

hex_hash_hmac=binascii.hexlify(hmac)

print('hexhashhmac:',hex_hash_hmac)

13.2.4WritethemAll

Saveallcodeforourdemoonthissection.Writethiscodeandsaveintoafile,calledch13_01.py.

importhashlib

importbinascii

plaintext='helloworldfrompython'

#md5

md5=hashlib.md5()

md5.update(plaintext.encode())

hash_md5=md5.digest()

hex_hash_md5=md5.hexdigest()

print('hashmd5:',hash_md5)

print('hexhashmd5:',hex_hash_md5)

#sha1

sha1=hashlib.sha1()

sha1.update(plaintext.encode())

hash_sha1=sha1.digest()

hex_hash_sha1=sha1.hexdigest()

print('hashsha1:',hash_sha1)

print('hexhashsha1:',hex_hash_sha1)

#sha256

sha256=hashlib.sha256()

sha256.update(plaintext.encode())

hash_sha256=sha256.digest()

Page 159: Python Programming by Example

hex_hash_sha256=sha256.hexdigest()

print('hashsha256:',hash_sha256)

print('hexhashsha256:',hex_hash_sha256)

#hashwithkey

#hmac

key='p4ssw0rd'

hmac=hashlib.pbkdf2_hmac('sha256',key.encode(),

plaintext.encode(),100000)

hex_hash_hmac=binascii.hexlify(hmac)

print('hexhashhmac:',hex_hash_hmac)

Saveall.

Nowyoucantesttobuildandruntheprogram.

$python3ch13_01.py

AsampleoutputcanbeseeninFigurebelow.

Page 160: Python Programming by Example

13.3Cryptography

Inthissection,wefocusSymmetricandAsymmetricCryptography.InSymmetricCryptography,weusethesamekeytoencryptanddecrypt.Otherwise,AsymmetricCryptographyusesdifferentkeytoencryptanddecrypt.

Weusepycrypto,https://github.com/dlitz/pycrypto.Youcaninstallitviapip.AsampleofcommandtoinstallpycryptoforPython3.x.

$pip3installpycrypto

Forillustration,wehashaplaintextusingSHA256.Writethesescriptsandsaveintoafile,calledch13_02.py.

fromCrypto.HashimportSHA256

plaintext='helloworldfrompython'

Page 161: Python Programming by Example

sha256=SHA256.new()

sha256.update(plaintext.encode())

hash_sha256=sha256.digest()

hex_hash_sha256=sha256.hexdigest()

print('hashsha256:',hash_sha256)

print('hexhashsha256:',hex_hash_sha256)

Saveandruntheprogram.

$python3ch13_02.py

Programoutput:

13.3.1SymmetricCryptography

TherearemanyalgorithmstoimplementSymmetricCryptography.Inthissection,weuseAESalgorithm.TheAdvancedEncryptionStandard(AES)isaspecificationfortheencryptionofelectronicdataestablishedbytheU.S.NationalInstituteofStandardsandTechnology(NIST)in2001,http://en.wikipedia.org/wiki/Advanced_Encryption_Standard.

Page 162: Python Programming by Example

WecandosymmetricCryptographyusingpycryptobywritingthesescriptsintoafile,calledch13_03.py

fromCryptoimportRandom

fromCrypto.CipherimportAES

message='helloworldfrompython'

#AESkeymustbeeither16,24,or32bytes

long

key='p4ssw0rdp4ssw0rd'

print('message:',message)

#encrypt

iv_aes=Random.new().read(AES.block_size)

cipher_aes=AES.new(key.encode(),

AES.MODE_CFB,iv_aes)

encrypted_aes=

cipher_aes.encrypt(message.encode())

print('encryptedAES:',encrypted_aes)

#decrypted

dec_iv_aes=Random.new().read(AES.block_size)

dec_cipher_aes=AES.new(key.encode(),

AES.MODE_CFB,iv_aes)

decrypted_aes=

dec_cipher_aes.decrypt(encrypted_aes)

print('decryptedAES:',decrypted_aes)

Explanation:Thefollowingisthestepsforencryption

Defineakey.Itshouldbe16,24,or32keylength

Page 163: Python Programming by Example

CalculateIVvalueforAESusingRandom.read()withAES.block_sizeparameterInstantiateAESusingAES.new()withpassingkeyandIVvalueEncryptmessagebycallingencrypt()Theresultisarrayofbyte

Thefollowingisthestepsforencryption

Defineakey.Itshouldbe16,24,or32keylengthCalculateIVvalueforAESusingRandom.read()withAES.block_sizeparameterInstantiateAESusingAES.new()withpassingkeyandIVvalueDecryptcipherbycallingdecrypt()Theresultisbestringinarrayofbyte

Saveandruntheprogram.

$python3ch13_03.py

Programoutput:

Page 164: Python Programming by Example

13.3.2AsymmetricCryptography

ThecommonalgorithmtoimplementAsymmetricCryptographyisRSAwhichiswidelyusedforsecuredatatransmission.YoureadabriefdescriptioninWikipedia,http://en.wikipedia.org/wiki/RSA_(cryptosystem).

pycryptolibraryhaslibraryforRSAimplementation.Inthissection,wetrytoimplementRSAusingpycrypto.Thefollowingisourscenario:

GenerateRSAkeys(publicandprivatekeys)Savethesekeystotwofiles(publicandprivatekeyfiles)Forencryption,weusepublickeyfileFordecryption,weuseprivatekeyfile

WestorepublicandprivatekeysintoafileinPEMdataencoding.

TogeneratepublicandprivatekeysforRSA,weuseRSA.generate().Weextractprivateandpublickeyvalues.Then,savethemintofile.encrypt()anddecrypt()fromRSAKeyareusedtoencryptanddecrypt.

ThefollowingisimplementationforourRSAscenario.

fromCrypto.PublicKeyimportRSA

fromCryptoimportRandom

#generateprivateandpublickeys

#Then,savethemintofiles

print('generatingprivateandpublickeys...')

key=RSA.generate(2048)

f=open('my_rsa_private_key.pem','wb')

f.write(key.exportKey('PEM'))

f.close()

f=open('my_rsa_public_key.pem','wb')

f.write(key.publickey().exportKey('PEM'))

f.close()

print('done')

Page 165: Python Programming by Example

message='helloworldfrompython'

print('plaintext:',message)

#encryptdatausingpublickey

f=open('my_rsa_public_key.pem','r')

RSAkey=RSA.importKey(f.read())

f.close()

k=Random.new().read(8)

encrypted_msg=RSAkey.encrypt(message.encode(),k)

ciphertext=encrypted_msg[0]

print('encrypted:',ciphertext)

#decryptdatausingprivatekey

f=open('my_rsa_private_key.pem','r')

RSAkey=RSA.importKey(f.read())

f.close()

decrypted_msg=RSAkey.decrypt(ciphertext)

print('decrypted:',decrypted_msg.decode("utf-8"))

Savethesescriptsintoafile,calledch13_04.py.

Nowyoucanbuildandrunit.AsampleoutputcanbeseeninFigurebelow.

$python3ch13_04.py

Programoutput:

Page 166: Python Programming by Example
Page 167: Python Programming by Example

14.DatabaseProgramming

ThischapterexplainshowtobuilddatabaseapplicationusingPython.

Page 168: Python Programming by Example

14.1DatabaseforPython

Pythoncancommunicatewithdatabaseserverthroughdatabasedriver.WecanuseMySQLdriverfroPython.Inthischapter,IonlyfocusonMySQLscenario.

Page 169: Python Programming by Example

14.2MySQLDriverforPython

WeuseMySQLdriverforPython.Furtherinformationaboutthisdriver,pleasevisitonhttps://dev.mysql.com/downloads/connector/python/2.1.htmltodownloadandinstall.

YoualsocaninstallMySQLdriverforPythonviapip3(Python3.x).

$sudopip3install--allow-externalmysql-connector-python

mysql-connector-python

Page 170: Python Programming by Example

14.3TestingConnection

Inthissection,wetrytoconnectMySQLdatabase.Wecanuseconnect()frommysql.connectorobject.

importmysql.connector

print('connectingtomysqlserver...')

cnx=mysql.connector.connect(user='pyuser',

password='password123',

host='127.0.0.1',

database='pydb')

print('connected')

Youshouldpassusername,password,databaseserveranddatabasename.

Page 171: Python Programming by Example

14.4CRUD(Create,Read,UpdateandDelete)Operations

Inthissection,wetrytocreate,read,updateanddeletedataonMySQL.Firstly,wecreatedatabaseanditstable.

ThefollowingisourtableschemeonMySQL.

CREATEDATABASE`pydb`;

CREATETABLE`pydb`.`product`(

`idproduct`INTNOTNULLAUTO_INCREMENT,

`name`VARCHAR(30)NOTNULL,

`code`VARCHAR(10)NOTNULL,

`price`DECIMALNOTNULL,

`quantity`INTNULL,

`created`DATETIMENULL,

PRIMARYKEY(`idproduct`));

File:pydb.sqlRuntheseSQLscriptsintoyourMySQL.

14.4.1CreateData

Tocreatedata,weuseSQLstatement,INSERTINTO,whichpasstoexecute()function.

defcreate_data(conn):

cursor=conn.cursor()

print('insertingdata...')

foriinrange(1,5):

insert_product=("INSERTINTOproduct"

"(name,code,price,quantity,created)"

"VALUES(%s,%s,%s,%s,%s)")

data_product=("product"+str(i),"F029"+str(i),

i*0.21,i,datetime.now())

Page 172: Python Programming by Example

cursor.execute(insert_product,data_product)

product_id=cursor.lastrowid

print('insertedwithid=',product_id)

conn.commit()

cursor.close()

print('done')

Toobtainthelastinsertedid,wecanuselastrowidfromcursorobject.

14.4.2ReadData

Toreaddata,youcanuseSELECT...FROMqueryonyourPythonscripts.

defread_data(conn):

print('readingdata....')

selected_id=0

cursor=conn.cursor()

query="SELECTidproduct,name,code,price,quantity,

createdFROMproduct"

cursor.execute(query)

for(id,name,code,price,quantity,created)incursor:

print("{},{},{},{},{},{:%d%b%Y

%H:%M:%S}".format(

id,name,code,price,quantity,created))

ifselected_id<=0:

selected_id=id

cursor.close()

print('done')

returnselected_id

14.4.3UpdateData

Toupdatedata,youcanuseUPDATE....SETqueryonyourPythonscripts.

Page 173: Python Programming by Example

Toupdatedata,youcanuseUPDATE....SETqueryonyourPythonscripts.

defupdate_data(conn,id):

print('updatingdatawithidproduct=',id,'...')

cursor=conn.cursor()

query="UPDATEproductSETname=%s,code=%s,price=%s,

quantity=%s,created=%swhereidproduct=%s"

name='updated-name'

code='F9999'

price=0.99

quantity=10

created=datetime.now()

cursor.execute(query,(name,code,price,quantity,

created,id))

conn.commit()

cursor.close()

print('done')

Don'tforgettocallcommit()afterchangedthedata.

14.4.4DeleteData

Toupdatedata,youcanuseDELETEFROMqueryonyourPythonscripts.

defdelete_data(conn,id):

print('deletingdataonidproduct=',id,'...')

cursor=conn.cursor()

query="DELETEFROMproductwhereidproduct=%s"

cursor.execute(query,(id,))

conn.commit()

cursor.close()

print('done')

defdelete_all(conn):

print('deletingalldata....')

cursor=conn.cursor()

query="DELETEFROMproduct"

cursor.execute(query)

Page 174: Python Programming by Example

conn.commit()

cursor.close()

print('done')

Don'tforgettocallcommit()afterchangedthedata.

14.4.5WritethemAll

NowwecanwriteourscriptsaboutCRUD.

Writethesescripts.

importmysql.connector

fromdatetimeimportdatetime

defcreate_data(conn):

cursor=conn.cursor()

print('insertingdata...')

foriinrange(1,5):

insert_product=("INSERTINTOproduct"

"(name,code,price,quantity,created)"

"VALUES(%s,%s,%s,%s,%s)")

data_product=("product"+str(i),"F029"+str(i),

i*0.21,i,datetime.now())

cursor.execute(insert_product,data_product)

product_id=cursor.lastrowid

print('insertedwithid=',product_id)

conn.commit()

cursor.close()

print('done')

defread_data(conn):

print('readingdata....')

selected_id=0

cursor=conn.cursor()

query="SELECTidproduct,name,code,price,quantity,

Page 175: Python Programming by Example

createdFROMproduct"

cursor.execute(query)

for(id,name,code,price,quantity,created)incursor:

print("{},{},{},{},{},{:%d%b%Y

%H:%M:%S}".format(

id,name,code,price,quantity,created))

ifselected_id<=0:

selected_id=id

cursor.close()

print('done')

returnselected_id

defupdate_data(conn,id):

print('updatingdatawithidproduct=',id,'...')

cursor=conn.cursor()

query="UPDATEproductSETname=%s,code=%s,price=%s,

quantity=%s,created=%swhereidproduct=%s"

name='updated-name'

code='F9999'

price=0.99

quantity=10

created=datetime.now()

cursor.execute(query,(name,code,price,quantity,

created,id))

conn.commit()

cursor.close()

print('done')

defdelete_data(conn,id):

print('deletingdataonidproduct=',id,'...')

cursor=conn.cursor()

query="DELETEFROMproductwhereidproduct=%s"

cursor.execute(query,(id,))

conn.commit()

cursor.close()

print('done')

defdelete_all(conn):

print('deletingalldata....')

cursor=conn.cursor()

query="DELETEFROMproduct"

cursor.execute(query)

Page 176: Python Programming by Example

conn.commit()

cursor.close()

print('done')

print('connectingtomysqlserver...')

cnx=mysql.connector.connect(user='pyuser',

password='password123',

host='127.0.0.1',

database='pydb')

print('connected')

create_data(cnx)

selected_id=read_data(cnx)

update_data(cnx,selected_id)

read_data(cnx)

delete_data(cnx,selected_id)

read_data(cnx)

delete_all(cnx)

cnx.close()

print('closedconnection')

Savethisprogram.Then,youcanrunit.

$python3ch14_01.py

Programoutput:

Page 177: Python Programming by Example
Page 178: Python Programming by Example

15.SocketProgramming

ThischapterexplainshowtocreatesocketapplicationusingPython.

Page 179: Python Programming by Example

15.1SocketModule

Wecancreateapplicationbasedonsocketstackusingnetpackage.Youcanfinditforfurtherinformationonhttps://docs.python.org/3/library/socket.html.Irecommendyoutoreadsomebooksorwebsitesabouttheconceptofsocket.

Page 180: Python Programming by Example

15.2HelloWorld

Togetstarted,wecreateasimpleapplicationtogetalistofIPAddressinlocalcomputer.Wecanusegethostname()andgethostbyname()fromsocketobject.

Inthissection,wetrytogetlocalIPAddress.Firstly,wecancreateanewfile,calledch15_01.py.

importsocket

hostname=socket.gethostname()

ip=socket.gethostbyname(hostname)

print('hostname:',hostname)

print('ipaddress:',ip)

Savethiscode.Trytobuildandrunit.

$python3ch15_01.py

YoushouldseeyourlocalIPaddressfromyourcomputer.

Page 181: Python Programming by Example
Page 182: Python Programming by Example

15.3Client/ServerSocket

Nowwecreateaclient/serversocketusingPython.Wewillusesocketpackagetobuildclient/serverapplication.Forillustration,wecreateserverandclient.

15.3.1ServerSocket

Howtocreateserversocket?Itiseasy.Thefollowingisasimplealgorithmhowtobuildserversocket

createserversocketlistenincomingclientonthespecificportifclientconnected,serversendsdataandthendisconnectfromclient

Inthissection,webuildserverapp.Firstly,wecancreateanewfile,calledch15_02.py.Then,writethesescripts.

importsocket

#createtcp/ipsocket

server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

host=socket.gethostname()

port=8091

#bindtotheport

server.bind((host,port))

#queueupto10clients

server.listen(10)

counter=0

print('waitingconnectionfromclients...')

whileTrue:

#establishaconnection

client,address=server.accept()

counter+=1

print('anewconnectionfrom',str(address))

Page 183: Python Programming by Example

message="welcome,yourid="+str(counter)+"\r\n"

client.send(message.encode('ascii'))

client.close()

Itusesport8091.Youcanchangeit.

15.3.2ClientSocket

Clientsocketisclientapplicationthatconnectstoserverandthensends/receivesdatafrom/toserver.WeshouldknowaboutIPaddressandportfromtargetserver.Wecancallconnect()toconnecttoserverandcallrecv()toreceiveincomingdata.

Inthissection,webuildclientapp.Firstly,wecancreateanewfile,calledch15_03.py.Then,writethesescripts.

importsocket

#createasocketobject

client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#ip/hostnameofserver

#changethisipaddress

host='192.168.0.10'

port=8091

print('connectingtoserver...')

client.connect((host,port))

print('connected')

recv=client.recv(1024)

print('received:',recv.decode('ascii'))

client.close()

print('closed')

Page 184: Python Programming by Example

YoushouldchangehostforIPaddressofserver.

15.3.3Testing

Nowwecantestourclient/serverapplication.Firstly,werunserverapplicationandthenexecuteclientapplication.

$python3ch15_02.py

Then,runclientapp.

$python3ch15_03.py

Hereissampleofprogramoutputforserverapplication:

Hereissampleofprogramoutputforclientapplication:

Page 185: Python Programming by Example

Thisisprogramoutputforthesecondclient.

Page 186: Python Programming by Example

16.PythonRegularExpressions

ThischapterexplainshowtoworkwithregularexpressionsinPython.

Page 187: Python Programming by Example

16.1GettingStarted

Regularexpressionsareapowerfullanguageformatchingtextpatterns.ThePython"re"moduleprovidesregularexpressionsupport.Thislibrarycanbereadonhttps://docs.python.org/3/library/re.html.

Youalsoobtainregularexpressionpatternsfromthissite,http://www.regxlib.com.

Page 188: Python Programming by Example

16.2Demo

Inthisdemo,wecreatethreescenario:

ValidatenumberdataSearchdataSearchandreplacedata

Wecanusematch()fromreobjecttovalidatethematchingofdefinedpattern.Tosearch,youcanusesearch()functionandusepub()toreplacedata.

Let'screateafile,calledch16_01.py,andwritethesescripts.

importre

#patternfornumbers

p=re.compile('^[0-9]+$')

print(p.match('19023'))

print(p.match('0000'))

print(p.match('12.789'))

print(p.match('12b23'))

#search

message='AnnaWilliam<[email protected]>'

match=re.search(r'[\w.-]+@[\w.-]+',message)

ifmatch:

print(match.group())

#searchandreplase

message='aaa:asasasw::sasas:::'

p=re.compile('(:|::|:::)')

resutl=p.sub('<>',message)

print(resutl)

Saveandruntheprogram.

Page 189: Python Programming by Example

$python3ch16_01.py

Programoutput:

Page 190: Python Programming by Example

17.PythonGUIProgramming

ThischapterexplainshowtoworkwithGUIinPython.

Page 191: Python Programming by Example

17.1GettingStarted

TherearemanymodulestoimplementGUIinPython.Inthischapter,welearntkintertobuildPythonGUI.Thislibrarycanbereadonthissite,https://docs.python.org/3/library/tk.html.ThislibraryhasinstalledonPython3.x.

Let'sstarttobuildPythonappwithtkinterlibrary.

Page 192: Python Programming by Example

17.2HelloPythonGUI

ThefirstdemoistobuildPythonGUIHelloWorld.WeuseTkobjecttobuildaform.

Writethesescripts.

importtkinterastk

dialog=tk.Tk()

dialog.title('SimpleForm')

dialog.mainloop()

Savethesescriptsintoafile,calledch17_01.py.Then,runtheprogram.

$python3ch17_01.py

Youshouldseeaformdialogwithtitle"SimpleForm".

Page 193: Python Programming by Example

17.3WorkingwithInputForm

NowwecanextendourTkobjectintoaninputform.Inthisscenario,weputtwoTextboxandabutton.Ifweclickabutton,wereadTextboxvalues.

Let'swritethesescriptsfordemo.

importtkinterastk

classInputForm(object):

def__init__(self):

self.root=tk.Tk()

self.root.title('InputForm')

self.num_a=''

self.num_b=''

self.frame=tk.Frame(self.root)

self.frame2=tk.Frame(self.root)

self.frame.pack()

self.frame2.pack()

self.initialization()

definitialization(self):

r=self.frame

k_a=tk.Label(r,text='NumberA')

k_a.grid(row=0,column=0)

self.e_a=tk.Entry(r,text='NumA')

self.e_a.grid(row=0,column=1)

self.e_a.focus_set()

k_b=tk.Label(r,text='NumberB')

k_b.grid(row=1,column=0)

self.e_b=tk.Entry(r,text='NumB')

self.e_b.grid(row=1,column=1)

self.e_b.focus_set()

r2=self.frame2

b=tk.Button(r2,text='Save',command=self.get_inputs)

b.pack(side='left')

defget_inputs(self):

self.num_a=self.e_a.get()

self.num_b=self.e_b.get()

self.root.destroy()

defget_values(self):

Page 194: Python Programming by Example

returnself.num_a,self.num_b

defwait_for_input(self):

self.root.mainloop()

dialog=InputForm()

dialog.wait_for_input()

num_a,num_b=dialog.get_values()

print('numa:',num_a)

print('numb:',num_b)

Savetheprogramintoafile,calledch17_02.py.

Runtheprogram.

$python3ch17_02.py

Youshouldseetheinputformdialog.

FillvaluesonTextbox.Then,clickSavebutton.

Page 195: Python Programming by Example

Afterclicked,theprogramwillreadinputvaluesandshowsthemonTerminal.

Page 196: Python Programming by Example

17.4WorkingwithCommonDialogs

tkinterlibraryalsoprovidescommondialogssuchMessagebox,filedialogandcolorchooser.

Forillustration,writethesescripts.

importtkinterastk

fromtkinterimportmessagebox,filedialog

fromtkinter.colorchooserimport*

#messagebox

print('demomessagebox')

messagebox.showinfo('Information','Thisismessage')

messagebox.showerror('Error','Thisiserrormessage')

messagebox.showwarning('Warning','Thisiswarningmessage')

#filedialog

dir=filedialog.askdirectory()

print('selecteddirectory:',dir)

file=filedialog.askopenfile(mode="r")

print('selectedfile:',file.name)

new_file_name=filedialog.asksaveasfilename()

print('saveasfile:',new_file_name)

#colorchooser

defget_color():

color=askcolor()

print('selectedcolor:',color)

dialog=tk.Tk()

tk.Button(dialog,text='SelectColor',

command=get_color).pack()

dialog.title('SimpleForm')

dialog.mainloop()

Savetheprogramintoafile,calledch17_03.py.Then,runtheprogram.

Page 197: Python Programming by Example

$python3ch17_03.py

Thefollowingisoutputformsforinformation,errorandwarning.

Page 198: Python Programming by Example

Outputformforselectingadirectory.

Page 199: Python Programming by Example

Outputformforselectingafile.

Page 200: Python Programming by Example
Page 201: Python Programming by Example

Outputformforsavingafile.

Outputformforselectingacolor.ClickSelectColorbuttontoshowColorsdialog.

Page 202: Python Programming by Example

ThefollowingisprogramoutputinTerminal.

Page 203: Python Programming by Example
Page 204: Python Programming by Example

18.PythonUnitTesting

ThischapterexplainshowtobuildunittestinginPython.

Page 205: Python Programming by Example

18.1GettingStarted

Unittestingisasoftwaredevelopmentprocessinwhichthesmallesttestablepartsofanapplication,calledunits,areindividuallyandindependentlyscrutinizedforproperoperation.Unittestingcanbeusedtominimizebugsontheapplication.

InPython,wecanuseunittestframework,https://docs.python.org/3/library/unittest.html.Pleasereadittoobtainmoreinformation.

Page 206: Python Programming by Example

18.2Demo

Fortesting,wecreateaclassanddounittestingforthisclass.

Writeafile,calledmathu.pyandwritethesescripts.

classMathu:

def__init__(self):

print('call__init__fromMathuclass')

defadd(self,num_a,num_b):

returnnum_a+num_b

defdiv(self,num_a,num_b):

try:

result=num_a/num_b

exceptZeroDivisionErrorase:

raisee

returnresult

defcheck_even(self,number):

returnnumber%2==0

Nowwewanttotestthisclassbythefollowingscenario:

testingadd()testindiv()testingcheck_even()testingforexceptionerror

Youcanwritethesescripts.

importunittest

importmathu

Page 207: Python Programming by Example

classTestMathu(unittest.TestCase):

deftest_add(self):

res=mathu.Mathu().add(5,8)

self.assertEqual(res,13)

deftest_div(self):

res=mathu.Mathu().div(10,8)

self.assertGreater(res,1)

deftest_check_even(self):

res=mathu.Mathu().check_even(4)

self.assertTrue(res)

deftest_error(self):

self.assertRaises(ZeroDivisionError,

lambda:mathu.Mathu().div(5,0))

if__name__=='__main__':

unittest.main()

Saveintoafile,calledch18_01.py.

Nowyoucanruntheprogram.

$python3ch18_01.py

Programoutput:

Page 208: Python Programming by Example
Page 209: Python Programming by Example

SourceCode

Youcandownloadsourcecodeonhttp://www.aguskurniawan.net/book/python2120151.zip.

Page 210: Python Programming by Example

Contact

Ifyouhavequestionrelatedtothisbook,[email protected]:http://blog.aguskurniawan.net