from model import *
from losses import *
from dataset import *
import tqdm

DATASET_DIR          = './Dataset/columbia'
PROCESSED_DATA_DIR   ='./Data'
IMG_NAME_TYPE        = '{}_2m_{}P_0V_0H.jpg'
IMG_DIM              = (128,128)
BATCH_SIZE           = 32
process_dir          = False

data_generator = generate_dataset(DATASET_DIR,PROCESSED_DATA_DIR,IMG_NAME_TYPE,IMG_DIM,BATCH_SIZE,process_dir)
G       = build_generator()
D1      = build_discriminator(1)
D2      = build_discriminator(2)

loss = LossFns()

EPOCHS   = 50
STEPS    = 100
LR       = 2e-4

G_optimizer = tf.keras.optimizers.Adam(LR)
D1_optimizer = tf.keras.optimizers.Adam(LR)
D2_optimizer = tf.keras.optimizers.Adam(LR)

outer_bar = tqdm.tqdm(total=EPOCHS, desc='Epochs', position=1)
for epoch in range(1,EPOCHS+1):

    inner_bar = tqdm.tqdm(total=STEPS, desc='Steps', position=0)
    for step in range(STEPS):
        I_a,I_b,P_a,P_b =  next(data_generator)
        with tf.GradientTape(persistent=True) as d_tape:
            I_b_fake = G([I_a,P_a,P_b])

            D1_real  = D1([I_b,I_a])
            D1_fake  = D1([I_b_fake,I_a])

            D2_real  = D2([I_b,P_b])
            D2_fake  = D2([I_b_fake,P_b])

            G_loss   = loss.generator_loss(I_b,I_b_fake,D1_fake,D2_fake)
            D1_loss  = loss.discriminator_loss(D1_real,D1_fake)
            D2_loss  = loss.discriminator_loss(D2_real,D2_fake)

        D1_grad = d_tape.gradient(D1_loss,D1.trainable_variables)
        D2_grad = d_tape.gradient(D2_loss,D2.trainable_variables)

        D1_optimizer.apply_gradients(zip(D1_grad,D1.trainable_variables))
        D2_optimizer.apply_gradients(zip(D2_grad,D2.trainable_variables))

        with tf.GradientTape() as g_tape:
            I_b_fake = G([I_a,P_a,P_b])
            D1_fake  = D1([I_b_fake,I_a])
            D2_fake  = D2([I_b_fake,P_b])

            G_loss   = loss.generator_loss(I_b,I_b_fake,D1_fake,D2_fake)

        G_grad = g_tape.gradient(G_loss,G.trainable_variables)

        G_optimizer.apply_gradients(zip(G_grad,G.trainable_variables))

        inner_bar.update(1)

    outer_bar.update(1)


from flask_sqlalchemy import SQLAlchemy
from trafficlights import db

from datetime import datetime

class User(db.Model):
    """
    User account details.
    """

    __tablename__ = 'users'
    id            = db.Column(db.Integer, primary_key=True)
    username      = db.Column(db.String(48))
    password_hash = db.Column(db.String(64))

    def __init__(self, username, password_hash):
        self.username      = username
        self.password_hash = password_hash

    def __repr__(self):
        return '<User %s>' % self.username

class Node(db.Model):
    __tablename__ = 'nodes'
    id      = db.Column(db.Integer, primary_key=True)
    checks  = db.relationship("Check", back_populates="node")
    results = db.relationship("Result", back_populates="node")
    name    = db.Column(db.String(80))
    token   = db.Column(db.String(80))

    def __init__(self, name):
        self.name  = name
        self.token = '1234567890'

class Service(db.Model):
    """
    Logical grouping of checks.
    """

    __tablename__ = 'services'
    id     = db.Column(db.Integer, primary_key=True)
    name   = db.Column(db.String(80))
    checks = db.relationship("Check", back_populates="service")

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Service "%s">' % self.name

class Check(db.Model):
    """
    Healthcheck configurations.
    """

    __tablename__ = 'checks'
    id         = db.Column(db.Integer, primary_key=True)
    service_id = db.Column(db.Integer, db.ForeignKey('services.id'))
    node_id    = db.Column(db.Integer, db.ForeignKey('nodes.id'))
    service    = db.relationship("Service", back_populates="checks")
    node       = db.relationship("Node", back_populates="checks")
    results    = db.relationship("Result", back_populates="check")
    name = db.Column(db.String(80))
    url  = db.Column(db.String(80))

    def __init__(self, name, url, service, node):
        self.name       = name
        self.url        = url
        self.service_id = service
        self.node_id    = node

    def __repr__(self):
        return '<Check "%s - %s">' % (self.service.name, self.name)

class Result(db.Model):
    """
    Results from Healthchecks.
    """
    # So I'm really not sure how best to manage the result objects. For now
    # it seems best to just create them using the entire dictionary, populating
    # the relevant fields...
    __tablename__ = 'results'
    id       = db.Column(db.Integer, primary_key=True)
    check_id = db.Column(db.Integer, db.ForeignKey('checks.id'))
    check    = db.relationship("Check", back_populates="results")
    node_id  = db.Column(db.Integer, db.ForeignKey('nodes.id'))
    node     = db.relationship("Node", back_populates="results")

    code      = db.Column(db.Integer)
    status    = db.Column(db.Text)
    time      = db.Column(db.Integer)
    headers   = db.Column(db.Text)
    content   = db.Column(db.Text)
    timestamp = db.Column(db.DateTime)
    success   = db.Column(db.Boolean)
    url       = db.Column(db.Text)

    def __init__(self, node, data):
        self.node_id  = node
        self.check_id = data['id']

        self.code      = data['metadata']['code']
        self.status    = data['metadata']['status']
        self.time      = data['metadata']['time']
        self.headers   = str(data['metadata']['headers'])
        self.content   = data['metadata']['content']
        self.timestamp = datetime.strptime(data['metadata']['timestamp'], '%Y-%m-%d %H:%M:%S')
        self.success   = data['success']
        self.url       = data['url']

    def __repr__(self):
        return '<Result %i>' % id
#
# Object for storing healthchecks. Requires a name, and a URL to check.
# class Check(db.Model):
#
#
#     id   = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(120))
#     url   = db.Column(db.String(120))
#
#     service_id = db.Column(db.Integer, db.ForeignKey('services.id'))
#
#     def __init__(self,name,url,service):
#         self.name = name
#         self.url  = url
#         self.service_id = service
#
#
#
#     __tablename__ = 'results'
#
#     id        = db.Column(db.Integer, primary_key=True)
#     check_id  = db.Column(db.Integer, db.ForeignKey('checks.id')) # ID of appropriate check.
#     timestamp = db.Column(db.DateTime)
#     code      = db.Column(db.Text)
#     status    = db.Column(db.Text)
#     time      = db.Column(db.Text)
#     content   = db.Column(db.Text)
#     headers   = db.Column(db.Text)
#
#     def __init__(self,result):
#         self.check_id  = result['id']
#         self.timestamp = result['timestamp']
#         self.code      = result['metadata']['code']
#         self.status    = result['metadata']['status']
#         self.time      = result['metadata']['time']
#         self.content   = result['content']
#         self.headers   = result['headers']
#
#     def __repr__(self):
#         return '<Result %i>' % self.id


class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        # define dictionaries
        winners = set()
        losers = set()
        winners_with_loss_count = dict()
        players_loss_count = dict()

        # answers
        winners_with_no_loss = []
        players_one_loss = []

        # iterate through the matches to identify
        for match in matches:
            # add winners to set
            winners.add(match[0])
            winners_with_loss_count[match[0]] = 0
            # add losers to set
            losers.add(match[1])
            players_loss_count[match[0]] = 0
            players_loss_count[match[1]] = 0

        # iterate through matches to get winners' & losers' loss count
        for match in matches:
            # if current match loser is in winner's set
            if match[1] in winners:
                winners_with_loss_count[match[1]] += 1

            players_loss_count[match[1]] += 1

        # iterate through winners' loss
        for key in winners_with_loss_count:
            if winners_with_loss_count[key] == 0:
                winners_with_no_loss.append(key)

        # iterate through all players' loss
        for key in players_loss_count:
            if players_loss_count[key] == 1:
                players_one_loss.append(key)

        winners_with_no_loss.sort()
        players_one_loss.sort()

        return [winners_with_no_loss, players_one_loss]


#-*- coding:utf-8 -*-
# @Author: Meiyan Jin
# @Time: 2020/4/25 0:29
# @File: 生成器.py
# @Software: PyCharm
import time,sys
a =[i*2 for i in range(100000)]
print(a)
b=( i*2 for i in range(10000))

def timera():
    start=time.time()
    a=[i*2 for i in range(10000000)]
    stop=time.time()
    print("a耗时：",(stop-start))
    print(sys.getsizeof(a))

def timerb():
    start = time.time()
    b = (i * 2 for i in range(10000000))
    stop = time.time()
    print("b耗时：", (stop - start))
    print(sys.getsizeof(b))

timera()
timerb()


#!/usr/bin/env python3
from jinja2 import Environment, FileSystemLoader
import pynetbox
import ipaddress
import argparse
import os

def check_env():
    if os.getenv("NB_URL") and os.getenv("NB_API_KEY"):
        pass
    else:
        print(f"NB_API_KEY and/or NB_URL not loaded in environment")
        quit()

def init_nb():
    nb = pynetbox.api(url=os.getenv("NB_URL"), token=os.getenv("NB_API_KEY"))
    return nb

def args_parse():
    parser = argparse.ArgumentParser(description="Set Hostname")
    parser.add_argument("--host", help="Set Hostname", required=True)
    parser.add_argument("--protocol", choices=["ospf", "bgp"])
    args = parser.parse_args()
    return args

def pull_device(nb, host):
    host_dict = {}
    host_api = nb.dcim.devices.get(name=host)
    lo0 = host_api.primary_ip.display
    host_dict["hostname"] = host
    host_dict["loopback"] = lo0[:-3]
    host_dict["type"] = host_api.device_type.slug
    return host_dict

def pull_interfaces(nb, data):
    intf_data = {}
    device_intf = nb.dcim.interfaces.filter(device=data["hostname"], connected="true")
    for interface in device_intf:
        intf_data[interface.display] = {}
        intf_data[interface.display][
            "z_peer"
        ] = interface.connected_endpoint.device.display
        intf_data[interface.display]["z_intf"] = interface.connected_endpoint.display
        intf_ip = nb.ipam.ip_addresses.get(
            device=data["hostname"], interface_id=interface.id, family=4
        )
        ip_intf = ipaddress.ip_interface(intf_ip.display)
        intf_data[interface.display]["ip_v4"] = ip_intf.ip.exploded
        intf_data[interface.display]["netmask"] = ip_intf.netmask.exploded
    return intf_data

def load_template(host_data, intf_data):
    file_loader = FileSystemLoader("templates")
    env = Environment(loader=file_loader)
    template = env.get_template("cisco_template")
    output = template.render(
        intf=intf_data,
        host=host_data,
    )
    print(output)

def load_protocol_template(host_data, intf_data):
    file_loader = FileSystemLoader("templates")
    env = Environment(loader=file_loader)
    template = env.get_template("cisco_ospf_template.j2")
    output = template.render(
        intf=intf_data,
        host=host_data,
    )
    print(output)

if __name__ == "__main__":
    check_env()
    args = args_parse()
    nb = init_nb()
    host_data = pull_device(nb, args.host)
    intf_data = pull_interfaces(nb, host_data)
    if not args.protocol:
        load_template(host_data, intf_data)
    if args.protocol:
        load_protocol_template(host_data, intf_data)


