# 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学习总结笔记的更多相关文章

  1. Python学习基础笔记(全)

    换博客了,还是csdn好一些. Python学习基础笔记 1.Python学习-linux下Python3的安装 2.Python学习-数据类型.运算符.条件语句 3.Python学习-循环语句 4. ...

  2. [python学习手册-笔记]001.python前言

    001.python前言 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明 ...

  3. [python学习手册-笔记]002.python核心数据类型

    python核心数据类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明 ...

  4. [python学习手册-笔记]003.数值类型

    003.数值类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...

  5. [python学习手册-笔记]004.动态类型

    004.动态类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...

  6. python学习应用笔记(一)

    之前一直用c++写程序  所以考虑程序一般都比较容易往数据结构的方向想 而自己设计数据结构往往要费很大事  昨天看了一下python  发现脚本语言 真是厉害    用来进行模拟运算确实不错  可以先 ...

  7. python学习第一次笔记

    python第一次学习记录 python是什么编程语言 变成语言主要从以下几个角度进行分类,编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言. 1.1编译型与解释性 编译型:一次性将 ...

  8. Python 学习开发笔记之IO操作

    文件或者目录的路径操作 获取当前工作目录 import os import sys cwd = os.getcwd() 路径的拼接 os.path.join(path,"dir") ...

  9. python学习总结笔记(一)

    1.raw_input("请输入:")提示录入信息,读取录入的字符串返回你录入的字符串2.os.environ 获取所有系统的环境变量,返回一个字典.3.str与repr区别str ...

  10. python 学习常见问题笔记

    1.for...if...构建List segs = [v for v in segs if not str(v).isdigit()]#去数字 https://www.cnblogs.com/eni ...

随机推荐

  1. 51nod 1298:圆与三角形(计算几何)

    题目链接 判断圆和三角形是否相交   可以转化为   判断三条线段是否和圆相交 #include<iostream> #include<cstdio> #include< ...

  2. 跨域共享cookie

    1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...

  3. Codeforces Round #578 (Div. 2) E. Compress Words (双哈希)

    题目:https://codeforc.es/contest/1200/problem/E 题意:给你n个单词,你需要把他合成成一个句子,相邻的两个单词,相邻部分相同的话可以把其中一个的删掉 思路:因 ...

  4. AcWing 244. 谜一样的牛 (树状数组+二分)打卡

    题目:https://www.acwing.com/problem/content/245/ 题意:有n只牛,现在他们按一种顺序排好,现在知道每只牛前面有几只牛比自己低,牛的身高是1-n,现在求每只牛 ...

  5. [HDU4969]Just a Joke

    题目:Just a Joke 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4969 分析: 呀,根本不会做,5555~(逃 http://blog.cs ...

  6. 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

    实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...

  7. soj#552 449E Jzzhu and Squares

    分析 https://www.cnblogs.com/Memory-of-winter/p/11209128.html 代码 #include<bits/stdc++.h> using n ...

  8. STM32几个IO的工作模式

    浮空,顾名思义就是浮在空中,上面用绳子一拉就上去了,下面用绳子一拉就沉下去了.  开漏,就等于输出口接了个NPN三极管,并且只接了e,b. c极 是开路的,你可以接一个电阻到3.3V,也可以接一个电阻 ...

  9. 抓包工具charles下载安装(MAC版)

    什么是charles? charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器,当浏览器连接Charles的代理访问互联网时,Charles可以监控浏览器发送和接收的所有数据.它允许一 ...

  10. 关于函数lower_bound()如何使用的问题

    这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...