PyGTK TreeView的一些心得

TreeView是属于MVC(Model/View/Control)模式的。
所需要的构件有:
TreeView -树视图,用于显示数据
TreeViewColumn -视图的列
CellRenderer -渲染器,用于控制数据的显示效果
TreeModel/ListModel -树模式(树状/列表),用于保存数据

创建一个TreeView的步骤:
1、创建一个模式,TreeModel或ListModel;
2、向模式中添加数据;
3、创建视图TreeView,并添加之前创建的模式;
4、创建列TreeViewColumn,并添加视图TreeView中;
5、创建渲染器CellRenderer,并添加到列TreeViewColumn中;
6、设置行选择信号函数和渲染器操作信号函数。

其中GTK提供的渲染器有CellRendererAccel、CellRendererCombo、CellRendererPixbuf、CellRendererProgress、CellRendererSpin、CellRendererSpinner、CellRendererText、CellRendererToggle 8种

这里有个例子介绍了这几种渲染器的用法,可以参考下再举一反三:
https://gist.github.com/2389339

将PIL的Image类型转化为pygtk的Image类型

Python代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# PIL的Image类型转化为gtk的Image类型
import gtk
import StringIO

f = StringIO.StringIO()
#im为PIL的Image类型
im.save(f, "ppm")
contents = f.getvalue()
f.close()
loader = gtk.gdk.PixbufLoader("pnm")
loader.write(contents, len(contents))
pixbuf = loader.get_pixbuf()
loader.close()
i = gtk.Image()
i.set_from_pixbuf(pixbuf)

使用Python打印日历

基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7

在公式中d表示日期中的日数+1,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方:
把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
#-*- coding:utf-8 -*-
##
# @文件名(file): date.py
# @作者(author): 龙昌锦(LongChangjin)
# @博客(blog): http://www.xefan.com
# @时间(date): 2012-04-13
#

monthday = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
weekname = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
# 计算某年某月某日是星期几
# 基姆拉尔森计算公式
def week(y, m, d):
if m == 1 or m == 2:
m = m + 12
y = y - 1
w = ((d + 2 * m + 3 * (m+1) / 5 + y + y/4 - y/100 + y/400) + 1) % 7
return int(w)

year, month = input("输入日期,如:2012,4:")
if year%4 == 0:
monthday[1] = 29
w = week(year, month, 1)

print("%d年%d月" % (year, month))
print weekname[0], weekname[1], weekname[2], weekname[3], weekname[4], weekname[5], weekname[6]
i = 0
while i < w:
print(" "),
i = i + 1
i = 1
while i <= monthday[month-1]:
print("%3d " % i),
i = i + 1
w = w + 1
if w == 7:
print("")
w = 0

#运行结果如下:
#[lcj@lcj time]$ python date.py
#输入日期,如:2012,4:2012,2
#2012年2月
#星期日 星期一 星期二 星期三 星期四 星期五 星期六
# 1 2 3 4
# 5 6 7 8 9 10 11
# 12 13 14 15 16 17 18
# 19 20 21 22 23 24 25
# 26 27 28 29
#[lcj@lcj time]$

u-Boot使用nfs根文件系统

使用nfs根文件系统对于开发非常方便,不用每次编译好后又要烧写到开发板上。

设置方法:
打开开发板电源后进入u-boot,然后选择“[0] Set the boot parameters”。
再选择“[1] Set NFS boot parameter ”。
然后依次输入PC的IP、开发板的IP、子网掩码、NFS目录。
例子如下:
Enter the PC IP address:(xxx.xxx.xxx.xxx)
10.10.10.2
Enter the SKY2440/TQ2440 IP address:(xxx.xxx.xxx.xxx)
10.10.10.3
Enter the Mask IP address:(xxx.xxx.xxx.xxx)
255.255.255.0
Enter NFS directory:(eg: /opt/EmbedSky/root_nfs)
/opt/EmbedSky/root
bootargs: console=ttySAC0 root=/dev/nfs nfsroot=10.10.10.2:/opt/EmbedSky/root ip=10.10.10.3:10.10.10.2:10.10.10.3:255.255.255.0:SKY2440.embedsky.net:eth0:off

输入好后再选择“[s] Save the parameters to Nand Flash”保存设置即可。

以上是一般情况,下面再说说我的特殊情况。

=====================分割线===============================

