Detailed python decorators, iterators & generators, re regular expressions, string formatting

Source: Internet
Author: User
Tags throw exception
The contents of this chapter:





Decorative Device



Iterators & Generators



Re-regular expression



String formatting



Decorative Device



The adorner is a well-known design pattern, which is often used in scenes where there is a demand for facets, with the classic insert log, performance test, transaction processing, and so on. Decorators are a great design for solving such problems, and with adorners, we can pull out a lot of the same code that is not relevant to the function itself and continue to reuse it. In summary, the function of an adorner is to add additional functionality to an already existing object.



Define a basic adorner first:


########### Basic decorator ###########
def orter (func): #define decorator
     def inner ():
         print ("This is inner before.")
         s = func () #Call the original passed parameter function to execute
         print ("This is inner after.")
         return s #return
     return inner #Return the inner function to the name function
 
@orter #Call the decorator (pass the function name as an argument to the orter decorator)
def name ():
     print ("This is name.")
     return True #name the original function return True
 
ret = name ()
print (ret)
 
Output results:
This is inner before.
This is name.
This is inner after.
True


To pass parameters to the adorner:


############## Decorator pass parameter ############
def orter (func):
     def inner (a, b): #Receive incoming 2 parameters
         print ("This is inner before.")
         s = func (a, b) #receive 2 parameters of the original function passed in
         print ("This is inner after.")
         return s
     return inner
 
@orter
def name (a, b): #Receive the 2 parameters passed in, and name the overall function when the parameters are passed into the orter decorator
     print ("This is name.% s,% s"% (a, b))
     return True
 
ret = name ('nick', 'jenny') #Pass in 2 parameters
print (ret)
 
Output results:
This is inner before.
This is name.nick, jenny
This is inner after.
True


To pass a universal parameter to the adorner:


########### Universal parameter decorator ###########
 
def orter (func):
     def inner (* args, ** kwargs): #universal parameters receive multiple parameters
         print ("This is inner before.")
         s = func (* args, ** kwargs) #universal parameters receive multiple parameters
         print ("This is inner after.")
         return s
     return inner
 
@orter
def name (a, b, c, k1 = 'nick'): #Accept multiple parameters passed in
     print ("This is name.% s,% s"% (a, b))
     return True
 
ret = name ('nick', 'jenny', 'car')
print (ret)
 
Output results:
This is inner before.
This is name.nick, jenny
This is inner after.
True


A function applies multiple adorner methods:


############# Apply multiple decorators to one function
 
def orter (func):
    def inner (* args, ** kwargs):
        print ("This is inner one before.")
        print ("This is inner one before angin.")
        s = func (* args, ** kwargs)
        print ("This is inner one after.")
        print ("This is inner one after angin.")
        return s
    return inner
 
def orter_2 (func):
    def inner (* args, ** kwargs):
        print ("This is inner two before.")
        print ("This is inner two before angin.")
        s = func (* args, ** kwargs)
        print ("This is inner two after.")
        print ("This is inner two after angin.")
        return s
    return inner
 
@orter #Pass the following functions as arguments to the orter decorator
@ orter_2 #Pass the following function as an argument to the orter_2 decorator
def name (a, b, c, k1 = 'nick'):
    print ("This is name.% s and% s."% (a, b))
    return True
 
ret = name ('nick', 'jenny', 'car')
print (ret)
 
Output results:
This is inner one before.
This is inner one before angin.
This is inner two before.
This is inner two before angin.
This is name.nick and jenny.
This is inner two after.
This is inner two after angin.
This is inner one after.
This is inner one after angin.
True





Iterators & Generators



1. iterators



An iterator is simply a container object that implements an iterator protocol.



Characteristics:



The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method



A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end



You can't go back halfway through the interview.



Facilitates recycling of large data sets, saving memory


