from cxxdoc.generators.generator import Generator
from cxxdoc.nodes.cxxfunction import CXXParam
from cxxdoc.renderers import markdown, pygments
import cxxdoc.diagnostics as diagnostics

from jinja2 import Environment, FileSystemLoader

import os
import shutil
import glob
import subprocess

class Theme(object):
    def __init__(self, root):
        self.root = root
        self.asset_root = os.path.join(root, 'assets')
        self.template_root = os.path.join(root, 'templates')
        self.css_root = os.path.join(self.asset_root, 'css')
        self.scss_root = os.path.join(self.asset_root, 'scss')

class HTMLGenerator(Generator):
    LAYOUT_NAME = 'layout'
    DOC_NAME = 'doc'
    ELEMENT_NAME = 'element'
    STATIC_DIR = 'static'

    def __init__(self, tree, theme_root, output_directory, extra_styles=None):
        # TODO(Jacob): add option for generating based on links
        # TODO(Jacob): add option for different extensions
        super().__init__(tree, output_directory)
        self.extension = 'html'
        self.static_output = os.path.join(self.output_directory, self.STATIC_DIR)
        self.extra_styles = extra_styles

        self.theme = Theme(theme_root)
        self.env = Environment(
            loader=FileSystemLoader(self.theme.template_root)
        )
        self.layout = self.__get_template(HTMLGenerator.LAYOUT_NAME)
        self.doc = self.__get_template(HTMLGenerator.DOC_NAME)
        self.element = self.__get_template(HTMLGenerator.ELEMENT_NAME)
        self.data = {
            'static_files': os.path.join('.', self.STATIC_DIR)
        }
        self.markdown = markdown.MarkdownRenderer()

    def generate_impl(self):
        if os.path.exists(self.static_output):
            shutil.rmtree(self.static_output)

        self.__generate_styles(self.theme.css_root)
        self.__generate_styles(self.theme.scss_root)
        if self.extra_styles is not None:
            self.__generate_styles(self.extra_styles)

        for elem, children in self.tree.items():
            self.__generate_files(elem, children)

    def __get_template(self, name):
        return self.env.get_template(name + '.' + self.extension)

    def __generate_files(self, file, children):
        for c in children:
            doc = self.__generate_doc(file, c)
            if isinstance(c, CXXParam):
                continue
            path = os.path.join(self.output_directory, c.get_filename() + '.html')
            with open(path, 'w') as html:
                html.write(doc)

    def __generate_doc(self, file, node):
        self.__generate_files(file, node.children)

        children = []
        for c in node.children:
            if not isinstance(c, CXXParam):
                highlighter = pygments.PygmentsRenderer('cpp', c.get_signature())
                keys = {
                    'name': c.cursor.spelling,
                    'signature': highlighter.highlight_inline(),
                    'link': c.get_filename() + '.html'
                }
                if c.docobj is not None:
                    keys['abstract'] = markdown.MarkdownRenderer().render(c.docobj.abstract)
                children.append(keys)

        highlighter = pygments.PygmentsRenderer('cpp', node.get_signature())
        keys = {
            'data': self.data,
            'file': file,
            'kind': node.kind,
            'name': node.cursor.spelling,
            'signature': highlighter.highlight_inline(),
            'children': children
        }

        if node.docobj is not None:
            keys['abstract'] = self.markdown.render(node.docobj.abstract)
            keys['discussion'] = self.markdown.render(node.docobj.discussion)
            keys['params'] = node.docobj.params
            keys['return'] = node.docobj.return_val

        return self.doc.render(keys)

    def __generate_styles(self, path):
        for p in glob.glob(os.path.join(path, '*.css')):
            shutil.copy(p, self.static_output)

        sass_files = glob.glob(os.path.join(path, '*.scss'))
        if len(sass_files) <= 0:
            return

        sass = shutil.which('sass')
        if sass is None:
            diagnostics.write_warning('SASS compilation not available. Install SASS for '
                                      'automatic compilation of *.scss stylesheets')
        if sass is not None:
            diagnostics.write_message('compiling SASS files')
            sass_dirs = '{0}:{1}'.format(path, self.static_output)
            subprocess.call([sass, '--update', sass_dirs])


from pandemic.client import BaseSimulator
import os
from pprint import pprint
import uuid

class AmphoraSimulator(BaseSimulator):

    # An example of logging simulation metrics somewhere

    def __init__(self, params=None, baseline=None):
        super().__init__(params=params, baseline=baseline)
        self.credentials = os.environ.get('AMPHORA')

    def start_of_run_callback(self):
        super().start_of_run_callback()
        self.run_id = str(uuid.uuid4())

    def end_of_day_callback(self):
        super().end_of_day_callback()
        payload = {'run_id':self.run_id,
                   'day':self.state['day'],
                   'metrics':self.daily_metrics(),
                   'positions':self.state['positions'],
                   'status': self.state['status']
                   }
        print('Pretending to send')
        pprint(payload)

if __name__=="__main__":
    os.environ['AMPHORA'] = 'Collosal Oca'
    simulator = AmphoraSimulator(baseline='town')
    simulator.run()


# coding: utf-8
# vim:fenc=utf-8:sts=0:ts=4:sw=4:et:tw=80

#
# Copyright © 2016 gr4ph3 <giraffeoncode@gmail.com>
#
# Distributed under terms of the MIT license.
#
from __future__ import unicode_literals
import re
from cached_property import cached_property
try:
    from seasonvar.requester import SeasonvarRequester
except ImportError:
    from requester import SeasonvarRequester

SEASON = r'(\/serial-(?P<id>\d+)-(?:.+?)'\
         '(?:-(?:\d+)-(?:sezon|season))?\.html)'

def secure(html):
    regexp = re.compile(r'secureMark\s*=\s*"([a-f0-9]+)"')
    for result in regexp.findall(html):
        return result

def url2thumb(url):
    regexp = re.compile(r'^(?:http:.*?)?{0}$'.format(SEASON))
    m = regexp.match(url)
    if m:
        return 'http://cdn.seasonvar.ru'\
               '/oblojka/{0}.jpg'.format(m.group('id'))

class Season:
    def __init__(self, **kwargs):
        self.url = kwargs.get('url')
        self.id = kwargs.get('id')
        self.trname = kwargs.get('trname')
        self.name = kwargs.get('name')
        self.number = kwargs.get('number')
        self.thumb = url2thumb(self.url)
        self.html = kwargs.get('html')
        self.__requester = kwargs.get('requester', SeasonvarRequester())
        self.__secure = None

    @property
    def episodes(self):
        playlist_url = 'http://seasonvar.ru/playls2/{0}x/trans/{1}/'\
                       'list.xml'.format(self.secure, self.id)
        playlist = self.__requester.get_json(playlist_url)
        return list(self._playlist_entries(playlist))

    def _playlist_entries(self, playlist_dict):
        playlist = playlist_dict['playlist']
        for entry in playlist:
            if 'playlist' in entry:
                for episode in entry['playlist']:
                    yield {'url': episode['file'],
                           'name': episode['comment'].replace('
', ' '),
                           'thumb': self.thumb}
            else:
                yield {'url': entry['file'],
                       'name': entry['comment'].replace('
', ' '),
                       'thumb': self.thumb}

    @cached_property
    def secure(self):
        return secure(self.html)

class Series:
    def __init__(self, url):
        self.__requester = SeasonvarRequester()
        absurl = self.__requester.absurl(url)
        relurl = self.__requester.relurl(url)
        self.__url = relurl
        self.__html = self.__requester.get(absurl)
        self.__current_season = None

    @cached_property
    def seasons(self):
        return list(self._seasons_from_html())

    @cached_property
    def current_season(self):
        for season in self.seasons:
            if season.url == self.__url:
                season.html = self.__html
                return season

    def _seasons_from_html(self):
        regexp = re.compile(r'
.*?]+?href="{0}"'.format(SEASON),
                            re.DOTALL)
        for num, (surl, sid) in enumerate(regexp.findall(self.__html), 1):
            yield Season(url=surl, id=sid, number=num,
                         requester=self.__requester)


'''
quick script test to download files and store the content
'''

import requests, os

print(os.getcwd())

url = 'http://www.textfiles.com/computers/accel.txt'  # update url accordingly
fileName = 'test'  # name the file to be written without the extension

downloadFile = requests.get(url)
writtenFile = '%s.txt' % fileName

if downloadFile.status_code == 200:
    print('File downloaded to the variable successfully')
else:
    print('File download error. Try again')

print('')
with open(writtenFile, 'wb') as f:
    for x in downloadFile.iter_content(1000):
        f.write(x)

with open(writtenFile, 'r') as f:
    tempStorage = f.read()

print(tempStorage)


import numpy as np
import os
from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout, Lambda, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD, RMSprop, Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import multi_gpu_model
import tensorflow as tf
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
import pickle

path = "/nfs/turbo/intmed-bnallamo-turbo/wsliu/Data/UC_colonoscopy/"
model_path = path + 'models/'

batch_size=32

train_datagen = ImageDataGenerator(
        rotation_range=180,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.1,
        zoom_range=0.2,
        horizontal_flip=True,
        vertical_flip=True,
        fill_mode='nearest',
        rescale=1./255)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        path+'split_patients/train/',  # this is the target directory
        target_size=(256, 320),
        batch_size=batch_size,
        class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
        path+'split_patients/validation/',
        target_size=(256, 320),
        batch_size=batch_size,
        shuffle=False,
        class_mode='categorical')

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(256, 320, 3), padding='same', activation='relu'))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2), strides=(2,2)))

model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2), strides=(2,2)))

model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2, 2), strides=(2,2)))

model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(4))
model.add(Activation('sigmoid'))

adam = Adam()
model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['accuracy'])

checkpointer = ModelCheckpoint(filepath=model_path+'splitp0201.h5', verbose=0, save_best_only=True, save_weights_only=True)
reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, patience=10, min_lr=1.e-7)
earlystop = EarlyStopping(monitor='loss', patience=30)

history = model.fit_generator(
            train_generator,
            steps_per_epoch=train_generator.samples // (batch_size),
            epochs=200,
            validation_data=validation_generator,
            validation_steps=validation_generator.samples // (batch_size),
            callbacks = [checkpointer, reduce_lr, earlystop],
            verbose=2);

model.save_weights(model_path+'splitp0201f.h5')

with open('output/splitp0201.pkl', 'wb') as f:
    pickle.dump(history.history, f, -1)


import configs
from model_utils import *
from typing import *
from model import Model
import os
from log import log
import time
import data_utils
import re
from nltk.translate.bleu_score import corpus_bleu
import json
from informative_lr_scheduler import InformativeLrScheduler

