import re
from typing import Tuple

from django.http import HttpResponse

from adapters.post import PostModelAdapter
from fixtures.types import CommentModelAdapterT
from form.find_urls import find_links_between_lines, get_url_display_names
from conftest import KeyVal

def find_edit_and_delete_urls(
        post_adapter: PostModelAdapter,
        comment_adapter: CommentModelAdapterT,
        post_page_response: HttpResponse, urls_start_with: KeyVal
) -> Tuple[KeyVal, KeyVal]:
    """Looks up two links in the post_page_response's content.
    The links must be found between the post's text and the first
    comment to the post.
    The one with the word `edit` in it is for editing the post,
    the other one, therefore, is for its deletion.
    !!! Make posts and comments have unique texts and titles.
    """

    post_page_content = post_page_response.content.decode('utf-8')

    links_not_found_err_msg = (
        'Убедитесь, что на странице публикации '
        'отображаются комментарии к публикации, и её автору '
        'доступны две ссылки под текстом публикации: '
        'одна для её редактирования и одна - для удаления. '
        'Адрес ссылок должен начинаться с '
        f'{urls_start_with.key}'
    )

    # Get info about html between two consecutive posts
    displayed_post_text = post_adapter.displayed_field_name_or_value
    displayed_comment_text = comment_adapter.displayed_field_name_or_value
    pattern = re.compile(
        fr'{displayed_post_text}([\w\W]*?){displayed_comment_text}')
    between_posts_match = pattern.search(post_page_content)
    assert between_posts_match, links_not_found_err_msg
    text_between_posts = between_posts_match.group(1)
    between_posts_start_lineix = post_page_content.count(
        '\n', 0, between_posts_match.start())
    between_posts_end_lineix = between_posts_start_lineix + (
        between_posts_match.group().count('\n'))

    post_links = find_links_between_lines(
        post_page_content,
        urls_start_with.val,
        between_posts_start_lineix,
        between_posts_end_lineix,
        link_text_in=text_between_posts,
    )
    if len(set(link.get('href') for link in post_links)) != 2:
        raise AssertionError(links_not_found_err_msg)

    # We have two links. Which one of them is the edit link,
    # and which - the delete link? Edit link must lead to a form.

    edit_link, del_link = post_links[0], post_links[1]
    if 'edit' in edit_link.get('href'):
        assert 'edit' not in del_link.get('href'), (
            'Убедитесь, что в адресе страницы удаления публикации '
            'нет слова `edit`.'
        )
    elif 'edit' in del_link.get('href'):
        edit_link, del_link = del_link, edit_link
    else:
        raise AssertionError(
            'Убедитесь, что страница редактирования публикации имеет '
            'адрес posts/<post_id>/edit/.'
        )

    post_url_display_names = get_url_display_names(
        urls_start_with,
        post_adapter.id,
        post_links
    )
    edit_url = edit_link.get('href')
    del_url = del_link.get('href')
    return (
        KeyVal(key=edit_url, val=post_url_display_names[edit_url]),
        KeyVal(key=del_url, val=post_url_display_names[del_url]),
    )


#class cfg
#@author dosi
#@category _NEW_
#@keybinding
#@menupath
#@toolbar

import numpy as np

class cfg():
    def __init__(self, func):
        self.function_name = func
        self.entry = None
        self.exit = []
        self.vertices = []
        self.edges = []
        self.no_of_bbs = 0
        self.no_of_edges = 0
        self.no_of_func_calls = 0
        self.no_of_incoming_calls = 0
        self.no_of_logic_inst = 0
        self.no_of_trans_inst = 0
        self.no_of_inst = 0
        self.no_of_local_var = 0
        self.no_of_global_var = 0
        self.in_degree_dict = {}
        self.out_degree_dict = {}
        self._distance_matrix = None
        self._distance_matrix_trans = None
        self._shortest_path_from_entry = {}
        self._shortest_path_to_exit_list = []

    def set_no_of_func_calls(self, no):
        self.no_of_func_calls = no

    def set_no_of_incoming_calls(self, no):
        self.no_of_incoming_calls = no

    def set_no_of_inst(self, no):
        self.no_of_inst = no

    def set_no_of_logic_inst(self, no):
        self.no_of_logic_inst = no

    def set_no_of_trans_inst(self, no):
        self.no_of_trans_inst = no

    def set_no_of_local_var(self, no):
        self.no_of_local_var = no

    def set_no_of_global_var(self, no):
        self.no_of_global_var = no

    def set_entry_bb(self, bb):
        if self._shortest_path_from_entry:
            bb_idx = self.vertices.index(bb)
            bb.entry_bb = self._shortest_path_from_entry[bb_idx]

    def set_bb_exit(self, bb):
        if self._shortest_path_to_exit_list:
            bb_idx = self.vertices.index(bb)
            exit_bb_list = [x[bb_idx] for x in self._shortest_path_to_exit_list]
            bb.bb_exit = min(exit_bb_list)

    def _shortest_path(self):
        try:
            if self._distance_matrix:
                if self.entry:
                    self._shortest_path_from_entry = self._dijskstra(0, 1)
                if self.exit:
                    for e in self.exit:
                        e_idx = self.vertices.index(e)
                        self._shortest_path_to_exit_list.append(self._dijskstra(e_idx, 0))
        except Exception as e:
            print(e)
            print('something wrong in shortest_path, return None None')
            return None, None

    def _dijskstra(self, idx, entry):
        dis = {}
        _distance_matrix = [self._distance_matrix_trans, self._distance_matrix]
        try:
            for i in range(len(_distance_matrix[entry][idx])):
                if _distance_matrix[entry][idx][i] != 0:
                    dis[i] = _distance_matrix[entry][idx][i]

            visited = []
            min_dis = None
            min_dis_point = None
            for i in range(len(dis)):
                sorted_dis = sorted(dis.items(), key = lambda item: item[1])
                for p, d in sorted_dis:
                    if p not in visited:
                        min_dis_point = p
                        min_dis = d
                        visited.append(p)
                        break
                for j in range(1, len(_distance_matrix[entry])):
                    if _distance_matrix[entry][min_dis_point][j] < float('inf'):
                        update = min_dis + _distance_matrix[entry][min_dis_point][j]
                        if update < dis[j]:
                            dis[j] = update
        except:
            print('something wrong in dijskstra')
        return dis

    def _gen_dis_mat(self):
        #weight of edge equals to 1
        self._distance_matrix = [[float('inf') for _ in range(len(self.vertices))] for _ in range(len(self.vertices))]
        for i in range(len(self._distance_matrix)):
            self._distance_matrix[i][i] = 0
        try:
            if self.out_degree_dict:
                for src, desList in self.out_degree_dict.items():
                    srcIdx = self.vertices.index(src)
                    for des in desList:
                        desIdx = self.vertices.index(des)
                        self._distance_matrix[srcIdx][desIdx] = 1
            np_array = np.array(self._distance_matrix)
            self._distance_matrix_trans = np_array.T.tolist()
        except:
            print('something wrong in generate distance matrix')

    def in_degree(self, t_bb):
        if not self.in_degree_dict or not self.in_degree_dict.__contains__(t_bb):
            t_bb.in_degree = []
        else:
            t_bb.in_degree = self.in_degree_dict[t_bb]

    def _gen_in_degree_dict(self):
        if self.edges:
            for e in self.edges:
                if e[1] not in self.in_degree_dict:
                    self.in_degree_dict[e[1]] = []
                self.in_degree_dict[e[1]].append(e[0])

    def out_degree(self, t_bb):
        if not self.edges or not self.out_degree_dict.__contains__(t_bb):
            t_bb.out_degree = []
        else:
            t_bb.out_degree = self.out_degree_dict[t_bb]

    def _gen_out_degree_dict(self):
        if self.edges:
            for e in self.edges:
                if e[0] not in self.out_degree_dict:
                    self.out_degree_dict[e[0]] = []
                self.out_degree_dict[e[0]].append(e[1])

    def all_children(self, t_bb):
        #implemented in other py
        pass

    def set_entry(self, e_bb):
            self.entry = e_bb

    def set_exit(self, e_bb):
            self.exit.append(e_bb)

    def set_vertices(self, bb):
            self.vertices.append(bb)
            self.no_of_bbs += 1

    def set_edges(self, bb, dest_bb):
            self.edges.append((bb, dest_bb))
            self.no_of_edges += 1

    #call this function when cfgraph is completed
    def prepare(self):
        self._gen_in_degree_dict()
        self._gen_out_degree_dict()
        self._gen_dis_mat()
        self._shortest_path()
        for v in self.vertices:
            self.in_degree(v)
            self.out_degree(v)
            v.set_betweenness()
            #v.entry_bb, v.bb_exit = self.shortest_path(v)

    def prepare_new(self):
        self._gen_in_degree_dict()
        self._gen_out_degree_dict()
        for v in self.vertices:
            self.in_degree(v)
            self.out_degree(v)

if __name__ == '__main__':
    cfgraph = cfg('init')
    cfgraph.vertices = [0,1,2,3]
    cfgraph.edges = [(0,1), (1,2), (2,3), (0,3)]
    cfgraph.set_entry(0)
    cfgraph.set_exit(3)
    cfgraph._gen_in_degree_dict()
    cfgraph._gen_out_degree_dict()
    print(cfgraph.in_degree_dict)
    print(cfgraph.out_degree_dict)
    cfgraph._gen_dis_mat()
    print('dis_mat', cfgraph._distance_matrix)
    cfgraph._shortest_path()
    cfgraph.set_entry_bb(2)
    cfgraph.set_bb_exit(2)


#WAP to count number character in text file.
with open('file2.txt','w+') as f:
    while True:
        str=input()
        if str=='exit':
            break
        else:
            f.write(str)
            f.write('\n')
    f.seek(0,0)
    count=0
    for i in (f.read()):
        count+=1
    print('number of character is ',count)
    print(f.read())


import csv

class Item:
    all = []

    # private method
    def __add_to_list(self):
        Item.all.append(self)

    @classmethod
    def instantiate_items_from_csv(cls, path):
        with open('items.csv', 'r') as f:
            reader = csv.DictReader(f)
            readers = list(reader)
        for item in readers:
            i = Item()
            i.name = item['name']
            i.price = item['price']
            i.quantity = item['quantity']
            i.__add_to_list()

    @staticmethod
    def return_integer_or_float(val):
        val = int(val) if float(val) == int(val) else val
        return val

    # Setters and getters for our three attributes
    @property
    def name(self):
        return self.__name
    @name.setter
    def name(self, value):
        if len(value) > 10:
            raise Exception("The name is too long!")
        else:
            self.__name = value

    @property
    def price(self):
        return self.__price
    @price.setter
    def price(self, value):
        if float(value) < 0:
            raise Exception("The price has to be bigger than 0!")
        else:
            self.__price = self.return_integer_or_float(float(value))

    @property
    def quantity(self):
        return self.__quantity
    @quantity.setter
    def quantity(self, value):
        if int(value) < 0:
            raise Exception("The quantity has to be bigger than 0!")
        else:
            self.__quantity = int(value)

    def __repr__(self):
        return f"{self.__class__.__name__}('{self.__name}', {self.__price}, {self.__quantity})"

if __name__ == '__main__':
    # Instantiate lots of Item objects from the CSV, send to a list thanks to our instantiate from csv method
    Item.instantiate_items_from_csv('./items.csv')
    print(Item.all[0:5])

    item1 = Item.all[0]
    item1.quantity = 50  # using a setter
    print(len(Item.all))
    # item1.add_to_list() # this would not work because "__add_to_list" is a private method not accessible by the instance
    print(len(Item.all))

