# OpenBCI Experiment: ExternalTriggerCreator
# A python script to customize video components and external trigger for EEG experiments

# Date: 06/09/2020
# Author: Fan Li

import cv2
import glob
import argparse
import json
import random
import copy
import os
import numpy as np

random.seed(30)

def resize_image(old_size, new_size):
    ratio_y = new_size[0] / old_size[0]
    ratio_x = new_size[1] / old_size[1]

    if ratio_y > ratio_x:
        y = old_size[0] * ratio_x
        x = new_size[1]
    else:
        y = new_size[0]
        x = old_size[1] * ratio_y

    size = (int(y), int(x), new_size[2])
    return size

def embed_trigger(img, background_size, trigger_position, trigger_color):
    # background
    background = np.zeros(background_size, dtype="uint8")

    background[:] = (0, 0, 0)

    background[
    int(background_size[0] / 2) - int(img.shape[0] / 2):int(background_size[0] / 2) - int(img.shape[0] / 2) + img.shape[
        0], int(background_size[1] / 2) - int(img.shape[1] / 2): int(background_size[1] / 2) - int(img.shape[1] / 2) +
                                                                 img.shape[1]] = img

    # trigger
    background[trigger_position[1]:trigger_position[3], trigger_position[0]:trigger_position[2]] = (trigger_color)
    return background

###
def create_session(kitten_image_path_list, puppy_image_path_list, frame_numbers, fps, trigger_position):
    frames_array = []
    label_array = []
    label_index = []
    filename_array = []

    # kitten
    for filename in kitten_image_path_list:

        # add current image
        img = cv2.imread(filename)
        frames, label = create_frames(img, frame_numbers, "Kitten", fps, trigger_position)
        frames_array.append(frames)

        label_array.append(label)
        label_index.append(1)
        filename_array.append(filename)

    # puppy
    for filename in puppy_image_path_list:
        img = cv2.imread(filename)
        frames, label = create_frames(img, frame_numbers, "Puppy", fps, trigger_position)
        frames_array.append(frames)
        label_array.append(label)
        label_index.append(2)
        filename_array.append(filename)

    # pack
    z = list(zip(frames_array, label_array, label_index, filename_array))
    random.shuffle(z)
    new_frame_array, new_label_array, new_label_index_array, new_filename_array = zip(*z)

    new_frame_array = list(new_frame_array)
    new_label_array = list(new_label_array)
    new_label_index_array = list(new_label_index_array)
    new_filename_array = list(new_filename_array)
    # add test
    test_frame_numbers = fps * 1
    frames, label = create_frames(img, frame_numbers, "Test", fps, trigger_position)
    print(new_frame_array[:3])
    new_frame_array.append(frames)
    new_label_array.append(label)
    new_label_index_array.append(3)
    new_filename_array.append(filename)

    return new_frame_array, new_label_array, new_label_index_array, new_filename_array