from time import time

start = time()
INPUT = open("./input_files/input_25", "r").read().strip("\n")

TRANSLATE = {
    '2': 2,
    '1': 1,
    '0': 0,
    '-': -1,
    '=': -2,
    2: '2',
    1: '1',
    0: '0',
    -1: '-',
    -2: '='
}

def from_snafu(value):
    result = 0
    for index, digit in enumerate(value[::-1]):
        base = 5 ** index
        result += base * TRANSLATE[digit]
    return result

def to_snafu(value):
    result = []
    power = 0
    while value != value % (5 ** power):
        power += 1

    power -= 1
    while power >= 0:
        base = 5 ** power
        change = value // base

        result.append(change)
        value -= change * base
        power -= 1

    for i, _ in enumerate(result[::-1], start=1):
        v = result[0 - i]
        if v < -2:
            result[0 - i] += 5
            result[0 - i - 1] -= 1
        if v > 2:
            result[0 - i] -= 5
            result[0 - i - 1] += 1
    return ''.join(TRANSLATE[x] for x in result)

BURNERS = [from_snafu(x) for x in INPUT.split('\n')]

def run_1():
    return to_snafu(sum(BURNERS))

def run_2():
    return "* * * Merry Christmas * * *"

print('\n', 'Part 1 ==> ', run_1())
print(f'in {int((time() - start) * 1000)} ms')
print('\n', 'Part 2 ==> ', run_2())
print(f'in {int((time() - start) * 1000)} ms')


#importing libraries
import cv2
import numpy as np
import os
import mediapipe as mp
import glob
from multiprocessing import Pool, Process,  cpu_count

#folders to store features
num_class = 10

if not os.path.isdir('./class_{}_features'.format(num_class)):
  os.mkdir('./class_{}_features'.format(num_class))

#create train, test and val folder
if not os.path.isdir('./class_{}_features/train'.format(num_class)):
  os.mkdir('./class_{}_features/train'.format(num_class))

if not os.path.isdir('./class_{}_features/test'.format(num_class)):
  os.mkdir('./class_{}_features/test'.format(num_class))

if not os.path.isdir('./class_{}_features/val'.format(num_class)):
  os.mkdir('./class_{}_features/val'.format(num_class))

#mediapipe helper function to read each frame and provide features
def mediapipe_detection(image, model):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # COLOR CONVERSION BGR 2 RGB
    image.flags.writeable = False                  # Image is no longer writeable
    results = model.process(image)                 # Make prediction
    image.flags.writeable = True                   # Image is now writeable
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # COLOR COVERSION RGB 2 BGR
    return image, results

#Extracting features for each keypoints
def extract_keypoints(results):
    pose = np.array([[res.x, res.y, res.z] for res in results.pose_landmarks.landmark]) if results.pose_landmarks else np.zeros((33,3))
    face = np.array([[res.x, res.y, res.z] for res in results.face_landmarks.landmark]) if results.face_landmarks else np.zeros((468,3))
    lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]) if results.left_hand_landmarks else np.zeros((21,3))
    rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]) if results.right_hand_landmarks else np.zeros((21,3))

    #padding zeros at end of array to make dimenion eqal to convert into square matrix
    feature_array = np.concatenate([pose, face, lh, rh, np.zeros((33,3))])
    feature_array = feature_array.reshape((24,24,3)) #reshapping into square matrix

    return feature_array

#helper function to read videos files and extract features
def extract_feature(files):
    #iterating over file list
    for file in files:
        #getting file name
        files_path = file.split('/')
        split = files_path[2]
        file_name = files_path[3].split('.')[0]

        cap = cv2.VideoCapture(file) #reading video file

        mp_holistic = mp.solutions.holistic # Using holistic model of mediapip framework

        results_array = [] #array to store individual frame features

        #reading frames from video and extracting features
        with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
            while(cap.isOpened()):
                ret, frame = cap.read()
                if ret:
                    image, results = mediapipe_detection(frame, holistic)
                    keypoints = extract_keypoints(results)
                    results_array.append(keypoints)

                else:
                    break

            #closing video reader
            cap.release()
            cv2.destroyAllWindows()

        results_array = np.array(results_array) #converting result to array

        # array_len = len(results_array)
        avg_frame_count = 100 #setting number of frames to be trained model with

        #scalling video to threshold frames length
        return_ind = [int(i) for i in np.linspace(1, len(results_array), num=avg_frame_count)]
        results_array = [ results_array[i-1] for i in return_ind]

        #saving the extracted features
        np.save('./class_{}_features/{}/{}.npy'.format(num_class,split,file_name),results_array)

#function to reading file using parallel processing
def parallel_process():
    cpu_cores = cpu_count() #getting number of cpu cores available

    #checking if features are already existed for current video
    existing_features = glob.glob('./class_{}_features/*/*'.format(num_class,))
    for i in range(len(existing_features)):
        existing_features[i] = existing_features[i].replace('.npy','.mp4')
        existing_features[i] = existing_features[i].replace('_features','_dataset')
    current_list = glob.glob('./class_{}_dataset/*/*.mp4'.format(num_class))
    file_list = list(set(current_list) - set(existing_features))

    # Split the input file list into sublists according to the number
    # of the available CPU cores
    for i in range(0, cpu_cores):
        sub_list = [file_list[j] for j in range(0, len(file_list)) if j % cpu_cores == i]

        if len(sub_list) > 0:
            p = Process(target=extract_feature, args=([sub_list]))
            p.start()

if __name__ == "__main__":
    parallel_process()#creating parallel process to reduce computation time


import re

def n_gram(uni, n):
	for k in range(len(uni)-n+1):
		print(uni[k:k+n])

str = "I am an NLPer"

words = re.split('\W+', str)
chara = [k for k in str if k != " "]

n_gram(words, 2)
n_gram(chara, 2)


from setuptools import setup, find_packages
from distutils.extension import Extension
#from Cython.Distutils import build_ext

version = '0.0'

setup(name='biostuff',
      version=version,
      description="",
      long_description="""\
""",
      ext_modules=[ Extension("biostuff/cblastline",
                      sources=["biostuff/cblastline.pyx"],)],
      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      keywords='bio',
      author='brentp',
      author_email='bpederse@gmail.com',
      url='',
      license='BSD',
      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
      include_package_data=True,
      test_suite='nose.collector',
      zip_safe=False,
      install_requires=[
          # -*- Extra requirements: -*-
      ],
      entry_points= {
          # -*- Entry points: -*-
          'console_scripts': ['biostuff = biostuff:main']
          }
      )


#frequency of element

n=int(input("Enter the number of elements in the list="))
list1=[]
list2=[]
count=1
counted=-1
print("Enter the elements:")
for i in range(0,n):
    elem=input()
    list1.append(elem)
list1.sort()
dist_elem=len(set(list1))
list(list1)
for i in range(0,n):
    for j in range(i+1,n):
        if list1[i]==list1[j] and list1[i]!=counted:
            count+=1
            list1[j]=counted
        else:
            continue
    if list1[i]!=counted:
        list2.append(count)
    count=1
print(list2)


from collections import Counter
from datetime import datetime

def calculate_age(bdate: str, current_year: int) -> int:
    day, month, year = map(int, bdate.split('.'))
    return current_year - year

def statistics(friends_data) -> tuple:
    current_year = datetime.now().year
    country_counter = Counter()
    city_counter = Counter()
    sex_counter = Counter()
    age_counter = Counter()
    unique_countries = set()
    unique_cities = set()

    for friend in friends_data:
        country = friend.get('country', None)
        city = friend.get('city', None)
        sex = friend.get('sex', None)

        if country:
            country_counter[country] += 1
            unique_countries.add(country)

        if city:
            city_counter[city] += 1
            unique_cities.add(city)

        if sex:
            sex_counter[sex] += 1

        bdate = friend.get('bdate', None)
        if bdate:
            try:
                age = calculate_age(bdate, current_year)
                if age < 14:
                    age_counter['less_14'] += 1
                elif age < 19:
                    age_counter['14_18'] += 1
                elif age < 25:
                    age_counter['18_24'] += 1
                elif age < 35:
                    age_counter['25_34'] += 1
                elif age < 45:
                    age_counter['35_44'] += 1
                else:
                    age_counter['45_54'] += 1
            except ValueError:
                pass  # Invalid date format

    def total_count(counter):
        return sum(counter.values())

    def rounded_stats(counter):
        return {key: round((value / total_count(counter)) * 100) for key, value in counter.items()}

    return (
        rounded_stats(country_counter),
        rounded_stats(city_counter),
        rounded_stats(sex_counter),
        list(unique_countries),
        list(unique_cities),
        rounded_stats(age_counter)
    )

def find_index_by_value(array_of_dicts, target_value):
    for index, dictionary in enumerate(array_of_dicts):
        for key, value in dictionary.items():
            if value == target_value:
                return index
    return -1  # Вернуть -1, если значение не найдено в массиве

def extract_additional_data(data_json) -> dict:
    additional_data = data_json
    result = {
        'username': additional_data.get('username', ''),
        'profile_link': additional_data.get('profile_link', ''),
        'icon_url': additional_data.get('icon_url', ''),
        'full_name': additional_data.get('full_name', ''),
        'follows_count': additional_data.get('follows_count', 0),
        'followers_count': additional_data.get('followers_count', 0),
        'posts_count': additional_data['data']['posts_count'],
        'views_count': additional_data['data']['views_count'],
        'likes_count': additional_data['data']['likes_count'],
        'comments_count': additional_data['data']['comments_count'],
        'taggets_count': additional_data['data']['tagget_count'],
        'highlights_count': additional_data['data']['highlights_count']
    }

    return result

def extract_posts_data(raw_posts) -> list:
    processed_posts = []

    for post in raw_posts:
        post_data = {}
        post_data['media'] = post.get('media', [])  # Иконка (вы можете взять её из другого места)
        post_data['text'] = post['text']  # Текст поста (если есть)
        post_data['views_count'] = post.get('view_count', 0)
        post_data['likes_count'] = post.get('likes_count', 0)
        post_data['comments_count'] = post.get('comments_count', 0)
        post_data['date'] = post.get('post_date', '')

        processed_posts.append({'data': post_data})

    return processed_posts

# функция подсчета лайков, просмотров, комментариев и репостов
def calculate_likes_views_comments_reposts(posts_data, username):
    likers_counter = Counter()

    if isinstance(posts_data, list):
        posts = posts_data
    elif isinstance(posts_data, dict) and 'posts' in posts_data:
        posts = posts_data['posts']
    else:
        print("Неподдерживаемый формат данных")
        return []

    all_likers = []
    for post in posts:
        likers = post.get("likes", [])
        all_likers += likers
        for liker in likers:
            likers_counter[liker["username"]] += 1

    top_10_likers = likers_counter.most_common(10)

    top_likers_list = []
    for domain, count in top_10_likers:

        if domain == username: continue

        value = find_index_by_value(all_likers, domain)

        top_likers_list.append({
            'icon_url': all_likers[value]['icon_url'],  # Здесь должна быть ссылка на аватар пользователя, если есть
            'username': all_likers[value]['username'],
            'likes_count': count
        })

    return top_likers_list

def calculate_top_commentators(posts_data, username) -> list:
    """Функция подсчета топ-10 комментаторов"""
    commentators_counter = Counter()

    # Словарь для хранения информации о комментаторах по их ID

    all_commentators = []
    for post in posts_data:
        commentators = post.get("comments", [])
        all_commentators += commentators
        for commentator in commentators:
            commentators_counter[commentator["username"]] += 1

    top_10_commentators = commentators_counter.most_common(10)

    top_commentators_list = []
    for domain, count in top_10_commentators:

        if domain == username: continue

        value = find_index_by_value(all_commentators, domain)

        top_commentators_list.append({
            'icon_url': all_commentators[value]['icon_url'],
            'username': all_commentators[value]['username'],
            'comments_count': count
        })

    return top_commentators_list


