#!/usr/bin/env python3
import argparse
import random
import os
'''
usage: python3 utils/dedup_and_split.py file --train 0.5 --valid 0.2 --test 0.3
'''

SEP = ' # '

def parse_and_dedup_examples(filename, sep=SEP):
    '''
    It is assumed that each line in the file constitutes one example, and if the
    separator = #, the lines are of the form of strings 'label#example'.
    '''
    labels = []
    examples = []
    with open(filename, 'r') as f:
        examples_lines = f.read().splitlines()
    examples_lines = [e for e in examples_lines if e]  # remove empty lines
    examples_lines = list(set(examples_lines))  # remove duplicates
    random.shuffle(examples_lines)
    for e in examples_lines:
        e_sep = e.split(sep)
        labels.append(e_sep[0])
        examples.append(e_sep[1])
    return labels, examples

def partition_list(l, partition_sizes):
    assert sum(partition_sizes) == 1.
    partitioned = []
    length = len(l)
    for s in partition_sizes:
        if l:
            index = round(s * length)
            partitioned.append(l[:index])
            l = l[index:]
    return partitioned

def split_to_train_valid_test(labels, examples, train=.6, valid=.3, test=.1):
    assert train + valid + test == 1.
    classes_examples = {c: [] for c in set(labels)}
    for i in range(len(labels)):
        classes_examples[labels[i]].append(examples[i])
    train_labels, train_examples = [], []
    valid_labels, valid_examples = [], []
    test_labels, test_examples = [], []
    for c in classes_examples:
        train_part, valid_part, test_part = \
            partition_list(classes_examples[c], (train, valid, test))
        train_examples.extend(train_part)
        valid_examples.extend(valid_part)
        test_examples.extend(test_part)
        train_labels.extend([c for i in train_part])
        valid_labels.extend([c for i in valid_part])
        test_labels.extend([c for i in test_part])
    return (train_labels, valid_labels, test_labels), \
           (train_examples, valid_examples, test_examples)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Split file with training examples into three balanced.")
    parser.add_argument(
        'filename',
        type=str,
        help="Path to file with examples.")
    parser.add_argument('--train', type=float, default=0.6, help="Train set size.")
    parser.add_argument('--valid', type=float, default=0.1, help="Validation set size.")
    parser.add_argument('--test', type=float, default=0.3, help="Test set size.")
    args = parser.parse_args()
    assert args.train + args.valid + args.test == 1.

dirname = os.path.dirname(args.filename)
labels, examples = parse_and_dedup_examples(args.filename)
(train_labels, valid_labels, test_labels), \
    (train_examples, valid_examples, test_examples) = \
    split_to_train_valid_test(labels, examples, args.train, args.valid, args.test)

with open(os.path.join(dirname, 'train.in'), 'w') as f:
    f.write('\n'.join(train_examples) + '\n')
with open(os.path.join(dirname, 'train.out'), 'w') as f:
    f.write('\n'.join(train_labels) + '\n')

with open(os.path.join(dirname, 'valid.in'), 'w') as f:
    f.write('\n'.join(valid_examples) + '\n')
with open(os.path.join(dirname, 'valid.out'), 'w') as f:
    f.write('\n'.join(valid_labels) + '\n')

with open(os.path.join(dirname, 'test.in'), 'w') as f:
    f.write('\n'.join(test_examples) + '\n')
with open(os.path.join(dirname, 'test.out'), 'w') as f:
    f.write('\n'.join(test_labels) + '\n')

with open(os.path.join(dirname, 'vocab.in'), 'w') as f:
    vocab = set(' '.join(examples).split(' '))
    f.write('\n'.join(vocab) + '\n')
with open(os.path.join(dirname, 'vocab.out'), 'w') as f:
    vocab = set(' '.join(labels).split(' '))
    f.write('\n'.join(vocab) + '\n')


from django.urls import path, include
from rest_framework.routers import DefaultRouter
from app.views import (WatchList, WatchListDetail,
                       StreamPlatform, StreamPlatformDetail,
                       ReviewList, ReviewDetail, ReviewCreate,
                       StreamPlatformVS, UserReview, WatchList_Filtering,
                       WatchList_Searching, WatchList_Ordering)

# router = DefaultRouter()
# router.register('stream/', StreamPlatformVS, basename='streamplatform')

urlpatterns = [
    path('list/', WatchList.as_view(), name='movie-list'),
    path('<int:pk>/', WatchListDetail.as_view(), name='movie-detail'),

    # this are using django-filter
    path('movie-filter/', WatchList_Filtering.as_view(), name='movie-filter'),
    path('movie-search/', WatchList_Searching.as_view(), name='movie-search'),
    path('movie-ordering/', WatchList_Ordering.as_view(), name='movie-ordering'),

    path('stream/', StreamPlatform.as_view(), name='streamplatform'),
    path('stream/<int:pk>/', StreamPlatformDetail.as_view(), name='streamplatform-detail'),

    path('<int:pk>/review-create/', ReviewCreate.as_view(), name='review-create'),
    path('<int:pk>/reviews/', ReviewList.as_view(), name='review-list'),
    path('review/<int:pk>/', ReviewDetail.as_view(), name='review-detail'),

    path('reviews/', UserReview.as_view(), name='user-reviews-detail'),     # fetching using parameters
    # this is without using django-filter

    path('reviews/<str:username>/', UserReview.as_view(), name='user-review-detail'),   # fetching using username
    # this is without using django-filter

    # path('', include(router.urls)),
    # path('review/', ReviewList.as_view(), name='review-list'),
    # path('review/<int:pk>/', ReviewDetail.as_view(), name='review-detail'),
]


#coding=utf8
from data import data
import json
import logging

def prints(s):
    pass
    print(s)

def get_startprob():
    """get BMES matrix
    """
    c = 0
    c_map = {"B":0, "M":0, "E":0, "S":0}

    #caculate the count
    for v in data :
        for key,value in v.items():
            c += 1
            print(f"key {key}, value[0] is {value[0]}")
            c_map[value[0]] += 1
            print(f"c_map[value[0]] is {c_map[value[0]]}")
    return [c_map[i]/float(c) for i in "BMES"]

def get_transmat():
    """get transmat of status
    """
    c = 0
    #record BE:1,BB:2
    c_map = {}
    for v in data :
        for key,value in v.items():
            print(f"key {key}, value[0] is {value[0]}")
            for v_i in range(len(value)-1):
                couple = value[v_i:v_i+2]
                c_couple_source = c_map.get(couple,0)
                c_map[couple] = c_couple_source + 1
                c += 1
            #c_map[value[0]]=c_map[value[0]] +1
            #prints("c_map[value[0]] is "+str(c_map[value[0]]) )
    print(f"get_transmat's c_map is {c_map}")

    res=[]
    for i in "BMES":
        col_count = 0
        for j in "BMES":
            col_count += c_map.get(i+j,0)
        col = [c_map.get(i+j,0)/float(col_count) for j in "BMES"]
        res.append(col)
    return res

def get_words():
    return u"我要吃饭天气不错谢天地"

def get_word_map():
    words=get_words()
    res={}
    for i in range(len(words)):
        res[words[i]]=i
    return res

def get_array_from_phase(phase):
    word_map=get_word_map()
    res=[]
    for key in phase:
        res.append(word_map[key])
    return res

def get_emissionprob():
    #get emmissionprob of status and observers
    c = 0
    #record Bc=0
    #record B我:1,B吃:2
    c_map = {}
    for v in data:
        for key,value in v.items():
            print(f"value[0] is {value[0]}")
            for v_i in range(len(value)):
                couple = value[v_i]+key[v_i]
                print(f"emmition's couple is {couple}")
                c_couple_source = c_map.get(couple,0)
                c_map[couple] = c_couple_source+1
                c += 1
    print(f"emmition's c_map is {c_map}")

    res=[]
    words = get_words()
    for i in "BMES":
        col = [c_map.get(i+j,0)/float(c) for j in words]
        res.append(col)
    return res

if __name__ == "__main__":
    pass
    #print("startprob is ",get_startprob())
    print("transmat is " ,get_transmat())
    #print("emissionprob is " , get_emissionprob())
    #print("word map is ",get_word_map())


"""
The Reeds-Shepp algorithm attempts to find the shortest path in the XY plane with a
start and end (X, Y) position and yaw angle. The path is for a non-holonomic robot which
must have a turn radius. The robot needs to be able to travel in forward
and reverse and the path is allowed to have cusps (transitions between forward and
reverse). Based on these constraints, this planner will return the shortest path between
the two poses regardless of any external factors such as obstacles or barriers.

Paper providing more information on the algorithm:
Reeds, J., & Shepp, L. (1990). Optimal paths for a car that goes both forwards
and backwards. https://msp.org/pjm/1990/145-2/pjm-v145-n2-p06-s.pdf
Curve formulas can be found on page 390-391.

Inspiration for this Python implementation of the Reeds-Shepp algorithm:
https://github.com/boyali/reeds_and_shepp_curves/tree/master

Our implementation adds the option for a runway (straightaway segment at the end of the
path) to improve precision in reaching the given end pose. We also prioritize a path
with fewer segments over a path with a shorter overall length as long as the two paths
have a total length within 2 meters of each others.
"""

from typing import List, Literal, Tuple

import numpy as np

from rsplan import curves, helpers, primitives

def path(
    start_pose: Tuple[float, float, float],
    end_pose: Tuple[float, float, float],
    turn_radius: float,
    runway_length: float,
    step_size: float,
    length_tolerance: float = 2.0,
) -> primitives.Path:
    """Generates a Reeds-Shepp path given start and end points (represented as
    [x, y, yaw]), turn radius, and step size. The step size is the distance between each
    point in the list of points (e.g. 0.05m).

    If the path has no runway, the runway length must be 0.0.

    If the path has a runway, calculates the runway start pose, creates a Reeds-Shepp
    path from the given start pose to the runway start pose, and adds a straight Segment
    to the end of the list of Segments in the Path. The runway Segment is a straightaway
    intended to improve the final position accuracy of navigation. The runway can either
    be driven in forward or reverse depending on the sign of the runway length specified
    and can have a variable length.
    """
    if runway_length != 0:  # Path has a runway
        runway_direction: Literal[-1, 1] = -1 if runway_length < 0.0 else 1
        abs_runway_length = abs(runway_length)

        runway_start_pose = _calc_runway_start_pose(
            end_pose, runway_direction, abs_runway_length
        )

        # Find all Reeds-Shepp paths and choose optimal one
        all_paths = _solve_path(start_pose, runway_start_pose, turn_radius, step_size)
        path_rs = _get_optimal_path(all_paths, length_tolerance)

        # Add runway Segment to Path list of Segments
        runway_segment = _calc_runway_segment(
            runway_start_pose, end_pose, runway_direction, turn_radius
        )
        segments = path_rs.segments + [runway_segment]
    else:
        # Find all Reeds-Shepp paths and choose optimal one
        all_paths = _solve_path(start_pose, end_pose, turn_radius, step_size)
        path_rs = _get_optimal_path(all_paths, length_tolerance)
        segments = path_rs.segments

    return primitives.Path(
        start_pose=start_pose,
        end_pose=end_pose,
        segments=segments,
        turn_radius=turn_radius,
        step_size=step_size,
    )

########################################################################################
# PATH PLANNING HELPER FUNCTIONS #######################################################
########################################################################################

def _solve_path(
    start: Tuple[float, float, float],
    end: Tuple[float, float, float],
    turn_rad: float,
    step_size: float,
) -> List[primitives.Path]:
    """Calls all 6 curve functions and returns a list of all valid Reeds-Shepp paths."""
    # If start is not origin, get end w.r.t. start instead of w.r.t. origin
    x, y, phi = helpers.change_base(start, end)

    # Create list of all Reeds-Shepp paths
    paths: List[primitives.Path] = []
    paths.extend(curves.csc(start, end, step_size, x, y, phi, turn_rad))
    paths.extend(curves.ccc(start, end, step_size, x, y, phi, turn_rad))
    paths.extend(curves.cccc(start, end, step_size, x, y, phi, turn_rad))
    paths.extend(curves.ccsc(start, end, step_size, x, y, phi, turn_rad))
    paths.extend(curves.cscc(start, end, step_size, x, y, phi, turn_rad))
    paths.extend(curves.ccscc(start, end, step_size, x, y, phi, turn_rad))

    return paths

