from tkinter import font
from turtle import circle
from manimlib import *
from manim_fonts import *
import pickle
import numpy as np

big = 60
middle = 40
small = 25

time_say_hi = 3
class TextExample(Scene):
    CONFIG = {
      "camera_config": {"background_color": "#000000"},
    }
    def construct(self):
        # #####################################################################################
        # Today I’m going to present our reproduction of pHPA: A Proactive Autoscaling Framework for Microservice Chain. I bet many of you know what a microservice is but I would like to briefly introduce the concept of a microservice for those who do not.
        #####################################################################################
        with RegisterFont("Source Sans Pro") as fonts:
          the_font = "Source Sans Pro ExtraLight"
          section = Text("1. Microservice", font=the_font, font_size=30).move_to([-6, 3.5, 0], aligned_edge=LEFT)

          def split(x, y, scale=0.8):
            new_square = SVGMobject("cloud.svg", stroke_width = 0, opacity=0.8).scale(0.8 * 1.6 * 1.6).scale(0.3, about_point=[0, -0.1, 0])
            new_image = SVGMobject("cube.svg", stroke_width = 0).move_to([0, -0.1, 0]).scale(0.25)
            m = VGroup(new_square, new_image).move_to([x, y, 0]).scale(scale)
            # line = Arrow(user,m, buff=0)
            self.play(FadeIn(m),run_time=0.01)
            m.add_updater(lambda mob, dt: mob.rotate(-1*DEGREES, about_point=[x + 0.05, y + 0.05, 0]))
            m.add_updater(lambda mob, dt: mob.rotate(1*DEGREES))
            return m, new_square

          text1 = Text("A Reproduction Of", font=the_font)
          text = Text("pHPA: A Proactive Autoscaling Framework\nfor Microservice Chain [APNet’21]", font=the_font, t2s={"APNet’21": ITALIC})
          print(fonts)
          text.set_color_by_gradient(BLUE, PURPLE, GREEN)
          texts = VGroup(text1, text).arrange(DOWN, buff=0.1, aligned_edge=LEFT)

          difference = Text(
              """
              Bonan Kou / Yuan Tian / Zhaoqing Wu / Chengfei Wu / Hao Shen
              """,
              font='Source Sans Pro ExtraLight', font_size=27,
          )
          VGroup(texts, difference).arrange(DOWN, buff=1)
          self.play(Write(text1), run_time=0.5)
          self.play(Write(text))
          self.add(difference)
          self.wait(time_say_hi)
          self.remove(VGroup(texts, difference))

          #####################################################################################
          # 1.	Suppose you have a single program that carries out some basic logic.
          #####################################################################################
          self.add(section)
          square = SVGMobject("cloud.svg", stroke_width = 0, opacity=0.8).scale(0.8)
          image = SVGMobject("cube.svg", stroke_width = 0).scale(0.2)
          image.move_to([0, -0.1, 0])

          self.play(FadeIn(square), FadeIn(image))
          services = [image]
          def spawn(distance):
            image2 = SVGMobject("cube.svg", stroke_width = 0).scale(0.2)
            fade_list = []
            play_list = []
            patch_fade = []
            patch_list = []
            for i in [0, distance, -distance]:
              for j in [0, distance, -distance]:
                if (i != 0 or j != 0) and not (abs(i) == 1 and abs(j) == 1):
                  image2 = SVGMobject("cube.svg", stroke_width = 0).scale(0.2)
                  image2.move_to([0, -0.1, 0])
                  services.append(image2)
                  if abs(distance) == 1.0:
                    image2.move_to([i / 2, j / 2 - 0.1, 0])
                  fade_list.append(FadeIn(image2))
                  play_list.append(image2.animate.move_to([i, j - 0.1, 0]))

                  if (i == 0 or j == 0) and abs(distance) == 1.0:
                    image3 = SVGMobject("cube.svg", stroke_width = 0).scale(0.2)
                    image4 = SVGMobject("cube.svg", stroke_width = 0).scale(0.2)
                    services.append(image3)
                    services.append(image4)
                    image3.move_to([i, j- 0.1, 0])
                    image4.move_to([i, j- 0.1, 0])
                    patch_fade.append(FadeIn(image3))
                    patch_fade.append(FadeIn(image4))
                    if i == 0:
                      patch_list.append(image3.animate.move_to([i - 0.5, j - 0.1, 0]))
                      patch_list.append(image4.animate.move_to([i + 0.5, j - 0.1, 0]))
                    else:
                      patch_list.append(image3.animate.move_to([i, j - 0.5 - 0.1, 0]))
                      patch_list.append(image4.animate.move_to([i, j + 0.5 - 0.1, 0]))

            self.play(*play_list, square.animate.scale(1.6, about_point=[0, -0.1, 0]), run_time=0.8)
            self.play(*patch_list, run_time=0.8)

          ########################################################################################
          # As more and more functionalities are added, this program becomes too complicated to debug and scale.
          #####################################################################################
          spawn(0.5)
          spawn(1.0)
          # self.play()
          for i in range(1):
            color_list = []
            self.play(*color_list, square.animate.set_color("#f0d23e"), run_time=0.5)
            self.play(*color_list, square.animate.set_color("#ff574d"), run_time=1)

          monolith = VGroup(square, *services)
          self.play(FadeOut(monolith, LEFT))

          microservices = []
          for j in [2, 1, 0, -1, -2]:
            for i in [-4, -2, 0, 2, 4]:
              microservices.append(split(i, j)[0])

          all_ms = VGroup(*microservices)

          for i in microservices:
            i.clear_updaters()
          self.play(all_ms.animate.scale(2))

          for i in microservices:
            i.clear_updaters()

          fades = []
          enlarge = []
          for index, i in enumerate(microservices):
            if index == 12:
              fades.append(i.animate.move_to([-4, 0, 0], aligned_edge=LEFT))

            elif index == 13:
              fades.append(i.animate.move_to([4, 0, 0], aligned_edge=RIGHT))

            else:
              fades.append(FadeOut(i))
          self.play(*fades, run_time=0.5)

          now = self.time
          microservices[12].add_updater(lambda m: m.shift(math.cos(self.time - now) * 0.001 * UP))
          microservices[13].add_updater(lambda m: m.shift(math.cos(self.time - now + 1) * 0.001 * UP))

          enlarge.append(microservices[12].animate.scale(1.2))
          enlarge.append(microservices[13].animate.scale(1.2))

          self.play(*enlarge, run_time=0.5)

          ############################################################
          # These components can be indenpendent developed and scaled
          ############################################################

          ################################################################
          # These microservices communicate via RPC, streaming, and message broker.
          cloud_height = (microservices[12].get_top()[1] - microservices[12].get_bottom()[1]) - 0.3
          cloud_width = (microservices[12].get_right()[0] - microservices[12].get_left()[0])
          distance = microservices[13].get_x() - microservices[12].get_x()
          user = SVGMobject("server.svg", stroke_width = 0, opacity=0.8).scale(0.8).move_to([0, 2, 0]).scale(0.4)
          m12_loc = microservices[12].get_center()
          m13_loc = microservices[13].get_center()
          db1 = SVGMobject("database.svg", stroke_width = 0, opacity=0.8).scale(0.8).move_to([m12_loc[0], -2, 0]).scale(0.4)
          db2 = SVGMobject("database.svg", stroke_width = 0, opacity=0.8).scale(0.8).move_to([m13_loc[0], -2, 0]).scale(0.4)

          user_loc = user.get_center()
          a1 = Arrow(start=user_loc, end=[m12_loc[0], m12_loc[1] - 0.1, m12_loc[2]], stroke_width = 3, color=BLUE)
          a2 = Arrow(start=user_loc, end=[m13_loc[0], m13_loc[1] - 0.1, m13_loc[2]], stroke_width = 3, color=BLUE)

          a3 = Arrow(start=[db2.get_center()[0], m12_loc[1]  + 0.9, m12_loc[2]], end=[db2.get_center()[0], db2.get_center()[1] + 0.1, db2.get_center()[2]], stroke_width = 3, color=BLUE)
          a4 = Arrow(start=[db1.get_center()[0], m13_loc[1] + 0.9, m13_loc[2]], end=[db1.get_center()[0], db1.get_center()[1] + 0.1, db1.get_center()[2]], stroke_width = 3, color=BLUE)

          line1 = Rectangle(height= cloud_height / 3, width=distance, color=WHITE, stroke_width=0)
          line1.set_fill(["#764BA2", "#667EEA"], opacity=1)

          line2 = Rectangle(height= cloud_height / 3, width=distance, color=WHITE, stroke_width=0)
          line2.set_fill(["#4E65FF", "#92EFFD", "#4E65FF"], opacity=1)

          line3 = Rectangle(height= cloud_height / 3, width=distance, color=WHITE, stroke_width=0)
          line3.set_fill(["#D4145A", "#FBB03B", "#D4145A"], opacity=1)

          lines = VGroup(line1, line2, line3).arrange(DOWN, buff=0).move_to([0 - distance / 2,0,0], aligned_edge=LEFT)

          self.wait(0.3)
          self.add(lines)
          self.add(microservices[12])
          self.add(microservices[13])

          text1 = Text("Remote procedure call (RPC)", font=the_font,font_size=20, color="#f5f5f5").move_to(line1)
          text2 = Text("Streaming", font=the_font,font_size=20, color="#f5f5f5").move_to(line2)
          text3 = Text("Message broker", font=the_font,font_size=20, color="#f5f5f5").move_to(line3)

          lines.set_width(0.1, stretch=True)
          self.play(lines.animate.set_width(distance, stretch=True), run_time=0.5)

          text1.set_color_by_gradient("#bdc9fc", "#FFFFFF", "#bdc9fc")
          text2.set_color_by_gradient("#e6e3e3", "#92EFFD", "#e6e3e3")
          text3.set_color_by_gradient("#FBB03B", "#e6e3e3", "#FBB03B")
          self.add(text1)
          self.add(text2)
          self.add(text3)
          self.wait(3)

          self.remove(text1)
          self.remove(text2)
          self.remove(text3)
          self.play(lines.animate.set_width(0.01, stretch=True), run_time=0.5)
          self.remove(lines)

          self.add(user)
          self.bring_to_back(a1)
          self.bring_to_back(a2)
          self.play(microservices[12].animate.shift(DOWN*1), microservices[13].animate.shift(DOWN*1), ShowCreation(a1), ShowCreation(a2))
          self.wait(2)
          self.play(Uncreate(a1), Uncreate(a2), FadeOut(user))
          self.bring_to_back(a1)
          self.bring_to_back(a2)
          self.play(microservices[12].animate.shift(UP*1), microservices[13].animate.shift(UP*1))

          ########################################################################
          # And have their own databse
          self.bring_to_back(a3)
          self.bring_to_back(a4)
          self.add(db1)
          self.add(db2)
          self.play(microservices[12].animate.shift(UP*1), microservices[13].animate.shift(UP*1), ShowCreation(a3), ShowCreation(a4))

          self.add(microservices[12])
          self.add(microservices[13])

          self.wait(3)
          self.play(Uncreate(a3), Uncreate(a4), FadeOut(db1), FadeOut(db2))
          self.play(microservices[12].animate.shift(DOWN*1), microservices[13].animate.shift(DOWN*1))

          ########################################################################
          self.play(microservices[12].animate.scale(3 / 2), run_time=0.5)
          self.play(microservices[12].animate.scale(2 / 3), run_time=0.5)
          self.play(microservices[13].animate.scale(3 / 2), run_time=0.5)
          self.play(microservices[13].animate.scale(2 / 3), run_time=0.5)

          # This flexibility brings a lot...
          left = VGroup(microservices[12], microservices[13])

          self.remove(text1)
          self.remove(text2)
          self.remove(text3)

          def shift(prev, next = None, wait=1):
            if next != None:
              self.play(prev.animate.move_to([-20, 0, 0]), next.animate.move_to([0, 0, 0]), run_time=wait)
            else:
              self.play(prev.animate.move_to([-20, 0, 0]))
            self.remove(prev)

          shift(VGroup(microservices[12], microservices[13]))

          self.remove(section)
          section = Text("2. Cascading effect", font=the_font, font_size=30).move_to([-6, 3.5, 0], aligned_edge=LEFT)
          self.add(section)

          ## The cascading effect #############################
          t1 = BulletedList("Microservices form complex dependency relationship", font=the_font, font_size=25).move_to([0, 0, 0])
          t2 = BulletedList("It takes 25 seconds (median) to create a new instance", font=the_font, font_size=25).move_to([0, 0, 0])
          t3 = BulletedList("These lags stack", font=the_font, font_size=25).move_to([0, 0, 0])
          t4 = BulletedList("Current autoscalers are slow", font=the_font, font_size=25)
          allts = VGroup(t1, t2, t3, t4).arrange(DOWN, buff=0.3, aligned_edge=LEFT)

          axes = NumberPlane(
            x_range=[0, 18, 1],
            y_range=[0, 3, 1],
            height = 3,
            width = 6,
            tips=False,
            faded_line_ratio = 4,
            color="#000000",
            axis_config={"include_numbers": False},
            background_line_style={
                "stroke_color": BLACK,
                "stroke_width": 2,
                "stroke_opacity": 0.6
            },
            faded_line_style={
              "stroke_color": BLACK,
            }
          ).rotate(-90 * DEGREES, about_edge=IN).shift(LEFT*3)

          client = axes.get_graph(
            lambda x: 0.8,
            color="#B2BEB5",
            stroke_width=0.2,
            opacity=0.5,
          )
          m1 = axes.get_graph(
            lambda x: 1.6,
            color="#B2BEB5",
            stroke_width = 0.2,
          )
          m2 = axes.get_graph(
            lambda x: 2.4,
            color="#B2BEB5",
            stroke_width = 0.2,
          )

          def traffic(thread, start, end, dimish_list, color = "#029ffa", width = 0.07, icon = True):
            box = Rectangle(height= 0.001, width=width, color=WHITE, stroke_width=0).move_to(axes.coords_to_point(start, thread), aligned_edge=UP)
            box.set_fill(color, opacity=1)
            height = axes.coords_to_point(start)[1] - axes.coords_to_point(end)[1]

            self.add(box)
            self.play(*dimish_list, box.animate.set_height(height, about_edge=UP, stretch=True), run_time=0.5)
            bl = SVGMobject("document.svg").scale(0.2).move_to(axes.coords_to_point((start + end) / 2, thread)).shift(RIGHT*0.25)
            if icon:
              self.play(FadeIn(bl))

            if thread == 0.8:
              bar_list1.append(box)
            elif thread == 1.6:
              bar_list2.append(box)
              icon_list1.append(bl)
            else:
              bar_list3.append(box)
              icon_list2.append(bl)
            return box

          def add_text(content, thread):
            client_t = Text(content, font=the_font, font_size=16).move_to(axes.coords_to_point(0, thread), aligned_edge=DOWN).shift(UP*0.13)
            self.add(client_t)
            return client_t

          label1 = add_text("Client", 0.8)
          label2 = add_text("Service 1", 1.6)
          label3 = add_text("Service 2", 2.4)

          self.play(FadeIn(axes))
          self.play(ShowCreation(m1), ShowCreation(m2), ShowCreation(client))
          self.add(allts)
          allts.next_to(axes).shift(RIGHT*1 + UP * 2)
          def diminish(bar_list, icon_list):
            animation_list = []
            for box in bar_list:
              box.generate_target()
              box.target.set_width(0.02, about_edge=UP, stretch=True)
              box.target.set_opacity(0.5)
              animation_list.append(MoveToTarget(box))
            for bl in icon_list:
              bl.generate_target()
              bl.target.set_opacity(0.5)
              bl.target.scale(0.8)
              animation_list.append(MoveToTarget(bl))
            return animation_list

          def survive(bar_list, icon_list):
            animation_list = []
            for box in bar_list:
              box.generate_target()
              box.target.set_width(0.07, about_edge=UP, stretch=True)
              box.target.set_opacity(1)
              animation_list.append(MoveToTarget(box))
            for bl in icon_list:
              bl.generate_target()
              bl.target.set_opacity(1)
              bl.target.scale(1.25)
              animation_list.append(MoveToTarget(bl))
            return animation_list

          def connect(start, end):
            dashed = DashedLine(start, end, color="#FFFFFF", stroke_width=0.5)
            dahsed_list.append(dashed)
            return [ShowCreation(dashed)]

          ########Traffic arrives
          bar_list1 = []
          bar_list2 = []
          bar_list3 = []
          icon_list1 = []
          icon_list2 = []
          dahsed_list = []
          traffic(0.8, 0, 3.1, [], "#12cc66", 0.1, False)
          temp = diminish(bar_list1, [])

          temp += connect(axes.coords_to_point(3.1, 0.8), axes.coords_to_point(3.1, 1.6))
          traffic(1.6, 3.1, 5.1, temp)
          temp = diminish(bar_list2, icon_list1)
          temp += connect(axes.coords_to_point(5.1, 1.6), axes.coords_to_point(5.1, 2.4))
          traffic(2.4, 5.1, 9.1, temp)
          temp = diminish(bar_list3, icon_list2)
          temp += survive(bar_list2, icon_list1)
          temp += connect(axes.coords_to_point(9.1, 2.4), axes.coords_to_point(9.1, 1.6))
          traffic(1.6, 9.1, 13.1, temp)

          temp = diminish(bar_list2, icon_list1)
          temp += survive(bar_list1, [])
          temp += connect(axes.coords_to_point(13.1, 1.6), axes.coords_to_point(13.1, 0.8))
          traffic(0.8, 13.1, 18, temp, "#12cc66", 0.07, False)

          self.wait(2)
          self.play(*[FadeOut(i) for i in dahsed_list + icon_list1 + icon_list2 + bar_list1 + bar_list2 + bar_list3])

          ########Traffic arrives
          bar_list1 = []
          bar_list2 = []
          bar_list3 = []
          icon_list1 = []
          icon_list2 = []
          dahsed_list = []

          def bare_traffic(thread, start, end, temp = [], color = "#029ffa", width = 0.07):
            box = Rectangle(height= 0.001, width=width, color=WHITE, stroke_width=0).move_to(axes.coords_to_point(start, thread), aligned_edge=UP)
            box.set_fill(color, opacity=1)
            height = axes.coords_to_point(start)[1] - axes.coords_to_point(end)[1]

            self.add(box)
            self.play(*temp, box.animate.set_height(height, about_edge=UP, stretch=True), run_time=0.5)

            if thread == 0.8:
              bar_list1.append(box)
            elif thread == 1.6:
              bar_list2.append(box)
            else:
              bar_list3.append(box)
            return box

          def traffic_jam(thread, start, end, dimish_list, color = "#df2c14", width = 0.07, icon = True):
            box = Rectangle(height= 0.001, width=width, color=WHITE, stroke_width=0).move_to(axes.coords_to_point(start, thread), aligned_edge=UP)
            box.set_fill(color, opacity=1)
            height = axes.coords_to_point(start)[1] - axes.coords_to_point(end)[1]

            self.add(box)
            self.play(*dimish_list, box.animate.set_height(height, about_edge=UP, stretch=True), run_time=0.5)
            bl = SVGMobject("cube.svg", stroke_width = 0).scale(0.12).move_to(axes.coords_to_point((start + end) / 2, thread)).shift(RIGHT*0.25)
            # self.play(ShowCreation(traffic1))
            bl2 = bl.copy().move_to(axes.coords_to_point((start + end) / 2, thread)).shift(RIGHT*0.25)

            if icon:
              self.play(FadeIn(bl), run_time=0.5)
              self.play(bl.animate.shift(UP*0.2), bl2.animate.shift(DOWN*0.2), run_time=0.5)

            if thread == 0.8:
              bar_list1.append(box)
            elif thread == 1.6:
              bar_list2.append(box)
              icon_list1.append(bl)
              icon_list1.append(bl2)
            else:
              bar_list3.append(box)
              icon_list2.append(bl)
              icon_list2.append(bl2)
            return box

          traffic(0.8, 0, 3.1, [], "#12cc66", 0.1, False)
          temp = diminish(bar_list1, [])

          temp += connect(axes.coords_to_point(3.1, 0.8), axes.coords_to_point(3.1, 1.6))
          traffic_jam(1.6, 3.1, 9.1, temp, "#df2c14")
          bare_traffic(1.6, 9.1, 9.3)
          temp = diminish(bar_list2, icon_list1)
          temp += connect(axes.coords_to_point(9.3, 1.6), axes.coords_to_point(9.3, 2.4))
          traffic_jam(2.4, 9.3, 15.3, temp, "#df2c14")
          bare_traffic(2.4, 15.3, 15.5)

          temp = diminish(bar_list3, icon_list2)
          temp += survive(bar_list2, icon_list1)
          temp += connect(axes.coords_to_point(15.5, 2.4), axes.coords_to_point(15.5, 1.6))

          bare_traffic(1.6, 15.5, 15.7, temp)

          temp = diminish(bar_list2, icon_list1)
          temp += survive(bar_list1, [])
          temp += connect(axes.coords_to_point(15.7, 1.6), axes.coords_to_point(15.7, 0.8))
          traffic(0.8, 15.7, 18, temp, "#12cc66", 0.07, False)

          self.play(FadeOut(m1), FadeOut(m2), FadeOut(client), FadeOut(label1), FadeOut(label2), FadeOut(label3), FadeOut(allts), FadeOut(axes), *[FadeOut(i) for i in dahsed_list + icon_list1 + icon_list2 + bar_list1 + bar_list2 + bar_list3])

          ###################### GNN
          self.remove(section)
          section = Text("3 . Graph Neural Network", font=the_font, font_size=30).move_to([-6, 3.5, 0], aligned_edge=LEFT)
          self.add(section)

          nodes = []
          edges = []
          labels = []
          def node(x, y, z):
            c = Circle()
            c.set_fill(BLUE, opacity=1)
            c.set_stroke(WHITE, width=1)
            c.set_width(0.1)
            c.move_to([x, y, 0])
            nodes.append(c)

            label = Text(str(z), font_size=30, font=the_font)
            if (z != "Frontend"):
              label.move_to(c).shift(DOWN * 0.2)
            else:
              label.move_to(c).shift(UP * 0.2)
            labels.append(label)

          def edge(a, b):
            l = Line(a, b, stroke_width=1)
            edges.append(l)

          node(0, 0, "Frontend")
          node(-3, -1, "Cart")
          node(-3, -2, "Recommendation")
          node(-2, -3, "Product")
          node(2, -3, "Shipping")
          node(3, -1.5, "Currency")

          edge(nodes[0], nodes[1])
          edge(nodes[0], nodes[2])
          edge(nodes[0], nodes[3])
          edge(nodes[0], nodes[4])
          edge(nodes[0], nodes[5])
          edge(nodes[2], nodes[3])

          graph = VGroup(VGroup(*edges, *nodes, *labels), Text("Online Boutique", font=the_font, t2s={"Online Boutique": ITALIC}))
          graph.arrange(DOWN, buff=0.4)
          graph.scale(0.7).move_to([-2, 0, 0])
          self.add(graph)

          node_matrix = Matrix([["1"], ["2"], ["1"], ["4"], ["3"]])
          node_matrix.get_brackets().set_color(GREEN, PURPLE)
          node_feature = VGroup(Text("Input Node Features:", font=the_font), node_matrix).arrange(RIGHT)
          edge_matrix = Matrix([["145"], ["298"], ["153"], ["621"], ["465"], ["412"]])
          edge_matrix.get_brackets().set_color(BLUE)
          edge_feature = VGroup(Text("Input Edge Features:", font=the_font), edge_matrix).arrange(RIGHT)

          features = VGroup(node_feature, edge_feature).arrange(DOWN)
          features.next_to(graph).scale(0.5)
          self.play(FadeIn(node_feature))

          self.play(FadeIn(edge_feature))

          all_cs = []
          all_lines = []
          def layer(x, color=BLUE):
            circles = VGroup()
            for i in range(x):
              c = Circle()
              c.set_fill(color, opacity=1)
              c.set_stroke(WHITE, width=1)
              c.set_width(0.2)
              circles.add(c)
            circles.arrange(DOWN, buff=0.15)
            all_cs.append(circles)
            return circles

          def cross(a, b):
            temp = VGroup()
            for i in a:
              for j in b:
                l = Line(i, j, stroke_color=WHITE, opacity=0.5, stroke_width = 0.1)
                temp.add(l)
                all_lines.append(l)
            return temp

          a = layer(5, GREEN)
          b = layer(5, GREEN)
          b.next_to(a, buff=1.6)
          f = layer(5, GREEN)
          f.next_to(b, buff=1.6)

          c = layer(6)
          d = layer(6)
          e = layer(6)
          d.next_to(c, buff=1)
          e.next_to(d, buff=1)
          g = layer(5, PURPLE)
          g.next_to(e, buff = 1)

          add_lines = VGroup()
          f1 = layer(5, ORANGE)
          f2 = layer(1, ORANGE)
          final = VGroup(f1, f2)
          final.arrange(RIGHT).shift(RIGHT * 2.5)

          node_label = Text("Node pooling", font=the_font, font_size=20)
          greens = VGroup(node_label, VGroup(a, b, f, cross(a, b), cross(b, f)))
          greens.arrange(DOWN)
          node_label.shift(RIGHT * 1.6)

          edge_label = Text("Edge pooling", font=the_font, font_size=20)
          blues = VGroup(edge_label, VGroup(c, d, e, g, cross(c, d), cross(d, e), cross(e, g)))
          blues.arrange(DOWN)
          edge_label.shift(RIGHT*1)

          gnn1 = VGroup(greens, blues)
          gnn1.arrange(DOWN, buff=0.5, aligned_edge=LEFT)

          count = 0
          for i in (g):
            temp = Line(i, f1[count], stroke_color=WHITE, opacity=0.5, stroke_width = 0.1)
            add_lines.add(temp)
            all_lines.append(temp)
            count += 1

          count = 0
          for i in (f):
            temp = Line(i, f1[count], stroke_color=WHITE, opacity=0.5, stroke_width = 0.1)
            add_lines.add(temp)
            all_lines.append(temp)
            count += 1

          big_gnn = VGroup(*all_cs, *all_lines)
          big_gnn.shift(RIGHT * 1.5)

          #######################
          #######################
          graph.generate_target()
          graph.target.scale(0.6)
          graph.target.shift(LEFT * 2)

          for i, j in [(node_matrix, a), (edge_matrix, c)]:
            i.generate_target()
            i.target.scale(0.83)
            i.target.next_to(j, aligned_edge=RIGHT).shift(LEFT * 1)

          self.remove(features)
          self.play(MoveToTarget(graph), MoveToTarget(node_matrix), MoveToTarget(edge_matrix))

          projection = Text("Projection", font=the_font, font_size=20)
          addtext = Text("Add", font=the_font, font_size=20)
          addtext.move_to(add_lines).rotate(-62 * DEGREES)
          addtext.shift(UP * 1.5 + RIGHT * 0.2)
          projection.move_to(g).rotate(-8 * DEGREES)
          projection.shift(LEFT * 0.6 + UP * 1)
          latency = Text("Latency", font=the_font, font_size=20)
          latency.move_to(f2).shift(0.5* RIGHT)
          self.play(ShowCreation(greens), ShowCreation(blues), FadeIn(projection))

          self.wait(2)
          fuck = cross(f1, f2)
          final = VGroup(f1, f2, fuck)
          self.play(ShowCreation(final), ShowCreation(add_lines), FadeIn(addtext), FadeIn(latency))
          self.play(*[FadeOut(i) for i in [big_gnn, node_label, edge_label, latency, addtext, projection, final, graph, edge_matrix, node_matrix]])

          ###################### GNN
          self.remove(section)
          section = Text("5. Results", font=the_font, font_size=30).move_to([-6, 3.5, 0], aligned_edge=LEFT)
          self.add(section)

          axes = NumberPlane(
              x_range=[0, 13, 1],
              y_range=[0, 5, 1],
              height = 3,
              width = 4.9,
              tips=False,
              faded_line_ratio = 4,
              color="#000000",
              axis_config={"include_numbers": False},
              background_line_style={
                  "stroke_color": BLACK,
                  "stroke_width": 2,
                  "stroke_opacity": 0.6
              },
              faded_line_style={
                "stroke_color": BLACK,
              }
            ).rotate(-90 * DEGREES, about_edge=IN).shift(LEFT*4)

          client = axes.get_graph(
            lambda x: 0.8,
            color="#B2BEB5",
            stroke_width=0.2,
            opacity=0.5,
          )

          ms_list = []
          for i in range(6):
            ms_list.append(axes.get_graph(
              lambda x: 0.8 + 0.8 * i,
              color="#B2BEB5",
              stroke_width = 0.2,
            ))

          def traffic(thread, start, end, dimish_list = [], color = "#029ffa", width = 0.07, icon = True):
            box = Rectangle(height= 0.001, width=width, color=WHITE, stroke_width=0).move_to(axes.coords_to_point(start, thread), aligned_edge=UP)
            box.set_fill(color, opacity=1)
            height = axes.coords_to_point(start)[1] - axes.coords_to_point(end)[1]

            self.add(box)
            self.play(*dimish_list, box.animate.set_height(height, about_edge=UP, stretch=True), run_time=0.1)

            if thread == 0.8:
              bar_list1.append(box)
            elif thread == 1.6:
              bar_list2.append(box)
            else:
              bar_list3.append(box)
            return box

          def add_text(content, thread, a = 0):
            client_t = Text(content, font=the_font, font_size=10).move_to(axes.coords_to_point(0, thread), aligned_edge=DOWN).shift(UP*0.13).rotate(20*DEGREES)
            self.add(client_t)
            return client_t

          label1 = add_text("Frontend", 0.8)
          label2 = add_text("Cart", 4.8, 0.5)
          label3 = add_text("Recommendation", 2.4)
          label4 = add_text("Product", 3.2, 0.5)
          label5 = add_text("Shipping", 4)
          label6 = add_text("Currency", 1.6, 0.5)

          self.play(FadeIn(axes))
          self.play(*[ShowCreation(i) for i in ms_list])
          def diminish(bar_list, icon_list = []):
            animation_list = []
            for box in bar_list:
              box.generate_target()
              box.target.set_width(0.02, about_edge=UP, stretch=True)
              box.target.set_opacity(0.5)
              animation_list.append(MoveToTarget(box))
            for bl in icon_list:
              bl.generate_target()
              bl.target.set_opacity(0.5)
              bl.target.scale(0.8)
              animation_list.append(MoveToTarget(bl))
            return animation_list

          def survive(bar_list, icon_list):
            animation_list = []
            for box in bar_list:
              box.generate_target()
              box.target.set_width(0.07, about_edge=UP, stretch=True)
              box.target.set_opacity(1)
              animation_list.append(MoveToTarget(box))
            for bl in icon_list:
              bl.generate_target()
              bl.target.set_opacity(1)
              bl.target.scale(1.25)
              animation_list.append(MoveToTarget(bl))
            return animation_list

          def connect(start, end):
            dashed = DashedLine(start, end, color="#FFFFFF", stroke_width=0.5)
            dahsed_list.append(dashed)
            return [ShowCreation(dashed)]

          ########Traffic arrives
          bar_list1 = []
          bar_list2 = []
          bar_list3 = []
          bar_list4 = []
          bar_list5 = []
          bar_list6 = []

          icon_list1 = []
          icon_list2 = []
          dahsed_list = []
          traffic(0.8, 0, 1.1, [], "#12cc66", 0.1, False)
          temp = connect(axes.coords_to_point(1.1, 0.8), axes.coords_to_point(1.1, 1.6))
          traffic(1.6, 1.1, 2.1, temp)
          temp = connect(axes.coords_to_point(2.1, 1.6), axes.coords_to_point(2.1, 0.8))
          traffic(0.8, 2.1, 2.6, temp, "#12cc66")
          temp = connect(axes.coords_to_point(2.6, 0.8), axes.coords_to_point(2.6, 2.4))
          traffic(2.4, 2.6, 3.6, temp)
          temp = connect(axes.coords_to_point(3.6, 2.4), axes.coords_to_point(3.6, 0.8))
          traffic(0.8, 3.6, 4.1, temp, "#12cc66")
          temp = connect(axes.coords_to_point(4.1, 0.8), axes.coords_to_point(4.1, 3.2))
          traffic(3.2, 4.1, 4.6, temp)
          temp = connect(axes.coords_to_point(4.6, 3.2), axes.coords_to_point(4.6, 4))
          traffic(4, 4.6, 5.6, temp)
          temp = connect(axes.coords_to_point(5.6, 4), axes.coords_to_point(5.6, 3.2))
          traffic(3.2, 5.6, 6.1, temp)
          temp = connect(axes.coords_to_point(6.1, 3.2), axes.coords_to_point(6.1, 0.8))
          traffic(0.8, 6.1, 6.6, temp, "#12cc66")
          temp = connect(axes.coords_to_point(6.6, 0.8), axes.coords_to_point(6.6, 4))
          traffic(4, 6.6, 7.6, temp)
          temp = connect(axes.coords_to_point(7.6, 4), axes.coords_to_point(7.6, 0.8))
          traffic(0.8, 7.6, 8.1, temp, "#12cc66")
          temp = connect(axes.coords_to_point(8.1, 0.8), axes.coords_to_point(8.1, 4))
          traffic(4, 8.1, 9.1, temp)
          temp = connect(axes.coords_to_point(9.1, 4), axes.coords_to_point(9.1, 0.8))
          traffic(0.8, 9.1, 9.6, temp, "#12cc66")
          temp = connect(axes.coords_to_point(9.6, 0.8), axes.coords_to_point(9.6, 4.8))
          traffic(4.8, 9.6, 10.6, temp)
          temp = connect(axes.coords_to_point(10.6, 4.8), axes.coords_to_point(10.6, 0.8))
          traffic(0.8, 10.6, 11.1, temp, "#12cc66")
          temp = connect(axes.coords_to_point(11.1, 0.8), axes.coords_to_point(11.1, 1.6))
          traffic(1.6, 11.1, 12.1, temp)
          temp = connect(axes.coords_to_point(12.1, 1.6), axes.coords_to_point(12.1, 0.8))
          traffic(0.8, 12.1, 13, temp, "#12cc66")

          self.wait(2)

          def create_axes(k8, label="K8 autoscaler"):
            axes = NumberPlane(
              x_range=[0, 176, 10],
              y_range=[0, 2000, 100],
              height = 3,
              width = 10,
              tips=True,
              faded_line_ratio = 4,
              # color= "#f5f5f5",
              color="#000000",
              axis_config={"include_numbers": False},
              # x_axis_config = {"include_ticks": True},
              background_line_style={
                  "stroke_color": BLACK,
                  "stroke_width": 2,
                  "stroke_opacity": 0.6
              },
              faded_line_style={
                "stroke_color": BLACK,
              }
              # y_axis_config={"scaling": LogBase(custom_labels=True)},
            )
            label = Text(label, font=the_font, font_size=20)
            label.move_to(axes.coords_to_point(20, 1800))
            label200 = Text("2000", font=the_font, font_size=20)
            label200.move_to(axes.coords_to_point(0, 2000)).shift(LEFT * 0.15).rotate(90 * DEGREES)
            label300 = Text("300", font=the_font, font_size=20)
            label300.move_to(axes.coords_to_point(0, 300)).shift(LEFT * 0.15).rotate(90 * DEGREES)

            xlabel = Text("Time (s)", font=the_font, font_size=20)
            xlabel.move_to(axes.coords_to_point(90, 0)).shift(DOWN * 0.35)
            ylabel = Text("Query per second", font=the_font, font_size=20)
            ylabel.move_to(axes.coords_to_point(0, 1000)).shift(LEFT * 0.35).rotate(90 * DEGREES)

            label50 = Text("50", font=the_font, font_size=20)
            label50.move_to(axes.coords_to_point(50, 0)).shift(DOWN * 0.15)
            label100 = Text("100", font=the_font, font_size=20)
            label100.move_to(axes.coords_to_point(100, 0)).shift(DOWN * 0.15)
            label150 = Text("150", font=the_font, font_size=20)
            label150.move_to(axes.coords_to_point(150, 0)).shift(DOWN * 0.15)

            return VGroup(axes, label, label200, label300, xlabel, ylabel, label50, label100, label150)

          color_list = ["#ed7d31",
                        "#ababab",
                        "#9e480e",
                        "#255e91",
                        "#ffc000"]
          def animations(axes, k8):
            animations = []
            axes = axes[0]
            for i in range(5):
              temp = axes.get_graph(
                lambda x: k8[i][int(x * len(k8[i]) / 177)],
                color=color_list[i],
                stroke_width=2,
                opacity=1,
              )
              animations.append(ShowCreation(temp))
            return animations

          k8 = pickle.load(open("k8.txt", 'rb'))
          php = pickle.load(open("php.txt", 'rb'))
          axes1 = create_axes(k8)
          axes2 = create_axes(php, "pHPA")

          ax = VGroup(axes1, axes2)
          ax.arrange(DOWN, buff=1)
          ax.scale(0.7)

          legend = VGroup()
          text_list = ["Cart", "Recommendation", "Product", "Shipping", "Currency"]
          for i in range(5):
            temp = Rectangle(0.2, 0.1, stroke_width=0)
            temp.set_fill(color_list[i], opacity=1)
            ttext = Text(text_list[i], font_size=20, font=the_font)
            each = VGroup(temp, ttext)
            each.arrange(RIGHT, buff=0.4)
            legend.add(each)
          legend.arrange(RIGHT, buff=0.6).scale(0.7)
          legend.move_to(ax, aligned_edge=DOWN)
          legend_and_ax = VGroup(ax, legend)
          legend_and_ax.arrange(DOWN, buff=0.3)

          legend_and_ax.next_to(axes).shift(RIGHT)
          # big = VGroup(axes, legend_and_ax)
          # big.arrange(RIGHT, buff=1)
          self.add(legend_and_ax)

          animations1 = animations(axes1, k8)
          animations2 = animations(axes2, php)
          self.play(*animations1, *animations2)

          self.wait(2)

          for i in self.mobjects:
            self.remove(i)

          ###############Questions
          self.remove(section)
          # section = Text("6. Questions", font=the_font, font_size=30).move_to([-6, 3.5, 0], aligned_edge=LEFT)
          # self.add(section)

          img = ImageMobject("balloon.jpg").scale(2.4).shift(DOWN * 0.7)
          self.add(img)

          self.wait(1)
          self.remove(img)

          jerry_photo = ImageMobject("jerry.jpg").set_width(1.5)
          jerry = Group(jerry_photo, Text("Hao Shen", font=the_font, font_size=30)).arrange(DOWN)
          william_photo = ImageMobject("william.jpg").set_width(1.5)
          william = Group(william_photo, Text("Chengfei Wu", font=the_font, font_size=30)).arrange(DOWN)
          zhaoqing_photo = ImageMobject("zhaoqing.jpg").set_width(1.5)
          zhaoqing = Group(zhaoqing_photo, Text("Zhaoqing Wu", font=the_font, font_size=30).set_color_by_gradient(BLUE, PURPLE, GREEN)).arrange(DOWN)
          yuan_photo = ImageMobject("yuan.jpg").set_width(1.5)
          yuan = Group(yuan_photo, Text("Yuan Tian", font=the_font, font_size=30)).arrange(DOWN)

          si_ren_bang = Group(yuan, jerry, william, zhaoqing)
          si_ren_bang.arrange(RIGHT, buff=0.5)

          self.add(si_ren_bang)
          self.play(*[i.animate.shift(UP * 0.4) for i in [jerry_photo, william_photo, zhaoqing_photo, yuan_photo]], rate_func=there_and_back, run_time=0.8)

          self.wait(5)
          # ##############
          # while True:
          #   self.wait(10)

