import glob
# blank input files go here
filename = input("Enter input filename: ")
fragments = input("Enter fragment filename: ")
filenames = glob.glob(filename)
for sdfs in filenames:
    data = ''
    with open(sdfs, 'r+') as f:
        right_file = False
        for line in f:
            if line.split('=')[0] == 'at_lists':
                with open(fragments, 'r') as f2:
                    lists = ''
                    for line2 in f2:
                        # xyz filenames printed in fragments file must equal input filenames
                        if line2.split('.')[0] == sdfs.split('.')[0]:
                            right_file = True
                        elif line2.split('.')[-1] == 'xyz\n':
                            right_file = False
                        elif right_file == True:
                            lists = lists + line2
                    if lists:
                        newline = "at_lists="+lists
                        data = data + newline
                    continue
            data = data + line
    with open(sdfs, 'w+') as f:
        f.write(data)


import time
import matplotlib.pyplot as plt
import numpy as np
from mpmath import mp

# Algorithm 1: Chudnovsky Algorithm
def chudnovsky_algorithm(n):
    mp.dps = n + 1
    C = 426880 * mp.sqrt(10005)
    M = 1
    L = 13591409
    X = 1
    K = 6
    S = L
    for _ in range(1, n):
        M = (K ** 3 - 16 * K) * M // K ** 3
        L += 545140134
        X *= -262537412640768000
        S += (M * L) // X
        K += 12
    return C / S

# Algorithm 2: Leibniz Formula
def leibniz_formula(n):
    pi = 0
    for i in range(n):
        pi += ((-1) ** i) / (2 * i + 1)
    return 4 * pi

# Algorithm 3: Bailey–Borwein–Plouffe (BBP) Formula
def bbp_formula(n):
    mp.dps = n + 1
    pi = 0
    for k in range(n):
        pi += (1 / (16 ** k)) * ((4 / (8 * k + 1)) - (2 / (8 * k + 4)) - (1 / (8 * k + 5)) - (1 / (8 * k + 6)))
    return pi

def analyze_algorithms(n):
    algorithms = [chudnovsky_algorithm, leibniz_formula, bbp_formula]
    timings = []
    for algorithm in algorithms:
        start_time = time.time()
        pi = algorithm(n)
        end_time = time.time()
        timings.append(end_time - start_time)
        print(f"{algorithm.__name__}: {pi}, time: {end_time - start_time:.6f} seconds")

    plt.bar(np.arange(len(algorithms)), timings, align='center', alpha=0.5)
    plt.xticks(np.arange(len(algorithms)), [algorithm.__name__ for algorithm in algorithms])
    plt.ylabel('Execution Time (seconds)')
    plt.title('Performance Comparison of Pi Calculation Algorithms')
    plt.show()

if __name__ == '__main__':
    n = 100000  # Specify the number of decimal places
    analyze_algorithms(n)


# coding=utf-8

'''
Given an array where elements are sorted in ascending order,
convert it to a height balanced BST.
'''

'''
还是分治思路，每次找数组的中间点，然后左右两部分分开递归向下
Beat 87.13%
'''

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if not nums: return None
        st, ed = 0 , len(nums) - 1
        mid = (st + ed) / 2
        root = TreeNode(nums[mid])
        root.left = self.sortedArrayToBST(nums[:mid])
        root.right = self.sortedArrayToBST(nums[mid + 1:])
        return root

Leetcode/Divide and Conquer/#108-Convert Sorted Array to Binary Search Tree/main.py


from functools import wraps
from typing import List

import pandas as pd

from nlp_code.preprocessing import Article, Word
from nlp_code.sentiment_lexicon import JOBLexicon

senti_lexicon = JOBLexicon.load("data/JOB_1.0/Slovene_sentiment_lexicon_JOB.txt")

def feature(feature_name=None):
    """
    This decorator makes the wrapped function return a dictionary {feature_name: return_value}
    """

    def wrapper(func):
        func.__name__ = feature_name or func.__name__

        @wraps(func)
        def aux(*args, **kwargs):
            return {func.__name__: func(*args, **kwargs)}

        return aux

    return wrapper

class FeaturePipeline:

    def __init__(self, *extractors):
        """
        Each function must take arguments (Article, Word, features), and return a dictironary of extracted features
        of form {feature_name: feature_value}.
        Features are currently calculated features for this word.

        These functions are called on each of the words passed into the __call__ function and
        collected into a Pandas DataFrame.

        For example:
        ```
        ...
        pipeline = FeaturePipeline(
            word_n_lemma(1), word_n_pos(1), word_n_word_sentiment(1),
            word_n_lemma(2), word_n_pos(2), word_n_word_sentiment(2),
            word_n_lemma(3), word_n_pos(3), word_n_word_sentiment(3),
        )

        df = pipeline(article, article.coreference_chains[chain_id])
        ...
        ```
        """
        self.extractors = extractors

    def __call__(self, article: Article, words: List[Word]) -> pd.DataFrame:
        """
        Returns a Pandas DataFrame with lines representing extracted features on each of the words
        provided in list 'words'.
        Features are extracted using self.extractors
        """
        records = []
        for word in words:
            rec = {}
            for f in self.extractors:
                rec.update(f(article, word, rec))
            records.append(rec)

        return pd.DataFrame.from_records(records)

"""
FEATURE EXTRACTORS
"""

def word_n_lemma(n: int):
    @feature(f"word_{n}_lemma")
    def aux(article: Article, word: Word, features: dict):
        if 0 <= word.word_index + n < article.num_words:
            return article.word_at(word.word_index + n).lemma
        else:
            return None

    return aux

def word_n_pos(n: int):
    @feature(f"word_{n}_pos")
    def aux(article: Article, word: Word, features: dict):
        if 0 <= word.word_index + n < article.num_words:
            return article.word_at(word.word_index + n).pos_tag
        else:
            return None

    return aux

def word_n_word_sentiment(n: int):
    @feature(f"word_{n}_word_sentiment")
    def aux(article: Article, word: Word, features: dict):
        if 0 <= word.word_index + n < article.num_words:
            return article.word_at(word.word_index + n).word_sentiment
        else:
            return None

    return aux

@feature()
def entity_type(article: Article, word: Word, features: dict):
    l = list(word.entity_types)
    return l[0] if len(l) > 0 else None

@feature()
def sentence_pos_count(article: Article, word: Word, features: dict):
    num = 0
    for w in article.iter_sentence(word.sentence_index):
        if w.word_sentiment is not None and len(w.chain_ids) == 0 and w.word_sentiment > 0:
            num += 1
    return num

@feature()
def sentence_neg_count(article: Article, word: Word, features: dict):
    num = 0
    for w in article.iter_sentence(word.sentence_index):
        if w.word_sentiment is not None and len(w.chain_ids) == 0 and w.word_sentiment < 0:
            num += 1
    return num

@feature()
def sentence_pos_neg(article, word, features):
    return (features["sentence_pos_count"] + 1) / (features["sentence_neg_count"] + 1)

@feature()
def sentence_neg_entities(article, word, features):
    """
    Number of surrounding entitiy references with negative sentiment(< 3) in a sentence
    """
    num = set()
    for w in article.iter_sentence(word.sentence_index):
        if len(w.chain_ids) > 0:
            for id in w.chain_ids:
                if (id not in word.chain_ids) and article.chain_sentiments[id] != None and article.chain_sentiments[id] < 3:
                    num.add(id)
    return len(num)

@feature()
def sentence_pos_entities(article, word, features):
    """
    Number of surrounding entitiy references with positive sentiment(> 3) in a sentence
    """
    num = set()
    for w in article.iter_sentence(word.sentence_index):
        if len(w.chain_ids) > 0:
            for id in w.chain_ids:
                if (id not in word.chain_ids) and article.chain_sentiments[id] != None and article.chain_sentiments[id] > 3:
                    num.add(id)
    return len(num)

@feature()
def entity_references_sentiment(article, word, features):
    """
    returns pos_count/neg_count of all reference words for an entity
    """
    pos_count = 0
    neg_count = 0
    for id in word.chain_ids:
        for w in article.coreference_chains[id]:
            if w.word_raw in senti_lexicon._word_dict and senti_lexicon._word_dict[w.word_raw] > 0:
                pos_count += 1
            elif  w.word_raw in senti_lexicon._word_dict and senti_lexicon._word_dict[w.word_raw] < 0:
                neg_count += 1
    return (pos_count + 1)/(neg_count + 1)

@feature()
def sentence_sentiment(article, word, features):
    return word.sentence_sentiment


import json
import glob
from acdh_tei_pyutils.tei import TeiReader

files = glob.glob("./mets/*/*_mets.xml")
data = {}
tr_url = "https://files.transkribus.eu/Get?id="
tr_param = "&fileType=view"

for x in files:
    doc = TeiReader(x)
    doc_id = doc.tree.xpath(".//title[1]/text()")[0].replace("grocerist-", "")
    print(doc_id)
    images = doc.tree.xpath(
        ".//mets:fileGrp[@ID='IMG']//mets:FLocat/@xlink:href",
        namespaces={
            "mets": "http://www.loc.gov/METS/",
            "xlink": "http://www.w3.org/1999/xlink",
        },
    )
    images = [i.replace(tr_url, "").replace(tr_param, "") for i in images]
    data[doc_id] = images

with open("data.json", "w", encoding="utf-8") as fp:
    json.dump(data, fp, ensure_ascii=False, indent=2)


from .base_scannable import ScannableParserBase
from ..scanner.whois_cira_ca import WhoisCiraCaScanner
from ..utils import array_wrapper
from ..record import Nameserver, Contact, Registrar
from ..exceptions import ParserError
from dateutil import parser as time_parser
import re