def create_video(image_base_path, fps, flick_times, screen_size, time_range_per_image, video_output, label_output,
                 trigger_position):
    screen_size = tuple(screen_size)
    img_array = []

    kitten_image_path_list = glob.glob(image_base_path + 'Image_Class/' + 'Kitten' + '/*.jpg')
    puppy_image_path_list = glob.glob(image_base_path + 'Image_Class/' + 'Puppy' + '/*.jpg')

    # session 1 1    s
    frame_numbers = int(fps * 1)
    # puppy_image_path = puppy_image_path_list[1]
    frame_array_session_1, label_array_session_1, label_index_array_session_1, filename_array_session_1 = create_session(
        kitten_image_path_list, puppy_image_path_list, frame_numbers, fps, trigger_position)

    # session 2 0.75 s
    frame_numbers = int(fps * 0.75)
    # puppy_image_path = puppy_image_path_list[2]
    frame_array_session_2, label_array_session_2, label_index_array_session_2, filename_array_session_2 = create_session(
        kitten_image_path_list, puppy_image_path_list, frame_numbers, fps, trigger_position)

    # session 3 0.5  s
    frame_numbers = int(fps * 0.5)
    # puppy_image_path = puppy_image_path_list[3]
    frame_array_session_3, label_array_session_3, label_index_array_session_3, filename_array_session_3 = create_session(
        kitten_image_path_list, puppy_image_path_list, frame_numbers, fps, trigger_position)

    # session 4 0.25 s
    frame_numbers = int(fps * 0.25)
    # puppy_image_path = puppy_image_path_list[4]
    frame_array_session_4, label_array_session_4, label_index_array_session_4, filename_array_session_4 = create_session(
        kitten_image_path_list, puppy_image_path_list, frame_numbers, fps, trigger_position)

    new_frame_array = frame_array_session_1 + frame_array_session_2 + frame_array_session_3 + frame_array_session_4
    new_label_array = label_array_session_1 + label_array_session_2 + label_array_session_3 + label_array_session_4
    new_label_index_array = label_index_array_session_1 + label_index_array_session_2 + label_index_array_session_3 + label_index_array_session_4
    new_filename_array = filename_array_session_1 + filename_array_session_2 + filename_array_session_3 + filename_array_session_4

    for frames in new_frame_array:
        for image in frames:
            img_array.append(image)
            # print("output shape: ", image[0].shape)

    with open(label_output, 'w') as handle:
        for i in range(len(new_label_array)):
            handle.write("%s," % new_label_index_array[i])
            handle.write("%s," % new_label_array[i])
            local_file_name = new_filename_array[i].split("\\")[-1]
            handle.write("%s," % local_file_name)
            handle.write("\n")

    # Welcome frames
    welcome_array = []

    img = cv2.imread(image_base_path + 'Welcome/welcome2OpenBCI.jpg')

    ######
    new_size = (1080, 1350, 3)
    background_size = (1080, 1920, 3)
    old_size = img.shape
    size = resize_image(old_size, new_size)

    img_new = cv2.resize(img, (size[1], size[0]))

    img_white = embed_trigger(img_new, background_size, trigger_position, (255, 255, 255))
    img_black = embed_trigger(img_new, background_size, trigger_position, (0, 0, 0))

    for i in range(3 * fps):
        welcome_array.append(img_black)

    img_1 = cv2.imread(image_base_path + 'Welcome/1.PNG')
    old_size = img_1.shape
    size = resize_image(old_size, new_size)
    img_new_1 = cv2.resize(img_1, (size[1], size[0]))
    img_1_black = embed_trigger(img_new_1, background_size, trigger_position, (0, 0, 0))
    for i in range(3 * fps):
        welcome_array.append(img_1_black)

    img_2 = cv2.imread(image_base_path + 'Welcome/2.PNG')
    old_size = img_2.shape
    size = resize_image(old_size, new_size)
    img_new_2 = cv2.resize(img_2, (size[1], size[0]))
    img_2_black = embed_trigger(img_new_2, background_size, trigger_position, (0, 0, 0))
    for i in range(3 * fps):
        welcome_array.append(img_2_black)

    img_2 = cv2.imread(image_base_path + 'Welcome/2.PNG')

    for j in range(flick_times):
        for i in range(int(0.05 * fps)):
            welcome_array.append(img_black)
        for i in range(int(0.05 * fps)):
            welcome_array.append(img_white)

    # ending frames
    ending_array = []
    for j in range(flick_times):
        for i in range(int(0.05 * fps)):
            ending_array.append(img_black)
        for i in range(int(0.05 * fps)):
            ending_array.append(img_white)

    img_array = welcome_array + img_array + ending_array
    print(len(welcome_array), len(img_array))

    out = cv2.VideoWriter(video_output, cv2.VideoWriter_fourcc(*'FMP4'), fps, screen_size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()

def create_frames(img, frame_num, label, fps, trigger_position):
    new_size = (1080, 1350, 3)
    background_size = (1080, 1920, 3)
    old_size = img.shape
    adjusted_size = resize_image(old_size, new_size)

    img_new = cv2.resize(img, (adjusted_size[1], adjusted_size[0]))

    print("Output Shape: ", img_new.shape)

    img_white = embed_trigger(img_new, background_size, trigger_position, (255, 255, 255))
    img_black = embed_trigger(img_new, background_size, trigger_position, (0, 0, 0))
    cv2.imwrite("test.jpg", img_white)

    # generate fixation across
    cross = np.zeros((1080, 1920, 3), np.uint8)
    cv2.line(cross, (950, 540), (970, 540), (0, 0, 255), 2)
    cv2.line(cross, (960, 530), (960, 550), (0, 0, 255), 2)

    # embed

    frame_array = []
    # random interval
    random_interval = random.randint(-2, 2) / 10
    # add cross before current image
    frame_array += [cross] * int((0.5 + random_interval) * fps)

    # add current image
    for i in range(int(0.05 * fps)):
        frame_array.append(img_white)
    for i in range(frame_num):
        frame_array.append(img_black)
    for i in range(int(0.05 * fps)):
        frame_array.append(img_black)

    # random interval
    # random_interval = random.randint(-2, 2) / 10

    # add cross after current image
    frame_array += [cross] * int((0.5 + random_interval) * fps)
    print(int((0.5 + random_interval) * fps))

    return frame_array, label

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--load_json", help="load json to parse args")
    args = parser.parse_args()
    if args.load_json:
        with open(args.load_json, 'rt') as f:
            t_args = argparse.Namespace()
            t_args.__dict__.update(json.load(f))
            args = parser.parse_args(namespace=t_args)
    create_video(args.image_base_path,
                 args.fps,
                 args.flick_times,
                 args.screen_size,
                 args.time_range_per_image,
                 args.video_output,
                 args.label_output,
                 args.trigger_position)


import unittest
from parse import parse
import copy
from pprint import pprint

import time

def input_file():
    # return the input_test file in a text
    file = open('input', 'r')
    text = file.read()
    file.close()
    return text

def output_file():
    # read line of output_1 file
    file = open('output', 'r')
    res = [line.rstrip('\n') for line in file]
    file.close()
    return res

class TestDay12part3(unittest.TestCase):

    def test_day_12_part_3(self):
        text = input_file()

        def get_current_velocity(current_moon, moons):
            x_vel, y_vel, z_vel = (0, 0, 0)
            for moon in moons:
                if current_moon != moon:
                    # velocity x
                    if current_moon[0] < moon[0]:
                        x_vel += 1
                    elif current_moon[0] > moon[0]:
                        x_vel -= 1
                    # velocity y
                    if current_moon[1] < moon[1]:
                        y_vel += 1
                    elif current_moon[1] > moon[1]:
                        y_vel -= 1
                    # velocity x
                    if current_moon[2] < moon[2]:
                        z_vel += 1
                    elif current_moon[2] > moon[2]:
                        z_vel -= 1
            return [x_vel, y_vel, z_vel]

        def get_new_position_using_velocity(moon, velocity):
            x, y, z = moon
            x_vel, y_vel, z_vel = velocity
            return [x + x_vel, y + y_vel, z + z_vel]

        def get_new_moons_velocity(moons, moons_velocity):
            new_moons_velocity = []
            for i_m, moon in enumerate(moons):
                x_vel, y_vel, z_vel = moons_velocity[i_m]
                new_x_vel, new_y_vel, new_z_vel = get_current_velocity(moon, moons)
                new_moons_velocity.append((x_vel + new_x_vel, y_vel + new_y_vel, z_vel + new_z_vel))
            return new_moons_velocity

        def set_new_moons_velocity(moons, moons_velocity):
            for i_m, moon in enumerate(moons):
                x_vel, y_vel, z_vel = moons_velocity[i_m]
                new_x_vel, new_y_vel, new_z_vel = get_current_velocity(moon, moons)
                moons_velocity[i_m] = [x_vel + new_x_vel, y_vel + new_y_vel, z_vel + new_z_vel]

        def get_new_moons_position(moons, moons_velocity):
            new_moons_position = []
            for i_m, moon in enumerate(moons):
                new_moons_position.append(get_new_position_using_velocity(moon, moons_velocity[i_m]))
            return new_moons_position

        def set_new_moons_position(moons, moons_velocity):
            for i_m, moon in enumerate(moons):
                moons[i_m] = get_new_position_using_velocity(moons[i_m], moons_velocity[i_m])

        def get_potential_or_kinetic_energy(object):
            x, y, z = object
            return abs(x) + abs(y) + abs(z)

        def get_energy(moon, moon_velocity):
            return get_potential_or_kinetic_energy(moon) * get_potential_or_kinetic_energy(moon_velocity)

        def get_total_energy(moons, moons_velocity):
            total = 0
            for i_m, moon in enumerate(moons):
                total += get_energy(moons[i_m], moons_velocity[i_m])
            return total

        def is_beg(moons_velocity):
            for moon_velocity in moons_velocity:
                x, y, z = moon_velocity
                if abs(x) + abs(y) + abs(z) != 0:
                    return False
            return True

        lines = text.split("\n")
        moons = []
        moons_velocity = []
        # step 0
        for line in lines:
            r, x, y, z = line.split("=")
            x = int(x.split(",")[0])
            y = int(y.split(",")[0])
            z = int(z.split(">")[0])
            moons.append([x, y, z])
            moons_velocity.append([0, 0, 0])
        #print("Step 0")
        #for i_m, moon in enumerate(moons):
        #    print(moons[i_m], moons_velocity[i_m])
        moons_velocity = get_new_moons_velocity(moons, moons_velocity)
        i = 0
        nb_steps = 1000000000000
        start = time.time()
        while i < nb_steps:
            # step 1
            # update moons
            set_new_moons_position(moons, moons_velocity)
            # update velocities
            #if i % 100000 == 0:
            #print("Step", i)
            #for i_m, moon in enumerate(moons):
            #    print(moons[i_m], moons_velocity[i_m])
            #print(get_total_energy(moons, moons_velocity))
            i += 1
            if is_beg(moons_velocity):
                break
            if i < nb_steps:
                set_new_moons_velocity(moons, moons_velocity)
            if i % 1000000 == 0:
                done = time.time()
                elapsed = done - start
                print(i, elapsed)
        print(i*2)

        #print(get_total_energy(moons, moons_velocity))

if __name__ == '__main__':
    unittest.main()


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.11-x86_64/egg/djblets/siteconfig/django_settings.py
# Compiled at: 2019-06-12 01:17:17
"""Utilities for going between SiteConfiguration and Django settings."""
from __future__ import unicode_literals
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.cache import DEFAULT_CACHE_ALIAS
from django.utils import six, timezone
from djblets.cache.backend_compat import normalize_cache_backend
from djblets.cache.forwarding_backend import DEFAULT_FORWARD_CACHE_ALIAS, ForwardingCacheBackend

def _set_cache_backend(settings, key, value):
    """Set the default cache backend.

    This will set the default cache backend to use Djblets's
    :py:class:`~djblets.cache.forwarding_backend.ForwardingCacheBackend`,
    and use that to forward on to the provided cache backend.

    The new cache settings will be instantly usable by the application
    without having to restart.

    Args:
        settings (django.conf.LazySettings):
            The Django settings object.

        key (unicode, unused):
            The settings key (``CACHES``).

        value (object):
            The cache backend settings. This may be a legacy URI or a
            dictionary containing cache backend information.
    """
    settings.CACHES.update({DEFAULT_FORWARD_CACHE_ALIAS: normalize_cache_backend(value, DEFAULT_FORWARD_CACHE_ALIAS) or normalize_cache_backend(value),
       DEFAULT_CACHE_ALIAS: {b'BACKEND': b'%s.%s' % (ForwardingCacheBackend.__module__,
                                        ForwardingCacheBackend.__name__),
                             b'LOCATION': DEFAULT_FORWARD_CACHE_ALIAS}})
    from django.core.cache import cache
    if isinstance(cache, ForwardingCacheBackend):
        cache.reset_backend()

def _set_static_url(settings, key, value):
    """Set the URL for static media.

    Args:
        settings (django.conf.LazySettings):
            The Django settings object.

        key (unicode, unused):
            The settings key (``STATIC_URL``).

        value (object):
            The new static URL.
    """
    settings.STATIC_URL = value
    staticfiles_storage.base_url = value

def _set_timezone(settings, key, value):
    """Set the server's timezone.

    Args:
        settings (django.conf.LazySettings):
            The Django settings object.

        key (unicode, unused):
            The settings key (``TIME_ZONE``).

        value (object):
            The new timezone.
    """
    settings.TIME_ZONE = value
    timezone.activate(settings.TIME_ZONE)

locale_settings_map = {b'locale_timezone': {b'key': b'TIME_ZONE', b'deserialize_func': six.text_type,
                        b'setter': _set_timezone},
   b'locale_language_code': b'LANGUAGE_CODE',
   b'locale_date_format': b'DATE_FORMAT',
   b'locale_datetime_format': b'DATETIME_FORMAT',
   b'locale_default_charset': {b'key': b'DEFAULT_CHARSET', b'deserialize_func': six.text_type},
   b'locale_language_code': b'LANGUAGE_CODE',
   b'locale_month_day_format': b'MONTH_DAY_FORMAT',
   b'locale_time_format': b'TIME_FORMAT',
   b'locale_year_month_format': b'YEAR_MONTH_FORMAT'}
mail_settings_map = {b'mail_server_address': b'SERVER_EMAIL',
   b'mail_default_from': b'DEFAULT_FROM_EMAIL',
   b'mail_host': b'EMAIL_HOST',
   b'mail_port': b'EMAIL_PORT',
   b'mail_host_user': {b'key': b'EMAIL_HOST_USER', b'deserialize_func': bytes},
   b'mail_host_password': {b'key': b'EMAIL_HOST_PASSWORD', b'deserialize_func': bytes},
   b'mail_use_tls': b'EMAIL_USE_TLS'}
site_settings_map = {b'site_media_root': b'MEDIA_ROOT',
   b'site_media_url': b'MEDIA_URL',
   b'site_static_root': b'STATIC_ROOT',
   b'site_static_url': {b'key': b'STATIC_URL', b'setter': _set_static_url},
   b'site_prepend_www': b'PREPEND_WWW',
   b'site_upload_temp_dir': b'FILE_UPLOAD_TEMP_DIR',
   b'site_upload_max_memory_size': b'FILE_UPLOAD_MAX_MEMORY_SIZE'}
cache_settings_map = {b'cache_backend': {b'key': b'CACHES', b'setter': _set_cache_backend},
   b'cache_expiration_time': b'CACHE_EXPIRATION_TIME'}
_django_settings_map = {}

def get_django_settings_map():
    """Return a map of customizable Django settings.

    These maps are passed to other functions, like :py:func:`generate_defaults`
    and :py:func:`apply_django_settings`. Consumers can make their own
    settings map based on this with additional settings they want to provide.

    Each entry maps a siteconfig settings key to either a Django settings key
    or detailed information on serializing/deserializing a Django setting.

    An entry with detailed information represents that as a dictionary
    containing the following fields:

    ``key`` (:py:class:`unicode`):
        The Django settings key.

    ``deserialize_func`` (callable, optional):
        A function taking the value from the siteconfig and returning a value
        usable in Django settings.

    ``setter`` (callable, optional):
        A function taking the Django settings object, the Django settings key,
        and the new value from the siteconfig (after going through
        ``deserialize_func``, if provided). This will set the value in Django's
        settings object.

    Returns:
        dict:
        The resulting settings map. This is generated once and cached for
        future calls.
    """
    if not _django_settings_map:
        _django_settings_map.update(locale_settings_map)
        _django_settings_map.update(mail_settings_map)
        _django_settings_map.update(site_settings_map)
        _django_settings_map.update(cache_settings_map)
    return _django_settings_map

def generate_defaults(settings_map):
    """Return a dictionary of siteconfig defaults for Django settings.

    This will iterate through the provided settings map and return a dictionary
    mapping a siteconfig settings key to the value from Django's settings (if
    one is found).

    Args:
        settings_map (dict):
            A settings map, generated from :py:func:`get_django_settings_map`
            or a similar function.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    defaults = {}
    for siteconfig_key, setting_data in six.iteritems(settings_map):
        if isinstance(setting_data, dict):
            setting_key = setting_data[b'key']
        else:
            setting_key = setting_data
        if hasattr(settings, setting_key):
            defaults[siteconfig_key] = getattr(settings, setting_key)

    return defaults

def get_locale_defaults():
    """Return a dictionary of siteconfig defaults for Django locale settings.

    The generated defaults are specific to the values in
    :py:data:`locale_settings_map`.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    return generate_defaults(locale_settings_map)

def get_mail_defaults():
    """Return a dictionary of siteconfig defaults for Django e-mail settings.

    The generated defaults are specific to the values in
    :py:data:`mail_settings_map`.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    return generate_defaults(mail_settings_map)

def get_site_defaults():
    """Return a dictionary of siteconfig defaults for Django site settings.

    The generated defaults are specific to the values in
    :py:data:`site_settings_map`.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    return generate_defaults(site_settings_map)

def get_cache_defaults():
    """Return a dictionary of siteconfig defaults for Django caching settings.

    The generated defaults are specific to the values in
    :py:data:`cache_settings_map`.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    return generate_defaults(cache_settings_map)

def get_django_defaults():
    """Return a dictionary of siteconfig defaults for Django settings.

    The generated defaults are specific to the values returned by
    :py:func:`get_django_settings_map`.

    Returns:
        dict:
        A dictionary of siteconfig defaults.
    """
    return generate_defaults(get_django_settings_map())

def apply_django_settings(siteconfig, settings_map=None):
    """Apply Django settings stored in the site configuration.

    This takes a siteconfiguration storing Django settings and a settings map,
    applying each of the settings to Django. Setting will generally be stored
    in the Django settings object, but some settings will be specially applied
    based on their rules in the settings map.

    Args:
        siteconfig (djblets.siteconfig.models.SiteConfiguration):
            The site configuration containing the Django settings to apply.

        settings_map (dict, optional):
            A map of siteconfig keys to Django settings information. See
            :py:func:`get_django_settings_map` for details.

            If not provided, the result of :py:func:`get_django_settings_map`
            will be used.
    """
    if settings_map is None:
        settings_map = get_django_settings_map()
    for key, setting_data in six.iteritems(settings_map):
        if key in siteconfig.settings:
            value = siteconfig.get(key)
            setter = setattr
            if isinstance(setting_data, dict):
                setting_key = setting_data[b'key']
                if b'setter' in setting_data:
                    setter = setting_data[b'setter']
                if b'deserialize_func' in setting_data and six.callable(setting_data[b'deserialize_func']):
                    value = setting_data[b'deserialize_func'](value)
            else:
                setting_key = setting_data
            setter(settings, setting_key, value)

    return


import requests
import base64
import hmac
from hashlib import sha1, md5
import time
import os
from xml.etree import ElementTree
import re
import json
import threading

blog_config_file = 'sync_blog_config.json'
assets_config_file = 'sync_assets_config.json'

class OSSBucket(object):
    content_type_map = {
        '.*': 'application/octet-stream',
        '.001': 'application/x-001',
        '.301': 'application/x-301',
        '.323': 'text/h323',
        '.906': 'application/x-906',
        '.907': 'drawing/907',
        '.a11': 'application/x-a11',
        '.acp': 'audio/x-mei-aac',
        '.ai': 'application/postscript',
        '.aif': 'audio/aiff',
        '.aifc': 'audio/aiff',
        '.aiff': 'audio/aiff',
        '.anv': 'application/x-anv',
        '.asa': 'text/asa',
        '.asf': 'video/x-ms-asf',
        '.asp': 'text/asp',
        '.asx': 'video/x-ms-asf',
        '.au': 'audio/basic',
        '.avi': 'video/avi',
        '.awf': 'application/vnd.adobe.workflow',
        '.biz': 'text/xml',
        '.bmp': 'application/x-bmp',
        '.bot': 'application/x-bot',
        '.c4t': 'application/x-c4t',
        '.c90': 'application/x-c90',
        '.cal': 'application/x-cals',
        '.cat': 'application/vnd.ms-pki.seccat',
        '.cdf': 'application/x-netcdf',
        '.cdr': 'application/x-cdr',
        '.cel': 'application/x-cel',
        '.cer': 'application/x-x509-ca-cert',
        '.cg4': 'application/x-g4',
        '.cgm': 'application/x-cgm',
        '.cit': 'application/x-cit',
        '.class': 'java/*',
        '.cml': 'text/xml',
        '.cmp': 'application/x-cmp',
        '.cmx': 'application/x-cmx',
        '.cot': 'application/x-cot',
        '.crl': 'application/pkix-crl',
        '.crt': 'application/x-x509-ca-cert',
        '.csi': 'application/x-csi',
        '.css': 'text/css',
        '.cut': 'application/x-cut',
        '.dbf': 'application/x-dbf',
        '.dbm': 'application/x-dbm',
        '.dbx': 'application/x-dbx',
        '.dcd': 'text/xml',
        '.dcx': 'application/x-dcx',
        '.der': 'application/x-x509-ca-cert',
        '.dgn': 'application/x-dgn',
        '.dib': 'application/x-dib',
        '.dll': 'application/x-msdownload',
        '.doc': 'application/msword',
        '.dot': 'application/msword',
        '.drw': 'application/x-drw',
        '.dtd': 'text/xml',
        '.dwf': 'Model/vnd.dwf',
        # '.dwf': 'application/x-dwf',
        '.dwg': 'application/x-dwg',
        '.dxb': 'application/x-dxb',
        '.dxf': 'application/x-dxf',
        '.edn': 'application/vnd.adobe.edn',
        '.emf': 'application/x-emf',
        '.eml': 'message/rfc822',
        '.ent': 'text/xml',
        '.epi': 'application/x-epi',
        '.eps': 'application/x-ps',
        # '.eps': 'application/postscript',
        '.etd': 'application/x-ebx',
        '.exe': 'application/x-msdownload',
        '.fax': 'image/fax',
        '.fdf': 'application/vnd.fdf',
        '.fif': 'application/fractals',
        '.fo': 'text/xml',
        '.frm': 'application/x-frm',
        '.g4': 'application/x-g4',
        '.gbr': 'application/x-gbr',
        '.	application/x-': '.gif',
        '.gl2': 'application/x-gl2',
        '.gp4': 'application/x-gp4',
        '.hgl': 'application/x-hgl',
        '.hmr': 'application/x-hmr',
        '.hpg': 'application/x-hpgl',
        '.hpl': 'application/x-hpl',
        '.hqx': 'application/mac-binhex40',
        '.hrf': 'application/x-hrf',
        '.hta': 'application/hta',
        '.htc': 'text/x-component',
        '.htm': 'text/html',
        '.html': 'text/html',
        '.htt': 'text/webviewhtml',
        '.htx': 'text/html',
        '.icb': 'application/x-icb',
        '.ico': 'image/x-icon',
        # '.ico': 'application/x-ico',
        '.iff': 'application/x-iff',
        '.ig4': 'application/x-g4',
        '.igs': 'application/x-igs',
        '.iii': 'application/x-iphone',
        '.img': 'application/x-img',
        '.ins': 'application/x-internet-signup',
        '.isp': 'application/x-internet-signup',
        '.IVF': 'video/x-ivf',
        '.java': 'java/*',
        '.jfif': 'image/jpeg',
        '.jpe': 'image/jpeg',
        # '.jpe': 'application/x-jpe',
        '.jpeg': 'image/jpeg',
        '.jpg': 'image/jpeg',
        # '.jpg': 'application/x-jpg',
        '.js': 'application/x-javascript',
        '.jsp': 'text/html',
        '.la1': 'audio/x-liquid-file',
        '.lar': 'application/x-laplayer-reg',
        '.latex': 'application/x-latex',
        '.lavs': 'audio/x-liquid-secure',
        '.lbm': 'application/x-lbm',
        '.lmsff': 'audio/x-la-lms',
        '.ls': 'application/x-javascript',
        '.ltr': 'application/x-ltr',
        '.m1v': 'video/x-mpeg',
        '.m2v': 'video/x-mpeg',
        '.m3u': 'audio/mpegurl',
        '.m4e': 'video/mpeg4',
        '.mac': 'application/x-mac',
        '.man': 'application/x-troff-man',
        '.math': 'text/xml',
        '.mdb': 'application/msaccess',
        # '.mdb': 'application/x-mdb',
        '.mfp': 'application/x-shockwave-flash',
        '.mht': 'message/rfc822',
        '.mhtml': 'message/rfc822',
        '.mi': 'application/x-mi',
        '.mid': 'audio/mid',
        '.midi': 'audio/mid',
        '.mil': 'application/x-mil',
        '.mml': 'text/xml',
        '.mnd': 'audio/x-musicnet-download',
        '.mns': 'audio/x-musicnet-stream',
        '.mocha': 'application/x-javascript',
        '.movie': 'video/x-sgi-movie',
        '.mp1': 'audio/mp1',
        '.mp2': 'audio/mp2',
        '.mp2v': 'video/mpeg',
        '.mp3': 'audio/mp3',
        '.mp4': 'video/mpeg4',
        '.mpa': 'video/x-mpg',
        '.mpd': 'application/vnd.ms-project',
        '.mpe': 'video/x-mpeg',
        '.mpeg': 'video/mpg',
        '.mpg': 'video/mpg',
        '.mpga': 'audio/rn-mpeg',
        '.mpp': 'application/vnd.ms-project',
        '.mps': 'video/x-mpeg',
        '.mpt': 'application/vnd.ms-project',
        '.mpv': 'video/mpg',
        '.mpv2': 'video/mpeg',
        '.mpw': 'application/vnd.ms-project',
        '.mpx': 'application/vnd.ms-project',
        '.mtx': 'text/xml',
        '.mxp': 'application/x-mmxp',
        '.net': 'image/pnetvue',
        '.nrf': 'application/x-nrf',
        '.nws': 'message/rfc822',
        '.odc': 'text/x-ms-odc',
        '.out': 'application/x-out',
        '.p10': 'application/pkcs10',
        '.p12': 'application/x-pkcs12',
        '.p7b': 'application/x-pkcs7-certificates',
        '.p7c': 'application/pkcs7-mime',
        '.p7m': 'application/pkcs7-mime',
        '.p7r': 'application/x-pkcs7-certreqresp',
        '.p7s': 'application/pkcs7-signature',
        '.pc5': 'application/x-pc5',
        '.pci': 'application/x-pci',
        '.pcl': 'application/x-pcl',
        '.pcx': 'application/x-pcx',
        '.pdf': 'application/pdf',
        '.pdx': 'application/vnd.adobe.pdx',
        '.pfx': 'application/x-pkcs12',
        '.pgl': 'application/x-pgl',
        '.pic': 'application/x-pic',
        '.pko': 'application/vnd.ms-pki.pko',
        '.pl': 'application/x-perl',
        '.plg': 'text/html',
        '.pls': 'audio/scpls',
        '.plt': 'application/x-plt',
        '.png': 'image/png',
        # '.png': 'application/x-png',
        '.pot': 'application/vnd.ms-powerpoint',
        '.ppa': 'application/vnd.ms-powerpoint',
        '.ppm': 'application/x-ppm',
        '.pps': 'application/vnd.ms-powerpoint',
        # '.ppt': 'application/vnd.ms-powerpoint',
        '.ppt': 'application/x-ppt',
        '.pr': 'application/x-pr',
        '.prf': 'application/pics-rules',
        '.prn': 'application/x-prn',
        '.prt': 'application/x-prt',
        '.ps': 'application/x-ps',
        # '.ps': 'application/postscript',
        '.ptn': 'application/x-ptn',
        '.pwz': 'application/vnd.ms-powerpoint',
        '.r3t': 'text/vnd.rn-realtext3d',
        '.ra': 'audio/vnd.rn-realaudio',
        '.ram': 'audio/x-pn-realaudio',
        '.ras': 'application/x-ras',
        '.rat': 'application/rat-file',
        '.rdf': 'text/xml',
        '.rec': 'application/vnd.rn-recording',
        '.red': 'application/x-red',
        '.rgb': 'application/x-rgb',
        '.rjs': 'application/vnd.rn-realsystem-rjs',
        '.rjt': 'application/vnd.rn-realsystem-rjt',
        '.rlc': 'application/x-rlc',
        '.rle': 'application/x-rle',
        '.rm': 'application/vnd.rn-realmedia',
        '.rmf': 'application/vnd.adobe.rmf',
        '.rmi': 'audio/mid',
        '.rmj': 'application/vnd.rn-realsystem-rmj',
        '.rmm': 'audio/x-pn-realaudio',
        '.rmp': 'application/vnd.rn-rn_music_package',
        '.rms': 'application/vnd.rn-realmedia-secure',
        '.rmvb': 'application/vnd.rn-realmedia-vbr',
        '.rmx': 'application/vnd.rn-realsystem-rmx',
        '.rnx': 'application/vnd.rn-realplayer',
        '.rp': 'image/vnd.rn-realpix',
        '.rpm': 'audio/x-pn-realaudio-plugin',
        '.rsml': 'application/vnd.rn-rsml',
        '.rt': 'text/vnd.rn-realtext',
        # '.rtf': 'application/msword',
        '.rtf': 'application/x-rtf',
        '.rv': 'video/vnd.rn-realvideo',
        '.sam': 'application/x-sam',
        '.sat': 'application/x-sat',
        '.sdp': 'application/sdp',
        '.sdw': 'application/x-sdw',
        '.sit': 'application/x-stuffit',
        '.slb': 'application/x-slb',
        '.sld': 'application/x-sld',
        '.slk': 'drawing/x-slk',
        '.smi': 'application/smil',
        '.smil': 'application/smil',
        '.smk': 'application/x-smk',
        '.snd': 'audio/basic',
        '.sol': 'text/plain',
        '.sor': 'text/plain',
        '.spc': 'application/x-pkcs7-certificates',
        '.spl': 'application/futuresplash',
        '.spp': 'text/xml',
        '.ssm': 'application/streamingmedia',
        '.sst': 'application/vnd.ms-pki.certstore',
        '.stl': 'application/vnd.ms-pki.stl',
        '.stm': 'text/html',
        '.sty': 'application/x-sty',
        '.svg': 'text/xml',
        '.swf': 'application/x-shockwave-flash',
        '.tdf': 'application/x-tdf',
        '.tg4': 'application/x-tg4',
        '.tga': 'application/x-tga',
        # '.tif': 'application/x-tif',
        '.tif': 'image/tiff',
        '.tiff': 'image/tiff',
        '.tld': 'text/xml',
        '.top': 'drawing/x-top',
        '.torrent': 'application/x-bittorrent',
        '.tsd': 'text/xml',
        '.txt': 'text/plain',
        '.uin': 'application/x-icq',
        '.uls': 'text/iuls',
        '.vcf': 'text/x-vcard',
        '.vda': 'application/x-vda',
        '.vdx': 'application/vnd.visio',
        '.vml': 'text/xml',
        '.vpg': 'application/x-vpeg005',
        # '.vsd': 'application/vnd.visio',
        '.vsd': 'application/x-vsd',
        '.vss': 'application/vnd.visio',
        # '.vst': 'application/vnd.visio',
        '.vst': 'application/x-vst',
        '.vsw': 'application/vnd.visio',
        '.vsx': 'application/vnd.visio',
        '.vtx': 'application/vnd.visio',
        '.vxml': 'text/xml',
        '.wav': 'audio/wav',
        '.wax': 'audio/x-ms-wax',
        '.wb1': 'application/x-wb1',
        '.wb2': 'application/x-wb2',
        '.wb3': 'application/x-wb3',
        '.wbmp': 'image/vnd.wap.wbmp',
        '.wiz': 'application/msword',
        '.wk3': 'application/x-wk3',
        '.wk4': 'application/x-wk4',
        '.wkq': 'application/x-wkq',
        '.wks': 'application/x-wks',
        '.wm': 'video/x-ms-wm',
        '.wma': 'audio/x-ms-wma',
        '.wmd': 'application/x-ms-wmd',
        '.wmf': 'application/x-wmf',
        '.wml': 'text/vnd.wap.wml',
        '.wmv': 'video/x-ms-wmv',
        '.wmx': 'video/x-ms-wmx',
        '.wmz': 'application/x-ms-wmz',
        '.wp6': 'application/x-wp6',
        '.wpd': 'application/x-wpd',
        '.wpg': 'application/x-wpg',
        '.wpl': 'application/vnd.ms-wpl',
        '.wq1': 'application/x-wq1',
        '.wr1': 'application/x-wr1',
        '.wri': 'application/x-wri',
        '.wrk': 'application/x-wrk',
        '.ws': 'application/x-ws',
        '.ws2': 'application/x-ws',
        '.wsc': 'text/scriptlet',
        '.wsdl': 'text/xml',
        '.wvx': 'video/x-ms-wvx',
        '.xdp': 'application/vnd.adobe.xdp',
        '.xdr': 'text/xml',
        '.xfd': 'application/vnd.adobe.xfd',
        '.xfdf': 'application/vnd.adobe.xfdf',
        '.xhtml': 'text/html',
        # '.xls': 'application/vnd.ms-excel',
        '.xls': 'application/x-xls',
        '.xlw': 'application/x-xlw',
        '.xml': 'text/xml',
        '.xpl': 'audio/scpls',
        '.xq': 'text/xml',
        '.xql': 'text/xml',
        '.xquery': 'text/xml',
        '.xsd': 'text/xml',
        '.xsl': 'text/xml',
        '.xslt': 'text/xml',
        '.xwd': 'application/x-xwd',
        '.x_b': 'application/x-x_b',
        '.sis': 'application/vnd.symbian.install',
        '.sisx': 'application/vnd.symbian.install',
        '.x_t': 'application/x-x_t',
        '.ipa': 'application/vnd.iphone',
        '.apk': 'application/vnd.android.package-archive',
        '.xap': 'application/x-silverlight-app',
    }
    filename_ext = re.compile(r'(\.[^.]+?)$')

    def __init__(self, host, bucket, access_key_id, access_key_secret):
        self.host = host
        self.bucket = bucket
        self.access_key_id = access_key_id
        self.access_key_secret = access_key_secret

    def make_auth(self, auth_info: dict) -> str:
        verb = auth_info.get('verb')
        content_md5 = auth_info.get('content-md5') if auth_info.get('content-md5') else ''
        content_type = auth_info.get('content-type') if auth_info.get('content-type') else ''
        date = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime())
        canonicalized_oss_headers = \
            auth_info.get('canonicalized_oss_headers') if auth_info.get('canonicalized_oss_headers') else ''
        canonicalized_resource = \
            auth_info.get('canonicalized_resource') if \
            auth_info.get('canonicalized_resource') else '/' + self.bucket + '/'

        signature = base64.b64encode(
            hmac.new(
                self.access_key_secret.encode(),
                (
                    verb + '\n' +
                    content_md5 + '\n' +
                    content_type + '\n' +
                    date + '\n' +
                    canonicalized_oss_headers +
                    canonicalized_resource
                ).encode(),
                sha1
            ).digest()
        ).decode()

        return 'OSS {ak_id}:{signature}'.format(ak_id=self.access_key_id, signature=signature)

    def list_object(self) -> list:
        objs = []
        marker = None

        while True:
            headers = {
                'Host': self.host,
                'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
                'Authorization': self.make_auth({
                    'verb': 'GET',
                })
            }
            res = requests.get(
                'https://' + self.host + '/{marker}'.format(marker=('?marker=' + marker) if marker else ''),
                headers=headers
            )
            etree = ElementTree.fromstring(res.text)
            for content in etree.findall('Contents'):
                objs.append((content.find('Key').text, content.find('ETag').text[1:-1]))

            marker = etree.findall('NextMarker')
            if marker:
                marker = marker[0].text
            else:
                break

        return objs

    def put_object(self, obj_name: str, data: bytes) -> str:
        # 检索Content-Type
        ext = OSSBucket.filename_ext.findall(obj_name)
        content_type = None
        if ext:
            content_type = OSSBucket.content_type_map.get(ext[0])
        if content_type is None:
            content_type = 'application/octet-stream'

        # 计算Content-MD5
        content_md5 = base64.b64encode(md5(data).digest()).decode()

        headers = {
            'Host': self.host,
            'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
            'Content-Type': content_type,
            'Content-MD5': content_md5,
            'Content-Disposition': 'inline',
            'Authorization': self.make_auth({
                'verb': 'PUT',
                'content-md5': content_md5,
                'content-type': content_type,
                'canonicalized_resource': '/' + self.bucket + '/' + obj_name
            })
        }
        res = requests.put('https://' + self.host + '/' + obj_name, data=data, headers=headers)
        return 'OK  ' if res.status_code == 200 else 'Fail'

    def get_object(self, obj_name: str) -> bytes:
        headers = {
            'Host': self.host,
            'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
            'Authorization': self.make_auth({
                'verb': 'GET',
                'canonicalized_resource': '/' + self.bucket + '/' + obj_name
            })
        }
        res = requests.get('https://' + self.host + '/' + obj_name, headers=headers)
        return res.content

    def del_object(self, obj_name: str) -> str:
        headers = {
            'Host': self.host,
            'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
            'Authorization': self.make_auth({
                'verb': 'DELETE',
                'canonicalized_resource': '/' + self.bucket + '/' + obj_name
            })
        }
        res = requests.delete('https://' + self.host + '/' + obj_name, headers=headers)
        return 'OK  ' if res.status_code == 204 else 'Fail'

class FileManager(object):
    def __init__(self, root_dir: str):
        self.root_dir = root_dir

    def list_file(self) -> list:
        root = self.root_dir
        if root[-1] in ['/', '\\']:
            root = root[:-1]

        root_len = len(self.root_dir) + 1
        res = []
        for path in os.walk(root):
            if path[2]:
                for file in path[2]:
                    res.append((path[0].replace('\\', '/') + '/' + file)[root_len:])
        return res

    def read_file(self, file_name: str) -> bytes:
        with open(os.path.join(self.root_dir, file_name), 'rb') as file:
            data = file.read()
        return data

    def write_file(self, file_name: str, data: bytes):
        path = os.path.join(self.root_dir, file_name)
        if not os.path.isdir(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))

        with open(path, 'wb') as file:
            file.write(data)

    def del_file(self, file_name: str):
        path = os.path.join(self.root_dir, file_name)
        if os.path.isfile(path):
            os.remove(path)

    def clear_empty_folder(self):
        for path in os.walk(self.root_dir, False):
            if path[0] == self.root_dir:
                continue
            if not path[1] and not path[2]:
                os.rmdir(path[0])

class OSSSynchronizer(object):
    def __init__(self, local_dir: FileManager, oss_bucket: OSSBucket):
        self.local_dir = local_dir
        self.oss_bucket = oss_bucket

    # 检查同步情况
    def sync_checking(self) -> list:
        file_list = self.local_dir.list_file()
        obj_list = self.oss_bucket.list_object()

        # 将(文件名, ETag)二元组列表转为键值映射字典
        obj_map = dict()
        for obj in obj_list:
            obj_map[obj[0]] = obj[1]

        # 同步列表，三元组(文件名, 是否在本地, ETag)的列表
        sync_list = []

        # 从本地文件列表更新同步列表
        for file in file_list:
            sync_list.append((file, True, obj_map.get(file)))
            if obj_map.get(file):
                obj_map.pop(file)

        # 从OSS对象字典更新同步列表
        for obj, etag in obj_map.items():
            sync_list.append((obj, False, etag))

        return sync_list

    # 从本地同步到OSS
    def sync_from_local_to_oss(self):
        # 进行同步
        def sync(target_list: list):
            for thing in target_list:
                if thing[1]:  # 文件在本地
                    if thing[2] is not None:  # 本地和OSS各有一份
                        data = self.local_dir.read_file(thing[0])
                        file_md5 = md5(data).hexdigest().upper()
                        if file_md5 != thing[2]:  # 内容不一致，上传本地文件到OSS
                            res = self.oss_bucket.put_object(thing[0], data)
                            print('{status} [M] {filename}'.format(status=res, filename=thing[0]))
                    else:  # 文件不在OSS，上传本地文件到OSS
                        data = self.local_dir.read_file(thing[0])
                        res = self.oss_bucket.put_object(thing[0], data)
                        print('{status} [+] {filename}'.format(status=res, filename=thing[0]))
                else:  # 文件不在本地，删除OSS上的对应对象
                    res = self.oss_bucket.del_object(thing[0])
                    print('{status} [-] {filename}'.format(status=res, filename=thing[0]))

        sync_lists = self.sync_checking()
        threads_num = 64
        if len(sync_lists) < 64:
            threads_num = len(sync_lists)
        target_num = len(sync_lists) // threads_num
        target_num_mod = len(sync_lists) % threads_num
        threads = []
        for i in range(threads_num):
            if i < target_num_mod:
                threads.append(threading.Thread(
                    target=sync,
                    args=(sync_lists[(target_num + 1) * i:(target_num + 1) * (i + 1)],)
                ))
            else:
                threads.append(threading.Thread(
                    target=sync,
                    args=(sync_lists[target_num * i + target_num_mod:target_num * (i + 1) + target_num_mod],)
                ))

        for t in threads:
            t.start()
        for t in threads:
            t.join()

    # 从OSS同步到本地
    def sync_from_oss_to_local(self):
        # 进行同步
        def sync(target_list: list):
            for thing in target_list:
                if thing[1]:  # 文件在本地
                    if thing[2] is not None:  # 本地和OSS各有一份
                        data = self.local_dir.read_file(thing[0])
                        file_md5 = md5(data).hexdigest().upper()
                        if file_md5 != thing[2]:  # 内容不一致，下载OSS对应文件
                            res = self.oss_bucket.get_object(thing[0])
                            if res:
                                self.local_dir.write_file(thing[0], res)
                            print('{status} [M] {filename}'.format(status='OK' if res else 'Fail', filename=thing[0]))
                    else:  # 文件不在OSS，删除本地文件
                        self.local_dir.del_file(thing[0])
                        print('{status} [-] {filename}'.format(status='OK', filename=thing[0]))
                else:  # 文件不在本地，下载OSS上的对应对象
                    res = self.oss_bucket.get_object(thing[0])
                    if res:
                        self.local_dir.write_file(thing[0], res)
                    print('{status} [+] {filename}'.format(status='OK' if res else 'Fail', filename=thing[0]))

        sync_lists = self.sync_checking()
        threads_num = 64
        if len(sync_lists) < 64:
            threads_num = len(sync_lists)
        target_num = len(sync_lists) // threads_num
        target_num_mod = len(sync_lists) % threads_num
        threads = []
        for i in range(threads_num):
            if i < target_num_mod:
                threads.append(threading.Thread(
                    target=sync,
                    args=(sync_lists[(target_num + 1) * i:(target_num + 1) * (i + 1)], )
                ))
            else:
                threads.append(threading.Thread(
                    target=sync,
                    args=(sync_lists[target_num * i + target_num_mod:target_num * (i + 1) + target_num_mod],)
                ))

        for t in threads:
            t.start()
        for t in threads:
            t.join()

        # 清理空文件夹
        self.local_dir.clear_empty_folder()

if __name__ == '__main__':
    # 同步博客
    with open(blog_config_file, 'r', encoding='utf-8') as fp:
        config = json.load(fp)

    oss = OSSBucket(
        config['oss']['host'],
        config['oss']['bucket'],
        config['oss']['access_key_id'],
        config['oss']['access_key_secret']
    )
    local = FileManager(config['local_dir'])
    oss_synchronizer = OSSSynchronizer(local, oss)
    oss_synchronizer.sync_from_local_to_oss()

    # 同步静态文件
    with open(assets_config_file, 'r', encoding='utf-8') as fp:
        config = json.load(fp)

    oss = OSSBucket(
        config['oss']['host'],
        config['oss']['bucket'],
        config['oss']['access_key_id'],
        config['oss']['access_key_secret']
    )
    local = FileManager(config['local_dir'])
    oss_synchronizer = OSSSynchronizer(local, oss)
    oss_synchronizer.sync_from_local_to_oss()


from django.shortcuts import render
from contact_list.models import Contact_list

# Create your views here.

def contact_list(request):
    if not request.user.is_authenticated():
        return render(request, 'login_error.html')
    else:
        return render(request, 'contact_list.html',
                      {'contact_list': Contact_list.objects.all()})

def single_contact(request, contact_list_id=1):
    if not request.user.is_authenticated():
        return render(request, 'login_error.html')
    else:
        return render(request, 'single_contact.html',
                      {'single_contact':
                       Contact_list.objects.get(id=contact_list_id)})

def search_list(request):
    if request.method == 'POST':
        search_text = request.POST['search_text']
    else:
        search_text = ''

    contacts = Contact_list.objects.filter(
        first_name__contains=search_text)
    return render
    (request, 'ajax_search.html', {'contact_list': contacts})


from bs4 import BeautifulSoup
import re, urllib, urllib2, os, requests

def lookup_by_name_on_NIST(cmpd):
	""" Takes the name of a chemical compound as a string and looks it up on the NIST chemical
		webbook; returns the text of the site reached by searching for the input chemical"""
	url_safe_cmpd = urllib.quote(cmpd.encode('utf-8'))
	address = "http://webbook.nist.gov/cgi/cbook.cgi?Name=" + url_safe_cmpd + "&Units=SI"
	site_text = requests.get(address).text
	#this is site_text input for parse_text_from_NIST_site or
	#for find_link_if_directed_to_search_page
	return BeautifulSoup(site_text)

def find_link_if_directed_to_search_page(BeautifulSoup_sitetext):
	""" If initial chemical search through NIST ends up
		on a search results page with multiple links to
		different chemical pages, choose the link to
		the first chemical page"""
	soup = BeautifulSoup_sitetext
	links = soup.find_all('a')
	l = links[0].get('href', None)
	link_to_chem = "http://webbook.nist.gov" + l
	chem_sitetext = requests.get(link_to_chem).text
	return BeautifulSoup(chem_sitetext)

def find_permanent_link(BeautifulSoup_sitetext):
	"""If there is a Permanent link to a page for a compound
	right after the list of 'Other names' instead of 'Information on this page',
	then follow the link to the permanent page and use this page to scrape instead"""
	perma_link = BeautifulSoup_sitetext.find_all('a', href=True, text="Permanent link")
	l = perma_link[0].get('href', None)
	link_to_chem = "http://webbook.nist.gov" + l
	chem_sitetext = requests.get(link_to_chem).text
	return BeautifulSoup(chem_sitetext)

def parse_text_from_NIST_site(BeautifulSoup_sitetext, dict):
	""" Takes the HTML text from the NIST lookup site and adds the title chemical
		as well as all of the chemical synonyms listed on the page as keys in a dictionary.
		All values are the title chemical. This will allow us to convert all chemical
		synonyms in our data sheets and kovats databases into a single name, so that we
		can search and match."""
	soup = BeautifulSoup_sitetext
	htmlless = soup.text #removes html tags, makes a unicode string
	nless = re.sub("\n", " ", htmlless) #removes line breaks
	#main_chem_start_index = nless.find("ChemistryWebBook", 0, len(nless)) +16
	#main_chem_end_index = nless.find("Formula", 0, len(nless))
	#main_chem = nless[main_chem_start_index : main_chem_end_index].strip()
	main_chem = soup.find('h1').text.strip()
	if main_chem in dict:
		pass
	else:
		val = main_chem
		dict[val] = val
		if nless.find("Other names", 0, len(nless))> 0:	#adds synonyms only if there are "Other names" on NIST site
			other_start_index = nless.find("Other names", 0, len(nless)) +13
			other_end_index = nless.find("Information on this page:  Notes ", 0, len(nless))
			indiv_synonyms = nless[other_start_index : other_end_index]. split(";")
			for indiv_synonym in indiv_synonyms:
				indiv_synonym = indiv_synonym.strip()
				dict[indiv_synonym] = val
	#REMEMBER, ALL VALUES ARE UNICODE STRINGS
	#WHEN WRITING MATCHING CODE, NEED TO MAKE SURE MATCHES
	#ARE IN UNICODE TOO

def scrape_chem_synonyms_from_NIST(cmpd, dict):
	""" Takes a compound string and dictionary and returns the
	dictionary with all synonyms for a given compound as keys,
	with values as the header from the nist webbook (a single
	synonym) added to the original dictionary. Accounts for the
	three different results you can get when you do the initial
	search in NIST (Name Not Found, mulitple results, one chem result)"""
	BS_sitetext = lookup_by_name_on_NIST(cmpd)
	header = BS_sitetext.find('h1').text
	if header == u'Name Not Found': #compound not in NIST database
		dict[unicode(cmpd)] = unicode(cmpd)
	elif header == u'Search Results': #multiple matches for search
		BS_chem_site = find_link_if_directed_to_search_page(BS_sitetext)
		if BS_chem_site.find_all('a', href=True, text="Permanent link") != []:
		#going to the 'permanent link' for a compound if it exists
		#because this takes the place of 'Other Information' that usually marks
		#the end of the synonyms ('Other names') list
			BS_chem_site = find_permanent_link(BS_chem_site)
		head_chem = BS_chem_site.find('h1').text
		parse_text_from_NIST_site(BS_chem_site, dict)
		if cmpd not in dict:
				dict[unicode(cmpd)] = head_chem
	else: #compound page reached (header =  compound name or synonym)
		if BS_sitetext.find_all('a', href=True, text="Permanent link") != []:
		#going to the 'permanent link' for a compound if it exists
		#because this takes the place of 'Other Information' that usually marks
		#the end of the synonyms ('Other names') list
			BS_sitetext = find_permanent_link(BS_sitetext)
		parse_text_from_NIST_site(BS_sitetext, dict)
		if cmpd not in dict:
			dict[unicode(cmpd)] = header
	return dict

def scrape_whole_list_from_NIST(cmpd_list, dict):
	for compound in cmpd_list:
		if compound not in dict.keys():
			dict = scrape_chem_synonyms_from_NIST(compound, dict)
	return dict


# -*- coding:utf-8 -*-
#!/usr/bin/python3
#_author_='HuangJiCheng';
#date: 2019/3/21
import urllib.request
import re

url = 'https://blog.csdn.net/nav/arch'
headers = ('User-Agent','Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/65.0')
opener = urllib.request.build_opener()
opener.add_handlers=[headers]
# 安装为全局
urllib.request.install_opener(opener)
data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
pat = '''r

                        (.*?)

      '''
alllink = re.compile(pat).findall(data)
print(len(alllink))


# Interactive python client for fizzbot

import json
import urllib.request
import urllib.error

domain = 'https://api.noopschallenge.com'

def print_sep():
    print('----------------------------------------------------------------------')

# print server response
def print_response(dict):
    print('')
    print('message:')
    print(dict.get('message'))
    print('')
    for key in dict:
        if key != 'message':
            print('%s: %s' % (key, json.dumps(dict.get(key))))
    print('')

# try an answer and see what fizzbot thinks of it
def try_answer(question_url, answer):
    print_sep()
    body = json.dumps({'answer': answer})
    print('*** POST %s %s' % (question_url, body))
    try:
        req = urllib.request.Request(domain + question_url, data=body.encode('utf8'),
                                     headers={'Content-Type': 'application/json'})
        res = urllib.request.urlopen(req)
        response = json.load(res)
        print_response(response)
        print_sep()
        return response

    except urllib.error.HTTPError as e:
        response = json.load(e)
        print_response(response)
        return response

def get_dict(rules):
    rule_dict = {}
    for d in rules:
        rule_dict[d['number']] = d['response']
    return rule_dict

def substitute(rule_dict, n):
    keys = sorted(rule_dict.keys())
    resp = n
    for i in keys:
        if n % i == 0:
            if isinstance(resp, int):
                resp = ''
            resp += rule_dict[i]
    return str(resp)

def solve(question_data):
    rules = question_data.get('rules')
    rule_dict = get_dict(rules)
    numbers = question_data.get('numbers')
    resp = ' '.join([substitute(rule_dict, n) for n in numbers])
    print('RESPONSE GENERATED: ', resp)
    return resp

# keep trying answers until a correct one is given
def get_correct_answer(question_url, question_data):
    while True:
        if question_url == '/fizzbot/questions/1':
            answer = 'Python'
        else:
            answer = solve(question_data)

        response = try_answer(question_url, answer)

        if response.get('result') == 'interview complete':
            print('congratulations!')
            exit()

        if response.get('result') == 'correct':
            return response.get('nextQuestion')

# do the next question
def do_question(domain, question_url):
    print_sep()
    print('*** GET %s' % question_url)

    request = urllib.request.urlopen('%s%s' % (domain, question_url))
    question_data = json.load(request)
    print_response(question_data)
    print_sep()

    next_question = question_data.get('nextQuestion')

    if next_question:
        return next_question

    return get_correct_answer(question_url, question_data)

def main():
    question_url = '/fizzbot'
    while question_url:
        question_url = do_question(domain, question_url)

if __name__ == '__main__':
    main()


#! /usr/bin/env python3

import sys
import time
import numpy
import pyinterface
import threading

import rospy
from std_msgs.msg import Float64
from std_msgs.msg import Int64
from std_msgs.msg import Bool

class cpz7415v_controller(object):

    motion = {
        'x': {
            'clock':  0, 'acc_mode': '', 'low_speed': 0,
            'speed': 0, 'acc': 0, 'dec': 0, 'step': 0
        },
        'y': {
            'clock':  0, 'acc_mode': '', 'low_speed': 0,
            'speed': 0, 'acc': 0, 'dec': 0, 'step': 0
        },
        'z': {
            'clock':  0, 'acc_mode': '', 'low_speed': 0,
            'speed': 0, 'acc': 0, 'dec': 0, 'step': 0
        },
        'u': {
            'clock':  0, 'acc_mode': '', 'low_speed': 0,
            'speed': 0, 'acc': 0, 'dec': 0, 'step': 0
        }
    }

    move_mode = {'x': '', 'y': '', 'z': '', 'u': ''}

    last_position = {'x': 0, 'y': 0, 'z': 0, 'u': 0}

    last_speed = {'x': 0, 'y': 0, 'z': 0, 'u': 0}

    do_status = 0

    step_flag = {'x': False, 'y': False, 'z': False, 'u': False}
    speed_flag = {'x': False, 'y': False, 'z': False, 'u': False}
    do_flag = False

    def __init__(self):
        ###=== Define member-variables ===###
        self.rsw_id = rospy.get_param('~rsw_id')
        self.node_name = rospy.get_param('~node_name')
        self.move_mode['x'] = rospy.get_param('~mode_x')
        self.move_mode['y'] = rospy.get_param('~mode_y')
        self.move_mode['z'] = rospy.get_param('~mode_z')
        self.move_mode['u'] = rospy.get_param('~mode_u')

        ###=== Create instance ===###
        try: self.mot = pyinterface.open(7415, self.rsw_id)
        except OSError as e:
            rospy.logerr("{e.strerror}. node={node_name}, rsw={rsw_id}".
                         format(self.node_name, self.rsw_id))
            sys.exit()

        ###=== Setting the board ===###
        self.mot.initialize()
        self.motion['x']['clock'] = rospy.get_param('~clock_x')
        self.motion['x']['acc_mode'] = rospy.get_param('~acc_mode_x')
        self.motion['x']['low_speed'] = rospy.get_param('~low_speed_x')
        self.motion['x']['speed'] = rospy.get_param('~speed_x')
        self.motion['x']['acc'] = rospy.get_param('~acc_x')
        self.motion['x']['dec'] = rospy.get_param('~dec_x')
        self.motion['x']['step'] = rospy.get_param('~step_x')
        self.motion['y']['clock'] = rospy.get_param('~clock_y')
        self.motion['y']['acc_mode'] = rospy.get_param('~acc_mode_y')
        self.motion['y']['low_speed'] = rospy.get_param('~low_speed_y')
        self.motion['y']['speed'] = rospy.get_param('~speed_y')
        self.motion['y']['acc'] = rospy.get_param('~acc_y')
        self.motion['y']['dec'] = rospy.get_param('~dec_y')
        self.motion['y']['step'] = rospy.get_param('~step_y')
        self.motion['z']['clock'] = rospy.get_param('~clock_z')
        self.motion['z']['acc_mode'] = rospy.get_param('~acc_mode_z')
        self.motion['z']['low_speed'] = rospy.get_param('~low_speed_z')
        self.motion['z']['speed'] = rospy.get_param('~speed_z')
        self.motion['z']['acc'] = rospy.get_param('~acc_z')
        self.motion['z']['dec'] = rospy.get_param('~dec_z')
        self.motion['z']['step'] = rospy.get_param('~step_z')
        self.motion['u']['clock'] = rospy.get_param('~clock_u')
        self.motion['u']['acc_mode'] = rospy.get_param('~acc_mode_u')
        self.motion['u']['low_speed'] = rospy.get_param('~low_speed_u')
        self.motion['u']['speed'] = rospy.get_param('~speed_u')
        self.motion['u']['acc'] = rospy.get_param('~acc_u')
        self.motion['u']['dec'] = rospy.get_param('~dec_u')
        self.motion['u']['step'] = rospy.get_param('~step_u')
        self.mot.set_motion(axis='x', mode=self.move_mode['x'], motion=self.motion)
        self.mot.set_motion(axis='y', mode=self.move_mode['y'], motion=self.motion)
        self.mot.set_motion(axis='z', mode=self.move_mode['z'], motion=self.motion)
        self.mot.set_motion(axis='u', mode=self.move_mode['u'], motion=self.motion)

        ###=== Define topic ===###
        topic_step_x_cmd = '/{0}_rsw{1}_x_step_cmd'.format(self.node_name, self.rsw_id)
        topic_step_x = '/{0}_rsw{1}_x_step'.format(self.node_name, self.rsw_id)
        topic_speed_x_cmd = '/{0}_rsw{1}_x_speed_cmd'.format(self.node_name, self.rsw_id)
        topic_speed_x = '/{0}_rsw{1}_x_speed'.format(self.node_name, self.rsw_id)
        topic_step_y_cmd = '/{0}_rsw{1}_y_step_cmd'.format(self.node_name, self.rsw_id)
        topic_step_y = '/{0}_rsw{1}_y_step'.format(self.node_name, self.rsw_id)
        topic_speed_y_cmd = '/{0}_rsw{1}_y_speed_cmd'.format(self.node_name, self.rsw_id)
        topic_speed_y = '/{0}_rsw{1}_y_speed'.format(self.node_name, self.rsw_id)
        topic_step_z_cmd = '/{0}_rsw{1}_z_step_cmd'.format(self.node_name, self.rsw_id)
        topic_step_z = '/{0}_rsw{1}_z_step'.format(self.node_name, self.rsw_id)
        topic_speed_z_cmd = '/{0}_rsw{1}_z_speed_cmd'.format(self.node_name, self.rsw_id)
        topic_speed_z = '/{0}_rsw{1}_z_speed'.format(self.node_name, self.rsw_id)
        topic_step_u_cmd = '/{0}_rsw{1}_u_step_cmd'.format(self.node_name, self.rsw_id)
        topic_step_u = '/{0}_rsw{1}_u_step'.format(self.node_name, self.rsw_id)
        topic_speed_u_cmd = '/{0}_rsw{1}_u_speed_cmd'.format(self.node_name, self.rsw_id)
        topic_speed_u = '/{0}_rsw{1}_u_speed'.format(self.node_name, self.rsw_id)
        topic_output_do_cmd = '/{0}_rsw{1}_do_cmd'.format(self.node_name, self.rsw_id)

        ###=== Define Publisher ===###
        self.pub_step_x = rospy.Publisher(topic_step_x, Int64, queue_size=1)
        self.pub_speed_x = rospy.Publisher(topic_speed_x, Int64, queue_size=1)
        self.pub_step_y = rospy.Publisher(topic_step_y, Int64, queue_size=1)
        self.pub_speed_y = rospy.Publisher(topic_speed_y, Int64, queue_size=1)
        self.pub_step_z = rospy.Publisher(topic_step_z, Int64, queue_size=1)
        self.pub_speed_z = rospy.Publisher(topic_speed_z, Int64, queue_size=1)
        self.pub_step_u = rospy.Publisher(topic_step_u, Int64, queue_size=1)
        self.pub_speed_u = rospy.Publisher(topic_speed_u, Int64, queue_size=1)

        ###=== Define Subscriber ===###
        self.sub_step_x_cmd = rospy.Subscriber(topic_step_x_cmd, Int64, self.set_step, callback_args='x')
        self.sub_speed_x_cmd = rospy.Subscriber(topic_speed_x_cmd, Int64, self.set_speed, callback_args='x')
        self.sub_step_y_cmd = rospy.Subscriber(topic_step_y_cmd, Int64, self.set_step, callback_args='y')
        self.sub_speed_y_cmd = rospy.Subscriber(topic_speed_y_cmd, Int64, self.set_speed, callback_args='y')
        self.sub_step_z_cmd = rospy.Subscriber(topic_step_z_cmd, Int64, self.set_step, callback_args='z')
        self.sub_speed_z_cmd = rospy.Subscriber(topic_speed_z_cmd, Int64, self.set_speed, callback_args='z')
        self.sub_step_u_cmd = rospy.Subscriber(topic_step_u_cmd, Int64, self.set_step, callback_args='u')
        self.sub_speed_u_cmd = rospy.Subscriber(topic_speed_u_cmd, Int64, self.set_speed, callback_args='u')
        self.sub_output_do_cmd = rospy.Subscriber(topic_output_do_cmd, Int64, self.output_do)

        pass

    def set_step(self, q, axis):
        self.motion[axis]['step'] = q.data
        self.step_flag[axis] = True
        return

    def _set_step(self):
        axis = ''
        step = []
        for i in self.move_mode:
            if self.step_flag[i]:
                axis += i
                step.append(self.motion[i]['step'])
            else: pass

        if axis != '':
            self.mot.set_motion(axis=axis, mode='ptp', motion=self.motion)
            onoff = self.mot.check_move_onoff(axis)

            for i, ax in enumerate(axis):
                if onoff[i]:
                    self.mot.change_step(ax, [self.motion[ax]['step']])
                else:
                    self.mot.start_motion(axis=ax, start_mode='acc', move_mode='ptp')
                self.step_flag[ax] = False
        else: pass
        time.sleep(0.0001)
        return

    def _get_step(self):
        step = self.mot.read_counter(axis='xyzu', cnt_mode='counter')

        if self.last_position['x'] != step[0]:
            self.pub_step_x.publish(step[0])
            self.last_position['x'] = step[0]
        if self.last_position['y'] != step[1]:
            self.pub_step_y.publish(step[1])
            self.last_position['y'] = step[1]
        if self.last_position['z'] != step[2]:
            self.pub_step_z.publish(step[2])
            self.last_position['z'] = step[2]
        if self.last_position['u'] != step[3]:
            self.pub_step_u.publish(step[3])
            self.last_position['u'] = step[3]

        time.sleep(0.0001)
        return

    def set_speed(self, q, axis):
        self.motion[axis]['speed'] = q.data
        self.speed_flag[axis] = True
        return

    def _set_speed(self):
        axis = ''
        speed = []
        for i in self.move_mode:
            if self.speed_flag[i]:
                axis += i
                speed.append(self.motion[i]['speed'])
            else: pass

        if axis != '':
            self.mot.driver.set_motion(axis=axis, mode='jog', motion=self.motion)
            onoff = self.mot.driver.check_move_onoff(axis)

            for i, ax in enumerate(axis):
                if onoff[i]:
                    self.mot.change_speed(axis=ax, mode='accdec_change', speed=[speed[i]])
                else:
                    self.mot.start_motion(axis=ax, start_mode='acc', move_mode='jog')
                self.speed_flag[ax] = False
        else: pass
        time.sleep(0.0001)
        return

    def _get_speed(self):
        speed = self.mot.read_speed(axis='xyzu')

        if self.last_speed['x'] != speed[0]:
            self.pub_speed_x.publish(speed[0])
            self.last_speed['x'] = speed[0]
        if self.last_speed['y'] != speed[1]:
            self.pub_speed_y.publish(speed[1])
            self.last_speed['y'] = speed[1]
        if self.last_speed['z'] != speed[2]:
            self.pub_speed_z.publish(speed[2])
            self.last_speed['z'] = speed[2]
        if self.last_speed['u'] != speed[3]:
            self.pub_speed_u.publish(speed[3])
            self.last_speed['u'] = speed[3]

        time.sleep(0.0001)
        return

    def output_do(self, q):
        self.do_status = q.data
        self.do_flag = True
        return

    def _output_do(self):
        self.mot.output_do(self.do_status)
        self.do_flag = False
        time.sleep(0.001)
        return

    def _main_thread(self):
        while not rospy.is_shutdown():
            self._set_step()
            self._set_speed()
            self._get_step()
            self._get_speed()
            if self.do_flag:
                self._output_do()
            continue
        return

    def start_thread_ROS(self):
        th = threading.Thread(target=self._main_thread)
        th.setDaemon(True)
        th.start()
        return

if __name__ == '__main__':
    rospy.init_node('cpz7415v')
    ctrl = cpz7415v_controller()
    ctrl.start_thread_ROS()
    rospy.spin()


import time
import argparse

def main (db_file,r_index):
    print ("Hello from data preprocess")

    import csv
    import numpy as np

    if r_index == 2:
        if "test" in db_file_out:
            db_file_out = db_file.replace(".csv",".txt")
        elif "train" in db_file_out:
            db_file_out = db_file.replace(".csv","_CLASS.txt")
    elif r_index == 3:
        db_file_out = db_file.replace(".csv","_SRC.txt")
    elif r_index == 4:
        db_file_out = db_file.replace(".csv","_DST.txt")
    else:
        print("Error! The extraction part should either 2, 3 or 4")

    outputFile = open (db_file_out, 'w')
    with open (db_file, 'r') as f:
        reader = csv.reader(f)

        counter = 0
        pastPhrase = 0
        reader.__next__()
        phraseString = ''
        for row in reader:
            if pastPhrase == int (row[0]):
                #We are still on the current phrase
                phraseString += row[r_index] + ' '
            else:
                print (phraseString, file = outputFile)
                phraseString = ""
                phraseString += row[r_index] + ' '

                counter += 1
                if counter == 20000:
                    break

            pastPhrase = int (row[0])

        print (counter)

    fTrain =  open (db_file_out, 'r')

    content = fTrain.readlines()

    print ("Total lenght of the files")
    print (len (content))

    linesTrain = []

    linesTest = []

    for i in range (len (content)):
        line = content[i].strip()

        randValue = np.random.uniform (0, 1)
        if randValue < 0.2:
            linesTest.append(line)

        else:
            linesTrain.append(line)

    print ("Len of trainFiles")
    print (len (linesTrain))

    print ("Len of test files")
    print (len (linesTest))

    with open (db_file_out, 'w') as f:
        for line in linesTrain:
            print(line, file=f)

    with open (db_file_out.replace("train","val"), 'w') as f:
        for line in linesTest:
            print(line, file=f)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Estimate the errors between two midis')

    parser.add_argument('database_file', help='the path of the database file to convert in a list of sentences')
    parser.add_argument('extraction_part', type=int, help='the word you need, the SRC(2 for Test / 3 for Train) or the DST(4)')

    args = parser.parse_args()

    main (args.database_file, args.extraction_part)


from sys import argv
import sqlite3
import traceback
import json

db = './scorelib.dat'

def get_score_id(conn, print_id):
    cur = conn.cursor()

    cur.execute('''SELECT edition.score FROM print JOIN edition
                   on print.edition = edition.id
                   WHERE print.id = ?''', (print_id,))

    return cur.fetchone()[0]

def get_composers(conn, score_id):
    cur = conn.cursor()

    cur.execute('''SELECT person.* FROM person JOIN score_author
                   on person.id = score_author.composer
                   WHERE score_author.score = ?''', (score_id,))

    return cur.fetchall()

def format_composer(composer):
    return {
        "name": composer[3],
        "born": composer[1],
        "died": composer[2]
    }

if __name__ == '__main__':

    print_num = int(argv[1])

    try:
        conn = sqlite3.connect(db)

        score_id = get_score_id(conn, print_num)
        composers_data = []

        composers = get_composers(conn, score_id)
        if composers:
            for composer in composers:
                composers_data.append(format_composer(composer))

        print(json.dumps(composers_data, indent=4, sort_keys=False, ensure_ascii=False))

        conn.commit()

    except:
        tb = traceback.format_exc()
        print(tb)


import yaml

# YAML fixtures
def parse_yaml(file):

    with open(file, "r") as stream:
        try:
            yaml.load_all(stream, Loader=yaml.SafeLoader)
        except yaml.YAMLError as exc:
            return exc

def get_yaml_as_dict(filepath):
    y = {}
    with open(filepath, "r") as stream:
        for name in yaml.load_all(stream, Loader=yaml.SafeLoader):
            for k,v in name.items():
                y[k] = v

    return y


import sys
import random

infile   = open('001134.fasta', 'r');
seq  = infile.read().replace('\n', '');
infile.close();

#print(seq)
test = 0
seqLength = len(seq)
fragSize = 400  #doing uniform reads
fold = 10       #amount of coverage needed
coverage = [0 for i in range (0,seqLength-1)] #Actual coverage done
numFrags = 0 #starting number of fragments taken
#frags = [0 for i in range(0,fold+1)]
frags = []
#print(len(frags))

def coverageMet(coverage, fold):
    i = 0
    met = True
    fin = len(coverage)
    while(i < fin and met == True): #while i is less than the length of coverage, which is the len of the seq - 1 and met == true
        if (coverage[i] < fold): #if coverage at index i is less than 10
            met = False #met = false
        i += 1 #increment i every time we check the value of coverage[i] against fold
    return met

randLength = 400

outputFile = open("frags001134noLim2.txt", 'a')

while True:
    randStart = random.randint(0, seqLength-randLength)

    outputFile.write(seq[randStart:randStart+400] + "\n")

    numFrags += 1

    if(numFrags % 1000 == 0):
        print(numFrags)

    for i in range(randStart,randStart+(fragSize-1)):
        coverage[i] += 1

    if ((coverageMet(coverage, fold) == True)):
        break

outputFile.close()
print(numFrags)


import re
counter=0
sum=0
with open('numbersinrows.txt','r') as file:
    for n in file:
        tab=re.split(',',n)
        counter+=len(tab)
        for i in tab:
            sum+=int(i)
print('Ilość liczb: {}\nSuma: {}'.format(counter,sum))


global pygame
global larguraPista
larguraPista = 2000
global segL
segL = 200# para fazer a pista
global camD
camD = 0.84 #visão da camera

def drawQuad (tela,cor,x1,y1,w1,x2,y2,w2): #criando a funçao de fazer quadrado
    Ponto0=(x1-w1,y1)#define as coordenadas de cada ponto
    Ponto1=(x2-w2,y2)
    Ponto2=(x2+w2,y2)
    Ponto3=(x1+w1,y1)
    pygame.draw.polygon(tela,cor,(Ponto0,Ponto1,Ponto2,Ponto3))#desenha o poligono

class Line:
    x=0
    y=0
    def __init__(self):
        self.x=0.0
        self.y=0.0
        self.z=0.0
        self.X=0.0
        self.Y=0.0
        self.W=0.0
        self.curve=0.0
        self.spriteX=0.0
        self.clip=0.0
        self.scale=0.0
        self.sprite=0
    def project(self,camX,camY,camZ,widht, height):
        self.scale = camD/(self.z-camZ)
        self.X = (1 + self.scale*(self.x - camX)) * width/2
        self.Y = (1 - self.scale*(self.y - camY)) * height/2
        self.W = self.scale * larguraPista  * width/2
    def drawSrite (tela):
        s= self.sprite
        self.w = s.getTextureRect().width
        self.h = s.getTextureRect().height

        destX += destW * spriteX; #offsetX
        destY += destH * (-1);    #offsetY

        self.clipH = destY+destH-clip
        if (clipH<0) :
            clipH=0


import sys

month_name_to_number = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)

def to_months(s):
    month, year = s.split()
    return (int(year) * 12) + month_name_to_number[month]

for line in open(sys.argv[1]):
    months = 0
    previous_end = 0
    for start, end in sorted((to_months(start), to_months(end))
                                for start, end in [pair.split('-')
                                    for pair in line.strip().split('; ')]):
        if start > previous_end:
            months += end - start + 1
        elif end > previous_end or start == previous_end:
            months += end - previous_end
        else:
            continue
        previous_end = end

    print(months // 12)

## datetime is slower and uses more memory than the simple convert to months
## code above.
#import sys
#from datetime import datetime
#
#for line in open(sys.argv[1]):
#    date_pairs = sorted([(datetime.strptime(start, '%b %Y'),
#                          datetime.strptime(end, '%b %Y'))
#                            for start, end in [pair.split('-')
#                                for pair in line.strip().split('; ')]])
#
#    months = 0
#    previous_end = None
#    for start, end in date_pairs:
#        if previous_end is None or start > previous_end:
#            months += (end.year*12 + end.month) - (start.year*12 + start.month) + 1
#        elif end > previous_end or start == previous_end:
#            months += (end.year*12 + end.month) - (previous_end.year*12 + previous_end.month)
#        else:
#            continue
#
#        previous_end = end
#
#    print(months // 12)


# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

#
# tanh paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle
import sys

data_type = 'float32'

def tanh(name:str, x):
    paddle.enable_static()

    with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
        data = paddle.static.data(name='x', shape=x.shape, dtype = data_type)
        out = paddle.tanh(data)

        cpu = paddle.static.cpu_places(1)
        exe = paddle.static.Executor(cpu[0])
        # startup program will call initializer to initialize the parameters.
        exe.run(paddle.static.default_startup_program())

        outs = exe.run(
            feed={'x': x},
            fetch_list=[out])

        saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])

    return outs[0]

def main():
    x = np.random.rand(8, 24, 32).astype(data_type)

    tanh("tanh", x)

if __name__ == "__main__":
    main()


import numpy as np
import math
import cv2
import matplotlib
import matplotlib.pyplot as plt
from skimage import exposure

printed = False

def histograph_eq(image):
    return (exposure.equalize_hist(image)*255).astype("uint8")

def desaturate(image):
    image_g = np.copy(image)
    for x in range(0, image.shape[1]):
        for y in range(0, image.shape[0]):
            image_g[y,x] = np.mean(image[y,x])
    return image_g

def invert(image):
    image_i = np.copy(image)
    for x in range(0, image.shape[1]):
        for y in range(0, image.shape[0]):
            image_i[y,x] = (255,255,255) - image[y,x]
    return image_i

def g_kernel_value(x, y, sigma):
    return (1/((2*math.pi*(sigma**2))))*(math.e**(-((x**2)+(y**2))/(2*(sigma**2))))

def create_gaussian_kernel(ksize, sigma):
    if not printed:
        print(sigma)
    kernel = np.empty((ksize,ksize), dtype='float')
    radius = int((ksize - 1) / 2)
    for x in range(-radius,radius + 1):
        for y in range(-radius,radius + 1):
            kernel[x + radius,y + radius] = g_kernel_value(x, y, sigma)
    kernel = kernel/np.sum(kernel)
    return kernel

def zero_padding(image, kernel):
    border_size = int((kernel.shape[0] - 1) / 2)
    arr = np.empty((image.shape[0] + (border_size * 2), image.shape[1] + (border_size * 2), 3), int)
    arr[border_size:-border_size,border_size:-border_size] = image
    return arr

def rem_zero_padding(image, kernel):
    border_size = int((kernel.shape[0] - 1) / 2)
    return image[border_size:-border_size, border_size:-border_size]

def apply_gaussian_kernel(image, kernel):
    ksize = kernel.shape[0]
    radius = int((ksize - 1) / 2)
    image_height = image.shape[0]
    image_width = image.shape[1]

    blurred_image = image.copy()

    for iy in range(radius, (image_height - radius)):
        for ix in range(radius, image_width - radius): #loop through each pixel from the original image
            new_pixel_tuple = 0
            for gy in range(-radius, radius+1):
                for gx in range(-radius, radius+1): #loop through kernel neighbors
                    new_pixel_tuple += (image[iy + gy, ix + gx]*kernel[gy,gx]).astype(int)
            blurred_image[iy,ix] = new_pixel_tuple
    return blurred_image

def gaussian_blur(image, ksize=3, sigma=1.0):
    return apply_gaussian_kernel(image, create_gaussian_kernel(ksize, sigma))

def saturation_blend(top_image, bottom_image): #saturation blend two images together and return the result
    top_image = cv2.cvtColor(top_image, cv2.COLOR_RGB2HSV) #convert the top image to HSV colorspace
    bottom_image = cv2.cvtColor(bottom_image, cv2.COLOR_RGB2HSV) #convert the bottom image to HSV colorspace
    bottom_image[:,:,1] = top_image[:,:,1] #copy the saturation from the top image into the bottom image
    bottom_image = cv2.cvtColor(bottom_image, cv2.COLOR_HSV2RGB) #convert the resulting image back to RGB colorspace
    return bottom_image #and return the result

def dodge_blend(top, bottom):
    return ((bottom / invert(top)) * 255).astype('uint8')

def trace_guide_filter(image, ksize=3, sigma=1.0, cycles=1):
    ### duplicate image into three layers
    top_layer = image
    middle_layer = image
    bottom_layer = image

    ### desaturate the top layer
    top_layer = desaturate(top_layer)

    ### invert the middle layer
    middle_layer = invert(middle_layer)

    ### gaussian blur the middle layer
    for i in range(0, cycles):
        middle_layer = gaussian_blur(middle_layer, ksize, sigma)

    ### blend bottom and middle layer
    bottom = dodge_blend(middle_layer, bottom_layer)

    ### blend that with the top layer
    return saturation_blend(top_layer, bottom)

###############################################
###### Time too use these functions! ##########
###############################################

import sys
import warnings
warnings.filterwarnings("ignore")
opts = [opt for opt in sys.argv[1:] if opt.startswith("-")]
args = [arg for arg in sys.argv[1:] if not arg.startswith("-")]

img = cv2.cvtColor(cv2.imread(args[0]), cv2.COLOR_BGR2RGB)

opts = np.array(opts)
if opts.size > 0:
    if opts[0] == "-he":
        img = histograph_eq(img)
    elif opts[0] == "-help":
        print("Required Arguments: inputFile ksize sigma numberOfCycles outputFile")
        print("Options: \"-he\" - to enable histographic equalization")
    else:
        print("unrecognized option specified. See README, or use \"-help\"")

img_filtered = trace_guide_filter(img, int(args[1]), float(args[2]), int(args[3]))

cv2.imwrite(args[4], img_filtered)


from biography.fields import MarkdownField
from django.db import models
from entity.models import Person

class Biography(models.Model):
    person = models.OneToOneField(Person, on_delete=models.CASCADE)
    notes = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.person.full_name

    class Meta:
        verbose_name_plural = "biographies"


list1 = []
list2 = ['kkkkkk']
list3 = ['cs', 'csgo', 'vn-csgo', 'proplayercsgo']

list3.append('simple')
list3.append('device')

list3[0] = "Deadpool"
print(list3)

#bai12
list3[-1] = "Naruto"
print(list3)

#bai13
#vi_tri = int(input("Nhap vi tri phan tu ban muon thay the "))
#thay_the = input("Ban muon thay no thanh gi ")

#list3[vi_tri] = thay_the
#print(list3)

#bai14
list3.pop(1)
print(list3)

#bai15
if "LOL" in list3:
  list3.remove("LOL")
else:
  print("Em khong thich lOL")


class Student:
    def __init__(self, roll_no=1, name="runjeeth", Course="cse"):
        self.roll_no = roll_no
        self.name = name
        self.Course = Course

    def showData(self):
        print("Roll no", self.roll_no)
        print("name ", self.name)
        print("Course ", self.Course)

student = Student()
student.showData()


#!/usr/bin/env/python

from netmiko import ConnectHandler
import yaml

devices = yaml.load(open('/home/dkhorn/.netmiko.yml'))

for dev in devices:
 try:
  net_conncet = ConnectHandler(**devices.get(dev))
  print (net_conncet.find_prompt())
 except:
  print ("It's not a device")


'''
Module to manage authentication between a client and the server.
'''

# Import python libs
import os
import sys
import logging

# Import 3rd party libs
import zmq

# Import caster libs
import caster.payload
import caster.exitcodes
from caster.client.crypto import ClientKeys

LOG = logging.getLogger(__name__)

class Auth:
    '''
    Provides the sequence for setting up communication
    with the server from a client.
    '''

    def __init__(self, opts):
        self.opts = opts
        self.keys = ClientKeys(opts)
        self.cached_pub_file = os.path.join(self.opts['crypto']['key_dir'],
                                            'server.pub')

        self.payload = caster.payload.Payload(opts)

    def client_sign_in_payload(self):
        payload = {
            'enc': 'clear',
            'load': {
                'action': '_auth',
                'id': self.opts['client']['id'],
                'pub': self.keys.public,
            },
        }
        return payload

    def decrypt_msg(self, msg):
        '''
        Decrypt message returned from the server.
        '''
        return self.keys.priv_key.decrypt(msg)

    def _verified_server_pub_key(self, server_pub):
        '''
        Verifies server's public key.
        '''
        if os.path.isfile(self.cached_pub_file):
            with open(self.cached_pub_file) as f:
                cached_pub = f.read()
            return cached_pub == server_pub
        else:
            with open(self.cached_pub_file, 'wb') as f:
                f.write(server_pub)
            return True

    def _verified_auth_payload(self, server_pub, token):
        '''
        Verifies server's auth payload.
        It must be 'caster full of salt'.
        '''
        auth_token = self.keys.decrypt(token, server_pub)
        return auth_token == 'caster full of salt'

    def verify_server(self, server_pub, token):
        '''
        Verifies server public key and auth payload.
        '''
        if self._verified_server_pub_key(server_pub):
            if self._verified_auth_payload(server_pub, token):
                LOG.debug('The server had been verified!')
                return True
        LOG.debug('The server had not been verified!')
        return False

    def sign_in(self):
        '''
        Send a sign in request to the server.
        '''
        auth = {}
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.connect(self.opts['server']['uri'])
        payload = self.payload.pack(self.client_sign_in_payload())
        socket.send(payload)
        payload = self.payload.unpack(socket.recv())
        if 'load' in payload and payload['load'].get('result') is None:
            msg = ("Server has rejected this client's key. "
                   'Delete the public key for this client on the server '
                   'and restart the client.\n')
            LOG.error(msg)
            sys.stderr.write(msg)
            sys.exit(caster.exitcodes.PKI_REJECTED)
        if not self.verify_server(payload['pub_key'], payload['token']):
            msg = ("The server's public key did not authenticate. "
                   "If you're confident that you're connecting to a valid "
                   "server, remove the server's public key and restart "
                   'the client.\n')
            LOG.error(msg)
            sys.stderr.write(msg)
            sys.exit(caster.exitcodes.AUTH_WRONG_TOKEN)
        auth['aes'] = self.decrypt(payload['aes'])
        auth['publish_port'] = payload['publish_port']
        LOG.debug(
            "The server's publish port is {}".format(auth['publish_port'])
        )
        return auth


from flask import Flask, request
from flask_restful import Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)

@app.route('/pi')
def pi():
    num = request.args.get('numsteps')
    numsteps = int(num)
    pi = calcPi(numsteps)
    return str(pi)

def calcPi(steps):
    """ Leibniz formula for arctan(1) = pi/4 """

    sum = 0
    step = 1.0 / steps

    for i in range(steps):
        x = (i + 0.5) * step
        sum += 4.0 / (1.0 + x**2)

    pi = sum * step
    return pi

if __name__ == '__main__':
# added "threades=true" to use multithreading of flask if necessary
    app.run(host="0.0.0.0",port='3000', threaded=True)


# -*- coding: utf-8 -*-
"""
Created on Tue Dec 29 01:54:59 2020

@author: ACER PREDATOR
"""
import heapq
l1=[]
l2=[]
for i in range(1,11):
    data1 = int(input("請輸入第%d個數字：" % (i)))
    l1.append(data1)
for i in range(1,4):
    data2 = max(l1)
    l2.append(data2)
    l1.remove(data2)
l2.sort()
data = map(l1.index, heapq.nlargest(3, l1))
print("最大的 3 個數字為：%s" % (l2))


import FWCore.ParameterSet.Config as cms

process = cms.Process("L1GtRates")

process.load("FWCore.MessageService.MessageLogger_cfi")

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )

inputfile = 'file:rpcttbits.root'

process.source = cms.Source("PoolSource",
                            fileNames = cms.untracked.vstring( inputfile ) )

process.l1gtrpctt = cms.EDAnalyzer( 'L1GtTechTrig',
                                    L1TTBitsLabel = cms.InputTag("rpcTechnicalTrigger"),
                                    MuonLabel     = cms.InputTag("selectedLayer1Muons"),
                                    RecoMuonLabel = cms.InputTag("muons"),
                                    MuonPtCut     = cms.untracked.double(4.0),
                                    SelectFilter  = cms.untracked.int32(1),
                                    MaxNBx        = cms.untracked.int32(1),
                                    MinBxNum      = cms.untracked.int32(1),
                                    MaxBxNum      = cms.untracked.int32(1),
                                    EventWindow   = cms.untracked.int32(10),
                                    Precision     = cms.untracked.int32(10),
                                    BitNumbers    = cms.vuint32(24,25,26,27,28,29,30) )

process.TFileService = cms.Service("TFileService", fileName = cms.string('rpctt_results.root') )

process.p = cms.Path( process.l1gtrpctt )


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import scipy.stats
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import (KNeighborsClassifier,
                               NeighborhoodComponentsAnalysis)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

def take_X_Y(WWC):
    Y = WWC['more_new_cases']
    X = WWC.drop(['Unnamed: 0.1.1', 'location', 'date', 'more_new_cases'], axis=1)
    return X, Y

def knn(WWC, k, tests):
    scores = []
    for i in range(tests):
        X, Y = take_X_Y(WWC)
        X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20)
        classifier = KNeighborsClassifier(n_neighbors=k)
        classifier.fit(X_train, y_train)
        Y_pred = classifier.predict(X_test)
        #print(confusion_matrix(y_test, y_pred))
        #print(classification_report(y_test, y_pred))

        acc_knn = round(metrics.accuracy_score(y_test, Y_pred) * 100, 2)
        scores.append(acc_knn)
        #print(acc_knn)

    print(scores)
    print(sum(scores) / len(scores))
    return sum(scores) / len(scores)

def knn_graph(WWC, neis, dataSetName):
    final_scores = [dataSetName]
    for k in range(1, neis):
        final_scores.append(knn(WWC, k, 10))
    return final_scores

def pca_knn(WWC, neis, components):
    mat = []
    X, Y = take_X_Y(WWC)
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20)
    for c in components:
        row = [str(c)]
        pca = make_pipeline(StandardScaler(),
                            PCA(n_components=c, random_state=0))
        for n in neis:
            knn = KNeighborsClassifier(n_neighbors=n)
            pca.fit(X_train, y_train)
            knn.fit(pca.transform(X_train), y_train)
            acc_knn = knn.score(pca.transform(X_test), y_test)
            row.append(round(acc_knn*100, 2))
        mat.append(row)
    print(mat)
    return mat

def lda_knn(WWC, neis, components):
    mat = []
    X, Y = take_X_Y(WWC)
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20)
    for c in components:
        row = [str(c)]
        lda = make_pipeline(StandardScaler(),
                            LinearDiscriminantAnalysis(n_components=c))
        for n in neis:
            knn = KNeighborsClassifier(n_neighbors=n)
            lda.fit(X_train, y_train)
            knn.fit(lda.transform(X_train), y_train)
            acc_knn = knn.score(lda.transform(X_test), y_test)
            row.append(round(acc_knn*100, 2))
        mat.append(row)
    print(mat)
    return mat

def nca_knn(WWC, neis, components):
    mat = []
    X, Y = take_X_Y(WWC)
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20)
    for c in components:
        row = [str(c)]
        nca = make_pipeline(StandardScaler(),
                        NeighborhoodComponentsAnalysis(n_components=c,
                                                       random_state=0))

        for n in neis:
            knn = KNeighborsClassifier(n_neighbors=n)
            nca.fit(X_train, y_train)
            knn.fit(nca.transform(X_train), y_train)
            acc_knn = knn.score(nca.transform(X_test), y_test)
            row.append(round(acc_knn*100, 2))
        mat.append(row)
    print(mat)
    return mat

if __name__ == '__main__':

    neis = [3,4,5,9,15,30]
    random_state = 0
    components = [2,3,5,9,15,20,30,40]

    col = ['n_components']
    for n in neis:
        col.append(str(n))

    # df_scores = pd.read_csv('WWC_09_01_2021_KNN_SCORES_ALL.csv')

    # no_nulls_
    WWC_no_nulls = pd.read_csv('WWC_all_data_clean_09_01_2021_no_nulls.csv')

    # no_nulls_AVG
    WWC_no_nulls_AVG = pd.read_csv('WWC_all_data_clean_09_01_2021_no_nulls.csv')

    # PCA
    no_nulls_pca = pca_knn(WWC_no_nulls, neis, components)
    df = pd.DataFrame(no_nulls_pca, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_PCA.csv')

    no_nulls_AVG_pca = pca_knn(WWC_no_nulls_AVG, neis, components)
    df = pd.DataFrame(no_nulls_AVG_pca, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_AVG_PCA.csv')

    # LDA
    no_nulls_lda = lda_knn(WWC_no_nulls, neis, [2])
    df = pd.DataFrame(no_nulls_lda, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_LDA.csv')

    no_nulls_AVG_lda = lda_knn(WWC_no_nulls_AVG, neis, [2])
    df = pd.DataFrame(no_nulls_AVG_lda, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_AVG_LDA.csv')

    # NCA
    no_nulls_nca = nca_knn(WWC_no_nulls, neis, components)
    df = pd.DataFrame(no_nulls_nca, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_NCA.csv')

    no_nulls_AVG_nca = nca_knn(WWC_no_nulls_AVG, neis, components)
    df = pd.DataFrame(no_nulls_AVG_nca, columns=col)
    df.to_csv('WWC_09_01_2021_no_nulls_AVG_NCA.csv')

    print("DONE")