# -*- coding: utf-8 -*-

u"""编译阶段"""

import subprocess
import logging
import glob
import os
import sys
import string
import codecs

from buildexp import BuildException
import butil

class CompilePhase(object):

    def __init__(self, build_prop):
        self._logger = logging.getLogger(__name__)
        self._build_prop = build_prop
        self._back_dir = os.path.join(self._build_prop.path.dist_dir, "bak")

        self._tasks = []
        self._determine_task()

    def _determine_task(self):
        build_type = self._build_prop.share.build_type

        if "debug" in build_type:
            self._tasks.append(DebugCompileTask(self._build_prop))

        if "release" in build_type:
            self._tasks.append(ReleaseCompileTask(self._build_prop))

    def compile_(self):
        u"""编译阶段"""
        self._init_dist_dir()
        self._init_backup_dir()
        self._generate_string_translator()
        self._generate_qm()

        if self._build_prop.package.inject_version:
            self._backup_version_relative_files()
            try:
                self._generate_version_relative_files()
                self._execute_all_task()
            except:
                raise
            finally:
                self._restore_version_relative_files()
        else:
            self._execute_all_task()

    def _init_dist_dir(self):
        u"""初始化dist目录。这个目录用于存放构建中生成中间文件（除了编译所生成的）
        这个目录需要在编译开始前创建，不能早也不能晚
        """
        butil.mkdir(self._build_prop.path.dist_dir)

    def _init_backup_dir(self):
        butil.mkdir(self._back_dir)

    def _generate_string_translator(self):
        u"""生成与国际化相关的代码文件"""
        returncode = subprocess.call(["python",
            "script/xmlstringtranslator.py",
            "bin/release/layout",
            "bin/release/addons/addonsmanager/resource",
            "src/yyapp/xml_string_translator.h",
            "src/yyapp/yyapp.pro",
            "HEADERS += xml_string_translator.h"],
            cwd=self._build_prop.path.app_dir)
        if returncode != 0:
            raise BuildException("String translatation failed")

    def _generate_qm(self):
        process = subprocess.Popen([
            "lrelease",
            "-compress",
            "../framework/src/duifw/duifw_zh.ts",
            "src/yycommon/yycommon_zh.ts",
            "src/yyapp/yyapp_zh.ts",
            "src/yymainframe/yymainframe_zh.ts",
            "src/yychannel/yychannel_zh.ts",
            "src/yyim/yyim_zh.ts",
            "src/yyhistroymsg/yymsg_zh.ts",
            "src/yygroup/yygroup_zh.ts",
            "src/yggroup/yggroup_zh.ts",
            "src/yyplugin/yyplugin_zh.ts",
            "src/addonfw/addonfw_zh.ts",
            "-qm",
            "src/yyapp/yyapp_zh.qm",
            ],
            cwd=self._build_prop.path.app_dir,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        process.wait()

        if process.returncode != 0:
            output_normalize = process.communicate()[1].replace("\r\n", "\n")
            self._logger.debug("\n%s" % output_normalize)
            sys.stderr.write(output_normalize)
            raise BuildException("Generate .qm file failed")

        butil.copy_file_to_dir("app/bin/release/lang", "app/src/yyapp",
            ["yyapp_zh.qm"])

    def _backup_version_relative_files(self):
        self._backup_productinfo()
        self._backup_protocol_module_version()

    def _backup_productinfo(self):
        butil.copy_file_to_dir(self._back_dir,
            "version",
            ["productinfo.cpp"])

    def _backup_protocol_module_version(self):
        butil.copy_file_to_dir(self._back_dir,
            "mw/protocol4/protocol",
            ["ProtocolModuleVersion.h"])

    def _generate_version_relative_files(self):
        self._generate_productinfo()
        self._generate_protocol_module_version()

    def _generate_productinfo(self):
        file_path = "version/productinfo.cpp"
        file_ = codecs.open(file_path, mode="r", encoding="GBK")

        template = string.Template(file_.read())
        result = template.substitute(self._build_prop.version.about.__dict__)
        file_.close()

        file_ = codecs.open(file_path, mode="w", encoding="GBK")
        file_.write(result)
        file_.close()

        butil.copy_file(file_path,
            os.path.join(self._back_dir, "%s.modify" % os.path.basename(file_path)))

    def _generate_protocol_module_version(self):
        file_path = "mw/protocol4/protocol/ProtocolModuleVersion.h"

        rev_string = self._build_prop.version.about.last_commit_rev
        if rev_string != '[rev]' and len(rev_string) > 1:
            rev = int(rev_string[1:])
        else:
            rev = 0

        if rev != 0:
            lines = []
            with codecs.open(file_path, mode="r", encoding="GBK") as f_read:
                lines = f_read.readlines()
                for index, line in enumerate(lines):
                    if line.startswith('#define REVISION_VERSION'):
                        lines[index] = '#define REVISION_VERSION %d\r\n' % rev
            with codecs.open(file_path, mode="w", encoding="GBK") as f_write:
                f_write.write(''.join(lines))

        butil.copy_file(file_path,
            os.path.join(self._back_dir, "%s.modify" % os.path.basename(file_path)))

    def _execute_all_task(self):
        for task in self._tasks:
            task.execute()

    def _restore_version_relative_files(self):
        self._restore_productinfo()
        self._restore_protocol_module_version()

    def _restore_productinfo(self):
        butil.copy_file_to_dir("version",
            self._back_dir,
            ["productinfo.cpp"])

    def _restore_protocol_module_version(self):
        butil.copy_file_to_dir("mw/protocol4/protocol",
            self._back_dir,
            ["ProtocolModuleVersion.h"])

class AbstractCompileTask(object):
    u"""提供编译任务的模板方法"""

    def __init__(self, build_prop):
        self._logger = logging.getLogger(__name__)
        self._build_prop = build_prop
        self._projects = [
            butil.Project("misc",
                "yymisc.sln",
                os.path.join(self._build_prop.path.tools_dir, "yymisc"),
                True),
            butil.Project("yy",
                "yy.sln",
                ".",
                True),
            butil.Project("crashreport",
                "crashreport.sln",
                self._build_prop.path.crash_report_dir,
                True),
            butil.Project("yy_checker",
                "yy_checker.sln",
                os.path.join(self._build_prop.path.tools_dir, "yy_checker"),
                True),
            butil.Project("duospeak",
                "duospeak.sln",
                os.path.join(self._build_prop.path.tools_dir, "duospeak"),
                True),
            butil.Project("yylauncher",
                "yylauncher.sln",
                self._build_prop.path.tools_dir,
                True),
        ]

    def execute(self):
        self._logger.info("execute compile task [%s]" % self._get_task_name())

        self._generate_pro_files()

        if self._build_prop.compile.recompile:
            self._clean_by_devenv(self._projects, self._get_vc_config_name())

        self._compile_by_devenv(self._projects, self._get_vc_config_name())
        self._unit_test()

    def _get_task_name(self):
        u"""供子类使用，定义用于标识task的名字，如Debug"""
        raise BuildException("_get_task_name must be overrided")

    def _generate_pro_files(self):
        u"""调用qmake，生成.sln和.vcproj文件"""
        command_list = ["qmake", "-tp", "vc", "-r"]

        if self._build_prop.share.official_build:
            command_list.append("DEFINES+=OFFICIAL_BUILD")

        if self._build_prop.share.coverage:
            command_list.append("DEFINES+=CODE_COVERAGE")
            command_list.append("QMAKE_LFLAGS_RELEASE+=/debugtype:cv,fixup")

        self._get_addtional_qmake_arguments(command_list)

        returncode = subprocess.call(command_list)

        if returncode != 0:
            raise BuildException("Invoke qmake failed")

    def _get_addtional_qmake_arguments(self, qmake_command_list):
        u"""供子类使用，添加附加的qmake参数"""
        pass

    def _get_vc_config_name(self):
        u"""供子类使用，定义vc的config名，用于调用devenv时使用"""
        raise BuildException("_get_vc_config_name must be overrided")

    def _clean_by_devenv(self, project_list, config):
        for project in project_list:
            devenv = butil.Devenv(self._build_prop)
            devenv(project,
                "clean",
                config)

    def _compile_by_devenv(self, project_list, config):
        for project in project_list:
            devenv = butil.Devenv(self._build_prop)
            devenv(project,
                "build",
                config)

    def _unit_test(self):
        u"""供子类使用，单元测试"""
        raise BuildException("_unit_test must be overrided")

class DebugCompileTask(AbstractCompileTask):
    u"""编译Debug版"""
    def _get_task_name(self):
        return "Debug"

    def _get_vc_config_name(self):
        return "Debug|Win32"

    def _unit_test(self):
        unittest_dir = "/app/bin/debug"
        returncode = subprocess.call([
            "app/bin/debug/dwutest.exe",
            unittest_dir,
            "--gtest_output=xml:log/unittest/dwutestd.xml",
        ])

        if returncode == 0:
            self._logger.info("%s: unittest success" % unittest_dir)
        else:
            raise BuildException("%s: unittest failed, returncode: %d"
                % (unittest_dir, returncode))

class ReleaseCompileTask(AbstractCompileTask):
    u"""编译Release版"""
    def _get_task_name(self):
        return "Release"

    def _get_vc_config_name(self):
        return "Release|Win32"

    def _unit_test(self):
        unittest_dir = "/app/bin/release"
        returncode = subprocess.call([
            "app/bin/release/dwutest.exe",
            unittest_dir,
            "--gtest_output=xml:log/unittest/dwutest.xml",
        ])

        if returncode == 0:
            self._logger.info("%s: unittest success" % unittest_dir)
        else:
            raise BuildException("%s: unittest failed, returncode: %d"
                % (unittest_dir, returncode))


import random
import torch
import torch.nn as nn
import torch.nn.parallel
from miscc.config import cfg
from torch.autograd import Variable
import torch.nn.functional as F
from torchvision import models
import torch.utils.model_zoo as model_zoo

class Encoder(nn.Module):
    def __init__(self):
        super(Encoder, self).__init__()

        self.hid_dim = cfg.CHARLSTM.DIMENSION
        self.n_layers = 1

        self.word_embeds = nn.Embedding(cfg.CHAR.VOCABSIZE, cfg.CHARVEC.DIMENSION)

        self.pokemon_embed = nn.Linear(cfg.POKEMON.SIZE, cfg.POKEMON.DIMENSION)

        self.rnn = nn.LSTM(cfg.CHARVEC.DIMENSION + cfg.POKEMON.DIMENSION, cfg.CHARLSTM.DIMENSION, 1, dropout=cfg.CHARLSTM.DROPOUT)

        self.dropout = nn.Dropout(cfg.CHARLSTM.DROPOUT)

    def forward(self, sentence, embedding):
        embeds = self.word_embeds(Variable(sentence))
        concated_emb = torch.cat((embeds, self.pokemon_embed(embedding).repeat(len(sentence), 1, 1)), dim=2)
        lstm_out, (hn, cn) = self.rnn(concated_emb)
        return hn, cn

class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()

        self.output_dim = cfg.CHAR.VOCABSIZE
        self.hid_dim = cfg.CHARLSTM.DIMENSION
        self.n_layers = 1

        self.word_embeds = nn.Embedding(cfg.CHAR.VOCABSIZE + 1, cfg.CHARVEC.DIMENSION)

        self.pokemon_embed = nn.Linear(cfg.POKEMON.SIZE, cfg.POKEMON.DIMENSION)

        self.rnn = nn.LSTM(cfg.CHARVEC.DIMENSION + cfg.POKEMON.DIMENSION, cfg.CHARLSTM.DIMENSION, 1, dropout=cfg.CHARLSTM.DROPOUT)

        self.fc_out = nn.Linear(cfg.CHARLSTM.DIMENSION, cfg.CHAR.VOCABSIZE + cfg.POKEMON.SIZE)

        self.dropout = nn.Dropout(cfg.CHARLSTM.DROPOUT)

    def forward(self, sentence, embedding, hidden, cell):
        sentence = sentence.unsqueeze(0)
        embeds = self.word_embeds(Variable(sentence))
        concated_emb = torch.cat((embeds, self.pokemon_embed(embedding).unsqueeze(0)), dim=2)
        output, (hidden, cell) = self.rnn(concated_emb, (hidden, cell))
        prediction = self.fc_out(output.squeeze(0))
        output_word = prediction[:, :cfg.CHAR.VOCABSIZE]
        output_emb = prediction[:, cfg.CHAR.VOCABSIZE:]

        return output_word, output_emb, hidden, cell

class Seq2Seq(nn.Module):
    def __init__(self, encoder, decoder, device):
        super(Seq2Seq, self).__init__()

        self.encoder = encoder
        self.decoder = decoder
        self.device = device

        assert encoder.module.hid_dim == decoder.module.hid_dim, \
            "Hidden dimensions of encoder and decoder must be equal!"
        assert encoder.module.n_layers == decoder.module.n_layers, \
            "Encoder and decoder must have equal number of layers!"

    def forward(self, pokemon_words, pokemon_emb, teacher_forcing_ratio=0.5):
        # src = [src len, batch size]
        # src = [src len, batch size]
        # teacher_forcing_ratio is probability to use teacher forcing
        # e.g. if teacher_forcing_ratio is 0.75 we use ground-truth inputs 75% of the time

        batch_size = pokemon_words.shape[1]
        src_len = pokemon_words.shape[0]
        src_vocab_size = self.decoder.module.output_dim

        # tensor to store decoder outputs
        outputs = torch.zeros(src_len, batch_size, src_vocab_size).to(self.device)

        # last hidden state of the encoder is used as the initial hidden state of the decoder
        hidden, cell = self.encoder(pokemon_words, pokemon_emb)

        # first input to the decoder is the <sos> tokens
        input = torch.tensor([cfg.CHAR.VOCABSIZE]).repeat(cfg.TRAIN.BATCH_SIZE)
        input_pokemon_emb = torch.zeros(cfg.TRAIN.BATCH_SIZE, cfg.POKEMON.SIZE)
        for t in range(src_len):
            # insert input token embedding, previous hidden and previous cell states
            # receive output tensor (predictions) and new hidden and cell states
            output_word, output_emb, hidden, cell = self.decoder(input, input_pokemon_emb, hidden, cell)

            # place predictions in a tensor holding predictions for each token
            outputs[t] = output_word

            # decide if we are going to use teacher forcing or not
            teacher_force = random.random() < teacher_forcing_ratio

            # get the highest predicted token from our predictions
            top1 = torch.argmax(output_word, dim=1)

            # if teacher forcing, use actual next token as next input
            # if not, use predicted token
            if teacher_force:
                input = pokemon_words[t]
                input_pokemon_emb = pokemon_emb
            else:
                input = top1
                input_pokemon_emb = output_emb

        return outputs, output_emb


#!/usr/bin/env python3

import argparse
import os
from subprocess import call
from wand.image import Image

__version__     = '0.2.0'
__author__      = 'Shawn McElroy'
__description__ = 'Convert text based files to other formats.'
__homepage__    = 'http://github.com/skiftio/txt2other'
__email__       = 'hey@skift.io'
__license__     = 'MIT'

parser = argparse.ArgumentParser(description='convert code to other format (ps, pdf, jpg).')
parser.add_argument('-r','--recursive', help='scan files recursively',                      default=False, action='store_true')
parser.add_argument('-f','--force',     help='overwrite any already generated files'      , default=False, action='store_true')
parser.add_argument('-d','--directory', help='top level directory to be scanned',           default='.',   required=False)
parser.add_argument('-i','--input file',help='file to be scanned',                          default=False, required=False)
parser.add_argument('-t','--type',      help='format to be converted to',                   default='pdf', required=False)
parser.add_argument('-o','--out',       help='location where the output will be generated', default='output', required=False)
parser.add_argument('-y','--only',      help='only convert files with these extensions. Comma seperated list (php,py,txt)',
                    default=False, required=False)
args = vars(parser.parse_args())

class txt2jpg():

    def __init__(self, out, recr, ftype, force, only=False):

        self.out   = out
        self.recr  = recr
        self.ftype = ftype
        self.force = force

        self.ignorePaths = (
            '__pycache__',
            'cache',
            'config',
            'console',
            'git',
            'idea',
            'lib',
            'locale',
            'node_modules',
            'out',
            'output',
            'plugin',
            'test',
            'tmp',
            'uploads',
            'vendor',
            'bootstrap',
            'sass',
            'font',
            'logs'
        )

        self.ignoreFiles = [
            'code2jpg.iml',
            'gruntfile',
            'composer',
            'bootstrap.css',
            'bootstrap.js',
            'jquery',
            'test',
            '.min.',
            'travis.yml'
        ]

        self.ignoreExtensions = (
            'jpg',
            'ps',
            'pdf',
            'gitignore',
            'md',
            'iml',
            'gitignore',
            'htaccess',
            'gitattributes',
            'project',
            'json'
        )

        if only is not False:
            self.onlyExtensions = only
            ign = list(self.ignoreExtensions)
            [ign.remove(ext) for ext in self.onlyExtensions if ext in self.ignoreExtensions]
            self.ignoreExtensions = tuple(ign)

        else:
            self.onlyExtensions = (
                'php',
                'js',
                'css',
                'ctp',
                'py'
            )

    def parseFile(self, file, fold, curPath, depth=0):
        if self.ignoreFile(file, curPath):
            return

        if fold == '/':
            output  = self.out + '/'
        else:
            output = self.out + fold + '/'

        self.processFiles(file, output, curPath)

    def parseDir(self, fold, curPath, depth=0):
        # print('')

        if self.ignoreDir(fold):
            return

        # print('Folder:[' + fold + ']')
        # print('Path  :[' + curPath + ']')

        # make sure path exists
        if not os.path.isdir(curPath):
            self.badPath(fold)

        # we need to make sure this dir is in the output dir
        if not os.path.isdir(self.out):
            self.cmd(['mkdir', '-p', self.out])

        newOutput = self.out + fold + '/'
        print('Output    :[' + newOutput + ']')
        if not os.path.isdir(newOutput):
            self.cmd(['mkdir', '-p', newOutput])

        currentItems = os.listdir(curPath)

        dirs  = []
        files = []
        other = []
        for item in currentItems:
            curr = curPath + item
            if os.path.isdir(curr) and not self.ignoreDir(item):
                dirs.append(item)
            elif os.path.isfile(curr) and not self.ignoreFile(item, curPath):
                files.append(item)
            else:
                other.append(item)

        # print('dir items:')
        # print(dirs)
        # print('file item:')
        # print(files)
        # print('other item:')
        # print(other)

        # first lest process the current files
        if len(files) > 0:
            [self.parseFile(newFile, fold, curPath, depth) for newFile in files]

        # parse directories, only if recursive is true
        if self.recr is True and len(dirs) > 0:
            [self.parseDir(fold + newDir + '/', curPath + newDir + '/', depth+1) for newDir in dirs]

    def ignoreFile(self, file, path):
        name = file.lower()

        if len(self.onlyExtensions) > 0 and not name.endswith(self.onlyExtensions):
            # if we have an only extension list, lets use that
            return True

        if name.endswith(self.ignoreExtensions):
            # check for extension
            return True

        for ignore in self.ignoreFiles:
            if ignore in name or name == ignore:
                return True

        filepath = path + name
        if not os.path.isfile(filepath) or os.path.islink(filepath):
            # check if is real file
            return True

        return False

    def ignoreDir(self, fold):
        fold = fold.lower()
        for ignore in self.ignorePaths:
            if ignore in fold or ignore == fold:
                # make sure were not ignoring this
                return True

        return False

    def processFiles(self, file, outPath, curPath):
        # files
        curFile = curPath + file
        outFile = outPath + file
        psFile  = outFile + '.ps'
        pdfFile = outFile + '.pdf'
        jpgFile = outFile + '.jpg'

        # start generating
        print('converting:[' + curFile + '] -> [' + outFile + '.' + self.ftype + ']')

        # check if file was already generated. No need to if already there.
        if self.force is False and \
            (os.path.isfile(outFile + '.' + self.ftype)
             or os.path.isfile(outFile + '-0.' + self.ftype)):
            return

        if self.ftype == 'ps':
            self.makePs(curFile, psFile)

        if self.ftype == 'pdf':
            self.makePdf(curFile, psFile, pdfFile)

        if self.ftype == 'jpg':
            altFile = outFile + '-0' + '.jpg'
            self.makeJpg(curFile, psFile, pdfFile, jpgFile)

        # remove ps file
        if self.ftype != 'ps': self.cmd(['rm', psFile])

        # remove ps file
        if self.ftype != 'pdf': self.cmd(['rm', pdfFile])

        # remove pdf file
        if self.ftype != 'jpg': self.cmd(['rm', jpgFile])

    def makePs(self, curFile, psFile):
        # easiest file to make
        self.cmd(['enscript', curFile, '-p', psFile])

    def makePdf(self, curFile, psFile, pdfFile):
        # we need a ps file first
        self.makePs(curFile, psFile,)
        self.cmd(['ps2pdf', psFile, pdfFile])

    def makeJpg(self, curFile, psFile, pdfFile, jpgFile):
        # first we need a pdf file
        self.makePdf(curFile, psFile, pdfFile)
        with Image(filename=pdfFile, resolution=300) as img:
            img.save(filename=jpgFile)

    def cmd(self, cmd):
        with open(os.devnull, "w") as fnull:
            return call(cmd, stdout=fnull, stderr=fnull)

    def badPath(self, fold):
        ## for lols mostly
        from random import choice
        comments = [
            'Bad path sir',
            'Did you spell that right',
            'Dude, really? That doesn\'t exist',
            'Not a real path',
            'No dice. No Folder',
            'PEBKAC Error. Folder does not exist'
        ]
        print(choice(comments) + ':[' + fold + ']')
        exit()

def main():
    # print(args)
    recr  = args['recursive']
    fold  = args['directory']
    file  = args['file']
    ftype = args['type']
    out   = args['out']
    force = args['force']
    only  = args['only']

    # make sure output dir is properly setup
    if not os.path.isdir(out):
        os.mkdir(out)
    out = os.path.realpath(out)

    # make sure current folder is correct
    if not os.path.isdir(fold):
        print('Not a directory:[' + fold + ']')

    if only is not True and 'str' in str(type(only)):
        only = tuple([x.strip() for x in only.split(',')])

    curPath = os.path.realpath(fold) + '/'

    t2j = txt2jpg(out=out, recr=recr, ftype=ftype, force=force, only=only)

    if file is not False:
        t2j.parseFile(file, '/', curPath=curPath)
    else:
        t2j.parseDir('/', curPath=curPath)

if __name__ == '__main__':
    main()


import requests
import json

class Douban():
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36",
            "Referer": "https://m.douban.com/tv/american"}

        self.url = "https://m.douban.com/rexxar/api/v2/subject_collection/filter_tv_american_hot/items?os=android&for_mobile=1&start=0&count=18&loc_id=108288&_=1529481198406"

    def send_url(self):#发送请求,获取相应
        response = requests.post(url=self.url,headers=self.headers)
        return response.content.decode()
    def get_content(self,send_dict):#接受数据,处理数据
        dict_ret=json.loads(send_dict)
        json_ret =dict_ret["subject_collection_items"]
        return json_ret
    def save_content(self,content):#保存数据
        with open('douban.txt', 'w', encoding='utf-8') as f:
            json.dump(content, f, ensure_ascii=False, indent=1)
    def run(self):#运行
        send_dict=self.send_url()
        html=self.get_content(send_dict)
        content=self.save_content(html)

