Python review-Chowder

Source: Internet
Author: User

Collection (Set) method

Union: Unions |

Intersection: Intersection &

Difference Set: Difference-

Symmetric difference set: symmetric_difference ^

Examples are as follows:

a={1,2,3,4,5}b={2,3,5,7,9}Print(A |b)PrintAb)Print(A ^b)Print(A &b)Print("*"*50)Print("*"*50)Print(a.union (b))Print(a.difference (b))Print(a.symmetric_difference (b))Print(a.intersection (b))######{1, 2, 3, 4, 5, 7, 9}{1, 4}{1, 4, 7, 9}{2, 3, 5}****************************************************************************************************{1, 2, 3, 4, 5, 7, 9}{1, 4}{1, 4, 7, 9}{2, 3, 5}
function comment

You can annotate the parameters of a function and the type of the return value when you define a function

Attention:

1) function comments only refer to prompt action, and do not type check and limit the input parameters or return values

2) function comments can be viewed through function.__annotations__

defFoo (x:int, Y:float)Str:returnSTR (x +y)Print(foo (3, 4))Print(Foo ('a','b'))Print(foo.)__annotations__)7ab{  "  x   ": < class   " int  ",  " y   ": <class   " float  ",  "  return   ": <class  "  str   " >}  defF (ham:str, eggs:str ='eggs'),Str:Print("Annotations:"F.__annotations__)     Print("Arguments:", ham, eggs)returnHam +' and'+EGGSF ('spam')Annotations: { 'Ham': <class 'Str','return': <class 'Str','eggs': <class 'Str'>}arguments:spam Eggs'spam and Eggs'
Requests module Add request header

Django receives the data submitted by the requests module, and needs to add a custom request header to follow certain rules

1) Dictionary headers defines the key-value pair for the request header, with the word split between the words in the key (_ segmentation is not recognized)

2) After the request header arrives at the service end, it exists in the dictionary after parsing requests. META

3) The key of the client request header has changed on the service side

1. Add HTTP_ before client's key

2. Convert the above processed keys to uppercase

3. Replace-to _

Auth-api ====> request. Meta.get ("Http_auth_api")

Import requests
Response=requests.get ("http://127.0.0.1:8000/test.html", headers={' Auth-api': Auth_header_val})
Print (Response.text)

Part of the source code is as follows ( need further finishing )

#django. core.servers.basehttp.py
ClassWsgirequesthandler (simple_server. Wsgirequesthandler, Object): ...defGet_environ (self):#Strip all headers with underscores in the name before constructing #The WSGI environ. This prevents header-spoofing based on ambiguity #between underscores and dashes both normalized to underscores in WSGI #env VARs. Nginx and Apache 2.4+ both do the as well. for K, V in self.headers.items (): if '_' in k: del self.headers[k] returnSuper (Wsgirequesthandler, self). Get_environ ()

 #wsgiref. simpleserver.py

ClassWsgirequesthandler (Basehttprequesthandler): Server_version="wsgiserver/"+__version__ defGet_environ (self): env=self.server.base_environ.copy () env['Server_protocol'] =self.request_version env['Server_software'] =self.server_version env['Request_method'] =Self.commandif '?' inchSelf.path:path,query= Self.path.split ('?', 1) Else: Path,query= Self.path,"'env['Path_info'] = urllib.parse.unquote (path,'iso-8859-1') env['query_string'] =Query Host=self.address_string ()ifHost! =Self.client_address[0]: env['Remote_host'] =host env['REMOTE_ADDR'] =Self.client_address[0]ifSelf.headers.get ('Content-type') isnone:env['Content_Type'] =Self.headers.get_content_type ()Else: env['Content_Type'] = self.headers['Content-type'] Length= Self.headers.get ('Content-length') iflength:env['content_length'] =length forKvinchSelf.headers.items (): k =k.replace ('-','_'). Upper ();v=V.strip ()ifKinchenv:Continue #Skip content length, type,etc. if 'HTTP_'+kinchenv:env['HTTP_'+k] + =','+v#comma-separate Multiple Headers Else: env[ 'http_'+k] =vreturnEnv

Python review-Chowder

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.