
Python
__HelloWorld__
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Java之keytool命令学习
Java Keytool is a key and certificate management utility. It allows users to manage their own public/private key pairs and certificates. It also allows users to cache certificates. Java Keytool store...原创 2019-06-20 21:46:54 · 854 阅读 · 0 评论 -
Flask Web开发入门(一)之简单的登录验证
Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. 我们的目标是通过Flask实现一个简单的web系统,系统需要用户登录,未登录用户无法访问授权页面,因此,我们定义页面如下:index.html默认首页,需要登录授权访问login.html登录页,输入用户名和密码e原创 2018-01-08 19:47:42 · 22354 阅读 · 2 评论 -
Flask Web开发入门(二)之Flask-Login使用
上一章我们介绍了使用Flask开发基本的Web应用,并使用session来进行基本的登录授权验证,接下来我们将使用Flask-Login来进行会话管理,来处理我们的“登入、登出”问题 Flask-Login provides user session management for Flask. It handles the common tasks of logging in, loggi原创 2018-01-08 20:18:59 · 9861 阅读 · 0 评论 -
静态方法(staticmethod),类方法(classmethod)与实例方法
@classmethod 将方法转为类方法,类方法接收类cls作为隐式的第一个参数class C: @classmethod def f(cls, arg1, arg2, ...): ...@staticmethod 将方法转换为静态方法,静态方法接收隐式的第一个参数。实例方法: 实例方法接收实例self作为隐式的第一个参数 def原创 2018-01-18 11:08:59 · 1140 阅读 · 0 评论 -
【format】 vs 【%】 in Python
相比【%】,Python中的【format】更显精致,对于【%】来说,它无法同时传递一个变量和元组(Tuple)比如,对于: “hi there %s” % name如果name传递Tuple元组类型参数,那么将抛出如下错误: TypeError: not all arguments converted during string formatting为保证其正常执原创 2018-01-16 19:59:34 · 428 阅读 · 0 评论 -
Python中的property类与@property装饰器
class property(fget=None, fset=None, fdel=None, doc=None)返回一个property属性fget:获取属性值fset:设置属性值fdel:删除属性doc:属性描述 注意,如果设置了doc参数,则参数值就是属性的docstring,否则系统将会读取fget参数的docstring(如果存在)作为属性的docstring,原创 2018-01-16 16:39:53 · 3272 阅读 · 0 评论 -
Flask Web开发入门(十四)之使用蓝图(Blueprints)
Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications. Blueprints can greatly simplify how large applications原创 2018-01-15 10:04:15 · 1552 阅读 · 0 评论 -
Flask Web开发入门(十三)之构建REST API
关于REST API,我们先看一个比较形象的图: 在我们之前的章节中,我们其实已经涉及到了Flask框架下REST API接口的构建,比如,文件下载接口:# 文件下载@app.route('/download/')def send_html(filename): logger.debug("download file, path is %s" % filename)原创 2018-01-12 13:56:03 · 3816 阅读 · 0 评论 -
Flask Web开发入门(三)之使用 Request Loader 定制登录
在上一章中,我们介绍了使用Flask-Login进行会话管理,实现基本的登入、登出功能,在实际的业务运用中,不仅要求基本的登录登出授权管理,还要求对外暴漏API接口服务,当然暴漏的服务接口不可能随意访问,还需要一定的授权信息,这时,我们需要Request Loader来进行定制登录,简单期间,我们使用HTTP Header中的Authorization作为查询参数的API密钥,只有请求Header原创 2018-01-08 20:37:17 · 2981 阅读 · 1 评论 -
Flask Web开发入门(四)之错误处理
在我们上一章中,我们介绍了使用 Request Loader 定制登录,在我们的逻辑分支中,错误处理我们使用了abort函数:当我们使用abort函数时,它表示放弃请求并返回错误代码,默认情况业务不友好,那么我们可以使用errorhandler() 装饰器来进行定制错误页面,errorhandler装饰器使用如下:@app.route('/error')@app.errorhan原创 2018-01-08 20:47:19 · 1956 阅读 · 0 评论 -
将数字金额转换为对应的中文大写金额
一个小问题,将指定的数字金额转换输出为对应的中文大写金额,比如:对于数字金额123,输出:壹佰贰拾叁圆 讨论之前,我们约定,数字最大不超过9999999999999.99,且最多只有两位小数。问题不难,主要需考虑以下几点:阿拉伯数字转换为对应的中文大写,比如,对于阿拉伯数字1,对应输出壹每个位置的数字后紧跟正确的单位,比如对于从右向左的第三位(不包含小数点),如果当...原创 2018-02-24 21:57:45 · 4360 阅读 · 2 评论 -
那些你不知道的爬虫反爬虫套路
转自:https://zhuanlan.zhihu.com/p/27299841?columnSlug=ctriptech前言爬虫与反爬虫,是一个很不阳光的行业。这里说的不阳光,有两个含义。第一是,这个行业是隐藏在地下的,一般很少被曝光出来。很多公司对外都不会宣称自己有爬虫团队,甚至隐瞒自己有反爬虫团队的事实。这可能是出于公司战略角度来看的,与技术无关。第二是,这个行业...转载 2018-02-23 20:45:42 · 723 阅读 · 0 评论 -
Python中的IP地址正则判断
def is_valid_ip(ip): r = '^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]).(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$' return re.match(r, ip)lst = {...原创 2018-02-23 12:03:28 · 2000 阅读 · 1 评论 -
Python中函数的参数
简单记录一下位置参数def add(a, b): return a + b这里a,b就是位置参数,调用时传入的两个值按顺序赋给参数a,b可变参数参数前面添加*号,函数内部接收的是一个tuple,可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple;def cal(*args): for n in args: ...原创 2018-02-22 10:25:53 · 709 阅读 · 0 评论 -
Python接收application/json与application/x-www-form-urlencoded数据
http://blog.youkuaiyun.com/kangkanglou/article/details/79027556原创 2018-01-10 20:23:17 · 5327 阅读 · 0 评论 -
Flask Web开发入门(八)之文件上传
本章我们介绍Flask Web开发中涉及的文件上传模块定义后台接收处理逻辑# http://flask.pocoo.org/docs/0.12/patterns/fileuploads/@app.route('/upload', methods=['POST'])def upload_file(): if request.method == 'POST':原创 2018-01-10 19:30:23 · 15103 阅读 · 12 评论 -
Default Parameter Values in Python
Default Parameter Values in PythonFredrik Lundh | July 17, 2008 | based on a comp.lang.python postPython’s handling of default parameter values is one of a few things that tends to trip up most n转载 2018-01-18 21:20:39 · 598 阅读 · 0 评论 -
Flask Web开发入门(六)之访问数据库
我们介绍两种访问MySQL数据库的方式,一种是使用mysql.connector直连,另一种是使用sqlalchemy ORM框架。mysql.connector直连方式# get database connectiondef get_connection(): try: conn = mysql.connector.connect(**config)原创 2018-01-08 21:20:19 · 1651 阅读 · 2 评论 -
Flask Web开发入门(五)之日志
作为码农,在开发功能中记录日志是必要且必须的,因为事情往往不像你想象的那样理想完美,就像你开发的代码那样,良好的日志习惯能助你快速排查定位问题,从 Flask 0.3 开始,Flask 就已经预置了日志系统。以下是Flask日志的运用示例:import osimport loggingimport sysLOG_PATH = 'logs'LOG_FILE = 'text.t原创 2018-01-08 21:05:02 · 3168 阅读 · 0 评论 -
Flask Web开发入门(十二)之图片下载与展现(使用send_from_directory)
在Flask Web开发入门(十一)之图片展现,我们介绍了Flask框架下我们通过file.open与make_response结合运用实现了图片的下载与展现功能,这略显复杂,其实Flask本身已经帮我们提供了相应的功能:即send_from_directory函数。首先,我们来看下send_from_directory函数定义: Send a file from a given di原创 2018-01-12 11:31:14 · 10889 阅读 · 5 评论 -
Flask Web开发入门(十一)之图片展现
在Flask Web开发入门(十)之图片上传(使用Flask-Uploads)我们介绍了使用Flask插件Flask-Uploads上传图片,本章就此话题继续展开。我们知道,我们可以通过UPLOADS_DEFAULT_DEST参数来指定文件上传的默认路径,假如我们指定的路径是upload目录,那么文件实际保存的路径为: upload/files/xxx注意,files为Uploa原创 2018-01-11 20:05:55 · 6077 阅读 · 0 评论 -
Python 使用mysql-connector-python访问MySql
conn = mysql.connector.connect(host='10.214.168.25', port='3306', user='user', password='password', database='test') cursor = conn.cursor() cursor.execute('se原创 2017-12-25 19:42:07 · 2353 阅读 · 1 评论 -
Python中的正则表达式
import repattern = "^\d{3}\-\d{3,8}$"phone0 = "010-66101234"phone1 = "010-123"phone2 = "010-12345"print("result is %s" % re.match(pattern, phone0))print("result is %s" % re.match(pattern, phone1))原创 2017-12-25 19:37:48 · 295 阅读 · 0 评论 -
Python 文件读写
1. 可以一次性加载到内存中def read_file(file_name): with open(file_name, "r", errors='ignore', encoding='utf-8') as f: str = f.read() print(str)2.按行读取def read_file_by_line(file_name): with原创 2017-12-25 19:34:21 · 352 阅读 · 0 评论 -
Python中的字符串反转
性能最佳者 推荐方法,使用切片:slicedef reversed_string(a_string): return a_string[::-1]可读性强def reverse_string_readable_answer(string): return ''.join(reversed(string))中规中矩 这种做法其实非常不推荐的,因为,记住,Python中字符串是原创 2017-12-06 20:38:02 · 4974 阅读 · 2 评论 -
最大子序列和
给定一个整数序列,a0, a1, a2,…, an(序列项可以为负数),求其中最大的子序列和,如果所有整数都是负数,那么最大子序列和为0;首先定义两个变量:max_so_far和max_ending_here然后从左到右开始遍历输入序列,其中:max_ending_here用以表示序列中截止到当前位置的最大子序列和,max_so_far则是通过冒泡排序的方式获取max_ending_here的最大原创 2017-12-14 20:05:39 · 430 阅读 · 0 评论 -
Python可视化展现—看博客大佬们的写作规律
在《使用BeautifulSoup爬取优快云博客文章》这篇文章中,我们使用了BeautifulSoup爬取了优快云博客的访问情况以及文章列表信息,紧接着,我们可以做一些更好玩的事情,比如:分析下这些排名靠前的一些博客写作规律等信息,我们首先按时间维度分析下这些“博客大佬们“的产量信息: 我们定义一个dict用以记录博客发表年份与发表文章数量关系,修改walk_tree如下: article原创 2017-12-13 20:08:02 · 508 阅读 · 0 评论 -
Python词云图绘制—看博客大佬们的写作热点
在《Python可视化展现》中,我们使用了Matplotlib可视化了一些博客大佬们的博客发表年份与数量的关系,接下来我们再看下这些博客文章的热点都有哪些。 我们仅对文章的标题进行分词处理,然后统计分词结果,并绘制出博客文章词云,我们使用了jieba和thulac进行中文分词,结果总体差不多,但thulac速度更显得慢。重新定义walk_treedef walk_tree_j(html, num)原创 2017-12-13 20:26:54 · 960 阅读 · 0 评论 -
Python3中dict按value排序
sorted_dict = sorted(keyword_dict.items(), key=lambda item: item[1], reverse=True)print(sorted_dict)sorted_dict = sorted(keyword_dict.items(), key=operator.itemgetter(1), reverse=True)print(sorted_d原创 2017-12-13 20:29:07 · 8610 阅读 · 0 评论 -
使用BeautifulSoup爬取优快云博客文章
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库 Requests is an elegant and simple HTTP library for Python, built for human beings. 通过Requests发起请求获取博客信息,然后再通过BeautifulSoup的基本应用,当然这里我们选取的是排名靠前的博客,注意如果是在内网原创 2017-12-12 20:41:57 · 860 阅读 · 0 评论 -
Python get path separator
print("pathsep is %s" % os.pathsep)print("path.sep is %s" % os.path.sep) pathsep is ; path.sep is \原创 2017-12-25 19:45:16 · 1836 阅读 · 0 评论 -
Python rfind and rindex
str.rfind(sub[, start[, end]]) Return the highest index in the string where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as原创 2017-12-28 20:06:29 · 813 阅读 · 0 评论 -
Flask Web开发入门(七)之SQLAlchemy
上一章中,我们简单介绍了使用 SQLAlchemy ORM框架来访问MySQL数据库,本章我们将继续就此话题进行展开。 SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQ原创 2018-01-09 20:03:37 · 1182 阅读 · 0 评论 -
Python中类的JSON序列化
这肯定是个老生常谈的问题,在此记录下。在Python中,当在我们使用sqlalchemy.orm来对数据库对象进行存取,并将这些数据展现到前台页面时,我们就面临如何将Python中的类对象进行JSON序列化。简单起见,我们从代码说起:首先,我们定义一个用户类,用以存储用户信息class User(object): def __init__(self, id, name, p原创 2018-01-11 20:30:12 · 1735 阅读 · 1 评论 -
使用Jackson进行序列化和反序列化
序列化: public static <T> String serialize(T t) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String jsonResult = mapper.writerWithDefaultPrettyPrinter()原创 2017-12-19 10:26:38 · 1646 阅读 · 0 评论 -
Python之str内置函数
str.capitalize() 字串首字母大写 a_str = "hello World"print("%s capitalized is %s" % (a_str, a_str.capitalize())) str.casefold() 与lower()类似,将字串转换为小写,可用于大小写不敏感字串匹配 与lower()不同的是:lower方法只原创 2018-01-02 20:30:27 · 4714 阅读 · 0 评论 -
删除pip安装缓存
指定–no-cache-dir确保安装不缓存如果pip版本在6.0以上,可以在安装时使用–no-cache-dir参数,查看pip版本 pip -Vpip命令格式:如果pip版本在6.0以下,可以使用如下命令升级pip版本 pip install -U pip删除已缓存文件,根据各自不同的操作系统,删除对应目录缓存文件Linux and Unix ~/.cache/pip # an原创 2018-01-02 20:11:24 · 105181 阅读 · 2 评论 -
Flask Web开发入门(十)之图片上传(使用Flask-Upload)
在Flask Web开发入门(八)之文件上传中,我们探讨了Flask框架下的文件上传,本章我们将使用Flask插件Flask-Uploads示例说明的图片上传与展现如何实现开始之前,我们先简单看下Flask-Uploads源码实现:类UploadSet:文件上传配置集合,包含三个参数:name:文件上传配置集合的名称,默认files,一般不用修改,只是一个标识,要求数字、字母或两原创 2018-01-11 19:42:46 · 18423 阅读 · 8 评论 -
pip命令使用代理
pip命令安装Python模块时,如果在内网需要使用代理上网,那么pip命令配置代理命令格式如下: pip install –proxy http://username:password@proxy_ip:proxy_port module_name原创 2017-12-18 20:30:39 · 10858 阅读 · 0 评论 -
Python convert a list to String
正常情况如果list由字串类型,一般可以用下面这种方法a_list = ["1", "2", "3"]print("".join(a_list))发散一下,如果list是数字型,那么上面这种方式就会报错: Traceback (most recent call last): File “D:/work/PyCharm/cn/com/icbc/basic/list_to_str.py”原创 2017-12-18 20:18:00 · 12287 阅读 · 0 评论