class Runner:
    def __init__(self):
        self.model = Model()
        self.xe_loss = nn.CrossEntropyLoss(ignore_index=data_utils.tgt_vocab.padding_id)
        self.optimizer = optim.Adam(self.model.parameters(), lr=configs.lr)
        self.lr_scheduler = InformativeLrScheduler(
            self.optimizer, 'min',
            patience=configs.lr_scheduler_patience,
            factor=configs.lr_scheduler_factor, verbose=True
        )
        self.epoch_idx = 0
        self.min_validating_ppl = 1000.
        self.ckpt_id = '0.0'
        self.prev_ckpt_path = ''

    def train(self):
        if configs.ckpt_id or configs.loads_ckpt or configs.loads_best_ckpt:
            self.load_ckpt()

        while self.epoch_idx < configs.epoch_num:
            log(f'starting epoch {self.epoch_idx}')
            log('training')

            avg_loss = 0.
            batch_num = 0
            next_logging_pct = .5

            start_time = time.time()

            for pct, batch in data_utils.gen_batches('train'):
                batch_num += 1
                self.model.train()
                (
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch,
                    # [max_tgt_len, batch_size], [batch_size]
                    tgt_sent_batch, tgt_len_batch,
                    _

                ) = batch

                self.optimizer.zero_grad()

                # [max_tgt_len - 1, batch_size, tgt_vocab_size]
                tgt_word_logit_vecs_batch = self.model(
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch,
                    # [max_tgt_len, batch_size], [batch_size]
                    tgt_sent_batch, tgt_len_batch,
                )

                loss = self.xe_loss(
                    # [(max_tgt_len - 1 * batch_size), tgt_vocab_size]
                    tgt_word_logit_vecs_batch.view(-1, data_utils.tgt_vocab.size),
                    # [(max_tgt_len - 1) * batch_size]
                    tgt_sent_batch[1:].contiguous().view(-1).to(torch.device(configs.decoder_device_id))
                )

                loss.backward()

                if configs.clips_grad_norm:
                    nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=configs.max_grad_norm)

                self.optimizer.step()
                avg_loss += loss.item()

                if pct >= next_logging_pct:
                    log(
                        f'{int(pct)}%, avg_train_loss: {avg_loss / batch_num:.6}, '
                        f'avg_train_ppl: {math.exp(avg_loss / batch_num):.6}, '
                        f'time: {time.time() - start_time:.6}'
                    )
                    next_logging_pct += 10.

            log(
                f'100%, avg_train_loss: {avg_loss / batch_num:.6}, '
                f'avg_train_ppl: {math.exp(avg_loss / batch_num):.6}, '
                f'time: {time.time() - start_time:.6}'
            )

            self.validate()
            self.epoch_idx += 1

    def validate(self):
        with torch.no_grad():
            log('validating')

            self.model.eval()
            batch_num = 0
            avg_loss = 0.
            next_logging_pct = .5

            start_time = time.time()

            for pct, batch in data_utils.gen_batches('valid'):
                batch_num += 1
                (
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch,
                    # [max_tgt_len, batch_size], [batch_size]
                    tgt_sent_batch, tgt_len_batch,
                    _
                ) = batch

                # [max_tgt_len - 1, batch_size, tgt_vocab_size]
                tgt_word_logit_vecs_batch = self.model(
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch,
                    # [max_tgt_len, batch_size], [batch_size]
                    tgt_sent_batch, tgt_len_batch,
                )

                loss = self.xe_loss(
                    # [(max_tgt_len - 1 * batch_size), tgt_vocab_size]
                    tgt_word_logit_vecs_batch.view(-1, data_utils.tgt_vocab.size),
                    # [(max_tgt_len - 1) * batch_size]
                    tgt_sent_batch[1:, :].contiguous().view(-1).to(torch.device(configs.decoder_device_id))
                )
                avg_loss += loss.item()

                # avg_dist += self.calc_avg_dist(best_seq_batch, transcript_batch, transcript_len_batch)

                if pct >= next_logging_pct:
                    log(
                        f'{int(pct)}%, avg_dev_loss: {avg_loss / batch_num:.6}, '
                        f'avg_dev_ppl: {math.exp(avg_loss / batch_num):.6}, '
                        f'time: {time.time() - start_time:.6}'
                    )
                    next_logging_pct += 10.

            avg_loss /= batch_num
            avg_ppl = math.exp(avg_loss)

            log(
                f'{int(pct)}%, avg_dev_loss: {avg_loss:.6}, '
                f'avg_dev_ppl: {avg_ppl:.6}, '
                f'time: {time.time() - start_time:.6}'
            )

            if self.lr_scheduler.step(avg_loss) and configs.training and configs.backtracks_when_worse:
                configs.uses_new_optimizer = True
                self.load_ckpt(self.prev_ckpt_path)
                self.epoch_idx -= 1

            saved_ckpt = False

            if avg_ppl < self.min_validating_ppl:
                self.min_validating_ppl = avg_ppl

                min_validating_ppl_file = open(configs.min_validating_ppl_path)

                if avg_ppl < float(min_validating_ppl_file.readline().strip()):
                    min_validating_ppl_file.close()
                    min_validating_ppl_file = open(configs.min_validating_ppl_path, 'w')
                    print(avg_ppl, file=min_validating_ppl_file)
                    self.save_ckpt()
                    saved_ckpt = True

                min_validating_ppl_file.close()

            if configs.saves_every_ckpts and not saved_ckpt:
                self.save_ckpt()

    def test(self, name='test'):
        with torch.no_grad():
            log('testing')
            self.load_ckpt()
            self.model.eval()
            next_logging_pct = .5
            best_seqs = [[] for idx in range(data_utils.get_dataset_size(name))]
            start_time = time.time()

            for pct, batch in data_utils.gen_batches(name):
                (
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch,
                    _, _,
                    # [batch_size]
                    idx_batch
                ) = batch
                max_src_len, *_ = src_sent_batch.shape
                best_seq_batch = self.model.decode(
                    # [max_src_len, batch_size], [batch_size]
                    src_sent_batch, src_len_batch
                )

                for idx, best_seq in zip(idx_batch, best_seq_batch):
                    best_seqs[idx] = data_utils.tgt_vocab.to_words(best_seq)

                if pct >= next_logging_pct:
                    log(
                        f'{int(pct)}%, time: {time.time() - start_time:.6}'
                    )
                    next_logging_pct += 10.

            references = data_utils.get_references(name)

            bleu_score = Runner.compute_bleu_score(
                references=references,
                hypotheses=best_seqs
            )

            log(
                f'100%, time: {time.time() - start_time:.6}, bleu: {bleu_score}'
            )

            with open(f'{configs.results_dir}/results.{name}.{self.ckpt_id}.txt', 'w') as results_file:
                # for best_seq, reference in zip(best_seqs, references):
                for best_seq in best_seqs:
                    print(' '.join(best_seq), file=results_file)
                    # print(' '.join(reference), file=results_file)
                    # print('', file=results_file)
            os.system(
                f'./multi-bleu.perl data/{name}.de-en.en < {configs.results_dir}/results.{name}.{self.ckpt_id}.txt'
            )

    @staticmethod
    def compute_bleu_score(references: List[List[str]], hypotheses: List[List[str]]) -> float:
        if references[0][0] == '<s>':
            references = [r[1:-1] for r in references]

        return corpus_bleu(
            [[r] for r in references],
            hypotheses
        )

    def get_ckpt(self):
        return {
            'id': f'{configs.timestamp}.{self.epoch_idx}',
            'parent_id': self.ckpt_id,
            'epoch_idx': self.epoch_idx,
            'min_validating_ppl': self.min_validating_ppl,
            'model': self.model.state_dict(),
            'optimizer': self.optimizer.state_dict(),
            'lr_scheduler': self.lr_scheduler.state_dict()
        }

    def set_ckpt(self, ckpt_dict):
        self.ckpt_id = ckpt_dict['id']
        self.epoch_idx = ckpt_dict['epoch_idx'] + 1
        self.min_validating_ppl = ckpt_dict['min_validating_ppl']

        model_state_dict = self.model.state_dict()
        model_state_dict.update(
            {
                name: param
                for name, param in ckpt_dict['model'].items()
                if name in model_state_dict
            }
        )

        self.model.load_state_dict(model_state_dict)
        del model_state_dict

        if not configs.uses_new_optimizer:
            self.optimizer.load_state_dict(ckpt_dict['optimizer'])
            self.lr_scheduler.load_state_dict(ckpt_dict['lr_scheduler'])

        del ckpt_dict

        torch.cuda.empty_cache()

    ckpt = property(get_ckpt, set_ckpt)

    def save_ckpt(self):
        ckpt_path = f'{configs.ckpts_dir}/{configs.timestamp}.{self.epoch_idx}.ckpt'
        log(f'saving checkpoint {ckpt_path}')
        torch.save(self.ckpt, f=ckpt_path)
        self.prev_ckpt_path = ckpt_path

    @staticmethod
    def to_timestamp_and_epoch_idx(ckpt_path_):
        date, time, epoch_idx = map(int, re.split(r'[-.]', ckpt_path_[:ckpt_path_.find('.ckpt')]))
        return date, time, epoch_idx

    def load_ckpt(self, ckpt_path=None):
        if not ckpt_path:
            if configs.ckpt_id:
                ckpt_path = f'{configs.ckpts_dir}/{configs.ckpt_id}.ckpt'
            elif configs.loads_best_ckpt:
                ckpt_path = configs.best_ckpt_path
            else:
                ckpt_paths = [path for path in os.listdir(f'{configs.ckpts_dir}/') if path.endswith('.ckpt')]
                ckpt_path = f'{configs.ckpts_dir}/{sorted(ckpt_paths, key=Runner.to_timestamp_and_epoch_idx)[-1]}'

        print(f'loading checkpoint {ckpt_path}')

        self.ckpt = torch.load(ckpt_path)

if __name__ == '__main__':
    runner = Runner()

    if configs.training:
        runner.train()
    elif configs.validating:
        runner.validate()
    else:
        runner.test()


"""
This file handles the details of the loss function during training.

This includes: LossComputeBase and the standard NMTLossCompute, and
               sharded loss compute stuff.
"""
from __future__ import division
import torch
import torch.nn as nn
import torch.nn.functional as F

import onmt
import onmt.inputters as inputters
from onmt.modules.sparse_losses import SparsemaxLoss
from math import isnan