TextExample().construct()


from django.test import SimpleTestCase, Client
from opentracing.mocktracer import MockTracer
from django.conf import settings
import opentracing
import mock

from signalfx_tracing.libraries.django_.instrument import (
    config,
    get_middleware_and_setting_name,
)
from signalfx_tracing import instrument

from .conftest import DjangoTestSuite

class TestDjangoOpenTracingMiddleware(SimpleTestCase, DjangoTestSuite):
    def test_set_global_tracer_middleware_sanity(self):
        config.set_global_tracer = True
        config.tracer_callable = "opentracing.mocktracer.MockTracer"
        config.tracer_parameters = dict(scope_manager=None)
        instrument(django=True)

        client = Client()
        client.get("/one/")
        tracer = opentracing.tracer
        span = tracer.finished_spans().pop()

        assert span.tags["path"] == "/one/"
        assert span.tags["method"] == "GET"

        assert settings.OPENTRACING_TRACER_CALLABLE == config.tracer_callable
        assert settings.OPENTRACING_TRACER_PARAMETERS == dict(scope_manager=None)
        assert settings.OPENTRACING_TRACER._tracer is opentracing.tracer

    def test_unset_global_tracer_middleware_sanity(self):
        config.set_global_tracer = False
        tracer = MockTracer()
        instrument(tracer, django=True)
        client = Client()
        client.get("/one/")

        span = tracer.finished_spans().pop()
        assert span.tags["path"] == "/one/"
        assert span.tags["method"] == "GET"

        assert not hasattr(settings, "OPENTRACING_TRACER_CALLABLE")
        assert not hasattr(settings, "OPENTRACING_TRACER_PARAMETERS")
        assert settings.OPENTRACING_TRACER.tracer is tracer
        assert opentracing.tracer is not tracer

    def test_middleware_untraced(self):
        client = Client()
        with mock.patch("opentracing.span.Span.set_tag") as set_tag:
            client.get("/one/")
            assert set_tag.called is False

        assert not hasattr(settings, "OPENTRACING_TRACED_ATTRIBUTES")
        assert not hasattr(settings, "OPENTRACING_TRACER_CALLABLE")
        assert not hasattr(settings, "OPENTRACING_TRACER_PARAMETERS")
        assert not hasattr(settings, "OPENTRACING_TRACE_ALL")
        assert not hasattr(settings, "OPENTRACING_SET_GLOBAL_TRACER")
        assert not hasattr(settings, "OPENTRACING_TRACING")
        assert not hasattr(settings, "OPENTRACING_TRACER")
        _, setting = get_middleware_and_setting_name()
        assert config.middleware_class not in getattr(settings, setting)


