- 博客(16)
- 收藏
- 关注
原创 go中的指针
go中只有值传递,传递参数的时候相当于拷贝一份给函数,在函数中的操作不能改变参数的值,如果需要改变值则需要使用指针传递func swap(a, b *int) { //指针类型 *a, *b = *b, *a}func main(){ a, b := 3, 4 swap(&a, &b)}...
2020-07-06 21:16:25
128
原创 go的循环语句和基本语法
1、for,if后面的条件没有括号2、if条件里面也可以定义变量if const_file, err := ioutil.ReadFile(file_name); err != nil { fmt.Println(err) } else { fmt.Printf("%s", const_file) }3、没有while4、switch不需要break,也可以直接switch多个条件func grade(score int) string { g := "" switch { c
2020-07-04 22:23:22
143
原创 python 装饰器的使用
案例:1、斐波那契数列1,1,2,3,5,8求数列第n项的值def fibonacci(n): if n <= 1: return 1 return fibonacci(n-1) + fibonacci(n-2)2、楼梯问题,有100阶楼梯,一个人可以每次迈1~3阶,一共有多少种走法?def climb(n, steps): count = 0 if n == 0: count = 1 elif n > 0: for step in steps: coun
2020-06-23 22:05:40
180
原创 第一天学习go
第一天学习go//程序所属包 必须要有package main// 导入的依赖包import "fmt"//常量的定义const NAME string = "hh1"// 变量的声明与赋值var a string = "hh"type hh int//结构的声明type Learn struct { //结构声明}type Ilearn interface { //接口的声明}func learnHe() { fmt.Println("Hello hh")
2020-06-13 21:18:30
104
原创 aiomysql
pymysql 的异步版本,aiomysql接口与pymysql一致# -*- coding: utf-8 -*-import aiomysqlimport asyncio, timeclass aio_Mysql(object): def __init__(self, **kwargs): self.db_configs = kwargs self.coon = None self.pool = None async def
2020-05-15 13:47:19
410
原创 dict和defaultdict
defaultdict的使用假如需要对一个dict中的元素做统计1、一般最先想到的下列方法users = ['ha1', 'ha2', 'ha1', 'ha3', 'ha2']for user in users: if user not in user_dict.keys(): user_dict[user] = 1 else: user_dict[user] += 1print(user_dict)2、python 中内置的setdefault
2020-05-13 22:18:54
350
原创 python中tuple和namedtuple
tuple拆包属性,不可变对象user_tuple = ('ha', 22, 175,)name, age, height = user_tupleprint(name, age, height)第二种形式user_tuple = ('ha', 22, 175)name, *other = user_tupleprint(name, other)元组的不可变不是绝对的,在元组中放入可修改对象时则可以修改,在实际使用中要避免这种情况user_tuple = ('ha', 22, 175
2020-05-10 22:56:02
339
原创 一个协程小爬虫示例
mark一下 协程小爬虫示例,思路挺好的import asyncioimport aiohttpimport aiomysqlfrom pyquery import PyQuerystart_url = "http://www.jobbole.com/"waiting_urls = [] # 待爬取队列seen_urls = set() # 以爬取队列set类型stoping = Falsesem = asyncio.Semaphore(5)headers={'Connectio
2020-05-10 18:19:04
184
原创 asyncio模拟http请求
import asynciofrom urllib.parse import urlparseasync def get_url(url): # 通过socket请求html url = urlparse(url) host = url.netloc path = url.path if path == "": path = "/"...
2020-05-04 21:50:38
656
原创 在asyncio中非要使用阻塞方法时
使用多线程:在协程中集成阻塞IO在asyncio编程中不要使用阻塞IO,在某些情况下必须使用时可将协程和多线程结合,可使用下列方法import asyncioimport timefrom concurrent.futures import ThreadPoolExecutordef get_html(sleep_time): print(f'start {sleep_tim...
2020-05-03 22:27:25
344
1
原创 asyncio中的call_soon、call_later、call_at、call_soon_threadsafe方法
call_soon方法用于在协程中执行一些普通函数,在执行的时候需要使用run_forever方法,而不是使用run_until_complete方法因为该方法不是协程方法import asynciodef callback(sleep_times): print(f'sleep {sleep_times} success')def stop_loop(loop): lo...
2020-05-03 21:44:36
2675
原创 asyncio中的task
取消asyncio中的taskrun_until_complete()和run_forever()run_until_complete() ,tasks运行完成时会停止loop,run_forever()则不会在平时使用asyncio时遇到某些异常需要停止task,比如数据库插入表出错时出错需要停止后面taskimport asyncioimport timeasync def g...
2020-05-03 15:10:52
554
原创 python中的wait和gather的用处和区别
python中的asyncio(2)asyncio中的wait和gather的用处和区别wait等待协程执行完成后才会执行下一句import asyncioasync def get_html(url): print(f'start get {url}') await asyncio.sleep(2) print(f'end get {url}')if __n...
2020-05-03 10:09:17
1252
3
原创 记一次pyppeteer的使用
记一次pyppeteer的使用下载文本中的网页源码,由于需要向下拉动滚动条所以使用pyppeteer渲染网页,并执行js代码,可是发现开启无界面的时候似乎执行不了js代码,还有异步的时候好像也执行不了js代码import asynciofrom pyppeteer import launchimport re, os,timeasync def create_page(): ...
2020-04-30 08:48:08
883
原创 python 中的glob
python 中的glob用于解决遍历文件问题import globresult = glob.glob(r'D:\Bandizip\**\*.exe',recursive=True)for i in result: print(i)上面代码表示遍历D:\Bandizip\文件夹下所有后缀名为exe的文件,包括子文件夹recursive参数为True则表示遍历子文件夹,默认...
2020-04-26 23:32:33
221
原创 asyncio的基本使用
python的asyncio(1)asyncio是python用于解决异步io编程的一整套解决方案也是采用事件循环+回调(驱动生成器)+epoll(IO多路复用)简单的使用asyncio的例子import asyncioimport timeasync def get_html(url): print('start get url') await asyncio.sl...
2020-04-22 00:13:55
370
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人