import json

from django.http        import JsonResponse
from django.views       import View

from django.db.models   import Avg
from homes.models       import Home
from users.models       import User
from bookmarks.models   import BookMark
from users.utils        import ConfirmLogin

class BookmarkView(View):
    @ConfirmLogin
    def post(self, request, home_pk):
        try:
            user = User.objects.get(id=request.user)
            home = Home.objects.get(id=home_pk)
            if not home_pk in user.bookmarks.values_list("id",flat=True):
                user.bookmarks.add(home)
                return JsonResponse({"MESSAGE":"SUCCESS"},status=201)
            else:
                return JsonResponse({"MESSAGE":"ALREADY_BOOKED"},status=400)
        except User.DoesNotExist:
            return JsonResponse({"MESSAGE":"USER_NOT_FOUND"},status=404)
        except Home.DoesNotExist:
            return JsonResponse({"MESSAGE":"HOME_NOT_FOUND"},status=404)

    @ConfirmLogin
    def delete(self, request, home_pk):
        try:
            home = Home.objects.get(id=home_pk)
            bookmark = BookMark.objects.get(home=home,user_id=request.user).delete()
            return JsonResponse({"MESSAGE":"DELETE_SUCCESSFULLY"},status=200)
        except User.DoesNotExist:
            return JsonResponse({"MESSAGE":"USER_NOT_FOUND"},status=404)
        except BookMark.DoesNotExist:
            return JsonResponse({"MESSAGE":"BOOKMARK_NOT_FOUND"},status=404)
        except Home.DoesNotExist:
            return JsonResponse({"MESSAGE":"HOME_NOT_FOUND"},status=404)

class BookmarkListView(View):
    @ConfirmLogin
    def get(self, request):
        PRICE_CATEGORY_NAME = '1박비용'
        offset = int(request.GET.get("offset",0))
        limit  = int(request.GET.get("limit",15))
        bookmarks = Home.objects.filter(bookmark__user=request.user).prefetch_related(
                "homeopiton_set",
                "homeopiton_set__option",
                "images",
                "review_set",
                "homeprice_set",
                "homeprice_set__price_category"
            ).select_related("home_type").all()

        bookmark_list   = {
            "bookmarks_count" : bookmarks.count(),
            "bookmarks" : [{
            "id"                : home.id,
            "name"              : home.name,
            "address"           : home.address,
            "home_type"         : home.home_type.name,
            "latitude"          : home.latitude,
            "longitude"         : home.longitude,
            "home_images"       : [image.url for image in home.images.all()],
            "home_options"       : [{
                    "option_name"  : option.option.name,
                    "option_count" : option.count
                } for option in home.homeopiton_set.all()],
            "price_of_1day" : [
                {
                    "category" : home_price.price_category.name,
                    "cost"     : home_price.cost
                    }for home_price in home.homeprice_set.all() if home_price.price_category.name==PRICE_CATEGORY_NAME],
            "rating"        : round(home.review_set.aggregate(avg_rating=Avg("rating"))["avg_rating"],2) if home.review_set.exists() else 0,
            "review_count"  : home.review_set.count()
            } for home in bookmarks[offset:offset+limit]]}

        if not bookmark_list["bookmarks"]:
            return JsonResponse({"MESSAGE":"BOOKMARK_NOT_FOUND"},status=404)

        return JsonResponse(bookmark_list,status=200)


# 导入若干工具包
import torch
import torch.nn as nn
import torch.nn.functional as F

# 定义一个简单的网络类
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 定义第一层卷积神经网络, 输入通道维度=1, 输出通道维度=6, 卷积核大小3*3
        self.conv1 = nn.Conv2d(1, 6, 3)
        # 定义第二层卷积神经网络, 输入通道维度=6, 输出通道维度=16, 卷积核大小3*3
        self.conv2 = nn.Conv2d(6, 16, 3)
        # 定义三层全连接网络
        self.fc1 = nn.Linear(16 * 6 * 6, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10) # 后面10个分类

    def forward(self, x):
        # 任意卷积层后面要加激活层、池化层
        # 在(2, 2)的池化窗口下执行最大池化操作
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        # 经过卷积层的处理后，张量要进入全连接层，进入前需要调整张量的形状
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        # 计算size, 除了第0个维度上的batch_size
        size = x.size()[1:]
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net = Net()
# print(net)

params = list(net.parameters())
# print(len(params))
# print(params[0].size())
input = torch.randn(1, 1, 32, 32)
out = net(input)
# print(out)
# print(out.size())
target = torch.randn(10)
target = target.view(1, -1)
criterion = nn.MSELoss()

loss = criterion(out, target)
print(loss)
loss.backward()


import string
import random
import re
import datetime

from tornado_json.requesthandlers import APIHandler
from python_mysql_dbconfig import getconnection

class LinksAPIHandler(APIHandler):# pylint: disable=too-few-public-methods
    __url_names__ = ["links"]

class GetLinks(LinksAPIHandler):# pylint: disable=too-few-public-methods
    def post(self):
        try:
            user_name = self.get_argument("User")
            conn = getconnection()
            cur = conn.cursor()
            cur.execute("SELECT DateCreat, Link, LinkShort, linkscol "
                        "FROM links WHERE user = %s;", (user_name))
            cur.close()
            conn.close()
            list_links = []
            for row in cur:
                list_links.append({
                    "Date": str(row[0]),
                    "Link": str(row[1]),
                    "ShortLink": str(row[2]),
                    "Kol": row[3]
                })
            self.success(tuple(list_links))
        except KeyError:
            self.fail("Fail")

class GetLinkAPIHandler(APIHandler):# pylint: disable=too-few-public-methods
    __url_names__ = ["getlink"]

class GetLinkHandler(GetLinkAPIHandler):# pylint: disable=too-few-public-methods

    def get(self):
        try:
            shortlink = self.get_argument("ShortLink")
            conn = getconnection()
            cur = conn.cursor()
            cur.execute("SELECT Link  FROM links WHERE LinkShort = %s;", (shortlink))
            cur.close()
            conn.close()
            if cur.rowcount != 0:
                row_value = cur._rows[0]#pylint: disable=protected-access
                self.success({'Link': row_value[0]})
            else:
                self.fail("No link")
            return
        except KeyError:
            self.fail("Fail")

class SlinkAPIHandler(APIHandler):# pylint: disable=too-few-public-methods
    __url_names__ = ["slink"]

class MakeAPIHandler(SlinkAPIHandler):# pylint: disable=too-few-public-methods
    def post(self):
        try:
            user = self.get_argument("User")
            link = self.get_argument("Link")
            conn = getconnection()
            cur = conn.cursor()
            cur.execute("SELECT LinkShort FROM links WHERE Link = %s ", (link))
            if cur.rowcount == 0:
                reg = re.compile('[^a-zA-Z ]')
                while True:
                    short_link = generator_short_link(6, reg.sub('', link))
                    cur.execute("SELECT Link FROM links WHERE LinkShort = %s ", (short_link))
                    if cur.rowcount == 0:
                        break
                now = str(datetime.datetime.now())
                cur.execute("INSERT INTO  links (linkscol, Link, LinkShort, User, DateCreat) "
                            "VALUES (0, %s, %s,%s,%s);", (link, short_link, user, now))
                conn.commit()
                cur.close()
                conn.close()
                answer_shortlink = {'ShortLink': short_link}
                self.success(answer_shortlink)
            else:
                self.fail("This link already exists")
        except KeyError:
            self.fail("Error in creating a link")

def generator_short_link(size: object = 6,
                         chars: object = string.ascii_uppercase + string.digits) -> object:
    return ''.join(random.choice(chars) for _ in range(size))


#references : https://youtu.be/pp61TbhJOTg
#ieee paper link : https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=brain+tumor+detection&oq=brain+#d=gs_qabs&u=%23p%3DVe4XM0A9CzEJ
import os
import cv2
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.models import load_model

image_dir='C:/Users/kotta/PycharmProjects/braintumordetection/dataset/'
INPUT_SIZE=64
dataset=[]
label=[]

no_tumor_images=os.listdir(image_dir+ 'no/')
yes_tumor_images=os.listdir(image_dir+ 'yes/')

for i, image_name in enumerate(no_tumor_images):
    if(image_name.split('.')[1]=='jpg'):
        image=cv2.cvtColor(cv2.imread(image_dir+'no/'+image_name), cv2.COLOR_BGR2GRAY)
        image=cv2.resize(image, (INPUT_SIZE,INPUT_SIZE))
        dataset.append(np.array(image))
        label.append(0)

for i, image_name in enumerate(yes_tumor_images):
    if(image_name.split('.')[1]=='jpg'):
        image=cv2.cvtColor(cv2.imread(image_dir+'yes/'+image_name), cv2.COLOR_BGR2GRAY)
        image=cv2.resize(image, (INPUT_SIZE,INPUT_SIZE))
        dataset.append(np.array(image))
        label.append(1)

dataset=np.array(dataset)
label=np.array(label)
#x train and x test is images
#y train and y test is digit representation of images

x_train, x_test, y_train ,y_test = train_test_split(dataset, label, test_size=0.2, random_state=0 )
x_train, x_val, y_train ,y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=0 )
y_train= tf.keras.utils.to_categorical(y_train, num_classes=2)#it convert vector class to binary class matrix
y_val= tf.keras.utils.to_categorical(y_val, num_classes=2)
y_test= tf.keras.utils.to_categorical(y_test, num_classes=2)

#Model Building

model=tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3,3),activation='ReLU', input_shape=(INPUT_SIZE, INPUT_SIZE, 1)),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(32,(3,3), kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(32,(3,3), kernel_initializer='he_uniform'),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(2, activation='sigmoid'),
])

model.summary()
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

#checkpoint = keras.callbacks.ModelCheckpoint("Best_1.h5",save_best_only=True) #confusion matrix
history=model.fit(x_train,
          y_train,
          batch_size=16,
          verbose=1,
          epochs=10,
          validation_data=(x_val,y_val),
          shuffle=False)

model.save('BrainTumor10EpochsCategorical.h5')

y = model.predict(x_test)
model.evaluate(x_test, y_test)
print(confusion_matrix(np.argmax(y_test, 1), np.argmax(y, 1)))

pd.DataFrame(history.history).plot(figsize = (15,8))
plt.gca().set_ylim(0,1)
plt.show()
#model1 = model.load_model("Best_1.h5")
#accuracy = model1.evaluate(x_test,y_test)
#accuracy[1]

#pred = (model1.predict(x_test) > 0.5).astype("int32")

#sns.heatmap(confusion_matrix(y_test,pred), annot = True, cmap = "rainbow")
#plt.title("Confusion Matrix")
#plt.show()
# RATIO_LIST = []
# for set in (x_train, x_test, x_val):
#     for img in set:
#         RATIO_LIST.append(img.shape[1] / img.shape[0])
#
# plt.hist(RATIO_LIST)
# plt.title('Distribution of Image Ratios')
# plt.xlabel('Ratio Value')
# plt.ylabel('Count')
# plt.show()

#values = ["No Tumor Detected","Tumor Detected"]
#y_t = np.array(y_test)
#pred = np.array(pred).reshape(1,-1)

#i = 0
#for data in x_test:
#    print("Predicted :- {} with {}% accuracy".format(values[pred[0][i]],round(accuracy[1]*100,2)))
#    print("Actual :- {}".format(values[y_t[i]]))
 #   plt.imshow(data, cmap="gray")
  #  plt.show()
   # i = i+1


import logging

logging.basicConfig(level=logging.INFO, format='%(message)s')

def addition(x, y):
    logging.info(f"Dodaję {x} i {y}")
    return x + y

def subtraction(x, y):
    logging.info(f"Odejmuję {y} od {x}")
    return x - y

