èšè¿°ãããã¯ã©ã¹ããã€ã³ã¹ã¿ã³ã¹ã1ã€ã ãäœæããããã1åã ã䜿çšããããã«ç Žæ£ãããã³ã«ã次ã®ããšãèããå¿ èŠããããŸãã ç°¡åã«ããã£ãšç°¡åã«ã§ããŸãïŒã
Pythonèšèªã®äž»èŠãªéçºè ã®1人ã§ããJack Didrichã«ããã¬ããŒãã®ç¿»èš³ã ã¬ããŒã㯠2012幎3æ9æ¥ã«PyCon USã«ã³ãã¡ã¬ã³ã¹ã§é ä¿¡ãããŸããã
çããã¯ãããããäœåºŠãZen Pythonãèªãã ããšããããŸãã 以äžã«ããã€ãã®ãã€ã³ãã瀺ããŸãã
- åçŽãªãã®ã¯è€éãªãã®ãããåªããŠãã
- ãã¹ãã£ã³ã°ãããã©ããã®æ¹ãè¯ã
- èªã¿ããããéèŠã§ã
- ããã°ã©ã ã説æããã®ãé£ããå Žåãããã¯æªãã§ã
- ããã°ã©ã ã®èª¬æãç°¡åãªå Žåã¯ãããããè¯ãã§ããã
ãã®ããã¹ãã¯ãã£ã ããŒã¿ãŒãºã«ãã£ãŠæžãããŸããã 圌ã¯ããªããšç§ããè³¢ãã ãœãŒãã¢ã«ãŽãªãºã ã®ååãç¥ã£ãŠãã人ã¯äœäººããŸããïŒ ãã¡ããZen Pythonãæžãã人ã§ãã ãããŠããã¹ãŠã®ãã€ã³ãã¯ããèªã¿ãŸãããé£ããããšãããªãã§ãã ããã ãŸãã«ãããè¡ããããããè°è«ããããã®ã§ãã
ã ããããŸã第äžã«ããããé£ãââãããªãã§ãã ããã ã¯ã©ã¹ã¯éåžžã«è€éã§ããããéåžžã«è€éãªå ŽåããããŸãã ããããç§ãã¡ã¯ãããããã«å°é£ã«ããŠããŸãã ãããã£ãŠããã®ã¬ããŒãã§ã¯ãå°ãã®ã³ãŒããèªã¿ãééã£ãæ¹åã«é²ãã§ããããšã«æ°ä»ãæ¹æ³ãšãæ»ãæ¹æ³ãåŠã³ãŸãã
ç§ã®è·å Žã§ã¯ãååã«æ¬¡ã®ããã«äŒããŠããŸãããç§ã¯ã³ãŒããå«ãã§ãã補åã§ã¯ã§ããã ãå°ãããããã®ã§ãããç§ãã¡ã¯ã³ãŒãã§ã¯ãªãæ©èœã販売ããŠããŸãã ç§ãã¡ã®é¡§å®¢ã¯ã³ãŒãã®ããã§ã¯ãªããå¹ åºãæ©èœã®ããã§ãã ã³ãŒããåé€ããããã³ã«ãããã¯è¯ãããšã§ãã ç§ãã¡ã¯4人ã§ãæšå¹Žã¯è£œåã®è¡æ°ã®ã«ãŠã³ããåæ¢ããŸããããæ°ããæ©èœãå°å ¥ãç¶ããŠããŸãã
ã¯ã©ã¹
ãã®ã¬ããŒããããæåã«ãã®ã³ãŒããèŠããŠããå¿ èŠããããŸãã ããã¯ãèªç¶çã§èŠãããæ倧ã®ã¯ã©ã¹ä¹±çšã§ãã
class Greeting(object): def __init__(self, word='Hello'): self.word = word def greet(self, name): return '%s, %s!' % (self.word, name) >>> greeting = Greeting('Hola') >>> greeting.greet('Jorge') Hola, Jorge!
ã¯ã©ã¹ã®ããã«èŠããŸãããããã¯ã¯ã©ã¹ã§ã¯ãããŸããã ååã¯åè©ãããããã€ãã§ãã åŒæ°ãåãã__ init__ã«ä¿åããŸãã ã¯ããã¯ã©ã¹ã®ããã«èŠããŸãã 圌ã«ã¯ããªããžã§ã¯ãã®ç¶æ ãèªã¿åããã¯ã©ã¹ã®ããã«ä»ã®ããšãè¡ãã¡ãœããããããŸãã 以äžã«ããã®ã¯ã©ã¹ã®äœ¿çšæ¹æ³ã瀺ããŸããGreetingsã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŠããããã®Greetingsã䜿çšããŠä»ã®ããšãè¡ããŸãã
ããããããã¯ã¯ã©ã¹ã§ã¯ãªãããã¯ã©ã¹ã§ãã£ãŠã¯ãªããŸããã ãã®å åã¯ãã¡ãœããã2ã€ãããªãããšã§ããããã®ãã¡ã®1ã€ã¯åæå__init__ã§ãã ãããèŠããã³ã«ãããããã1ã€ã®é¢æ°ãå¿ èŠãªã ãã ããšèããŠãã ããã
èšè¿°ãããã¯ã©ã¹ããã€ã³ã¹ã¿ã³ã¹ã1ã€ã ãäœæããããã1åã ã䜿çšããããã«ç Žæ£ãããã³ã«ã次ã®ããšãèããå¿ èŠããããŸãã ç°¡åã«ããã£ãšç°¡åã«ã§ããŸãïŒã
def greet(name): ob = Greeting('') print ob.greet(name) return
ãã®é¢æ°ã¯4è¡ã®ã³ãŒãã§æ§æãããŠããŸãã ãããŠã次ã®2è¡ã§åãããšãã§ããŸãã
def greet(greeting, name): return '%s, %s!' % (greeting, name) import functools greet = functools.partial(greet, '') greet('')
åžžã«åãæåã®åŒæ°ã§é¢æ°ãåŒã³åºãå Žåãæšæºã©ã€ãã©ãªã«ã¯ããŒã«ããããŸãïŒ functools.partialã äžèšã®ã³ãŒããèŠãŠãã ããïŒåŒæ°ãè¿œå ãããšãçµæãè€æ°ååŒã³åºãããšãã§ããŸãã
ITã®åŠäœãæã£ãŠãã人ãã©ãã ãããã®ãããããŸããã ç§ã¯æ¬¡ã®ãããªæŠå¿µãæããŸãã
-æš©åã®åé¢
-ã³ãŒãæ¥ç¶ã®åæž
-ã«ãã»ã«å
-販売ã®åé¢
åæ¥ããŠä»¥æ¥ããããã®çšèªã15幎é䜿çšããŠããŸããã ãããã®èšèãèããŠãããªãã¯ã ãŸãããŠããŸãã ãããã®çšèªèªäœã¯å¿ èŠãããŸããã ãããã䜿çšãããå Žåã人ã ã¯äŒè©±ã劚害ããã ãã®å®å šã«ç°ãªããã®ãæå³ããŸãã
äŸïŒãã³ãã¿ãŒã³...
ããªãã®å€ãã¯ãæ¥åžžæ¥åã§ãµãŒãããŒãã£ã®ã©ã€ãã©ãªã䜿çšããŠããŸãã ä»ã®äººã®ã³ãŒãã䜿çšããå¿ èŠããããã³ã«ãæåã«ããããšã¯ãããèªãããšã§ãã çµå±ã®ãšãããããã«ã©ã®ãããªå質ããããããã¹ããªã©ããããã©ããã¯äžæã§ãã é»æºãå ¥ããåã«ã³ãŒãã確èªããå¿ èŠããããŸãã ã³ãŒããèªãã®ãé£ããå ŽåããããŸãã
ãµãŒãããŒãã£ã®APIã©ã€ãã©ãªããããShaurMailãšåŒã³ãŸãããã1ã€ã®ããã±ãŒãžã22ã®ã¢ãžã¥ãŒã«ã20ã®ã¯ã©ã¹ã660è¡ã®ã³ãŒããå«ãŸããŠããŸãã 補åã«å«ããåã«ãããããã¹ãŠãèªãå¿ èŠããããŸããã ããããããã¯åœŒãã®å ¬åŒAPIã§ãã£ããããããã䜿çšããŸãã ã APIã®æŽæ°ãæ¥ããã³ã«ãå·®åã確èªããå¿ èŠããããŸãããäœãå€æŽãããã®ãããããªãã£ãããã§ãã ããããéä¿¡ããŸãã-ã¢ããããŒãã«è¡šç€ºãããŸãããïŒ
660è¡ã®ã³ãŒãã20ã¯ã©ã¹ã¯ãããã°ã©ã ãé»åã¡ãŒã«ã¢ãã¬ã¹ã®ãªã¹ããæçŽã®ããã¹ããæäŸããé ä¿¡ãããªãã£ãæçŽãšè³Œèªè§£é€ãã人ãèŠã€ããã ãã§ããå Žåãå°ãå€ããªããŸãã
ã¯ã©ã¹èåŸ ãšã¯äœã§ããïŒ å€ãã®å Žåã人ã ã¯å°æ¥ãäœããå¿ èŠã«ãªããšèããŸãã å¿ èŠãªãšãã«ãã¹ãŠãæžããŸãã ShaurMailã©ã€ãã©ãªã«ã¯ShaurHashã¢ãžã¥ãŒã«ãããã次ã®2è¡ã®ã³ãŒããå«ãŸããŸãã
class ShaurHash(dict): pass
誰ããèŸæžã®ã¢ããªã³ãåŸã§å¿ èŠã«ãªããšæ±ºããŸããã å¿ èŠã§ã¯ãããŸããã§ããããã³ãŒãã®ã©ãã«ã§ãæåã®è¡ããããŸããã
my_hash = ShaurMail.ShaurHash.ShaurHash(id='cat') d = dict(id='cat') d = {'id': 'cat'}
ã³ãŒãã®2è¡ç®ãš3è¡ç®-誰ã説æããå¿ èŠã¯ãããŸããã ããããããã§ã¯ããã®ãã³ãã©ãShaurMail-ShaurHash-ShaurHashããã©ãã§ãç¹°ãè¿ãããŸããã ãShaurããšããåèªã®3åã®ç¹°ãè¿ãã¯ãéå°ã®å¥ã®å åã§ãã ç¹°ãè¿ããããã¹ãŠã®å®³ãŸã§ã ãŠãŒã¶ãŒã«ãShaurãã3åæžãããã«åŒ·å¶ããããšã§ãŠãŒã¶ãŒãå°ãããŸãã ïŒããã¯å®éã®äŒç€Ÿåã§ã¯ãªããæ¶ç©ºã®ãã®ã§ããïŒ
ãããã圌ãã¯ãã®ç·ã解éãã圌ãäœãããŠããããç¥ã£ãŠãã人ãéã£ãã APIã®2çªç®ã®ããŒãžã§ã³ã¯æ¬¡ã®ãšããã§ãã
class API: def __init__(self, key): self.header = dict(apikey=key) def call(self, method, params): request = urllib2.Request( self.url + method[0] + '/' + method[1], urllib.urlencode(params), self.header ) try: response = json.loads(urllib2.urlopen(request).read()) return response except urllib2.HTTPError as error: return dict(Error=str(error))
ãã®ã³ãŒãã«ã¯660è¡ããã®ã³ãŒãã«ã¯15è¡ããããŸãããã®ã³ãŒããè¡ãããšã¯ãã¹ãŠãæšæºã©ã€ãã©ãªã®ã¡ãœããã䜿çšããŠããŸãã æ°ç§ã§ç°¡åã«å šäœãèªãããšãã§ããäœãããŠããã®ãããã«ç解ã§ããŸãã ã¡ãªã¿ã«ã20è¡ã®ãã¹ãããããŸããã æžãæ¹ã¯æ¬¡ã®ãšããã§ãã 圌ããAPIãæŽæ°ãããšããç§ã¯æåéãæ°ç§ã§å€æŽãèªãããšãã§ããŸããã
ããããããã§åé¡ã«æ°ã¥ãããšãã§ããŸãã ã¯ã©ã¹ã«ã¯2ã€ã®ã¡ãœãããããããã®ãã¡ã®1ã€ã¯__init__ã§ãã èè ã¯ãããé ããŸããã§ããã 2çªç®ã®æ¹æ³ã¯ãåŒã³åºãããåŒã³åºããã§ãã ãã®APIã®äœ¿çšæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
ShaurMail.API(key=' ').call(('mailing', 'statistics'), {'id': 1})
è¡ãé·ãããããšã€ãªã¢ã¹ãäœæããŠäœåºŠãåŒã³åºããŸãã
ShaurMail.request = ShaurMail.API(key=' ').call ShaurMail.request(('mailing', 'statistics'), {'id': 1})
ãã®ã¯ã©ã¹ãé¢æ°ãšããŠäœ¿çšããŠããããšã«æ³šæããŠãã ããã ãããããã¯ãã§ãã ããã衚瀺ãããå Žåãããã§ã¯ã¯ã©ã¹ãå¿ èŠãªãããšãç¥ã£ãŠãã ããã ããã§ãç§ã¯åœŒãã«APIã®3çªç®ã®ããŒãžã§ã³ãéä¿¡ããŸããã
ShaurMail_API = url = 'https://api.shaurmail.com/%s/%s' ShaurMail_API_KEY = ' ' def request(noun, verb, **params): headers = {'apikey': ShaurMail_API_KEY} request = urllib2.Request(ShaurMail_API % (noun, verb), urllib.urlencode(params), headers) return json.loads(urllib2.urlopen(request).read())
䜿çšããã¢ãžã¥ãŒã«ã«ãã¡ã€ã«ãæ¿å ¥ããããããããžã§ã¯ãã«ãã¡ã€ã«ãäœæããŸããã 15æååAPIãå®è¡ãããã¹ãŠã®åŠçãšã660æååAPIãå®è¡ãããã¹ãŠã®åŠçãå®è¡ããŸãã
ãããç§ãã¡ãå§ããå Žæã§ãããç§ãã¡ãæ¥ãç®çã§ãïŒ
- 1ããã±ãŒãž+ 20ã¢ãžã¥ãŒã«=> 1ã¢ãžã¥ãŒã«
- 20ã¯ã©ã¹=> 1ã¯ã©ã¹=> 0ã¯ã©ã¹
- 130ã¡ãœãã=> 2ã¡ãœãã=> 1é¢æ°
- ã³ãŒã660è¡=> 15è¡=> 5è¡
䜿ãããããæžãããããäœãèµ·ãã£ãŠããã®ããç解ããå¿ èŠã¯ãããŸããã
æšæºã©ã€ãã©ãª
Javaèšèªããæ¥ã人ã¯èª°ã§ããåé¡ã«åå空éãå¿ èŠã§ãããšèãããããããŸããã ããã¯çå®ã§ã¯ãããŸããã ååã®äžèŽãé²ãããã«å¿ èŠã§ãã ã¹ããŒã¹ã®æ·±ãéå±€ãããå Žåã誰ã«ãäœãæäŸããŸããã ShaurMail.ShaurHash.ShaurHashã¯ã人ã ãèŠããŠæžãå¿ èŠãããåãªãè¿œå ã®åèªã§ãã
Pythonæšæºã©ã€ãã©ãªã§ã¯ãã¢ãžã¥ãŒã«ã®ååãèŠããŠããããããã¥ã¡ã³ããåç §ããå¿ èŠããããããåå空éã¯éåžžã«æµ ããªã£ãŠããŸãã ãã§ãŒã³ãæ€çŽ¢ããããã±ãŒãžããã®ããã±ãŒãžã次ã®ããã±ãŒãžãããã³ãã®äžã®ã¢ãžã¥ãŒã«ã®ååãææ¡ããå¿ èŠãããå Žåã¯ãäœãè¯ãããšã¯ãããŸããã ã¢ãžã¥ãŒã«ã®ååãç¥ãå¿ èŠãããã ãã§ãã
æ®å¿µãªããšã«ãããã«ã³ãŒãã®äŸã瀺ããŸããåã眪ãããã«ãããŸãã
services.crawler.crawlerexceptions.ArticleNotFoundException
2è¡ã®ã¢ãžã¥ãŒã«ãäŸå€ã¯ã©ã¹ãããã³ããã¹ããããããã±ãŒãžã ãã®äŸå€ã䜿çšããã«ã¯ããã¯ããŒã©ãŒãã2åããäŸå€ããšããåèªã®2åèšè¿°ããå¿ èŠããããŸãã ArticleNotFoundExceptionãšããååã¯ç¹°ãè¿ãããŸãã ããããªãã§ãã ããã äŸå€ãåŒã³åºãå Žåã¯ãEmptyBeerãBeerErrorãBeerNotFoundã«ããŸãããBeerNotFoundErrorã¯ãã§ã«ãããããããŸãã
æšæºã©ã€ãã©ãªã®äŸå€ãåçŽã«äœ¿çšã§ããŸãã ãããã¯èª°ã«ã§ãæããã§ãã ç¹å®ã®ç¶æ ããã£ããããå¿ èŠããªãéããLookupErrorã¯åé¡ãããŸããã ããªããã¡ãŒã«ã§ããããšãåãåã£ãå Žåãããªãã¯ããããŸã èªãŸãªããã°ãªããªãã®ã§ãäŸå€ãäœãšåŒã°ãããã¯é¢ä¿ãããŸããã
ããã«ãã³ãŒãå ã®äŸå€ã¯éåžžãraiseããã³exceptã®åŸã«ããã誰ããããããäŸå€ã§ããããšãããã«ç解ããŸãã ãããã£ãŠãååã«ãäŸå€ããè¿œå ããå¿ èŠã¯ãããŸããã
Pythonæšæºã©ã€ãã©ãªã«ãéã³ãéšåããããŸãããã³ãŒãç·šæã®éåžžã«è¯ãäŸã§ãã
- 200,000è¡ã®ã³ãŒã
- 200ã®æäžäœã¢ãžã¥ãŒã«
- ããã±ãŒãžããšã«å¹³å10ãã¡ã€ã«
- 165äŸå€
ããã±ãŒãžå ã®10åã®ãã¡ã€ã«ã¯å€§éã§ãããããã¯ã©ã€ãã©ãªã«è¿œå ãããããã€ãã®ãµãŒãããŒãã£ãããžã§ã¯ããåå ã§ã2ã€ã®ãã¡ã€ã«ã®ã¿ã®ããã±ãŒãžããããŸããã æ°ããäŸå€ãäœæããããšã«ããå Žåãæšæºã©ã€ãã©ãªã¯1200è¡ã®ã³ãŒãã«å¯ŸããŠ1ã€ã®äŸå€ãèŠãããããããèããŠãã ããã
ç§ã¯ååãšããŠã¯ã©ã¹ã«å察ã§ã¯ãããŸããã ã¯ã©ã¹ãå¿ èŠã§ã-å€åããããŒã¿ãšé¢é£ããé¢æ°ããããããããšãã ãã ããããã¯æ¥åžžæ¥åã§ã¯ãŸãã«ããçºçããŸããã æšæºã©ã€ãã©ãªãå®æçã«äœ¿çšããå¿ èŠãããããã§ã«é©åãªã¯ã©ã¹ããããŸãã ãããã¯ãã§ã«ããªãã®ããã«æžãããŠããŸãã
Pythonã©ã€ãã©ãªã®å¯äžã®äŸå€ã¯heapqã¢ãžã¥ãŒã«ã§ãã ããŒããã¥ãŒãããŒããã¥ãŒã¯ãåžžã«ãœãŒããããé åã§ãã heapqã¢ãžã¥ãŒã«ã«ã¯å€æ°ã®ã¡ãœãããããããããã¯ãã¹ãŠåãããŒãã§åäœããŸãã æåã®åŒæ°ã¯åžžã«åããŸãŸã§ããã€ãŸããã¯ã©ã¹ã¯æ¬åœã«ãããé Œã¿ãŸãã
heapify(data) pushleft(data, item) popleft(data) pushright(data, item) popright(data)
ãªã©
heapqã䜿çšããå¿ èŠããããã³ã«ããã®ã¯ã©ã¹ã®å®è£ ãããŒã«ãããããååŸããŸãã
class Heap(object): def __init__(self, data=None, key=lambda x: None): self.heap = data or [] heapq.heapify(self.heap) self.key = key def pushleft(self, item): if self.key: item = (self.key(item), item) heapq.pushleft(self.heap, item) def popleft(self): return heapq.popleft(self.heap)[1]
ã¯ã©ã¹ã¯éèã®ããã«æé·ãã
Pythonã®OAuthã¹ããŒã¿ã¹ã¯éèŠã§ã¯ãããŸããã ç¹°ãè¿ããŸããããµãŒãããŒãã£ã®ã©ã€ãã©ãªãããããããžã§ã¯ãã§äœ¿çšããåã«ãããããèªãå¿ èŠããããŸãã
Googleã®URLã¬ãã¥ãŒãµãŒã䜿çšããããšããŸãããURLãååŸããŠççž®ããå¿ èŠããããŸããã Googleã«ã¯ã10,000è¡ã®ã³ãŒããå«ããããžã§ã¯ãããããŸãã 115ã¢ãžã¥ãŒã«ãš207ã¯ã©ã¹ã Google +ã§re責ãæžããŸãããã ãããèŠã人ã¯ã»ãšãã©ããŸããã§ãããGuidoïŒVan Rossum-ãããPerãïŒã¯ã³ã¡ã³ãããŸããïŒãç§ã¯Googleã®APIã³ãŒãã«è²¬ä»»ãè² ããŸããã10,000è¡ã®ã³ãŒã- -ShaurMaleã®ãããªãã®ã ããšãã°ãããã«ã¯ãä»ã®ã¯ã©ã¹ãç¶æ¿ããFlowã¯ã©ã¹ããããŸãã
class Flow(object): pass class Storage(object): def put(self, data): _abstract() def get(self): _abstract() def _abstract(): raise NotImplementedError
圌ã¯ç©ºã§ãã ãããã圌ã«ã¯ç¬èªã®ã¢ãžã¥ãŒã«ãããããããç¶æ¿ããã¯ã©ã¹ãèªã¿åããã³ã«ãå€åºãããã®ãã¡ã€ã«ããã§ãã¯ãããã®ã¯ã©ã¹ã空ã§ããããšã確èªããå¿ èŠããããŸãã 誰ããæªæ¥ãèŠã€ãããä»ãã3è¡ã®ã³ãŒããæžãã®ã§ãå°æ¥ãããã®3è¡ãå€ãããªãããã«ããããšæ±ºããŸããããããŠã圌ã®ã©ã€ãã©ãªãèªããã¹ãŠã®äººããæéããããŸããã Storageã¯ã©ã¹ããããã»ãšãã©äœãããŸããã æšæºã®äŸå€ã䜿çšããŠãšã©ãŒãæ£ããåŠçããŸããããšã€ãªã¢ã¹ã䜿çšãããããã³ãŒããèªãã§åäœã確èªããå¿ èŠããããŸãã
OAuth2ãå®è£ ããã®ã«1é±éããããŸããã 1äžè¡ã®ã³ãŒããèªãã®ã«æ°æ¥ããããŸããããã®åŸãä»ã®ã©ã€ãã©ãªãæ¢ãå§ããŸããã python-oauth2ãèŠã€ãããŸããã ããã¯python-oauthã®2çªç®ã®ããŒãžã§ã³ã§ãããOAuth2ã®æäœæ¹æ³ãå®éã«ã¯ããããŸããã ãã ãããã®ã©ã€ãã©ãªã¯Googleãããããã«åªããŠããŸãïŒ540è¡ãš15ã¯ã©ã¹ã®ã¿ã§ãã
ããã«ç°¡åã«æžãçŽããŠã python-foauth2ãšåŒã³ãŸãã ã 135è¡ã®ã³ãŒããš3ã€ã®ã¯ã©ã¹ããããŠããã¯ãŸã ãããããããŸããç§ã¯ãããååã«ãªãã¡ã¯ã¿ãªã³ã°ããŸããã§ããã ããã3ã€ã®ã¯ã©ã¹ã®1ã€ã次ã«ç€ºããŸãã
class Error(Exception): pass
æ¥ïŒ
人ç
æåŸã®äŸã Conwayã®Lifeã²ãŒã 㯠ãååãããããªããŠãèŠãããšãã人ã¯ãã¹ãŠããŸãã åžæŸæš¡æ§ã®ãã£ãŒã«ãããããããªããèããååãã¯åã»ã«ã«é£æ¥ããŠããããããã«å¿ããŠãããã¯çããŠãããæ»ãã§ããã§ãããã ãããŠãã°ã©ã€ããŒã®ãããªçŸããå®å®ãããã¿ãŒã³ãåŸãããŸãã现èã¯åã§çãè¿ããåŸãã§æ»ã«ãã°ã©ã€ããŒã¯ãã£ãŒã«ãã暪åã£ãŠé£ãã§ããããã§ãã
ã²ãŒã ã®ãã©ã€ããã¯éåžžã«ã·ã³ãã«ã§ãããã£ãŒã«ããšããã€ãã®ã«ãŒã«ã§ãã ã€ã³ã¿ãã¥ãŒã§ãã®ã¿ã¹ã¯ãäŸé ŒããŸãããããè¡ãæ¹æ³ãããããªãå Žåã¯ãäœã話ãããšãã§ããŸããã å€ãã®äººã¯ããã«ãã现èã¯åè©ã§ãã ã¯ã©ã¹ãå¿ èŠã§ããããã®ã¯ã©ã¹ã®ããããã£ã¯äœã§ããïŒ å ŽæãçããŠãããã©ããã次ã®ã¿ãŒã³ã®ç¶æ ããã¹ãŠïŒ ãŸã é£äººãããŸãã 次ã«ããã£ãŒã«ãã®èª¬æãéå§ããŸãã ãã£ãŒã«ãã¯å€æ°ã®ã»ã«ã§ãããããã°ãªããã§ãããå éšã®ã»ã«ãã«ãŠã³ããããã«ãŠã³ããã¡ãœããããããŸãã
class Cell(object): def __init__(self, x, y, alive=True): self.x = x self.y = y self.alive = alive self.next = None def neigbors(self): for i, j in itertools.product(range(-1, 2), repeat=2): if (i, j) != (0, 0): yield (self.x + i, self.y + j) class Board(object): def __init__(self): self.cells = {} # { (x, y): Cell() } def advance(self): for (x, y), cell in self.cells.items(): alive_neighbors = len(cell.neighbors) cell.next = (alive_neighbors == 3 or (alive_neighbors == 2 and cell.alive))
ãã®æç¹ã§ããstopããšèšãå¿ èŠããããŸããFieldã¯ã©ã¹ãããããã®äžã«__init__ãšãmake a moveãã®2ã€ã®ã¡ãœããããããŸãã ãã®äžã«1ã€ã®ããããã£ããããŸã-èŸæžã§ããããã¯ãèŸæžãæäœããå¿ èŠãããããšãæå³ããŸãã ãã€ã³ãã®è¿åãä¿åããå¿ èŠã¯ãªãããšã«æ³šæããŠãã ããããããã¯æ¢ã«èŸæžã«ãããŸãã çããŠãããã©ããã¯åãªãããŒã«å€ã§ãããããçããŠãã现èã®åº§æšã®ã¿ãä¿åããŸãã ãŸããèŸæžã«ã¯Trueã®ã¿ãæ ŒçŽãããŠãããããèŸæžã¯å¿ èŠãªããåã«åº§æšã®ã»ãããå¿ èŠã§ãã æåŸã«ãæ°ããç¶æ ã¯å¿ èŠãããŸãããç现èã®ãªã¹ããç°¡åã«åäœæã§ããŸãã
def neigbors(point): x, y = point for i, j in itertools.product(range(-1, 2), repeat=2): if any((i, j)): yield (x + i, y + j) def advance(board): newstate = set() recalc = board | set(itertools.chain(*map(neighbors, board))) for point in recalc: count = sum((neigh in board) for neigh in neighbors(point)) if count == 3 or (count == 2 and point in board): newstate.add(point) return newstate glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)]) for i in range(1000): glider = advance(glider) print glider
ã²ãŒã ã®éåžžã«ã·ã³ãã«ã§ç°¡æœãªå®è£ ãå€æããŸããã ããã§ã¯2ã€ã®ã¯ã©ã¹ã¯å¿ èŠãããŸããã äž-ã°ã©ã€ããŒã®åº§æšããã£ãŒã«ãã«æ¿å ¥ãããã°ã©ã€ããŒãé£è¡ããŸãã ããã ãã§ã ããã¯ãã²ãŒã ãã©ã€ããã®å®å šãªå®è£ ã§ãã
ãŸãšã
1. __init__ãå«ã2ã€ã®ã¡ãœãããæã€ã¯ã©ã¹ã衚瀺ãããå Žåãããã¯ã¯ã©ã¹ã§ã¯ãããŸããã
2.å¿ èŠã§ãªãå ŽåïŒããã³äžèŠãªå ŽåïŒãæ°ããäŸå€ãäœæããªãã§ãã ããã
3.ç°¡çŽ åããŸãã
翻蚳è ããïŒã³ã¡ã³ãã§ã¯ãå€ãã®äººããã®ã¬ããŒããPLOã®å®å šãªåŠå®ã ãšèªèããŠããããšãããããŸããã ããã¯ééãã§ãã çµæã®ãã€ã³ã1ã¯ããããã¯ã©ã¹ã§ã¯ãªãããšãæ確ã«ç€ºããŠããŸãã ã¯ã©ã¹ãå¿ èŠã§ãããã¬ããŒãã®æ¬è³ªã¯ãã¯ã©ã¹ãä¹±çšãããã¹ãã§ã¯ãªããšããããšã§ãã