First, iterators and generators
1. iterators
Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files
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,5])
>>> A
<list_iterator Object at 0x101402630>
>>> a.__next__ ()
1
>>> a.__next__ ()
2
>>> a.__next__ ()
3
>>> a.__next__ ()
4
>>> a.__next__ ()
5
>>> a.__next__ ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
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 func ():
Yield 1
Yield 2
Yield 3
Yield 4
In the code above: Func is a function called a generator, and when you execute this function func () you get an iterator.
>>> temp = func ()
>>> temp.__next__ ()
1
>>> temp.__next__ ()
2
>>> temp.__next__ ()
3
>>> temp.__next__ ()
4
>>> temp.__next__ ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Stopiteration
3. Example
A. Use the generator to customize the range
def nrange (num):
temp =-1
While True:
temp = temp + 1
If temp >= num:
Return
Else
Yield Temp
B. Using iterators to access range
Second, the module
module, which implements a set of code for a function with a mound code.
Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (functions can be in different. py files), and the Code collection consisting of N. py files is called a module.
such as: OS is a system-related module; file is a module related to the operation of files
The modules are divided into three types:
- Custom Modules
- Built-in standard module (also known as standard library)
- Open source Module
2.1 Time & DateTime module
Print (Time.clock ()) #返回处理器时间, 3.3 started obsolete
Print (Time.process_time ()) #返回处理器时间, 3.3 started obsolete
Print (Time.time ()) #返回当前系统时间戳
Print (Time.ctime ()) #输出Tue Jan 26 18:23:48 2016, current system time
Print (Time.ctime (Time.time () -86640)) #将时间戳转为字符串格式
Print (Time.gmtime (Time.time () -86640)) #将时间戳转换成struct_time格式
Print (Time.localtime (Time.time () -86640)) #将时间戳转换成struct_time格式, but the local time returned
Print (Time.mktime (Time.localtime ())) #与time. LocalTime () function instead, turn struct_time format back into timestamp format
#time. Sleep (4) #sleep
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ())) #将struct_time格式转成指定的字符串格式
Print (Time.strptime ("2016-01-28", "%y-%m-%d")) #将字符串格式转换成struct_time格式
#datetime Module
Print (Datetime.date.today ()) #输出格式 2016-01-26
Print (Datetime.date.fromtimestamp (Time.time () -864400)) #2016-01-16 turn the timestamp into a date format
Current_time = Datetime.datetime.now () #
Print (current_time) #输出2016-01-26 19:04:30.335935
Print (Current_time.timetuple ()) #返回struct_time格式
#datetime. replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo] []] []])
Print (Current_time.replace (2014,9,12)) #输出2014-09-12 19:06:24.074900, returns the current time, but the specified value is replaced
Str_to_date = Datetime.datetime.strptime ("21/11/06 16:30", "%d/%m/%y%h:%m") #将字符串转换成日期格式
New_date = Datetime.datetime.now () + Datetime.timedelta (days=10) #比现在加10天
New_date = Datetime.datetime.now () + Datetime.timedelta (days=-10) #比现在减10天
New_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10) #比现在减10小时
New_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120) #比现在 +120s
Print (new_date)
2.2 Random Number
Mport Random
Print Random.random ()
Print Random.randint
Print Random.randrange (1,10)
Generate Random Verification code
Import Random
Checkcode = ' '
For I in range (4):
Current = Random.randrange (0,4)
If current! = I:
temp = Chr (Random.randint (65,90))
Else
temp = Random.randint (0,9)
Checkcode + = str (temp)
Print Checkcode
2.3 OS Module
Provides an interface to invoke the operating system
OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on
Os.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () Delete a file
Os.rename ("Oldname", "newname") renaming files/directories
Os.stat (' path/filename ') get File/directory information
OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/"
OS.LINESEP output The line terminator used by the current platform, win under "\t\n", Linux "\ n"
OS.PATHSEP output string for splitting the file path
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.environ Getting system environment variables
Os.path.abspath (path) returns the absolute path normalized by path
Os.path.split (path) splits path into directory and file name two tuples returned
Os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
Os.path.basename (Path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)
Os.path.exists (path) returns true if path exists, false if path does not exist
Os.path.isabs (path) returns True if path is an absolute path
Os.path.isfile (path) returns True if path is a file that exists. otherwise returns false
Os.path.isdir (path) returns True if path is a directory that exists. otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and the parameters before the first absolute path are ignored
Os.path.getatime (Path) returns the last access time of the file or directory to which path is pointing
Os.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing
More Punch here
2.4 SYS Module
SYS.ARGV command line argument list, the first element is the path of the program itself
Sys.exit (n) exit program, Exit normally (0)
Sys.version get version information for Python interpreter
Sys.maxint the largest int value
Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing
Sys.platform returns the operating system platform name
Sys.stdout.write (' please: ')
val = Sys.stdin.readline () [:-1]
2.5 JSON & Pickle Module
Two modules for serialization
JSON, used to convert between string and Python data types
Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
2.6 Logging Module
Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into Debug (), info (), warning (), error () and critical () 5 levels, let's take a look at how to use it.
The simplest usage
Import logging
Logging.warning ("User [Alex] attempted wrong password more than 3 times")
Logging.critical ("Server is Down")
#输出
WARNING:root:user [Alex] attempted wrong password more than 3 times
CRITICAL:root:server is down
Take a look at what these log levels mean, respectively.
Levelwhen it ' s used
Debugdetailed information, typically of interest only when diagnosing problems.
Infoconfirmation that things is working as expected.
Warningan indication that something unexpected happened, or indicative of some problem in the near future (e.g. ' Disk SPAC e low '). The software is still working as expected.
Errordue to a more serious problem, the software have not been able to perform some function.
Criticala serious error, indicating that the program itself is unable to continue running.
If you want to write the log in a file, it's easy.
Import logging
Logging.basicconfig (filename= ' Example.log ', level=logging.info)
Logging.debug (' This message should go to the log file ')
Logging.info (' So should this ')
Logging.warning (' And this, too ')
One of the level=loggin in the following sentence. Info means that the logging level is set to info, that is, only logs that are higher than the log is info or the info level will be recorded in the file, in this case, the first log is not recorded, if you want to record the debug log, Change the log level to debug on the line.
Logging.basicconfig (filename= ' Example.log ', level=logging.info)
Feel the above log format forgot to add time, log do not know how to line, the following to add!
Import logging
Logging.basicconfig (format= '% (asctime) s% (message) s ', datefmt= '%m/%d/%y%i:%m:%s%p ')
Logging.warning (' If this event was logged. ')
#输出
12/12/2010 11:46:36 AM is while this event was logged.
If you want to print log in the screen and file log at the same time, you need to know a little bit more complex knowledge.
The logging library takes a modular approach and offers several categories of components:loggers, handlers, filters, and Formatters.
Loggers expose the interface that application code directly uses.
Handlers send the log records (created by loggers) to the appropriate destination.
Filters provide a finer grained facility for determining which log records to output.
Formatters specify the layout of log records in the final output.
Import logging
#create Logger
Logger = Logging.getlogger (' Test-log ')
Logger.setlevel (logging. DEBUG)
# Create console handler and set level to debug
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
# Create file handler and set level to warning
FH = logging. Filehandler ("Access.log")
Fh.setlevel (logging. WARNING)
# Create Formatter
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')
# Add formatter to CH and FH
Ch.setformatter (Formatter)
Fh.setformatter (Formatter)
# Add CH and FH to Logger
Logger.addhandler (CH)
Logger.addhandler (FH)
# ' Application ' code
Logger.debug (' Debug message ')
Logger.info (' info message ')
Logger.warn (' Warn message ')
Logger.error (' Error message ')
Logger.critical (' critical message ')
2.7 Reflection
Reflection
For beginners python may be difficult to understand, but reflection is very useful.
Imagine, when another program passed to you to write this code a variable (var= "math"), this variable is a string, the string is a module or a module under a method, you need to import this module or method through a variable, how to import this module or method, if directly execute Import Var is an error, because VAR is a variable in your code, which requires reflection and how to use reflection.
If the value of this variable is a module, you can use Mathmodule=__import__ (VAR), after importing, you can call any method under the math module in your code with mathmodule.***
When you need to get a method under a module, you can use GetAttr, which has specific examples below.
example, how to import the math module by variable import
module= "Math"
mathmodule=__import__ (module)
Print Mathmodule.pow (2,4)
example, how to use the variable import method, the next top of the code
func= "POW"
Pow=getattr (Mathmodule,func)
Print POW (2,4)
A specific scenario that uses reflection:
If you have server A and b,a running centralized task distribution, B receives the task given by a
A through the queue to send the task to B, the task is to let B execute the Math.pow method, B to get the task in the queue, it must be used to the reflection
In practical applications, the queue used should be a Message Queuing server, such as a server such as REDIS,ZEROMQ, where the Python queue module is used to simulate
Define a queue:
Import Queue
Queue=queue.queue ()
Define ServerA
Def ServerA ():
dict={' server ': ' B ', ' module ': ' Math ', ' func ': ' POW '}
Queue.put (Dict)
Run the ServerA function to put the task you want to perform in the queue.
ServerA ()
Define serverb,b to get tasks in the task queue:
Def ServerB ():
Task=queue.get ()
#首先需要导入module
If task[' server ']== ' B ':
mathmodule=__import__ (task[' module ')
#其次在module中找到task [' func ']
Func=getattr (mathmodule,task[' func ')
Print func (2,4)
Run the ServerB function to perform the corresponding task.
ServerB ()
2.7 Re
Python in the RE module provides regular expression-related operations
Character:
. Match any character other than line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
Number:
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
Match
# match, starting from the starting position match, match successfully returned an object, unmatched successfully returned none
Match (pattern, string, flags=0)
# Pattern: Regular model
# string: String to match
# Falgs: Matching mode
X VERBOSE Ignore whitespace and comments for nicer looking RE ' s.
I IGNORECASE Perform case-insensitive matching.
M MULTILINE "^" matches the beginning of lines (after a newline)
As well as the string.
"$" matches the end of lines (before a newline) as well
As the end of the string.
S Dotall "." matches any character @ all, including the newline.
A ASCII for string patterns, make \w, \w, \b, \b, \d, \d
Match the corresponding ASCII character categories
(rather than the whole Unicode categories, which is the
Default).
For bytes patterns, this flag was the only available
Behaviour and Needn ' t be specified.
L locale make \w, \w, \b, \b, dependent in the current locale.
U UNICODE for compatibility only. Ignored for string patterns (it
is the default), and forbidden for bytes patterns.
Copy Code
# no grouping
r = Re.match ("h\w+", origin)
Print (R.group ()) # gets all the results that match to
Print (R.groups ()) # Gets the grouped results that are matched in the model
Print (R.groupdict ()) # Gets the grouped results that are matched in the model
# There are groups
# Why should I have a group? Extracts the specified content that matches successfully (first matches all the regular, then matches the successful local content extracted)
r = Re.match ("H (\w+)." * (? p<name>\d) $ ", origin)
Print (R.group ()) # gets all the results that match to
Print (R.groups ()) # Gets the grouped results that are matched in the model
Print (R.groupdict ()) # gets all the groups in the model that have the key executed in the matching group
Copy Code
Search
1
2
# Search, browse the entire string to match the first one, the unmatched successful return none
# Search (pattern, string, flags=0)
Copy Code
# no grouping
r = Re.search ("a\w+", origin)
Print (R.group ()) # gets all the results that match to
Print (R.groups ()) # Gets the grouped results that are matched in the model
Print (R.groupdict ()) # Gets the grouped results that are matched in the model
# There are groups
r = Re.search ("A (\w+)." * (? p<name>\d) $ ", origin)
Print (R.group ()) # gets all the results that match to
Print (R.groups ()) # Gets the grouped results that are matched in the model
Print (R.groupdict ()) # gets all the groups in the model that have the key executed in the matching group
Copy Code
FindAll
# FindAll, gets a non-repeating match list, and if one group is returned as a list, each match is a string, and if there are multiple groups in the model, it is returned as a list, and each match is ganso;
# An empty match will also be included in the result
#findall (Pattern, string, flags=0)
Copy Code
# no grouping
r = Re.findall ("a\w+", origin)
Print (R)
# There are groups
Origin = "Hello Alex bcd abcd Lge ACD 19"
r = Re.findall ("A ((\w*) c) (d)", origin)
Print (R)
Copy Code
Sub
# Sub, replacing the specified location string that matches the success
Sub (pattern, REPL, String, count=0, flags=0)
# Pattern: Regular model
# Repl: String or executable object to replace
# string: String to match
# count: Specify the number of matches
# Flags: Matching pattern
Demo
Split
Split, split string according to regular match
Split (pattern, String, maxsplit=0, flags=0)
# Pattern: Regular model
# string: String to match
# Maxsplit: Specify the number of splits
# Flags: Matching pattern
Copy Code
# no grouping
Origin = "Hello Alex bcd Alex Lge" Alex ACD 19 "
r = Re.split ("Alex", origin, 1)
Print (R)
# There are groups
Origin = "Hello Alex bcd Alex Lge" Alex ACD 19 "
R1 = Re.split ("(Alex)", origin, 1)
Print (R1)
r2 = Re.split ("(Al (ex))", origin, 1)
Print (R2)
Copy Code
Ip:
^ (25[0-5]|2[0-4]\d| [0-1]?\d?\d) (\. ( 25[0-5]|2[0-4]\d| [0-1]?\d?\d)] {3}$
Phone Number:
^1[3|4|5|8][0-9]\d{8}$
Mailbox:
[a-za-z0-9_-] [Email protected] [A-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +
The depth of Python learning