class WhoisCiraCaParser(ScannableParserBase):

    _scanner = WhoisCiraCaScanner

    @property
    def disclaimer(self):
        return self.node("field:disclaimer")

    @property
    def domain(self):
        return self.node("Domain name")

    @property
    def status(self):
        match = re.search("Domain status:\s+(.+?)\n", self.content_for_scanner)
        if match:
            status = match.group(1)
            if status in ['registered', 'redemption', 'auto-renew grace', 'to be released', 'pending delete']:
                return 'registered'
            elif status == 'available':
                return 'available'
            elif status == 'unavailable':
                return 'invalid'
            else:
                raise ParserError("Unknown status '%s'" % status)
        else:
            raise ParserError("Unable to parse status")

    @property
    def available(self):
        return self.status == 'available'

    @property
    def registered(self):
        return not self.available

    @property
    def created_on(self):
        if self.node("Creation date"):
            return time_parser.parse(self.node("Creation date"))

    @property
    def updated_on(self):
        if self.node("Updated date"):
            return time_parser.parse(self.node("Updated date"))

    @property
    def expires_on(self):
        if self.node("Expiry date"):
            return time_parser.parse(self.node("Expiry date"))

    @property
    def registrar(self):
        if self.node("Registrar"):
            return Registrar(**{
                'id': self.node("Registrar")["Number"],
                'name': self.node("Registrar")["Name"],
                'organization': self.node("Registrar")["Name"],
            })

    @property
    def registrant_contacts(self):
        return self._build_contact("Registrant", Contact.TYPE_REGISTRANT)

    @property
    def admin_contacts(self):
        return self._build_contact("Administrative contact", Contact.TYPE_ADMINISTRATIVE)

    @property
    def technical_contacts(self):
        return self._build_contact("Technical contact", Contact.TYPE_TECHNICAL)

    # Nameservers are listed in the following formats:
    #
    #   ns1.google.com
    #   ns2.google.com
    #
    #   ns1.google.com  216.239.32.10
    #   ns2.google.com  216.239.34.10
    #
    @property
    def nameservers(self):
        nameservers = []
        for nameserver in array_wrapper(self.node("field:nameservers")):
            _tmp = re.split("/\s+", nameserver)
            if len(_tmp) == 1:
                name = _tmp[0]
                ipv4 = None
            else:
                name, ipv4 = _tmp
            nameservers.append(Nameserver(name = name, ipv4 = ipv4))
        return nameservers

    # Attempts to detect and returns the version.
    #
    # TODO: This is very empiric.
    #       Use the available status in combination with the creation date label.
    #
    # NEWPROPERTY
    @property
    def version(self):
        match = re.search("^% \(c\) (.+?) Canadian Internet Registration Authority", self.content_for_scanner)
        if match:
            if match.group(1) == '2007':
                return '1'
            elif match.group(1) == '2010':
                return '2'
            else:
                raise ParserError("Unable to detect version.")
        else:
            raise ParserError("Unable to detect version.")

    @property
    def invalid(self):
        return self.status == 'invalid'

    @property
    def valid(self):
        return not self.invalid

    def _build_contact(self, element, type_):
        if self.node(element):
            return Contact(**{
                'type'         : type_,
                'id'           : None,
                'name'         : self.node(element).get("Name"),
                'organization' : None,
                'address'      : self.node(element).get("Postal address"),
                'city'         : None,
                'zip'          : None,
                'state'        : None,
                'country'      : None,
                'phone'        : self.node(element).get("Phone"),
                'fax'          : self.node(element).get("Fax"),
                'email'        : self.node(element).get("Email")
            })


from Board import *
from lsm6ds33 import LSM6DS33
import time
import os
import data_function

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pin1 = 17 # pin 11 on the RP board
#pin2 = 27
GPIO.setup(pin1, GPIO.OUT)
#GPIO.setup(pin2, GPIO.OUT)

board = Board()

lsm6ds33 = LSM6DS33()
lsm6ds33.enable()

# show ready
board.output(150)
time.sleep(1)
board.output(200)
time.sleep(1)

# create folder to store data
timestr = time.strftime("%Y%m%d-%H%M%S")
path = '/home/pi/Desktop/Data/'+timestr+'/'
if not os.path.exists(path):
    os.makedirs(path)

while (1):
##    print("%s: control:%d light:%d temp:%d custom:%d" % (time.asctime(),
##                                                         board.control(),
##                                                         board.light(),
##                                                         board.temperature(),
##                                                         board.custom()))
    value = board.custom()
    accel_g_force_L = lsm6ds33.get_accelerometer_g_forces()

    print("%s: force_sensor:%d" % (time.asctime(), value))
    #print(type(value))
    print("Accel_g_force:" , accel_g_force_L)
    #print(type(accel_g_force_L))
    float_list = accel_g_force_L
    float_list = data_function.round_floatlist(float_list,3)
    float_list.append(data_function.rss_floatlist(float_list))
    float_list.append(value)
    print(float_list)
    #print("Roll_Pitch:", lsm6ds33.get_accelerometer_angles())
    #print("Accel_3_angles:", lsm6ds33.getAccelerometer3Angles())

    if (value >= 3):
        board.output(200) # turn on LED on ADDA board
        #GPIO.output(pin, (GPIO.LOW if state == 0 else GPIO.HIGH))
        GPIO.output(pin1, (GPIO.HIGH))
    #else:
        #GPIO.output(pin2, (GPIO.HIGH))
    #sleep(0.05)

    # save data
    data_function.record_data_csv(float_list, path)

    #sleep(0.01)
    GPIO.output(pin1, (GPIO.LOW))
    #GPIO.output(pin2, (GPIO.LOW))


# 문제5.
# 선택정렬(제자리 정렬 알고리즘)을 적용하는 코드를 완성하세요.
# 문제에 주어진 배열 [ 5, 9, 3, 8, 60, 20, 1 ] 를 크기 순서대로 정렬하여 다음과 같은 출력이 되도록 완성하는 문제입니다.
#
#
# <<실행결과>>
#
# Before Sort.
# 5 9 3 8 60 20 1
# After Sort.
# 60 20 9 8 5 3 1

j = [ 5, 9, 3, 8, 60, 20, 1 ]

def sss(y):
    z = 0
    for l in range(0, len(y)-2):
        for k in range(l, len(y)-1):
            if j[l] < j[k+1]:
                z = j[l]
                j[l] = j[k+1]
                j[k+1] = z
    return y

print(sss(j))


import geopandas as gpd

def addGeoData(countryData):
    url = "https://opendata.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1.geojson"
    world = gpd.read_file(url)
    world.to_file("backupWorld.geojson", driver="GeoJSON")
    # world = gpd.read_file("backupWorld.geojson")
    world = world.drop(columns=['OBJECTID'])
    world = world.rename(columns={"CNTRY_NAME": "Country"})
    combined_gdf = world.merge(countryData, on='Country', how='left').dropna()
    return combined_gdf


# date 参数是时间格式的时间， n是要计算前几个月
import datetime

def getTheMonth(date, n):
    month = date.month
    year = date.year
    for i in range(n):
        if month == 1:
            year -= 1
            month = 12
        else:
            month -= 1
    return datetime.date(year, month, 1).strftime('%Y-%m')

# 测试
date = datetime.datetime.today()         # 输出 2019-04-15
getTheMonth(date, 1).strftime("%Y-%m")   # 输出 2019-03
getTheMonth(date, 6).strftime("%Y-%m")   # 输出 2018-10


def mediapipe_landmarks(image, model):
    """
    Detects mediapipe landmarks and returns.

    Arguments:
        image: numpy.ndarray     - frame
        model: model             - for processing image

    Returns:
        image: numpy.ndarray     - frame with landmarks drawn
        landmarks: numpy.ndarray - dataset of landmarks

    """
    import mediapipe as mp
    import cv2
    import numpy as np
    import os

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image.flags.writeable = False
    results = model.process(image)
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    holistic = mp.solutions.holistic
    drawing_utils = mp.solutions.drawing_utils
    drawing_utils.draw_landmarks(image, results.left_hand_landmarks, holistic.HAND_CONNECTIONS,
                              drawing_utils.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=3),
                              drawing_utils.DrawingSpec(color=(121, 44, 250), thickness=2, circle_radius=1))
    drawing_utils.draw_landmarks(image, results.right_hand_landmarks, holistic.HAND_CONNECTIONS,
                              drawing_utils.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=3),
                              drawing_utils.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=1))

    right_hand_landmarks = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
    left_hand_landmarks = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
    pose_landmarks = np.array([[res.x, res.y, res.z, res.visibility] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(132)
    face_landmarks = np.array([[res.x, res.y, res.z] for res in results.face_landmarks.landmark]).flatten() if results.face_landmarks else np.zeros(1404)
    landmarks = np.concatenate([pose_landmarks, face_landmarks, left_hand_landmarks, right_hand_landmarks])

    return image, landmarks


# import csv
# with open('Day25_weather_data.csv') as weather_data:
#     data = csv.reader(weather_data)
#     temperature = []
#     for row in data:
#         if row[1] != 'temp':
#             temperature.append(int(row[1]))
#     print(temperature)

import pandas

# data = pandas.read_csv('Day25_weather_data.csv')
# # print(data['temp'])
# # temp_list = data['temp'].to_list()
# # average = data['temp'].mean()
# # print(round(average,2))
# # maximum_value = data['temp'].max()
# # print(maximum_value)
# # print(data.temp.max())
# # print(data[data.temp == data.temp.max()])
# monday = (data[data.day == 'Monday'])
# monday_fahrenheit = (monday.temp * 1.8) + 32
# print(round(monday_fahrenheit, 2))

data = pandas.read_csv('day_25_2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv')
grey = (data['Primary Fur Color'].value_counts()['Gray'])
red = (data['Primary Fur Color'].value_counts()['Cinnamon'])
black = (data['Primary Fur Color'].value_counts()['Black'])

data_dict = {f'Fur Color': ['grey', 'red', 'black'],
             'count': [grey, red, black]
             }
data_count = pandas.DataFrame(data_dict)
data_count.to_csv('squirrel_count.csv')


import os
from datetime import datetime

import sys

from mvapi.libs.misc import render_template
from mvapi.settings import settings

def save_error(save_to_file=True):
    timestamp = datetime.utcnow()
    exc_type, exc_value, exc_traceback = sys.exc_info()
    python_executable = sys.executable
    python_version = sys.version_info[0:3]
    python_path = sys.path
    error_timestamp = timestamp.strftime('%b %d, %Y at %H:%M:%S')
    frames = get_traceback_frames(exc_traceback)
    last_frame = frames[-1]

    error_str = render_template('mvapi/error.tmpl', locals())

    if save_to_file:
        errors_dir = settings.ERRORS_PATH
        error_loc = last_frame['filename'].rpartition('/')[-1]
        error_ts = timestamp.strftime('%Y%m%d.%H%M%S')
        filename = os.path.join(errors_dir, f'{error_loc}.{error_ts}.html')

        if not os.path.exists(errors_dir):
            os.makedirs(errors_dir, exist_ok=True)

        with open(filename, 'wt') as error_file:
            error_file.write(error_str)

    return error_str

def get_traceback_frames(traceback):
    frames = []

    while traceback is not None:
        filename = traceback.tb_frame.f_code.co_filename
        lineno = traceback.tb_lineno

        frames.append({
            'filename': filename,
            'function': traceback.tb_frame.f_code.co_name,
            'lineno': lineno,
            'file_lines': get_file_lines(filename, lineno),
            'vars': traceback.tb_frame.f_locals.items()
        })

        traceback = traceback.tb_next

    return frames

def get_file_lines(filename, lineno):
    if not os.path.exists(filename):
        return []

    with open(filename, 'rt') as file:
        lines = file.readlines()

    if not lines:
        return []

    offset_lines = 7
    start_lineno = max(1, lineno - offset_lines) - 1
    end_lineno = lineno + offset_lines

    out_lines = []
    cur_lineno = start_lineno + 1

    for line in lines[start_lineno:end_lineno]:
        out_lines.append((cur_lineno, line))
        cur_lineno += 1

    return out_lines


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from numpy import array
import csv

tf.compat.v1.disable_v2_behavior()

""" file1 = 'Student_Performance.csv'
data_set1 = []
with open(file1, mode='r', encoding = 'utf-8-sig') as file:
    reader = csv.reader(file)
    for row in reader:
        data_set1.append(row)
in_data1 = np.array(data_set1)
print(in_data1)
print(in_data1[:][0]) """

df = pd.read_csv('Student_Performance.csv')
in_data = array(df.iloc[:,:])
hours_studies = np.array(in_data[:, 0])
print(hours_studies)
performance_index = np.array(in_data[:, 5])

# Plot of Training Data
plt.scatter(hours_studies, performance_index)
plt.xlabel('hours_studies')
plt.ylabel('performance_index')
plt.title("Training Data")
plt.show()

X = tf.compat.v1.placeholder(tf.float32, shape=None)
Y = tf.compat.v1.placeholder(tf.float32, shape=None)

n = len(hours_studies)
print(n)
# khởi tạo biến w và b
W = tf.Variable(np.random.randn(), name = "W")
print(W)
b = tf.Variable(np.random.randn(), name = "b")
# thiết lập tốc độ học
learning_rate = 0.01
# số vòng lặp
training_epochs = 200
# Hàm tuyến tính
y_pred = tf.compat.v1.add(tf.multiply(X, W), b)

# Mean Squared Error Cost Function
cost = tf.compat.v1.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)

# Tối ưu bằng Gradient Descent
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Thiết lập Global Variables
init = tf.compat.v1.global_variables_initializer()
# Starting the Tensorflow Session
with tf.compat.v1.Session() as sess:

    # Initializing the Variables
    sess.run(init)

    # Iterating through all the epochs
    for epoch in range(training_epochs):

        # Feeding each data point into the optimizer using Feed Dictionary
        for (_x, _y) in zip(hours_studies, performance_index):
            sess.run(optimizer, feed_dict = {X : _x, Y : _y})

        # Displaying the result after every 50 epochs
        if (epoch + 1) % 50 == 0:
            # Calculating the cost a every epoch
            c = sess.run(cost, feed_dict = {X : hours_studies, Y : performance_index})
            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))

    training_cost = sess.run(cost, feed_dict ={X : hours_studies, Y : performance_index})
    weight = sess.run(W)
    bias = sess.run(b)