if __name__ == '__main__':
    douban = Douban()
    douban.run()


import argparse
import codecs
import logging
from collections import Counter

import epihan
import panphon
import unicodecsv as csv

logging.basicConfig(level=logging.WARNING)

def normpunc(epi, s):
    def norm(c):
        if c in dict(epi.punc):
            return epi.normalize_punc(c)
        else:
            return c
    return ''.join(map(norm, s))

def add_record_gen(epi, ft, orth):
    space = Counter()
    orth = normpunc(epi, orth)
    trans = epi.transliterate(orth)
    while trans:
        pref = ft.longest_one_seg_prefix(trans)
        if pref != '':
            space[pref] += 1
            trans = trans[len(pref):]
        else:
            if trans[0] in epi.puncnorm_vals:
                space[trans[0]] += 1
            else:
                space[trans[0]] += 1
            trans = trans[1:]
    return space

def add_file(epi, ft, fn):
    space = Counter()
    with codecs.open(fn, 'r', 'utf-8') as f:
        for line in f:
            fields = line.split(u'\t')
            if len(fields) > 0:
                orth = fields[0]
                space.update(add_record_gen(epi, ft, orth))
    logging.debug(u'Length of counter:\t{}'.format(len(space)))
    return space

def print_space(output, space):
    pairs = enumerate(sorted(filter(lambda x: x, space.keys())))
    with open(output, 'wb') as f:
        writer = csv.writer(f, encoding='utf-8')
        for i, char in pairs:
            writer.writerow((i, char))