def _get_optimal_path(
    paths: List[primitives.Path], length_tolerance: float = 2.0
) -> primitives.Path:
    """Choose optimal Reeds-Shepp path from list of valid paths. If two paths have
    comparable total length (difference is within length tolerance in meters), we will
    choose the path that has less segments.
    """
    paths.sort(key=lambda x: x.total_length, reverse=False)

    # Choose the second shortest path
    roughly_equivalent_lengths = (
        paths[1].total_length - paths[0].total_length
    ) < length_tolerance
    longer_less_segments = len(paths[1].segments) < len(paths[0].segments)
    if roughly_equivalent_lengths and longer_less_segments:
        best_path = paths[1]  # Choose second shortest path because it has less segments
    else:
        best_path = paths[0]  # Choose shortest path (> length tolerance shorter than other paths)

    return best_path

def _calc_runway_start_pose(
    end_pose: Tuple[float, float, float],
    driving_direction: Literal[1, -1],
    runway_length: float,
) -> Tuple[float, float, float]:
    """The start of the runway. Driving direction indicates whether the robot is
    travelling in forward or reverse along the runway.
    """
    end_x, end_y, yaw = end_pose  # Yaw is in radians
    x = end_x - (driving_direction * abs(runway_length) * np.cos(yaw))
    y = end_y - (driving_direction * abs(runway_length) * np.sin(yaw))

    return x, y, yaw

def _calc_runway_segment(
    start_pose: Tuple[float, float, float],
    end_pose: Tuple[float, float, float],
    direction: Literal[1, -1],
    turn_radius: float,
) -> primitives.Segment:
    """Creates a straight Segment representing a path's runway."""
    path_length = round(helpers.euclidean_distance(start_pose, end_pose), 3)

    return primitives.Segment(
        type="straight",
        direction=direction,
        length=path_length,
        turn_radius=turn_radius,
    )


"""Now consider another common pattern: going through a list and
keeping only those items that meet certain criteria.
This is called a filter.
--We'll write a function that returns even numbers from a
listof 1-10"""

def keepEvenNUmbers(nums):
    evenNumberList = []

    for num in nums:
        if num % 2 == 0 :
            evenNumberList.append(num)
    return evenNumberList

