百度空间 | 百度首页 
 
文章列表
 
您正在查看 "Python" 分类下的文章

2009-10-12 12:34

 ubunoon 出的,已经完成pylons和uliweb的实现。响应者不多啊。

http://wiki.woodpecker.org.cn/moin/FrameworksShow

类别:Python | 评论(3) | 浏览()
 
2009-09-30 15:05

目前是在啄木鸟的wiki上创建的页面,具体内容参见: 

http://wiki.woodpecker.org.cn/moin/FrameworksShow

欢迎大家来展示和出题,不过出题及回答请看相应的说明。

类别:Python | 评论(9) | 浏览()
 
2009-02-22 11:20
在昨天与了关于pytz的东西后,还是发现一些问题。

>>> import pytz, datetime
>>> tz = pytz.timezone('Asia/Shanghai')
>>> tz
<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>

可以看到,它有一个LMT,这是Local Mean Time的缩写,网上查一查意思是本地平均时。而且时间是+8:06,说明与UTC的时差不是8个小时整。先不管它,让我们转换一下试试。

>>> d = datetime.datetime(2009,2,21,23,18,5,tzinfo=tz)
>>> d
datetime.datetime(2009, 2, 21, 23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)

好,时区与tz是一样的,没什么。

>>> x = d.astimezone(pytz.utc)
>>> x
datetime.datetime(2009, 2, 21, 15, 12, 5, tzinfo=<UTC>)

我们转为了UTC时区,时间上有差异,没问题。

让我们再转回来。

>>> x.astimezone(tz)
datetime.datetime(2009, 2, 21, 23, 12, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)

奇怪,看到了吧,变成了CST了。时差也成了+8:00了。CST就是Central Standard Time的意思。但这样就造成了转换的不一致。我们应该使用CST标准才对。

让我们再看一下:

