from queue import Queue
from sys import maxsize
from linked_list.doubly_linked_list import print_list
from logging import RootLogger

class Queue:

    def __init__(self):
        self.items = []

    def enqueue(self, item):
        self.items.insert(0, item)

    def dequeue(self):
        if not self.is_empty():
            return self.items.pop()
        return False

    def peek(self):
        if not self.is_empty():
            return self.items[-1].data

    def is_empty(self):
        return len(self.items) == 0

    def __len__(self):
        return len(self.items)

class Stack:

    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(object)

    def len(self):
        return len(self.items)

    def is_empty(self):
        return True if len(self.items) == 0 else False

    def pop(self):
        if not self.is_empty():
            return self.items.pop()

class Dist_Node:

    def __init__(self, node, distance):
        self.node = node
        self.distance = distance

class  Node:
    def __init__(self, value):
        self.data = value
        self.left = None
        self.right = None

    def __str__(self):
        return str(self.data)

class BinaryTree:

    def __init__(self, root):
        self.root = Node(root)

    def pre_order_traversal(self, root):
        if root:
            print(root.data)
            self.pre_order_traversal(root.left)
            self.pre_order_traversal(root.right)

    def in_order_traversal(self, root):
        if root:
            self.in_order_traversal(root.left)
            print(root.data)
            self.in_order_traversal(root.right)

    def post_order_traversal(self, root):
        if root:
            self.post_order_traversal(root.left)
            self.post_order_traversal(root.right)
            print(root.data)

    def max(self,root):
        if not root:
            return 0
        if root.left==None and root.right==None:
            return root.data
        l=self.max(root.left)
        r=self.max(root.right)
        return max(root.data, max(l,r))

    def min(self,root):
        if not root:
            return maxsize
        if root.left==None and root.right==None:
            return root.data
        l=self.min(root.left)
        r=self.min(root.right)
        return min(root.data, min(l,r))

    def get_level(self, data):
        return self.get_level_(self.root, data, 1)

    def get_level_(self, node, data, level):
        if not node:
            return 0
        if node.data == data:
            return level
        downlevel = self.get_level_(node.left, data, level + 1)
        if downlevel != 0:
            return downlevel
        downlevel = self.get_level_(node.right, data, level + 1)
        return downlevel

    def nodes_at_given_level(self,root,level):
        if not root:
            return
        if level==0:
            print(root.data)
        self.nodes_at_given_level(root.left, level-1)
        self.nodes_at_given_level(root.right, level-1)

    def in_order_traversal_iterative(self, root):
        curr_node = root
        stack = []
        while True:
            if curr_node:
                stack.append(curr_node)
                curr_node = curr_node.left
            elif stack:
                curr_node = stack.pop()
                print(curr_node.data)
                curr_node = curr_node.right
            else:
                break

    def pre_order_traversal_iterative(self, root):
        curr_node = root
        stack = []
        while True:
            if curr_node:
                print(curr_node.data)
                stack.append(curr_node)
                curr_node = curr_node.left
            elif stack:
                curr_node = stack.pop()
                curr_node = curr_node.right
            else:
                break

    def post_order_traversal_iterative(self, root):
        curr_node = root
        stack1 = []
        stack2 = []
        while True:
            if curr_node:
                stack2.append(curr_node.data)
                stack1.append(curr_node)
                curr_node = curr_node.right
            elif stack1:
                curr_node = stack1.pop()
                curr_node = curr_node.left
            else:
                break
        while stack2:
            print(stack2.pop())

    def reverse_pre_order_traversal_iterative(self, root):
        curr_node = root
        stack = []
        while True:
            if curr_node:
                print(curr_node.data)
                stack.append(curr_node)
                curr_node = curr_node.right
            elif stack:
                curr_node = stack.pop()
                curr_node = curr_node.left
            else:
                break

    def level_order_traversal(self, root):
        if root:
            queue = Queue()
            queue.enqueue(root)
            traversal = ""
            while len(queue) > 0:
                node = queue.dequeue()
                print(node.data)
                if node.left:
                    queue.enqueue(node.left)
                if node.right:
                    queue.enqueue(node.right)
            return traversal

    def reverseLevelOrder(self, root):
        if root:
            another_queue = Queue()
            queue = Queue()
            queue.enqueue(root)

            while(len(queue) > 0):

                root = queue.dequeue()
                another_queue.append(root)

                if (root.right):
                    queue.append(root.right)

                if (root.left):
                    queue.append(root.left)

            while(len(another_queue) > 0):
                root = another_queue.pop()
                print(root.data),

    def left_view(self, root):
        if root:
            queue = []
            queue.append(root)
            while queue:
                count = len(queue)
                queueCount = len(queue)
                while count > 0:
                    element = queue.pop()
                    if count == queueCount:
                        print(element.data)
                    if element.left:
                        queue.insert(0, element.left)
                    if element.right:
                        queue.insert(0, element.right)
                    count -= 1

    def right_view(self, root):
        if root:
            queue = []
            queue.append(root)
            while queue:
                count = len(queue)
                while count > 0:
                    element = queue.pop()
                    if count == 1:
                        print(element.data)
                    if element.left:
                        queue.insert(0, element.left)
                    if element.right:
                        queue.insert(0, element.right)
                    count -= 1

    def bottom_view(self, root):
        if root:
            queue = []
            distMap = {}
            queue.insert(0, Dist_Node(root, 0))
            while queue:
                element = queue.pop()
                distMap[element.distance] = element.node.data
                if element.node.left:
                    queue.insert(0, Dist_Node(element.node.left, element.distance - 1))
                if element.node.right:
                    queue.insert(0, Dist_Node(element.node.right, element.distance + 1))
            for index in sorted(distMap):
                print(distMap[index])

    def top_view(self, root):
        if root:
            queue = []
            distMap = {}
            queue.append(Dist_Node(root, 0))
            while queue:
                element = queue.pop()
                if not element.distance in distMap:
                    distMap[element.distance] = element.node.data
                if element.node.left:
                    queue.append(Dist_Node(element.node.left, element.distance - 1))
                if element.node.right:
                    queue.append(Dist_Node(element.node.right, element.distance + 1))

            for index in sorted(distMap):
                print(distMap[index])

    def vertical_order_traversal(self, root):
        if root:
            distMap = {}
            queue = []
            queue.append(Dist_Node(root, 0))
            while queue:
                dist_node = queue.pop()
                if dist_node.distance in distMap:
                    distMap[dist_node.distance].append(dist_node.node)
                else:
                    distMap[dist_node.distance] = [dist_node.node]
                if dist_node.node.left:
                    queue.append(Dist_Node(dist_node.node.left, dist_node.distance - 1))
                if dist_node.node.right:
                    queue.append(Dist_Node(dist_node.node.right, dist_node.distance + 1))

            for key in sorted(distMap):
                val = distMap[key]
                for value in val:
                    print(value)
                print()