from nose.tools import assert_equal
from unittest.mock import Mock

from desktop.lib.fs.gc.gsstat import GSStat

class TestGSStat(object):
  def setUp(self):
    self.mock_gs_bucket = Mock()
    self.mock_gs_bucket.name = 'gethue_bucket'

    self.mock_gs_key = Mock()
    self.mock_gs_key.name = 'test.csv'
    self.mock_gs_key.bucket = self.mock_gs_bucket
    self.mock_gs_key.size = 123
    self.mock_gs_key.last_modified = '2023-09-21T12:03:00.000Z' # Some mock timestamp

  def test_from_bucket(self):
    gs_bucket_stat = GSStat.from_bucket(self.mock_gs_bucket)

    assert_equal(gs_bucket_stat.name, 'gethue_bucket')
    assert_equal(gs_bucket_stat.path, 'gs://gethue_bucket')
    assert_equal(gs_bucket_stat.isDir, True)
    assert_equal(gs_bucket_stat.size, 0)
    assert_equal(gs_bucket_stat.mtime, None)

  def test_from_key(self):
    gs_key_stat = GSStat.from_key(self.mock_gs_key)

    assert_equal(gs_key_stat.name, 'test.csv')
    assert_equal(gs_key_stat.path, 'gs://gethue_bucket/test.csv')
    assert_equal(gs_key_stat.isDir, False)
    assert_equal(gs_key_stat.size, 123)
    assert_equal(gs_key_stat.mtime, 1695297780)  # Replace with the expected timestamp

  def test_for_gs_root(self):
    gs_root_stat = GSStat.for_gs_root()

    assert_equal(gs_root_stat.name, 'GS')
    assert_equal(gs_root_stat.path, 'gs://')
    assert_equal(gs_root_stat.isDir, True)
    assert_equal(gs_root_stat.size, 0)
    assert_equal(gs_root_stat.mtime, None)

  def test_to_json_dict(self):
    gs_key_stat = GSStat.from_key(self.mock_gs_key)

    json_dict = gs_key_stat.to_json_dict()
    expected_dict = {
      'path': 'gs://gethue_bucket/test.csv',
      'size': 123,
      'atime': 1695297780,
      'mtime': 1695297780,
      'mode': 33206,
      'user': '',
      'group': '',
      'aclBit': False
    }

    assert_equal(json_dict, expected_dict)


from datetime import timedelta, datetime

import pandas as pd
from sqlalchemy.orm import Session

from db.base_model import sc_wrapper
from db.model import DepthModel, SymbolModel, Factor
from util.kline_util import get_kline

pd.set_option('display.max_rows', 50)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

@sc_wrapper
def tether(sc: Session = None):
    df_kline = get_kline(866, start_date='2019-04-18 16:21:03')
    objs = sc.query(Factor).filter_by(source='twitter_whale', type='transaction', tag='USDT').order_by(Factor.timestamp)
    data = [o.to_dict() for o in objs]
    df_tether = pd.DataFrame(data)
    df_tether = df_tether[(df_tether['from_addr'] == 'Tether Treasury') & (df_tether['to_addr'] != 'unknown')]
    df_tether = df_tether[['timestamp', 'usd_number']]
    df_tether['timestamp'] = pd.to_datetime(df_tether['timestamp'])
    df_tether['timestamp'] = df_tether['timestamp'].dt.ceil('T')
    df_tether.set_index('timestamp', inplace=True)
    df = pd.concat([df_kline, df_tether], axis=1)
    df['usd_number'].fillna(value=0, inplace=True)
    df = df.resample(rule='3h').agg({"Open": "first", "High": "max", "Low": "min", "Close": "last", "Volume": "sum", 'usd_number': 'sum'})
    return df

if __name__ == '__main__':
    print(tether())


import csv
from PIL import Image
from pathlib import Path
import glob, os, sys
import piexif

row = (1, 2, 3)

#initDir = './imageFolder/'
#saveTo = './data_exported.csv'

initDir = './' + sys.argv[1] + '/'
saveTo = sys.argv[2]

ogImages = glob.glob(initDir + '/*/*/*.*')
exportList = []
headers = ['filename', 'width', 'height', 'filesize']
exifNone = {'0th': {}, 'Exif': {}, 'GPS': {}, 'Interop': {}, '1st': {}, 'thumbnail': None}

i = 0
for image in ogImages:
    imagedata_o = Image.open(image)
    name_o = os.path.basename(image)
    width_o, height_o = imagedata_o.size
    file_stats_o = os.stat(image)
    size_o = file_stats_o.st_size
    quantize_o = imagedata_o.quantization
    qtable_o = quantize_o
    exif_o = piexif.load(image)

    socialNet = glob.glob(os.path.dirname(image) + '/*/*.*')
    exportList.append([name_o, width_o, height_o, size_o])

    j = 0
    for image2 in socialNet:
        imagedata_m = Image.open(image2)
        name_m = os.path.basename(image2)
        network_m = os.path.basename(os.path.dirname(image2))
        width_m, height_m = imagedata_m.size
        file_stats_m = os.stat(image2)
        size_m = file_stats_m.st_size
        quantize_m = imagedata_m.quantization
        qtable_m = quantize_m
        exif_m = piexif.load(image2)
        if name_o == name_m:
            renamed = 0
        else:
            renamed = 1
        if width_o == width_m and height_o == height_m:
            resized = 0
        else:
            resized = 1
        if quantize_o == quantize_m:
            recompressed = 0
        else:
            recompressed = 1
        if exif_o == exifNone:
            reexif = 'No EXIF'
        else:
            if exif_m == exifNone:
                reexif = 'Deleted'
            else:
                if exif_m == exif_o:
                    reexif = 'Identical'
                else:
                    reexif = 'Edited'
        if i == 0:
            headers.append(network_m + ' renamed')
            headers.append(network_m + ' resized')
            headers.append(network_m + ' recompressed')
            headers.append(network_m + ' exif')
        exportList[i].append(renamed)
        exportList[i].append(resized)
        exportList[i].append(recompressed)
        exportList[i].append(reexif)
        j = j + 1
    i = i + 1

# open the file in the write mode
f = open(saveTo, 'w')

# create the csv writer
writer = csv.writer(f)

# write a row to the csv file
writer.writerow(headers)
writer.writerows(exportList)

# close the file
f.close()


# Matheus Henrique dos Santos, https://github.com/mtheusdev  UDESC CCT CS50X - HARVARD

from cs50 import SQL
from csv import reader
from sys import argv

banco = SQL("sqlite:///students.db")

if len(argv) < 2:
    print("usage error, import.py characters.csv")
    exit()

with open(argv[1], newline='') as charactersFile:
    characters = reader(charactersFile)
    for character in characters:
        if character[0] == 'name':
            continue

        nomeInteiro = character[0].split()
        if len(nomeInteiro) < 3:
            banco.execute("INSERT INTO students(first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
                       nomeInteiro[0], None, nomeInteiro[1], character[1], character[2])

        else:
            banco.execute("INSERT INTO students(first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
                       nomeInteiro[0], nomeInteiro[1], nomeInteiro[2], character[1], character[2])


# -*- coding: utf-8 -*-
"""
Created on Fri May 22 11:49:15 2020

@author: jesse
"""

null = {
    "name": "null",
    "weight": 0,
    "cost": 0,
    "effect": ["null", 0]
}

caveOfDarkMap = {
    "name": "Cave of Dark Map",
    "weight": 1,
    "cost": 100,
    "effect": ["map", 0],
    "use": "dungeons.displayCaveOfDarkMap()"
}

sunkenCitadelMap = {
    "name": "Sunken Citadel Map",
    "weight": 1,
    "cost": 100,
    "effect": ["map", 0],
    "use": "dungeons.displaySunkenCitadelMap()"
}

smallLoafOfBread = {
    "name": "Small Loaf of Bread",
    "weight": 1,
    "cost": 3,
    "effect": ["stamina", 4],
    "use": "useFood(items.smallLoafOfBread)"
}

freshCherryPie = {
    "name": "Fresh Cherry Pie",
    "weight": 2,
    "cost": 8,
    "effect": ["stamina", 8],
    "use": "useFood(items.freshCherryPie)"
}

smallHealthPotion = {
    "name": "Small Health Potion",
    "weight": 2,
    "cost": 4,
    "effect": ["health", 2],
    "use": "usePotion(items.smallHealthPotion)"
}

mediumHealthPotion = {
    "name": "Medium Health Potion",
    "weight": 3,
    "cost": 6,
    "effect": ["health", 4],
    "use": "usePotion(items.mediumHealthPotion)"
}

largeHealthPotion = {
    "name": "Large Health Potion",
    "weight": 4,
    "cost": 10,
    "effect": ["health", 8],
    "use": "usePotion(items.largeHealthPotion)"
}

ultraHealthPotion = {
    "name": "Ultra Health Potion",
    "weight": 6,
    "effect": ["health", 12],
    "use": "usePotion(items.ultraHealthPotion)"
}

# elixir = {
#     "name": "Elixir",
#     "weight": 10,
#     "effect": ["mult", 0],
#     "effects": [freshCherryPie, largeHealthPotion]
# }

potionOfAscendance = {
    "name": "Potion Of Ascendance",
    "weight": 2,
    "cost": 999,
    "effect": ["ascend", 0],
    "use": "ascend(items.potionOfAscendance)"
}

def getItemUsage(item):
    usage = ""

    if item["effect"][0] == "health" or item["effect"][0] == "stamina":
        usage += "(restores " + str(item["effect"][1]) + " " + item["effect"][0] + ")"

    # elif item["effect"][0] == "ascend":
    # usage = "* Soul Bound *"

    return usage


import pytz
from datetime import datetime
import yaml
import logging
import re

#logging
# logging.basicConfig(filename="scraping.log",
#                     filemode='a',
#                     format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
#                     datefmt='%H:%M:%S',
#                     level=logging.INFO)

# logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
# rootLogger = logging.getLogger().setLevel(logging.INFO)

# fileHandler = logging.FileHandler("scraping.log",mode="a")
# fileHandler.setFormatter(logFormatter)
# rootLogger.addHandler(fileHandler)

# consoleHandler = logging.StreamHandler()
# consoleHandler.setFormatter(logFormatter)
# rootLogger.addHandler(consoleHandler)

logger = logging.getLogger("scraper")

def get_utc_now_date():
    return pytz.utc.localize(datetime.utcnow())

def load_config_file(file_path):
    with open(file_path, "r") as stream:
        try:
            config = yaml.safe_load(stream)
            return config
        except yaml.YAMLError as exc:
            logger.exception("yaml error")

# craper utils
def validate_article_url(url):
    valid = True
    if "http" in url:
        valid = False

    if "/video/" in url:
        valid = False

    if "/videos/" in url:
        valid = False

    if "/live-news/" in url:
        valid = False

    if "/gallery/" in url: # only has pictures
        valid = False

    return valid

def validate_list_category_urls(list_category):
    '''
    list_category=[{"category_name": "world", "category_url": "/world"}, ...]
    '''
    list_urls = []
    valid_list_category = []
    for cat in list_category:
        if cat["category_url"] not in list_urls:
            list_urls.append(cat["category_url"])
            valid_list_category.append(cat)

    return valid_list_category

