用pylint, 写好代码

时间:2023年01月13日

/

来源:眼睛瞪得像铜铃

/

编辑:本站小编

收藏本文

下载本文

以下是小编收集整理的用pylint, 写好代码,本文共5篇,仅供参考,欢迎大家阅读。本文原稿由网友“眼睛瞪得像铜铃”提供。

篇1:用pylint, 写好代码

简介

Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8)和有潜在问题的代码,

用pylint, 写好代码

Pylint 是一个 Python 工具,除了平常代码分析工具的作用之外,它提供了更多的功能:如检查一行代码的长度,变量名是否符合命名标准,一个声明过的接口是否被真正实现等等。

Pylint 的一个很大的好处是它的高可配置性,高可定制性,并且可以很容易写小插件来添加功能。

如果运行两次 Pylint,它会同时显示出当前和上次的运行结果,从而可以看出代码质量是否得到了改进。

目前在 eclipse 的 pydev 插件中也集成了 Pylint。

目前最好的介绍 pylint 的文章应该是这篇《如何使用 Pylint 来规范 Python 代码风格 》(上面那段文字的来源),但里面讲到的安装方式有点过时了,而且在 windows 下安装的时候,有点小状况:使用 Easy_install 安装完毕之后报告成功安装,但执行时出错:

Usingd:/python26/lib/site-packages/unittest2-0.5.1-py2.6.egg

Finishedprocessingdependenciesforpylint==0.22.0

D:/pylint-0.22.0>pylint

python:can'topenfile'D:/Python26/Scripts/pylint':[Errno2]Nosuchfileor

directory

后来我解决了这个问题,在这里备份一下:

Windows 下的安装

确保 Python 的安装目录和相应的 Scripts 目录已经在环境变量 path 中

先到 pypi.python.org/pypi/pylint下载安装包,然后解压到某目录,这里假定在 D:/pylint-0.22.0

进入 D:/pylint-0.22.0 目录,然后在命令行执行以下指令:python setup.py install

在上一步会出错,但会报告成已经成功安装,这时候需要打开 D:/pylint-0.22.0/bin 目录,然后把那里的所有文件拷贝到 Python 的 Scripts 目录下(如:D:/Python26/Scripts)

在命令行尝试执行 pylint,如果输出帮助,则表示已经安装成功

与 PyDev 集成

PyDev 的安装略过...以下是集成配置的过程:

Window ->preferences ->Pydev ->Pylint,选中 “Use pylint?”

在 Location of pylint 处输入你安装的 lint.py 的地址,如:D:/pylint-0.22.0/lint.py

在下方的 Arguments to pass to pylint 处输入 --rcfile=“E:/svn/misc/pylint.conf”,以使用自己的 pylintrc 配置

Project ->Properties ->PyDev?-PYTHONPATH 增添项目的源文件目录到“Project Source Folders”,

篇2:代码用英语怎么写

There was coded criticism of the government from some party members.