#             for key,value in sorted(distMap).items():
#                 for i in value:
#                     print(i)
#                 print()

    def right_diagonal_traversal(self, root):
        if root:
            queue = []
            map = {}
            queue.insert(0, Dist_Node(root, 0))
            while queue:
                element = queue.pop()
                if element.distance in map:
                    map[element.distance].append(element.node.data)
                else:
                    map[element.distance] = [element.node.data]
                if element.node.left:
                    queue.insert(0, Dist_Node(element.node.left, element.distance + 1))
                if element.node.right:
                    queue.insert(0, Dist_Node(element.node.right, element.distance))
            for index in sorted(map):
                print(map[index])

    def left_diagonal_traversal(self, root):
        if root:
            queue = []
            map = {}
            queue.insert(0, Dist_Node(root, 0))
            while queue:
                element = queue.pop()
                if element.distance in map:
                    map[element.distance].append(element.node.data)
                else:
                    map[element.distance] = [element.node.data]
                if element.node.left:
                    queue.insert(0, Dist_Node(element.node.left, element.distance))
                if element.node.right:
                    queue.insert(0, Dist_Node(element.node.right, element.distance + 1))
            for index in sorted(map):
                print(map[index])

    def height(self, root):
        if root is None:
            return -1
        left_height = self.height(root.left)
        right_height = self.height(root.right)
        return 1 + max(left_height, right_height)

    def height_(self, root):
        if not root:
            return 0
        return max(self.height_(root.left), self.height_(root.right)) + 1

    def size_recursive(self, root):
        if root is None:
            return 0
        left_size = self.size_recursive(root.left)
        right_size = self.size_recursive(root.right)
        return 1 + left_size + right_size

    def size_iterative(self):
        if self.root is None:
            return 0
        queue = Queue()
        queue.enqueue(self.root)
        size = 0
        while len(queue) > 0:
            node = queue.dequeue()
            size += 1
            if node.left:
                queue.enqueue(node.left)
            if node.right:
                queue.enqueue(node.right)
        return size

    def number_of_leaf_nodes(self, root):
        if root:
            count = 0
            queue = []
            queue.insert(0, root)
            while queue:
                element = queue.pop()
                if not element.left and not element.right:
                    count += 1
                if element.left:
                    queue.insert(0, element.left)
                if element.right:
                    queue.insert(0, element.right)
            return count

    def print_leaf_nodes(self, root):
        if root:
            queue = []
            queue.insert(0, root)
            while queue:
                element = queue.pop()
                if element.left == None and element.right == None:
                    print(element.data)
                if element.left:
                    queue.insert(0, element.left)
                if element.right:
                    queue.insert(0, element.right)

    def print_left_except_leaf(self, root):
        if not root:
            return
        if root.left == None and root.right == None:
            return
        print(root.data)
        self.print_left_except_leaf(root.left)

    def print_right_except_leaf_reverse(self, root):
        if not root:
            return
        if root.left == None and root.right == None:
            return
        self.print_right_except_leaf_reverse(root.right)
        print(root.data)

    def sum_of_all_nodes_in_tree(self, root):
        if root:
            queue = []
            queue.insert(0, root)
            sum = 0
            while queue:
                element = queue.pop()
                sum += element.data
                if element.left:
                    queue.insert(0, element.left)
                if element.right:
                    queue.insert(0, element.right)
            return sum

    def sprial_order_traversal(self, root):
        if not root:
            return
        s1 = []
        s2 = []
        s1.append(root)
        while s1 or s2:
            while s1:
                elem = s1.pop()
                print(elem.data)
                if elem.left:
                    s2.append(elem.left)
                if elem.right:
                    s2.append(elem.right)

            while s2:
                elem = s2.pop()
                print(elem.data)
                if elem.right:
                    s1.append(elem.right)
                if elem.left:
                    s1.append(elem.left)

    def boundary_traversal(self, root):
        self.print_left_except_leaf(root)
        self.print_leaf_nodes_(root)
        self.print_right_except_leaf_reverse(root.right)

    def print_leaf_nodes_(self, root):
        if not root:
            return
        if root.left == None and root.right == None:
            print(root.data)
            return
        self.print_leaf_nodes_(root.left)
        self.print_leaf_nodes_(root.right)

    def mirror(self, root):
        if not root:
            return
        self.mirror(root.left)
        self.mirror(root.right)
        root.left, root.right = root.right, root.left

    def root_to_leaf_with_given_sum(self, root, sum , list):
        if not root:
            return
        if root.left == None and root.right == None:
            if root.data == sum:
                list.append(root.data)
                return True
            else:
                return False
        if self.root_to_leaf_with_given_sum(root.left, sum - root.data, list):
            list.append(root.data)
            return True, list
        if self.root_to_leaf_with_given_sum(root.right, sum - root.data, list):
            list.append(root.data)
            return True, list
        return False

    def print_all_paths_from_root_to_leaf(self, root, stack):
        if not root:
            return
        stack.append(root)
        if not root.left and not root.right:
            print(''.join([str(i) for i in stack]))
        self.print_all_paths_from_root_to_leaf(root.left, stack)
        self.print_all_paths_from_root_to_leaf(root.right, stack)
        stack.pop()

    def is_identical(self, root1, root2):
        if not root1 and not root2:
            return True
        elif root1 and root2:
            if root1.data == root2.data and self.is_identical(root1.left, root2.left) and self.is_identical(root1.right, root2.right):
                return True
        return False

    def is_mirror(self, root1, root2):
        if not root1 and not root2:
            return True
        elif root1 and root2:
            if root1.data == root2.data and self.is_mirror(root1.left, root2.right) and self.is_mirror(root1.right, root2.left):
                return True
        return False

    def to_doubly_linked_list(self,root):
        if not root:
            return
        queue=[]
        queue.insert(0, root)
        head=prev=None
        while queue:
            element=queue.pop()

            curr=Node(element)
            if not prev:
                head=curr
            else:
                curr.left=prev
                prev.right=curr
            prev=curr

            if element.left:
                queue.insert(0, element.left)
            if element.right:
                queue.insert(0, element.right)

        return head

    def max_path_sum(self, root):
        if not root:
            return 0
        l = self.max_path_sum(root.left)
        r = self.max_path_sum(root.right)
        return max(l, r) + root.data

    def isMirror(self, root1 , root2):
        # If both trees are empty, then they are mirror images
        if root1 is None and root2 is None:
            return True

        """ For two trees to be mirror images, the following three
            conditions must be true
            1 - Their root node's key must be same
            2 - left subtree of left tree and right subtree
              of the right tree have to be mirror images
            3 - right subtree of left tree and left subtree
               of right tree have to be mirror images
        """
        if (root1 is not None and root2 is not None):
                if  root1.data == root2.data:
                    return (self.isMirror(root1.left, root2.right)and
                    self.isMirror(root1.right, root2.left))
        return False

    def is_symmetric(self,root):
        if not root:
            return True
        return self.isMirror(root,root)

    def search(self, root, value):
        if not root:
            return False
        if root.data==value:
            return True
        if self.search(root.left, value):
            return True
        if self.search(root.right, value):
            return True
        return False

    def delete_all_nodes(self,root):
        if not root:
            return
        self.delete_all_nodes(root.left)
        self.delete_all_nodes(root.right)
        root=None

    def delete_tree(self, root):
        if not root:
            return
        queue=[]
        queue.insert(0, root)
        while queue:
            element=queue.pop()
            if element.left:
                queue.insert(0, element.left)
            if element.right:
                queue.insert(0, element.right)
            element=None

    def are_siblings(self, root, value1, value2):
        if not root:
            return
        if root.left and root.right:
            if root.left.data==value1 and root.right.data==value2:
                return True
        if self.are_siblings(root.left, value1, value2):
            return  True
        if self.are_siblings(root.right, value1, value2):
            return True

        return False

class BinarySearchTree:

    def __init__(self):
        self.root = None

    def insert (self, data):
        if self.root is None:
            self.root = Node(data)
        else:
            self.insert_(data, self.root)

    def insert_(self, data, curr_node):
        if data < curr_node.data:
            if curr_node.left is None:
                curr_node.left = Node(data)
            else:
                self.insert_(data, curr_node.left)

        elif data > curr_node.data:
            if curr_node.right is None:
                curr_node.right = Node(data)
            else:
                self.insert_(data, curr_node.right)
        else:
            print('data already exist in the tree')

    def find(self, data):
        if self.root:
            found = self.find_(data, self.root)
            return True if found else False

    def find_(self, data, curr_node):
        if curr_node.data == data:
            return True
        if data < curr_node.data and curr_node.left:
            return self.find_(data, curr_node.left)
        if data > curr_node.data and curr_node.right:
            return self.find_(data, curr_node.right)

    def inorder_traversal(self, root, list_):
        if root:
            self.inorder_traversal(root.left, list_)
            list_.append(root.data)
            self.inorder_traversal(root.right, list_)
        return list_

    def inorder_predecessor(self, root, main_root, key):
        if not root:
            return
        if root.data==key:
            if root.left:
                temp=root.left
                while temp.right!=None:
                    temp=temp.right
                print(temp.data)
            else:
                store=main_root
                while root.data!=main_root.data:
                    if root.data>main_root.data:
                        store=main_root
                        main_root=main_root.right
                    else:
                        main_root=main_root.left
                print(store.data)

        elif key<root.data:
            self.inorder_predecessor(root.left, main_root, key)
        else:
            self.inorder_predecessor(root.right, main_root, key)

    def inorder_successor(self, root, main_root, key):
        if not root:
            return
        if root.data==key:
            if root.right:
                temp=root.right
                while temp.left!=None:
                    temp=temp.left
                print(temp.data)
            else:
                store=main_root
                while root.data!=main_root.data:
                    if root.data<main_root.data:
                        store=main_root
                        main_root=main_root.left
                    else:
                        main_root=main_root.right
                print(store.data)
        elif key<root.data:
            self.inorder_successor(root.left, main_root, key)
        else:
            self.inorder_successor(root.right, main_root, key)

    def next_min_node(self,root):
        if not root:
            return
        temp=root
        while temp.left:
            temp=temp.left
        return temp

    def delete_node(self,root,key):
        if not root:
            return
        if key<root.data:
            root.left=self.delete_node(root.left, key)
        elif key>root.data:
            root.right=self.delete_node(root.right, key)
        else:
            if not root.left:
                temp=root.right
                root=None
                return temp
            if not root.right:
                temp=root.left
                root=None
                return temp
            temp=self.next_min_node(root.right)
            root.data=temp.data
            root.right=self.delete_node(root.right, temp.data)
        return root