def build_loss_compute(model, tgt_vocab, opt, train=True):
    """
    This returns user-defined LossCompute object, which is used to
    compute loss in train/validate process. You can implement your
    own *LossCompute class, by subclassing LossComputeBase.
    """
    device = torch.device("cuda" if onmt.utils.misc.use_gpu(opt) else "cpu")

    if opt.copy_attn:
        compute = onmt.modules.CopyGeneratorLossCompute(
            model.generator, tgt_vocab, opt.copy_attn_force,
            opt.copy_loss_by_seqlength, focal_gamma=opt.focal_gamma)
    elif opt.model_type=="vector":
        sequential_target = False
        if opt.decoder_type=="vecdif_multi":
            sequential_target=True
        compute = AcosLoss(model.generator, tgt_vocab, model.decoder.hidden_size, device, sequential_target=sequential_target) #model.generator
    else:
        compute = NMTLossCompute(
            model.generator, tgt_vocab,
            label_smoothing=opt.label_smoothing if train else 0.0)
    compute.to(device)

    return compute

class LossComputeBase(nn.Module):
    """
    Class for managing efficient loss computation. Handles
    sharding next step predictions and accumulating mutiple
    loss computations

    Users can implement their own loss computation strategy by making
    subclass of this one.  Users need to implement the _compute_loss()
    and make_shard_state() methods.

    Args:
        generator (:obj:`nn.Module`) :
             module that maps the output of the decoder to a
             distribution over the target vocabulary.
        tgt_vocab (:obj:`Vocab`) :
             torchtext vocab object representing the target output
        normalzation (str): normalize by "sents" or "tokens"
    """

    def __init__(self, generator, tgt_vocab):
        super(LossComputeBase, self).__init__()
        self.generator = generator
        self.tgt_vocab = tgt_vocab
        self.padding_idx = tgt_vocab.stoi[inputters.PAD_WORD]

    def _make_shard_state(self, batch, output, range_, attns=None):
        """
        Make shard state dictionary for shards() to return iterable
        shards for efficient loss computation. Subclass must define
        this method to match its own _compute_loss() interface.
        Args:
            batch: the current batch.
            output: the predict output from the model.
            range_: the range of examples for computing, the whole
                    batch or a trunc of it?
            attns: the attns dictionary returned from the model.
        """
        return NotImplementedError

    def _compute_loss(self, batch, output, target, **kwargs):
        """
        Compute the loss. Subclass must define this method.

        Args:

            batch: the current batch.
            output: the predict output from the model.
            target: the validate target to compare output with.
            **kwargs(optional): additional info for computing loss.
        """
        return NotImplementedError

    def monolithic_compute_loss(self, batch, output, attns):
        """
        Compute the forward loss for the batch.

        Args:
          batch (batch): batch of labeled examples
          output (:obj:`FloatTensor`):
              output of decoder model `[tgt_len x batch x hidden]`
          attns (dict of :obj:`FloatTensor`) :
              dictionary of attention distributions
              `[tgt_len x batch x src_len]`
        Returns:
            :obj:`onmt.utils.Statistics`: loss statistics
        """
        range_ = (0, batch.tgt.size(0))
        shard_state = self._make_shard_state(batch, output, range_, attns)
        to_compare = batch.src[0, :1, :]
        shard_state["to_compare"] = to_compare
        _, batch_stats = self._compute_loss(batch, **shard_state)

        return batch_stats

    def monolithic_compute_loss_multivec(self, batch, output):
        """
        Compute the forward loss for the batch.

        Args:
          batch (batch): batch of labeled examples
          output (:obj:`FloatTensor`):
              output of decoder model `[tgt_len x batch x hidden]`
          attns (dict of :obj:`FloatTensor`) :
              dictionary of attention distributions
              `[tgt_len x batch x src_len]`
        Returns:
            :obj:`onmt.utils.Statistics`: loss statistics
        """
        stats = None
        i = 0
        for o in output:
            range_ = (i, i+1)
            shard_state = self._make_shard_state(batch, o, range_, None)
            to_compare = batch.src[:, i, :] # to compare makes no point in validation.
            shard_state["to_compare"] = to_compare
            _, batch_stats = self._compute_loss(batch, **shard_state)
            if stats is None:
                stats = batch_stats
            else:
                stats.update(batch_stats)
            i+=1

        return stats

    def sharded_compute_loss(self, batch, output, attns,
                             cur_trunc, trunc_size, shard_size,
                             normalization, to_compare=None):
        """Compute the forward loss and backpropagate.  Computation is done
        with shards and optionally truncation for memory efficiency.

        Also supports truncated BPTT for long sequences by taking a
        range in the decoder output sequence to back propagate in.
        Range is from `(cur_trunc, cur_trunc + trunc_size)`.

        Note sharding is an exact efficiency trick to relieve memory
        required for the generation buffers. Truncation is an
        approximate efficiency trick to relieve the memory required
        in the RNN buffers.

        Args:
          batch (batch) : batch of labeled examples
          output (:obj:`FloatTensor`) :
              output of decoder model `[tgt_len x batch x hidden]`
          attns (dict) : dictionary of attention distributions
              `[tgt_len x batch x src_len]`
          cur_trunc (int) : starting position of truncation window
          trunc_size (int) : length of truncation window
          shard_size (int) : maximum number of examples in a shard
          normalization (int) : Loss is divided by this number
          to_compare (vector) - sources used for current prediction - used only in vecdiff

        Returns:
            :obj:`onmt.utils.Statistics`: validation loss statistics

        """
        batch_stats = onmt.utils.Statistics()
        range_ = (cur_trunc, cur_trunc + trunc_size)
        shard_state = self._make_shard_state(batch, output, range_, attns)
        for shard in shards(shard_state, shard_size):
            if to_compare is not None:
                shard["to_compare"]=to_compare
            loss, stats = self._compute_loss(batch, **shard)
            #try:
            loss.div(float(normalization)).backward()
            # except Exception as e:
            #     print("PROBLEM "+str(e))
            batch_stats.update(stats)
        return batch_stats

    def _stats(self, loss, scores, target):
        """
        Args:
            loss (:obj:`FloatTensor`): the loss computed by the loss criterion.
            scores (:obj:`FloatTensor`): a score for each possible output
            target (:obj:`FloatTensor`): true targets

        Returns:
            :obj:`onmt.utils.Statistics` : statistics for this batch.
        """
        pred = scores.max(1)[1]
        non_padding = target.ne(self.padding_idx)
        num_correct = pred.eq(target) \
                          .masked_select(non_padding) \
                          .sum() \
                          .item()
        num_non_padding = non_padding.sum().item()
        return onmt.utils.Statistics(loss.item(), num_non_padding, num_correct)

    def _stats_vec(self, loss, scores, target):
        """
        Args:
            loss (:obj:`FloatTensor`): the loss computed by the loss criterion.
            scores (:obj:`FloatTensor`): a score for each possible output
            target (:obj:`FloatTensor`): true targets

        Returns:
            :obj:`onmt.utils.Statistics` : statistics for this batch.
        """
        # equal = scores.eq(target).sum().item()
        # pred = scores.max(1)[1]
        # non_padding = target.ne(self.padding_idx)
        # num_correct = pred.eq(target) \
        #                   .masked_select(non_padding) \
        #                   .sum() \
        #                   .item()
        # num_non_padding = non_padding.sum().item()
        return onmt.utils.Statistics(loss.item(), 1 ,1 ) # equal, target.size()[1])

    def _bottle(self, _v):
        return _v.view(-1, _v.size(2))

    def _unbottle(self, _v, batch_size):
        return _v.view(-1, batch_size, _v.size(1))

class LabelSmoothingLoss(nn.Module):
    """
    With label smoothing,
    KL-divergence between q_{smoothed ground truth prob.}(w)
    and p_{prob. computed by model}(w) is minimized.
    """
    def __init__(self, label_smoothing, tgt_vocab_size, ignore_index=-100):
        assert 0.0 < label_smoothing <= 1.0
        self.padding_idx = ignore_index
        super(LabelSmoothingLoss, self).__init__()

        smoothing_value = label_smoothing / (tgt_vocab_size - 2)
        one_hot = torch.full((tgt_vocab_size,), smoothing_value)
        one_hot[self.padding_idx] = 0
        self.register_buffer('one_hot', one_hot.unsqueeze(0))

        self.confidence = 1.0 - label_smoothing

    def forward(self, output, target):
        """
        output (FloatTensor): batch_size x n_classes
        target (LongTensor): batch_size
        """
        model_prob = self.one_hot.repeat(target.size(0), 1)
        model_prob.scatter_(1, target.unsqueeze(1), self.confidence)
        model_prob.masked_fill_((target == self.padding_idx).unsqueeze(1), 0)

        return F.kl_div(output, model_prob, reduction='sum')

class AcosLoss(LossComputeBase):
    """
    arcus cosine loss
    """
    def __init__(self, generator, tgt_vocab, output_size, device, sequential_target=False):
        super(AcosLoss, self).__init__(generator, tgt_vocab)
        self.zero_vec = torch.zeros(1,output_size, device=device)
        self.filled_vec = torch.zeros(1, output_size, device=device).fill_(0.0001)
        #self.prev_vec = torch.zeros(1,output_size, device=device)
        self.prev_distance = None # torch.zeros(1, 1, device=device)
        self.sequential_target=sequential_target
        self.lrelu = nn.LeakyReLU(0.01)

    def _compute_loss(self, batch, output, target, to_compare):
        """
        output (FloatTensor): batch_size x n_classes
        target (LongTensor): batch_size
        """
        if self.generator is not None:
            output = torch.squeeze(output, dim=0)
            output = self.generator(output)
        while len(output.size()) < len(target.size()):
            output = output.unsqueeze(0)

        v1 = F.cosine_similarity(output, target, dim=(len(target.size())-1) ) #torch.abs()

        v2 = torch.acos(v1)
        vstat = v2.clone()

        if self.prev_distance is None:
            self.prev_distance = torch.ones_like(v2) *1.5

        if v2.size()[0]> self.prev_distance.size()[0]: # in such case,
            v2 = v2[:self.prev_distance.size()[0]]
        elif v2.size()[0]< self.prev_distance.size()[0]: # in such case,
            self.prev_distance = self.prev_distance[:v2.size()[0]]

        v3 = v2 - self.prev_distance[:v2.size()[0]] # v2/10 + F.relu remove relu ?
        if self.sequential_target:
            optimal_improvement = torch.abs(F.cosine_similarity(to_compare, target, dim=(len(target.size()) - 1)))
            optimal_improvement = torch.acos(optimal_improvement)
            if v2.size()[0] > optimal_improvement.size()[0]:  # in such case,
                v2 = v2[:optimal_improvement.size()[0]]
            elif v2.size()[0] < optimal_improvement.size()[0]:  # in such case,
                optimal_improvement = optimal_improvement[:v2.size()[0]]
            if v2.size()[0] != optimal_improvement.size()[0]:
                print("v2 "+str(v2.size))
                print("optimal_improvement " + str(optimal_improvement.size))
            v3a = v2 - optimal_improvement
            v4 = v3a + F.relu(v3)
        else:
            v4 = v3
        #print(str(v2)+" \n v3="+str(v3)+" \n v3a="+str(v3a)+"   \n v4="+str(v4)+"\n sum= "+str(v4.sum())+" \n\n" )
        self.prev_distance = v2.detach()

        #print("targe " + str(target[0,0:5]) + "   outout= " + str(output[0,0:5]) + " loss = " + str(v2.item())+"  final loss = "+str(v3))
        stats = self._stats_vec(vstat.sum()/vstat.size()[0], output, target)
        return v4.sum(), stats

    def _make_shard_state(self, batch, output, range_, attns=None):
        if self.sequential_target:
            return {
                "output": output,
                "target": batch.tgt[:,range_[0]: range_[1],:].squeeze(1),
            }
        return {
            "output": output,
            "target": batch.tgt[range_[0]: range_[1]],
        }