print(keepEvenNUmbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

COURSE 3 - data collection and processing with python/week2/filter1.py


"""msteams_webhooks.types."""
from typing import Literal, Union

Colors = Literal["default", "accent", "good", "warning", "attention", "light", "dark"]
ColumnWidthTypes = Union[Literal["auto", "stretch"], str]
ContainerStyleTypes = Literal["default", "emphasis", "good", "attention", "warning", "accent"]
FontSizes = Literal["default", "small", "medium", "large", "extraLarge"]
FontTypes = Literal["default", "monospace"]
FontWeights = Literal["default", "lighter", "bolder"]
HorizontalAlignmentTypes = Literal["left", "center", "right"]
ImageSizeTypes = Literal["auto", "stretch", "small", "medium", "large"]
ImageStyleTypes = Literal["default", "person"]
SpacingTypes = Literal["default", "none", "small", "medium", "large", "extraLarge", "padding"]
TextBlockStyles = Literal["default", "heading"]
VerticalAlignmentTypes = Literal["top", "center", "bottom"]


import blenderproc as bproc
import argparse
import numpy as np
from pathlib import Path
import os
import shutil
import pytransform3d.rotations as pr
import pytransform3d.transformations as pt
from PIL import Image
import h5py

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--environment", type=str, default="00700")

    args = parser.parse_args()

    home_dir = Path(os.environ.get("BASE_DIR", "/local/home/hanlonm"))
    environment = args.environment
    environment_dataset_path = home_dir / "active-viewpoint-selection/data"
    hloc_datasets_path = home_dir / "Hierarchical-Localization/datasets"
    output_directory: Path = hloc_datasets_path / environment
    output_directory.mkdir(exist_ok=True)
    # output_directory = Path("output")

    image_output_dir: Path = hloc_datasets_path / environment / "mapping"
    image_output_dir.mkdir(exist_ok=True)
    # image_output_dir = Path("output")

    pose_file_name = Path(output_directory) / "image_poses.txt"
    pose_file = open(pose_file_name, "w")

    # [dev_LF, dev_LL, dev_RF, dev_RR]
    hololens_transforms = np.load(
        str(home_dir) + "/active-viewpoint-selection/data/hololens_transforms.npy")
    cam_rotation = np.array([[0.0, -0.0, -1.0], [-1.0, 0.0, -0.0],
                             [0.0, 1.0, 0.0]])
    bproc.init()
    scene = bproc.loader.load_blend(
        str(environment_dataset_path) + f"/{environment}/{environment}.blend")
    bproc.lighting.light_surface(scene, 1.0)
    # K = [366.8789978027344, 0.0, 240.0, 0.0, 365.71429443359375, 320.0, 0.0, 0.0, 1.0]
    camera_matrix = np.array([[366.8789978027344, 0, 240],
                              [0.0, 365.71429443359375, 320], [0, 0.0, 1.0]])
    bproc.camera.set_intrinsics_from_K_matrix(K=camera_matrix,
                                              image_width=480,
                                              image_height=640)
    bproc.renderer.set_max_amount_of_samples(5)
    bproc.renderer.set_output_format("JPEG")
    bproc.renderer.set_denoiser("OPTIX")
    bproc.renderer.enable_depth_output(activate_antialiasing=True)

    depth_h5 = h5py.File(f"output/{environment}_depth.h5", "w")

    image_names = []
    with open(
            str(environment_dataset_path) +
            f"/{environment}/mapping_trajectory.txt", "r") as f:
        frame = 0
        for line in f.readlines():
            line = [float(x) for x in line.split()]
            T_world_dev = pt.transform_from(
                pr.matrix_from_quaternion(
                    pr.quaternion_wxyz_from_xyzw(line[3:])), line[:3])
            for i, cam in enumerate(["LF", "LL", "RF", "RR"]):
                T_dev_cam = pt.transform_from_pq(hololens_transforms[i])

                T_world_base = T_world_dev @ T_dev_cam

                cam_pose = pt.pq_from_transform(T_world_base)

                image_file_name = cam + "_" + str(frame).zfill(4) + ".jpeg"
                image_names.append(image_file_name)

                pose_file.write("mapping/" + image_file_name + " " +
                                str(cam_pose[0]) + " " + str(cam_pose[1]) +
                                " " + str(cam_pose[2]) + " " +
                                str(cam_pose[4]) + " " + str(cam_pose[5]) +
                                " " + str(cam_pose[6]) + " " +
                                str(cam_pose[3]) + "\n")

                p_xyz = cam_pose[:3]
                q_wxyz = cam_pose[3:]
                rot = pr.matrix_from_quaternion(q_wxyz)
                rot = rot @ cam_rotation
                matrix_world = bproc.math.build_transformation_mat(p_xyz, rot)
                bproc.camera.add_camera_pose(matrix_world)
            frame += 1

    data = bproc.renderer.render()
    #bproc.writer.write_hdf5("output", data)

    for i, image_array in enumerate(data["colors"]):
        # Convert the NumPy array to PIL Image
        pil_image = Image.fromarray(image_array)

        # Save the image to disk using PIL
        pil_image.save(image_output_dir / image_names[i])
    for i, depth_array in enumerate(data["depth"]):
        depth_h5.create_dataset(image_names[i], data=depth_array)
    depth_h5.close()

if __name__ == "__main__":
    main()


def show1(a, b, c, d):
    print(a, b, c, d)

def show2(k, e, m):
    return (k+e+m)/3

def show3(k, e, m):
    avg = (k+e+m)/3
    if avg>=60:
        return '합격'
    else:
        return '불합격'

show1(10, 'A', 100.4, True)
a=show2(95, 85, 73)
print('평균 : %.2f' % show2(95, 85, 73))
print(a)
print('결과 : ', show3(95,85,73))


import sys

# 사다리 타기(DFS)
# 답에서 부터 거꾸로 출발하는 코드
sys.stdin = open("input.txt", 'r')

dx = [0, 0, -1]
dy = [-1, 1, 0]

def DFS(x, y):
    if x == 0:
        print(y)
        sys.exit()

    for k in range(3):
        xx = x + dx[k]
        yy = y + dy[k]
        if 0 <= xx < 10 and 0 <= yy < 10 and ladder[xx][yy] == 1:
            ladder[xx][yy] = 0
            DFS(xx, yy)

if __name__ == "__main__":
    goal = 2
    ladder = [list(map(int, input().split())) for _ in range(10)]

    # 시작 지점 찾기
    for i in range(10):
        if ladder[9][i] == goal:
            DFS(9, i)


# -*- coding: utf-8 -*-

from imio.smartweb.core.testing import IMIO_SMARTWEB_CORE_FUNCTIONAL_TESTING
from imio.smartweb.core.testing import ImioSmartwebTestCase
from plone import api
from plone.app.testing import TEST_USER_ID
from plone.app.testing import setRoles
from plone.app.textfield.value import RichTextValue
from zope.component import queryMultiAdapter

import json

class TestSearch(ImioSmartwebTestCase):
    layer = IMIO_SMARTWEB_CORE_FUNCTIONAL_TESTING

    def setUp(self):
        """Custom shared utility setup for tests"""
        self.request = self.layer["request"]
        self.portal = self.layer["portal"]
        setRoles(self.portal, TEST_USER_ID, ["Manager"])
        self.folder = api.content.create(
            container=self.portal,
            type="imio.smartweb.Folder",
            id="folder",
        )
        api.content.transition(self.folder, "publish")

    def test_search(self):
        self.request.form["SearchableText"] = "kamoulox"
        search = queryMultiAdapter((self.portal, self.request), name="search")
        self.assertEqual(len(search.results()), 0)

        page = api.content.create(
            container=self.folder,
            type="imio.smartweb.Page",
            title="kamoulox",
        )

        self.assertEqual(len(search.results()), 1)

        section_text = api.content.create(
            container=page,
            type="imio.smartweb.SectionText",
            title="kamoulox",
        )
        section_text.text = RichTextValue("
kamoulox
", "text/html", "text/html")
        section_text.reindexObject()

        # Some sections (like SectionText) are not searchable
        self.assertEqual(len(search.results()), 1)

        api.content.create(
            container=self.portal,
            type="imio.smartweb.PortalPage",
            title="kamoulox",
        )
        self.assertEqual(len(search.results()), 2)

    def test_search_result_option(self):
        search = queryMultiAdapter((self.portal, self.request), name="search")
        options = json.loads(search.get_search_result_option())
        self.assertDictEqual(options, {"directory": True, "events": True, "news": True})


import sys
sys.setrecursionlimit(100000)
def main():

    string = readFiles()

    result = reducePolymer(string)

    print(len(result))

def reducePolymer(string):
    idx = 0
    while idx != len(string) - 1:
        if canReact(string[idx], string[idx + 1]):
            string = string[:idx] + string[idx + 2:]
            if idx != 0:
                idx -= 1
        else:
            idx += 1
    return string

def canReact(char1, char2):
    if char1.islower():
        if char2 == char1.upper():
            return True
        else:
            return False
    else:
        if char2 == char1.lower():
            return True
        else:
            return False

def readFiles():
    file = open("input.txt", "r")
    data = file.readline().strip()
    return data

if __name__ == "__main__":
    main()


import os

# 获取当前文件夹的绝对路径
current_folder = os.getcwd()

# 获取当前文件夹同级的 output_files 文件夹的绝对路径
SAVE_DIR_PATH = os.path.abspath(os.path.join(
    current_folder, 'Excel-处理转化', 'output_files'))

# 获取当前文件夹同级的 pending_files 文件夹的绝对路径
SOURCE_DIR_PATH = os.path.abspath(os.path.join(
    current_folder, 'Excel-处理转化', 'pending_files'))

NEW_SHEET_TITLE = '埋点用例'


import datetime
import io
import os
import pandas as pd
import functions as f

def find_first_date(incst_df, b_df, cf_df, price_df):
    def get_doc_first_date(n):
        incst_first_date = datetime.datetime.strptime(incst_df.columns[n], '%Y-%m-%d').date()
        b_first_date = datetime.datetime.strptime(b_df.columns[n], '%Y-%m-%d').date()
        cf_first_date = datetime.datetime.strptime(cf_df.columns[n], '%Y-%m-%d').date()
        f_d_d = max(incst_first_date, b_first_date, cf_first_date)
        return f_d_d

    f_doc_date = get_doc_first_date(1)
    price_dates_list = list(datetime.datetime.strptime(p, '%Y-%m-%d').date() for p in price_df['Date'].values)
    f_price_date = min(price_dates_list)
    if f_doc_date > f_price_date:
        f_price_date = f.find_nearest_price_date(f_doc_date, price_dates_list)
    else:
        i = 2
        while f_doc_date < f_price_date:
            f_doc_date = get_doc_first_date(i)
            i += 1
        f_price_date = f.find_nearest_price_date(f_doc_date, price_dates_list)
    return f_doc_date, f_price_date

def first_date_data_collect(first_date, sector, first_date_by_sector_dic):
    if sector in first_date_by_sector_dic.keys():
        first_date_by_sector_dic[sector].append(first_date)
    else:
        first_date_by_sector_dic[sector] = [first_date]
    return first_date_by_sector_dic

def compare_doc_dates(df1, df2, df3):
    df1_cols = df1.columns
    df2_cols = df2.columns
    df3_cols = df3.columns
    comparison = False
    if len(df1_cols.difference(df2_cols)) == 0 and len(df1_cols.difference(df3_cols)) == 0 and len(
            df2_cols.difference(df3_cols)) == 0:
        comparison = True
    return comparison

def create_docs_and_price_date_lists(d_df1, d_df2, d_df3, p_df):
    d1 = list(d_df1.columns)[1:]
    d2 = list(d_df2.columns)[1:]
    d3 = list(d_df3.columns)[1:]
    p_d = list(p_df['Date'])
    return d1, d2, d3, p_d

def compability_price_and_docs_dates(dates_lists):
    min_dates_list = []
    max_dates_list = []
    successes = []
    for dates_list in dates_lists:
        min_dates_list.append(min(dates_list))
        max_dates_list.append(max(dates_list))
    for min_date in min_dates_list:
        for max_date in max_dates_list:
            if min_date < max_date:
                successes.append(True)
            else:
                successes.append(False)
    if False in successes:
        success = False
    else:
        success = True
    return success

def create_doc_dfs(company, companys_dictionary, directory_path):
    for doc in companys_dictionary[company]:
        file_name = '{0} - {1}.csv'.format(company, doc)
        file_path = os.path.join(directory_path, file_name)
        doc_df = pd.read_csv(file_path)
        doc_df = f.prepare_df(doc_df)
        if doc == 'Income Statement':
            incst_df = doc_df
        elif doc == 'Balance Sheet':
            b_df = doc_df
        elif doc == 'Cash Flow Statement':
            cf_df = doc_df
    return incst_df, b_df, cf_df

def create_price_df(yfticker):
    price_file_name = r'C:\Users\Bartek\Desktop\ALK praca magisterska\China_stock_data\price longer after calc cny\{}_price.csv'.format(yfticker)
    price_df = pd.read_csv(price_file_name)
    return price_df

def verify_data(company, companys_dictionary, companys_sector_dic, directory_path,
                first_date_by_company_dic, first_date_by_sector_dic,
                min_date, number_of_periods,
                late_first_sector_all, late_first_sector_doc, late_first_sector_price,
                price_docs_date_not_compatible_sector_dict, doc_dates_not_equal_dict,
                late_first_sector_price_companies):

    incst_df, b_df, cf_df = create_doc_dfs(company, companys_dictionary, directory_path)

    sector, yf_ticker = companys_sector_dic[company]
    price_df = create_price_df(yf_ticker)
    incst_dates, b_dates, cf_dates, price_dates = create_docs_and_price_date_lists(incst_df, b_df, cf_df, price_df)

    if compability_price_and_docs_dates([incst_dates, b_dates, cf_dates, price_dates]) is False:
        price_docs_date_not_compatible_sector_dict[sector] = price_docs_date_not_compatible_sector_dict[sector] + [company]
        print('{}: dates are not compatible'.format(company))
        return None

    doc_first_date, price_first_date = find_first_date(incst_df, b_df, cf_df, price_df)

    if compare_doc_dates(incst_df, b_df, cf_df) is False:
        doc_dates_not_equal_dict[sector] = doc_dates_not_equal_dict[sector] + [company]
        print('Dates in docs are not equal for {}'.format(company))
        return None

    incst_first_date = datetime.datetime.strptime(incst_df.columns[1], '%Y-%m-%d').date()
    b_first_date = datetime.datetime.strptime(b_df.columns[1], '%Y-%m-%d').date()
    cf_first_date = datetime.datetime.strptime(cf_df.columns[1], '%Y-%m-%d').date()
    f_d_d = max(incst_first_date, b_first_date, cf_first_date)
    price_dates_list = list(datetime.datetime.strptime(p, '%Y-%m-%d').date() for p in price_df['Date'].values)
    f_price_date = min(price_dates_list)

    if f_d_d > min_date and f_price_date > min_date:
        str_doc_first_date = str(doc_first_date)
        late_first_sector_all = f.add_lack_of_data_instance(late_first_sector_all, sector, str_doc_first_date)
        print('First date {} is greater than min_date for {}'.format(str_doc_first_date, company))
        print(f_d_d, f_price_date)
        return None

    if f_d_d > min_date:
        str_doc_first_date = str(doc_first_date)
        late_first_sector_doc = f.add_lack_of_data_instance(late_first_sector_doc, sector, str_doc_first_date)
        print('First date {} is greater than min_date for {}'.format(str_doc_first_date, company))
        print(f_d_d, f_price_date)
        return None

    if f_price_date > min_date:
        str_doc_first_date = str(price_first_date)
        late_first_sector_price = f.add_lack_of_data_instance(late_first_sector_price, sector, str_doc_first_date)
        late_first_sector_price_companies[sector] = late_first_sector_price_companies[sector] + [company]
        print('First date {} is greater than min_date for {}'.format(str_doc_first_date, company))
        print(f_d_d, f_price_date)
        return None

    if doc_first_date > min_date:
        str_doc_first_date = str(doc_first_date)
        print('First date {} is greater than min_date for {}'.format(str_doc_first_date, company))
        return None

    last_date = datetime.date(year=2022, month=1, day=2)
    for d in [incst_dates, b_dates, cf_dates]:
        tmp_date_l = []
        for x in d:
            x_date = datetime.datetime.strptime(x, '%Y-%m-%d').date()
            if min_date <= x_date < last_date:
                tmp_date_l.append(x)
        fil_number_of_periods = len(tmp_date_l)
        if fil_number_of_periods != number_of_periods:
            print('Not all periods available ({}) for {}'.format(fil_number_of_periods, company))
            return None

    first_date_by_company_dic[company] = [doc_first_date, price_first_date]
    first_date_by_sector_collection_dic = first_date_data_collect(doc_first_date, sector, first_date_by_sector_dic)
    dfs = incst_df, b_df, cf_df, price_df
    return dfs, first_date_by_sector_collection_dic, first_date_by_company_dic, late_first_sector_all, late_first_sector_doc, late_first_sector_price, price_docs_date_not_compatible_sector_dict, doc_dates_not_equal_dict, late_first_sector_price_companies


from django.shortcuts import get_object_or_404, render, redirect
from .forms import InputFormset
from .models import DataModel

def index(request):
    """Renders index.html page, handles POST method to save inputted data."""
    model_data = {}

    if request.method == 'POST':
        formset = InputFormset(data = request.POST)
        if  formset.is_valid():
            for key, value in formset.data.items():
                if 'input_field' in key:
                    model_data[f'{key}'] = value
            formset_model= DataModel(data = model_data)
            formset_model.save()
            return redirect('/')
        else:
            print('There is an error:', formset.non_form_errors())

    formset = InputFormset()

    context = {'formset': formset}

    return render(request, 'index.html', context)

def saved_text(request):
    """Renders data-page.html page. Returns text form db in context."""
    database = DataModel.objects.all()
    number = 0
    entries = {}

    for table in database:
        number += 1
        entries[str(number)] = []
        for value in table.data.values():
            print(entries)
            entries[str(number)] += [value]

    context = {'entries': entries}

    return render(request, 'data-page.html', context)


'''
# 백준_1107_리모컨. 골드 5. 풀이: 23.05.05 -> 실패

# 풀이방법
- 100번에서 시작.
- 숫자 버튼이 고장날 수도, 고장나지 않을 수도 있다.
- 채널은 무한대 만큼 있다.

# 풀이
- 눌러야 할 버튼의 숫자를 기준으로 더 낮은 수와 더 높은 수를 선택해서 입력수를 센다.
- index 이용
: list.index(x, start, end)
기본적으로 start=0, end=len(list)
start:end 까지의 범위에서 x를 탐색하여 index 반환
- 고장난 버튼이 없거나, 이동할 곳이 100번이거나, 누를 버튼이 전부 고장나지 않은 경우는 조건문으로 처리하여 바로 출력

# 반례
14
2
1 5
- 1이 고장났네? -> 더 낮은 숫자: (0)9 입력(1번 누름) -> + 입력(5번 누름) => 정답: 6
- 1이 고장났네? -> 더 높은 숫자: 20 입력(2번 누름) -> - 입력(6번 누름) => 정답: 8
-> 최솟값: 6
'''

'''
# 보완할 것
- 진짜.. 이 문제만 몇시간을 쏟았는지 모른다. 만만히 봤는데, 자꾸 틀린다.
- 예제를 통과해도, 반례를 찾아서 통과해도, 이것저것 넣어봐도 ValueError가 뜬다..
- 무언가 빠트린 조건이 있다는건데, 뭘까?
- 설령 이 코드에서 더 조건을 빈틈없이 추가한다 하더라도, 너무 지저분하다. 더 좋은 아이디어를 찾아보자.
'''

# 풀이 기록
# 이동하고자 하는 채널
n = input()
len_n = len(n)
num = int(n)

# 고장난 버튼의 개수
m = int(input())

# 고장난 버튼이 없으면
if m == 0:
    print(min(len_n, abs(num-100)))

else:
    no_buttons = list(map(int, input().split()))

    # 현재 100번이므로, 이동할 필요가 없음
    if n == '100':
        print(0)

    else:
        buttons = [True] * 10
        for i in no_buttons:
            buttons[i] = False

        buttons_reverse = list(reversed(buttons))

        # 0123456789, 0~9
        # 9876543210, -10~-1

        cnt_100 = abs(num-100)

        check = True
        for i in range(len(n)):
            if not buttons[int(n[i])]:
                check = False
                break

        # 버튼을 눌러서 바로 이동 가능하다면,
        if check:
            print(min(len_n, cnt_100))

        else:
            # 남은 눌러야할 수
            if i == 0:
                tmp = n
            else:
                tmp = n[:i]

            # 남은 눌러야할 버튼 수
            remain_len = len_n - i
            # 현재 몇번째 누를 수인가
            idx = int(n[i])

            # 더 낮은 수
            # 더 낮은 버튼이 있다면
            if True in buttons[:idx]:
                tmp_l = tmp + str(9-buttons_reverse.index(True, -idx))
                tmp_l += str(9-buttons_reverse.index(True)) * (remain_len-1)

            else:
                tmp_l = tmp + str(buttons.index(True)) * remain_len
            if tmp_l[0] == '0' and len(tmp_l) > 1:
                tmp_l = tmp_l[1:]

            cnt_l = len(tmp_l) + abs(num - int(tmp_l))

            # 더 높은 수
            # 더 낮은 버튼이 있다면
            if True in buttons[idx+1:]:
                tmp_h = str(buttons.index(True, idx+1))
                tmp_h += str(buttons.index(True)) * (remain_len-1)

            else:
                # 현재 누를게 9인데, 가장 앞의 자리 수라면.
                if i == 0:
                    tmp_h = '10'
                    tmp_h += str(buttons.index(True)) * (remain_len-1)
                tmp_h = tmp + str(9-buttons_reverse.index(True)) * remain_len

            cnt_h = len(tmp_h) + abs(int(tmp_h) - num)

            # 최솟값 찾기
            answer = min(cnt_100, cnt_l, cnt_h)

            print(answer)

'''
# 결과
메모리:  KB
시간:  ms
코드 길이:  B
'''


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def new_my_fun(*args, **kwargs):
    print('Внимание! Работает новая функция!')
    print(f'x_new = {args}, kwargs_new = {kwargs}')
    print('Новая функция завершила работу!')
    return True

def my_fun(*args, **kwargs):
    print(f'x = {args}, kwargs = {kwargs}')
    new_my_fun(*args, **kwargs)
    print('*' * 50, '\n')
    return True

my_fun(y=22)
my_fun(y=11)
my_fun(3, 12, 33, 55, 5675, 441, y=55, w=343)

tt = {'sep': '<->', 'end': 'QQQ\nqqq\n'}
w = [11, 22, 33, 44, 55]

print('END')


from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Command

from loader import dp, vdb

def parsing_str(string: str):
    return list(map(str, string.split(" ")))

@dp.message_handler(Command("add_vehicle"))
async def add_vehicle(message: types.Message, state: FSMContext):
    await message.answer("Для регистрации нового автомобиля"
                         "мне нужны некоторые данные от тебя: \n"
                         "1.id - индифакационный номер автомобиля (натуральное число).\n"
                         "2.model - модель автомобиля (строка).\n"
                         "3.plat number - номер автомоюиля (строка). \n"
                         "P.S Пожалуйста, следуй инструкциям. \n"
                         "Пример правильной записи: 1 Volvo AA-0000-CB")
    vehicle = vdb.select_all_vehicles()
    await message.answer(f"Сейчас я тебе отправлю список автомобилей до регистрации: \n"
                         f"{vehicle}")
    await state.set_state("new_vehicle")

    @dp.message_handler(state="new_vehicle")
    async def add_driver_second_step(message: types.Message, state: FSMContext):
        string = message.text
        parsing_list = parsing_str(string)
        id = int(parsing_list[0])
        model = parsing_list[1]
        plate_number = parsing_list[-1].replace("-", " ")
        vdb.add_vehicle(id=id, model=model, plate_number=plate_number)
        vehicle = vdb.select_vehicle(id=id)
        vehicles = vdb.select_all_vehicles()
        await message.answer(f"Автомобиль успешно зарегестрирован: \n"
                             f" {vehicle}")
        await message.answer(f"База данных после регестрации автомобиля: \n"
                             f"{vehicles}")
        await state.finish()


import re
import uuid
import contextlib
import time

from datetime import datetime, timedelta

import sentry_sdk

from sentry_sdk.utils import capture_internal_exceptions, logger, to_string
from sentry_sdk._compat import PY2
from sentry_sdk._types import MYPY

if PY2:
    from collections import Mapping
else:
    from collections.abc import Mapping

if MYPY:
    import typing

    from typing import Generator
    from typing import Optional
    from typing import Any
    from typing import Dict
    from typing import List
    from typing import Tuple

_traceparent_header_format_re = re.compile(
    "^[ \t]*"  # whitespace
    "([0-9a-f]{32})?"  # trace_id
    "-?([0-9a-f]{16})?"  # span_id
    "-?([01])?"  # sampled
    "[ \t]*$"  # whitespace
)

class EnvironHeaders(Mapping):  # type: ignore
    def __init__(
        self,
        environ,  # type: typing.Mapping[str, str]
        prefix="HTTP_",  # type: str
    ):
        # type: (...) -> None
        self.environ = environ
        self.prefix = prefix

    def __getitem__(self, key):
        # type: (str) -> Optional[Any]
        return self.environ[self.prefix + key.replace("-", "_").upper()]

    def __len__(self):
        # type: () -> int
        return sum(1 for _ in iter(self))

    def __iter__(self):
        # type: () -> Generator[str, None, None]
        for k in self.environ:
            if not isinstance(k, str):
                continue

            k = k.replace("-", "_").upper()
            if not k.startswith(self.prefix):
                continue

            yield k[len(self.prefix) :]

class _SpanRecorder(object):
    __slots__ = ("maxlen", "finished_spans", "open_span_count")

    def __init__(self, maxlen):
        # type: (int) -> None
        self.maxlen = maxlen
        self.open_span_count = 0  # type: int
        self.finished_spans = []  # type: List[Span]

    def start_span(self, span):
        # type: (Span) -> None

        # This is just so that we don't run out of memory while recording a lot
        # of spans. At some point we just stop and flush out the start of the
        # trace tree (i.e. the first n spans with the smallest
        # start_timestamp).
        self.open_span_count += 1
        if self.open_span_count > self.maxlen:
            span._span_recorder = None

    def finish_span(self, span):
        # type: (Span) -> None
        self.finished_spans.append(span)

class Span(object):
    __slots__ = (
        "trace_id",
        "span_id",
        "parent_span_id",
        "same_process_as_parent",
        "sampled",
        "transaction",
        "op",
        "description",
        "start_timestamp",
        "_start_timestamp_monotonic",
        "status",
        "timestamp",
        "_tags",
        "_data",
        "_span_recorder",
        "hub",
        "_context_manager_state",
    )

    def __init__(
        self,
        trace_id=None,  # type: Optional[str]
        span_id=None,  # type: Optional[str]
        parent_span_id=None,  # type: Optional[str]
        same_process_as_parent=True,  # type: bool
        sampled=None,  # type: Optional[bool]
        transaction=None,  # type: Optional[str]
        op=None,  # type: Optional[str]
        description=None,  # type: Optional[str]
        hub=None,  # type: Optional[sentry_sdk.Hub]
        status=None,  # type: Optional[str]
    ):
        # type: (...) -> None
        self.trace_id = trace_id or uuid.uuid4().hex
        self.span_id = span_id or uuid.uuid4().hex[16:]
        self.parent_span_id = parent_span_id
        self.same_process_as_parent = same_process_as_parent
        self.sampled = sampled
        self.transaction = transaction
        self.op = op
        self.description = description
        self.status = status
        self.hub = hub
        self._tags = {}  # type: Dict[str, str]
        self._data = {}  # type: Dict[str, Any]
        self.start_timestamp = datetime.utcnow()
        try:
            # TODO: For Python 3.7+, we could use a clock with ns resolution:
            # self._start_timestamp_monotonic = time.perf_counter_ns()

            # Python 3.3+
            self._start_timestamp_monotonic = time.perf_counter()
        except AttributeError:
            pass

        #: End timestamp of span
        self.timestamp = None  # type: Optional[datetime]

        self._span_recorder = None  # type: Optional[_SpanRecorder]

    def init_finished_spans(self, maxlen):
        # type: (int) -> None
        if self._span_recorder is None:
            self._span_recorder = _SpanRecorder(maxlen)
        self._span_recorder.start_span(self)

    def __repr__(self):
        # type: () -> str
        return (
            "<%s(transaction=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>"
            % (
                self.__class__.__name__,
                self.transaction,
                self.trace_id,
                self.span_id,
                self.parent_span_id,
                self.sampled,
            )
        )

    def __enter__(self):
        # type: () -> Span
        hub = self.hub or sentry_sdk.Hub.current

        _, scope = hub._stack[-1]
        old_span = scope.span
        scope.span = self
        self._context_manager_state = (hub, scope, old_span)
        return self

    def __exit__(self, ty, value, tb):
        # type: (Optional[Any], Optional[Any], Optional[Any]) -> None
        if value is not None:
            self.set_status("internal_error")

        hub, scope, old_span = self._context_manager_state
        del self._context_manager_state

        self.finish(hub)
        scope.span = old_span

    def new_span(self, **kwargs):
        # type: (**Any) -> Span
        rv = type(self)(
            trace_id=self.trace_id,
            span_id=None,
            parent_span_id=self.span_id,
            sampled=self.sampled,
            **kwargs
        )

        rv._span_recorder = self._span_recorder
        return rv

    @classmethod
    def continue_from_environ(cls, environ):
        # type: (typing.Mapping[str, str]) -> Span
        return cls.continue_from_headers(EnvironHeaders(environ))

    @classmethod
    def continue_from_headers(cls, headers):
        # type: (typing.Mapping[str, str]) -> Span
        parent = cls.from_traceparent(headers.get("sentry-trace"))
        if parent is None:
            return cls()
        parent.same_process_as_parent = False
        return parent

    def iter_headers(self):
        # type: () -> Generator[Tuple[str, str], None, None]
        yield "sentry-trace", self.to_traceparent()

    @classmethod
    def from_traceparent(cls, traceparent):
        # type: (Optional[str]) -> Optional[Span]
        if not traceparent:
            return None

        if traceparent.startswith("00-") and traceparent.endswith("-00"):
            traceparent = traceparent[3:-3]

        match = _traceparent_header_format_re.match(str(traceparent))
        if match is None:
            return None

        trace_id, span_id, sampled_str = match.groups()

        if trace_id is not None:
            trace_id = "{:032x}".format(int(trace_id, 16))
        if span_id is not None:
            span_id = "{:016x}".format(int(span_id, 16))

        if sampled_str:
            sampled = sampled_str != "0"  # type: Optional[bool]
        else:
            sampled = None

        return cls(trace_id=trace_id, parent_span_id=span_id, sampled=sampled)

    def to_traceparent(self):
        # type: () -> str
        sampled = ""
        if self.sampled is True:
            sampled = "1"
        if self.sampled is False:
            sampled = "0"
        return "%s-%s-%s" % (self.trace_id, self.span_id, sampled)

    def to_legacy_traceparent(self):
        # type: () -> str
        return "00-%s-%s-00" % (self.trace_id, self.span_id)

    def set_tag(self, key, value):
        # type: (str, Any) -> None
        self._tags[key] = value

    def set_data(self, key, value):
        # type: (str, Any) -> None
        self._data[key] = value

    def set_status(self, value):
        # type: (str) -> None
        self.status = value

    def set_http_status(self, http_status):
        # type: (int) -> None
        self.set_tag("http.status_code", http_status)

        if http_status < 400:
            self.set_status("ok")
        elif 400 <= http_status < 500:
            if http_status == 403:
                self.set_status("permission_denied")
            elif http_status == 404:
                self.set_status("not_found")
            elif http_status == 429:
                self.set_status("resource_exhausted")
            elif http_status == 413:
                self.set_status("failed_precondition")
            elif http_status == 401:
                self.set_status("unauthenticated")
            elif http_status == 409:
                self.set_status("already_exists")
            else:
                self.set_status("invalid_argument")
        elif 500 <= http_status < 600:
            if http_status == 504:
                self.set_status("deadline_exceeded")
            elif http_status == 501:
                self.set_status("unimplemented")
            elif http_status == 503:
                self.set_status("unavailable")
            else:
                self.set_status("internal_error")
        else:
            self.set_status("unknown_error")

    def is_success(self):
        # type: () -> bool
        return self.status == "ok"

    def finish(self, hub=None):
        # type: (Optional[sentry_sdk.Hub]) -> Optional[str]
        hub = hub or self.hub or sentry_sdk.Hub.current

        if self.timestamp is not None:
            # This transaction is already finished, so we should not flush it again.
            return None

        try:
            duration_seconds = time.perf_counter() - self._start_timestamp_monotonic
            self.timestamp = self.start_timestamp + timedelta(seconds=duration_seconds)
        except AttributeError:
            self.timestamp = datetime.utcnow()

        _maybe_create_breadcrumbs_from_span(hub, self)

        if self._span_recorder is None:
            return None

        self._span_recorder.finish_span(self)

        if self.transaction is None:
            # If this has no transaction set we assume there's a parent
            # transaction for this span that would be flushed out eventually.
            return None

        client = hub.client

        if client is None:
            # We have no client and therefore nowhere to send this transaction
            # event.
            return None

        if not self.sampled:
            # At this point a `sampled = None` should have already been
            # resolved to a concrete decision. If `sampled` is `None`, it's
            # likely that somebody used `with sentry_sdk.Hub.start_span(..)` on a
            # non-transaction span and later decided to make it a transaction.
            if self.sampled is None:
                logger.warning("Discarding transaction Span without sampling decision")

            return None

        return hub.capture_event(
            {
                "type": "transaction",
                "transaction": self.transaction,
                "contexts": {"trace": self.get_trace_context()},
                "tags": self._tags,
                "timestamp": self.timestamp,
                "start_timestamp": self.start_timestamp,
                "spans": [
                    s.to_json(client)
                    for s in self._span_recorder.finished_spans
                    if s is not self
                ],
            }
        )

    def to_json(self, client):
        # type: (Optional[sentry_sdk.Client]) -> Dict[str, Any]
        rv = {
            "trace_id": self.trace_id,
            "span_id": self.span_id,
            "parent_span_id": self.parent_span_id,
            "same_process_as_parent": self.same_process_as_parent,
            "op": self.op,
            "description": self.description,
            "start_timestamp": self.start_timestamp,
            "timestamp": self.timestamp,
        }  # type: Dict[str, Any]

        transaction = self.transaction
        if transaction:
            rv["transaction"] = transaction

        if self.status:
            self._tags["status"] = self.status

        tags = self._tags
        if tags:
            rv["tags"] = tags

        data = self._data
        if data:
            rv["data"] = data

        return rv

    def get_trace_context(self):
        # type: () -> Any
        rv = {
            "trace_id": self.trace_id,
            "span_id": self.span_id,
            "parent_span_id": self.parent_span_id,
            "op": self.op,
            "description": self.description,
        }
        if self.status:
            rv["status"] = self.status

        return rv

def _format_sql(cursor, sql):
    # type: (Any, str) -> Optional[str]

    real_sql = None

    # If we're using psycopg2, it could be that we're
    # looking at a query that uses Composed objects. Use psycopg2's mogrify
    # function to format the query. We lose per-parameter trimming but gain
    # accuracy in formatting.
    try:
        if hasattr(cursor, "mogrify"):
            real_sql = cursor.mogrify(sql)
            if isinstance(real_sql, bytes):
                real_sql = real_sql.decode(cursor.connection.encoding)
    except Exception:
        real_sql = None

    return real_sql or to_string(sql)

@contextlib.contextmanager
def record_sql_queries(
    hub,  # type: sentry_sdk.Hub
    cursor,  # type: Any
    query,  # type: Any
    params_list,  # type:  Any
    paramstyle,  # type: Optional[str]
    executemany,  # type: bool
):
    # type: (...) -> Generator[Span, None, None]

    # TODO: Bring back capturing of params by default
    if hub.client and hub.client.options["_experiments"].get(
        "record_sql_params", False
    ):
        if not params_list or params_list == [None]:
            params_list = None

        if paramstyle == "pyformat":
            paramstyle = "format"
    else:
        params_list = None
        paramstyle = None

    query = _format_sql(cursor, query)

    data = {}
    if params_list is not None:
        data["db.params"] = params_list
    if paramstyle is not None:
        data["db.paramstyle"] = paramstyle
    if executemany:
        data["db.executemany"] = True

    with capture_internal_exceptions():
        hub.add_breadcrumb(message=query, category="query", data=data)

    with hub.start_span(op="db", description=query) as span:
        for k, v in data.items():
            span.set_data(k, v)
        yield span

def _maybe_create_breadcrumbs_from_span(hub, span):
    # type: (sentry_sdk.Hub, Span) -> None
    if span.op == "redis":
        hub.add_breadcrumb(
            message=span.description, type="redis", category="redis", data=span._tags
        )
    elif span.op == "http":
        hub.add_breadcrumb(type="http", category="httplib", data=span._data)
    elif span.op == "subprocess":
        hub.add_breadcrumb(
            type="subprocess",
            category="subprocess",
            message=span.description,
            data=span._data,
        )


import tweepy
import pandas as pd
import time
import csv

# Personal credentials given when creating a developer account for twitter
consumerKey = " "
consumerSecret = " "
accessToken = " "
accessTokenSecret = " "

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth,wait_on_rate_limit = True)

csvFile = open('result.csv', 'a')
account = "lplenglish"
numberOfTweets = 20
csvWriter = csv.writer(csvFile)

def tweetIt(account, numberOfTweets):
    for tweet in api.user_timeline(id = account, count = numberOfTweets):

       csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

       print(tweet.created_at, tweet.text)

tweetIt(account, numberOfTweets)
csvFile.close()


from collections import defaultdict
import re
from itertools import groupby

def ref_len(cigar_string):
	"""
	Given a CIGAR string, return the number of
	bases consumed in the reference sequence.
	"""
	ref_consuming_ops = {"M", "D", "N", "=", "X"}
	result = 0
	cig_iter = groupby(cigar_string, lambda chr: chr.isdigit())
	for _, length_digits in cig_iter:
		length = int(''.join(length_digits))
		op = next(next(cig_iter)[1])
		if op in ref_consuming_ops:
			result += length
	return result

def subset_cigar_string(full_cigar, alignment_pos, ref_start, ref_stop):
	"""
	This function will return the cigar string that overlaps
	the reference coordinates from ref_start to ref_stop inclusive.
	For now we assume that the alignment fully covers this region.
	This function is using the 1-based coordinate system.
	So, we assume that alignment_pos is 1-based (as it should be from SAM),
	and that ref_start and ref_stop are 1-based.
	"""
	consumes_ref = {"M", "D", "N", "=", "X"}
	assert alignment_pos <= ref_start
	X = re.finditer("(\d+)([MIDNSHPX=])", full_cigar)
	previous_block_stop = alignment_pos - 1
	subset_cigar = ""
	overlapping_region = False
	for block in X:
		cigar_digits = int(block.group(1))
		cigar_operation = block.group(2)
		if cigar_operation in consumes_ref:
			block_start = previous_block_stop + 1
			block_stop = block_start + cigar_digits - 1
			#if we overlap the region of interest
			if block_stop >= ref_start:
				overlapping_region = True
				overlap_start = max(ref_start, block_start)
				overlap_stop = min(ref_stop, block_stop)
				overlap_length = overlap_stop - overlap_start + 1
				subset_cigar += f"{overlap_length}{cigar_operation}"
			#if we have finished the region of interest
			if block_stop >= ref_stop:
				break
		else:
			if overlapping_region:
				subset_cigar += f"{cigar_digits}{cigar_operation}"
			block_stop = previous_block_stop
		previous_block_stop = block_stop
	return subset_cigar

transcript_junction_to_intron = {}
with open(snakemake.input[0], "r") as input_intron_file:
	for line in input_intron_file:
		geneid, transcript, junction, intron = line.strip().split()
		transcript_junction_to_intron[(transcript, junction)] = intron

readname_to_introns = defaultdict(set)
intron_to_transcripts = defaultdict(set)
with open(snakemake.input[1], "r") as input_spannedjunctions_file:
	for line in input_spannedjunctions_file:
		geneie, transcript, junction, readname = line.strip().split()
		if (transcript, junction) in transcript_junction_to_intron:
			chrom = "_".join(transcript.split("|")[1].split("_")[:2])
			intron = transcript_junction_to_intron[(transcript, junction)]
			readname_to_introns[readname].add(intron)
			intron_to_transcripts[intron].add(transcript)

def cigar_missing_percent(cigar):
	consumes_ref = {"M", "D", "N", "=", "X"}
	X = re.finditer("(\d+)([MIDNSHPX=])", cigar)
	numerator = 0
	denominator = 0
	for block in X:
		cigar_digits = int(block.group(1))
		cigar_operation = block.group(2)
		if cigar_operation in consumes_ref:
			denominator += cigar_digits
			if cigar_operation in {"N", "D"}:
				numerator += cigar_digits
	missing_percent = numerator/denominator
	return missing_percent

with open(snakemake.input[2], "r") as input_subsetsam_file, open(snakemake.output[0], "w") as output_file:
	for line in input_subsetsam_file:
		if not line.startswith("@"):
			geneid, readname, _, chrom, pos, _, cigar = line.strip().split()[:7]
			pos = int(pos)
			introns = readname_to_introns[readname]
			cigar_ref_len = ref_len(cigar)
			for intron in introns:
				intron_chrom = intron.split(":")[0]
				if intron_chrom == chrom:
					intron_start, intron_stop = intron.split(":")[1].split("-")
					intron_start, intron_stop = int(intron_start), int(intron_stop)
					#if the read spans across the entire intron
					if pos <= intron_start and intron_stop <= pos + cigar_ref_len:
						transcripts = intron_to_transcripts[intron]
						region_cigar = subset_cigar_string(cigar, pos, intron_start, intron_stop)
						missing_percent = cigar_missing_percent(region_cigar)
						for transcript in transcripts:
							output_file.write(f"{geneid}\t{transcript}\t{intron}\t{readname}\t{missing_percent}\t{region_cigar}\n")
					else:
						output_file.write("Read doesn't span across entire intron.\n")


# import matplotlib.pyplot as plt
import cv2
from numpy import genfromtxt
import matplotlib.pyplot as plt
import numpy as np
import pandas
import torch
import torch.nn as nn
from mmcv import Config, DictAction
from mmaction.models import build_model
from mmcv.runner import get_dist_info, init_dist, load_checkpoint
import os
from torch.utils.data import DataLoader, TensorDataset
import random
from tqdm import tqdm
from torchvision import transforms

class VideoDataset(Dataset):
    def __init__(self, root_dir, chunk_size=32, num_chunks=10, max_frames=None, fps_step=4):
        self.root_dir = root_dir
        self.chunk_size = chunk_size
        self.num_chunks = num_chunks
        self.fps_step = fps_step
        self.max_frames = max_frames or chunk_size * num_chunks
        self.transform = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])

        self.clips = []
        for people_id in range(1, 15):
            for sit in ['glove', 'noglove']:
                video_file_1, video_file_2, time_file = self.get_file_paths(people_id, sit)
                if not (os.path.exists(video_file_1) and os.path.exists(video_file_2) and os.path.exists(time_file)):
                    continue
                ann = genfromtxt(time_file, delimiter=',')
                for action in ann:
                    action_id = int(action[0]) - 1
                    start, end = int(action[1]), int(action[2])
                    self.clips.extend([
                        (video_file_1, start, end, action_id),
                        (video_file_2, start, end, action_id)
                    ])
    def __len__(self):
        return len(self.clips)

    def __getitem__(self, idx):
        video_file, start, end, label = self.clips[idx]
        cap = cv2.VideoCapture(video_file)
        fps = cap.get(cv2.CAP_PROP_FPS)
        starting_frame, end_frame = int(start * fps), int(end * fps)

        frames_list = []
        for frame_num in range(starting_frame, end_frame, self.fps_step):
            if len(frames_list) >= self.max_frames:
                break
            ret, frame = cap.read()
            if not ret:
                break
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # Change the color space from BGR to RGB
            frame = self.transform(frame)  # [3, 224, 224]
            frames_list.append(frame)

        cap.release()

        frames_tensor = torch.stack(frames_list)  # [T, 3, 224, 224]

        # Cut into chunks
        num_frames = frames_tensor.size(0)
        num_frames = num_frames - num_frames % self.chunk_size
        frames_tensor = frames_tensor[:num_frames]  # [num_chunks*chunk_size, 3, 224, 224]
        chunks_tensor = frames_tensor.view(-1, self.chunk_size, 3, 224, 224)  # [num_chunks, chunk_size, 3, 224, 224]
        if chunks_tensor.size(0) > self.num_chunks:
            chunks_tensor = chunks_tensor[:self.num_chunks]

        return chunks_tensor, torch.tensor([label] * chunks_tensor.size(0), dtype=torch.long)

    def get_file_paths(self, people_id, sit):
        if people_id < 10:
            video_file_1 = os.path.join(self.root_dir, f'0{people_id}_{sit}_cam1_720.mp4')
            video_file_2 = os.path.join(self.root_dir, f'0{people_id}_{sit}_cam2_720.mp4')
            time_file = os.path.join(self.root_dir, f'0{people_id}_{sit}.csv')
        else:
            video_file_1 = os.path.join(self.root_dir, f'{people_id}_{sit}_cam1_720.mp4')
            video_file_2 = os.path.join(self.root_dir, f'{people_id}_{sit}_cam2_720.mp4')
            time_file = os.path.join(self.root_dir, f'{people_id}_{sit}.csv')
        return video_file_1, video_file_2, time_file