# Calculating the predictions
predictions = weight * hours_studies + bias
print("Training cost =", training_cost, "Weight =", weight, "bias =", bias, '\n')
# Plotting the Results
plt.plot(hours_studies, performance_index, 'ro', label ='Original data')
plt.plot(hours_studies, predictions, label ='Fitted line')
plt.title('Linear Regression Result')
plt.legend()
plt.show()


import random
from replit import clear
def deal_card():
  """Returns a random card from the deck."""
  cards=[11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  r_card=cards[random.randint(0, 12)]
  return r_card

def calculate_score(cards):

  if sum(cards) == 21 and len(cards) == 2 :
    return 0
  if 11 in cards and sum(cards) > 21 :
    cards.remove(11)
    cards.append(1)
  return sum(cards)

def compare(user_score, computer_score):
  if user_score == computer_score :
    return "it's a draw"
  elif computer_score == 0 :
    return "Lose, opponent has Blackjack"
  elif user_score == 0 :
    return "Win with a blackjack"
  elif user_score > 21 :
    return "You went over 21. You lose"
  elif computer_score > 21 :
    return "Opponent went over 21. You win"
  elif user_score > computer_score:
    return "You win"
  else :
    return "You lose"
def play_game() :
  user_cards=[]
  computer_cards=[]
  is_game_over = False
  user_cards.append(deal_card())
  user_cards.append(deal_card())
  computer_cards.append(deal_card())
  computer_cards.append(deal_card())
  while is_game_over == False :
    user_score =calculate_score(user_cards)
    computer_score = calculate_score(computer_cards)
    print(f"Your cards: {user_cards}, current score: {sum(user_cards)}")
    print(f"Computer's first card: {computer_cards[0]}")
    if user_score == 0 or computer_score == 0 or user_score > 21 :
      is_game_over = True
    else:
      anser=input("Do you want to draw another card? Tipe 'y' or 'n' ")
      if anser == "y":
        user_cards.append(deal_card())
      else:
        is_game_over = True
  while computer_score < 17 and computer_score != 0:
    computer_cards.append(deal_card())
    computer_score = calculate_score(computer_cards)
  print(f"   You'r final hand: {user_cards}, final score: {user_score}")
  print(f"   Computer's final hand: {computer_cards}, final score: {computer_score}")
  print(compare(user_score, computer_score))
while input("\nDo you want to play a game of Blackjack? Type 'y' or 'n': ") == "y":
  clear()
  play_game()


import sys
import xml.etree.ElementTree as ET

def main(inFile, outFile, eFilter=100):
	# load in xml
	tree = ET.parse(inFile)
	root = tree.getroot()

	with open(outFile,'w') as outf:
		# loop over HSPs in the xml
		for hit in range(len(root[8][0][4])):
			matchName = root[8][0][4][hit][2].text
			for hsp in range(len(root[8][0][4][hit][5])):
				#implementation of evalue filter here
				if float(root[8][0][4][hit][5][hsp][3].text) <= eFilter:
					outf.write('>query ' + str(hit+1) + '.' + str(hsp+1) + '\n')
					outf.write(root[8][0][4][hit][5][hsp][14].text + '\n')
					outf.write('>' + matchName + '\n')
					outf.write(root[8][0][4][hit][5][hsp][15].text + '\n')

if __name__ == '__main__':

	if len(sys.argv) == 3:
		main(sys.argv[1],sys.argv[2])
	elif len(sys.argv) == 4:
		main(sys.argv[1],sys.argv[2],float(sys.argv[3]))
	else:
		sys.exit('USAGE: python parseBlastXML.py input_XML output_fasta [e-value filter] \n \
			alignments with e-values greater than the specified value are removed if \n \
			that option is specified')


import numpy as np
import matplotlib
import matplotlib.pyplot as plt

LEN = 40
x = (np.random.rand(LEN,2) - 0.5) * 10
line = np.random.rand(2,1)
label = np.sign(np.dot(x, line))
label[label == 0] = 1

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
idx_0 = np.where(label[:, 0] == -1)

# error label point
#label[list(idx_0[0])[0], 0] = 1
#idx_0 = np.where(label[:, 0] == -1)

p0 = ax.scatter(x[idx_0,1], x[idx_0,0], marker = '+', color = 'c', label='-1', s = 50)
idx_1 = np.where(label[:, 0] == 1)
p1 = ax.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 50)
plt.title('PLA')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim((-6,6))
plt.ylim((-6,6))
plt.legend()
plt.grid(True)
#use ion to show onetime and disapear
plt.ion()
plt.show()

cnt = 0
pla = np.zeros((2,1))
_update = 0

while cnt<LEN:
    _update = np.sign(np.dot(x[cnt,:], pla)) * label[cnt, 0]
    if _update <= 0:
        _x = x[cnt, :]
        _x.shape = (1,2)
        _x = np.transpose(_x)
        pla = pla + _x*label[cnt, 0]
        cnt = 0
    else:
        cnt = cnt+1
        continue
    try:
        ax.lines.remove(lines[0])
    except Exception:
        pass
    if pla[0, :] == 0:
        line_x = 0
        line_y = np.linspace(-5,5,50)
        lines = ax.plot(line_x, line_y, 'r-', lw=2)
    else:
        k = -pla[1,:]/pla[0,:]
        line_x = np.linspace(-5,5,50)
        line_y = k*line_x
        lines = ax.plot(line_x, line_y, 'r-', lw=2)
    plt.pause(0.5)
plt.ioff()
plt.show()
print(pla)


import pathlib

from enum import Enum

import streamlit as st
from streamlit_ace import st_ace, THEMES

from pollination_streamlit_io import (
    get_geometry,
    send_geometry,
    get_hbjson,
    send_hbjson,
    auth_user,
    select_account,
    select_project,
    select_recipe,
    select_run,
    select_study,
    select_cloud_artifact,
    create_study,
    recipe_inputs_form,
    send_results )

# needed for get_and_show_model.py
import requests

# needed for get_and_show_model.py
import json
from honeybee.model import Model as HBModel
from honeybee_vtk.model import Model as VTKModel
from pollination_streamlit_viewer import viewer

st.set_page_config(
    page_title='Tutorial App',
    page_icon='https://app.pollination.cloud/favicon.ico',
    initial_sidebar_state='collapsed',
)

from pollination_streamlit.selectors import get_api_client

# needed for get_and_show_model.py
# set_page_config needs to be called prior to this import
from templates.get_show_model import create_vtkjs, show_model

st.sidebar.image(
    'https://uploads-ssl.webflow.com/6035339e9bb6445b8e5f77d7/616da00b76225ec0e4d975ba'
    '_pollination_brandmark-p-500.png',
    use_column_width=True
)

# get api_client from pollination
api_client = get_api_client()

TEMPLATES = './templates'

class Command(Enum):
    GET_MODEL = 'Get Model'
    SEND_MODEL = 'Send Model'
    SEND_MODEL_FROM_HB = 'Send Model From HB'
    GET_GEOMETRY = 'Get Geometry'
    SEND_GEOMETRY = 'Send Geometry'
    SEND_GEOMETRY_FROM_LB = 'Send Geometry From LB'
    SEND_GEOMETRY_FROM_DICT = 'Send Geometry From Dict'
    SEND_RESULTS = 'Send Results'
    AUTH_USER = 'Authenticated User'
    SEL_ACCOUNT = 'Select Account'
    SEL_PROJECT = 'Select Project'
    SEL_RECIPE = 'Select Recipe'
    SEL_STUDY = 'Select Study'
    SEL_RUN = 'Select Run'
    RUN_STUDY_BASIC = 'Create Study'
    RUN_STUDY_PROGRESS = 'Study Progress'
    RUN_STUDY_INTERMEDIATE = 'Create Study by Recipe Form'
    RUN_STUDY_ADVANCED = 'Create Study by Recipe Form (Advanced)'
    DOWNLOAD_ARTIFACT = 'Select and Download an Artifact'
    GET_SHOW_RESULTS = 'Get and Show a Study Result (VSF)'
    GET_SHOW_MODEL = 'Get and Show a Hbjson Model'

SCRIPTS = {
    Command.GET_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('get_model.py').read_text(),
    Command.SEND_MODEL_FROM_HB.value: pathlib.Path(TEMPLATES)
        .joinpath('send_model_from_hb.py').read_text(),
    Command.SEND_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('send_model.py').read_text(),
    Command.GET_GEOMETRY.value: pathlib.Path(TEMPLATES)
        .joinpath('get_geometry.py').read_text(),
    Command.SEND_GEOMETRY.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry.py').read_text(),
    Command.SEND_GEOMETRY_FROM_LB.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry_from_lb.py').read_text(),
    Command.SEND_GEOMETRY_FROM_DICT.value: pathlib.Path(TEMPLATES)
        .joinpath('send_geometry_from_dict.py').read_text(),
    Command.SEND_RESULTS.value: pathlib.Path(TEMPLATES)
        .joinpath('send_results.py').read_text(),
    Command.AUTH_USER.value: pathlib.Path(TEMPLATES)
        .joinpath('auth_user.py').read_text(),
    Command.SEL_ACCOUNT.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_account.py').read_text(),
    Command.SEL_PROJECT.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_project.py').read_text(),
    Command.SEL_RECIPE.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_recipe.py').read_text(),
    Command.SEL_STUDY.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_study.py').read_text(),
    Command.SEL_RUN.value: pathlib.Path(TEMPLATES)
        .joinpath('sel_run.py').read_text(),
    Command.RUN_STUDY_BASIC.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_basic.py').read_text(),
    Command.RUN_STUDY_PROGRESS.value: pathlib.Path(TEMPLATES)
        .joinpath('create_study_progress.py').read_text(),
    Command.RUN_STUDY_INTERMEDIATE.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_intermediate.py').read_text(),
    Command.RUN_STUDY_ADVANCED.value: pathlib.Path(TEMPLATES)
        .joinpath('run_study_advanced.py').read_text(),
    Command.DOWNLOAD_ARTIFACT.value: pathlib.Path(TEMPLATES)
        .joinpath('download_artifact.py').read_text(),
    Command.GET_SHOW_RESULTS.value: pathlib.Path(TEMPLATES)
        .joinpath('visualize_study_results.py').read_text(),
    Command.GET_SHOW_MODEL.value: pathlib.Path(TEMPLATES)
        .joinpath('get_show_model.py').read_text()
}

