from ...application.domain.models import Classification
from ...application.domain.common import QueryParser
from ...application.domain.repositories import ClassificationRepository
from .rst_repository import RstRepository, RstLoader

class RstClassificationRepository(
        RstRepository[Classification], ClassificationRepository):
    """Restructuredtext Classification Repository"""

    def __init__(self, parser: QueryParser, loader: RstLoader) -> None:
        super().__init__(parser=parser, loader=loader,
                         item_class=Classification)
        self.counter = 0

    def load(self):
        for value in self.loader.nodes:
            classifier_ids = value.get('classifiers', [])
            if isinstance(classifier_ids, str):
                classifier_ids = [classifier_ids]

            for classifier_id in classifier_ids:
                self.counter += 1
                item = self.item_class(
                    **{'id': self.counter,
                        'classifier_id': classifier_id,
                       'task_id': value['id']})
                self.data[self._location][
                    str(self.counter)] = item  # type: ignore


import os
import tempfile
import math
import torch.distributed as dist
import numpy as np
import pandas as pd
import torch
import argparse
import torch.nn as nn
import torch.optim as optim
import torch.multiprocessing as mp
from apex.parallel import DistributedDataParallel as DDP
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.distributed import DistributedSampler
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
from LSTM_Model import STA_LSTM
from apex import amp
import random

torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '42594'
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3,4,5,6,7'
myseed = 42000

os.makedirs('../distributed_model', exist_ok=True)
def windows_input(input, output, window_range, pred_range):
    num = len(input) - window_range + 1 - pred_range
    input = input.unsqueeze(0)
    output = output.unsqueeze(0)
    seq_input = []
    for i in range(num):
        seq_input.append(input[:, i:i + window_range, :])
    input = torch.cat(seq_input, dim=0)
    seq_target = []
    for j in range(num):
        seq_target.append(output[:, window_range + j - 1:window_range + j - 1 + pred_range, :])
    output = torch.cat(seq_target, dim=0)
    return input, output

window_length=100
pred_length=10
data = pd.read_csv("../data/data_final.csv", index_col=0)
feature_num=len(data.columns)-1
data = data.values

input = torch.from_numpy(data[:,:feature_num]).float()
target = torch.from_numpy(data[:,feature_num:]).float()

config = {
    'epoch_num': 800,
    'batch_size': 50,
    'optim_hyper': {'lr': 0.0015, 'weight_decay': 0},
    'early_stop': 90,
    'optimizer': 'Adam',
    'save_name': 'sintering_model.pth'
}
loss_function = nn.MSELoss()
records = []

class dataset(Dataset):
    def __init__(self,
                 train_input,
                 train_target,
                 window_length,
                 pred_length,
                 mode='train'
                 ):
        self.mode = mode
        input_seq, output_seq = windows_input(train_input, train_target, window_length, pred_length)
        output_seq = output_seq.squeeze(-1)

        dev_split_start = round(len(input_seq) * 0.6)
        dev_split_end = dev_split_start + round(len(input_seq) * 0.2)
        if mode == 'train':
            indices = [i for i in range(dev_split_start) ]
        elif mode == 'dev':
            indices = [i for i in range(dev_split_start,dev_split_end) ]
        elif mode=='test':
            indices=[i for i in range(dev_split_end,len(input_seq)) ]
        self.data = input_seq[indices]
        self.target = output_seq[indices]

    def __getitem__(self, index):
        return self.data[index], self.target[index]
        # if self.mode in ['train', 'dev']:
        #     return self.data[index], self.target[index]
        # else:
        #     return self.data[index]

    def __len__(self):
        return len(self.data)

dataset_train = dataset(input,target,window_length,pred_length,mode='train')
dataset_dev=dataset(input,target,window_length,pred_length,mode='dev')
dataset_test=dataset(input,target,window_length,pred_length,mode='test')
test_dataloader = DataLoader(dataset=dataset_test,
                             batch_size=config['batch_size'],
                             shuffle=False,
                             drop_last=False,
                             pin_memory=True,
                             num_workers=0)

def set_up(rank, world_size):
    torch.cuda.set_device(rank)
    # 初始化某号进程组
    # 注：第一个参数选择后端，nccl后端是单机多卡情况下的推荐，比gloo快很多。
    dist.init_process_group("nccl", rank=rank, world_size=world_size)

def train_pro(train_dataloader, dev_dataloader, model, device, loss_fn, rank):
    early_point = 0
    min_loss = 10000
    # create model and move it to GPU with id rank
    model = model.cuda(rank)
    optimizer = getattr(torch.optim, config['optimizer'])(model.parameters(), **config['optim_hyper'])
    model, optimizer = amp.initialize(model, optimizer,
                                      opt_level='O1')
    ddp_model = DDP(model)
    for epoch in range(config['epoch_num']):
        ddp_model.train()
        for x, y in train_dataloader:
            optimizer.zero_grad()
            x, y = x.to(device), y.to(device)
            pred = ddp_model(x)
            loss = torch.sqrt(loss_fn(pred, y))
            with amp.scale_loss(loss, optimizer) as scaled_loss:
                scaled_loss.backward()
            optimizer.step()
        train_loss = dev_pro(dev_dataloader, ddp_model, device, loss_fn)
        records.append(train_loss)
        if (train_loss < min_loss):
            min_loss = train_loss
            if (rank == 0):
                print('Saving model (epoch = {:4d}, loss = {:.4f})'
                      .format(epoch + 1, min_loss))
            torch.save(ddp_model.state_dict(), '../distributed_model/{}'.format(config['save_name']))
            early_point = 0
        else:
            early_point += 1
        if early_point >= config['early_stop']:
            print("stop")
            if (rank == 0):
                loss_dev = pd.DataFrame(records)
                loss_dev.to_csv("loss_dev.csv")
            break
    # if rank == 0:
    #     print('Finished after {}'.format(epoch + 1))
    return min_loss

def dev_pro(dev_dataloader, model, device, loss_fn):
    model.eval()
    total_loss = 0
    for x, y in dev_dataloader:
        x, y = x.to(device), y.to(device)
        with torch.no_grad():
            pred = model(x)
            loss = loss_fn(pred, y)
        total_loss += loss.detach().cpu().item()
    total_loss = math.sqrt(total_loss / len(dev_dataloader))
    return total_loss

def test(tt_set, model, device):
    model.eval()  # set model to evalutation mode
    total_loss = torch.zeros(pred_length)
    preds = []
    for x, y in tt_set:  # iterate through the dataloader
        x = x.to(device)  # move data to device (cpu/cuda)
        y = y.to(device)
        with torch.no_grad():  # disable gradient calculation
            pred = model(x)  # forward pass (compute output)
            preds.append(pred.detach().cpu())  # collect prediction
            loss = torch.sum((pred - y) ** 2, dim=0)
        total_loss += loss.detach().cpu()
    total_loss = torch.sqrt(total_loss / len(tt_set.dataset))
    preds = torch.cat(preds, dim=0).numpy()  # concatenate all predictions and convert to a numpy array
    return preds, total_loss
def set_seed(seed):
    # 必须禁用模型初始化中的任何随机性。
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)

def run_fn(rank, world_size):
    set_seed(myseed)
    print(f"Running on rank {rank}.")
    set_up(rank, world_size)

    train_sampler = DistributedSampler(dataset_train)
    dl_train_dist = DataLoader(dataset=dataset_train,
                               batch_size=config['batch_size'],
                               sampler=train_sampler,
                               shuffle=False)
    dev_sampler = DistributedSampler(dataset_dev)
    dl_dev_dist = DataLoader(dataset=dataset_dev,
                             batch_size=config['batch_size'],
                             sampler=dev_sampler,
                             shuffle=False)

    model = STA_LSTM(feature_num=feature_num, sa_hidden=window_length*2, ta_hidden=window_length,
                     output_size=pred_length, length=window_length)

    RMSE = train_pro(
        dl_train_dist,
        dl_dev_dist,
        model,
        device='cuda',
        loss_fn=loss_function,
        rank=rank
    )

def main(run_fn, world_size):
    mp.spawn(run_fn, args=(world_size,), nprocs=world_size, join=True)

if __name__ == "__main__":
    n_gpus = torch.cuda.device_count()
    assert n_gpus >= 2, f"Requires at least 2 GPUs to run, but got {n_gpus}"
    world_size = n_gpus

    set_seed(myseed)
    main(run_fn, world_size)
    model_1 = STA_LSTM(feature_num=feature_num, sa_hidden=40, ta_hidden=40,
                       output_size=pred_length, length=window_length).cuda()
    os.makedirs('../distributed_model', exist_ok=True)
    ckpt = torch.load('../distributed_model/{}'.format(config['save_name']), map_location='cuda')
    model_1.load_state_dict({k.replace('module.', ''): v for k, v in ckpt.items()})
    preds, loss = test(test_dataloader, model_1, "cuda")

    loss_test = pd.DataFrame(loss.numpy())
    preds=pd.DataFrame()

    # _, y_true = windows_input(da, data_test_target, lookback, time_range)
    # y_true = y_true.squeeze().numpy()
    # r2_loss = r2_score(y_true, preds, multioutput='raw_values')
    #
    # r2_loss_test = pd.DataFrame(r2_loss)
    # loss_test.to_csv("loss_test.csv")
    # r2_loss_test.to_csv("r2_loss_test.csv")


'''
Program to Map two lists into a Dictionary

'''

# Python Program to Map two lists into a Dictionary

keys = []
values = []
num = int(input("Please enter the Number of elements for this Dictionary : "))
print("Integer Values for Keys")
for i in range(0, num):
    x = int(input("Enter Key " + str(i + 1) + " = "))
    keys.append(x)

print("Integer Values for Values")
for i in range(0, num):
    x = int(input("Enter Value " + str(i + 1) + " = "))
    values.append(x)

myDict = dict(zip(keys, values))
print("Dictionary Items  :  ",  myDict)

Python Code/52.A.Dictionary Algorithms/6.Program to Map two lists into a Dictionary.py


# num = int(input())

def EvenOrOdd(n):
    if(n == 0):
        return 0
    if n % 2 == 0 and n % 2 != 0:
        return n + 3
    else:
        return n + 2

def EvenOdd(n):
    if (n == 0):
        return 0
    if n % 2 != 0:
        return n + 2
    elif n % 2 == 0:
        return  n + 2

def EvenOROdd(n):

    if n % 2 == 0 or n == 1:
        return n * 3
    else:
        return n * 2
#print(EvenOROdd(num))

num = input()

n = (int(num) % 2)

if(n == 1):
    print (int(num)*3)
else:
    print (int(num)*2)