def parse_datetime(to_be_parsed, webname):
    if webname == "CNN":
        # Updated 0615 GMT (1415 HKT) July 16, 2022
        t = re.compile("(\d{4})").search(to_be_parsed).span()
        str_t = to_be_parsed[t[0]:t[1]]
        str_t = str_t[:2] + ":" + str_t[2:]
        d = re.compile("([a-z]{1,10} \d{1,2}, \d{4})").search(to_be_parsed.lower()).span()
        str_d = to_be_parsed[d[0]:d[1]]

        datetime_object = datetime.strptime(f"{str_d} {str_t}", '%B %d, %Y %H:%M')
        return datetime_object


from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import dsa, rsa, padding

# Generating keys for DSA and RSA
dsa_private_key = dsa.generate_private_key(key_size=1024, backend=default_backend())
rsa_private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())

message = b"Hello, World!"

# Signing with DSA
dsa_signature1 = dsa_private_key.sign(
    message,
    hashes.SHA256()
)

dsa_signature2 = dsa_private_key.sign(
    message,
    hashes.SHA256()
)

# Signing with RSA
rsa_signature1 = rsa_private_key.sign(
    message,
    padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH),
    hashes.SHA256()
)

rsa_signature2 = rsa_private_key.sign(
    message,
    padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH),
    hashes.SHA256()
)

# Output the signatures
print("DSA Signatures:")
print("Signature 1:", dsa_signature1.hex())
print("Signature 2:", dsa_signature2.hex())
print("DSA signatures are different:", dsa_signature1 != dsa_signature2)

print("\nRSA Signatures:")
print("Signature 1:", rsa_signature1.hex())
print("Signature 2:", rsa_signature2.hex())
print("RSA signatures are same:", rsa_signature1 == rsa_signature2)


# coding: utf-8

"""
    OpenAPI Petstore
    This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\  # noqa: E501
    The version of the OpenAPI document: 1.0.0
    Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

from __future__ import annotations
from petstore_api.shared_imports.schema_imports import *  # pyright: ignore [reportWildcardImportFromLibrary]

Data: typing_extensions.TypeAlias = schemas.StrSchema
Id: typing_extensions.TypeAlias = schemas.IntSchema
Properties = typing.TypedDict(
    'Properties',
    {
        "data": typing.Type[Data],
        "id": typing.Type[Id],
    }
)

class FromSchemaDict(schemas.immutabledict[str, schemas.OUTPUT_BASE_TYPES]):

    __required_keys__: typing.FrozenSet[str] = frozenset({
    })
    __optional_keys__: typing.FrozenSet[str] = frozenset({
        "data",
        "id",
    })

    def __new__(
        cls,
        *,
        data: typing.Union[
            str,
            schemas.Unset
        ] = schemas.unset,
        id: typing.Union[
            int,
            schemas.Unset
        ] = schemas.unset,
        configuration_: typing.Optional[schema_configuration.SchemaConfiguration] = None,
        **kwargs: schemas.INPUT_TYPES_ALL,
    ):
        arg_: typing.Dict[str, typing.Any] = {}
        for key_, val in (
            ("data", data),
            ("id", id),
        ):
            if isinstance(val, schemas.Unset):
                continue
            arg_[key_] = val
        arg_.update(kwargs)
        used_arg_ = typing.cast(FromSchemaDictInput, arg_)
        return FromSchema.validate(used_arg_, configuration=configuration_)

    @staticmethod
    def from_dict_(
        arg: typing.Union[
            FromSchemaDictInput,
            FromSchemaDict
        ],
        configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
    ) -> FromSchemaDict:
        return FromSchema.validate(arg, configuration=configuration)

    @property
    def data(self) -> typing.Union[str, schemas.Unset]:
        val = self.get("data", schemas.unset)
        if isinstance(val, schemas.Unset):
            return val
        return typing.cast(
            str,
            val
        )

    @property
    def id(self) -> typing.Union[int, schemas.Unset]:
        val = self.get("id", schemas.unset)
        if isinstance(val, schemas.Unset):
            return val
        return typing.cast(
            int,
            val
        )

    def get_additional_property_(self, name: str) -> typing.Union[schemas.OUTPUT_BASE_TYPES, schemas.Unset]:
        schemas.raise_if_key_known(name, self.__required_keys__, self.__optional_keys__)
        return self.get(name, schemas.unset)
FromSchemaDictInput = typing.Mapping[str, schemas.INPUT_TYPES_ALL]

@dataclasses.dataclass(frozen=True)
class FromSchema(
    schemas.Schema[FromSchemaDict, tuple]
):
    """NOTE: This class is auto generated by OpenAPI JSON Schema Generator.
    Ref: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator

    Do not edit the class manually.
    """
    types: typing.FrozenSet[typing.Type] = frozenset({schemas.immutabledict})
    properties: Properties = dataclasses.field(default_factory=lambda: schemas.typed_dict_to_instance(Properties)) # type: ignore
    type_to_output_cls: typing.Mapping[
        typing.Type,
        typing.Type
    ] = dataclasses.field(
        default_factory=lambda: {
            schemas.immutabledict: FromSchemaDict
        }
    )

    @classmethod
    def validate(
        cls,
        arg: typing.Union[
            FromSchemaDictInput,
            FromSchemaDict,
        ],
        configuration: typing.Optional[schema_configuration.SchemaConfiguration] = None
    ) -> FromSchemaDict:
        return super().validate_base(
            arg,
            configuration=configuration,
        )


#from __future__ import absolute_import
from multiprocessing import Process
import importlib
import argparse,json
mod = importlib.import_module("scrapy_cluster.kafka-monitor.kafka_monitor")
from models import PageDocument

from celery import task

def feed(json_req):
    KafkaMonitor=mod.KafkaMonitor

    #KAFKA_HOSTS = 'kafka.1.svc:9092'
    kafka_monitor = KafkaMonitor('localsettings.py')

    kafka_monitor.setup(level='INFO', log_file='INFO',
                        json='INFO')
    try:
        parsed = json.loads(json_req)
    except ValueError:
        kafka_monitor.logger.info("JSON failed to parse")

    #data={}
    #data['url'] = json_req['url']
    #data['title'] = json_req['title']
    #data['raw_content'] = json_req['raw_content']
    #data['extract_content'] = json_req['extract_content']

    #serializer = PageSerializer(data=data)
    #if serializer.is_valid():
    #    serializer.save()

    return kafka_monitor.feed(parsed)

@task()
def crawl(json_req):
    p = Process(target=feed, args=(json_req))
    p.start()
    p.join()


import torch
import pandas as pd
import sklearn
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.model_selection import KFold
import copy

from torch.utils.data import Dataset,DataLoader

class Embedding(nn.Module):
    """
    각 년차별로 쓸 mlp
    """

    def __init__(self,in_features,hidden_features=None,out_features=None,act_layer=nn.ReLU,dropout=0.1):
        super().__init__()
        out_features = out_features or in_features
        self.fc1=nn.Linear(in_features,hidden_features)
        self.act = act_layer()
        self.dropout = nn.Dropout(dropout)
        self.fc2 = nn.Linear(hidden_features, out_features)

    def forward(self,x):
        x = self.fc1(x)
        x = self.act(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return x

def batch_to_embbedings(datas,networks):
    """
    !!제일 중요!!
    batches : years of data. seperated outputs of dataloader. each element of datas has different feature size.
    network : embedding linear networks that matches feature size of each data
    batches에 저장된 nan 데이터를 모두 0으로 바꾸고 각자 맞는 network에 통과시킴. 통과시킨 데이터를 emb_list에 저장
    batches에 있는 nan을 토대로 각 데이터에 어떤 년도가 nan인지 저장.
    저장한 것을 바탕으로 transformer에 쓸 attention mask 만듬.
    return : emb_list, attn_mask
    emb_list : list of embbedings. embeddings = (batch, emb_features eg. 72)
    attn_mask : 트랜스포머에 쓸 마스크. (batch,1,seq_len+1,seq_len+1). seq_len에 1을 더하는건 트랜스포머의 cls_token 때문.

    """
    emb_list = []
    batch_nan_list = []
    batch_size = datas[0].shape[0]
    device = datas[0].device
    for i,net in enumerate(networks):
        datas_nan = torch.isnan(datas[i])
        _nan = datas_nan[:,0].clone().detach()
        x = torch.nan_to_num(datas[i])
        batch_nan_list.append(_nan)
        emb = net(x)
        emb[_nan] = 0
        emb_list.append(emb)

    attn_mask = torch.stack(batch_nan_list,dim=1)
    seq_len = len(batch_nan_list)
    temp = torch.BoolTensor(batch_size).to(device)

    temp[:] = False
    attn_mask = torch.concat((temp.unsqueeze(1),attn_mask),dim=1)
    attn_mask = attn_mask.unsqueeze(1).expand(-1,seq_len+1,-1)
    attn_mask = attn_mask.unsqueeze(1)
    return emb_list, attn_mask

def make_split_list(year_datas):
    """make split list used for spliting batches. batches must be splitted with torch.tensor_split with split_list"""
    split_list = []
    split = 0
    for data in year_datas:
        split += data.shape[1]
        split_list.append(split)
    split_list.pop() #
    return split_list

def batch_to_splited_datas(batch,split_list):
    """
    batch : 모든 feature들이 concat된 행렬 하나. 얘를 다시 년차별 데이터 리스트로 나눠줘야함.
    split_list : feature 나누는 번호가 기록된 리스트.
    return : 년차별 행렬이 담긴 리스트
    """

    list = torch.tensor_split(batch,split_list,dim=1)
    return list

class KELSDataSet(Dataset):
    """

    make dataset with list of dataframe.
    input : list of dataframe

    __getitem__ returns (batch, concated featres eg. 233 )

    """
    def __init__(self,input,label,is_regression=False):

        self.is_regression = is_regression
        self.label = label.to_numpy()
        self.seq_len = len(input)
        self.data_len = input.shape[0]
        self.data = input

    def __len__(self):
        return self.data_len

    def __getitem__(self,idx):

        x = torch.FloatTensor(self.data[idx])
        if self.is_regression == True:
            y_E,y_K,y_M  = torch.FloatTensor(self.label[idx])[0],torch.FloadTensor(self.label[idx])[1],torch.FloadTensor(self.label[idx])[2]
        else:
            y_E,y_K,y_M = torch.LongTensor(self.label[idx])[0],torch.LongTensor(self.label[idx])[1],torch.LongTensor(self.label[idx])[2]

        return (x,(y_E,y_K,y_M))

def make_embbeding_networks(sample_datas,hidden_features = 100, out_features = 72, dropout=0.1):
    """
    make embedding networks based on train_dataset. batch size must be same with dataloaders.
    transformer 내부 __init__에서 사용함.
    """

    #modulelist : 리스트는 리스트인데 모듈이 들어가는 리스트.
    embbeding_networks = nn.ModuleList() #모듈리스트에 등록하지 않으면 학습이 안됨. 원래는 자동으로 되지만, 우리는 6개의 임베딩네트워크를 리스트로 묶을 거라서 그냥 리스트에다 넣으면 안됨.
    # embbeding networks : 총 6개의 인코딩 네트워크. 흠.. nan 들어오면 batch x feature 사이즈의 nan true false 내놔야..?
    # batch x seq 의 nanlist도 필요..

    for sample_data in sample_datas:
        in_features = sample_data.shape[1]
        emb_net = Embedding(in_features,hidden_features=hidden_features,out_features=out_features,dropout=dropout)
        embbeding_networks.append(emb_net)
    return embbeding_networks

# def make_attn_mask(emb_seq_batch):
#     """
#     make attention mask from embedding batch.
#     batch = (batch, seq_len,embedding_size)
#     return = (batch, seq_len, seq_len)
#     """
#     batch_size = emb_seq_batch.shape[0]
#     seq_len = emb_seq_batch.shape[1]
#     emb_seq_batch_isnan = torch.isnan(emb_seq_batch)
#     torch.nan_to_num(emb_seq_batch) # emb_seq_batch 내용물의 nan을 0으로

