KevinHuang

KevinHuang的博客


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 一些书

  • 常用命令

  • python tips

  • 站点地图

  • rss

  • 公益404

  • 搜索

python for else

发表于 2017-09-12 | 分类于 python | 阅读次数:
1234567891011121314>>> for i in range(0,10): if i > 10: break; else: print "hello world";输出:hello world>>> for i in range(0,10): if i > 5: break; else: print "hello world"; 没有输出------------------- 即在for 循环中,如果没有从任何一个break中退出,则会执行和for对应的else只要从break中退出了,则else部分不执行。
阅读全文 »

CentOS7设置ss开机启动

发表于 2017-08-03 | 分类于 linux | 阅读次数:
CentOS7设置ss开机启动### 配置自启动新建启动脚本文件/etc/systemd/system/shadowsocks.service,内容如下: 123456789[Unit]Description=Shadowsocks[Service]TimeoutStartSec=0ExecStart=/usr/bin/ssserver -c /etc/shadowsocks.json[Install]WantedBy=multi-user.target 执行以下命令启动 shadowsocks 服务: 12$ systemctl enable shadowsocks$ systemctl start shadowsocks 为了检查 shadowsocks 服务是否已成功启动,可以执行以下命令查看服务的状态: 123456789101112[root@kevin ~]# systemctl status ssserver -l* ssserver.service - Ssserver Loaded: loaded (/etc/systemd/system/ssserver.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2017-08-03 05:56:09 UTC; 23s ago Main PID: 691 (ssserver) CGroup: /system.slice/ssserver.service `-691 /usr/bin/python2 /usr/bin/ssserver -c /etc/shadowsocks.json --log-file /var/log/shadowsocks.log startAug 0 ...
阅读全文 »

google开源python代码规范

发表于 2017-07-31 | 分类于 openstack | 阅读次数:
1. 行长度 每行不超过80个字符(例外 a.长的导入模块语句; b. 注释里的url) Python会将 圆括号, 中括号和花括号中的行隐式的连接起来 :123In [2]: print('hello' ...: 'world')helloworld 2. 文件和sockets 在文件和sockets结束时, 显式的关闭它. 推荐使用 “with”语句 以管理文件; 对于不支持使用”with”语句的类似文件的对象,使用 contextlib.closing():1234import contextlibwith contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page: for line in front_page: print line 3. todo注释 为临时代码使用TODO注释, 它是一种短期解决方案. 不算完美, 但够好了. 4. 导入格式 每个导入应该独占一行 导入顺序: 标准库导入 第三方库导入 应用程序指定导入 5. 命名约定 所谓”内部(Internal)”表示仅模块内可用, 或者, 在类内是保护或私有的. 用单下划线(_)开头表示模块变量或函数是protected的(使用import * from时不会包含). 用双下划线(__)开头的实例变量或方法表示类内私有. 将相关的类和顶级函数放在同一个模块里. 不像Java, 没必要限制一个类一个模块. 对类名使用大写字母开头的单词(如CapWords, 即Pascal风格), 但是模块名应该用小写加下划线的方式(如lower_with_under.py). 尽管已经有很多现存的模块使用类似于CapWords.py这样的命名, 但现在已经不鼓励这样做, ...
阅读全文 »

生成PDF文档

发表于 2017-07-05 | 分类于 文档 | 阅读次数:
主要使用pdfkit模块: 安装pdfkit: 123$ pip install pdfkit$ sudo apt-get install wkhtmltopdf 使用pdfkit的一些高级功能需要研究wkhtmltopdf的一些功能:Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script. 使用高级功能需要执行以下脚本: 123456#!/bin/shsudo apt-get install -y openssl build-essential xorg libssl-devwget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2tar xvjf wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2sudo chown root:root wkhtmltopdf-amd64 基本原理功能:Pdfkit文档链接:https://pypi.python.org/pypi/pdfkitPdfkit可以渲染url,file,string 成pdf文档,也支持多个file生产一个pdf文档 Notices: pdf正文部分字 ...
阅读全文 »

web站点相关解决方案

发表于 2017-06-22 | 分类于 python | 阅读次数:
邮箱或手机验证码存储解决方案: 使用web框架自带的缓存系统,比如django中自带的cache 1234from django.core import cachecache.set('key', 'value', 10) #键,值和过期时间cache.get('key', 'not fount or has expired') 使用redis存储: 123redis_db = RedisDB()redis_db.set(phone=username, val=code, ex=60*10)
阅读全文 »

