From 1b7d261835dd27e686067a2e95ac99ceb64a36dd Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 15 Aug 2013 14:31:40 +0000 Subject: add Primitive for the 'plus' block --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 394b5eb..3aab25f 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -615,7 +615,6 @@ tasetshade :shade \n') colors=["#FF00FF", "#A000A0"], help_string=_('Palette of numeric operators')) - primitive_dictionary['plus'] = self._prim_plus palette.add_block('plus2', style='number-style', label='+', @@ -624,8 +623,10 @@ tasetshade :shade \n') prim_name='plus', logo_command='sum', help_string=_('adds two alphanumeric inputs')) - self.tw.lc.def_prim( - 'plus', 2, lambda self, x, y: primitive_dictionary['plus'](x, y)) + self.tw.lc.def_prim('plus', 2, + # TODO re-enable use with lists + Primitive(Primitive.plus, slot_wrappers={ + (0, 2): Primitive(Primitive.convert_for_plus)})) palette.add_block('minus2', style='number-style-porch', diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index f7794d8..c2a4cf6 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -361,6 +361,12 @@ class Primitive(object): # standard operators elif self.func.__name__ in Primitive.STANDARD_OPERATORS: op = Primitive.STANDARD_OPERATORS[self.func.__name__] + # BEGIN hack for 'plus': unpack tuples + if (self == Primitive.plus and len(new_arg_asts) == 1 and + isinstance(new_arg_asts[0], (list, tuple)) and + len(new_arg_asts[0]) == 2): + new_arg_asts = new_arg_asts[0] + # END hack for 'plus' if len(new_arg_asts) == 1: if isinstance(op, tuple): op = op[0] @@ -383,7 +389,8 @@ class Primitive(object): len(new_arg_asts))) # type conversion - elif self in (Primitive.convert_for_cmp, Primitive.convert_to_number): + elif self in (Primitive.convert_for_cmp, Primitive.convert_to_number, + Primitive.convert_for_plus): return self.func(*new_arg_asts, **new_kwarg_asts) # identity @@ -580,9 +587,7 @@ class Primitive(object): convert_to_ast = False def _get_value_and_ast(val): - val_ast = None if isinstance(val, ast.AST): - convert_to_ast = True val_ast = val val = None if isinstance(val_ast, ast.Num): @@ -594,7 +599,9 @@ class Primitive(object): elif (isinstance(val_ast, ast.Name) and val_ast.id.startswith("CONSTANTS[")): val = eval(val_ast.id) - return (val, val_ast, convert_to_ast) + return (val, val_ast, True) + else: + return (val, None, False) (value1, value1_ast, convert_to_ast) = _get_value_and_ast(value1) (value2, value2_ast, convert_to_ast) = _get_value_and_ast(value2) @@ -602,10 +609,9 @@ class Primitive(object): """ Return strings as they are, convert Colors to an integer and then to a string, and convert everything else directly to a string. """ - if isinstance(val, basestring): - val_conv = val - val_conv_ast = val_ast - else: + val_conv = val + val_conv_ast = val_ast + if not isinstance(val, basestring): if isinstance(val, Color): conv_prim = Primitive(str, slot_wrappers={ 0: Primitive(int)}) @@ -620,10 +626,9 @@ class Primitive(object): def _to_number(val, val_ast): """ Return numbers as they are, and convert everything else to an integer. """ - if isinstance(val, (float, int, long)): - val_conv = val - val_conv_ast = val_ast - else: + val_conv = val + val_conv_ast = val_ast + if not isinstance(val, (float, int, long)): conv_prim = Primitive(int) if not convert_to_ast: val_conv = conv_prim(val) @@ -637,8 +642,8 @@ class Primitive(object): (value2_conv, value2_conv_ast) = _to_string(value2, value2_ast) else: # convert both to numbers - (value1_conv, value1_conv_ast) = _to_number(value1) - (value2_conv, value2_conv_ast) = _to_number(value2) + (value1_conv, value1_conv_ast) = _to_number(value1, value1_ast) + (value2_conv, value2_conv_ast) = _to_number(value2, value2_ast) if convert_to_ast: return (value1_conv_ast, value2_conv_ast) @@ -648,7 +653,11 @@ class Primitive(object): @staticmethod def plus(arg1, arg2=None): """ If only one argument is given, prefix it with '+'. If two - arguments are given, add the second to the first. """ + arguments are given, add the second to the first. If the first + argument is a tuple of length 2 and the second is None, use the + values in the tuple as arg1 and arg2. """ + if isinstance(arg1, (list, tuple)) and len(arg1) == 2 and arg2 is None: + (arg1, arg2) = arg1 if arg2 is None: return + arg1 else: -- cgit v0.9.1