a = iter([1,2,3,4])
print a
print a.next()
print a.next()
print a.next()
print a.next()
print a.next()
 
 
<listiterator object at 0x00000000023E9710>
1
2
3
4
Traceback (most recent call last):
  File "D:/python/untitled4/test.py", line 23, in <module>
    print a.next()
StopIteration


2. Generator



When a function call returns an iterator, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator.


def xran ():
     print ("one")
     yield 1
     print "two"
     yield 2
     print "sr"
     yield 3
 
ret = xran ()
#print ret # <generator object xran at 0x00000000022D8AB0>
 
 
result = ret.next ()
print result
 
result = ret.next ()
print result
 
result = ret.next ()
print result
 
 
# ret.next () #Throw exception after loop
# ret.close () #Close the generator
 
one
1
two
2
sr
3


The expression for the generator:


a = [1,2,3]
b = [i + 3 for i in a]
print b
print type (b)
 
 
ib = (i + 3 for i in a)
print ib
print ib.next ()
print ib.next ()
print ib.next ()
 
 
[4, 5, 6]
<type 'list'>
<generator object <genexpr> at 0x00000000023E8A20>
4
5
6


Regular Expressions:



Regular expressions are a very powerful tool for matching strings, and in other programming languages there is also the concept of regular expressions. In essence, a regular expression (or RE) is a small, highly specialized programming language (in Python) that is embedded in Python and implemented through the RE module. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.


#Import the re module
import re
  
s = 'nick jenny nice'
  
# Matching method (1)
b = re.match (r'nick ', s)
q = b.group ()
print (q)
  
# Matching method (2)
# Generate a Pattern object instance, r means match the source string
a = re.compile (r'nick ')
print (type (a)) # <class '_sre.SRE_Pattern'>
  
b = a.match (s)
print (b) # <_ sre.SRE_Match object; span = (0, 4), match = 'nick'>
  
q = b.group ()
print (q)
  
  
#Matched strings are placed in string
print (b.string) #nick jenny nice
#The string to be matched is placed in re


The difference between the two matching methods is that the first shorthand is to make a matching formula when each match is compiled, the second way is to match the format in advance (to parse the matching formula), so that the match will not be compiled in the matching format.



Matching rules:





<br> # "." matches any character (except \ n)
a = re.match (r ".", "95nick")
b = a.group ()
print (b)
  
# [...] match character set
a = re.match (r "[a-zA-Z0-9]", "123Nick")
b = a.group ()
print (b)
# \ d \ D match numbers / not numbers
a = re.match (r "\ D", "nick")
b = a.group ()
print (b)
  
# \ s \ S matches whitespace / non-whitespace characters
a = re.match (r "\ s", "")
b = a.group ()
print (b)
  
# \ w \ W Match word characters [a-zA-Z0-9] / non-word characters
a = re.match (r "\ w", "123Nick")
b = a.group ()
print (b)
a = re.match (r "\ W", "+-* /")
b = a.group ()
print (b)
# "*" Matches the previous character 0 times or unlimited times
a = re.match (r "[A-Z] [a-z] *", "Aaaaaa123") #Can only match A, 123 will not match
b = a.group ()
print (b)
  
# "+" Matches the previous character once or unlimited times
a = re.match (r "[_ a-zA-Z] +", "nick")
b = a.group ()
print (b)
  
# "?" Matches a character 0 or 1 time
a = re.match (r "[0-8]? [0-9]", "95") # (0-8) No match on 9
b = a.group ()
print (b)
  
# {m} {m, n} matches the previous character m times or m to n times
a = re.match (r "[\ w] {6,10} @ qq.com", "630571017@qq.com")
b = a.group ()
print (b)
  
# *? +? ?? Match pattern becomes non-greedy (match strings as little as possible)
a = re.match (r "[0-9] [a-z] *?", "9nick")
b = a.group ()
print (b)
a = re.match (r "[0-9] [a-z] +?", "9nick")
b = a.group ()
print (b)
# "^" Matches the beginning of a string, and matches the beginning of each line in a multiline pattern.
li = "nick \ nnjenny \ nsuo"
a = re.search ("^ s. *", li, re.M)
b = a.group ()
print (b)
  
