码迷,mamicode.com
首页 > 编程语言 > 详细

python复习-杂烩

时间:2017-10-01 23:00:15      阅读:276      评论:0      收藏:0      [点我收藏+]

标签: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__)

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复习-杂烩

标签:toc   ota   for   method   scores   text   print   注释   def   

原文地址:http://www.cnblogs.com/NewTaul/p/7618021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!