def set_seed(seed):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    os.environ['PYTHONHASHSEED'] = str(seed)

class Mlp(nn.Module):
    """ Multilayer perceptron."""

    def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
        super().__init__()
        out_features = out_features or in_features
        hidden_features = hidden_features or in_features
        self.fc1 = nn.Linear(in_features, hidden_features)
        self.act = act_layer()
        self.fc2 = nn.Linear(hidden_features, out_features)
        self.drop = nn.Dropout(drop)

    def forward(self, x):

        x = self.fc1(x)
        x = self.act(x)
        x = self.drop(x)
        x = self.fc2(x)
        x = self.drop(x)
        return x

def changeVideo2Tensor(data_path,time_file_path):
    if not os.access(time_file_path, os.F_OK):
        print(time_file_path,'时间文件不存在')
        return [],[]
    ann = genfromtxt(time_file_path, delimiter=',')
    if not os.access(data_path, os.F_OK):
        print(data_path,'视频文件不存在')
        return [],[]
    cap = cv2.VideoCapture(data_path)
    # print(cap.isOpened())
    # print('cap',cap)
    result_frames_list = []
    action_ids_list = []
    # print(ann)
    for action in ann:
        # print(action)
        start = action[1]
        end = action[2]
        action_id = int(action[0])-1

        fps = cap.get(cv2.CAP_PROP_FPS) #FPS of the video
        # print('fps',fps)
        starting_frame=int(start*fps) # value of the starting frame
        end_frame = int(end * fps) # value of the end frame
        # print('starting_frame',starting_frame)
        # print('end_frame',end_frame)
        cap.set(1,starting_frame) # Where frame_no is the frame you want
        # print(starting_frame)
        # print(action_id)
        frames_list = []
        for frame_num in range(starting_frame, end_frame):
            if frame_num % 4 == 0:
                ret, frame = cap.read()
                # print('ret',ret)
                # print('frame',frame)
                if ret:
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    frame = cv2.resize(frame, (224, 224))
                    frames_list.append(frame)
                else:
                    break

        result_frames = torch.as_tensor(np.stack(frames_list)) # [temporal_dim, height, width, channel]
        result_frames = result_frames.permute(3, 0, 1, 2)
        result_frames = result_frames.float()/255.

        num_frames = result_frames.size(1)
        num_frames = num_frames - num_frames % 32

        result_frames = result_frames[:, :num_frames, :, :]
        num_chunks = result_frames.size(1) // 32

        chunks = torch.chunk(result_frames, chunks=num_chunks, dim=1)
        if num_chunks >10:
            num_chunks = 10
        chunks = chunks[:num_chunks]
        print('num_chunks',num_chunks)
        action_ids = [action_id] * num_chunks
        action_ids = torch.tensor(action_ids, dtype=torch.long)

        # chunks_tensor = torch.stack(chunks, dim=0)
        action_ids_list.append(action_ids)
        result_frames_list.append(chunks)

    cap.release()
    return result_frames_list, action_ids_list