def multiplication(x, y):
    logging.info(f"Mnożę {x} razy {y}")
    return x * y

def division(x, y):
    logging.info(f"Dzielę {x} przez {y}")
    return x / y

def calculator(x, y, operation):
    if operation == "1":
        addition(x, y)
    elif operation == "2":
        subtraction(x, y)
    elif operation == "3":
        multiplication(x, y)
    elif operation == "4":
        if y == 0:
            logging.info("Składnik 2. nie może być równy 0")
            y = input("Podaj składnik 2.")
            y = float(y)
            division(x, y)
        else:
            division(x, y)
    else:
        exit(1)

if __name__ == "__main__":
    while True:
        operation = input("Podaj działanie, posługując się odpowiednią liczbą: 1 Dodawanie, 2 Odejmowanie, 3 Mnożenie, 4 Dzielenie:")
        x = input("Podaj składnik 1.")
        y = input("Podaj składnik 2.")
        x = float(x)
        y = float(y)
        print(f"Wynik to {calculator(x, y, operation)}")


from cpu import CPU

if __name__ == "__main__":
    cpu = CPU()
    cpu.parse_instruction_definitions(["instructions/core.instr"])
    interrupts = [

    ]
    while not cpu.halt:
        cpu.run(1)
        if len(interrupts) > 0:
            cpu.devices[interrupts[0][0]].interrupt(cpu, interrupts[0][1])
            interrupts.pop(0)
    cpu.dump()


import logging

import requests
import pandas as pd
from datetime import date
from datetime import datetime

def main():
    logging.basicConfig(filename='app.log', filemode='a', format='%(asctime)s - %(message)s', level=logging.INFO)
    df = pd.DataFrame(
        columns=['district', 'city', 'county', 'instructional delivery', 'school count', 'grades served', 'total PreK-12 enrollment',
                 'modified id', 'date scraped'])
    url = "https://services2.arcgis.com/3yCQWqEMIRwEdrth/arcgis/rest/services/School_District_Survey_Public/FeatureServer/0/query?f=json&where=(InstrFormat%20%3D%20%27Blended%20Remote%20Learning%27)%20OR%20(InstrFormat%20%3D%20%27In-Person%20Learning%27)%20OR%20(InstrFormat%20%3D%20%27Remote%20Learning%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=USER_Facil%20asc&resultOffset=0&resultRecordCount=670&resultType=standard&cacheHint=true"
    openUrl = requests.get(url)
    jsonD = openUrl.json()
    logging.info("Received Illinois Data", exc_info=False);
    for p in jsonD["features"]:
        district = p["attributes"]["USER_Facil"]
        city = p["attributes"]["USER_City"]
        county = p["attributes"]["USER_Count"]
        instructional_delivery = p["attributes"]["InstrFormat"]
        school_count = p["attributes"]["School_Count"]
        grades_served = p["attributes"]["GradeServed"]
        total_enrollment = p["attributes"]["PreK_12_Total_Enrollment"]
        modified_id = p["attributes"]["Modified"]
        new_row = pd.Series(data={'district': district, 'city': city, 'county': county,
                                  'instructional delivery': instructional_delivery, 'school count': school_count,
                                  'grades served': grades_served, 'total PreK-12 enrollment': total_enrollment,
                                  'modified id': modified_id, 'date scraped': date.today()})
        df = df.append(new_row, ignore_index=True)
    df.to_csv('out/IL_' + datetime.now().strftime('%Y%m%d') + '.csv', index=False)
    logging.info("Wrote Illinois Data", exc_info=False);

#main()


from torch import cuda
USE_CUDA = cuda.is_available()
LOAD_IMAGES_TO_MEMORY = False

# Files
CAPTIONS_DIR = '../data/coco/annotations/captions_{}2014.json'
KARPATHY_SPLIT_DIR = '../data/karpathy_splits/karpathy_{}_images.txt'
FEATURES_DIR = '../data/features/extracts/{}.npy'
CAPTION_VECTORS_DIR = '../data/caption_vectors/{}/{}.npy'
MODEL_DIR = '../models/{}'

# Neural Network Training Settings
LEARNING_RATE = 0.001
MOMENTUM = 0.9
LR_DECAY_PER_EPOCH = 0.9
LR_DECAY_STEP_SIZE = 1  # decay every epoch
BATCH_SIZE = 128
SHUFFLE = True
MAX_EPOCH = 50

BEAM_SIZE = 5

# RL Training Settings
LEARNING_RATE_RL = 1e-4
BATCH_SIZE_RL = 64
MAX_WORDS = 30
MODEL_WEIGHTS = MODEL_DIR.format('RETRAIN-0512-2307-E9')
# Final base LSTM: RETRAIN-0512-2307-E9 (greedy CIDEr starts at 1.00)

BETA = 0.1  # if 1: pure cider score
INCLUDE_CONTEXT_SCORE = True

# Data Settings
PAIR_ALL_CAPTIONS = True
RESULT_DIR = '../data/{}_results.json'
#  http://cocodataset.org/#format-results

# Network Architecture
# Follows Bottom-Up Top-Down paper.
LSTM_HIDDEN_UNITS = 1000
ATTENTION_HIDDEN_UNITS = 512
WORD_EMBEDDING_SIZE = 1000
VOCABULARY_SIZE = 10000 + 2  # 10,000 most common words plus <SOS> and <EOS>

IMAGE_FEATURE_DIM = 2048
IMAGE_FEATURE_REGIONS = 36

ATTENTION_LSTM_INPUT_SIZE = (
    LSTM_HIDDEN_UNITS +
    IMAGE_FEATURE_DIM +
    WORD_EMBEDDING_SIZE
)

LANGUAGE_LSTM_INPUT_SIZE = (
    IMAGE_FEATURE_DIM +
    LSTM_HIDDEN_UNITS
)


'''
add data to dataset

'''

import os
import re
import sys
import time
import fcntl
import datetime
import commands
import brokerage.broker
from dataservice import DynDataDistributer
from dataservice.MailUtils import MailUtils
from dataservice.Notifier import Notifier
from taskbuffer.JobSpec import JobSpec
from dataservice.datriHandler import datriHandler

from config import panda_config
from pandalogger.PandaLogger import PandaLogger

# logger
_logger = PandaLogger().getLogger('EventPicker')
DynDataDistributer.initLogger(_logger)

class EventPicker:
    # constructor
    def __init__(self,taskBuffer,siteMapper,evpFileName,ignoreError):
        self.taskBuffer      = taskBuffer
        self.siteMapper      = siteMapper
        self.ignoreError     = ignoreError
        self.evpFileName     = evpFileName
        self.token           = datetime.datetime.utcnow().isoformat(' ')
        self.pd2p            = DynDataDistributer.DynDataDistributer([],self.taskBuffer,self.siteMapper,
                                                                     token=self.token)
        self.userDatasetName = ''
        self.creationTime    = ''
        self.params          = ''
        self.lockedBy        = ''
        self.evpFile         = None

    # main
    def run(self):
        try:
            self.putLog('start %s' % self.evpFileName)
            # lock evp file
            self.evpFile = open(self.evpFileName)
            try:
                fcntl.flock(self.evpFile.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB)
            except:
                # relase
                self.putLog("cannot lock %s" % self.evpFileName)
                self.evpFile.close()
                return True
            # options
            runEvtList          = []
            eventPickDataType   = ''
            eventPickStreamName = ''
            eventPickDS         = []
            eventPickAmiTag     = ''
            inputFileList       = []
            tagDsList           = []
            tagQuery            = ''
            tagStreamRef        = ''
            # read evp file
            for tmpLine in self.evpFile:
                tmpMatch = re.search('^([^=]+)=(.+)$',tmpLine)
                # check format
                if tmpMatch == None:
                    continue
                tmpItems = tmpMatch.groups()
                if tmpItems[0] == 'runEvent':
                    # get run and event number
                    tmpRunEvt = tmpItems[1].split(',')
                    if len(tmpRunEvt) == 2:
                        runEvtList.append(tmpRunEvt)
                elif tmpItems[0] == 'eventPickDataType':
                    # data type
                    eventPickDataType = tmpItems[1]
                elif tmpItems[0] == 'eventPickStreamName':
                    # stream name
                    eventPickStreamName = tmpItems[1]
                elif tmpItems[0] == 'eventPickDS':
                    # dataset pattern
                    eventPickDS = tmpItems[1].split(',')
                elif tmpItems[0] == 'eventPickAmiTag':
                    # AMI tag
                    eventPickAmiTag = tmpItems[1]
                elif tmpItems[0] == 'userName':
                    # user name
                    self.userDN = tmpItems[1]
                    self.putLog("user=%s" % self.userDN)
                elif tmpItems[0] == 'userDatasetName':
                    # user dataset name
                    self.userDatasetName = tmpItems[1]
                elif tmpItems[0] == 'lockedBy':
                    # client name
                    self.lockedBy = tmpItems[1]
                elif tmpItems[0] == 'creationTime':
                    # creation time
                    self.creationTime = tmpItems[1]
                elif tmpItems[0] == 'params':
                    # parameters
                    self.params = tmpItems[1]
                elif tmpItems[0] == 'inputFileList':
                    # input file list
                    inputFileList = tmpItems[1].split(',')
                    try:
                        inputFileList.remove('')
                    except:
                        pass
                elif tmpItems[0] == 'tagDS':
                    # TAG dataset
                    tagDsList = tmpItems[1].split(',')
                elif tmpItems[0] == 'tagQuery':
                    # query for TAG
                    tagQuery = tmpItems[1]
                elif tmpItems[0] == 'tagStreamRef':
                    # StreamRef for TAG
                    tagStreamRef = tmpItems[1]
                    if not tagStreamRef.endswith('_ref'):
                        tagStreamRef += '_ref'
            # convert
            if tagDsList == [] or tagQuery == '':
                # convert run/event list to dataset/file list
                tmpRet,locationMap,allFiles = self.pd2p.convertEvtRunToDatasets(runEvtList,
                                                                                eventPickDataType,
                                                                                eventPickStreamName,
                                                                                eventPickDS,
                                                                                eventPickAmiTag)
                if not tmpRet:
                    self.endWithError('Failed to convert the run/event list to a dataset/file list')
                    return False
            else:
                # get parent dataset/files with TAG
                tmpRet,locationMap,allFiles = self.pd2p.getTagParentInfoUsingTagQuery(tagDsList,tagQuery,tagStreamRef)
                if not tmpRet:
                    self.endWithError('Failed to get parent dataset/file list with TAG')
                    return False
            # use only files in the list
            if inputFileList != []:
                tmpAllFiles = []
                for tmpFile in allFiles:
                    if tmpFile['lfn'] in inputFileList:
                        tmpAllFiles.append(tmpFile)
                allFiles = tmpAllFiles
            # make dataset container
            tmpRet = self.pd2p.registerDatasetContainerWithDatasets(self.userDatasetName,allFiles,locationMap)
            if not tmpRet:
                self.endWithError('Failed to make a dataset container %s' % self.userDatasetName)
                return False
            # get candidates
            tmpRet,candidateMaps = self.pd2p.getCandidates(self.userDatasetName,checkUsedFile=False,
                                                           useHidden=True)
            if not tmpRet:
                self.endWithError('Failed to find candidate for destination')
                return False
            # collect all candidates
            allCandidates = []
            for tmpDS,tmpDsVal in candidateMaps.iteritems():
                for tmpCloud,tmpCloudVal in tmpDsVal.iteritems():
                    for tmpSiteName in tmpCloudVal[0]:
                        if not tmpSiteName in allCandidates:
                            allCandidates.append(tmpSiteName)
            if allCandidates == []:
                self.endWithError('No candidate for destination')
                return False
            # get size of dataset container
            tmpRet,totalInputSize = self.pd2p.getDatasetSize(self.userDatasetName)
            if not tmpRet:
                self.endWithError('Failed to get the size of %s' % self.userDatasetName)
                return False
            # run brokerage
            tmpJob = JobSpec()
            tmpJob.AtlasRelease = ''
            self.putLog("run brokerage for %s" % tmpDS)
            brokerage.broker.schedule([tmpJob],self.taskBuffer,self.siteMapper,True,allCandidates,
                                      True,datasetSize=totalInputSize)
            if tmpJob.computingSite.startswith('ERROR'):
                self.endWithError('brokerage failed with %s' % tmpJob.computingSite)
                return False
            self.putLog("site -> %s" % tmpJob.computingSite)
            # send request to DaTRI
            if self.lockedBy.startswith('ganga'):
                tmpHandler = datriHandler(type='ganga')
            else:
                tmpHandler = datriHandler(type='pathena')
            # remove redundant CN from DN
            tmpDN = self.userDN
            tmpDN = re.sub('/CN=limited proxy','',tmpDN)
            tmpDN = re.sub('(/CN=proxy)+$','',tmpDN)
            tmpMsg = "%s ds=%s site=%s id=%s" % ('datriHandler.sendRequest',
                                                 self.userDatasetName,
                                                 self.siteMapper.getSite(tmpJob.computingSite).ddm,
                                                 tmpDN)
            self.putLog(tmpMsg)
            tmpHandler.setParameters(data_pattern=self.userDatasetName,
                                     site=self.siteMapper.getSite(tmpJob.computingSite).ddm,
                                     userid=tmpDN)
            nTry = 3
            for iTry in range(nTry):
                dhStatus,dhOut = tmpHandler.sendRequest()
                # succeeded
                if dhStatus == 0 or "such request is exist" in dhOut:
                    self.putLog("%s %s" % (dhStatus,dhOut))
                    break
                if iTry+1 < nTry:
                    # sleep
                    time.sleep(60)
                else:
                    # final attempt failed
                    self.endWithError('Failed to send request to DaTRI : %s %s' % (dhStatus,dhOut))
                    return False
            # send email notification for success
            tmpMsg =  'A transfer request was successfully sent to DaTRI.\n'
            tmpMsg += 'You will receive a notification from DaTRI when it completed.'
            self.sendEmail(True,tmpMsg)
            try:
                # unlock and delete evp file
                fcntl.flock(self.evpFile.fileno(),fcntl.LOCK_UN)
                self.evpFile.close()
                os.remove(self.evpFileName)
            except:
                pass
            # successfully terminated
            self.putLog("end %s" % self.evpFileName)
            return True
        except:
            errType,errValue = sys.exc_info()[:2]
            self.endWithError('Got exception %s:%s' % (errType,errValue))
            return False

    # end with error
    def endWithError(self,message):
        self.putLog(message,'error')
        # unlock evp file
        try:
            fcntl.flock(self.evpFile.fileno(),fcntl.LOCK_UN)
            self.evpFile.close()
            if not self.ignoreError:
                # remove evp file
                os.remove(self.evpFileName)
                # send email notification
                self.sendEmail(False,message)
        except:
            pass
        self.putLog('end %s' % self.evpFileName)

    # put log
    def putLog(self,msg,type='debug'):
        tmpMsg = self.token+' '+msg
        if type == 'error':
            _logger.error(tmpMsg)
        else:
            _logger.debug(tmpMsg)

    # send email notification
    def sendEmail(self,isSucceeded,message):
        # mail address
        toAdder = Notifier(self.taskBuffer,None,[]).getEmail(self.userDN)
        if toAdder == '':
            self.putLog('cannot find email address for %s' % self.userDN,'error')
            return
        # subject
        mailSubject = "PANDA notification for Event-Picking Request"
        # message
        mailBody = "Hello,\n\nHere is your request status for event picking\n\n"
        if isSucceeded:
            mailBody += "Status  : Passed to DaTRI\n"
        else:
            mailBody += "Status  : Failed\n"
        mailBody +=     "Created : %s\n" % self.creationTime
        mailBody +=     "Ended   : %s\n" % datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
        mailBody +=     "Dataset : %s\n" % self.userDatasetName
        mailBody +=     "\n"
        mailBody +=     "Parameters : %s %s\n" % (self.lockedBy,self.params)
        mailBody +=     "\n"
        mailBody +=     "%s\n" % message
        # send
        retVal = MailUtils().send(toAdder,mailSubject,mailBody)
        # return
        return


