1. 程式人生 > >python復習-雜燴

python復習-雜燴

以及 apach ren headers dash .com super 添加 服務

集合(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__)

7
ab
{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 + eggs f(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復習-雜燴