def main(infiles, output):
    epi = epihan.Epihan('cedict', 'pinyin-to-ipa')
    ft = panphon.FeatureTable()
    space = Counter()
    for fn in infiles:
        logging.debug(u'Scanning:\t{}'.format(fn).encode('utf-8'))
        space.update(add_file(epi, ft, fn))
    print_space(output, space)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-o', '--output', help='Output file.')
    parser.add_argument('infiles', nargs='+', help='CONLL files serving as basis for segment space.')
    args = parser.parse_args()
    main(args.infiles, args.output)


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 15 16:31:04 2018

@author: fmuret
"""

import functions_bot

#==========================   INIT   ==========================================

new_liste=lister_fichier_mp3()  #list of the file in the current repertory
search_term=get_liste_information_name(new_liste)   #list of the key word to search and DL
#search_term=['genji','hanzo','fatale','symetra overwatch','faucheur','sombra','mc cree']
lengh=len(search_term)  #number of the term to search, number of music as well

main_directory="ALBUM IMAGE FOLDER"
folder_creation(main_directory) #check if there is already a folder which exist
print('\n')

#====================== IMAGE DL ==============================================
for p in range (0,lengh,1):

    url = 'https://www.google.com/search?q=' + quote(search_term[p]) + '&espv=2&biw=1366&bih=667&site=webhp&source=lnms&tbm=isch'
    raw_html = (download_page(url))
    items =(_images_get_all_items(raw_html))    #sort and find all the picture  of the web page
    #print("Total Image Links = " + str(len(items)))       #display the number of all the picture string found

    k=0
    limit=5     #limit of attempt before to find a good picture, in case of error
    errorCount=0

    while (k < limit):      #DL start
                try:
                    req = Request(items[k], headers={
                        "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"})
                    response = urlopen(req, None, 15)
                    image_name = str(items[k][(items[k].rfind('/')) + 1:])
                    if '?' in image_name:
                        image_name = image_name[:image_name.find('?')]
                    if ".jpg" in image_name or ".png" in image_name or ".jpeg" in image_name or ".svg" in image_name:
                        output_file = open(main_directory + "/" + "/" + search_term[p] + ".jpg", 'wb')
                    else:
                        output_file = open(main_directory + "/" + "/" + search_term[p] + ".jpg", 'wb')
                        image_name = image_name + ".jpg"

                    data = response.read()
                    output_file.write(data)
                    response.close()

                    print("completed ====> " + str(k + 1) + ". " + image_name)
                    break
                    k = k + 1

                except IOError:  # If there is any IOError
                    errorCount += 1
                    print("IOError on image " + str(k + 1))
                    k = k + 1

                except HTTPError as e:  # If there is any HTTPError
                    errorCount += 1
                    print("HTTPError" + str(k))
                    k = k + 1

                except URLError as e:
                    errorCount += 1
                    print("URLError " + str(k))
                    k = k + 1

                except ssl.CertificateError as e:
                    errorCount += 1
                    print("CertificateError " + str(k))
                    k = k + 1

                if args.delay:
                    time.sleep(int(args.delay))

#================ IMAGE REPLACEMENT ===========================================

for n in range (0,lengh,1):
    path_music=new_liste[n]
    path_image=search_term[n]
    image_replacement(path_music,path_image)


from scipy import io
import numpy as np
import os

matFile_path = 'data' + os.path.sep + 'mnist-original.mat'
initialData = io.loadmat(matFile_path)

data = initialData['data'].transpose([1,0])
data = data.reshape([-1,1,28,28])
label = initialData['label'].transpose([1,0])

data_save_path = 'data'

if not(os.path.exists(data_save_path)):
	os.makedirs(data_save_path)

np.save(data_save_path + os.path.sep + 'mnist',data)
np.save(data_save_path + os.path.sep + 'mnist_label',label)


# uncompyle6 version 3.7.4
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/mkoenig/git/sbmlutils/sbmlutils/dfba/sedml.py
# Compiled at: 2017-11-11 18:07:31
# Size of source mod 2**32: 1243 bytes
"""
Helper functions to create SED-ML.
"""
import os, phrasedml

def create_sedml(sedml_location, sbml_location, directory, dt, tend, species_ids, reaction_ids):
    """ Creates SED-ML file for the given simulation.

    :param sedml_location:
    :param sbml_location:
    :param directory:
    :param dt:
    :param tend:
    :return:
    """
    phrasedml.setWorkingDirectory(directory)
    steps = int(1.0 * tend / dt)
    p = '\n          model1 = model "{}"\n          sim1 = simulate uniform(0, {}, {})\n          sim1.algorithm = kisao.500\n          task1 = run sim1 on model1\n          plot "Figure 1: DFBA species vs. time" time vs {}\n          plot "Figure 2: DFBA fluxes vs. time" time vs {}\n          report "Report 1: DFBA species vs. time" time vs {}\n          report "Report 2: DFBA fluxes vs. time" time vs {}\n\n    '.format(sbml_location, tend, steps, species_ids, reaction_ids, species_ids, reaction_ids)
    return_code = phrasedml.convertString(p)
    if return_code is None:
        print(phrasedml.getLastError())
    sedml = phrasedml.getLastSEDML()
    sedml_file = os.path.join(directory, sedml_location)
    with open(sedml_file, 'w') as (f):
        f.write(sedml)
    print(sedml_file)


# coding: utf8

def checkio(number):
    result = 0
    num_of_pigeons = []
    next_pigeon = [x for x in range(100)]

    while number > 0:
        for x in  next_pigeon:              # Проходим по списку очереди из следующих голубей
            while x > 0:                    # Добавляем группу следующих голубей в общий список
                num_of_pigeons.append(0)
                x -= 1
            for y in range(len(num_of_pigeons)):    # Кормим
                if number > 0:              # Если корм есть, кормим голубей из общего списка
                    num_of_pigeons[y] = num_of_pigeons[y] + 1
                    number -= 1
                else:
                    break
    for z in num_of_pigeons:                # Суммируем покормленных голубей
        if z > 0:
            result += 1
    return result

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio(1) == 1, "1st example"
    assert checkio(2) == 1, "2nd example"
    assert checkio(5) == 3, "3rd example"
    assert checkio(10) == 6, "4th example"


"""
Uploadwithus is a command line tool to facilitate keeping sendwithus templates
and snippets code up to date with an emails repository.  The tool allows you to
maintain your email templates and snippets under a version control system, and
also allows separation of testing and production emails.  For more in depth
information on its usage and features, check out the README.

