import datetime
import logging
import os
import random
import numpy as np
import torch
import transformers

from util.args import TypedArgumentParser
from util.settings import CiSetting, OnlineSetting, SupervisedSetting, GDumb

def init_logging(filename):
    transformers.logging.set_verbosity_error()
    class TimeFilter(logging.Filter):
        def filter(self, record):
            try:
                last = self.last
            except AttributeError:
                last = record.relativeCreated

            delta = record.relativeCreated/1000 - last/1000
            record.relative = "{:.1f}".format(delta)
            record.uptime = str(datetime.timedelta(seconds=record.relativeCreated//1000))
            self.last = record.relativeCreated
            return True

    logging_format = "%(asctime)s - %(uptime)s - %(relative)ss - %(levelname)s - %(name)s - %(message)s"
    logging.basicConfig(format=logging_format, filename=filename, filemode='a', level=logging.INFO)
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(logging.Formatter(logging_format))
    root_logger = logging.getLogger()
    root_logger.addHandler(console_handler)
    for handler in root_logger.handlers:
        handler.addFilter(TimeFilter())

def set_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True

def main():
    args = TypedArgumentParser().parse_args()
    set_seed(args.random_seed)
    if not os.path.exists('log'):
        os.mkdir('log')
    init_logging(f'log/{args.setting}_{args.dataset}_{args.model}{"_" + args.proto_update if args.model == "ProtoNet" else ""}{"_" + args.metric if args.model == "ProtoNet" else ""}.log')
    logger = logging.getLogger(__name__)
    logger.info(f'PID: {os.getpid()}; PPID: {os.getppid()}')

    if args.setting == 'sup':
        setting = SupervisedSetting(args)
    elif args.setting == 'CI':
        if args.model == 'GDumb':
            setting = GDumb(args)
        else:
            setting = CiSetting(args)
    elif args.setting == 'online':
        if args.model == 'GDumb':
            setting = GDumb(args)
        else:
            setting = OnlineSetting(args)
    else:
        raise NotImplementedError(f'{args.setting} has not been implemented!')
    setting.run()

if __name__ == '__main__':
    main()


#   Exercício Python 010 - Conversor d emoeda
'''
Crie um programa que leia quanto dinheiro uma pessoa tem na
carteira e mostre quantos dolares ela pode comprar

Considere US$1,00 = R$3,27
'''

dolar = 3.27

dinheiro = float(input("Digite quanto valor você tem na carteira: "))

dolarComprado = dinheiro/dolar

print(f"Você pode comprar US${dolarComprado:.2f}")


# Генераторы

# Простейший генератор - функция в yield.

def even_range(start, end):
	current = start
	while current < end:
		yield current
		current += 2

# yield -
# временный return. Возвращается значение, но функция не завершает работу.

for number in even_range(0, 10):
	print(number)

ranger = even_range(0, 4)

next(ranger) # 0
next(ranger) # 2
# ...
# next(ranger) - exec. StopIteration

# Очень важно бывает хранить состояние функции
# и возвращаться к нему раз за разом.
# Это также помогает оптимизировать работу с памятью.

# Генератор N-ого числа Фибоначчи:

def fibonacci(number):
	a = b = 1
	for _ in range(number):
		yield a
		a, b = b, a + b

# Также генератор может получать значения (исп. в асинхрон.):

def accumulator():
	total = 0
	while True:
		value = yield total
		print('Got: {}'.format(value))

		if not value: break
		total += value

generator = accumulator()

next(generator)
print('Accumulated: {}'.format(generator.send(1)))
print('Accumulated: {}'.format(generator.send(1)))
print('Accumulated: {}'.format(generator.send(3)))


import sys
# sys.stdin = open('input.txt')
input = sys.stdin.readline

def cal_distance(data):
    result = int(data[0])
    for idx in range(1, 2*N-1, 2):
        oper = data[idx]
        n2 = int(data[idx+1])
        if oper == '+':
            result += n2
        elif oper == '-':
            result -= n2
        elif oper == '*':
            result *= n2
    return result

def dfs(i, j, total):
    global max_result, min_result
    if i == N-1 and j == N-1:
        result = cal_distance(total)
        max_result = max(max_result, result)
        min_result = min(min_result, result)
    for d in range(2):
        ni = i + di[d]
        nj = j + dj[d]
        if (0<=ni<N and 0<=nj<N):
            dfs(ni, nj, total + [arr[ni][nj]])

di = [1, 0]
dj = [0, 1]

N = int(input())

max_result = -5**N
min_result = 5**N

arr = [input().split() for _ in range(N)]

dfs(0, 0, [arr[0][0]])
print(max_result, min_result)


MORTALITY_PROB = 0.1        # annual probability of mortality
DRUG_EFFECT_RATIO = 0.75    # drug effectiveness:
                            # ratio of the annual mortality probability when using the drug
                            # to when not using the drug.
TIME_STEPS = 1000    # simulation length
ALPHA = 0.05        # significance level

# settings to calculate confidence interval of mean survival time
SIM_POP_SIZE = 1000     # population size of the simulated cohort

# settings to calculate prediction interval of mean survival time
REAL_POP_SIZE = 100     # size of the real cohort for which we'd like to project its outcomes
NUM_SIM_COHORTS = 200  # number of simulated cohorts used for making projections


n = int(input())
press = list(map(int,input().split(' ')))
press.sort(reverse = True)
temp = 0
base = 0
for i in range(0,n):
    temp = temp + press[i]
    if(temp > n):
        base = i + 1
        break
else:
    base = n
if(base != n):
    pileLst = press[:base]
    press = press[base:]
else:
    print(n)


import json
import requests

# Ex 12.1
request = "https://api.chucknorris.io/jokes/random"
response = requests.get(request).json()
print("Random Chuck Norris joke:")
print(response["value"])

# Ex 12.2
city = input("Enter city: ")
city = city.capitalize()
key = "################################"
request = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + key + "&units=metric"
response = requests.get(request).json()
weather = response["weather"][0]["description"]
temp = response["main"]["temp"]
print(f"Current weather at {city}: {weather}\nTemperature: {temp}")


from numpy import *
import operator
def createDataSet():
    group = array([[1.0, 1.1], [2.0, 2.0], [0, 0], [4.1, 5.1]])
    labels = ['A', 'B', 'C', 'D']
    return group, labels

def classify0(inX, dataSet, labels, k):
    """
    :param inX: 用于分类的输出向量
    :param dataSet:输入的样本集
    :param labels:标签向量
    :param k:用于选择最近邻居的树目
    :return:
    """
    dataSetsize = dataSet.shape[0]  # 得到数据集的行数
    diffMat = tile(inX, (dataSetsize, 1)) - dataSet  # tile生成和训练样本对应的矩阵，并与训练样本求差
    sqDiffMat = diffMat ** 2
    sqDistances = sqDiffMat.sum(axis=1)  # 将矩阵的每一行相加
    distances = sqDistances ** 0.5
    sortedDistIndicies = distances.argsort()  # 从小到大排序 返回对应的索引位置
    classCount = {}
    for i in range(k):
        voteIlabel = labels[sortedDistIndicies[i]]  # 找到该样本的类型
        classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1  # 在字典中将该类型加一
    sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)  # reverse = True代表降序
    return sortedClassCount[0][0]  # 排序并返回出现最多的那个类型
#测试
group,labels = createDataSet()
print(classify0([0,0],group,labels,3))
print(classify0([1,2],group,labels,3))
print(classify0([3,3],group,labels,3))
print(classify0([5,5],group,labels,3))


from decimal import Decimal

import requests

from cryptoportfolio.interfaces.base import Address

class PoolBitcoinCom(Address):
    decimal_places = 18
    __api_data = None

    def __init__(self, api_key, **kwargs):
        self.api_key = api_key
        super(PoolBitcoinCom, self).__init__(**kwargs)

    def _get_api_data(self):
        if not self.__api_data:
            self.__api_data = requests.get(
                "https://console.pool.bitcoin.com/srv/api/user?apikey=%s" % self.api_key).json()
        return self.__api_data

    def _get_addr_coins_and_tokens_balance(self):
        result = self._get_api_data()
        response = []
        if Decimal(result['bitcoinCashBalance']) != Decimal('0.0'):
            response.append(("BCH", Decimal(result['bitcoinCashBalance'])))
        if Decimal(result['bitcoinBalance']) != Decimal('0.0'):
            response.append(("BTC", Decimal(result['bitcoinCashBalance'])))
        return response


# -*- coding: utf-8 -*-
"""
Created on Sat Feb  6 19:37:25 2021
Purpose of this code is to find the m-th prime
@author: willi
"""

import math

#This function will return a found factor or if the number is prime
#it will just return the prime number
def factorFind(x,y,n):
    d = 1
    while d==1:
        x = ((x^2)+1)%n
        y = (((((y^2)+1)%n)^2)+1)%n
        d = math.gcd(abs(x-y),n)
    return d

if __name__=="__main__":

    #Initializes parameters for Pollard Rho Factorization Method
    x,y = 2,2

    primeCount = 0

    p = 0

    n = 2

    m = 10001

    while primeCount != m:
        if factorFind(x,y,n) == n:
            p = n
            primeCount += 1
        n += 1

    print(p)


"""
DYNAMOAPI: GET CURRENT WORKSPACE PATH AND FILE NAME FOR DYNAMO ON ALL HOST VERSIONS
"""
__author__ = 'Brendan cassidy'
__twitter__ = '@brencass86'
__version__ = '1.0.1'
"""
Original code indicated here https://forum.dynamobim.com/t/get-graph-filename/67434/5
This version includes 2 additional outputs for File name with and without file type.
"""
# we need to import the common language runtime to be able to interact with Dynamo

# Importing Reference Modules
# CLR ( Common Language Runtime Module )
import clr
# Adding the DynamoServices.dll module to work with the Dynamo API we need
clr.AddReference('DynamoServices')
from Dynamo.Events import *

# Access to the current Dynamo workspace path(eg location of the opened dynamo file)
currentWorkspacePath = ExecutionEvents.ActiveSession.CurrentWorkspacePath

# Gets the File Name with file type
fileName = currentWorkspacePath.split("\\")[-1]

# Removes the dyn File type from the end of the file name
fileNameWithoutFileType = fileName.replace(".dyn","")

# Outputs the full file path, file name with file type and just file name
OUT = currentWorkspacePath, fileName, fileNameWithoutFileType


import launch
import launch.actions
import launch.substitutions
import launch_ros.actions
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
import os
from ament_index_python.packages import get_package_share_directory

def generate_launch_description():
    cam_config = os.path.join(
        get_package_share_directory('vh_dronekit'),
        'config',
        'camera_params.yaml'
    )

    return launch.LaunchDescription([
        launch_ros.actions.Node(
            package='vh_image',
            namespace='',
            executable='vh_image',
            arguments=['--ros-args', '--log-level', 'info'],
            output='log'
        ),
        launch_ros.actions.Node(
            package='usb_cam',
            namespace='',
            executable='usb_cam_node_exe',
            arguments=['--ros-args', '--log-level', 'info'],
            output='log',
            parameters=[
                cam_config
            ]
        ),
    ])


from django.db import models
from django.conf import settings
from django.utils import timezone
from django.urls import reverse
from django.core.validators import RegexValidator
import string
from datetime import timedelta
import secrets
import abn
import re

from .base import MutableDataRecord

MOBILE_PHONE_REGEX = re.compile(r'^(\+614|04|00614)\d{8}$')
BSB_REGEX = re.compile(r'^\d{3}-?\d{3}$')
BANK_ACCT_REGEX = re.compile(r'^\d{4,20}$')

def validate_abn(value):
    if value and not abn.validate(value):
        return False
    return True

class Person(MutableDataRecord):
    """
    Model representing a tour operator (rider) or other staff member.
    Corresponds 1:1 with Deputy Employees.
    """
    class Meta:
        verbose_name = 'Person'
        verbose_name_plural = 'Riders & Deputy Users'

    MUTABLE_FIELDS = ('first_name', 'last_name', 'active', 'phone', 'email', 'email_verified')
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    display_name = models.CharField(max_length=200, unique=True, null=True, verbose_name='Preferred Name',
        error_messages={
            'unique': 'Display name already taken',
            'blank': 'Display name cannot be blank',
        })
    email = models.EmailField(unique=True, null=True, blank=True, error_messages={
        'unique': 'Email address already in use',
        'blank': 'You must enter an email address',
    })
    email_verified = models.BooleanField(blank=True, default=False)
    phone = models.CharField(max_length=30, unique=True, null=True, blank=True, verbose_name='Contact Phone Number',
        error_messages={
            'unique': 'Phone number already taken',
            'blank': 'You must enter a phone number',
        },
    )

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True,
        blank = True,
        verbose_name = 'Username for login (Optional)',
        help_text = "Allow this person to login directly with a username/password")

    active = models.BooleanField(blank=True)
    abn = models.CharField(max_length=30, blank=True, verbose_name='Contractor ABN',
        validators=[validate_abn])
    bank_bsb = models.CharField(max_length=20, blank=True, verbose_name='Bank account BSB')
    bank_acct = models.CharField(max_length=20, blank=True, verbose_name='Bank account number')
    last_seen = models.DateTimeField(default=timezone.now, null=True, blank=True, verbose_name='Last activity')
    created = models.DateTimeField(default=timezone.now, verbose_name='Date joined')

    # Rider specific data
    RIDER_CLASS_CHOICES = [
        ('00_rider_probationary', 'Probationary Rider (noob)'),
        ('10_rider_standard', 'Standard Rider'),
        ('20_rider_senior', 'Senior Rider'),
        ('30_rider_professional', 'Pro Rider'),
    ]
    rider_class = models.CharField(max_length=30, null=True, blank=True, choices=RIDER_CLASS_CHOICES)
    is_core_rider = models.BooleanField(blank=True, default=False)
    override_pay_rate = models.PositiveIntegerField(null=True, blank=True,
        help_text='Override pay rate for specific rider - LEAVE BLANK unless you are sure!')

    signup_status = models.CharField(max_length=20, choices=(
        ('migrated', 'Migrated account - verification needed'), # migrated -> confirmed
        ('confirmed', 'Signed up - email confirmed'), # confirmed -> complete
        ('complete', 'Signed up - all details completed'),
    ), default='migrated')

    @property
    def name(self):
        fullname = ('%s %s' % (self.first_name, self.last_name)).strip()
        return fullname or self.display_name or (self.user.username if self.user else None) or '(no name)'

    @property
    def has_deputy_account(self):
        return self.source_row_state == 'live' and self.source_row_id is not None

    def __str__(self):
        return '%s (%s)' % (self.name, self.pk)

    def can_login(self):
        return self.active and self.signup_status in ('complete', 'migrated')

    def phone_valid(self):
        if self.has_deputy_account:
            return True
        if self.phone is not None:
            return MOBILE_PHONE_REGEX.fullmatch(self.phone)
        return False

    def abn_valid(self):
        return abn.validate(self.abn) != False

    def bank_details_valid(self):
        return BSB_REGEX.fullmatch(self.bank_bsb) and BANK_ACCT_REGEX.fullmatch(self.bank_acct)

    def profile_complete(self):
        if not self.active or not self.rider_class:
            # don't show profile warnings on non-rider accounts
            return True
        return (
            self.display_name and self.email_verified
            and (self.phone_valid() or self.has_deputy_account) # can't change phone or email once invited to deputy
            and self.abn_valid()
            and self.bank_details_valid()
            and self.signup_status == 'complete'
        )

    def pay_rate(self):
        if not self.rider_class:
            return None
        if self.override_pay_rate:
            return self.override_pay_rate
        from peddleconcept.settings import get_rider_payrate_setting
        return get_rider_payrate_setting().get(self.rider_class)

    def rider_class_label(self):
        for x, y in self.RIDER_CLASS_CHOICES:
            if x == self.rider_class:
                return y

    def bank_details_text(self):
        if self.bank_details_valid():
            return 'Account number ending in %s' % (self.bank_acct[:3])
        else:
            return 'Not provided'

    def to_json(self, in_editor=False):
        data = {
            'id': self.pk,
            'title': self.display_name,
            'phone': self.phone,
        }

        if in_editor:
            data.update({
                'active': self.active,
                'rider_class': self.rider_class,
                'rider_class_label': self.rider_class_label(),
                'is_core_rider': self.is_core_rider,
                'has_deputy': self.has_deputy_account,
            })
        return data

    exists = True # see middleware.py