# bst=BinarySearchTree()
# bst.insert(50)
# bst.insert(30)
# bst.insert(70)
# bst.insert(20)
# bst.insert(40)
# bst.insert(60)
# bst.insert(80)
# bst.insert(38)
# bst.insert(42)
# bst.delete_node(bst.root, 30)
# bst.inorder_predecessor(bst.root, 4)
# bst.inorder_predecessor(bst.root,bst.root,10)
# bst.inorder_successor(bst.root, bst.root, 2)
# print(bst.find(11))
# print(bst.inorder_traversal(bst.root, []))
# tree=BinaryTree(1)
# tree.root.left = Node(2)
# tree.root.right = Node(3)
# tree.root.left.left = Node(4)
# tree.root.left.right = Node(5)
# tree.root.right.left = Node(6)
# tree.root.right.left.right = Node(8)
# tree.root.right.right = Node(7)
# tree.post_order_traversal(tree.root)
# tree.delete_all_nodes(tree.root)
# tree.delete_tree(tree.root)
# print("=====================")
# tree.post_order_traversal(tree.root)

# linkedlist=tree.to_doubly_linked_list(tree.root)
# print_list(linkedlist)
# print(tree.max(tree.root))
# print(tree.min(tree.root))

# tree=BinaryTree(1)
# tree.root.left=Node(2)
# tree.root.right=Node(3)
# tree.root.left.right=Node(4)
# print(tree.min(tree.root))
# print(tree.findMax(tree.root))
# print(tree.search(tree.root, 2))

# tree = BinaryTree(1)
# tree.left = Node(2)
# tree.right = Node(2)
# tree.left.left = Node(3)
# tree.left.right = Node(4)
# tree.right.left = Node(4)
# tree.right.right = Node(5)
# print(tree.is_symmetric(tree.root))

#
tree1=BinaryTree(1)
tree1.root.left = Node(2)
tree1.root.right = Node(3)
tree1.root.left.left = Node(4)
tree1.root.left.right = Node(5)
tree1.root.right.left = Node(6)
tree1.root.right.left.right = Node(8)
tree1.root.right.right = Node(7)
# tree1.delete_tree(tree.root)
# print('max sum = ',tree1.max_path_sum(tree1.root))
# tree1.print_all_paths_from_root_to_leaf(tree1.root, [])
# tree1.nodes_at_given_level(tree1.root, 3)
print(tree1.are_siblings(tree1.root, 5,6))
# print(tree.is_identical(tree.root, tree1.root))

# tree=BinaryTree(10)
# tree.root.left = Node(-2)
# tree.root.right = Node(7)
# tree.root.left.left = Node(8)
# tree.root.left.right = Node(-4)
# print(tree.max_path_sum(tree.root))

# print(tree.root_to_leaf_with_given_sum(tree.root, 18, []))
# print(tree.sum_of_all_nodes_in_tree(tree.root))
# tree.in_order_traversal(tree.root)
# tree.mirror(tree.root)
# print("===================")
# tree.sprial_order_traversal(tree.root)
# tree.in_order_traversal(tree.root)

# tree.boundary_traversal(tree.root)
# tree.print_right_except_leaf_reverse(tree.root)
# tree.right_diagonal_traversal(tree.root)
# print("===================")
# tree.left_diagonal_traversal(tree.root)
# print(tree.size_iterative())
# tree.print_leaf_nodes(tree.root)
# tree.print_leaf_nodes_(tree.root)
# print(tree.number_of_leaf_nodes(tree.root))
# print(tree.size_recursive(tree.root))
# print(tree.get_level(7))
# tree.vertical_order_traversal(tree.root)
# tree.top_view(tree.root)
# tree.bottom_view(tree.root)
# tree.left_view(tree.root)
# tree.right_view(tree.root)
# tree.root.right.left.right = Node(8)
# tree.root.right.right.right = Node(9)
# tree.in_order_traversal_iterative(tree.root)
# tree.pre_order_traversal_iterative(tree.root)
# tree=BinaryTree(1)
# tree.root.left=Node(2)
# tree.root.right=Node(3)
# tree.reverse_pre_order_traversal_iterative(tree.root)
# tree.post_order_traversal_iterative(tree.root)

# # print(tree.size_recursive(tree.root))
# print(tree.size_iterative())
# tree.print_vertical_order(tree.root)
# print(tree.height(tree.root))

# tree=BinaryTree(1)
# tree.root.left      = Node(2)
# tree.root.right     = Node(3)
# tree.root.left.left  = Node(4)
# tree.root.left.right  = Node(5)
# # tree.post_order_traversal(tree.root)
# # print(tree.level_order_traversal(tree.root))
# tree.reverseLevelOrder(tree.root)
#
# tree=BinaryTree(1)
# tree.root.left=Node(2)
# tree.root.right=Node(3)
# tree.root.left.left=Node(4)
# tree.root.left.right=Node(5)
# tree.root.right.left=Node(6)
# tree.root.right.right=Node(7)
# tree.root.right.right.right=Node(8)
# tree.pre_order_traversal(tree.root)
# print("=================")
# tree.in_order_traversal(tree.root)
# print("=================")
# tree.post_order_traversal(tree.root)


"""This module provides a model for a monitoring station, and tools
for manipulating/modifying station data

"""

class MonitoringStation:
    """This class represents a river level monitoring station"""

    def __init__(self, station_id, measure_id, label, coord, typical_range,
                 river, town):

        self.station_id = station_id
        self.measure_id = measure_id

        # Handle case of erroneous data where data system returns
        # '[label, label]' rather than 'label'
        self.name = label
        if isinstance(label, list):
            self.name = label[0]

        self.coord = coord
        self.typical_range = typical_range
        self.river = river
        self.town = town

        self.latest_level = None

    def __repr__(self):
        d = "Station name:     {}\n".format(self.name)
        d += "   id:            {}\n".format(self.station_id)
        d += " measure id: {}\n".format(self.measure_id)
        d += "   coordinate:    {}\n".format(self.coord)
        d += "   town:          {}\n".format(self.town)
        d += "   river:         {}\n".format(self.river)
        d += "   typical range: {}".format(self.typical_range)
        return d

    def typical_range_consistent(self):
        """"Checks that stations do not have inconsistent data. checks the typical high/low range data for consistency.
         The method should return True if the data is consistent and False if the data is inconsistent or unavailable"""

        if self.typical_range == None:
            return False
        elif self.typical_range[1] < self.typical_range[0]:
            return False
        else:
            return True

    def relative_water_level(self):
        """returns latest water level as a fraction of the typical range. 1.0 = level at typical high
        0.0 = level at typical low"""

        if not self.typical_range_consistent() or self.latest_level is None or isinstance(self.latest_level,list):
            return None
        else:
            return (self.latest_level - self.typical_range[0])/(self.typical_range[1]-self.typical_range[0])

def check_station_input(stations):
    if type(stations) != list:
        raise TypeError('input was not a list of stations')
    try:
        stations[1].river
    except Exception:
        print("ValueError: Please enter a list of stations")
    pass

def inconsistent_typical_range_stations(stations):
    "given a list of stations objects, returns a list of stations that have inconsistent data."
    check_station_input(stations)
    bad_stations = list()
    for station in stations:
        if station.typical_range_consistent() == False:
            bad_stations.append(station)
    return bad_stations


from pytest import fixture
from deploy_beanstalk import previous_git_commit, parse

@fixture()
def git_log_output():
    return '''commit bc061fc755015162741eec71f1a71ea6c3fdb786
Author: j1z0 <jeremy_johnson@hms.harvard.edu>
Date:   Thu Sep 22 22:23:54 2016 -0400

    we need .aws for both master and production

    '''

def test_parse_git_commit(git_log_outpu):
    author = "j1z0 <jeremy_johnson@hms.harvard.edu>"
    msg = "we need .aws for both master and production"
    expected = "%s - %s" % (author, msg)
    actual = parse(previous_git_commit())

    print("expected result: ", expected)
    print("actual result: ", actual)
    assert expected == actual

if __name__ == "__main__":
    print(".")
    test_parse_git_commit(git_log_output())
    print("all good!")


import os
from shutil import copyfile, copytree
import pandas as pd
import numpy as np
import math
import argparse

arg_parser = argparse.ArgumentParser(
description="Use a trained DeepSDF decoder to reconstruct a shape given SDF samples."
)
arg_parser.add_argument(
"--meshes_dir", "-d", dest="meshes_dir", required=True, help="The data source directory."
)

data_dir = '/home/nugrinovic/datasets/3DPeopleOcc'

subjects = ['all']
subjects_out = ['']

# get subjects from folders
root_path = data_dir

if not 'subjects' in locals():
    subjects = os.listdir(root_path)
    subjects.sort()
    subjects = [c for c in subjects if os.path.isdir(os.path.join(root_path, c))]
if 'all' in subjects:
    subjects = os.listdir(data_dir)
    subjects.sort()
    subjects = [c for c in subjects if os.path.isdir(os.path.join(root_path, c))]

# get actions from folders
source_metadata = []
target_metadata = []

im_by_cam = {}
train = []
val = []
test = []
data = []

epoch = 100
args = arg_parser.parse_args()
meshes_dir = args.meshes_dir
exp_dir = meshes_dir.split('reconstruction/')[0]
eval_dir = os.path.join(exp_dir, 'eval')