#tátika
import sys
import random
q="q"
w="w"
e="e"
r="r"
t="t"
z="z"
u="u"
i="i"
side1=[q,w,e,r]
side2=[t,z,u,i]
#a kör ciklushoz kell
help1=1
help2=2
#váltzók kiválasztása és változtatása.
while help1<help2:
    print("the variables : " ,q,w,e,r,t,z,u,i)
    var=str(input("which variable do you want to change?: "))
    new_var=str(input("the new variable:"))
    if var=="q":
        q=new_var
    if var=="w":
        w=new_var
    if var=="e":
        e=new_var
    if var=="r":
        r=new_var
    if var=="t":
        t=new_var
    if var=="z":
        z=new_var
    if var=="u":
        u=new_var
    if var=="i":
        i=new_var
    help1=help1+1
    run_the_code=str(input("do you want to play the [tátika]?(i)"))
    if run_the_code == "i":
        while help1==help2:
            tatika_help=int(input("choose a number 1-10: "))
            side_help=tatika_help%2
            if side_help==0:
                print("◉ ◉ ◉ ◉")
                tatika_help2=int(input("choose a number 1-4  :"))
                if tatika_help2==1:
                    print(q)
                if tatika_help==2:
                    print(w)
                if tatika_help==3:
                    print(e)
                if tatika_help==4:
                    print(r)

            if side_help!=0:
                print(side2)
    else:
        print("")
        help2=help2+1


from typing import Any
import numpy as np
from numpy.linalg import norm
from numpy.linalg import pinv
import pinocchio as pin
import time
from pinocchio.utils import rotate
from bezier import Bezier
from config import DT, LEFT_HAND, RIGHT_HAND
from scipy.optimize import fmin_bfgs
import math

# in my solution these gains were good enough for all joints but you might want to tune this.
Kp = 1200  # proportional gain (P of PD)
Ki = 24
i = 0
Kd = 2 * np.sqrt(Kp)

CUBE_PLACEMENT_UP = pin.SE3(rotate("z", 0.0), np.array([0.33, -0.3, 1.13]))

graspForce = 100
cubeweight = 100
graspTorque = 1

def getGraspForces(sim, robot):
    fext = [pin.Force.Zero()] * (robot.nv + 1)

    lid = (
        sim.bulletCtrlJointsInPinOrder.index(sim.bullet_names2indices["LARM_EFF"] - 1)
        + 1
    )
    rid = (
        sim.bulletCtrlJointsInPinOrder.index(sim.bullet_names2indices["RARM_EFF"] - 1)
        + 1
    )

    fext[lid] = pin.Force(np.array([-graspForce, 0, -cubeweight, 0, 0, graspTorque]))
    fext[rid] = pin.Force(np.array([-graspForce, 0, -cubeweight, 0, 0, -graspTorque]))

    fext_wrapper = pin.StdVec_Force()
    for ext_force in fext:
        fext_wrapper.append(ext_force)

    return fext_wrapper

def controllaw(sim, robot, trajs, tcurrent, cube):
    global i
    q, vq = sim.getpybulletstate()
    q_of_t, vq_of_t, vvq_of_t = trajs

    # PID
    e_q = q_of_t(tcurrent) - q
    i += e_q * DT
    e_qd = vq_of_t(tcurrent) - vq
    dvq = Kp * e_q + Kd * e_qd + i * Ki + vvq_of_t(tcurrent)

    # Add grasp force
    graps_ext = getGraspForces(sim, robot)

    b = pin.rnea(robot.model, robot.data, q, vq, dvq, graps_ext)
    M = pin.crba(robot.model, robot.data, q)

    torques = M @ dvq + b

    sim.step(torques)

if __name__ == "__main__":
    from tools import setupwithpybullet, setupwithpybulletandmeshcat, rununtil

    robot, sim, cube = setupwithpybullet()

    from config import CUBE_PLACEMENT, CUBE_PLACEMENT_TARGET
    from inverse_geometry import computeqgrasppose
    from path import computepath

    q0, successinit = computeqgrasppose(robot, robot.q0, cube, CUBE_PLACEMENT, None)
    qe, successend = computeqgrasppose(
        robot,
        robot.q0,
        cube,
        CUBE_PLACEMENT_TARGET,
        None
        # robot, robot.q0, cube, CUBE_PLACEMENT_UP, None
    )
    path = computepath(robot, cube, q0, qe, CUBE_PLACEMENT, CUBE_PLACEMENT_TARGET)
    # path = computepath(robot, cube, q0, qe, CUBE_PLACEMENT, CUBE_PLACEMENT_UP)

    # setting initial configuration
    sim.setqsim(q0)

    # TODO this is just an example, you are free to do as you please.
    # In any case this trajectory does not follow the path
    # 0 init and end velocities

    class curve_wrapper:
        def __init__(self, curves, maxtime=10):
            self.curves = curves
            self.segments = len(curves)
            self.T = maxtime

        def __call__(self, t):
            curveNo = math.floor(((t / self.T) * self.segments))
            if curveNo == self.segments:
                curve = self.curves[self.segments - 1]
                return curve(1)
            else:
                curve = self.curves[curveNo]
                t = t * self.segments / self.T - curveNo
                return curve(t)

    def getControls(path, segments):
        start = path[0]
        end = path[-1]
        noPoints = len(path) - 2
        segmentSize = noPoints // segments
        print(noPoints, segmentSize)
        controls = [None] * segments
        curves = [None] * segments
        for i in range(segments - 1):
            path[(i + 1) * segmentSize] = (
                path[(i + 1) * segmentSize - 1] + path[(i + 1) * segmentSize + 1]
            ) / 2
            controls[i] = path[(i * segmentSize) : (i + 1) * segmentSize + 1]

        controls[0] = [start] * 2 + controls[0]
        controls[-1] = path[(segments - 1) * segmentSize + 1 :] + [end] * 2

        for i in range(segments):
            curves[i] = Bezier(
                controls[i],
                t_max=1,
            )

        return curves

    def maketraj(q0, q1, T):  # TODO compute a real trajectory !
        curves = getControls(path, 4)
        vqs = [curve.derivative(1) for curve in curves]
        vvqs = [vq.derivative(1) for vq in vqs]
        curves = curve_wrapper(curves, T)
        vqs = curve_wrapper(vqs, T)
        vvqs = curve_wrapper(vvqs, T)

        return curves, vqs, vvqs

    # TODO this is just a random trajectory, you need to do this yourself
    total_time = 5.0
    trajs = maketraj(q0, qe, total_time)

    from tools import setupwithmeshcat

    if True:
        robot, cube, viz = setupwithmeshcat()
        for t in np.linspace(0, total_time, 100):
            viz.display(trajs[0](t))

    tcur = 0.0

    while tcur < total_time:
        rununtil(controllaw, DT, sim, robot, trajs, tcur, cube)
        tcur += DT


import logging
import time
import unittest

from cros.factory.device import device_utils
from cros.factory.test import state
from cros.factory.test.utils import stress_manager
from cros.factory.utils.arg_utils import Arg
from cros.factory.goofy.plugins import plugin_controller

class StressAppTest(unittest.TestCase):
  """Run stressapptest to test the memory and disk is fine."""

  ARGS = [
      Arg('seconds', int, 'Time to execute the stressapptest.', default=60),
      Arg('memory_ratio', float, 'Radio of memory to be used by stressapptest.',
          default=0.9),
      Arg(
          'free_memory_only', bool,
          'Only use free memory for test. When set to True, only '
          'memory_radio * free_memory are used for stressapptest.',
          default=True),
      Arg('wait_secs', int,
          'Time to wait in seconds before executing stressapptest.', default=0),
      Arg('disk_thread', bool,
          'Stress disk using -f argument of stressapptest.', default=True),
      Arg(
          'disk_thread_dir', str,
          'Directory of disk thread file will be placed '
          '(default to system stateful partition.)', default=None),
      Arg('max_errors', int, 'Number of errors to exit early.',
          default=stress_manager.DEFAULT_MAX_ERRORS),
      Arg('num_threads', int,
          'Number of threads to be used. Default to number of cores.',
          default=None),
      Arg('taskset_args', list,
          'Argument to taskset to change CPU affinity for stressapptest.',
          default=None),
      Arg('scaling_min_freq', int,
          'Argument to set the value of scaling_min_freq.', default=None),
      Arg('scaling_max_freq', int,
          'Argument to set the value of scaling_max_freq.', default=None),
      Arg('scaling_governor', str,
          'Argument to set the value of scaling_governor.', default=None)
  ]

  def setUp(self):
    self.dut = device_utils.CreateDUTInterface()
    self.goofy = state.GetInstance()
    self._cpu_freq_manager = plugin_controller.GetPluginRPCProxy(
        'cpu_freq_manager')

  def runTest(self):
    # Wait other parallel tests memory usage to settle to a stable value, so
    # stressapptest will not claim too much memory.
    if self.args.wait_secs:
      time.sleep(self.args.wait_secs)

    cpufreq_to_value = {
        'scaling_min_freq': self.args.scaling_min_freq,
        'scaling_max_freq': self.args.scaling_max_freq,
        'scaling_governor': self.args.scaling_governor
    }
    self._cpu_freq_manager.SetFrequency(cpufreq_to_value)

    try:
      with stress_manager.StressManager(self.dut).Run(
          duration_secs=self.args.seconds,
          memory_ratio=self.args.memory_ratio,
          free_memory_only=self.args.free_memory_only,
          disk_thread=self.args.disk_thread,
          disk_thread_dir=self.args.disk_thread_dir,
          max_errors=self.args.max_errors,
          num_threads=self.args.num_threads,
          taskset_args=self.args.taskset_args,
      ):
        pass
    except stress_manager.StressManagerError as e:
      logging.error('StressAppTest failed: %s', e)
      raise
    finally:
      self.goofy.WaitForWebSocketUp()
      self._cpu_freq_manager.RestoreFrequency()


import sys

a,b = map(int,sys.stdin.readline().split())

num = [0]

for i in range(46): # 1000
    for j in range(i):
        num.append(i)

print(sum(num[a:b+1]))


import kivy
from kivy.properties import ObjectProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen
import points

class Type5Window(Screen):

    video = ObjectProperty(None)
    answer = ""
    ppp = 0
    start = 0

    def update(self, btn):

        self.start = self.start + 1
        if self.start == 3:
            self.start = 0

        if self.start == 0:
            self.ids.video.source = "bubble_sort.gif"
        elif self.start == 1:
            self.ids.video.source = "scalanie.gif"
        elif self.start == 2:
            self.ids.video.source = "wstawianie.gif"

        if self.ids.video.source == "scalanie.gif" and self.answer == "B":
            self.ppp = self.ppp + 1
            points.globalPoint += 1
            # self.ids.points.text = "POINTS: {}".format(self.ppp)
        elif self.ids.video.source == "wstawianie.gif" and self.answer == "C":
            self.ppp = self.ppp + 1
            points.globalPoint += 1
            # self.ids.points.text = "POINTS: {}".format(self.ppp)
        elif self.ids.video.source == "bubble_sort.gif" and self.answer == "A":
            self.ppp = self.ppp + 1
            points.globalPoint += 1
            # self.ids.points.text = "POINTS: {}".format(self.ppp)

        self.ids.answerA.active = False
        self.ids.answerB.active = False
        self.ids.answerC.active = False

    AMOUNT = 30
    seconds = AMOUNT
    widget = None

    def start1(self):
        Clock.schedule_interval(self.Callback_Clock, 1)

    def Callback_Clock(self, dt):
        self.seconds = self.seconds - 1
        self.time.text = "00:{}".format(self.seconds)
        if self.seconds == 0:
            self.time.text = "FINISH"
            self.seconds = self.AMOUNT
            self.confirm.trigger_action(duration=0.1)
        if self.confirm.state == "down":
            self.seconds = self.AMOUNT