def get_random_token():
    return secrets.token_urlsafe(42)

class PersonToken(models.Model):
    """ Database-backed URL tokens corresponding to a particular action, ie. for a particular Person object """
    class Meta:
        verbose_name = 'Magic Token (Advanced)'
        verbose_name_plural = 'Magic Tokens (Advanced)'

    TOKEN_ACTION_CHOICES = [
        # Create a generic "signup invitation" URL with a particular random token
        ('rider_invite_generic', 'Rider signup invitation URL'),
        # Existing rider login URLs are converted to this type
        ('rider_login_migrated', 'Migrated rider login URL'),
    ]
    action = models.CharField(max_length=30, choices=TOKEN_ACTION_CHOICES)
    token = models.CharField(max_length=80, default=get_random_token)
    person = models.ForeignKey(Person, on_delete=models.CASCADE, null=True)

    valid_from = models.DateTimeField(auto_now_add=True)
    valid_days = models.PositiveIntegerField(blank=True, default=0,
        verbose_name='Number of days token is valid (0 = forever)')

    def full_url(self):
        return settings.SITE_BASE_URL + self.url

    @property
    def url(self):
        if self.action == 'rider_login_migrated':
            return reverse('token_login_migrate', args=[self.token])
        elif self.action == 'rider_invite_generic':
            return reverse('rider_setup_invite', args=[self.token])

    def is_valid(self):
        if self.valid_days == 0:
            return True
        return self.valid_from + timedelta(days=self.valid_days) > timezone.now()


from src.gerador_de_objetos import gerador_objetos
from src.gerador_de_coordenadas2 import *
from src import globais

qtde_policiais = 5
todos2 = []
lista_tiros = []
lista_pts = []
qtde_vidas = []
vida = globais.quadrado.copy()
vida['y'] = 95
vida['id'] = 8
vida['altura'] = 5
vida['largura'] = 5
vida['cor'] = (0, 0, 0)
lpts = globais.quadrado.copy()
lpts['y'] = 95
lpts['id'] = 10
lpts['altura'] = 5
lpts['largura'] = 5
lpts['cor'] = (0, 0, 0)
lpts1 = lpts.copy()
lpts1['id'] = 11
lpts2 = lpts.copy()
lpts2['id'] = 12
lpts3 = lpts.copy()
lpts3['id'] = 13

tiro = globais.quadrado.copy()
barricada_e = globais.quadrado.copy()
barricada_e['altura'] = 8
barricada_e['cor'] = (0, 1, 0)
barricada_e['id'] = 5
barricada_e['largura'] = 100
barricada_e['velocidade'] = 0.5
barricada_e['y'] = 120
barricada_e['x'] = -100
d = 200 - barricada_e['largura']
barricada_d = barricada_e.copy()
barricada_d['x'] = 100
barricada_d['id'] = 6

todos2.append((gerador_objetos(4, 15, 15, 15, qtde_policiais, (0, 0, 0))))
todos2[0].append(barricada_e)
todos2[0].append(barricada_d)
all2 = gerador_objetos2(todos2)

lista_tiros.append((gerador_objetos(7, 12, 4, 4, qtde_policiais, (0, 0, 0))))
shots = coord_tiro(lista_tiros)

qtde_vidas.append((gerador_objetos(8, 5, 5, 0, 25, (1, 1, 1))))
lives = coord_lives(qtde_vidas)

lista_pts.append((gerador_objetos(10, 5, 5, 0, 1, (1, 1, 1))))
lista_pts[0].extend((lpts1, lpts2, lpts3))
PTS = coord_pts(lista_pts)


from rest_framework import serializers, status, exceptions
from api.serializer_extender import SerializerExtenderManagerByAddon
from lockable_resource.models import LockableResource
from rqueue.models import Rqueue

# Serializer are helper classes to parse the Model objects to JSON objects,
# so we can return Lockable resources object responses via JSON
class LockableResourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = LockableResource
        fields = "__all__"
        read_only_fields = ["id"]

    # Use this section to add data from addons
    expiry_addon = SerializerExtenderManagerByAddon(
        addon_name="expiry_addon",
        cls_serializer="LockableResourceSerializer",
    )

    def validate(self, attrs):
        """
        Check that the Resource is not Locked
        :param data:
        :return:
        """
        lr_name = attrs.get("name")
        lr_object_pre_save = LockableResource.objects.get(name=lr_name)
        if lr_object_pre_save.is_locked and attrs.get("is_locked"):
            # Important note: raises 406 code status for handling in different services
            raise exceptions.NotAcceptable(
                detail=f"You can not modify a Lockable Resource [{lr_name}] object through the REST API if the resource is locked! "
                "This is prohibited as it might be an attempt locking a resource that is already locked. "
                "Please release first the resource before performing a request that differs from GET. "
            )
        return attrs

class RqueueSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rqueue
        # Here I hardcoded everything, because the ordering is important to display nicer! data is the ugliest display to better it will be last when outputting
        fields = (
            "id",
            "priority",
            "status",
            "time_requested",
            "pending_time",
            "pending_time_descriptive",
            "pended_time_descriptive",
            "description",
            "data",
            "last_beat",
        )


import csv

def where(rows, field, value):
    matches = []
    for row in rows:
        if row[field] == value:
            matches.append(row)
    return matches

def select(rows, field):
    values = []
    for row in rows:
        values.append(row[field])
    return values

def db_sum(rows, field):
    total = 0
    for row in rows:
        total = total + float(row[field])
    return total

with open('courses.csv') as f:
    reader = csv.DictReader(f)
    courses = list(reader)

with open('rooms.csv') as f:
    reader = csv.DictReader(f)
    rooms = list(reader)

def join(rows1, rows2, field):
    new_rows = []
    for row1 in rows1:
        for row2 in rows2:
            if row1[field] == row2[field]:
                new_row = {}
                new_row.update(row1)
                new_row.update(row2)
                new_rows.append(new_row)
    return new_rows

total_capacity = db_sum(join(where(courses, 'times', 'MW10'), rooms, 'roomid'), 'capacity')

print('The total capacity is ' + str(total_capacity))


# coding=utf-8
# Author: Winter_pig 2018-06-27

