def __init__(self, arg, **options): if not (type(arg) == list or isinstance(arg, Point)): raise error.WrongArgument(' list|Point ', str(type(arg))) elif type(arg) == list: if not len(arg) == 2: raise error.WrongArgument(' a list of length ' + str(len(arg)), ' a list of length 2 ') if not type(arg[0]) == str: raise error.WrongArgument(str(type(arg[0])), ' a str ') if not (type(arg[1]) == tuple and len(arg[1]) == 2 and is_.a_number(arg[1][0]) and is_.a_number(arg[1][1])): # __ raise error.WrongArgument(str(arg), ' (x, y) ') self._name = arg[0] self._x = Decimal(arg[1][0]) self._y = Decimal(arg[1][1]) else: self._name = arg.name self._x = arg.x self._y = arg.y self._x_exact = self._x self._y_exact = self._y
def rotate(self, center, angle, **options): if not isinstance(center, Point): raise error.WrongArgument(' a Point ', str(type(center))) if not is_.a_number(angle): raise error.WrongArgument(' a number ', str(type(angle))) delta_x = self.x_exact - center.x_exact delta_y = self.y_exact - center.y_exact rx = delta_x * Decimal(str(math.cos(deg_to_rad(angle)))) \ - delta_y * Decimal(str(math.sin(deg_to_rad(angle)))) \ + center.x_exact ry = delta_x * Decimal(str(math.sin(deg_to_rad(angle)))) \ + delta_y * Decimal(str(math.cos(deg_to_rad(angle)))) \ + center.y_exact new_name = self.name + "'" if 'keep_name' in options and options['keep_name']: new_name = self.name elif 'new_name' in options and type(options['new_name']) == str: new_name = options['new_name'] return Point([new_name, (rx, ry)])
def mark(self, arg): if not type(arg) == str: raise error.WrongArgument(' str ', str(type(arg))) if arg not in AVAILABLE_SEGMENT_MARKS: raise error.WrongArgument(arg, 'a string from this list: ' + str(AVAILABLE_SEGMENT_MARKS)) self._mark = arg
def mark(self, arg): if type(arg) == str: if arg in AVAILABLE_ANGLE_MARKS: self._mark = arg else: raise error.WrongArgument(arg, 'a string from this list: ' + str(AVAILABLE_ANGLE_MARKS)) else: raise error.WrongArgument(arg, 'str')
def insert_picture(self, drawable_arg, **options): if not isinstance(drawable_arg, Drawable): raise error.WrongArgument(str(drawable_arg), 'a Drawable') if self.create_pic_files: drawable_arg.into_pic(**options) else: drawable_arg.into_pic(create_pic_files=False, **options) s = "1" if 'scale' in options: s = str(options['scale']) if 'vertical_alignment_in_a_tabular' in options: return "\\raisebox{-.5\height}{" \ + "\includegraphics[scale=" + s + "]{" \ + drawable_arg.eps_filename \ + "}" + "}" elif 'top_aligned_in_a_tabular' in options: # return "\includegraphics[align=t, scale=" + s + "]{" \ # + drawable_arg.eps_filename \ # + "}" + "\\newline" + "\n" return "\\raisebox{-0.9\height}{" \ + "\includegraphics[scale=" + s + "]{" \ + drawable_arg.eps_filename \ + "}" + "}" else: return "\includegraphics[scale=" + s + "]{" \ + drawable_arg.eps_filename \ + "}" + "\\newline" + "\n"
def label_display_angle(self, arg): if not is_.a_number(arg): raise error.WrongArgument(arg, ' a number ') else: self._label_display_angle = round(Decimal(str(arg)), Decimal('0.1'), rounding=ROUND_HALF_UP)
def bisector_vector(self, arg): """ Return a vector colinear to the bisector of self and another vector. :param arg: the other vector :type arg: Vector """ if not isinstance(arg, Vector): raise error.WrongArgument(str(type(arg)), "a Vector") return self.unit_vector() + arg.unit_vector()
def length(self, arg): if not isinstance(arg, Value): raise TypeError('Expected a Value, got ' + str(type(arg)) + " " 'instead.') if not arg.is_numeric(): raise error.WrongArgument('numeric Value', 'a Value but not numeric, it contains ' + str(arg.raw_value)) self._length = arg self._length_has_been_set = True
def get_legs_matching_given_hypotenuse(side_length): if not type(side_length) == int: raise error.WrongArgument(str(side_length), "int") result = [] for elt in ALL_TRIPLES_5_200: if elt[2] == side_length: result += [elt[0], elt[1]] return result
def __init__(self, arg, **options): self._x_exact = Decimal('1') self._y_exact = Decimal('1') if isinstance(arg, Point): Point.__init__(self, Point(["", (arg.x_exact, arg.y_exact)]), **options) elif isinstance(arg, tuple) and len(arg) == 2: if all([isinstance(elt, Point) for elt in arg]): Point.__init__(self, Point(["", (arg[1].x_exact - arg[0].x_exact, arg[1].y_exact - arg[0].y_exact)])) elif all([is_.a_number(elt) for elt in arg]): Point.__init__(self, Point(["", (arg[0], arg[1])])) else: raise error.WrongArgument("a tuple not only of Points or" " numbers", "(Point,Point)|(x,y)") else: raise error.WrongArgument(str(type(arg)), "Point|(,)")
def __init__(self, arg, **options): self._label = Value("") if not (type(arg) == tuple or isinstance(arg, Segment)): raise error.WrongArgument(' tuple|Segment ', str(type(arg))) elif type(arg) == tuple: if not (isinstance(arg[0], Point) and isinstance(arg[1], Point)): # __ raise error.WrongArgument(' (Point, Point) ', str(arg)) self._points = (arg[0].clone(), arg[1].clone()) if 'label' in options and type(options['label']) == str: self._label = options['label'] else: self._points = (arg.points[0].clone(), arg.points[1].clone()) self._label = arg.label self._mark = "" self._name = "[" + self.points[0].name + self.points[1].name + "]" self._length = Value(0) self._length_has_been_set = False self._length_name = self._points[0].name + self._points[1].name
def __init__(self, arg, **options): Exponented.__init__(self) if type(arg) == str: self._name = arg if 'exponent' in options: self._exponent = Value(options['exponent']) elif type(arg) == Unit: self._name = copy.deepcopy(arg.name) self._exponent = copy.deepcopy(arg.exponent) else: raise error.WrongArgument(arg, "a string or a Unit")
def __init__(self, arg, **options): self._ray0 = None self._ray1 = None self._points = None self._measure = None self._mark = "" self._label = Value("") self._name = None if (isinstance(arg, tuple) and len(arg) == 3 and isinstance(arg[0], Point) and isinstance(arg[1], Point) and isinstance(arg[2], Point)): # __ self._ray0 = Ray((arg[1], arg[0])) self._ray1 = Ray((arg[1], arg[2])) self._points = [arg[0].clone(), arg[1].clone(), arg[2].clone()] # Let's determine the measure of the angle: aux_side0 = Segment((self._points[0], self._points[1])) aux_side1 = Segment((self._points[1], self._points[2])) aux_side2 = Segment((self._points[2], self._points[0])) aux_num = aux_side0.real_length * aux_side0.real_length \ + aux_side1.real_length * aux_side1.real_length \ - aux_side2.real_length * aux_side2.real_length aux_denom = 2 * aux_side0.real_length * aux_side1.real_length aux_cos = aux_num / aux_denom self._measure = Decimal(str(math.degrees(math.acos(aux_cos)))) if 'label' in options and type(options['label']) == str: self._label = Value(options['label']) if 'mark' in options and type(options['mark']) == str: self._mark = options['mark'] self._name = MARKUP['opening_widehat'] self._name += arg[0].name + arg[1].name + arg[2].name self._name += MARKUP['closing_widehat'] else: raise error.WrongArgument(' (Point, Point, Point) ', str(type(arg))) self._label_display_angle = round(Decimal(str(self._measure)), Decimal('0.1'), rounding=ROUND_HALF_UP) / 2
def label(self, arg): if not type(arg) == Value: raise error.WrongArgument(' Value ', str(type(arg))) self._label = arg
def __init__(self, font_size_offset, sheet_layout_unit, sheet_layout, layout_type, **options): try: self.derived except AttributeError: raise error.NotInstanciableObject(self) self.exercises_list = list() shared.machine.set_font_size_offset(font_size_offset) self.sheet_layout_unit = sheet_layout_unit self.layout_type = layout_type self.write_texts_twice = False if 'write_texts_twice' in options and options['write_texts_twice']: self.write_texts_twice = True # Some tests on sheet_layout before using it ; # but it's a bit complicated to write a complete set of tests on it ; # e.g. if the user doesn't use the same number of exercises in the # 'exc' key as in 'ans' key (which would be stupid) this # won't be checked here and so it won't work. if type(sheet_layout) != dict: raise error.UncompatibleType(str(type(sheet_layout)), 'dict') if len(sheet_layout) != 2: raise error.WrongArgument('SHEET_LAYOUT should have two keys', 'it has ' + str(len(sheet_layout)) + ' keys') for k in ['exc', 'ans']: if k not in sheet_layout: raise error.WrongArgument('SHEET_LAYOUT should have a key ' + k, 'it has no such key') if type(sheet_layout[k]) != list: raise error.WrongArgument('SHEET_LAYOUT[' + k + '] should be' + ' a list', str(type(sheet_layout[k]))) if len(sheet_layout[k]) % 2: raise error.WrongArgument('SHEET_LAYOUT[' + k + '] should have' + ' an even number of elements', str(len(sheet_layout[k])) + ' elements') for i in range(int(len(sheet_layout[k]) // 2)): if (not (sheet_layout[k][2 * i] is None or type(sheet_layout[k][2 * i]) == list or sheet_layout[k][2 * i] == 'jump')): # __ raise error.WrongArgument('SHEET_LAYOUT[' + k + '][' + str(2 * i) + '] should be ' 'either a list ' 'or None or "jump"', str(type( sheet_layout[k][2 * i]))) elif sheet_layout[k][2 * i] is None: if (not (type(sheet_layout[k][2 * i + 1]) == int or sheet_layout[k][2 * i + 1] in ['all', 'all_left', 'jump'])): # __ raise error.WrongArgument( 'SHEET_LAYOUT[' + k + '][' + str(2 * i + 1) + '] should be an ' + 'int since it follows the None' + 'keyword', type(sheet_layout[k][2 * i + 1])) elif sheet_layout[k][2 * i] == 'jump': if not sheet_layout[k][2 * i + 1] == 'next_page': raise error.WrongArgument( 'SHEET_LAYOUT[' + k + '][' + str(2 * i + 1) + '] should be: ' + 'next_page since it follows ' + 'the jump keyword', type(sheet_layout[k][2 * i + 1])) elif type(sheet_layout[k][2 * i]) == list: if not type(sheet_layout[k][2 * i + 1]) == tuple: raise error.WrongArgument( 'SHEET_LAYOUT[' + k + '][' + str(2 * i + 1) + '] should be a tuple', type(sheet_layout[k][2 * i + 1])) if (not len(sheet_layout[k][2 * i + 1]) == (len(sheet_layout[k][2 * i]) - 1) * sheet_layout[k][2 * i][0]): # __ raise error.WrongArgument( 'SHEET_LAYOUT[' + k + '][' + str(2 * i + 1) + '] should have ' + ' as many elements as the ' + 'number of cols described in ' + 'SHEET_LAYOUT[' + k + '][' + str(2 * i) + ']', str(len(sheet_layout[k][2 * i + 1])) + ' instead of ' + str(len(sheet_layout[k][2 * i]) - 1)) else: raise error.WrongArgument( 'SHEET_LAYOUT[' + k + '][' + str(2 * i) + '] is not of any ' + ' of the expected types or ' + 'values.', str(len(sheet_layout[k][2 * i]))) self.sheet_layout = sheet_layout
def set_has_been_rounded(self, arg): if arg not in [True, False]: raise error.WrongArgument(str(type(arg)), "True|False") else: self._has_been_rounded = arg
def __add__(self, arg): if not isinstance(arg, Vector): raise error.WrongArgument(str(type(arg)), "a Vector") return Vector((self.x_exact + arg.x_exact, self.y_exact + arg.y_exact))
def name(self, arg): if not (type(arg) == str or type(arg) == int): raise error.WrongArgument(str(type(arg)), "str|int") self._name = str(arg)
def y(self, arg): if not is_.a_number(arg): raise error.WrongArgument(' a number ', str(arg)) self._y = arg
def label(self, arg): if type(arg) == str or isinstance(arg, Value): self._label = Value(arg) else: raise error.WrongArgument(arg, 'str or Value')