import boto3
import logging
import hashlib
import os
import requests
from s3_client_lib.utils import *
from botocore.client import Config

logger = logging.getLogger(__name__)
import re

class S3Client:
    def __init__(
        self,
        address,
        access_key,
        secret_access_key,
        tenant=None,
        signature_version='s3v4',
    ):
        self.address = address
        config = Config(signature_version=signature_version)
        self.client = boto3.client(
            's3',
            endpoint_url=address,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_access_key,
            config=config,
        )

        def extend_bucket_with_tenant(
            request,
            signing_name,
            region_name,
            signature_version,
            request_signer,
            operation_name,
            **kwargs,
        ):
            try:
                logger.debug(f"BEFORE `{kwargs}`")
                logger.debug(f"BEFORE `{request.context}`")
                logger.info(f"BEFORE parsing bucket `{request.url}`")
                if tenant and request is not None:
                    bucket = None
                    match = re.match(r"(https|http):\/\/.+?\/(.*?)\/.*", request.url)
                    if match is not None:
                        bucket = match.group(2)
                    else:
                        # no bucket!!
                        logger.error(f"Cannot find bucket in url `{request}`, {kwargs}")
                        return
                    # bucket = request.context['signing'].get('bucket', kwargs.get('bucket', None))
                    logger.info(f' {tenant} => {bucket}')
                    request.url = request.url.replace(bucket, f'{tenant}%3A{bucket}')
                else:
                    logger.info('TENANT IS NOT DEFINED => do nothing')
            except Exception as e:
                logger.error(f'Error {e}')
        self.client.meta.events.register(
            'before-sign.s3.UploadPart', extend_bucket_with_tenant
        )
        self.resource = boto3.resource(
            's3',
            endpoint_url=address,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_access_key,
        )

    def finish_file_metadata(self, bucket, object_name, filename):
        s3_object = self.get_object_head(bucket, object_name)
        logger.debug(f'Fetched metadata header {s3_object}')
        self.update_metadata_object(
            bucket,
            object_name,
            {
                'contentLength': str(s3_object['ContentLength']),
                'contentType': s3_object['ContentType'],
                'contentName': filename,
            },
        )

    def create_bucket_if_not_exists(self, bucket_name, **kwargs):
        """
        Creates bucket in S3
        :param bucket_name:
        :param kwargs: {
            "ACL": 'private'|'public-read'|'public-read-write'|'authenticated-read',
            "Bucket": 'string',
            "CreateBucketConfiguration": {
                'LocationConstraint': 'EU'|'eu-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'sa-east-1'|'cn-north-1'|'eu-central-1'
            },
            "GrantFullControl"='string',
            "GrantRead"='string',
            "GrantReadACP"='string',
            "GrantWrite"='string',
            "GrantWriteACP"='string',
            "ObjectLockEnabledForBucket"=True|False
            }
        :return: dict
        """
        bucket = self.resource.Bucket(bucket_name)

        if bucket.creation_date:
            logger.info(f'The bucket exists {bucket_name}')
        else:
            logger.info(f'The bucket does not exist {bucket_name}')
            return self.client.create_bucket(Bucket=bucket_name, **kwargs)

    def upload_local_file(self, local_file, bucket, object_name):
        """

        :param local_file:
        :param object_name:
        :param size:
        :return:
        """
        # file is greater then 512 mb we have to upload it with multipart
        file_size = os.path.getsize(os.path.abspath(local_file))
        if file_size > GB_1:
            raise Exception('File size is greater than 1GB use S3MultipartClient')
        response_api = self.sign_s3_upload(bucket, object_name)
        s3_request_data = response_api['data']['fields']
        url = response_api['data']['url']
        filename = os.path.basename(local_file)
        with open(local_file, 'rb') as rf:
            logger.debug(f'Presigned upload => url: {url} fields: {s3_request_data}')
            s3_response = requests.post(
                url, data=s3_request_data, files={'file': (filename, rf)}
            )
            logger.debug(f'Response {s3_response} {s3_response.text}')
            self.finish_file_metadata(bucket, object_name, filename)
            return 'Upload completed'

    def sign_s3_upload(
        self, bucket, object_name, fields=None, conditions=None, expires=3600
    ) -> dict:
        """
        Create presigned url for upload of object to S3. This method is for smaller files.

        :param object_name: The name of the bucket to presign the post to
        :param fields:  A  dictionary  of  prefilled  form  fields  to  build  on  top  of.   Elements  that  may  be
                        included  are  acl,  Cache-Control,  Content-Type,  Content-Disposition, Content-Encoding,
                        Expires, success_action_redirect, redirect, success_action_status, and x-amz-meta-.
                        Note that if a particular element is included in the fields dictionary it will not
                        be automatically added to the conditions list.  You must specify a condition
                        for the element as well.
        :param conditions: A list of conditions to include in the policy.
                        Each element can be either a list or a structure.
                        For example:[{“acl”:  “public-read”}, [”content-length-range”, 2, 5],
                            [”starts-with”, “$success_action_redirect”, “”]]
                        Conditions that are included may pertain to acl,  content-length-range,  Cache-Control,
                        Content-Type,  Content-Disposition,  Content-Encoding,  Expires,  success_action_redirect,
                        redirect, success_action_status, and/or x-amz-meta-.Note that if you include a condition,
                        you must specify the a valid value in the fields dictionary as well.
                        A value will not be added automatically to the fields dictionary based on the conditions.
        :param expires: The number of seconds the presigned post is valid for.
        :return: {
            "data": {...},
            "url": string
        }
        """
        # object_name += "-${filename}"
        presigned_post = self.client.generate_presigned_post(
            Bucket=bucket,
            Key=object_name,
            Fields=fields,
            Conditions=conditions,
            ExpiresIn=expires,
        )
        url = os.path.join(os.path.join(self.address, bucket), object_name)
        return {'data': presigned_post, 'url': f'{url}'}

    def sign_s3_download(self, bucket, object_name, filename, expires=3600) -> dict:
        """
        Pre-signed url for download of file.
        :param bucket: The name of the bucket where is object
        :param object_name: The name of the object to presign
        :param filename: How should file named after download
        :param expires: The  number  of  seconds  the  presigned  url  is  valid  for.
                    By default it expires in an hour (3600 seconds)
        :return:{
            "url": string
        }
        """

        logger.debug(f'Signing url for {object_name} in bucket {bucket}')
        logger.info(f'Signing url for {object_name} in bucket {bucket}')

        presigned_url = self.client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': bucket,
                'Key': object_name,
                'ResponseContentDisposition': f'attachment; attachment; filename={filename}',
            },
            ExpiresIn=expires,
        )
        return {'url': presigned_url}

    def copy_from_s3(
        self, bucket, object_name, destination_path, chunk_size=CHUNK_SIZE_16M
    ):
        """
        Function will copy data from s3 to destination_path. For download is used get_object function which is dict which
        contains in Body key StreamingBody -> loading by chunks.

        :param object_name: The name of the object
        :param destination_path: destination where should object be downloaded
        :param chunk_size:
        :return:
        """

        logger.info(
            f'Copy data from S3 for: {object_name} from bucket: {bucket} to path: {destination_path}'
        )
        response = self.client.get_object(Bucket=bucket, Key=object_name)

        digest = hashlib.sha256()
        try:
            with open(destination_path, 'wb') as wf:
                for chunk in response['Body'].iter_chunks(chunk_size):
                    if chunk is None or not any(chunk):
                        break
                    digest.update(chunk)
                    wf.write(chunk)
            checksum_result = digest.hexdigest()
            logger.info(f'Checksum result: {checksum_result}')
            return checksum_result
        except Exception as e:
            logger.error(
                f'Something goes wrong in copy from S3 response: {response}, '
                f'destination = {destination_path}, '
                f'object_name: {object_name}'
                f'bucket: {bucket}'
                f'error: {e}'
            )

    def copy_to_s3(self, bucket, from_path, object_name, extra_args=None):
        """
        Copy file from local storage to S3.
        :param from_path: path to file
        :param object_name: how should object name
        :param extra_args: dict data which will be bind in object
        :return:
        """

        logger.info(
            f'Copy data to S3 for: {object_name} to bucket: {bucket} from path: {from_path}'
        )
        bucket = self.resource.Bucket(bucket)
        return bucket.upload_file(from_path, object_name, ExtraArgs=extra_args)

    def copy_from_bucket_to_bucket(
        self, object_name, source_bucket, destination_bucket
    ):
        """
        Copy objects from one bucket to another
        :param object_name: The name of the object to copy
        :param source_bucket:
        :param destination_bucket:
        :return:
        """
        copy_source = {'Bucket': source_bucket, 'Key': object_name}
        logger.info(
            f'Copy data between buckets S3 for: {object_name} source bucket: {source_bucket} '
            f'destination bucket: {destination_bucket}'
        )
        to_bucket = self.resource.Bucket(destination_bucket)
        return to_bucket.copy(copy_source, object_name)

    @staticmethod
    def __paginate(bucket, marker, size, iterator):
        result = []
        page = None

        while True:
            response_iterator = iterator(bucket, marker)

            for page in response_iterator:
                logger.debug(page)
                result.append(page)
            if page is None or page.get('Marker') == '':
                return page, result
            if size is not None:
                if len(result) > size:
                    return page.get('Marker', ''), result
            try:
                previous = marker
                marker = page.get('Marker', '')
            except KeyError:
                break
            if previous == marker and len(previous) > 0 and len(marker) > 0:
                break

    def list_objects(
        self, bucket=None, prefix=None, max_items=100, start_from=None, size=100
    ):
        """
        List objects from bucket

        :param prefix: filter objects which starts with
        :param max_items: max items on page
        :param start_from: marker from which will be data fetched
        :param size: max size of result data is is None return all data
        :return: marker, [objects]
        """
        config = {'PageSize': max_items}
        if prefix is not None:
            config.update({'Prefix': prefix})
        marker = start_from
        paginator = self.client.get_paginator('list_objects')

        def iterate(bucket, mark):
            config.update({'StartingToken': mark})
            return paginator.paginate(Bucket=bucket, PaginationConfig=config)

        return self.__paginate(bucket, marker, size, iterate)

    def search_objects(self, bucket, query, start_from=None, max_items=100, size=100):
        """
        Search by query in https://jmespath.org/ format.
        :param query:
        :param start_from: marker from start
        :param max_items: max items on page
        :param size: max size of result data is is None return all data
        :return: marker, [objects]
        """
        config = {'PageSize': max_items}
        marker = start_from
        paginator = self.client.get_paginator('list_objects')

        def iterate(bucket, mark):
            config.update({'StartingToken': mark})
            page_iterator = paginator.paginate(Bucket=bucket, PaginationConfig=config)
            return page_iterator.search(query)

        return self.__paginate(bucket, marker, size, iterate)

    def list_buckets(self):
        """
        List all buckets from S3
        :return:
        """
        return self.client.list_buckets()

    def delete_object(self, bucket, object_name):

        logger.debug(f'Deleting object {object_name} from bucket {bucket}')
        response = self.resource.Object(bucket, object_name).delete()
        return response

    def update_metadata_object(self, bucket, object_name, metadata):
        logger.debug(
            f'Update metadata on object {object_name} in bucket: {bucket} With metdata: {metadata}'
        )
        s3_object = self.resource.Object(bucket, object_name)
        s3_object.metadata.update(metadata)
        result = s3_object.copy_from(
            CopySource={'Bucket': bucket, 'Key': object_name},
            Metadata=s3_object.metadata,
            MetadataDirective='REPLACE',
        )
        return result

    def get_object_head(self, bucket, object_name):
        """
        Get mostly metadatada about object.
        :param object_name:
        :return:
        """

        response = self.client.head_object(Bucket=bucket, Key=object_name)
        return response

    def get_object(self, bucket, object_name):
        """
        Get whole object
        :param object_name:
        :return:
        """
        response = self.client.get_object(Bucket=bucket, Key=object_name)
        return response

    def get_stream(self, bucket_name, object_name):
        logger.info(
            f'Getting stream data from S3 for: {object_name} from bucket: {bucket_name}'
        )
        response = self.resource.Object(bucket_name=bucket_name, key=object_name)
        return S3File(response)

    def copy_data_from_s3_by_chunks_with_calc_sha(
        self,
        bucket,
        object_name,
        destination_path,
        chunk_size=CHUNK_SIZE_128M,
        process_chunk=lambda y, x: (y, x),
    ):
        """
        Function will copy data from s3 to destination_path. For download is used get_object function which is dict which
        contains in Body key StreamingBody -> loading by chunks.

        :param object_name:
        :param destination_path:
        :param bucket:
        :param chunk_size:
        :return:
        """
        import hashlib
        logger.info(
            f'Copy data from S3 for: {object_name} from bucket: {bucket} to path: {destination_path}'
        )
        response = self.s3_client.get_object(Bucket=bucket, Key=object_name)
        digest = hashlib.sha256()
        size = 0
        try:
            with open(destination_path, 'wb') as wf:
                for idx, chunk in enumerate(response['Body'].iter_chunks(chunk_size)):
                    if chunk is None or not any(chunk):
                        break
                    size += len(chunk)
                    digest.update(chunk)
                    process_chunk(idx, chunk)
                    wf.write(chunk)

            checksum_result = digest.hexdigest()
            logger.info(f'Checksum result: {checksum_result}')
            return checksum_result
        except Exception as e:
            print(e)
            logger.error(
                f'{self.name}-> Something goes wrong in copy from S3 response: {response}, '
                f'destination = {destination_path}, '
                f'object_name: {object_name}'
                f'bucket: {bucket}'
            )


