From ee0474c2690ef221ddadbb8b556511f92b81bd27 Mon Sep 17 00:00:00 2001 From: blob8108 Date: Wed, 28 May 2014 20:50:08 +0100 Subject: GIGA-COMMIT: Blockext 0.2 draft version --- example.py | 157 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 74 insertions(+), 83 deletions(-) (limited to 'example.py') diff --git a/example.py b/example.py index 02c3896..0eec225 100644 --- a/example.py +++ b/example.py @@ -1,92 +1,83 @@ # coding=utf-8 +from __future__ import unicode_literals -import blockext -from blockext import * - +import time +from blockext import * -@predicate("not %b") -def not_(value): - return not value - -@command("say %s for %n secs", blocking=True) -def say_for_secs(text="Hello", duration=5): - import time - print(text) - time.sleep(duration) - - -@command("play note %n") -def play_note(note): - print("DING {note}".format(note=note)) - time.sleep(2) - -menu("pizza", ["tomato", "cheese", "hawaii", "nothing", - "spinach and cauliflower", "empty return value", - "cheese and tomato", "fancy", - "ü", - "/", - ]) - -@reporter("colour of %m.pizza flavour pizza") -def pizza_colour(pizza="tomato"): - return { - "tomato": "red", - "cheese": "yellow", - "cheese and tomato": "YELLOW", - "hawaii": "orange AND BLUE", - "spinach and cauliflower": "GREEN ü", - "empty return value": "", - "nothing": "", - "/": "SLASH", - u"ü": "unicode", - u"fancy": "❤☀☆☂", - }[pizza] - -@reporter("spaaace") -def getSpaaace(): - return "space space space space space!" - -@reporter("id %s") -def id(text): - """Tests strings can get passed from Snap! to Python and back.""" - print(text) - return text - -@command("set number to %n% units") -def percent(number=42): - print(number) - -foo = None - -@command("set foo to %s") -def set_foo(value=''): - global foo - foo = value - -@reporter("foo") -def get_foo(): - return foo - -@command("ü") -def x(): pass - -@problem -def my_problem(): - if time.time() % 8 > 4: - return "The Scratch Sensor board is not connected. Foo." - -@reset -def my_reset(): - print(""" - Reset! The red stop button has been clicked, - And now everything is how it was. - ... - (Poetry's not my strong point, you understand.) - """) +class Example: + def __init__(self): + self.foo = 0 + + def _problem(self): + if time.time() % 8 > 4: + return "The Scratch Sensor board is not connected. Foo." + + def _on_reset(self): + print(""" + Reset! The red stop button has been clicked, + And now everything is how it was. + ... + (Poetry's not my strong point, you understand.) + """) + + @predicate("not %b") + def not_(self, value): + return not value + + @command("say %s for %n secs", is_blocking=True) + def say_for_secs(self, text="Hello", duration=5): + print(text) + time.sleep(duration) + + @command("play note %n") + def play_note(self, note): + print("DING {note}".format(note=note)) + time.sleep(2) + + @reporter("colour of %m.pizza flavour pizza", defaults=["tomato"]) + def pizza_colour(self, pizza): + return { + "tomato": "red", + "cheese": "yellow", + "hawaii": "orange and blue", + }[pizza] + + @reporter("id %s") + def id(self, text): + """Tests strings can get passed from Snap! to Python and back.""" + print(text) + return text + + @command("set number to %n% units") + def percent(self, number=42): + print(number) + + @command("set foo to %s") + def set_foo(self, value=''): + self.foo = value + + @reporter("foo") + def get_foo(self): + return self.foo + + @command("ü") + def x(self): + pass + +descriptor = Descriptor( + name = "Fancy Spaceship", + port = 1234, + blocks = get_decorated_blocks_from_class(Example), + menus = dict( + pizza = ["tomato", "cheese", "hawaii"], + ), +) + +extension = Extension(Example, descriptor) if __name__ == "__main__": - blockext.run("Fancy Spaceship", "spaceship", 1234) + extension.run_forever(debug=True) -- cgit v1.3.1