python advanced 3.the python std lib by example – algorithm

21
THE PYTHON STD LIB BY EXAMPLE – ALGORITHM John Saturday, April 23, 2022

Upload: john-zhang

Post on 11-May-2015

398 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Python advanced 3.the python std lib by example – algorithm

THE PYTHON STD LIB BY EXAMPLE – ALGORITHM

JohnWednesday, April 12, 2023

Page 2: Python advanced 3.the python std lib by example – algorithm

Brief introduction

• Python includes several modules which can implement algorithm elegantly and concisely.

• It support uprely procedural, OOP and functional styles.

• It includes: functools, partial , itertools, operator, contextlib etc

Page 3: Python advanced 3.the python std lib by example – algorithm

FUNCTOOLS –TOOLS FOR MANIPULATING FUNCTIONS

Page 4: Python advanced 3.the python std lib by example – algorithm

Partial Objects: provide default argument• The partial objects can provide or change the default

value of the argument.• Example code (assume we have define function

myfunc(a,b=1) :>>> import functools>>> p1 = functools.partial(myfunc,b=4)>>> p1(‘passing a’)>>> p2 = functools.partial(myfunc,’default a’,b=99)>>> p2()>>> p2(b=‘override b’)

Page 5: Python advanced 3.the python std lib by example – algorithm

Function update_wrapper()

• The partial object does not have __name__ and __doc__ attributes by default.

• Using update_wrapper(0 copies or added attributes from the original function.

Format:>>> functools.update_wrapper(p1,myfunc)

Page 6: Python advanced 3.the python std lib by example – algorithm

the “rich comparison”

First let us learn which is “rich comparion” in python.•Rich comparison method API (__lt__, __le__, __eq__, __gt__, __ge__)(Here le means less than, le means “less or equal”, gt means “greater than”, ge means “greater than or equal”)•These method API can help perform a single comparison operation and return a Boolean value

Page 7: Python advanced 3.the python std lib by example – algorithm

Example of rich comparision

We implement __eq__ and __gt__. Functools.total_ordering can implement other operator (<, <=, >= etc) base on eq and gt.

Page 8: Python advanced 3.the python std lib by example – algorithm

Function cmp_to_key: convert cmp to key for sorting• In Python 2.xx, cmp do comparion:

cmp(2,1) -> 1cmp(1,1) -> 0cmp(1,2) -> -1

• In python 3, cmp in sort function no longer supported.

• Functools.cmp_to_key convert cmp to key for sorting

Page 9: Python advanced 3.the python std lib by example – algorithm

Quick example of cmp_to_key

• Built-in funtion cmp need two argument.• Sorted function can use other option

key=func. Sorted by key (only support this on Python 3.X)

Page 10: Python advanced 3.the python std lib by example – algorithm

ITERTOOLS-ITERATOR FUNCTIONS

Page 11: Python advanced 3.the python std lib by example – algorithm

Brief introduction

• The itertools module includes a set of functions for working with sequence data sets (list, tuple,set,dict etc).

• Iterator based code offer better memory comsumption.

Page 12: Python advanced 3.the python std lib by example – algorithm

Function chain(): Merge iterators

• Take serveral iterators as arguments and return a single iterator

Page 13: Python advanced 3.the python std lib by example – algorithm

Function imap: similar as map

• Imap accept a function, and multiple sequences, return a tuple.

Page 14: Python advanced 3.the python std lib by example – algorithm

Other function merge and split iterators• Function izip: like zip, but combine iterator

and return iterator of tuple instead of list• Function islice: similar as slice• Function imap: similar as map• Function ifilter: similar as filter, filter those

items test functions return True• Function ifilterfalse: filter those items where

the test function return False

Page 15: Python advanced 3.the python std lib by example – algorithm

Function starmap: • First, let us review the star * syntax in Python.• Star * means unpack the sequence reference as

argument list.>>> def foo(bar,lee):

print bar,lee>>> a = [1,2]>>> foo(a) # it is wrong, need two arguments>>> foo(*a) # it is right. The list is unpack>>>foo(1,2) # it is the same thing

Page 16: Python advanced 3.the python std lib by example – algorithm

Function starmap: unpack the input• Unpack the item as argument using the *

syntax

Page 17: Python advanced 3.the python std lib by example – algorithm

Function count(): iterator produce consecutive integers• Function count(start=0,step=1): user can pass

the start and step value.No upper bound argument.

>>> a = itertools.count(start=10,step=10)>>> for i in a:

print Iif I >100:

break

Print list 10,20,30 … 110

Page 18: Python advanced 3.the python std lib by example – algorithm

Function cycle: iterator do indefinitely repeats • It need remember the whole input, so it may

consume quite a bit memo if input iterator is long.

Page 19: Python advanced 3.the python std lib by example – algorithm

Function repeat: repeat same value several time• This example mean repeat ‘a’ 5 times.>>> itertools.repeat(‘a’, 5)It is similar as list [‘a’,’a’.’a’,’a’,’a’]

The return is a iterator but not list. So it use the memo only when it is called.

Page 20: Python advanced 3.the python std lib by example – algorithm

Function dropwhile and takewhile

• Func dropwhile start output while condition become false for the first time

• Example, 3rd element do not met x<1. So it return 3 to end of this list

Page 21: Python advanced 3.the python std lib by example – algorithm

Function dropwhile and takewhile

• The opposite of dropwhile: stop output while condition become false for the first time

• So all output items meet the condition function.