OOP/06 - Getters and Setters/another_example_private_methods.py


import torch
import os

from CSVDataset import CSVDataset
from models.DBN.DBN import DBN

# for reproducibility

torch.manual_seed(42)

# check if GPU is available

use_cuda = torch.cuda.is_available()

# to put tensors on GPU if available

device = torch.device('cuda' if use_cuda else 'cpu')

# initialize DBN

dbn = DBN(num_RBMs = 3,
          num_visible = [50,25,15],
          num_hidden = [25,15,10],
          num_categories = 5,
          num_sampling_iter = [5,5,5],
          device = device)

# initialize dataloaders

data_dir = '../../data/projG'
train_path = os.path.join(data_dir,'data_train.csv')
val_path = os.path.join(data_dir,'data_val.csv')
test_path = os.path.join(data_dir,'data_test.csv')
dataloaders = {}

# optimize dataloaders with GPU if available

dl_config = {'num_workers': 0, 'pin_memory': True} if use_cuda else {}

# batch sizes for training, validation, and testing

train_batch_size = 64
val_batch_size = 64
test_batch_size = 64

for mode,path,batch_size in [('train',train_path,train_batch_size),
                             ('val',val_path,val_batch_size),
                             ('test',test_path,test_batch_size)]:

    dataset = CSVDataset(path)

    dataloaders[mode] = torch.utils.data.DataLoader(
                               dataset = dataset,
                               batch_size = batch_size,
                               shuffle = True,
                               **dl_config)

optimizer1 = torch.optim.SGD(params = dbn.rbms[0].parameters(),
                             lr = 1,#0.01/train_batch_size,
                             momentum = 0.9,
                             weight_decay = 0.001)

optimizer2 = torch.optim.SGD(params = dbn.rbms[1].parameters(),
                             lr = 1,#0.01/train_batch_size,
                             momentum = 0.9,
                             weight_decay = 0.001)

optimizer3 = torch.optim.SGD(params = dbn.rbms[2].parameters(),
                             lr = 1,#0.01/train_batch_size,
                             momentum = 0.9,
                             weight_decay = 0.001)

optimizers = [optimizer1,optimizer2,optimizer3]

# number of epochs to train and validate each RBM for

num_epochs = [5,5,5]

"""
need to add this if statement such that the multiprocessing package
used by the PyTorch dataloaders works correctly. See here for details:

https://discuss.pytorch.org/t/if-name-main-for-window10/19377

https://stackoverflow.com/questions/20222534/python-multiprocessing-on-windows-if-name-main

Also, this code MUST be run on the command line and NOT through an
IPython Notebook so that it doesn't throw an error.

"""

if __name__ == '__main__':

    dbn.train(num_epochs = num_epochs,
              dataloaders = dataloaders,
              optimizers = optimizers,
              device = device)

    # test example

    x = torch.randn(1,5,50,1).to(device)
    y = dbn(x)


import os
import torch
import torch.optim as optim
import time
import numpy as np
from datetime import datetime
from pathlib import Path
from collections import defaultdict

from data.data_loader import get_datasets, get_dataloader
from utils.loss_func import PermLoss
from utils.evaluation_metric import matching_accuracy, compute_metrics, summarize_metrics, print_metrics
from utils.model_sl import load_model, save_model
from utils.hungarian import hungarian
from utils.visdomshow import VisdomViz
from utils.config import cfg
from parallel import DataParallel
from eval import eval_model