set_seed(2023)

# os.environ["CUDA_VISIBLE_DEVICES"] = '1,2'
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = "cuda:1"
config = './configs/recognition/swin/swin_base_patch244_window1677_sthv2.py'
checkpoint = './checkpoints/swin_base_patch244_window1677_sthv2.pth'

cfg = Config.fromfile(config)
model = build_model(cfg.model, train_cfg=None, test_cfg=cfg.get('test_cfg'))
load_checkpoint(model, checkpoint, map_location=device)
# model = nn.DataParallel(model)
model.to(device)

all_frames = []
all_action_ids = []
# [batch_size, channel, temporal_dim, height, width]
for people_id in tqdm(range(1,15)):
    # all_frames = []
    # all_action_ids = []
    # if people_id == 2 or people_id==5:
    #     continue
    for sit in ['glove','noglove']:
        if people_id < 10:
            data_path_cam1 = '../data/0'+str(people_id)+'_'+sit+'_cam1_720.mp4'
            data_path_cam2 = '../data/0'+str(people_id)+'_'+sit+'_cam2_720.mp4'
            time_file_path = '../data/0'+str(people_id)+'_'+sit+'.csv'
        else:
            data_path_cam1 = '../data/'+str(people_id)+'_'+sit+'_cam1_720.mp4'
            data_path_cam2 = '../data/'+str(people_id)+'_'+sit+'_cam2_720.mp4'
            time_file_path = '../data/'+str(people_id)+'_'+sit+'.csv'

        result_frames_list_cam1,action_ids_list_cam1 = changeVideo2Tensor(data_path_cam1,time_file_path)
        result_frames_list_cam2,action_ids_list_cam2 = changeVideo2Tensor(data_path_cam2,time_file_path)
        # result_frames_list = result_frames_list_cam1 + result_frames_list_cam2
        # action_ids_list = action_ids_list_cam1 + action_ids_list_cam2
        all_frames += result_frames_list_cam1 + result_frames_list_cam2
        all_action_ids += action_ids_list_cam1 + action_ids_list_cam2
        # if len(all_frames) != 0:
        #     all_frames_tensor = torch.cat(all_frames, dim=0).float()
        #     all_action_ids_tensor = torch.cat(all_action_ids, dim=0)

        #     torch.save(all_frames_tensor, 'frames_train_{}.pth'.format(people_id))
        #     torch.save(all_action_ids_tensor, 'actions_train_{}.pth'.format(people_id))