Copyright (c) 2017, Nick Balboni.
License: MIT (see LICENSE for details)
"""

# python 2 support
from __future__ import print_function, unicode_literals
try:
    from builtins import input
    import errno
except ImportError:
    pass

import os
import re
import sys
import yaml
from cached_property import cached_property
from sendwithus import api as sendwithus_api
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

__author__ = 'Nick Balboni'
__version__ = '0.2.5'
__license__ = 'MIT'

### Constants ##################################################################
DEFAULT_TPL_VER = 'general'
DEV_NAME_TPL    = lambda name: 'test_{}'.format(name)
DEV_SUBJECT_TPL = lambda subject: '[DEV] {}'.format(subject)
TPL_CREATE_MSG  = 'created new template < {} >'
TPL_CREATE_ERR  = 'unable to create template < {} > :\n\t{}'
TPL_UPDATE_MSG  = 'updated template < {} >'
TPL_UPDATE_ERR  = 'unable to update template < {} > :\n\t{}'
VER_CREATE_MSG  = 'created new version < {} > for template < {} >'
VER_CREATE_ERR  = 'unable to create version < {} > on template < {} > :\n\t{}'
VER_UPDATE_MSG  = 'updated version < {} > on template < {} >'
VER_UPDATE_ERR  = 'unable to update version < {} > on template < {} > :\n\t{}'
SNP_CREATE_MSG  = 'created new snippet < {} >'
SNP_CREATE_ERR  = 'unable to create snippet < {} > :\n\t{}'
SNP_UPDATE_MSG  = 'updated snippet < {} >'
SNP_UPDATE_ERR  = 'unable to update snippet < {} > :\n\t{}'
TPL_NOT_FOUND_ERR = 'template < {} > not found'
VER_NOT_FOUND_ERR = 'version < {} > on template < {} > not found'
SNP_NOT_FOUND_ERR = 'snippet < {} > not found'

### Helpers ####################################################################
def log(template, *args, **kwargs):
    status = 'ERROR' if 'error' in kwargs and kwargs['error'] else 'DEBUG'
    print(status, ':', template.format(*args))

def snippet_replace(content, snippet):
    reg = r'{%\s+snippet\s+[\'\"]{1}' + snippet + '[\'\"]{1}\s+%}'
    return re.sub(reg, get_content(snippet, snippet=True), content)

def get_content(name, version=None, snippet=False):
    """ throws FileNotFoundError """
    if snippet:
        fp = os.path.join('snippets', name + '.html')
    elif version is None:
        fp = os.path.join('templates', name + '.html')
    else:
        fp = os.path.join('templates', name, version + '.html')
    try:
        with open(fp) as fs:
            return fs.read()
    except FileNotFoundError as e:
        if snippet:
            log(SNP_NOT_FOUND_ERR, name, error=True)
        elif version is None:
            log(TPL_NOT_FOUND_ERR, name, error=True)
        else:
            log(VER_NOT_FOUND_ERR, name, version, error=True)

### The Meat and Potatos #######################################################
class API:
    def __init__(self, key, expands):
        self.api = sendwithus_api(api_key=key)
        self.expands = expands

    def parse_content(self, content):
        # expand snippets before uploading
        for snippet in self.expands:
            content = snippet_replace(content, snippet)
        # regex replace
        ptn = re.compile(r'{%\s+snippet\s+[\'\"]{1}(.*?)[\'\"]{1}\s+%}')
        for match in ptn.finditer(content):
            new_name = 'test_' + match.group(1)
            new_snippet = match.group(0).replace(match.group(1), new_name)
            content = content.replace(match.group(0), new_snippet)
        return content

    ### TEMPLATES ##############################################################
    @cached_property
    def sendwithus_templates(self):
        templates_data = {}
        for tpl in self.api.templates().json():
            templates_data[tpl['name']] = {
                'id': tpl['id'],
                'versions': { v['name']: v['id'] for v in tpl['versions'] }
            }
        return templates_data

    @cached_property
    def local_templates(self):
        templates_data = {}
        with open('template_info.yaml', 'r') as f:
            templates_data = yaml.load(f)
        return templates_data

    def create_template(self, template, development=True):
        """ create new templates with the general version
            returns template id and array of created versions """
        try:
            html = get_content(template, version=DEFAULT_TPL_VER)
            subject = self.local_templates[template]['subject']
            if development:
                html = self.parse_content(html)
                subject = DEV_SUBJECT_TPL(subject)
                template = DEV_NAME_TPL(template)
            # create new template
            resp = self.api.create_template(template, subject, html)
            resp.raise_for_status()
            tid = resp.json()['id']
            # get id of the new version
            resp = self.api.get_template(tid)
            resp.raise_for_status()
            vid = list(resp.json()['versions'])[0]['id']
            # rename template to DEFAULT_TPL_VER
            # NOTE: template can potentially be created but not renamed: add cleanup?
            self.api.update_template_version(
                DEFAULT_TPL_VER, subject, tid, vid, html=html
            ).raise_for_status()
        except Exception as e:
            log(TPL_CREATE_ERR, template, e, error=True)
        else:
            log(TPL_CREATE_MSG, template)
            return ( tid, [ DEFAULT_TPL_VER ] )

    def create_template_version(self, template, tid, version, development=True):
        """ adds a new template version to an existing template
            returns boolean """
        try:
            html = get_content(template, version=version['name'])
            subject = version['subject']
            if subject is None:
                subject = self.local_templates[template]['subject']
            if development:
                html = self.parse_content(html)
                subject = DEV_SUBJECT_TPL(subject)
                template = DEV_NAME_TPL(template)
            self.api.create_new_version(
                version['name'], subject, template_id=tid, html=html
            ).raise_for_status()
        except Exception as e:
            log(VER_CREATE_ERR, version['name'], template, e, error=True)
            return False
        else:
            log(VER_CREATE_MSG, version['name'], template)
            return True

    def add_new_templates(self, development=True):
        """ creates new templates for any local templates that don't have
            sendwithus copies; throws FileNotFoundError """
        created_templates = {}
        for key, value in self.local_templates.items():
            name = DEV_NAME_TPL(key) if development else key
            tid = None
            # if local template is not on sendwithus
            if not name in self.sendwithus_templates:
                content = self.create_template(key, development)
                if not content is None:
                    tid, versions = content
                    created_templates[name] = versions
            else:
                tid = self.sendwithus_templates[name]['id']
            # search local versions for ones not already on sendwithus
            for version in value['versions']:
                v = version['name']
                if v == DEFAULT_TPL_VER or (name in self.sendwithus_templates and \
                v in self.sendwithus_templates[name]['versions']):
                    continue
                if self.create_template_version(key, tid, version, development):
                    if name in created_templates:
                        created_templates[name].append(v)
                    else:
                        created_templates[name] = [v]
        return created_templates

    def update_templates(self, development=True):
        """ updates the templates copies on sendwithus
            throws KeyError and FileNotFoundError """
        created_templates = self.add_new_templates(development=development)
        for key, value in self.local_templates.items():
            name = DEV_NAME_TPL(key) if development else key
            versions = [ (v['name'], v['subject']) for v in value['versions'] ]
            for version, subject in versions:
                if name in created_templates and version in created_templates[name]:
                    continue # don't re-add templates or versions
                try:
                    html = get_content(key, version)
                    if subject is None:
                        subject = value['subject']
                    template_id = self.sendwithus_templates[name]['id']
                    version_id = self.sendwithus_templates[name]['versions'][version]
                    if development:
                        html = self.parse_content(html)
                        subject = DEV_SUBJECT_TPL(subject)
                    self.api.update_template_version(
                        version, subject, template_id, version_id, html=html
                    ).raise_for_status()
                except Exception as e: # KeyError, requests Error
                    log(VER_UPDATE_ERR, version, name, e, error=True)
                else:
                    log(VER_UPDATE_MSG, version, name)

    def get_sendwithus_ids(self):
        for key, value in self.sendwithus_templates.items():
            print(key, ':', value['id'])

    ### SNIPPETS ###############################################################
    @cached_property
    def sendwithus_snippets(self):
        return { s['name']: s['id'] for s in self.api.snippets().json() }

    @cached_property
    def local_snippets(self):
        snippets_data = []
        with open('snippet_info.yaml', 'r') as f:
            snippets_data = yaml.load(f)
        return snippets_data

    def add_new_snippets(self, development=True):
        """ creates new snippets for any local snippets that don't have
            sendwithus copies """
        created_snippets = {}
        for snippet in self.local_snippets:
            name = DEV_NAME_TPL(snippet) if development else snippet
            if not name in self.sendwithus_snippets:
                try:
                    html = get_content(snippet, snippet=True)
                    if development:
                        html = self.parse_content(html)
                    resp = self.api.create_snippet(name, html)
                    resp.raise_for_status()
                except Exception as e:
                    log(SNP_CREATE_ERR, name, e, error=True)
                else:
                    log(SNP_CREATE_MSG, name)
                    created_snippets[name] = resp.json()['snippet']['id']
        return created_snippets

    def update_snippets(self, snippets=[], development=True):
        """ overwrites sendwithus snippet copies with locally held snippets """
        if len(snippets) == 0:
            snippets = self.local_snippets
        created_snippets = self.add_new_snippets(development=development)
        for snippet in snippets:
            name = DEV_NAME_TPL(snippet) if development else snippet
            if name in created_snippets:
                continue
            try:
                html = get_content(snippet, snippet=True)
                if development:
                    html = self.parse_content(html)
                snippet_id = self.sendwithus_snippets[name]
                resp = self.api.update_snippet(snippet_id, name, html)
                resp.raise_for_status()
            except Exception as e: # KeyError, requests exception
                log(SNP_UPDATE_ERR, name, e, error=True)
            else:
                log(SNP_UPDATE_MSG, name)

### Command Line Interface #####################################################
def parse_args():
    parser = ArgumentParser(
        formatter_class=ArgumentDefaultsHelpFormatter,
        description=__doc__ )
    parser.add_argument('-v', '--version', action='store_true',
        help='print out version string and exit')
    parser.add_argument('-i', '--info', action='store_true',
        help='print out the sendwithus ids and names of the templates and '
        'snippets included in the templates yaml file')
    parser.add_argument('--update-dev', action='store_true',
        help='update the sendwithus development templates and snippets')
    parser.add_argument('--update-prod', action='store_true',
        help='update the sendwithus production templates and snippets')
    # parser.add_argument('templates', metavar='T', type=str, nargs='*',
    #     help='the list of templates to upload')
    # parser.add_argument('-s', '--snippets', action="store_true",
    #     help='provide this flag to upload all local snippets.'
    #     '  Any new snippets will be created')
    return parser.parse_args()

### Main #######################################################################
def main():
    options = parse_args()
    if options.version:
        print('uploadwithus', __version__)
        sys.exit(0)
    config = {}
    try: # read config file
        with open('config.yaml', 'r') as f:
            config = yaml.load(f)
    except IOError as e:
        if e.errno == errno.ENOENT: # FileNotFoundError
            log('config file not found')
        else:
            raise
    if not 'api_key' in config:
        try: # initiate api key from environmental variable
            config['api_key'] = os.environ['SENDWITHUS_API_KEY']
        except KeyError as e:
            log('SENDWITHUS_API_KEY environmental variable not found.', error=True)
            sys.exit(1)
    if not 'expand' in config:
        config['expand'] = []
    _api = API(config['api_key'], config['expand'])
    if options.info:
        _api.get_sendwithus_ids()
    if options.update_dev:
        _api.update_snippets(development=True)
        _api.update_templates(development=True)
    if options.update_prod:
        resp = input(
            'NOTE: this option modifies production emails, use only when '
            'deploying development code.  Type `I understand` to continue.'
            '\n-->  '
        )
        if resp == 'I understand':
            _api.update_snippets(development=False)
            _api.update_templates(development=False)

if __name__ == '__main__':
    main()


import json
import csv

class OutFile(object):

    def __init__(self, file, items, fieldnames=None):
        self.file = file
        self.items = items
        self.fieldnames = fieldnames

    def _import(self, delimiter):
        with open(self.file, 'w') as openfile:
            writer = csv.DictWriter(
                openfile,
                delimiter=delimiter,
                fieldnames=self.fieldnames)
            writer.writeheader()
            for item in self.items.values():
                writer.writerow({
                    k:unicode(v).encode("utf-8")
                    for k,v in item.items()})

    def csv(self):
        self._import(',')

    def txt(self):
        self._import('\t')

    def json(self):
        with open(self.file, 'w') as openfile:
            json.dump(
                self.items,
                openfile,
                sort_keys = True,
                indent = 4,
                ensure_ascii=True)


#! /usr/bin/env python3

import os
import signal
from subprocess import call
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as AppIndicator

APPINDICATOR_ID = "screenrotator"
orientation = "right"

def main():
    global indicator
    script_path = os.path.dirname(os.path.realpath(__file__))
    icon_path = script_path + '/icon.svg'
    indicator = AppIndicator.Indicator.new(APPINDICATOR_ID, icon_path, AppIndicator.IndicatorCategory.SYSTEM_SERVICES)
    indicator.set_label("Horizontal", "Horizontal")
    indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(build_menu())
    Gtk.main()

def build_menu():
    menu = Gtk.Menu()
    #rotate
    item_rotate = Gtk.MenuItem(label='Rotar pantalla')
    item_rotate.connect('activate', rotate_screen)
    menu.append(item_rotate)
    #seperator
    seperator = Gtk.SeparatorMenuItem()
    menu.append(seperator)
    #quit
    item_quit = Gtk.MenuItem(label='Cerrar')
    item_quit.connect('activate', quit)
    menu.append(item_quit)
    menu.show_all()
    return menu

def rotate_screen(source):
    global orientation
    if orientation == "right":
        indicator.set_label("Vertical", "Horizontal")
        direction = "normal"
    elif orientation == "normal":
        indicator.set_label("Horizontal", "Horizontal")
        direction ="right"
    call(["xrandr", "-o", direction])
    orientation = direction

if __name__ == "__main__":
    #make sure the screen is in "right" orientation when the script starts
    call(["xrandr", "-o", orientation])
    #keyboard interrupt handler
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()


import json
import pandas
import numpy
import os
from itertools import chain
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin

class ForecastTransformer(BaseEstimator, TransformerMixin):

    def __init__(self, n_time_steps=8, prediction_steps=1):
        self.n = n_time_steps
        self.pred_steps = prediction_steps
        self.tuple_index = None

    def fit(self, X, y=None):
        """

        Args:
            X (pandas.DataFrame):
            y (pandas.Series, numpy.array):

        Returns:

        """
        top_columns = [['t_' + str(self.n - y) for x in range(self.n) for y in [x]*len(X.columns)] + ['t_0'],
                       list(X.columns)*self.n + ['target']]
        self.tuple_index = list(zip(*top_columns))
        return self

    def transform(self, X, y=None, live=False):
        data = []
        index = []
        for i, sub_df in X.groupby(numpy.arange(len(X))//self.n):

            target = sub_df['price'].values[-1]
            index.append(sub_df.index[-1])
            new_row = list(chain.from_iterable(sub_df.values.tolist())) + [target]
            data.append(new_row)

            if len(new_row) != len(self.tuple_index):
                continue

        forecasting = pandas.DataFrame(data, columns=pandas.MultiIndex.from_tuples(self.tuple_index), index=index)
        forecasting[('t_0', 'target')] = forecasting[('t_0', 'target')].shift(-self.pred_steps)
        if len(forecasting) > 1:
            forecasting = forecasting.dropna()
            return forecasting[:-self.pred_steps].values
        else:
            forecasting = forecasting.fillna(1)
            return forecasting.values

class TimeEmbedder(BaseEstimator, TransformerMixin):

    def __init__(self, inital_dims):
        """
        Transform to a time-embedded array from a single long array. I.e., group features into their
        buckets so that they actually represent data from related timestamps

        Args:
            inital_dims (int): initial dimensions of array
        """
        self.initial_dims = inital_dims
        self.middle_shape = None

    def fit(self, X, y=None):
        self.middle_shape = (X.shape[1]-1)//self.initial_dims
        return self

    def transform(self, X):
        y = X[:, -1]
        X = X[:, :-1]
        return numpy.reshape(X, (X.shape[0], self.middle_shape, self.initial_dims)), y

def get_data(source, remote=False, keep_keys=list(['ts']),
             categorical=list(['side'])):
    file_path = '/media/carlo/HDD/kafka_local/'
    file_path = os.path.join(file_path, source + '.txt')
    rows = []
    with open(file_path) as inf:
        for i, row in enumerate(inf):
            if remote:
                if i >= 8:
                    break
            row_dict = json.loads(row)
            rows.append({k: v for k, v in row_dict.items() if k in keep_keys})

    df = pandas.DataFrame(rows)
    df.index = pandas.to_datetime(df['ts'])
    df['ts'] = pandas.to_datetime(df['ts'])
    df['time_diff'] = df['ts'].diff().dt.seconds.div(1, fill_value=0)
    if categorical:
        df = one_hot_encode(df, categorical)
    df = df.drop('ts', 1)
    return df

def one_hot_encode(df, categ_vars):
    ohe = []
    for categ in categ_vars:
        ohe.append(pandas.get_dummies(df[categ]))
        df = df.drop(categ, 1)
    return pandas.concat([df] + ohe, 1)

if __name__ == '__main__':

    local_df = get_data('gdax', remote=False, keep_keys=['ts', 'price', 'volume_24h',
                                                         'spread', 'side'])  # type: pandas.DataFrame

    remote_df = get_data('gdax', remote=True, keep_keys=['ts', 'price', 'volume_24h',
                                                         'spread', 'side'])  # type: pandas.DataFrame

    pipe = Pipeline([
        ('tr', ForecastTransformer()),
        ('scaler', MinMaxScaler()),
        ('time', TimeEmbedder(inital_dims=len(local_df.columns)))
    ])

    x, y = pipe.fit_transform(local_df)


import sys
from cx_Freeze import setup, Executable

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
    name="XcalxS",
    version="0.1.0",
    description="RPN Calculator",
    options={
        "build_exe": {
            "include_msvcr": False,
            "optimize": 2,
            "packages": [],
            "includes": ['atexit', 'solver_tab', 'solver_lex'],
            "excludes": ["email", 'PySide.QtNetwork'],
            "bin_path_includes": [],
            "bin_path_excludes": [],
            "bin_includes": [],
            "bin_excludes": ['imageformats/*.dll'],
            "include_files": [],
            },
        },
    executables=[
        Executable("xcalcs.py", base=base),
        Executable("console.py", base=base)
        ],
    )


from imitation.reward_net import BasicRewardNet, BasicShapedRewardNet
import pytest
import tensorflow as tf

ENVS = ['FrozenLake-v0', 'CartPole-v1', 'Pendulum-v0']

@pytest.mark.parametrize("env", ENVS)
def test_init_no_crash(env):
    for i in range(3):
        with tf.variable_scope(env + str(i) + "shaped"):
            BasicShapedRewardNet(env)
        with tf.variable_scope(env + str(i)):
            BasicRewardNet(env)


__author__ = 'Isaac Thinkpad'
import sys

if __name__ == '__main__':
    try:
        FileName = sys.argv[1]
        fp = open(FileName, "r")
    except IndexError:
        print("Usage: parse.py [filename];")
        sys.exit(1)
    except:
        print(FileName, "is not a readable file.")
        sys.exit(2)

    for line in fp:
        Sum = 0.0
        Text = ""
        Count = 0
        Line = line.strip()
        Values = Line.split(" ")

        for item in Values:
            try:
                Sum += int(item)
                Count += 1
            except:
                Text = Text + item + " "
        if Sum != 0:
            print("%.3f" %(Sum / Count), Text[:-1])
        else:
            print(Text[:-1])


# Bioinformatics Stronghold
# dna - Counting DNA Nucleotides
# Michelle Ragsac (mragsac@github)
# 2016 Mar 2

# Given: 	A DNA string ss of length at most 1000 nt.

# Return: 	Four integers (separated by spaces) counting
# 			the respective number of times that the symbols 'A', 'C', 'G', and 'T'
# 			occur in ss.

def CountNucleotides(pattern):
	"""
	Determine the counts of the nucleotides in a sequence

	Input: 	AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC

	Output:	20 12 17 21
	"""

	# initiate variables to hold counts
	A = 0
	C = 0
	G = 0
	T = 0

	for x in range(0, len(pattern) ):
		if pattern[x] == "A":
			A += 1
		elif pattern[x] == "C":
			C += 1
		elif pattern[x] == "G":
			G += 1
		elif pattern[x] == "T":
			T += 1

	return "%d %d %d %d" % (A,C,G,T)


#!/usr/bin/python3
# vim:ft=python ts=4 sw=4 sts=4 et

# table.py - Simple python script that dynamically formats an ascii art table where
# each table cell holds an integer number (incremented, per cell).

# This is a typical excercise task coding lesson for new workers and I just
# wanted to see how bad I perform on this task.

# Written by Armin (Github: netzverweigerer)
# v2 - Thu Jan 22 16:15:24 CET 2015

import os
import sys
from subprocess import Popen, PIPE

output_raw = Popen(['stty', '-a'], stdout=PIPE)
# output = output_raw.stdout.read()
output = output_raw.stdout
print(output.readlines()[0])
# print(type(output))

#sys.exit(0)

# Table class to hold all our stuff
class Table(object):
    # constructor method, asks for number of rows/columns and
    # initiates empty variable/list states
    def __init__(self):
        terminalcolumns = os.environ['COLUMNS']
        terminalrows = os.environ['ROWS']
        print("")
        print("Please enter number of columns: ", end="")
        columns = input()
        self.columns = int(columns)
        print("Please enter number of rows: ", end="")
        rows = input()
        print("")
        self.dimensions = (columns,rows)
        self.lines = []
        self.rows = int(rows)
        self.total = self.rows * self.columns
        self.maxlength = len(str(self.total))
        self.currentrow = 0
        self.maxlinelength = 0
        self.outputlines = []
        self.resultlines = []
        self.errors = False

    # pad number with leading zeroes (or spaces)
    def pad(self,number):
        return format(number,str(self.maxlength)).replace(' ',' ')
        # return format(number,str(self.maxlength)).replace(' ','0')

    # build row with line drawing characters and numbers
    def buildrow(self,row):
        line = "|"
        for number in range(1,self.columns + 1):
            number = number + (row * (self.columns))
            line = line + " " + self.pad(number) + " " + "|"
        return line

    # build basic table layout by building each row and
    # appending it to the "lines" list object
    def buildtable(self):
        for row in range(0,self.rows):
            r = self.buildrow(row)
            self.lines.append(r)

    # go through all lines and update the maximum length of line variable
    def updatemaxlengthofline(self):
        for line in self.lines:
            length = len(line)
            if length > self.maxlinelength:
                self.maxlinelength = length

    def checkterminalsize(self):
        if self.columns > self.terminalcolumns:
            print("Error: your table would be bigger (columns) than your terminal could show.")
            self.errors = True
        if self.rows > self.terminalrows:
            print("Error: your table would be bigger (rows) than your terminal could show.")
            self.errors = True

    # returns a nicely formatted header
    def getheader(self):
        r = ","
        for char in range(1,self.maxlinelength - 1):
            r = r + '-'
        r = r + '.'
        return r

    # returns a nicely formatted footer
    def getfooter(self):
        r = '`'
        for char in range(1,self.maxlinelength - 1):
            r = r + '-'
        r = r + '´'
        return r

    # returns a nicely formatted horizontal seperator line
    def getseperator(self):
        chars = ''
        for char in range(0,self.maxlinelength):
            chars += '-'
        return chars

    # add header, table lines, seperators and footer to the "resultlines" list
    def buildresultlines(self):
        self.resultlines.append(self.getheader())
        for line in self.lines:
            self.resultlines.append(line)
            self.resultlines.append(self.getseperator())
        self.resultlines = self.resultlines[:-1]
        self.resultlines.append(self.getfooter())

    # finally print lines from "resultlines" list, displaying a beautly table
    def printtable(self):
        for line in self.resultlines:
            print(line)

# build table object:
a = Table()

# run class methods necessary to properly build the ascii table
a.buildtable()
a.updatemaxlengthofline()
a.buildresultlines()

# print the resulting table
a.printtable()


class MyQueue:

    def __init__(self):
        """
        Initialize your data structure here.
        """

        #intialising two datastructures here
        #instack - push all the elements until u get into pop() function
        #once u get pop(), pop all elements from instack, and push it in outstack
        #pop the top element (FIFO - Queue)

        self.instack=[]
        self.outstack=[]

    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """

        #just pushing the element
        self.instack.append(x)

    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        #if there are items in outstack, we just pop the element from outstack
        if len(self.outstack)!=0:
            item=self.outstack.pop()
            return item
        else:
        #elements are shifted from instck to outstack, and then popped because,
        #in a stack, the first element will be at the bottom of the stop.
        #to bring it on the top, you have to move all the elements to a  new stack
        #FIFO - QUEUE
            while len(self.instack)!=0:
                self.outstack.append(self.instack.pop())
            item=self.outstack.pop()
            return item

    def peek(self) -> int:
        """
        Get the front element.
        """
        # doing the same as top, but you are just returning the item
        if len(self.outstack)!=0:
            return self.outstack[-1]
        else:
            while len(self.instack)!=0:
                self.outstack.append(self.instack.pop())
            return self.outstack[-1]

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        if len(self.instack)==0 and len(self.outstack)==0:
            return True
        return False

# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()t


import uuid
import random
import types

import pytest
from pymongo import MongoClient
from bson.objectid import ObjectId

from vakt.storage.mongo import MongoStorage, Migration0To1x0x3
from vakt.policy import Policy
from vakt.rules.string import StringEqualRule
from vakt.exceptions import PolicyExistsError, UnknownCheckerType
from vakt.guard import Inquiry
from vakt.checker import StringExactChecker, StringFuzzyChecker, RegexChecker

MONGO_HOST = '127.0.0.1'
MONGO_PORT = 27017
DB_NAME = 'vakt_db'
COLLECTION = 'vakt_policies'

def create_client():
    return MongoClient(MONGO_HOST, MONGO_PORT)

@pytest.mark.integration
class TestMongoStorage:

    @pytest.fixture()
    def st(self):
        client = create_client()
        yield MongoStorage(client, DB_NAME, collection=COLLECTION)
        client[DB_NAME][COLLECTION].remove()
        client.close()

    def test_add(self, st):
        id = str(uuid.uuid4())
        p = Policy(
            uid=id,
            description='foo bar баз',
            subjects=('Edward Rooney', 'Florence Sparrow'),
            actions=['<.*>'],
            resources=['<.*>'],
            rules={
                'secret': StringEqualRule('i-am-a-teacher'),
            },
        )
        st.add(p)
        back = st.get(id)
        assert id == back.uid
        assert 'foo bar баз' == back.description
        assert isinstance(back.rules['secret'], StringEqualRule)

    def test_add_with_bson_object_id(self, st):
        id = str(ObjectId())
        p = Policy(
            uid=id,
            description='foo',
        )
        st.add(p)

        back = st.get(id)
        assert id == back.uid

    def test_policy_create_existing(self, st):
        id = str(uuid.uuid4())
        st.add(Policy(id, description='foo'))
        with pytest.raises(PolicyExistsError):
            st.add(st.add(Policy(id, description='bar')))

    def test_get(self, st):
        st.add(Policy('1'))
        st.add(Policy(2, description='some text'))
        assert isinstance(st.get('1'), Policy)
        assert '1' == st.get('1').uid
        assert 2 == st.get(2).uid
        assert 'some text' == st.get(2).description

    def test_get_nonexistent(self, st):
        assert None is st.get(123456789)

    @pytest.mark.parametrize('limit, offset, result', [
        (500, 0, 200),
        (101, 1, 101),
        (500, 50, 150),
        (200, 0, 200),
        (200, 1, 199),
        (199, 0, 199),
        (200, 50, 150),
        (0, 0, 200),
        (1, 0, 1),
        (5, 4, 5),
    ])
    def test_get_all(self, st, limit, offset, result):
        for i in range(200):
            desc = ''.join(random.choice('abcde') for _ in range(30))
            st.add(Policy(str(i), description=desc))
        policies = list(st.get_all(limit=limit, offset=offset))
        assert result == len(policies)

    def test_get_all_check_policy_properties(self, st):
        p = Policy(
            uid='1',
            description='foo bar баз',
            subjects=('Edward Rooney', 'Florence Sparrow'),
            actions=['<.*>'],
            resources=['<.*>'],
            rules={
                'secret': StringEqualRule('i-am-a-teacher'),
            },
        )
        st.add(p)
        policies = list(st.get_all(100, 0))
        assert 1 == len(policies)
        assert '1' == policies[0].uid
        assert 'foo bar баз' == policies[0].description
        assert ['Edward Rooney', 'Florence Sparrow'] == policies[0].subjects
        assert ['<.*>'] == policies[0].actions
        assert ['<.*>'] == policies[0].resources
        assert isinstance(policies[0].rules['secret'], StringEqualRule)

    def test_get_all_with_incorrect_args(self, st):
        for i in range(10):
            st.add(Policy(str(i), description='foo'))
        with pytest.raises(ValueError) as e:
            list(st.get_all(-1, 9))
        assert "Limit can't be negative" == str(e.value)
        with pytest.raises(ValueError) as e:
            list(st.get_all(0, -3))
        assert "Offset can't be negative" == str(e.value)

    def test_get_all_returns_generator(self, st):
        st.add(Policy('1'))
        st.add(Policy('2'))
        found = st.get_all(500, 0)
        assert isinstance(found, types.GeneratorType)
        l = []
        for p in found:
            l.append(p.uid)
        assert 2 == len(l)

    @pytest.mark.parametrize('checker', [
        None,
        RegexChecker(256),
    ])
    def test_find_for_inquiry_with_regex_or_none_checker_specified_return_all_existing_policies(self, st, checker):
        st.add(Policy('1', subjects=['max', 'bob']))
        st.add(Policy('2', subjects=['sam', 'foo']))
        st.add(Policy('3', subjects=['bar']))
        inquiry = Inquiry(subject='Jim', action='delete', resource='server')
        found = st.find_for_inquiry(inquiry, checker)
        found = list(found)
        assert 3 == len(found)

    def test_find_for_inquiry_with_exact_string_checker(self, st):
        st.add(Policy('1', subjects=['max', 'bob'], actions=['get'], resources=['books', 'comics', 'magazines']))
        st.add(Policy('2', subjects=['maxim'], actions=['get'], resources=['books', 'comics', 'magazines']))
        st.add(Policy('3', subjects=['sam', 'nina']))
        inquiry = Inquiry(subject='max', action='get', resource='books')
        found = st.find_for_inquiry(inquiry, StringExactChecker())
        found = list(found)
        assert 1 == len(found)
        assert '1' == found[0].uid

    def test_find_for_inquiry_with_fuzzy_string_checker(self, st):
        st.add(Policy('1', subjects=['max', 'bob'], actions=['get'], resources=['books', 'comics', 'magazines']))
        st.add(Policy('2', subjects=['maxim'], actions=['get'], resources=['books', 'foos']))
        st.add(Policy('3', subjects=['Max'], actions=['get'], resources=['books', 'comics']))
        st.add(Policy('4', subjects=['sam', 'nina']))
        inquiry = Inquiry(subject='max', action='et', resource='oo')
        found = st.find_for_inquiry(inquiry, StringFuzzyChecker())
        found = list(found)
        assert 2 == len(found)
        ids = [found[0].uid, found[1].uid]
        assert '1' in ids
        assert '2' in ids
        inquiry = Inquiry(subject='Max', action='get', resource='comics')
        found = st.find_for_inquiry(inquiry, StringFuzzyChecker())
        found = list(found)
        assert 1 == len(found)
        assert '3' == found[0].uid

    def test_find_for_inquiry_returns_generator(self, st):
        st.add(Policy('1', subjects=['max', 'bob'], actions=['get'], resources=['comics']))
        st.add(Policy('2', subjects=['max', 'bob'], actions=['get'], resources=['comics']))
        inquiry = Inquiry(subject='max', action='get', resource='comics')
        found = st.find_for_inquiry(inquiry)
        assert isinstance(found, types.GeneratorType)
        l = []
        for p in found:
            l.append(p.uid)
        assert 2 == len(l)

    def test_find_for_inquiry_with_unknown_checker(self, st):
        st.add(Policy('1'))
        inquiry = Inquiry(subject='sam', action='get', resource='books')
        with pytest.raises(UnknownCheckerType):
            list(st.find_for_inquiry(inquiry, Inquiry()))

    def test_update(self, st):
        id = str(uuid.uuid4())
        policy = Policy(id)
        st.add(policy)
        assert id == st.get(id).uid
        assert None is st.get(id).description
        assert [] == st.get(id).actions
        policy.description = 'foo'
        policy.actions = ['a', 'b', 'c']
        st.update(policy)
        assert id == st.get(id).uid
        assert 'foo' == st.get(id).description
        assert ['a', 'b', 'c'] == st.get(id).actions

    def test_update_non_existing_does_not_create_anything(self, st):
        id = str(uuid.uuid4())
        st.update(Policy(id, actions=['get'], description='bar'))
        assert st.get(id) is None

    def test_delete(self, st):
        policy = Policy('1')
        st.add(policy)
        assert '1' == st.get('1').uid
        st.delete('1')
        assert None is st.get('1')

    def test_delete_nonexistent(self, st):
        uid = str(ObjectId())
        st.delete(uid)
        assert None is st.get(uid)

    def test_returned_condition(self, st):
        uid = str(uuid.uuid4())
        p = Policy(
            uid=uid,
            rules={
                'secret': StringEqualRule('i-am-a-teacher'),
                'secret2': StringEqualRule('i-am-a-husband'),

            },
        )
        st.add(p)
        rules = st.get(uid).rules
        assert rules['secret'].satisfied('i-am-a-teacher')
        assert rules['secret2'].satisfied('i-am-a-husband')