for subject in subjects:
    if subject in subjects_out:
        continue
    f = os.path.join(eval_dir, '%d_%s_eval.csv' % (epoch, subject))

    df = pd.read_csv(f)
    # eval_data = df.to_dict()
    eval_by_iou = df.sort_values(by=['iou'])
    # filter around inflection iou=0.77+-0.1
    set = 0.7900
    # delta = 0.02
    # this line added due to .lst data mal generation
    eval_by_iou = eval_by_iou[eval_by_iou['subject_id'].str.startswith(subject )]

    eval_by_iou = eval_by_iou[eval_by_iou['iou'] > set ]

    subs_by_iou = eval_by_iou['subject_id'].tolist()
    iou_sorted = eval_by_iou['iou'].tolist()
    instances = subs_by_iou
    instances.sort()

    subject_path = os.path.join(root_path, subject)
    actions = os.listdir(subject_path)
    actions.sort()
    actions = [c for c in actions if os.path.isdir(os.path.join(subject_path, c))]
    data = []

    print('start to generate list for every instance of subject: %s'%(subject))
    l = len(instances)
    train = ['/'.join(c.split('/')[1:]) for c in instances]
    train = [c for c in train if 'points.npz' in os.listdir(os.path.join(subject_path, c))]

    data.append(train)

    data =  [item for sublist in data for item in sublist]
    # chunks = [data[x:x + 100] for x in range(0, len(data), 100)]

    df_train = pd.DataFrame(data)
    dst = os.path.join(subject_path, 'data_above_07900_iou.lst')
    np.savetxt(dst, df_train.values, fmt='%s')

    # for i, data in enumerate(chunks):
    #     df_train = pd.DataFrame(data)
    #     dst = os.path.join(subject_path, 'data_all_%d.lst'%(i))
    #     np.savetxt(dst, df_train.values, fmt='%s')


from setuptools import setup, find_packages
import os

version = '0.3'

setup(name='z3c.recipe.mkdir',
      version=version,
      description="Buildout recipe to create directories.",
      long_description=open("README.txt").read() + "\n\n" +
                       open(os.path.join("z3c", "recipe", "mkdir",
                                         "README.txt")).read() + "\n\n" +
                       open("CHANGES.txt").read(),
      classifiers=[
        "Development Status :: 3 - Alpha",
        "Framework :: Buildout",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "License :: OSI Approved :: Zope Public License",
        ],
      keywords='buildout directory folder mkdir',
      author='Uli Fouquet',
      author_email='grok-dev@zope.org',
      url='http://pypi.python.org/pypi/z3c.recipe.mkdir',
      license='ZPL 2.1',
      packages=find_packages(exclude=['ez_setup']),
      namespace_packages=['z3c', 'z3c.recipe' ],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          'zc.buildout',
      ],
      extras_require=dict(
        test = [
            'zope.testing',
            ]
        ),
      entry_points="""
      [zc.buildout]
      default = z3c.recipe.mkdir:Recipe
      """,
      )


import unittest
import unittest.mock
import os
import io
import sys

from app import ScreenPrinter, LinkedList, Stack, Queue

class TestScreenPrinter(unittest.TestCase):

    def setUp(self):
        self.capturedOutput = io.StringIO()
        sys.stdout = self.capturedOutput

    def tearDown(self):
        sys.stdout = sys.__stdout__

    def testInit(self):
        for name in ["a", "b", "c", "d", "e"]:
            self.assertEqual(ScreenPrinter(name).name, name)

    def testVisitLogLinkedList(self):

        liste = LinkedList()

        for i in range(5):
            liste.append(i)

        liste.accept(ScreenPrinter(""))

        self.assertEqual(self.capturedOutput.getvalue(), "\n\n(0,1,2,3,4)\n\n")

    def testVisitLogStack(self):

        stack = Stack(10)

        for i in range(5):
            stack.push(i)

        stack.accept(ScreenPrinter(""))

        s="\n-------\n"
        expected = '\n'+s+'   4   '+s+'   3   '+s+'   2   '+s+'   1   '+s+'   0   '+s+'\n'

        self.assertEqual(self.capturedOutput.getvalue(), expected)

    def testVisitLogQueue(self):

        queue = Queue(10)

        for i in range(5):
            queue.enqueue(i)

        queue.accept(ScreenPrinter(""))

        self.assertEqual(self.capturedOutput.getvalue(), "\n\n|0|1|2|3|4|\n\n")

#pour tester ScreenPrinter uniquement
if __name__ == '__main__':
    unittest.main()


import argparse
import pandas as pd
import os
from datetime import datetime

# in progress

def setup_parser(parser):
    subparsers = parser.add_subparsers(help="description of all parsers:", dest="command")

    parser_add = subparsers.add_parser(name="add", help="""Provide task to list ,example(task2.py add -n name1 -dl 2020-02-15 -d exemplary task),
                                                        remember about proper format of date YYYY-MM-DD,and that each string having more than one word
                                                        must be inside quotation mark """)
    parser_add.set_defaults(func=add)
    parser_add.add_argument("-n", "--name", dest="name", help="name", required=True)
    parser_add.add_argument("-dl", "--deadline", dest="deadline", help="deadline YYYY-MM_DD", required=False, type=lambda d: datetime.strptime(d, '%Y-%m-%d'))
    parser_add.add_argument("-d", "--description", dest="description", help="description", required=False)

    parser_update = subparsers.add_parser(name="update", help="""Update chosen task by its  hash value(visible using list function),remember about proper format of date YYYY-MM-DD,
                                                                and that each string having more than one word must be inside quotation mark """)
    parser_update.set_defaults(func=update)
    parser_update.add_argument("-n", "--name", dest="name", help="name", required=False)
    parser_update.add_argument("-dl", "--deadline", dest="deadline", help="deadline YYYY-MM_DD", required=False, type=lambda d: datetime.strptime(d, '%Y-%m-%d'))
    parser_update.add_argument("-d", "--description", dest="description", help="description", required=False)
    parser_update.add_argument("-hs", "--hash", dest="hash", help="hash made by index value", required=True, type=int)

    parser_remove = subparsers.add_parser(name="remove", help="remove task from list using hash value(hash value "
                                                              "visible using list function)")
    parser_remove.add_argument('hash', help='hash value of the task intended for removing ')
    parser_remove.set_defaults(func=remove)

    parser_list = subparsers.add_parser(name="list", help="display tasks in list,options to choose:-all (displaying "
                                                          "all tasks),-today(displaying tasks with today's date)  ")
    parser_list.set_defaults(func=listing)
    parser_list.add_argument("-all", "--all", action='store_true')
    parser_list.add_argument("-today", "--today", action='store_true')

def add(args):
    df = pd.DataFrame({'name': [args.name], 'deadline': [args.deadline], 'description': [args.description]})
    df['hash'] = hash(str(args.name) + str(args.deadline) + str(args.description))
    df.set_index('hash', inplace=True, drop=False)
    with open('task.csv', 'a'):
        if os.path.getsize('task.csv') == 0:
            df.to_csv('task.csv', header=True, index=False)
        else:
            df.to_csv('task.csv', mode='a', header=False, index=False)

def update(args):
    df2 = pd.read_csv('task.csv')
    df2.set_index('hash', inplace=True, drop=False)
    if args.name != None:
        df2.at[args.hash, 'name'] = args.name
    if args.deadline != None:
        df2.at[args.hash, 'deadline'] = args.deadline.date()
    if args.deadline != None:
        df2.at[args.hash, 'description'] = args.description

    df2.to_csv('task.csv', header=True, index=False)

def listing(args):
    if args.all:
        print(pd.read_csv('task.csv'))
    if args.today:
        df = pd.read_csv('task.csv')
        df2 = df[(df['deadline']==str(datetime.today().date()))]
        df2.reset_index(drop=True,inplace=True)
        print(df2)

def remove(args):
    df = pd.read_csv('task.csv')
    df = df[df.hash != int(args.hash)]
    df.to_csv('task.csv',header=True, index=False)

def main():
    parser = argparse.ArgumentParser(description="List of tasks")
    setup_parser(parser)
    res = parser.parse_args()
    res.func(res)

main()


def log_Extractor(file):
    file=open(file,'r')
    f1 = open('error.log', 'w+')
    f2 = open('warning.log', 'w+')
    f3=open('another.log','w+')
    read=file.readlines()
    for i in read:
        if 'ERROR' in i:
            f1.write(i)
        elif 'WARNINGS' in i:
            f2.write(i)
        else:
            f3.write(i)
log_Extractor('test.log')


"""
This is a template for creating custom BatchExpectations.
For detailed instructions on how to use it, please see:
    https://docs.greatexpectations.io/docs/guides/expectations/creating_custom_expectations/how_to_create_custom_batch_expectations
"""

from typing import Dict, Optional

from great_expectations.core.expectation_configuration import ExpectationConfiguration
from great_expectations.exceptions import InvalidExpectationConfigurationError
from great_expectations.execution_engine import (
    ExecutionEngine,
    PandasExecutionEngine,
    SparkDFExecutionEngine,
    SqlAlchemyExecutionEngine,
)
from great_expectations.expectations.expectation import BatchExpectation
from great_expectations.expectations.metrics.metric_provider import (
    MetricConfiguration,
    metric_value,
)
from great_expectations.expectations.metrics.table_metric_provider import (
    TableMetricProvider,
)

