# 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. vue中v-model详解

    vue中经常使用到<input>和<textarea>这类表单元素,vue对于这些元素的数据绑定和我们以前经常用的jQuery有些区别.vue使用v-model实现这些标签数据 ...

  2. js中数组Array 详解

    unshift:将参数添加到原数组开头,并返回数组的长度    pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined    push:将参数添加到原数组末尾,并返回数组 ...

  3. 英语单词escapes

    escapes 来源 [root@centos7 ~]# help echo echo: echo [-neE] [arg ...] Write arguments to the standard o ...

  4. 聊聊Dubbo - Dubbo可扩展机制实战

    1. Dubbo的扩展机制 在Dubbo的官网上,Dubbo描述自己是一个高性能的RPC框架.今天我想聊聊Dubbo的另一个很棒的特性, 就是它的可扩展性. 如同罗马不是一天建成的,任何系统都一定是从 ...

  5. 【HDOJ6604】Blow up the city(支配树)

    题意:给定一个n点m边的DAG,将只有入边的点称为周驿东点 q次询问,每次给定a,b两点,询问删去某个点x和其相连的所有边,能使a,b至少其中之一不能到达任何周驿东点的x的个数 n,q<=1e5 ...

  6. paper 163: opencv知识点回顾

    From Here: https://zhuanlan.zhihu.com/p/24425116 Python下使用OpenCV 本篇将介绍和深度学习数据处理阶段最相关的基础使用,并完成4个有趣实用的 ...

  7. ExportOptions.plis是什么?

    参考: 具体每个字段的说明,参考: https://group.cnblogs.com/topic/80752.html 为什么自动化打包时,需要用到ExportOptions.plis文件,参考: ...

  8. 记录规则(recording rules)与告警规则(alerting rule)

    记录规则(recording rules) 配置规则 Prometheus支持两种类型的规则,可以对其进行配置,然后定期进行评估:记录规则和警报规则.要在Prometheus中包含规则,请创建一个包含 ...

  9. java File I/O

    File类: 常用方法: boolean exists( ):判断文件或目录是否存在 boolean isFile( ):判断是否是文件 boolean isDirectory( ):判断是否是目录 ...

  10. python学习那点事---列表生成式实现大小写字母相互转换

    题目: 已知列表list=["pYTHON","iS",eASY],要求使用列表生成式实现,生成一个新的列表,要求将大写字母转换为小写字母,小写字母转换为大写字 ...