print("load train data done")

# 将列表转换为Tensor

# all_frames_tensor = torch.cat(all_frames, dim=0).float()/255.
all_action_ids_tensor = torch.cat(all_action_ids, dim=0)

# torch.save(all_frames_tensor, 'frames_train.pth')
# torch.save(all_action_ids_tensor, 'actions_train.pth')

# 读取数据
# all_frames_tensor = torch.load('frames_train.pth')/255.
# all_action_ids_tensor = torch.load('actions_train.pth')
print("uniquetrain",torch.unique(all_action_ids_tensor))
train_dataset = VideoDataset(all_frames,all_action_ids_tensor)
print("train_dataset_prepared")

# print("all_frames_tensor",all_frames_tensor.shape)
print("all_action_ids_tensor",all_action_ids_tensor.shape)

all_frames = []
all_action_ids = []
# [batch_size, channel, temporal_dim, height, width]
for people_id in range(15,18):
    # all_frames = []
    # all_action_ids = []
    # if people_id == 2 or people_id==5:
    #     continue
    for sit in ['glove','noglove']:
        if people_id < 10:
            data_path_cam1 = '../data/0'+str(people_id)+'_'+sit+'_cam1_720.mp4'
            data_path_cam2 = '../data/0'+str(people_id)+'_'+sit+'_cam2_720.mp4'
            time_file_path = '../data/0'+str(people_id)+'_'+sit+'.csv'
        else:
            data_path_cam1 = '../data/'+str(people_id)+'_'+sit+'_cam1_720.mp4'
            data_path_cam2 = '../data/'+str(people_id)+'_'+sit+'_cam2_720.mp4'
            time_file_path = '../data/'+str(people_id)+'_'+sit+'.csv'
        result_frames_list_cam1,action_ids_list_cam1 = changeVideo2Tensor(data_path_cam1,time_file_path)
        result_frames_list_cam2,action_ids_list_cam2 = changeVideo2Tensor(data_path_cam2,time_file_path)

        all_frames += result_frames_list_cam1 + result_frames_list_cam2
        all_action_ids += action_ids_list_cam1 + action_ids_list_cam2

print("load test data done")

all_action_ids_tensor = torch.cat(all_action_ids, dim=0)

print("uniquetest",torch.unique(all_action_ids_tensor))
test_dataset = VideoDataset(all_frames,all_action_ids_tensor)

# print("testall_frames_tensor",all_frames_tensor.shape)
print("testall_action_ids_tensor",all_action_ids_tensor.shape)

train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)

classify_model = Mlp(1024,9,9).to(device)
# classify_model = nn.DataParallel(classify_model)  # 包装模型以使用DataParallel
classify_model.to(device)
backbone = model.backbone

optimizer = torch.optim.AdamW(classify_model.parameters(), lr=3e-4)

losses = []
# 开始训练
for epoch in range(100):  # 设定训练轮数为100，可以根据需要进行调整
    classify_model.train()  # 设置模型为训练模式
    running_loss = 0.0
    for i, data in enumerate(train_loader):
        inputs, labels = data
        inputs = inputs.to(device)
        labels = labels.to(device)

        # 传递输入数据到模型
        backbone.eval()
        with torch.no_grad():
            features = backbone(inputs)
            features = features.mean(dim=[2,3,4])
        outputs = classify_model(features)

        # 计算损失
        loss_fn = nn.CrossEntropyLoss()
        loss = loss_fn(outputs, labels)

        # 反向传播并优化
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        # print("loss",loss)
        running_loss += loss.item()
    average_loss = running_loss / len(train_loader)
    losses.append(average_loss)
    # 打印每个epoch的loss
    print('[%d] Loss: %.3f' % (epoch + 1, running_loss / len(train_loader)))

print('Finished Training')
plt.figure(figsize=(10, 5))
plt.plot(losses)
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig('training_loss.png')

# 开始评估
classify_model.eval()  # 设置模型为评估模式
correct = 0
total = 0

torch.cuda.empty_cache()

with torch.no_grad():  # 在评估阶段，不需要计算梯度
    for data in test_loader:
        inputs, labels = data
        inputs = inputs.to(device)
        labels = labels.to(device)

        # 传递输入数据到模型
        backbone.eval()
        with torch.no_grad():
            features = backbone(inputs)
            features = features.mean(dim=[2,3,4])
        outputs = classify_model(features)

        # 获取预测结果
        _, predicted = torch.max(outputs.data, 1)

        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy on the test set: %d %%' % (100 * correct / total))


#!/usr/bin/env python3
"""mapper.py
With help from
https://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/
"""

import sys
from string import punctuation

with open("stopwords.txt", "r") as f:
    stop_words = f.read().split("\n")

byte_offset = 0

for line in sys.stdin:
    line = line.strip().lower()
    words: list[str] = line.split()

    for word in words:
        new_word = word.strip().strip(punctuation)
        if (
            new_word
            and new_word not in stop_words
            and "-" not in new_word
            and "." not in new_word
            and "," not in new_word
            and "[" not in new_word
            and "]" not in new_word
            and "/" not in new_word
        ):
            print(f"{new_word}\t{byte_offset}")

        byte_offset += len(word) + 1


# Реализуйте свой аналог генераторной функции range(). Да, вы теперь
# знаете, что это - генератор.

