Added Eng and Dec formatters
| | |
| | | |
| | | #StartRezultat |
| | | TeX: U |
| | | izpis: sci 3 |
| | | izpis: eng 3 |
| | | enota: V |
| | | formula: eps0=8.854e-12;D=sqrt(d^2+4*h^2);U=q*log(2*h*d/(r0*D))/pi/eps0;U |
| | | napacna: pi*eps0/log(2*h*d/(r0*D))/q^2 |
| | |
| | | |
| | | #StartSkalar |
| | | ime: r0 |
| | | izpis: sci 1 |
| | | izpis: dec 1 |
| | | nacin: FixedVals 5e-3 6e-3 8e-3 |
| | | #EndSkalar |
| | | |
| | |
| | | |
| | | class AnswerProximityError(Exception): |
| | | pass |
| | | |
| | | class PrefixError(Exception): |
| | | pass |
| | |
| | | retstr += Template(self.template["subproblem"]).substitute( |
| | | { |
| | | "text": sp, |
| | | "ans1": sol["shuffled"][0][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), #sol["correct"][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), |
| | | "ans2": sol["shuffled"][1][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), |
| | | "ans3": sol["shuffled"][2][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), |
| | | "ans4": sol["shuffled"][3][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), |
| | | "ans1": sol["shuffled"][0][0].format_as_tex( |
| | | glyph=sol["glyph"], unit=sol["unit"] |
| | | ), |
| | | "ans2": sol["shuffled"][1][0].format_as_tex( |
| | | glyph=sol["glyph"], unit=sol["unit"] |
| | | ), |
| | | "ans3": sol["shuffled"][2][0].format_as_tex( |
| | | glyph=sol["glyph"], unit=sol["unit"] |
| | | ), |
| | | "ans4": sol["shuffled"][3][0].format_as_tex( |
| | | glyph=sol["glyph"], unit=sol["unit"] |
| | | ), |
| | | } |
| | | ) |
| | | retstr += Template(self.template["subproblem_end"]).substitute() |
| | |
| | | { |
| | | "problem_number": 0, |
| | | "text": problem_dict["introduction"], |
| | | "ans1": problem_dict["solutions"][0]["shuffled"][0][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]), |
| | | "ans2": problem_dict["solutions"][0]["shuffled"][1][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]), |
| | | "ans3": problem_dict["solutions"][0]["shuffled"][2][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]), |
| | | "ans4": problem_dict["solutions"][0]["shuffled"][3][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]) |
| | | "ans1": problem_dict["solutions"][0]["shuffled"][0][ |
| | | 0 |
| | | ].format_as_tex( |
| | | glyph=problem_dict["solutions"][0]["glyph"], |
| | | unit=problem_dict["solutions"][0]["unit"], |
| | | ), |
| | | "ans2": problem_dict["solutions"][0]["shuffled"][1][ |
| | | 0 |
| | | ].format_as_tex( |
| | | glyph=problem_dict["solutions"][0]["glyph"], |
| | | unit=problem_dict["solutions"][0]["unit"], |
| | | ), |
| | | "ans3": problem_dict["solutions"][0]["shuffled"][2][ |
| | | 0 |
| | | ].format_as_tex( |
| | | glyph=problem_dict["solutions"][0]["glyph"], |
| | | unit=problem_dict["solutions"][0]["unit"], |
| | | ), |
| | | "ans4": problem_dict["solutions"][0]["shuffled"][3][ |
| | | 0 |
| | | ].format_as_tex( |
| | | glyph=problem_dict["solutions"][0]["glyph"], |
| | | unit=problem_dict["solutions"][0]["unit"], |
| | | ), |
| | | } |
| | | ) |
| | | return retstr |
| | |
| | | self.formatting = formatting |
| | | |
| | | def is_float(self): |
| | | if self.type==FLOAT: |
| | | if self.type == FLOAT: |
| | | return True |
| | | else: |
| | | return False |
| | |
| | | return float(man) |
| | | |
| | | |
| | | class DecFloatFormatter(SciFloatFormatter): |
| | | pass |
| | | |
| | | |
| | | class EngFloatFormatter(SciFloatFormatter): |
| | | pass |
| | | |
| | | |
| | | class EngFloatFormatter1(FormatterFactory): |
| | | class EngFloatFormatter(FormatterFactory): |
| | | def __init__(self, formatparameters): |
| | | if len(formatparameters) != 1: |
| | | raise Exceptions.WrongParameters("Eng format accept only one argument") |
| | | self.precision = int(formatparameters[0]) |
| | | |
| | | def realign3(self, exp, man): |
| | | mul = exp % 3 |
| | | man = man * 10 ** mul |
| | | exp = exp - mul |
| | | return (exp, man) |
| | | |
| | | def toFormat(self, num): |
| | | try: |
| | | num = float(num) |
| | | except (ValueError, TypeError): |
| | | except ValueError: |
| | | raise ValueError |
| | | # TODO: Change formatting string |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | return ("{:." + str(self.precision) + "f}").format(num) |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | (exp, man) = self.realign3(exp, man) |
| | | if exp == 0: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!") |
| | | ) |
| | | else: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}") |
| | | .format(man, int(exp)) |
| | | .replace(".", ",\!") |
| | | ) |
| | | |
| | | def getValue(self, num): |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | man = ("{:." + str(self.precision - 1) + "f}e{}").format(man, int(exp)) |
| | | return float(man) |
| | | |
| | | |
| | | class DecFloatFormatter(EngFloatFormatter): |
| | | def __init__(self, formatparameters): |
| | | if len(formatparameters) != 1: |
| | | raise Exceptions.WrongParameters("Dec format accept only one argument") |
| | | self.precision = int(formatparameters[0]) |
| | | |
| | | def exp2prefix(self, exp): |
| | | prefixes = { |
| | | "24": "Y", |
| | | "21": "Z", |
| | | "18": "E", |
| | | "15": "P", |
| | | "12": "T", |
| | | "9": "G", |
| | | "6": "M", |
| | | "3": "k", |
| | | "-3": "m", |
| | | "-6":"\\upmu", |
| | | "-9":"n", |
| | | "-12":"p", |
| | | "-15":"f", |
| | | "-18":"a", |
| | | "-21":"z", |
| | | "-24":"y" |
| | | } |
| | | try: |
| | | prefix = prefixes[str(exp)] |
| | | except KeyError: |
| | | raise Exceptions.PrefixError("Could not change exponent " + str(exp) + " into prefix form!") |
| | | return prefix |
| | | |
| | | |
| | | def toFormat(self, num): |
| | | try: |
| | | num = float(num) |
| | | except ValueError: |
| | | raise ValueError |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | (exp, man) = self.realign3(exp, man) |
| | | if exp == 0: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!") |
| | | ) |
| | | else: |
| | | prefix = self.exp2prefix(exp) |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}~\mathrm{{{}}}") |
| | | .format(man, prefix) |
| | | .replace(".", ",\!") |
| | | ) |