def train_eval_model(model,
                     permLoss,
                     optimizer,
                     dataloader,
                     num_epochs=25,
                     resume=False,
                     start_epoch=0,
                     viz=None,
                     savefiletime='time'):
    print('**************************************')
    print('Start training...')
    dataset_size = len(dataloader['train'].dataset)
    print('train datasize: {}'.format(dataset_size))

    since = time.time()
    lap_solver = hungarian
    optimal_acc = 0.0
    optimal_rot = np.inf
    device = next(model.parameters()).device

    print('model on device: {}'.format(device))

    checkpoint_path = Path(cfg.OUTPUT_PATH) / 'params'
    if not checkpoint_path.exists():
        checkpoint_path.mkdir(parents=True)

    if resume:
        assert start_epoch != 0
        model_path = str(checkpoint_path / 'params_{:04}.pt'.format(start_epoch))
        print('Loading model parameters from {}'.format(model_path))
        load_model(model, model_path)

        optim_path = str(checkpoint_path / 'optim_{:04}.pt'.format(start_epoch))
        print('Loading optimizer state from {}'.format(optim_path))
        optimizer.load_state_dict(torch.load(optim_path))

    scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                               milestones=cfg.TRAIN.LR_STEP,
                                               gamma=cfg.TRAIN.LR_DECAY,
                                               last_epoch=cfg.TRAIN.START_EPOCH - 1)

    for epoch in range(start_epoch, num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        model.train()  # Set model to training mode

        print('lr = ' + ', '.join(['{:.2e}'.format(x['lr']) for x in optimizer.param_groups]))

        iter_num = 0
        running_since = time.time()
        all_train_metrics_np = defaultdict(list)

        # Iterate over data3d.
        for inputs in dataloader['train']:
            P1_gt, P2_gt = [_.cuda() for _ in inputs['Ps']]     #keypoints coordinate
            n1_gt, n2_gt = [_.cuda() for _ in inputs['ns']]     #keypoints number
            A1_gt, A2_gt = [_.cuda() for _ in inputs['As']]     #edge connect matrix
            perm_mat = inputs['gt_perm_mat'].cuda()             #permute matrix
            T1_gt, T2_gt = [_.cuda() for _ in inputs['Ts']]
            Inlier_src_gt, Inlier_ref_gt = [_.cuda() for _ in inputs['Ins']]

            batch_cur_size = perm_mat.size(0)
            iter_num = iter_num + 1

            # zero the parameter gradients
            optimizer.zero_grad()

            with torch.set_grad_enabled(True):
                # forward
                s_pred, Inlier_src_pre, Inlier_ref_pre = model(P1_gt, P2_gt, A1_gt, A2_gt, n1_gt, n2_gt)

                # multi_loss = []
                if cfg.DATASET.NOISE_TYPE == 'clean':
                    permloss = permLoss(s_pred, perm_mat, n1_gt, n2_gt)
                    loss = permloss
                else:
                    if cfg.PGM.USEINLIERRATE:
                        s_pred = Inlier_src_pre * s_pred * Inlier_ref_pre.transpose(2, 1).contiguous()
                    permloss = permLoss(s_pred, perm_mat, n1_gt, n2_gt)
                    loss = permloss

                # backward + optimize
                loss.backward()
                optimizer.step()

                # training accuracy statistic
                s_perm_mat = lap_solver(s_pred, n1_gt, n2_gt, Inlier_src_pre, Inlier_ref_pre)
                match_metrics = matching_accuracy(s_perm_mat, perm_mat, n1_gt)
                perform_metrics = compute_metrics(s_perm_mat, P1_gt[:,:,:3], P2_gt[:,:,:3], T1_gt[:, :3, :3], T1_gt[:, :3, 3])

                for k in match_metrics:
                    all_train_metrics_np[k].append(match_metrics[k])
                for k in perform_metrics:
                    all_train_metrics_np[k].append(perform_metrics[k])
                all_train_metrics_np['loss'].append(np.repeat(loss.item(), 4))

                if iter_num % cfg.STATISTIC_STEP == 0:
                    running_speed = cfg.STATISTIC_STEP * batch_cur_size / (time.time() - running_since)
                    # globalstep = epoch * dataset_size + iter_num * batch_cur_size
                    print('Epoch {:<4} Iteration {:<4} {:>4.2f}sample/s Loss={:<8.4f} GT-Acc:{:.4f} Pred-Acc:{:.4f}'
                          .format(epoch, iter_num, running_speed,
                                  np.mean(np.concatenate(all_train_metrics_np['loss'])[-cfg.STATISTIC_STEP*batch_cur_size:]),
                                  np.mean(np.concatenate(all_train_metrics_np['acc_gt'])[-cfg.STATISTIC_STEP*batch_cur_size:]),
                                  np.mean(np.concatenate(all_train_metrics_np['acc_pred'])[-cfg.STATISTIC_STEP*batch_cur_size:])))
                    running_since = time.time()

        all_train_metrics_np = {k: np.concatenate(all_train_metrics_np[k]) for k in all_train_metrics_np}
        summary_metrics = summarize_metrics(all_train_metrics_np)
        print('Epoch {:<4} Mean-Loss: {:.4f} GT-Acc:{:.4f} Pred-Acc:{:.4f}'.
              format(epoch, summary_metrics['loss'], summary_metrics['acc_gt'], summary_metrics['acc_pred']) )
        print_metrics(summary_metrics)

        save_model(model, str(checkpoint_path / 'params_{:04}.pt'.format(epoch + 1)))
        torch.save(optimizer.state_dict(), str(checkpoint_path / 'optim_{:04}.pt'.format(epoch + 1)))

        # to save values during training
        metric_is_save= False
        if metric_is_save:
            np.save(str(Path(cfg.OUTPUT_PATH) / ('train_log_' + savefiletime + '_metric')),
                    all_train_metrics_np)

        if viz is not None:
            viz.update('train_loss', epoch, {'loss': summary_metrics['loss']})
            viz.update('train_acc', epoch, {'acc': summary_metrics['acc_gt']})
            viz.update('train_metric', epoch, {'r_mae': summary_metrics['r_mae'],
                                                    't_mae': summary_metrics['t_mae']})

        # Eval in each epochgi
        val_metrics = eval_model(model, dataloader['val'])
        if viz is not None:
            viz.update('val_acc', epoch, {'acc': val_metrics['acc_gt']})
            viz.update('val_metric', epoch, {'r_mae': val_metrics['r_mae'],
                                             't_mae': val_metrics['t_mae']})
        if optimal_acc < val_metrics['acc_gt']:
            optimal_acc = val_metrics['acc_gt']
            print('Current best acc model is {}'.format(epoch + 1))
        if optimal_rot > val_metrics['r_mae']:
            optimal_rot = val_metrics['r_mae']
            print('Current best rotation model is {}'.format(epoch + 1))

        # Test in each epochgi
        test_metrics = eval_model(model, dataloader['test'])
        if viz is not None:
            viz.update('test_acc', epoch, {'acc': test_metrics['acc_gt']})
            viz.update('test_metric', epoch, {'r_mae': test_metrics['r_mae'],
                                              't_mae': test_metrics['t_mae']})

        scheduler.step()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}h {:.0f}m {:.0f}s'
          .format(time_elapsed // 3600, (time_elapsed // 60) % 60, time_elapsed % 60))

    return model

if __name__ == '__main__':
    from utils.dup_stdout_manager import DupStdoutFileManager
    from utils.parse_argspc import parse_args
    from utils.print_easydict import print_easydict
    import socket

    args = parse_args('Point could registration of graph matching training & evaluation code.')

    if cfg.VISDOM.OPEN:
        hostname = socket.gethostname()
        Visdomins = VisdomViz(env_name=hostname+'RGM_Train', server=cfg.VISDOM.SERVER, port=cfg.VISDOM.PORT)
        Visdomins.viz.close()
    else:
        Visdomins = None

    import importlib
    mod = importlib.import_module(cfg.MODULE)
    Net = mod.Net

    torch.manual_seed(cfg.RANDOM_SEED)

    pc_dataset = {x: get_datasets(partition = x,
                                  num_points = cfg.DATASET.POINT_NUM,
                                  unseen = cfg.DATASET.UNSEEN,
                                  noise_type = cfg.DATASET.NOISE_TYPE,
                                  rot_mag = cfg.DATASET.ROT_MAG,
                                  trans_mag = cfg.DATASET.TRANS_MAG,
                                  partial_p_keep = cfg.DATASET.PARTIAL_P_KEEP,
                                  crossval = (x == 'train'),
                                  train_part = (x == 'train')) for x in ('train', 'test')}
    pc_dataset['val'] = get_datasets(partition = 'train',
                                     num_points = cfg.DATASET.POINT_NUM,
                                     unseen = cfg.DATASET.UNSEEN,
                                     noise_type = cfg.DATASET.NOISE_TYPE,
                                     rot_mag = cfg.DATASET.ROT_MAG,
                                     trans_mag = cfg.DATASET.TRANS_MAG,
                                     partial_p_keep = cfg.DATASET.PARTIAL_P_KEEP,
                                     crossval = True,
                                     train_part = False)

    dataloader = {x: get_dataloader(pc_dataset[x], shuffle=(x == 'train')) for x in ('train', 'val', 'test')}

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    model = Net()
    model = model.cuda()
    permLoss = PermLoss()

    if cfg.TRAIN.OPTIM == 'SGD':
        optimizer = optim.SGD(model.parameters(), lr=cfg.TRAIN.LR, momentum=cfg.TRAIN.MOMENTUM, nesterov=True)
    else:
        optimizer = optim.Adam(model.parameters(), lr=cfg.TRAIN.LR, weight_decay=1e-4)

    model = DataParallel(model, device_ids=cfg.GPUS)

    if not Path(cfg.OUTPUT_PATH).exists():
        Path(cfg.OUTPUT_PATH).mkdir(parents=True)

    now_time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')

    with DupStdoutFileManager(str(Path(cfg.OUTPUT_PATH) / ('train_log_' + now_time + '.log'))) as _:
        print_easydict(cfg)
        model = train_eval_model(model, permLoss, optimizer, dataloader,
                                 num_epochs=cfg.TRAIN.NUM_EPOCHS,
                                 resume=cfg.TRAIN.START_EPOCH != 0,
                                 start_epoch=cfg.TRAIN.START_EPOCH,
                                 viz = Visdomins,savefiletime=now_time)


import sys
sys.path.append('.')
from models import Generator
from hparams import get_sampler_hparams
from utils.log_utils import save_images, set_up_visdom, config_log, log, start_training_log, display_images, load_model
from utils.sampler_utils import get_sampler, get_samples, retrieve_autoencoder_components_state_dicts

def main(H, vis):

    quanitzer_and_generator_state_dict = retrieve_autoencoder_components_state_dicts(
        H,
        ['quantize', 'generator'],
        remove_component_from_key=True
    )

    embedding_weight = quanitzer_and_generator_state_dict.pop(
        'embedding.weight')
    if H.deepspeed:
        embedding_weight = embedding_weight.half()
    embedding_weight = embedding_weight.cuda()
    generator = Generator(H)

    generator.load_state_dict(quanitzer_and_generator_state_dict, strict=False)
    generator = generator.cuda()
    sampler = get_sampler(H, embedding_weight).cuda()

    sampler = load_model(sampler, f'{H.sampler}_ema', H.load_step, H.load_dir)
    sampler.n_samples = 25  # get samples in 5x5 grid

    for i in range(100):
        images = get_samples(H, generator, sampler)
        display_images(vis, images, H, win_name=f'{H.sampler}_samples')
        save_images(images, "samples", i, H.log_dir)

if __name__ == '__main__':
    H = get_sampler_hparams()
    vis = set_up_visdom(H)
    config_log(H.log_dir)
    log('---------------------------------')
    log(f'Setting up training for {H.sampler}')
    start_training_log(H)
    main(H, vis)


#! python3
# genemodel_evidence_combine_and_curate.py
# Python script to combine a number of forms of evidence to suggest whether a gene model should be kept or not.
# Evidence includes: 1-BLAST outfmt6 formatted file and 2-Integrated table from repeat_genemodel_overlaps.py -> genemodel_lcr_filtration.py.
# Output is a text file listing sequences to remove.

import os, argparse, re
from collections import Counter
from itertools import groupby

##### USER INPUT SECTION

usage = """%(prog)s reads in a BLAST outfmt6 formatted file containing results of BLASTP against nr database as well as
the integrated table produced by repeat_genemodel_overlaps.py -> genemodel_lcr_filtration.py scripts. Output is a text file
listing sequences that should be removed from the final annotation.
"""
p = argparse.ArgumentParser(description=usage)
p.add_argument("-b", "-blast", dest="blastFile",
                  help="Specify gene model fasta file")
p.add_argument("-i", "-t", "-table", dest="integratedTable",
                  help="Specify integrated table containing repeat overlap / seg LCR proportions")
p.add_argument("-ovl", "-overlap", dest="maskOverlap", type=float,
               help="Percentage value that the sequence must be overlapped by to consider it for curation (default == 20)", default = 20.0)
p.add_argument("-lcr50e", "-lcr50evalue", dest="lcr50evalue", type=float,
               help="Evalue cut-off to enforce for 50<x<70 LCR sequences (default == 1e-10)", default = 1e-10)
p.add_argument("-o", "-output", dest="outputFile",
               help="Output file name")
p.add_argument("-fo", "-force", dest="force", choices = ['y', 'n', 'Y', 'N'],
               help="default == 'n', which means the program will not overwrite existing files. Specify 'y' to allow this behaviour at your own risk.", default='n')

args = p.parse_args()

# Obtain data from arguments
blastFile = args.blastFile
integratedTable = args.integratedTable
maskOverlap = args.maskOverlap
lcr50evalue = args.lcr50evalue
outputFileName = args.outputFile
force = args.force

# Format output names and check that output won't overwrite another file
if os.path.isfile(outputFileName) and force.lower() != 'y':
        print('There is already a file named ' + outputFileName + '. Either specify a new file name, delete these older file(s), or provide the -force argument either "Y" or "y"')
        quit()
elif os.path.isfile(outputFileName) and force.lower() == 'y':
        os.remove(outputFileName)

### CORE PROCESS

# Parse the BLAST file
grouper = lambda x: x.split('\t')[0]
blastDict = {}
with open(blastFile, 'r') as fileIn:
        for key, value in groupby(fileIn, grouper):
                for line in value:                              # We're just going to look at the first hit to reduce the overall complexity of this analysis - this is why I'm using a grouper
                        sl = line.rstrip('\n').split('\t')
                        tid = sl[0]
                        #start = min([int(sl[6]), int(sl[7])])           # This lets us handle reverse complement BLAST hits ### Don't think this is necessary, we'll just look at E-value only
                        #stop = max([int(sl[6]), int(sl[7])])
                        e = float(sl[10])
                        #blastpos = set(range(start, stop+1))           # +1 to stop position to keep things 1-based
                        # Hold onto relevant information in a dictionary
                        blastDict[tid] = e
                        break

# Parse the integrated table file
## Do a first run through of the table to find genes which have isoform variants
geneID = []
with open(integratedTable, 'r') as fileIn:
        for line in fileIn:
                if line == '\n' or line.startswith('gene_id\ttranscript_id'):
                        continue
                sl = line.rstrip('\n').split('\t')
                geneID.append(sl[0])
isoformIDs = []
counted = Counter(geneID)
for key, value in counted.items():
        if value > 1:
                isoformIDs.append(key)

## Now run through the integrated table and pull out relevant details
integratedDict = {}
with open(integratedTable, 'r') as fileIn:
        for line in fileIn:
                if line == '\n' or line.startswith('gene_id\t'):                # Shouldn't be in the file, but it doesn't hurt to make sure
                        continue
                sl = line.rstrip('\n').split('\t')
                # Figure out if this transcript has other isoforms
                if sl[0] in isoformIDs:
                        isoform = 'y'
                else:
                        isoform = 'n'
                # Save details of this transcript
                integratedDict[sl[1]] = [isoform, int(sl[4]), float(sl[5]), float(sl[6])]

# Compile forms of evidence and decide if the sequence should be dropped
"""Goals for this step:
I'm going to check that the sequence has >=X% overlap with repeats (default 50) to decide if it should be considered for curation,
then check if the transcript has alternative isoforms (== good models), then check that the sequence has <=50% LCR (== good models),
then check sequences <50% LCR to see if they have a good BLAST (default <=1e-40; == good), then drop anything with <=70% LCR or those which
haven't been marked as good by this point

maskOverlap #default 50
lcr50evalue #default 1e-40
"""
dropList = []
#goodList = []           # Just for debug
for key, value in integratedDict.items():
        # Extract values into easily understood labels
        if key in blastDict:
                evalue = blastDict[key]       # If it's not in the dictionary, it didn't get a BLAST hit
        else:
                evalue = 99999.0                # Just sub in values that will never affect anything
        isoform, orflen, ovlPerc, lcrPerc = value
        # Check 1: is this sequence overlapped by repeats?
        if ovlPerc < maskOverlap:
                #goodList.append(key + '\t-\tless than 50 overlap')
                continue
        # Check 2: does this sequence have other isoforms?
        if isoform == 'y':
                #goodList.append(key + '\t-\thas isoforms')
                continue
        # Check 3: does this sequence have <=50% LCR?
        if lcrPerc <= 50:
                #goodList.append(key + '\t-\tless than 50% LCR')
                continue
        # Check 4: does this sequence have 50%<x<70% LCR and a good BLAST?
        elif 50 <= lcrPerc <= 70:
                if evalue < lcr50evalue:
                        #goodList.append(key + '\t50%<x<70%\thas good BLAST')
                        continue
                else:
                        #dropList.append(key + '\t50%<x<70%\tnot good enough E-value')
                        dropList.append(key)
        elif lcrPerc >= 70:
                #dropList.append(key + '\tx>70%\ttoo much LCR')
                dropList.append(key)
        else:
                print('Unhandled condition. I didn\'t think this was possible. Check this sequence manually.')
                print(key)
                print(value)

# Dump to output
with open(outputFileName, 'w') as fileOut:
        for val in dropList:
                fileOut.write(val + '\n')
        # Debugging
        #import pyperclip
        #pyperclip.copy('\r\n'.join(goodList))


from time import sleep

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class TestCase:
    new_name = '新的姓名'

    def setup(self):
        # com.tencent.wework/.launch.LaunchSplashActivity
        _package = 'com.tencent.wework'
        _activity = '.launch.LaunchSplashActivity'
        caps = {'platformName': 'android', 'deviceName': 'hogworts', 'appPackage': _package, 'appActivity': _activity,
                'autoGrantPermissions': True, 'noReset': True}
        self._driver = webdriver.Remote('http://localhost:4723/wd/hub', caps)
        self._driver.implicitly_wait(3)
        # 直接启动app
        # self._driver.start_activity(_package, _activity)

    def teardown(self):
        self._driver.quit()
        # pass

    def test_add(self):
        """
        # 点击通讯录
          点击添加成员；点击手动输入添加；
          选择必填选项，输入名字，
          选择手机号选项，输入名字，
          点击地址，进入新页面，输入地址；点击确定;
          点击保存；
        :return:
        """

        self._driver.find_element(MobileBy.XPATH, '//*[@text="通讯录"]').click()
        self._driver.find_element(MobileBy.XPATH, '//*[@text="添加成员"]').click()
        self._driver.find_element(MobileBy.XPATH, '//*[@text="手动输入添加"]').click()

        self._driver.find_element(MobileBy.XPATH, '//*[@text="必填"]').send_keys(self.new_name)
        self._driver.find_element(MobileBy.XPATH, '//*[@text="手机号"]').send_keys('123456789123')

        # 地址 = //*[contains(@resource-id,'com.tencent.wework:id/iy')]//*[contains(@resource-id,'com.tencent.wework:id/b5n')]
        address = "//*[contains(@resource-id,'com.tencent.wework:id/iy')]//*[contains(@resource-id,'com.tencent.wework:id/b5n')]"
        self._driver.find_element(MobileBy.XPATH, address).click()
        address_name = "//*[contains(@resource-id,'com.tencent.wework:id/iz')]"
        self._driver.find_element(MobileBy.XPATH, address_name).send_keys('新地址')
        sleep(1)
        self._driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'com.tencent.wework:id/i6k')]").click()
        sleep(3)
        self._driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'com.tencent.wework:id/i6r')]").click()
        sleep(3)
        self._driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'com.tencent.wework:id/i63')]").click()
        #
        name = "//*[contains(@resource-id,'com.tencent.wework:id/icn')]/*[@class='android.widget.TextView']"
        names = [value.text for value in self._driver.find_elements(MobileBy.XPATH, name)]
        assert self.new_name in names

    def test_del(self):
        self._driver.find_element(MobileBy.XPATH, '//*[@text="通讯录"]').click()
        self._driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'com.tencent.wework:id/i6i')]").click()
        self._driver.find_element(MobileBy.XPATH, f'//*[@text="{self.new_name}"]').click()
        self._driver.find_element(MobileBy.XPATH, '//*[@text="删除成员"]').click()
        sleep(1)
        self._driver.find_element(MobileBy.XPATH, "//*[contains(@resource-id,'com.tencent.wework:id/blx')]").click()
        sleep(1)
        name_xpath = "//*[contains(@resource-id,'com.tencent.wework:id/icn')]/*[@class='android.widget.TextView']"
        names = [value.text for value in self._driver.find_elements(MobileBy.XPATH, name_xpath)]
        assert self.new_name not in names