import random

from datetime import datetime, timedelta
from django.db import transaction
from django.db.models import OuterRef, Exists

from common.models import *
from config import *

class TimeUtil(object):
    @classmethod
    def format_time(cls, date_string):
        return datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")

class MoneyUtil(object):
    @classmethod
    def yuan_to_fen(cls, yuan):
        return round(100*yuan)

class PaymentUtil(object):
    @classmethod
    def gen_serial_number(cls, t_time):
        return t_time.strftime('%Y%m%d%H%M%S%f') + ('%012d' % random.randint(0, 999999999999))

class MockProcessUtil(object):

    @classmethod
    def mock_get_coupon(cls, client, coupon_rule, obtain_time, originator_merchant):
        new_coupon = Coupon.objects.create(
            rule=coupon_rule,
            client=client,
            discount=coupon_rule.discount,
            min_charge=coupon_rule.min_charge,
            originator_merchant=originator_merchant,
            status=COUPON_STATUS.NOT_USED,
            obtain_datetime=TimeUtil.format_time(obtain_time)
        )

        return new_coupon

    @classmethod
    def mock_request_payment(cls, user_id=None, merchant_id=None, coupon_id=None, order_price=0,
                            pay_time_str=None, pay_way=None):

        client = Client.objects.get(pk=user_id)
        merchant = Merchant.objects.get(pk=merchant_id)
        pay_time = TimeUtil.format_time(pay_time_str)

        coupon = None
        if coupon_id:
            coupon = Coupon.objects.get(pk=coupon_id)
            coupon.status = COUPON_STATUS['USED']
            coupon.use_datetime = pay_time
            coupon.save()

        paid_price = order_price - coupon.discount if coupon else order_price

        new_payment = Payment.objects.create(
            serial_number=PaymentUtil.gen_serial_number(pay_time),
            pay_channel=pay_way, # PAY_CHANNELS['WECHAT'],
            status=PAYMENT_STATUS['UNPAID'],
            merchant=merchant,
            client=client,
            coupon=coupon,
            order_price=order_price,
            datetime=pay_time,
            platform_share=round(max(0, paid_price) * PLATFORM_SHARE_RATE) if coupon else 0,
            inviter_share=round(max(0, paid_price) * INVITER_SHARE_RATE) if coupon else 0,
            originator_share=round(max(0, paid_price) * ORIGINATOR_SHARE_RATE) if coupon else 0,
        )

        return new_payment

    @classmethod
    def mock_on_payment_success(cls, platform_account_id, payment, time_str):
        """ mock_payment """
        with transaction.atomic():
            platform_account = Account.objects.select_for_update().get(id=platform_account_id)
            merchant_account = Account.objects.select_for_update().get(
                id=payment.merchant.account.id)
            payment = Payment.objects.select_for_update().get(serial_number=payment.serial_number)
            timestamp = TimeUtil.format_time(time_str)

            if payment.status != PAYMENT_STATUS['UNPAID']:
                raise Exception("payment's status is invalid") # Duplicated callback
            payment.status = PAYMENT_STATUS['FROZEN']
            payment.save()

            is_wechat_pay = True if payment.pay_channel == PAY_CHANNELS['WECHAT'] else False

            if not payment.coupon:  # 没有使用优惠券的情况
                merchant_account.balance = merchant_account.balance + payment.order_price
                merchant_account.save()

                Transaction.objects.bulk_create([
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_RECEIVE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=payment.order_price,
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) + payment.order_price
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EXPEND_MERCHANT_RECEIVE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-payment.order_price,
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['MERCHANT_RECEIVE'],
                        datetime=timestamp,
                        account=merchant_account,
                        amount=payment.order_price,
                        balance_after_transaction=merchant_account.balance if is_wechat_pay else merchant_account.alipay_balance
                    )
                ])
            else:  # 使用了优惠券的情况
                paid_price = payment.order_price - payment.coupon.discount
                total_share = payment.platform_share + payment.inviter_share + payment.originator_share

                if is_wechat_pay:
                    platform_account.balance = platform_account.balance + total_share
                    merchant_account.balance = merchant_account.balance + paid_price - total_share
                else:
                    platform_account.alipay_balance = platform_account.alipay_balance + total_share
                    merchant_account.alipay_balance = merchant_account.alipay_balance + paid_price - total_share
                platform_account.save()
                merchant_account.save()

                Transaction.objects.bulk_create([
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_RECEIVE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=paid_price,
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) + paid_price - total_share
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EXPEND_MERCHANT_RECEIVE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-(paid_price - total_share),
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['MERCHANT_RECEIVE'],
                        datetime=timestamp,
                        account=merchant_account,
                        amount=paid_price - total_share,
                        balance_after_transaction=merchant_account.balance if is_wechat_pay else merchant_account.alipay_balance
                    )
                ])

    @classmethod
    def mock_request_refund(cls, payment, time_str):
        with transaction.atomic():
            payment = Payment.objects.select_for_update().get(serial_number=payment.serial_number)

            if payment.status == PAYMENT_STATUS['FROZEN']:
                new_refund = Refund.objects.create(
                    serial_number=PaymentUtil.gen_serial_number(TimeUtil.format_time(time_str)),
                    status=REFUND_STATUS['REQUESTED'],
                    payment=payment,
                    datetime=TimeUtil.format_time(time_str)
                )
            else:
                raise Exception("refund's payment's status is invalid")

            payment.status = PAYMENT_STATUS['REFUND_REQUESTED']
            payment.save()

            return new_refund

    @classmethod
    def mock_on_refund_success(cls,  platform_account_id, refund, time_str):
        with transaction.atomic():
            platform_account = Account.objects.select_for_update().get(id=platform_account_id)
            merchant_account = Account.objects.select_for_update().get(
                id=refund.payment.merchant.account.id)

            payment = Payment.objects.select_for_update().get(
                serial_number=refund.payment.serial_number)
            refund = Refund.objects.select_for_update().get(serial_number=refund.serial_number)
            timestamp = TimeUtil.format_time(time_str)

            if refund.status != REFUND_STATUS['REQUESTED'] \
                    or payment.status != PAYMENT_STATUS['REFUND_REQUESTED']:
                raise Exception("mock_refund failed")

            refund.status = REFUND_STATUS['FINISHED']
            refund.save()

            payment.status = PAYMENT_STATUS['REFUND']
            payment.save()

            is_wechat_pay = True if payment.pay_channel == PAY_CHANNELS['WECHAT'] else False

            if not payment.coupon:  # 没有使用优惠券

                if is_wechat_pay:
                    merchant_account.balance = merchant_account.balance - payment.order_price
                else:
                    merchant_account.alipay_balance = merchant_account.alipay_balance - payment.order_price
                merchant_account.save()

                Transaction.objects.bulk_create([
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['MERCHANT_REFUND'],
                        datetime=timestamp,
                        account=merchant_account,
                        amount=-payment.order_price,
                        balance_after_transaction=merchant_account.balance if is_wechat_pay else merchant_account.alipay_balance
                    ),
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EARNING_MERCHANT_REFUND'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=payment.order_price,
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) + payment.order_price
                    ),
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_REFUND'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-payment.order_price,
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    )
                ])
            else:  # 有使用优惠券的情况
                paid_price = payment.order_price - payment.coupon.discount
                total_share = payment.platform_share + payment.inviter_share + payment.originator_share

                if is_wechat_pay:
                    merchant_account.balance = merchant_account.balance - (paid_price - total_share)
                    platform_account.balance = platform_account.balance - total_share
                else:
                    merchant_account.alipay_balance = merchant_account.alipay_balance - (paid_price - total_share)
                    platform_account.alipay_balance = platform_account.alipay_balance - total_share

                merchant_account.save()
                platform_account.save()

                Transaction.objects.bulk_create([
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['MERCHANT_REFUND'],
                        datetime=timestamp,
                        account=merchant_account,
                        amount=-(paid_price - total_share),
                        balance_after_transaction=merchant_account.balance if is_wechat_pay else merchant_account.alipay_balance
                    ),
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EARNING_MERCHANT_REFUND'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=(paid_price - total_share),
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) + paid_price
                    ),
                    Transaction(
                        content_object=refund,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_REFUND'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-paid_price,
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    )
                ])

                # 设置原优惠券为销毁状态，并退给用户一张新的优惠券
                coupon = Coupon.objects.select_for_update().get(id=payment.coupon_id)
                coupon.status = COUPON_STATUS.DESTROYED
                coupon.save()

                new_coupon = Coupon.objects.create(
                    rule_id=coupon.rule_id,
                    client_id=coupon.client_id,
                    discount=coupon.discount,
                    min_charge=coupon.min_charge,
                    originator_merchant_id=coupon.originator_merchant_id,
                    status=COUPON_STATUS.NOT_USED,
                    obtain_datetime=coupon.obtain_datetime,
                )
                new_coupon.save()

    @classmethod
    def mock_withdraw(cls, account, amount, time_str, withdraw_type):
        with transaction.atomic():
            timestamp = TimeUtil.format_time(time_str)

            is_wechat_pay = True if withdraw_type == WITHDRAW_TYPE['WECHAT'] else False

            account = Account.objects.select_for_update().annotate(
                has_merchant=Exists(Merchant.objects.filter(
                    account=OuterRef('id')
                ))).annotate(
                has_marketer=Exists(Marketer.objects.filter(
                    account=OuterRef('id')
                ))).get(id=account.id)

            if (account.withdrawable_balance if is_wechat_pay else account.alipay_withdrawable_balance) < amount:
                raise Exception("withdrawable_balance less than amount")
            if account.has_merchant:
                transaction_type = TRANSACTION_TYPE.MERCHANT_WITHDRAW
            elif account.has_marketer:
                transaction_type = TRANSACTION_TYPE.MARKETER_WITHDRAW
            else:
                raise Exception("Don't know the owner of the account.")

            new_withdraw = Withdraw.objects.create(
                withdraw_type=WITHDRAW_TYPE['WECHAT'] if is_wechat_pay else WITHDRAW_TYPE['ALIPAY'],
                serial_number=PaymentUtil.gen_serial_number(timestamp),
                status=WITHDRAW_STATUS['PROCESSING'],
                datetime=timestamp,
                account=account,
                amount=amount
            )

            if is_wechat_pay:
                account.withdrawable_balance = account.withdrawable_balance - amount
                account.balance = account.balance - amount
            else:
                account.alipay_withdrawable_balance = account.alipay_withdrawable_balance - amount
                account.alipay_balance = account.alipay_balance - amount
            account.save()

            Transaction.objects.create(
                content_object=new_withdraw,
                transaction_type=transaction_type,
                datetime=timestamp,
                account=account,
                amount=-amount,
                balance_after_transaction=account.balance
            )
            new_withdraw.status = WITHDRAW_STATUS['FINISHED']
            new_withdraw.save()

    @classmethod
    def mock_on_payment_unfreeze(cls, platform_account_id, payment, time_str):
        with transaction.atomic():

            timestamp = TimeUtil.format_time(time_str)
            payment = Payment.objects.select_for_update().select_related('coupon').get(
                serial_number=payment.serial_number)

            if payment.status != PAYMENT_STATUS.FROZEN:
                raise Exception("payment's status is invalid")

            is_wechat_pay = True if payment.pay_channel == PAY_CHANNELS['WECHAT'] else False

            if payment.coupon:  # 使用了优惠券的情况
                merchant_account_id = payment.merchant.account_id
                originator_account_id = payment.coupon.originator_merchant.account_id
                inviter_account_id = payment.merchant.inviter.account_id

                accounts = Account.objects.select_for_update().filter(id__in=(
                    platform_account_id, merchant_account_id, originator_account_id,
                    inviter_account_id))

                accounts = {a.id: a for a in accounts}
                if len(accounts) != 4:
                    raise Account.DoesNotExist()

                platform_account = accounts[platform_account_id]
                merchant_account = accounts[merchant_account_id]
                originator_account = accounts[originator_account_id]
                inviter_account = accounts[inviter_account_id]

                if is_wechat_pay:
                    platform_account.balance -= payment.originator_share + payment.inviter_share
                    originator_account.balance += payment.originator_share
                    originator_account.withdrawable_balance += payment.originator_share
                    inviter_account.balance += payment.inviter_share
                    inviter_account.withdrawable_balance += payment.inviter_share
                    merchant_account.withdrawable_balance += payment.order_price - payment.coupon.discount - sum(
                        (payment.platform_share,
                         payment.originator_share,
                         payment.inviter_share))
                else:
                    platform_account.alipay_balance -= payment.originator_share + payment.inviter_share
                    originator_account.alipay_balance += payment.originator_share
                    originator_account.alipay_withdrawable_balance += payment.originator_share
                    inviter_account.alipay_balance += payment.inviter_share
                    inviter_account.alipay_withdrawable_balance += payment.inviter_share
                    merchant_account.alipay_withdrawable_balance += payment.order_price - payment.coupon.discount - sum(
                        (payment.platform_share,
                         payment.originator_share,
                         payment.inviter_share))

                platform_account.save()
                originator_account.save()
                inviter_account.save()
                merchant_account.save()

                payment.status = PAYMENT_STATUS.FINISHED
                payment.save()

                # 记录Transaction
                Transaction.objects.bulk_create([
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EXPEND_MERCHANT_SHARE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-payment.originator_share,
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) + payment.inviter_share
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EXPEND_MARKETER_SHARE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-payment.inviter_share,
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_EXPEND_PLATFORM_SHARE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=-payment.platform_share,
                        balance_after_transaction=(platform_account.balance if is_wechat_pay
                                                   else platform_account.alipay_balance) - payment.platform_share
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['PLATFORM_SHARE'],
                        datetime=timestamp,
                        account=platform_account,
                        amount=payment.platform_share,
                        balance_after_transaction=platform_account.balance if is_wechat_pay else platform_account.alipay_balance
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['MERCHANT_SHARE'],
                        datetime=timestamp,
                        account=originator_account,
                        amount=payment.originator_share,
                        balance_after_transaction=originator_account.balance if is_wechat_pay else originator_account.alipay_balance
                    ),
                    Transaction(
                        content_object=payment,
                        transaction_type=TRANSACTION_TYPE['MARKETER_SHARE'],
                        datetime=timestamp,
                        account=inviter_account,
                        amount=payment.inviter_share,
                        balance_after_transaction=inviter_account.balance if is_wechat_pay else inviter_account.alipay_balance
                    )
                ])

            else:  # 没有使用优惠券的情况
                merchant_account_id = payment.merchant.account_id
                merchant_account = Account.objects.select_for_update().get(id=merchant_account_id)

                payment.status = PAYMENT_STATUS.FINISHED
                payment.save()

                if is_wechat_pay:
                    merchant_account.withdrawable_balance += payment.order_price
                else:
                    merchant_account.alipay_withdrawable_balance += payment.order_price
                merchant_account.save()


