Go deep into Python Summary

Source: Internet
Author: User

English dive in python can be found below Chinese translation http://linuxtoy.org/docs/dip/toc/index.html

 

Module 3.1

_ Name __of the module. When the module is imported, it is the module name. When the module is executed as main, It is _ main __

 

3.2 dictionary

Key-value pairs in dictionary are of any type. They can be mixed in the same dictionary.

The dictionary key is case sensitive and must be of the constant type. You can use del DIC [Key] to delete an element.

Items () returns a list, where the elements are binary tuple, t [0] is key, t [1] = Val

 

3.3 list

List also supports the overload + operation, which is used to connect two lists and return a list. Therefore, it does not have extended for efficient execution. List also supports the + = operation to connect two lists.

The list append () operation adds an element to the end of the list. to connect two lists, use extend instead of append.

* It can act on the list and act as a repeat.

List can be referenced in [] with two types of slice objects. One is [start: end: Step], and the other is the slice returned by the [sliece (START, end, step)] operation.

The built-in operation range (START, end, step) returns a list.

 

3.4 tuple

Tuple cannot add, delete elements to, and return functions such as indexes, but in exists. Tuple itself can be considered immutable, so it can be used as the key of a dictionary.

Tuple and list can be converted explicitly to freeze or unfreeze the content. Tuple can be considered as the enum type in C. The elements cannot be changed and can be randomly accessed. The variable returns an integer by position.

 

When using a variable, you do not need to declare its type first, but the variables used must be previously defined.

Python supports assigning values to multiple variables at a time, such as t = (1, 2, 3) (x, y, z) = T.

 

3.5 format a string

Similar to sprintf in C, print "% s,/T % d" % (STR, 10)

When defining
Tuple is a comma. If comma is omitted, Python
Don't know(Usercount)
Is it a tuple or variable that only contains one element?Usercount
.

T = (1) // type (t) => int

T = (1,) // type (t) => tuple

 

3.6 ing list

Use [] to apply a function to the first element in lista to generate another list. For example, [expression for ELEM in list1]

It is safe to map a list to yourself.

4.5 Filter list

If [express for ELEM in list1 if if_express] is used, the filter expression starting with if is called a filter expression. In python, the elements are added to the ing list only when the filter expression returns true.

 

3.7 character-based join and division of elements in a list

"Delem". Join (list1) splits the characters in the list by delem and concatenates them into a string.

The opposite operation can call split ("delem", CNT) on a string, which splits a STR by delem for CNT times, puts the elements in the list, and returns the list;

Useful Tips ""
. Join (string. Split (), which removes unnecessary spaces in the string and uses a single space to format and connect them.

 

4.2 Functions

The list of function parameters in python can be viewed as a dictionary. parameters can be passed in a non-defined order. However, you need to explicitly specify the parameter name.

STR and type can accept any type of object. The former returns its STR, and the latter returns its type. These built-in functions are included in _ builtin.

Getattr (OBJ, 'name' [, default]) returns an attribute reference called name on OBJ. This function can act on any object, return method reference or data reference.

 

4.6 logical expressions and/or

UseAnd
In a Boolean environment, the value of the expression is calculated from left to right until false is returned. In this case, the false object is returned. Otherwise, the last (true) object is returned.

0
,''
,[]
,()
,{}
,None

False in a Boolean environment; everything else is true.

UseOr
In a Boolean environment, the value is calculated from left to right, just likeAnd
Same. If one value is true,Or
Return the value immediately. Otherwise, the last (false) object is returned.

And-or
Skill, that isBool
AndA
OrB

Expression, whenA

When the value in a Boolean environment is false

Language ExpressionsBool
?A
:B

Work that way. When a is true, it can be like a three-object expression in C. The security method is (1 and [a] or [B]) [0].

 

4.7 lambda expressions

Only single-row functions can be defined in Python. However, it can accept multiple parameters. Common functions can be closed in any place where Lambda functions are used, and there is no complexity limit.

 

5.2 object Import

Module object

The import module does not make the methods below the module visible. If you want to use the methods below, you need to use module. Method to reference them. If you want to use it frequently, you can use the from module import method to make the method visible. From module import *

 

 

5.3 categories

If _ init __is defined in the class and your class inherits other classes, you need __() explicitly call the _ Init () function of the base class. Although _ init _ () cannot be called a constructor, it undertakes the initialization of an object after construction. When you need an object instance of a class, you need to call the class and input the parameter defined in _ init.

The data member of a class can be suddenly added to the class at the first definition and can be dynamically added or deleted. Unlike C ++, when we want to reference data or method members within the class, we need to explicitly specify self (similar to this pointer ).

Each class has a built-in attribute _ module __, which is used to describe the module to which the object belongs.

 

5.6 dedicated class methods

Some built-in functions in the form of _ * _ are defined by the class and called by Python's external functions when appropriate, for example, each class can define its own _ STR _ (). When the built-in function STR () acts on a class, the class's _ STR __() will be called. These are called types of self-expression ability.

In python, check the identity of the two objects. Use is to check whether the two objects are equal =

 

5.8 class attributes

The Class Object identifies its class by the attribute _ class _. We can use it to simulate static variables in the C ++ class. Python does not contain private variables, but all variables starting with _ are named by mangle, which is invisible to the outside.

 

5.9 private functions

If a python
The name of a function, class method, or attribute starts with two underscores.
(But not the end), it is private; all others are public.
Python
No class MethodProtection

(Can only be used in their own classes and subclasses ). Class method or private
(Only available in their own classes) or public (available anywhere ).


6.1 Exception Handling

If you use a hierarchical exception processing logic, you can use else to process the hierarchical logic when the exception does not occur.

 

6.2 File Operations

Standard built-in functions include open () Seek () Tell () read () write () Close ()

 

6.3 path and directory operations

Functions in OS. Path include tools such as split () splitext () and join ().

In OS, operations such as isfile (), isdir (), chdir (), fchdir (), getcwd (), fdopen (), popen (), DUP (), fstat () and so on

 

7.1 Regular Expression

  • ^
    Start of matching string.
  • $
    Matches the end of a string.
  • /B
    Match the boundary of a word.
  • /D
    Match any number.
  • /D
    Match any non-numeric characters.
  • X?
    Match an optionalX
    Character (in other words, it matches once or 0 timesX
    Characters ).
  • X *
    Matches 0 times or multiple timesX
    Character.
  • X +
    Match once or multiple timesX
    Character.
  • X {n, m}
    MatchX
    Character, at leastN
    Times, upM
    Times.
  • (A | B | C)
    Either matchA
    , Or matchB
    , Or matchC
    .
  • (X)
    GenerallyRemembered Group)

    . You can useRe. Search
    The function returnsGroups ()
    Function to obtain its value.

