Flaskããã³Werkzeug Webãã¬ãŒã ã¯ãŒã¯ã®äœæè ã§ããArmin RonacherãJinja2ãã³ãã¬ãŒããšã³ãžã³ãããã³3çªç®ã®pythonã®ãµããŒããè¿œå ããéã«åœŒã®ãããžã§ã¯ãã§äœ¿çšãããŠããçŸåšã®ãã¯ããã¯ãšèœãšãç©Žã«ã€ããŠã®äžè¬çã«æåãªpythonistã«ããèå³æ·±ãèšäºã®ç¿»èš³ã玹ä»ããŸãã ãã®èšäºã®ã¿ã€ãã«ã«é¢ããçãã¡ã¢ã ããã¯ãArminã®2010幎ã®èšäºãPython 3ãžã®ç§»æ€ãã¬ã€ããžã®åç §ã§ããã 2to3ãŠãŒãã£ãªãã£ãä»ããèªå移æ€ã®ããã®ã³ãŒãã®æºåã«ã€ããŠèª¬æããŠããŸãã å®è·µã瀺ãããã«ãä»æ¥ããã®ã¢ãããŒãã¯æ¬¡ã®ããã«ã¢ã³ããã¿ãŒã³ã§ããå¯èœæ§ãé«ã äžæ¹ã§ã¯ããã®ãããªæäœã®çµæãšããŠã®ã³ãŒãã®å質ã¯èããæªåããŠãããããã«ããã®ãããªã³ãŒãã¯ä¿å®ãèããå°é£ã§ãã
Jinja2ã3çªç®ã®pythonã«ç§»æ€ãããšããéåžžã«èŠçãªçµéšã®åŸããããžã§ã¯ãããã°ããã¢ã€ãã«ç¶æ ã®ãŸãŸã«ããŠããå¿ èŠããããŸããã ç§ã¯ãPythonããŒãžã§ã³3ã®ãµããŒããäžæããããšãããŸãã«ãæããŠããŸããã ç§ã䜿çšããã¢ãããŒãã¯ãPythonããŒãžã§ã³2ã®ã³ãŒããèšè¿°ããããã±ãŒãžã®ã€ã³ã¹ããŒã«æã«2to3ã3çªç®ã®pythonã«å€æããããšã§æ§æãããŠããŸããã æãäžå¿«ãªå¯äœçšã¯ãå€æŽãè¡ããšç¿»èš³ã«çŽ1åããããããå埩ã®é床ãäœäžããããšã§ãã 幞ããªããšã«ãæçµããŒãžã§ã³ã®pythonãæ£ããæå®ãããšãããã»ã¹ãå€§å¹ ã«é«éã«ãªãããšãããããŸããã
MoinMoinãããžã§ã¯ãã®Thomas Voldmanã¯ãæ£ãããã©ã¡ãŒã¿ãŒã䜿çšããŠpython-modernizeããJinja2ãèµ·åããããšããå§ãã2.6ã2.7ã3.3ã§åäœããåäžã®ã³ãŒããæãä»ããŸããã å°ããªãã³ãã䜿çšããŠããã¹ãŠã®ããŒãžã§ã³ã®pythonã§åäœããåæã«ã»ãšãã©ã®éšåãéåžžã®pythonã³ãŒãã®ããã«èŠããçŽ æŽãããã³ãŒãããŒã¹ã«å°éããããšãã§ããŸããã
ãã®çµæã«è§ŠçºãããŠãç§ã¯ã³ãŒããäœåºŠã調ã¹ãçµåãããã³ãŒãããŒã¹ãããã«å®éšããããã«ä»ã®ã³ãŒãã®ç¿»èš³ãéå§ããŸããã
ãã®èšäºã§ã¯ã䌌ããããªç¶æ³ã§èª°ããå©ããå Žåã«å ±æã§ããããã€ãã®ãã³ããšããªãã¯ãéžæçã«ã¬ãã¥ãŒããŸãã
ãµããŒã2.5ã3.1ãããã³3.2ãç Žæ£ãã
ããã¯æãéèŠãªãã³ãã®1ã€ã§ãã Python 2.5ã®äœ¿çšãæåŠããããšã¯ãå€ãã®äººã䜿çšããŠããªããããä»æ¥ã§ã¯å¯èœã§ãã 3.1ãš3.2ã®æåŠã¯ã3çªç®ã®pythonã®äººæ°ãäœãããšãèãããšãããªãç°¡åãªè§£æ±ºçã§ãã ãããããããã®ããŒãžã§ã³ã®ãµããŒããæåŠããæå³ã¯äœã§ããïŒ èŠããã«ã2.6ãš3.3ã«ã¯ãäž¡æ¹ã®å Žåã«åãã³ãŒããæ£åžžã«åäœããããšãå¯èœã«ããå€æ°ã®éè€ããæ§æãšæ©èœãå«ãŸããŠããŸãã
- äºææ§ã®ããæååãªãã©ã«ã 2.6ããã³3.3ã¯ãæååã«å¯ŸããŠåãæ§æããµããŒãããŠããŸãã ãã€ãã£ãã®æåååïŒ2.xã®ãã€ãæååãš3.xã®UnicodeæååïŒã«ã¯
u'foo'
ãUnicodeæååã«ã¯b'foo'
ããã€ãæååãŸãã¯ãã€ããªããžã§ã¯ãã«ã¯b'foo'
äž¡æ¹ã䜿çšã§ããŸãã - äºææ§ã®ãã
print
æ§æãfrom __future__ import print_function
ã䜿çšããå Žåã¯ãã©ãããŒé¢æ°ã䜿çšãããä»ã®éäºææ§ã«æ©ãŸãããããšãªããfrom __future__ import print_function
è¿œåfrom __future__ import print_function
ãŠé¢æ°ãšããŠprint
ã䜿çšã§ããŸãã - äºææ§ã®ãããã£ããã£æ§æã Python 2.6ã§ã¯ã3.xã§äœ¿çšã
except Exception as e
ãæ°ããæ§æãå°å ¥ãããŠããŸãã - ã¯ã©ã¹ãã³ã¬ãŒã¿ãå©çšå¯èœã§ãã ã¯ã©ã¹æ§é ã«ãã¬ãŒã¹ãæ®ããã«ã移åããã€ã³ã¿ãŒãã§ã€ã¹ãèªåçã«ä¿®æ£ããã®ã«éåžžã«äŸ¿å©ã§ãã ããšãã°ãPython 2.xã§ã¯ãã¡ãœããåã
next
ãã__next__
ããŸãã¯__str__
ãã__unicode__
ã«èªåçã«ååå€æŽããã®ã«åœ¹ç«ã¡ãŸãã -
next
ãŸãã¯__next__
ãåŒã³åºãçµã¿èŸŒã¿é¢æ°next()
ã 䟿å©ã ãã çŽæ¥çãªã¡ãœããåŒã³åºããšã»ãŒåãé床ã§åäœãããããã©ã³ã¿ã€ã ãã§ãã¯ãç¬èªã®ã©ãããŒé¢æ°ã®è¿œå ãšæ¯èŒããŠããã©ãŒãã³ã¹ãæ¯æãå¿ èŠã¯ãããŸããã - Python 2.6ã§ã¯ã3.3ãšåãã€ã³ã¿ãŒãã§ãŒã¹ãæã€æ°ãã
bytearray
åãè¿œå ãããŸããã ããã¯äŸ¿å©ã§ã python 2.6ã«ã¯bytes
ãªããžã§ã¯ãããããŸããããçµã¿èŸŒã¿ãªããžã§ã¯ãããããåãååãæã¡ãstr
ãšå矩ã§ããããŸã£ããç°ãªãåäœããstr
ã - Python 3.3ã§ã¯ã3.1ããã³3.2ã§å£ããŠãããã€ããããã€ããžãããã³è¡ããè¡ãžå€æããããã®ã³ãŒããã¯ãåç»å ŽããŸããã æ®å¿µãªããããããã®ã€ã³ã¿ãŒãã§ãŒã¹ã¯ããè€éã«ãªãããšã€ãªã¢ã¹ã¯ãããŸããããããã¯ä»¥åããããã¹ãŠ2.xã«ãã£ããã®ã«ãã£ãšè¿ããªã£ãŠããŸãã ããã¯ãã¹ããªãŒã ããŒã¹ã®ã³ãŒãã£ã³ã°ãå¿ èŠãªå Žåã«ç¹ã«éèŠã§ãã ãã®æ©èœã¯3.0ãã3.3ãŸã§å®å šã«æ¬ èœããŠããŸããã
ã¯ãã 6ã€ã®ã¢ãžã¥ãŒã«ã¯åé²ã«åœ¹ç«ã¡ãŸãããã¯ãªãŒã³ãªã³ãŒããèŠãããšãã§ããããšã®å©ç¹ãéå°è©äŸ¡ããªãã§ãã ããã 3çªç®ã®python Jinja2ãžã®ç§»æ€ããµããŒãããããšã«èå³ã倱ããŸããã ç§ã¯åœŒå¥³ã®ã³ãŒãã«ææãèŠããŸããã åœæãçµåãããã³ãŒãã¯èŠèŠãããããã©ãŒãã³ã¹ã®ç¹ã§åé¡ããããŸããïŒå®æ°
six.b('foo')
ããã³
six.u('foo')
ïŒããŸãã¯2to3ã®äœãå埩é床ã§ããã ããŠãããããã¹ãŠãæ±ã£ãåŸãç§ã¯åã³æ¥œãã¿ãŸãã Jinja2ã³ãŒãã¯éåžžã«ãããã«èŠããã®ã§ãPython 2ããã³3ããŒãžã§ã³ã®äºææ§ãµããŒããèŠã€ããããã«æ€çŽ¢ããå¿ èŠããããŸãã
if PY2:
ã¹ã¿ã€ã«ã§äœããè¡ãã³ãŒãã¯ããããã
if PY2:
èšäºã®æ®ãã®éšåã§ã¯ããããã®ããŒãžã§ã³ã®pythonããµããŒãããããšãåæãšããŠããŸãã ãŸããPythonããŒãžã§ã³2.5ããµããŒãããè©Šã¿ã¯éåžžã«èŠçãªã®ã§ãããããæ§ããããšã匷ããå§ãããŸãã ãã¹ãŠã®è¡ãé¢æ°åŒã³åºãã§ã©ããããæºåãã§ããŠããå Žåã¯ããµããŒã3.2ãå¯èœã§ããããã¯ãçŸèŠ³ãšããã©ãŒãã³ã¹ã®çç±ããå人çã«ã¯ãå§ãããŸããã
6ããããããŸã
Sixã¯éåžžã«ãã¡ããšããã©ã€ãã©ãªã§ãããJinja2ã¯ããããå§ããŸããã ãããæåŸã«ãæ°ããã°ã6ã€ç®ã¯3çªç®ã®pythonã®äžã§ããŒããèµ·åããããã«å¿ èŠãªãã®ãããã»ã©å€ããªãã§ãããã ãã¡ãããPython 2.5ããµããŒãããå Žåã¯6ã€ãå¿ èŠã§ããã2.6以éã§ã¯6ã䜿çšããçç±ã¯ããŸããããŸããã Jinja2ã«ã¯ãããã€ãã®å¿ èŠãªãã«ããŒãå«ã_compatã¢ãžã¥ãŒã«ããããŸãã Python 3ã«ãªãè€æ°ã®è¡ãå«ããŠãäºææ§ã¢ãžã¥ãŒã«å šäœã«å«ãŸããã³ãŒãã¯80è¡æªæºã§ãã
ããã¯ãç°ãªãã©ã€ãã©ãªãŸãã¯ãããžã§ã¯ãã«ç°ãªãäŸåé¢ä¿ãè¿œå ããããã«ããŠãŒã¶ãŒãããã±ãŒãž6ã®ç°ãªãããŒãžã§ã³ãæåŸ ããå Žåã®åé¡ãåé¿ããã®ã«åœ¹ç«ã¡ãŸãã
ã¢ããã€ãºããå§ãã
Python-modernizeã¯ã移æ€ãéå§ããã®ã«é©ããã©ã€ãã©ãªã§ãã ããã¯ãPythonã®äž¡æ¹ã®ããŒãžã§ã³ã§åäœããã³ãŒããçæãã2to3ããŒãžã§ã³ã§ãã ååãªãã°ããããããã©ã«ãã®ãªãã·ã§ã³ãæé©ã§ã¯ãªããšããäºå®ã«ãããããããéå±ãªä»äºãããŠçå£ã«åé²ããã®ã«åœ¹ç«ã¡ãŸãã ãã®å Žåãã³ãŒããèŠçŽããŠãã€ã³ããŒããšã©ããã¹ãã¯ãªãŒã³ã¢ããããå¿ èŠããããŸãã
ãã¹ããä¿®æ£ãã
ä»ã®ããšãå§ããåã«ããã¹ãã調ã¹ãŠããŸã æå³ã倱ãããŠããªãããšã確èªããŠãã ããã æšæºã®Pythonã©ã€ãã©ãªããŒãžã§ã³3.0ããã³3.1ã«ã¯ã移æ€ã®çµæãšããŠãã¹ãã®åäœãäžæ³šæã«å€æŽãããçµæãå€æ°ã®åé¡ãçºçããŸããã
äºææ§ã¢ãžã¥ãŒã«ãæžã
ããã§ãããªãã6人ããããããããšã«æ±ºãããªããããªãã¯ãã«ããŒãªãã§çããããšãã§ããŸããïŒ æ£è§£ã¯ããŒã§ãã ãŸã å°ããªäºææ§ã¢ãžã¥ãŒã«ãå¿ èŠã§ãããããã±ãŒãžã«å ¥ããŠããã«ã¯ååã«å°ããå¿ èŠããããŸãã äºææ§ã¢ãžã¥ãŒã«ã®å€èŠ³ã®ç°¡åãªäŸã次ã«ç€ºããŸãã
import sys PY2 = sys.version_info[0] == 2 if not PY2: text_type = str string_types = (str,) unichr = chr else: text_type = unicode string_types = (str, unicode) unichr = unichr
ãã®ã¢ãžã¥ãŒã«ã®ã³ãŒãã¯ãã©ãã ãå€æŽããããã«ãã£ãŠç°ãªããŸãã Jinja2ã®å Žåãããã€ãã®é¢æ°ãããã«é 眮ããŸããã ããšãã°ã ifilter ã imapãããã³3.xã®æšæºã©ã€ãã©ãªã®äžéšã«ãªã£ãitertoolsã®ä»ã®åæ§ã®é¢æ°ããããŸãïŒ2.xã®é¢æ°åã䜿çšããŠãå埩åã®äœ¿çšãæå³çã§ãããšã©ãŒã§ã¯ãªãããšãèªè ã«æ確ã«ããŸãïŒ ïŒ
3.xã§ã¯ãªã2.xã確èªããŸã
ããæç¹ã§ãã³ãŒããPythonã®2.xãŸãã¯3.xããŒãžã§ã³ã§å®è¡ãããŠãããã©ããã確èªããå¿ èŠããããŸãã ãã®å Žåãæåã«2çªç®ã®ããŒãžã§ã³ããã§ãã¯ããelseãã©ã³ãã§3çªç®ã®ããŒãžã§ã³ã«ãã§ãã¯ãå ¥ããéãåæ§ã«è¡ãããšããå§ãããŸãã ãã®å ŽåãPythonã®4çªç®ã®ããŒãžã§ã³ã衚瀺ããããšãã«äžå¿«ãªé©ããå°ãªããªããŸãã
è¯ãïŒ
if PY2: def __str__(self): return self.__unicode__().encode('utf-8')
ããã»ã©å®ç§ã§ã¯ãªãïŒ
if not PY3: def __str__(self): return self.__unicode__().encode('utf-8')
ã©ã€ã³åŠç
ééããªãã3çªç®ã®pythonã§ã®æ倧ã®å€æŽã¯ãUnicodeã€ã³ã¿ãŒãã§ã€ã¹ã®å€æŽã§ããã æ®å¿µãªããããããã®å€æŽã¯äžéšã®å Žæã§ããªãèŠçã§ããããšãå€æããæšæºã©ã€ãã©ãªãäžè²«ããŠå€æŽããŸããã ã»ãšãã©ã®ç§»æ€æéã¯ãã®æ®µéã§è²»ããããŸãã ããã¯å®éã«ã¯å¥ã®èšäºã®ãããã¯ã§ãããJinja2ãšWerkzeugãé å®ããŠãããã€ã³ãã®å°ããªãªã¹ãã以äžã«ç€ºããŸãã
-
'foo'
åžžã«ããã€ãã£ãã¹ããªã³ã°å®è£ ãšåŒã°ãããã®ãæå³ããŸãã ãããã¯ãèå¥åããœãŒã¹ã³ãŒãããã¡ã€ã«åãããã³ãã®ä»ã®äœã¬ãã«é¢æ°ã§äœ¿çšãããæååã§ãã ããã«ã2.xã§ã¯ãUnicodeæååããªãã©ã«ãšããŠäœ¿çšã§ããŸãããASCIIæåã®ã¿ãå«ãŸããŠããå Žåã®ã¿ã§ãã
ãã®æ©èœã¯ã次ã®ããã«åäžã®ã³ãŒãããŒã¹ã«éåžžã«åœ¹ç«ã¡ãŸãã 3çªç®ã®pythonã®äžè¬çãªåŸåã¯ã以åã¯ãµããŒãããŠããªãã£ãã€ã³ã¿ãŒãã§ã€ã¹ã«UnicodeãµããŒããè¿œå ããããšã§ãã ãã€ãã£ãã®æååãªãã©ã«ã¯Unicodeã«ãã¢ããã°ã¬ãŒãããããŸããã2.xã§ãUnicodeããµããŒãããŠãããããéåžžã«äŸ¿å©ã§ãã
ããšãã°ãé¢æ°datetime.strftime
ã¯Python 2ã§ã¯UnicodeããŸã£ãããµããŒãããŠããŸãããã3çªç®ã®ããŒãžã§ã³ã§ã¯Unicodeã®ã¿ããµããŒãããŠããŸãã ã»ãšãã©ã®å Žåã2.xã®æ»ãå€ã¯ASCIIã®ã¿ã§ãã£ãããããã®ãããªããšã¯2.xãš3.xã®äž¡æ¹ã§æ©èœããŸãã
>>> u'<p>Current time: %s' % datetime.datetime.utcnow().strftime('%H:%M') u'<p>Current time: 23:52'
strftime
æž¡ãããæååstrftime
ãã€ãã£ãã§ãïŒ2.xã®ãã€ãã3.xã®UnicodeïŒã æ»ãå€ããã€ãã£ãæååã§ãããASCIIã®ã¿ã§ãã ãã®çµæãé©åã«ãã©ãŒããããããUnicodeæååã2.xãš3.xã®äž¡æ¹ã§è¿ãããŸãã -
u'foo'
åžžã«Unicodeæååãæå³ããŸãã å€æ°ã®ã©ã€ãã©ãªããã§ã«2.xã®Unicodeãå®å šã«ãµããŒãããŠãããããUnicodeãªãã©ã«ã¯èª°ã«ãšã£ãŠãé©ãã§ã¯ãããŸããã -
b'foo'
åžžã«ãå®éã®ãã€ãã®æ ŒçŽæ¹æ³ãç¥ã£ãŠããäœããæå³ããŸãã 2.6ã«ã¯å®éã«ã¯bytes
ãªããžã§ã¯ãããªããããPython 3.3ãšã¯ç°ãªããå®éã®ãã€ãè¡ãæ¬ ããŠããããããã®ãªãã©ã«ã®æçšæ§ã¯å€å°å¶éãããŠããŸãã ãã ãã2.xãš3.xã§åãã€ã³ã¿ãŒãã§ã€ã¹ãæã€bytearray
ãªããžã§ã¯ããšäžç·ã«äœ¿çšãããšãåã³äŸ¿å©ã«ãªããŸãã
>>> bytearray(b' foo ').strip() bytearray(b'foo')
ããã¯å¯å€ã§ããããããã€ããçŽæ¥æäœãã段éã§ããã銎æã¿ã®ãããã®ã«å€æããŠãçµæãå床bytes()
ã©ããã§ãbytes()
ã
ãããã®åçŽãªã«ãŒã«ã«å ããŠãäžèšã®ããã«ãäºæã¢ãžã¥ãŒã«ã«å€æ°
text_type
ã
unichr
text_type
ãè¿œå ããŸããã ãã®çµæã次ã®å€æŽãçºçããŸãã
-
isinstance(x, basestring)
ã¯isinstance(x, string_types)
-
isinstance(x, unicode)
ã¯isinstance(x, text_type)
-
isinstance(x, str)
ãã€ããåŠçããå¿ èŠãããå Žåãisinstance(x, bytes)
ãŸãã¯isinstance(x, (bytes, bytearray))
ãŸãã
__str__
ãŸãã¯
__str__
ã¯ã©ã¹ã
implements_to_string
ããã®ã«åœ¹ç«ã€ã¯ã©ã¹ãã³ã¬ãŒã¿ãŒ
implements_to_string
ã
__str__
ã
if PY2: def implements_to_string(cls): cls.__unicode__ = cls.__str__ cls.__str__ = lambda x: x.__unicode__().encode('utf-8') return cls else: implements_to_string = lambda x: x
äž»ãªã¢ã€ãã¢ã¯ã
__str__
ã¡ãœããã2.xãš3.xã®äž¡æ¹ã§å®è£ ããUnicodeæååãè¿ãããšã§ãïŒã¯ããããã¯2.xã§ã¯ããäžæ Œå¥œã«èŠããŸãïŒããã³ã¬ãŒã¿ã¯èªåçã«
__unicode__
ã2.xã«ååå€æŽããŸãã ã
__str__
ãåŒã³åºããutf-8ãžã®åŒã³åºãã®çµæããšã³ã³ãŒããã
__str__
ãè¿œå ããŸãã ãã®ã¢ãããŒãã¯ã2.xã®ã¢ãžã¥ãŒã«ã§æè¿åºãæ®åããŠããŸãã ããšãã°ãJinja2ãDjangoãåæ§ã§ãã
以äžã«äœ¿çšäŸã瀺ããŸãã
@implements_to_string class User(object): def __init__(self, username): self.username = username def __str__(self): return self.username
ã¡ã¿ã¯ã©ã¹æ§æã®å€æŽ
3çªç®ã®pythonã§ã¯ãã¡ã¿ã¯ã©ã¹ãå®çŸ©ããããã®æ§æã®å€æŽã¯2çªç®ãšã¯äºææ§ããªãããã移æ€ããã»ã¹ã¯ããå°ãé£ãããªããŸãã Sixã«ã¯ããã®åé¡ã解決ããããã«èšèšããã
with_metaclass
é¢æ°ããããŸãã 圌女ã¯ç©ºã®ã¯ã©ã¹ãäœæããç¶æ¿ããªãŒã«è¡šç€ºããŸãã Jinja2ã®ãã®ãœãªã¥ãŒã·ã§ã³ã¯æ°ã«å ¥ããªãã£ãã®ã§ãå€æŽããŸããã å€éšAPIã¯åããŸãŸã§ãããå®è£ ã§ã¯äžæã¯ã©ã¹ã䜿çšããŠã¡ã¿ã¯ã©ã¹ãè¿œå ããŸãã ãã®ãœãªã¥ãŒã·ã§ã³ã®å©ç¹ã¯ã䜿çšããããã«ããã©ãŒãã³ã¹ãæ¯æãå¿ èŠããªããç¶æ¿ããªãŒãã¯ãªãŒã³ãªãŸãŸã§ããããšã§ãã
ãœãªã¥ãŒã·ã§ã³ã³ãŒããç解ããã®ã¯ããæ··ä¹±ããŸãã äž»ãªã¢ã€ãã¢ã¯ã芪ã¯ã©ã¹ã«ãã£ãŠäœ¿çšãããäœææã«ã¯ã©ã¹ãå€æŽããã¡ã¿ã¯ã©ã¹ã®æ©èœã«äŸåããŠããŸãã ç§ã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ãã¯ã©ã¹ãç¶æ¿ãããšãã«ã¡ã¿ã¯ã©ã¹ã䜿çšããŠãç¶æ¿ããªãŒãã芪ãåé€ããŸãã æåŸã«ãé¢æ°ã¯ç©ºã®ã¡ã¿ã¯ã©ã¹ãæã€ç©ºã®ã¯ã©ã¹ãäœæããŸãã ç¶æ¿ããã空ã®ã¯ã©ã¹ã®ã¡ã¿ã¯ã©ã¹ã«ã¯ãæ£ãã芪ããæ°ããã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åããç®çã®ã¡ã¿ã¯ã©ã¹ãå²ãåœãŠãã³ã³ã¹ãã©ã¯ã¿ãŒããããŸãïŒæ³šïŒãã¹ãŠãæ£ãã翻蚳ãããŠãããã©ããã¯ããããŸãã ã 以äžã®ãœãŒã¹ã¯ããéåŒã«èŠããŸãïŒ ã ãããã£ãŠã空ã®ã¯ã©ã¹ãšã¡ã¿ã¯ã©ã¹ã¯æ±ºããŠè¡šç€ºãããŸããã
ããã¯æ¬¡ã®ãããªãã®ã§ãã
def with_metaclass(meta, *bases): class metaclass(meta): __call__ = type.__call__ __init__ = type.__init__ def __new__(cls, name, this_bases, d): if this_bases is None: return type.__new__(cls, name, (), d) return meta(name, bases, d) return metaclass('temporary_class', None, {}) And here is how you use it: class BaseForm(object): pass class FormType(type): pass class Form(with_metaclass(FormType, BaseForm)): pass
èŸæž
3çªç®ã®pythonã§ã®åä»ãªå€æŽã®1ã€ã¯ãèŸæžã€ãã¬ãŒã¿ã®ãããã³ã«ã®å€æŽã§ããã 2çªç®ã®pythonã§ã¯ããã¹ãŠã®èŸæžã«ã¡ãœããããããŸããïŒãªã¹ããè¿ã
keys()
ã
values()
ããã³
items()
ãããã³
iterkeys()
ã
itervalues()
ããã³
iteritems()
ã 3çªç®ã®pythonã§ã¯ãã©ããããã§ã¯ãããŸããã 代ããã«ããã¥ãŒãªããžã§ã¯ããè¿ãã¡ãœããã«çœ®ãæããããŸããã
keys()
ã¯äžå€ã»ããã®ããã«æ¯ãèããã¥ãŒãªããžã§ã¯ããè¿ãã
values()
ã¯èªã¿åãå°çšæ©èœãåããå埩å¯èœãªã³ã³ããïŒã€ãã¬ãŒã¿ã§ã¯ãããŸããïŒïŒãè¿ãã
items()
ã¯äžå€ã»ããã«äŒŒããã®ãè¿ããŸãã éåžžã®ã»ãããšã¯ç°ãªãããããã¯å¯å€ãªããžã§ã¯ããæãããšãã§ããŸãããã®å Žåãããã°ã©ã ã®å®è¡äžã«ããã€ãã®ã¡ãœãããèœã¡ãå¯èœæ§ããããŸãã
ãã¥ãŒãªããžã§ã¯ãã¯ã€ãã¬ãŒã¿ã§ã¯ãªããšããç¹ãå€ãã®äººãèŠéããŠãããšããäºå®ã«ãããããããã»ãšãã©ã®å Žåããããåã«ç¡èŠããããšãã§ããŸãã WerkzeugãšDjangoã¯ç¬èªã®èŸæžã®ãããªãªããžã§ã¯ããããã€ãå®è£ ããŠããŸããã©ã¡ãã®å Žåãã解決çã¯åã«ãã¥ãŒãªããžã§ã¯ãã®ååšãç¡èŠãã
keys()
ãšãã®å人ãã€ãã¬ãŒã¿ãè¿ãããã«ããããšã§ããã
çŸæç¹ã§ã¯ãPythonã€ã³ã¿ãŒããªã¿ãŒã課ãå¶éãèæ ®ã«å ¥ããŠããããå¯äžã®åççãªãœãªã¥ãŒã·ã§ã³ã§ãã 以äžã«åé¡ããããŸãã
- ãã¥ãŒãªããžã§ã¯ãèªäœãå埩åã§ã¯ãªããšããããšã¯ãç¹å¥ãªçç±ããªãäžæãªããžã§ã¯ããäœæããããšãæå³ããŸãã
- çµã¿èŸŒã¿èŸæžãã¥ãŒãªããžã§ã¯ãã®ã»ãããšåæ§ã®åäœã¯ã ã€ã³ã¿ãŒããªã¿ãŒã®å¶éã®ããã«çŽç²ãªPythonã§ã¯åçŸã§ããŸãã
- 3.xã®ãã¥ãŒãªããžã§ã¯ããš2.xã®ã€ãã¬ãŒã¿ã®å®è£ ã¯ãå€ãã®ã³ãŒãã®ç¹°ãè¿ããæå³ããŸãã
èŸæžã®ç¹°ãè¿ãåŠçã«é¢ããŠãJinja2ãããããã®ã¯æ¬¡ã®ãšããã§ãã
if PY2: iterkeys = lambda d: d.iterkeys() itervalues = lambda d: d.itervalues() iteritems = lambda d: d.iteritems() else: iterkeys = lambda d: iter(d.keys()) itervalues = lambda d: iter(d.values()) iteritems = lambda d: iter(d.items())
èŸæžã®ãããªãªããžã§ã¯ããå®è£ ããããã«ãã¯ã©ã¹ãã³ã¬ãŒã¿ã¯åã³åœ¹ç«ã¡ãŸãã
if PY2: def implements_dict_iteration(cls): cls.iterkeys = cls.keys cls.itervalues = cls.values cls.iteritems = cls.items cls.keys = lambda x: list(x.iterkeys()) cls.values = lambda x: list(x.itervalues()) cls.items = lambda x: list(x.iteritems()) return cls else: implements_dict_iteration = lambda x: x
ãã®å Žåãããªããããªããã°ãªããªãã®ã¯ã
keys()
ã¡ãœãããšãã®åéãã€ãã¬ãŒã¿ãŒãšããŠå®è£ ããããšã ãã§ãããã以å€ã¯ãã¹ãŠèªåçã«è¡ãããŸãã
@implements_dict_iteration class MyDict(object): ... def keys(self): for key, value in iteritems(self): yield key def values(self): for key, value in iteritems(self): yield value def items(self): ...
äžè¬çãªã€ãã¬ãŒã¿ã®å€æŽ
ã€ãã¬ãŒã¿ã¯åºæ¬çã«å€æŽãããŠãããããç¶æ³ãä¿®æ£ããã«ã¯ããã€ãã®ãã«ããŒãå¿ èŠã§ãã å®éãå¯äžã®å€æŽã¯
next()
ãã
__next__
ãžã®ç§»è¡
__next__
ã 幞ããªããšã«ãããã¯ãã§ã«ééçã«åŠçãããŠããŸãã ããªããããå¿ èŠãããå¯äžã®ããšã¯
next(x)
x.next()
ãä¿®æ£ããããšã§ãããæ®ãã¯PythonãåŠçããŸãã
ã€ãã¬ãŒã¿ã宣èšããå Žåã¯ãã¯ã©ã¹ãã³ã¬ãŒã¿ã圹ç«ã¡ãŸãã
if PY2: def implements_iterator(cls): cls.next = cls.__next__ del cls.__next__ return cls else: implements_iterator = lambda x: x
ã¯ã©ã¹ãå®è£ ããã«ã¯ã次ã®å埩ã¹ããã
__next__
ã¡ãœããã«ååãä»ããã ã
__next__
ã
@implements_iterator class UppercasingIterator(object): def __init__(self, iterable): self._iter = iter(iterable) def __iter__(self): return self def __next__(self): return next(self._iter).upper()
ã³ãŒããã¯ãå€æŽãã
2çªç®ã®pythonã®ãšã³ã³ãŒãã£ã³ã°ãããã³ã«ã®åªããæ©èœã®1ã€ã¯ããã®ã¿ã€ãã®ç¬ç«æ§ã§ãã å¿ èŠã«å¿ããŠãcsvãã¡ã€ã«ãnumpyé åã«å€æãããšã³ã³ãŒããç»é²ã§ããŸãã ãã ãããã®å¯èœæ§ã¯ããŸãç¥ãããŠããŸããã§ããããã¢ã³ã¹ãã¬ãŒã·ã§ã³äžãæååãšã³ã³ãŒããã¡ã€ã³ã®ãšã³ã³ãŒãã€ã³ã¿ãŒãã§ã€ã¹ã§ãã£ãããã§ãã 3.x以éãããå³æ Œã«ãªã£ããããã»ãšãã©ã®æ©èœã¯ããŒãžã§ã³3.0ã§åé€ããã3.3ã«æ»ããŸããã ãã®äŸ¡å€ã蚌æããŸããã ç°¡åã«èšãã°ãUnicodeãšãã€ãã®éã§ãšã³ã³ãŒãããªãã³ãŒããã¯ã¯3.3ãŸã§å©çšã§ããŸããã§ããã ããšãã°ãã³ãŒããã¯hexããã³base64ã
ãããã®ã³ãŒããã¯ã䜿çšãã2ã€ã®äŸã¯ãã©ã€ã³æäœãšããŒã¿ã¹ããªãŒã æäœã§ãã 2.xã®å€ã
str.encode()
ãå€æŽãããŸããã 2.xããã³3.xããµããŒãããå ŽåãAPIæååã®å€æŽãèããŸãïŒ
>>> import codecs >>> codecs.encode(b'Hey!', 'base64_codec') 'SGV5IQ==\n'
ãŸãã3.3ã®ã³ãŒããã¯ã§ã¯ãšã€ãªã¢ã¹ã倱ãããŠããããã
'base64'
ã§ã¯ãªã
'base64_codec'
æ瀺çã«èšè¿°ããå¿ èŠããããŸãã
ãããã®ã³ãŒããã¯ã䜿çšããããšã¯ã binacsiiã¢ãžã¥ãŒã«ã®é¢æ°ã䜿çšãããããæãŸããã§ãã ãããã¯ãã€ã³ã¯ãªã¡ã³ã¿ã«ãšã³ã³ãŒãã£ã³ã°ãšãã³ãŒãã£ã³ã°ã®ãµããŒããéããŠãããŒã¿ã¹ããªãŒã ã®æäœããµããŒãããŸã ã
ãã®ä»ã®æ³šæäºé
ãŸãããŸã è¯ã解決çããªãããè¿·æãªç¹ãããã€ããããŸããããããã¯ããŸãæ±ãã«ããã®ã§ã察åŠããããããŸããã æ®å¿µãªããããããã®ããã€ãã¯3çªç®ã®python APIã®äžéšã§ãããå¢çã®ã±ãŒã¹ãèæ ®ãããŸã§ã»ãšãã©èŠããŸããã
- Linuxã§ã¯ããã¡ã€ã«ã·ã¹ãã ãšãã¡ã€ã«IOãžã®ã¢ã¯ã»ã¹ãåŒãç¶ãåä»ã§ãã Unicodeã«åºã¥ããŠããŸããã
open()
é¢æ°ãšãã¡ã€ã«ã·ã¹ãã ã¬ãã«ã«ã¯ãå±éºãªããã©ã«ãèšå®ããããŸãã ããšãã°ãde_AT
ãã·ã³ããen_US
ãã±ãŒã«ã®ãã·ã³ã«SSHã§de_AT
å ŽåãPythonã¯ãã¡ã€ã«ã·ã¹ãã ã®æäœãšãã¡ã€ã«æäœã®ããã«ASCIIãšã³ã³ãŒãã£ã³ã°ã«åãæ¿ããã®ã奜ãã§ãã
äžè¬çãªã±ãŒã¹ã§ã¯ã3çªç®ã®pythonã§ããã¹ããæäœããæãä¿¡é Œæ§ã®é«ãæ¹æ³ãæ€èšããŸããããã¯2.xã§ãæ£åžžã«æ©èœããŸãããã¡ã€ã«ããã€ããªã¢ãŒãã§éããæ瀺çã«ãã³ãŒãããã ãã§ãã ãŸãã¯ã2.xã§codecs.open
ãŸãã¯io.open
䜿çšããæ瀺çãªãšã³ã³ãŒãã£ã³ã°ã§3.xã§çµã¿èŸŒã¿ã®open
ã䜿çšããããšãã§ããŸãã - æšæºã©ã€ãã©ãªã®URLã¯Unicode圢åŒã§èª€ã£ãŠè¡šç€ºããã3.xã®äžéšã®URLã®éåžžã®äœ¿çšã劚ããå¯èœæ§ããããŸãã
- ãã¬ãŒã¹ããã¯ãªããžã§ã¯ãã䜿çšããå®è¡ã®åé€ã«ã¯ãã¢ã·ã¹ã¿ã³ãæ©èœãå¿
èŠã§ãã æ§æãå€æŽãããŸããã äžè¬ã«ãããã¯ããŸãäžè¬çãªåé¡ã§ã¯ãªããã©ãããŒã«ãã£ãŠéåžžã«ç°¡åã«è§£æ±ºã§ããŸãã ãªããªã æ§æãå€æŽãããŸãããããã§ã¯ãexecãããã¯å
ã«ã³ãŒããé
眮ããå¿
èŠããããŸãã
if PY2: exec('def reraise(tp, value, tb):\n raise tp, value, tb') else: def reraise(tp, value, tb): raise value.with_traceback(tb)
- 以åã®
exec
ããã¯ã¯ãæ§æã«äŸåããã³ãŒããããå Žåã«åœ¹ç«ã¡ãŸãã ãã ããexecèªäœã®æ§æãå€æŽãããŠãããããä»»æã®åå空éã§äœããåŒã³åºãæ¹æ³ã¯ãããŸããã ããã¯å€§ããªåé¡ã§ã¯ãããŸããããªããªãeval
ããã³compile
ã¯ãäž¡æ¹ã®ããŒãžã§ã³ã§æ©èœãã眮æãšããŠäœ¿çšã§ããŸããexec
èªäœãexec_
ããŠé¢æ°exec_
宣èšããããšãã§ããŸãã
exec_ = lambda s, *a: eval(compile(s, '<string>', 'exec'), *a)
- python C APIã®äžã«æžãããCã¢ãžã¥ãŒã«ãããå Žåãããã«èªåèªèº«ãæã€ããšãã§ããŸãã çŸæç¹ã§ã¯ãããã§åœ¹ç«ã€ããŒã«ã®ååšãèªèããŠããŸããã ãã®æ©äŒãå©çšããŠãã¢ãžã¥ãŒã«ã®èšè¿°æ¹æ³ãå€æŽãã cffiãŸãã¯ctypesã䜿çšããŠãã¹ãŠãæžãæããŸãã ããªããnumpyã®ãããªãã®ãæã£ãŠããããã«ãã®ãªãã·ã§ã³ãèæ ®ããªãå Žåãããªãã®ããã«æ®ã£ãŠããå¯äžã®ãã®ã¯è¬èã«çã¿ãåãå ¥ããããšã§ãã Cããªããã»ããµã®äžã«åä»ãªãã®ãæžããŠã移æ€ã容æã«ããããšãã§ããŸãã
- ããŒã«ã«ãã¹ãã«ã¯toxã䜿çšããŸãã å¿ èŠãªãã¹ãŠã®ããŒãžã§ã³ã®pythonã§äžåºŠã«ãã¹ããå®è¡ã§ããããšã¯ãå€ãã®åé¡ãåé¿ããã®ã«åœ¹ç«ã€éåžžã«åªããæ©èœã§ãã
ãããã«
çŸåšã2.xããã³3.xã®åäžã®ã³ãŒããå¯èœã§ãã , , , API . , , 2.5, 3.0-3.2, .