Пример #1
0
    def test_formatter_reuse(self):
        runs = 100

        value1, recreate_time = StaticTimer.time_it(lambda: Formatter(
            "Age: @age, Domain: @email.$split('@').1, Friends: @friends.$map('index', 'name').$join"
        ).single(large_data[0]),
                                                    runs=runs,
                                                    iterations_per_run=1,
                                                    display=False,
                                                    log_arguments=False)

        f = Formatter(
            "Age: @age, Domain: @email.$split('@').1, Friends: @friends.$map('index', 'name').$join"
        )
        value2, reuse_time = StaticTimer.time_it(f.single,
                                                 large_data[0],
                                                 runs=runs,
                                                 iterations_per_run=1,
                                                 display=False,
                                                 log_arguments=False)

        print(f.single(large_data[0]))
        print(recreate_time / reuse_time,
              "x faster to reuse Formatter then recreate")
        self.assertEqual(value1, value2)
        self.assertGreater(recreate_time / reuse_time, 5)
Пример #2
0
 def test_nested_capital_prefix(self):
     data = {"env": {"VERSION": "1.0.0"}}
     prefix = "build/"
     self.assertEqual(
         prefix + data["env"]["VERSION"],
         Formatter(prefix+"@env.VERSION").single(data)
     )
Пример #3
0
 def test_special_surrounded(self):
     x = 5
     y = 5.6
     self.assertEqual(
         f"{x}+{y}={x + y}={y + x}",
         Formatter("@x+@y=@x.$add(@y)=@y.$add(@x)").single({"x": x, "y": y})
     )
Пример #4
0
 def test_at_at(self):
     self.assertEqual(
         f"<redacted>@{small_data[0]['email'].split('@')[1]}",
         Formatter("<redacted>@@@email.$split('@').1").single(small_data[0])
     )
Пример #5
0
 def test_many(self):
     self.assertEqual(
         [f"Name: {item['name']}" for item in small_data[:3]],
         Formatter("Name: @name").many(small_data[:3])
     )
Пример #6
0
 def test_complex_field_as_argument(self):
     self.assertEqual(
         "5.0", Formatter("@a.$distance(@b)").single({"a": [1, 1], "b": [4, 5]})
     )
Пример #7
0
 def test_join(self):
     self.assertEqual(
         f"Tags: [{'], ['.join(small_data[0]['tags'])}]",
         Formatter('Tags: [@tags.$join("], [")]').single(small_data[0])
     )
Пример #8
0
 def test_multiple(self):
     self.assertEqual(
         f"{small_data[0]['name']} {small_data[0]['gender']}",
         Formatter("@name @gender").single(small_data[0])
     )
Пример #9
0
 def test_nested_number(self):
     self.assertEqual(
         "Balance: $750",
         Formatter("Balance: $@balance.$subtract(@pending_charges)").single(
             {"balance": 1000, "pending_charges": 250}),
     )
Пример #10
0
 def test_format_missing_nested_field(self):
     self.assertEqual(
         "<missing>",
         Formatter("@a.$index(@index, '<missing>')").single({"a": [1, 2]})
     )
Пример #11
0
 def test_format_missing_nested(self):
     self.assertEqual(
         "N/A",
         Formatter("@found.missing", "N/A").single({"found": {}})
     )
Пример #12
0
 def test_format_missing(self):
     self.assertEqual(
         "<missing>",
         Formatter("@missing").single({})
     )
Пример #13
0
 def test_nested_capital(self):
     data = {"env": {"VERSION": "1.0.0"}}
     self.assertEqual(
         data["env"]["VERSION"],
         Formatter("@env.VERSION").single(data)
     )
Пример #14
0
 def test_surrounded_query(self):
     self.assertEqual(
         f'<p class="lead">{small_data[0]["email"]}</p>',
         Formatter('<p class="lead">@email</p>').single(small_data[0])
     )
Пример #15
0
 def test_nested_fallback(self):
     self.assertEqual("5", Formatter("@missing.something.$fallback(5)").single({}))
Пример #16
0
 def test_one_missing_out_of_several(self):
     self.assertEqual(
         f"Name: {small_data[0]['name']}, Missing: <missing>",
         Formatter("Name: @name, Missing: @missing").single(small_data[0])
     )
Пример #17
0
    #            curve=timer.best_fit_curve(1, curve_type="Linear", transformers=len),
    #            equation_rounding=4)
    # timer.plot(split_index=2, transformer=len, plot_curve=True,
    #            curve=timer.best_fit_curve(2, curve_type="Linear", transformers=len),
    #            equation_rounding=4)
    #
    # timer.statistics()

    # q = Query("friends.0.name.$split(' ').$join(': ')")
    # print(q.single(large_data[0]))
    # timer.time_it(
    #     q.many, large_data,
    #     iterations_per_run=1, runs=10
    # )
    #
    # q = Query("registered.$strptime.$attr('year')")
    # print(q.single(small_data[0]))
    # timer.time_it(
    #     q.many, small_data,
    #     iterations_per_run=50, runs=10
    # )

    item = {"x1": 1, "y1": 1, "x2": 12, "y2": 54}
    f = Formatter(
        "Midpoint: [@x2.$subtract(@x1).$divide(2), @y2.$subtract(@y1).$divide(2)]"
    )
    print(f.single(item))
    timer.time_it(f.single, item, iterations_per_run=10000, runs=10)

    timer.statistics()