from argparse import ArgumentParser
from pathlib import Path

import medianizer.app_logger as app_logger
from medianizer.file_location import FileLocation
from medianizer.metadata import Metadata

logger = app_logger.get_logger(__name__)

def parse_args():
    parser = ArgumentParser()
    parser.add_argument('--path', help='Specify the path of the media files',
                        required=True)
    parser.add_argument('--from-name', required=False, action='store_true',
                        help='Try to fetch creation date from name. By default'
                             ', creation date fetched from metadata only')
    return parser.parse_args()

class Executor:
    def __init__(self, path, date_from_name=None):
        self.path = path
        self.date_from_name = date_from_name
        self.location = FileLocation(self.path)
        self.metadata = Metadata()

    def process_image(self):
        for image in self.location.locate_jpg_files(self.path):
            try:
                with open(image, 'rb') as img:
                    date = self.metadata.fetch_date(img, self.date_from_name)
                    file_name = f'{date["date"]}-{date["date_time"]}.jpg'
                    new_image_path = Path(self.path, 'medianizer_sorted',
                                          date['year'], date['month'],
                                          file_name).resolve()
                    if image.name == file_name:
                        continue
                    logger.info(f'Rename file {image} to {file_name}')
                    if self.location.verify_path(new_image_path):
                        logger.info('The image with the same metadata '
                                    'already exists in the dest path. Moving '
                                    f'the {image} to "manual_sort" directory')
                        file_name = f'duplicate_of_{file_name}'
                        image.rename(image.parent/file_name)
                        self.location.media_sort(image.parent/file_name,
                                                 manual_sort=True)
                        continue
                    image.rename(image.parent/file_name)
                    self.location.media_sort(image.parent/file_name,
                                             dir=new_image_path.parent)
            except IOError:
                logger.error(f'Unable to open the {image} file.')
            except KeyError:
                logger.error(f'Unable to read the {image} EXIF metadata. '
                             'Moving the image to the "manual_sort" directory')
                self.location.media_sort(image, manual_sort=True)