# -*- coding: utf-8 -*-
"""
Created on Fri May 18 10:25:58 2018

@author: 刘闯

欧几里得算法 求解公约数
"""

#A=Q*B+R  if A>B
def GCD(A,B):
    if A==0:
        gcd=B
    elif B==0:
        gcd=A
    else:
        return GCD(B,A%B)
    return gcd
print(GCD(27,123))


from celery import states
from celery.task import Task
from celery.exceptions import Ignore

from vigil.celery import app

class LambdaBotInsufficientFundsTask(Task):
    """
    Pre-process data from Lambda Bot when reporting
    insufficient funds to place total walls
    """
    expected_data = {
        'exchange': '',
        'bot_name': '',
        'currency': '',
        'target_amount': '',
        'amount_available': '',
        'amount_on_order': ''
    }

    action_type = 'preprocessor'

    def check_data(self, data):
        for key in self.expected_data:
            if key not in data:
                self.update_state(
                    state=states.FAILURE,
                    meta='A value for \'{}\' was not present in the passed data'.format(key)
                )
                return False
        return True

    def run(self, *args, **kwargs):
        data = kwargs.get('data', {})

        if not self.check_data(data):
            raise Ignore()

        return {
            'title': 'Refill {} on {} for {}'.format(data['currency'], data['exchange'], data['bot_name']),
            'message': (
                'There are not enough {} to place orders on the {} {} pair.\n'
                'Require {:.4f} to reach the target of {:.4f} but there are only {:.4f} available.\n'
                'There are currently {:.4f} on order.'.format(
                    data['currency'],
                    data['exchange'],
                    data['bot_name'],
                    float(data['target_amount']) - float(data['amount_available']),
                    float(data['target_amount']),
                    float(data['amount_available']),
                    float(data['amount_on_order'])
                )
            )
        }

LambdaBotInsufficientFunds = app.register_task(LambdaBotInsufficientFundsTask())


import pandas as pd
import numpy as np
import sys
sys.path.append(".")
import unittest
import doctest
from func_analises import reindexacao_e_filtragem, df

class TestReindexacaoEFiltragem(unittest.TestCase):
    # teste com parâmetro válido
    def test_reindexacao_e_filtragem_com_dataframe(self):

        # Chama a função a ser testada
        resultado = reindexacao_e_filtragem(df, "Conceito Médio de Graduação")

        # Adiciona as asserções apropriadas para verificar o resultado
        self.assertIsInstance(resultado, pd.core.frame.DataFrame) # Resultado é um DataFrame pandas

    # teste com parâmetro inválido, ou seja, um que não seja DataFrame
    def test_reindexacao_e_filtragem_com_coluna_errada(self):

        resultado = reindexacao_e_filtragem(df, "Tio Rafa")

        # Conferindo se ela não retorna nada com o erro
        self.assertIsNone(resultado)

    # teste sem parâmetro
    def test_analise_1_ana_sem_parametro(self):
        with self.assertRaises(TypeError) as cm:
            resultado = reindexacao_e_filtragem()

        self.assertEqual(str(cm.exception), "reindexacao_e_filtragem() missing 2 required positional arguments: 'df' and 'coluna'")

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


import flask
import json
import os
import alsaaudio
import mpv
import threading
import requests
import signal
import subprocess

app = flask.Flask(__name__, static_folder = '../gui/build/', static_url_path="/")

if os.environ.get('PIRADIO_DEV') is not None:
    from flask_cors import CORS
    CORS(app)

conf = json.load(open('../config.json', 'r'))
selected_radio = 0
stopped = False
mixer = alsaaudio.Mixer(alsaaudio.mixers()[0])
playYT = False
YTHandlingThread = None
playingTitle = ""

class MPV:
    def __init__(self):
        self.proc = None

    def play(self, url):
        self.stop()
        self.proc = subprocess.Popen(
            ['mpv', '--no-video', url]
        )

    def stop(self):
        if self.proc is None:
            return
        self.proc.kill()

    def wait(self):
        self.proc.wait()

player = MPV()

@app.route('/')
def index():
    return flask.redirect('/index.html', 301)

@app.route("/state/")
def state():
    return flask.jsonify({
        'title':playingTitle,
        'volume':getvolume(),
        'playing': not stopped,
        'radioId': -1 if playYT else selected_radio,
    })

@app.route("/radios/")
def getradios():
    return flask.jsonify({'radios':conf['radios']})

@app.route('/radio/<radio_id>/')
def select_radio(radio_id):
    global player, selected_radio, stopped, playingTitle
    stopped = False
    with open('default_radio.txt', 'w') as f:
        f.write(radio_id)
    radio_id = int(radio_id)
    playingTitle = conf['radios'][radio_id]['name']
    selected_radio = radio_id
    stopPlaying()
    player.play(conf['radios'][radio_id]['url'])
    return "", 200

@app.route('/yt/')
def youtube():
    global YTHandlingThread, playingTitle
    url = flask.request.args.get('url')
    if url.endswith('/'): url = url[:-1]
    print(url)
    stopPlaying()
    ytid = url.split('/')[-1].split('=')[-1]
    playingTitle = getYTTitle(ytid)
    YTHandlingThread = threading.Thread(target=handleYT, args=(ytid,))
    YTHandlingThread.start()
    return "", 200

@app.route('/stop/')
def stop():
    stopPlaying()
    return "", 200

@app.route('/volumeup/')
def volumeup():
    setvolume(getvolume()+3)
    return "", 200

@app.route('/volumedown/')
def volumedown():
    setvolume(getvolume()-3)
    return "", 200

def handleYT(ytid):
    global playYT, playingTitle
    playYT = True
    firstVideo = True
    previousYTId = None
    while playYT:
        if not firstVideo:
            playingTitle = getYTTitle(ytid)
        player.stop()
        player.play("https://www.youtube.com/watch?v="+ytid)
        player.wait()
        items = requests.get(
            "https://www.googleapis.com/youtube/v3/search?part=snippet&relatedToVideoId={}&type=video&key={}".format(
                ytid, conf['YT_API_KEY']
            )
        ).json()["items"]

        tempYTId = items[0]['id']['videoId']
        if not firstVideo:
            if tempYTId == previousYTId:
                tempYTId = items[1]['id']['videoId']
        previousYTId = ytid
        ytid = tempYTId
        firstVideo = False
    return

def stopPlaying():
    global playYT, YTHandlingThread, stopped
    if playYT:
        playYT = False
        player.stop()
        YTHandlingThread.join()
        YTHandlingThread = None
    else:
        player.stop()
    stopped = True
    return

def getvolume():
    global mixer
    return int(mixer.getvolume()[0])

def setvolume(vol):
    global mixer
    mixer.setvolume(max(0,min(100,vol)))

def getYTTitle(ytid):
    return requests.get(
        "https://www.googleapis.com/youtube/v3/videos?part=snippet&id={}&key={}".format(
            ytid, conf['YT_API_KEY']
        )
    ).json()['items'][0]['snippet']['title']

if os.path.isfile('default_radio.txt'):
    with open('default_radio.txt', 'r') as f:
        selected_radio = f.readline()
    try:
        selected_radio = int(selected_radio)
    except ValueError:
        selected_radio = 0
    if selected_radio>=len(conf['radios']) and selected_radio<0:
        selected_radio = 0
