Parser of berki style problems and generator of latex file
Samo Penic
2018-10-24 5288d6d04b01d7705f153bd9ca43cee1516876eb
Sci formatter done
2 files modified
63 ■■■■■ changed files
tools/Formatter.py 16 ●●●● patch | view | raw | blame | history
tools/tests/formatter_test.py 47 ●●●●● patch | view | raw | blame | history
tools/Formatter.py
@@ -1,5 +1,5 @@
from . import Exceptions
from math import floor, log10
class FormatterFactory:
    @staticmethod
@@ -13,6 +13,14 @@
        else:
            return None
    @staticmethod
    def fexp(f):
        return int(floor(log10(abs(f)))) if f != 0 else 0
    @staticmethod
    def fman(f):
        return f/10**FormatterFactory.fexp(f)
class SciFloatFormatter(FormatterFactory):
@@ -29,7 +37,10 @@
        except TypeError:
            raise ValueError
        return ("${:." + str(self.precision) + "f}$").format(num)
        exp=self.fexp(num)
        man=self.fman(num)
        return ("${:." + str(self.precision-1) + "f} \cdot 10^{{{}}}$").format(man, int(exp))
class EngFloatFormatter(FormatterFactory):
@@ -44,4 +55,5 @@
        except (ValueError, TypeError):
            raise ValueError
        # TODO: Change formatting string
        return ("${:." + str(self.precision) + "f}$").format(num)
tools/tests/formatter_test.py
@@ -26,25 +26,38 @@
            -1.123e6,
            9.8123e255,
            0,
            "-NaN",
            "+NaN",
        ]
        sci2_solutions = [
            "$1.24$",
            "$1.20$",
            "$3.00$",
            "$3.50$",
            "$0.05$",
            "$-1.23$",
            "$-3.00$",
            "$1.23 \cdot 10^{-6}$",
            "$-1.23 \cdot 10^{6}$",
            "$9.91 \cdot 10^{255}$",
            "$0.00$",
            "ni rezultata",
            "ni rezultata",
            "$1.2 \cdot 10^{0}$",
            "$1.2 \cdot 10^{0}$",
            "$3.0 \cdot 10^{0}$",
            "$3.5 \cdot 10^{0}$",
            "$5.0 \cdot 10^{-2}$",
            "$-1.2 \cdot 10^{0}$",
            "$-3.0 \cdot 10^{0}$",
            "$1.1 \cdot 10^{-6}$",
            "$-1.1 \cdot 10^{6}$",
            "$9.8 \cdot 10^{255}$",
            "$0.0 \cdot 10^{0}$",
        ]
        sci3_solutions = [
            "$1.24 \cdot 10^{0}$",
            "$1.20 \cdot 10^{0}$",
            "$3.00 \cdot 10^{0}$",
            "$3.50 \cdot 10^{0}$",
            "$5.00 \cdot 10^{-2}$",
            "$-1.23 \cdot 10^{0}$",
            "$-3.00 \cdot 10^{0}$",
            "$1.12 \cdot 10^{-6}$",
            "$-1.12 \cdot 10^{6}$",
            "$9.81 \cdot 10^{255}$",
            "$0.00 \cdot 10^{0}$",
        ]
        sci2 = Formatter.FormatterFactory().get_formatter("sci 2")
        for case, result in zip(testcases, sci2_solutions):
            self.assertEqual(sci2.toFormat(case), result)
        for case, result in zip(testcases,sci2_solutions):
            self.assertEqual(result, sci2.toFormat(case))
        sci3 = Formatter.FormatterFactory().get_formatter("sci 3")
        for case, result in zip(testcases,sci3_solutions):
            self.assertEqual(result, sci3.toFormat(case))