class NMTLossCompute(LossComputeBase):
    """
    Standard NMT Loss Computation.
    """

    def __init__(self, generator, tgt_vocab, normalization="sents",
                 label_smoothing=0.0):
        super(NMTLossCompute, self).__init__(generator, tgt_vocab)
        self.sparse = not isinstance(generator[1], nn.LogSoftmax)
        self.vector = not isinstance(generator[1], nn.Sigmoid)
        if label_smoothing > 0:
            self.criterion = LabelSmoothingLoss(
                label_smoothing, len(tgt_vocab), ignore_index=self.padding_idx
            )
        elif self.sparse:
            self.criterion = SparsemaxLoss(
                ignore_index=self.padding_idx, size_average=False
            )
        elif self.vector:
            self.criterion = SparsemaxLoss(
                ignore_index=self.padding_idx, size_average=False
            )
        else:
            self.criterion = nn.NLLLoss(
                ignore_index=self.padding_idx, reduction='sum'
            )

    def _make_shard_state(self, batch, output, range_, attns=None):
        return {
            "output": output,
            "target": batch.tgt[range_[0] + 1: range_[1]],
        }

    def _compute_loss(self, batch, output, target):
        bottled_output = self._bottle(output)
        if self.sparse:
            # for sparsemax loss, the loss function operates on the raw output
            # vector, not a probability vector. Hence it's only necessary to
            # apply the first part of the generator here.
            scores = self.generator[0](bottled_output)
        else:
            scores = self.generator(bottled_output)
        gtruth = target.view(-1)

        loss = self.criterion(scores, gtruth)
        stats = self._stats(loss.clone(), scores, gtruth)

        return loss, stats

def filter_shard_state(state, shard_size=None):
    """ ? """
    for k, v in state.items():
        if shard_size is None:
            yield k, v

        if v is not None:
            v_split = []
            if isinstance(v, torch.Tensor):
                for v_chunk in torch.split(v, shard_size):
                    v_chunk = v_chunk.data.clone()
                    v_chunk.requires_grad = v.requires_grad
                    v_split.append(v_chunk)
            yield k, (v, v_split)

def shards(state, shard_size, eval_only=False):
    """
    Args:
        state: A dictionary which corresponds to the output of
               *LossCompute._make_shard_state(). The values for
               those keys are Tensor-like or None.
        shard_size: The maximum size of the shards yielded by the model.
        eval_only: If True, only yield the state, nothing else.
              Otherwise, yield shards.

    Yields:
        Each yielded shard is a dict.

    Side effect:
        After the last shard, this function does back-propagation.
    """
    if eval_only:
        yield filter_shard_state(state)
    else:
        # non_none: the subdict of the state dictionary where the values
        # are not None.
        non_none = dict(filter_shard_state(state, shard_size))

        # Now, the iteration:
        # state is a dictionary of sequences of tensor-like but we
        # want a sequence of dictionaries of tensors.
        # First, unzip the dictionary into a sequence of keys and a
        # sequence of tensor-like sequences.
        keys, values = zip(*((k, [v_chunk for v_chunk in v_split])
                             for k, (_, v_split) in non_none.items()))

        # Now, yield a dictionary for each shard. The keys are always
        # the same. values is a sequence of length #keys where each
        # element is a sequence of length #shards. We want to iterate
        # over the shards, not over the keys: therefore, the values need
        # to be re-zipped by shard and then each shard can be paired
        # with the keys.
        for shard_tensors in zip(*values):
            yield dict(zip(keys, shard_tensors))

        # Assumed backprop'd
        variables = []
        for k, (v, v_split) in non_none.items():
            if isinstance(v, torch.Tensor) and state[k].requires_grad:
                variables.extend(zip(torch.split(state[k], shard_size),
                                     [v_chunk.grad for v_chunk in v_split]))
        inputs, grads = zip(*variables)
        torch.autograd.backward(inputs, grads)


from django.contrib import admin
from home.models import Setting, ContactMessage
class SettingAdmin(admin.ModelAdmin):
    list_display = ['title',  'company', 'update_at', 'status']

class ContactMessageAdmin(admin.ModelAdmin):
    list_display = ['name',  'subject', 'update_at', 'status']
    readonly_fields = ('name', 'subject', 'email', 'message', 'ip')
    list_filter= ['status',]

admin.site.register(Setting, SettingAdmin)
admin.site.register(ContactMessage, ContactMessageAdmin)


"""
How to run:
python find_HSV_pixels.py <image path>
"""

import argparse
import cv2
import os

from guiutils import HSV

def main():
    parser = argparse.ArgumentParser(description='Visualizes the line for hough transform.')
    parser.add_argument('filename')

    args = parser.parse_args()

    img = cv2.imread(args.filename)

    cv2.imshow('input', img)

    edge_finder = HSV(img, 0, 255, 1)

    print ("Edge parameters:")
    print ("Layer: %s" % edge_finder.layer())
    print ("Threshold1: %f" % edge_finder.threshold1())
    print ("Threshold2: %f" % edge_finder.threshold2())

    (head, tail) = os.path.split(args.filename)

    (root, ext) = os.path.splitext(tail)

    smoothed_filename = os.path.join("output_images", root + "-smoothed" + ext)
    edge_filename = os.path.join("output_images", root + "-edges" + ext)

    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


# _*_coding: utf-8_*_

import json
import time

import requests

from public import params
from 测试用例.接口自动化.接口自动化_V1 import lcl_data

BaseUrl = params.BaseUrl
ParaDir = params.ParaFile

headers = {
        'Content-Tpye': 'application/json;charset=utf-8'
    }

def lcl_data_workorder(sheet, row):
    time.sleep(1)
    ordercode = "IT_WO_" + str(time.time())
    print(ordercode)
    dl = lcl_data(sheet, row)
    dl['code'] = ordercode
    # print(dl)
    return dl

def workorder_create(data):
    """工单—创建"""
    url = BaseUrl + '/ime-container/imeWorkOrder/insertWorkOrder.action'
    # print(data)
    try:
        req = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
        return req
    except Exception as e:
        return e

def workorder_modify(data):
    """工单-修改"""
    url = BaseUrl + '/ime-container/imeWorkOrder/modifyWorkOrder.action'
    # print(data)
    try:
        req = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
        return req
    except Exception as e:
        return e

def workorder_refecreaate(data):
    """工单-参照订单生成"""
    url = BaseUrl + '/ime-container/imeWorkOrder/insertWorkOrderByPlanOrder.action'
    # print(data)
    try:
        req = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
        return req
    except Exception as e:
        return e

def workorder_savesort(flgid, wogidlist):
    """工单-编排保存"""
    url = BaseUrl + '/ime-container/imeWorkOrder/saveWorkOrderSort.action?factoryLineGid=' + flgid
    data = {
        "ids": wogidlist,
        "dateStr": "2018-01-01 00:00:00"
    }
    print(url)
    print(data)
    try:
        req = requests.post(url, headers=headers, data=json.dumps(data)).content.decode()
        return req
    except Exception as e:
        return e

def workorder_release(wogidlist):
    """工单-下发"""
    url = BaseUrl + '/ime-container/imeWorkOrder/releaseWorkOrder.action'
    try:
        req = requests.post(url, headers=headers, data=json.dumps(wogidlist)).content.decode()
        return req
    except Exception as e:
        return e

if __name__ == '__main__':
    pass

测试用例/接口自动化/接口自动化_V1/生产执行/工单/workorder_public.py

测试用例.接口自动化.接口自动化_V1.lcl_data


from flask import jsonify, make_response, request, url_for
from app import app
from functools import wraps
from app.models import User, postItem

def post_required(f):
    """
    Decorator to ensure that a valid post id is sent in the url path parameters
    :param f:
    :return:
    """

    @wraps(f)
    def decorated_function(*args, **kwargs):
        post_id_ = request.view_args['post_id']
        try:
            int(post_id_)
        except ValueError:
            return response('failed', 'Provide a valid post Id', 401)
        return f(*args, **kwargs)

    return decorated_function

def response(status, message, status_code):
    """
    Make an http response helper
    :param status: Status message
    :param message: Response Message
    :param status_code: Http response code
    :return:
    """
    return make_response(jsonify({
        'status': status,
        'message': message
    })), status_code

def response_with_post_item(status, item, status_code):
    """
    Http response for response with a post item.
    :param status: Status Message
    :param item: postItem
    :param status_code: Http Status Code
    :return:
    """
    return make_response(jsonify({
        'status': status,
        'item': item.json()
    })), status_code

def response_with_pagination(items, previous, nex, count):
    """
    Get the post items with the result paginated
    :param items: Items within the post
    :param previous: Url to previous page if it exists
    :param nex: Url to next page if it exists
    :param count: Pagination total
    :return: Http Json response
    """
    return make_response(jsonify({
        'status': 'success',
        'previous': previous,
        'next': nex,
        'count': count,
        'items': items
    })), 200

def get_user_post(current_user, post_id):
    """
    Query the user to find and return the post specified by the post Id
    :param post_id: post Id
    :param current_user: User
    :return:
    """
    user_post = User.get_by_id(current_user.id).posts.filter_by(id=post_id).first()
    return user_post