else:
    selected_radio=0
if not stopped:
    select_radio(str(selected_radio))

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=8000)


from rest_framework.test import APIClient
# import status_code
from rest_framework import status
import pytest
from model_bakery import baker as Baker
from store.models import Collection ,Product ,Cart

# THIS FIXTURE IS SPECIFIC TO THIS TEST FILE
# RETURN THE API CALL FUNCTION
@pytest.fixture
# we cant not take parameteras value because a it consider it as a fixture
def get_api_call():
    # so we return the fuction with required parameters
    # we are taking client as argument beacuse some client ar authenticated and some are not
    def do_api_call(data, client):
        return client.post('/store/products/', data=data)
    return do_api_call

@pytest.mark.django_db  # this decorator will create a new database for testing
class TestCreateProduct :

    def test_if_user_is_anonymous_returns_401(self, get_client, get_api_call):

        response = get_api_call(data ={
            "title": "Nimbuu",
            "price_of_product": 45,
            "inventory":  90,
            "collection":  3,
            "description" : "nice PRoduct",
        }, client=get_client)
        # Assert
        assert response.status_code == status.HTTP_401_UNAUTHORIZED

    def test_if_user_is_authenticated_but_not_admin_return_403(self, get_authenticated_client, get_api_call):

        client = get_authenticated_client(is_staff=False)

        response = get_api_call(data ={
            "title": "Nimbuu",
            "price_of_product": 45,
            "inventory":  90,
            "collection":  3,
            "description" : "nice PRoduct",
        }, client=client)
        # Assert
        assert response.status_code == status.HTTP_403_FORBIDDEN

    def test_if_user_is_admin_but_data_is_invalid_returns_400(self, get_authenticated_client, get_api_call):

            client = get_authenticated_client(is_staff=True)

            response = get_api_call(data ={
                "title": "kii",
                "price_of_product": 45,
                "inventory":  -90,
                "collection":  3,
                "description" : "nice PRoduct",
            }, client=client)
            # Assert
            assert response.status_code == status.HTTP_400_BAD_REQUEST

    def test_if_user_is_admin_and_data_is_valid_returns_201(self, get_authenticated_client, get_api_call):

            client = get_authenticated_client(is_staff=True)
            collection = Baker.make(Collection)
            response = get_api_call(data ={
                "title": "Nimbuu",
                "price_of_product": 45,
                "inventory":  90,
                "collection":  collection.id,
                "description" : "nice PRoduct",
            }, client=client)
            # Assert
            assert response.status_code == status.HTTP_201_CREATED


import os
import re
from difflib import SequenceMatcher, get_close_matches
from typing import Tuple, Union

import pandas as pd

_src_path = os.path.dirname(__file__)

_data = pd.read_feather(os.path.join(_src_path, "data/state.ft"))
_states = list(_data["State"])

_data = pd.read_feather(os.path.join(_src_path, "data/city.ft"))
_cities = list(_data["City"])

_data = pd.read_feather(os.path.join(_src_path, "data/address.ft"))
address = list(_data["Address"])

_data = pd.read_feather(os.path.join(_src_path, "data/state_and_city.ft"))
_states_and_cities = list(_data["StateAndCity"])

_state_expression = "(" + "|".join(_states) + ")"
_city_expression = "(" + "|".join(_cities) + ")"
_state_regex = re.compile("^" + _state_expression)
_city_regex = re.compile("^" + _city_expression)

def split_address(addr: str) -> Tuple[Union[str, None], Union[str, None], Union[str, None]]:
    m = _state_regex.match(addr)
    if not m:
        return None, None, None
    state = m[1]
    m = _city_regex.match(addr[len(state) :])
    if not m:
        return state, None, None
    city = m[1]
    return state, city, addr[len(state) + len(city) :]

def _state_complement(_target: str) -> Union[str, None]:
    return _target if _target in _states else None

def _city_complement(_target: str) -> Union[str, None]:
    matches = [c for c in _states_and_cities if re.search(_target + "$", c)]
    return None if len(matches) == 0 else matches[0]

def _complement(a: str, b: str) -> str:
    if len(a) <= 10:
        res = _state_complement(a)
        if res is not None:
            return res
        res = _city_complement(a)
        if res is not None:
            return res

    s = SequenceMatcher(None, a, b)
    res = ""
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        if tag == "equal":
            res += a[i1:i2]
        elif tag == "replace":
            res += b[j1:j2]
        elif tag == "insert":
            res += b[j1:j2]
        elif tag == "delete" and len(b) == j1:  # pragma: no cover
            res += a[i1:i2]
        else:  # pragma: no cover
            pass
    return res

def _normalize(address: str) -> Tuple[str, str]:
    m = re.search("^(.+?)([0-9０-９]+.*)", address)
    if m is not None:
        return m.group(1), m.group(2)
    return address, ""

def _rule_based_complement(address: str) -> str:
    rules = [
        ("東京都大手町", "東京都千代田区大手町"),
        ("^海外([^町]|$)", r"\1"),
    ]

    for rule in rules:
        address = re.sub(rule[0], rule[1], address)
    return address

def complement_address(addr: str) -> str:
    addr = _rule_based_complement(addr)
    head, tail = _normalize(addr)
    candidates = get_close_matches(head, address, n=1, cutoff=0.3)
    complemented_head = _complement(head, candidates[0]) if len(candidates) > 0 else head
    return complemented_head + tail


import matplotlib.pyplot as plt
import numpy as np

# Datenfile erzeugen
data_x = np.linspace(0, 10, 50)
data_y = 10 * np.exp(-data_x)
np.savetxt("3.txt", np.column_stack([data_x, data_y]), header="x y")

x, y = np.genfromtxt("3.txt", unpack=True)

fig, (ax1, ax2) = plt.subplots(1, 2, layout="constrained")

ax1.plot(x, y, ".")
ax1.set_xlabel(r"$x$")
ax1.set_ylabel(r"$y$")

ax2.plot(x, y, ".")
ax2.set_xlabel(r"$x$")
ax2.set_ylabel(r"$y$")
ax2.set_yscale("log")

fig.savefig("loesung.pdf")


import torch
import torch.nn as nn
import numpy as np
import sys
import os

sys.path.append(os.path.join(os.getcwd(), "lib")) # HACK add the lib folder
# from models.backbone_module import Pointnet2Backbone
from lib.pointnet2.pointnet2_modules import PointnetSAModuleVotes, PointnetFPModule
from models.voting_module import VotingModule

class Pointnet2Backbone(nn.Module):
    r"""
       Backbone network for point cloud feature learning.
       Based on Pointnet++ single-scale grouping network.

       Parameters
       ----------
       input_feature_dim: int
            Number of input channels in the feature descriptor for each point.
            e.g. 3 for RGB.
    """
    def __init__(self, input_feature_dim=0):
        super().__init__()

        self.input_feature_dim = input_feature_dim

        # --------- 4 SET ABSTRACTION LAYERS ---------
        self.sa1 = PointnetSAModuleVotes(
                npoint=2048,
                radius=0.2,
                nsample=64,
                mlp=[input_feature_dim, 64, 64, 128],
                use_xyz=True,
                normalize_xyz=True
            )

        self.sa2 = PointnetSAModuleVotes(
                npoint=1024,
                radius=0.4,
                nsample=32,
                mlp=[128, 128, 128, 256],
                use_xyz=True,
                normalize_xyz=True
            )

        self.sa3 = PointnetSAModuleVotes(
                npoint=512,
                radius=0.8,
                nsample=16,
                mlp=[256, 128, 128, 256],
                use_xyz=True,
                normalize_xyz=True
            )

        self.sa4 = PointnetSAModuleVotes(
                npoint=256,
                radius=1.2,
                nsample=16,
                mlp=[256, 128, 128, 256],
                use_xyz=True,
                normalize_xyz=True
            )

        # --------- 2 FEATURE UPSAMPLING LAYERS --------
        self.fp1 = PointnetFPModule(mlp=[256+256,256,256])
        self.fp2 = PointnetFPModule(mlp=[256+256,256,256])

    def _break_up_pc(self, pc):
        xyz = pc[..., :3].contiguous()
        features = pc[..., 3:].transpose(1, 2).contiguous() if pc.size(-1) > 3 else None

        return xyz, features

    def forward(self, data_dict):
        r"""
            Forward pass of the network

            Parameters
            ----------
            pointcloud: Variable(torch.cuda.FloatTensor)
                (B, N, 3 + input_feature_dim) tensor
                Point cloud to run predicts on
                Each point in the point-cloud MUST
                be formated as (x, y, z, features...)

            Returns
            ----------
            data_dict: {XXX_xyz, XXX_features, XXX_inds}
                XXX_xyz: float32 Tensor of shape (B,K,3)
                XXX_features: float32 Tensor of shape (B,K,D)
                XXX-inds: int64 Tensor of shape (B,K) values in [0,N-1]
        """

        pointcloud = data_dict["point_clouds"]

        batch_size = pointcloud.shape[0]

        xyz, features = self._break_up_pc(pointcloud)

        # --------- 4 SET ABSTRACTION LAYERS ---------
        xyz, features, fps_inds = self.sa1(xyz, features)
        data_dict['sa1_inds'] = fps_inds
        data_dict['sa1_xyz'] = xyz
        data_dict['sa1_features'] = features

        xyz, features, fps_inds = self.sa2(xyz, features) # this fps_inds is just 0,1,...,1023
        data_dict['sa2_inds'] = fps_inds
        data_dict['sa2_xyz'] = xyz
        data_dict['sa2_features'] = features

        xyz, features, fps_inds = self.sa3(xyz, features) # this fps_inds is just 0,1,...,511
        data_dict['sa3_inds'] = fps_inds
        data_dict['sa3_xyz'] = xyz
        data_dict['sa3_features'] = features

        xyz, features, fps_inds = self.sa4(xyz, features) # this fps_inds is just 0,1,...,255
        data_dict['sa4_inds'] = fps_inds
        data_dict['sa4_xyz'] = xyz
        data_dict['sa4_features'] = features

        # --------- 2 FEATURE UPSAMPLING LAYERS --------
        features = self.fp1(data_dict['sa3_xyz'], data_dict['sa4_xyz'], data_dict['sa3_features'], data_dict['sa4_features'])
        features = self.fp2(data_dict['sa2_xyz'], data_dict['sa3_xyz'], data_dict['sa2_features'], features)
        data_dict['fp2_features'] = features
        data_dict['fp2_xyz'] = data_dict['sa2_xyz']
        num_seed = data_dict['fp2_xyz'].shape[1]
        data_dict['fp2_inds'] = data_dict['sa1_inds'][:,0:num_seed] # indices among the entire input point clouds

        return data_dict

