python複習-雜燴

來源:互聯網
上載者:User

標籤:toc   ota   for   method   scores   text   print   注釋   def   

集合(set)方法

並集:union |

交集:intersection &

差集:difference -

對稱差集: symmetric_difference ^

執行個體如下:

a={1,2,3,4,5}b={2,3,5,7,9}print(a | b)print(a - b)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}
函數注釋

定義函數時可對函數的參數以及傳回值的類型進行注釋

注意:

1)函數注釋僅僅提到提示作用,並不會對輸入的參數或或傳回值進行類型檢查和限制

2)函數注釋可通過Function.__annotations__進行查看

def foo(x: int, y: float) -> str:    return str(x + y)print(foo(3, 4))print(foo(‘a‘, ‘b‘))print(foo.__annotations__)7ab{‘x‘: <class ‘int‘>, ‘y‘: <class ‘float‘>, ‘return‘: <class ‘str‘>}def f(ham: str, eggs: str = ‘eggs‘) -> str:     print("Annotations:", f.__annotations__)     print("Arguments:", ham, eggs)     return ham + ‘ and ‘ + eggsf(‘spam‘)Annotations: {‘ham‘: <class ‘str‘>, ‘return‘: <class ‘str‘>, ‘eggs‘: <class ‘str‘>}Arguments: spam eggs‘spam and eggs‘
requests模組添加要求標頭

django中接收requests模組提交的資料,需添加自訂的要求標頭需遵循一定的規則

1) 字典headers定義要求標頭的索引值對,鍵中的單詞之間以-分割(_分割不會被識別)

2)要求標頭到達服務端後解析後存在於字典中requests.META

3)用戶端要求標頭的鍵在服務端發生了改變 

  1. 用戶端的鍵之前添加http_

  2. 將上述處理的鍵轉化為大寫

  3. 將-替換為_

  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)

部分源碼如下(需進一步整理)

#django.core.servers.basehttp.py
class WSGIRequestHandler(simple_server.WSGIRequestHandler, object): ...... def get_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 this as well. for k, v in self.headers.items(): if ‘_‘ in k: del self.headers[k] return super(WSGIRequestHandler, self).get_environ()

 

#wsgiref.simpleserver.py

class WSGIRequestHandler(BaseHTTPRequestHandler): server_version = "WSGIServer/" + __version__ def get_environ(self): env = self.server.base_environ.copy() env[‘SERVER_PROTOCOL‘] = self.request_version env[‘SERVER_SOFTWARE‘] = self.server_version env[‘REQUEST_METHOD‘] = self.command if ‘?‘ in self.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() if host != self.client_address[0]: env[‘REMOTE_HOST‘] = host env[‘REMOTE_ADDR‘] = self.client_address[0] if self.headers.get(‘content-type‘) is None: env[‘CONTENT_TYPE‘] = self.headers.get_content_type() else: env[‘CONTENT_TYPE‘] = self.headers[‘content-type‘] length = self.headers.get(‘content-length‘) if length: env[‘CONTENT_LENGTH‘] = length for k, v in self.headers.items(): k=k.replace(‘-‘,‘_‘).upper(); v=v.strip() if k in env: continue # skip content length, type,etc. if ‘HTTP_‘+k in env: env[‘HTTP_‘+k] += ‘,‘+v # comma-separate multiple headers else: env[‘HTTP_‘+k] = v return env

 

python複習-雜燴

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.