Python Regular Expression howto

 

8. html Processing

Basic HTML knowledge

 

8.5 namespace

Locals () returns the variable in the local namespace in the dict format, but it is a duplicate. modifying this dictionary will not change the variable value in the local namespace. Globals () returns a variable in the global namespace in the dict format. It is a reference. modifying this dictionary will change the variable value in the global namespace.

In a python program, there are several namespaces, local, global, and built-in. The search for the name is also in this order.

Local namespace: In the function or class method.

Global namespace: Used in the current Module

Built-in namespace: visible to any module.

 

8.6 dictionary-Based String formatting

The following format is used: dict = {key1: value1, key2: value2,...} "we can see key1's value is % (key1) S"

 

8.8 how sgmlparser works in sgmllib

Sgmlparser defines how to process the markup language. by inheriting this class and defining functions similar to start_a () end_a, when the parser encounters a tag <A> and </a>, it checks whether the user has defined the corresponding processing function. If yes, it calls the user function to perform the operation. Otherwise, unkonw_starttag () and unknown_endtag () are called for processing. You can reload these functions to define the operation you want.

 

8.9 dynamic Python

Python can get the corresponding attributes and classes or instances at runtime through strings. For attributes, you can use getattr (), the classes in the namespace can be directly found in the dictionary returned by globals () by the STR type name.

 

9 XML Processing

9.2 Package concept

The most basic module is the. py file. A package is a directory. Packages can be nested. nested packages or included modules can be obtained through. The package can be imported into a module by the Import Name Defined in the _ init _. py file in the package directory. The package directory must contain the _ init _. py file, which can be empty but must exist. Otherwise, the directory is not identified as a package and imported into its internal modules.

9.3 XML processing interface

Xmldoc = Dom. parse ()

Root = xmldoc. childnodes [0]

Note that, in the child nodes under the root, the labels are separated by a hard carriage return as the child nodes. The data is obtained under the leaf node through childnodes [0]. Data.

You can use these two interfaces to traverse simple XML files. Any node has the toxml () method and the childnodes attribute.

Each node can have attributes and text, which can be accessed through attributes or data. Attributes returns a dictionary containing the attribute key-value pairs. They can be obtained through keys () and values () respectively.

9.4 character encoding

The encoding in the environment is obtained or specified through SYS. getdefaultencoding () or SYS. setdefaultencoding. For each file #-*-coding: UTF-8 -*-

9.5 search XML documents

You can use getelementsnamebytagname () to return a group of child trees with the same tag name. You can traverse the returned Val [I] in sequence.

 

10 scripts and streams

10.1 file-like object

All objects that support read (size = none) operations. Many functions are defined in Python to directly operate the file-like object, instead of explicitly calling the read and close operations of the file-like object, such as XML. minidom. parse () function.

 

10.2 input/output redirection

You can save and rewrite SYS. stdout, SYS. stdin, and SYS. stderr as other file objects to redirect input and output. To avoid specifying a file (for exampleBinary. xml
), You need to specify-

", It will make your script load the script from the standard input, rather than from the magnetic
A specific file on the disk.

 

10.3 process command line parameters

The sys. argv and getopt modules can be used.