import tables
import numpy as np
import logging
logger = logging.getLogger('root')

class EnterExitPrint(object):
    """docstring for EnterExitPrint"""
    def __init__(self, enter, exit):
        super(EnterExitPrint, self).__init__()
        self.enter = enter
        self.exit = exit

    def __enter__(self):
        logger.info(self.enter)

    def __exit__(self ,type, value, traceback):
        logger.info(self.exit)

def count_events(files):
    n_events = 0
    for file in files:
        f = tables.open_file(file, 'r')
        n_events += f.root.label.shape[0]
        f.close()
    return n_events

def chunks(files, chunksize, max_q_size=4, shuffle=True, max_count=None):
    """Yield successive n-sized chunks from a and b."""
    i_chunk = 0
    for file in files:
        f = tables.open_file(file, 'r')
        nrows = f.root.label.nrows
        for istart in range(0,nrows,max_q_size*chunksize):
            i_chunk += 1
            if max_count and i_chunk > max_count:
                break
            a = np.array(f.root.img_pt[istart:istart+max_q_size*chunksize]) # Images
            b = np.array(f.root.label[istart:istart+max_q_size*chunksize]) # Labels
            if shuffle:
                c = np.c_[a.reshape(len(a), -1), b.reshape(len(b), -1)] # shuffle within queue size
                np.random.shuffle(c)
                test_images = c[:, :a.size//len(a)].reshape(a.shape)
                test_labels = c[:, a.size//len(a):].reshape(b.shape)
            else:
                test_images = a
                test_labels = b
            for jstart in range(0,len(test_labels),chunksize):
                yield normalize_and_rgb(test_images[jstart:jstart+chunksize].copy()),test_labels[jstart:jstart+chunksize].copy(), len(test_labels[jstart:jstart+chunksize].copy())
        f.close()

def normalize_and_rgb(images):
    #normalize image to 0-255 per image.
    image_sum = 1/np.sum(np.sum(images,axis=1),axis=-1)
    given_axis = 0
    # Create an array which would be used to reshape 1D array, b to have
    # singleton dimensions except for the given axis where we would put -1
    # signifying to use the entire length of elements along that axis
    dim_array = np.ones((1,images.ndim),int).ravel()
    dim_array[given_axis] = -1
    # Reshape b with dim_array and perform elementwise multiplication with
    # broadcasting along the singleton dimensions for the final output
    image_sum_reshaped = image_sum.reshape(dim_array)
    images = images*image_sum_reshaped*255

    # make it rgb by duplicating 3 channels.
    images = np.stack([images, images, images],axis=-1)
    return images

# def eval_performance(truths, predictions, times):


#!/bin/python3

import time
import sys
import os
from PIL import Image

on = os.system('ps -aux | grep "wall.py" | wc -l')

if on > 0:
    os.system('killall wall.py')

file = sys.argv[1]

if file == "":
    print("Dame un parametro plox, un gif")

if not file.endswith(".gif"):
    print("Introduce un gif")

if os.path.isdir('/tmp/wall'):
    os.system('rm -r /tmp/wall && mkdir /tmp/wall')
else:
    os.system('mkdir /tmp/wall')

num_key_frames = int(os.popen("identify "+file+"| wc -l").read())

with Image.open(file) as im:
    for i in range(num_key_frames):
        im.seek(im.n_frames // num_key_frames * i)
        im.save('/tmp/wall/{}.png'.format(i))

times = os.popen("identify "+file+" | cut -d ':' -f 2").read().split("s")
timing = [s.replace("\n", "") for s in times]
while("" in timing) :
    timing.remove("")

while True:

    for pic in zip(range(num_key_frames), timing):
        print(pic)
        os.system('feh --bg-fill /tmp/wall/'+str(pic[0])+'.png')
        time.sleep(float(pic[1])*10)
    pic=[]


import os
from Nude import Nude

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description='Detect nudity in images.')
    parser.add_argument('files', metavar='image', nargs='+',
                        help='Images you wish to test')
    parser.add_argument('-r', '--resize', action='store_true',
                        help='Reduce image size to increase speed of scanning')
    parser.add_argument('-v', '--visualization', action='store_true',
                        help='Generating areas of skin image')

    args = parser.parse_args()

    for fname in args.files:
        if os.path.isfile(fname):
            n = Nude(fname)
            if args.resize:
                n.resize(maxheight=800, maxwidth=600)
            n.parse()
            if args.visualization:
                n.showSkinRegions()
            print(n.result, n.inspect())
        else:
            print(fname, "is not a file")


from tkinter import *
from tkinter import ttk

def Margines(root,TextArea):
    Mar = ['Default',
           '10,10px',
           '20,20px',
           '30,30px',
           '40,40px',
           '50,50px']

    def selected(event):
        mar = clicked.get()

        if mar == 'Default':
            TextArea.config(padx=0, pady=0)
        elif mar == '10,10px':
            TextArea.config(padx=10, pady=10)
        elif mar == '20,20px':
            TextArea.config(padx=20, pady=20)
        elif mar == '30,30px':
            TextArea.config(padx=30, pady=30)
        elif mar == '40,40px':
            TextArea.config(padx=40, pady=40)

        elif mar == '50,50px':
            TextArea.config(padx=50, pady=50)

    clicked = StringVar()
    clicked.set(Mar[0])

    F = Frame(root, bd=3, relief=GROOVE)
    F.place(x=10, y=10, width=100, height=100)

    marginelabel = Label(F, text="Margins", font=("times new roman", 10, "bold"), bg="white", relief=GROOVE)
    marginelabel.place(x=10, y=15, width=75, height=23)

    margin_Box = OptionMenu(F, clicked, *Mar, command=selected)
    margin_Box.place(x=5, y=55)


import matplotlib.pyplot as plt
import os
import seaborn as sns
import numpy as np
import pingouin as pg

plt.rcParams["font.sans-serif"] = ["SimHei"]  # 用来正常显示中文标签

def model_overlap_plot(
    raw_data,
    sub_nums,
    assign_name,
    save_path=None,
    aspect_ratio=1.5,
    negative=False,
    print_corr=True,
    show_plot=False,
    title_fontsize=20,
    tick_fontsize=20,
):
    # Print the red text "===== Checking the consistency of the model capturing the experimental design ====="
    print("\033[31m===== 检测模型捕获实验设计的一致性 =====\033[0m")

    num_subs = len(sub_nums)  # Number of subjects
    num_models = len(assign_name)  # Number of models

    if save_path is not None:
        if not os.path.exists(save_path):
            os.makedirs(save_path)

    def value_data(value, negative=bool(negative)):
        return abs(1 - value) if negative else value

    # Calculate the new figure size based on the given aspect ratio
    new_fig_height = 5 * num_subs
    new_fig_width = new_fig_height * aspect_ratio

    # Create a new figure with subplots
    fig, axes = plt.subplots(
        num_subs, num_models, figsize=(new_fig_width, new_fig_height)
    )

    # Ensure axes is a 2D array even if there's only one row or one column
    if num_subs == 1:
        axes = axes.reshape(1, -1)
    if num_models == 1:
        axes = axes.reshape(-1, 1)

    # Loop through each subject and each model to populate the subplots
    for i, sub_num in enumerate(sub_nums):
        single_sub_data = raw_data[raw_data["sub_num"] == sub_num].reset_index(
            drop=True
        )

        # Extract and preprocess exp_design
        exp_design = single_sub_data["prop"].values
        if "MI" in exp_design:
            exp_design = [0.20 if x == "MI" else x for x in exp_design]
            exp_design = [0.80 if x == "MC" else x for x in exp_design]
        exp_design = [float(x) for x in exp_design]

        # Initialize a dictionary to store model estimates
        model_estimate = {}
        for names in assign_name:
            key = "_".join(names).split("_")[0:2]
            key = "_".join(key)
            model_estimate[key] = single_sub_data[names].mean(axis=1)

        # Plotting and correlation calculation
        colors = ["#e87e72", "#56bcc2"]
        for j, (key, value) in enumerate(model_estimate.items()):
            ax = axes[i, j]  # Select the corresponding subplot
            label = (
                "Bayesian Learning"
                if "bl" in key
                else "Reinforcement Learning"
                if "rl" in key
                else key
            )

            ax.plot(exp_design, color="darkblue", linewidth=5.0, label="exp_design")
            ax.plot(
                value_data(value, negative),
                color=colors[j],
                alpha=0.75,
                linewidth=5.0,
                label=label,
            )

            ax.set_title(
                f"Sub {sub_num}: {label}", fontsize=title_fontsize
            )  # Set y label fontsize
            ax.tick_params(axis="both", which="major", labelsize=tick_fontsize)
            ax.set_ylim([0, 1])

    plt.tight_layout()

    if print_corr:
        all_exp_design = raw_data["prop"].values
        if "MI" in all_exp_design:
            all_exp_design = [0.20 if x == "MI" else x for x in all_exp_design]
            all_exp_design = [0.80 if x == "MC" else x for x in all_exp_design]
        all_exp_design = [float(x) for x in all_exp_design]
        # Check for NaN values and raise an exception if found
        if np.isnan(exp_design).any() or np.isnan(value_data(value, negative)).any():
            raise ValueError(
                "Data contains NaN values, cannot calculate Pearson correlation."
            )

        for i in assign_name:
            # Calculate and print correlation and significance
            # 从 raw_data 中提取对应的所有 subject_num 的 value

            model_data = raw_data[i].mean(axis=1)
            value = np.array(model_data)

            if "bl" in i[0]:
                corr_label = "Bayesian Learning"
            elif "rl" in i[0]:
                corr_label = "Reinforcement Learning"

            # 计算 exp_design 和 values 的相关性
            # 使用 pingouin 进行统计分析
            # Convert the Series to a numpy array
            value_array = value_data(value, negative)

            # Calculate the correlation
            corr_results = pg.corr(all_exp_design, value_array)
            print(corr_results)

    # Save the figure to the specified path if given
    if save_path is not None:
        corr_results.to_csv(
            os.path.join(save_path, "correlation_results.csv"), index=False
        )
        plt.savefig(os.path.join(save_path, "model_overlap_plot.png"))
    if show_plot:
        plt.show()

    # Print the green text "========== Analysis completed =========="
    print("\033[32m========== 分析结束 ==========\033[0m")

# Update the function to include t-test for differences between volatility conditions
def plot_bl_v_boxplot(
    raw_data,
    subject_colname="sub_num",
    volatility_colname="volatile",
    bl_v_colname="bl_sr_v",
    colors=["#e87e72", "#56bcc2"],
    save_path=None,
    fig_size=(10, 6),
    print_ttest=True,
):
    print("\033[31m===== 贝叶斯模型箱线图绘制 =====\033[0m")
    # Convert volatility values to lowercase for consistency
    raw_data[volatility_colname] = raw_data[volatility_colname].str.lower()

    # Group the data by subject and volatility, then calculate the mean for each group
    grouped_data = (
        raw_data.groupby([subject_colname, volatility_colname])[bl_v_colname]
        .mean()
        .reset_index()
    )

    # Check if the required data for t-test is available
    if (
        grouped_data.empty
        or "s" not in grouped_data[volatility_colname].values
        or "v" not in grouped_data[volatility_colname].values
    ):
        raise ValueError("Insufficient data for t-test.")

    # Create the boxplot using seaborn
    plt.figure(figsize=(10, 6))
    sns.boxplot(x=volatility_colname, y=bl_v_colname, data=grouped_data, palette=colors)

    # Additional plot settings
    plt.title("Bayesian Learning")
    plt.xlabel("Volatility")
    plt.ylabel("Subject Mean Value")

    # Perform t-test between s and v conditions
    s_data = grouped_data[grouped_data[volatility_colname] == "s"][bl_v_colname]
    v_data = grouped_data[grouped_data[volatility_colname] == "v"][bl_v_colname]

    # 使用 pingouin 进行 t-test
    t_test_results = pg.ttest(s_data, v_data)

    if print_ttest:
        # 输出 APA 格式的报告
        print(t_test_results)

    if save_path is not None:
        # 保存 t-test 结果到 CSV 文件
        t_test_results.to_csv(
            os.path.join(save_path, "bl_ttest_results.csv"), index=False
        )
        # 保存 boxplot 图像
        fig = plt.gcf()
        fig.set_size_inches(fig_size[0], fig_size[1])
        plt.savefig(os.path.join(save_path, "bl_v_boxplot.png"), dpi=300)

    print("\033[32m========== 分析结束 ==========\033[0m")

# Re-plot the boxplot for RL model's alpha with all subjects included
def plot_rl_alpha_boxplot(
    alpha_table,
    subject_colname="sub_num",
    alpha_colnames=["alpha_s", "alpha_v"],
    colors=["#e87e72", "#56bcc2"],
    save_path=None,
    fig_size=(10, 6),
):
    print("\033[31m===== 强化学习模型箱线图绘制 =====\033[0m")
    # Check if the required columns exist in the dataframe
    for col in [subject_colname] + alpha_colnames:
        if col not in alpha_table.columns:
            print(f"Error: Missing required column '{col}' in the dataframe.")
            return

    # Check for missing values
    if alpha_table.isnull().any().any():
        print(
            "Warning: The dataframe contains missing values."
            "Please handle them before proceeding."
        )
        return

    # Reshape the data for boxplot
    melted_data = alpha_table.melt(
        id_vars=subject_colname,
        value_vars=alpha_colnames,
        var_name="volatile",
        value_name="alpha",
    )
    melted_data["volatile"] = melted_data["volatile"].map(
        {"alpha_s": "s", "alpha_v": "v"}
    )

    # Create the boxplot using seaborn
    plt.figure(figsize=(10, 6))
    sns.boxplot(x="volatile", y="alpha", data=melted_data, palette=colors)

    # Additional plot settings
    plt.title("Boxplot of RL Alpha")
    plt.xlabel("Volatility Condition")
    plt.ylabel("Alpha Value")

    # Perform t-test between s and v conditions
    s_data = alpha_table[next(col for col in alpha_colnames if "s" in col)]
    v_data = alpha_table[next(col for col in alpha_colnames if "v" in col)]
    # 使用 pingouin 进行 t-test
    t_test_results = pg.ttest(s_data, v_data)

    # 输出 APA 格式的报告
    print(t_test_results.round(3))

    if save_path is not None:
        t_test_results.to_csv(
            os.path.join(save_path, "rl_ttest_results.csv"), index=False
        )

        fig = plt.gcf()
        fig.set_size_inches(fig_size[0], fig_size[1])
        plt.savefig(os.path.join(save_path, "rl_alpha_boxplot.png"), dpi=300)

    print("\033[32m========== 分析结束 ==========\033[0m")


from .client import PORT

from helper.IPC import Server, FuncCall, Connection, Response, Packet
from helper.main_handler import main_handler
from helper.report_exceptions import report_exception

from .nearcrowd_account import NearCrowdAccount
from helper.provider_config import provider
from .accounts_db import Accounts
import helper.db_config as db_config
from .locks import get_lock
from .functions import *
import asyncio
from typing import Any
import sd_notify

server = Server(PORT, Connection, report_exception)
TIMEOUT = 30

async def get_account(account_id: str) -> NearCrowdAccount | None:
	try:
		private_key = await Accounts.get_key(account_id)
		return NearCrowdAccount(account_id, private_key)
	except Exception:
		return None

async def apply_accountless(call: FuncCall):
	return await call.apply(globals()[call.name])

async def apply_for_accounts(accounts: NearCrowdAccount | list[NearCrowdAccount], call: FuncCall):
	return await asyncio.gather(*[call.apply(globals()[call.name], i) for i in accounts])

async def get_accounts_list() -> list[NearCrowdAccount]:
	return [NearCrowdAccount(account_id, private_key) for account_id, private_key in await Accounts.get_accounts_credentials()]

async def lock_account(account: NearCrowdAccount):
	await get_lock(account.account_id).acquire()
	return account

async def lock_accounts(accounts: list[NearCrowdAccount]) -> list[NearCrowdAccount]:
	if len(accounts) == 0:
		return []

	tasks = [asyncio.create_task(lock_account(i)) for i in accounts]
	done, pending = await asyncio.wait(tasks)
	for i in pending:
		i.cancel()
	return [i.result() for i in done]

def unlock(accounts: list[NearCrowdAccount]):
	for i in accounts:
		get_lock(i.account_id).release()

async def get_locked_accounts(account_ids: list[str] | None) -> list[NearCrowdAccount]:
	assert isinstance(account_ids, list | None), type(account_ids)

	if account_ids is None:
		accounts = await get_accounts_list()
	else:
		accounts = await asyncio.gather(*[get_account(i) for i in account_ids])

	accounts = list(filter(lambda x: x is not None, accounts))
	return await lock_accounts(accounts)

def get_ids(accounts: list[NearCrowdAccount]) -> list[str]:
	return [i.account_id for i in accounts]

class ConnectionHandler:
	def __init__(self, conn: Connection) -> None:
		self.conn = conn
		self.accounts = []

	async def proceed_client(self):
		tasks = []
		while self.conn.is_active():
			try:
				packet: Packet = await self.conn.read(timeout=TIMEOUT)
				assert isinstance(packet, Packet), type(packet)
				tasks.append(asyncio.create_task(self._proceed_call(Response(self.conn, packet))))
			except TimeoutError:
				if all([task.done() for task in tasks]):
					print('Probably forgot to close connection', self.accounts)
					break
				else:
					continue
			except Exception:
				break

		for task in tasks:
			task.cancel()
		unlock(self.accounts)
		await self.conn.close()

	async def _proceed_call(self, resp: Response):
		await resp.respond(await self.execute_call(resp.data))

	async def execute_call(self, call: FuncCall) -> Any:
		assert isinstance(call, FuncCall), type(call)

		if call.name in ('set_accounts'):
			unlock(self.accounts)
			self.accounts = await call.apply(get_locked_accounts)
			return get_ids(self.accounts)

		if call.name in ('create_account', 'is_connected', 'get_coef', 'get_access_keys', 'verify_keys'):
			return await apply_accountless(call)

		assert call.name in ('query'), call.name
		result = await apply_for_accounts(self.accounts, call)
		return {k.account_id: v for k, v in zip(self.accounts, result)}

@server.on_connect
async def on_client_connect(conn: Connection):
	c = ConnectionHandler(conn)
	await c.proceed_client()

async def start():
	provider.start()
	await db_config.start()
	await server.start()
	sd_notify.Notifier().ready()

	await server.run()

async def stop():
	await provider.close()
	await server.stop()

if __name__ == '__main__':
	main_handler(start, report_exception, server.close)


import pygame as pg
import random
import os
import threading

# INFO: This code is a little bit messy
# TODO: Optimize code or cleaning
pg.init()

screen = pg.display.set_mode((800, 800))

clock = pg.time.Clock()

running = True

dt = 0

width = screen.get_width()
height = screen.get_height()
player_pos = pg.Vector2(width - (width / 2), height - (height / 10))
y_pos = 0
speed = 5
score = 0
high_score = 0
miss = 0
ctime = 0

x_pos = random.randint(10, width - 20)

white = (255, 255, 255)
pg.font.init()
font = pg.font.SysFont('firacode', 22)

with open('highScore.txt','r') as f:
    high_score =int( f.read() )

# fonts = pg.font.get_fonts()
# for f in fonts:
#    print(f)
#
sunny = pg.image.load(os.path.join('resource', 'sunny.png'))
boat = pg.image.load(os.path.join('resource', 'boat.png'))
sea = pg.image.load(os.path.join('resource', 'sea.jpg'))
sunny = pg.transform.scale(sunny,(50,50))
boat = pg.transform.scale(boat,(100,60))

choper = pg.mixer.music.load(os.path.join('resource', 'choper.mp3'))
theme = pg.mixer.music.load(os.path.join('resource', 'theme.mp3'))
pg.mixer.music.play(-1)

def effect(sounds,chan):
    # m=pg.mixer.Sound(os.path.join('resource', sounds))
    pg.mixer.Channel(chan).play(pg.mixer.Sound(os.path.join('resource', sounds))
)

t2 = threading.Thread(target=effect, args=('theme.mp3',0,))
t2.start()

while running:

    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False

    # ctime = time.time()
    screen.blit(sea,(0,0))
    # pg.draw.rect(screen, "blue",(player_pos,(100,30)) )
    screen.blit(boat, player_pos)
    # txt_time= font.render(f"{ctime} ", True, white)
    txt_score= font.render(f"Score : {score} ", True, white)
    txt_speed = font.render(f"Speed : {speed} ", True, white)
    txt_miss = font.render(f"Misses : {miss}", True, white)
    txt_high= font.render(f"High Score : {high_score}", True, white)
    # screen.blit(txt_time,(10,20))
    screen.blit(txt_score,(10,50))
    screen.blit(txt_speed,(10,80))
    screen.blit(txt_miss,(10,110))
    screen.blit(txt_high,(10,140))

    ball = pg.Vector2(x_pos, y_pos)

    if score >= 80:
        speed = 7
    if score >= 150:
        speed = 10

    if ball.y <= height:
       y_pos +=  speed

    else:

        t1 = threading.Thread(target = effect, args=("nami.mp3",1))
        t1.start()
        t1.join()
        y_pos = 0
        x_pos = random.randint(10, width - 20)
        miss+= 1
        if score > 0:
            score -= 20
        if score < 0:
            score = 0

    if ball.y >= player_pos.y - 50 and \
       ball.y <= player_pos.y and \
       ball.x >= player_pos.x and \
       ball.x <= player_pos.x + 100:
        y_pos = 0
        x_pos = random.randint(10, width - 20)
        t1 = threading.Thread(target = effect, args=("recieve.mp3",2))
        t1.start()
        t1.join()

        if score >= 300:
            score += 20
        elif score >=80 and score < 300:
            score += 15
        else:
            score += 10

    # pg.draw.rect(screen, "yellow", (ball, (5,50)))
    screen.blit(sunny, ball)
    keys = pg.key.get_pressed()

    # TODO: need to implement more function
    # if keys[pg.K_k]:
    #     player_pos.y -= 300 * dt
    # if keys[pg.K_j]:
    #     player_pos.y += 300 * dt
    if (keys[pg.K_h] or keys[pg.K_LEFT]) and player_pos.x >= 0:
        player_pos.x -= 500 * dt

    if (keys[pg.K_l] or keys[pg.K_RIGHT]) and player_pos.x <= (width - 100):
        player_pos.x += 500 * dt

    if score > high_score:
        high_score = score
        with open("highScore.txt", 'w') as f:
            f.write(str(score))

    # if miss >= 10:
    #     running = False
    pg.display.update()
    dt = clock.tick(60) / 1000
pg.quit()


import datetime
import os
import numpy as np

# classifiers
from model import ToMatrix
from neural_network import NeuralNetwork
from twinsvm.TVSVM import TwinSVMClassifier
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB, GaussianNB
from sklearn.pipeline import Pipeline
from sklearn.preprocessing.data import MinMaxScaler
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis, LinearDiscriminantAnalysis
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier

# features
from features.word_based.word_counts import WordCounts
from features.character_based.number_of_characters import NumberOfCharacters
from features.character_based.alpha_ratio import AlphaRatio
from features.character_based.digit_ratio import DigitRatio
from features.character_based.whitespace_ratio import WhitespaceRatio
from features.character_based.special_chars_ratio import SpecialCharsRatio
from features.word_based.number_of_words import NumberOfWords
from features.word_based.short_words_ratio import ShortWordsRatio
from features.word_based.average_word_len import AverageWordLen
from features.word_based.unique_words_ratio import UniqueWordsRatio
from features.word_based.spam_words import SpamWords
from features.flesh_reading_score import FleschReadingEase
from sklearn.neural_network import MLPClassifier

dataset_dir = "enron-dataset"

def get_dataset():
    emails_dirs = [os.path.join(dataset_dir, f) for f in os.listdir(dataset_dir)]

    instances = []
    for emails_dir in emails_dirs:
        if not os.path.isdir(emails_dir): continue
        dirs = [os.path.join(emails_dir, f) for f in os.listdir(emails_dir)]

        for d in dirs:
            if not os.path.isdir(d): continue
            emails = [os.path.join(d, f) for f in os.listdir(d)]
            for mail in emails:
                if '.DS_Store' in mail: continue

                with open(mail, "rb") as mailDescriptor:
                    instances.append({
                        "content": mailDescriptor.read(),
                        "is_spam": int(mail.split(".")[-2] == 'spam'),
                        "features": {}
                    })

    # return instances[3000:4000]
    return instances

def get_features(train):
    features = [
        # ('word_counts', WordCounts(train)),
        ('number_of_characters', NumberOfCharacters()),
        ('alpha_ratio', AlphaRatio()),
        ('digit_ratio', DigitRatio()),
        ('whitespace_ratio', WhitespaceRatio()),
        ('special_chars_ratio', SpecialCharsRatio()),
        ('number_of_words', NumberOfWords()),
        ('short_words_ratio', ShortWordsRatio()),
        ('average_word_len', AverageWordLen()),
        ('unique_words_ratio', UniqueWordsRatio()),
        ('spam_words', SpamWords()),
        ('flesch_reading_score', FleschReadingEase()),
    ]
    return features

def get_pipeline(features):
    feature_names = []
    for feature in features:
        feature_names += feature[1].FEATS
    print(feature_names)
    return Pipeline(features + [
        ('transform', ToMatrix(features=feature_names)),
        ('norm', MinMaxScaler())
    ])

def run_classifiers(test, train):
    nnClassifier = NeuralNetwork(activation='sigmoid')

    params3 = {'Epsilon1': 0.1, 'Epsilon2': 0.1, 'C1': 1, 'C2': 1, 'kernel_type': 0, 'kernel_param': 1, 'fuzzy': 0}
    params6 = {'Epsilon1': 0.1, 'Epsilon2': 0.1, 'C1': 1, 'C2': 1,'kernel_type':1,'kernel_param': 1,'fuzzy' :1}

    classifiers = [
        # SVC(kernel="linear", C=0.025),
        # SVC(gamma=2, C=1),
        # TwinSVMClassifier(**params3),
        # TwinSVMClassifier(**params6),
        # MultinomialNB(),
        # KNeighborsClassifier(1),
        nnClassifier,
        # DecisionTreeClassifier(max_depth=5),
        # RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
        # MLPClassifier(alpha=1),
        # AdaBoostClassifier(),
        # GaussianNB(),
        # LinearDiscriminantAnalysis(),
        # QuadraticDiscriminantAnalysis(),
    ]

    print("Transforming data to features...")
    #  get pipeline of features and generate them
    pipeline = get_pipeline(get_features(train))

    # X_train = np.load('enron_word_cnt_train_features_matrix.npy')
    # trainLabels = np.load('enron_word_cnt_train_labels.npy')
    X_train = pipeline.fit_transform(train)
    trainLabels = [instance['is_spam'] for instance in train]
    # np.save('enron_train_features_matrix.npy', X_train)
    # np.save('enron_train_labels.npy', trainLabels)

    # X_test = np.load('enron_word_cnt_test_features_matrix.npy')
    # testLabels = np.load('enron_word_cnt_test_labels.npy')
    X_test = pipeline.fit_transform(test)
    testLabels = [instance["is_spam"] for instance in test]
    # np.save('enron_test_features_matrix.npy', X_test)
    # np.save('enron_test_labels.npy', testLabels)

    nnClassifier.X_test = np.array(X_test)
    nnClassifier.Y_test = np.array(testLabels)

    print("Finished transforming data to features...")

    for classifier in classifiers:
        print("\n==================== {0} ====================".format(str(classifier.__class__.__name__)))

        print("Start training...")
        startTime = datetime.datetime.now()
        classifier.fit(X_train, trainLabels)
        endTime = datetime.datetime.now()
        print("Finished training for", endTime-startTime)

        print("Starting prediction...")
        startTime = datetime.datetime.now()
        predictedLabels = classifier.predict(X_test)
        endTime = datetime.datetime.now()
        print("Finished prediction for", endTime-startTime)

        confusionMatrix = confusion_matrix(testLabels, predictedLabels)
        precision = confusionMatrix[1, 1] / (confusionMatrix[1, 1] + confusionMatrix[0, 1])
        recall = confusionMatrix[1,1] / (confusionMatrix[1,1] + confusionMatrix[1,0])
        f1 = 2 * (precision * recall) / (precision + recall)
        accuracy = ((confusionMatrix[0, 0] + confusionMatrix[1, 1]) / len(predictedLabels)) * 100

        print("\nConfusion matrix:\n{0}\n".format(confusionMatrix))
        print("Precision: {0}".format(round(precision, 2)))
        print("Recall: {0}".format(round(recall, 2)))
        print("F1: {0}".format(round(f1, 2)))
        print("Accuracy: {0}".format(round(accuracy, 2)))

if __name__ == "__main__":
    train, test = train_test_split(get_dataset(), test_size=0.40)
    predictedLabels = run_classifiers(test, train)


import os
from pprint import pprint

dir = "data/"
files = []
files2 = os.listdir(dir)
for file in files2:
    file = dir+file
    files.append(file)

counts = []
for file in files:
    with open(file, encoding='utf-8') as f:
        file = file.split("/")[1]
        data = f.readlines()
        ff = []
        for item in data:
            item = item.strip().strip("\n")
            ff.append(item)
        count = len(ff)
    counts.append({'count':len(ff), 'file':file, 'lines':ff})
data_files = sorted(counts, key=lambda x:x['count'])

finish_file_name = 'finish.txt'

with open(finish_file_name, 'w') as f:
    for data in data_files:
        f.write(data['file']+'\n')
        f.write(str(data['count'])+'\n')
        lines = data['lines']
        for line in lines:
            f.write(line+'\n')

1 Основы языка программирования Python/2 Открытие и чтение файла, запись в файл/задача 3/sorteds.py


# -*- coding: utf-8 -*-
"""
Created on Mon Apr  8 12:29:39 2019

@author: grin
"""

import logging
import sys

class StreamToLogger(object):
    """
    Fake file-like stream object that redirects writes to a logger instance.
    """
    def __init__(self, logger, log_level=logging.INFO):
        self.logger = logger
        self.log_level = log_level
        self.linebuf = ''

    def close(self):
        logging.shutdown()

    def flush(self):
        pass

    def write(self, buf):
        for line in buf.rstrip().splitlines():
            self.logger.log(self.log_level, line.rstrip())

if (not '--verbose' in sys.argv) and (not '-v' in sys.argv):
    fh = logging.FileHandler('./log/out.log', 'a', 'utf-8')
    logging.basicConfig(
        level=logging.INFO,
        datefmt = '%Y-%m-%d;%H:%M:%S',
        format='%(asctime)s;%(levelname)s;%(name)s;%(message)s',
        #filename="out.log",
        handlers = [fh]
    )

# logging.getLogger('bokeh.server').setLevel(logging.WARNING)
logging.getLogger('tornado.access').disabled = True

def write_to_log(mes, mesType = 'info'):
    if (not '--verbose' in sys.argv) and (not '-v' in sys.argv):
        stdout_logger = logging.getLogger('STDOUT')
        sl1 = StreamToLogger(stdout_logger, logging.INFO)
        sys.stdout = sl1
        stderr_logger = logging.getLogger('STDERR')
        sl2 = StreamToLogger(stderr_logger, logging.ERROR)
        sys.stderr = sl2
        print(mes)
        for i in (sl1, sl2): i.close()
    else:
        print(mes)


import numpy as np
import matplotlib.pyplot as plt

# Given Parameters

N_fatalities = np.linspace(1,1000,1000)  # fatalities

P_df = 0.1; beta = 0.1

alpha = 2
k = 3
N_A = 100

r= 0.025
I = 5*10**6
B = 0.33

# Individual Risk

Pf_ind_risk = (beta*(10**-4))/P_df  # per year

# print('Acceptable Failure Probability: ', Pf_ind_risk)

# Societal Risk

C = ((beta*100)/(k*np.sqrt(N_A)))**2  # binomial distribution

Pf_SR = (C/(N_fatalities**alpha))

# Economic Risk

D_damage = (5*10**7)*N_fatalities

Pf_ER = (I*B*r)/(D_damage)

# Visualize

my_linewidth = 4

plt.figure(figsize=(8,6))

plt.yscale('log')
plt.xscale('log')
plt.plot(N_fatalities, Pf_SR, linewidth=my_linewidth, label='Societal Risk')
plt.plot(N_fatalities,Pf_ER, linewidth=my_linewidth, color='r', label='Economic Risk')
plt.axhline(Pf_ind_risk, linewidth=my_linewidth, color='g', label='Individual Risk')

plt.ylabel('Probability of Exceedance, $1-F_N(n)$')
plt.xlabel('Fatalities, N')
plt.ylim(10**(-7),1)

plt.legend(loc='upper right')

plt.savefig('risk_types_py.svg')


from ray import air, tune

def tune_hyper_param(algo, config, log_dir, iterations,
                     name,max_concurrent_trials=1):
    """
    Wrapper for the ray.tune.Tuner.fit function used for tuning the hyper-parameters
    :param algo: The algorithm to use for training
    :param config: The algorithm configs
    :param log_dir: Where to save logs and the checkpoints
    :param iterations: Number of iterations to tune for
    :param name: name of the training to group the logs
    :param max_concurrent_trials: maximum number of trial tuned in parallel
    :return: the results of the hyper-parameter tuning
    """
    tuner = tune.Tuner(
        algo,
        param_space=config,
        tune_config=tune.TuneConfig(
            metric="episode_reward_mean",
            mode="max",
            max_concurrent_trials=max_concurrent_trials
        ),
        run_config=air.RunConfig(
            name=name,
            local_dir=log_dir,

            stop={"training_iteration": iterations},
        ),

    )

    results = tuner.fit()
    return results


#Code referenced from TA Alek Bedard
from flask import Flask, jsonify, request
import sys

app = Flask(__name__)

@app.route('/')
def light():
    if len(sys.argv) < 2:
         return "Dude! You are not curling from a server!!"

    num = 10
    counter = 30
    while(counter < 30):
        num = num*num*num
        counter = counter - 1
    return "from" + sys.argv[1] + "\n"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)


import requests
from flask import Flask
from flask import request
import os
import json

app = Flask(__name__)

## переносим таблицы с телефона локально
def get_remote_table():

   r = requests.post(url = "http://192.168.0.72:8095/?mode=SQLQueryText&query=SELECT * FROM RS_barcodes&params=",   #SELECT name FROM sqlite_master WHERE type='table'
                     headers={'Content-Type': 'Application/json; charset=utf-8'})  #

   if r.status_code == 200:
        r.encoding = 'utf-8'
        #jdata = json.loads(r.text.encode("utf-8"))
        print(r.text)
#get_remote_table()

@app.route('/post', methods=['POST'])
def post():
    path = os.getcwd()

    for filename in request.files:
        file = request.files[filename] #rightscan
        if file:
            file.save(path + '\\'+ filename + '.db')
            print('file uploaded')
    return '200'

@app.route('/unload_log', methods=['POST'])
def unload_log():
    path = os.getcwd()
    data = request.get_json()
    if data:
        with open(os.path.join(path, 'rs_log.json'), 'w', encoding='utf-8') as f:
            json.dump(json.loads(data), f, ensure_ascii=False, indent=2)
            print('file uploaded')
        return '200'

if __name__ == '__main__':
    #ui_global.init()
    app.run(host='0.0.0.0', port=2444, debug=True)


print("{:=^60}".format('Progressão Aritimética v2.0'))
termo = int(input('Primeiro termo: '))
razao = int(input('Razão: '))
dec = termo + (10-1) * razao
print(termo, end=' - ')
while termo < dec:
    termo += razao-1
    termo += 1
    print(termo, end=' - ')
print('Fim')


# Exercício Python 044: Elabore um programa que calcule o valor
# a ser pago por um produto, considerando o seu preço normal e condição de pagamento:
# - à vista dinheiro/cheque: 10% de desconto
# - à vista no cartão: 5% de desconto
# - em até 2x no cartão: preço formal
# - 3x ou mais no cartão: 20% de juros
print("==========LOJAS CORTES==========")
preço = float(input("Preço das compras: R$"))
print("FORMAS DE PAGAMENTO\n"
      "[1] à vista dinheiro/cheque\n"
      "[2] à vista cartão\n"
      "[3] 2x no cartão\n"
      "[4] 3x ou mais no cartão")
opção = int(input("Qual é a opção? "))
if opção == 1:
    total = preço - (preço * 10 / 100)
elif opção == 2:
    total = preço - (preço * 5 / 100)
elif opção == 3:
    total = preço
    parcela = total / 2
    print(f"A compra será parcelada em 2x de R${parcela:.2f}")
elif opção == 4:
    parcela = int(input("Em quantas vezes deseja parcelar? "))
    total = preço + (preço * 20 / 100)
    vf = total / parcela
    print(f"A compra será parcelada em {parcela}x de R${vf:.2f}")
print(f"Sua compra de R${preço:.2f} vai custar R${total:.2f} no final.")