Python学习总结笔记
# windows
python -m http.server [<portNo>]
# linux
python -m SimpleHTTPServer [<portNo>]
def fun(a,*args,**kwargs):
print("a = "+str(a))
for i in args:
print('===================')
print(i)
print('===================')
for key,val in kwargs.items():
print(["key : "+str(key)," val : "+str(val)])
if __name__=="__main__":
fun(1,'a','b','c',*('t',2,3),**{'c':1,'b':2},s=5,u=6)
# output
a = 1
a
b
c
t
['key : c', ' val : 1']
['key : b', ' val : 2']
['key : s', ' val : 5']
['key : u', ' val : 6']
# pip install 2to3
2to3 -w example.py
# pip install autopep8
autopep8.exe --in-place --aggressive --aggressive test.py
import asyncio
import time
import concurrent.futures as cf
import requests
from bs4 import BeautifulSoup
def get_title(i):
url = 'https://movie.douban.com/top250?start={}&filter='.format(i*25)
headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-us",
"Connection": "keep-alive",
"Accept-Charset": "GB2312,utf-8;q=0.7,*;q=0.7"}
r = requests.get(url,headers=headers)
soup = BeautifulSoup(r.content)
lis = soup.find('ol', class_='grid_view').find_all('li')
for li in lis:
title = li.find("span",class_="title").text
print(title)
async def title():
with cf.ThreadPoolExecutor(max_workers = 10) as excutor:
loop = asyncio.get_event_loop()
futures = (loop.run_in_executor(excutor,get_title,i) for i in range(10))
for result in await asyncio.gather(*futures):
pass
def myfunc(i):
print("start {}th".format(i))
time.sleep(1)
print("finish {}th".format(i))
async def main():
with cf.ThreadPoolExecutor(max_workers = 10) as executor:
loop = asyncio.get_event_loop()
futures=(loop.run_in_executor(executor,myfunc,i) for i in range(10))
for result in await asyncio.gather(*futures):
pass
if __name__=="__main__":
time1=time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(title())
#下面代码供测试速度对比
# for i in range(10):
# get_title(i)
print("花费了:"+str(time.time()-time1)+"s")
# Ascii85编解码
import base64
s = "Hello World!"
b = s.encode("UTF-8")
e = base64.a85encode(b)
s1 = e.decode("UTF-8")
print("ASCII85 Encoded:", s1)
b1 = s1.encode("UTF-8")
d = base64.a85decode(b1)
s2 = d.decode("UTF-8")
print(s2)
# base64编解码
import base64
s = "Hello World!"
b = s.encode("UTF-8")
e = base64.b64encode(b)
s1 = e.decode("UTF-8")
print(s1)
#base85编解码
import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base85 Encode the bytes
e = base64.b85encode(b)
# Decoding the Base85 bytes to string
s1 = e.decode("UTF-8")
# Printing Base85 encoded string
print("Base85 Encoded:", s1)
# Encoding the Base85 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base85 bytes
d = base64.b85decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)
import configparser
config = configparser.ConfigParser()
# config['settings']={'email':"2561908792@qq.com",'phone':'15827993562'}
# with open('config.txt','w') as configfile:
# config.write(configfile)
if __name__=="__main__":
config.read("config.txt")
for key,val in config['settings'].items():
print("key : "+key+" val : "+val)
# for key, val in config['host'].items():
# print("key : " + key + " val : " + val)
创建空双端队列:
dl = deque() # deque([]) creating empty deque
使用一些元素创建deque:
dl = deque([1, 2, 3, 4]) # deque([1, 2, 3, 4])
向deque添加元素:
dl.append(5) # deque([1, 2, 3, 4, 5])
在deque中添加元素左侧:
dl.appendleft(0) # deque([0, 1, 2, 3, 4, 5])
向deque添加元素列表:
dl.extend([6, 7]) # deque([0, 1, 2, 3, 4, 5, 6, 7])
从左侧添加元素列表:
dl.extendleft([-2, -1]) # deque([-1, -2, 0, 1, 2, 3, 4, 5, 6, 7])
使用.pop()元素自然会从右侧删除一个项目:
dl.pop() # 7 => deque([-1, -2, 0, 1, 2, 3, 4, 5, 6])
使用.popleft()元素从左侧删除项目:
dl.popleft() # -1 deque([-2, 0, 1, 2, 3, 4, 5, 6])
按值删除元素:
dl.remove(1) # deque([-2, 0, 2, 3, 4, 5, 6])
反转deque中元素的顺序:
dl.reverse() # deque([6, 5, 4, 3, 2, 0, -2])
>>> import dis
>>> def hello():
... print "Hello, World"
...
>>> dis.dis(hello)
2 0 LOAD_CONST 1 ('Hello, World')
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 0 (None)
8 RETURN_VALUE
# 生成器表达式
a=(x*2 for x in range(10)) #<generator object <genexpr> at 0x000001A3ACC7CF48>
next(a)
print([i for i in a])
b=[x*2 for x in range(10)] #list
# 生成器
def fib(n):
prev,curr = 0,1
while n>0:
n-=1
yield curr
prev,curr=curr,curr+prev
print(i for i in fib(10))
# lambda外汇返佣
s=lambda x:x*x
s(2)
# 格式化输出
a="this {} a new {}".format("is","start")
b="this %s a new %s"%("is","start")
import time
import requests
from bs4 import BeautifulSoup
def timer(info):
def decorator(func):
def wrapper(*args,**kwargs):
if info=="m":
start=time.time()
func(*args,**kwargs)
print((time.time()-start)/60)
if info=="s":
start=time.time()
func(*args,**kwargs)
print((time.time()-start))
return wrapper
return decorator
@timer('s')
def get_title(s):
for i in range(s):
url = 'https://movie.douban.com/top250?start={}&filter='.format(i*25)
headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-us",
"Connection": "keep-alive",
"Accept-Charset": "GB2312,utf-8;q=0.7,*;q=0.7"}
r = requests.get(url,headers=headers)
soup = BeautifulSoup(r.content)
lis = soup.find('ol', class_='grid_view').find_all('li')
for li in lis:
title = li.find("span",class_="title").text
print(title)
class Timer:
def __init__(self,func):
self._func=func
def __call__(self, *args, **kwargs):
start=time.time()
result = self._func(*args,**kwargs)
end = time.time()
print("time : "+str(end-start))
return result
@Timer
def get_title1(s):
for i in range(s):
url = 'https://movie.douban.com/top250?start={}&filter='.format(i*25)
headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) ",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-us",
"Connection": "keep-alive",
"Accept-Charset": "GB2312,utf-8;q=0.7,*;q=0.7"}
r = requests.get(url,headers=headers)
soup = BeautifulSoup(r.content)
lis = soup.find('ol', class_='grid_view').find_all('li')
for li in lis:
title = li.find("span",class_="title").text
print(title)
if __name__=="__main__":
get_title1(10)
原文链接:https://blog.csdn.net/qq_43221499/article/details/103451060
Python学习总结笔记的更多相关文章
- Python学习基础笔记(全)
换博客了,还是csdn好一些. Python学习基础笔记 1.Python学习-linux下Python3的安装 2.Python学习-数据类型.运算符.条件语句 3.Python学习-循环语句 4. ...
- [python学习手册-笔记]001.python前言
001.python前言 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明 ...
- [python学习手册-笔记]002.python核心数据类型
python核心数据类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明 ...
- [python学习手册-笔记]003.数值类型
003.数值类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...
- [python学习手册-笔记]004.动态类型
004.动态类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...
- python学习应用笔记(一)
之前一直用c++写程序 所以考虑程序一般都比较容易往数据结构的方向想 而自己设计数据结构往往要费很大事 昨天看了一下python 发现脚本语言 真是厉害 用来进行模拟运算确实不错 可以先 ...
- python学习第一次笔记
python第一次学习记录 python是什么编程语言 变成语言主要从以下几个角度进行分类,编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言. 1.1编译型与解释性 编译型:一次性将 ...
- Python 学习开发笔记之IO操作
文件或者目录的路径操作 获取当前工作目录 import os import sys cwd = os.getcwd() 路径的拼接 os.path.join(path,"dir") ...
- python学习总结笔记(一)
1.raw_input("请输入:")提示录入信息,读取录入的字符串返回你录入的字符串2.os.environ 获取所有系统的环境变量,返回一个字典.3.str与repr区别str ...
- python 学习常见问题笔记
1.for...if...构建List segs = [v for v in segs if not str(v).isdigit()]#去数字 https://www.cnblogs.com/eni ...
随机推荐
- CSP-S2019退役记/爆内存记
DAY 0 准备出发. 出发前教练说遇到事不慌,打电话,又听教练说了说历年赶车经历. 然后这趟路上居然没有什么大事. 在车上有些闲,于是就和其他人聊了会天,聊着聊着没意思,就用手机翻博客园. 这样就不 ...
- 【靶场练习_sqli-labs】SQLi-LABS Page-4 (Challenges)
Less-54: ?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where tabl ...
- window安装nginx
下载安装 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html: 下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如 ...
- 后端技术杂谈3:Lucene基础原理与实践
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- Classic IPC Problems 经典的进程间通信问题
The Producer-Consumer Problem Presenter Notes: 生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bo ...
- oracle 远程访问
oracle 本机能连外部访问不了 新装的 oracle 数据库经常会出现本地计算机能连接,但是局域网内的其他计算机不能连接的问题,如果出现此问题可以参考此文来解决. 本文中用的数据库版本为 Orac ...
- Nuget-Doc:Nuget 简介
ylbtech-Nuget-Doc:Nuget 简介 1.返回顶部 1. NuGet 简介 2019/05/24 适用于任何现代开发平台的基本工具可充当一种机制,通过这种机制,开发人员可以创建.共享和 ...
- 动态创建类/ swizzle class
动态创建类 Class subclass = objc_allocateClassPair(baseClass, subclassName, );//生成,指定父类 //添加方法,变量...一些操作 ...
- Bash Shell中命令行选项/参数处理
0.引言 写程序的时候经常要处理命令行参数,本文描述在Bash下的命令行处理方式. 选项与参数: 如下一个命令行: ./test.sh -f config.conf -v --prefix=/home ...
- 服务端:WCF服务层安全检查核心类
using System.Data; using CSFrameworkV4_5.Common; using CSFrameworkV4_5.Core.SystemSecurity; using CS ...