#     attn_mask = emb_seq_batch_isnan[:,:,0]
#     temp = torch.BoolTensor(batch_size)

#     temp[:] = False
#     attn_mask = torch.concat((temp.unsqueeze(1),attn_mask),dim=1)

#     attn_mask = attn_mask.unsqueeze(1).expand(-1,seq_len+1,-1)
#     attn_mask = attn_mask.unsqueeze(1)
#     return attn_mask

def accuracy_roughly(y_pred, y_label):
    if len(y_pred) != len(y_label):
        print("not available, fit size first")
        return
    cnt = 0
    correct = 0
    for pred, label in zip(y_pred, y_label):
        cnt += 1
        if abs(pred-label) <= 1:
            correct += 1
    return correct / cnt

def train_net(model,train_loader,test_loader,optimizer_cls = optim.AdamW, criterion = nn.CrossEntropyLoss(),
n_iter=10,device='cpu',lr = 0.001,weight_decay = 0.01,mode = None):

        train_losses = []
        train_acc = []
        val_accs = []
        positive_accs = []
        #optimizer = optimizer_cls(model.parameters(),lr=lr,weight_decay=weight_decay)
        optimizer = optimizer_cls(model.parameters(),lr=lr)
        #scheduler = optim.lr_scheduler.MultiStepLR(optimizer,milestones=[25,40,60,80], gamma=0.5,last_epoch=-1)

        for epoch in range(n_iter):
                running_loss = 0.0
                model.train()
                n = 0
                n_acc = 0
                ys = []
                ypreds = []
                for i,(xx,(label_E,label_K,label_M)) in tqdm(enumerate(train_loader)):

                        xx = xx.to(device)
                        if mode == 'E':
                                yy = label_E
                        elif mode == 'K':
                                yy = label_K
                        elif mode == 'M':
                                yy = label_M
                        else:
                                assert True

                        yy = yy.to(device)

                        optimizer.zero_grad()
                        outputs = model(xx)
                        _,y_pred = outputs.max(1)

                        loss1 = criterion(outputs,yy)
                        loss2 = criterion(outputs,(yy+1).clamp(max=8))
                        loss3 = criterion(outputs,(yy-1).clamp(min=0))
                        loss = loss1 + loss2 + loss3

                        # Getting gradients w.r.t. parameters
                        loss.backward()

                        # Updating parameters
                        optimizer.step()
                        ys.append(yy)
                        ypreds.append(y_pred)

                        i += 1
                        n += len(xx)
                        _, y_pred = outputs.max(1)
                        n_acc += (yy == y_pred).float().sum().item()
                #scheduler.step()
                train_losses.append(running_loss/i)
                train_acc.append(n_acc/n)
                ys = torch.cat(ys)
                ypreds = torch.cat(ypreds)
                train_positive_acc = accuracy_roughly(ypreds,ys)
                acc, positive_acc = eval_net(model,test_loader,device,mode = mode)
                val_accs.append(acc)
                positive_accs.append(positive_acc)

                print(f'epoch : {epoch},train_positive_acc : {train_positive_acc} train_acc : {train_acc[-1]}, acc : {val_accs[-1]}. positive_acc : {positive_accs[-1]}',flush = True)

        return np.array(val_accs), np.array(positive_accs)

def eval_net(model,data_loader,device,mode=None):
    model.eval()
    ys = []
    ypreds = []
    for xx,(label_E,label_K,label_M) in data_loader:

        xx = xx.to(device)
        if mode == 'E':
            y = label_E
        elif mode == 'K':
            y = label_K
        elif mode == 'M':
            y = label_M
        else:
            assert True

        y = y.to(device)

        with torch.no_grad():
                score = model(xx)
                _,y_pred = score.max(1)
        ys.append(y)
        ypreds.append(y_pred)

    ys = torch.cat(ys)
    ypreds = torch.cat(ypreds)
    positive_acc = accuracy_roughly(ypreds,ys)
    acc= (ys == ypreds).float().sum() / len(ys)

    # print(sklearn.metrics.confusion_matrix(ys.numpy(),ypreds.numpy()))

    # print(sklearn.metrics.classification_report(ys.numpy(),ypreds.numpy()))

    return acc, positive_acc
    #return acc.item()


#!/usr/bin/env python
# encoding: utf-8
'''
@File  : search_Shanghai.py
@Date  : 2021/11/15 23:19
@Author: Cxs
@Email: Cxs1103@163.com
@Desc  :
'''
import requests
from bs4 import BeautifulSoup

url = 'http://www.tcmap.com.cn/shanghai/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}

response = requests.get(url, headers=headers)  # 发送HTTP请求
response.encoding = 'gb18030'  # 设置一下网站的中文编码
soup = BeautifulSoup(response.text, 'html.parser')  # 使用 BeautifulSoup 进行解析
# print(soup)
streets = []

# 找到数据对应的 html 节点，然后使用get_text()函数获取
for street in soup.find_all('a', class_='blue'):
    streets.append(street.get_text())

# 最后写入到相关文件夹中
with open(r'./address.txt', 'w', encoding='utf-8') as f:
    f.write('\n'.join(streets[:238]))


def capicua():
    result = "Not yet implemented"
    num = int(input("Escreva o número: "))

    #Contar o número de digitos do número
    digitos = num
    contador = 0
    while digitos > 0:
        contador += 1
        digitos //= 10

    #Obter o capicua
    digitos2 = num
    capicua = 0

    for i in range(contador):
        base_number = digitos2 % 10                     #Obter o número mais à direita
        digitos2 //= 10                                 #Remover o número mais à direita para obter o seguinte no próximo loop
        capicua += (base_number)*(10**(contador-1-i))   #Colocar o número da direita à esquerda, utilizando potencias de 10

    if capicua == num:
        result = str(num) + " is a palidrome."
    else:
        result = str(num) + " is not a palidrome."
    print(result)
    return result

capicua()

Exercises/RE04 - Conditionals and Iteration/cap.py


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

# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license

import os
import subprocess

from thumbor.optimizers import BaseOptimizer
from thumbor.utils import logger
from PIL import Image, ImageStat

class Optimizer(BaseOptimizer):
    def __init__(self, context):
        super(Optimizer, self).__init__(context)

        self.runnable = True
        self.imgmin_path = self.context.config.IMGMIN_PATH

        # All defaults are from imgmin unless noted otherwise
        self.error_threshold = self.context.config.AUTO_ERROR_THRESHOLD or '1.0'
        self.color_density_ratio = self.context.config.AUTO_COLOR_DENSITY_RATIO or '0.11'
        self.min_unique_colors = self.context.config.AUTO_MIN_UNIQUE_COLORS or '4096'
        self.quality_out_max = self.context.config.AUTO_QUALITY_OUT_MAX or '100' # imgming default 95
        self.quality_out_min = self.context.config.AUTO_QUALITY_OUT_MIN or '95' # imgming default 70
        self.quality_in_min = self.context.config.AUTO_QUALITY_IN_MIN or '82'
        self.max_steps = self.context.config.AUTO_MAX_STEPS or '5'

        if not ( os.path.isfile(self.imgmin_path) ):
            logger.error("ERROR path '{0}' or '{1}' is not accessible".format(self.imgmin_path))
            self.runnable = False

    def should_run(self, image_extension, buffer):
        return self.runnable and 'auto' in self.context.request.filters and ('png' in image_extension or 'jpg' in image_extension or 'jpeg' in image_extension)

    def optimize(self, buffer, input_file, output_file):
        input_image = Image.open(input_file)
        stats = ImageStat.Stat(input_image).extrema
        has_alpha = False
        if len(stats) > 3 and (stats[3][0] < 255):
            has_alpha = True

        if has_alpha == False:
            intermediary = output_file + '-intermediate'
            input_image.save(intermediary, 'JPEG', quality=100)
            input_file = intermediary

        command = '%s --error-threshold %s --color-density-ratio %s --min-unique-colors %s --quality-out-max %s --quality-out-min %s --quality-in-min %s --max-steps %s %s %s > /dev/null 2>&1' % (
            self.imgmin_path,
            self.error_threshold,
            self.color_density_ratio,
            self.min_unique_colors,
            self.quality_out_max,
            self.quality_out_min,
            self.quality_in_min,
            self.max_steps,
            input_file,
            output_file,
        )
        with open(os.devnull) as null:
            logger.debug("[AUTO IMGMIN] running: " + command)
            subprocess.call(command, shell=True, stdin=null)


#We are going to be opening "um-server-01.txt" file and save it into the variable 'log_file'
log_file = open("um-server-01.txt")

#Function named 'sales_reports' is declared and 'log_file' will be passed into it as an argument
def sales_reports(log_file):
    #Running a for-in loop for 'log_file' as the function combs through the text
    for line in log_file:
        #Each line will be looped through and have its trailing characters removed with the '.rstrip' method
        line = line.rstrip()
        #accessing the list, we're going to slice, or accessing a specific range using the colon operator starting at the beginning and stopping at third specified day
        day = line[0:3]
        #using an "if" statement, if the text matches the day (Tuesday), the lines in the file will be printed
        if day == "Mon":
            print(line)

#Calling the 'sales_reports' function while passing 'log_file' in as an argument
sales_reports(log_file)


import os
import argparse
import sys

import re