def analog_range(start, stop, step):
    while start <= stop:
        yield start
        start += step
    return

for i in analog_range(0, 100, 5):
    print(i, end=' ')
# два варианта вывода инфы
print('\n', *analog_range(0, 100, 5))


from typing import List

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        # DP
        m = len(obstacleGrid)
        if m ==0: return 0
        n = len(obstacleGrid[0])
        if n == 0: return 0

        table = [[0]*n for i in range(m)]
        for j in range(n):
            if obstacleGrid[0][j] == 1 or (table[0][j-1] ==0 and j > 0):
                table[0][j] = 0
            else:
                table[0][j] = 1

        for i in range(1, m):
            for j in range(n):
                if(obstacleGrid[i][j] == 1):
                    table[i][j] = 0
                    continue
                table[i][j] = table[i-1][j]
                if j > 0:
                    table[i][j] += table[i][j-1]
        # print(table)
        return table[m-1][n-1]

instance = Solution()
print(instance.uniquePathsWithObstacles([[0,0,0],[0,1,0],[0,0,0]]))

print(instance.uniquePathsWithObstacles([[0,1],[0,0]]))


#!/usr/bin/env python
# coding: utf-8

# Madhur Tandon, Biz2Credit

import click
import os
import glob
import traceback
from bottle import Bottle, run, request, HTTPResponse
from constants import APP_HOST, APP_PORT, DIRECTORY
from REST.request import validate
from REST.response import error, http, success
from Converter import convert
from Parser import ParseBank
from Logger import error as error_logger
from Exception import ScannedPDFException, InvalidBankNameException

app = Bottle()

@app.route('/api/v1/health')
def health():
    return HTTPResponse(status=200, body='Healthy')

@app.route('/api/v1/analyser', method='POST')
def analyser():
    """
    Analyser analyse the bank statement
    Converter convert this into a xml
    Parser parse the xml and generates the data dictionary
    :return: HTTPResponse
    """
    errors = validate(request)
    if errors:
        return http(422, error(errors, 422))

    # Upload and save the incoming file to the temp directory
    upload = request.files.get('upload')
    if not os.path.exists('temp'):
        os.makedirs('temp')

    # Generate the directory path for pdf and xml
    name, ext = os.path.splitext(upload.filename)
    directory_path = os.path.join(DIRECTORY, name)
    pdf_path = directory_path + '.pdf'
    xml_path = directory_path + '.xml'

    # Save the file
    upload.save(pdf_path, overwrite=True)

    try:
        # Converting the pdf into a xml and saving it into a temp directory
        convert(pdf_path, xml_path)

        # Initiate the parser to parse the xml and generate the data dictionary
        parsebank_obj = ParseBank()
        data = parsebank_obj.parse(xml_path)

    except InvalidBankNameException as exception:
        exp = dict(invalid_data=str(exception))
        return http(400, error(exp, 400))
    except ScannedPDFException as exception:
        exp = dict(resource_not_found=str(exception))
        return http(404, error(exp, 404))
    except Exception as exception:
        error_logger(traceback.format_exc())
        error_logger(str(exception))
        exp = dict(internal_server_error='Failed to analyse the bank statement')
        return http(500, error(exp, 500))
    finally:
        # Remove temp directory after getting transactions
        if os.path.exists('temp'):
            for filename in glob.glob(directory_path + "*"):
                os.remove(filename)

    return http(201, success(201, data, 'analyser'))

@click.command()
@click.option('--host', help='Host', default=APP_HOST)
@click.option('--port', help='Post', default=APP_PORT)
def start(host, port):
    run(app=app, host=host, port=port, reloader=True)

if __name__ == '__main__':
    start()


# -*- coding: utf-8 -*-
# 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/.

# -*- coding: utf-8 -*-
# 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 json
from pathlib import Path

import mozunit
import pytest
from mozilla_version.gecko import GeckoVersion

from mozrelease.buglist_creator import (
    create_bugs_url,
    get_bugs_in_changeset,
    get_previous_tag_version,
    is_backout_bug,
    is_excluded_change,
    parse_tag_version,
    tag_version,
)

DATA_PATH = Path(__file__).with_name("data")

def test_beta_1_release():
    buglist_str_54_0b1 = create_bugs_url(
        product="firefox",
        current_version=GeckoVersion.parse("54.0b1"),
        current_revision="cf76e00dcd6f",
    )
    assert buglist_str_54_0b1 == "", "There should be no bugs to compare for beta 1."

@pytest.mark.parametrize(
    "description,is_excluded",
    (
        (
            "something something something a=test-only something something something",
            True,
        ),
        ("this is a a=release change!", True),
    ),
)
def test_is_excluded_change(description, is_excluded):
    assert is_excluded_change({"desc": description}) == is_excluded

@pytest.mark.parametrize(
    "description,is_backout",
    (
        ("I backed out this bug because", True),
        ("Backing out this bug due to", True),
        ("Backout bug xyz", True),
        ("Back out bug xyz", True),
        ("this is a regular bug description", False),
    ),
)
def test_is_backout_bug(description, is_backout):
    assert is_backout_bug(description) == is_backout

@pytest.mark.parametrize(
    "product,version,tag",
    (
        ("firefox", GeckoVersion.parse("53.0b10"), "FIREFOX_53_0b10_RELEASE"),
        ("firefox", GeckoVersion.parse("52.0"), "FIREFOX_52_0_RELEASE"),
        ("fennec", GeckoVersion.parse("52.0.2"), "FENNEC_52_0_2_RELEASE"),
    ),
)
def test_tag_version(product, version, tag):
    assert tag_version(product, version) == tag

@pytest.mark.parametrize(
    "tag,version",
    (
        ("FIREFOX_53_0b10_RELEASE", GeckoVersion.parse("53.0b10")),
        ("FIREFOX_52_0_RELEASE", GeckoVersion.parse("52.0")),
        ("FENNEC_52_0_2_RELEASE", GeckoVersion.parse("52.0.2")),
    ),
)
def test_parse_tag_version(tag, version):
    assert parse_tag_version(tag) == version

@pytest.mark.parametrize(
    "version,tag,previous_tag",
    (
        (
            GeckoVersion.parse("48.0b4"),
            "FIREFOX_48_0b4_RELEASE",
            "FIREFOX_48_0b3_RELEASE",
        ),
        (
            GeckoVersion.parse("48.0b9"),
            "FIREFOX_48_0b9_RELEASE",
            "FIREFOX_48_0b7_RELEASE",
        ),
        (
            GeckoVersion.parse("48.0.2"),
            "FIREFOX_48_0_2_RELEASE",
            "FIREFOX_48_0_1_RELEASE",
        ),
        (
            GeckoVersion.parse("48.0.1"),
            "FIREFOX_48_0_1_RELEASE",
            "FIREFOX_48_0_RELEASE",
        ),
    ),
)
def test_get_previous_tag_version(version, tag, previous_tag):
    product = "firefox"
    ff_48_tags = [
        u"FIREFOX_BETA_48_END",
        u"FIREFOX_RELEASE_48_END",
        u"FIREFOX_48_0_2_RELEASE",
        u"FIREFOX_48_0_2_BUILD1",
        u"FIREFOX_48_0_1_RELEASE",
        u"FIREFOX_48_0_1_BUILD3",
        u"FIREFOX_48_0_RELEASE",
        u"FIREFOX_48_0_BUILD2",
        u"FIREFOX_RELEASE_48_BASE",
        u"FIREFOX_48_0b10_RELEASE",
        u"FIREFOX_48_0b10_BUILD1",
        u"FIREFOX_48_0b9_RELEASE",
        u"FIREFOX_48_0b9_BUILD1",
        u"FIREFOX_48_0b7_RELEASE",
        u"FIREFOX_48_0b7_BUILD1",
        u"FIREFOX_48_0b6_RELEASE",
        u"FIREFOX_48_0b6_BUILD1",
        u"FIREFOX_48_0b5_RELEASE",
        u"FIREFOX_48_0b5_BUILD1",
        u"FIREFOX_48_0b4_RELEASE",
        u"FIREFOX_48_0b4_BUILD1",
        u"FIREFOX_48_0b3_RELEASE",
        u"FIREFOX_48_0b3_BUILD1",
        u"FIREFOX_48_0b2_RELEASE",
        u"FIREFOX_48_0b2_BUILD2",
        u"FIREFOX_48_0b1_RELEASE",
        u"FIREFOX_48_0b1_BUILD2",
        u"FIREFOX_AURORA_48_END",
        u"FIREFOX_BETA_48_BASE",
        u"FIREFOX_AURORA_48_BASE",
    ]

    mock_hg_json = {"tags": [{"tag": ff_48_tag} for ff_48_tag in ff_48_tags]}

    assert get_previous_tag_version(product, version, tag, mock_hg_json) == previous_tag

def test_get_bugs_in_changeset():
    with DATA_PATH.joinpath("buglist_changesets.json").open("r") as fp:
        changeset_data = json.load(fp)
    bugs, backouts = get_bugs_in_changeset(changeset_data)

    assert bugs == {
        u"1356563",
        u"1348409",
        u"1341190",
        u"1360626",
        u"1332731",
        u"1328762",
        u"1355870",
        u"1358089",
        u"1354911",
        u"1354038",
    }
    assert backouts == {u"1337861", u"1320072"}

if __name__ == "__main__":
    mozunit.main()


import random # for generating random numbers
import sys
import pip  # we will use sys.exit to exit the program
import pygame
from pygame.locals import *
from scipy import rand  #basix pygame imports

#global variables for the game

FPS = 32
Screenwidth = 289
Screenheight = 511

screen = pygame.display.set_mode((Screenwidth, Screenheight))
groundY =  Screenheight * 0.8
game_images = {}
game_audio = {}
player = 'Gallery/images/bird.png'
background = 'Gallery/images/background.png'
PIPE = 'Gallery/images/pipe.png'