def main():
    args = parse_args()
    executor = Executor(args.path, args.from_name)
    logger.info('Start medianizer app')
    if not executor.location.verify_path(args.path):
        raise ValueError('The given path does not exist')
    logger.info(f'The given path has been found: {args.path}')
    executor.process_image()

if __name__ == '__main__':
    main()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os

from setuptools import find_packages, setup

try:
    here = os.path.dirname(os.path.abspath(__file__))
    description = open(os.path.join(here, "README")).read()
except OSError:
    description = ""

version = "0.0"

with open(os.path.join(here, "requirements.txt")) as f:
    dependencies = f.read().splitlines()

dependency_links = []

setup(
    name="talos",
    version=version,
    description="Performance testing framework for Windows, Mac and Linux.",
    long_description=description,
    classifiers=[
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 2 :: Only",
    ],
    # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
    author="Mozilla Foundation",
    author_email="tools@lists.mozilla.org",
    url="https://wiki.mozilla.org/Buildbot/Talos",
    license="MPL",
    packages=find_packages(exclude=["ez_setup", "examples", "tests"]),
    include_package_data=True,
    package_data={
        "": [
            "*.config",
            "*.css",
            "*.gif",
            "*.htm",
            "*.html",
            "*.ico",
            "*.js",
            "*.json",
            "*.manifest",
            "*.php",
            "*.png",
            "*.rdf",
            "*.sqlite",
            "*.svg",
            "*.xml",
            "*.xul",
        ]
    },
    zip_safe=False,
    install_requires=dependencies,
    dependency_links=dependency_links,
    entry_points="""
      # -*- Entry points: -*-
      [console_scripts]
      talos = talos.run_tests:main
      talos-results = talos.results:main
      """,
    test_suite="tests",
)


