# 2005-07-29 《邮件列表的规范和礼节：创造良性发展的交流空间》幻灯

CPUG/BPUG成立大会上的报告。

## 用到的统计邮件列表中MUA使用情况的小程序

```   1 #!/usr/bin/env python
2
3 import email
4 import mailbox
5
6
7 class Statics(object):
8     def __init__(self):
9         self.muas = {}
10         self.muas_by_from = {}
11
12     def process(self, msg):
13         mua = self.get_mua(msg)
14         if not mua:
15             return
16
17         self.muas[mua] = self.muas.get(mua, 0) + 1
18
20
21     def get_mua(self, msg):
22         mua = msg.get('X-Mailer') or msg.get('User-Agent')
23         return mua
24
25     def get_frequent_muas(self):
26         muas = sorted(((count, mua) for mua, count in self.muas.iteritems()),
27                 reverse=True)
28         return muas
29
30     def get_popular_muas(self):
31         return sorted(((len(y), x) for x, y in self.muas_by_from.iteritems()),
32                 reverse=True)
33
34
35 def main():
36     import sys
37
38     mbox_fn = sys.argv[1]
39     mbox = mailbox.UnixMailbox(file(mbox_fn), email.message_from_file)
40
41     stat = Statics()
42
43     total = 0
44     for msg in mbox:
45         total += 1
46         stat.process(msg)
47     print total, 'messages processed'
48     print
49
50     print 'The top 10 MUAs are:'
51     m = stat.get_frequent_muas()
52     c = sum(x[0] for x in m)
53     for count, mua in m[:10]:
54         print mua, '(%.1f%%)' % (count * 100. / c)
55     print 'Messages without MUA field: %.1f%%' % ((total - c) * 100. / total)
56     print
57
58     print 'The top 10 popular MUAs are:'
59     m = stat.get_popular_muas()
60     c = sum(x[0] for x in m)
61     for count, mua in m[:10]:
62         print mua, '(%.1f%%)' % (count * 100. / c)
63
64
65
66 if __name__ == '__main__':
67     main()
```

## 反馈

QiangningHong/2005-07-29 (last edited 2009-12-25 07:14:57 by localhost)