@pytest.mark.integration
class TestMigration0To1x0x3:

    @pytest.fixture()
    def migration(self):
        client = create_client()
        storage = MongoStorage(client, DB_NAME, collection=COLLECTION)
        yield Migration0To1x0x3(storage)
        client[DB_NAME][COLLECTION].remove()
        client.close()

    def test_order(self, migration):
        assert 1 == migration.order

    def test_has_access_to_storage(self, migration):
        assert hasattr(migration, 'storage') and migration.storage is not None

    def test_up(self, migration):
        migration.up()
        created_indices = [i['name'] for i in migration.storage.collection.list_indexes()]
        assert created_indices == ['_id_', 'actions_idx', 'subjects_idx', 'resources_idx']

    def test_down(self, migration):
        migration.down()
        left_indices = [i['name'] for i in migration.storage.collection.list_indexes()]
        assert left_indices == ['_id_']


"""Controller implementations for item menu."""
from app.config import Stripe, fb_db

__all__ = ('MenuController',)

class MenuController:
    """Controller for menu items."""

    @staticmethod
    def get_all_menu_items():
        """
        Get all menu items.

        Returns ``None`` if any exceptions occur during the fetch.
        """
        final_menu = {'menu': []}
        col_ref = fb_db.collection('Menu')

        doc_ref = col_ref.get()
        for doc in doc_ref:
            final_menu['menu'].append(doc.to_dict())

        return final_menu

    @staticmethod
    def get_menu_item_by_id(item_id):
        """
        Get a menu item by its ``item_id``.

        Returns ``None`` if any exceptions occur during the fetch.
        """
        doc_ref = fb_db.collection('Menu').document(item_id)

        doc = doc_ref.get()
        if not doc.exists:
            raise Exception('Menu item does not exist!')

        return doc.to_dict()

    @staticmethod
    def create_menu_item(menu_item):
        """Create a new menu item."""
        # registers a new product
        new_product = Stripe.Product.create(
            name=menu_item.item_name,
            type='good',
            description=menu_item.item_desc,
            images=[menu_item.img],
            metadata={
                'Type': menu_item.item_type
            }
        )
        # registers a new price and binds it to the new product made above
        Stripe.Price.create(
            unit_amount=int(menu_item.item_price * 100),
            currency='usd',
            product=new_product['id'])

        new_menu_item = {
            'name': menu_item.item_name,
            'description': menu_item.item_desc,
            'price': menu_item.item_price,
            'item_id': new_product['id'],
            'type': menu_item.item_type,
            'img': menu_item.img,
            'in_stock': menu_item.in_stock
        }

        fb_db.collection('Menu').document(new_product['id']).set(new_menu_item)

        return new_product['id']

    @staticmethod
    def delete_menu_item(item_id):
        """Delete a menu item at ``item_id``."""
        fb_db.collection('Menu').document(item_id).delete()
        Stripe.Product.modify(item_id, active='false')
        return item_id

    @staticmethod
    def get_product_price_id(item_id):
        """Get the product price by ``item_id``."""
        price_data = Stripe.Price.list(product=item_id)
        price_id = price_data['data'][0]['id']
        return price_id

    @staticmethod
    def update_stripe_price(item_id, updated_price):
        """Update the price of ``item_id`` to ``updated_price`` on stripe."""
        target_price = int(updated_price * 100)
        price_list = Stripe.Price.list(product=item_id)
        data = price_list['data']

        price_exists = False

        # check if there is already a matching price
        for price_obj in data:
            if price_obj['unit_amount'] == target_price:
                price_exists = True
                if not price_obj['active']:
                    Stripe.Price.modify(price_obj['id'], active=True)
            else:
                if price_obj['active']:
                    Stripe.Price.modify(price_obj['id'], active=False)

        if not price_exists:
            Stripe.Price.create(
                unit_amount=int(updated_price * 100),
                currency='usd',
                product=item_id,
                active=True)  # new_price

    @classmethod
    def update_menu_item(cls, item_id, properties):
        """
        Updates a menu item based on what the user wants to change.

        A user is only permitted to change the following properties
        of a given menu item:
        - name
        - desc
        - price
        - in_stock
        - type
        - img
        """
        doc_ref = fb_db.collection('Menu')
        doc = doc_ref.document(item_id)

        doc.update(properties)

        for prop in properties:
            if prop == 'price':
                cls.update_stripe_price(item_id, properties[prop])
            if prop == 'description':
                Stripe.Product.modify(item_id, description=properties[prop])
            if prop == 'in_stock':
                Stripe.Product.modify(item_id, active=properties[prop])
            if prop == 'img':
                images = [properties[prop]]
                Stripe.Product.modify(item_id, images=images)
            if prop == 'type':
                Stripe.Product.modify(item_id, metadata={'Type': properties[prop]})
            if prop == 'name':
                Stripe.Product.modify(item_id, name=properties[prop])

        return item_id