DOCS = {
    Command.GET_MODEL.value: get_hbjson.__doc__,
    Command.SEND_MODEL.value: send_hbjson.__doc__,
    Command.SEND_MODEL_FROM_HB.value: send_hbjson.__doc__,
    Command.GET_GEOMETRY.value: get_geometry.__doc__,
    Command.SEND_GEOMETRY.value: send_geometry.__doc__,
    Command.SEND_GEOMETRY_FROM_LB.value: get_geometry.__doc__,
    Command.SEND_GEOMETRY_FROM_DICT.value: send_geometry.__doc__,
    Command.SEND_RESULTS.value: send_results.__doc__,
    Command.AUTH_USER.value: auth_user.__doc__,
    Command.SEL_ACCOUNT.value: select_account.__doc__,
    Command.SEL_PROJECT.value: select_project.__doc__,
    Command.SEL_RECIPE.value: select_recipe.__doc__,
    Command.SEL_STUDY.value: select_study.__doc__,
    Command.RUN_STUDY_BASIC.value: create_study.__doc__,
    Command.RUN_STUDY_PROGRESS.value: create_study.__doc__,
    Command.RUN_STUDY_INTERMEDIATE.value: recipe_inputs_form.__doc__,
    Command.RUN_STUDY_ADVANCED.value: recipe_inputs_form.__doc__,
    Command.SEL_RUN.value: select_run.__doc__,
    Command.DOWNLOAD_ARTIFACT.value: select_cloud_artifact.__doc__,
    Command.GET_SHOW_RESULTS.value: viewer.__doc__,
    Command.GET_SHOW_MODEL.value: viewer.__doc__
}