class ProposalModule(nn.Module):
    def __init__(self, num_class, num_heading_bin, num_size_cluster, mean_size_arr, num_proposal, sampling, seed_feat_dim=256):
        super().__init__()

        self.num_class = num_class
        self.num_heading_bin = num_heading_bin
        self.num_size_cluster = num_size_cluster
        self.mean_size_arr = mean_size_arr
        self.num_proposal = num_proposal
        self.sampling = sampling
        self.seed_feat_dim = seed_feat_dim

        # Vote clustering
        self.vote_aggregation = PointnetSAModuleVotes(
            npoint=self.num_proposal,
            # radius=1.6,
            radius=5,
            nsample=512,
            mlp=[self.seed_feat_dim, 128, 128, 128],
            use_xyz=True,
            normalize_xyz=True
        )

        # Object proposal/detection
        # center coordinates (3),
        # size class+residual(num_size_cluster*4)
        self.proposal = nn.Sequential(
            nn.Conv1d(128,128,1, bias=False),
            nn.BatchNorm1d(128),
            nn.ReLU(),
            nn.Conv1d(128,128,1, bias=False),
            nn.BatchNorm1d(128),
            nn.ReLU(),
            nn.Conv1d(128,3+num_size_cluster*4+self.num_class,1)
        )

    def forward(self, xyz, features, data_dict):
        """
        Args:
            xyz: (B,K,3)
            features: (B,C,K)
        Returns:
            scores: (B,num_proposal,2+3+NH*2+NS*4)
        """

        # Farthest point sampling (FPS) on votes
        xyz, features, fps_inds = self.vote_aggregation(xyz, features)

        # # max pool
        # xyz, features, fps_inds = data_dict['fp2_xyz'], data_dict['fp2_features'], data_dict['fp2_inds']
        # features = features.max(dim=-1, keepdim=True)[0]

        sample_inds = fps_inds

        data_dict['aggregated_vote_xyz'] = xyz # (batch_size, num_proposal, 3)
        data_dict['aggregated_vote_features'] = features.permute(0, 2, 1).contiguous() # (batch_size, num_proposal, 128)
        data_dict['aggregated_vote_inds'] = sample_inds # (batch_size, num_proposal,) # should be 0,1,2,...,num_proposal

        # --------- PROPOSAL GENERATION ---------
        net = self.proposal(features)
        data_dict = self.decode_scores(net, data_dict, self.num_class, self.num_heading_bin, self.num_size_cluster, self.mean_size_arr)

        return data_dict

    def decode_scores(self, net, data_dict, num_class, num_heading_bin, num_size_cluster, mean_size_arr):
        """
        decode the predicted parameters for the bounding boxes

        """
        net_transposed = net.transpose(2,1).contiguous() # (batch_size, 1024, ..)
        batch_size = net_transposed.shape[0]
        num_proposal = net_transposed.shape[1]

        base_xyz = data_dict['aggregated_vote_xyz'] # (batch_size, num_proposal, 3)
        center = base_xyz + net_transposed[:,:,0:3] # (batch_size, num_proposal, 3)

        size_scores = net_transposed[:,:,3:3+num_size_cluster]
        size_residuals_normalized = net_transposed[:,:,3+num_size_cluster:3+num_size_cluster*4].view([batch_size, num_proposal, num_size_cluster, 3]) # Bxnum_proposalxnum_size_clusterx3

        sem_cls_scores = net_transposed[:,:,3+num_size_cluster*4:] # Bxnum_proposalx10

        # store
        data_dict['center'] = center
        data_dict['size_scores'] = size_scores
        data_dict['size_residuals_normalized'] = size_residuals_normalized
        data_dict['size_residuals'] = size_residuals_normalized * torch.from_numpy(mean_size_arr.astype(np.float32)).cuda().unsqueeze(0).unsqueeze(0)
        data_dict['sem_cls_scores'] = sem_cls_scores

        return data_dict

class MaskVoteNet(nn.Module):
    def __init__(self, num_class, num_heading_bin, num_size_cluster, mean_size_arr,
    input_feature_dim=0, num_proposal=1, vote_factor=1, sampling="vote_fps"):
        super().__init__()

        self.num_class = num_class
        self.num_heading_bin = num_heading_bin
        self.num_size_cluster = num_size_cluster
        self.mean_size_arr = mean_size_arr
        assert(mean_size_arr.shape[0] == self.num_size_cluster)
        self.input_feature_dim = input_feature_dim
        self.num_proposal = num_proposal
        self.vote_factor = vote_factor
        self.sampling = sampling

        # --------- PROPOSAL GENERATION ---------
        # Backbone point feature learning
        self.backbone_net = Pointnet2Backbone(input_feature_dim=self.input_feature_dim)

        # Hough voting
        self.vgen = VotingModule(self.vote_factor, 256)

        # Vote aggregation and object proposal
        self.proposal = ProposalModule(num_class, num_heading_bin, num_size_cluster, mean_size_arr, num_proposal, sampling)

    def forward(self, data_dict):
        """ Forward pass of the network

        Args:
            data_dict: dict
                {
                    point_clouds,
                    lang_feat
                }

                point_clouds: Variable(torch.cuda.FloatTensor)
                    (B, N, 3 + input_channels) tensor
                    Point cloud to run predicts on
                    Each point in the point-cloud MUST
                    be formated as (x, y, z, features...)
        Returns:
            end_points: dict
        """

        #######################################
        #                                     #
        #           DETECTION BRANCH          #
        #                                     #
        #######################################

        # --------- HOUGH VOTING ---------
        data_dict = self.backbone_net(data_dict)

        # --------- HOUGH VOTING ---------
        xyz = data_dict["fp2_xyz"]
        features = data_dict["fp2_features"]
        data_dict["seed_inds"] = data_dict["fp2_inds"]
        data_dict["seed_xyz"] = xyz
        data_dict["seed_features"] = features

        xyz, features = self.vgen(xyz, features)
        features_norm = torch.norm(features, p=2, dim=1)
        features = features.div(features_norm.unsqueeze(1))
        data_dict["vote_xyz"] = xyz
        data_dict["vote_features"] = features

        # --------- PROPOSAL GENERATION ---------
        data_dict = self.proposal(xyz, features, data_dict)
        # data_dict = self.proposal(data_dict)

        return data_dict


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

def BubbleSort_sort(n):   # O(n^2)
    # for k in range(n-1):
    for i in range(2):
        for j in range(i+1, n):
            if a[i] > a[j]:
                a[i], a[j] = a[j], a[i]

n = int(input())
a = list(map(int, input().split()))
res = 0
a.sort()
while len(a) != 1:
    b = a.pop(0) + a.pop(0)
    res += b
    a.insert(0, b)
    # BubbleSort_sort(n-(1+len(a)))
    a.sort()

# print(a)
print(res)

A1-최소비용으로 포장 다시하기.py


"""
File: logger.py
Modified by: Senthil Purushwalkam
Code referenced from https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514
Email: spurushw<at>andrew<dot>cmu<dot>edu
Github: https://github.com/senthilps8
Description:
"""

#import tensorflow as tf
from torch.autograd import Variable
import numpy as np
import scipy.misc
import os
import torch
from os import path

from tensorboardX import SummaryWriter
import torchvision.utils as vutils
from utils.utils import colorize
from dataloader.data import OpticalConverter

try:
    from StringIO import StringIO  # Python 2.7
except ImportError:
    from io import BytesIO  # Python 3.x

def DepthNorm(depth, maxDepth=500.0):
    return maxDepth / depth

class Logger(object):

    def __init__(self, log_dir='./', network='ResnetUnet', name=None):
        """Create a summary writer logging to log_dir."""
        self.name = name
        self.network = network
        if name is not None:
            try:
                os.makedirs(os.path.join(log_dir, name))
            except:
                pass
            print('Tensorboard Log is logged : ', os.path.join(log_dir, name))
            self.writer = SummaryWriter(logdir="{}".format(os.path.join(log_dir, name)))
        else:
            print('Tensorboard Log is logged : ', log_dir)
            self.writer = SummaryWriter(logdir="{}".format(log_dir))

        self.opt = OpticalConverter(train_dataset='scannet', test_dataset='scannet')

    def scalar_summary(self, tags, values, step):
        """Log a scalar variable.
        """
        self.writer.add_scalar(tag=tags, scalar_value=values, global_step=step)
        self.writer.flush()

    def LogProgressImage(self, model, sample_batched, epoch):
        with torch.no_grad():
            image = torch.autograd.Variable(sample_batched['image'].cuda())

            depth = self.opt.scale_refine(torch.autograd.Variable(sample_batched['depth'].cuda(non_blocking=True)))
            mask = torch.autograd.Variable(sample_batched['rgb_mask'].cuda())
            depth = torch.mul(depth, mask)

            if epoch % 50 == 0:
                self.writer.add_image('Train.1.Image', vutils.make_grid(image.data, nrow=6, normalize=True), epoch)
            if epoch % 50 == 0:
                for idx in range(sample_batched['image'].size(0)):
                    depth[idx] = depth[idx] / torch.amax(depth[idx])
                self.writer.add_image('Train.2.Depth', colorize(vutils.make_grid(depth.data, nrow=6, normalize=False)),
                                      epoch)

            if self.network == 'ResnetUnetPartialConv_v1' or self.network == 'ResnetUnetPartialConv_v2' or self.network == 'UnetPartialConv' or self.network == 'ResnetUnetPartialConv_v3':
                pred_depth, pred_mask = model(image, mask)
                pred_depth = self.opt.scale_refine(pred_depth)

                ##masking process
                # mask = mask[:, 0, :, :].unsqueeze(1)
                pred_depth = torch.mul(pred_depth, mask)  # pred

                for idx in range(sample_batched['image'].size(0)):
                    pred_depth[idx] = pred_depth[idx] / torch.amax(pred_depth[idx])
                    # pred_mask[idx] = pred_mask[idx] / torch.amax(pred_mask[idx])
                # pred_mask = torch.clamp(pred_mask, min=0.0, max=1.0)
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth.data, nrow=6, normalize=False)), epoch)
                # self.writer.add_image('Train.4.Output Mask',
                #                       colorize(vutils.make_grid(pred_mask.data, nrow=6, normalize=False)), epoch)
                del pred_mask
            elif self.network == 'ResnetUnet' or self.network == 'DenseDepthNet' or self.network == 'ResnetUnetGatedConv':
                pred_depth = model(image)
                pred_depth = self.opt.scale_refine(pred_depth)
                for idx in range(sample_batched['image'].size(0)):
                    pred_depth[idx] = pred_depth[idx] / torch.amax(pred_depth[idx])
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth.data, nrow=6, normalize=False)), epoch)
            elif 'SpatialRectifierResnetUnet' in self.network:
                pred_depth = model(sample_batched)
                pred_depth['depth'] = self.opt.scale_refine(pred_depth['depth'])
                pred_depth['depth'] = torch.mul(pred_depth['depth'], mask)  # pred

                for idx in range(sample_batched['image'].size(0)):
                    pred_depth['depth'][idx] = pred_depth['depth'][idx] / torch.amax(pred_depth['depth'][idx])
                self.writer.add_image('Train.3.Output Depth',
                                      colorize(vutils.make_grid(pred_depth['depth'].data, nrow=6, normalize=False)),
                                      epoch)
            del image
            del depth
            del mask
            del pred_depth


from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch
from diffusers import DDIMScheduler

from diffusers.utils import BaseOutput

@dataclass
class NestedSchedulerOutput(BaseOutput):
    """
    Output class for the scheduler's step function output.

    Args:
        prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
            Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the
            denoising loop.
        pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
            The predicted denoised sample (x_{0}) based on the model output from the current timestep.
            `pred_original_sample` can be used to preview progress or for guidance.
    """

    prev_sample: torch.FloatTensor
    pred_original_sample: Optional[torch.FloatTensor] = None