# "$" Matches the end of a string, and the end of each line in a multi-line pattern.
li = "nick \ njenny \ nnick"
a = re.search (". * y $", li, re.M)
b = a.group ()
print (b)
  
# \ A matches only the beginning of a string
li = "nickjennyk"
a = re.findall (r "\ Anick", li)
print (a)
  
# \ Z Matches only the end of the string
li = "nickjennyk"
a = re.findall (r "nick \ Z", li)
print (a)
  
# \ b Matches a word boundary, that is, the position between a word and a space
a = re.search (r "\ bnick \ b", "jenny nick car")
b = a.group ()
print (b)
# "|" Matches any expression
a = re.match (r "nick | jenny", "jenny")
b = a.group ()
print (b)
  
# (ab) Expressions in parentheses as a group
a = re.match (r "[\ w] {6,10} @ (qq | 163) .com", "630571017@qq.com")
b = a.group ()
print (b)
  
# \ <number> quote the string matched by num group
a = re.match (r "<([\ w] +>) [\ w] + </ \ 1", "<book> nick </ book>")
b = a.group ()
print (b)
  
# (? P <key> vlace) match output dictionary
li = 'nick jenny nnnk'
a = re.match ("(? P <k1> n) (? P <k2> \ w +). * (? P <k3> n \ w +)", li)
print (a.groupdict ())
Output results:
{'k2': 'ick', 'k1': 'n', 'k3': 'nk'}
  
# (? P <name>) group an alias
# (? P = name) quote a group match string with alias name
a = re.match (r "<(? P <jenny> [\ w] +>) [\ w] + </ (? P = jenny)", "<book> nick </ book>")
b = a.group ()
print (b)



Module Method Introduction





######### Module Method Introduction ##########
# match match from scratch
  
# search matches the entire string until a match is found
  
# findall find matches, return a list of all matching parts
# findall parentheses
li = 'nick jenny nick car girl'
  
r = re.findall ('n \ w +', li)
print (r)
#Output result: ['nick', 'nny', 'nick']
r = re.findall ('(n \ w +)', li)
print (r)
#Output result: ['nick', 'nny', 'nick']
r = re.findall ('n (\ w +)', li)
print (r)
#Output result: ['ick', 'ny', 'ick']
r = re.findall ('(n) (\ w +) (k)', li)
print (r)
#Output result: [('n', 'ic', 'k'), ('n', 'ic', 'k')]
r = re.findall ('(n) ((\ w +) (c)) (k)', li)
print (r)
#Output result: [('n', 'ic', 'i', 'c', 'k'), ('n', 'ic', 'i', 'c', 'k')]
  
  
# finditer returns an iterator, same as findall
li = 'nick jenny nnnk'
a = re.finditer (r'n \ w + ', li)
for i in a:
    print (i.group ())
  
# sub Replace the part of the string that matches the regular expression with another value
li = 'This is 95'
a = re.sub (r "\ d +", "100", li)
print (a)
  
li = "nick njenny ncar ngirl"
a = re.compile (r "\ bn")
b = a.sub ('cool', li, 3) #Subsequent parameter replacement several times
print (b)
  
#Output results:
#coolick cooljenny coolcar ngirl
  
# split split string based on match, return a list of split strings
li = 'nick, suo jenny: nice car'
a = re.split (r ": | |,", li) # or |, split:, white space
print (a)
  
li = 'nick1jenny2car3girl5'
a = re.compile (r "\ d")
b = a.split (li)
print (b)
  
#Output result:
# ['nick', 'jenny', 'car', 'girl', ''] #Note the empty elements behind
li = 'nick jenny nnnk'
  
a = re.match ("n \ w +", li)
print (a.group ())
  
