巧妙使用引号 将Shell和Python杂交(为python代码选择解析器)

前面shell的作用就是为python代码选择解析器

#!/bin/bash
# -*- mode: Python -*-

""":"
# bash code here; finds a suitable python interpreter and execs this file.
# prefer unqualified "python" if suitable:
python -c 'import sys; sys.exit(not (0x020500b0 < sys.hexversion /dev/null 
    && exec python "$0" "$@"

for pyver in 2.6 2.7 2.5; do
    which python$pyver > /dev/null 2>&1 && exec python$pyver "$0" "$@"
done

echo "No appropriate python interpreter found." >&2
exit 1
":"""

print 'Hello from python code'

几个图形化工具

命令行是万能的什么都能搞定,很多时候为了快捷,即便是本地开发也是一直使用命令行、vim等。

在新的工作环境里,年轻程序员非常多,互联网公司的技术和传统系统软件差异很大,很多也比较新潮。还有就是我买了个鼠标。。。

最近觉得MySQLWorkbench比较好用,特别是联合查询的复杂sql。下面顺便介绍一下用着很爽的一些图形化软件:

  • MySQLWorkbench 数据库操作工具,不用每次都手动输入,或者从固定地方粘贴sql语句。很方便的数据导出、表结构字段调整。
  • Trello 项目管理网站:trello.com (手机APP也很好用),worktile也不错
  • Mou for mac 编辑MarkDown实时预览,快速产生富文本的内容。
  • PyCharm虽然有些vim很cool的特性缺少,但作为一个成熟的IDE很多自带功能需要vim配置很多插件才能达到
  • BeyondCompare:使用ftp/ssh等比较、同步本地服务器文件,vimdiff的效果,鼠标一点就同步不同的区域。缺点:有时候刷新比较慢,网络一挂服务器内容就为空了,再同步本地的也不见了。
  • Axure PP Pro: 出原型速度快,直观
  • FoxMail:mutt绝对牛逼,但是面对中文多、Excel多、富文本多、Thread乱的具体情况还是FoxMail对腾讯企业邮箱支持好。
  • Lantern: 代理工具,双击了事,智能切换。原来一直用ssh -D,然后还得手动切换,因为手动维护的autoproxy规则,有时候对子域名有问题。
  • Excel:不要鄙视我,我一直不用这个的,不过一些数据报表的处理还是能省好多事
  • Evernote:这个不用多介绍

还不能接受的GUI工具

  • TortoiseSVN:操作版本这种事,还是命令行更靠谱  svn blame/revert/diff/rebase/merge/log/…

命令行工具:

  • htop:升级版top
  • nethogs: 查看进程带宽实时占用比率

lua/python调用C动态库

使用lua编写游戏程序,往往使用c 或者 c++ 编写游戏引擎,那么就需要在lua中调用动态链接库,脚本语言写逻辑比低级语言方便。

//mylualib.c

#include 
#include 
#include 

static int hello_c (lua_State *L) {
    const char * from_lua = lua_tostring(L,1);
    printf("Lua: %sn",from_lua);    
    lua_pushstring(L,"Hi Lua, nice to meet you");
    return 1;
}
static const struct luaL_Reg mylualib [] = {
    {"hello_c", hello_c},
    {NULL, NULL} /* sentinel */
};
int luaopen_mylualib (lua_State *L) {
    luaL_newlib(L, mylualib);     /* register a array of c functions exported to lua */
    lua_pushvalue(L, -1);
    lua_setglobal(L, "mylualib"); /* the module name */

    return 1;
}
# main.lua 

require "mylualib"
hi = mylualib.hello_c("Hi C, I'm Lua")
print(hi)
#编译命令
gcc -O2 -bundle -undefined dynamic_lookup -o mylualib.so mylualib.c  -I /usr/local/include/

#执行
lua main.lua

python里边相对简洁一些

//mylib.c

#include 

int hello_c(char *str)
{
	printf("output param: %sn", str);
	return 0;
}
# main.py

#coding: utf-8
from ctypes import *
import os 

#参数为生成的.so文件所在的绝对路径
libtest = cdll.LoadLibrary(os.getcwd() + '/mylib.so') 

#直接用方法名进行调用
libtest.hello_c("Hello C, I'm calling you from Python") 
#编译命令
gcc -O2 -bundle -undefined dynamic_lookup -o mylib.so mylib.c

#执行
python main.py

(Python Author) Guido van Rossum on finding his way

Guido van Rossum is the creator and benevolent dictator for life (a.k.a. BDFL) of the Python programming language. Here he reflects on his path and shares what he’s been working on since joining Dropbox. Read on for this and a hint at what’s next for Dropbox, for Python, and for the BDFL himself.

You were an electronics hobbyist before becoming a programmer. How did you get started in electronics?

Okay, we’re going waaaay back. I don’t know exactly why I got into electronics as a kid. I remember that from the last grade in elementary school and probably through my second year in university, electronics kits and my own designs were one of my big passions.

It wasn’t always an easy path. In elementary school, I took one of the first projects I built into class as a show and tell project. There was no one who understood what it was or why it was interesting or cared. It’s a very vague memory. I just know that I took it in, and it fell flat.

Was there a time when you eventually found your people?

In high school, I hung out with a few other guys who had similar hobbies. We were all the geekiest kids in class.

I remember our physics teacher really encouraging us. The three of us worked together to build a large electronic device that was then used in demonstrations in front of the classroom. That was very satisfying.

Throughout high school, I had a very vague idea of what kinds of careers would be open to me. My general idea was that I was good at both sciences and languages and not so good in social areas. I ended up choosing mathematics.

How did you go from electronics to programming? Continue reading “(Python Author) Guido van Rossum on finding his way”