发布于 2014-10-09 13:57:55 | 201 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的Python3 官方中文指南,程序狗速度看过来!
Python编程语言
Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
本文是一个python实现sublime3的less编译插件示例代码,感兴趣的同学参考下.
利用http://tool.oschina.net/less 提供的接口,发送请求进行远程编译.
再将编译好的less,保存为同名后缀为css的文件中.
第一次使用python,代码也是拼拼凑凑的.需要加上线程进行异步请求,但是不会...
class exampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
file_name=self.view.file_name();
if file_name.find('.less') == -1:
print('only .less file can compile to css!!');
return;
file_object_from = open(file_name);
all_the_text = file_object_from.read();
url = "http://tool.oschina.net/action/less/less_compile";
data = all_the_text.encode(encoding='UTF8');
headers = {'User-Agent':'sublime_plugin'};
req = urllib.request.Request(url,data,headers);
response = urllib.request.urlopen(req);
the_page = response.read();
css=json.loads(the_page.decode("utf8"))['css'];
file_object_to = open(self.view.file_name().replace('.less', '.css'), 'w')
file_object_to.write(css);
file_object_from.close();
file_object_to.close();
print(css);