°Ù¶È¿Õ¼ä | °Ù¶ÈÊ×Ò³ 
 
ÎÄÕÂÁбí
 
ÄúÕýÔڲ鿴 "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µÄËõд£¬ÍøÉϲéÒ»²éÒâ˼ÊDZ¾µØÆ½¾ùʱ¡£¶øÇÒʱ¼äÊÇ+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ʹÓõÄÊDZê×¼µÄÊ±ÇøÊý¾Ý¿â£¬ÎÒÌØÒâÏÂÔØÁ˲鿴£¬µÄÈ·ÊÇûÓС£

Ê±Çø´¦ÀíµÄÈ·ÊÇͦÂé·³µÄÊ¡£ÏóÓÐЩÊý¾Ý¿âÒ²Ö§³ÖÕâÑùµÄ¹¦ÄÜ£¬Èç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ÊDz»Ðе쬲»ÖªµÀΪʲô¡£ËùÒÔ×îºÃµÄ·½·¨ÊÇÄÚ²¿Ê¹ÓÃ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ÉÏÓÐʱ½øÐвâÊÔ£¬ÎÒ·¢ÏÖËü¿ÉÒÔÊä³öÖ´ÐеijÌÐòÃû¡£ÆäʵÕâÒ»µã²¢²»À§ÄÑ£¬ÏÂÃæ¾ÍÊǸæËßÄãÈçºÎ×ö¡£ÕâÀïÎÒ½ñÌìÔÚ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(96)
 
Python(13)
 
Ulipad(12)
 
Web(3)
 
 
 
     
 
ÎÄÕ´浵
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     
 
×îÐÂÎÄÕÂÆÀÂÛ
   
 
 
 
 
 
     


©2009 Baidu