import ccxt
import time
from datetime import datetime
import math

#取消全部委托单
def CancelPendingOrders(exchange,symbol):
    now = datetime.now()
    orders = getOpenOrders(exchange,symbol)
    for order in orders:
        try:
            if ((now - timestamp_datetime(order['timestamp'])).seconds > 10):
                exchange.cancel_order(order['id'], symbol)
            else:
                continue
        except Exception as e:
            print("Cancel Error: {}".format(e))
        else:
            time.sleep(0.5)

#计算下单价格
def GetPrice(exchange,symbol):
    try:
        orderbook = exchange.fetchOrderBook(symbol)
        bid = orderbook['bids'][1][0] if len(orderbook['bids']) > 0 else None
        ask = orderbook['asks'][1][0] if len(orderbook['asks']) > 0 else None
        spread = (orderbook['asks'][0][0]  - orderbook['bids'][0][0])/2 if (bid and ask) else None
        print (exchange.id, 'market price', {'bid': bid, 'ask': ask, 'spread': spread})
        buy_price = bid + spread
        sell_price = ask - spread
        return buy_price, sell_price
    except Exception as e:
        print("GetPrice Error: {}".format(e))
    else:
        return None,None

def GetTickerPrice(exchange,symbol):
    try:
        #orderbook = exchange.fetchOrderBook(symbol)
        #bid = orderbook['bids'][0][0] if len(orderbook['bids']) > 0 else None
        #ask = orderbook['asks'][0][0] if len(orderbook['asks']) > 0 else None
        ticker = exchange.fetch_ticker(symbol)
        bid = ticker['bid']
        ask = ticker['ask']
        spread = (ask  - bid)/2 if (bid and ask) else None
        print (exchange.id, 'market price', {'bid': bid, 'ask': ask, 'spread': spread})
        return bid, ask, spread
    except Exception as e:
        print("GetPrice Error: {}".format(e))
    else:
        return None,None
#timestamp转换成日期
def timestamp_datetime(ts):
     if isinstance(ts, (int, float, str)):
         try:
             ts = int(ts)
         except ValueError:
             raise

         if len(str(ts)) == 13:
             ts = int(ts / 1000)
         if len(str(ts)) != 10:
             raise ValueError
     else:
        raise ValueError()
     return datetime.fromtimestamp(ts)

def getAccount(exchange):
    try:
        account = exchange.fetchBalance()
        return account
    except Exception as e:
        print("getAccount Error: {}".format(e))
    else:
        time.sleep(3)
        return getAccount(exchange)

def getOpenOrders(exchange, symbol):
    try:
        orders = None
        orders = exchange.fetchOpenOrders(symbol)
        return orders
    except Exception as e:
        print("getOpenOrders Error: {}".format(e))
    else:
        time.sleep(3)
        return getOpenOrders(exchange,symbol)

def adjustFloat(v):
    return math.floor(v*100)/100


import numpy as np

from bases import SimpleGate
from pycyt import math, util

class PolyGate(SimpleGate):

	def __init__(self, channels, vertices, **kwargs):

		if len(channels) != 2:
			raise ValueError('PolyGate must have exactly two channels')

		super(PolyGate, self).__init__(channels, **kwargs)

		if len(vertices) < 2:
			raise ValueError('Must have at least 3 vertices')

		# Process vertices
		self._vertices = []
		for vertex in vertices:

			# Convert to ndarray, remove extra axes
			vertex = np.squeeze(vertex)

			if vertex.shape != (2,):
				raise ValueError('Vertices must be 2-dimensional')

			self._vertices.append(vertex)

		# Check if polygon is convex
		self._is_convex, self._is_rh = math.is_poly_convex(self._vertices)

		# Compute bounding box
		self._bbox = [
			[f(v[i] for v in self._vertices) for f in (min, max)]
			for i in range(2)]

	@property
	def is_convex(self):
		return self._is_convex

	@property
	def bbox(self):
		return [list(r) for r in self._bbox]

	@property
	def vertices(self):
		return self._vertices[:]

	def copy(self, channels=None, vertices=None, **kwargs):
		if channels is None:
			channels = self._channels
		if vertices is None:
			vertices = self._vertices

		return PolyGate(channels, vertices, **kwargs)

	def _inside(self, array):

		# Convex polygon can use faster algorithm
		if self._is_convex:

			contains = np.full(array.shape[0], True, dtype=np.bool)

			# Loop over pairs of vertices (sides)
			for v1, v2 in util.cycle_adjacent(self._vertices, 2):

				# Vectors from vertex 1 to all test points
				dp = array - v1

				# Vector from vertex 1 to vertex 2
				dv = v2 - v1

				# Cross product between them
				cp = dp[:,0] * dv[1] - dp[:,1] * dv[0]

				# Reject where cp isn't positive (or negative if left-handed)
				contains &= (cp > 0) ^ self._is_rh

			return contains

		# Non-convex is slower (there is probably a faster way though)
		else:

			# Test against bounding box
			in_box = np.ones(array.shape[0], dtype=np.bool)
			in_box &= array[:,0] > self._bbox[0][0]
			in_box &= array[:,0] < self._bbox[0][1]
			in_box &= array[:,1] > self._bbox[1][0]
			in_box &= array[:,1] < self._bbox[1][1]

			box_points = array[in_box, :]

			# Will cast a ray from the point in the positive x direction,
			# if it crosses an odd number of times the point is inside.
			# Note - special cases arise when the ray intersects a side
			# just at a vertex. If we count this as a regular side
			# intersection it will be counted twice, which is the same as
			# not counting it at all. Technically this will never ever
			# happen enough to make a difference in an analysis but I'm
			# being pedantic here. The solution is to count all rays
			# passing through a vertex as actually going just above the
			# vertex, forcing it to be consistently counted one way or
			# the other.
			in_p = np.zeros(box_points.shape[0], dtype=np.bool)

			# Loop over 3-tuples of vertices (pairs of sides)
			# Will check the first side each loop but the 2nd is
			# sometimes needed if the ray intersects at a vertex.
			for v1, v2, v3 in util.cycle_adjacent(self._vertices, 3):

				# Indices of points currently looking at
				ci = np.arange(box_points.shape[0])

				# Translate to put v1 at origin
				t_points = box_points - v1
				v = v2 - v1

				# Bounding box of side in these coordinates
				left = min(v[0], 0)
				right = max(v[0], 0)
				bottom = min(v[1], 0)
				top = max(v[1], 0)

				# Skip if point entirely above or below side
				# Note we use >= on the top and < on the bottom, according
				# to the rule of counting exact intersection with a vertex
				# to being slightly above it. This also avoids counting
				# the case where the side is horizontal (v[1] == 0 and
				# top == bottom).
				which = (t_points[:, 1] >= top) |  (t_points[:, 1] < bottom)
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Skip if point is to right of side's bounding box
				which = t_points[:, 0] >= right
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Yes if point to left of side's bounding box
				which = t_points[:, 0] < left
				in_p[ci[which]] = ~in_p[ci[which]]
				t_points = t_points[~which, :]
				ci = ci[~which]

				# Point within bounding box - compare slopes
				which = (v[1] > 0) == (v[0] * t_points[:, 1] > v[1] * t_points[:, 0])
				in_p[ci[which]] = ~in_p[ci[which]]

			contains = np.zeros(array.shape[0], dtype=np.bool)
			contains[in_box] = in_p
			return contains


import argparse
import pandas as pd
import csv
import numpy as np
import json
import sys
sys.path.insert(0, '..')  # Add path from parent folder
sys.path.insert(0, '.')  # Add path from current folder
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.externals import joblib
from sklearn.grid_search import ParameterGrid
from shutil import move
import os