>>> datetime.datetime.now(tz)
datetime.datetime(2009, 2, 22, 11, 11, 2, 125000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>> datetime.time(23, 18, 5, tzinfo=tz)
datetime.time(23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)

可以看到now()函数得到的是CST的,而time传入tzinfo是LMT的。(date不支持tzinfo参数)所以我们要进行修订,怎么做,使用timezone对象的localize()方法,如:

>>> d = datetime.datetime(2009,2,21,23,18,5)
>>> tz.localize(d)
datetime.datetime(2009, 2, 21, 23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)

所以我才明白pytz的文档上说的:

Creating localtimes is also tricky, and the reason why working with local times is not recommended. Unfortunately, you cannot just pass a ‘tzinfo’ argument when constructing a datetime.

所以我的建议是生成带时区的时间时,一定要使用timezone.localize()来生成。不要在时间对象的构造函数中传入tzinfo的方式来实现,为些我封装了一些函数放在了uliweb/utils/date.py中。

另外关于北京时间。在pytz中,我无法找到Asia/Beijing和GMT+8这样的时区设置,但是有些时间转换的工具却有。按理说pytz使用的是标准的时区数据库,我特意下载了查看,的确是没有。

时区处理的确是挺麻烦的事。象有些数据库也支持这样的功能,如postgres支持set timezone的命令,这是在django中看到的。



类别:Python | 评论(5) | 浏览()
 
2009-02-21 15:37
如果你的程序要考虑时区,可以使用pytz。datetime模块中有tzinfo相关的东西,但是它是一个抽象类,文档上说:

tzinfo is an abstract base clase, meaning that this class should not be instantiated directly. You need to derive a concrete subclass, and (at least) supply implementations of the standard tzinfo methods needed by the datetime methods you use. The datetime module does not supply any concrete subclasses of tzinfo.

上面是说tzinfo是一个抽象类,不应该被直接实例化。你需要派生子类,提供相应的标准方法。datetime模块并不提供tzinfo的任何子类。

所以你可能会使用pytz这个模块。通过easy_install可以安装。目前它的最新文档在这里

关于时区使用的几点想法:

1. 如果你的网站可能有来自其它时区的,可能你要考虑这个问题。都是一个地区的话,还要看服务器是否与用户在一个地区,如果不在,也要考虑。
2. 因此,基本上要考虑服务器时区与用户时区。服务器时区可以配置在系统中,全局生效。而用户时区则与用户相关,可以由用户自已进行设置。
3. 在生成相关时间对象时要加入时区的信息,并在输出时进行合适的转换。

而pytz提供了创建某个时区对象的方法,如,中国时区:

>>> import pytz
>>> pytz.country_timezones('cn')
['Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing', 'Asia/Urumqi', 'Asia/Kashgar']

可以看到,中国的时区可能有:'Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing',最后两个不知道是什么。我们可能使用的'Asia/Shanghai'比较多。

因此可以创建一个时区对象:

>>> tz = pytz.timezone('Asia/Shanghai')

然后在创建时间对象时进行指定:

>>> import datetime
>>> datetime.datetime.now(tz)
datetime.datetime(2009, 2, 21, 15, 12, 33, 906000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>> datetime.datetime(2009, 2, 21, 15, 12, 33, tzinfo=tz)
datetime.datetime(2009, 2, 21, 15, 12, 33, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)
>>> datetime.date(2009, 2, 21, tzinfo=tz)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tzinfo' is an invalid keyword argument for this function
>>> datetime.time(15, 12, 33, tzinfo=tz)
datetime.time(15, 12, 33, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)

从上面可以看出now(), datetime(), time()都是可以指定tzinfo信息的,而date是不行的,不知道为什么。所以最好的方法是内部使用datetime对象,需要时进行时区转换,然后再输出。

时区转换:

>>> utc = pytz.utc
>>> n = datetime.datetime.now(tz)
>>> n
datetime.datetime(2009, 2, 21, 15, 16, 41, 843000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>> new = n.astimezone(utc)
>>> new
datetime.datetime(2009, 2, 21, 7, 16, 41, 843000, tzinfo=<UTC>)
>>> utc.normalize(n.astimezone(utc))
datetime.datetime(2009, 2, 21, 7, 16, 41, 843000, tzinfo=<UTC>)

utc是世界标准时间。

上面的代码通过astimezone(utc)将中国时间转为utc标准时间,可以看到不同的时区时间已经不一样了。不过在pytz的文档上说:

Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct.

要注意什么呢?是 daylight savings time,中文叫日光节约时间或夏令时。对于有采用了夏时制的要使用normzlize来处理,不采用的,直接使用astimezone来处理。所以在通常情况下使用astimezone()就足够了。

另外pytz还提供了全部的timezone信息,如:

>>> from pytz import all_timezones
>>> len(all_timezones)
559
>>> from pytz import common_timezones
>>> len(common_timezones)
393

可以看到有很多。
类别:Python | 评论(2) | 浏览()
 
2009-02-12 22:45
今天在试验jython时,我使用的是2.5b1版本,结果发现在使用:

    from java.lang import *

时,得到的东西都是NoneType。结果我只好换成2.2版本就没有这个问题。

到了晚上终于搜到了:有人报告到jython的问题中,编号是 #1230

这个问题已经被修复,见这里。于1月21日改的。改动就是两行,但是太影响使用了。

如果你不想换回老版本(因为新版本有许多特性),那么可以:

    from java.lang import Class

之类的,这样就没有问题,不过就是有些麻烦。希望早些得到新版本。
类别:Python | 评论(0) | 浏览()
 
2009-02-10 10:21
平时logging模块我用得不多,不过在GAE上有时进行测试,我发现它可以输出执行的程序名。其实这一点并不困难,下面就是告诉你如何做。这里我今天在uliweb中修改的一段代码。

    import logging

    log = None
    FORMAT = "%(levelname)-8s %(asctime)-15s %(filename)s,%(lineno)d] %(message)s"

    def get_logger(format=FORMAT, datafmt=None):
        global log
        handler = logging.StreamHandler()
        fmt = logging.Formatter(format, datafmt)
        handler.setFormatter(fmt)
       
        log = logging.getLogger('uliweb')
        log.addHandler(handler)
        log.setLevel(logging.INFO)
        return log
       
    if __name__ == '__main__':
        log = get_logger()
        log.error('This is an error %s', 'aaaa')
        log.warning('This is an warning')
        try:
            1/0
        except Exception, e:
            log.exception(e)

每个Handler可以设置自已的日志格式,上面我预定义了一个FORMAT,其中%(asctime)-15s用来输出日,精确到毫秒,%(filename)s 输出文件主名,%(lineno)d为执行的行号,%(message)是用来输出用户的日志。这些参数都是logging自带的,在logging.py中的Formatter类中的详细的描述:

    %(name)s            Name of the logger (logging channel)
    %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
                        WARNING, ERROR, CRITICAL)
    %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
                        "WARNING", "ERROR", "CRITICAL")
    %(pathname)s        Full pathname of the source file where the logging
                        call was issued (if available)
    %(filename)s        Filename portion of pathname
    %(module)s          Module (name portion of filename)
    %(lineno)d          Source line number where the logging call was issued
                        (if available)
    %(funcName)s        Function name
    %(created)f         Time when the LogRecord was created (time.time()
                        return value)
    %(asctime)s         Textual time when the LogRecord was created
    %(msecs)d           Millisecond portion of the creation time
    %(relativeCreated)d Time in milliseconds when the LogRecord was created,
                        relative to the time the logging module was loaded
                        (typically at application startup time)
    %(thread)d          Thread ID (if available)
    %(threadName)s      Thread name (if available)
    %(process)d         Process ID (if available)
    %(message)s         The result of record.getMessage(), computed just as
                        the record is emitted

比文档好象详细多了。

上面的测试代码输出结果为:

    ERROR    2009-02-10 10:03:10,733 log.py,19] This is an error aaaa
    WARNING 2009-02-10 10:03:10,733 log.py,20] This is an warning
    ERROR    2009-02-10 10:03:10,733 log.py,24] integer division or modulo by zero
    Traceback (most recent call last):
      File "D:\project\svn\uliweb\uliweb\utils\log.py", line 22, in <module>
        1/0
    ZeroDivisionError: integer division or modulo by zero
