def from_str(klass, s): """Convert a string representation of a C{RatPoly} to a C{RatPoly} @param s: @type s: string """ if s == "nan": return RatPoly(RatNum(1, 0), 0) temp_list = [] # Prevent the first negative sign from being seperated. temp_list += s[0] i = 0 for sub_str in s[1:]: if sub_str == "-": temp_list += "-" i += 1 elif sub_str == "+": temp_list.append("") i += 1 else: temp_list[i] += sub_str li = [] for sub_item in temp_list: li.append([RatTerm.from_str(sub_item).coeff, RatTerm.from_str(sub_item).expt]) poly = RatPoly() poly.terms = [] degree = max(i[1] for i in li) i = degree while i >= 0: for coeff, expt in li: if i == expt: poly.terms += [RatTerm(coeff, expt)] break else: poly.terms += [RatTerm(RatNum(0, 1), i)] i -= 1 return poly
def assert_eq_str(expected, s): assert expected == RatTerm.from_str(s)