from django.db import models

"""
	Le model qui permet de creer les salles
"""
class Room(models.Model):
	appelation = models.CharField(max_length=200)
	notes = models.TextField(u'Commentaires')

	def __str__(self):
		return self.appelation

	class Meta:
		verbose_name = u'Room'

"""
	Le model qui permet de creer les salles
"""
class Motif(models.Model):
	appelation = models.CharField(max_length=200)
	notes = models.TextField(u'Commentaires')

	def __str__(self):
		return self.appelation

	class Meta:
		verbose_name = u'Motif'

"""
	Ce model contient l'evenement en lui meme (la reservation)
"""
class Event(models.Model):
	day_start = models.DateField(u'Jour debut (jj/mm/aaaa)')
	time_start = models.TimeField(u'Heure de debut (hh:mm)')
	day_end = models.DateField(u'Jour fin (jj/mm/aaaa)')
	time_end = models.TimeField(u'Heure de fin (hh:mm)')
	room_id = models.ForeignKey(Room, on_delete=models.CASCADE)
	visio = models.BooleanField(default=True)
	motif_id = models.ForeignKey(Motif, on_delete=models.CASCADE)
	notes = models.TextField(u'Theme / Commentaires')

	def __str__(self):
		return self.notes

	class Meta:
		verbose_name = u'Event'

	def check_overlap(self,fixed_start, fixed_end, new_start, new_end):
			overlap = False
			if new_start == fixed_end or new_end == fixed_start:
				overlap = False
			elif (new_start >= fixed_start and new_start <= fixed_end) or (new_end >= fixed_start and new_end <= fixed_end):
				overlap = True
			elif new_start <= fixed_start and new_end >= fixed_end:
				overlap = True
			return overlap

	def clean(self):
		if self.day_end <= self.day_start:
			raise ValidationError ('Ending time must be after starting time')

		# events = Event.objects.filter(day=self.day)
		# if events.exists():
		# 	for event in events:
		# 		if self.check_overlap(event.start_time, event.end_time, self.start_time, self.end_time):
		# 			raise ValidationError ('There is an overlap with another event: ' + str(event.day) + ', '+ str(event.start_time) + '-' + str(event.end_time))