我在PC上安装的是ArchLinux,按照以上方法运行时挂载失败,提示Root-NFS: Server returned error -93 while mounting /opt/EmbedSky/root
-93的意思好像是没有这个协议。

于是我又在虚拟机里安装了ubuntu,再尝试以上方法结果却成功了。
最后我查了资料,原因好像是u-boot使用的nfs3,而ArchLinux的官网上说nfs3太老了所以Arch用的是nfs4。因此就出现了这个问题。
解决方法:添加一个”,v3”参数。如:
Enter NFS directory:(eg: /opt/EmbedSky/root_nfs)
/opt/EmbedSky/root,v3
bootargs: console=ttySAC0 root=/dev/nfs nfsroot=10.10.10.2:/opt/EmbedSky/root,v3 ip=10.10.10.3:10.10.10.2:10.10.10.3:255.255.255.0:SKY2440.embedsky.net:eth0:off

Git常用命令

最近正在看Git,做个笔记,把常用的命令记下。

git init 初始化
git add <file> 将file添加到跟踪
git commit -m “..” 将修改提交到库,备注为”…”
git commit -a -m “…” 将所有跟踪文件全部提交
git status 查看状态
git rm <file> 移除文件,之后再commit提交
git mv <file1> <file2> 移动文件,之后再commit提交

git log 查看记录
git commit –amend 修改最后一次提交
git remote -v 查看远程仓库,-v显示地址
git remote add <shortname> <url> 添加远程仓库,别名为shortname
git fetch <remote-name> 从远程仓库抓取数据
git push <remote-name> <branch> 从本地branch推送到远程remote

git tag 显示所有标签
git tag -l <reg> 按照reg表达式来搜索标签
git tag -a <name> -m “..” 创建标签
git tag -a <name> <hash> 为某次提交打标签
git push remote —tags 推送本地所有标签

git branch <name> 从当前分支新建一个分支
git checkout <name> 切换到name分支
git checkout -b <name> 新建并切换到name分支
Gti merge <name> 将name分支合并到当前分支
git branch -d <name> 删除name分支
git branch -v 查看各分支最后一次提交

git fetch <remote-name> 从远程分支获取数据
git push <remote> [localbranch]:[remotebranch] 推送本地localbran到远程remotebranch,若localbranch参数为空则删除远程remotebranch分支
git chekcout -b <branch> <remote/branch> 从远程分支分化出一个新本地分支

git stash 暂存不想提交的修改
git stash list 查看暂存列表
git stash apply <stash-name> 应用暂存

git checkout —set-upstream <localbranch> <remote/branch> 本地分支localbranch跟踪远程分支branch
git clone <url> 克隆远程项目

git submodule add <url> <name> 创建子模块,保存到name目录
git submodule init 初始化子模块
git submodule update 从远程下载更新子模块

git archive [–format==tar|zip] [–prefix=<prefix>/] [-o file] <commit> [<path>…] 将commit提交记录中的path目录下的文件以format格式打包导出到file
git revert HEAD^ 撤消前前一次提交
git revert <hash> 撤消指定的版本
git revert —hard <commit> 彻底回退到某个版本

Linux进程通信——使用消息队列

头文件:

#include <sys/ipc.h>

#include <sys/msg.h>

#include <sys/types.h>

函数: key_t ftok(const char *filename, int proj_id);
通过文件名和项目号获得System V IPC键值(用于创建消息队列、共享内存所用)
proj_id:项目号,不为0即可
返回:成功则返回键值,失败则返回-1

函数: int msgget(key_t key, int msgflg);
key:键值,当为IPC_PRIVATE时新建一块共享内存;
shmflg:标志。
IPC_CREAT:内存不存在则新建,否则打开;
IPC_EXCL:只有在内存不存在时才创建,否则出错。
返回:成功则返回标识符,出错返回-1

函数: int msgsnd(int msgid, const void *msgp, size_t sz, int flg);
向消息队列发送消息
msgid:通过msgget获取
msgp:指向消息内容的指针
sz:消息内容的大小
flg:处理方式;如为IPC_NOWAIT时表示空间不足时不会阻塞
返回:成功则返回0,失败返回-1

函数: int msgrcv(int msgid, void *msgp, size_t sz, long type, int flg);
从消息队列读取消息
msgid:通过msgget获取
msgp:指向消息内容的指针
sz:消息内容的大小
type:指定接收的消息类型;若为0则队列第一条消息将被读取,而不管类型;若大于0则队列中同类型的消息将被读取,如在flg中设了MSG_RXCEPT位将读取指定类型的其他消息;若小于0读取绝对值小于type的消息。
flg:处理方式;
返回:成功返回收到消息长度,错误返回-1