def welcomescreen():
    #show welcome image on the screen
    playerx = int(Screenwidth/5)
    playery = int((Screenheight - game_images['player'].get_height())/2)
    messagex = int((Screenwidth - game_images['message'].get_width())/2)
    messagey = int(Screenheight * 0.13)
    basex = 0
    while True:
        for event in pygame.event.get():
        #if user clicks on Cross button, close the game
            if event.type == QUIT or (event.type==KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
          # if user press space or up key , start the game
            elif event.type == KEYDOWN and (event.key==K_SPACE or event.key == K_UP):
                return
            else:
                screen.blit(game_images['background'],(0, 0))
                screen.blit(game_images['player'],(playerx, playery))
                screen.blit(game_images['message'],(messagex, messagey))
                screen.blit(game_images['base'],(basex, groundY))
                pygame.display.update()
                FPSCLOCK.tick(FPS)
def mainGame():
    score = 0
    playerx = int(Screenwidth/5)
    playery = int(Screenwidth/2)
    basex = 0
     #create two pipe for blitting on screen

    newPipe1 = getRandomPipe()
    newPipe2 = getRandomPipe()

    upperPipes = [
        {'x': Screenwidth + 200, 'y': newPipe1[0]['y']},
        {'x': Screenwidth + 200 + (Screenwidth / 2), 'y': newPipe2[0]['y']},
    ]
    # my List of lower pipes
    lowerPipes = [
        {'x': Screenwidth + 200, 'y': newPipe1[1]['y']},
        {'x': Screenwidth + 200 + (Screenwidth / 2), 'y': newPipe2[1]['y']},
    ]

    pipevelX = -4

    playerVelY = -9
    PlayerMaxVelY = 10
    PlayerMinVelY = -8
    PlayerAccY= 1

    PlayerFlappAccv = -8 #velocity while flapping
    PlayerFlapped = False # it is only tre when the bird is flapping

    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key ==K_ESCAPE):
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
                if player>0:
                    playerVelY = PlayerFlappAccv
                    PlayerFlapped = True
                    game_audio['wing'].play()

        crashTest = isCollide(playerx , playery,upperPipes, lowerPipes) #this func will return true if the player is crashed

        if crashTest:
            return

        PlayerMidPos = playerx + game_images['plaayer'].get_width()/2
        for pipe in upperPipes:
            pipeMidPos = pipe['x'] + game_images['PIPE'][0].get_width()/2
            if pipeMidPos<= PlayerMidPos < pipeMidPos:
                score +=1
                print(f"your score is {score}")
                game_audio['point'].play()

        if playerVelY< PlayerMaxVelY and not PlayerFlapped:
            playerVelY += PlayerAccY

        if PlayerFlapped:
            PlayerFlapped = False
        playerHeight = game_images['player'].get_height()
        playery = playery + min(playerVelY, groundY - playery - playerHeight)

        #move pipes to the left

        for upperPipe , lowerPipe in zip(upperPipes,lowerPipes):
            upperPipes['x'] += pipevelX
            lowerPipes['x'] += pipevelX

        #if the pipe is out of the screen , remove it
        if 0< upperPipes[0]['x']<5:
            newpipe = getRandomPipe()
            upperPipes.append(newpipe[0])
            lowerPipes.append(newpipe[1])

        if upperPipes[0]['x'] < -game_images['PIPE'][0].get_width():
            upperPipes.pop(0)
            lowerPipes.pop(0)

        #Lets blits our images

        screen.blit(game_images['background'],(0, 0))

        for upperpipe , lowerpipe in zip(upperPipes , lowerPipes):
            screen.blit(game_images['PIPE'][0], (upperPipe['x'], upperPipe['y']))
            screen.blit(game_images['PIPE'][1], (lowerPipe['x'], lowerPipe['y']))

        screen.blit(game_images['base'],(basex, groundY))
        screen.blit(game_images['player'],(playerx, playery))
        myDigits = [int(x) for x in list(str(score))]
        width = 0
        for digit in myDigits:
            width += game_images['numbers'][digit].get_width()
        Xoffset = (Screenwidth - width)/2

        for digit in myDigits:
            screen.blit(game_images['numbers'][digit],(Xoffset , Screenheight*0.12))
            Xoffset += game_images['numbers'][digit].get_width()

        pygame.display.update()
        FPSCLOCK.tick(FPS)
def isCollide(playerx , playery,upperPipes, lowerPipes):
    if playery > groundY - 25 or playery<0:
        game_audio['hit'].play()
        return True

    for pipe in upperPipes:
        pipeHeight = game_images['PIPE'][0].get_height()
        if(playery <pipeHeight + pipe['y'] and abs(playerx - pipe['x'])< game_images['pipe'][0].get_width()):
            game_audio['hit'].play()
            return True

    for pipe in lowerPipes:
        if(playery + game_images['player'].get_height()> pipe['y']) and abs(playerx - pipe['x']) < game_images['pipe'][0].get_width():
            game_audio['hit'].play()
            return True

def getRandomPipe():
    """
    generate position of two pipe
    """
    pipeheight = game_images['PIPE'][0].pygame.font.get_height()
    offset = Screenheight/3
    y2 = offset + random.randrange(0,int(Screenheight - game_images['base'].get_height() - 1.2 *offset))
    pipeX = Screenwidth + 10
    y1 = pipeheight - y2 + offset
    pipe = [
        {'x':pipeX, 'y': -y1},
        {'x':pipeX, 'y':y2}
    ]
    return pipe

if __name__== "__main__":
    pygame.init() #initialize all pygames module
    FPSCLOCK = pygame.time.Clock()
    pygame.display.set_caption('Flappy bird by Saksham')
    game_images['numbers'] = (
        pygame.image.load('Gallery/images/0.png').convert_alpha(),
        pygame.image.load('Gallery/images/1.png').convert_alpha(),
        pygame.image.load('Gallery/images/2.png').convert_alpha(),
        pygame.image.load('Gallery/images/3.png').convert_alpha(),
        pygame.image.load('Gallery/images/4.png').convert_alpha(),
        pygame.image.load('Gallery/images/5.png').convert_alpha(),
        pygame.image.load('Gallery/images/6.png').convert_alpha(),
        pygame.image.load('Gallery/images/7.png').convert_alpha(),
        pygame.image.load('Gallery/images/8.png').convert_alpha(),
        pygame.image.load('Gallery/images/9.png').convert_alpha(),
    )

    game_images['message'] = pygame.image.load('Gallery/images/message.png').convert_alpha()
    game_images['base'] = pygame.image.load('Gallery/images/base.png').convert_alpha()
    game_images['pipe'] =(
        pygame.transform.rotate(pygame.image.load(PIPE).convert_alpha(), 180),
        pygame.image.load(PIPE).convert_alpha()
    )

    game_audio['die'] = pygame.mixer.Sound('Gallery/audio/die.mp3')
    game_audio['hit'] = pygame.mixer.Sound('Gallery/audio/hit.mp3')
    game_audio['swoosh'] = pygame.mixer.Sound('Gallery/audio/swoosh.mp3')
    game_audio['point'] = pygame.mixer.Sound('Gallery/audio/points.mp3')
    game_audio['wing'] = pygame.mixer.Sound('Gallery/audio/swing.mp3')

    game_audio['background'] = pygame.image.load(background).convert()
    game_audio['player'] = pygame.image.load(background).convert_alpha()

    while True:
        welcomescreen()
        mainGame()


from pydesim import Model

from pycsmaca.simulations.modules import RandomSource, WiredTransceiver, Queue, \
    WiredInterface
from pycsmaca.simulations.modules.station import Station

class WiredLineNetwork(Model):
    def __init__(self, sim):
        super().__init__(sim)

        if sim.params.num_stations < 2:
            raise ValueError('minimum number of stations in network is 2')

        # Reading parameters and building stations:
        stations = []
        next_address, n = 1, sim.params.num_stations
        destination_address = 2 + (n - 2) * 2
        for i in range(n):
            if i in sim.params.active_sources:
                source = RandomSource(
                    sim, sim.params.payload_size, sim.params.source_interval,
                    source_id=i, dest_addr=destination_address
                )
            else:
                source = None

            # Building wired interfaces:
            interfaces = []
            for _ in range(2 if 0 < i < n - 1 else 1):
                transceiver = WiredTransceiver(
                    sim, bitrate=sim.params.bitrate,
                    header_size=sim.params.header_size,
                    preamble=sim.params.preamble,
                    ifs=sim.params.ifs,
                )
                queue = Queue(sim)
                iface = WiredInterface(sim, next_address, queue, transceiver)
                next_address += 1
                interfaces.append(iface)

            # Building station:
            sta = Station(sim, source=source, interfaces=interfaces)
            stations.append(sta)

            # Writing routing table for all station except the last one:
            if i < n - 1:
                out_iface = interfaces[-1]
                switch_conn = sta.get_switch_connection_for(out_iface)
                sta.switch.table.add(
                    destination_address,
                    switch_conn.name,
                    out_iface.address + 1
                )

        # Adding stations as children:
        self.children['stations'] = stations

        # Connecting stations interfaces in chain:
        for i in range(n - 1):
            if1, if2 = stations[i].interfaces[-1], stations[i+1].interfaces[0]
            conn = if1.connections.set('wire', if2, rname='wire')
            conn.delay = sim.params.distance / sim.params.speed_of_light

    @property
    def stations(self):
        return self.children['stations']

    @property
    def clients(self):
        return self.stations[:-1]

    @property
    def server(self):
        return self.stations[-1]

    @property
    def num_stations(self):
        return len(self.stations)

    def get_tx_iface(self, sta_index):
        if sta_index < self.num_stations - 1:
            return self.stations[sta_index].interfaces[-1]
        raise ValueError(
            f'TX interface defined for the first {self.num_stations - 1} '
            f'stations only')

    def get_rx_iface(self, sta_index):
        if sta_index > 0:
            return self.stations[sta_index].interfaces[0]
        raise ValueError(
            f'RX interface defined for the last {self.num_stations - 1} '
            f'stations only')

    def __str__(self):
        return 'Network'

    def print_children(self):
        def get_all_leafs(module):
            children = [module]
            for child in module.children.all():
                children.extend(get_all_leafs(child))
            return children

        print('Network components:')
        for m in get_all_leafs(self):
            print(f'+ {m}')

    # noinspection PyTypeChecker
    def describe_topology(self):
        def str_sid(c):
            return c.source.source_id if c.source else '<NONE>'

        def str_ifaces(c):
            _prefix = "\n\t\t\t"
            return _prefix + _prefix.join([
                f'[addr:{iface.address}], '
                f'connected to: {iface.connections["wire"].module.address}'
                for i, iface in enumerate(c.interfaces)
            ])

        def str_sw_table(c):
            d = c.switch.table.as_dict()
            if not d:
                return 'EMPTY'
            return "\n\t\t\t" + "\n\t\t\t".join([
                f'{key} via {val[1]} (interface "{val[0]}")'
                for key, val in d.items()
            ])

        s1 = 'NETWORK TOPOLOGY'
        s2 = f'- num stations: {self.num_stations}'
        s3 = '- clients:\n\t' + '\n\t'.join([
            f'{i}: SID={str_sid(cli)}\n\t\t- interfaces: {str_ifaces(cli)}'
            f'\n\t\t- switching table:{str_sw_table(cli)}'
            for i, cli in enumerate(self.clients)
        ])
        s4 = f'- server:\n\t\t- interfaces: {str_ifaces(self.server)}'
        return '\n'.join([s1, s2, s3, s4])

    # noinspection PyUnresolvedReferences
    def get_stats(self):
        from collections import namedtuple
        client_fields = [
            'index', 'service_time', 'queue_size', 'tx_busy', 'rx_busy',
            'arrival_intervals', 'num_packets_sent', 'delay', 'sid',
        ]
        server_fields = [
            'arrival_intervals', 'num_packets_received',
        ]
        client_class = namedtuple('Client', client_fields)
        server_class = namedtuple('Server', server_fields)

        _client_sources = [cli.source for cli in self.clients]
        _client_ifaces = [(cli.interfaces[0], cli.interfaces[-1])
                          for cli in self.clients]
        _srv = self.server

        clients = [
            client_class(
                index=i,
                service_time=out_if.transceiver.service_time.mean(),
                queue_size=out_if.queue.size_trace.timeavg(),
                tx_busy=out_if.transceiver.tx_busy_trace.timeavg(),
                rx_busy=inp_if.transceiver.rx_busy_trace.timeavg(),
                arrival_intervals=(
                    src.arrival_intervals.statistic().mean() if src else None),
                num_packets_sent=out_if.transceiver.num_transmitted_packets,
                delay=(
                    _srv.sink.source_delays.get(
                        src.source_id).mean() if src else None),
                sid=(src.source_id if src else None),
            ) for i, (src, (inp_if, out_if)) in enumerate(
                zip(_client_sources, _client_ifaces))
        ]
        server = server_class(
            arrival_intervals=_srv.sink.arrival_intervals.statistic().mean(),
            num_packets_received=_srv.sink.num_packets_received,
        )
        return (client_fields, clients), (server_fields, server)


from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from .models import PlayerDB
from .views import SimpleGameList
from .serializers import PlayerSerializer
from rest_framework.test import APIRequestFactory, force_authenticate

class TestPlayers(TestCase):
    """
    Sample for testing
    """

    def setUp(self):
        self.request_factory = APIRequestFactory()
        self.user = PlayerDB(name='user1', game='game', round_1='user1', round_2='user2', round_3='user')
        self.user._set_pk_val(1)
        self.user.save()

    def test_get_all_players(self):
        todo1 = PlayerDB(name='Luis')
        todo1.save()
        todo2 = PlayerDB(name='Juan')
        todo2.save()

        request = self.request_factory.get(reverse('players'), format='json')
        force_authenticate(request, user=self.user)
        view = SimpleGameList.as_view()
        response = view(request)
        response.render()
        players = PlayerDB.objects.all()
        serializer = PlayerSerializer(players, many=True, context={'request': request})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)