From e919179c2c5eeff3ab814f8c0927775f463671b7 Mon Sep 17 00:00:00 2001 From: Marion Date: Wed, 04 Sep 2013 10:06:03 +0000 Subject: remove obsolete (pre-type system) type conversion methods --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index e71b226..84a374d 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -132,13 +132,6 @@ class Palettes(): self.tw = turtle_window self.prim_cache = { - "check_number": Primitive(self.check_number, - return_type=TYPE_NUMBER, # TODO make this function obsolete (the return type is actually nonsense) - arg_descs=[ArgSlot(TYPE_OBJECT)], export_me=False), - "convert_for_cmp": Primitive(Primitive.convert_for_cmp, - constant_args={'decimal_point': self.tw.decimal_point}), - "convert_to_number": Primitive(Primitive.convert_to_number, - constant_args={'decimal_point': self.tw.decimal_point}), "minus": Primitive(Primitive.minus, return_type=TYPE_NUMBER, arg_descs=[ArgSlot(TYPE_NUMBER)]) @@ -1215,29 +1208,6 @@ variable')) break self.tw.lc.ireturn() yield True - - def check_number(self, value): - ''' Check if value is a number. If yes, return the value. If no, - raise a logoerror. ''' - if not _num_type(value): - raise logoerror("#notanumber") - return value - - def check_non_negative(self, x, msg="#negroot"): - ''' Raise a logoerror iff x is negative. Otherwise, return x - unchanged. - msg -- the name of the logoerror message ''' - if x < 0: - raise logoerror(msg) - return x - - def check_non_zero(self, x, msg="#zerodivide"): - ''' Raise a logoerror iff x is zero. Otherwise, return x - unchanged. - msg -- the name of the logoerror message ''' - if x == 0: - raise logoerror(msg) - return x def after_right(self, *ignored_args): if self.tw.lc.update_values: diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index f303910..ea3f847 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -404,10 +404,6 @@ class Primitive(object): elif self == Primitive.square_root: return get_call_ast('sqrt', new_arg_asts, new_kwarg_asts) - # type conversion # TODO remove when obsolete - elif self in (Primitive.convert_for_cmp, Primitive.convert_to_number): - return self.func(*new_arg_asts, **new_kwarg_asts) - # identity elif self == Primitive.identity: if len(new_arg_asts) == 1: @@ -588,77 +584,6 @@ class Primitive(object): 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., - Str(s='1.2') -> Call(func=Name('float'), args=[Str(s='1.2')]) - 1. Return all numbers (float, int, long) unchanged. - 2. Convert a string containing a number into a float. - 3. Convert a single character to its ASCII integer value. - 4. Extract the first element of a list and convert it to a number. - 5. Convert a Color to a float. - If the value cannot be converted to a number and the value is not - an AST, return None. If it is an AST, return an AST representing - `float(value)'. """ # TODO find a better solution - # 1. number - if isinstance(value, (float, int, long, ast.Num)): - return value - - converted = None - conversion_ast = None - convert_to_ast = False - if isinstance(value, ast.AST): - convert_to_ast = True - value_ast = value - value = ast_to_value(value_ast) - if isinstance(decimal_point, ast.AST): - decimal_point = ast_to_value(decimal_point) - - # 2./3. string - if isinstance(value, basestring): - if convert_to_ast: - conversion_ast = Primitive.convert_for_cmp(value_ast, - decimal_point) - if not isinstance(conversion_ast, ast.Num): - converted = None - else: - converted = Primitive.convert_for_cmp(value, decimal_point) - if not isinstance(converted, (float, int, long)): - converted = None - # 4. list - elif isinstance(value, list): - if value: - number = Primitive.convert_to_number(value[0]) - if convert_to_ast: - conversion_ast = number - else: - converted = number - else: - converted = None - if convert_to_ast: - conversion_ast = get_call_ast('float', [value_ast]) - # 5. Color - elif isinstance(value, Color): - converted = float(value) - if convert_to_ast: - conversion_ast = get_call_ast('float', [value_ast]) - else: - converted = None - if convert_to_ast: - conversion_ast = get_call_ast('float', [value_ast]) - - if convert_to_ast: - if conversion_ast is None: - return value_ast - else: - return conversion_ast - else: - if converted is None: - return value - else: - return converted - - @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. """ @@ -719,59 +644,6 @@ class Primitive(object): return not arg @staticmethod - def convert_for_cmp(value, decimal_point='.'): - """ Convert value such that it can be compared to something else. If - value is an AST, another AST is wrapped around it to represent the - conversion, e.g., - Str(s='a') -> Call(func=Name('ord'), args=[Str(s='a')]) - 1. Convert a string containing a number into a float. - 2. Convert a single character to its ASCII integer value. - 3. Return all other values unchanged. """ - converted = None - conversion_ast = None - convert_to_ast = False - if isinstance(value, ast.AST): - convert_to_ast = True - value_ast = value - value = ast_to_value(value_ast) - if isinstance(decimal_point, ast.AST): - decimal_point = ast_to_value(decimal_point) - - if isinstance(value, basestring): - # 1. string containing a number - replaced = value.replace(decimal_point, '.') - try: - converted = float(replaced) - except ValueError: - pass - else: - if convert_to_ast: - conversion_ast = get_call_ast('float', [value_ast]) - - # 2. single character - if converted is None: - try: - converted = ord(value) - except TypeError: - pass - else: - if convert_to_ast: - conversion_ast = get_call_ast('ord', [value_ast]) - - # 3. normal string or other type of value (nothing to do) - - if convert_to_ast: - if conversion_ast is None: - return value_ast - else: - return conversion_ast - else: - if converted is None: - return value - else: - return converted - - @staticmethod def equals(arg1, arg2): """ Return arg1 == arg2 """ return arg1 == arg2 -- cgit v0.9.1