from typing import Optional, Set, Sequence, Union
import numbers

import numpy as np

import xnmt.tensor_tools as tt
from xnmt import batchers, event_trigger, events, inferences, input_readers, reports, sent, vocabs
from xnmt.modelparts import attenders, embedders, scorers, transforms
from xnmt.models import base as models
from xnmt.transducers import recurrent, base as transducers
from xnmt.persistence import serializable_init, Serializable, bare

class SeqLabeler(models.ConditionedModel, models.GeneratorModel, Serializable, reports.Reportable):
  """
  A simple sequence labeler based on an encoder and an output softmax layer.

  Args:
    src_reader: A reader for the source side.
    trg_reader: A reader for the target side.
    src_embedder: A word embedder for the input language
    encoder: An encoder to generate encoded inputs
    transform: A transform to be applied before making predictions
    scorer: The class to actually make predictions
    inference: The inference method used for this model
    auto_cut_pad: If ``True``, cut or pad target sequences so the match the length of the encoded inputs.
                  If ``False``, an error is thrown if there is a length mismatch.
  """

  yaml_tag = '!SeqLabeler'

  @events.register_xnmt_handler
  @serializable_init
  def __init__(self,
               src_reader: input_readers.InputReader,
               trg_reader: input_readers.InputReader,
               src_embedder: embedders.Embedder = bare(embedders.SimpleWordEmbedder),
               encoder: transducers.SeqTransducer = bare(recurrent.BiLSTMSeqTransducer),
               transform: transforms.Transform = bare(transforms.NonLinear),
               scorer: scorers.Scorer = bare(scorers.Softmax),
               inference: inferences.Inference = bare(inferences.IndependentOutputInference),
               auto_cut_pad: bool = False) -> None:
    super().__init__(src_reader=src_reader, trg_reader=trg_reader)
    self.src_embedder = src_embedder
    self.encoder = encoder
    self.attender = attenders
    self.transform = transform
    self.scorer = scorer
    self.inference = inference
    self.auto_cut_pad = auto_cut_pad

  def shared_params(self) -> Sequence[Set[str]]:
    return [{".src_embedder.emb_dim", ".encoder.input_dim"},]

  def _encode_src(self, src: Union[sent.Sentence, batchers.Batch]) -> tuple:
    event_trigger.start_sent(src)
    embeddings = self.src_embedder.embed_sent(src)
    encodings = self.encoder.transduce(embeddings)
    encodings_tensor = encodings.as_tensor()
    encoding_reshaped = tt.merge_time_batch_dims(encodings_tensor)
    outputs = self.transform.transform(encoding_reshaped)
    return tt.batch_size(encodings_tensor), encodings, outputs, tt.sent_len(encodings_tensor)

  def calc_nll(self, src: Union[batchers.Batch, sent.Sentence], trg: Union[batchers.Batch, sent.Sentence]) \
          -> tt.Tensor:
    assert batchers.is_batched(src) and batchers.is_batched(trg)
    batch_size, encodings, outputs, seq_len = self._encode_src(src)

    if trg.sent_len() != seq_len:
      if self.auto_cut_pad:
        trg = self._cut_or_pad_targets(seq_len, trg)
      else:
        raise ValueError(f"src/trg length do not match: {seq_len} != {trg.sent_len()}")

    ref_action = np.asarray([trg_sent.words for trg_sent in trg]).reshape((seq_len * batch_size,))
    loss_expr_perstep = self.scorer.calc_loss(outputs, batchers.mark_as_batch(ref_action))
    loss_expr_perstep = tt.unmerge_time_batch_dims(loss_expr_perstep, batch_size)
    loss_expr = tt.aggregate_masked_loss(loss_expr_perstep, trg.mask)

    return loss_expr

  def _cut_or_pad_targets(self, seq_len: numbers.Integral, trg: batchers.Batch) -> batchers.Batch:
    old_mask = trg.mask
    if trg.sent_len() > seq_len:
      trunc_len = trg.sent_len() - seq_len
      trg = batchers.mark_as_batch([trg_sent.create_truncated_sent(trunc_len=trunc_len) for trg_sent in trg])
      if old_mask:
        trg.mask = batchers.Mask(np_arr=old_mask.np_arr[:, :-trunc_len])
    else:
      pad_len = seq_len - trg.sent_len()
      trg = batchers.mark_as_batch([trg_sent.create_padded_sent(pad_len=pad_len) for trg_sent in trg])
      if old_mask:
        trg.mask = np.pad(old_mask.np_arr, pad_width=((0, 0), (0, pad_len)), mode="constant", constant_values=1)
    return trg

  def generate(self,
               src: batchers.Batch,
               normalize_scores: bool = False) -> Sequence[sent.ReadableSentence]:
    if not batchers.is_batched(src):
      src = batchers.mark_as_batch([src])
    assert src.batch_size() == 1, "batch size > 1 not properly tested"

    batch_size, encodings, outputs, seq_len = self._encode_src(src)

    best_words, best_scores = self.scorer.best_k(outputs, k=1, normalize_scores=normalize_scores)
    best_words = best_words[0, :]
    score = np.sum(best_scores, axis=1)

    outputs = [sent.SimpleSentence(words=best_words, idx=src[0].idx,
                                   vocab=self.trg_vocab if hasattr(self, "trg_vocab") else None,
                                   output_procs=self.trg_reader.output_procs,
                                   score=score)]

    return outputs

  def set_trg_vocab(self, trg_vocab: Optional[vocabs.Vocab] = None) -> None:
    """
    Set target vocab for generating outputs. If not specified, word IDs are generated instead.

    Args:
      trg_vocab: target vocab, or None to generate word IDs
    """
    self.trg_vocab = trg_vocab


"""Test script for BAMS port."""

import random

import matplotlib.pyplot as plt
import numpy as np

from bams.learners import ActiveLearner
from bams.models import GrammarModels
from bams.query_strategies import (
    BALD,
    HyperCubePool,
    RandomStrategy,
)

s = 5822646
random.seed(s)
np.random.seed(s)

def f1(x):
    """Define a test oracle function."""
    return np.sin(10 * x[2]) + 0.1 * np.random.randn()

def f2(x):
    """Define another test oracle function."""
    return x[2]

def f3(x):
    """Define a third test oracle function."""
    return max(0, min(0.25 + 0.75 * x[2]**3, 1))

if __name__ == '__main__':

    """Compare learning rates of a random and BALD learners."""

    NUM_ROUNDS = 100
    NDIM = 5
    POOL_SIZE = 500
    BUDGET = 40
    BASE_KERNELS = ["PER", "LIN"]
    DEPTH = 1

    def grammar_oracle_sampler(ndim=1, max_depth=1, base_kernels=None, pool_size=POOL_SIZE):
        """Construct an oracle by sampling from a grammar over GPs."""
        if not base_kernels:
            base_kernels = ["LIN", "PER"]

        models = GrammarModels(
            base_kernels=base_kernels,
            max_depth=max_depth,
            ndim=ndim,
        )

        model_idx = random.randint(0, len(models) - 1)
        # print("Model {}".format(model_idx))
        model = models[model_idx]

        print(model)

        pool = HyperCubePool(ndim, pool_size)
        oracle_ys = model.sample(pool)

        def f(x, noise_level=0.00):
            """An oracle that looks up value from the sampled function."""
            idx = np.where((pool == x).all(axis=1))
            if not idx[0].size:
                raise ValueError
            else:
                return oracle_ys[idx][0] + np.random.normal(scale=noise_level)

        return f, models, model_idx

    def learning_curve(learner_factory):

        learning_curves = np.zeros((NUM_ROUNDS, BUDGET))
        for i in range(NUM_ROUNDS):
            print(i)

            learner = learner_factory()

            f0, models, model_idx = grammar_oracle_sampler(
                base_kernels=BASE_KERNELS,
                ndim=NDIM,
                max_depth=DEPTH,
            )

            print(model_idx)

            j = 0
            while learner.budget > 0:
                print(j)
                x = learner.next_query()
                y = learner.query(f0, x)
                learner.update(x, y)
                learning_curves[i, j] = learner.posteriors[model_idx]
                j += 1

        return learning_curves.mean(axis=0)

    def learner_factory_random():
        pool = HyperCubePool(NDIM, POOL_SIZE)
        qs = RandomStrategy(pool=pool)

        learner = ActiveLearner(
            query_strategy=qs,
            budget=BUDGET,
            base_kernels=BASE_KERNELS,
            max_depth=DEPTH,
            ndim=NDIM,
        )
        return learner

    def learner_factory_bald():
        pool = HyperCubePool(NDIM, POOL_SIZE)
        qs = BALD(pool=pool)

        learner = ActiveLearner(
            query_strategy=qs,
            budget=BUDGET,
            base_kernels=BASE_KERNELS,
            max_depth=DEPTH,
            ndim=NDIM,
        )
        return learner

    curve_random = learning_curve(learner_factory_random)
    curve_bald = learning_curve(learner_factory_bald)
    plot_rnd = plt.scatter(1 + np.arange(BUDGET), curve_random, label="Random")
    plot_bald = plt.scatter(1 + np.arange(BUDGET), curve_bald, label="BALD")
    plt.legend(handles=[plot_rnd, plot_bald], loc='upper right')
    plt.ylim([0, 1])
    plt.xlabel("Sample number")
    plt.ylabel("Posterior probability of generating model")
    plt.show()


from flask import render_template, session, request, Markup, redirect
from app import app, LINK, get_preview, PLATFORM_WALLET

from requests import post
from json import loads
import re