'''

'''

# Enter your code here. Read input from STDIN. Print output to STDOUT

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

def nCr(n,r):
    return factorial(n)/(factorial(r)*factorial(n-r))

def solve(n,k):
    if n == 1:
        return k
    else:
        return (k-1) * solve(n-1,k)

t = int(input())
for i in range(t):
    n,k = map(int,input().split())
    print(solve(n,k)%(10**9+7))


"""Speak the note name."""

import sl4a
import time
import random
def randNote():
 return chr(int((random.random()*10000000000)**2)%7+1+64)

def randSolfege():
 solfegenote=['do','di','ra','re','ri','me','mi','fa','fi','se','so','si','le','la','li','ti']
 return solfegenote[int((random.random()*10000000000)**2)%16]

char1 = randSolfege()
char2 = randSolfege()
while True:
 time.sleep(2)
 droid = sl4a.Android()
 while char1==char2:
  char2 = randSolfege()
 print(char1)
# if char1=='B.':
#  char1='Be'
# if char1=='E.':
#  char1='ea'
 droid.ttsSpeak(char1)
 char1=char2


import json

lt = [
    {'name': '王静', 'age': '7', 'height': 130},
    {'name': '闫晓红', 'age': 13, 'height': 160},
    {'name': '刘惠芬', 'age': 16, 'height': 158},
    {'name': '赵铁柱', 'age': 20, 'height': 170}
]