def get_paginated_items(post, post_id, page, q):
    """
    Get the items from the post and then paginate the results.
    Items can also be search when the query parameter is set.
    Construct the previous and next urls.
    :param q: Query parameter
    :param post: post
    :param post_id: post Id
    :param page: Page number
    :return:
    """

    if q:
        pagination = postItem.query.filter(postItem.name.like("%" + q.lower().strip() + "%")) \
            .order_by(postItem.create_at.desc()) \
            .filter_by(post_id=post_id) \
            .paginate(page=page, per_page=app.config['post_AND_ITEMS_PER_PAGE'], error_out=False)
    else:
        pagination = post.items.order_by(postItem.create_at.desc()).paginate(page=page, per_page=app.config[
            'post_AND_ITEMS_PER_PAGE'], error_out=False)

    previous = None
    if pagination.has_prev:
        if q:
            previous = url_for('items.get_items', q=q, post_id=post_id, page=page - 1, _external=True)
        else:
            previous = url_for('items.get_items', post_id=post_id, page=page - 1, _external=True)
    nex = None
    if pagination.has_next:
        if q:
            nex = url_for('items.get_items', q=q, post_id=post_id, page=page + 1, _external=True)
        else:
            nex = url_for('items.get_items', post_id=post_id, page=page + 1, _external=True)
    return pagination.items, nex, pagination, previous


from flask import Flask, render_template, request
import pickle
import numpy as np
model = pickle.load(open('model.pkl', 'rb'))
app = Flask(__name__)
@app.route('/')
def home():
    return render_template('forest.html')

@app.route('/predict', methods = ['POST'])
def result():
    data1 = request.form['a']
    data2 = request.form['b']
    data3 = request.form['c']
    ar = np.array([[data1, data2, data3]])
    p = model.predict(ar)
    return render_template('prediction.html', data=p)
if __name__ == "__main__":
    app.run(debug=True)


import sys
import cv2
import numpy as np
import os
from configparser import ConfigParser
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen, QGuiApplication
from PyQt5.QtCore import QRect, Qt, QTimer, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog, QGridLayout, QLabel, QPushButton, QSlider, QMessageBox

def qtpixmap_to_cvimg(qtpixmap):
    """ transform qtImage into numpy array ( regular image) """
    qimg = qtpixmap.toImage()
    temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth())
    temp_shape += (4,)
    ptr = qimg.bits()
    ptr.setsize(qimg.byteCount())
    result = np.array(ptr, dtype=np.uint8).reshape(temp_shape)
    result = result[..., :3]
    return result

class mainUI(QDialog):
    """ deployment of the user interface """

    def __init__(self):
        """ initialize the parameter and numpy array to save photoes """
        super().__init__()
        self.initUI()

        self.img_regular = np.ndarray(())
        self.img_corp = np.ndarray(())
        self.img_processed = np.ndarray(())
        self.img_threshold = np.ndarray(())
        self.img_overlap = np.ndarray(())

        self.threshold_value = 0

        self.camera = cv2.VideoCapture()
        self.CAM_NUM = 0  # Set Camera num

        self.camera_timer = QtCore.QTimer()
        self.camera_timer.timeout.connect(self.queryFrame)
        self.corp_timer = QtCore.QTimer()
        self.corp_timer.timeout.connect(self.cropImg)
        self.corp_timer.timeout.connect(self.thresImg)
        self.corp_timer.timeout.connect(self.overlapImg)

    def initUI(self):
        """ deifine the component of the user interface """
        # Define Size
        self.setGeometry(50,50,600,500)
        self.setWindowTitle('Load Image')

        # Define Buttum
        self.btnOpen = QPushButton('Open', self)
        self.btnSave = QPushButton('Save', self)
        self.btnRect = QPushButton('Rect', self)
        self.btnCrop = QPushButton('Corp', self)
        self.btnSaveParam = QPushButton('Save Param', self)
        self.btnLoadParam = QPushButton('Load Param', self)
        self.btnQuit = QPushButton('Quit', self)

        # Define Label
        self.label_regulerImg_sign = QLabel("Reguler Image : ")
        self.label_roiImg_sign = QLabel("ROI Image : ")
        self.label_thresImg_sign = QLabel("Threshold Image : ")
        self.label_overlapImg_sign = QLabel("Overlap : ")

        self.label_regularImg = CutImage(self)
        self.label_processedImg = QLabel("Processed img")
        self.label_thresholdImg = QLabel("Threshold img")
        self.label_overlapImg = QLabel("Overlapping img")
        self.label_threshold = QLabel("threshold: 0 ",self)
        self.label_thresholdrate = QLabel("佔比率 : 0 ",self)

        # Define Slider
        self.threshold_slider = QSlider(Qt.Horizontal,self)
        self.threshold_slider.setMinimum(0)
        self.threshold_slider.setMaximum(255)
        self.threshold_slider.valueChanged[int].connect(self.changevalue)

        # Layout
        layout = QGridLayout(self)
        layout.addWidget(self.label_regulerImg_sign, 1, 1, 1, 1)
        layout.addWidget(self.label_regularImg, 2, 1, 2, 3)    # (y,x,yspan,xspan)
        layout.addWidget(self.label_roiImg_sign, 4, 1, 1, 1)
        layout.addWidget(self.label_processedImg, 5, 1, 2, 3)
        layout.addWidget(self.label_thresImg_sign, 1, 4, 1, 1)
        layout.addWidget(self.label_thresholdImg, 2, 4, 2, 3)
        layout.addWidget(self.label_overlapImg_sign, 4, 4, 1, 1)
        layout.addWidget(self.label_overlapImg, 5, 4, 2, 3)

        layout.addWidget(self.label_threshold, 8, 7, 1, 1)
        layout.addWidget(self.label_thresholdrate, 7, 7, 1, 1)

        layout.addWidget(self.btnOpen, 9, 1, 1, 1)
        layout.addWidget(self.btnSave, 9, 2, 1, 1)
        layout.addWidget(self.btnRect, 9, 3, 1, 1)
        layout.addWidget(self.btnCrop, 9, 4, 1, 1)
        layout.addWidget(self.btnSaveParam, 9, 5, 1, 1)
        layout.addWidget(self.btnLoadParam, 9, 6, 1, 1)
        layout.addWidget(self.btnQuit, 9, 7, 1, 1)

        layout.addWidget(self.threshold_slider, 8, 5,1,2)

        # Define the Buttum Function
        self.btnOpen.clicked.connect(self.cameraSlot)
        self.btnSave.clicked.connect(self.saveSlot)
        self.btnRect.clicked.connect(self.rectSlot)
        self.btnCrop.clicked.connect(self.cropSlot)
        self.btnSaveParam.clicked.connect(self.saveParamSlot)
        self.btnLoadParam.clicked.connect(self.loadParamSlot)
        self.btnQuit.clicked.connect(self.close)

    def cameraSlot(self):

        if self.camera_timer.isActive() == False:
            self.openCamera()
        else:
            self.closeCamera()

    def openCamera(self):

        flag = self.camera.open(self.CAM_NUM)

        if flag == False:
            msg = QMessageBox.warning(self, u'Warning', u'Please Check Your Camera Connection',
                                        buttons = QMessageBox.Ok,
                                        defaultButton = QMessageBox.Ok)
        else:
            self.camera_timer.start(50)
            self.btnOpen.setText('Cam Off')

    def closeCamera(self):

        self.camera_timer.stop()
        self.corp_timer.stop()
        self.camera.release()
        self.label_regularImg.clear()
        self.label_processedImg.clear()
        self.label_thresholdImg.clear()
        self.btnOpen.setText('Cam On')

    def queryFrame(self):
        """When Qtimer time out, refresh regular_img """
        ret, self.frame = self.camera.read()
        scale_percent = 80       # percent of original size
        width = int(self.frame.shape[1] * scale_percent / 100)
        height = int(self.frame.shape[0] * scale_percent / 100)
        dim = (width, height)
        show = cv2.resize(self.frame,dim)
#         show = cv2.resize(self.frame,(480,360))
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
        QImg = QImage(show.data, show.shape[1],show.shape[0],QImage.Format_RGB888)
        self.label_regularImg.setPixmap(QPixmap.fromImage(QImg))

    def saveSlot(self):
        """ Save Photo """
        fileName, tmp = QFileDialog.getSaveFileName(self, 'Save Image', 'Image', '*.png *.jpg *.bmp')
        if fileName is '':
            return
        if self.img_processed.size == 1:
            return

        # Calling OpenCV function to save photo
        cv2.imwrite(fileName, self.img_processed)

    def rectSlot(self):
        """ Draw Rectangle on image """

        self.label_regularImg.setCursor(Qt.CrossCursor)
        self.corp_timer.stop()

    def cropSlot(self):

        self.corp_timer.start(30)
        self.label_regularImg.setCursor(Qt.ArrowCursor)
        self.label_processedImg.clear()

    def cropImg(self):
        """ Corp the Image"""
        self.label_processedImg.setPixmap(processed_img)
        self.img_corp = qtpixmap_to_cvimg(processed_img)
        self.img_processed = cv2.cvtColor(self.img_corp, cv2.COLOR_BGR2GRAY)

    def changevalue(self,threshold):
        """ let the label change with the scroll bar """
        sender = self.sender()
        if sender == self.threshold_slider:
            self.threshold_slider.setValue(threshold)
        self.label_threshold.setText('模型敏感度:'+str(threshold))
        self.threshold_value = threshold
        print (self.threshold_value)

    def thresImg(self):
        """Threshold"""
        ret , self.img_threshold = cv2.threshold(self.img_processed,self.threshold_value,255,cv2.THRESH_BINARY)

        height, width = self.img_threshold.shape
        bytesPerline = 1 * width

        # Qimage read image
        self.qImg_threshold = QImage(self.img_threshold.data, width, height, bytesPerline, QImage.Format_Grayscale8)

        # show Qimage
        self.label_thresholdImg.setPixmap(QPixmap.fromImage(self.qImg_threshold))

        # Calculate the threshold value
        rate = PixelRate(self.img_threshold)
        self.label_thresholdrate.setText("佔比率 :"+str(rate.thresholdRate())+"%")

    def overlapImg(self):
        """Overlap Img"""
        mask = self.img_threshold
        mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)

        height, width, depth = mask_rgb.shape
        for i in range(height):
            for j in range(width):
                if mask[i, j] == 255:
                    mask_rgb[i, j] = (255,0,0)

        img = cv2.cvtColor(self.img_corp, cv2.COLOR_BGR2RGB)

        self.img_overlap = cv2.addWeighted(img, 0.8, mask_rgb, 0.2, 0)

        height, width, bp = self.img_overlap.shape
        bytesPerline = 3 * width

        # Qimage read image
        self.qImg_overlap = QImage(self.img_overlap.data, self.img_overlap.shape[1], self.img_overlap.shape[0],bytesPerline, QImage.Format_RGB888)

        # show Qimage
        self.label_overlapImg.setPixmap(QPixmap.fromImage(self.qImg_overlap))

    def saveParamSlot(self):
        config = ConfigParser()

        config['locate'] = {'x0': xywh[0],
                           'y0': xywh[1],
                           'w': xywh[2],
                           'h': xywh[3]}

        config['threshold'] = {'rate': self.threshold_value }

        with open('thres_param.ini', 'w') as configfile:
            config.write(configfile)

        msg = QMessageBox.warning(self, u'Warning', u'File save success!',
                                        buttons = QMessageBox.Ok)

    def loadParamSlot(self):
        filepath = "thres_param.ini"

        if os.path.isfile(filepath):
            msg = QMessageBox.warning(self, u'Warning', u'File exists',
                                        buttons = QMessageBox.Ok)

        else:
            msg = QMessageBox.warning(self, u'Warning', u'File does not exist',
                                        buttons = QMessageBox.Ok)