一些党员对政府进行了间接的`批评。

There are two codes for London.

伦敦有两个电话区号。

The sport has a strict code of conduct.

体育运动有严格的行为规范。

It's written in code.

那是用密码写的。

The company has a strict dress code ─ all male employees are expected to wear suits.

公司有严格的着装规定——所有男职员都要穿西服。

篇3:qq秀代码的使用方法、怎么用

QQ秀代码使用方法:

1:点击自己的QQ秀进入QQ秀商城(show.qq.com);

2:将复制来的QQ秀代码整个粘贴到页面上方浏览器地址栏中;

3:按回车键Enter;(用同一代码重复操作一次既可去掉效果)

4:最后点左边自己QQ秀下方的“保存形象”即可,

qq秀代码的使用方法、怎么用

提示:网上提供的代码有些都不能用(使用后没有反应),或者不是免费的(需要支付)。所以要耐心挑选了。

篇4:用KnockoutJS实现ToDoMVC代码分析

HTML View

以下是html view中主要部分

todos

0

left

All

Active

Completed

Clear completed ()

核心JS

/*global ko, Router */

(function () {

'use strict';

var ENTER_KEY = 13;

var ESCAPE_KEY = 27;

// A factory function we can use to create binding handlers for specific

// keycodes.

function keyhandlerBindingFactory(keyCode) {

return {

init: function (element, valueAccessor, allBindingsAccessor, data, bindingContext) {

var wrappedHandler, newValueAccessor;

// wrap the handler with a check for the enter key

wrappedHandler = function (data, event) {

if (event.keyCode === keyCode) {

valueAccessor().call(this, data, event);

}

};

// create a valueAccessor with the options that we would want to pass to the event binding

newValueAccessor = function () {

return {

keyup: wrappedHandler

};

};

// call the real event binding's init function

ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data, bindingContext);

}

};

}

// a custom binding to handle the enter key

ko.bindingHandlers.enterKey = keyhandlerBindingFactory(ENTER_KEY);

// another custom binding, this time to handle the escape key

ko.bindingHandlers.escapeKey = keyhandlerBindingFactory(ESCAPE_KEY);

// wrapper to hasFocus that also selects text and applies focus async

ko.bindingHandlers.selectAndFocus = {

init: function (element, valueAccessor, allBindingsAccessor, bindingContext) {

ko.bindingHandlers.hasFocus.init(element, valueAccessor, allBindingsAccessor, bindingContext);

ko.utils.registerEventHandler(element, 'focus', function () {

element.focus();

});

},

update: function (element, valueAccessor) {

ko.utils.unwrapObservable(valueAccessor()); // for dependency

// ensure that element is visible before trying to focus

setTimeout(function () {

ko.bindingHandlers.hasFocus.update(element, valueAccessor);

}, 0);

}

};

// represent a single todo item

var Todo = function (title, completed) {

this.title = ko.observable(title);

this.completed = ko.observable(completed);

this.editing = ko.observable(false);

};

// our main view model

var ViewModel = function (todos) {

// map array of passed in todos to an observableArray of Todo objects

this.todos = ko.observableArray(todos.map(function (todo) {

return new Todo(todo.title, todo.completed);

}));

// store the new todo value being entered

this.current = ko.observable();

this.showMode = ko.observable('all');

this.filteredTodos = ko.computed(function () {

switch (this.showMode()) {

case 'active':

return this.todos().filter(function (todo) {

return !todo.completed();

});

case 'completed':

return this.todos().filter(function (todo) {

return todo.completed();

});

default:

return this.todos();

}

}.bind(this));

// add a new todo, when enter key is pressed

this.add = function () {

var current = this.current().trim();

if (current) {

this.todos.push(new Todo(current));

this.current('');

}

}.bind(this);

// remove a single todo

this.remove = function (todo) {

this.todos.remove(todo);

}.bind(this);

// remove all completed todos

this.removeCompleted = function () {

this.todos.remove(function (todo) {

return todo.completed();

});

}.bind(this);

// edit an item

this.editItem = function (item) {

item.editing(true);

item.previousTitle = item.title();

}.bind(this);

// stop editing an item. Remove the item, if it is now empty

this.saveEditing = function (item) {

item.editing(false);

var title = item.title();

var trimmedTitle = title.trim();

// Observable value changes are not triggered if they're consisting of whitespaces only

// Therefore we've to compare untrimmed version with a trimmed one to chech whether anything changed

// And if yes, we've to set the new value manually

if (title !== trimmedTitle) {

item.title(trimmedTitle);

}

if (!trimmedTitle) {

this.remove(item);

}

}.bind(this);

// cancel editing an item and revert to the previous content

this.cancelEditing = function (item) {

item.editing(false);

item.title(item.previousTitle);

}.bind(this);

// count of all completed todos

this.completedCount = ko.computed(function () {

return this.todos().filter(function (todo) {

return todo.completed();

}).length;

}.bind(this));

// count of todos that are not complete

this.remainingCount = ko.computed(function () {

return this.todos().length - this.completedCount();

}.bind(this));

// writeable computed observable to handle marking all complete/incomplete

this.allCompleted = ko.computed({

//always return true/false based on the done flag of all todos

read: function () {

return !this.remainingCount();

}.bind(this),

// set all todos to the written value (true/false)

write: function (newValue) {

this.todos().forEach(function (todo) {

// set even if value is the same, as subscribers are not notified in that case

todo.completed(newValue);

});

}.bind(this)

});

// helper function to keep expressions out of markup

this.getLabel = function (count) {

return ko.utils.unwrapObservable(count) === 1 ? 'item' : 'items';

}.bind(this);

// internal computed observable that fires whenever anything changes in our todos

ko.computed(function () {

// store a clean copy to local storage, which also creates a dependency on the observableArray and all observables in each item

localStorage.setItem('todos-knockoutjs', ko.toJSON(this.todos));

alert(1);

}.bind(this)).extend({

rateLimit: { timeout: 500, method: 'notifyWhenChangesStop' }

}); // save at most twice per second

};

// check local storage for todos

var todos = ko.utils.parseJson(localStorage.getItem('todos-knockoutjs'));

// bind a new instance of our view model to the page

var viewModel = new ViewModel(todos || []);

ko.applyBindings(viewModel);

// set up filter routing

/*jshint newcap:false */

Router({ '/:filter': viewModel.showMode }).init();

}());

JS代码解析

在上述版本todo app中,Todo可视为app的Model,包含3 个属性分别是title,completed,editing,并且这三个属性均被注册为observable的

var Todo = function (title, completed) {

this.title = ko.observable(title);

this.completed = ko.observable(completed);

this.editing = ko.observable(false);

};

在ViewModel中,首先定义了下面属性:

todos,current,showmode,filteredTodos

其中todos被注册为observableArray,并用形参todos,调用map方法为ViewModel的todos属性赋值

current没有被赋值

showmode被初始化为'all'

filteredTodos是一个依赖属性,通过ko.computed()计算获得,其值依赖于todos和showmode属性,根据showmode的值,在todos中选择合适的todo对象

在计算todos和filteredTodos属性时,发现调用了map,filter方法,这些是ECMAScript5中定义的 Array的标准方法,其余常用的还有forEach,every,some等,

用KnockoutJS实现ToDoMVC代码分析

this.todos = ko.observableArray(todos.map(function (todo) {

return new Todo(todo.title, todo.completed);

}));

// store the new todo value being entered

this.current = ko.observable();

this.showMode = ko.observable('all');

this.filteredTodos = ko.computed(function () {

switch (this.showMode()) {

case 'active':

return this.todos().filter(function (todo) {

return !todo.completed();

});

case 'completed':

return this.todos().filter(function (todo) {

return todo.completed();

});

default:

return this.todos();

}

}.bind(this));

接下来依次为Viewmodel定义了add,remove,removeCompleted,editItem,saveEditing,cancelEditing,completedCount,remainingCount,allCompleted,getLabel方法

其中completedCount,remainingCount,allCompleted也是通过ko.computed()计算得出

与completedCount,remainingCount不同,allCompleted同时定义了read和write方法,在write方法中,将todos集合中各个todo对象的computed属性赋值

下面片段应用到了knockout中一个扩展用法

使用了Rate-limiting observable notifications,来达到在更新发生后的指定时间,来触发ko.computed()中的匿名函数

下面片段中,localStorage在knockout判断所有更新已结束,notifyWhenChangesStop函数触发后的500毫秒后将序列化为JSON对象的todos存到浏览器localStorage中

// internal computed observable that fires whenever anything changes in our todos

ko.computed(function () {

// store a clean copy to local storage, which also creates a dependency on the observableArray and all observables in each item

localStorage.setItem('todos-knockoutjs', ko.toJSON(this.todos));

}.bind(this)).extend({

rateLimit: { timeout: 500, method: 'notifyWhenChangesStop' }

}); // save at most twice per second

在将viewmodel绑定至ko时,以下代码先从localStorage读取,如有则使用,没有则为空,利用localStorage的本地存储功能,已经可以完成一个完整体验的todo app

最下面使用了一个Router来路由All Active Completed三个tab的请求,knockout自身不包含路由模块,这里的Router是由其余模块提供的

// check local storage for todos

var todos = ko.utils.parseJson(localStorage.getItem('todos-knockoutjs'));

// bind a new instance of our view model to the page

var viewModel = new ViewModel(todos || []);

ko.applyBindings(viewModel);

// set up filter routing

/*jshint newcap:false */

Router({ '/:filter': viewModel.showMode }).init();

说完了todo Model和ViewModel,再来看一下todo app中自定义绑定

在todo app中,分别提供了对键盘回车键ENTER_KEY、取消键ESCAPE_KEY的事件绑定

当为dom元素绑定enter_key、escape_key事件时,会以当前dom元素作用域执行赋予的valueAccessor函数

在selectAndFocus自定义绑定中,同时定义了init方法和update方法,在init中为dom元素注册了foucs方法,在update方法中来触发元素的focus,其目的是为了在选中todo元素,可以立即进入可编辑的状态

// A factory function we can use to create binding handlers for specific

// keycodes.

function keyhandlerBindingFactory(keyCode) {

return {

init: function (element, valueAccessor, allBindingsAccessor, data, bindingContext) {

var wrappedHandler, newValueAccessor;

// wrap the handler with a check for the enter key

wrappedHandler = function (data, event) {

if (event.keyCode === keyCode) {

valueAccessor().call(this, data, event);

}

};

// create a valueAccessor with the options that we would want to pass to the event binding

newValueAccessor = function () {

return {

keyup: wrappedHandler

};

};

// call the real event binding's init function

ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data, bindingContext);

}

};

}

// a custom binding to handle the enter key

ko.bindingHandlers.enterKey = keyhandlerBindingFactory(ENTER_KEY);

// another custom binding, this time to handle the escape key

ko.bindingHandlers.escapeKey = keyhandlerBindingFactory(ESCAPE_KEY);

// wrapper to hasFocus that also selects text and applies focus async

ko.bindingHandlers.selectAndFocus = {

init: function (element, valueAccessor, allBindingsAccessor, bindingContext) {

ko.bindingHandlers.hasFocus.init(element, valueAccessor, allBindingsAccessor, bindingContext);

ko.utils.registerEventHandler(element, 'focus', function () {

element.focus();

});

},

update: function (element, valueAccessor) {

ko.utils.unwrapObservable(valueAccessor()); // for dependency

// ensure that element is visible before trying to focus

setTimeout(function () {

ko.bindingHandlers.hasFocus.update(element, valueAccessor);

}, 0);

}

};

HTML View解析

在todo的输入框中,默认绑定了current属性,初始时current默认为空,所以显示的是placeholder中的值

输入框使用上上述自定义绑定,将ViewModel的add方法传给了enterKey

placeholder和autofucus都是HTML5支持的标准属性

下面片段是todo app的展示列表

列表ul元素使用foreach绑定了ViewModel的filteredTodos属性

每一个li对应一个todo对象

随着该todo被勾选为完成与否的变化,该li元素的css class同时发生变化

event:{dbclickL:$root.editItem}即为label元素绑定了双击事件,事件处理函数为ViewModel的editItem方法,而删除按钮绑定了click事件,事件处理函数为ViewModel的remove方法

当双击label元素时,li中的input输入框可见,可以对todo对象进行编辑,这里也采用了自定义绑定,同时绑定了enterKey,escapeKey,selectAndFocus事件,也绑定了标准事件blur,其事件处理函数为ViewModel的cancelEditing(该方法未实现)

篇5:用python登录Dr.com思路以及代码

2011-03-03布同 Python中文问题解决方法(总结了多位前人经验,初学者必看)

2014-02-02python使用rabbitmq实现网络爬虫示例

2014-01-01python使用paramiko模块实现ssh远程登陆上传文件并执行

2014-06-06win7 下搭建sublime的python开发环境的配置方法

2008-09-09Python 字符串中的字符倒转

2013-12-12python算法学习之桶排序算法实例(分块排序)

2014-01-01python使用scrapy解析js示例

2009-04-04用python实现的可以拷贝或剪切一个文件列表中的所有文件

2014-02-02python基础教程之popen函数操作其它程序的输入和输出示例

2014-02-02python求斐波那契数列示例分享

考研科目代码一览

javaweb代码学习总结

留言板生日祝福代码

网页设计的代码范文

10010是哪个学校代码

下载用pylint, 写好代码(合集5篇)
用pylint, 写好代码.doc
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
点击下载本文文档