a = re.match ("(n) (\ w +)", li)
print (a.groups ())
  
a = re.match ("(? P <k1> n) (? P <k2> \ w +). * (? P <k3> n \ w +)", li)
print (a.groupdict ())
  
-----------------------------------------------
import re
a = "123abc456"
 re.search ("([0-9] *) ([a-z] *) ([0-9] *)", a) .group (0) # 123abc456, returns the whole
 re.search ("([0-9] *) ([a-z] *) ([0-9] *)", a) .group (1) # 123
 re.search ("([0-9] *) ([a-z] *) ([0-9] *)", a) .group (2) #abc
 re.search ("([0-9] *) ([a-z] *) ([0-9] *)", a) .group (3) # 456
  
 group (1) lists the first bracket matching section, group (2) lists the second bracket matching section, and group (3) lists the third bracket matching section.
  
-----------------------------------------------





#re. I make the match case insensitive



A = Re.search (r "Nick", "Nick", re.) I)



Print (A.group ())



#re. L do localization identification (locale-aware) matching



#re. U resolves characters based on the Unicode character set. This sign affects \w, \w, \b, \b.



#re. S:. Will match line breaks, default. Commas do not match line breaks



A = Re.findall (r ".", "Nick\njenny", re. S



Print (a)



Output Result:



[' n ', ' I ', ' C ', ' k ', ' \ n ', ' j ', ' e ', ' n ', ' n ', ' Y ']



#re. The m:^$ flag will match each row, and the default ^ will only match the first line that matches the regular, and the default $ will only match the last line that matches the regular



n = "" "Drummers drumming,



One by one pipers piping, Lords a-leaping "" "



p = re.compile ("^\d+")



P_multi = Re.compile ("^\d+", re. M



Print (Re.findall (p,n))



Print (Re.findall (p_multi,n))



Common regex:



Match phone Number:


# Match phone number
phone_num = '13001000000'
a = re.compile (r "^ 1 [\ d +] {10}")
b = a.match (phone_num)
print (b.group ())


Match IPV4:


# Match IP address
ip = '192.168.1.1'
a = re.compile (r "(((1? [0-9]? [0-9]) | (2 [0-4] [0-9]) | (25 [0-5])) \ .) {3} ((1? [0-9]? [0-9]) | (2 [0-4] [0-9]) | (25 [0-5])) $ ")
b = a.search (ip)
print (b)


Match email:


# Match email
email = '630571017@qq.com'
a = re.compile (r "(. *) {0,26} @ (\ w +) {0,20}. (\ w +) {0,8}")
b = a.search (email)
print (b.group ())


String formatting:



1, percent of the way



%[(name)][flags][width]. [Precision]typecode



(name) optional, used to select the specified key



Flags optional, selectable values are: width selectable, occupied



+ Right-aligned, positive plus, negative before plus minus;



-left-justified; no sign before positive number, plus minus sign before negative number;



Spaces are right-justified, plus a positive number before a negative number plus a minus sign;



0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank



. Precision optional, number of digits retained after the decimal point



TypeCode must choose



S, gets the return value of the __str__ method of the passed-in object and formats it to the specified location



R, gets the return value of the __repr__ method of the passed-in object and formats it to the specified location



C, Integer: Converts a number to its Unicode corresponding value, 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add character to specified position



O, converts an integer to an octal representation and formats it to a specified location



X, converts an integer to a hexadecimal representation and formats it to a specified location



D, converts an integer, floating-point number to a decimal representation, and formats it to a specified position



E, converting integers, floating-point numbers to scientific notation, and formatting them to a specified location (lowercase e)



E, converting integers, floating-point numbers into scientific notation, and formatting them to a specified position (uppercase E)



F, converts an integer, floating-point number to a floating-point number representation, and formats it to a specified position (the default is 6 digits after the decimal point)



F, ibid.



G, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if scientific counts are e;)



G, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if scientific counts are e;)