def main():
    """Learning doing."""

    # title
    st.header('Select a tutorial!')

    # set tabs
    tab1, tab2, tab3 = st.tabs(["CAD Interactions", "Cloud Interactions", "Pollination Viewer"])

    # sidebar
    st.sidebar.markdown('## Editor Settings')
    st.sidebar.markdown('----------')
    auto_update = st.sidebar.checkbox(label='Auto update',
        value=True)
    font_size = st.sidebar.number_input(label='Font size',
        value=15, min_value=1, max_value=30)
    theme = st.sidebar.selectbox(
        label='Select a theme', options=THEMES, index=5)

    # tabs layout
    with tab1:
        mod_option = st.selectbox(
            'Select a script to test',
            (Command.SEND_MODEL.value,
            Command.SEND_MODEL_FROM_HB.value,
            Command.GET_MODEL.value,
            Command.SEND_GEOMETRY.value,
            Command.SEND_GEOMETRY_FROM_LB.value,
            Command.SEND_GEOMETRY_FROM_DICT.value,
            Command.GET_GEOMETRY.value,
            Command.SEND_RESULTS.value))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[mod_option])
        mod_script = st_ace(language="python",
            value=SCRIPTS[mod_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(mod_script)

    with tab2:
        sel_option = st.selectbox(
            'Select a script to test',
            (Command.AUTH_USER.value,
            Command.SEL_ACCOUNT.value,
            Command.SEL_PROJECT.value,
            Command.SEL_RECIPE.value,
            Command.SEL_STUDY.value,
            Command.SEL_RUN.value,
            Command.RUN_STUDY_BASIC.value,
            Command.RUN_STUDY_PROGRESS.value,
            Command.DOWNLOAD_ARTIFACT.value,
            Command.RUN_STUDY_INTERMEDIATE.value,
            Command.RUN_STUDY_ADVANCED.value))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[sel_option])
        sel_script = st_ace(language="python",
            value=SCRIPTS[sel_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(sel_script)

    with tab3:
        viewer_option = st.selectbox(
            'Select a script to test',
            ([Command.GET_SHOW_RESULTS.value, \
              Command.GET_SHOW_MODEL.value]))
        with st.expander(label='Docs', expanded=False):
            st.markdown(DOCS[viewer_option])
        viewer_script = st_ace(language="python",
            value=SCRIPTS[viewer_option],
            auto_update=auto_update,
            font_size=font_size,
            theme=theme)
        exec(viewer_script)

if __name__ == '__main__':
    main()


# !\usr\bin\python3
# _*_ coding:utf-8 _*_
"""
求数据中，元素和为特定值的元素对
"""

# method1  遍历所有元素组合的和是否满足要求
def count_t1(a, t):
    b = len(a)
    for i in range(b):
        for j in range(i+1, b):
            if a[i] + a[j] == t:
                print(a[i], a[j])

# method2  对于每个元素a[i]，遍历t-a[i]是否在数组中,
# 通过对数组排序，然后二分查找，从而提高程序效率。
def count_t2(a, t):
    b = len(a)
    for i in range(b):
        for j in range(i+1, b):
            if t - a[i] == a[j]:
                print(a[i], a[j])

count_t1([5, 6, 1, 4, 7, 9, 8], 10)
count_t2([5, 6, 1, 4, 7, 9, 8], 10)

元素和为特定值的元素对.py

元素和为特定值的元素对.py


import typing
from decimal import Decimal
from datetime import datetime

import pydantic
from pyrsistent import pmap
from typing_extensions import Literal, TypedDict

from noobit_markets.base.request import (
    # retry_request,
    _validate_data,
)

# Base
from noobit_markets.base import ntypes
from noobit_markets.base.models.result import Result, Err
from noobit_markets.base.models.rest.response import (
    NoobitResponseSymbols,
    NoobitResponseTrades,
    T_PublicTradesParsedItem,
    T_PublicTradesParsedRes,
)
from noobit_markets.base.models.rest.request import NoobitRequestTrades
from noobit_markets.base.models.frozenbase import FrozenBaseModel

# FTX
from noobit_markets.exchanges.ftx import endpoints
from noobit_markets.exchanges.ftx.rest.base import get_result_content_from_req

__all__ = "get_trades_ftx"

# ============================================================
# FTX REQUEST
# ============================================================

class FtxLimit(ntypes.NInt):
    ge = 0
    le = 100
    strict = False

class FtxRequestTrades(FrozenBaseModel):
    # https://docs.ftx.com/?python#get-trades

    market_name: str
    limit: typing.Optional[FtxLimit]
    start_time: typing.Optional[pydantic.PositiveInt]
    end_time: typing.Optional[pydantic.PositiveInt]

# hint fields to mypy
class _ParsedReq(TypedDict):
    market_name: typing.Any
    limit: typing.Any
    start_time: typing.Any
    end_time: typing.Any

def parse_request(
    valid_request: NoobitRequestTrades,
) -> _ParsedReq:

    payload: _ParsedReq = {
        "market_name": valid_request.symbols_resp.asset_pairs.get(
            valid_request.symbol
        ).exchange_pair,
        "limit": 100,
        "start_time": valid_request.since,
        "end_time": None
        # noobit ts are in ms vs ohlc kraken ts in s
    }

    return payload

# ============================================================
# FTX RESPONSE
# ============================================================

# SAMPLE RESPONSE
# {
#   "success": true,
#   "result": [
#     {
#       "id": 3855995,
#       "liquidation": false,
#       "price": 3857.75,
#       "side": "buy",
#       "size": 0.111,
#       "time": "2019-03-20T18:16:23.397991+00:00"
#     }
#   ]
# }

class FtxResponseItemTrades(FrozenBaseModel):

    id: pydantic.PositiveInt
    liquidation: bool
    price: Decimal
    side: Literal["buy", "sell"]
    size: Decimal
    time: str

class FtxResponseTrades(FrozenBaseModel):

    trades: typing.Tuple[FtxResponseItemTrades, ...]

def parse_result(
    result_data: FtxResponseTrades, symbol: ntypes.SYMBOL
) -> T_PublicTradesParsedRes:

    parsed_trades = [_single_trade(data, symbol) for data in result_data.trades]

    return tuple(parsed_trades)

def _single_trade(
    data: FtxResponseItemTrades, symbol: ntypes.SYMBOL
) -> T_PublicTradesParsedItem:

    parsed: T_PublicTradesParsedItem = {
        "symbol": symbol,
        "orderID": None,
        "trdMatchID": data.id,
        # noobit timestamp = ms
        "transactTime": datetime.timestamp(datetime.fromisoformat(data.time)),
        "side": data.side.upper(),
        # binance only lists market order
        # => trade = limit order lifted from book by market order
        # FIXME change model to allow None
        "ordType": "MARKET",
        "avgPx": data.price,
        "cumQty": data.size,
        "grossTradeAmt": data.price * data.size,
        "text": None,
    }

    return parsed

# ============================================================
# FETCH
# ============================================================

# @retry_request(retries=pydantic.PositiveInt(10), logger=lambda *args: print("===xxxxx>>>> : ", *args))
async def get_trades_ftx(
    client: ntypes.CLIENT,
    symbol: ntypes.SYMBOL,
    symbols_resp: NoobitResponseSymbols,
    since: typing.Optional[ntypes.TIMESTAMP] = None,
    #  prevent unintentional passing of following args
    *,
    logger: typing.Optional[typing.Callable] = None,
    base_url: pydantic.AnyHttpUrl = endpoints.FTX_ENDPOINTS.public.url,
    endpoint: str = endpoints.FTX_ENDPOINTS.public.endpoints.trades,
) -> Result[NoobitResponseTrades, pydantic.ValidationError]:

    symbol_to_exchange = lambda x: {
        k: v.exchange_pair for k, v in symbols_resp.asset_pairs.items()
    }[x]

    # ftx has variable urls besides query params
    # format: https://ftx.com/api/markets/{market_name}/candles
    req_url = "/".join([base_url, "markets", symbol_to_exchange(symbol), endpoint])
    method = "GET"
    headers: typing.Dict = {}

    valid_noobit_req = _validate_data(
        NoobitRequestTrades,
        pmap({"symbol": symbol, "symbols_resp": symbols_resp, "since": since}),
    )
    if isinstance(valid_noobit_req, Err):
        return valid_noobit_req

    if logger:
        logger(f"Trades - Noobit Request : {valid_noobit_req.value}")

    parsed_req = parse_request(valid_noobit_req.value)

    valid_ftx_req = _validate_data(FtxRequestTrades, pmap(parsed_req))
    if valid_ftx_req.is_err():
        return valid_ftx_req

    if logger:
        logger(f"Trades - Parsed Request : {valid_ftx_req.value}")

    result_content = await get_result_content_from_req(
        client, "GET", req_url, valid_ftx_req.value, headers
    )
    if result_content.is_err():
        return result_content

    if logger:
        logger(f"Trades - Result Content : {result_content.value}")

    valid_result_content = _validate_data(
        FtxResponseTrades, pmap({"trades": result_content.value})
    )
    if valid_result_content.is_err():
        return valid_result_content

    parsed_result = parse_result(valid_result_content.value, symbol)

    valid_parsed_response_data = _validate_data(
        NoobitResponseTrades,
        pmap(
            {
                "trades": parsed_result,
                "rawJson": result_content.value,
                "exchange": "FTX",
            }
        ),
    )
    return valid_parsed_response_data


import os
from datetime import datetime
from pathlib import Path
import json

def create_log_file_name(coin):
    logdir = Path.cwd() / Path('logs')
    if not logdir.exists():
        logdir.mkdir()

    if logdir.exists() and logdir.is_dir():
        date_format = datetime.now().strftime('%Y_%m_%d-%H%M%S')
        filename = f'{date_format}_{coin}.log'
        log_file = logdir / filename
    return log_file

def write_log_header(file, coin):
    with open(file, 'a', encoding='UTF-8') as logfile:
        line_1 = f'========== {datetime.now().strftime("%d.%m.%Y %H:%M:%S")} =========\n'
        line_2 = f'============== {coin} ==============\n'
        logfile.write(line_1)
        logfile.write(line_2)

def write_buy_receipt(file, data):

    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write(f'Куплена монета {data["symbol"]}\n')
        if len(data['fills']) > 0:
            fills = data['fills'][0]
            logfile.write(f'Стоимость: {fills["price"]}\n')
            logfile.write(f'Количество: {fills["qty"]}\n')
        else:
            logfile.write('Пустой массив fills\n')

def write_cell_receipt(file, data):
    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write('Продажа монеты\n')

        if len(data['fills']) > 0:
            for row in data['fills']:
                logfile.write('Раздел квитка\n')
                logfile.write(f'Стоимость продажи: {row["price"]}\n')
                logfile.write(f'Продано количесво: {row["qty"]}\n')
        else:
            logfile.write('Опять пустой массив fills\n')

def write_error(file, error):
    with open(file, 'a', encoding='UTF-8') as logfile:
        logfile.write('=== Произошла ошибка ===\n')
        # logfile.write(f'Статус код: {error.status_code}\n')
        # logfile.write(f'Ответ: {error.response}\n')
        # logfile.write(f'Код ошибки: {error.code}\n')
        # logfile.write(f'Описание: {error.message}\n')
        # logfile.write(f'Запрос: {error.request}\n')
        logfile.write(str(error))


from bs4 import BeautifulSoup
import requests
import re
import urllib3
import os
import pandas as pd

def ConsoleClear():
    os.system('cls' if os.name == 'nt' else 'clear')

ConsoleClear()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

game = input("What game do you want to search for?\n")

url = "https://www.newegg.com/p/pl?d=ps5+games"
page = requests.get(url, verify=False).text
doc = BeautifulSoup(page, "html.parser")

page_text = doc.find(class_="list-tool-pagination-text").strong
pages = int(str(page_text).split("/")[-2].split(">")[-1][:-1])

items_found = {}

for page in range(1, pages + 1):
    url = f"https://www.newegg.com/p/pl?d=ps5+games&page={page}"
    page = requests.get(url, verify=False).text
    doc = BeautifulSoup(page, "html.parser")
    div = doc.find(
        class_="item-cells-wrap border-cells items-grid-view four-cells expulsion-one-cell"
    )

    items = div.find_all(string=re.compile(game))
    for item in items:
        parent = item.parent
        link = None
        if parent.name != "a":
            continue

        link = parent["href"]
        next_parent = item.find_parent(class_="item-container")
        try:
            price_element = next_parent.find(class_="price-current")
            if price_element:
                price = price_element.find("strong").get_text(strip=True)
            else:
                price = "N/A"

            items_found[item] = {"price": price, "link": link}
        except:
            pass

sorted_items = sorted(items_found.items(), key=lambda x: x[1]["price"])

for item in sorted_items:
    print(item[0])
    print(f"${item[1]['price']}")
    print(item[1]["link"])
    print("----------------------------------------------")


import os
import argparse

import wandb
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.cuda.amp import GradScaler, autocast
from torch.utils.data import DataLoader, random_split
from torchvision import transforms
from tqdm import tqdm

from underwater_unet.model import UNet
from utils.data_load import UnderwaterDataset
from utils.dice_score import dice_loss

def train_model(
        model,
        device,
        epochs=10,
        batch_size=1,
        learning_rate=0.001,
        val_percent=10.0,
        save_checkpoint=True,
        weight_decay=1e-8,
        momentum=0.9,
        amp=False,  # Add AMP flag to the function arguments
        gradient_clipping=1.0
):
    # Define transform
    transform = transforms.Compose([
        transforms.ToTensor(),
        # Add any other transformations here
    ])

    # Load dataset and dataloader
    train_dataset = UnderwaterDataset(images_dir='data/images', mask_dir='data/masks', augmentations=transform)
    n_val = int(len(train_dataset) * val_percent / 100)
    n_train = len(train_dataset) - n_val
    train_set, val_set = random_split(train_dataset, [n_train, n_val])

    train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
    val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=4, pin_memory=True)

    # Initialize model, loss, optimizer, and AMP scaler
    criterion = nn.CrossEntropyLoss() if model.n_classes > 1 else nn.BCEWithLogitsLoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay, betas=(momentum, 0.999))
    scaler = GradScaler(enabled=amp)

    # Initialize learning rate scheduler
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2)

    # Initialize experimnet logging
    wandb.init(project="UW-Unet", config={
        "epochs": epochs,
        "batch_size": batch_size,
        "learning_rate": learning_rate,
        "val_percent": val_percent,
        "save_checkpoint": save_checkpoint,
        "amp": amp,
        "weight_decay": weight_decay,
        "momentum": momentum,
        "gradient_clipping": gradient_clipping,
    })

    experiment_id = f"exp_{wandb.run.id}"
    local_checkpoint_dir = os.path.join("experiment", experiment_id)
    os.makedirs(local_checkpoint_dir, exist_ok=True)

    best_val_loss = float("inf")

    # Training loop
    for epoch in tqdm(range(1, epochs + 1), desc="Epochs"):
        model.train()
        epoch_loss = 0
        # Inside the training loop
        for batch in tqdm(train_loader, desc="Training", leave=False):
            images = batch['image'].to(device=device, dtype=torch.float32, memory_format=torch.channels_last)
            masks = batch['mask'].to(device=device, dtype=torch.float32 if model.n_classes == 1 else torch.long)

            with autocast(enabled=amp):
                masks_pred = model(images)
                if model.n_classes == 1:
                    masks_pred = masks_pred.squeeze(1)  # Remove channel dim because we have a single class
                    masks = masks.squeeze(1)  # Squeeze the masks for binary case
                    loss = criterion(masks_pred, masks)
                    loss += dice_loss(torch.sigmoid(masks_pred), masks, multiclass=False)
                else:
                    # No need to squeeze masks for multi-class since CrossEntropy expects a 1D target
                    loss = criterion(masks_pred, masks)
                    loss += dice_loss(
                        F.softmax(masks_pred, dim=1),
                        F.one_hot(masks, model.n_classes).permute(0, 3, 1, 2),
                        multiclass=True
                    )
            # Backward and optimization steps remain the same

            assert masks_pred.shape == \
                masks.shape, f"Output shape {masks_pred.shape} and mask shape {masks.shape} do not match"
            # Backward pass and optimization
            optimizer.zero_grad()
            scaler.scale(loss).backward()

            # Gradient clipping
            if gradient_clipping > 0:
                scaler.unscale_(optimizer)
                torch.nn.utils.clip_grad_norm_(model.parameters(), gradient_clipping)

            scaler.step(optimizer)
            scaler.update()

            epoch_loss += loss.item()

        # Log training loss after each epoch
        train_loss = epoch_loss / len(train_loader)
        wandb.log({
            "epoch": epoch,
            "training_loss": train_loss,
        }, step=epoch)

        val_loss = 0
        model.eval()

        with torch.no_grad():
            for batch in tqdm(val_loader, desc="Validation", leave=False):
                images = batch['image'].to(device)
                masks = batch['mask'].to(device).unsqueeze(1).float()
                outputs = model(images)
                probs = torch.sigmoid(outputs)  # Apply sigmoid for dice_loss
                dice = dice_loss(probs, masks, multiclass=False)
                bce = criterion(outputs, masks)
                loss = bce + dice  # Consistent with training loss
                val_loss += loss.item()

                # Log the predicted masks and the true masks as images
                wandb.log({
                    "val_images": [wandb.Image(image.cpu(), caption="Val Image") for image in images],
                    "val_masks": [wandb.Image(mask.cpu(), caption="True Mask") for mask in masks],
                    "val_predictions": [wandb.Image(torch.sigmoid(output).cpu(),
                                                    caption="Pred Mask") for output in outputs],
                }, step=epoch)

        scheduler.step(val_loss)
        # Calculate and log the average validation loss
        val_loss_avg = val_loss / len(val_loader)

        wandb.log({
            "epoch": epoch,
            "validation_loss": val_loss_avg,
        }, step=epoch)

        # Log histograms of model parameters
        for name, param in model.named_parameters():
            if param.requires_grad:
                wandb.log({f"{name}": wandb.Histogram(param.detach().cpu().numpy())}, step=epoch)

        print(f'Epoch {epoch + 1}/{epochs}, Training Loss: {epoch_loss / len(train_loader):.4f}, \
              Validation Loss: {val_loss / len(val_loader):.4f}')

        if val_loss_avg < best_val_loss:
            best_val_loss = val_loss_avg

            # Save the checkpoint in the unique directory
            checkpoint_path = os.path.join(local_checkpoint_dir, f"model_epoch_{epoch + 1}.pth")
            torch.save(model.state_dict(), checkpoint_path)

            # Log the checkpoint as an artifact in WandB
            artifact = wandb.Artifact('model-checkpoint', type='model')
            artifact.add_file(checkpoint_path)
            wandb.log_artifact(artifact)

            # Optional: If you want to keep the checkpoint in the WandB directory structure as well
            wandb_checkpoint_dir = os.path.join(wandb.run.dir, "checkpoints")
            os.makedirs(wandb_checkpoint_dir, exist_ok=True)
            wandb_checkpoint_path = os.path.join(wandb_checkpoint_dir, f"model_epoch_{epoch + 1}.pth")
            torch.save(model.state_dict(), wandb_checkpoint_path)

    print("Training completed.")