parser = argparse.ArgumentParser(description="This script gets the unigram probabilities of words.",
                                 epilog="E.g. " + sys.argv[0] + " --vocab-file=data/rnnlm/vocab/words.txt "
                                        "--data-weights-file=exp/rnnlm/data_weights.txt data/rnnlm/data "
                                        "> exp/rnnlm/unigram_probs.txt",
                                 formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument("--vocab-file", type=str, default='', required=True,
                    help="Specify the vocab file.")
parser.add_argument("--unk-word", type=str, default='',
                    help="String form of unknown word, e.g. <unk>.  Words in the counts "
                    "but not present in the vocabulary will be mapped to this word. "
                    "If the empty string, we act as if there is no unknown-word, and "
                    "OOV words are treated as an error.")
parser.add_argument("--data-weights-file", type=str, default='', required=True,
                    help="File that specifies multiplicities and weights for each data source: "
                    "e.g. if <text_dir> contains foo.txt and bar.txt, then should have lines "
                    "like 'foo 1 0.5' and 'bar 5 1.5'.  These "
                    "don't have to sum to one.")
parser.add_argument("--smooth-unigram-counts", type=float, default=1.0,
                    help="Specify the constant for smoothing. We will add "
                         "(smooth_unigram_counts * num_words_with_non_zero_counts / vocab_size) "
                         "to every unigram counts.")
parser.add_argument("text_dir",
                    help="Directory in which to look for data")

args = parser.parse_args()

SPECIAL_SYMBOLS = ["<eps>", "<s>", "<brk>"]

# get the name with txt and counts file path for all data sources except dev
# return a dict with key is the name of data_source,
#                    value is a tuple (txt_file_path, counts_file_path)
def get_all_data_sources_except_dev(text_dir):
    data_sources = {}
    for f in os.listdir(text_dir):
        full_path = text_dir + "/" + f
        if f == 'dev.txt' or f == 'dev.counts' or os.path.isdir(full_path):
            continue
        if f.endswith(".txt"):
            name = f[0:-4]
            if name in data_sources:
                data_sources[name] = (full_path, data_sources[name][1])
            else:
                data_sources[name] = (full_path, None)
        elif f.endswith(".counts"):
            name = f[0:-7]
            if name in data_sources:
                data_sources[name] = (data_sources[name][0], full_path)
            else:
                data_sources[name] = (None, full_path)
        else:
            sys.exit(sys.argv[0] + ": Text directory should not contain files with suffixes "
                     "other than .txt or .counts: " + f)

    for name, (txt_file, counts_file) in data_sources.items():
        if txt_file is None or counts_file is None:
            sys.exit(sys.argv[0] + ": Missing .txt or .counts file for data source: " + name)

    return data_sources

# read the data-weights for data_sources from weights_file
# return a dict with key is name of a data source,
#                    value is a tuple (repeated_times_per_epoch, weight)
def read_data_weights(weights_file, data_sources):
    data_weights = {}
    with open(weights_file, 'r', encoding="utf-8") as f:
        for line in f:
            try:
                fields = line.split()
                assert len(fields) == 3
                if fields[0] in data_weights:
                    raise Exception("duplicated data source({0}) specified in "
                                    "data-weights: {1}".format(fields[0], weights_file))
                data_weights[fields[0]] = (int(fields[1]), float(fields[2]))
            except Exception as e:
                sys.exit(sys.argv[0] + ": bad data-weights line: '" +
                         line.rstrip("\n") + "': " + str(e))

    for name in data_sources.keys():
        if name not in data_weights:
            sys.exit(sys.argv[0] + ": Weight for data source '{0}' not set".format(name))

    return data_weights

# read the voab
# return the vocab, which is a dict mapping the word to a integer id.
def read_vocab(vocab_file):
    vocab = {}
    with open(vocab_file, 'r', encoding="utf-8") as f:
        for line in f:
            fields = line.split()
            assert len(fields) == 2
            if fields[0] in vocab:
                sys.exit(sys.argv[0] + ": duplicated word({0}) in vocab: {1}"
                                       .format(fields[0], vocab_file))
            vocab[fields[0]] = int(fields[1])

    # check there is no duplication and no gap among word ids
    sorted_ids = sorted(vocab.values())
    for idx, id in enumerate(sorted_ids):
        assert idx == id
    if args.unk_word != '' and args.unk_word not in vocab:
        sys.exit(sys.argv[0] + "--unk-word={0} does not appear in vocab file {1}".format(
            args.unk_word, vocab_file))
    return vocab

# Get total (weighted) count for words from all data_sources
# return a list of counts indexed by word id.
def get_counts(data_sources, data_weights, vocab):
    counts = [0.0] * len(vocab)

    for name, (_, counts_file) in data_sources.items():
        weight = data_weights[name][0] * data_weights[name][1]
        if weight == 0.0:
            continue

        with open(counts_file, 'r', encoding="utf-8") as f:
            for line in f:
                fields = line.split()
                if len(fields) != 2: print("Warning, should be 2 cols:", fields, line, file=sys.stderr);
                assert(len(fields) == 2)
                word = fields[0]
                count = fields[1]
                if word not in vocab:
                    if args.unk_word == '':
                        sys.exit(sys.argv[0] + ": error: an OOV word {0} is present in the "
                                 "counts file {1} but you have not specified an unknown word to "
                                 "map it to (--unk-word option).".format(word, counts_file))
                    else:
                        word = args.unk_word
                counts[vocab[word]] += weight * int(fields[1])

    return counts

# Smooth counts and get unigram probs for words
# return a list of probs indexed by word id.
def get_unigram_probs(vocab, counts, smooth_constant):
    special_symbol_ids = [vocab[x] for x in SPECIAL_SYMBOLS]
    vocab_size = len(vocab) - len(SPECIAL_SYMBOLS)
    num_words_with_non_zero_counts = 0
    for word_id, count in enumerate(counts):
        if word_id in special_symbol_ids:
            continue
        if counts[word_id] > 0:
            num_words_with_non_zero_counts += 1

    if num_words_with_non_zero_counts < vocab_size and smooth_constant == 0.0:
        sys.exit(sys.argv[0] + ": --smooth-unigram-counts should not be zero, "
                               "since there are words with zero-counts")

    smooth_count = smooth_constant * num_words_with_non_zero_counts / vocab_size

    total_counts = 0.0
    for word_id, count in enumerate(counts):
        if word_id in special_symbol_ids:
            continue
        counts[word_id] += smooth_count
        total_counts += counts[word_id]

    probs = []
    for count in counts:
        probs.append(count / total_counts)

    return probs

if os.system("rnnlm/ensure_counts_present.sh {0}".format(args.text_dir)) != 0:
    print(sys.argv[0] + ": command 'rnnlm/ensure_counts_present.sh {0}' failed.".format(
        args.text_dir))

data_sources = get_all_data_sources_except_dev(args.text_dir)
data_weights = read_data_weights(args.data_weights_file, data_sources)
vocab = read_vocab(args.vocab_file)

counts = get_counts(data_sources, data_weights, vocab)
probs = get_unigram_probs(vocab, counts, args.smooth_unigram_counts)

for idx, p in enumerate(probs):
    print(idx, p)

print(sys.argv[0] + ": generated unigram probs.", file=sys.stderr)


### Note: most of this copy-paste from SciPy's L-BFGS-B wrapper:
### https://github.com/scipy/scipy/blob/master/scipy/optimize/lbfgsb.py

import numpy as np
from lbfgsb_ns import _lbfgsb_ns
from scipy.optimize import OptimizeResult
from scipy.optimize.lbfgsb import LbfgsInvHessProduct
from numpy import array, asarray, float64, int32, zeros
from scipy.optimize._optimize import (MemoizeJac, OptimizeResult,
					   _check_unknown_options, _prepare_scalar_function)
from scipy.optimize._constraints import old_bound_to_new
from scipy.optimize import _lbfgsb

def minimize_lbfgsb_ns(fun, x0, args=(), jac=None, bounds=None,
	disp=None, maxcor=10, ftol=2.2204460492503131e-09,
	gtol=1e-5, eps=1e-8, maxfun=15000, maxiter=15000,
	iprint=-1, callback=None, maxls=20, finite_diff_rel_step=None, taux=1e-3, taud=1e-6, **unknown_options):
	"""
	Minimize a scalar function of one or more variables using the L-BFGS-B-NS
	algorithm.

	Parameters
	----------
	disp : None or int
		If `disp is None` (the default), then the supplied version of `iprint`
		is used. If `disp is not None`, then it overrides the supplied version
		of `iprint` with the behaviour you outlined.
	maxcor : int
		The maximum number of variable metric corrections used to
		define the limited memory matrix. (The limited memory BFGS
		method does not store the full hessian but uses this many terms
		in an approximation to it.)
	ftol : float
		The iteration stops when ``(f^k -
		f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol``.
	gtol : float
		The iteration will stop when ``max{|proj g_i | i = 1, ..., n}
		<= gtol`` where ``pg_i`` is the i-th component of the
		projected gradient.
	eps : float
		Step size used for numerical approximation of the jacobian.
	maxfun : int
		Maximum number of function evaluations.
	maxiter : int
		Maximum number of iterations.
	maxls : int, optional
		Maximum number of line search steps (per iteration). Default is 20.
	taux : float
		Parameter for line search
	taud: float
		Parameter for line search

	Returns
	-------
	res : OptimizeResult
		dict-like object with the optimal values in entry 'x' and other associated info.

	References
	----------
	Henao, W., 2014. An L-BFGS-B-NS optimizer for non-smooth functions. Master's thesis, Courant Institute of Mathematical Science, New York University.
	"""
	_check_unknown_options(unknown_options)
	m = maxcor
	epsilon = eps
	pgtol = gtol
	factr = ftol / np.finfo(float).eps

	x0 = asarray(x0).ravel()
	n, = x0.shape

	if bounds is None:
		bounds = [(None, None)] * n
	if len(bounds) != n:
		raise ValueError('length of x0 != length of bounds')
	# unbounded variables must use None, not +-inf, for optimizer to work properly
	bounds = [(None if l == -np.inf else l, None if u == np.inf else u) for l, u in bounds]
	# LBFGSB is sent 'old-style' bounds, 'new-style' bounds are required by
	# approx_derivative and ScalarFunction
	new_bounds = old_bound_to_new(bounds)

	# check bounds
	if (new_bounds[0] > new_bounds[1]).any():
		raise ValueError("LBFGSB-NS - one of the lower bounds is greater than an upper bound.")

	# initial vector must lie within the bounds. Otherwise ScalarFunction and
	# approx_derivative will cause problems
	x0 = np.clip(x0, new_bounds[0], new_bounds[1])

	if disp is not None:
		if disp == 0:
			iprint = -1
		else:
			iprint = disp

	if jac == True:
		fun = MemoizeJac(fun)
		jac = fun.derivative
	sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
								  bounds=new_bounds,
								  finite_diff_rel_step=finite_diff_rel_step)

	func_and_grad = sf.fun_and_grad

	fortran_int = _lbfgsb.types.intvar.dtype

	nbd = zeros(n, fortran_int)
	low_bnd = zeros(n, float64)
	upper_bnd = zeros(n, float64)
	bounds_map = {(None, None): 0,
				  (1, None): 1,
				  (1, 1): 2,
				  (None, 1): 3}
	for i in range(0, n):
		l, u = bounds[i]
		if l is not None:
			low_bnd[i] = l
			l = 1
		if u is not None:
			upper_bnd[i] = u
			u = 1
		nbd[i] = bounds_map[l, u]

	if not maxls > 0:
		raise ValueError('maxls must be positive.')

	x = array(x0, float64)
	f = array(0.0, float64)
	g = zeros((n,), float64)
	jmax= maxls
	wa = zeros(2*m*n + 5*n + 11*m*m + 8*m+ 2*jmax*n, float64)
	iwa = zeros(3*n, fortran_int)
	task = zeros(1, 'S120')
	csave = zeros(1, 'S120')
	lsave = zeros(4, fortran_int)
	isave = zeros(47, fortran_int)
	dsave = zeros(30, float64)

	task[:] = 'START'

	n_iterations = 0

	nfg = array(0, fortran_int)
	nbisect = array(0, fortran_int)

	taux = array(taux, float64)
	taud = array(taud, float64)
	jmax = array(maxls, fortran_int)
	m = array(m, fortran_int)
	n = array(n, fortran_int)

	while 1:
		_lbfgsb_ns.setulb(
				n = n,
				m = m,
				x = x,
				l = low_bnd,
				u = upper_bnd,
				nbd = nbd,
				f = f,
				g = g,
				factr = factr,
				pgtol = pgtol,
				wa = wa,
				iwa = iwa,
				task = task,
				iprint = iprint,
				csave = csave,
				lsave = lsave,
				isave = isave,
				dsave = dsave,
				taux = taux,
				nfg = nfg,
				jmax = jmax,
				taud = taud,
				nbisect = nbisect
			)

		task_str = task.tobytes()
		if task_str.startswith(b'FG'):
			# The minimization routine wants f and g at the current x.
			# Note that interruptions due to maxfun are postponed
			# until the completion of the current minimization iteration.
			# Overwrite f and g:
			f, g = func_and_grad(x)
		elif task_str.startswith(b'NEW_X'):
			# new iteration
			n_iterations += 1
			if callback is not None:
				callback(np.copy(x))

			if n_iterations >= maxiter:
				task[:] = 'STOP: TOTAL NO. of ITERATIONS REACHED LIMIT'
			elif sf.nfev > maxfun:
				task[:] = ('STOP: TOTAL NO. of f AND g EVALUATIONS '
						   'EXCEEDS LIMIT')
		else:
			break

	task_str = task.tobytes().strip(b'\x00').strip()
	if task_str.startswith(b'CONV'):
		warnflag = 0
	elif sf.nfev > maxfun or n_iterations >= maxiter:
		warnflag = 1
	else:
		warnflag = 2

	# These two portions of the workspace are described in the mainlb
	# subroutine in lbfgsb.f. See line 363.
	s = wa[0: m*n].reshape(m, n)
	y = wa[m*n: 2*m*n].reshape(m, n)

	# See lbfgsb.f line 160 for this portion of the workspace.
	# isave(31) = the total number of BFGS updates prior the current iteration;
	n_bfgs_updates = isave[30]

	n_corrs = min(n_bfgs_updates, maxcor)
	hess_inv = LbfgsInvHessProduct(s[:n_corrs], y[:n_corrs])

	return OptimizeResult(fun=f, jac=g, nfev=sf.nfev, njev=sf.ngev,
						  nit=n_iterations, status=warnflag, message=task_str,
						  x=x, success=(warnflag == 0), hess_inv=hess_inv)