class NestedScheduler(DDIMScheduler):

    def set_timesteps(self, num_inference_steps: int, max_timestep: int = 1000, device: Union[str, torch.device] = None):
        """
        Sets the discrete timesteps used for the diffusion chain. Supporting function to be run before inference.

        Args:
            num_inference_steps (`int`):
                the number of diffusion steps used when generating figures with a pre-trained model.
            max_timestep (`int`):
                the highest timestep to use for choosing the timesteps
        """

        if num_inference_steps > self.config.num_train_timesteps:
            raise ValueError(
                f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:"
                f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle"
                f" maximal {self.config.num_train_timesteps} timesteps."
            )

        self.num_inference_steps = num_inference_steps
        max_timestep = min(self.config.num_train_timesteps - 1, max_timestep)
        timesteps = np.linspace(1, max_timestep, min(num_inference_steps, max_timestep)).round()[::-1].copy().astype(np.int64)
        self.timesteps = torch.from_numpy(timesteps).to(device)

    def step(
        self,
        model_output: torch.FloatTensor,
        timestep: int,
        sample: torch.FloatTensor,
        eta: float = 0.0,
        use_clipped_model_output: bool = False,
        generator=None,
        variance_noise: Optional[torch.FloatTensor] = None,
        return_dict: bool = True,
        override_prediction_type = '',
    ) -> Union[NestedSchedulerOutput, Tuple]:
        """
        Predict the sample at the previous timestep by reversing the SDE. Core function to propagate the diffusion
        process from the learned model outputs (most often the predicted noise).

        Args:
            model_output (`torch.FloatTensor`): direct output from learned diffusion model.
            timestep (`int`): current discrete timestep in the diffusion chain.
            sample (`torch.FloatTensor`):
                current instance of sample being created by diffusion process.
            eta (`float`): weight of noise for added noise in diffusion step.
            use_clipped_model_output (`bool`): if `True`, compute "corrected" `model_output` from the clipped
                predicted original sample. Necessary because predicted original sample is clipped to [-1, 1] when
                `self.config.clip_sample` is `True`. If no clipping has happened, "corrected" `model_output` would
                coincide with the one provided as input and `use_clipped_model_output` will have not effect.
            generator: random number generator.
            variance_noise (`torch.FloatTensor`): instead of generating noise for the variance using `generator`, we
                can directly provide the noise for the variance itself. This is useful for methods such as
                CycleDiffusion. (https://arxiv.org/abs/2210.05559)
            return_dict (`bool`): option for returning tuple rather than DDIMSchedulerOutput class

        Returns:
            [`~schedulers.scheduling_utils.DDIMSchedulerOutput`] or `tuple`:
            [`~schedulers.scheduling_utils.DDIMSchedulerOutput`] if `return_dict` is True, otherwise a `tuple`. When
            returning a tuple, the first element is the sample tensor.

        """
        if self.num_inference_steps is None:
            raise ValueError(
                "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"
            )

        # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf
        # Ideally, read DDIM paper in-detail understanding

        # Notation (<variable name> -> <name in paper>
        # - pred_noise_t -> e_theta(x_t, t)
        # - pred_original_sample -> f_theta(x_t, t) or x_0
        # - std_dev_t -> sigma_t
        # - eta -> η
        # - pred_sample_direction -> "direction pointing to x_t"
        # - pred_prev_sample -> "x_t-1"

        # 1. get previous step value (=t-1)
        # prev_timestep = timestep - self.config.num_train_timesteps // self.num_inference_steps
        cur_idx = (self.timesteps == timestep).nonzero().item()
        prev_timestep = self.timesteps[cur_idx + 1] if cur_idx < len(self.timesteps) - 1 else 0

        # 2. compute alphas, betas
        alpha_prod_t = self.alphas_cumprod[timestep]
        alpha_prod_t_prev = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod

        beta_prod_t = 1 - alpha_prod_t

        # 3. compute predicted original sample from predicted noise also called
        # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
        prediction_type = override_prediction_type if override_prediction_type else self.config.prediction_type
        if prediction_type == "epsilon":
            pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
            pred_epsilon = model_output
        elif prediction_type == "sample":
            pred_original_sample = model_output
            pred_epsilon = (sample - alpha_prod_t ** (0.5) * pred_original_sample) / beta_prod_t ** (0.5)
        elif prediction_type == "v_prediction":
            pred_original_sample = (alpha_prod_t**0.5) * sample - (beta_prod_t**0.5) * model_output
            pred_epsilon = (alpha_prod_t**0.5) * model_output + (beta_prod_t**0.5) * sample
        else:
            raise ValueError(
                f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, `sample`, or"
                " `v_prediction`"
            )

        # 4. Clip or threshold "predicted x_0"
        if self.config.thresholding:
            pred_original_sample = self._threshold_sample(pred_original_sample)
        elif self.config.clip_sample:
            pred_original_sample = pred_original_sample.clamp(
                -self.config.clip_sample_range, self.config.clip_sample_range
            )

        # 5. compute variance: "sigma_t(η)" -> see formula (16)
        # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1)
        variance = self._get_variance(timestep, prev_timestep)
        std_dev_t = eta * variance ** (0.5)

        if use_clipped_model_output:
            # the pred_epsilon is always re-derived from the clipped x_0 in Glide
            pred_epsilon = (sample - alpha_prod_t ** (0.5) * pred_original_sample) / beta_prod_t ** (0.5)

        # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
        pred_sample_direction = (1 - alpha_prod_t_prev - std_dev_t**2) ** (0.5) * pred_epsilon

        # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
        prev_sample = alpha_prod_t_prev ** (0.5) * pred_original_sample + pred_sample_direction

        if eta > 0:
            if variance_noise is not None and generator is not None:
                raise ValueError(
                    "Cannot pass both generator and variance_noise. Please make sure that either `generator` or"
                    " `variance_noise` stays `None`."
                )

            if variance_noise is None:
                variance_noise = torch.randn(
                    model_output.shape, generator=generator, device=model_output.device, dtype=model_output.dtype
                )
            variance = std_dev_t * variance_noise

            prev_sample = prev_sample + variance

        if not return_dict:
            return (prev_sample,)

        return NestedSchedulerOutput(prev_sample=prev_sample, pred_original_sample=pred_original_sample)


import sys
import os
import shutil
import zipfile

from cx_Freeze import setup, Executable

APP_NAME = "MergeLogs"

# ビルドに含めるパッケージとモジュールを指定
packages = ['module']
includes = []
excludes = []

# 実行ファイルの設定
exe = Executable(
    script='main.py',  # 実行ファイルとなるスクリプト
    targetName=(APP_NAME + '.exe'),  # 出力ファイル名
    base=None  # コンソールアプリケーションとしてビルド
)

# セットアップ実行で exe作成
setup(
    name='MergeLogs',
    version='0.1',
    description='Parse logs And Merge to csv file',
    options={
        'build_exe': {
            'packages': packages,
            'includes': includes,
            'excludes': excludes,
            'include_files': [],
            'optimize': 0,
            'include_msvcr': True,
        }
    },
    executables=[exe],
    copyright=(
        "Copyright (c) 2023 "
        "Pinfu-jp. "
        "All rights reserved."
    ),)

# 設定ファイルをコピーしてくる
json_name = APP_NAME + ".json"
src_path = os.path.join(".", json_name)
dest_path = os.path.join(".", "build", "exe.win-amd64-3.11", json_name)
if os.path.exists(dest_path):
    os.remove(dest_path)
shutil.copy(src_path, dest_path)

dest_path = os.path.join(".", "build", "exe.win-amd64-3.11")

# 作成されたexe等の出力先を開く
if sys.platform == 'win32':
    os.system('explorer "{}"'.format(os.path.abspath(dest_path)))

# # ZIP化
# zip_path = os.path.join(".", "package", APP_NAME + ".zip")
# if os.path.exists(zip_path):
#     os.remove(zip_path)
# with zipfile.ZipFile(zip_path, mode='w', compression=zipfile.ZIP_DEFLATED) as zipf:
#     zipf.write(dest_path)


#!/usr/bin/env python3

# Requires python3 >= 3.4, and stress-ng to be installed.

import subprocess
import sys
from statistics import stdev, mean

def measure():
  time_seconds = float(str(subprocess.check_output("stress-ng -c2 --cpu-method ackermann --cpu-ops 10 | grep -o '[0-9][0-9\.]*s'", shell=True), encoding="utf-8").strip().rstrip("s"))
  return time_seconds

sample = [measure() for x in range(1,50)]
s = stdev(sample)
m = mean(sample)
relative_stdev = s / m

print("Standard deviation: ", s)
print("Relative standard deviation: ", relative_stdev)

if relative_stdev < 0.16:
  print("not bugged")
else:
  print("bugged")
  sys.exit(1)


from datetime import datetime, timedelta

#Local imports
import dictionaries as D
import date_compute as Date

from markup import CC, exact_length, remove_chars

#Common objects
def_song_dict = D.default_song_dict #Dictionary for song data.

class Editable():
    """An abstract object that allows you to give it any initial attributes using dictionaries."""

    def __init__(self, default_dict, real_dict):
        """
        default_dict: dictionary, giving the default values for each variable.
        real_dict: dictionary, giving the real values for each variable, if it differs from the default_dict values.

        Creates an instance with the attributes listed by default_dict. Values are given by real_dict unless they don't exist
        in real_dict, in which case they come from default_dict.
        """

        for attribute in default_dict: #Get all desired attributes

            if attribute in real_dict: #If attribute has a unique value
                value = real_dict[attribute] #Get value

            else: #If attribute has a generic value given by default_dict

                value = default_dict[attribute] #Get value

            self.__dict__ [attribute] = value #Set attribute value

    def get_attrs(self):
        """
        Returns: a dictionary containing the default attributes of the object as keys and the current values as values.
            {"attribute": value}
        """

        return self.__dict__

class Song(Editable):
    """A class that stores data and methods for one song."""

#Basic functionality
    def __init__(self, real_dict):
        super().__init__(def_song_dict, real_dict) #Use Editable's useful properties

    def __str__(self):
        """When a string is requested, the song name will be displayed."""
        name = self.song_name
        artist = self.artist
        return name + ' by ' + artist

    def __repr__(self):
        """When the song needs to be represented, the song name with be displayed."""
        return self.__str__()

    def disp(self):
        """
        Allows for a simple way to display all song information.

        Returns: String containing line breaks between each pair of keys and values in a user-friendly form.
        """
        output = '' #Text to be printed

        for attribute in def_song_dict: #Print every attribute

            value = self.__dict__[attribute]  #Get value

            new_text = str(attribute) + ': ' + str(value) + '\n' #Convert to full string (with line break '\n')
            output+=new_text #Add it to what you display

        return output

#Update
    def sang_today(self):
        """
        Indicate that you sang a song today.

        Edited Attributes: self.last_sung, self.dates_sung, self.how_many_times, self.xp.

        Calls Class Functions: self.check_streak, self.check_level
        """
        today = Date.get_today() #Get the date as string

        if today!=self.last_sung: #If it's a new date from last sung, then add it to the data.
            self.dates_sung=[today]+self.dates_sung
            self.last_sung=today

        self.how_many_times+=1 #Increase times sung
        self.xp+=1 #Increase xp

        #Check streaks and level after this event
        self.check_streak()
        self.check_level()