类别:Python | 评论(0) | 浏览()
 
2009-02-04 22:47
The original article title is "Get with the program as contextmanager" wirtten by jesse. This article talks about the "with“ usage, it illustrates with many examples. If you have interesting in this, you should read it carefully.
类别:Python | 评论(1) | 浏览()
 
2009-02-04 16:25
I saw 浊风 has begun to revise my old "Django Step to Step" tutorial, my old tutorial is written at 2006.8, and at that time, the Django version is still 0.96. Later, I revised it to 0.97, but I've not maintained it any more. Because I'm working my own Python web framework -- Uliweb -- now. And some places in the tutorial can not be run in lastest 1.0.2 version, so 浊风's work is great! He doesn't just fix the problem in the tutorial, but rewrite it at all, and the main part are similar with the old one.

So I'm thinking maybe I should write a tutorial about Uliweb just like "Django Step to Step".

[Link]Django Step by Step 1.0.2 The new tutorial is not finished yet.
类别:Python | 评论(5) | 浏览()
 
2009-02-03 14:44
The artical is at here.

You can use this module to interactive when you are testing with doctest.

There are also some tests in Uliweb, and some of them are implemented by doctest, but some times, it's not easy to inspect the variables, run some other code when you executing the test, and this tool will help me a lot.

It's simple to use, just add below code in your doctest:

    >>> ...
    >>> import interlude
    >>> interlude.interact(locals())
    >>> ...

Remember, you should install interlude first, you can use:

    easy_install interlude

to install it.
类别:Python | 评论(0) | 浏览()
 
2008-12-17 16:05
今天在使用时,写了一个setup.py文件,结果在运行时出现: NameError: global name 'log' is not defined 在网上搜了一下,好象是0.6c8版本有问题。于是找到了0.6c9版本。不过安装后问题依旧,因为旧的版本没有删除,于是到Python的Lib/site-packages下将0.6c8删除,并且将easy_install.pth中的路径也删掉了,然后重新安装了0.6c9,于是正常了。
类别:Python | 评论(4) | 浏览()
 
     
 
 
文章分类
 
 
 
Uliweb(95)
 
Python(13)
 
Ulipad(11)
 
Web(3)
 
 
 
     
 
文章存档
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     
 
最新文章评论
   
 
 

期待ulicms~
 

噢,正好看到前几天有人问相同的问题,你已经回答过了。谢谢!
 
 
     


©2009 Baidu