Python LEGN作用域总结

发表于 2017-06-19 | 分类于 python | 阅读次数:
LEGB规则 Python2.2开始引入嵌套函数,嵌套函数为python提供了闭包实现。 1234567891011a = 1def foo(): a = 2 def bar(): print a //[1] return bar func = foo()func() 函数bar和a=2捆包在一起组成一个闭包,因此这里a=2即使脱离了foo所在的local作用域,但调用func的时候(其实就是调用bar)查找名字a的顺序是LEGB规则,这里的E就是enclosing的缩写,代表的“直接外围作用域”这个概念。查找a时,在bar对应的local作用域中没有时,然后在它外围的作用域中查找a。LEGB规定了查找一个名称的顺序为:local–>enclosing–>global–>builtin。
阅读全文 »

matplotlib RuntimeError: Python is not installed as a framework 错误解决方案

发表于 2017-05-25 | 分类于 python | 阅读次数:
在virtualenv环境下使用matplotlib绘图时遇到了这样的问题: import matplotlib.pyplot as pltTraceback (most recent call last):File “”, line 1, in… infrom matplotlib.backends import _macosxRuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see ‘Working with Matplotlib in Virtual environments’ in the Matplotlib FAQ 似乎是因为虚拟环境与默认环境的安装配置不同造成的。 搜索错误信息之后,在STO上找到了解决方案: 1、pip安装matplotlib之后,会在根目录下产生一个.matplotlib的目录: ➜ bin ll ~/.matplotlibtotal 280-rw-r–r– 1 me staff 78K 10 4 2 ...
阅读全文 »

casperjs传参

发表于 2017-04-18 | 分类于 爬虫 | 阅读次数:
casperjs传递动态参数: 12345678910111213141516171819var casper = require('casper').create({ pageSettings: { javascriptEnabled: true , loadImages: true, loadPlugins: true, userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36' }, // logLevel: "debug",//日志等级 // verbose: true, // 记录日志到控制台 viewportSize: {width: 1024, height: 768}});var args2 = casper.cli.args;var NET_SessionId = args2[0];var EhireGuid = args2[1];var AccessKey = args2[2];var HRUSERINFO = args2[3];
阅读全文 »

casperjs截取验证码图片和设置cookies,headers,模拟鼠标点击selected元素

发表于 2017-04-14 | 分类于 爬虫 | 阅读次数:
casperjs设置headers: 123456789101112131415161718192021222324252627282930313233var casper = require('casper').create({ pageSettings: { loadImages: true, loadPlugins: true, // userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER' userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', } logLevel: "debug",//日志等级 verbose: true // 记录日志到控制台});var mouse = require("mouse").create(casper);var url = 'http://baidu.com/';casper.start(url);// casper.thenClick('#verify-state'); //鼠标点击casper.thenClick('#btnBeginValidate');casper.thenClick('#btnVRefresh',function(response){ this.echo((response.head ...
阅读全文 »

Python中的一些魔法

发表于 2017-01-23 | 分类于 python | 阅读次数:
将多个序列串放在一起遍历: 123456>>> from itertools import chain >>> a = [1, 2, 3, 4] >>> b = ['a', 'b', 'c'] >>> for x in chain(a, b): ... print(x) ... python表示昨天的日期: 12345678#-*-coding:utf-8-*- import datetimedef getYesterday(): # today=datetime.date.today() oneday=datetime.timedelta(days=1) yesterday=today-oneday return yesterday 打印代码出错信息: 12import tracebacktraceback.format_exc() traceback.print_exc()跟traceback.format_exc()有什么区别呢?format_exc()返回字符串,print_exc()则直接给打印出来。
阅读全文 »
1234…20
Kevin Huang

Kevin Huang

197 日志
24 分类
84 标签
RSS
E-Mail
友情链接
  • 董伟明
  • FOOFISH
  • 酷壳
  • 知道创宇
  • 阮一峰
  • 求索
  • Sdandroid
  • 全栈实验室
© 2023 Kevin Huang 豫ICP备16018730号-1
本站访客数 人次 本站总访问量 次