#Check data
    def check_level(self):
        """
        Check to see if the level needs to be updated, either by demotion or promotion.

        Calls Class Functions: self.check_demotion, self.check_promotion
        """
        current_level=self.current_level

        if current_level==20 or self.levelfreeze: #If you're level 20, or level is frozen, then make no edits
            return None

        #Check whether to demote or promote
        self.check_demotion()
        self.check_promotion()

    def check_demotion(self):
        """
        Check whether to demote this song based on days since last sung.

        Calls Class Functions: self.demote, self.get_days_since_last
        """

        days_since=self.get_days_since_last() #Get days since last sung
        days_past_deadline = days_since - D.leveldict[self.current_level]["days_to_lose"] #Get days to start losing xp

        #Conditions
        song_past_deadline = days_past_deadline>0
        song_above_level_zero = self.current_level>0

        if song_past_deadline and song_above_level_zero: #If too long, demote
            self.demote()

    def check_promotion(self):
        """
        Check whether to promote this song to next level based on your current level and xp.

        Edited Attributes: self.xp, self.current_level
        """

        new_level=self.current_level+1 #Get new level
        xp_to_next_level = D.leveldict[ new_level ]["req_xp"] #Get xp to reach next level

        #Conditions
        streak_is_long_enough = self.longest_streak>=D.leveldict[new_level]["req_streak"]
        xp_is_high_enough = self.xp>=xp_to_next_level

        if streak_is_long_enough and xp_is_high_enough:
            #If longest streak is sufficiently long, and xp is high enough, upgrade

            song = self.song_name
            level = CC('R',str(new_level))

            print(CC('G','Level up!'),\
                  song, 'is now level', level+'.')

            #Change values
            self.current_level=new_level
            self.xp=0

    def check_streak(self):
        """
        Check to see if the current streak needs to be updated.
        Will also update highest streak.
        Streak is broken if it has been more than one day since you've sung.

        Edited Attributes: self.current_streak, self.longest_streak
        """

        if self.last_sung == None: #If never sung
            self.current_streak = 0

        yesterday = Date.n_days_ago(1)
        last_sung = Date.string_to_date(self.last_sung) #Last time song was sung

        streak_broken = ( yesterday > last_sung) #Yesterday is more recent than last sung, too long

        if streak_broken:
            self.current_streak = 0 #If the streak is broken, reset
            return None

        last_date = Date.n_days_ago(0)#Start from today
        streak_length = 0 #Start fresh

        for day in self.dates_sung:
            date = Date.string_to_day(day)

            desired_dates = {last_date, last_date - timedelta(days = 1) } #The expected or the day before are both acceptable

            if date in desired_dates: #Streak is unbroken
                streak_length += 1
                last_date = date - timedelta(days = 1)#Go back in time

            else: #Streak ends
                break

        self.current_streak = streak_length #Set streak

        if self.current_streak > self.longest_streak: #Update longest streak if necessary
            self.longest_streak=self.current_streak

#Apply changes with no user input
    def demote(self):
        """
        Demoting this song to remove either XP or levels. Only removes levels if XP has reached below zero.
        Previously penalized days will not be penalized repeatedly with XP losses.
        Level will not drop below zero or half of the maximum level achieved.

        Edited Attributes: self.xp, self.current_level, self.last_demoted
        """
        level = self.current_level
        days_allowed = D.leveldict[level]["days_to_lose"] #Days you can skip with no penalty

        days_past_deadline = self.get_days_since_last() - days_allowed #Total days

        def get_penalty_days():
            """Get how many days you should penalize the late player for."""

            if self.last_demoted != None: #If it has been demoted previously, check how recently
                today = Date.get_today()
                last_demotion = Date.string_to_date(self.last_demoted)

                days_since_demotion = (today - last_demotion).days #Compare today to last demotion

                if days_since_demotion<days_past_deadline: #If it's been demoted recently, don't penalize days already demoted
                    how_many_days_late = days_since_demotion
                else: #Otherwise, it's an old demotion, and just use how late you are
                    how_many_days_late = days_past_deadline

            else: #If never demoted, then you don't need to worry about previous demotion days
                how_many_days_late = days_past_deadline

            return how_many_days_late #Number of days to penalize for being late

        def apply_penalty():
            """Apply penalty days to remove xp."""
            for penalty_day in range(get_penalty_days()): #Iterate over penalty days

                if self.current_level <= self.max_level//2: #If your level is low, stop penalizing
                    return None

                xp_to_next_level = D.leveldict[self.current_level+1]["req_xp"] #XP to level up

                xp_loss = xp_to_next_level//4 #Get xp loss
                xp_loss = xp_loss if xp_loss>=1 else 1 #Make sure xp_loss is at least 1
                self.xp -= xp_loss#Remove a quarter of this level's xp

                song = CC('R',self.song_name)
                print(song, 'has lost xp.')

                self.last_demoted = Date.get_today() #Set last demotion

                if self.xp < 0: #Remove a level
                    self.current_level-=1
                    next_level = self.current_level + 1

                    print(song, 'has lost a level.')

                    xp_to_next_level = D.leveldict[next_level]["req_xp"] #XP to reach old level
                    self.xp= round( xp_to_next_level*(3/4) ) #Set xp to 3/4 of what it takes to reach next level

        apply_penalty()

#Display data

        def list_item(self,xp_show=False):
            """
            Compactly print this song's most important parameters.

            xp_show: If xp_show is set to False, then the total number of times this song has been played will be displayed.
                     If xp_show is set to True, then the "current_xp/xp_to_next_level" will be displayed.

            Calls Class Functions: self.get_days_since_last
            """
            days_since_last_sung = self.get_days_since_last()

            #Convert all strings to the correct length
            song      = exact_length( self.song_name,          15 )
            level     = exact_length( str(self.current_level),  2 )
            artist    = exact_length( self.artist,             15 )
            streak    = exact_length( str(self.current_streak), 2 )
            last_sung = exact_length( self.last_sung,          10 )

            #Check whether to display xp or

            if xp_show: #Display XP
                xp = str(self.xp) #Get xp
                xp_to_next = D.leveldict[ self.current_level + 1] ["req_xp"] #Get xp goal

                xp_ratio = xp + '/' + xp_to_next #Combine as xp/goal
                xp_or_total = exact_length( xp_ratio, 5) #Correct length

            else: #Display total
                total =  'x' + str( self.how_many_times ) #Get total times sung
                xp_or_total = exact_length( total, 3 )

            #Final display

            V = CC('R', ' | ') #Draw red vertical bar

            print( song +              ": L." + level +       ( V ) + artist + ( V )+ \

                   "S:"+streak +        ( V )  + xp_or_total + ( V ) + \

                   'Last: '+last_sung+  ( V )  + 'Days:', days_since_last_sung)

#View data

        def get_days_since_last(self):

            if self.last_sung == None: #If never sung
                return 'N/A'

            #Get dates to compare
            today = Date.string_to_date(      Date.get_today()  )
            last_sung = Date.string_to_date(  self.last_sung  )

            if today == last_sung: #Zero days since sung
                return 0

            time_difference = today - last_sung #Get the timedelta if not same delta

            return time_difference.days #Get *days*

            #####################


"""Blackcap job POST route."""

from http import HTTPStatus
import json

from flask import make_response, request, Response
from pydantic import parse_obj_as, ValidationError
from sqlalchemy.exc import SQLAlchemyError

from blackcap.blocs.cluster import create_cluster
from blackcap.routes.cluster import cluster_bp
from blackcap.schemas.api.cluster.post import ClusterPOSTRequest, ClusterPOSTResponse
from blackcap.schemas.user import User
from blackcap.utils.auth import check_authentication