%, when there is a format flag in the string, a percent sign is required


Common formatting:
  
tpl = "i am% s"% "nick"
   
tpl = "i am% s age% d"% ("nick", 18)
   
tpl = "i am% (name) s age% (age) d"% {"name": "nick", "age": 18}
   
tpl = "percent% .2f"% 99.97623
   
tpl = "i am% (pp) .2f"% {"pp": 123.425556,}
   
tpl = "i am% .2f %%"% {"pp": 123.425556,}


2. Format mode



[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]



Fill "optional" white space filled with characters



Align "optional" alignment (required with width)



<, content left justified



Content right-aligned (default)



=, the content is right-aligned, the symbol is placed to the left of the padding character, and only valid for numeric types. Even: symbol + filler + number



^, content centered



Sign "optional" with unsigned numbers



+, positive home Plus, minus plus negative;



-The plus sign does not change, minus is negative;



Spaces, plus spaces, minus signs and negative;



# "optional" for binary, octal, hex, if added #, 0b/0o/0x is displayed, otherwise not displayed



, "optional" adds a delimiter to the number, such as: 1,000,000



width "Optional" format the size of the placeholder



. Precision "optional" decimal digits reserved Precision



Type "optional" formatting types



Parameters passed into the string type



Parameters passed into the integer type



Parameters passed in floating-point or decimal type



s, format string type data



Blank, no type specified, default is None, same as S



B, automatic conversion of 10 binary integers to 2 binary representation and then formatting



C, automatic conversion of 10 binary integers to their corresponding Unicode characters



d, decimal integer



O, the 10 binary integers are automatically converted to 8 binary representation and then formatted;



X, automatically converts 10 binary integers to 16 binary representation and then formats (lowercase x)



X, automatically converts 10 binary integers to 16 binary representation and then formats (uppercase X)



E, converted to scientific notation (lowercase e), and then formatted;



E, converted to scientific notation (capital E), and then formatted;



F, converted to floating-point type (6 digits after the default decimal point), and then formatted;



F, converted to floating-point type (6 digits after the default decimal point), and then formatted;



G, automatically switch between E and F



G, automatically switch between E and F



%, display percent (default 6 digits after decimal)


Common formatting:
  
tpl = "i am {}, age {}, {}". format ("nick", 18, 'jenny')
    
tpl = "i am {}, age {}, {}". format (* ["nick", 18, 'jenny'])
    
tpl = "i am {0}, age {1}, really {0}". format ("nick", 18)
    
tpl = "i am {0}, age {1}, really {0}". format (* ["nick", 18])
    
tpl = "i am {name}, age {age}, really {name}". format (name = "nick", age = 18)
    
tpl = "i am {name}, age {age}, really {name}". format (** {"name": "nick", "age": 18})
    
tpl = "i am {0 [0]}, age {0 [1]}, really {0 [2]}". format ([1, 2, 3], [11, 22, 33])
    
tpl = "i am {: s}, age {: d}, money {: f}". format ("nick", 18, 88888.1)
  
tpl = "i am {: s}, age {: d}, money {: 0.2f}". format ("nick", 18, 88888.111111111111)
    
tpl = "i am {: s}, age {: d}". format (* ["nick", 18])
    
tpl = "i am {name: s}, age {age: d}". format (name = "nick", age = 18)
    
tpl = "i am {name: s}, age {age: d}". format (** {"name": "nick", "age": 18})
   
tpl = "numbers: {: b}, {: o}, {: d}, {: x}, {: X}, {:%}". format (15, 15, 15, 15, 15, 15.87623, 2)
   
tpl = "numbers: {0: b}, {0: o}, {0: d}, {0: x}, {0: X}, {0:%}". format (15)
   
tpl = "numbers: {num: b}, {num: o}, {num: d}, {num: x}, {num: X}, {num:%}". format (num = 15) 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.