def get_args():
    parser = argparse.ArgumentParser(description='Train the UNet on images and target masks')
    parser.add_argument('--epochs', '-e', metavar='E', type=int, default=10, help='Number of epochs')
    parser.add_argument('--batch-size', '-b', dest='batch_size', metavar='B', type=int, default=1, help='Batch size')
    parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=0.001, help='Learning rate')
    parser.add_argument('--val-percent', '-v', dest='val_percent', type=float, default=10.0,
                        help='Percent of the data that is used as validation (0-100)')
    parser.add_argument('--save-checkpoint', action='store_true', help='Save a checkpoint at the end of each epoch')
    parser.add_argument('--amp', action='store_true', help='Use automatic mixed precision for training')
    parser.add_argument('--weight-decay', type=float, default=1e-8, help='Weight decay used in optimization')
    parser.add_argument('--momentum', type=float, default=0.9, help='Momentum used in optimization')
    parser.add_argument('--n-classes', type=int, default=1, help='Number of classes for segmentation')
    parser.add_argument('--bilinear', action='store_true', help='Use bilinear upsampling')
    parser.add_argument('--gradient-clipping', type=float, default=1.0, help='Max norm of the gradients')
    # Add more arguments as required

    return parser.parse_args()

if __name__ == '__main__':
    # Parse command line arguments
    args = get_args()

    # Initialize wandb
    wandb.init(project="UW-Unet5", config=args)
    config = wandb.config

    # Set device for training
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # Initialize and transfer the U-Net model to the device
    model = UNet(n_channels=3, n_classes=args.n_classes, bilinear=args.bilinear).to(device)

    # Pass the configurations from wandb to your training function
    train_model(
        model=model,
        device=device,
        epochs=config.epochs,
        batch_size=config.batch_size,
        learning_rate=config.learning_rate,
        val_percent=config.val_percent,
        save_checkpoint=config.save_checkpoint,
        amp=config.amp,
        weight_decay=config.weight_decay,
        momentum=config.momentum,
        gradient_clipping=config.gradient_clipping
    )


import json
import requests

class TelegramBot:
    """
    Telegram bot class
    """

    def __init__(self, token: str):
        """
        Initialize the class.
        :param token: telegram bot token, given by BotFather.
        """
        self.base_url = "https://api.telegram.org/bot"
        self.token = token
        self.updates = None
        self.last_id = None

    def get_updates(self):
        """
        stores new incoming updates.
        """
        url = f"{self.base_url}{self.token}/getUpdates"
        if self.last_id:
            url = f"{url}?offset={self.last_id}"
        self.updates = requests.get(url).json()["result"]
        self.set_last_id()

    def set_last_id(self):
        """
        set the current last update_id.
        """
        if len(self.updates) > 0:
            self.last_id = self.updates[-1]["update_id"] + 1

    def send_message(self, chat_id: int, text: str):
        """
        Sends a message to a telegram user.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param text: Text of the message to be sent
        """
        url = f"{self.base_url}{self.token}/sendMessage"
        params = {"chat_id": int(chat_id), "text": text}
        requests.get(url, params=params)

    def send_video(self, chat_id: int, video):
        """
        Sends a video to a telegram user.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param video: video file to be sent
        """
        url = f"{self.base_url}{self.token}/sendVideo"
        data = {"chat_id": int(chat_id)}
        files = {"video": video}
        requests.post(url, data=data, files=files)

    def request_contact(self, chat_id: int, text: str = "Your Contact:"):
        """
        Asking the user to give back is phone number.
        :param chat_id: Unique identifier user_id / user_name / chat_id
        :param text: Text of the message to be sent
        """
        url = f"{self.base_url}{self.token}/sendMessage"
        reply_markup = {
            "keyboard": [[{"text": "Press Me", "request_contact": True}]],
            "one_time_keyboard": True,
            "resize_keyboard": True,
        }
        params = {
            "chat_id": chat_id,
            "text": text,
            "reply_markup": json.dumps(reply_markup),
        }
        requests.get(url, params=params)


# -*- coding: utf-8 -*-
"""
Created on Sun Feb 28 22:10:13 2021

@author: shrut
"""

import networkx as nx
import random
import numpy as np

def add_edges(G, p=0.3):
    for u in G.nodes():
        for v in G.nodes():
            if (u != v):
                r = random.random()
                if (r <= p):
                    G.add_edge(u, v)
                else:
                    continue
    return G

def distribute_points(G, points):
    new_points = [0 for _ in range(G.number_of_nodes())]
    for u in G.nodes():
        outlinks = G.out_edges(u)
        if len(outlinks) == 0:
            new_points[u] += points[u]

        else:
            share = points[u]/len(outlinks)
            for link in outlinks:
                new_points[link[1]] += share
    return new_points

def taxation(G, points, s=0.8):
    for i in range(len(points)):
        points[i] = points[i]*s

    n = G.number_of_nodes()
    extra = (n * 100 * (1-s)) / n
    for i in range(len(points)):
        points[i] += extra

    return points

def distribute_points_till_conv(G, points):
    print("Enter # to stop")
    while(1):
        new_points = distribute_points(G, points)

        new_points = taxation(G, new_points)
        points = new_points
        print(points)
        c = input()
        if c == '#':
            break
    return points

def sort_nodes_by_points(points):
    points = np.array(points)
    sorted_nodes = np.argsort(-points)
    return sorted_nodes

def main():
    # Create the graph
    G = nx.DiGraph()
    G.add_nodes_from(list(range(10)))
    G = add_edges(G, p=0.3)

    # Distribute points
    points = [100 for _ in range(G.number_of_nodes())]
    print(points)
    points = distribute_points_till_conv(G, points)

    # Rank the nodes
    sorted_nodes = sort_nodes_by_points(points)
    print("Nodes sorted by points: ", sorted_nodes)

    # Compare with NetworkX's imolementation
    print("Result from the networkx implementation of PageRank(for comparision):")
    pr = nx.pagerank(G)
    pr_sorted = sorted(pr.items(), key=lambda x : x[1], reverse=True)

    for i in pr_sorted:
        print(i[0], end=' ')

main()


import numpy as np
class NeuralNetwork:
    def __init__(self):
        np.random.seed(10) # for generating the same results
        self.wij   = np.random.rand(3,4) # input to hidden layer weights
        self.wjk   = np.random.rand(4,1) # hidden layer to output weights

    def sigmoid(self, x, w):
        z = np.dot(x, w)
        return 1/(1 + np.exp(-z))

    def sigmoid_derivative(self, x, w):
        return self.sigmoid(x, w) * (1 - self.sigmoid(x, w))

    def gradient_descent(self, x, y, iterations):
        for i in range(iterations):
            Xi = x
            Xj = self.sigmoid(Xi, self.wij)
            yhat = self.sigmoid(Xj, self.wjk)
            # gradients for hidden to output weights
            g_wjk = np.dot(Xj.T, (y - yhat) * self.sigmoid_derivative(Xj, self.wjk))
            # gradients for input to hidden weights
            g_wij = np.dot(Xi.T, np.dot((y - yhat) * self.sigmoid_derivative(Xj, self.wjk), self.wjk.T) * self.sigmoid_derivative(Xi, self.wij))
            # update weights
            self.wij += g_wij
            self.wjk += g_wjk
        print('The final prediction from neural network are: ')
        print(yhat)
if __name__ == '__main__':
    neural_network = NeuralNetwork()
    print('Random starting input to hidden weights: ')
    print(neural_network.wij)
    print('Random starting hidden to output weights: ')
    print(neural_network.wjk)
    X = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
    y = np.array([[0, 1, 1, 0]]).T
    neural_network.gradient_descent(X, y, 10000)


from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

class Db:
    ARG_SERVER_URI_KEY = "server_uri"
    DEFAULT_SERVER_URI = "mongodb://mongo"

    DEFAULT_CONNECT_TIMEOUT = 2000

    def __init__(self, **kwargs):
        self.server = kwargs.get(Db.ARG_SERVER_URI_KEY, Db.DEFAULT_SERVER_URI)

        self.client = MongoClient(self.server, serverSelectionTimeoutMS=Db.DEFAULT_CONNECT_TIMEOUT)

    def connect(self):
        try:
            # The ismaster command is cheap and does not require auth.
            self.client.admin.command('ismaster')
            return True
        except ConnectionFailure:
            print("Server not available")
            return False

    def create_db(self, database_name):
        db = self.client[database_name]
        mycol = db["to_delete"]
        mydict = { "name": "John", "address": "Highway 37" }

        x = mycol.insert_one(mydict)
        print ("inserted id: {}".format(x.inserted_id))

    def drop_db(self, database_name):
        self.client.drop_database(database_name)

    def get_database_names(self):
        dbs = self.client.list_database_names()
        print("list databases: {}".format(dbs))
        return dbs


from fabric.api import lcd, task, settings, hide
from fabpolish import polish, sniff, local, info
from fabpolish.contrib import find_merge_conflict_leftovers
import os
from subprocess import call

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))

# @sniff(severity='major', timing='fast')
# def run_eslint():
#     info('Running ESLint...')
#     return local(
#         "git ls-files | "
#         "grep '\.js$' | "
#         "xargs ./node_modules/eslint/bin/eslint.js  --config=.eslintrc"
#     )

@sniff(severity='major', timing='fast')
def remove_compiled_classes():
    info('Removing compiled python classes...')
    local('pyclean .')
    return local(
        'find ./federation_api ./config -name "*.py[co]" -print0 | xargs -0 rm -f'
    )

@sniff(severity='major', timing='fast')
def fix_directory_permission():
    info('Fixing directory permissions...')
    return local(
        '! find ./federation_api ./config '
        '-path .git -prune -o '
        '-name .webassets-cache -prune -o '
        '-name gen -prune -o '
        '-type d -not -perm 0775 -print0 | '
        'tee /dev/stderr | '
        'xargs -0 chmod 0775 >/dev/null 2>&1'
    )

@sniff(severity='major', timing='fast')
def fix_file_permission():
    """Fixing permissions for files"""
    info('Fixing permissions for files...')
    return local(
        "git ls-files -z | "
        "grep -PvZz '\.sh$' | "
        "grep -PvZz 'tests/' | "
        "xargs -0 chmod -c 0664 > /dev/null 2>&1"
    )

@sniff(severity='major', timing='fast')
def check_code_standard():
    """Running coding standards check"""
    info('Running coding standards check...')
    return local(
        'pep8 ./federation_api ./config'
    )

@sniff(severity='major', timing='fast')
def static_code_analyzer():
    """Running static code analyzer"""
    info('Running static code analyzer...')
    return local(
        'pyflakes ./federation_api ./config'
    )

@sniff(severity='major', timing='fast')
def check_print_statement():
    """Checking for debug print statements"""
    info('Checking for debug print statements...')
    return local(
        '! find ./federation_api ./config -type f -name "*.py" -print0 | '
        'xargs -0 grep -Pn \'(?<![Bb]lue|>>> )print\' | '
        'grep -v NOCHECK'
    )

@sniff(severity='major', timing='fast')
def servername_compatibility():
    """Checking for no-servername compatibility"""
    info('Checking for no-servername compatibility...')
    return local(
        '! find ./federation_api ./config -name "default_settings.py" -o '
        ' -type f -name "*.py" -print0 | '
        'xargs -0 grep -Pn \'SERVER_NAME|ENABLE_HTTPS|SSO_DOMAIN|'
        '(SOFTWARE(_WEBSITE)?|EPICENTER|FABRIC|PVR|PRACTONAV)_HOST\''
    )

# @sniff(severity='major', timing='fast')
# def check_migrations():
#     """Checking migration branches"""
#     info('Checking migration branches...')
#     return local(
#         '! alembic branches | grep branchpoint'
#     )