@cluster_bp.post("/")
@check_authentication
def post(user: User) -> Response:
    """Post cluster.

    Args:
        user (User): Extracted user from request

    Returns:
        Response: Flask response
    """
    # Parse json from request
    try:
        cluster_create_request_list = parse_obj_as(
            ClusterPOSTRequest, json.loads(request.data)
        ).cluster_list
    except ValidationError as e:
        response_body = ClusterPOSTResponse(
            msg="json validation failed", errors={"main": e.errors()}
        )
        return make_response(response_body.json(), HTTPStatus.BAD_REQUEST)
    except Exception:
        response_body = ClusterPOSTResponse(
            msg="unknown error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)

    # ceate cluster in DB and publish msg
    try:
        cluster_list = create_cluster(cluster_create_request_list, user)
    except SQLAlchemyError:
        response_body = ClusterPOSTResponse(
            msg="internal databse error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)
    except Exception:
        response_body = ClusterPOSTResponse(
            msg="unknown error", errors={"main": ["unknown internal error"]}
        )
        return make_response(response_body.json(), HTTPStatus.INTERNAL_SERVER_ERROR)

    # return created cluster in response
    response_body = ClusterPOSTResponse(
        msg="cluster successfully created", items={"cluster_list": cluster_list}
    )
    return make_response(response_body.json(), HTTPStatus.OK)


import os
import glob
from copy import copy

Import ('env')

test_env = env.Clone()

test_env['LIBS'] = [env['MAPNIK_NAME']]
test_env.AppendUnique(LIBS=copy(env['LIBMAPNIK_LIBS']))
test_env.AppendUnique(LIBS='mapnik-wkt')
test_env.AppendUnique(LIBS='sqlite3')
if env['PLATFORM'] == 'Linux':
    test_env.AppendUnique(LIBS='dl')
    test_env.AppendUnique(LIBS='rt')
test_env.AppendUnique(CXXFLAGS='-g')
test_env['CXXFLAGS'] = copy(test_env['LIBMAPNIK_CXXFLAGS'])
test_env.Append(CPPDEFINES = env['LIBMAPNIK_DEFINES'])
if test_env['HAS_CAIRO']:
    test_env.PrependUnique(CPPPATH=test_env['CAIRO_CPPPATHS'])
    test_env.Append(CPPDEFINES = '-DHAVE_CAIRO')
test_env.PrependUnique(CPPPATH='include', delete_existing=True)
test_env['LINKFLAGS'] = copy(test_env['LIBMAPNIK_LINKFLAGS'])
if env['PLATFORM'] == 'Darwin':
    test_env.Append(LINKFLAGS='-F/ -framework CoreFoundation')

test_env_local = test_env.Clone()

benchmarks = glob.glob("src/*.cpp")

for src in benchmarks:
    name, ext = os.path.splitext(os.path.basename(src))
    out = os.path.join("out", name)
    test_program = test_env_local.Program(out, source=[src])
    if 'install' in COMMAND_LINE_TARGETS:
        env.Alias('install',test_program)
    #Depends(test_program, env.subst('../src/%s' % env['MAPNIK_LIB_NAME']))


#coding:utf-8
'''
各进程的变量是独立的，而线程的是共享的，如果不处理这个问题会导致出错.
由于线程的调度是由操作系统决定的，当t1、t2交替执行时，只要循环次数足够多，balance的结果就不一定是0了
给change_it 加上一个锁
'''
from threading import Thread, Lock
import time

#实例化一个锁对象
lock = Lock()
balance = 0

def change_it(n):
    #先存后取，结果应该为0
    global balance
    balance += n
    balance -= n

def run_thread(n):
    for i in range(100000):
        #对change_it加锁,其它线程就不能执行change_it()
        lock.acquire()
        try:
            change_it(n)
        #一定要记得在操作结束后释放锁
        finally:
            lock.release()

if '__name__' == '__main__':
    t1 = Thread(target=run_thread, args=(5,))
    t2 = Thread(target=run_thread, args=(8,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print(balance)


import socket
import sys
import struct
import csv
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_PSS
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256

# Sign message with private key
def sign_message(private_key, message):
    h = SHA256.new(message)
    signer = PKCS1_PSS.new(private_key)
    signature = signer.sign(h)

    return signature

# Verify signature with public key
def verify_signature(public_key, signature, h):
    verifier = PKCS1_PSS.new(public_key)
    if verifier.verify(h, signature):
        print("Signature valid :)")
        return True
    else:
        print("Signature invalid :(")
        return False

try:
    with open('clients_keys.csv', 'r') as f:
        reader = csv.reader(f, delimiter=',')
        data = list(reader)
        public_key_n = int(data[0][1])
        public_key_e = int(data[0][2])

        # Reconstruct the public key from the keydata
        data_key = [public_key_n, public_key_e]
        public_key = RSA.construct(data_key)

        # print("public_key hex = ", public_key_n)
        # public_key_n = bytes.fromhex(public_key_n)
        # print("public_key comverted back = ", public_key_n)
        # public_key_n = public_key_n.decode("utf-8")
        # print("public_key decoded = ", public_key_n)
except FileNotFoundError:
    print("File doesn't exist.")

sk_client = RSA.importKey(open('private_key_CID0.pem', 'r').read())

message = 'hoi'
print("message = ", message)
signature = sign_message(sk_client, message.encode("utf-8"))
print("signature = ", signature)
h = SHA256.new(message.encode("utf-8"))
verify_signature(public_key, signature, h)


import pandas as pd

def getSample(act, pred):
    sample = sum(2 * abs(pred - act)/(pred + act)) / pred.size
    return sample

def getMonoScore(test_df, preds):
    test_sample_df = test_df[['广告id', '曝光广告出价bid']]

    test_sample_df['预测曝光量'] = preds
    test_sample_df.sort_values(by=["广告id", "曝光广告出价bid"], inplace=True)

    # 作为基准
    standard = test_sample_df.groupby(by='广告id').head(1)
    standard.rename(columns={'曝光广告出价bid': '基准出价', '预测曝光量': '基准曝光量'}, inplace=True)

    test_sample_df = pd.merge(test_sample_df, standard, how="left", left_on='广告id', right_on='广告id')

    def getScore(x):
        if x['基准曝光量'] == x['预测曝光量'] or x['基准出价'] == x['曝光广告出价bid']:
            return 1
        else:
            return ((x['基准曝光量'] - x['预测曝光量']) * (x['基准出价'] - x['曝光广告出价bid'])) / abs(
                (x['基准曝光量'] - x['预测曝光量']) * (x['基准出价'] - x['曝光广告出价bid']))

    test_sample_df['score'] = test_sample_df.apply(lambda x: getScore(x), axis=1)

    monoscore = test_sample_df.groupby(by='广告id')['score'].mean().mean()
    #     print("经过相关性计算成绩为："+str(monoscore))
    return monoscore


from downloader.config import AllowDelete
from downloader.constants import K_BASE_PATH, K_ALLOW_DELETE
from downloader.db_entity import DbEntity, DbEntityValidationException

class OfflineImporter:
    def __init__(self, file_system_factory, file_downloader_factory, logger):
        self._file_system_factory = file_system_factory
        self._file_downloader_factory = file_downloader_factory
        self._logger = logger

    def apply_offline_databases(self, importer_command):
        self._logger.bench('Offline Importer start.')

        for db, store, config in importer_command.read_dbs():
            for db_file in db.db_files:
                db_importer = _OfflineDatabaseImporter(config, self._file_system_factory.create_for_config(config), self._file_downloader_factory, self._logger)
                db_importer.update_store_from_offline_db(db.db_id, db_file, store)
                store.write_only().set_base_path(config[K_BASE_PATH])

        self._logger.bench('Offline Importer done.')

class _OfflineDatabaseImporter:
    def __init__(self, config, file_system, file_downloader_factory, logger):
        self._config = config
        self._file_system = file_system
        self._file_downloader_factory = file_downloader_factory
        self._logger = logger

    def update_store_from_offline_db(self, store_id, db_file, store):
        write_store = store.write_only()
        read_store = store.read_only()

        if not self._file_system.is_file(db_file):
            return

        hash_db_file = self._file_system.hash(db_file)
        if hash_db_file in read_store.offline_databases_imported:
            self._remove_db_file(db_file)
            return

        self._logger.print()
        try:
            db = DbEntity(self._file_system.load_dict_from_file(db_file), store_id)
        except DbEntityValidationException as e:
            self._logger.print('WARNING! Offline database "%s", could not be load from file %s' % (store_id, db_file))
            self._logger.debug(e)
            self._logger.print(str(e))
            self._logger.print('Ignoring the offline database.')
            return

        self._logger.print('Importing %s into the local store.' % db_file)

        self._import_folders(db.folders, read_store, write_store)
        self._import_files(db.files, read_store, write_store)

        errors = []
        if len(db.zips) > 0:
            errors.extend(self._update_from_zips(db, read_store, write_store))

        if len(db.files) > 0:
            self._logger.print()
        self._logger.print()

        if len(errors) == 0:
            write_store.add_imported_offline_database(hash_db_file)
            self._remove_db_file(db_file)
        else:
            for e in errors:
                self._logger.print('Offline importer error: ' + e)
            self._logger.print()

    def _update_from_zips(self, db, read_store, write_store):
        summary_downloader = self._file_downloader_factory.create(self._config, parallel_update=True)
        zip_ids_by_temp_zip = dict()

        zip_ids_to_download = []
        zip_ids_from_internal_summary = []

        for zip_id, zip_desc in db.zips.items():
            if 'summary_file' in zip_desc:
                zip_ids_to_download.append(zip_id)
            elif 'internal_summary' in zip_desc:
                zip_ids_from_internal_summary.append(zip_id)

        for zip_id in zip_ids_from_internal_summary:
            summary = db.zips[zip_id]['internal_summary']
            self._import_folders(summary['folders'], read_store, write_store)
            self._import_files(summary['files'], read_store, write_store)
            db.zips[zip_id].pop('internal_summary')
            write_store.add_zip(zip_id, db.zips[zip_id])

        temp_filename = self._file_system.unique_temp_filename()

        for zip_id in zip_ids_to_download:
            temp_zip = '%s_%s.json.zip' % (temp_filename.value, zip_id)
            zip_ids_by_temp_zip[temp_zip] = zip_id

            summary_downloader.queue_file(db.zips[zip_id]['summary_file'], temp_zip)

        temp_filename.close()

        self._logger.print()
        self._logger.print()
        summary_downloader.download_files(False)
        self._logger.print()

        for temp_zip in summary_downloader.correctly_downloaded_files():
            summary = self._file_system.load_dict_from_file(temp_zip)

            zip_id = zip_ids_by_temp_zip[temp_zip]

            if 'summary_file' in db.zips[zip_id] and 'unzipped_json' in db.zips[zip_id]['summary_file']:
                db.zips[zip_id]['summary_file'].pop('unzipped_json')

            write_store.add_zip(zip_id, db.zips[zip_id])
            self._import_folders(summary['folders'], read_store, write_store)
            self._import_files(summary['files'], read_store, write_store)
            self._file_system.unlink(temp_zip)

        return summary_downloader.errors()

    def _import_files(self, files, read_store, write_store):
        for file_path, file_description in files.items():
            if self._file_system.is_file(file_path) and \
                    (file_description['hash'] == 'ignore' or self._file_system.hash(file_path) == file_description['hash']) and \
                    file_path not in read_store.files:
                write_store.add_file(file_path, file_description)

                self._logger.print('+', end='', flush=True)

    def _import_folders(self, db_folders, read_store, write_store):
        for folder_path, folder_description in db_folders.items():
            if self._file_system.is_folder(folder_path) and folder_path not in read_store.folders:
                write_store.add_folder(folder_path, folder_description)

    def _remove_db_file(self, db_file):
        if self._config[K_ALLOW_DELETE] == AllowDelete.ALL:
            self._file_system.unlink(db_file)


#### Origin : https://www.topcoder.com/thrive/articles/web-crawler-in-python

import requests
import lxml
from bs4 import BeautifulSoup
from xlwt import *

url = "https://www.manchester.ac.uk/study/international/study-abroad-programmes/study-abroad/course-units/subject-list/"
headers = {
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
f = requests.get(url, headers = headers)
soup = BeautifulSoup(f.content, 'lxml')

data = soup.select("table tbody tr")

workbook = Workbook(encoding = 'utf-8')
table = workbook.add_sheet('data')
table.write(0, 0, 'Number')
table.write(0, 1, 'Courses')
table.write(0, 2, 'Fall assessment available?')
table.write(0, 3, 'Faculty')
line = 1

tags = []
for  x in data :
  tags.extend((i.prettify() for i in x.find_all('td') ) )

####### Write the crawled data to Excel
k = 0
while k <= ( len(tags)//3 ):

  table.write(line, 0, line)
  table.write(line, 1, tags[k])
  table.write(line, 2, tags[k+1])
  table.write(line, 3, tags[k+2])
  line += 1
  k = k + 3

workbook.save('courses.xls')


import time
import wrap
from wrap import sprite
import tank,brick

wrap.world.create_world(800, 653,)
wrap.world.set_back_color(0,0,0)

lazer=tank.add_lazer()
lazer1=tank.add_lazer()
tank1 = sprite.add('battle_city_tanks', 400, 300, 'tank_player_size1_green1')
tank2 = sprite.add('battle_city_tanks', 300, 400, 'tank_enemy_size1_white1')

@wrap.always()
def lazer2():
    tank.lazer(tank2,lazer)
    tank.lazer(tank1,lazer1)

bricks =[]
for f in range(25):
    brick.bbrick(bricks,f,0)
    brick.bbrick(bricks,f,19)
    brick.bbrick(bricks,0,f)
    brick.bbrick(bricks,24,f)

brick.ctaf_cteny_vertikil(bricks, 1, 5, 10)
brick.ctaf_cteny_vertikil(bricks, 3, 3, 5)

brick.ctaf_cteny_gorizont(bricks, 3, 3, 5)

bullets = []
bullet2 = None
bullet = None

# tank1
tank1_speed = 4
tank1_angle = 4
fire_time = time.time() - 3
# tank2
tank2_speed = 5
tank2_angle = 3
fire_time2 = time.time() - 3

@wrap.on_key_always(wrap.K_w)
def game_play_w(keys):
    tank.move_tank(tank1, tank1_speed, 'tank_player_size1_green1', 'tank_player_size1_green2')
    for b in bricks:
        tank.blok(tank1,b)
    tank.tank_tank(tank1,tank2)
    tank.nop(tank1)

@wrap.on_key_always(wrap.K_a)
def game_play_w():
    tank.rotate(tank1, -tank1_angle)

@wrap.on_key_always(wrap.K_LEFT)
def game_play_w():
    tank.rotate(tank2, -tank2_angle)

@wrap.on_key_always(wrap.K_RIGHT)
def game_play_d():
    tank.rotate(tank2, tank2_angle)

@wrap.on_key_always(wrap.K_d)
def game_play_d():
    tank.rotate(tank1, tank1_angle)

@wrap.on_key_always(wrap.K_UP)
def game_play_up():
    tank.move_tank(tank2, tank2_speed, 'tank_enemy_size1_white1', 'tank_enemy_size1_white2')
    for b in bricks:
        tank.blok(tank2,b)
    tank.tank_tank(tank1, tank2)
    tank.nop(tank2)

@wrap.on_key_down(wrap.K_s)
def game_play_shot():
    global bullet
    global fire_time
    tim = time.time()
    yt = tim - fire_time
    if yt >= 3:
        bullet = tank.shot(tank1)
        bullets.append(bullet)
        print(bullets)
        fire_time = time.time()

@wrap.on_key_down(wrap.K_DOWN)
def game_play_shot():
    global fire_time2
    global bullet2
    tim = time.time()
    yt = tim - fire_time2
    if yt >= 3:
        bullet2 = tank.shot(tank2)
        bullets.append(bullet2)
        print(bullets)
        fire_time2 = time.time()

@wrap.always()
def game_play_shot():
    for b in bullets:
        for s in bricks:
            tank.blok(b,s)
        sprite.move_at_angle_dir(b, 10)
        g = sprite.is_collide_sprite(tank1, b)
        f = sprite.is_collide_sprite(tank2, b)
        if g == True:
            sprite.remove(b)
            sprite.remove(b)
            tank.boom(tank1)
            wrap.sprite.add_text('ПОБЕДА БЕЛЫЕ', 299, 400, text_color=[255, 250, 250], font_size=80)

        if f == True:
            sprite.remove(b)
            sprite.remove(b)
            tank.boom(tank2)
            wrap.sprite.add_text('ПОБЕДА ЗЕЛЁНЫЕ', 299, 315, text_color=[0, 140, 49], font_size=80)


#!/usr/bin/python3

if __name__ == "__main__":
    from sys import argv
    result = 0
    argc = len(argv)
    if argc > 1:
        for i in range(1, argc):
            result = result + int(argv[i])
    print("{}".format(result))