# This class defines a Metric to support your Expectation.
# For most BatchExpectations, the main business logic for calculation will live in this class.
class BatchMeetsSomeCriteria(TableMetricProvider):
    # This is the id string that will be used to reference your Metric.
    metric_name = "METRIC NAME GOES HERE"

    # This method implements the core logic for the PandasExecutionEngine
    @metric_value(engine=PandasExecutionEngine)
    def _pandas(
        cls,
        execution_engine,
        metric_domain_kwargs,
        metric_value_kwargs,
        metrics,
        runtime_configuration,
    ):
        raise NotImplementedError

    # @metric_value(engine=SqlAlchemyExecutionEngine)
    # def _sqlalchemy(
    #         cls,
    #         execution_engine,
    #         metric_domain_kwargs,
    #         metric_value_kwargs,
    #         metrics,
    #         runtime_configuration,
    # ):
    #    raise NotImplementedError

    # @metric_value(engine=SparkDFExecutionEngine)
    # def _spark(
    #         cls,
    #         execution_engine,
    #         metric_domain_kwargs,
    #         metric_value_kwargs,
    #         metrics,
    #         runtime_configuration,
    # ):
    #    raise NotImplementedError

    @classmethod
    def _get_evaluation_dependencies(
        cls,
        metric: MetricConfiguration,
        configuration: Optional[ExpectationConfiguration] = None,
        execution_engine: Optional[ExecutionEngine] = None,
        runtime_configuration: Optional[dict] = None,
    ):
        return {
            "table.columns": MetricConfiguration(
                "table.columns", metric.metric_domain_kwargs
            ),
        }

# This class defines the Expectation itself
# The main business logic for calculation lives here.
class ExpectBatchToMeetSomeCriteria(BatchExpectation):
    """TODO: add a docstring here"""

    # These examples will be shown in the public gallery.
    # They will also be executed as unit tests for your Expectation.
    examples = []

    # This is a tuple consisting of all Metrics necessary to evaluate the Expectation.
    metric_dependencies = ("METRIC NAME GOES HERE",)

    # This a tuple of parameter names that can affect whether the Expectation evaluates to True or False.
    success_keys = ()

    # This dictionary contains default values for any parameters that should have default values.
    default_kwarg_values = {}

    def validate_configuration(
        self, configuration: Optional[ExpectationConfiguration]
    ) -> None:
        """
        Validates that a configuration has been set, and sets a configuration if it has yet to be set. Ensures that
        necessary configuration arguments have been provided for the validation of the expectation.

        Args:
            configuration (OPTIONAL[ExpectationConfiguration]): \
                An optional Expectation Configuration entry that will be used to configure the expectation
        Returns:
            None. Raises InvalidExpectationConfigurationError if the config is not validated successfully
        """

        super().validate_configuration(configuration)
        configuration = configuration or self.configuration

        # # Check other things in configuration.kwargs and raise Exceptions if needed
        # try:
        #     assert (
        #         ...
        #     ), "message"
        #     assert (
        #         ...
        #     ), "message"
        # except AssertionError as e:
        #     raise InvalidExpectationConfigurationError(str(e))

    # This method performs a validation of your metrics against your success keys, returning a dict indicating the success or failure of the Expectation.
    def _validate(
        self,
        configuration: ExpectationConfiguration,
        metrics: Dict,
        runtime_configuration: dict = None,
        execution_engine: ExecutionEngine = None,
    ):
        raise NotImplementedError

    # This object contains metadata for display in the public Gallery
    library_metadata = {
        "tags": [],  # Tags for this Expectation in the Gallery
        "contributors": [  # Github handles for all contributors to this Expectation.
            "@your_name_here",  # Don't forget to add your github handle here!
        ],
    }

if __name__ == "__main__":
    ExpectBatchToMeetSomeCriteria().print_diagnostic_checklist()


from transformers import AutoModelForQuestionAnswering, AutoTokenizer

mnames = ['deepset/bert-base-cased-squad2', 'deepset/electra-base-squad2', 'twmkn9/albert-base-v2-squad2', 'deepset/roberta-base-squad2', 'Primer/bart-squad2']

def wer_score(hyp, ref, print_matrix=False):
    import numpy as np
    N = len(hyp)
    M = len(ref)
    L = np.zeros((N,M))
    for i in range(0, N):
        for j in range(0, M):
            if min(i,j) == 0:
                L[i,j] = max(i,j)
            else:
                deletion = L[i-1,j] + 1
                insertion = L[i,j-1] + 1
                sub = 1 if hyp[i] != ref[j] else 0
                substitution = L[i-1,j-1] + sub
                L[i,j] = min(deletion, min(insertion, substitution))
    if print_matrix:
        print("WER matrix ({}x{}): ".format(N, M))
        print(L)
    return int(L[N-1, M-1])

def metrics(fname):
    # BLEU
    from nltk.translate.bleu_score import sentence_bleu, corpus_bleu
    scores = []
    f = open("poc_english.txt", "r")
    f2 = open(fname, "r")
    lines = f.readlines()
    cand = f2.readlines()
    for i in range(len(cand)):
        line = lines[i]
        candidate = []
        l = cand[i].lower().strip('\n')[1:len(cand[i])-2].split(", ")
        for item in l:
            item = item.strip('.').split(" ")
            candidate.append(item)
        arr = line.strip('.\n').split(" ")
        for i in range(len(arr)):
            arr[i] = arr[i].lower()
        reference = [arr]
        for c in candidate:
            # print(reference, c, ': ', sentence_bleu(reference, c, weights=(1,0)))
            scores.append(sentence_bleu(reference, c, weights=(1,0)))

    print("BLEU: " + str(sum(scores)/(1.0*len(scores))))

    # Word2Vec Cosine Similarity
    import torch
    import torch.nn.functional as F
    from sentence_transformers import SentenceTransformer
    import nltk
    from nltk import tokenize
    def similarity(par1, par2):
        transformer = SentenceTransformer('roberta-base-nli-stsb-mean-tokens')
        transformer.eval()
        par1 = tokenize.sent_tokenize(par1)
        vec1 = torch.Tensor(transformer.encode(par1))
        vec1 = vec1.mean(0)
        par2 = tokenize.sent_tokenize(par2)
        vec2 = torch.Tensor(transformer.encode(par2))
        vec2 = vec2.mean(0)
        cos_sim = F.cosine_similarity(vec1, vec2, dim=0)
        return cos_sim.item()

    scores = []
    f = open("poc_english.txt", "r")
    f2 = open(fname, "r")
    lines = f.readlines()
    cand = f2.readlines()
    for i in range(len(cand)):
        line = lines[i]
        candidate = []
        l = cand[i].lower().strip('\n')[1:len(cand[i])-2].split(", ")
        for item in l:
            item = item.strip('.').split(" ")
            candidate.append(item)
        arr = line.strip('.\n').split(" ")
        if (len(arr) == 1):
            continue
        for i in range(len(arr)):
            arr[i] = arr[i].lower()
        reference = arr
        for c in candidate:
            scores.append(similarity(" ".join(reference), " ".join(c)))
    print("Word2Vec Cosine Similarity: " + str(sum(scores)/(1.0*len(scores))))

    # WER
    scores = []
    f = open("poc_english.txt", "r")
    f2 = open(fname, "r")
    lines = f.readlines()
    cand = f2.readlines()
    for i in range(len(cand)):
        line = lines[i]
        candidate = []
        l = cand[i].lower().strip('\n')[1:len(cand[i])-2].split(", ")
        for item in l:
            item = item.strip('.').split(" ")
            candidate.append(item)
        arr = line.strip('.\n').split(" ")
        if (len(arr) == 1):
            continue
        for i in range(len(arr)):
            arr[i] = arr[i].lower()
        reference = arr
        for c in candidate:
            scores.append(wer_score(c, reference))
    print("WER: " + str(sum(scores)/(1.0*len(scores))))

    # TER
    import pyter

    scores = []
    f = open("poc_english.txt", "r")
    f2 = open(fname, "r")
    lines = f.readlines()
    cand = f2.readlines()
    for i in range(len(cand)):
        line = lines[i]
        candidate = []
        l = cand[i].lower().strip('\n')[1:len(cand[i])-2].split(", ")
        for item in l:
            item = item.strip('.').split(" ")
            candidate.append(item)
        arr = line.strip('.\n').split(" ")
        if (len(arr) == 1):
            continue
        for i in range(len(arr)):
            arr[i] = arr[i].lower()
        reference = arr
        for c in candidate:
            scores.append(pyter.ter(reference, c))
    print("TER: " + str(sum(scores)/(1.0*len(scores))))