"""
 This class is similar to SKLearn_RandomForest but permutes the labels prior to training.
 The seed for np.random.seed(seed=process_num) is set to process_num.

 Note: Unlike the other models, this model was trained and evaluated on the prospective data
       AFTER the true prospective data was available. The goal is to see if scrambling the
       the labels would result in worse performance for RF_h (best model on prospective data
       according to top250hits).
"""
class RFH_YScrambled:
    def __init__(self, conf, process_num):
        self.conf = conf
        self.input_layer_dimension = 1024
        self.label_names = conf['label_names']
        self.EF_ratio_list = conf['enrichment_factor']['ratio_list']

        self.process_num = process_num

        self.param = conf['params']
        self.n_estimators = self.param['n_estimators']
        self.max_features = self.param['max_features']
        self.min_samples_leaf = self.param['min_samples_leaf']
        self.class_weight = self.param['class_weight']
        print('Testing set:', self.param)

        self.model_dict = {}
        return

    def get_prediction_info(self, X, y_true):
        y_pred = np.zeros(shape=y_true.shape)

        for i, label in zip(range(len(self.label_names)), self.label_names):
            model = self.model_dict[label]

            y_true[np.where(np.isnan(y_true[:,i]))[0],i] = -1
            if i in [0,1,2]:
                rf_pred = model.predict_proba(X)
                if rf_pred.shape[1] == 1:
                    y_pred[:,i] = 0
                else:
                    y_pred[:,i] = rf_pred[:,1]

        return y_true, y_pred

    def setup_model(self):
        for i in range(len(self.label_names)):
            self.model_dict[self.label_names[i]] = RandomForestClassifier(n_estimators=self.n_estimators,
                                           max_features=self.max_features,
                                           min_samples_leaf=self.min_samples_leaf,
                                           n_jobs=3,
                                           class_weight=self.class_weight,
                                           random_state=self.process_num,
                                           oob_score=False,
                                           verbose=1)
        return

    def train(self, X_train, y_train, model_file):
        self.setup_model()

        # perform random shuffling of training data (including X_train)
        p = np.random.permutation(len(X_train))
        X_train = X_train[p,:]
        y_train = y_train[p,:]

        for i, label in zip(range(len(self.label_names)), self.label_names):
            y = y_train[:,i]
            indexes = np.where(np.isnan(y))[0]

            y = np.delete(y, indexes, axis=0)
            X = np.delete(X_train, indexes, axis=0)
            self.model_dict[label].fit(X, y)

            joblib.dump(self.model_dict[label], model_file+'_'+label+'.pkl', compress = 1)
        return

    def predict_with_existing(self, X, y):
        y_true, y_pred = self.get_prediction_info(X, y)

        print
        print('precision: {}'.format(precision_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print('roc: {}'.format(roc_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print('bedroc: {}'.format(bedroc_auc_multi(y_true, y_pred, range(y_true.shape[1]), np.mean)))
        print

        label_list = self.label_names
        nef_auc_mean = np.mean(np.array(nef_auc(y_true, y_pred, self.EF_ratio_list, label_list)))
        print('nef auc: {}'.format(nef_auc_mean))
        return

    def save_model_params(self, config_csv_file):
        data = str(self.param)
        with open(config_csv_file, 'w') as csvfile:
            csvfile.write(data)
        return

'''
Note: Copied from function.py

Read the data from all files in input_file_list
And merged into one dataset
'''
def read_merged_data(input_file_list, usecols=None):
    whole_pd = pd.DataFrame()
    for input_file in input_file_list:
        data_pd = pd.read_csv(input_file, usecols=usecols)
        whole_pd = whole_pd.append(data_pd)
    return whole_pd

'''
Note: Copied from function.py

Reshape vector into 2-dimension matrix.
'''
def reshape_data_into_2_dim(data):
    if data.ndim == 1:
        n = data.shape[0]
        data = data.reshape(n, 1)
    return data

'''
Note: Copied from function.py

Get the fingerprints, with feature_name specified, and label_name specified
'''
def extract_feature_and_label(data_pd,
                              feature_name,
                              label_name_list):
    # By default, feature should be fingerprints
    X_data = data_pd[feature_name].tolist()
    X_data = [list(x) for x in X_data]
    X_data = np.array(X_data)

    y_data = data_pd[label_name_list].values.tolist()
    y_data = np.array(y_data)
    y_data = reshape_data_into_2_dim(y_data)

    X_data = X_data.astype(float)
    y_data = y_data.astype(float)

    return X_data, y_data

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_json_file', action="store", dest="config_json_file", required=True)
    parser.add_argument('--model_dir', action="store", dest="model_dir", required=True)
    parser.add_argument('--dataset_dir', action="store", dest="dataset_dir", required=True)
    parser.add_argument('--prospective_file', action="store", dest="prospective_file", required=True)
    parser.add_argument('--process_num', action="store", dest="process_num", required=True)
    #####
    given_args = parser.parse_args()
    config_json_file = given_args.config_json_file
    model_dir = given_args.model_dir
    dataset_dir = given_args.dataset_dir
    prospective_file = given_args.prospective_file
    process_num = int(given_args.process_num)
    #####
    config_csv_file = model_dir+'model_config.csv'
    #####
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)

    # set seed according to process_num
    np.random.seed(seed=process_num)

    # specify dataset
    directory = dataset_dir
    file_list = []
    k=5
    for i in range(k):
        file_list.append('file_{}.csv'.format(i))

    # merge training and test dataset
    dtype_list = {'Molecule': np.str,
                  'SMILES': np.str,
                  'Fingerprints': np.str,
                  'Keck_Pria_AS_Retest': np.int64,
                  'Keck_Pria_FP_data': np.int64,
                  'Keck_Pria_Continuous': np.float64,
                  'Keck_RMI_cdd': np.float64}
    output_file_list = [directory + f_ for f_ in file_list]

    with open(config_json_file, 'r') as f:
        conf = json.load(f)

    if not os.path.exists(model_dir+'process_'+str(process_num)):
        os.makedirs(model_dir+'process_'+str(process_num))
    model_file = model_dir+'process_'+str(process_num)+'/rf_clf'

    csv_file_list = output_file_list[:]
    train_pd = read_merged_data(csv_file_list)
    test_pd = pd.read_csv(prospective_file, compression='gzip')

    labels = ["Keck_Pria_AS_Retest"]

    # extract data
    X_train, y_train = extract_feature_and_label(train_pd,
                                                 feature_name='Fingerprints',
                                                 label_name_list=labels)
    X_test, y_test = extract_feature_and_label(test_pd,
                                               feature_name='Fingerprints',
                                               label_name_list=labels)

    # scramble y_train
    ind_permutation = np.random.permutation(y_train.shape[0])
    assert not np.array_equal(y_train, y_train[ind_permutation,:]) # check original labels and permuted labels not the same
    y_train = y_train[ind_permutation,:]
    print('done data preparation')

    task = RFH_YScrambled(conf=conf, process_num=process_num)
    task.train(X_train, y_train, model_file)
    task.save_model_params(config_csv_file)

    # save prediction arrays
    y_train, y_pred_on_train = task.get_prediction_info(X_train, y_train)
    y_test, y_pred_on_test = task.get_prediction_info(X_test, y_test)
    npz_file_name = model_dir+'process_'+str(process_num)
    np.savez_compressed(npz_file_name,
                        labels=labels, y_train=y_train, y_val=None, y_test=y_test,
                        y_pred_on_train=y_pred_on_train, y_pred_on_val=None, y_pred_on_test=y_pred_on_test)


from openerp import models, fields, api, _

class AnalyticAccount(models.Model):

    _inherit = 'account.analytic.account'

    @api.one
    def parent_name_get(self):
        if self.parent_id:
            if self.parent_id.parent_id:
                parent_name = self.parent_id.parent_name_get()[0]
                return parent_name + '/' + self.parent_id.code
            else:
                return self.parent_id.code
        else:
            return ''

    @api.one
    def _short_name(self):
        if self.parent_id:
            parent_name = self.parent_name_get()[0]
            self.short_name = parent_name + '/' + self.name
        else:
            self.short_name = self.name

    short_name = fields.Char ('Account Name', compute='_short_name')

    @api.multi
    def name_get(self):
        result = []
        for account in self:
            result.append((account.id, account.short_name ))
        return result

    @api.model
    def name_search(self, name, args=None, operator='ilike', limit=100):
        args = args or []
        recs = self.browse()
        if name:
            recs = self.search([('code', operator, name)] + args, limit=limit)
        if not recs:
            recs = self.search([('name', operator, name)] + args, limit=limit)
        return recs.name_get()


n = int(input())
house = list(map(int, input().split()))
distance = 0

for i in house:
    distance += i

avg = distance/n
closet = []
for i in house:
    closet.append(abs(avg - i))

print(house[closet.index(min(closet))])


from runner_class import Runner
from jumper_class import Jumper

class Discipline:
    def __init__(self, name: str, discipline_type: str, num_of_competitors: int):
        self.name = name
        self.discipline_type = discipline_type
        self.num_of_competitors = num_of_competitors
        self.athlete_info = []

    def load_info(self):
        for competitor in range(self.num_of_competitors):
            print(f"Takmicar broj {competitor + 1}")
            name = input(f"Ime: ")
            surname = input("Prezime: ")
            score = int(input("Rezultat: "))
            if self.discipline_type.lower() == "runner":
                self.athlete_info.append(Runner(name, surname, score, self.name))
            elif self.discipline_type.lower() == "jumper":
                self.athlete_info.append(Jumper(name, surname, score, self.name))

    def get_winner_info(self):
        list_of_scores = []
        for athlete in self.athlete_info:
            list_of_scores.append(athlete.get())
        if self.discipline_type.lower() == "runner":
            index = list_of_scores.index(min(list_of_scores))
        elif self.discipline_type.lower() == "jumper":
            index = list_of_scores.index(max(list_of_scores))
        print("\nPodaci o pobedniku:")
        return self.athlete_info[index].__str__() + "\n"

Week 6/homework 10/part 2/discipline_class.py


import sys
import copy
input = sys.stdin.readline
N = int(input())
ans = 0
xy = [list(map(int, input().split())) for i in range(N)]

def maken(n):
    s = 1
    for i in range(1, n+1):
        s *= i
    return s

for i in range(N):
    xy2 = copy.deepcopy(xy)
    c = xy2[i]
    del xy2[i]
    for l in xy2:
        ans += (((c[0]-l[0])**2+(c[1]-l[1])**2)**(1/2))*(maken(N-1))
print(ans/maken(N))


A,B=map(str,input().split())
C={}
D=0
for i in range(len(A)):
    if(A[i] not in C.keys()):
        C[A[i]]=B[i]
    else:
        if(C[A[i]]==B[i]):
            continue
        else:
            D=D+1
if(D==1 or D==0):
    print("yes")
else:
    print("no")


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        queue = deque([(root)])
        direction = 1
        result = []
        while queue:
            size = len(queue)
            level = []

            for _ in range(size):
                node = queue.popleft()
                if node:
                    level.append(node.val)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
            if level:
                result.append(level[::direction])
            direction *= -1
        return result


from Logger.PlotLogger import PlotLogger
from Network.LstmAutoencoder import LstmAutoencoder
from TaskConfig.ITaskConfig import ITaskConfig
from Trainers.RAETrainer import RAETrainer
from DataNormalizer.DataNormalizer import DataNormalizer
from DataSeperator.NoSepDataSeperator import NoSepDataSeperator

import torch
import os.path as path

class RAETaskConfig(ITaskConfig):
    def __init__(self, modelFolderPath):
        self.modelFolderPath = modelFolderPath

    def getConfig(self, isCuda = False):
        feature_size = 1
        output_size = 1
        logger = PlotLogger()
        mlModel = LstmAutoencoder(feature_size,4,output_size,2)
        taskName = 'RegularTask'
        try:
            mlModel.load_state_dict(torch.load(path.join(self.modelFolderPath, taskName + ".pt")))
        except:
            pass
        if torch.cuda.is_available():
            mlModel.cuda()
        trainer = RAETrainer(mlModel, logger)
        datasetSeperator = NoSepDataSeperator()
        dataNormalizer = DataNormalizer()

        return mlModel, datasetSeperator, trainer, logger, dataNormalizer, taskName


# code by @JymPatel
# edited by @bupboi1337, (editors can put their name here && thanks for contribution :)

# this code uses GPL V3 LICENSE
## check license it https://github.com/JymPatel/Python-FirstEdition/blob/Main/LICENSE
print("this code uses GPL V3 LICENSE")
print("")

# start of code
# import library
import pickle
import os

# get array from pickle data
infile = open('data/pickle-main', 'rb')
# defining array
array = pickle.load(infile)
infile.close()

# get key if path exists
keyacess = False
path = 'data/pickle-key'
if os.path.isfile('data/pickle-key'):
    pklekey = open('data/pickle-key', 'rb')
    key = pickle.load(pklekey)
    pklekey.close()
    if key == 'SKD0DW99SAMXI19#DJI9':
        keyacess = True
        print("key found & is correct")
        print("ALL FEATURES ENABLED")
    else:
        print("key is WRONG\nSOME FEATURES ARE DISABLED")
        print("check https://github.com/JymPatel/Python-FirstEdition/tree/Main/PyPrograms/contacts for key, it's free")
        print("key isn't added to this repo check above repo")
else:
    print("key not found\nSOME FEATURES ARE DISABLED")
    print("check https://github.com/JymPatel/Python-FirstEdition/tree/Main/PyPrograms/contacts for key, it's free")

print("")
print("update-22.02 ADDS SAVING YOUR DATA WHEN CLOSED BY SAVING USING OPTION 0\n##")

# for ease in reading
fname = 0
lname = 1
number = 2
email = 3
# getting some variables
promptvar = 0 # variable for prompt
loopvar = 0 # variable for main loop
# making loop to run
while loopvar < 1:
    # ask user what to do
    print("")  # putting blank line before running new loop
    if promptvar == 0:
        print("0.  exit program")
        print("1.  get all contacts")
        print("2.  add new contact")
        print("3.  remove any contact")
        print("4.  sort contacts by first name")
        print("9.  stop getting this prompt")

    a = input("WHAT WOULD YOU LIKE TO DO?  ")

    # check for integer & calculate length of array
    try:
        a = int(a)
    except ValueError:
        print("!! PLEASE ENTER AN INTEGRAL VALUE")
    # get length of array
    arraylen = len(array[fname])

    # if option 1 is selected
    if a == 1:
        print("")
        print("== YOUR CONTACT LIST ==")
        print("")
        i1 = 0
        # print all names
        while i1 < arraylen:
            print(f"{array[fname][i1]} {array[lname][i1]},  {array[number][i1]}  {array[email][i1]}")
            i1 += 1
        print("=======================")

    # option 2 is selected
    elif a == 2:
        # get a new contact
        array[fname].append(input("First Name: "))
        array[lname].append(input("Last Name: "))
        array[number].append(input("Phone Number: "))
        array[email].append(input("email ID: "))
        arraylen += 1

    # option 3
    elif a == 3:
        print("which contact would you like to delete? (enter first name)")
        print("enter '\nSTOP' to STOP deleting contact")
        rmcontact = input("INPUT:  ")
        if rmcontact != '\nSTOP':
            tempvar = 0
            rmvar = 0
            for i in range(arraylen):
                if array[fname][i].upper() == rmcontact.upper():
                    tempvar += 1
                    rmvar = i
            # if no cotacts found
            if tempvar == 0:
                print("no cantact matches first name provided")
            # if only one contact is found
            elif tempvar == 1:
                print("DO YOU WANT TO DELETE CONTACT")
                for i in range(4):
                    print(array[i][rmvar])
                tempinp = input("y/n?  ")
                if tempinp == 'y' or tempinp == 'Y':
                    for i in range(4):
                        del array[i][rmvar]
                    print("contact REMOVED.")
                else:
                    print("failed to REMOVE contact")
            # if more than one contact is found
            else:
                print("there are more than one contact with same name")
                # TODO

    # if option 4 is selected
    elif a == 4:
        if keyacess == True:
            sortcounter = 1
            while sortcounter != 0:
                # reset counter
                sortcounter = 0
                arraylen = len(array[fname])
                for i in range(arraylen - 1):
                    if array[fname][i].upper() > array[fname][i + 1].upper():
                        for j in range(4):
                            temp = array[j][i]
                            array[j][i] = array[j][i + 1]
                            array[j][i + 1] = temp
                        # add one for changing values
                        sortcounter += 1
                    if array[fname][i].upper() == array[fname][i + 1].upper():
                        # if first name are same, compare last
                        if array[lname][i].upper() > array[lname][i + 1].upper():
                            for j in range(4):
                                temp = array[j][i]
                                array[j][i] = array[j][i + 1]
                                array[j][i + 1] = temp
                            # add one for changing values
                            sortcounter += 1
            # if no values are swapped, sortcounter = 0; no next loop
            print("CONTACTS ARE NOW SORTED")
        else:
            print("NEED CORRECT KEY TO ENABLE THIS FEATURE")

    # option 9
    elif a == 9:
        if keyacess:
            # change prompt settings
            if promptvar == 0:
                promptvar += 1
                print("you won't get prompt now!")
                print("ENTER 9 AGAIN TO START GETTING PROMPT AGAIN!!")
            else:
                promptvar -= 1
        else:
            print("NEED CORRECT KEY TO ENABLE THIS FEATURE")

    # if option 0 is selected
    elif a == 0:
        print("Saving your Data ...")
        outfile = open('data/pickle-main', 'wb')
        pickle.dump(array, outfile)
        outfile.close()
        print("YOUR DATA HAS BEEN SAVED SUCESSFULLY!")
        loopvar += 1

    # if no true option is selected
    else:
        print("!! PLEASE ENTER VALUE FROM GIVEN INTEGER")

# end of code
print("")
print("get this code at https://github.com/JymPatel/Python-FirstEdition")


from __future__ import print_function, division

import odrive
from odrive.enums import *
import time
import math
import numpy as np
import readchar
import sys
testing = False # testing variable
command = ["Stopped", 0]
incrament = 0.5

config = {
	"wheel_radius" : 0.1,
	"drive_gearing" : 28.55, # 64 means a gearing of 64 to one
	"flipper_gearing" : 81.37
}

serial_list_raw = ["209137933548", "336631563536"]

odrv = None

############################################
#### CALIBRATION PROCESS ###################
############################################

# Calibrate one odrive
# Input: Serial id of the odrive as a string
# Output: ODrive object
def full_reset_and_calibrate(targetID, i=0):
	"""Completely resets the Odrive, calibrates axis0 and configures axis0 to only encoder index search on startup and be ready in AXIS_STATE_CLOSED_LOOP_CONTROL"""
	print("------------- CALIBRATION ODRV " + str(i) + " -------------\n")
	print("Searching for Serial number: " + targetID)
	odrv0 = odrive.find_any(serial_number=targetID)

	odrv0.erase_configuration()
	print("ODRV NO." + str(i) + "  " + "Erased [1/7]")
	try: # Reboot causes loss of connection, use try to supress errors
		odrv0.reboot()
	except:
		pass
	print("ODRV NO." + str(i) + "  " + "Rebooted [2/7]")
	odrv0 = odrive.find_any(serial_number = targetID) # Reconnect to the Odrive
	print("ODRV NO." + str(i) + "  " + "Connected [3/7]")
	odrv0.axis0.motor.config.pre_calibrated = True # Set all the flags required for pre calibration
	odrv0.axis0.encoder.config.pre_calibrated = True
	odrv0.axis0.encoder.config.use_index = True
	odrv0.axis0.config.startup_encoder_index_search = True # Change startup sequence
	odrv0.axis0.config.startup_closed_loop_control = True
	odrv0.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE # Calibrate
	print("ODRV NO." + str(i) + "  " + "Started calibration 1 [4/7]", end="")
	while odrv0.axis0.current_state != AXIS_STATE_IDLE: # Wait for calibration to be done
		time.sleep(0.1)
		print(".", end="")
	odrv0.save_configuration()

	print("\nODRV NO." + str(i) + "  " + "AXIS 1. Calibration complete [5/7]\n")
	print("now will begin calibration sequence for second axis for ODRV NO." + str(i))
	time.sleep(3)
	odrv0.axis1.motor.config.pre_calibrated = True # Set all the flags required for pre calibration
	odrv0.axis1.encoder.config.pre_calibrated = True
	odrv0.axis1.encoder.config.use_index = True
	odrv0.axis1.config.startup_encoder_index_search = True # Change startup sequence
	odrv0.axis1.config.startup_closed_loop_control = True
	odrv0.axis1.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE # Calibrate
	print("ODRV NO." + str(i) + "  " + "Started calibration 2 [6/7]", end="")
	while odrv0.axis1.current_state != AXIS_STATE_IDLE: # Wait for calibration to be done
		time.sleep(0.5)
		print(".", end="")

	print("\nODRV NO." + str(i) + "  " + "AXIS 2. Calibration 2 complete [7/7]")

	#closed loop control for both axis
	odrv0.save_configuration()
	odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
	odrv0.axis1.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL

	return odrv0

# Calibrate all odrives.
# Input: Serial ids of the odrives as a list of strings
# Output: List of all ODrive objects
def calibrateODrives(serialIDs):
	odrives = []
	for i, ID in enumerate(serialIDs):
		odrives.append(full_reset_and_calibrate(ID, i+1))

	return odrives

def obtainODriveObjects(serialIDs):
	print("No calibration Mode")
	odrives = []
	for i, ID in enumerate(serialIDs):
		print("Searching for ODrive with id = " + ID)
		odrives.append(odrive.find_any(serial_number=ID))
		print("Found an ODrive")

	return odrives

############################################
#### ODRIVE CONTROL COMMANDS ###############
############################################

# Set rps of the output shaft
# Input: axis as ODrive object, desired rps of the output, gear ratio of output to motor
def set_rps(axis, rps, gear_ratio):
	axis.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
	axis.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL
	axis.controller.vel_setpoint = rps * 2 * math.pi * gear_ratio * 8192

# Set current of a particular axis
# Input: axis as ODrive object, max current through that axis
def set_current_limit(axis, current_limit):
	axis.motor.config.current_lim = current_limit

# Set current of a particular axis
# Input: axis as ODrive object, max current through that axis
def set_velocity_limit(axis, velocity_limit):
	axis.controller.config.vel_limit = velocity_limit

############################################
#### PROGRAM NAVIGATION ####################
############################################

def initial_setting(debug = False):
	global odrv

	if not debug:
		print("-------------- ODRIVE SANITY CHECK --------------\n\n")
		print("Full Reset and Calibrate? (y/n)")

		while 1:
			value = input()
			if value == 'y':
				odrv = calibrateODrives(serial_list_raw)
				break
			elif value == 'n':
				odrv = obtainODriveObjects(serial_list_raw)
				break
			else:
				print("Enter either y or n to continue")

def control_loop(debug = False):
	global odrv

	print("Control Loop: Press Enter to Begin")
	input()

	previous = 0
	speed = 0
	incrament = 0.0002
	threshold = incrament * 10

	max_velocity = 500000

	if not debug:
		set_velocity_limit(odrv[0].axis0, max_velocity)
		set_velocity_limit(odrv[0].axis1, max_velocity)
		set_velocity_limit(odrv[1].axis0, max_velocity)
		set_velocity_limit(odrv[1].axis1, max_velocity)

		rightTrack = odrv[0].axis1
		leftTrack = odrv[0].axis0
		frontFlipper = odrv[1].axis0
		rearFlipper = odrv[1].axis1

	mode = 0

	select = ["Move FWD", "Move Left Track", "Move Right Track", "Move Front Flipper", "Move Rear Flipper"]
	while 1:

		input_val = readchar.readchar()

		if input_val == "1":
			mode = 1
			print("--  MODE : " + select[mode - 1])
		elif input_val == "2":
			mode = 2
			print("--  MODE : " + select[mode - 1])
		elif input_val == "3":
			mode = 3
			print("--  MODE : " + select[mode - 1])
		elif input_val == "4":
			mode = 4
			print("--  MODE : " + select[mode - 1])
		elif input_val == "5":
			mode = 5
			print("--  MODE : " + select[mode - 1])

		elif input_val == "w":
			speed += incrament
			print("speed INCREASE")
		elif input_val == "s":
			speed -= incrament
			print("speed DECREASE")
		elif input_val == "0":
			speed = 0
			print("speed ZERO")
		elif input_val == "q":
			break
		else:
			print("No command parsed")

		current = speed

		if current < 0:
			current = 0

		if abs(previous - current) > threshold:
			current = previous + np.sign(current - previous)*threshold

		if not debug:

			if mode == 1:
				set_rps(rightTrack, -1* current, config['drive_gearing'])
				set_rps(leftTrack, current, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 2:
				set_rps(rightTrack, current, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 3:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, current, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 4:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, current, config['flipper_gearing'])
				set_rps(rearFlipper, 0, config['flipper_gearing'])

			elif mode == 5:
				set_rps(rightTrack, 0, config['drive_gearing'])
				set_rps(leftTrack, 0, config['drive_gearing'])
				set_rps(frontFlipper, 0, config['flipper_gearing'])
				set_rps(rearFlipper, current, config['flipper_gearing'])

		print("speed is: " + str(current))

		previous = current

# MAIN PROGRAM
def main():
	initial_setting(debug = False)
	control_loop(debug = False)

if __name__ == "__main__":
	main()


from typing import Optional, List, Dict

from fastapi import Request, FastAPI
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from .dense_classifier import predict_emotions_with_dense_model
from .embedding import get_embedding_for_list_of_texts
from .seq_classifier import predict_emotions_with_seq_model
from .conv_classifier import predict_emotions_with_conv_model
from copy import deepcopy

from pydantic import BaseModel

class Item(BaseModel):
    tweets: List[Dict[str, str]] = []

app = FastAPI()

origins = [
    "*",
    "https://smapwr.github.io",
    "http://127.0.0.1",
    "http://localhost",
    "http://localhost:3000",
    "http://localhost:3001",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def read_root():
    return RedirectResponse("/docs")

@app.get("/status")
def read_root():
    return {"status": True}

@app.post("/predictions/")
async def get_predictions(request: Item):
    """
    :param request:
        [
            {
                "text": str,
                "id": str
            }
        ]
    :return: {
        "data": [
            {
                "id": str,
                "text": str,
                "model1": {

                },
                "model2": {

                },
                "model3": {

                }
            }
        ]
    }
    """
    body = request.dict()
    print(body)

    response = {"data": []}

    texts = [item["text"] for item in body["tweets"]]

    (
        seq_embeddings_tensor,
        sentence_embeddings_tensor,
    ) = get_embedding_for_list_of_texts(texts)

    list_of_predicted_emotions_in_dense_model = predict_emotions_with_dense_model(
        sentence_embeddings_tensor
    )

    list_of_predicted_emotions_in_sequence_model = predict_emotions_with_seq_model(
        seq_embeddings_tensor
    )

    list_of_predicted_emotions_in_teacher_student_model = (
        predict_emotions_with_conv_model(sentence_embeddings_tensor)
    )

    for idx, item in enumerate(body["tweets"]):

        response["data"].append(
            {
                "id": item["id"],
                "text": item["text"],
                "model1": list_of_predicted_emotions_in_dense_model[idx],
                "model2": list_of_predicted_emotions_in_sequence_model[idx],
                "model3": list_of_predicted_emotions_in_teacher_student_model[idx],
            }
        )

    return response


'''
Using the JavaScript support in requests_html, parse the contents of
an Instagram page you are interested in.

Fetch and prepare all the image links - and only the image links!
* How can you exclude other links present on the page?
* BONUS: Can you find a way to download those images and save them to
         your computer using python?

'''

from requests_html import HTMLSession

session = HTMLSession()
r = session.get('https://www.instagram.com/martin.breuss/')
r.html.render()

list_img = [link for link in r.html.absolute_links if link[:28] == "https://www.instagram.com/p/"]

print(list_img)


n=int(input())
boys=input().split(' ');boys=[int(x) for x in boys]
m=int(input())
girls=input().split(' ');girls=[int(x) for x in girls]
boys.sort();girls.sort()
count1=0
for i in range(len(boys)):
    #rec=len(boys)
    for j in range(len(girls)):
        if len(girls)==0:
            break
        if boys[i]+1 >= girls[j] >= boys[i]-1:
            count1+=1
            del girls[j]
            break
    #if rec==len(boys):
     #   del boys[i]
print(count1)


import yaml

def main():
    import argparse

    parser = argparse.ArgumentParser(description="Diff the MASTG test cases covered.")
    parser.add_argument("-o", "--old", required=True)
    parser.add_argument("-n", "--new", required=True)

    args = parser.parse_args()

    MASVS_OLD = yaml.safe_load(open(args.old))
    MASVS_NEW = yaml.safe_load(open(args.new))

    updated = 0
    added = 0
    removed = 0

    print("OWASP MAS Checklists Changes")

    for mstg_id, req in MASVS_NEW.items():
        old_links = MASVS_OLD[mstg_id].get("links")
        new_links = req.get("links")

        if old_links and new_links:
            diff = list(set(new_links) - set(old_links))
            updated += 1
            if diff:
                print(f"- [UPDATED] {mstg_id}:")
                for link in diff:
                    print(f"  - {link}")
                print("\n")
        elif old_links is None and new_links:
            added += 1
            print(f"- [ADDED] {mstg_id}:")
            for link in new_links:
                print(f"  - {link}")
            print("\n")
        elif old_links and new_links is None:
            removed += 1
            print(f"- [REMOVED] {mstg_id}\n")

    print(f"\nSUMMARY: removed ({removed}) added ({added}) updated ({updated})")

if __name__ == "__main__":
    main()


import heapq

def solution(land, height):
    N = len(land)  # land의 길이

    visited = [[False for _ in range(N)] for _ in range(N)]  # 방문했는지
    move = [(0, 1), (0, -1), (1, 0), (-1, 0)] # 이동할 방향
    queue = [] # 뀨?

    queue.append((0, 0, 0))  # 큐에 초기값 지정
    visit_count = 0  # 방문 횟수
    max_count = N * N  # 최대 방문 가능 횟수
    value = 0  # 사다리 설치비용

    while(visit_count < max_count):
        print(queue)
        v, y, x = heapq.heappop(queue)
        if visited[y][x]:  # 이미 방문했다면
            continue # 넘긴다
        visited[y][x] = True  # 방문 완료

        visit_count += 1  # 방문 횟수 + 1
        value += v  # 사다리 설치 비용 추가

        c_height = land[y][x]  # 현 좌표의 height
        for ay, ax in move:
            ny, nx = y + ay, x + ax  # 이동한 좌표의 y, x
            if move2(ny, nx, N, visited):  # 이동 가능한지 체크
                n_height = land[ny][nx]  # 이동한 좌표의 height

                if abs(n_height - c_height) > height:  # 사다리가 필요한 경우 필요한 비용을 값으로 줌
                    heapq.heappush(queue, (abs(n_height - c_height), ny, nx))
                else:  # 사다리 없이 방문 가능한 좌표는 비용은 0을 준다
                    heapq.heappush(queue, (0, ny, nx))
    return value


from pathlib import Path
from contextlib import contextmanager
from functools import wraps
import csv
import pickle
import logging

@contextmanager
def dataset(name):
    path = Path(__file__) \
        .parent \
        .parent \
        .joinpath('datasets') \
        .joinpath('{}.csv'.format(name))

    print(path)
    with path.open() as f:
        yield csv.DictReader(f)

def cached(name):
    def decorator(fn):
        @wraps(fn)
        def wrapper():
            path = Path(__file__) \
                .parent \
                .parent \
                .joinpath('cached') \
                .joinpath('{}.pickle'.format(name))

            try:
                with path.open('rb') as f:
                    return pickle.load(f)
            except Exception as e:
                logging.info("Can't load cached object %s: %s", name, e)
                val = fn()

                with path.open('wb') as f:
                    pickle.dump(val, f)

                return val

        return wrapper

    return decorator


# -*- coding: utf-8 -*-
"""
Assignment 3:
Write a python class that is able to return the flight distance between two cities given their
latitude and longitude coordinates.
Expected output:
$ python city_distance.py
> City 1: 51.5074 N, 0.1278 W
> City 2: 48.8566 N, 2.3522 E
> Output: City 1 and City 2 are 342.87 km apart
"""
from math import sin, cos, sqrt, atan2, radians

class Distance:
    def __init__(self, lat_1, lon_1, lat_2, lon_2):
        self.lat1=lat_1
        self.lon1=lon_1
        self.lat2=lat_2
        self.lon2=lon_2

    def flightDistance(self):
        R=6371      #Radius of earth in km

        self.lat1 = radians(self.lat1)
        self.lon1 = radians(self.lon1)
        self.lat2 = radians(self.lat2)
        self.lon2 = radians(self.lon2)

        #Haversine Formula that will calculate the flight distance between two cities given their latitude and longitude coordinates.
        dLon=self.lon2 - self.lon1
        dLat=self.lat2 - self.lat1

        a = sin(dLat / 2)**2 + cos(self.lat1) * cos(self.lat2) * sin(dLon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        distance = R * c

        return distance

if __name__=="__main__":
    city1=input("(eg. City 1: 51.5074 N, 0.1278 W) Enter City 1: ")
    city2=input("(eg. City 2: 48.8566 N, 2.3522 E) Enter City 2: ")
    City1= city1.strip().split()
    City2=city2.strip().split()
    #print(City1)
    #print(City2)
    """
    Implementing convention:
        W(-)
        E(+)
        S(-)
        N(+)
        """

    if City1[1]=="W," or City1[1]=="W" or City1[1]=="S," or City1[1]=="S":
        City1[0]= -float(City1[0])
    elif City1[3]=="W," or City1[3]=="W" or City1[3]=="S," or City1[3]=="S":
        City1[2]= -float(City1[2])
    elif City2[1]=="W," or City2[1]=="W" or City2[1]=="S," or City2[1]=="S":
        City2[0] = -float(City2[0])
    elif City2[3]=="W," or City2[3]=="W" or City2[3]=="S," or City2[3]=="S":
        City2[2] = -float(City2[2])

    lat_1=float(City1[0])
    lon_1=float(City1[2])
    lat_2=float(City2[0])
    lon_2=float(City2[2])

    result=Distance(lat_1, lon_1, lat_2, lon_2).flightDistance()
    print("City 1 and City 2 are {:.2f} km apart".format(result))


"""
This task is very weird.

1. Clear the story of punctuation.
2. Create var story_words: list with words from story
3. Create var story_list: list with sentences from story
4. 2 vars that has Number of elements in story_words & story_list
5. Add story_title to story_list and save it to new var full_text_list
6. Select 3 list methods and use them with story_words
"""

story_title = 'THE BOY WHO LIVED'
story = "Mr. and Mrs. Dursley, of number four, Privet Drive, were \
        proud to say that they were perfectly normal, thank \
        you very much. They were the last people you’d expect to be involved in anything strange or mysterious, \
        because they just didn’t hold with such nonsense. \
        Mr. Dursley was the director of a fi rm called Grunnings, which \
        made drills. He was a big, beefy man with hardly any neck, although he did have a very large mustache. \
        Mrs. Dursley was thin and blonde and had nearly twice the usual amount of neck, which \
        came in very useful as she spent so much of her time craning over \
        garden fences, spying on the neighbors. The Dursleys had a small \
        son called Dudley and in their opinion there was no fi ner boy \
        anywhere."

def punctuation_handler(text, dict_) -> str:
	"""
	format for the dict:
	"from_what_change": "to_what_change"

	dict_ = {
	"." : "--",
	"," : "--"
	}				<<< - it will change all . and , to --

	Returns back text with changed text
	:param text: text
	:param dict_: dict with changes
	:return: changed text
	"""
	for k, v in dict_.items():
		text = text.replace(k, v)
	return text

def delete_punctuation(text: str) -> str:
	"""
	Check every chapter in the text, if it's letter or space - it adds to the another
	list and then joins it to one string. In the result we have line with no punctuation
	:param text: text from which delete delete_punctuation
	:return: text with no delete_punctuation
	"""
	return "".join([x for x in text if x.isalpha() or x.isspace()])

delete_dot = {
	"Mrs. ": "Mrs ",
	"Mr. ": "Mr "
}

return_dot = {
	"Mrs ": "Mrs. ",
	"Mr ": "Mr. "
}

# 0 - Delete unnecessary spaces
while "  " in story:
	story = story.replace("  ", " ")
# Also can be done by using split and then join. But I think "while" is faster
# " ".join(story.split())

# 1
one_line = delete_punctuation(story)
story_no_punctuation = punctuation_handler(one_line, return_dot)
print(story_no_punctuation)

# 2
story_words = story_no_punctuation.split(" ")
print(story_words)

# 3
raw_story = punctuation_handler(story, delete_dot)		# delete DOTs from Mr & Mrs
story_list_punctuation = raw_story.split(".")			# split by dot
story_list_no_punctuation = [delete_punctuation(x.strip()) for x in story_list_punctuation if x]	# remove punctuation
story_list = [punctuation_handler(x, return_dot) for x in story_list_no_punctuation]		 # return DOTs to Mr and Mrs
print(story_list)										# Done

# 4
number_of_words = len(story_words)
number_of_sentences = len(story_list)
print(number_of_words, number_of_sentences, sep=" - ")

# 5
story_list.append(story_title)			# add
full_text_list = story_list.copy()		# save to new var

# 6
without_upper = full_text_list.pop()	# delete title

# 7 - Done
res = story_words.pop(0)
story_words.append(res)
story_words.clear()


from .utils import Atom, Residue, ActiveSite
import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
import copy
import umap
import seaborn as sns

def compute_similarity(site_a, site_b):
    """
    Compute the similarity between two given ActiveSite instances.

    Input: two ActiveSite instances
    Output: the similarity between them (a floating point number)
    """
    similarity = 0.0
    #print("THIS LINE IS TO TEST THAT COMPUTE SIMILARITY IS RUNNING")

    ## my GOAL is to have my similarity matrix compare how many amino acids
    ## residues of each protein have in common and normalize it to total they could have in common
    ##  of the residues to account for the varying length of the residues

    AminoAcids = {'ALA': 'A', 'ARG': 'R', 'ASN': 'N', 'ASP': 'D', 'CYS': 'C',
                  'GLU': 'E', 'GLN': 'Q', 'GLY': 'G', 'HIS': 'H', 'ILE': 'I',
                  'LEU': 'L', 'LYS': 'K', 'MET': 'M', 'PHE': 'F', 'PRO': 'P',
                  'SER': 'S', 'THR': 'T', 'TRP': 'W', 'TYR': 'Y', 'VAL': 'V'}
                  #shorten them to their 1 letter code to make this easier

    def AminoAcidSequence(active_site):
        return [AminoAcids[i.type[0:3]] for i in active_site.residues]
        #convert the input sequences so they are more workable

    seqA = AminoAcidSequence(site_a) #take the inputs to the compute similarity function
    seqB = AminoAcidSequence(site_b) #and convert their sequences using new function
    observedRes = set(seqA + seqB) #these are the residues that are observed in the inputs
    total = 0 #initializations
    common = 0
    for res in observedRes: #for each amino acid residue in observed residues
        inA = seqA.count(res) #count the # of occurrences of the residue in each seq
        inB = seqB.count(res)
        if inA < inB: #res more common in B than A it is added to total and common calc for A
            common += inA
            total += inB
        else: #opposite
            common += inB
            total += inA
    similarity = (common / total)*100 # therefore it returns the # in common vs max possible in common, example:
    # AND vs ANA
    # A - 1 in common out of 2 max
    # N - 1 in common out of 1
    # D - 0 in common out of 1
    # (1 + 1 + 0) / (2 + 1 + 1) = 2/4 = similarity of 0.5 or 50%
    #print("SIMILARITY IS:", similarity)
    return similarity

def cluster_by_partitioning(active_sites):
    """
    Cluster a given set of ActiveSite instances using a partitioning method.

    Input: a list of ActiveSite instances
    Output: a clustering of ActiveSite instances
            (this is really a list of clusters, each of which is list of
            ActiveSite instances)
    My k-means clusering algorithm was adapted from: http://benalexkeen.com/k-means-clustering-in-python/
    """
    # Fill in your code here!
    df = 100 - pd.DataFrame(
            np.asarray([ #compile the similarity matrix by iterating all of the
                np.asarray([  #protein structures over each other
                    compute_similarity(i,j) for i in active_sites])
                         for j in active_sites]))

    k = 3 #arbitrary beginning value
    centroids = {
        i+1: [i*25] #select a value from dataframe for centroid
        for i in range(k)
    } #select three of the points to be the arbitrary centroids to start algorithm
    #originally this was going to be [np.random.randint(0, 100)] and i was happy to learn that
    #it correctly clustered the points together each time, however the centroid they were
    #assigned to was not always consistent due to the random draw so the assert statement
    #was difficult to right. as a solution I picked values 25, 50, 75

    ##### BEGIN ASSIGNMENT TO CENTROID #####
    def assignment(df, centroids): #we will assign each value in df to a centroid
        for i in centroids.keys():
        #euclidian distance calculation
            for j in range(len(df.index)):
                df['distance_from_{}'.format(i)] = ( #distance of each point from the centroid
                    np.sqrt((centroids[i][0] - df[j] ) ** 2)
        )
        centroid_distance_cols = ['distance_from_{}'.format(i) for i in centroids.keys()]
        df['closest'] = df.loc[:, centroid_distance_cols].idxmin(axis=1)
        df['closest'] = df['closest'].map(lambda x: int(x.lstrip('distance_from_')))
        return df

    df = assignment(df, centroids) #initialize the new dataframe with each sorted to a centroid

    def update(k): #change the value of the centroids to be more representative of the cluster
        for i in centroids.keys():
            for j in range(len(df.index)):
                centroids[i][0] = np.mean(df[df['closest'] == i][j])
        return k #k means takes the average x and y and moves the centroid closer

    centroids = update(centroids) #update the previous arbitrary centroids to be more accurate
    ##### REPEAT ASSIGNMENT TO CENTROID #####

    df = assignment(df, centroids)

    ##### REPEAT UNTIL THERE ARE NO MORE CHANGES TO THE CLUSTERS #####
    while True:
        closest_centroids = df['closest'].copy(deep=True)
        centroids = update(centroids) #previously defined update function realigns centroids
        df = assignment(df, centroids) #new dataframe is created with new centroids
        #print(df.head())
        if closest_centroids.equals(df['closest']):
            break #end the while loop if there are no newly assigned points

    #example plot for what would be shown in 2d
    """
    reducer = umap.UMAP() #UMAP to make it easy to visualize
    embedding = reducer.fit_transform(df)

    plt.scatter(embedding[:, 0], embedding[:, 1], c=df['closest']) #make the plot
    plt.title('UMAP projection of Amino Acid Similarity', fontsize=24)
    #for i in centroids.keys():
     #   plt.scatter(*centroids[i], *centroids[i])
    plt.show()
    """
    clusterList = list(range(0, df.shape[0])) #initialize output

    for i in range(len(df.index)):
        for j in range(k):
            clusterList[j] = list(df.index[df.closest == j+1])
    a = np.array(clusterList)
    print(a.shape)

    #save the row # (residue) to the correct column in the output corresponding to its centroid
    #this iteration is similar to the one from the initialization of the centroid
    #print(clusterList)
    return clusterList #output is a list with proteins assigned to clusters

def cluster_hierarchically(active_sites):
    """
    Cluster the given set of ActiveSite instances using a hierarchical algorithm.                                                                  #

    Input: a list of ActiveSite instances
    Output: a list of clusterings
            (each clustering is a list of lists of Sequence objects)
    """
    # Fill in your code here!
    def flatten(flatList): #this is a solution in order to flatten a nested list
        if isinstance(flatList, list): #its used in the clustering loop
            keepGoing = True #go more for list
            while keepGoing:
                lll = []
                keepGoing = False #finite
                for x in flatList:
                    if isinstance(x, list):
                        lll += x
                        keepGoing = True
                    else:
                        lll.append(x)
                flatList = lll
        return flatList #returns a flat list so i can swap the clusters later on

    df = 100 - np.asarray([ #compile the similarity matrix by iterating all of the
                        np.asarray([  #protein structures over each other
                            compute_similarity(i,j) for i in active_sites])
                                for j in active_sites]) #same as before

    inCluster = np.array(list(range(0, df.shape[0]))) #which cluster will the point be in

    clusterList = list(range(0, df.shape[0])) #all clusters, will be out output

    for i in range(0, df.shape[0]): #format the df to be useable in future
        df[i][i] = np.inf

    while (len(clusterList)) > 2: #run while there are still points to be assigned
        near = np.argmin(df)

        nearI, nearJ = int(near / df.shape[0]), near % df.shape[0]
        if inCluster[nearI] != inCluster[nearJ]:  #this will combine clusters
            old = clusterList[inCluster[nearJ]] #here we join them if this is triggered
            oldNearJ = inCluster[nearJ] #hold this but itll be overwritten
            clusterList[inCluster[nearI]] = [
                clusterList[inCluster[nearJ]], clusterList[inCluster[nearI]]]

            inCluster[flatten(clusterList[inCluster[nearJ]])] = inCluster[nearI] #set the new location of a member

            clusterList.remove(old) #remove old value

            for i in range(0, len(inCluster)):
                if inCluster[i] > oldNearJ:
                    inCluster[i] -= 1   #work out way down clusters when one is created
        df[nearI][nearJ] = np.inf #format similar to before this began
        df[nearJ][nearI] = np.inf
    #print(clusterList)
    return clusterList #finish and return

"""
    reducer = umap.UMAP()
    embedding = reducer.fit_transform(df)

    plt.scatter(embedding[:, 0], embedding[:, 1])
    plt.title('UMAP projection of Amino Acid Similarity', fontsize=24)
    #for i in centroids.keys():
     #   plt.scatter(*centroids[i], *centroids[i])
    plt.show()
"""

def comparison(clusterList, active_sites):
    """
    I am going to use a comparison method that averages the intracluster similarities
    and the intercluster dissimilarities to make a score that will be returned and compared
    for both of the algorithms

    #silhouetter score!
    """
    index = {active_sites[i]: i for i in range(0,len(active_sites))} #for any amount of active sites used

    df = 100 - np.asarray([ #compile the similarity matrix by iterating all of the
                        np.asarray([  #protein structures over each other
                            compute_similarity(i,j) for i in active_sites])
                                for j in active_sites])

    n = len(clusterList) #input
    intraCluster = [(np.sum([[df[index[u]][index[v]] for u in x] for v in x
                         ])) / (len(x) ** 2) for x in clusterList] #intracluster similarity computation

    score = 0 #initialization
    for i in range(0, len(clusterList)): #for all submitted clusters
        for j in range(i, len(clusterList)): #nested ticker
            score += np.sqrt(intraCluster[i] * intraCluster[j]) * ( #* intercluster dissimilarity
                1.0 - np.sum([[df[index[u]][index[v]] for u in clusterList[i]] for v in clusterList[j]]
                                ) / (len(clusterList[i]) + len(clusterList[j]))) #add the calculated score and normalize

    score = ((1 / (n * (n-1))) * score) / 1000 #compute the comparison value normalized to the amount of clusters

    return score


"""
给定一个目录信息列表，包括目录路径，以及该目录中的所有包含内容的文件，您需要找到文件系统中的所有重复文件组的路径。一组重复的文件至少包括二个具有完全相同内容的文件。

输入列表中的单个目录信息字符串的格式如下：

"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"

这意味着有 n 个文件（f1.txt, f2.txt ... fn.txt 的内容分别是 f1_content, f2_content ... fn_content）在目录 root/d1/d2/.../dm 下。注意：n>=1 且 m>=0。如果 m=0，则表示该目录是根目录。

该输出是重复文件路径组的列表。对于每个组，它包含具有相同内容的文件的所有文件路径。文件路径是具有下列格式的字符串：
"directory_path/file_name.txt"

示例 1：
输入：
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
输出：
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
"""

import re
# 本题用dict做，思路很明确：用文件内容作key域，value域需要拼接，是文件全名。
class Solution(object):
    def findDuplicate(self, paths):
        """
        :type paths: List[str]
        :rtype: List[List[str]]
        """
        mydict = {}
        for item in paths:
            temp = item.split()
            # 获取当前路径
            path = temp[0]
            # 获取当前路径下的所有文件
            files = temp[1:]
            # 遍历每一个文件，取文件内容作为key，往mydict里存
            for file in files:
                # 用正则表达式获取文件内容
                fileContextList = re.findall(r'[(](.*?)[)]', file)
                fileContextStr = ''.join(fileContextList)
                extraSize = len(fileContextStr) + 2
                fileName = list(file)[:-extraSize]
                if fileContextStr in mydict.keys():
                    mydict[fileContextStr].append(path + "/" + ''.join(fileName))
                else:
                    mydict[fileContextStr] = [path + "/" + ''.join(fileName)]
        res = []
        for item in mydict.values():
            if len(item) > 1:
                res.append(item)
        return res

if __name__ == '__main__':
    s = Solution()
    print(s.findDuplicate(
        ["root/a 1.txt(abcd) 2.txt(efsfgh)", "root/c 3.txt(abdfcd)", "root/c/d 4.txt(efggdfh)"]))

哈希表/609. 在系统中查找重复文件.py

609. 在系统中查找重复文件.py


# Write a program that uses nested loops to draw this pattern:
#
# * * * * * * *
# * * * * * *
# * * * * *
# * * * *
# * * *
# * *
# *

# Initialize Base Size
BASE_SIZE = 8

# Repetition Structure
for row in range(BASE_SIZE, 1, -1):
    for column in range(row - 1):
        print('* ', end='')
    print()

Chapter 4 - Programming Exercises/CH:4 - * Nested Loop.py


from tkinter import *

# 2. Розробити екранний додаток, який зчитає з файла речення, у якому
# слова розділені пробілами, і виведе ці слова, відсортувавши за алфавітом, у
# поле Text у стовпчик. Вікно програми повинно містити віджети: кнопка, фрейм,
# поле Text.

def doSort():
    with open("file.txt", "r", encoding="utf-8") as f:
        data = f.read().split()

    data.sort()
    T.delete(1.0, END)
    T.insert(1.0, "\n".join(data) )

win = Tk()
win.geometry("300x300")
win.configure(bg='blue')

with open("file.txt", "r", encoding="utf-8") as f:
    data = f.read().split()

f = Frame(win, background="red")
T = Text(f, border=5, height=10, width=20)

T.insert(1.0, "\n".join(data))
but = Button(text="Відсортувати", bg="red", foreground="white", border=5, command=doSort)

f.pack(expand=True)
T.pack()
but.pack(side="bottom")

win.mainloop()


#!/usr/bin/env python3 -tt
import sys
import os
from subprocess import Popen, PIPE
from time import sleep
import argparse

# Bin Paths
ssh_bin = "/usr/bin/ssh"
svnsync_bin = "/usr/local/bin/svnsync"
svnadmin_bin = "/usr/local/bin/svnadmin"
svnlook_bin = "/usr/local/bin/svnlook"

# SVN HTTP client (this MUST be set to 'serf' if using svn 1.8 or later)
# However, the 'svnsync sync' command seems to have issues communicating with SCM Manager
# using the 'serf' client.
svn_http_client = 'neon'

# Global Vars
server_name = "server.domain.tld"
user_name = "username"
pass_word = r"password"
ssh_user_name = "ssh_username"
local_repo_directory = "/repositories/svn/"
__verbose = False

# Functions
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Display more verbose output')
    parser.set_defaults(verbose=False)
    args = parser.parse_args()
    # Set verbose flag
    global __verbose
    __verbose = args.verbose

def check_paths():
    if not os.path.exists(ssh_bin):
        return "Can't find SSH client at {}".format(ssh_bin)
    if not os.path.exists(svnsync_bin):
        return "Can't find svnsync binary at {}".format(svnsync_bin)
    if not os.path.exists(svnadmin_bin):
        return "Can't find svnadmin binary at {}".format(svnadmin_bin)
    if not os.path.exists(svnlook_bin):
        return "Can't find svnlook binary at {}".format(svnlook_bin)
    if not os.path.isdir(local_repo_directory):
        return "Can't find local repository directory at {}".format(local_repo_directory)
    return None

def process_output(file):
    output = file.read().split()
    for x in range(0, len(output)):
        output[x] = output[x].decode()
    return output

def append_to_list(original_list, list_to_append):
    for i in list_to_append:
        original_list.append(i)

def get_remote_dir_names():
    p = Popen([ssh_bin,
               '{}@{}'.format(
                   ssh_user_name,
                   server_name
               ),
               'find',
               '/var/lib/scm/repositories/svn/',
               '-maxdepth', '1',
               '-type', 'd',
               '-exec', 'basename {} \;'], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Failed to list remote directories with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return None
    elif __verbose:
        print(' '.join(output))
    return output

def sync_repo(path):
    # Sync existing mirror with new changes
    p = Popen([svnsync_bin,
               'sync',
               '--username',
               user_name,
               '--password',
               pass_word,
               '--config-option=servers:global:http-library={}'.format(svn_http_client),
               'file://{}'.format(path)], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnsync sync failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(' '.join(output))
    return True

def create_sync_repo(path, url):
    # Create new repository
    p = Popen([svnadmin_bin,
               'create',
               path], stdout=PIPE, stderr=PIPE)
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnadmin create failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    if __verbose:
        print(' '.join(output))
    revprop_path = None
    # Prep for svnsync
    try:
        # Make sure pre-revprop-change has content
        revprop_path = "{}/hooks/pre-revprop-change".format(path)
        with open(revprop_path, 'w') as out:
            out.write("#!/bin/sh")
        p = Popen(['chmod',
                   '755',
                   revprop_path], stdout=PIPE, stderr=PIPE)
        # Safely process the output
        output = []
        error = []
        while p.poll() is None:
            sleep(1)
            append_to_list(output, process_output(p.stdout))
            append_to_list(error, process_output(p.stderr))
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
        if not p.returncode == 0:
            print("chmod 755 failed with return code {}".format(p.returncode), file=sys.stderr)
            print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
            print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
            return False
        elif __verbose:
            print(' '.join(output))
    except IOError as err:
        print("An error occurred while writing to {} with error code {}: {}".format(
            revprop_path, err.errno, err.output), file=sys.stderr)
        return False
    # Now we're ready for svnsync init
    p = Popen([svnsync_bin,
               'init',
               '--username',
               user_name,
               '--password',
               pass_word,
               'file://{}'.format(path),
               url], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not (p.returncode == 0 or p.returncode == 1):
        print("Svnsync init failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(p.stdout.readlines())
    p = Popen([svnlook_bin,
               'pg',
               '--revprop',
               '-r0',
               path,
               r'svn:sync-from-uuid'], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnlook pg failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    # Don't worry about displaying the output of svnlook pg, only contains a uuid if it succeeds
    # Speaking of which, set uuid to the output
    uuid = ''.join(output)
    p = Popen([svnadmin_bin,
               'setuuid',
               path,
               uuid], stdout=PIPE, stderr=PIPE)
    # Safely process the output
    output = []
    error = []
    while p.poll() is None:
        sleep(1)
        append_to_list(output, process_output(p.stdout))
        append_to_list(error, process_output(p.stderr))
    append_to_list(output, process_output(p.stdout))
    append_to_list(error, process_output(p.stderr))
    if not p.returncode == 0:
        print("Svnadmin setuuid failed with return code {}".format(p.returncode), file=sys.stderr)
        print("Process Output: {}".format(' '.join(output)), file=sys.stderr)
        print("Error Output: {}".format(' '.join(error)), file=sys.stderr)
        return False
    elif __verbose:
        print(' '.join(output))
    return True

# Main Function
def main():
    print("Enumerating directories from {}".format(server_name))
    names = get_remote_dir_names()
    if names is None:
        sys.exit(-2)
    names = [n for n in names if not n.startswith('svn')]
    print("Directory listing from {} succeeded!".format(server_name))
    for n in names:
        print()
        n_path = local_repo_directory + n + '/'
        url = "https://{}/scm/svn/{}".format(
            server_name,
            n
        )
        if os.path.isdir(n_path):
            print("Synchronizing {}...".format(n))
            print("Remote URL is {}. Starting svnsync sync...".format(url))
            ret = sync_repo(n_path)
            if not ret:
                print("Project {} failed to sync.".format(n))
                continue
        else:
            print("First time synchronization on new project {}".format(n))
            print(("Remote URL is {}. Initializing mirror repository if necessary"
                   " and performing initial sync...".format(url)))
            ret = create_sync_repo(n_path, url)
            if not ret:
                print("Project {} failed to sync.".format(n), file=sys.stderr)
                continue
            ret = sync_repo(n_path)
            if not ret:
                print("Project {} failed to sync.".format(n), file=sys.stderr)
                continue

# Entry Point
if __name__ == '__main__':
    check = check_paths()
    parse_args()
    if check is None:
        main()
    else:
        print(check)
        sys.exit(-1)


import json
import requests
import folium
import pandas as pd
import numpy as np
from lxml import etree
from pandas.io.json import json_normalize
import matplotlib.cm as cm
import matplotlib.colors as colors
from geopy.geocoders import Nominatim

wikiData = requests.get('https://en.wikipedia.org/wiki/Boroughs_of_New_York_City')
html = etree.HTML(wikiData.text)
boroughTable = html.xpath('//table[@class="wikitable sortable"]/tbody/tr')

geolocator = Nominatim(user_agent="ny_explorer")

newyork_df_columns = ['Borough','Latitude','Longitude', 'Population', 'Density(Persons/Sq. Km)']
newyork_borough_df = pd.DataFrame(columns = newyork_df_columns)

for row in boroughTable[3:8]:
    rowElements = row.xpath('td')
    name = rowElements[0].xpath('div/b/a')[0].text.strip()

    population_str = rowElements[2].text.strip().split(',')
    population = int("".join(population_str))

    density_str = rowElements[8].text.strip().split(',')
    density = int("".join(density_str))

    location = geolocator.geocode(name)
    latitude = location.latitude
    longitude = location.longitude
    newyork_borough_df = newyork_borough_df.append({'Borough' : name,
                                                    'Latitude' : latitude,
                                                    'Longitude' : longitude,
                                                    'Population' : population,
                                                    'Density(Persons/Sq. Km)' : density}, ignore_index=True)

newyork_borough_df = newyork_borough_df.sort_values(by = 'Density(Persons/Sq. Km)',ascending = False)
newyork_borough_df = newyork_borough_df.reset_index(drop=True)

location = geolocator.geocode('New York City')
latitude = location.latitude
longitude = location.longitude
print('The geograpical coordinate of New York City are {}, {}.'.format(latitude, longitude))

# create map of New York using latitude and longitude values
map_newyork = folium.Map(location=[latitude, longitude], zoom_start=10)

# add markers to map
for lat, lng, borough in zip(newyork_borough_df['Latitude'], newyork_borough_df['Longitude'], newyork_borough_df['Borough']):
    label = '{}'.format(borough)
    label = folium.Popup(label, parse_html=True)
    folium.CircleMarker(
        [lat, lng],
        radius=5,
        popup=label,
        color='blue',
        fill=True,
        fill_color='#3186cc',
        fill_opacity=0.7,
        parse_html=False).add_to(map_newyork)

map_newyork.save('map.html')