@app.route('/space/<int:id>')
@app.route('/space/<int:id>/')
def space(id):
	ip = request.remote_addr
	user = loads(post(LINK, json={'method': 'users.get', 'ip': ip, 'id': session['id']}).text)['user'] if 'id' in session else {'id': 0, 'admin': 2}

	url = 'space/%d' % id

	if 'token' in session:
		req = loads(post(LINK, json={
			'method': 'study.get',
			'token': session['token'],
			'ip': ip,
			'id': id,
		}).text)

		if req['error']:
			return render_template('message.html', cont=req['message'])

		finished = req['finished']

		req = req['study']

		req['theory'] = Markup(req['theory'])

		if req['teacher']:
			for i in range(len(req['messages'])):
				req['messages'][i]['cont'] = Markup(req['messages'][i]['cont'])

		return render_template('space.html',
			title = 'Theory',
			description = re.sub(r'\<[^>]*\>', '', req['ladder_name'] + '\n' + req['step_name'] + '\n' + req['step_cont']),
			tags = ['theory'] + req['tags'],
			url = url,

			user = user,

			LINK = LINK,
			preview = get_preview,
			PLATFORM_WALLET = PLATFORM_WALLET,

			id = id,
			student = req['student'],
			teacher = req['teacher'],
			ladder = req['ladder_name'],
			step = req['step_name'],
			ladder_id = req['ladder'],
			step_id = req['step'],
			theory = req['theory'],
			cont = Markup(req['step_cont']),
			messages = req['messages'] if req['teacher'] else [],
			status = req['status'],
			price = req['price'],
			wallet = req['wallet'],
			finished = finished,
		)
	else:
		return redirect(LINK + 'login?url=' + url)


import os
from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
log_path = here + '/blog/logs'
if not os.path.exists(log_path):
  os.makedirs(log_path+'/access_logs')
  os.makedirs(log_path+'/api_logs')

with open(os.path.join(here, 'README.rst')) as f:
    README = f.read()
requires = [
    'pyramid',
    'pyramid_chameleon',
    'pyramid_debugtoolbar',
    'pyramid_tm',
    'SQLAlchemy',
    'transaction',
    'zope.sqlalchemy',
    'waitress',
    'paste',
    'pyramid_exclog',
    'mysql-python',
    'cornice',
    'colander'
    ]

setup(name='blog',
      version=0.1,
      description='blog app',
      long_description=README,
      classifiers=[
          "Programming Language :: Python",
          "Framework :: Pylons",
          "Topic :: Internet :: WWW/HTTP",
          "Topic :: Internet :: WWW/HTTP :: WSGI :: Application"
      ],
      keywords="web services",
      author='Atul Kumar',
      author_email='atul.kumar0401@gmail.com',
      url='',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      install_requires=requires,
      entry_points = """\
      [paste.app_factory]
      main=blog:main
      """,
      paster_plugins=['pyramid'])


from django.shortcuts import render
from app.models import *
from rest_framework.response import Response
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.views import APIView
from res_manager.serializers import *
from res_manager.pagination import MangerOrderPagination
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from app.api.permissions import IsAdminUserOrReadOnly
from rest_framework_simplejwt.authentication import JWTAuthentication
from res_manager.Manager.restaurant_manager import get_category_list, get_category_by_id, post_category, put_category, \
                                            delete_category, get_item_by_id, get_item_list, post_item, put_item, \
                                            delete_item, get_address, get_sales_report

# Create your views here.
class Category(APIView):
    permission_classes = [IsAdminUserOrReadOnly]
    authentication_classes = [JWTAuthentication]

    def get(self, request, pk=None):
        if pk:
            return get_category_by_id(pk)
        else:
            return get_category_list(request)

    def post(self, request):
        return post_category(request)

    def put(self, request, pk):
        obj = Categorylist.objects.get(pk=pk)
        self.check_object_permissions(request, obj)
        return put_category(request, obj)

    def delete(self, request, pk):
        if pk:
            category = Categorylist.objects.get(pk=pk)
            self.check_object_permissions(request, category)
            return delete_category(category)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

class Item(APIView):
    permission_classes = [IsAdminUserOrReadOnly]
    authentication_classes = [JWTAuthentication]

    def get(self, request, pk=None):
        if pk:
            return get_item_by_id(pk)
        else:
            return get_item_list(request)

    def post(self, request):
        return post_item(request)

    def put(self, request, pk):
        try:
            item = Itemlist.objects.get(pk=pk)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)
        self.check_object_permissions(request, item)
        return put_item(request, item)

    def delete(self, request, pk):
        if pk:
            item = Itemlist.objects.get(pk=pk)
            self.check_object_permissions(request, item)
            return delete_item(item)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