@sniff(severity='major', timing='fast')
def run_tests():
    """Running tests"""
    info('Running tests...')
    value = 'PYTHONPATH=%s FLASK_ENV=TEST py.test --cov=. ./tests' % ROOT_DIR
    return local(
        value
    )

@task
def grep(term, flags=''):
    with lcd(ROOT_DIR), settings(hide('running')):
        local('find federation_api ./config tests '
              '-name "*~" -o '
              '-name "*.py[co]" -o '
              '-name "*.dot" -o '
              '-name "*.min.*" -o '
              '-name "gen" -prune -o '
              '-name ".webassets-cache" -prune -o '
              '-type d -o '
              '-print0 | '
              'xargs -0 grep -n --color=force ' + flags + ' "' + term + '"; '
              ' echo -n')


#!/usr/bin/python
# -*- coding: utf-8 -*-
# Script chạy trên py32 (không sử dụng anaconda)

from __future__ import print_function
import collections
import numpy as np
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from svmutil import *
from sklearn.model_selection import train_test_split
import time
import CONSTANT
import util.log_util

# Hàm load dữ liệu
def LoadData (filename1, filename2):
    file_data = open(filename1,'r')
    file_labels = open(filename2,'r')
    data = file_data.read().splitlines()
    labels = file_labels.read().splitlines()
    for i in range(len(labels)):
        labels[i] = int(labels[i])
    return data, labels

# Hàm chuân hóa dữ liệu: loại stopword, lower case nó xuống

def NormalizeData (training_data):
    # Định nghĩa biến trả về
    new_training_data = training_data
    # Đọc file stopword có sẵn trong thư viện lưu vào cache
    cachedStopWords = stopwords.words("english")

    for i in range(len(training_data)):
        # Viết thường
        temp = training_data[i].lower()
        # Loại stopword
        temp = ' '.join([word for word in temp.split() if word not in cachedStopWords])
        # Gán vào biến trả về
        new_training_data[i]= temp

    return new_training_data

def Readfile(file):
    data = []
    lines = file.read().splitlines()

    for i in range(len(lines)):
        values = lines[i].split('\t')
        sentence_class = int(values[0])
        sentence_value = values[1]
        temp = [sentence_value ,sentence_class ]
        data.append(temp)
    return data

def Convert_word2Idx (raw_data):
    # Building a dictionary
    Dict = []
    for i in range(len(raw_data)):
        # Split word in each line
        words = raw_data[i][0].split(' ')
        Dict = Dict + words
        # Eliminate duplicate elements
        Dict = list(set(Dict))
    # Now we have dictionary, each word in dictionary have its index, is also the index of Dictionary
    # Here we convert training sentence into <sentence class 1 -1> <word index>:<word class - 0 1>
    # Class of each sentence
    y = []
    for i in range(len(raw_data)):
        y.append(raw_data[i][1])
    # Sentences in right format
    x = []
    for i in range(len(raw_data)):
        # Init a small dict to contain words in one sentence that appear or not
        sent_dict = {}

        words_sent = raw_data[i][0].split(' ')
        for j in range(len(words_sent)):
            if (words_sent[j] in Dict):
                index = Dict.index(words_sent[j])
                sent_dict[index] = 1
        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)

def LoadWord2VecLabels(filename):
    file = open(filename,'r')
    data= file.read().splitlines()
    for i in range(len(data)):
        data[i] = int(data[i])
    return data

def LoadWord2VecLabels_str(filename):
    file = open(filename,'r')
    data= file.read().splitlines()
    return data

def LoadWord2VecData(filename):
    data = []
    with open(filename) as f:
        for line in f:  #Line is a string
            #split the string on whitespace, return a list of numbers
            # (as strings)
            numbers_str = line.split()
            #convert numbers to floats
            numbers_float = [float(x) for x in numbers_str]  #map(float,numbers_str) works too
            data.append(numbers_float)
    return data

# Convert POS-tagged data
def Convert_word2Idx_NoPOStagged (nopostagged_data):
    # Building a dictionary
    Dict = []
    # Building a data in the appreciate form for LIBSVM
    Data = []
    for i in range(len(nopostagged_data)):
        # Split word in each line
        word = nopostagged_data[i].split(' ')
        Dict = Dict + word
        Data.append(word)
    # Eliminate duplicate elements
    Dict = list(set(Dict))
    print ("    - Complete Dictionary")
    #print Data
    print ("Number of Vocabulary: ")
    print (len(Dict))
    #Save dictionary
    file_dic = open("Dictionary.txt",'w')
    for i in range(len(Dict)):
        file_dic.write(str(Dict[i]) + "\n")
    # Sentences in right format
    x = []
    for i in range(len(Data)):
        sent_dict = {}
        # Check word
        for j in range(len(Data[i])):
            # Check if a word appeared in dictionary
            if (Data[i][j] in Dict):
                index = Dict.index(Data[i][j])
                sent_dict[index] = 1

        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)
    return x

def Convert_word2Idx_NoPOStagged_predict (nopostagged_data):
    # Reading dictionary
    file_dict = open("Dictionary.txt",'r')
    Dict = file_dict.read().splitlines()

    Data = []
    for i in range(len(nopostagged_data)):
        # Split word in each line
        word = nopostagged_data[i].split(' ')
        Data.append(word)

    # Sentences in right format
    x = []

    for i in range(len(Data)):
        sent_dict = {}
        # Check word
        for j in range(len(Data[i])):
            # Check if a word appeared in dictionary
            if (Data[i][j] in Dict):
                index = Dict.index(Data[i][j])
                sent_dict[index] = 1

        # Sort the dict and append it to data
        sort_sent_dict = collections.OrderedDict(sorted(sent_dict.items()))
        x.append(sort_sent_dict)
    return x

if __name__ == "__main__":

    # create logger
    logger = util.log_util.create_logger("SVM_aspect_classifier", print_console=True)
    print = logger.info # redirect print function to logger.info
    print ('Language %s' %(CONSTANT.LANGUAGE))

    # Define
    data_size = 0 # set data_size = 0 to train on all data

    print ("Loading Data...")
    # Load dữ liệu
    data, labels = LoadData(CONSTANT.DATASET_FOLDER_DIR+'/'+ CONSTANT.full_aspect_data_raw,
                            CONSTANT.DATASET_FOLDER_DIR+'/'+ CONSTANT.full_aspect_labels_raw)
    print (len(labels))
    if (data_size>0):
        data = data[:data_size]
        labels = labels[:data_size]

    X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.25, random_state=42)
    print("Data: %d train sample, %d test sameple" %(len(y_train), len(y_test)))

    # training_set =  data[:40] + data[38694:38734] + data[52876:52916]
    training_set =  X_train
    training_labels =  y_train

    # Make dictionary
    print ("Making Dictionary...")
    training_set = Convert_word2Idx_NoPOStagged (training_set)

    # Tạo model
    print ("Making a model")
    prob  = svm_problem(training_labels, training_set)
    param = svm_parameter('-t 0 -c 1 -b 1')
    # Train the model
    print ("Training...")
    start_training_time = time.time()
    model = svm_train(prob, param)
    # Save the model
    svm_save_model('SVM_aspect.model', model)

    # Kiểm thử, xuất file predict label, cho biết accuracy
    model = svm_load_model('SVM_aspect.model')
    test_set = Convert_word2Idx_NoPOStagged_predict(X_test)
    test_labels = y_test
    done_train_time = time.time()
    print ('Training in %f' %(done_train_time-start_training_time))
    # Predict
    print ("Predict ...")
    p_label, p_acc, p_val = svm_predict(test_labels, test_set, model, '-b 1')
    ACC, MSE, SCC = evaluations(test_labels, p_label)

    # Ghi file predict labels
    file = open('svm_aspect_predict.txt','w')
    for i in range(len(p_label)):
        file.write(str(p_label[i]) + '\n')

    # Tìm Precision, Recall và F1
    true_labels = test_labels
    predict_labels = p_label

    print (predict_labels[0:10])
    print (true_labels[0:10])
    # Food
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 1 and true_labels[i] == 1):
            true_positive += 1
        elif (predict_labels[i] == 1 and true_labels[i] != 1) :
            false_positive += 1
        elif ((predict_labels[i] == 5 or predict_labels[i] == 3)  and true_labels[i] == 1) :
            false_negative += 1
    print ("For Food Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")

     # Staff
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 5 and true_labels[i] == 5):
            true_positive += 1
        elif (predict_labels[i] == 5 and true_labels[i] != 5) :
            false_positive += 1
        elif ((predict_labels[i] == 1 or predict_labels[i] == 3)  and true_labels[i] == 5) :
            false_negative += 1
    print ("For Staff Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")

     # Ambience
    true_positive = 0
    false_positive = 0
    false_negative = 0
    for i in range(len(predict_labels)):
        if (predict_labels[i] == 3 and true_labels[i] == 3):
            true_positive += 1
        elif (predict_labels[i] == 3 and true_labels[i] != 3) :
            false_positive += 1
        elif ((predict_labels[i] == 5 or predict_labels[i] == 1)  and true_labels[i] == 3) :
            false_negative += 1
    print ("For Ambience Aspect =================")
    print ('Precision is: ')
    pre = 0
    if (true_positive+false_positive != 0):
        pre = true_positive*100*1.0/(true_positive+false_positive)*1.0
    print (pre)

    print ('Recall is: ')
    recall = 0
    if ((true_positive+false_negative) != 0):
        recall = true_positive*100*1.0/(true_positive+false_negative)*1.0
    print (recall)

    print ('F1 is: ')
    f1 = 0
    if ((pre+recall) != 0):
        f1 = 2*pre*recall*1.0/(pre+recall)*1.0
    print (f1)
    print ("=================================")


import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder,StandardScaler
from sklearn.model_selection import KFold
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score,roc_auc_score,f1_score,precision_score

csvPath = r"C:\Users\YSK\Desktop\1030520813_patternrecognition_final_project\breast-cancer.csv"
dataset = pd.read_csv(csvPath)
sc = StandardScaler()

dataset = dataset.drop(['id'], axis=1)
le = LabelEncoder()
dataset['diagnosis'] = le.fit_transform(dataset['diagnosis'])

X = dataset.drop(columns=['diagnosis'])
y = dataset['diagnosis']

X = sc.fit_transform(X)
le = LabelEncoder()
y = le.fit_transform(y)

classifier = DecisionTreeClassifier()
kfold=KFold(n_splits=5)
accs =list()
for fold_index,(train_idx,test_idx) in enumerate(kfold.split(X,y)):
    print("Fold: ",fold_index+1)
    X_train,X_test = X[train_idx],X[test_idx]
    y_train,y_test = y[train_idx],y[test_idx]

    classifier.fit(X_train,y_train)
    acc = classifier.score(X_test,y_test)
    print("Accuracy in Fold: ",acc)
    accs.append(acc)

print("Mean Accuracy: ", sum(accs)/len(accs))
y_pred = classifier.predict(X_test)
cMatrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cMatrix)
print("Classification Report:\n", classification_report(y_test, y_pred))
print("Accuracy:", accuracy_score(y_test, y_pred))
print('Sensitivity : ', cMatrix[0, 0]/(cMatrix[0, 0]+cMatrix[0, 1]))
print('Specificity : ', cMatrix[1, 1]/(cMatrix[1, 0]+cMatrix[1, 1]))
print('AUC :',roc_auc_score(y_test, y_pred))
print('f1-score:', f1_score(y_test, y_pred))
print('Precision: %.3f' % precision_score(y_test, y_pred))