class PixelRate():
    """Count the threshold rate"""

    def __init__(self, img):
        self.thresh_img = img

    def thresholdPixel(self):
        """Count the pixel which is above the threshold"""
        area = 0
        height, width = self.thresh_img.shape
        for i in range(height):
            for j in range(width):
                if self.thresh_img[i, j] > 180:
                    area += 1
        return area

    def totalPixel(self):
        """Count the total pixel"""
        height, width = self.thresh_img.shape
        return height*width

    def thresholdRate(self):
        """Calculate the Rate of the threshold"""
        Rate = (self.thresholdPixel()/self.totalPixel())*100
        Rate = np.round(Rate,2)
        return Rate

class CutImage(QLabel):
    """ define a class of Label to draw rectangle """
    x0 = 0
    y0 = 0
    x1 = 0
    y1 = 0
    flag = False

    def mousePressEvent(self,event):
        self.flag = True
        self.x0 = event.x()
        self.y0 = event.y()
        self.x1 = 0 ##
        self.y1 = 0 ##
        print("Start : ",self.x0,self.y0) ##

    def mouseReleaseEvent(self,event):
        self.flag = False
        print("End : ",self.x1,self.y1) ##

    def mouseMoveEvent(self,event):
        if self.flag:
            self.x1 = event.x()
            self.y1 = event.y()
            self.update()

    def paintEvent(self, event):
        super().paintEvent(event)
        rect =QRect(self.x0, self.y0, self.x1-self.x0, self.y1-self.y0)

        painter = QPainter(self)
        painter.setPen(QPen(Qt.red,2,Qt.SolidLine))
        painter.drawRect(rect)

        pqscreen  = QGuiApplication.primaryScreen()
        pixmap2 = pqscreen.grabWindow(self.winId(), min(self.x0, self.x1)+1,
                                                    min(self.y0, self.y1)+1,
                                                    abs(self.x1-self.x0)-2,
                                                    abs(self.y1-self.y0)-2)

        global processed_img, xywh
        processed_img = pixmap2
        global xywh
        xywh = [min(self.x0, self.x1)+1, min(self.y0, self.y1)+1, abs(self.x1-self.x0)-2, abs(self.y1-self.y0)-2]

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwindow = mainUI()
    mainwindow.show()
    sys.exit(app.exec_())


"""crop face from image """

import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