class FoodOrder(APIView):
    pagination_class = MangerOrderPagination
    permission_classes = [IsAdminUserOrReadOnly]
    authentication_classes = [JWTAuthentication]

    @property
    def paginator(self):
        """The paginator instance associated with the view, or `None`."""
        if not hasattr(self, '_paginator'):
            if self.pagination_class is None:
                self._paginator = None
            else:
                self._paginator = self.pagination_class()
        return self._paginator

    def paginate_queryset(self, queryset):
        """Return a single page of results, or `None` if pagination is disabled."""
        if self.paginator is None:
            return None
        return self.paginator.paginate_queryset(queryset, self.request, view=self)

    def get_paginated_response(self, data):
        """Return a paginated style `Response` object for the given output data."""
        assert self.paginator is not None
        return self.paginator.get_paginated_response(data)

    def get(self, request, pk=None):
        if pk:
            try:
                order = Foodorder.objects.get(pk=pk)
                serializer = FoodOrderSerializer(order)
            except Orderitem.DoesNotExist:
                return Response(status=status.HTTP_404_NOT_FOUND)
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            order = Foodorder.objects.all().order_by('-id')
            page = self.paginate_queryset(order)
            serializer = FoodOrderSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

    def put(self, request, pk):
        try:
            order = Foodorder.objects.get(pk=pk)
        except Foodorder.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        self.check_object_permissions(request, order)
        serializer = FoodOrderSerializer(order, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class AddressManager(APIView):
    def get(self, request):
        return get_address(request)

class Sales(APIView):
    def get(self, request):
        return get_sales_report(request)


import argparse
import torch
import time

from genre.fairseq_model import GENRE
from genre.entity_linking import get_end_to_end_prefix_allowed_tokens_fn_fairseq as get_prefix_allowed_tokens_fn
from helper_pickle import pickle_load

def main(args):
    if args.yago:
        model_name = "models/fairseq_e2e_entity_linking_aidayago"
    else:
        model_name = "models/fairseq_e2e_entity_linking_wiki_abs"
    print(f"read model from {model_name}...")
    model = GENRE.from_pretrained(model_name).eval()

    if torch.cuda.is_available():
        print("move model to GPU...")
        model = model.cuda()

    mention_trie = pickle_load(args.trie)
    mention_to_candidates_dict = pickle_load(args.candidates)

    while True:
        text = input("> ")
        sentences = [text]

        start_time = time.time()

        prefix_allowed_tokens_fn = get_prefix_allowed_tokens_fn(
            model,
            sentences,
            mention_trie=mention_trie,
            mention_to_candidates_dict=mention_to_candidates_dict
        )

        result = model.sample(
            sentences,
            prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
        )

        runtime = time.time() - start_time

        print("== result ==")
        print(result)

        print("== beams ==")
        for beam in result[0]:
            print(beam)
        print(f"{runtime:.4f} seconds")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--yago", action="store_true")
    parser.add_argument("--trie", type=str, default=None, required=False)
    parser.add_argument("--candidates", type=str, default=None, required=False)
    args = parser.parse_args()
    main(args)


def noVowel(s):
    'return True if string s contains no vowel, False otherwise'
    for char in s:
        if char.lower() in 'a':
            return True
    return False

print(noVowel(input("Enter:  ")))

print("This is for two letters\n")
def noVowel(s):
    'return True if string s contains no vowel, False otherwise'
    for char in s:
        if char.lower() in ('s' and 'k'):
            return True
    return False

def jkm():
    jkm.function(noVowel)
    print(noVowel(input("Enter:  ")))

jkm()


#Classes
import time
import pandas as pd
import numpy as np
from detectors import *
import re
import os

#Fix functions

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

def replace_line(subject_path,badIndex): #Replace or remove the bad line of the file using the index

    #First open the current bad file and search for the error line
    f=open(subject_path,"r")

    data=f.readlines()
    badLine = data[badIndex-1]
    del f

    if "gamble" in badLine: #If that bad line contains gamble then try to replace it
        print("REPLACING")
        template = "    "+"\t"+"gamble"
        data[badIndex-1] = template
        with open(subject_path, 'w') as fileG:
            fileG.writelines(data)
            del fileG

    else: # If is not gamble then remove it
        print("REMOVING")
        template = "    "
        data[badIndex-1] = template
        with open(subject_path, 'w') as fileB:
            fileB.writelines(data)
            del fileB

def get_badIndex(e): # Get the index as integer from error
    result = [f for f in re.split("[^0-9]", str(e)) if f != '']
    return (max(map(int, result)))

def check_et(et_path,subject_nr): # This function will verify the et file for any problems
    temp = False
    while temp == False: # Continuosly try to read the file, if worked then stop, otherwise try to fix it
        try:
            et = pd.read_csv(et_path,sep="\t",skiprows=17,low_memory=False) # Read the tab separated file with pandas and skip the first 17 rows
            temp = True # no more bad lines in the code
        except pd.errors.ParserError as e:
            print("Subject %d has a bad line, trying to fix."%subject_nr)
            badIndex = get_badIndex(e) # Get the index of where the error was
            replace_line(et_path,badIndex) # Replace the error line with good or bad
            temp = False
    return et

#Classes
class Trial: # This class creates an object with every table trial from the subject
    def __init__(self,dataframe,x_loc):
        self.dataframe = dataframe # The trial table or dataframe in pandas
        self.x_loc = x_loc

    def get_fixations_samples(self): #GEt the fixations for the given dataframe

        #Convert the necessary rows to list so they can be used by the fixation algorithm
        dfListX = self.dataframe["GazePointX"].tolist()
        dfListY = self.dataframe["GazePointY"].tolist()
        dfListT = self.dataframe["TimeStamp"].tolist()

        dfListX =list(map(float, dfListX))
        dfListY =list(map(float, dfListY))
        dfListT =list(map(float, dfListT))

        #Take the X and Y samples
        gazeX_samples = dfListX
        gazeY_samples = dfListY

        subject_totalX = len(gazeX_samples)
        subject_totalY = len(gazeY_samples)

        subject_goodX = []
        subject_goodY = []

        #Add the good samples to a list of good samples
        for vX in gazeX_samples:
            if vX > 0:
                subject_goodX.append(vX)

        for vY in gazeY_samples:
            if vY > 0:
                subject_goodY.append(vY)

        #Calculate the percentange
        percx_good = (len(subject_goodX) /len(gazeX_samples))*100
        percy_good = (len(subject_goodY) /len(gazeY_samples))*100

        results = fixation_detection(dfListX, dfListY, dfListT, missing=0.0, maxdist=25, mindur=16.7) # Calculate the fixations
        fixations = results[1]

        #Return the fixations and the samples data
        return [fixations] + [percx_good] +[percy_good] +[len(subject_goodX)] + [len(subject_goodY)]

    def get_AOI_MT(self,AOI_p,AOI_q,AOI_x,AOI_y): #Return the the mean and total of the AOIs_perc

        #calculate the means
        p_Meanfix = mean(AOI_p)
        q_Meanfix = mean(AOI_q)
        x_Meanfix = mean(AOI_x)
        y_Meanfix = mean(AOI_y)

        #calculate the length
        p_TotalFix = len(AOI_p)
        q_TotalFix = len(AOI_q)
        x_TotalFix = len(AOI_x)
        y_TotalFix = len(AOI_y)

        return [p_Meanfix,q_Meanfix,x_Meanfix,y_Meanfix,p_TotalFix,q_TotalFix,x_TotalFix,y_TotalFix]

    def get_AOIs_perc_Perc(self):
        fixations_samples = self.get_fixations_samples()

        fixations = fixations_samples[0]

        total_fixations = 0
        bad_fixations = 0

        AOI_p = []
        AOI_x = []
        AOI_q = []
        AOI_y = []
        for f in fixations:
                endx = f[3]
                endy = f[4]
                duration = f[2]

                #AOI 1 coordonates
                a1x_up = 944
                a1x_low = 656

                a1y_up = 524
                a1y_low = 236

                #AOI 2 coordonates
                a2x_up = 1264
                a2x_low = 976

                a2y_up = 524
                a2y_low = 236

                #AOI 3 coordonates
                a3x_up = 944
                a3x_low = 656

                a3y_up = 844
                a3y_low = 556

                #AOI 4 coordonates
                a4x_up = 1264
                a4x_low = 976

                a4y_up = 844
                a4y_low = 556

                # Determine in which AOI should the fixation be
                if a1x_low  <= endx <= a1x_up and a1y_low <= endy <= a1y_up: # AOI 1
                    if self.x_loc == 1:
                        AOI_p.append(duration)

                        #Add to AOI_p
                    elif self.x_loc == 2:
                        AOI_x.append(duration)

                        #add to AOI_x
                    elif self.x_loc == 3:
                        AOI_q.append(duration)

                        #add to AOI Q
                    elif self.x_loc == 4:
                        AOI_y.append(duration)
                        #add to AOI y

                elif a2x_low  <= endx <= a2x_up and a2y_low <= endy <= a2y_up: # AOI 2*
                    if self.x_loc == 1:
                        AOI_x.append(duration)
                        #add to aoi x

                    elif self.x_loc == 2:
                        AOI_p.append(duration)
                        #add to aoi p

                    elif self.x_loc == 3:
                        AOI_y.append(duration)
                        #add to aoi y

                    elif self.x_loc == 4:
                        AOI_q.append(duration)
                        #add to q AOI

                elif a3x_low  <= endx <= a3x_up and a3y_low <= endy <= a3y_up: # AOI 3
                    if self.x_loc == 1:
                        AOI_q.append(duration)

                        #add to aoi q
                    elif self.x_loc == 2:
                        AOI_y.append(duration)

                        #add to y
                    elif self.x_loc == 3:
                        AOI_p.append(duration)

                        #add to p
                    elif self.x_loc == 4:
                        AOI_x.append(duration)

                        #add to x

                elif a4x_low  <= endx <= a4x_up and a4y_low <= endy <= a4y_up: # AOI 4*
                    if self.x_loc == 1:
                        AOI_y.append(duration)

                        #add to y
                    elif self.x_loc == 2:
                        AOI_q.append(duration)

                        #add to q
                    elif self.x_loc == 3:
                        AOI_x.append(duration)

                        #add to x
                    elif self.x_loc == 4:
                        AOI_p.append(duration)

        #Bad and Good fixations
        total_fixations = len(fixations)
        good_fixations = len(AOI_p)+len(AOI_q)+len(AOI_x)+len(AOI_y)
        bad_fixations = total_fixations - good_fixations

        #Percentage for good and bad fixations
        if good_fixations != 0 or total_fixations != 0:
            perc_good_fix = ((good_fixations/total_fixations)*100)
        else:
            perc_good_fix = 0

        if bad_fixations != 0 or total_fixations != 0:
            perc_bad_fix = ((bad_fixations/total_fixations)*100)
        else:
            perc_bad_fix = 0

        #After every fixation has been put in their particular AOI process and return the AOIs_perc
        AOIs_perc = self.get_AOI_MT(AOI_p,AOI_q,AOI_x,AOI_y)

        #Return the AOI, percentanges for fixations and samples data
        return [AOIs_perc] + [perc_good_fix] + [perc_bad_fix] + fixations_samples[1:3]

class Subject:
    def __init__(self,et_file_path,beh_file_path,subject_nr):
        self.et = et_file_path
        self.beh = beh_file_path
        self.subject_nr = subject_nr
        self.read_clean_files()

    def read_clean_files(self):
        #Load and create the dataframes(tables)
        self.et = check_et(self.et,self.subject_nr) #Clean et of any possible errors and return the dataframe - et had problems with the file
        self.beh = pd.read_csv(self.beh) #Read the table using pandas

        # Use loc method in which the first argument takes every row from the table ":" and in the second argument takes only specific columns
        self.beh = self.beh.loc[:,["CBcond","Gamble","domain","X_location","risk_selected","response_time"]]

    def get_trials(self): # This method return a list of indexes representing the start and end  of a trials in the et table

        trials = []
        trials_indexes = [] # Here we store the tuples with the start-end indexes
        ph = 0 #A place holder for start-gamble index

        temp_values = []
        ####   This may be temporary in case other columns have to be used if gamble may not be in the Event column
        self.et = self.et.loc[:,["TimeStamp","Event","GazePointX","GazePointY"]]
        #####
        for i in self.et.index:          # Loop though the et table with the index
            val = self.et.at[i,"Event"] # Pandas "at" method returns the value at that particular cell using the current row, in this the index and column
            if "gamble" in str(val): # If gamble is in value then store the index
                if ph != 0:   # if ph !=0 then ph is start-gamble
                    trials_indexes.append((ph,i)) #Append the ph and the current index
                    ph = 0 #Reset
                else: # If ph is 0 then take the index of start-gamble
                    ph = i

        if len(trials_indexes) < 150 :
            print("Warning! Subject %d has %d trials instead of 150 "%(self.subject_nr,len(trials_indexes)))
        for trial_index in trials_indexes:
            trials.append(pd.DataFrame(self.et.iloc[(trial_index[0]+1):trial_index[1]])) #Add the trial table to a list of table trials
                                                                                # "iloc" is similiar to loc but it uses indexes, in this case the start and end of a particular trial in the et table

        return trials

    def get_beh_data(self,current_trial_index): #This method return values from the beh file at the current index using the "at" method
        x_loc = self.beh.at[current_trial_index,"X_location"]
        response_time = self.beh.at[current_trial_index,"response_time"]
        domain = self.beh.at[current_trial_index,"domain"]
        gamble_nr = self.beh.at[current_trial_index,"Gamble"]
        risk = self.beh.at[current_trial_index,"risk_selected"]
        return x_loc,response_time,domain,gamble_nr,risk

    def get_output(self):# this should be main method to return the dataframe for this current subject with all of his trials processed

        trials = self.get_trials() # Get the trials found from the subject table
        subject_table = []

        #Go through the trials tables and create a trial object, return the row for that trial
        for i,trial in enumerate(trials):

            beh_data = self.get_beh_data(i) # Get the current row with the values we want from the beh file

            current_trial = Trial(trial,beh_data[0]) # Create an trial object with the trial table and the current x_loc

            AOIs_perc = current_trial.get_AOIs_perc_Perc() # Get the AOIs_perc from that trial

            row = [self.subject_nr] + [beh_data[3]] + [beh_data[2]] + AOIs_perc[0]  + [AOIs_perc[1]] + [AOIs_perc[2]] + [AOIs_perc[3]] + [AOIs_perc[4]] + [beh_data[4]]+[beh_data[1]] # Create the trial row using the AOI and the values extracted from beh

            subject_table.append(row) # Add the row to the subject table

        return subject_table


import matplotlib.pyplot as plt
import sys
import math

if len(sys.argv) == 2 :
	fichier = open(sys.argv[1], "r")
	evolution =""
	c = 0
	for line in fichier:
		c =c+1
		if "evolution" in line:
			if "densite" in line:
				evolution = "densite"
			elif "probabilite" in line:
				evolution = "probabilite"
			else:
				print("impossible de visualiser ce fichier")

		if "probaDef" in line:
			line = line.replace("probaDef:","")
			line = line.replace("\n","")
			nbProbaDef = len(line.split(","))
			valueProbaDef = line.split(",")
			valueProbaDef = list(map(float, valueProbaDef))

		if "parametre" in line:
			line = line.replace("parametre:","")
			line = line.replace("\n","")
			nbLambda = len(line.split(","))
			valueLambda = line.split(",")
			valueLambda = list(map(float, valueLambda))

		if "nbIteration" in line:
			line = line.replace("nbIteration:","")
			line = line.replace("\n","")
			nbIter = len(line.split(","))
			valueNbIter = line.split(",")
			valueNbIter = list(map(float, valueNbIter))
		if "-\n" in line:
			break

	data = [[[[]] * nbIter for i in range(nbProbaDef)]for i in range(nbLambda)]

	if evolution == "densite":
		while(line != ''):
			value = fichier.readline()
			value = value.split(",")
			for v in value:
				if "lambda" in v:
					v = v.replace("lambda:","")
					posLambda = valueLambda.index(float(v))
				if "probaDef" in v:
					v = v.replace("probaDef:","")
					posProbaDef = valueProbaDef.index(float(v))
				if "T" in v:
					v = v.replace("T:","")
					posNbIter = valueNbIter.index(float(v))

			line = fichier.readline()

			while(line != "-\n"):
				data[posLambda][posProbaDef][posNbIter].append(line)
				line = fichier.readline()
				if (line == ''):
					break

		if nbProbaDef < nbLambda:
			fig, axs = plt.subplots(1,nbProbaDef)

			for i in range(nbProbaDef):
				if nbProbaDef >1 :
					axs[i].set_title("Evolution de la densité avec $\kappa = $"+str(valueProbaDef[i]))
					axs[i].set(xlabel="nombre d'itérarions", ylabel="Population en état d'alerte(%)")
				else:
					axs.set_title("Evolution de la densité avec $\kappa = $"+str(valueProbaDef[i]))
					axs.set(xlabel="nombre d'itérarions", ylabel="Population en état d'alerte(%)")
				for j in range(nbLambda):
					abs = []
					ord = []
					for element in data[j][i][0]:
						element = element.split(",")
						ord.append(float(element[1]))
						abs.append(float(element[0]))
					if nbProbaDef >1 :
						axs[i].plot(abs,ord,label = "lambda :"+str(valueLambda[j]))
						axs[i].legend(loc = "lower right")
					else:
						axs.plot(abs,ord,label = "lambda :"+str(valueLambda[j]))
						axs.legend(loc = "lower right")
		else:
			fig, axs = plt.subplots(1,nbLambda)

			for i in range(nbLambda):
				if nbLambda >1 :
					axs[i].set_title("Evolution de la densité avec un lambda  de "+str(valueLambda[i]))
					axs[i].set(xlabel="nombre d'itérarions", ylabel="Population en état d'alerte(%)")
				else:
					axs.set_title("Evolution de la densité avec un lambda de  "+str(valueLambda[i]))
					axs.set(xlabel="nombre d'itérarions", ylabel="Population en état d'alerte(%)")
				for j in range(nbProbaDef):
					abs = []
					ord = []
					for element in data[i][j][0]:
						element = element.split(",")
						ord.append(float(element[1]))
						abs.append(float(element[0]))
					if nbLambda >1 :
						axs[i].plot(abs,ord,label = "proba de def :"+str(valueProbaDef[j]))
						axs[i].legend(loc = "lower right")
					else:
						axs.plot(abs,ord,label = "proba de def :"+str(valueProbaDef[j]))
						axs.legend(loc = "lower right")

	elif evolution == "probabilite":
		while(line != ''):
			value = fichier.readline()
			value = value.split(",")
			for v in value:
				if "lambda" in v:
					v = v.replace("lambda:","")
					posLambda = valueLambda.index(float(v))
				if "probaDef" in v:
					v = v.replace("probaDef:","")
					posProbaDef = valueProbaDef.index(float(v))
				if "T" in v:
					v = v.replace("T:","")
					posNbIter = valueNbIter.index(float(v))

			line = fichier.readline()
			while(line != "-\n"):
				data[posLambda][posProbaDef][posNbIter].append(line)
				line = fichier.readline()
				if (line == ''):
					break

		fig, axs = plt.subplots(1,nbIter)

		for k in range(nbIter):
			if nbIter > 1:
				axs[k].set_title("Evolution de la probabilité d'avoir atteint le seuil en "+str(valueNbIter[k])+" itérations")
				axs[k].set(ylabel="Probabilité(%) d'avoir atteint le seuil", xlabel="Probabilité de défaillance")
			else:
				axs.set_title("Evolution de la probabilité d'avoir atteint le seuil en "+str(valueNbIter[0])+" itérations")
				axs.set(ylabel="Probabilité(%) d'avoir atteint le seuil", xlabel="Probabilité de défaillance")
			for j in range(nbLambda):
				abs = []
				ord = []
				for i in range(nbProbaDef):
					for element in data[j][i][k]:
						ord.append(float(element))
						abs.append(float(valueProbaDef[i]))
				if nbIter > 1:
					axs[k].plot(abs,ord,label = "lambda :"+str(valueLambda[j]))
					axs[k].legend(loc = "lower right")
				else:
					axs.plot(abs,ord,label = "lambda :"+str(valueLambda[j]))
					axs.legend(loc = "lower right")

	plt.show()
else:
	print ('veuiller choisir un fichier à afficher')


#!/usr/bin/python

import sys

current_branch = None
current_num_trips = 0
current_trip_distance = 0.0

# input comes from STDIN (stream data that goes to the program)
for line in sys.stdin:
    branch, trip_distance, num_trips = line.strip().split("\t")
    try:
        num_trips = int(num_trips)
        trip_distance = float(trip_distance)
    except ValueError:
        continue

    if branch == current_branch:
        current_num_trips += num_trips
        current_trip_distance += trip_distance
    else:
        if current_branch:
            # output goes to STDOUT (stream data that the program writes)
            print("%s\t%f" %( current_branch,current_trip_distance/current_num_trips ))
        current_branch = branch
        current_num_trips = num_trips
        current_trip_distance = trip_distance

if current_branch == branch:
    print("%s\t%f" %( current_branch,current_trip_distance/current_num_trips ))


# -*- coding: utf-8 -*-
"""
  Name     : c3_03_IRRs_funciton.py
  Book     : Python for Finance (2nd ed.)
  Publisher: Packt Publishing Ltd.
  Author   : Yuxing Yan
  Date     : 6/6/2017
  email    : yany@canisius.edu
             paulyxy@hotmail.com
"""

import numpy as sp
cashflows=[55,-50,-50,-50,100]
n=1000
n_cashflow=len(cashflows)
epsilon=sp.abs(sp.mean(cashflows))*0.01
irr=[-99.00]
lag_sign=sp.sign(sp.npv(0,cashflows))
i=1
j=1
while(i<=n):
    interest=i*0.001
    s=sp.sign(sp.npv(interest,cashflows))
    i=i+1
    if s*lag_sign<0:
        lag_sign=s
        if j==1:
            irr=[interest]
            j=2
        else:
            irr.append(interest)


# Script to run a smoothing filter in chunks.

import os
import numpy as np
from osgeo import gdal
import ospybook as pb

in_fn = r"D:\osgeopy-data\Nepal\everest.tif"
out_fn = r'D:\Temp\everest_smoothed_chunks.tif'

# Open the input.
in_ds = gdal.Open(in_fn)
in_band = in_ds.GetRasterBand(1)
xsize = in_band.XSize
ysize = in_band.YSize

# Create the empty output raster.
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create(out_fn, xsize, ysize, 1, gdal.GDT_Int32)
out_ds.SetProjection(in_ds.GetProjection())
out_ds.SetGeoTransform(in_ds.GetGeoTransform())
out_band = out_ds.GetRasterBand(1)
out_band.SetNoDataValue(-99)

# Loop through the rows 100 at a time.
n = 100
for i in range(0, ysize, n):

    # Figure out how many rows can be read. Remember we want to read n + 2
    # rows if possible.
    if i + n + 1 < ysize:
        rows = n + 2
    else:
        rows = ysize - i

    # This makes sure we don't try to read off the top edge the first time
    # through.
    yoff = max(0, i - 1)

    # Read and process the data as before.
    in_data = in_band.ReadAsArray(0, yoff, xsize, rows)
    slices = pb.make_slices(in_data, (3, 3))
    stacked_data = np.ma.dstack(slices)
    out_data = np.ones(in_data.shape, np.int32) * -99
    out_data[1:-1, 1:-1] = np.mean(stacked_data, 2)

    # If it's the first time through, write the entire output array
    # starting at the first row. Otherwise, don't write the first row of
    # the output array because we don't want to overwrite good data from
    # the previous chunk. Because we're ignoring this first row, the row
    # offset needs to be increased.
    if yoff == 0:
        out_band.WriteArray(out_data)
    else:
        out_band.WriteArray(out_data[1:], 0, yoff + 1)

# Finish up.
out_band.FlushCache()
out_band.ComputeStatistics(False)

del out_ds, in_ds


import csv
import glob
import pandas as pd

def csv_writer_spisok(data, path):
    new_list = []
    for i in range(len(data)):
        listys = [data[i]]
        new_list.append(listys)
    with open(path, "w", encoding='utf-8', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerows(new_list)
        csv_file.close()

def csv_writer(data, path):
    with open(path, "w", encoding='utf-8', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerows(data)
        csv_file.close()

def file_search(way, name):
    way_list_xls = glob.glob(way, recursive=True)
    name_ = name
    csv_writer_spisok(way_list_xls, name_)
    '''
    way_list_xlsx = glob.glob(
        'W:\\Theoretical Planning\\02 - Бланки заказов (All foto cmd)\\**\\*.xlsx',
        recursive=True)
    name1 = "C:\\Python's_project\\RTB-job\\отгрузочные часы\\new_general_address_file2021(xlsx).csv"
    csv_writer_spisok(way_list_xlsx, name1)
    '''
    return [name, 'new_general_address_file2021(xlsx).csv']

def search_by_client(address_file, year):
    file_csv = pd.read_csv(address_file, names=['path'])
    address_file_client = []
    file = file_csv.loc[:, 'path']
    for i in range(len(file)):
        file_adrress_client = []
        name_file = file[i].split("\\")
        try:
            if not 'рхив' in name_file[-2]:
                num_order = int(name_file[-1][:5])
                file_adrress_client.append(file[i])
                file_adrress_client.append(num_order)
                address_file_client.append(file_adrress_client)
        except ValueError:
            continue
    name = 'address_and_order_' + str(year) + '.csv'
    csv_writer(address_file_client, name)
    return name

отгрузочные часы/step_1.py


import unittest
import target_calculator as tc

class target_calculator_test(unittest.TestCase):
    simpleCase = [1,2,3]
    simpleCoefficients = [1,1,1]
    def test_sum_of_products_mutability(self):
        tc.calculate_sum_of_products(self.simpleCase, self.simpleCoefficients)

        self.assertEqual(self.simpleCase, [1,2,3])

        self.assertEqual(self.simpleCoefficients, [1,1,1])

    def test_sum_of_products_simple(self):
        result = tc.calculate_sum_of_products(self.simpleCase, \
                self.simpleCoefficients)

        expected = 6

        self.assertEqual(expected, result)

    def test_sum_of_products_interesting(self):
        result = tc.calculate_sum_of_products(self.simpleCase,[0.1,0.4,0.5])

        expected = 2.4

        self.assertEqual(result,expected)

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


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author:     jinzj
@desc:
"""
import time
from appium import webdriver
import unittest

class MyTestCase(unittest.TestCase):

    def setUp(self):
        desired_caps={}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.4.4'
        desired_caps['deviceName'] = '0c875323'
        desired_caps['appPackage'] = 'com.cloudy.linglingbang'
        desired_caps['appActivity'] = '.activity.welcome.WelcomeActivity'
        desired_caps["unicodeKeyboard"] = "True" #不会自动转码
        desired_caps["resetKeyboard"] = "True"  #输入完成后自动转为默认键盘
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def testadd(self):
        time.sleep(5)
        print(self.driver.contexts)
        print(self.driver.current_context)
        print(self.driver.current_activity)
        self.driver.find_element_by_id('owner_btn').click()
        print(self.driver.current_activity)
        time.sleep(2)
        self.driver.find_element_by_id('tv_login').click()
        print(self.driver.current_activity)
        # phone = self.driver.find_element_by_android_uiautomator("new UiSelector().resourceID('com.cloudy.linglingbang:id/login_et_phone')")
        phone = self.driver.find_element_by_xpath(r'//android.widget.RelativeLayout[@id="com.cloudy.linglingbang:id/login_et_phone"]/descendant::android.widget.EditText')
        phone.clear()
        phone.send_keys('15801006286')
        # mima = self.driver.find_element_by_android_uiautomator("new UiSelector().resourceID('com.cloudy.linglingbang:id/login_et_password')")
        mima = self.driver.find_element_by_xpath(r'//android.widget.RelativeLayout[@id="com.cloudy.linglingbang:id/login_et_password"]/descendant::android.widget.EditText')
        mima.clear()
        mima.send_keys('123456')
        time.sleep(2)
        # self.driver.find_element_by_xpath(r'//android.support.v4.view.ViewPager[3]/android.widget.LinearLayout[0]/android.widget.RelativeLayout[0]/android.widget.RelativeLayout[3]/android.widget.Button').click()
        self.driver.find_element_by_id(r'com.cloudy.linglingbang:id/btn_login').click()
        # self.driver.find_element_by_name('登录').click() 第一，appium1.5及之后的版本废弃了name属性(如name=账单，将不被支持用于定位)，name被废弃了，但是xpath的写法如//android.widget.TextView[@text="账单"]是被支持的
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()


def cutRod(N, myData) :
    '''
    통나무의 길이 N과 가격표가 dictionary로 주어질 때, 통나무를 잘라서 얻을 수 있는 최대 이익을 반환하세요.
    예) 길이 1의 가격이 3일 때, myData[1] = 3
    max_myData[i] = max(max_myData[n-i] + myData[i] for 0 ~ n)
    '''

    max_myData = [0 for i in range(N+1)]

    for i in range(N+1):
        for l, p in myData.items():
            if i >= l:
                max_myData[i] = max(max_myData[i], max_myData[i-l] + p)

    return max_myData[-1]

def main():
    N, M = map(int, input().split())
    arr = {}
    for i in range(M):
        l, p = list(map(int, input().split()))
        arr[l] = p
    print(cutRod(N, arr))

if __name__ == "__main__":
    main()


import numpy

def integer_to_edge_condition( k ):
    """Given a pattern id k, return left, middle, and right conditions

    Args:
        k (int): pattern id

    Returns:
        (Lcond, Mcond, Rcond) a tuple of three functions.
    """
    assert k < 63, "Input k = {} must be < 63.".format(k)

    bdigs = [ int(bit) for bit in '{:06b}'.format(k) ]

    Lcond = lambda x, y : x==bdigs[0] and y==bdigs[1]
    Rcond = lambda x, y : x==bdigs[4] and y==bdigs[5]

    #xs and ys here MUST be numpy arrays or the logical check will fail
    #Mcond = lambda xs, ys : numpy.all(xs==bdigs[2]) and numpy.all(ys==bdigs[3])

    Mcond = lambda xs, ys : numpy.logical_and( numpy.logical_xor(not bdigs[2], xs),  numpy.logical_xor(not bdigs[3], ys) )

    return (Lcond, Mcond, Rcond)

def consecutive(data, stepsize=1):
    """Find left and right indices of consecutive elements.

    Args:
        data (numpy.array(int)): 1D numpy.array

    Returns:
        [(L, R)] a list of tuples that contains the left and right indeces in data that breaks continuity.
    """
    if len(data)==0:
        return data

    runs = numpy.split(data, numpy.where(numpy.diff(data) != stepsize)[0]+1)
    return [ (run[0],run[-1]) for run in runs ]

def ranges_satisfying_condition( A, B, Lcond, Mcond, Rcond ):
    """Compute the left and right boundries of a particular pause pattern.

    Args:
        A (numpy.array(int)): 0-1 1D numpy integer array with 1s marking pause of speaker A.
        B (numpy.array(int)): 0-1 1D numpy integer array with 1s marking pause of speaker B.
        Lcond (function): bool, bool -> bool
        Mcond (function): numpy.array(bool), numpy.array(bool) -> numpy.array(bool)
        Rcond (function): bool, bool -> bool

    Returns:
        [(L, R)] a list of tuples that contains the left and right indeces in pause profiles that satifies Lcond and Mcond and Rcond
    """
    N = A.shape[0]
    middles = consecutive(numpy.where( Mcond(A, B) == True )[0])
    return [ (L,R) for (L,R) in middles if L>0 and R+1<N and Lcond(A[L-1],B[L-1]) and Rcond(A[R+1],B[R+1]) ]

#up = uptake
#so = successful overtake
#fo = failed overtake
#ip = inner pause
name_to_edge_condition = dict({
    "AupB":integer_to_edge_condition(18),
    "BupA":integer_to_edge_condition(33),
    "AsoB":integer_to_edge_condition(30),
    "AfoB":integer_to_edge_condition(29),
    "BsoA":integer_to_edge_condition(45),
    "BfoA":integer_to_edge_condition(46),
    "AipB":integer_to_edge_condition(34),
    "BipA":integer_to_edge_condition(17)
})

short_to_long_name = dict({
    "AupB":"A uptakes B",
    "BupA":"B uptakes A",
    "AsoB":"A successful overtake B",
    "AfoB":"A failed overtake B",
    "BsoA":"B successful overtake A",
    "BfoA":"B failed overtake A",
    "AipB":"A inner pause",
    "BipA":"B inner pause",
})

################################testing backyard################################
#def test_edge_conditions():
#    #A UP B
#    L,M,R = name_to_edge_condition["AupB"]
#    A = numpy.array([0,0,0,0,1])
#    B = numpy.array([1,0,0,0,0])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #B UP A
#    L,M,R = name_to_edge_condition["BupA"]
#    A = numpy.array([1,0,0,0,0])
#    B = numpy.array([0,0,0,0,1])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #A SO B
#    L,M,R = name_to_edge_condition["AsoB"]
#    A = numpy.array([0,1,1,1,1])
#    B = numpy.array([1,1,1,1,0])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #A FO B
#    L,M,R = name_to_edge_condition["AfoB"]
#    A = numpy.array([0,1,1,1,0])
#    B = numpy.array([1,1,1,1,1])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #B SO A
#    L,M,R = name_to_edge_condition["BsoA"]
#    A = numpy.array([1,1,1,1,0])
#    B = numpy.array([0,1,1,1,1])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #B FO A
#    L,M,R = name_to_edge_condition["BfoA"]
#    A = numpy.array([1,1,1,1,1])
#    B = numpy.array([0,1,1,1,0])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #A IP B
#    L,M,R = name_to_edge_condition["AipB"]
#    A = numpy.array([1,0,0,0,1])
#    B = numpy.array([0,0,0,0,0])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )
#
#    #B IP A
#    L,M,R = name_to_edge_condition["BipA"]
#    A = numpy.array([0,0,0,0,0])
#    B = numpy.array([1,0,0,0,1])
#    print( L(A[0],B[0]), M(A[1:-1],B[1:-1]), R(A[-1],B[-1]) )


from lib import requests
import uuid
import webbrowser
import threading
import httplib
import json
import time
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import urlparse
from workflow import Workflow

auth_url = "https://www.wunderlist.com/oauth/authorize"
callback = "http://127.0.0.1:8080"

class OAuthListener(BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)
        self.query = parsed_path.query
        try:
            code = self.query[self.query.find("code")+5:]
            state = self.query[self.query.find("state")+6:self.query.find("code")-1]
            message = "
Authorization successful
"
            set_token(state, code)
            self.wfile.write(message)
        except:
            pass
        finally:
            self.server.stop = True
        return

    def do_QUIT(self):
        self.send_response(200)
        self.end_headers()
        self.server.stop = True

class StoppableServer(HTTPServer):
    def serve_forever(self):
        self.stop = False
        while not self.stop:
            self.handle_request()

def end_server(s):
    time.sleep(15)
    s.stop = True

def set_token(state, code):
    wf = Workflow()
    if wf.stored_data('state') != None and wf.stored_data('state') == state:
        wf.store_data('code', code)
    token_url = "https://www.wunderlist.com/oauth/access_token"
    headers = {"accept": "application/json"}
    params = {"client_id": wf.settings['api']['client_id'],
              "client_secret": wf.settings['api']['client2'],
              "code": wf.stored_data('code')}
    r = requests.post(token_url, data=params, headers=headers)
    data = r.json()
    token = data['access_token']
    wf.save_password('token', token)

def do_login():
    wf = Workflow()
    if wf.stored_data('state') == None:
        wf.store_data('state', str(uuid.uuid1()))
    server = StoppableServer(('127.0.0.1', 8080), OAuthListener)
    t = threading.Thread(target=server.serve_forever)
    stop = threading.Thread(target=end_server, args=(server,))
    url = auth_url + "?client_id=" + wf.settings['api']['client_id']
    url += "&redirect_uri=" + callback
    url += "&state=" + wf.stored_data('state')
    webbrowser.open(url)
    t.start()
    stop.start()


import argparse
import os
from pathlib import Path
import subprocess
import sys
import tempfile

def main() -> int:
    """Run the vendoring utility. See module-level docstring."""
    loud = False
    if os.environ.get("DEBUG") or os.environ.get("V"):
        loud = True

    # No options or anything for now, but I guess
    # you'll figure that out when you run --help.
    parser = argparse.ArgumentParser(
        prog="vendor",
        description="QEMU python vendoring utility",
    )
    parser.parse_args()

    packages = {
        "meson==0.63.3":
        "d677b809c4895dcbaac9bf6c43703fcb3609a4b24c6057c78f828590049cf43a",

        "tomli==2.0.1":
        "939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc",
    }

    vendor_dir = Path(__file__, "..", "..", "wheels").resolve()

    with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8") as file:
        for dep_spec, checksum in packages.items():
            print(f"{dep_spec} --hash=sha256:{checksum}", file=file)
        file.flush()

        cli_args = [
            "pip",
            "download",
            "--dest",
            str(vendor_dir),
            "--require-hashes",
            "-r",
            file.name,
        ]
        if loud:
            cli_args.append("-v")

        print(" ".join(cli_args))
        subprocess.run(cli_args, check=True)

    return 0

if __name__ == "__main__":
    sys.exit(main())


#!/usr/bin/python3
"""
You have n number of locked boxes in front of you. Each box is numbered
sequentially from 0 to n - 1 and each box may contain keys to the other boxes.
"""

def canUnlockAll(boxes):
    """
    Method that checks if every box can be opened
    """
    opened = [0]
    if len(boxes) == 0:
        return True
    for box in opened:
        for key in boxes[box]:
            if key not in opened:
                if key < len(boxes):
                    opened.append(key)

    if len(opened) == len(boxes):
        return True
    return False


from db import DB

class BaseModel:
    db: DB = None
    def __init__(self, db: DB) -> None:
        self.db = db
    #region Create
    def createRecord(self, sql_query, sql_datas: list[tuple] = []) -> None:
        try:
            cursor = self.db._conn.cursor()
            if len(sql_datas) == 0:
                cursor.execute(sql_query)
            elif len(sql_datas) == 1:
                print(sql_datas)
                cursor.execute(sql_query, sql_datas[0])
            else: # if many insertions for example
                cursor.executemany(sql_query, sql_datas)
            self.db._conn.commit()
        except Exception as err:
            print("Virhe lisättäessä rivejä tietokantaan.")
            print(err)
        return None
    #endregion Create
    #region Read
    def readRecords(self, sql_query, sql_datas: list[tuple] = []) -> list[tuple]:
        records = []
        try:
            cursor = self.db._conn.cursor()
            if len(sql_datas) == 0:
                cursor.execute(sql_query)
            elif len(sql_datas) == 1:
                cursor.execute(sql_query, sql_datas)
            else: # if many insertions for example
                cursor.executemany(sql_query, sql_datas)
            self.db._conn.commit()
            records = cursor.fetchall()
        except Exception as err:
            print("Virhe lisättäessä rivejä tietokantaan.")
            print(err)
        return records
    #endregion Read
    #region Update

    #endregion Update
    #region Delete

    #endregion Delete


from pyspark import SparkContext
from pyspark.sql import HiveContext
import pyspark.sql.functions as F
import datetime
import os
import csv
import sys
import time

def myConcat(*cols):
    concat_columns = []
    for c in cols[:-1]:
        concat_columns.append(F.coalesce(c, F.lit("*")))
        concat_columns.append(F.lit(","))
    concat_columns.append(F.coalesce(cols[-1], F.lit("*")))
    return F.concat(*concat_columns)

sc = SparkContext(appName="Hive_Connect")
sqlContext = HiveContext(sc)
sqlContext.setConf('spark.sql.shuffle.partitions', '50')

try:
    df = df_text = None
    query1 = """
                     SELECT
                        ts_r,
                        trx_date,
                        a_party,
                        charging_id,
                        dialled_apn,
                        event_id
                    FROM (
                        SELECT *,
                            ROW_NUMBER()
                            OVER (PARTITION BY event_date, ORDER BY file_id DESC) as rank
                        FROM base.temp_usage_ocs_chg
                        ) ranked_usage_ocs_chg
                    WHERE ranked_usage_ocs_chg.rank=1;
                """
    df = sqlContext.sql(query1)
    # df_text = df.withColumn("combined", myConcat(*df.columns)).select("combined")
    df.repartition(1).rdd.map(lambda x: ",".join(map(str, x))).saveAsTextFile(
        'hdfs://nameservice1/user/13010840/qtspark_lrf/19GPRS_1')
    # df_text.write.format("text").option("header", "false").mode("append").save(
    # 'hdfs://nameservice1/user/13010840/qtspark_lrf') df.repartition(1000).write.option("header", "false").mode(
    # "append").csv("hdfs://nameservice1/user/13010840/qtspark_lrf")
except Exception as e:
    print("Exception:{0}".format(e))

sc.stop()