#!/usr/bin/env python

""" MultiQC module to parse output from QualiMap """

from __future__ import print_function
from collections import OrderedDict
import io
import logging
import os

from collections import defaultdict

from multiqc import config, BaseMultiqcModule

# Initialise the logger
log = logging.getLogger(__name__)

class MultiqcModule(BaseMultiqcModule):

    def __init__(self):

        # Initialise the parent object
        super(MultiqcModule, self).__init__(name='QualiMap', anchor='qualimap',
        href="http://qualimap.bioinfo.cipf.es/",
        info="is a platform-independent application to facilitate the quality"\
        " control of alignment sequencing data and its derivatives like"\
        " feature counts.")

        self.parsed_stats = defaultdict(dict)

        # Find QualiMap reports
        qualimap_raw_data = {}
        for directory in config.analysis_dir:
            for root, dirnames, filenames in os.walk(directory, followlinks=True):
                raw_data_dir = 'raw_data'
                for d in dirnames:
                    if raw_data_dir in d:
                        raw_data_dir = d
                if 'genome_results.txt' in filenames and raw_data_dir in dirnames:
                    with io.open(os.path.join(root, 'genome_results.txt'), 'r') as gr:
                        for l in gr:
                            if 'bam file' in l:
                                s_name = self.clean_s_name(os.path.basename(l.split(' = ')[-1]), root)

                    s_name = self.clean_s_name(s_name, root)
                    if s_name in qualimap_raw_data:
                        log.debug("Duplicate sample name found! Overwriting: {}".format(s_name))

                    qualimap_raw_data[s_name] = {}
                    qualimap_raw_data[s_name]['reports'] = {os.path.splitext(r)[0]: os.path.join(root, raw_data_dir, r) \
                        for r in os.listdir(os.path.join(root, raw_data_dir))}

        if len(qualimap_raw_data) == 0:
            log.debug("Could not find any reports in {}".format(config.analysis_dir))
            raise UserWarning

        log.info("Found {} reports".format(len(qualimap_raw_data)))

        self.sections = list()

        # Section 1 - Coverage Histogram
        histogram_data = self.qualimap_cov_his(qualimap_raw_data)
        if len(histogram_data) > 0:

            # Chew back on histogram to prevent long flat tail
            # (find a sensible max x - lose 1% of longest tail)
            max_x = 0
            for d in histogram_data.values():
                total = sum(d.values())
                cumulative = 0
                for count in sorted(d.keys(), reverse=True):
                    cumulative += d[count]
                    if cumulative / total > 0.01:
                        max_x = max(max_x, count)
                        break

            self.sections.append({
                'name': 'Coverage Histogram',
                'anchor': 'qualimap-coverage-histogram',
                'content': self.plot_xy_data(histogram_data, {
                    'title': 'Coverage Histogram',
                    'ylab': 'Genome Bin Counts',
                    'xlab': 'Coverage (X)',
                    'ymin': 0,
                    'xmin': 0,
                    'xmax': max_x,
                    'xDecimals': False,
                    'tt_label': '{point.x}X: {point.y}',
                })
            })

        # Section 2 - Insert size histogram
        histogram_data = self.qualimap_ins_size_his(qualimap_raw_data)
        if len(histogram_data) > 0:
            self.sections.append({
                'name': 'Insert size Histogram',
                'anchor': 'qualimap-insert-size-histogram',
                'content': self.plot_xy_data(histogram_data, {
                    'title': 'Insert Size Histogram',
                    'ylab': 'Fraction of reads',
                    'xlab': 'Insert Size (bp)',
                    'ymin': 0,
                    'xmin': 0,
                    'tt_label': '{point.x} bp: {point.y}',
                })
            })

        # Section 3 - Genome Fraction coverage
        histogram_data = self.qualimap_gen_frac_his(qualimap_raw_data)
        if len(histogram_data) > 0:
            self.sections.append({
                'name': 'Genome Fraction Coverage',
                'anchor': 'qualimap-genome-fraction-coverage',
                'content': self.plot_xy_data(histogram_data, {
                    'title': 'Genome Fraction Coverage',
                    'ylab': 'Fraction of reference (%)',
                    'xlab': 'Coverage (X)',
                    'ymax': 100,
                    'ymin': 0,
                    'xmin': 0,
                    'tt_label': '{point.x}X: {point.y:.2f}%',
                })
            })

            # Section 4 - GC-content distribution
            histogram_data = self.qualimap_gc_distribution(qualimap_raw_data)
            if len(histogram_data) > 0:
                self.sections.append({
                    'name': 'GC-content distribution',
                    'anchor': 'qualimap-gc-distribution',
                    'content': self.plot_xy_data(histogram_data, {
                        'title': 'GC-content distribution',
                        'ylab': 'Fraction of reads',
                        'xlab': 'GC content (%)',
                        'ymin': 0,
                        'xmin': 0,
                        'xmax': 100,
                        'tt_label': '{point.x}%: {point.y:.3f}',
                    })
                })

        # General stats table
        self.qualimap_stats_table()

    def qualimap_gc_distribution(self, qualimap_raw_data):
        parsed_data = {}
        for sn, data in qualimap_raw_data.items():
            gc_report = data['reports']['mapped_reads_gc-content_distribution']
            if gc_report:
                counts={}
                avg_gc = 0
                with io.open(gc_report, 'r') as fh:
                    next(fh)
                    for l in fh:
                        sections = l.split(None, 2)
                        gc = int(round(float(sections[0])))
                        cont = float(sections[1])
                        avg_gc += gc*cont
                        counts[gc] = cont

                parsed_data[sn] = counts

                #Add reads avg. GC to the general stats table
                self.parsed_stats[sn]['avg_gc'] = avg_gc

        return parsed_data

    def qualimap_cov_his(self, qualimap_raw_data):
        parsed_data = {}
        for sn, data in qualimap_raw_data.items():
            cov_report = data['reports'].get('coverage_histogram')
            if cov_report:
                counts={}
                with io.open(cov_report, 'r') as fh:
                    next(fh)
                    for l in fh:
                        coverage, count = l.split(None, 1)
                        coverage = int(round(float(coverage)))
                        count = float(count)
                        counts[coverage] = count

                parsed_data[sn] = counts

                # Find median
                num_counts = sum(counts.values())
                cum_counts = 0
                median_coverage = None
                for thiscov, thiscount in counts.items():
                    cum_counts += thiscount
                    if cum_counts >= num_counts/2:
                        median_coverage = thiscov
                        break

                # Add median to the general stats table
                self.parsed_stats[sn]['median_coverage'] = median_coverage

        return parsed_data

    def qualimap_ins_size_his(self, qualimap_raw_data):
        parsed_data = {}
        for sn, data in qualimap_raw_data.items():
            ins_size = data['reports'].get('insert_size_histogram')
            if ins_size:
                counts = {}
                zero_insertsize = 0
                with io.open(ins_size, 'r') as fh:
                    next(fh)
                    for l in fh:
                        insertsize, count = l.split(None, 1)
                        insertsize = int(round(float(insertsize)))
                        count = float(count) / 1000000
                        if(insertsize == 0):
                            zero_insertsize = count
                        else:
                            counts[insertsize] = count

                parsed_data[sn] = counts

                # Find median
                num_counts = sum(counts.values())
                cum_counts = 0
                median_insert_size = None
                for thisins, thiscount in counts.items():
                    cum_counts += thiscount
                    if cum_counts >= num_counts/2:
                        median_insert_size = thisins
                        break

                # Add the median insert size to the general stats table
                self.parsed_stats[sn]['median_insert_size'] = median_insert_size

        return parsed_data

    def qualimap_gen_frac_his(self, qualimap_raw_data):
        parsed_data = {}
        for sn, data in qualimap_raw_data.items():
            frac_cov = data['reports'].get('genome_fraction_coverage')
            if frac_cov:
                thirty_x_pc = 100
                max_obs_x = 0
                halfway_cov = None
                counts={}
                with io.open(frac_cov, 'r') as fh:
                    next(fh)
                    for l in fh:
                        coverage, percentage = l.split(None, 1)
                        coverage = int(round(float(coverage)))
                        percentage = float(percentage)
                        counts[coverage] = percentage

                        if coverage <= 30 and thirty_x_pc > percentage:
                            thirty_x_pc = percentage

                parsed_data[sn] = counts

                # Add the median % genome >= 30X coverage to the general stats table
                self.parsed_stats[sn]['thirty_x_pc'] = thirty_x_pc

        return parsed_data

    def qualimap_stats_table(self):
        """ Take the parsed stats from the QualiMap report and add them to the
        basic stats table at the top of the report """

        headers = OrderedDict()
        headers['median_coverage'] = {
            'title': 'Coverage',
            'description': 'Median coverage',
            'min': 0,
            'scale': 'RdBu'
        }
        headers['median_insert_size'] = {
            'title': 'Insert Size',
            'description': 'Median Insert Size',
            'min': 0,
            'scale': 'PuOr',
            'format': '{:.0f}'
        }
        headers['thirty_x_pc'] = {
            'title': '≥ 30X',
            'description': 'Fraction of genome with at least 30X coverage',
            'max': 100,
            'min': 0,
            'scale': 'RdYlGn',
            'format': '{:.1f}%'
        }
        headers['avg_gc'] = {
            'title': 'Avg. GC',
            'description': 'Average GC content',
            'max': 80,
            'min': 20,
            'scale': 'BrBG',
            'format': '{:.0f}%'
        }
        self.general_stats_addcols(self.parsed_stats, headers)