#!/bin/python
def insertion_sort(array):
    for i in range(len(array)-1,-1,-1):
        j = i-1
        temp = array[i]
        while j >= 0 and (array[i] < array[j]):
            array[i] = array[j]
            print(' '.join(str(x) for x in array))
            j = j-1
        array[j+1] = temp
    return array
m = input()
ar = [int(i) for i in raw_input().strip().split()]
print(' '.join(str(x) for x in insertion_sort(ar)))


# Improting Image class from PIL module
from PIL import Image
# Importing argparse for adding argument parser
import argparse
# Impoting os module for creating folder
import os

# Cropper function
def cropper(croppedHeight, croppedWidth):
   # Giving first value to the counters
   nameCounter  = 1
   # i is counting x axis column of image
   for i in range(int(height/croppedHeight)):
      # j is counting y axis row of image
      for j in range(int(width/croppedWidth)):
         # Set the cropping area with box=(left, upper, right, lower).
         croppedImage = img.crop((croppedWidth*j, croppedWidth*i, croppedHeight*(j+1), croppedHeight*(i+1)))
         # Saving image to folder which created name as cropped_images
         croppedImage.save(os.getcwd() + "/cropped_images/image" + str(nameCounter) + ".png")
         print("[INFO] file created: " + os.getcwd() + "/cropped_images/image" + str(nameCounter) + ".png")
         # Adding one xAxisCounter and nameCounter after take a photo
         nameCounter  = nameCounter  + 1

if __name__ == "__main__":
   # Construct the argument parse and parse the arguments
   parser = argparse.ArgumentParser(description='Process command line arguments.')
   parser.add_argument('-path', "--path", required=True, help="path to file contain image which will crop")
   parser.add_argument('-cW', "--cw", required=True, help='cropped image width value', type=int)
   parser.add_argument('-cH', "--ch", required=True, help='cropped image Height value', type=int)
   args = parser.parse_args()

   # Height and width value that we want to crop
   croppedWidth = args.cw
   croppedHeight = args.ch

   # Open a image
   img = Image.open(args.path)

   # Size of the image in pixels (size of orginal image)
   width, height = img.size

   # Creating file for cropped images where is cropper.py file
   os.mkdir("cropped_images")

   # Function calling
   cropper(croppedHeight, croppedWidth)


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 10:04:24 2021

@author: gordon
"""

# Python program to implement client side of chat room.
import socket
import select
import time
import sys

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
    print ("Correct usage: script, IP address, port number")
    exit()
IP_address = str(sys.argv[1])
Port = int(sys.argv[2])
server.connect((IP_address, Port))
todo=""
mode="none"
while True:

    # maintains a list of possible input streams
    sockets_list = [ server] #sys.stdin,

    """ There are two possible input situations. Either the
    user wants to give manual input to send to other people,
    or the server is sending a message to be printed on the
    screen. Select returns from sockets_list, the stream that
    is reader for input. So for example, if the server wants
    to send a message, then the if condition will hold true
    below.If the user wants to send a message, the else
    condition will evaluate as true"""
    read_sockets,write_socket, error_socket = select.select(sockets_list,[],[],0.1)
    for socks in read_sockets:
        if socks == server:
            message = socks.recv(2048)
            commend=message.decode()
            commend=commend.split()
            print (commend)
            if(commend[0]=="mode1"):
                todo="color-depth"
            elif(commend[0]=="mode2"):
                todo="color*6"
            elif(commend[0]=="mode3"):
                todo="depth*6"
            elif(commend[0]=="rec"):
                if(len(commend)==3):
                    todo="record :"+commend[1]+".bag"
                    print(todo)
                    time.sleep(int(commend[2]))
            elif(commend[0]=="set"):
                if(len(commend)==2 and(commend[1]=="see" or commend[1]=="review")):
                    mode=commend[1]
            else :
                mode="none"
                todo=""
        else:
            message = sys.stdin.readline()
            server.send(message)
            sys.stdout.write("<You>")
            sys.stdout.write(message)
            sys.stdout.flush()
    print(mode+" "+todo)
    if(mode!="none"):
        server.send((mode+" "+todo).encode())
server.close()


#!/usr/bin/env python

# Python wrapper for METEOR implementation, by Xinlei Chen
# Acknowledge Michael Denkowski for the generous discussion and help
from __future__ import division

import atexit
import logging
import os
import re
import subprocess
import sys
import threading

import psutil

# Assumes meteor-1.5.jar is in the same directory as meteor.py.  Change as needed.
METEOR_JAR = 'meteor-1.5.jar'

def enc(s):
    return s.encode('utf-8')

def dec(s):
    return s.decode('utf-8')

class Meteor:

    def __init__(self):
        # Used to guarantee thread safety
        self.lock = threading.Lock()

        mem = '2G'
        mem_available_G = psutil.virtual_memory().available / 1E9
        if mem_available_G < 2:
            logging.warning("There is less than 2GB of available memory.\n"
                            "Will try with limiting Meteor to 1GB of memory but this might cause issues.\n"
                            "If you have problems using Meteor, "
                            "then you can try to lower the `mem` variable in meteor.py")
            mem = '1G'

        meteor_cmd = ['java', '-jar', '-Xmx{}'.format(mem), METEOR_JAR,
                      '-', '-', '-stdio', '-l', 'en', '-norm']
        env = os.environ.copy()
        env['LC_ALL'] = "C"
        self.meteor_p = subprocess.Popen(meteor_cmd,
                                         cwd=os.path.dirname(os.path.abspath(__file__)),
                                         env=env,
                                         stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)

        atexit.register(self.close)

    def close(self):
        with self.lock:
            if self.meteor_p:
                self.meteor_p.kill()
                self.meteor_p.wait()
                self.meteor_p = None
        # if the user calls close() manually, remove the
        # reference from atexit so the object can be garbage-collected.
        if atexit is not None and atexit.unregister is not None:
            atexit.unregister(self.close)

    def compute_score(self, gts, res):
        assert (gts.keys() == res.keys())
        imgIds = gts.keys()
        scores = []

        eval_line = 'EVAL'
        with self.lock:
            for i in imgIds:
                assert (len(res[i]) == 1)
                stat = self._stat(res[i][0], gts[i])
                eval_line += ' ||| {}'.format(stat)

            self.meteor_p.stdin.write(enc('{}\n'.format(eval_line)))
            self.meteor_p.stdin.flush()
            for i in range(0, len(imgIds)):
                v = self.meteor_p.stdout.readline()
                try:
                    scores.append(float(dec(v.strip())))
                except:
                    sys.stderr.write("Error handling value: {}\n".format(v))
                    sys.stderr.write("Decoded value: {}\n".format(dec(v.strip())))
                    sys.stderr.write("eval_line: {}\n".format(eval_line))
                    # You can try uncommenting the next code line to show stderr from the Meteor JAR.
                    # If the Meteor JAR is not writing to stderr, then the line will just hang.
                    # sys.stderr.write("Error from Meteor:\n{}".format(self.meteor_p.stderr.read()))
                    raise
            score = float(dec(self.meteor_p.stdout.readline()).strip())

        return score, scores

    def method(self):
        return "METEOR"

    def _stat(self, hypothesis_str, reference_list):
        # SCORE ||| reference 1 words ||| reference n words ||| hypothesis words
        hypothesis_str = hypothesis_str.replace('|||', '')
        score_line = ' ||| '.join(('SCORE', ' ||| '.join(reference_list), hypothesis_str))
        score_line = re.sub(r'\s+', ' ', score_line)
        self.meteor_p.stdin.write(enc(score_line))
        self.meteor_p.stdin.write(enc('\n'))
        self.meteor_p.stdin.flush()
        return dec(self.meteor_p.stdout.readline()).strip()

    def _score(self, hypothesis_str, reference_list):
        with self.lock:
            # SCORE ||| reference 1 words ||| reference n words ||| hypothesis words
            hypothesis_str = hypothesis_str.replace('|||', '').replace('  ', ' ')
            score_line = ' ||| '.join(('SCORE', ' ||| '.join(reference_list), hypothesis_str))
            self.meteor_p.stdin.write(enc('{}\n'.format(score_line)))
            self.meteor_p.stdin.flush()
            stats = dec(self.meteor_p.stdout.readline()).strip()
            eval_line = 'EVAL ||| {}'.format(stats)
            # EVAL ||| stats
            self.meteor_p.stdin.write(enc('{}\n'.format(eval_line)))
            self.meteor_p.stdin.flush()
            score = float(dec(self.meteor_p.stdout.readline()).strip())
            # bug fix: there are two values returned by the jar file, one average, and one all, so do it twice
            # thanks for Andrej for pointing this out
            score = float(dec(self.meteor_p.stdout.readline()).strip())
        return score

    def __del__(self):
        self.close()


class Postfix_Eps:

    result = []
    cache = []
    priority = {'+':1, '-':1, '*':2, '/':2,'^':3,'(':4}
    opreators = {'+':lambda a,b: b+a, '-':lambda a,b: b-a, '*':lambda a,b: b*a, '/':lambda a,b: b/a,'^':lambda a,b: b**a}

    def __init__(self, input = ""):
        flag = 0
        for index,i in enumerate(input):
            if(flag != 0 and index < flag):
                continue
            if i.isdigit():
                if index == len(input)-1:
                    self.result.append(i)
                else:
                    if index + 1 != len(input):
                        temp = i
                        index += 1
                        while(input[index].isdigit() and (index +1 < len(input))):
                            temp += input[index]
                            index += 1
                        if index == len(input)-1 and input[index].isdigit():
                            temp += input[index]
                            index += 1
                        self.result.append(temp)
                flag = index
            else:
                if len(self.cache) == 0:
                    self.cache.append(i)
                else:
                    if i == ')':
                        while self.cache:
                            if self.cache[-1] != '(':
                                self.result.append(self.cache.pop())
                            else:
                                self.cache.pop()
                                break
                        continue
                    if self.priority[i] > self.priority[self.cache[-1]]:
                        self.cache.append(i)
                    else:
                        if self.cache[-1] == '(':
                            self.cache.append(i)
                        else:
                            while self.cache:
                                if(self.priority[self.cache[-1]] >= self.priority[i]):
                                    self.result.append(self.cache.pop())
                                else:
                                    break
                            self.cache.append(i)

        while self.cache:
            self.result.append(self.cache.pop())

    def getExpression(self):
        re = ""
        for i in self.result:
            re += i
        return re

    def getResult(self):
        self.cache = []
        for i in self.result:
            if i.isdigit():
                self.cache.append(i)
            else:
                self.cache.append(self.opreators.get(i)(int(self.cache.pop()),int(self.cache.pop())))

        return int(self.cache[0])

if __name__ == '__main__':
    stri = input("Please input the expression, Press enter to finish entering.\n")
    ep = Postfix_Eps(stri)
    print("The postfix expression is: " + ep.getExpression())
    print("The result of the expression is: "+ str(ep.getResult()))