Last active
August 29, 2015 13:57
-
-
Save yuitest/9698922 to your computer and use it in GitHub Desktop.
昔々あるところに
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from __future__ import unicode_literals, print_function | |
import contextlib | |
def main(): | |
yama = Place() | |
kawa = Kawa(None, None, None, None, SpecialMomo(), None, None) | |
ojiisan = Human() | |
obaasan = Human() | |
ojiisan.love(obaasan) | |
obaasan.love(ojiisan) | |
with ojiisan.go(yama), obaasan.go(kawa): | |
while obaasan.hear(): | |
something = kawa.flow() | |
if something is not None: | |
obaasan.take(something) | |
momo = obaasan.show() | |
try: | |
ojiisan.cut(momo) | |
except Exception as e: | |
leftpart_momo, momotaro, rightpart_momo = e.inside | |
ojiisan.love(momotaro) | |
obaasan.love(momotaro) | |
people = [Human(), Human(), Human(), Human(), ojiisan, obaasan, momotaro] | |
oni_team = [Oni(), Oni(), Oni(), Oni()] | |
for i, oni in enumerate(oni_team): | |
oni.deprive(people[i]) | |
obaasan.give(momotaro, Kibidango(), Kibidango(), Kibidango()) | |
ojiisan.give(momotaro, Nobori()) | |
inu = Dog() | |
inu.momo_request(momotaro) | |
kiji = Kiji() | |
kiji.momo_request(momotaro) | |
saru = Monkey() | |
saru.momo_request(momotaro) | |
Onigashima = Place() | |
with momotaro.go(Onigashima), inu.go(Onigashima), \ | |
kiji.go(Onigashima), saru.go(Onigashima): | |
inu.kill(Oni()) | |
kiji.kill(Oni()) | |
saru.kill(Oni()) | |
for oni in oni_team: | |
momotaro.kill(oni) | |
if len(list(isinstance(thing, Treasure) for thing in momotaro.bring)) > 3: | |
Sound('medetashi') | |
class Food(object): | |
pass | |
class Nobori(object): | |
pass | |
class Kibidango(Food): | |
pass | |
class Momo(Food): | |
pass | |
class SpecialMomo(Momo): | |
def split(self): | |
e = Exception() | |
e.inside = (HalfMomo(), Human(), HalfMomo()) | |
raise e | |
class HalfMomo(Momo): | |
pass | |
class Treasure(object): | |
pass | |
class Sound(object): | |
def __init__(self, hearing): | |
pass | |
class Place(object): | |
pass | |
class Kawa(Place): | |
def __init__(self, *args): | |
self._upstream = list(args) | |
def sound(self): | |
if any(something is not None for something in self._upstream): | |
return Sound('donburako') | |
else: | |
return None | |
def flow(self): | |
return self._upstream.pop(0) | |
class Being(object): | |
def __init__(self): | |
self.bring = [] | |
def die(self): | |
return None | |
def take(self, something): | |
self.bring.append(something) | |
class Animal(Being): | |
def kill(self, target): | |
target.die() | |
def belong(self, target): | |
target.take(self) | |
def momo_request(self, target): | |
thing = target.show() | |
if isinstance(thing, Kibidango): | |
self.belong(target) | |
return target.give(self, thing) | |
@contextlib.contextmanager | |
def go(self, place): | |
self.place = place | |
yield self | |
class Bird(Animal): | |
pass | |
class Kiji(Bird): | |
pass | |
class Dog(Animal): | |
pass | |
class Monkey(Animal): | |
pass | |
class Humanoid(Animal): | |
def give(self, target, *things): | |
if things: | |
for thing in things: | |
target.take(thing) | |
else: | |
target.take(self.bring.show()) | |
def deprive(self, target): | |
while target.bring: | |
target.give(self, target.bring.pop(0)) | |
class Oni(Humanoid): | |
def deprive(self, target): | |
target.give(self, Treasure()) | |
class Human(Humanoid): | |
def love(self, target=None): | |
pass | |
def grow(self): | |
pass | |
def kill(self, target): | |
super(Human, self).kill(target) | |
self.deprive(target) | |
def cut(self, something): | |
if hasattr(something, 'split'): | |
something.split() | |
def show(self): | |
return self.bring.pop(0) | |
def hear(self, target=None): | |
if target is None: | |
target = self.place | |
if target.sound() is not None: | |
return True | |
return False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment