ããã°å¼ã£è¶ã
å¼ã£è¶ãã¾ããã
æ°ããã° â Blog :: Meatware
ã¤ã¼ã¹ã¿ã¼ã¨ãã°
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
æå¾ã¯ãã¯ãªã¹ãã¹ã¨ããªã¹ãã§ç¹ãã£ããã¤ã¼ã¹ã¿ã¼ã¨ãã°ã«ã¤ãã¦ã§ãã
Python ã«é ãããåµãæ¢ãã¦æ¥½ãã¿ã¾ãããã
The Zen of Python
import this ããããã¨ã§ The Zen of Python ããã¤ã§ã表示ã§ãã¾ãã
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
ããããã®é
ç®ã®å¤ããè¤éã ããè©°ãè¾¼ã¿ããã ããèªã¿æããªããã§ãThe Zen of Python ã®ããã¹ã㯠The Zen of Python ãæèããã«æ¸ããã¦ããæ°ããã*1ã
空ç½ã§ã®ã¤ã³ãã³ããããã
from __future__ import braces ããããã¨ã§ã空ç½ã§ã®ã¤ã³ãã³ãå¶å¾¡ã C ã®ãããªãã¬ã¼ã¹ã®ã¹ã¿ã¤ã«ã«ã§ãããããã§ãã
ã¨ãããã¨ã§ãã£ã¦ã¿ãã¨â¦ã
>>> from __future__ import braces #doctest: +ELLIPSIS Traceback (most recent call last): ... SyntaxError: not a chance ...
not a chanceï¼çµ¶å¯¾ã«ããå¾ãªããï¼ï¼ã¨ããã¨ã©ã¼ãåºã¾ãã
ãã¬ã¼ã¹ã«ããå¶å¾¡ãæ¨æºã«å
¥ããã¨ã¯ç¡ãããã§ã*2ã
import __hello__
>>> import __hello__
Hello world...
ãªãã Hello world... ã¨ããæååãåºåããã¾ãã
ã㨠__phello__ ã使ã£ã¦ãåãã§ãã
>>> import __phello__ Hello world... >>> from __phello__ import spam Hello world...
ãã㯠C è¨èªãªã©ãã Python ã®ã¢ã¸ã¥ã¼ã«ã import ããã¨ãã«ããã¹ãã®ããã«ä½¿ã£ããããããã§ãã
antigravity
>>> import antigravity
ã¨æ¸ãã¨ããã©ã¦ã¶ãéããこのページã«é£ã°ããã¾ãã
ãããªæãã®ãã¨ãè¨ã£ã¦ãã¯ãã
AãPythonï¼ã
Bãé£ãã§ãï¼ã©ããã£ã¦ï¼ï¼ã
Aãæ¨æ¥ã®å¤ãPython ãåå¼·ãããã ãã¨ã¦ãã·ã³ãã«ã ã£ããã
ããHello World ãåºåãããªãåã« print "Hello, World!" ã¨æ¸ãã ãã§ãããã ã
Bãåçåä»ãï¼ç©ºç½ã¤ã³ãã³ãï¼ããåãããªããâ¦ãã
Aããã£ã¡ã«ããã§ãï¼ããã°ã©ãã³ã°ãã¾ã楽ãããªããï¼ããããã¯ä¸çå ¨ä½ãè¦æ¸¡ãããï¼ã
Bãã§ãåã¯ã©ããã£ã¦é£ãã ãã ãï¼ã
Aãimport antigravity ã£ã¦æã£ãã ãã ãã
Bãããã ãï¼ã
Aãâ¦â¦ãã¨ãPython ã¨æ¯è¼ããããã«è¬æ£ã®ä¸èº«ãå ¨é¨é£²ãã§ã¿ããã©â¦â¦ã
ããã§ããã㯠Python ã ãï¼å¤åï¼ã
.
*1:Python ã³ã¼ããããªãããããã®ãããããªããã©
*2:詳細㯠(訂正)Pythonの__future__ - 西尾泰和のはてなダイアリー ãåç §
æ£è¦è¡¨ç¾ãããã
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
Python ã®æ£è¦è¡¨ç¾ã«ã¯ã便å©ãªæ©è½ãããã¤ãããã¾ããã¨ãããã¨ã§ãããã¤ãè¦ã¦ããã¾ãããã
ã³ã¡ã³ãæ©è½
>>> import re >>> re.compile(r''' ... ^ # start of a line ... \[font # the font tag ... (?:=(?P<size> # optional [font=+size] ... [-+][0-9]{1,2} # size specification ... ))? ... \] # end of tag ... (.*?) # text between the tags ... \[/font\] # end of the tag ... ''', re.VERBOSE) #doctest: +ELLIPSIS <_sre.SRE_Pattern object at 0x...>
ãã®ããã« VERBOSE ãã©ã°ãä»ããã¨ã³ã¡ã³ããä»ããã¾ãã
VERBOSE ãã©ã°ã¯ã空ç½ã¨ # 以éã®æååãç¡è¦ããããã«ãªãã¾ãããã空ç½ã # ã使ãããå ´å㯠\# ã®ããã«ã¨ã¹ã±ã¼ãããå¿
è¦ãããã¾ãã
ãããã°ã¢ã¼ã
>>> import re >>> re.compile(r'^\[font(?:=(?P<size>[-+][0-9]{1,2}))?\](.*?)[/font]', re.DEBUG) #doctest: +ELLIPSIS at at_beginning literal 91 literal 102 literal 111 literal 110 literal 116 max_repeat 0 1 subpattern None literal 61 subpattern 1 in literal 45 literal 43 max_repeat 1 2 in range (48, 57) literal 93 subpattern 2 min_repeat 0 65535 any None in literal 47 literal 102 literal 111 literal 110 literal 116 <_sre.SRE_Pattern object at 0x...>
ãã®ããã«ãã©ããªç¶æ
æ©æ¢°ã«ãªã£ãã®ããæ¨æºåºåã«åºåãããã®ã§ããããã°ã容æã«ãªãã¾ãã
ç½®ææã«é¢æ°ã渡ã
sub 関数ã使ã£ã¦ç½®æããå ´åãé常ã¯æååãæå®ãã¾ãããé¢æ°ãæå®ãããã¨ãã§ãã¾ãã
>>> import re >>> re.sub('ab|bb', \ ... lambda m: 'xxx' if m.group(0) == 'ab' else 'yyy', \ ... 'aaabbb') 'aaxxxyyy'
ãã®ããã«é¢æ°ã渡ãã¨ãæååãããããããã³ã«é¢æ°ãå¼ã°ãã¾ãã
å¼æ°ã«ã¯ MatchObject ã渡ãããã©ã®æååã«ä¸è´ããããªã©ã®æ
å ±ãåãã¾ãã
è¤æ°ã®æååç½®æ
sub é¢æ°ã¯ãä¾ãã°è¤æ°ã®æååãç½®æããå ´åã«å½¹ç«ã¡ã¾ãã
ä¾ãã° html ã®ã¨ã¹ã±ã¼ããªã©ã§æ®éã« replace é¢æ°ã使ã£ãå ´åã以ä¸ã®æ§ãªåé¡ã«ééãããã¨ãããã¾ãã
>>> text = '<hoge>&&&</hoge>' >>> text.replace('<', '<') \ ... .replace('>', '>') \ ... .replace('&', '&') '&lt;hoge&gt;&&&&lt;/hoge&gt;'
'<' ã '<' ã«ç½®æããå¾ã'&' ã '&' ã«ç½®æãã¦ããã®ã§ '&lt;' ã«ãªã£ã¦ãã¾ããããã¯æå³ããåä½ã§ã¯ç¡ãã§ãããã
ãã®å ´åã§ããã° '&' ã®ç½®æãå
é ã«ããã°ä¸å¿åä½ã¯ãã¾ããããã®ãããªé åºã«ä¾åããå¤æã¯ãã°ãçºçãããããªã£ã¦ãã¾ãã¾ãã
ãã㯠sub ã®å¼æ°ã«é¢æ°ãæå®ãããã¨ã§ç°¡åã«ä¸æ¬ã§ç½®æã§ãã¾ãã
>>> def multiple_replace(text, dic): ... if len(dic) == 0: ... return text ... import re ... return re.sub('|'.join(map(re.escape, dic)), ... lambda m: dic[m.group(0)], ... text) >>> multiple_replace('<hoge>&&&</hoge>', { ... '<': '<', ... '>': '>', ... '&': '&' }) '<hoge>&&&</hoge>'
ããããã®ç½®æ対象ã®æååã '|' ã§ç¹ãã§ã©ããã«ããããããããã«ãã¦ããã®ãããããæååããã©ã®æååã«ç½®æãããããæå®ãã¦ãã¾ãã
Python ã®æ£è¦è¡¨ç¾ã¯ã¡ããã¨ä½¿ãã¨ããªã便å©ã«ãªãã®ã§ãä¸åº¦ã¯ re のドキュメントãå ¨é¨èªãã§ãããæ¹ãããã§ãããã
.
ã·ã°ããã£ç¢ºèª
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
dir é¢æ°ã§ããã®ãªãã¸ã§ã¯ããæã£ã¦ããã¡ã½ããã®ä¸è¦§ãåºåã§ãã¾ãã
>>> dir("foo") #doctest: +ELLIPSIS ['__add__', '__class__', ..., 'upper', 'zfill']
ã¾ããhelp é¢æ°ã使ããã¨ã§ããã®é¢æ°ãã¯ã©ã¹ã®èª¬æãè¦ããã¨ãã§ãã¾ãã
>>> help("foo".upper) Help on built-in function upper: upper(...) S.upper() -> string Return a copy of the string S converted to uppercase.
ããã¯åã« docstring ã表示ãã¦ããã ããªã®ã§ã以ä¸ã®æ§ã«èªä½ã®é¢æ°ã«ã¤ãã¦ã説æãè¦ããã¨ãã§ãã¾ãã
>>> def f(v): ... '''function f ... ... v - value ... ''' ... pass >>> help(f) Help on function f in module __main__: f(v) function f v - value
.
zlib, base64, rot13 ã¨ã³ã³ã¼ãã£ã³ã°
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
Python ã§ãã¼ã¿ãå§ç¸®ãããæãé常㯠zlib ã¢ã¸ã¥ã¼ã«ã使ã£ããããããããã¾ããããç¹ã«ç¹æ®ãªæå®ãå¿ è¦ã¨ããªãã®ã§ããã°ãencode é¢æ°ã§äºè¶³ãã¾ãã
>>> s = 'Hello World!!' >>> s.encode('zlib') 'x\x9c\xf3H\xcd\xc9\xc9W\x08\xcf/\xcaIQT\x04\x00 \xa8\x04_' >>> s.encode('zlib').decode('zlib') 'Hello World!!'
åæ§ã« base64 ãããã®ã§ãåç´ãªå©ç¨ã®å ´åã¯ãã¡ãã使ãã¨ããã§ãããã
>>> s.encode('base64') 'SGVsbG8gV29ybGQhIQ==\n' >>> s.encode('base64').decode('base64') 'Hello World!!'
ãã¨ã¯ rot13 ã¨ããã®ãããã¾ãã
ãã㯠a-z 㨠A-Z ã 13 åãã¼ãã¼ãããããåç´ãªæå·åã§ããã¢ã«ãã¡ããã㯠26 æåããã®ã§ãï¼åã¨ã³ã³ã¼ãããã¨å
ã«æ»ãã¾ãã
>>> s.encode('rot13') 'Uryyb Jbeyq!!' >>> s.encode('rot13').encode('rot13') 'Hello World!!'
.
yield ã®æ»ãå¤ãåãåã
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
yield ã®å¼ã³åºãã«ã¯æ»ãå¤ãããã¾ãã
æ®æ®µã¯ None ã§ãããå¼ã³åºãå
ã send é¢æ°ã使ã£ãå ´åããã®å¼æ°ã yield ã®æ»ãå¤ã¨ãã¦æ¸¡ããã¾ãã
>>> def f(xs): ... n = None ... for x in xs: ... if n is None or n == 0: ... n = yield x ... else: ... n -= 1 >>> it = f([1,2,3,4,5,6,7,8,9]) >>> it.next() 1 >>> it.send(1) # skip 1 3 >>> it.send(4) # skip 4 8
it.send(1) ã«ãã£ã¦ n = yield x ã® n ã« 1 ã渡ããã¾ãã
.
é¢æ°ã®ããã©ã«ãå¤ã®åæåã¿ã¤ãã³ã°
ãã®è¨äºã¯ Python Tips Advent Calendar 2012 ï¼ï¼æ¥ç®ã®è¨äºã§ãã
Python ã®é¢æ°ã«ã¯ããã©ã«ãå¤ãä»ãããã¾ãã
ãã®ããã©ã«ãå¤ã¯ããã®é¢æ°ãå¼ã³åºããã¿ã¤ãã³ã°ã§ã¯ãªãããã®é¢æ°ãå®ç¾©ããã¿ã¤ãã³ã°ã§åæåããã¾ãã
>>> def f(): ... print 'called f' ... return [] >>> def bar(x = f()): # ãã® bar é¢æ°ã®å®ç¾©ã§ f() ãå¼ã°ãã ... x.append(1) ... return x called f >>> bar() # ãã®å¼ã³åºãã®ã¨ãã«ã¯ f() ã¯å¼ã°ããªã [1] >>> bar() # åãããã©ã«ãå¤ã使ã£ã¦ããã®ã§ãååã®ãªã¹ããæ®ã£ãã¾ã¾ã«ãªã£ã¦ãã [1, 1] >>> bar() [1, 1, 1]
ãã®ããã«ãªãã¾ãã
ããå¼ã³åºããã¿ã¤ãã³ã°ã§ããã©ã«ãå¤ã決ããããªãããã®å¼æ°ã«é常å
¥ã£ã¦ããªãå®æ°ã¨æ¯è¼ããã®ãä¸è¬çãªããã§ãã
>>> def bar(x = None): ... x = x or f() # x ã False ã¨ãã¦è©ä¾¡ã§ããå¤ã ã£ãã f() ãä»£å ¥ãã ... x.append(1) ... return x >>> bar() called f [1] >>> bar() called f [1]
.