#!/usr/bin/python # A script for posting diaries created by VIM Calender to blogger.com # Author: Wang Yuanxuan <zellux@gmail.com> import smtplib, os, re, datetime from email.mime.text import MIMEText fromaddr =xxxxx@fudan.edu.cn' toaddr =xxxx.xxxx@blogger.com' smtpserver ='mail.fudan.edu.cn' diarydir ='/home/user_name/diary' username ='xxxxxx' password ='xxxxxx' logpath = diarydir +'/poster.log' def PostMail(title, content): msg = MIMEText(content +'\r\n#end\r\n') msg['Subject'] = title msg['From'] = fromaddr msg['To'] = toaddr server = smtplib.SMTP(smtpserver) server.login(username, password) # server.set_debuglevel(1) server.sendmail(fromaddr, [toaddr], msg.as_string()) server.quit() # Load log file. Create a new one if not exist. posted = [] if os.path.isfile(logpath): temp = open(logpath, 'r') posted = [line[:-1] for line in temp.readlines()] log = open(logpath, 'a') else: print"A new poster log has been created at "+ logpath log = open(logpath, 'w') pattern = r'(\d{4})/(\d{1,2})/(\d{1,2}).cal$' scanner = re.compile(pattern) for (top, dirname, filenames) in os.walk(diarydir): for filename in filenames: fullpath = os.path.join(top, filename) if scanner.search(fullpath): (year, month, day) = scanner.search(fullpath).groups() filedate = datetime.date(int(year), int(month), int(day)) title = filedate.isoformat() if filedate == datetime.date.today(): continue if fullpath notin posted: log.write(fullpath +'\n') text = open(fullpath).read() PostMail(title, text) print'The diary '+ title +' has been posted' log.close()
]]>[zz]True closure in Pythonhttp://www.aygfsteel.com/zellux/archive/2008/02/11/179611.htmlZelluXZelluXMon, 11 Feb 2008 08:18:00 GMThttp://www.aygfsteel.com/zellux/archive/2008/02/11/179611.htmlhttp://www.aygfsteel.com/zellux/comments/179611.htmlhttp://www.aygfsteel.com/zellux/archive/2008/02/11/179611.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/179611.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/179611.html
Z么水木上的帖子每行末N是用I格填充的,每次转蝲q要先放到vim里面处理一下。。?br />
by ilovecpp
C:\Python\Lib>diff -u compiler/symbols.py.orig compiler/symbols.py
--- compiler/symbols.py.orig Thu Aug 17 10:28:56 2006
+++ compiler/symbols.py Mon Feb 11 12:03:01 2008
@@ -21,6 +21,7 @@
self.params = {}
self.frees = {}
self.cells = {}
+ self.outers = {}
self.children = []
# nested is true if the class could contain free variables,
# i.e. if it is nested within another function.
@@ -54,8 +55,10 @@
if self.params.has_key(name):
raise SyntaxError, "%s in %s is global and parameter" % \
(name, self.name)
- self.globals[name] = 1
- self.module.add_def(name)
+ if self.nested:
+ self.outers[name] = 1
+ else:
+ self.globals[name] = 1
def add_param(self, name):
name = self.mangle(name)
@@ -90,6 +93,8 @@
"""
if self.globals.has_key(name):
return SC_GLOBAL
+ if self.outers.has_key(name):
+ return SC_FREE
if self.cells.has_key(name):
return SC_CELL
if self.defs.has_key(name):
@@ -107,6 +112,7 @@
return ()
free = {}
free.update(self.frees)
+ free.update(self.outers)
for name in self.uses.keys():
if not (self.defs.has_key(name) or
self.globals.has_key(name)):
@@ -134,6 +140,9 @@
free.
"""
self.globals[name] = 1
+ if self.outers.has_key(name):
+ self.module.add_def(name)
+ del self.outers[name]
if self.frees.has_key(name):
del self.frees[name]
for child in self.children:
因ؓ我们没有修改built-in compilerQ所以程序要写在字符串里Q用compiler.compile~译Q用exec执行Q?br />
>>> from compiler import compile
>>> s = '''
... def counter():
... n = 0
... def inc():
... global n
... n += 1
... def dec():
... global n
... n -= 1
... def get():
... return n
... return inc, dec, get
... '''
>>> exec compile(s, '', 'exec')
>>> inc, dec, get = counter()
>>> get()
0
>>> inc()
>>> get()
1
>>> dec()
>>> get()
0
]]>在Python的for循环中计?/title>http://www.aygfsteel.com/zellux/archive/2007/12/25/170396.htmlZelluXZelluXTue, 25 Dec 2007 13:29:00 GMThttp://www.aygfsteel.com/zellux/archive/2007/12/25/170396.htmlhttp://www.aygfsteel.com/zellux/comments/170396.htmlhttp://www.aygfsteel.com/zellux/archive/2007/12/25/170396.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/170396.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/170396.html利用enumerate
for i, obj in enumerate(list):
print i, obj
Help on class enumerate in module __builtin__:
class enumerate(object)
| enumerate(iterable) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be an other object that supports
| iteration. The enumerate object yields pairs containing a count (from
| zero) and a value yielded by the iterable argument. enumerate is useful
| for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| next(...)
| x.next() -> the next value, or raise StopIteration
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object at 0xb7f35d20>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
]]>texttable - module for creating simple ASCII tableshttp://www.aygfsteel.com/zellux/archive/2007/09/10/144122.htmlZelluXZelluXMon, 10 Sep 2007 15:40:00 GMThttp://www.aygfsteel.com/zellux/archive/2007/09/10/144122.htmlhttp://www.aygfsteel.com/zellux/comments/144122.htmlhttp://www.aygfsteel.com/zellux/archive/2007/09/10/144122.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/144122.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/144122.htmlhttp://jefke.free.fr/soft/texttable/
dl: http://jefke.free.fr/soft/texttable/texttable.py
NAME
texttable - module for creating simple ASCII tables
FILE
/usr/lib/python2.3/site-packages/texttable.py
DESCRIPTION
Example:
table = Texttable()
table.header(["Name", "Age"])
table.set_cols_align(["l", "r"])
table.add_row(["Xavier\nHuon", 32])
table.add_row(["Baptiste\nClement", 1])
table.draw()
Result:
+----------+-----+
| Name | Age |
+==========+=====+
| Xavier | 32 |
| Huon | |
+----------+-----+
| Baptiste | 1 |
| Clement | |
+----------+-----+
CLASSES
exceptions.Exception
ArraySizeError
Texttable
class ArraySizeError(exceptions.Exception)
| Exception raised when specified rows don't fit the required size
|
| Methods defined here:
|
| __init__(self, msg)
|
| __str__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from exceptions.Exception:
|
| __getitem__(...)
class Texttable
| Methods defined here:
|
| __init__(self, max_width=80)
| Constructor
| - max_width is an integer, specifying the maximum width of the t
able
| - if set to 0, size is unlimited, therefore cells won't be wrapp
ed
|
| add_row(self, array)
| Add a row in the rows stack
|
| Cells can contain newlines.
|
| draw(self)
| Draw the table
|
| header(self, array)
| Specify the header of the table
|
| reset(self)
| Reset the instance:
| - reset rows and header
|
| set_chars(self, array)
| Set the characters used to draw lines between rows and
| columns.
|
| The array should contain 4 fields:
|
| [horizontal, vertical, corner, header]
|
| Default is set to:
|
| ['-', '|', '+', '=']
|
| set_cols_align(self, array)
| Set the desired columns alignment
|
| The elements of the array should be either "l", "c" or "r"
| - "l": column flushed left
| - "c": column centered
| - "r": column flushed right
|
| set_cols_width(self, array)
| Set the desired columns width
|
| The elements of the array should be integers, specifying the
| width of each column. For example:
|
| [10, 20, 5]
|
| set_deco(self, deco)
| Set the table decoration. 'deco' can be a combinaison of:
|
| Texttable.BORDER: Border around the table
| Texttable.HEADER: Horizontal line below the header
| Texttable.HLINES: Horizontal lines between rows
| Texttable.VLINES: Vertical lines between columns
|
| Example:
|
| Texttable.BORDER | Texttable.HEADER
|
| All of them are enabled by default.
|
| --------------------------------------------------------------------
--
| Data and other attributes defined here:
|
| BORDER = 1
|
| HEADER = 4
|
| HLINES = 8
|
| VLINES = 16
DATA
__all__ = ['Texttable', 'ArraySizeError']
__author__ = 'Gerome Fournier <jefke(at)free.fr>'
__credits__ = 'Jeff Kowalczyk:\n - textwrap improved import\n - ..
.
__license__ = 'GPL'
__revision__ = '$Id: texttable.py,v 1.3 2003/10/05 13:53:39 jef Exp je..
.
__version__ = '0.3'
VERSION
0.3
AUTHOR
Gerome Fournier <jefke(at)free.fr>
CREDITS
Jeff Kowalczyk:
- textwrap improved import
- comment concerning header output
def start_td(self, attrs): """ When encountered with tag td, check whether there's an align property in the tag, which will distinguish score table from others. """
for (k, v) in attrs: if (k =="align"): self.tdOpen =1 break
def end_td(self): self.tdOpen = 0
def handle_data(self, text): if (self.tdOpen > 0): if (len(text.strip()) > 0): self.colCount +=1 if (self.colCount >6): self.colCount = 0 self.firstRow = 0 print if (self.firstRow): return if (self.colCount ==2): print"\t", else: print text.strip(),"\t",
]]>Python 学习W记 (3)http://www.aygfsteel.com/zellux/archive/2007/07/10/129284.htmlZelluXZelluXTue, 10 Jul 2007 03:00:00 GMThttp://www.aygfsteel.com/zellux/archive/2007/07/10/129284.htmlhttp://www.aygfsteel.com/zellux/comments/129284.htmlhttp://www.aygfsteel.com/zellux/archive/2007/07/10/129284.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/129284.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/129284.html׃Python的判断语句返回值的Ҏ性,and-or语句可以辑ֈcM三元q算W的效果?br>bool ? a : b 可以写成 bool and a or b
]]>[zz]Ruby两本书读后感http://www.aygfsteel.com/zellux/archive/2007/06/16/124684.htmlZelluXZelluXSat, 16 Jun 2007 11:54:00 GMThttp://www.aygfsteel.com/zellux/archive/2007/06/16/124684.htmlhttp://www.aygfsteel.com/zellux/comments/124684.htmlhttp://www.aygfsteel.com/zellux/archive/2007/06/16/124684.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/124684.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/124684.htmlhttp://www.aurora-x.net/blog/oasis/?p=92 by opengl@rygh
那么入门I竟用什么书更好Q我推荐《Everyday Scripting with Ruby》这本。和《Programming Ruby》同一个出版社Q今q一月䆾刚出了原版。它是以相当循序渐进的方式带领读者进入Ruby的世界,其是作者精心设计的几个Project是全书亮点(学习一门语a最好的方式q是要动手写E序Q?/p>
]]>Ruby 学习W记 (2)http://www.aygfsteel.com/zellux/archive/2007/06/16/124681.htmlZelluXZelluXSat, 16 Jun 2007 11:30:00 GMThttp://www.aygfsteel.com/zellux/archive/2007/06/16/124681.htmlhttp://www.aygfsteel.com/zellux/comments/124681.htmlhttp://www.aygfsteel.com/zellux/archive/2007/06/16/124681.html#Feedback0http://www.aygfsteel.com/zellux/comments/commentRss/124681.htmlhttp://www.aygfsteel.com/zellux/services/trackbacks/124681.html1. Array#reject Ҏ遍历一个集合的每个元素Qƈ把符合条件的元素删去?br/>例如L数组中的所有素?br/>nums = nums.reject do | num | prime?(num) end puts nums
2. String#chomp Ҏ str.chomp(separator=$/) => new_str Returns a new +String+ with the given record separator removed from the end of _str_ (if present). If +$/+ has not been changed from the default Ruby record separator, then +chomp+ also removes carriage return characters (that is it will remove +\n+, +\r+, and +\r\n+).
5. 关于试 q本?Everyday Scripting with Ruby)的很多程序都是依循测试驱动开发的思想写出来的Q测试单元中的方法通常有两U目的?br/>一U是direct testQ需要测试那个函数就直接调用那个函数Q传递的参数都是直接写出来的?br/>另一U是bootstrapping testQ被试函数的参C是通过生成q些参数的函数生成的Q即一个方法测试了多个对象?br/>Everyone finds their own balance between testing directly and testing indirectly. You will too.
6. Time#strftime Ҏ t = Time.now t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003" t.strftime("at %I:%M%p") #=> "at 08:56AM"
irb(main):007:0> [1, 2, 3].each do | element | irb(main):008:1* puts element irb(main):009:1> end 1 2 3 => [1, 2, 3]
后者与前者的不同之处在于Q在处理数据的同Ӟ每次处理的返回结果都会保存到一个新的数l中q回?br>irb(main):036:0> newarray = ["aBC", "B"].collect do |e| irb(main):037:1* e.downcase irb(main):038:1> end => ["abc", "b"]
3. Messages and Methods It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it. 呃,q是没有感性认识?br> 4. Delimiting Blocks 块的两种表示方式Q?br>array.each do | element | puts element end array.each { | element | puts element } 通常使用W一U,但可以用一行写成的情况也可以用第二种Q?br>array.each { | element | puts element }