def run(modelname):
    model = AutoModelForQuestionAnswering.from_pretrained(modelname)
    tokenizer = AutoTokenizer.from_pretrained(modelname)

    from transformers import pipeline
    nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)

    rel_and_food = "A mom is a human. A dad is a human. A mom is a parent. A dad is a parent. A son is a child. A daughter is a child. A son is a human. A daughter is a human. A mom likes cake. A daughter likes cake. A son likes sausage. A dad likes sausage. Cake is a food. Sausage is a food. Mom is a human now. Dad is a human now. Mom is a parent now. Dad is a parent now. Son is a child now. Daughter is a child now. Son is a human now. Daughter is a human now. Mom likes cake now. Daughter likes cake now. Son likes sausage now. Dad likes sausage now. Cake is a food now. Sausage is a food now. Mom was a daughter before. Dad was a son before. Mom was not a parent before. Dad was not a parent before. Mom liked cake before. Dad liked sausage before. Cake was a food before. Sausage was a food before."
    prof = "Mom is on the board of directors. Dad is on the board of directors. Son is on the board of directors. Daughter is on the board of directors. Mom writes with chalk on the board. Dad writes with chalk on the board. Son writes with chalk on the board. Daughter writes with chalk on the board. Dad wants Mom to be on the board of directors. Mom wants Dad to be on the board of directors. Dad wants his son to be on the board of directors. Mom wants her daughter to be on the board of directors. Mom writes to Dad with chalk on the board. Dad writes to Mom with chalk on the board. Son writes to Dad with chalk on the board. Daughter writes to Mom with chalk on the board."
    tools_and_pos = "Mom has a hammer. Mom has a saw. Dad has a hammer. Dad has a saw. Mom has a telescope. Mom has binoculars. Dad has a telescope. Dad has binoculars. Mom saw Dad with a hammer. Mom saw Dad with a saw. Dad saw Mom with a hammer. Dad saw Mom with a saw. Saw is a tool. Hammer is a tool. Binoculars are a tool. A telescope is a tool. Mom sawed the wood with a saw. Dad sawed the wood with a saw. Son sawed the wood with a saw. Daughter sawed the wood with a saw. Mom knocked the wood with a hammer. Dad knocked the wood with a hammer. Son knocked the wood with a hammer. Daughter knocked the wood with a hammer. Mom saw Dad with binoculars. Mom saw Dad with a telescope. Dad saw Mom with binoculars. Dad saw Mom with a telescope."

    f = open("poc_english_queries.txt", "r")
    f2name = modelname.split("/")[1] + ".txt"
    f2 = open(f2name, "w")

    for line in f:
        parts = line.split(" ")
        context = ""
        if "relationships" in parts[0]:
            context = rel_and_food
        elif "tools" in parts[0]:
            context = tools_and_pos
        else:
            context = prof
        question = ""
        for i in range(len(parts)-1):
            question = question + parts[i+1].rstrip() + " "
        question = question[0:len(question)-1] + "?"
        f2.write(nlp({'question': question, 'context': context })['answer'].replace(".",",") + "\n")

    f2.close()

    print(f2name)
    metrics(f2name)
    print('\n')

for m in mnames:
    run(m)


from django.views import View
from django.http import HttpResponse, JsonResponse, HttpResponseForbidden
from django_redis import get_redis_connection
from random import randint

from MD.libs.captcha.captcha import captcha
from .constants import VERIFICATION_CODE_EXPIRE
from MD.utils.response_code import RETCODE
from celery_tasks.sms.tasks import send_sms_code
import logging

logger = logging.getLogger('django')

class VerificationCodeView(View):
    """图形验证码"""

    def get(self, request, uuid):
        name, text, code_image = captcha.generate_captcha()
        # redis_conn = get_redis_connection('verification_code')
        # redis_conn.setex(uuid, VERIFICATION_CODE_EXPIRE, text)
        get_redis_connection('verification_code').setex(f'v_code_{uuid}', VERIFICATION_CODE_EXPIRE, text)
        return HttpResponse(code_image, content_type='image/png')

class SMSCodeView(View):
    """发短信验证码"""

    # url = this.host + '/verification/sms_codes/' + this.mobile + '/?v_code=' + this.image_code + '&uuid=' + this.uuid;
    def get(self, request, phone_num):
        redis_conn = get_redis_connection('verification_code')
        if redis_conn.get('send_flag_%s' % phone_num):
            return JsonResponse({'code': RETCODE.THROTTLINGERR, 'errmsg': '频繁发送短信'})

        # 接收前端传入的数据
        x = request.GET
        v_code = x.get('v_code')
        uuid = x.get('uuid')

        # 校验
        if not all([v_code, uuid]):
            return HttpResponseForbidden('缺少参数')

        redis_v_code = redis_conn.get(f'v_code_{uuid}')
        redis_conn.delete(f'v_code_{uuid}')
        if not redis_v_code:
            return JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '验证码已过期'})
        redis_v_code = redis_v_code.decode()

        if v_code.lower() != redis_v_code.lower():
            return JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '验证码输入错误'})

        # 发送短信验证码
        sms_code = '%06d' % randint(0, 999999)
        logger.info(sms_code)
        # 创建redis管道
        pl = redis_conn.pipeline()
        # 将短信验证码缓存到redis
        pl.setex('sms_%s' % phone_num, 300, sms_code)
        # 当前手机号发过短信后,向redis存储一个发送过的标记
        pl.setex('send_flag_%s' % phone_num, 60, 1)
        # 执行管道
        pl.execute()
        # send_sms_code(phone_num, sms_code)
        send_sms_code.delay(phone_num, sms_code)  # 将发短信的函数内存添加到仓库中,让worker去执行
        # 响应
        return JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})


#!/usr/bin/env python3

import rospy
from std_msgs.msg import Float32, String
from duckietown_msgs.msg import *
from PID_class import PID

class PID_Controller_v2():
    def __init__(self,time):

        self.d_obj = PID(3,0,0,time)
        self.phi_obj = PID(1.5,0,0,time)
        rospy.Subscriber("lane_filter_node/lane_pose", LanePose, self.callback)
        self.pub = rospy.Publisher("car_cmd_switch_node/cmd", Twist2DStamped, queue_size=1)
        self.pub1 = rospy.Publisher("/my_dist_error", Float32, queue_size=10)
        self.pub2 = rospy.Publisher("/my_phi_error", Float32, queue_size=10)
        self.pub3 = rospy.Publisher("/my_desired", Float32, queue_size=10)
        self.pub4 = rospy.Publisher("/lab5_output", String, queue_size=10)
        self.vel = 0.17
        self.count = 0

        #rospy.set_param("/p_d",self.d_obj.kp)
        #rospy.set_param("/i_d",self.d_obj.ki)
        #rospy.set_param("/d_d",self.d_obj.kd)

        #rospy.set_param("/p_phi",self.phi_obj.kp)
        #rospy.set_param("/i_phi",self.phi_obj.ki)
        #rospy.set_param("/d_phi",self.phi_obj.kd)

    def callback(self,data):
        #tmp_d_p = rospy.get_param("/p_d")
        #tmp_d_i = rospy.get_param("/i_d")
        #tmp_d_d = rospy.get_param("/d_d")
        #tmp_phi_p = rospy.get_param("/p_phi")
        #tmp_phi_i = rospy.get_param("/i_phi")
        #tmp_phi_d = rospy.get_param("/d_phi")
        #self.d_obj.changePID(tmp_d_p, tmp_d_i, tmp_d_d)
        #self.phi_obj.changePID(tmp_phi_p, tmp_phi_i, tmp_phi_d)
        rospy.logwarn("FBI ON PATROL")
        if self.d_obj.integral>1000000000:
            self.d_obj.integral = 0
        if self.phi_obj.integral>1000000000:
            self.phi_obj.integral = 0

        d_error = -0.05 - data.d
        phi_error = 0 - data.phi
        timing = rospy.get_rostime()
        temp_time = timing.secs+(timing.nsecs/1000000000)

        temp_time_diff = temp_time - self.d_obj.past_time_stamp
        d_inp = self.d_obj.calculateSignal(d_error,temp_time)
        phi_inp = self.phi_obj.calculateSignal(phi_error,temp_time)
        if self.count>=5: #Logging once every 5 callbacks
            output_str = "time: {:.3f}, time_diff: {:.3f}, d: {:.3f}, phi: {:.3f}, d_input: {:.3f}, phi_input: {:.3f}".format(temp_time, temp_time_diff, data.d, data.phi, d_inp, phi_inp)
            self.pub4.publish(output_str)
            #rospy.loginfo("FBI ON PATROL")
            #rospy.loginfo(("time: {:.3f}, time_diff: {:.3f}, d: {:.3f}, phi: {:.3f}, d_input: {:.3f}, phi_input: {:.3f}".format(temp_time, temp_time_diff, data.d, data.phi, d_inp, phi_inp)))
            self.count = 0
        #elif self.count%3==0:
        #    return
        self.count += 1
        self.pub1.publish(d_error)
        self.pub2.publish(phi_error)
        self.pub3.publish(0)

        vel = self.vel
        om = d_inp + phi_inp
        if om<0.5 and om>-0.5:
            om = 0
        #elif om>2:
        #    om = 2
        #elif om<-2:
        #    om = -2
        if om!=0:
            vel=0

        self.move(vel,om)

    def move(self,vel,om):
        move_msg = Twist2DStamped()
        move_msg.v = vel
        move_msg.omega = om

        self.pub.publish(move_msg)