import requests
from bs4 import BeautifulSoup

# URL of the website you want to scrape
url = 'https://example.com'

# Send an HTTP GET request to the URL
response = requests.get(url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
    # Parse the HTML content of the page
    soup = BeautifulSoup(response.text, 'html.parser')

    # Find and print specific elements (e.g., headings)
    headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
    for heading in headings:
        print(heading.text)

    # Find and print links
    links = soup.find_all('a')
    for link in links:
        print(link.get('href'))

    # Find and print paragraphs
    paragraphs = soup.find_all('p')
    for paragraph in paragraphs:
        print(paragraph.text)

else:
    print('Failed to retrieve the web page. Status code:', response.status_code)


# IMPORT MODULE HERE
import pyttsx3
import datetime
import time
import xlsxwriter
from docx import Document
import speech_recognition as sr
import webbrowser as wb
import os

class virtual_assistant:
    def __init__(self):
        # tạo trợ lý
        self.assistant = pyttsx3.init()
        # giúp nhận diện trên 1 dòng
        self.voice = self.assistant.getProperty('voices')
         # chọn giọng nam/ nữ (0,1)
        self.assistant.setProperty('voice',self.voice[0].id)

    def speak(self,audio):
        self.assistant.say(audio)
        self.assistant.runAndWait()

    def welcome(self):
        hour = datetime.datetime.now().hour
        if 4 < hour < 10:
            self.speak('Good moring, Boss')
        elif hour < 18:
            self.speak('Good afternoon, Boss')
        else:
            self.speak('Good evening, Boss')

    def showtime(self):
        time = datetime.datetime.now().strftime("%H:%M:%p")
        print("S.I.R.I: It ís", time)
        self.speak("It is" + time)

    def showdate(self):
        date = datetime.datetime.now().strftime('%A %d %B %Y')
        print("S.I.R.I: It ís", date)
        self.speak("It is" + date)

    def wait(self):
            cmd = sr.Recognizer()
            # xác nhận nguồn nhận --> microphone
            with sr.Microphone() as source:
                # setup thời gian đợi
                cmd.pause_threshold = 2
                audio = cmd.listen(source)
                try:
                    query = cmd.recognize_google(audio,language = 'en').lower()
                    if 'hello' in query or 'wake' in query:
                        return False
                    return True
                except:
                    return True

    def sleep(self):
        check = self.wait()
        while check:
            time.sleep(1)
            check = self.wait()

    def command(self):
        # tạo đối tượng giúp nhận diện giọng nói
        cmd = sr.Recognizer()

        # xác nhận nguồn nhận --> microphone
        with sr.Microphone() as source:
            # setup thời gian đợi
            cmd.pause_threshold = 2
            audio = cmd.listen(source)

            try:
                query = cmd.recognize_google(audio,language = 'en')
                print('-- YOU: ' + query)

            except sr.UnknownValueError:
                print('Please repeat or typing your command: ')
                query = input('Your command is: ')
        return query

    def create_Excel(self):
        self.speak('Please input name of excel file')
        name_file = input('Name of excel file: ')
        workbook = xlsxwriter.Workbook(f'{name_file}.xlsx')
        worksheet = workbook.add_worksheet()
        workbook.close()

        os.startfile(f'{name_file}.xlsx')

    def create_Word(self):
        self.speak('Please input name of word file')
        name_file = input('Name of word file: ')
        document = Document()
        document.save(f'{name_file}.docx')

        os.startfile(f'{name_file}.docx')

def main():
    siri = virtual_assistant()
    siri.welcome()
    while True:
        print("S.I.R.I: How can I help you ?")
        siri.speak('How can I help you ?')

        query = siri.command().lower()

        if 'google' in query:
            print('S.I.R.I: What do you want to search ?')
            siri.speak('What do you want to search ?')
            search = siri.command().lower()
            url = f'https://www.google.com.vn/search?q={search}'
            wb.get().open(url)
            siri.speak(f'Here you are')

        elif 'youtube' in query:
            print('S.I.R.I: What do you want to watch ?')
            siri.speak('What do you want to watch ?')
            search = siri.command().lower()
            url = f'https://www.youtube.com/results?search_query={search}'
            wb.get().open(url)
            siri.speak(f'Here you are')

        elif 'time' in query:
            siri.showtime()

        elif 'date' in query or 'day' in query:
            siri.showdate()

        elif 'notepad' in query:
            siri.speak(f'Openning Notepad')
            os.system('Notepad')

        elif 'excel' in query:
            siri.create_Excel()
            siri.speak(f'Openning Microsolf Excel')

        elif 'word' in query:
            siri.create_Word()
            siri.speak(f'Openning Microsolf Word')

        elif 'sleep' in query or 'pause' in query:
            print('S.I.R.I: Call me when you need')
            siri.speak('Call me when you need')
            siri.sleep()

        elif 'exit' in query or 'quit' in query:
            print('Goodbye, boss. Later')
            siri.speak('Goodbye, Boss.')
            siri.speak('Later')
            return

        else:
            print("S.I.R.I: Sorry I don't understand ?")
            siri.speak("Sorry I don't understand ?")

main()


#!/usr/bin/env python
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

if rank == 0:
    data = np.random.rand(1000,1)
else:
    data = None
data = comm.scatter(data, root=0)

print("Process %d has data" % rank, data)

data *= 2
print("Process %d now has data" % rank, data)

data /= 2
print("Process %d now has data" % rank, data)

if 0:
    comm.reduce(data, op=MPI.SUM)

    print("The root is %d" % MPI.ROOT)
    if rank == 0:
        print("The sum is %d" % data)


#!/usr/bin/env python3
def main():
    n, m = map(int,input().split())
    a = [int(input()) for _ in range(m)]

    dp = [0] * (n+1)
    dp[0] = 1
    mod = 10**9 + 7

    if 1 not in a:
        dp[1] = 1

    for i in range(2,n+1):
        if i not in a:
            dp[i] = (dp[i-1] + dp[i-2]) % mod

    return dp[-1]

print(main())

# not self AC


"""empty message

Revision ID: 884c94e491c3
Revises: 7f9971c51679
Create Date: 2019-12-05 15:16:38.137728

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '884c94e491c3'
down_revision = '7f9971c51679'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('coupons',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('code', sa.String(length=10), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('code')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('coupons')
    # ### end Alembic commands ###


import sys
sys.path.append('../')
from argparse import ArgumentParser
import json
import os
import shutil
import cv2
import numpy as np
import math
from numpy import linalg as LA
from scipy.spatial import distance

import pathlib
import time
import copy
import matplotlib.pyplot as plt
import pickle
import datetime
import json
from collections import defaultdict
from csv import reader
from utils import *
import glob

# ----------------------------------------
#  Curations of the whole experiment
# ----------------------------------------
class Config:
    def __init__(self):
        # --------------------------
        #  Paramaters of experiment
        # --------------------------
        self.user = 'brcao' # 'anon'
        self.root_path = '/media/' + self.user + '/eData2' # '/home/' + self.user
        self.seq_root_path = self.root_path + '/Data/datasets/RAN/seqs/outdoor'
        self.IMU_200 = False # edit
        if self.IMU_200: self.dataset4model_root_path = self.root_path + '/Data/datasets/RAN4model_dfv4_IMU_200'
        else: self.dataset4model_root_path = self.root_path + '/Data/datasets/RAN4model_dfv4'
        self.seq4model_root_path = self.dataset4model_root_path + '/seqs/outdoor'
        if not os.path.exists(self.seq4model_root_path):
            os.makedirs(self.seq4model_root_path)
        print('self.seq_root_path: ', self.seq_root_path)
        # self.scene_ls = ['scene0', 'scene1', 'scene2', 'scene3', 'scene4']
        self.seq_id_path_ls = glob.glob(self.seq_root_path + '/**/*')
        print('self.seq_id_path_ls: ', self.seq_id_path_ls)
        print('len(self.seq_id_path_ls): ', len(self.seq_id_path_ls)) # 79
        self.seq_id_ls = [seq_id_path[-15:] for seq_id_path in self.seq_id_path_ls]

        self.img_type = 'RGB_ts16_dfv4_anonymized'
        # --------------------------------------
        #  To be updated in update_parameters()
        print(); print() # debug
        print('self.seq_id_path_ls: ', self.seq_id_path_ls)
        print('self,seq_id_ls: ', self.seq_id_ls)
        self.seq_id = self.seq_id_ls[0] # dummy one
        self.exp = 1 # edit
        self.exp_path = self.root_path + '/Data/datasets/RAN/seqs/exps/exp' + str(self.exp)
        self.meta_path = self.exp_path + '/meta_outdoor.csv'

        if '20210907' in self.seq_id: self.scene_id = 1
        elif '20211004' in self.seq_id: self.scene_id = 2
        elif '20211006' in self.seq_id: self.scene_id = 3
        elif self.seq_id < '20211007_120000': self.scene_id = 4
        elif self.seq_id > '20211007_120000': self.scene_id = 5

        if self.scene_id == 1 or self.scene_id == 2: self.subjects = [43, 80, 0]
        else: self.subjects = [43, 80, 53, 0]
        print(); print() # debug
        print('self.subjects: ', self.subjects)
        self.subj_to_rand_id_file_path = self.exp_path + '/subj_to_rand_id.json'
        with open(self.subj_to_rand_id_file_path, 'r') as f:
            self.subj_to_id_dict = json.load(f)
            print(self.subj_to_rand_id_file_path, 'loaded!')

        self.seq_path = self.seq_root_path + '/scene' + str(self.scene_id) + '/' + self.seq_id
        self.seq_date = self.seq_id[:8]
        self.seq_id_to_start_end_ots_offsets = defaultdict()
        self.subj_to_offset = defaultdict()
        self.start_ots, self.end_ots, self.subj_to_offset = get_start_end_ts_update_phone_with_offsets(self.seq_id, self.meta_path)

        # -----
        #  GND
        # -----
        self.GND_path = self.seq_path + '/GND/RAN_' + self.seq_id + '-export.json'
        with open(self.GND_path, 'r') as f:
            self.GND_dict = json.load(f)
            # print();print() # debug
            # print('self.GND_dict: ', self.GND_dict)
            '''
            e.g.
            {'id': '8u1F_c4vs', 'type': 'RECTANGLE', 'tags': ['Murtadha'],
            'boundingBox': {'height': 147.33590733590734, 'width': 78.72648335745296,
            'left': 276.931982633864, 'top': 65.5019305019305}, 'points': [{'x':
            276.931982633864, 'y': 65.5019305019305}, {'x': 355.6584659913169,
            'y': 65.5019305019305}, {'x': 355.6584659913169, 'y': 212.83783783783784},
            {'x': 276.931982633864, 'y': 212.83783783783784}]}], 'version': '2.1.0'}}}
            '''

        self.GND_ts16_dfv4_path = self.seq_path + '/GND_ts16_dfv4'
        if not os.path.exists(self.GND_ts16_dfv4_path):
            os.makedirs(self.GND_ts16_dfv4_path)

        # -------------------
        #  Main data to save
        # -------------------
        self.GND_ts16_dfv4_to_BBX4tlhw_path = self.GND_ts16_dfv4_path + '/GND_ts16_dfv4_to_BBX4tlhw.json'
        self.GND_ts16_dfv4_to_BBX4tlhw = defaultdict()
        #  To de updated in update_parameters()
        # --------------------------------------

C = Config()

def update_parameters(seq_id_idx):
    C.seq_id = C.seq_id_ls[seq_id_idx]
    if '20210907' in C.seq_id: C.scene_id = 1
    elif '20211004' in C.seq_id: C.scene_id = 2
    elif '20211006' in C.seq_id: C.scene_id = 3
    elif C.seq_id < '20211007_120000': C.scene_id = 4
    elif C.seq_id > '20211007_120000': C.scene_id = 5
    print(); print() # debug
    print('C.scene: ', C.scene_id)

    if C.scene_id == 1 or C.scene_id == 2: C.subjects = [43, 80]
    else: C.subjects = [43, 80, 53]
    print(); print() # debug
    print('C.subjects: ', C.subjects)
    C.subj_to_rand_id_file_path = C.exp_path + '/subj_to_rand_id.json'
    with open(C.subj_to_rand_id_file_path, 'r') as f:
        C.subj_to_id_dict = json.load(f)
        print(C.subj_to_rand_id_file_path, 'loaded!')
    C.seq_path = C.seq_root_path + '/scene' + str(C.scene_id) + '/' + C.seq_id
    C.seq_date = C.seq_id[:8]

    C.img_path = C.seq_path + '/' + C.img_type

    # -----
    #  GND
    # -----
    C.GND_path = C.seq_path + '/GND/RAN_' + C.seq_id + '-export.json'
    with open(C.GND_path, 'r') as f:
        C.GND_dict = json.load(f)
        # print();print() # debug
        # print('C.GND_dict: ', C.GND_dict)
        '''
        e.g.
        {'id': '8u1F_c4vs', 'type': 'RECTANGLE', 'tags': ['Murtadha'],
        'boundingBox': {'height': 147.33590733590734, 'width': 78.72648335745296,
        'left': 276.931982633864, 'top': 65.5019305019305}, 'points': [{'x':
        276.931982633864, 'y': 65.5019305019305}, {'x': 355.6584659913169,
        'y': 65.5019305019305}, {'x': 355.6584659913169, 'y': 212.83783783783784},
        {'x': 276.931982633864, 'y': 212.83783783783784}]}], 'version': '2.1.0'}}}
        '''

    C.GND_ts16_dfv4_path = C.seq_path + '/GND_ts16_dfv4'
    if not os.path.exists(C.GND_ts16_dfv4_path):
        os.makedirs(C.GND_ts16_dfv4_path)

    # -------------------
    #  Main data to save
    # -------------------
    C.GND_ts16_dfv4_to_BBX4tlhw_path = C.GND_ts16_dfv4_path + '/GND_ts16_dfv4_to_BBX4tlhw.json'
    C.GND_ts16_dfv4_to_BBX4tlhw = defaultdict()

def get_RGB_ts16_dfv4_ls():
    file_dir = pathlib.Path(C.img_path)
    file_pattern = "*.jpg"

    C.img_names_ls = []
    C.img_file_paths = []
    C.RGB_ts16_dfv4_ls = []
    for file in file_dir.glob(file_pattern):
        # img_names_to_file_path[file.name] = file
        C.img_file_paths.append(file.__str__())
        # C.img_names_ls.append(file.name)
        C.RGB_ts16_dfv4_ls.append(file.name[:17])
    C.img_file_paths.sort()
    # C.img_names_ls.sort()
    C.RGB_ts16_dfv4_ls.sort()
    # print();print() # debug
    # print('C.img_file_paths: ', C.img_file_paths)
    # print('C.img_names_ls: ', C.img_names_ls)
    # print('C.RGB_ts16_dfv4_ls: ', C.RGB_ts16_dfv4_ls)
    # e.g. 1608750783.029950

def convert_to_BBX4tlhw_save():
    # https://www.geeksforgeeks.org/python-find-closest-number-to-k-in-given-list/
    def closest(lst, K):
        return lst[min(range(len(lst)), key = lambda i: abs(float(lst[i])-float(K)))]
    for k in C.GND_dict['assets'].keys():
        ots26 = C.GND_dict['assets'][k]['asset']['name']
        ots26 = ots26[:10] + ' ' + ots26[13:-4]
        # print();print() # debug
        # print('ots26: ', ots26) # e.g. 2021-10-04 14:31:27.338290

        if '_' in ots26:
            ots26 = ots26.replace('_', ':')
        ts16_dfv4 = ots26_to_ts16_dfv4(ots26)
        # print();print() # debug
        # print('ts16_dfv4: ', ts16_dfv4) # e.g. 1633372287.338290
        if ts16_dfv4 in C.RGB_ts16_dfv4_ls:
            for reg in C.GND_dict['assets'][k]['regions']:
                # print();print() # debug
                # print('reg: ', reg)
                subj = reg['tags'][0]
                # if subj == 'Others' or 'Unknown': subj = 'Other' + str(reg['id'])
                if subj not in C.subjects: subj = 'Other' + str(reg['id'])
                data = {'id': reg['id'], 't': reg['boundingBox']['top'], 'l': reg['boundingBox']['left'], \
                        'h': reg['boundingBox']['height'], 'w': reg['boundingBox']['width']}
                if ts16_dfv4 not in C.GND_ts16_dfv4_to_BBX4tlhw.keys():
                    C.GND_ts16_dfv4_to_BBX4tlhw[ts16_dfv4] = defaultdict()
                C.GND_ts16_dfv4_to_BBX4tlhw[ts16_dfv4][subj] = data

    # print();print() # debug
    # print('C.GND_ts16_dfv4_to_BBX4tlhw: ', C.GND_ts16_dfv4_to_BBX4tlhw)
    with open(C.GND_ts16_dfv4_to_BBX4tlhw_path, 'w') as f:
        json.dump(C.GND_ts16_dfv4_to_BBX4tlhw, f)
        print();print() # debug
        print('C.GND_ts16_dfv4_to_BBX4tlhw: ', C.GND_ts16_dfv4_to_BBX4tlhw)
        print(C.GND_ts16_dfv4_to_BBX4tlhw_path, 'saved!')

# ------------------
#  Process all seqs
# ------------------
# ---------
#  Outdoor
# ---------
for seq_id_idx, seq_id in enumerate(C.seq_id_ls):
    update_parameters(seq_id_idx)
    get_RGB_ts16_dfv4_ls()
    convert_to_BBX4tlhw_save()


#speelhal.py
personen = int (input('met hoeveel personen ben je?'))
ticket = 7,45
ticketprijs = personen * ticket

tijd = int (input('hoeveel minuten wil je de vr gebruiken?'))
vr = tijd / 5 * personen

totaal = int (input(ticketprijs + vr))
print ('Totaal', totaal ,'euro')


import os
import subprocess
import sys

def KillPort(port):
    try:
        port = int(port)
        cmd = 'lsof -t -i:{0}'.format(port)
        pid = None
        try:
            pid = subprocess.check_output(cmd, shell=True)
        except Exception as e:
            print("No process running on port {} by current user. Checking if root is running the proecess".format(port))
            if pid is None:
                cmd = 'sudo lsof -t -i:{0}'.format(port)
                pid = subprocess.check_output(cmd, shell=True)
        pids =  pid.decode().split("\n")
        pids_int = []
        for pid in pids:
            if pid:
                pid = int(pid)
                pids_int.append(pid)
    except ValueError as e:
        print(e)
        return -1
    except Exception as e:
        print("No process found running on port {0}.".format(port))
        return -1

    for pid in pids_int:
        processTypeCmd = 'ps -p {0} -o comm='.format(pid)
        processType = subprocess.check_output(processTypeCmd, shell=True, text=True).rstrip('\n')

        userCmd = 'ps -o user= -p {}'.format(pid)
        user = subprocess.check_output(userCmd, shell=True, text=True).rstrip('\n')
        if user.lower() == "root":
            killCmd = 'sudo kill -9 {0}'.format(pid)
        else:
            killCmd = 'kill -9 {0}'.format(pid)
        isKilled = os.system(killCmd)
        if isKilled == 0:
            print("Port {0} is free. Processs {1} killed successfully".format(port, pid))
        else:
            print("Cannot free port {0}.Failed to kill process {1}, err code:{2}".format(port, pid, isKilled))


class Solution(object):
    def restoreIpAddresses(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        res = []
        self.dfs(s, 0, 0, '', res)
        return res

    def dfs(self, s, beg, part, mid_res, res):
        # 每个 ip 地址由四部分组成
        if part == 4:
            # 已经有 part 0, 1, 2, 3 四部分了
            if beg == len(s):
                res.append(mid_res[:-1])
            return

        # 每部分最多 3 个字符
        for end in range(beg, beg+4):
            ip = s[beg:end+1]

            # 判断当前 ip 合不合法，以下两种情况不合法
            # 1. ip 长度大于 1，且第一个字符为 0
            # 2. ip 大于 255
            if not ((len(ip) > 1 and ip[0] == '0') or (len(ip) and int(ip) > 255)):
                # 进行下一部分的判断
                self.dfs(s, end+1, part+1, mid_res+ip+".", res)


import os
from configparser import ConfigParser

import GPUtil

from app.utilities.Config import Config

class Actions:
    conf: Config = None
    DSConfig = None
    IMConfig = None
    MDConfig = None

    def __init__(self, config_file: str):
        if not os.path.isfile(config_file):
            raise FileExistsError(f"Configuration file {config_file} not found")
        cp = ConfigParser()
        cp.read(config_file)
        self.config_file = config_file
        self.conf = Config(cp=cp)
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # Suppress debug message and warnings

        self.parse_config()
        self.check_gpu()

    def check_gpu(self):
        if self.conf.gpu != 0:
            print(f"** Use assigned numbers of gpu ({self.conf.gpu}) only")
            CUDA_VISIBLE_DEVICES = ",".join([str(i) for i in range(self.conf.gpu)])
        else:
            try:
                gpus = len(GPUtil.getGPUs())
            except ValueError:
                gpus = 1
            print(f"** Use all gpus = ({gpus})")
            CUDA_VISIBLE_DEVICES = ",".join([str(i) for i in range(gpus)])
        os.environ["CUDA_VISIBLE_DEVICES"] = f"{CUDA_VISIBLE_DEVICES}"

    def parse_config(self):
        self.DSConfig = self.conf.DatasetConfig
        self.IMConfig = self.conf.ImageConfig
        self.MDConfig = self.conf.ModelConfig


import sys
line = sys.stdin.readline().strip()
d = {}
for c in line:
    if c not in d:
        d[c] = 1
    else:
        d[c] += 1

count = 0
for key in d:
    if d[key] % 2 != 0:
        count += 1
if count != 0:
    print(count - 1)
else:
    print(0)