string = json.dumps(lt, ensure_ascii=False)

# print(string)
obj = json.loads(string)

print(type(obj))


import redis
import time

conn = redis.Redis()

while True:

    cities = conn.keys()

    for city in cities:

        d = conn.hgetall(city)

        for ref in d:
            if int(d[ref]) > 1:
                count = int(d[ref])
                count -= 1
                d[ref] = str(count)

        conn.hmset(city,d)

    time.sleep(2)


# // Given a MxN matrix where each element can either be 0 or 1.
# We need to find the shortest path between a given source cell to a
# destination cell. The path can only be created out of a cell if its
# value is 1.

# // Sample input:
# // {{1, 1, 1, 1},
# //  {1, 0, 0, 1},
# //  {1, 1, 1, 1}}

# //  {0,0} -> {2, 1}

# // Sample output:
# // 3

from collections import deque

def isValid(grid, x, y):
    if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] != 0:
        return True
    else:
        return False

def minDistance(grid, start, end):
    # empty conditions
    if not start or not end or not grid or not grid[0]:
        return -1
    # validity check
    if not(isValid(grid, start[0], start[1])) or not(isValid(grid, end[0], end[1])):
        return -1
    # trivial
    if start == end:
        return 0
    # breadth first search
    q = deque([(start[0], start[1], 0)])
    visited = set()
    visited.add((start[0], start[1]))
    while q:
        x, y, depth = q.popleft()
        if x == end[0] and y == end[1]:
            return depth
        else:
            for i, j in ((x+1, y), (x-1, y), (x, y+1), (x, y-1)):
                if isValid(grid, i, j) and (i, j) not in visited:
                    q.append((i, j, depth+1))
                    visited.add((i, j))
    return -1

A = [[1, 0, 1, 1],
     [1, 1, 0, 1],
     [1, 1, 1, 1]]

s = (0, 0)
e = (2, 1)

print(minDistance(A, e, s))


#! ust/bin/python
# -*- coding: utf-8 -*-

class _Node(object):

    def __init__(self, key, val):
        self.key = key
        self.val = val
        self.left = None
        self.right = None

class BST(object):

    def __init__(self):
        self.__root = None

    def get(self, key):
        return self.__get(self.__root, key)

    def __get(self, x, key):
        """Find the key's value and return it in the tree based on x as root
        node.

        Args:
            x: root node.
            key: the corresponding key.

        Returns:
            x.val: node's value.
        """
        if x == None: return None
        if key < x.key:
            return self.__get(x.left, key)
        elif key > x.key:
            return self.__get(x.right, key)
        else:
            return x.val

    def put(self, key, val):
        self.__root = self.__put(self.__root, key, val)

    def __put(self, x, key, val):
        """Find the key and update the value in the tree based on x as root
        node, else insert the key and value to the tree.

        Args:
            x: root node.
            key: the corresponding key.
            value: the corresponding value.

        Returns:
            x: root node.
        """
        if x == None: return _Node(key, val)
        if key < x.key:
            x.left = self.__put(x.left, key, val)
        elif key > x.key:
            x.right = self.__put(x.right, key, val)
        else:
            x.val = val
        return x

    def contains(self, key):
        if self.get(key):
            return True
        else:
            False

    def keys(self):
        arr = []

        def preorder(node):
            if node is not None:
                arr.append(node.key)
                preorder(node.left)
                preorder(node.right)

        preorder(self.__root)
        return arr

if __name__ == "__main__":

    st = BST()
    minlen = int(input('> min length of the word: '))

    print('> searching tale.txt...')
    with open('../test-data/tale.txt') as f:
        for dataIn in f.readlines():
            for word in dataIn.strip().split():
                if len(word) < minlen: continue
                if not st.contains(word):
                    st.put(word, 1)
                else:
                    st.put(word, st.get(word)+1)

    max = ''
    st.put(max, 0)
    for word in st.keys():
        if st.get(word) > st.get(max):
            max = word
    print('> most frequent word: %s %s' % (max, st.get(max)))


# -*- coding: cp936 -*-
import os
import shutil
import datetime

fileWritedHeadline=open("headline.txt",'w')
def las2txt(filePath_forwardLog,filePath_txtLog):
        fileOpened_LasLog=open(filePath_forwardLog,'r')
        fileWrited_txtLog=open(filePath_txtLog.lower().replace(".las",".txt"),'w')
        lineIndex=0
        flag=0
        logSeriesList=[]
        firstMeet = 1  ## 标志第一空行不写
        numOfseriers = 0
        valueList =[]
        for line in fileOpened_LasLog.readlines():
            lineIndex+=1
            if flag==1 and line.startswith('~'): ##结束读取~c段
                flag=4
            if line.startswith('~C'): ##开始读取~c段
                flag=1
            if  flag==1 and (not (line.startswith('~') or line.startswith('#') )): ##写Headline
                splitLine=line.split()
                logSeriesList.append(splitLine[0])

            if line.startswith('~A'):  #开始写数据
                flag=2
            if  flag==2 :  #开始写测井系类头数据
                temp='\t'.join(logSeriesList)
                fileWrited_txtLog.write(temp+'\n')
                flag=3
                numOfseriers = len(logSeriesList)

            if  flag==3 and (not (line.startswith('~') or line.startswith('#') )): #开始写数据行
                splitLine=line.split()
                valueList.extend(splitLine)
                if len(valueList) == numOfseriers:
                    fileWrited_txtLog.write('\t'.join(valueList)+'\n')
                    valueList=[]

        fileWritedHeadline.write(os.path.basename(filePath_forwardLog)+"\t"+'\t'.join(logSeriesList)+"\n")
        fileWrited_txtLog.close()
        print(filePath_forwardLog+"convert to txtlog complete.")