函数: int msgctl(int msgid, int cmd, struct msgid_ds *buf);
msgid:通过msgget获取
cmd:控制命令,如下:
IPC_STAT:获取消息队列状态
IPC_SET:改变消息队列状态
IPC_RMID:删除消息队列
buf:结构体指针,用于存放消息队列状态
返回:成功返回与cmd相关的正数,错误返回-1

注意:消息队列一旦创建就会一直存在系统中,直到手动删除或者重启系统。可以使用ipcs -q命令来查看系统存在的消息队列。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/****************************************
*
* 使用消息队列进行进程通信——写进程
* 该进程用于创建信号量
* 龙昌博客:http://www.xefan.com
*
****************************************/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>

typedef struct _msg_buf{
long type; //消息类型
char buf[100]; //消息内容
} msg_buf;

int main()
{
int key, qid;
msg_buf buf;
key = ftok("tmp", 10);
qid = msgget(key, IPC_CREAT);
printf("key: %d\nqid: %d\n", key, qid);
buf.type = 10;
while (1)
{
fgets(buf.buf, 100, stdin);
if (msgsnd(qid, (void *)&buf, 100, 0) < 0)
{
perror("msgsnd");
exit(-1);
}
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/****************************************
*
* 使用消息队列进行进程通信——读进程
* 该进程用于创建信号量
* 龙昌博客:http://www.xefan.com
*
****************************************/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>

typedef struct _msg_buf{
long type; //消息类型
char buf[100]; //消息内容
} msg_buf;

int main()
{
int key, qid;
msg_buf buf;
key = ftok("tmp", 10);
qid = msgget(key, IPC_CREAT);
printf("key: %d\nqid: %d\n", key, qid);
while (1)
{
if (msgrcv(qid, (void *)&buf, 100, 0, 0) < 0)
{
perror("msgrcv");
exit(-1);
}
printf("type:%d\nget:%s\n", buf.type, buf.buf);
}
return 0;
}

先运行写进程再运行读进程。

Linux进程通信——使用信号量

头文件:

#include <sys/ipc.h>

#include <sys/sem.h>

#include <sys/types.h>

函数: key_t ftok(const char *filename, int proj_id);
通过文件名和项目号获得System V IPC键值(用于创建消息队列、共享内存所用)
proj_id:项目号,不为0即可
返回:成功则返回键值,失败则返回-1

函数: int semget(key_t key, int nsems, int msgflg);
key:键值,当为IPC_PRIVATE时新建。
nsems:信号个数。
msgflg:标志。
IPC_CREAT:不存在则新建,否则打开;
IPC_EXCL:与IPC_CREAT一同使用时,只有在不存在时才创建,否则出错。
返回:成功则返回IPC标识符,出错返回-1

函数: int semop(int semid, struct sembuf *sops, unsigned nsops);
semid:通过semget获取
sops:指向待操作的信号灯结构体,原型如下:
struct sembuf{
unsigned short sem_num; //信号灯编号,从0开始
short sem_op; //为正数代表释放信号;为负代表获取信号
Short sem_flg; //操作的标识;IPC_NOWAIT:不阻塞;IPC_UNDO:程序结束时释放信号量
}
nsops:要操作的信号量数
返回:成功则返回共享内存起始地址,失败返回-1

函数: void semctl(int semid, int semnum, int cmd, union semun arg);
semid:通过semget获取
semnum:操作的信号灯编号
cmd:控制命令,如下:
GETPID:获取sempid
GETVAL:获取semval
SETVAL:设置semval
IPC_RMID:删除信号灯
arg:各个量使用与cmd设置有关
返回:成功返回与cmd相关的正数,错误返回-1

注意:信号量一旦创建就会一直存在系统中,直到手动删除或者重启系统。可以使用ipcs -s命令来查看系统存在的信号量。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/****************************************
*
* 使用信号量进行进程通信——进程1
* 该进程用于创建信号量
* 龙昌博客:http://www.xefan.com
*
****************************************/
#include <sys/types.h>
//书上说是用sys/ipc.h和sys/sem.h这两个头文件,但是我用了出错,
//而用linux/ipc.h和linux/sem.h却没问题
//#include <sys/ipc.h>
//#include <sys/sem.h>
#include <linux/sem.h>
#include <linux/ipc.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
int key, sid, pid;
union semun val;
val.val = 1;
key = ftok("tmp",10);
if ((sid = semget(key, 1, IPC_CREAT)) < 0)
{
perror("semget");
exit(-1);
}
printf("key: %d sid:%d\n", key, sid);

if ((semctl(sid, 0, SETVAL, val)) < 0)
{
perror("semctl");
exit(-1);
}
if ((semctl(sid, 0, GETVAL, val)) < 0)
{
perror("semctl");
exit(-1);
}
printf("sem_val:%d\n", val.val);

struct sembuf p_op, v_op;
p_op.sem_num = 0;
p_op.sem_op = -1;
//获取信号
if (semop(sid, &p_op, 1) < 0) //p op
{
perror("smeop");
exit(-1);
}
printf("father get the semaphore\n");
sleep(8);
printf("father release the senaphore\n");
v_op.sem_num = 0;
v_op.sem_op = 1;
v_op.sem_flg = 0;
//释放信号
if (semop(sid, &v_op, 1) < 0) //v op
{
perror("semop");
exit(-1);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/****************************************
*
* 使用信号量进行进程通信——进程2
* 该进程用于获取信号量
* 龙昌博客:http://www.xefan.com
*
****************************************/
#include <sys/types.h>
//书上说是用sys/ipc.h和sys/sem.h这两个头文件,但是我用了出错,
//而用linux/ipc.h和linux/sem.h却没问题
//#include <sys/ipc.h>
//#include <sys/sem.h>
#include <linux/sem.h>
#include <linux/ipc.h>
#include <errno.h>
#include <math.h>

int main(int argc, char *argv[])
{
int key, sid, pid;
key = ftok("tmp",10);
if ((sid = semget(key, 1, IPC_CREAT)) < 0)
{
perror("semget");
exit(-1);
}
printf("key: %d sid:%d\n", key, sid);

struct sembuf p_op, v_op;
p_op.sem_num = 0;
p_op.sem_op = -1;
//p_op.sem_flg = 0;
if (semop(sid, &p_op, 1) < 0) //p op
{
perror("smeop");
exit(-1);
}
printf("father get the semaphore\n");
sleep(8);
printf("father release the senaphore\n");
v_op.sem_num = 0;
v_op.sem_op = 1;
v_op.sem_flg = 0;
if (semop(sid, &v_op, 1) < 0) //v op
{
perror("semop");
exit(-1);
}
return 0;
}

先运行进程1,再运行进程2查看效果

Linux进程通信——使用共享内存

头文件:

#include <sys/ipc.h>

#include <sys/shm.h>

函数:shmget 分配共享内存
函数原型: int shmget(key_t key, size_t size, int shmflg);
key:键值,当为IPC_PRIVATE时新建一块共享内存;若为0时而shmflg中设了IPC_PRIVATE也将新建。
size:内存大小。
shmflg:标志。
IPC_CREAT:内存不存在则新建,否则打开;
IPC_EXCL:只有在内存不存在时才创建,否则出错。
返回:成功则返回标识符,出错返回-1

函数: void shmat(int shmid, char shmaddr, int shmflg);
shmid:通过shmget获取
shmaddr:映射到进程地址空间的起始地址,设为NULL将自动分配
shmflg:标志;SHM-RDONLY为只读,否则可读可写
返回:成功则返回内存起始地址,失败返回-1

函数: int shmdt(const void *shmaddr);
使共享内存区脱离映射的进程的地址空间
返回:成功返回0,错误返回-1

函数: void shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid:通过shmget获取
cmd:控制命令,如下:
IPC_STAT:获取内存状态
IPC_SET:改变内存状态
IPC_RMID:删除内存
buf:结构体指针,用于存放共享内存状态
返回:成功返回0,错误返回-1

注意:共享内存一旦创建就会一直存在系统中,直到手动删除或者重启系统。
例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**************************************
*
* 使用共享内存进行进程通信
* 龙昌博客:http://www.xefan.com
*
**************************************/
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage:%s str\n", argv[0]);
exit(1);
}
int shmid;
char *p_addr, *c_addr;
//创建共享内存
if ((shmid=shmget(IPC_PRIVATE, 1024, IPC_CREAT)) == -1)
{
perror("shmget");
exit(errno);
}
if(fork() > 0) //父进程向内存写入数据
{
//获取内存起始地址
p_addr = shmat(shmid, 0, 0);
memset(p_addr, 0, 1024);
//向内存中写入数据
strncpy(p_addr, argv[1], 1024);
shmdt(p_addr);
sleep(3);
wait(0);
}
else //子进程从内存中读取数据
{
//获取内存起始地址
c_addr = shmat(shmid, 0, 0);
sleep(4);
printf("child get %s\n", c_addr);
shmdt(c_addr);
}
//删除共享内存
if (shmctl(shmid, IPC_RMID, NULL) < 0)
{
perror("shmctl");
exit(1);
}
return 0;
}

Linux进程通信——使用信号

函数:signal 设置某一信号对应的动作
头文件: #include <signal.h>
函数原型: void (signal(int signum, void (handler) (int))) (int);
signal:信号编号
handler:信号处理函数,若该参数不是函数指针则必须为以下两个常数之一:
SIG_IGN:忽略信号
SIG_DFL:重设为预设的处理方式
返回:成功则返回先前的信号处理函数指针,错误则返回SIG_ERR(-1)

函数:pause 让进程暂停直到信号出现
头文件:#include <unistd.h>
函数原型: int pause(void);
只返回-1

函数: kill 传送信号
头文件:

#include <signal.h>

#include <sys/types.h>
函数原型: int kill(pid_t pid, int sig);
pid:接收信号的进程号;当pid=0时为相同进程组的所有进程;当pid=-1时为系统内所有进程;当pid>0时为指定进程;当pid<-1时为进程组识别码为pid绝对值的所有进程。
sig:要传送的信号
返回:成功返回0,错误返回-1

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/****************************************
*
*设置信号处理
*等待2秒后向进程本身传送一个SIGALRM信号
*龙昌博客:http://www.xefan.com
*
****************************************/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void func(int sig_no)
{
if (sig_no == SIGALRM)
{
printf("get SIGALRM\n");
}
}

int main()
{
printf("%d waiting for signal..\n", getpid());
alarm(2);
signal(SIGALRM, func);
raise(SIGALRM);
pause();
return 0;
}

Linux进程通信——使用有名管道

无名管道只能用于父子进程通信,而有名管道可以用于任意进程间通信。
头文件:

#include <sys/types.h>

#include <sys/stat.h>

函数:
int mkfifo(const char *filename, mode_t mode);
创建有名管道对应的实名文件,该文件必须事先不存在。
返回0代表创建成功,否则返回-1。
filename:文件路径
mode:文件权限

创建成功之后可以像操作普通文件一样使用read、write进行读写操作。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**************************************
*
* 使用有名管道进行进程通信——写进程
* 文件名:fifo_write.c
* 龙昌博客:http://www.xefan.com
*
**************************************/

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>

#define FIFO "/tmp/myfifo"

int main(int argc, char *argv[])
{
int fd;
char buf[100];
int num;

unlink(FIFO); //先删除文件
//创建有名管道
if ( (mkfifo(FIFO, O_CREAT|O_EXCL|O_RDWR) < 0) && (errno != EEXIST))
{
perror("mkfifo");
}

fd = open(FIFO, O_WRONLY);
if (fd < 0)
{
perror("open");
exit(1);
}
while (1)
{
scanf("%s", buf);
if ((num=write(fd, buf, 100)) < 0)
{
if(errno == EAGAIN)
{
printf("FIFO has not been read yet.\n");
}
}
}
close(fd);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**************************************
*
* 使用有名管道进行进程通信——读进程
* 文件名:fifo_read.c
* 龙昌博客:http://www.xefan.com
*
**************************************/
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#define FIFO "/tmp/myfifo"

int main(int argc, char *argv[])
{
int fd;
int num;
char buf[100];

//打开管道文件
//fd = open(FIFO, O_RDONLY|O_NONBLOCK); //非阻塞方式打开
fd = open(FIFO, O_RDONLY);
if (fd < 0)
{
perror("open");
exit(1);
}
while (1)
{
memset(buf, 0, sizeof(buf));
if ((num=read(fd, buf, 100)) < 0)
{
if (errno == EAGAIN)
{
printf("no data yet\n");
}
sleep(1);
continue;
}
printf("read %s from FIFO\n", buf);
sleep(1);
}
return 0;
}

先运行写进程再运行读进程