img = cv2.imread('cz.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x, y, w, h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    sub_face = gray[y:y + h, x:x + w]

cv2.imwrite("croped_face.jpg", sub_face)
cv2.imshow('img', img)

while 1 :
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        cv2.destroyAllWindows()
        break


# -*- coding: utf-8 -*-
import json
import boto3
from boto3.session import Session
from pprint import pprint

STREAM_NAME = 'terraform-kinesis-test'
PARTITION_KEY = 'mp4'
IMAGE_PATH = 'test.mp4'

session = Session(region_name='us-west-2')
kinesis = session.client('kinesis')

response = kinesis.describe_stream(StreamName=STREAM_NAME)

file_object = open(IMAGE_PATH, 'rb')
try:
    while True:
        # 1MB = 1kB * 1000
        chunk = file_object.read(1000 * 1000)
        if not chunk:
            break
        response = kinesis.put_record(
            StreamName=STREAM_NAME,
            Data=chunk,
            PartitionKey=PARTITION_KEY,
            # ExplicitHashKey='string',
            # SequenceNumberForOrdering='string'
        )
finally:
    file_object.close()


#!/usr/bin/python2
import sys
import multiprocessing
import logging
import os
import itertools
import math
#import gmpy2
import copy
import collections
import string
import scipy.stats as stats

def solve(casedata):
    """ Solve case """
    (naomi, ken, N) = casedata
    warp = 0
    dwarp = 0
    i = 0
    j = 0
    while True:
        if ken[i] > naomi[j]:
            i += 1
            j += 1
        else:
            warp += 1
            i += 1
        if i == N or j == N:
            break
    i = N-1
    j = N-1
    while True:
        if ken[i] > naomi[j]:
            i -= 1
        else:
            i -= 1
            j -= 1
            dwarp += 1
        if i < 0 or j < 0:
            break

    return "%d %d" % (dwarp, warp)

def parse():
    """ Returns a list of N lists containing imput data for each case """
    t = int(sys.stdin.readline())
    cases = list()
    for case in range(t):
        N = int(sys.stdin.readline())
        naomi = sorted(map(float, sys.stdin.readline().split()))
        ken = sorted(map(float, sys.stdin.readline().split()))
        casedata = [naomi, ken, N]
        cases.append(casedata)
    return cases

if __name__ == '__main__':
    cases = parse()
    #p = multiprocessing.Pool(multiprocessing.cpu_count())
    #results = p.map(solve, cases)
    #for case, result in enumerate(results):
    #    print('Case #%d: %s' % (case + 1, result))
    #    sys.stdout.flush()

    #for case, data in enumerate(cases):
    #    result = solve(data)
    #    print('Case #%d: %s' % (case + 1, result))
    #    sys.stdout.flush()

    p = multiprocessing.Pool(multiprocessing.cpu_count())
    resultobjs = [p.apply_async(solve, [case]) for case in cases]
    for case, resultobj in enumerate(resultobjs):
        print('Case #%d: %s' % (case + 1, resultobj.get()))
    #    sys.stdout.flush()


#Input the information into SQl or SAS
import sqlite3

conn = sqlite3.connect('mrt21.sqlite')
cur = conn.cursor()

cur.executescript('''
DROP TABLE IF EXISTS name;
DROP TABLE IF EXISTS gender;

CREATE TABLE User (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name   TEXT UNIQUE,
    gender TEXT UNIQUE
);

CREATE TABLE Topic (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    gender  TEXT UNIQUE
)
''')

fname = input('Enter file name: ')
if (len(fname) < 1): fname = 'C:/Users/Solly/Desktop/data-science/July2.xlsx'
fh = open(fname)

gender = str(rows)
name = str(row)

#print(gender,name)

cur.execute('''INSERT OR IGNORE INTO User (name, gender)
        VALUES ( ?, ? )''', ( name, gender ) )
cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
user_id = cur.fetchone()

cur.execute('''INSERT OR IGNORE INTO Topic (gender)
    VALUES ( ? )''', ( gender, ) )
cur.execute('SELECT id FROM Topic WHERE gender = ? ', (gender, ))
crisis = cur.fetchone()[0]

sqlstr = '''SELECT User.name, user.gender
    FROM name JOIN gender
    ORDER BY User.name LIMIT 3'''
print(sqlstr)

#for rower in cur.execute(sqlstr):
 #   print(str(rower[0]),str(rower[1]),str(rower[2]),str(rower[3]))

conn.commit()

cur.close()
#C:/Users/Solly/Desktop/py4e/mbox.txt


from direct.showbase.ShowBase import ShowBase
from panda3d.core import ExecutionEnvironment

from p3dopenvr.p3dopenvr import P3DOpenVR

import openvr
import os

class MinimalOpenVR(P3DOpenVR):
    def __init__(self):
        P3DOpenVR.__init__(self)
        self.left_hand = None
        self.right_hand = None

    def init_action(self):
        main_dir = ExecutionEnvironment.getEnvironmentVariable("MAIN_DIR")
        filename = os.path.join(main_dir, "demo_actions.json")
        self.load_action_manifest(filename, "/actions/demo")
        self.action_haptic_left = self.vr_input.getActionHandle('/actions/demo/out/Haptic_Left')
        self.source_left = self.vr_input.getInputSourceHandle('/user/hand/left')
        self.action_pose_left = self.vr_input.getActionHandle('/actions/demo/in/Hand_Left')
        self.action_haptic_right = self.vr_input.getActionHandle('/actions/demo/out/Haptic_Right')
        self.source_right = self.vr_input.getInputSourceHandle('/user/hand/right')
        self.action_pose_right = self.vr_input.getActionHandle('/actions/demo/in/Hand_Right')
        self.action_left_trigger = self.vr_input.getActionHandle('/actions/demo/in/left_trigger')
        self.action_right_trigger = self.vr_input.getActionHandle('/actions/demo/in/right_trigger')

    def update_action(self):
        left_trigger_state, device = self.get_digital_action_rising_edge(self.action_left_trigger)
        if left_trigger_state:
            print("LEFT")
            self.vr_input.triggerHapticVibrationAction(self.action_haptic_left, 0, 1, 4, 1, openvr.k_ulInvalidInputValueHandle)
        right_trigger_state, device = self.get_digital_action_rising_edge(self.action_right_trigger)
        if right_trigger_state:
            print("RIGHT")
            self.vr_input.triggerHapticVibrationAction(self.action_haptic_right, 0, 1, 4, 1, openvr.k_ulInvalidInputValueHandle)
        left_pose = self.get_action_pose(self.action_pose_left)
        if  left_pose.pose.bPoseIsValid:
            left_matrix = self.get_pose_modelview(left_pose.pose)
            if self.left_hand is None:
                self.left_hand = self.tracking_space.attach_new_node('left-hand')
                model = loader.loadModel("box")
                model.reparent_to(self.left_hand)
                model.set_scale(0.1)
            self.left_hand.show()
            self.left_hand.set_mat(left_matrix)
        else:
            if self.left_hand is not None:
                self.left_hand.hide()
        right_pose = self.get_action_pose(self.action_pose_right)
        if  right_pose.pose.bPoseIsValid:
            right_matrix = self.get_pose_modelview(right_pose.pose)
            if self.right_hand is None:
                self.right_hand = self.tracking_space.attach_new_node('right-hand')
                model = loader.loadModel("box")
                model.reparent_to(self.right_hand)
                model.set_scale(0.1)
            self.right_hand.show()
            self.right_hand.set_mat(right_matrix)
        else:
            if self.right_hand is not None:
                self.right_hand.hide()

base = ShowBase()
base.setFrameRateMeter(True)

myvr = MinimalOpenVR()
myvr.init()

model = loader.loadModel("panda")
model.reparentTo(render)
model.setPos(0, 10, -5)

base.accept('d', myvr.list_devices)
base.accept('b', base.bufferViewer.toggleEnable)

base.run()


# The purpose of this code is to transform
import json
import re
import string

with open('lyincomey_18_04_12_11_30_02.txt') as json_data:
    string = json_data.read()
    tweets = json.loads(string)

# A means to loop through all keys in a dictionary
def loopOverValue (key, possibleDictOrList):
    if isinstance(possibleDictOrList, list):

        print('\n' + key + ' is list with length: ' + str(len(possibleDictOrList)))
        for x in range(len(possibleDictOrList)):
            loopOverValue(key, possibleDictOrList[x])

    elif isinstance(possibleDictOrList, dict):

        print('\n' + key + ' is dict with length: ' + str(len(possibleDictOrList)))
        for k, v in possibleDictOrList.items():
            loopOverValue(k, v)
    else:

        print(key + ': ' + str(possibleDictOrList))

for idx, status in enumerate(tweets['statuses']):

    if idx == 0:
        for k1, v1 in status.items():
            loopOverValue(k1, v1)

# 	# if idx != 45:
# 	# 	continue
# 	####### REMOVE EMOJIS ######
#  	# Emojis need to be converted to a description for accessibility purposes
#  	# Without the lookup table, I am simply removing them all together.
#  	tweetText = status['text']
#  	userDescription = status['user']['description']
#  	 # Remove emojis using regular expressions
#  	tweetText = emojis.sub(r'', tweetText)
#  	userDescription = emojis.sub(r'', userDescription)

# 	tweetList[idx]['MSR']['text'] = tweetText
# 	tweetList[idx]['text'] = tweetText
# 	tweetList[idx]['user']['entities']['description'] = userDescription

#  	if 'retweeted_status' in status:
#  		retweetText = status["retweeted_status"]["text"]
#  		retweetUserDescription = status['retweeted_status']['user']['description']

#  		retweetText = emojis.sub(r'',retweetText)
#  		retweetUserDescription = emojis.sub(r'',retweetUserDescription)
#  		tweetList[idx]["retweeted_status"]["text"] = retweetText
# 		tweetList[idx]['retweeted_status']['user']['description'] = retweetUserDescription

# with open('dataNoEmojis.json', 'w') as f:
# 	json.dump(tweetList, f, sort_keys=True, indent=4, separators=(',', ': '))


# encoding: utf-8
import psycopg2
import psycopg2.extras
import pandas as pd
import numpy as np
import json
from base64 import b64decode
from . import fails
from .files import File
from .states import State

class Data(object):
    def __init__(self, args, config, **kwargs):
        self.RuleNotFound = fails.RuleNotFound
        self.ColumnNotFound = fails.ColumnNotFound
        self.FailDataFound = fails.FailDataFound
        self.np = np
        self.pd = pd
        self.args = args
        self.config = config
        self.action = self.args.action
        self.state = State(self)
        self.gen_json(self.args.load)
        self.read_csv(self.file.file_path)
        self.init_ibge()

    @property
    def connection(self):
        """
        database – the database name (only as keyword argument)
        user – user name used to authenticate
        password – password used to authenticate
        host – database host address (defaults to UNIX socket if not provided)
        port – connection port number (defaults to 5432 if not provided)
        """
        try:
            return self._connection
        except AttributeError:
            self._connection = psycopg2.connect(**self.config.database)
            return self._connection

    @property
    def id_register(self):
        return self.state.id_register

    def gen_json(self, load):
        sql = "SELECT fn_gera_json('{0}')".format(load)
        with self.connection as conn:
            with conn.cursor() as curs:
                curs.execute(sql)
                raw = curs.fetchone()[0]
                self.json = json.loads(raw)['arquivo']
                return self.json

    def read_csv(self, file):
        try:
            self.content = pd.read_csv(file, delimiter=str(';'), dtype=np.object, encoding=self.file.encoding)
            return self.content
        except Exception as e:
            raise self.FailDataFound(e)

    @property
    def file(self):
        if hasattr(self, '_file'):
            return self._file
        else:
            self._file = File(self.args.file)
            return self._file

    @property
    def id_file(self):
        if hasattr(self, '_id_file'):
            return self._id_file
        else:
            sql = """
                    SELECT * FROM iniciativa.tbarquivo_acao
                        WHERE
                    cod_arquivo='{0}'
                  """.format(self.args.load)
            with self.connection as conn:
                with conn.cursor() as curs:
                    curs.execute(sql)
                    self._id_file = curs.fetchone()[0]
                    return self._id_file

    @property
    def origin(self):
        return 1

    @property
    def file_columns(self):
        result = [i.lower().strip() for i in self.content.columns.values.tolist()]
        return result

    @property
    def json_columns(self):
        return [value['logico'].lower().strip() for key, value in self.json['colunas'].items()]

    @property
    def fisic_columns(self):
        result = [self.get_fisic_column(i) for i in self.file_columns]
        return result

    @property
    def dest_table(self):
        return self.json['tabela']

    def is_base64(self, value):
        try:
            result = b64decode(value)
            return result.decode()
        except:
            return False

    def get_rules(self, column):
        for key, value in self.json['colunas'].items():
            if value['logico'].lower().strip() == column.lower().strip():
                rules = value['dominio']
                result = self.is_base64(rules['expressao'])
                if result:
                    rules['expressao'] = result
                else:
                    pass
                return value['dominio']
            else:
                pass
        raise self.RuleNotFound('Regra não existe')

    def get_column(self, column):
        for key, value in self.json['colunas'].items():
            if value['logico'].lower().strip() == column.lower().strip():
                return value
            else:
                pass
        raise self.ColumnNotFound('Coluna não existe')

    def get_fisic_column(self, column):
        return self.get_column(column)['fisico']

    def add_column(self, column, cells):
        self.content.loc[:, column] = cells

    def init_ibge(self):
        sql = "SELECT cod_municipio cod7, cod_municipio6 cod6 FROM geo.tbmunicipio"
        if not hasattr(self, 'ibge7'):
            with self.connection as conn:
                with conn.cursor() as curs:
                    curs.execute(sql)
                    ibge_list = curs.fetchall()
                    self.ibge7 = [row[0] for row in ibge_list]
                    self.ibge6 = [row[1] for row in ibge_list]

    def search_ibge(self, item):
        method = len(item)
        try:
            if method == 6:
                if int(item) in self.ibge6:
                    return True
                else:
                    return False
            elif method == 7:
                if int(item) in self.ibge7:
                    return True
                else:
                    return False
            else:
                return False
        except ValueError:
            return False

    @property
    def project_name(self):
        sql = """
                SELECT a.nom_acao || ' - '|| ac.nom_rotulo_arquivo nome
                FROM iniciativa.tbacao a, iniciativa.tbarquivo_acao ac
                WHERE ac.cod_arquivo = '{0}'
                and a.chv_acao = ac.chv_acao
            """.format(self.args.load)
        with self.connection as conn:
            with conn.cursor() as curs:
                curs.execute(sql)
                return curs.fetchone()[0]

    @property
    def id_user(self):
        if hasattr(self, '_id_user'):
            return self._id_user
        else:
            sql = """
                    SELECT chv_usuario
                        FROM
                    acesso.tbusuario WHERE nom_identificacao = '{0}';
                  """.format(self.args.user_id)
            with self.connection as conn:
                with conn.cursor() as curs:
                    curs.execute(sql)
                    return curs.fetchone()[0]

    @property
    def user_email(self):
        sql = """
                SELECT nom_contato from acesso.tbcontato c
                WHERE c.chv_usuario='{0}'
                AND chv_tipo_contato=3
             """.format(self.id_user)
        with self.connection as conn:
            with conn.cursor() as curs:
                curs.execute(sql)
                return curs.fetchone()[0]


import math
import cadquery as cq

from paramak import RotateStraightShape
from paramak.utils import rotate, intersect_solid, coefficients_of_line_from_points

class PoloidalSegments(RotateStraightShape):
    """Creates a ring of wedges from a central point. When provided with a shape
    to_segment the shape will be segmented by the wedges. This is useful for
    segmenting geometry into equal poloidal angles. Intended to segment the
    firstwall geometry for using in neutron wall loading simulations.

    Args:
        shape_to_segment (paramak.Shape): the Shape to segment, if None then
            the segmenting solids will be returned
        max_distance_from_center (float): the maximum distance from the center
            point outwards (cm).
        center_point (tuple of floats): the center of the segmentation wedges
            (x,z) values (cm).
        number_of_segments (int): the number of equal angles segments in 360
            degrees.

    Keyword Args:
        name (str): the legend name used when exporting a html graph of the
            shape.
        color (sequences of 3 or 4 floats each in the range 0-1): the color to
            use when exporting as html graphs or png images.
        material_tag (str): The material name to use when exporting the
            neutronics description.
        stp_filename (str): The filename used when saving stp files as part of a
            reactor.
        azimuth_placement_angle (float or iterable of floats): The angle or
            angles to use when rotating the shape on the azimuthal axis.
        rotation_angle (float): The rotation angle to use when revolving the
            solid (degrees).
        workplane (str): The orientation of the CadQuery workplane. Options are
            XY, YZ or XZ.
        intersect (CadQuery object): An optional CadQuery object to perform a
            boolean intersect with this object.
        cut (CadQuery object): An optional CadQuery object to perform a boolean
            cut with this object.
        union (CadQuery object): An optional CadQuery object to perform a
            boolean union with this object.
        tet_mesh (str): Insert description.
        physical_groups (type): Insert description.

    Returns:
        a paramak shape object: A shape object that has generic functionality
        with points determined by the find_points() method. A CadQuery solid of
        the shape can be called via shape.solid.
    """

    def __init__(
        self,
        center_point,
        shape_to_segment=None,
        number_of_segments=10,
        max_distance_from_center=1000,
        rotation_angle=360,
        stp_filename="PoloidalSegmenter.stp",
        stl_filename="PoloidalSegmenter.stl",
        color=(0.5, 0.5, 0.5),
        azimuth_placement_angle=0,
        name="poloidal_segmenter",
        material_tag="poloidal_segmenter_mat",
        **kwargs
    ):

        default_dict = {
            "points": None,
            "workplane": "XZ",
            "solid": None,
            "intersect": None,
            "cut": None,
            "union": None,
            "tet_mesh": None,
            "physical_groups": None,
        }

        for arg in kwargs:
            if arg in default_dict:
                default_dict[arg] = kwargs[arg]

        super().__init__(
            name=name,
            color=color,
            material_tag=material_tag,
            stp_filename=stp_filename,
            stl_filename=stl_filename,
            azimuth_placement_angle=azimuth_placement_angle,
            rotation_angle=rotation_angle,
            hash_value=None,
            **default_dict
        )

        self.center_point = center_point
        self.shape_to_segment = shape_to_segment
        self.number_of_segments = number_of_segments
        self.max_distance_from_center = max_distance_from_center

    @property
    def number_of_segments(self):
        return self._number_of_segments

    @number_of_segments.setter
    def number_of_segments(self, value):
        if isinstance(value, int) is False:
            raise ValueError(
                "PoloidalSegmenter.number_of_segments must be an int.")
        if value < 1:
            raise ValueError(
                "PoloidalSegmenter.number_of_segments must be a minimum of 1.")
        self._number_of_segments = value

    @property
    def shape_to_segment(self):
        return self._shape_to_segment

    @shape_to_segment.setter
    def shape_to_segment(self, value):
        self._shape_to_segment = value

    @property
    def center_point(self):
        return self._center_point

    @center_point.setter
    def center_point(self, center_point):
        self._center_point = center_point

    @property
    def max_distance_from_center(self):
        return self._max_distance_from_center

    @max_distance_from_center.setter
    def max_distance_from_center(self, value):
        self._max_distance_from_center = value

    @property
    def solid(self):
        if self.get_hash() != self.hash_value:
            self.create_solid()
        return self._solid

    @solid.setter
    def solid(self, value):
        self._solid = value

    def find_points(self):
        """Finds the XZ points joined by straight connections that describe the 2D
        profile of the poloidal segmentation shape."""

        angle_per_segment = 360. / self.number_of_segments

        points = []

        current_angle = 0

        outer_point = (
            self.center_point[0] +
            self.max_distance_from_center,
            self.center_point[1])
        for i in range(self.number_of_segments):

            points.append(self.center_point)

            outer_point_1 = rotate(
                self.center_point,
                outer_point,
                math.radians(current_angle))
            outer_point_2 = rotate(
                self.center_point, outer_point, math.radians(
                    current_angle + angle_per_segment))

            if outer_point_1[0] < 0:
                m, c = coefficients_of_line_from_points(
                    outer_point_1, self.center_point)
                points.append((0, c))
            else:
                points.append(outer_point_1)

            if outer_point_2[0] < 0:
                m, c = coefficients_of_line_from_points(
                    outer_point_2, self.center_point)
                points.append((0, c))
            else:
                points.append(outer_point_2)

            current_angle = current_angle + angle_per_segment

        self.points = points

    def create_solid(self):
        """Creates a 3d solid using points with straight connections
           edges, azimuth_placement_angle and rotation angle.

           individual solids in the compound can be accessed using .Solids()[i] where i is an int

           Returns:
              A CadQuery solid: A 3D solid volume
        """

        iter_points = iter(self.points)
        triangle_wedges = []
        for p1, p2, p3 in zip(iter_points, iter_points, iter_points):

            solid = (
                cq.Workplane(self.workplane)
                .polyline([p1, p2, p3])
                .close()
                .revolve(self.rotation_angle)
            )
            triangle_wedges.append(solid)

        if self.shape_to_segment is None:

            compound = cq.Compound.makeCompound(
                [a.val() for a in triangle_wedges]
            )

        else:

            intersected_solids = []
            for segment in triangle_wedges:
                overlap = intersect_solid(segment, self.shape_to_segment)
                intersected_solids.append(overlap)

            compound = cq.Compound.makeCompound(
                [a.val() for a in intersected_solids]
            )

        self.solid = compound

        self.hash_value = self.get_hash()

        return compound


from typing import (
    Any,
    Dict,
    List,
    Optional,
)

import requests

from easypost.constant import TIMEOUT
from easypost.easypost_object import convert_to_easypost_object
from easypost.error import Error
from easypost.requestor import (
    RequestMethod,
    Requestor,
)

class ReferralCustomer:
    @staticmethod
    def create(api_key: Optional[str] = None, **params) -> Dict[str, Any]:
        """Create a referral user.

        This function requires the Partner User's API key.
        """
        requestor = Requestor(local_api_key=api_key)
        new_params = {"user": params}
        response, api_key = requestor.request(
            method=RequestMethod.POST,
            url="/referral_customers",
            params=new_params,
        )
        return convert_to_easypost_object(response=response, api_key=api_key)

    @staticmethod
    def update_email(email, user_id, api_key: Optional[str] = None) -> None:
        """Update a referral user.

        This function requires the Partner User's API key.
        """
        requestor = Requestor(local_api_key=api_key)
        url = f"/referral_customers/{user_id}"
        wrapped_params = {
            "user": {
                "email": email,
            }
        }
        _, _ = requestor.request(
            method=RequestMethod.PUT,
            url=url,
            params=wrapped_params,
        )

    @staticmethod
    def all(api_key: Optional[str] = None, **params) -> List:
        """Retrieve a list of referral users.

        This function requires the Partner User's API key.
        """
        requestor = Requestor(local_api_key=api_key)
        response, api_key = requestor.request(
            method=RequestMethod.GET,
            url="/referral_customers",
            params=params,
        )
        return convert_to_easypost_object(response=response, api_key=api_key)

    @classmethod
    def get_next_page(
        cls,
        referrals: Dict[str, Any],
        page_size: int,
        api_key: Optional[str] = None,
    ) -> List["ReferralCustomer"]:
        """Retrieve next page of a referral customers."""
        requestor = Requestor(local_api_key=api_key)
        url = "/referral_customers"
        referral_array = referrals["referral_customers"]

        if referral_array is None or len(referral_array) == 0 or not referrals.get("has_more"):
            raise Error(message="There are no more pages to retrieve.")

        params = {
            "before_id": referral_array[-1].id,
            "page_size": page_size,
        }

        response, api_key = requestor.request(method=RequestMethod.GET, url=url, params=params)
        if response is None or len(response["referral_customers"]) == 0 or not response["has_more"]:
            raise Error(message="There are no more pages to retrieve.")

        return convert_to_easypost_object(response=response, api_key=api_key)

    @staticmethod
    def add_credit_card(
        referral_api_key: str,
        number: str,
        expiration_month: int,
        expiration_year: int,
        cvc: str,
        priority: str = "primary",
    ) -> Dict[str, Any]:
        """Add credit card to a referral customer.

        This function requires the ReferralCustomer User's API key.
        """
        easypost_stripe_api_key = ReferralCustomer._retrieve_easypost_stripe_api_key()

        try:
            stripe_token = ReferralCustomer._create_stripe_token(
                number,
                expiration_month,
                expiration_year,
                cvc,
                easypost_stripe_api_key,
            )
        except Exception:
            raise Error(message="Could not send card details to Stripe, please try again later")

        response = ReferralCustomer._create_easypost_credit_card(
            referral_api_key,
            stripe_token.get("id", ""),
            priority=priority,
        )
        return convert_to_easypost_object(response)

    @staticmethod
    def _retrieve_easypost_stripe_api_key() -> str:
        """Retrieve EasyPost's Stripe public API key."""
        requestor = Requestor()
        public_key, _ = requestor.request(
            method=RequestMethod.GET,
            url="/partners/stripe_public_key",
        )
        return public_key.get("public_key", "")

    @staticmethod
    def _create_stripe_token(
        number: str,
        expiration_month: int,
        expiration_year: int,
        cvc: str,
        easypost_stripe_key: str,
    ) -> Dict[str, Any]:
        """Get credit card token from Stripe."""
        headers = {
            # This Stripe endpoint only accepts URL form encoded bodies
            "Content-type": "application/x-www-form-urlencoded",
        }

        credit_card_dict = {
            "card": {
                "number": number,
                "exp_month": expiration_month,
                "exp_year": expiration_year,
                "cvc": cvc,
            }
        }

        form_encoded_params = Requestor.form_encode_params(credit_card_dict)
        url = "https://api.stripe.com/v1/tokens"

        stripe_response = requests.post(
            url,
            params=form_encoded_params,
            headers=headers,
            auth=requests.auth.HTTPBasicAuth(easypost_stripe_key, ""),
            timeout=TIMEOUT,
        )
        return stripe_response.json()

    @staticmethod
    def _create_easypost_credit_card(
        referral_api_key: str,
        stripe_object_id: str,
        priority: str = "primary",
    ) -> Dict[str, Any]:
        """Submit Stripe credit card token to EasyPost."""
        requestor = Requestor(local_api_key=referral_api_key)

        params = {
            "credit_card": {
                "stripe_object_id": stripe_object_id,
                "priority": priority,
            }
        }

        response, _ = requestor.request(
            method=RequestMethod.POST,
            params=params,
            url="/credit_cards",
        )
        return response


#!/usr/bin/env python3
"""
Author : schackartk
Date   : 2019-02-22
Purpose: Pull the first line of each poem within a directory
"""

import argparse
import sys
import os

# --------------------------------------------------
def get_args():
    """get command-line arguments"""
    parser = argparse.ArgumentParser(
        description='Get the first lines',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument(
        'positional', metavar='DIR', nargs='+', help='A positional argument')

    parser.add_argument(
        '-w',
        '--width',
        type=int,
        metavar='int',
        help='Output line width',
        default=50)

    return parser.parse_args()

# --------------------------------------------------
def warn(msg):
    """Print a message to STDERR"""
    print(msg, file=sys.stderr)

# --------------------------------------------------
def die(msg='Something bad happened'):
    """warn() and exit with error"""
    warn(msg)
    sys.exit(1)

# --------------------------------------------------
def main():
    """Make a jazz noise here"""
    args = get_args()
    dirs = args.positional
    width = args.width

    out = {}
    for dir_n in dirs:
        if not os.path.isdir(dir_n):
            print('"{}" is not a directory'.format(dir_n), file=sys.stderr)
        else:
            print(dir_n)
            files = os.listdir(dir_n)
            for file_n in files:
                with open('{}/{}'.format(dir_n,file_n)) as f:
                    out[f.readline().strip()] = file_n
            for items in sorted (out):
                num_dots = width - len(items) - len(out[items])
                dots = '.' * num_dots
                print('{} {} {}'.format(items,dots,out[items]))
            out.clear()

# --------------------------------------------------
if __name__ == '__main__':
    main()