if __name__=="__main__":
    starttime = datetime.datetime.now()

    sourceDirPath="log_las"
    goalDirPath='log_txt'

    print ('prepare las to ',goalDirPath)
    if os.path.exists(goalDirPath):
        shutil.rmtree(goalDirPath)
    os.mkdir(goalDirPath)

    ##  把操作目录下文件存入filenameslist
    fileNames=os.listdir(sourceDirPath)
    for wellItem in fileNames:
        print ('Las2txt...'+'-'*10,wellItem)
        fileOpened=sourceDirPath+'\\'+wellItem
        fileWrited=goalDirPath+'\\'+wellItem
        las2txt(fileOpened,fileWrited)

    fileWritedHeadline.close()
    #long running
    endtime = datetime.datetime.now()
    print ("耗时(秒):" + str((endtime - starttime).seconds))
    print("处理完毕")


import sqlite3

class DataBase:
    def __init__(self):
        print("test")

    def __retrieve(self, query):
        connection = sqlite3.connect(self.__db_CPU_Load.db)
        cursor = connection.cursor()
        cursor.execute(query)
        rows = cursor.fetchall()
        count = 0
        results = {}
        for row in rows:
            results[count] = dict(zip([c[0] for c in cursor.description], row))
            count += 1
        connection.close()
        return  results

    def get_last(self, value="10"):
        if not value.isnumeric():
           value = 10
        else:
            value = abs(int(value))
        query = f"SELECT * FROM {self.CPULoad}"\
                f"  ORDER BY created_at DESC"\
            f"  LIMIT {value}"
        return self.__retrieve(query)

    def get_in_last(self, value="1", period="MINUTES"):
        period = period.upper()
        if period not in (
            'SECONDS', 'MINUTES', 'HOURS', 'DAYS'
            'WEEKS', 'MONTHS', 'YEARS'):
            period = 'MINUTES'

        if not value.isnumeric():
           value = -1
        else:
           value = -abs(int(value))
        query = f"SELECT * FROM {self.CPULoad}"\
                f"  WHERE created_at > datetime('now', '{value} {period}')"\
                f"  ORDER BY created_at DESC"
        return self.__retrieve(query)


from abc import ABC, abstractmethod
import torch
from .loss import build_loss

class TaskHead(torch.nn.Module, ABC):

    def __init__(self):
        torch.nn.Module.__init__(self)

    @abstractmethod
    def forward(self, images, texts):
        pass

    @abstractmethod
    def compute_loss(self, source, target=None, labels=None):
        pass

class Classification(TaskHead):

    def __init__(self, loss, embed_dim=512, num_classes=3):
        super().__init__()
        self.classification_head = torch.nn.Linear(embed_dim, num_classes)
        self.softmax = torch.nn.Softmax(dim=1)
        self.loss = loss

    def forward(self, composed):
        return self.classification_head(composed)

    def probabilities(self, composed):
        logits = self(composed)
        return self.softmax(logits)

    def compute_loss(self, source, target=None, labels=None):
        logits = self(source)
        preds = torch.argmax(logits, dim=1)
        accuracy = (preds == labels).sum().item() / len(labels)
        loss_value = self.loss(logits, labels)
        metrics = {"loss": loss_value.item(),
                   "accuracy": accuracy}
        return loss_value, metrics

class Regression(TaskHead):

    def __init__(self, loss, embed_dim=512):
        super().__init__()
        self.regression_head = torch.nn.Linear(embed_dim, 1)
        self.loss = loss

    def forward(self, composed):
        return torch.sigmoid(self.regression_head(composed))

    def compute_loss(self, source, target=None, labels=None):
        output = self(source)
        loss_value = self.loss(output, labels.float())
        metrics = {"loss": loss_value.item()}
        return loss_value, metrics

class NormalizationLayer(torch.nn.Module):
    """Class for normalization layer."""

    def __init__(self, normalize_scale=1.0, learn_scale=True):
        super().__init__()
        self.norm_s = torch.log(torch.FloatTensor([normalize_scale]))
        if learn_scale:
            self.norm_s = torch.nn.Parameter(self.norm_s)
        self.epsilon = 1e-9

    def forward(self, x):
        norm = torch.norm(x, dim=1, keepdim=True).expand_as(x)
        factor = torch.exp(self.norm_s)
        features = factor * x / (norm + self.epsilon)
        return features

class Metric(TaskHead):

    def __init__(self, loss, initial_normalization_factor=4.0):
        super().__init__()
        self.loss = loss
        self.normalization_layer = NormalizationLayer(
            normalize_scale=initial_normalization_factor, learn_scale=True)

    def forward(self, composed):
        return self.normalization_layer(composed)

    def compute_loss(self, source, target, labels=None):
        source_emb = self.forward(source)
        target_emb = self.forward(target)

        assert source_emb.shape[1] == target_emb.shape[1]
        loss_value = self.loss(source_emb, target_emb, labels=labels)
        metrics = {"loss": loss_value.item()}
        if torch.isnan(loss_value):
            import IPython; IPython.embed()
        return loss_value, metrics

def get_task_head(cfg):
    loss_obj, task = build_loss(cfg)

    if task == "metric":
        head = Metric(loss_obj, cfg.MODEL.INITIAL_NORMALIZATION_FACTOR)
    elif task == "regression":
        head = Regression(loss_obj, cfg.MODEL.EMBED_DIM)
    else:
        head = Classification(loss_obj, embed_dim=cfg.MODEL.EMBED_DIM,
                              num_classes=cfg.DATASET.NUM_CLASSES)

    return head, task


from ..utils.utils import *

from ..models.zephir import ZephIR
from ..zephod.model import ZephOD

def build_models(
    container,
    dimmer_ratio,
    grid_shape,
    fovea_sigma,
    n_chunks,):
    """Compile PyTorch models for tracking.

    Models are built according to user arguments. Zephod is only built when a
    compatible model is available.

    :param container: variable container, needs to contain: dataset, allow_rotation,
    dev, img_shape, n_frame, shape_n
    :param dimmer_ratio: Zephir parameter, dims descriptor edges relative to center
    :param grid_shape: Zephir parameter, descriptor size
    :param fovea_sigma: Zephir parameter, size of foveated region
    :param n_chunks: Zephir parameter, number of chunks to divide forward pass into
    :return: container (updated entry for: grid_shape), zephir, zephod
    """
    # pull variables from container
    dataset = container.get('dataset')
    allow_rotation = container.get('allow_rotation')
    dev = container.get('dev')
    img_shape = container.get('img_shape')
    n_frame = container.get('n_frame')
    shape_n = container.get('shape_n')

    z_stacks = min(img_shape[0], grid_shape[0])
    grid_shape = (z_stacks - (z_stacks+1) % 2, grid_shape[1], grid_shape[2])
    grid_spacing = tuple(np.array(grid_shape) / np.array(img_shape))

    # models and loss function compiled
    print(f'\nCompiling models and loss function...')
    model_kwargs = {
        'allow_rotation': allow_rotation,
        'dimmer_ratio': dimmer_ratio,
        'fovea_sigma': fovea_sigma,
        'grid_shape': grid_shape,
        'grid_spacing': grid_spacing,
        'n_chunks': n_chunks,
        'n_frame': n_frame,
        'shape_n': shape_n,
        'ftr_ratio': 0.6,
        'ret_stride': 2,
    }
    zephir = ZephIR(**model_kwargs)

    # compiling Zephod models from existing kwargs and state_dict
    zephod_path = dataset / 'zephod.pt'
    if not zephod_path.is_file():
        zephod_path = Path(__file__).parent.parent / 'zephod' / 'models.pt'
    if zephod_path.is_file():
        try:
            zephod_checkpoint = torch.load(str(zephod_path))
        except RuntimeError:
            print('*** CUDA NOT AVAILABLE! Mapping Zephod parameters to CPU...')
            zephod_checkpoint = torch.load(str(zephod_path), map_location=torch.device('cpu'))
        zephod = ZephOD(**zephod_checkpoint['model_kwargs'])
        zephod.load_state_dict(zephod_checkpoint['state_dict'])
        zephod.to(dev)
    else:
        print('******* ERROR: Zephod checkpoint file not found!')
        zephod = None

    # push variables to container
    container.update({
        'grid_shape': grid_shape,
    })

    # push models to checkpoint
    update_checkpoint(dataset, {
        'zephir_kwargs': model_kwargs,
        'zephir': zephir,
        'zephod': zephod
    })

    return container, zephir, zephod