if __name__ == '__main__':
    try:
        rospy.init_node('pid_control_v2')
        timing = rospy.get_rostime()
        PID_Controller_v2((timing.secs+(timing.nsecs/1000000000)))

        rospy.spin()

    except rospy.ROSInterruptException:
        pass


""" Test that all TFmodels can be constructed. Some of the bigger models are omitted due to slow initialization. """
# pylint: disable=import-error, no-name-in-module, unused-import
# pylint: disable=redefined-outer-name
import pytest

from batchflow.models.tf import LinkNet, UNet, VNet, \
                                FCN8, FCN16, FCN32, \
                                DenseNetFC56, DenseNetFC67, DenseNetFC103, \
                                RefineNet, GCN, DeepLabXS, DeepLabX8, DeepLabX16, \
                                VGG16, VGG19, VGG7, \
                                ResNet18, ResNet34, ResNet50, ResNet101, ResNet152, \
                                ResNeXt18, ResNeXt34, ResNeXt50, ResNeXt101, ResNeXt152, \
                                Inception_v1, InceptionResNet_v2, Inception_v3, Inception_v4, \
                                SqueezeNet, MobileNet, MobileNet_v2, MobileNet_v3, MobileNet_v3_small, \
                                DenseNet121, DenseNet169, DenseNet201, DenseNet264, \
                                ResNetAttention56, ResNetAttention92, \
                                PyramidNet18, PyramidNet34, PyramidNet50, PyramidNet101, \
                                XceptionS, Xception41, Xception64, \
                                EfficientNetB0, EfficientNetB1, EfficientNetB2, EfficientNetB3, \
                                EfficientNetB4, EfficientNetB5, EfficientNetB6, EfficientNetB7, \
                                PSPNet18, PSPNet34, PSPNet50

MODELS_CLF = [
    VGG7, VGG16, VGG19,
    ResNet18, ResNet34, # ResNet50, ResNet101, ResNet152,
    ResNeXt18, ResNeXt34, # ResNeXt50, ResNeXt101, ResNeXt152,
    Inception_v1,
    # InceptionResNet_v2, Inception_v3, Inception_v4, # fail
    SqueezeNet,
    MobileNet, MobileNet_v2, MobileNet_v3, MobileNet_v3_small,
    DenseNet121,  # DenseNet169, DenseNet201, DenseNet264,
    ResNetAttention56, # ResNetAttention92,
    PyramidNet18, PyramidNet34, # PyramidNet50, PyramidNet101,
    XceptionS, Xception41, # Xception64
    EfficientNetB0, EfficientNetB1, # EfficientNetB2, EfficientNetB3, \
    # EfficientNetB4, EfficientNetB5, EfficientNetB6, EfficientNetB7
]

MODELS_SEG = [
    LinkNet,
    UNet,
    VNet,
    FCN8, FCN16, FCN32,
    DenseNetFC56, # DenseNetFC67, DenseNetFC103,
    RefineNet,
    GCN,
    DeepLabXS, DeepLabX8, # DeepLabX16,
    PSPNet18, # PSPNet34, PSPNet50
]

@pytest.fixture()
def base_config_clf():
    """ Fixture to hold default configuration for classification. """
    config = {'inputs/images/shape': (16, 16, 1),
              'inputs/labels/classes': 10,
              'initial_block/inputs': 'images',
              'loss': 'ce'}
    return config

@pytest.fixture()
def base_config_segment():
    """ Fixture to hold default configuration for segmentation. """
    config = {
        'inputs': {'images': {'shape': (16, 16, 1)},
                   'masks': {'name': 'targets', 'shape': (16, 16, 1)}},
        'initial_block': {'inputs': 'images'},
        'loss': 'mse'
    }
    return config

@pytest.mark.slow
@pytest.mark.parametrize('model', MODELS_CLF)
def test_clf(base_config_clf, model):
    """ Test models for classification """
    _ = model(base_config_clf)

@pytest.mark.slow
@pytest.mark.parametrize('model', MODELS_SEG)
def test_seg(base_config_segment, model):
    """ Test models for segmentation """
    _ = model(base_config_segment)


#!/usr/bin/python3
#This script takes input IP's and hostnames or FQDN's from an input file and outputs pairs of IP:FQDN
#There is a list of predefined domains, that are checked used when only a machine name is given without a domain. Can be benefician in company intranet.
import sys, os
import socket
import re

#Domains to check in case only hostname is given and it is not resolved
domains = [".domain.ee", ".google.com", ".google.ee", ".test.local.org"]

#Read in input from a file
def load_input(arg1):
    #print("Starting to read input from: " + full_path)
    with open(arg1, "r") as file:
        line = file.readline()
        while line:
            #print("Starting to process line: " + line.strip())
            identify_input(line.strip())
            line = file.readline()
#Input can be either a hostname or an IP
#Check which one is provided and act accordingly
def identify_input(arg1):
    #arg1 should be either IPv4, hostname or FQDN
    #print("Checking if",arg1,"is IPv4, FQDN or hostname")
    try:
        #Check if it is IP
        socket.inet_aton(arg1)
        #print(arg1, "is an IP")
        reverse_dns_lookup(arg1)
    except socket.error:
        #print("Not an IP:",arg1)
        #Remove trailing .
        if arg1[-1] == ".":
            arg1 = arg1[:-1]
        #Check if it is just a hostname. Example: host
        if re.match(r"^[A-Z][A-Z\d-]{1,63}$", arg1.upper()) is not None:
            #print(arg1.upper(),"is a hostname")
            resolve_hostname(arg1.upper())
        #Check if it is a domain or FQDN. Example: host.domain.com
        elif re.match(r"^[A-Z]([A-Z\d-]{1,63})?([.A-Z\d-]{1,63})$", arg1.upper()) is not None:
            #print(arg1.upper(),"is a FQDN")
            resolve_fqdn(arg1.upper())
        else:
            print("ERROR, not sure what",arg1.upper(),"is")

def resolve_fqdn(fqdn):
    try:
        ip = socket.gethostbyname(fqdn)
        print(ip+":"+fqdn)
    except:
        print("Unresolvable FQDN",fqdn)

def resolve_hostname(hostname):
    try:
        ip = socket.gethostbyname(hostname)
        print(ip+":"+hostname)
        return
    except:
        #Add a domain name to the hostname to see if it can be resolved to IP
        for domain in domains:
            try:
                ip = socket.gethostbyname(hostname+domain)
                print(ip+":"+hostname+domain)
                #finish with this function as a value was found
                return
            except:
                continue
        print("Unresolvable hostname",hostname)

def reverse_dns_lookup(ip):
    try:
        fqdn = socket.getfqdn(ip).upper()
        if fqdn == ip:
            print("Unresolvable ip",ip)
            return
        print(ip+":"+fqdn)
    except:
        print("Unresolvable ip",ip)

#A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
    if len(sys.argv) == 1:
        print("IP to hostname and hostname to IP resolving script needs an input file that contains either IP's, hostnames, FQDN, or all of them")
        print(sys.argv[0] + str(" input_file.txt "))
        sys.exit(-1)
    #Accepting only one argument, the input file
    if len(sys.argv) == 2:
        if not os.path.isfile(sys.argv[1]):
            print("Please retry with a different argument " + sys.argv[1])
            sys.exit(-1)
        #full_path = /home/user/file.txt
        full_path = os.path.abspath(sys.argv[1])
        #sys.argv[1] = file.txt
        load_input(sys.argv[1])


from sys import stdin

def Find(v):
    if v != padre[v]: padre[v] = Find(padre[v])
    return padre[v]

def Union(v1,v2):
    rv1 = Find(v1)
    rv2 = Find(v2)

    if rv1 == rv2: return

    if rango[rv1] > rango[rv2]: padre[rv2] = rv1
    else:
        padre[rv1] = rv2
        if rango[rv1] == rango[rv2]: rango[rv2] += 1

def main():
    global padre,rango
    casos = int(stdin.readline())
    stdin.readline()#blank line
    for _ in range(casos):
        n = int(stdin.readline())
        padre = [None]*(n+1)
        rango = [None]*(n+1)
        for i in range(1,n+1):
            padre[i] = i
            rango[i] = 0

        cor = inco = 0
        while True:
            line = stdin.readline().strip().split()

            if line == []: break
            else:
                query,v1,v2 = line
                v1 = int(v1)
                v2 = int(v2)
                if query == 'c':
                    Union(v1,v2)
                else:
                    if Find(v1) == Find(v2):
                        cor += 1
                    else:
                        inco += 1
        print("{},{}".format(cor,inco))
        if _+1 < casos:
            print()

main()