Import getopt <br/> def test_getopt (argv = none): <br/> If argv = none: <br/> Print ("error ") <br/> return <br/> opts, argS = getopt. getopt (argv, "HP: D:", ["help", "Port", "DST"]) <br/> Print (OPTs) <br/> Print (ARGs) <br/> for opt, ARG in opts: <br/> If opt in ('-H',' -- help '): <br/> Print ("user need help") <br/> Elif opt in ('-p',' -- port '): <br/> Print ('port is % s' % Arg) <br/> Elif opt in ('-d',' -- DST '): <br/> Print ('dst is % s' % Arg) <br/> else: <br/> Break <br/> STR = '-H-P 80-D 192.168.1.1 "C:/boot. ini "'<br/> If _ name _ =" _ main _ ": <br/> test_getopt (Str. split () <br/> test_getopt () <br/> # output is followed <br/> # [('-H ',''), ('-p', '80'), ('-d', '2017. 168.1.1 ')] <br/> # [' "C:/boot. ini "'] <br/> # user need help <br/> # port is 80 <br/> # DST is 192.168.1.1 <br/> # error <br/> #

 

11 process HTTP

In Python 3, delete htmllib and move the corresponding functions into urllib.

For more information about HTTP specifications, see rfc2616 http1.1.

 

12. Soap

12.1 WSDL

WSDL is a specification for network service description. It is written in XML, so that a service can be automatically recognized and processed by machines without the need for programmers to describe the WSDL. In addition, different enterprises can integrate various published services through UDDI to complete larger workflows.

For more information about WSDL and UDDI, see here.

12.2 http: // service.ap-southeast-1.maxcompute.aliyun-inc.com

Based on the services described in the WSDL, soap uses the HTTP protocol to transmit and receive messages between different nodes in the network, which can complete the RPC function. In this way, the user uses the WSDL to obtain the service description on the network. The machine parses the WSDL and obtains how to use the service, and uses soap to send the service call message to the server provided in the service description. The server processes received XML documents, completes related functions, and then transmits the results back using soap. This eliminates the need for senior users to focus on service details and improve work efficiency.

 

Unit 13 testing

13.1 importance

Unit testing is an important part of the core development strategy. If you want to write unit test code as early as possible (preferably before being tested)
It is important to develop and update code based on changes in code development and requirements. Unit tests cannot replace higher-level functions and system tests, but are important at every stage of development:

  • Before code development, you are forced to consider the details of requirements in an effective way.
  • Code development to prevent excessive development. After all the test cases are passed, the program development is complete.
  • When refactoring the code, make sure that the new and old features are consistent.
  • During code maintenance, you can save your face when your code changes cause code problems. ("
    YesSir

    , Check in)
    All unit tests passed in the code ......
    ")
  • When developing a team, you can be confident that the code you submit will not destroy the code of others, because you can
    Run the unit test code of another person first. (I have seen this in "code Storm. A team splits tasks, and each person develops unit tests based on their own needs and shares them with other members.
    No one may make too many deviations and the Code cannot be integrated.

13.4 requirements for test cases

A test case should be:

  • It runs independently without manual input. Unit tests should be automated.
  • You can determine whether the tested function passes or fails without manual intervention.
  • Run independently and can be isolated from other test cases (although they may test the same function ). Each test case is an isolated island.

13.5 classification of Test Cases

Positive Test Case: ensure correct output for all valid inputs

Negative test cases: All invalid input must be properly processed. Negative test cases are classified into: type and range

Completeness Test: whether the input/output mode is valid

 

For more information about the test framework, see the python unit test framework description.

 

14 test-first Development

 

15 Reconstruction

 

16 function Programming

16.5 data center ideological Programming

By using functions such as map and filter, programmers do not need to use for to process every step of the process, so that programmers only need to focus on how to process data.

 

16.6 dynamic import of modules

With the help of _ import _ ('module'), programmers can dynamically import the module name given in the form of strings.

 

17. Dynamic Functions

17.7 yield Generator

The yield generator must be used inside the function. The yield function is called the generator function. yield can use this generator function to return a value each time next () is called, until the returned object becomes non-iterated. To illustrate yield's working process, you can refer to the following code:

Def generator (): <br/> x = 1 <br/> while 1: <br/> yield x <br/> X + = 1 <br/> Gen = generator () <br/> for I in range (10 ): <br/> Print Gen. next ()

 

When we declare a generator Gen, the function is not called for execution until we call next () for the first time so that yield returns the value 1 of X in the function, then the function pauses in the yield statement until the next () call returns the value of X. It is convenient to access the generator through for. The following is an example. Of course, yield can return any object you want to return.

Def maid (max): <br/> A, B = 0, 1 <br/> while a <MAX: <br/> yield a <br/>, B = B, A + B <br/> for I in maid (1000): <br/> print I

By using dynamic functions and generators, you can make your code more abstract. You can dynamically generate various functions based on external rules in use to keep the code highly abstract and concise.

 

18 Performance Optimization

18.2 Python Performance Testing Framework

Python has a built-in time test framework. You only need to import the timeit module to conveniently perform performance tests. First, you need to generate a timer object. It requires two parameters, both of which are strings. The former is the function to be tested, and the latter is the name of the module to be imported. Note: The tested code must be in different modules from the recorded code, and an explicit import command is provided. Then you can use this object for time measurement. There are two methods: timeit () and repeat ()

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.