
python 笔记
文章平均质量分 53
hunyxv
这个作者很懒,什么都没留下…
展开
-
aiomysql + sqlalchemy(ORM) 配合使用
官方文档:https://aiomysql.readthedocs.io/en/latest/sa.html (过时)其他教程:https://gzm1997.github.io/2018/05/26/%E4%BD%BF%E7%94%A8%E5%BC%82%E6%AD%A5ORM-aiomysql-sa/项目地址:https://github.com/aio-libs/aiomysqlimp...原创 2020-03-28 21:51:09 · 2838 阅读 · 1 评论 -
happyhbase: python 调用 hbase 接口包.md
happyhbase 文档地址注意:创建 连接时注意兼容,通过参数 compat 设置兼容级别(默认 0.98, thrift 的版本),具体看这里https://happybase.readthedocs.io/en/latest/api.html#connectionimport happybaseimport timehbase = happybase.Connection(ho...原创 2020-01-08 13:44:45 · 583 阅读 · 0 评论 -
上传几张之前看flask 源码时画的结构图
原创 2019-12-30 10:47:55 · 262 阅读 · 0 评论 -
python 函数参数的 ‘*’和‘/’ 的意义.md
/ 之前的参数都是 positional-only(位置) 参数,不能 写出参数的 name* 之后的参数都是 keyword-only(命名参数) 参数,必须 写出参数的 name比如,我们用参数签名的方法来举例:from inspect import Signature, Parameterparms = [Parameter('x', Parameter.POSITIONAL_O...原创 2019-12-16 19:14:06 · 379 阅读 · 0 评论 -
asyncio 是如何处理io事件的.md
asyncio 是如何利用事件循环来监控和处理io事件的,看源代码:# asyncio.streams.pyasync def open_connection(host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds): """A wrapper for crea...原创 2019-12-06 17:20:07 · 517 阅读 · 10 评论 -
asyncio
文章目录例子:开始看源代码例子解析例子:In [1]: import asyncioIn [2]: async def f(i): ...: await asyncio.sleep(i) ...: print(i) ...: In [3]: async def func(): ...: tasks = [] ...: ...原创 2019-12-05 20:12:26 · 1570 阅读 · 0 评论 -
awaitable 可等待对象.md
能在 await 表达式中使用的对象。可以是 coroutine 或是具有 __await__() 方法的对象。参见 PEP 492。注意可等待对象有两种:coroutine,在上节中有讲;具有 __await__() 方法的对象,只要一个类实现了__await__方法,那么通过它构造出来的实例就是一个Awaitable。下面这段代码就是 awaitable 抽象基类:class A...原创 2019-12-05 10:25:32 · 1489 阅读 · 0 评论 -
python coroutine协程
python3.7In [8]: async def funcc(): ...: print('2333') ...: return '2333'In [14]: x = funcc()In [15]: try: ...: x.send(None) ...: except StopIteration as exc: .....原创 2019-12-02 14:19:56 · 291 阅读 · 0 评论 -
flask send_file 下载文件,断点续传.md
函数包含在 flask.helpers文件中:def send_file( filename_or_fp, mimetype=None, as_attachment=False, attachment_filename=None, add_etags=True, cache_timeout=None, conditional=False,...原创 2019-11-12 16:02:53 · 3658 阅读 · 1 评论 -
Local、LocalStack、LocalManager和LocalProxy 实现协程/线程间数据隔离
文章目录LocalLocalStackLocalManagerLocalProxyLocalLocal 对 dict 做了一层封装:{id:{...}, id:{...}, ...},每次存储或取出数据时,根据当前id来进行操作。# get_ident 是优先获取协程id的(若当前环境安装了 greenlet)class Local(object): __slots__ = ("_...原创 2019-11-12 11:02:41 · 516 阅读 · 0 评论 -
zerorpc 详解.md
zerorpc 是利用 zeroMQ消息队列 + msgpack 消息序列化(二进制) 来实现类似 grpc 的功能,跨语言远程调用。主要使用到 zeroMQ 的通信模式是 ROUTER–DEALER,模拟 grpc 的 请求-响应式 和 应答流式 RPC :zerorpc 还支持 PUSH–PULL 通信模式的远程调用zerorpc 的 UML 图(大体):Server 端向 Cl...原创 2019-10-24 11:09:37 · 2245 阅读 · 0 评论 -
python的with用法
转载: kissdata With语句是什么? 有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取数据,然后关闭文件句柄。如果不用with语句,代码如下:file = open("/tmp/foo.txt")data = file.read()file.close(转载 2017-05-11 21:19:29 · 530 阅读 · 0 评论 -
BeautifulSoup find() 和 find_all()
BeautifulSoup 里的 find() 和 findAll() 可能是你最常用的两个函数。借助它们,你可以通过标签的不同属性轻松地过滤 HTML 页面,查找需要的标签组或单个标签。这两个函数非常相似,BeautifulSoup 文档里两者的定义就是这样:find_all(tag, attributes, recursive, text, limit, keywords)find(tag,原创 2017-04-14 08:57:47 · 3081 阅读 · 0 评论 -
python .strip()、.split() (切片)、.join()(合并)、 .replace方法
strip()方法 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 str.strip([chars]) 参数 chars – 移除字符串头尾指定的字符。str = "0000000this is string example....wow!!!0000000";print str.strip( '0' );输出:this is string example.原创 2016-09-17 22:26:05 · 6924 阅读 · 1 评论 -
scrapy ------ 爬取豆瓣电影TOP250
转载自 —> 原文#items.py# -*- coding: utf-8 -*-import scrapyclass DoubanMovieItem(scrapy.Item): ranking = scrapy.Field() #排名 movie_name = scrapy.Field() #电影名称 score = scrapy.Field()转载 2017-04-17 21:37:28 · 1867 阅读 · 0 评论 -
(转)SQLite3 多线程访问
import timeimport threadingimport sqlite3def nomal_producer(conn): ''' @summary: producer defination ''' counter = 0 conn.isolation_level = None conn.row_factory = sqlite3.Row转载 2017-03-15 08:31:48 · 1959 阅读 · 0 评论 -
Flask app.py
源码中有这么几个装饰器:before_request(self, f) # 注册一个在请求到达前要执行的函数before_first_request(self, f) # 注册在整个应用实例第一个请求到达前要执行的函数after_request(self, f) # 注册一个在请求处理后的函数teardown_request(self, f) ...原创 2018-11-15 17:27:33 · 1804 阅读 · 0 评论 -
python 多重装饰器执行顺序
#file_1.py#--coding:utf-8 --#代码来自http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.htmlclass mylocker: def __init__(self): print("mylocker.__init__() called.") @staticmethod原创 2017-03-03 09:37:04 · 2653 阅读 · 0 评论 -
scrapy 登录
登录网站http://c.biancheng.net登录成功会显示 xxx在线import scrapyclass BianchengItem(scrapy.Item): url = scrapy.Field() user = scrapy.Field() # -*- coding:utf-8 -*-from scrapy.spider import Cr原创 2017-04-23 10:40:10 · 1831 阅读 · 0 评论 -
拉手网Python程序员面试题
拉手网Python程序员面试题 拉手网Python程序员面试题,有用人用10行代码解决,有人用了一行代码解决是多么牛的赶脚。有种被秒杀的赶脚,题目在此https://www.jinshuju.net/f/EGQL3Ddic={}def num(aa,bb,cc): if aa%bb==0: return cc else: return aadef原创 2017-07-19 16:38:53 · 529 阅读 · 0 评论 -
Python lambda(匿名函数)函数总结
Python lambda(匿名函数)函数总结 除了def语句之外,Python还提供了一种生成函数对象的表达式形式。由于它与LISP语言中的一个工具很相似,所以就称为lambda。表达式创建一个之后能够调用的函数,但是它返回了一个函数而不是将这个函数赋值给一个变量名,这也是lambda有时候叫做匿名函数的原因。 lambda表达式 lambda的一般形式是关键字lambda,之后是一个或多原创 2016-05-13 22:06:32 · 1294 阅读 · 0 评论 -
scrapy实例 ----- 爬取小说
借鉴: 静觅scrapy教程 爬取目标:顶点小说网 http://www.23us.com/ 希望顶点小说网不要生气首先来编写items.py#-*- coding:utf-8 -*-# Define here the models for your scraped items## See documentation in:# http://doc.scrapy.org/en/late转载 2017-04-21 17:35:02 · 8497 阅读 · 0 评论 -
Python assert 断言函数
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单。在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件时就崩溃,这时候就需要assert断言的帮助。本文主要是讲assert断言的基础知识。python assert断言的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异原创 2016-10-05 08:56:02 · 63736 阅读 · 4 评论 -
使用由 Python 编写的 lxml 实现高性能 XML 解析
转载自:文章lxml 简介 Python 从来不出现 XML 库短缺的情况。从 2.0 版本开始,它就附带了 xml.dom.minidom 和相关的 pulldom 以及 Simple API for XML (SAX) 模块。从 2.4 开始,它附带了流行的 ElementTree API。此外,很多第三方库可以提供更高级别的或更具有 python 风格的接口。 尽管任转载 2016-10-04 21:34:33 · 4596 阅读 · 0 评论 -
flask 核心 之 应用上下文 及 请求上下文
Werkzeugs 是 Flask 的底层WSGI库。###什么是WSGI?一段简单的app:def dispath_request(self, request): return Response('Hello World!')def wsgi_app(self, environ, start_response): request = Request(environ...原创 2018-11-14 12:39:06 · 1216 阅读 · 0 评论 -
python的with用法(1)
转自:http://blog.kissdata.com/2014/05/23/python-with.htmlWith语句是什么? 有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取数据,然后关闭文件句柄。如果不用with语句,代码如下:file = open("/tmp转载 2016-09-17 23:04:12 · 333 阅读 · 0 评论 -
yield表达式, 四种形式
yield表达式, 四种形式: a. 不接受输入值或者输入值是None yield 1 b. 接受输入值 s = yield 1 c. 接受输入,但不返回数据,这样默认返回None s = yield d.既不接受输入,也不返回值,默认返回None yield 第一种:当函数调用到yield时,返回y转载 2017-03-10 15:10:11 · 3096 阅读 · 0 评论 -
Django笔记 ”coercing to Unicode: need string or buffer, int found“
def unicode(self): return self.id unicode() 方法可以进行任何处理来返回对一个对象的字符串表示。 Publisher和Book对象的unicode()方法简单地返回各自的名称和标题, Author对象的unicode()方法则稍微复杂一些,它将first_name和last_name字段值以空格连接后再返回。 对unicode转载 2016-11-03 23:59:50 · 9894 阅读 · 0 评论 -
python 爬虫基础笔记(一)
笔记记录来自慕课网(IMOOC):http://www.imooc.com/video/10683 例:import urllib2,cookielib#创建cookie容器cj = cookielib.CookieJar()#创建1个openeropener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))#转载 2016-09-29 10:38:18 · 457 阅读 · 0 评论 -
Django(Session,Cookie)
转载自:博主:BeginMan http://www.cnblogs.com/BeginMan/p/3890761.html一.Django authenticationdjango authentication提供了一个便利的user api接口,无论在py中 request.user,参见Request and response objects.还是模板中的{{user}}都能随时随地使用,如转载 2016-11-07 09:33:46 · 2410 阅读 · 0 评论 -
Django笔记 数据库数据外键 多对多关系访问
from django.db import models# Create your models here.class Publisher(models.Model): name = models.CharField(max_length=30,blank=True,null=True) address = models.CharField(max_length=50,blank=原创 2016-10-22 21:58:55 · 1104 阅读 · 0 评论 -
Django笔记 模板 标签
这个工程在之前的基础上做改变一、模板 一个网站有很多结构相同的网页 ,使用模板能有效的避免繁琐重复的工作: 1)、在templates文件夹(与xxx.html同层)下新建base文件夹来存放模板#base.html<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>{{ Title }原创 2016-10-22 00:41:51 · 392 阅读 · 0 评论 -
Django 数据库表单查询
from django.shortcuts import renderfrom django.http import HttpResponsefrom django.template import loader,Contextfrom blog.models import Person# Create your views here.def student_list(request):原创 2016-10-15 09:36:20 · 2876 阅读 · 0 评论 -
python With关键字用法(2)
OpenStack的源码就是一部学习Python语言的指导书。各种Python的技巧都在其中,把之前学习with关键字的记录帖上来。 从python2.6开始,with就成为默认关键字了。With是一个控制流语句,跟if for while try之类的是一类,with可以用来简化try finally代码,看起来比try finally更清晰。With关键字的用法如下:>>with ex转载 2016-09-25 10:29:37 · 364 阅读 · 0 评论 -
Python中使用pickle持久化对象
Python中可以使用 pickle 模块将对象转化为文件保存在磁盘上,在需要的时候再读取并还原。具体用法如下:pickle.dump(obj, file[, protocol])这是将对象持久化的方法,参数的含义分别为: obj: 要持久化保存的对象; file: 一个拥有 write() 方法的对象,并且这个 write() 方法能接收一个字符串作为参数。这个对象可以是一个以写模式打开的转载 2016-09-19 13:02:08 · 395 阅读 · 0 评论 -
python sorted() 排序
转:http://www.cnblogs.com/65702708/archive/2010/09/14/1826362.html 我们需要对List进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序 方法2.用built-in函数sorted进行排序(从2.4开始)sorted**:** help(sorted)转载 2016-09-18 11:12:46 · 446 阅读 · 0 评论 -
Python List index()方法
描述 index() 函数用于从列表中找出某个值第一个匹配项的索引位置。 语法 index()方法语法: list.index(obj) 参数 obj – 查找的对象。 返回值 该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。 实例 以下实例展示了 index()函数的使用方法:aList = [123, 'xyz', 'zara', 'abc'];print "Ind转载 2016-09-18 10:36:24 · 2248 阅读 · 0 评论 -
python中的三个读read(),readline()和readlines()
我们谈到“文本处理”时,我们通常是指处理的内容。Python 将文本文件的内容读入可以操作的字符串变量非常容易。文件对象提供了三个“读”方法: .read()、.readline() 和 .readlines()。read 读取整个文件 readline 读取下一行 readlines 读取整个文件到一个迭代器以供我们遍历def countlines(name):原创 2016-09-16 15:07:25 · 530 阅读 · 0 评论 -
CentOS 7 升级Python到3.5后,yum,和gnome-twear-tool 出现的问题
关于CentOS 7升级Python到3.5后,yum出现的问题。 By lincanbin at 23 天前 • 0人收藏 • 115人看过 lincanbin CentOS 7升级Python到3.5后,我跟以前CentOS 6一样,在/usr/bin/python创建了一个指向Python 3的软连接,然后将/usr/bin/yum的顶部的:1!/usr/bin/python改成了1!原创 2016-06-06 19:56:06 · 10341 阅读 · 3 评论 -
Django 笔记 user 注册 登录 及 权限
#urls.pyfrom django.conf.urls import urlfrom django.contrib import adminurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/$','blog.views.login'), #登录 url(r'^register/$','blog原创 2016-10-20 18:17:23 · 2458 阅读 · 0 评论