#题目：判断101-200之间有多少个素数，并输出所有素数。
#程序分析：判断素数的方法：用一个数分别去除2到sqrt(这个数)，如果能被整除，则表明此数不是素数，反之是素数。

for i in range(101,200):
    flag = 0
    for j in range (2,i):
        if i % j == 0:
            break
        else:
            flag = 1
    if (flag == 1):print (i)


import click
import numpy as np
import pandas as pd

from typing import Union, List
from pathlib import Path
from loguru import logger
from scipy.stats import norm, lognorm

from vivarium_public_health.risks.data_transformations import pivot_categorical

from vivarium_csu_sanofi_multiple_myeloma.constants import metadata

def len_longest_location() -> int:
    """Returns the length of the longest location in the project.

    Returns
    -------
       Length of the longest location in the project.
    """
    return len(max(metadata.LOCATIONS, key=len))

def sanitize_location(location: str):
    """Cleans up location formatting for writing and reading from file names.

    Parameters
    ----------
    location
        The unsanitized location name.

    Returns
    -------
        The sanitized location name (lower-case with white-space and
        special characters removed.

    """
    # FIXME: Should make this a reversible transformation.
    return location.replace(" ", "_").replace("'", "_").lower()

def delete_if_exists(*paths: Union[Path, List[Path]], confirm=False):
    paths = paths[0] if isinstance(paths[0], list) else paths
    existing_paths = [p for p in paths if p.exists()]
    if existing_paths:
        if confirm:
            # Assumes all paths have the same root dir
            root = existing_paths[0].parent
            names = [p.name for p in existing_paths]
            click.confirm(f"Existing files {names} found in directory {root}. Do you want to delete and replace?",
                          abort=True)
        for p in existing_paths:
            logger.info(f'Deleting artifact at {str(p)}.')
            p.unlink()

def read_data_by_draw(artifact_path: str, key : str, draw: int) -> pd.DataFrame:
    """Reads data from the artifact on a per-draw basis. This
    is necessary for Low Birthweight Short Gestation (LBWSG) data.

    Parameters
    ----------
    artifact_path
        The artifact to read from.
    key
        The entity key associated with the data to read.
    draw
        The data to retrieve.

    """
    key = key.replace(".", "/")
    with pd.HDFStore(artifact_path, mode='r') as store:
        index = store.get(f'{key}/index')
        draw = store.get(f'{key}/draw_{draw}')
    draw = draw.rename("value")
    data = pd.concat([index, draw], axis=1)
    data = data.drop(columns='location')
    data = pivot_categorical(data)
    return data

class LogNormalHazardRate:
    """Defines an instance of a lognormal hazard rate normal distribution.
    Parameters
    ----------
    hr
        mean of distribution
    hr_lower
        lower bound the lognormal distribution
    hr_upper
        upper bound of lognormal distribution
    Returns
    -------
        An object with parameters for scipy.stats.lognorm
    """

    def __init__(self, hr: float, hr_lower: float, hr_upper: float):
        self.hr = hr
        self.hr_upper = hr_upper
        q_975_stdnorm = norm().ppf(0.975)
        mu = np.log(self.hr)
        sigma = (np.log(self.hr_upper) - mu) / q_975_stdnorm
        assert sigma > 0, "look for typos"
        self.hr_distribution = lognorm(s=sigma, scale=self.hr)

    def get_random_variable(self, percentile: float) -> float:
        """Gets a single random draw from a log normal hazard rate distribution.
        Parameters
        ----------
        percentile
            Percentile for sample.
        Returns
        -------
            The random variate from the log normal hazard rate distribution.
        """
        return self.hr_distribution.ppf(percentile)


# coding = utf-8
"""
@author: zhou
@time:2019/1/31 17:42
"""

import os
from util import change_labels, get_data
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.externals import joblib
from sklearn import metrics

model_save_path = r'./model'
d = r'.\text_classification-master\text classification\stop'
txt = os.path.join(d, 'stopword.txt')

class Radish(object):
    def __init__(self, labels=None):
        self.labels = labels
        self.train_vocabulary = None
        self.clf = None
        self.stop_words = [line.strip() for line in open(txt, encoding='utf-8').readlines()]

    def train(self, traindir, labels):
        if not os.path.isdir(traindir):
            raise ValueError('The directory' + traindir + 'does not exist!')

        self.labels = change_labels.change_labels(labels)
        train_labels = change_labels.change_labels(self.labels)
        t_data, t_target = get_data.getdata(traindir, labels, self.stop_words)
        tv = TfidfVectorizer()
        train_data_features = tv.fit_transform(t_data)
        self.train_vocabulary = tv.vocabulary_
        clf = MultinomialNB(alpha=0.001)
        # print(train_data_features)
        print(train_labels)
        clf.fit(train_data_features, t_target)
        self.clf = clf
        save_path_name = os.path.join(model_save_path, "NB" + '.pkl')
        joblib.dump(clf, save_path_name)
        """测试开始"""
        """
        dirt = r'.\text_classification-master\text classification\test'
        test_data, test_target = get_data.gettestdata(dirt, '女性')
        tv = TfidfVectorizer(vocabulary=self.train_vocabulary)
        t_data_features = tv.fit_transform(test_data)
        predicted_label = clf.predict(t_data_features)
        print(metrics.accuracy_score(test_target, predicted_label))
        """
        print('Train finish!')

    def test(self, testdir, label):
        if not os.path.isdir(testdir):
            raise ValueError('The directory' + testdir + 'does not exist!')

        self.labels = change_labels.change_labels(label)
        test_data, test_target = get_data.gettestdata(testdir, label, self.stop_words)
        tv = TfidfVectorizer(sublinear_tf=True, vocabulary=self.train_vocabulary)
        t_data_features = tv.fit_transform(test_data)
        # clf = joblib.load(model_save_path + "NB" + '.pkl')
        clf = self.clf
        predicted_label = clf.predict(t_data_features)
        print("%s准确率：" % label, metrics.accuracy_score(test_target, predicted_label))

if __name__ == "__main__":
    labels = ['女性', '体育', '文学', '校园']
    dirf = r'.\text_classification-master\text classification\train'
    dirt = r'.\text_classification-master\text classification\test'
    r = Radish()
    r.train(dirf, labels)
    r.test(dirt, '女性')
    r.test(dirt, '体育')
    r.test(dirt, '文学')
    r.test(dirt, '校园')


# Категории статуса игры: победа, поражение, продолжение игры

STATUS_WIN = "win"
STATUS_LOSE = "lose"
STATUS_ONGOING = "ongoing"

class Hangman(object):
    def __init__(self, word):
        self.remaining_guesses = 9
        self.status = STATUS_ONGOING
        self._word = word
        self._guesses = []
    def guess(self, char):
        if self.status == STATUS_LOSE:
            raise ValueError("Не правильно!")
        elif self.status == STATUS_WIN:
            raise ValueError("Правильно!")
        if char in self._word:
            if char in self._guesses:
                # без повторов; засчитывается как ход
                self.remaining_guesses -= 1
            else:
                self._guesses.append(char)
        else:
            # промах

            self.remaining_guesses -= 1
            self._guesses.append(char)
        # проверка: победа или поражение
        if self.remaining_guesses < 0:
            self.status = STATUS_LOSE

        # если только не угадали правильно...
        if all([l in self._guesses for l in self._word]):
            self.status = STATUS_WIN
    def get_masked_word(self):
        m = ["_"] * len(self._word)
        for l in self._guesses:
            for i, letter in enumerate(self._word):
                if letter == l:
                    m[i] = l
        return ''.join(m)

    def get_status(self):
        return self.status


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 28 14:39:57 2020

@author: Vikee
"""

import numpy

def levenshteinDistanceDP(token1, token2):
    distances = numpy.zeros((len(token1) + 1, len(token2) + 1))

    for t1 in range(len(token1) + 1):
        distances[t1][0] = t1

    for t2 in range(len(token2) + 1):
        distances[0][t2] = t2

    a = 0
    b = 0
    c = 0

    for t1 in range(1, len(token1) + 1):
        for t2 in range(1, len(token2) + 1):
            if (token1[t1-1] == token2[t2-1]):
                distances[t1][t2] = distances[t1 - 1][t2 - 1] # if 2 characters at end of 2 prefixesa re equal,then the distance is equal to the value in the top-left corner of the 2 x 2 matrix.
            else:   # If the two characters are not equal, then the distance in the current cell is equal to the minimum of the three existing values in the 2 x 2 matrix after adding a cost of 1
                a = distances[t1][t2 - 1]
                b = distances[t1 - 1][t2]
                c = distances[t1 - 1][t2 - 1]

                if (a <= b and a <= c):
                    distances[t1][t2] = a + 1
                elif (b <= a and b <= c):
                    distances[t1][t2] = b + 1
                else:
                    distances[t1][t2] = c + 1

    print(distances, len(token1), len(token2))
    return distances[len(token1)][len(token2)]

distance = levenshteinDistanceDP("Keerthana", "Keerthaanaa")

Levenshtein Distance or Minimum Edit Distance.py