"""
django-sage-painless - UWSGI Config Generator

:author: Mehran Rahmanzadeh (mrhnz13@gmail.com)
"""
import os
import time

from django.conf import settings

# Base
from sage_painless.services.abstract import AbstractUWSGIGenerator

# Helpers
from sage_painless.utils.git_service import GitSupport
from sage_painless.utils.jinja_service import JinjaHandler
from sage_painless.utils.json_service import JsonHandler
from sage_painless.utils.timing_service import TimingService

from sage_painless import templates

class UwsgiGenerator(AbstractUWSGIGenerator, JinjaHandler, JsonHandler, TimingService, GitSupport):
    """generate uwsgi config"""
    UWSGI_TEMPLATE = 'uwsgi.jinja'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def generate(self, diagram_path, git_support=False):
        """generate uwsgi.ini
        template:
            sage_painless/templates/uwsgi.jinja
        """
        start_time = time.time()

        diagram = self.load_json(diagram_path)

        config = self.extract_uwsgi_config(diagram)  # get uwsgi config from diagram

        if git_support:
            self.init_repo(settings.BASE_DIR)

        # generate uwsgi.ini
        self.stream_to_template(
            output_path=f'{settings.BASE_DIR}/uwsgi.ini',
            template_path=os.path.abspath(templates.__file__).replace('__init__.py', self.UWSGI_TEMPLATE),
            data={
                'config': config
            }
        )

        if git_support:
            self.commit_file(
                f'{settings.BASE_DIR}/uwsgi.ini',
                f'deploy (uwsgi): Create uwsgi config file'
            )

        end_time = time.time()
        return True, 'uwsgi config generated ({:.3f} ms)'.format(self.calculate_execute_time(start_time, end_time))


from django.urls import path, include
from . import views
from django.conf.urls import url
from django.views.generic import TemplateView

REACT_ROUTES = [
    'service-list',
    'think-bank',
    'post/',
    r'^post/(?P<user>\d+)/(?P<id>\d+)/$',
    'userbank/',
    r'^userbank/(?P<userbankid>\d+)/$',
]

urlpatterns = [
    path('', views.index),
    url(r'^(%s)?$' % '|'.join(REACT_ROUTES),
        TemplateView.as_view(template_name='frontend/index.html')),
]


import requests
import json

def challenge3(page):
    url = "https://www.python-spider.com/api/challenge3"
    payload = f"page={page}"
    headers = {
        'cookie': 'm=74ccb88b480335eac2a340f0f3846979|1675243619000'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.text

def run():
    data_num = 0
    for page in range(1, 101):
        response_text = challenge3(page)
        res_dict = json.loads(response_text)
        data_list = res_dict.get('data')
        print(data_list)
        for data in data_list:
            data_num += int(data.get('value'))
        print(data_num)
    print(data_num)

if __name__ == '__main__':
    run()

猿人学练习/3js加密js第六节实战例题/main.py


import random
import time
class node:
      def __init__(self,data):
            self.data = data
            self.right = None
            self.left = None
def insert(root,data):
      if root == None:
            return node(data)
      else:
            curr = root
            if curr.data > data:
                  curr = insert(root.left,data)
                  root.left = curr
            else:
                  curr = insert(root.right,data)
                  root.right = curr
      return root
def printLevelOrder(root):
      if root == None:
            return
      else:
            h = height(root)
            print("\nTree Height is: ",h)
            print("\nLevel Order Traversal : \n")
            for i in range(1,h+1):
                  printLevel(root,i)
def height(root):
      if root is None:
            return 0
      else:
            l = height(root.left)
            r = height(root.right)
            if l < r:
                  return r+1
            else:
                  return l+1
def printLevel(root,level):
      if root is None:
            return
      if level is 1:
            # time.sleep(.1)
            print(root.data,end=" ")
      elif level > 1:
            printLevel(root.left,level-1)
            printLevel(root.right,level-1)

#main
n = int(input("enter size: "))
root = None
print("elements are: ")
for i in range(n):
      val = random.randint(1,n*10)
      root = insert(root,val)
      print(val,end=" ")

printLevelOrder(root)


from ideadatamodel import (
    constants,
    exceptions
)

import ideaadministrator
from ideaadministrator.app.cdk.stacks import IdeaBaseStack

from ideaadministrator.app.cdk.constructs import (
    ExistingSocaCluster
)

from typing import Optional
import aws_cdk as cdk
import constructs
from aws_cdk import (
    aws_aps as aps,
    aws_cloudwatch as cloudwatch
)

class MetricsStack(IdeaBaseStack):

    def __init__(self, scope: constructs.Construct,
                 cluster_name: str,
                 aws_region: str,
                 aws_profile: str,
                 module_id: str,
                 deployment_id: str,
                 termination_protection: bool = True,
                 env: cdk.Environment = None):
        super().__init__(
            scope=scope,
            cluster_name=cluster_name,
            aws_region=aws_region,
            aws_profile=aws_profile,
            module_id=module_id,
            deployment_id=deployment_id,
            termination_protection=termination_protection,
            description=f'ModuleId: {module_id}, Cluster: {cluster_name}, Version: {ideaadministrator.props.current_release_version}',
            tags={
                constants.IDEA_TAG_MODULE_ID: module_id,
                constants.IDEA_TAG_MODULE_NAME: constants.MODULE_METRICS,
                constants.IDEA_TAG_MODULE_VERSION: ideaadministrator.props.current_release_version
            },
            env=env
        )

        self.cluster = ExistingSocaCluster(self.context, self.stack)

        self.cloudwatch_dashboard: Optional[cloudwatch.Dashboard] = None
        self.amazon_prometheus_workspace: Optional[aps.CfnWorkspace] = None

        if self.is_cloudwatch():
            self.build_cloudwatch()
        elif self.is_amazon_managed_prometheus():
            self.build_amazon_managed_prometheus()
        elif self.is_prometheus():
            self.build_prometheus()
        else:
            raise exceptions.general_exception(f'metrics provider: {self.get_metrics_provider()} not supported')

        self.build_cluster_settings()

    def get_metrics_provider(self) -> str:
        return self.context.config().get_string('metrics.provider', required=True)

    def is_cloudwatch(self) -> bool:
        return self.get_metrics_provider() == constants.METRICS_PROVIDER_CLOUDWATCH

    def is_amazon_managed_prometheus(self) -> bool:
        return self.get_metrics_provider() == constants.METRICS_PROVIDER_AMAZON_MANAGED_PROMETHEUS

    def is_prometheus(self) -> bool:
        return self.get_metrics_provider() == constants.METRICS_PROVIDER_PROMETHEUS

    def build_cloudwatch(self):
        dashboard_name = self.context.config().get_string('metrics.cloudwatch.dashboard_name', required=True)
        # todo - add widgets based on modules deployed
        self.cloudwatch_dashboard = cloudwatch.Dashboard(
            scope=self.stack,
            id='cloudwatch-dashboard',
            dashboard_name=dashboard_name
        )

    def build_amazon_managed_prometheus(self):
        workspace_name = self.context.config().get_string('metrics.amazon_managed_prometheus.workspace_name', required=True)
        self.amazon_prometheus_workspace = aps.CfnWorkspace(
            scope=self.stack,
            id='prometheus-workspace',
            alias=workspace_name
        )
        self.add_common_tags(self.amazon_prometheus_workspace)

    def build_prometheus(self):
        # validate and do nothing as of now.
        # might need additional configurations to provision secrets to authenticate with remote write url
        self.context.config().get_string('metrics.prometheus.remote_write.url', required=True)
        self.context.config().get_string('metrics.prometheus.query.url', required=True)

    def build_cluster_settings(self):
        cluster_settings = {
            'deployment_id': self.deployment_id
        }

        if self.is_cloudwatch():
            cluster_settings['cloudwatch.dashboard_arn'] = self.cloudwatch_dashboard.dashboard_arn
        elif self.is_amazon_managed_prometheus():
            cluster_settings['amazon_managed_prometheus.workspace_id'] = self.amazon_prometheus_workspace.attr_workspace_id
            cluster_settings['amazon_managed_prometheus.workspace_arn'] = self.amazon_prometheus_workspace.attr_arn
            cluster_settings['prometheus.remote_write.url'] = f'{self.amazon_prometheus_workspace.attr_prometheus_endpoint}api/v1/remote_write'
            cluster_settings['prometheus.remote_read.url'] = f'{self.amazon_prometheus_workspace.attr_prometheus_endpoint}api/v1/query'
        elif self.is_prometheus():
            pass

        self.update_cluster_settings(cluster_settings)