Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-08-15 13:27:41 (GMT)
committer Marion <marion.zepf@gmail.com>2013-08-15 13:27:41 (GMT)
commitc67ecd0ed4c039a67880f6393cca71248d371034 (patch)
tree454472cebdc37a8ab2b6a7d01ad8b8af28987acf
parentb7ea629037af38054b92b11c39719fcf19b175b2 (diff)
add type conversion method for the 'plus' block
-rw-r--r--TurtleArt/taprimitive.py91
1 files changed, 82 insertions, 9 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py
index b92f2cc..f7794d8 100644
--- a/TurtleArt/taprimitive.py
+++ b/TurtleArt/taprimitive.py
@@ -573,6 +573,88 @@ class Primitive(object):
return return_val
@staticmethod
+ def convert_for_plus(value1, value2):
+ """ If at least one value is a string, convert both to a string.
+ Otherwise, convert both to a number. (Colors are converted to an
+ integer before they are converted to a string.) """
+ 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):
+ val = val_ast.n
+ elif isinstance(val_ast, ast.Str):
+ val = val_ast.s
+ elif isinstance(val_ast, ast.List):
+ val = val_ast.elts
+ elif (isinstance(val_ast, ast.Name) and
+ val_ast.id.startswith("CONSTANTS[")):
+ val = eval(val_ast.id)
+ return (val, val_ast, convert_to_ast)
+ (value1, value1_ast, convert_to_ast) = _get_value_and_ast(value1)
+ (value2, value2_ast, convert_to_ast) = _get_value_and_ast(value2)
+
+ def _to_string(val, val_ast):
+ """ 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:
+ if isinstance(val, Color):
+ conv_prim = Primitive(str, slot_wrappers={
+ 0: Primitive(int)})
+ else:
+ conv_prim = Primitive(str)
+ if not convert_to_ast:
+ val_conv = conv_prim(val)
+ else:
+ val_conv_ast = conv_prim.get_ast(val_ast)
+ return (val_conv, val_conv_ast)
+
+ 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:
+ conv_prim = Primitive(int)
+ if not convert_to_ast:
+ val_conv = conv_prim(val)
+ else:
+ val_conv_ast = conv_prim.get_ast(val_ast)
+ return (val_conv, val_conv_ast)
+
+ if isinstance(value1, basestring) or isinstance(value2, basestring):
+ # convert both to strings
+ (value1_conv, value1_conv_ast) = _to_string(value1, value1_ast)
+ (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)
+
+ if convert_to_ast:
+ return (value1_conv_ast, value2_conv_ast)
+ else:
+ return (value1_conv, value2_conv)
+
+ @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. """
+ if arg2 is None:
+ return + arg1
+ else:
+ return arg1 + arg2
+
+ @staticmethod
def convert_to_number(value, decimal_point='.'):
""" Convert value to a number. If value is an AST, another AST is
wrapped around it to represent the conversion, e.g.,
@@ -650,15 +732,6 @@ class Primitive(object):
return converted
@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. """
- if arg2 is None:
- return + arg1
- else:
- return arg1 + arg2
-
- @staticmethod
def minus(arg1, arg2=None):
""" If only one argument is given, change its sign. If two
arguments are given, subtract the second from the first. """