"""

       task1_3_starter.py   -   A simple grep utility

    Use the following command-line syntax run running this script:

        python task1_3_starter.py wordexpression directory

        Example:  python task1_3_starter.py print .

        (Note: within Pycharm, select Run > Edit Configurations and add wordexpression and directory
               into the parameters section)

    Tips to help you out:
    1. Obtain the command-line arguments.  There are two: wordexpression and directory.
       This step has been taken care of for you in the starter code below.

       You should use os.listdir() to get a list of files from the specified directory.
       Iterate over the list of filenames and verify if they are all files.
       Hint: you can use os.path.isfile() to check for valid files.
       Note: another helpful tool is os.path.join(directory_name, filename).

       Use the append() method to add files to the provided file_list variable.

    2. Opening/reading/writing/closing files will be discussed further later,
       so for now, you have been shown how to read a line from a file:

            for line in open(filename, encoding='utf8'):
                # process line from file

        Overall your steps should be something like this:
            - Iterate over the file_list.
            - Open each file (from the list) one at a time
            - Read each line in, check for the presence of the wordexpression
                Hint: Use .find() for this
            - Output a result if there is a match

    Note: This exercise works only with text files.  Non-text files (binary
          files) will raise a UnicodeDecodeError (which can be handled, but this
          hasn't been discussed yet).
"""

import sys
import os

args = sys.argv                                                 # get command-line args

if len(args) != 3:
    print('Insufficient arguments provided. Syntax:')
    print('python task1_3.py wordexpression directory')
    sys.exit(42)

# put your solution here
wordexpression = args[1]
directory = args[2]
file_list = []


# 1244_fail2.py
import sys
sys.stdin = open('1244input.txt', 'r')
def getNum():
    val = ''
    for i in range(M):
        val += NUM[i]
    return int(val)

def switch(k): # k번째 교환
    global MAX
    if k == N:
        tmp = getNum()
        MAX = max(MAX, tmp)
        return

    for i in range(M - 1):
        for j in range(i + 1, M):
            NUM[i], NUM[j] = NUM[j], NUM[i]
            tmp = getNum()
            if visit[k][tmp]:
                NUM[i], NUM[j] = NUM[j], NUM[i]
                return # 다음 for문이 돌아가지 않음!
            visit[k][tmp] = True
            switch(k + 1)
            NUM[i], NUM[j] = NUM[j], NUM[i]

t = int(input())
for tc in range(1, t + 1):
    inputs, N = input().split()
    NUM = list(inputs)
    N = int(N)
    M = len(NUM)
    MAX = 0
    visit = [[False] * 1000000 for _ in range(N)]
    switch(0)
    print('#{} {}'.format(tc, MAX))


import os
from datetime import datetime

# `__file__` is relative to the current working directory
# `os.chdir()` can change current working directory, but don't change `__file__`
# so if `os.chdir()` is used, this method fails to get current directory
current_dir = os.path.dirname(os.path.realpath(__file__))
COOKIES = open(os.path.join(current_dir, 'cookie.txt'), 'r').readline().strip()

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

# 保存文件粒度
GRANULARITY_HOUR = False

# 强制爬取
FORCE_CRAWL = False

# 汇率
DOLLAR_TO_CNY = 7.0
# steam视频售卖税率
STEAM_SELL_TAX = 0.13

# filter
# 爬取历史价格的话，每个都要单独爬一次，爬取量翻了好几十倍，所以扔掉一些，只爬取某个价格区间内的饰品……
# 大致价格分位点：0 - 10000; 20 - 5000; 50 - 4000; 100 - 3400; 200 - 3000; 500 - 2200; 1000 - 1200
CRAWL_MIN_PRICE_ITEM = 100
CRAWL_MAX_PRICE_ITEM = 300
# buff最低价门槛，低于该价格的条目忽略（要不然统计出来很多封装的涂鸦……）
MIN_PRICE_THRESHOLD = 100
# 交易记录阈值，冷门物品不在考虑范围
MIN_SOLD_THRESHOLD = 10
# 太大都是steam定价虚高，实际卖不掉的……有了steam求购价之后就不需要这个了
MAX_GAP_PERCENTAGE = 0.8
# 每一项指标的输出个数
TOP_N = 50

# 文件
DATE_DAY = str(datetime.now().strftime('%Y-%m-%d'))
DATE_HOUR = str(datetime.now().strftime('%Y-%m-%d-%H'))
OUTPUT_PATH = "output"
OUTPUT_NAME_HOUR = "csgo_skins_" + DATE_HOUR
OUTPUT_NAME_DAY = "csgo_skins_" + DATE_DAY
OUTPUT_NAME = OUTPUT_NAME_HOUR if GRANULARITY_HOUR else OUTPUT_NAME_DAY

OUTPUT_FILE_NAME = os.path.join(
    OUTPUT_PATH, OUTPUT_NAME + ".csv"
)
OUTPUT_FILE_NAME_DAY = os.path.join(
    OUTPUT_PATH, OUTPUT_NAME_DAY + ".csv"
)


#!/usr/bin/python
# Synthetic Photomotery
import warnings, glob, os, pickle, numpy as np, astropy.units as q, astropy.constants as ac, matplotlib.pyplot as plt
from SEDkit import utilities as u
from itertools import combinations, chain, groupby

warnings.simplefilter('ignore')
RSR = u.get_filters()

def vega(bbody=False):
    '''
    Returns the wavelength [um] and energy flux in [erg/s/cm2/A] calibrated to 10pc for Vega (
    http://www.stsci.edu/hst/observatory/cdbs/calspec.html)
    '''
    pi = 130.23
    w, f = np.genfromtxt(os.path.dirname(u.__file__)+'/Models/Vega/STSci_Vega.txt')
    w, f = (w * q.AA).to(q.um), f * q.erg / q.s / q.cm ** 2 / q.AA
    return [w, f, u.blackbody(w, 9610, Flam=False, radius=27.3)] if bbody else [w, f]

def get_eff(band, wave, flux, photon=True, calculate=False):
    '''
    Calculates the filter effective wavelength for a given *band*, *wave* and *flux*. If *wave* is out of range (
    uncalculable), returns the given value.
    '''
    filt = RSR[band]
    if (wave[0] < filt['wav'][0]) and (wave[-1] > filt['wav'][-1]) and calculate:
        I = np.interp(wave.value, filt['wav'].value, filt['rsr'].value, left=0, right=0)
        return (np.trapz(wave * flux * I * (wave / (ac.h * ac.c) if photon else 1), x=wave) / np.trapz(
            flux * I * (wave / (ac.h * ac.c) if photon else 1), x=wave)).to(q.um)
    else:
        return filt['eff']

def get_zp(band, photon=True):
    '''
    Calculates the zero point flux density for a given *band* using a flux calibrated Vega SED [erg/s/cm2/A]
    '''
    # return (np.trapz((u.rebin_spec(vega(), RSR[band]['wav'])[1]*RSR[band]['rsr']*((RSR[band]['wav']/(
    # ac.h*ac.c)).to(1/q.erg) if photon else 1)).to((1 if photon else q.erg)/q.s/q.cm**2/q.AA), x=RSR[band][
    # 'wav'])*q.um*(1 if photon else q.erg)/q.s/q.cm**2/(np.trapz(RSR[band]['rsr'], x=RSR[band]['wav'])*q.um)).to((1
    # if photon else q.erg)/q.s/q.cm**2)
    return (np.trapz((u.rebin_spec(vega(), RSR[band]['wav'])[1] * RSR[band]['rsr'] * (
    (RSR[band]['wav'] / (ac.h * ac.c)).to(1 / q.erg) if photon else 1)).to(
        (1 if photon else q.erg) / q.s / q.cm ** 2 / q.AA), x=RSR[band]['wav']) / np.trapz(RSR[band]['rsr'],
                                                                                x=RSR[band]['wav'])).to(
        (1 if photon else q.erg) / q.s / q.cm ** 2 / q.AA)

def get_mag(band, spectrum, exclude=[], photon=True, plot=False, to_flux=False, Flam=False, eff=False):
    '''
    Returns the magnitude in *band* for given *wave* in [um] and *flux* in [erg/s/cm2/A] with *airmass* corrections
    if provided. If uncertainty is provided, returns [mag, mag_uncertainty] instead.
    '''
    spectrum, filt = u.scrub(spectrum), RSR.get(band)
    if len(spectrum) == 2:
        spectrum += ['']
    if filt and np.logical_and(filt['max'] < spectrum[0][-1], filt['min'] > spectrum[0][0]) and all(
            [np.logical_or(all([i < filt['min'].value for i in rng]), all([i > filt['max'].value for i in rng])) for rng
             in exclude]):
        # Calculate synthetic magnitude
        f, sig_f = u.rebin_spec(spectrum, filt['wav'])[1:] if spectrum[2] is not '' else [
            u.rebin_spec(spectrum, filt['wav'])[1], '']
        F = (np.trapz((f * filt['rsr'] * ((filt['wav'] / (ac.h * ac.c)).to(1 / q.erg) if photon else 1)).to(
            (1 if photon else q.erg) / q.s / q.cm ** 2 / q.AA), x=filt['wav']) / (
             np.trapz(filt['rsr'], x=filt['wav']))).to(
            (1 if photon else q.erg) / q.s / q.cm ** 2 / (1 if Flam else q.AA))
        sig_F = np.sqrt(np.sum(((sig_f * filt['rsr'] * np.gradient(filt['wav']).value * (
        (filt['wav'] / (ac.h * ac.c)).to(1 / q.erg) if photon else 1)) ** 2).to(
            (1 if photon else q.erg) ** 2 / q.s ** 2 / q.cm ** 4 / (1 if Flam else q.AA ** 2)))) if sig_f else ''

        # Calculate effective wavelength
        w = get_eff(band, spectrum[0], spectrum[1], photon=photon, calculate=eff)

        return (u.flux2mag(band, F, sig_f=sig_F, filter_dict=RSR) if not to_flux else [F, sig_F]) + [w]

    else:
        return ['', '', '']

def all_mags(spectrum, bands=RSR.keys(), photon=True, to_flux=False, Flam=True, exclude=[], eff=False, to_list=False):
    magDict, magList = {}, []
    for band in bands:
        M = get_mag(band, spectrum, photon=photon, to_flux=to_flux, Flam=Flam, exclude=exclude, eff=eff)
        if M[0]:
            magDict[band], magDict[band + '_unc'], magDict[band + '_eff'] = M
            magList.append(M)
    return sorted(magList, key=lambda x: x[-1]) if to_list else magDict


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import pandas as pd
from pandas import DataFrame
import json

data1 = pd.read_json("categorized-comments.jsonl", lines = True)

import re
import nltk
nltk.download('stopwords')
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords

data1.loc[data1["cat"]=="sports", "cat"]=0
data1.loc[data1["cat"]=="science_and_technology", "cat"]=1
data1.loc[data1["cat"]=="video_games", "cat"]=2
data1.loc[data1["cat"]=="news", "cat"]=3

data1.cat.astype(int).head()

data1.dtypes

data1.head()

ps = PorterStemmer()

# =============================================================================
# corpus = []
# for i in range(0, 2347476, n_jobs = -1):
#         txt = re.sub('[^a-zA-Z]', " ", data1["txt"][0])
#         txt = txt.lower()
#         txt = txt.split()
#         txt = [ps.stem(word) for word in txt if not word in set(stopwords.words("english"))]
#         txt = " ".join(txt)
#         corpus.append(txt)
#
# =============================================================================
# =============================================================================
# data1.info(memory_usage = "deep")
#
# X = data1.iloc[:1].values
# y = data1.iloc[:0].values
# =============================================================================

# =============================================================================
# X = data1.txt
# y = data1.cat
#
# from sklearn.cross_validation import train_test_split
#
# X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.20, random_state = 1)
#
# from sklearn.feature_extraction.text import CountVectorizer
#
# vect = CountVectorizer(max_features = 1000)
# vect.fit(X_train)
# x_train_dtm = vect.transform(X_train).toarray()
#
# d = x_train_dtm[0:5, 1:10]
#
# from sklearn.linear_model import LogisticRegressionCV
#
# classifier = LogisticRegressionCV(n_jobs = -1, random_state = 0, penalty = "l2")
# classifier.fit(x_train_dtm, y_train.astype("int"))
# =============================================================================

data_0 = data1[data1["cat"]==0].iloc[0:2000]
data_1 = data1[data1["cat"]==1].iloc[0:2000]
data_2 = data1[data1["cat"]==2].iloc[0:2000]
data_3 = data1[data1["cat"]==3].iloc[0:2000]

data_condensed = pd.concat([data_0, data_1], axis=0)
data_condensed = pd.concat([data_condensed, data_2], axis=0)
data_condensed = pd.concat([data_condensed, data_3], axis=0)

corpus = []

for i in range(0, 8000):
        txt = re.sub('[^a-zA-Z]', " ", data1["txt"][i])
        txt = txt.lower()
        txt = txt.split()
        txt = [ps.stem(word) for word in txt if not word in set(stopwords.words("english"))]
        txt = " ".join(txt)
        corpus.append(txt)

from sklearn.feature_extraction.text import CountVectorizer

cv = CountVectorizer(max_features = 5000)
X = cv.fit_transform(corpus).toarray()
y = data_condensed.iloc[:, 0].values

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.25, random_state = 1)

from sklearn.linear_model import LogisticRegressionCV

classifier = LogisticRegressionCV(n_jobs = -1, random_state = 0, penalty = "l2")
classifier.fit(X_train, y_train.astype("int"))

test_predictions = classifier.predict(X_test)
train_predictions = classifier.predict(X_train)

from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, test_predictions)

accuarcy_test = accuracy_score(y_test, test_predictions)
accuarcy_train = accuracy_score(y_train, train_predictions)


'''
n stations.
A finding path is one that merges all starting points into one destination
We don't need to find an optimal finding path
We can instead merge the starting points pairwise.
If each pair of starting points can be merged by a path, then they
can all be merged by some combination of those paths, which will therefore
itself be a finding path.

Keep a cache of mergeable starting points:
    initialize cache[n1][n2] = -1 for all n1, n2 < n
    set cache[s1][s2] = s3 if there is a known path
        that merges routes starting at s1, s2
        symmetircal about diagonal
        0, 1, 2, 3 along diagonal

If the cache can be filled (all entries > -1)
then a finding path exists.

We search for pairwise mergers using DFS wiht the help of the cache.
If the merger exists, we add it to the cache.
If it doesn't there is no finding path.

It remains to deal with the closed stations
The question is, can we modify the cache left over from the first search
so that it can serve as a ((nearly?) complete?) cache for the subsequent searches.
I don't know, so we just have to start with a new cache each time.
'''

def apply_path(subway, path, station):
    for direction in path:
        station = subway[station][direction]
    return station

def algorithm(subway, n, m):
    cache = [[-1 for x in range(n)] for y in range(n)]
    for i in range(n):
        cache[i][i] = i
    #we will only maintain cache[s1][s2] if s1 <= s2,
    #since the cache is symmetrical about the diagonal

    #do DFS search for remaining gaps in the cache
    while True:
        s1, s2 = get_cache_miss(cache, n)
        if s1 > -1: #there are still gaps in cache
            if not dfs(subway, cache, n, m, s1, s2):
                #dfs updates the cache
                return -2
        else:   #cache is complete
            return -1

def modify_subway(subway, n, m, s):
    modified_subway = []
    for station in range(n):
        if station == s:
            continue
        modified_station = []
        for direction in range(m):
            if subway[station][direction] == s:
                if subway[s][direction] == s:
                    destination = station
                else:
                    destination = subway[s][direction]
            else:
                destination = subway[station][direction]
            if destination > s:
                destination -= 1
            modified_station.append(destination)
        modified_subway.append(modified_station)
    return modified_subway

def answer(subway):
    n = len(subway) #number of stations
    m = len(subway[0]) #number of directions
    if algorithm(subway, n, m) == -1:
        print -1
        return -1
    for s in range(n):
        if algorithm(modify_subway(subway, n, m, s), n - 1, m) == -1:
            return s
    return -2

def get_cache_miss(cache, n):
    #find the first cache miss (above diagonal)
    for i in range(n - 1):
        for j in range(i + 1, n):
            if cache[i][j] == -1:
                return i, j
    return -1, -1

def dfs(subway, cache, n, m, s1, s2):
    #attempt to find a path that merges s1 and s2.
    #If the path generates a circuit, we know that
    #EITHER it is a finding path but a shorter one
    #exists that we haven't explored yet,
    #OR it cannot be extended to a finding path.
    #We back up in either case
    prior_s1s2 = [(s1, s2)]
    path = [0] #path and prior_s1s2 same length
    while path:
        s1, s2 = subway[s1][path[-1]], subway[s2][path[-1]]
        s1, s2 = min(s1, s2), max(s1, s2)
        c = cache[s1][s2]
        if c > -1:
            #hit: all prior_s1s2 pairs can be added to the cache
            #this also captures hits on the cache diagonal
            for (s1, s2) in prior_s1s2:
                cache[s1][s2] = c
            return True
        if (s1, s2) in prior_s1s2:
            #need to back up
            while path and path[-1] == m - 1:
                path.pop()
                (s1, s2) = prior_s1s2.pop()
                #path and prior_s1s2 same length
            if path: #if we haven't backed up to the empty path
                path[-1] += 1
        else: #path ok so far, keep digging
            prior_s1s2.append((s1, s2))
            path.append(0)
            #path and prior_s1s2 same length
    #there is no path that merges the original s1 and s2
    #this means that there is no finding path for this subway.
    return False


def stats_text_cn(text):    #定义检索中文函数
    if type(text)!=str:
        raise ValueError('输入参数非字符串')
    dict={}
    for i in text:
        if u'\u4e00' <= i <= u'\u9fa5': #提取中文汉字，\u是unincode编码，u4e00是十六进制表达值
            dict[i]=text.count(i)
        list2= sorted(dict.items(),key=lambda x:x[1],reverse=True) #降序排列
    print(list2)
    return

def stats_text_en(text):#定义检索英文函数
    t = text.replace(',',' ').replace('.',' ').replace('-',' ').replace('"',' ').replace('!',' ').replace('?',' ') #替换标点为空格
    if type(text)!=str:
        raise ValueError('输入参数非字符串')
    list = ''   #存储英文单词的字符串

    for s in t:  # 只保留英文单词
        if s.isascii():
            list += s

    t1= list.split()

    dict = {}#统计单词出现次数
    for i in t1:
        dict[i] = dict.get(i, 0) + 1

    list1= sorted(dict.items(),key=lambda x:x[1],reverse=True) #降序排列
    print(list1)
    return

def stats_text(text):
    if type(text)!=str:
        raise ValueError('输入参数非字符串')
    stats_text_cn(text)
    stats_text_en(text)
    return


# make_scala_binary, etc are customizable rule macros. We can add or modify the definitions by adding/swapping phases
load(
    "@io_bazel_rules_scala//scala:advanced_usage/scala.bzl",
    _make_scala_binary = "make_scala_binary",
    _make_scala_library = "make_scala_library",
    _make_scala_test = "make_scala_test",
)

# ScalaRulePhase lets us provide phases to the customizable rules
load(
    "@io_bazel_rules_scala//scala:advanced_usage/providers.bzl",
    _ScalaRulePhase = "ScalaRulePhase",
)
load(
    "@phase_zinc//rules:providers.bzl",
    _ScalaConfiguration = "ScalaConfiguration",
    _ZincConfiguration = "ZincConfiguration",
    _ZincInfo = "ZincInfo",
)

# ext_zinc_compile is a dictionary that could contain up to 3 keys: "attrs", "outputs", and "phase_providers".
# Each of these 3 items will be passed to the rule definition when the make_scala_binary function is called.
load(
    "@phase_zinc//rules/ext:phase_zinc_compile_ext.bzl",
    _ext_zinc_compile = "ext_zinc_compile",
)

# These lines create custom versions of rules (eg. a custom scala_binary rule named "zinc_scala_binary").
# In this case, by passing in _ext_zinc_compile, the rule macro replaces the
#  normal compiler with the Zinc compiler. "attrs" and "outputs" are also passed to the rule.

# These rules have the Zinc compiler
zinc_scala_binary = _make_scala_binary(_ext_zinc_compile)
zinc_scala_library = _make_scala_library(_ext_zinc_compile)
zinc_scala_test = _make_scala_test(_ext_zinc_compile)

#  These rules are the default versions, without Zinc compiler
scala_binary = _make_scala_binary()
scala_library = _make_scala_library()
scala_test = _make_scala_test()

def _configure_bootstrap_scala_implementation(ctx):
    return [
        _ScalaConfiguration(
            compiler_classpath = ctx.attr.compiler_classpath,
            global_plugins = ctx.attr.global_plugins,
            global_scalacopts = ctx.attr.global_scalacopts,
            runtime_classpath = ctx.attr.runtime_classpath,
            version = ctx.attr.version,
        ),
    ]

configure_bootstrap_scala = rule(
    attrs = {
        "version": attr.string(mandatory = True),
        "compiler_classpath": attr.label_list(
            mandatory = True,
            providers = [JavaInfo],
        ),
        "runtime_classpath": attr.label_list(
            mandatory = True,
            providers = [JavaInfo],
        ),
        "global_plugins": attr.label_list(
            doc = "Scalac plugins that will always be enabled.",
            providers = [JavaInfo],
        ),
        "global_scalacopts": attr.string_list(
            doc = "Scalac options that will always be enabled.",
        ),
    },
    implementation = _configure_bootstrap_scala_implementation,
)

# This rule/implementation load values into ScalaConfiguration and ZincConfiguration, which are later passed into phase_zinc_compile
def _configure_zinc_scala_implementation(ctx):
    return [
        _ScalaConfiguration(
            compiler_classpath = ctx.attr.compiler_classpath,
            global_plugins = ctx.attr.global_plugins,
            global_scalacopts = ctx.attr.global_scalacopts,
            runtime_classpath = ctx.attr.runtime_classpath,
            version = ctx.attr.version,
        ),
        _ZincConfiguration(
            compile_worker = ctx.attr._compile_worker,
            compiler_bridge = ctx.file.compiler_bridge,
            log_level = ctx.attr.log_level,
        ),
    ]

configure_zinc_scala = rule(
    attrs = {
        "version": attr.string(mandatory = True),
        "runtime_classpath": attr.label_list(
            mandatory = True,
            providers = [JavaInfo],
        ),
        "compiler_classpath": attr.label_list(
            mandatory = True,
            providers = [JavaInfo],
        ),
        "compiler_bridge": attr.label(
            allow_single_file = True,
            mandatory = True,
        ),
        "global_plugins": attr.label_list(
            doc = "Scalac plugins that will always be enabled.",
            providers = [JavaInfo],
        ),
        "global_scalacopts": attr.string_list(
            doc = "Scalac options that will always be enabled.",
        ),
        "log_level": attr.string(
            doc = "Compiler log level",
            default = "warn",
        ),
        "_compile_worker": attr.label(
            default = "@phase_zinc//src/main/scala/workers",
            allow_files = True,
            executable = True,
            cfg = "host",
        ),
    },
    implementation = _configure_zinc_scala_implementation,
)


import re
import database
from response import Response

regex={}
regex['extends']=re.compile(r'{% extends ["\'](.*?)["\'] %}')
regex['block']=re.compile(r'{% block (.*?) %}(?:\s*\n)?(.*?)(?:\s*\n)?{% endblock %}',flags=re.DOTALL)
regex['for']=re.compile(r'(?:\s*\n\s*){% for (.*?) in (.*?) %}(?:\s*\n)?(.*?)(?:\s*\n\s*)?{% endfor %}',flags=re.DOTALL)
regex['variable']=re.compile(r'{{(.*?)}}',flags=re.DOTALL)

def unfold_extensions(template):
	blocks=[]
	do_continue=True
	content=open(template,'r').read().replace('\t','  ')
	while True:
		matches=regex['extends'].findall(content)
		if len(matches) == 1:
			blocks=regex['block'].findall(content)
			content=open(matches[0],'r').read().replace('\t','  ')
		elif len(matches) == 0:
			break
		else:
			raise Exception("can only extend once per file")
	return (content, dict(blocks))

def replace_blocks(content, blocks):
	match=regex['block'].search(content)
	while match:
		(block_name,__empty)=match.groups()
		if block_name in blocks:
			block_content=blocks[block_name]
			block_content=re.sub('^  ','',block_content,re.MULTILINE)
			content=content[:match.start()]+block_content+content[match.end():]
		match=regex['block'].search(content)
	return content

def replace_fors(content):
	match=regex['for'].search(content)
	while match:
		(variable, datalist, inner)=match.groups()
		inner=re.sub('^  ','',inner,re.MULTILINE)
		array=database.search(datalist)
		table_headers=database.get_headers(datalist)
		(table,conditions,returns)=database.parse_variable(datalist)
		new_inner=''
		for element in reversed(array):
			new_inner+=re.sub('{{\s*[^@].*?(:.*?)\s*}}','{{@'+table+'[rowid='+str(element[0])+']'+r'\1}}', inner)
		content=content[:match.start()]+'\n'+new_inner+content[match.end():]
		match=regex['for'].search(content)
	return content

def replace_variables(content, admin):
	match=regex['variable'].search(content)
	while match:
		variable=match.groups()[0]
		value=database.search(variable)
		if value:
			value=value[0][1]
		else:
			value=''
		if admin:
			value='<textarea class="admin" form="update-form"  name="'+variable+'">'+value+'</textarea>'
		content=content[:match.start()]+value+content[match.end():]
		match=regex['variable'].search(content)
	return content

def generate_full_template(template, admin=False):
	(content,blocks)=unfold_extensions(template)
	content=replace_blocks(content, blocks)
	content=replace_fors(content)
	content=replace_variables(content, admin)
	if admin:
		content+='<form action="/btpp/update" method="post" id="update-form"><input type="submit"/></form>'
	return content

def to_html(template_name,variables={}):
	return Response(generate_full_template('templates/'+template_name))


#!/usr/bin/env python
#coding:utf-8
import os
import sys

import time
from os.path import join as pjoin
import subprocess
import json
import cookielib

import mechanize
import bs4

USERID = "****"
PASSWORD = "***"
MAILADDR = "***"

SCRIPT_DIR = os.path.dirname(__file__) # For Cron

browser = mechanize.Browser()
browser.set_cookiejar(cookielib.LWPCookieJar())
browser.set_handle_equiv(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
                           max_time=1)

browser.addheaders = [('User-agent',
                      ('Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)'
                    'Gecko/20041001 Firefox/0.10.1'))]
browser.open("https://www.srp.tohoku.ac.jp/")
browser.select_form(nr=0)
browser.form['twuser'] = USERID
browser.form["twpassword"] = PASSWORD
browser.submit()

browser.open("https://www.srp.tohoku.ac.jp/sa/login.do")
time.sleep(5)
browser.open("https://www.srp.tohoku.ac.jp/sa/wssrlstr.do?clearAccessData=true&kjnmnNo=29")
source = browser.response().read()

soup = bs4.BeautifulSoup(source)
res = {}
for eelem in soup.find_all(class_="column_odd"):
    elem = eelem.find_all("td")
    if len(elem) < 7:continue
    class_name, grade = elem[0].text.strip(), elem[5].text.strip()
    res[class_name.encode("utf-8")] = grade.encode("utf-8") or u""

changed = False
try:
    with open(pjoin(SCRIPT_DIR, "grade")) as f:
        t = json.load(f)
except IOError: #初回
    with open(pjoin(SCRIPT_DIR, "grade"), "w") as f:
        json.dump(res, f)
        sys.exit()
last = {}
for k, v in t.iteritems():
    last[k.encode("utf-8")] = v.encode("utf-8")
for k, v in res.iteritems():
    if last[k] != v:
        changed = True

if changed:
    with open(pjoin(SCRIPT_DIR, "grade"), "w") as f:
        json.dump(res, f)
    print >>sys.stderr, "changed"
    t = ""
    for k, v in res.iteritems():
        k=k.decode("utf-8")
        v=v.decode("utf-8")
        t += k+":"+v+"\n"
    subprocess.Popen(args=["/usr/sbin/sendmail", MAILADDR],
                     stdin=subprocess.PIPE).communicate(t.encode("utf-8"))
else:
    print >>sys.stderr, "not changed"


# -*- coding: UTF-8 -*-
# Public pack
import sys
# Private pack
import ROOT
import headpy.hbes.hopp as hopp
import headpy.hbes.htree as htree
import headpy.hbes.hsci as hsci
import headpy.hbes.hstyle as hstyle
import headpy.hbes.hconst as hconst

def plot(energy=0,
         tree='',
         branch='',
         picture='',
         stop=''):
    # 读取数据
    massages = htree.massage_read()
    trees = htree.tree_read(energy=energy,
                            tree=tree,
                            read=['real', 'omeganpw'])
    cuts = hopp.cut()
    cuts[branch]['cut'] = cuts[branch]['mass']
    cuts['momega']['cut'] = 0.05
    cuts['momega']['shift'] = 0
    # 初始化数据
    alldata = hsci.ALLDATA(trees, cuts, massages)
    alldata.get_weight(data='omeganpw',
                       energy=energy,
                       name_weight='momegapi02_mpi02pi03',
                       name_branch='wpi02_pi02pi03',
                       name_ratio='ratio',
                       dimension=2)
    new_tree = alldata.get_tree('omeganpw')
    new_tree = htree.tree_cut(new_tree,
                              cuts,
                              ['chisq', 'mpi01', 'mpi02', 'mpi03', 'momega'])
    num = len(new_tree[branch])
    mass = hconst.pdg()['m_pi0']
    tuse = new_tree[branch]
    tweight = new_tree['wpi02_pi02pi03']
    total_weight_left = 0
    total_weight_right = 0
    total_sq_left = 0
    total_sq_right = 0
    for i in range(num):
        if(tuse[i] < mass):
            total_weight_left += tweight[i]
            total_sq_left += tweight[i] * pow(tuse[i] - mass, 2)
        elif(tuse[i] > mass):
            total_weight_right += tweight[i]
            total_sq_right += tweight[i] * pow(tuse[i] - mass, 2)
    total_sq_left = pow(total_sq_left / total_weight_left, 0.5)
    total_sq_right = pow(total_sq_right / total_weight_right, 0.5)
    print('Sigma of %s' % (branch))
    print('left:%f' % (total_sq_left))
    print('right:%f' % (total_sq_right))

plot(energy=2.125,
     tree='fit4c',
     branch='mpi01',
     picture='',
     stop='yes')

plot(energy=2.125,
     tree='fit4c',
     branch='mpi02',
     picture='',
     stop='yes')

plot(energy=2.125,
     tree='fit4c',
     branch='mpi03',
     picture='',
     stop='yes')


import logging
from typing import Sequence

import aioredis
from aioredis.commands import MultiExec

from job_scheduler.broker.base import ScheduleBroker

class RedisBroker(ScheduleBroker):
    redis: aioredis.Redis
    brokers: int = 0

    job_queue = "job_queue"
    jobs_in_broker = "enqueued"
    running_jobs = "running_jobs"

    def __init__(self):
        RedisBroker.brokers += 1

    async def publish(self, *messages: str) -> Sequence[str]:
        """
        Publishes a list of messages to a broker. Returns a list containing the
        deduplicated messages that were successfully added onto the queue for
        execution.
        """
        deduplicated = [
            m
            for m in messages
            if not await self.redis.sismember(self.jobs_in_broker, m)
        ]

        if len(deduplicated) > 0:
            await self.redis.rpush(self.job_queue, *deduplicated)
            await self.redis.sadd(self.jobs_in_broker, *deduplicated)

        logging.debug(f"Published {len(deduplicated)} schedules for execution.")
        return deduplicated

    async def get(self) -> str:
        _, message = await self.redis.blpop(self.job_queue)
        await self.redis.sadd(self.running_jobs, message)
        logging.debug(f"Got {message}")
        return message

    async def drain(self, limit=100) -> Sequence[str]:
        messages = [await self.get()]
        while await self.redis.llen(self.job_queue) > 0 and len(messages) < limit:
            m = await self.get()
            messages.append(m)
        return messages

    async def ack(self, *messages: str):
        tr: MultiExec = self.redis.multi_exec()
        tr.srem(self.jobs_in_broker, *messages)
        tr.srem(self.running_jobs, *messages)
        await tr.execute()

    @property
    async def size(self):
        return await self.redis.scard(self.jobs_in_broker)

    @classmethod
    async def requeue_unacked(cls):
        messages = await cls.redis.smembers(cls.running_jobs)
        if len(messages) > 0:
            await cls.redis.rpush(cls.job_queue, *messages)
        await cls.redis.delete(cls.running_jobs)
        logging.info(f"Moved {len(messages)} items from processing into job queue")

    @classmethod
    async def get_broker(cls, address: str = "redis://localhost"):
        if not hasattr(cls, "redis"):
            logging.info(f"Instantiating broker using redis at {address}.")
            rp = await aioredis.create_redis_pool(address, encoding="utf-8")
            logging.info(f"Broker sucessfully instantiated.")
            cls.redis = rp

        return cls()

    @classmethod
    async def shutdown(cls):
        if cls.brokers == 1:
            await cls.requeue_unacked()

        cls.redis.close()
        await cls.redis.wait_closed()
        cls.brokers -= 1
        logging.info(f"Closed redis broker connection to {cls.redis.address}")


# coding: UTF-8
import urllib.request
from bs4 import BeautifulSoup
from datetime import datetime
import csv
import time

f = open('madomagi_miku_sumiyoshi.csv','a')
writer = csv.writer(f,lineterminator='\n')

csv_list = []

time_ = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
csv_list.append(time_)

# アクセスするURL
url = 'https://daidata.goraggio.com/100433/unit_list?hist_num=1&model=SLOT%E9%AD%94%E6%B3%95%E5%B0%91%E5%A5%B3%E3%81%BE%E3%81%A9%E3%81%8B%E2%98%86%E3%83%9E%E3%82%AE%E3%82%AB&ballPrice=20.00&disp=1&graph=1'

try:
    # htmlオブジェクトが返ってくる
    print('httpリクエストの送信')
    html = urllib.request.urlopen(url)

    # htmlをBeautifulSoupで扱い、htmlタグのかたまりが返ってくる
    soup = BeautifulSoup(html, "html.parser")
    td = soup.find_all("td")

    i = 0
    for tag in td:
        if tag.a != None:
            csv_list.append(tag.a.string)
        elif tag.string != None:
            csv_list.append(tag.string)
        i += 1
        if i == 12:
            print(csv_list)
            writer.writerow(csv_list)
            i = 0
            csv_list = []
            time_ = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
            csv_list.append(time_)
except:
    pass

f.close()


class Human:
	def __init__(self, first, last, age):
		self.first = first
		self.last = last
		self._age = max(age, 0)

	@property
	def age(self):
		return self._age

	@age.setter
	def age(self, value):
		if value >= 0:
			self._age = value
		else:
			raise ValueError("age can not be negative!")

	@property
	def full_name(self):
		return f"{self.first} {self.last}"

	@full_name.setter
	def full_name(self, name):
		self.first, self.last = name.split(" ")

stan = Human("Stan", "DeePaQ", 24)
print(stan.age)
stan.age = 25
print(stan.age)
print(stan.full_name)
stan.full_name = "Yashi Yashimoto"
print(stan.__dict__)


# -*- coding: utf-8 -*-
# modules
from .smartPeak import smartPeak
from .smartPeak_i import smartPeak_i
from smartPeak.pyTOPP.OpenSwathChromatogramExtractor import OpenSwathChromatogramExtractor
from smartPeak.pyTOPP.OpenSwathRTNormalizer import OpenSwathRTNormalizer
from smartPeak.pyTOPP.OpenSwathFeatureXMLToTSV import OpenSwathFeatureXMLToTSV
from smartPeak.pyTOPP.MRMFeatureSelector import MRMFeatureSelector
from smartPeak.pyTOPP.MRMFeatureValidator import MRMFeatureValidator
from smartPeak.pyTOPP.FeaturePlotter import FeaturePlotter
from smartPeak.data.ReferenceDataMethods import ReferenceDataMethods
# external
import copy
# 3rd part libraries
try:
    import pyopenms
except ImportError as e:
    print(e)

class smartPeak_openSWATH():
    def __init__(self):
        self.featureMap = None
        self.chromatogram_map = None
        self.targeted = None
        self.trafo = None
        self.msExperiment = None
        self.validation_metrics = None
        self.swath = None
        self.reference_data = None
        self.meta_data = None

    def clear_data(self):
        """Remove all data"""
        self.featureMap = None
        self.chromatogram_map = None
        self.targeted = None
        self.trafo = None
        self.msExperiment = None
        self.validation_metrics = None
        self.swath = None
        self.reference_data = None
        self.meta_data = None

    def load_TraML(self, filenames_I, verbose_I=False):
        """Load TraML file

        Args:
            filenames_I (list): list of filename strings

        Internals:
            targeted (TargetedExperiment)

        """
        if verbose_I:
            print("Loading TraML")

        traML_csv_i, traML_i = None, None
        if 'traML_csv_i'in filenames_I.keys():
            traML_csv_i = filenames_I['traML_csv_i']
        if 'traML_i'in filenames_I.keys():
            traML_i = filenames_I['traML_i']

        # load and make the transition file
        targeted = pyopenms.TargetedExperiment()  # must use "PeptideSequence"
        if traML_csv_i is not None:
            tramlfile = pyopenms.TransitionTSVReader()
            tramlfile.convertTSVToTargetedExperiment(
                traML_csv_i.encode('utf-8'), 21, targeted)
        elif traML_i is not None:
            targeted = pyopenms.TargetedExperiment()
            tramlfile = pyopenms.TraMLFile()
            tramlfile.load(traML_i.encode('utf-8'), targeted)
        self.targeted = targeted

    def load_Trafo(
        self,
        filenames_I,
        MRMFeatureFinderScoring_params_I={},
        verbose_I=False
    ):
        """Load Trafo file

        Args:
            filenames_I (list): list of filename strings
            MRMFeatureFinderScoring_params_I (dict): dictionary of parameter
                names, values, descriptions, and tags

        Internals:
            targeted (TargetedExperiment)

        """
        if verbose_I:
            print("Loading Trafo")

        trafo_csv_i = None
        if 'trafo_csv_i'in filenames_I.keys():
            trafo_csv_i = filenames_I['trafo_csv_i']
        MRMFeatureFinderScoring_params = MRMFeatureFinderScoring_params_I

        # set up MRMFeatureFinderScoring (featurefinder) and
        # parse the MRMFeatureFinderScoring params
        featurefinder = pyopenms.MRMFeatureFinderScoring()
        parameters = featurefinder.getParameters()
        smartpeak = smartPeak()
        parameters = smartpeak.updateParameters(
            parameters,
            MRMFeatureFinderScoring_params,
            )
        featurefinder.setParameters(parameters)

        # # prepare the model parameters for RTNormalization (interpolation)
        # model_params_list = [
        #   {'name':'interpolation_type','value': 'linear','type': 'string'},
        #     {'name':' extrapolation_type','value': 'two-point-linear','type': 'string'},
        # ]
        # model_params = pyopenms.Param()
        # model_params = smartpeak.setParameters(model_params_list,model_params)

        trafo = pyopenms.TransformationDescription()
        if trafo_csv_i is not None:
            # load and make the transition file for RTNormalization
            targeted_rt_norm = pyopenms.TargetedExperiment()
            tramlfile = pyopenms.TransitionTSVReader()
            tramlfile.convertTSVToTargetedExperiment(
                trafo_csv_i.encode('utf-8'), 21, targeted_rt_norm
                )
            # Normalize the RTs
            # NOTE: same MRMFeatureFinderScoring params will be used to pickPeaks
            RTNormalizer = OpenSwathRTNormalizer()
            trafo = RTNormalizer.main(
                self.chromatogram_map,
                targeted_rt_norm,
                model_params=None,
                # model_params=model_params,
                model_type="linear",
                # model_type="interpolated",
                min_rsq=0.95,
                min_coverage=0.6,
                estimateBestPeptides=True,
                MRMFeatureFinderScoring_params=parameters
                )
        self.trafo = trafo

    def load_MSExperiment(
        self,
        filenames_I,
        MRMMapping_params_I={},
        chromatogramExtractor_params_I={},
        verbose_I=False
    ):
        """Load MzML into an MSExperiment

        Args:
            filenames_I (list): list of filename strings
            MRMMapping_params_I (list):
                list of key:value parameters for OpenMS::MRMMapping
            chromatogramExtractor_params_I (list):
                list of key:value parameters for OpenMS::ChromatogramExtractor

        Internals:
            msExperiment (TargetedExperiment)

        """
        if verbose_I:
            print("Loading mzML")

        mzML_feature_i = None
        if 'mzML_feature_i'in filenames_I.keys():
            mzML_feature_i = filenames_I['mzML_feature_i']

        # load chromatograms
        chromatograms = pyopenms.MSExperiment()
        if mzML_feature_i is not None:
            fh = pyopenms.FileHandler()
            fh.loadExperiment(mzML_feature_i.encode('utf-8'), chromatograms)

        if chromatogramExtractor_params_I and \
            chromatogramExtractor_params_I is not None and \
            self.targeted is not None:
            # convert parameters
            smartpeak = smartPeak()
            chromatogramExtractor_params = {d['name']: smartpeak.castString(
                d['value'],
                d['type']) for d in chromatogramExtractor_params_I}
            # chromatogramExtractor_params = {d['name']:smartpeak.parseString(d['value'])
            #   for d in chromatogramExtractor_params_I}
            # exctract chromatograms
            chromatograms_copy = copy.copy(chromatograms)
            chromatograms.clear(True)
            if chromatogramExtractor_params['extract_precursors']:
                tr = self.targeted.getTransitions()
                for t in tr:
                    t.setProductMZ(t.getPrecursorMZ())
                self.targeted.setTransitions(tr)
            chromatogramExtractor = pyopenms.ChromatogramExtractor()
            chromatogramExtractor.extractChromatograms(
                chromatograms_copy,
                chromatograms,
                self.targeted,
                chromatogramExtractor_params['extract_window'],
                chromatogramExtractor_params['ppm'],
                pyopenms.TransformationDescription(),
                chromatogramExtractor_params['rt_extraction_window'],
                chromatogramExtractor_params['filter'],
                )

        self.msExperiment = chromatograms

        # map transitions to the chromatograms
        if MRMMapping_params_I and \
            MRMMapping_params_I is not None and \
            self.targeted is not None:
            # set up MRMMapping and
            # parse the MRMMapping params
            mrmmapper = pyopenms.MRMMapping()
            smartpeak = smartPeak()
            parameters = mrmmapper.getParameters()
            parameters = smartpeak.updateParameters(
                parameters,
                MRMMapping_params_I,
                )
            mrmmapper.setParameters(parameters)
            chromatogram_map = pyopenms.MSExperiment()

            # mrmmapper = MRMMapper()
            # chromatogram_map = mrmmapper.algorithm(
            #     chromatogram_map=chromatograms,
            #     targeted=self.targeted,
            #     precursor_tolerance=0.0009, #hard-coded for now
            #     product_tolerance=0.0009, #hard-coded for now
            #     allow_unmapped=True,
            #     allow_double_mappings=True
            # )

            mrmmapper.mapExperiment(chromatograms, self.targeted, chromatogram_map)
        self.chromatogram_map = chromatogram_map

    def load_SWATHorDIA(
        self,
        filenames_I,
        verbose_I=False
    ):
        """Load SWATH or DIA into an MSExperiment
        Args:
            filenames_I (list): list of filename strings
        Internals:
            msExperiment (TargetedExperiment)

        """
        if verbose_I:
            print("Loading SWATH/DIA files")

        dia_csv_i = None
        if 'dia_csv_i'in filenames_I.keys():
            dia_csv_i = filenames_I['dia_csv_i']

        # load in the DIA data
        swath = pyopenms.MSExperiment()
        if dia_csv_i is not None:
            chromatogramExtractor = OpenSwathChromatogramExtractor()
            # read in the DIA data files:
            # dia_files_i = ...(dia_csv_i)
            swath = chromatogramExtractor.main(
                infiles=[],
                targeted=self.targeted,
                extraction_window=0.05,
                min_upper_edge_dist=0.0,
                ppm=False,
                is_swath=False,
                rt_extraction_window=-1,
                extraction_function="tophat"
            )
        self.swath = swath

    def openSWATH(
        self,
        MRMFeatureFinderScoring_params_I={},
        verbose_I=False
    ):
        """Run the openSWATH workflow for a single sample

        Args:
            filenames_I (list): list of filename strings
            MRMFeatureFinderScoring_params_I (dict): dictionary of parameter
                names, values, descriptions, and tags

        """
        if verbose_I:
            print("Picking peaks using OpenSWATH")

        # helper classes
        smartpeak = smartPeak()

        # make the decoys
        # MRMDecoy
        # How are the decoys added into the experiment?

        # Create empty output
        output = pyopenms.FeatureMap()

        # set up MRMFeatureFinderScoring (featurefinder) and
        # parse the MRMFeatureFinderScoring params
        featurefinder = pyopenms.MRMFeatureFinderScoring()
        parameters = featurefinder.getParameters()
        parameters = smartpeak.updateParameters(
            parameters,
            MRMFeatureFinderScoring_params_I,
            )
        featurefinder.setParameters(parameters)

        # set up MRMFeatureFinderScoring (featurefinder) and
        # run
        featurefinder.pickExperiment(
            self.chromatogram_map,
            output,
            self.targeted,
            self.trafo,
            self.swath)

        self.featureMap = output

    def filterAndSelect_py(
        self,
        filenames_I,
        MRMFeatureFilter_filter_params_I={},
        MRMFeatureSelector_select_params_I={},
        MRMFeatureSelector_schedule_params_I={},
        verbose_I=False
    ):
        """Run the openSWATH post processing filtering workflow for a single sample

        Args:
            filenames_I (list): list of filename strings
            MRMFeatureFilter_filter_params_I (dict): dictionary of parameter
                names, values, descriptions, and tags
            MRMFeatureSelector_select_params_I (dict): dictionary of parameter
                names, values, descriptions, and tags
            MRMFeatureSelector_schedule_params_I (dict): dictionary of parameter
                names, values, descriptions, and tags

        Internals:
            features (FeatureMap): output from SWATH workflow
            msExperiment (MSExperiment):
            targeted (TargetedExperiment):

        Returns:
            output_selected (FeatureMap): filtered and/or selected features

        """
        # variables
        calibrators_csv_i = None
        mrmfeatureqcs_csv_i = None
        if 'calibrators_csv_i'in filenames_I.keys():
            calibrators_csv_i = filenames_I['calibrators_csv_i']
        if 'mrmfeatureqcs_csv_i'in filenames_I.keys():
            mrmfeatureqcs_csv_i = filenames_I['mrmfeatureqcs_csv_i']

        # filter features
        if verbose_I:
            print("Filtering picked features")
        if MRMFeatureFilter_filter_params_I:
            # set up MRMFeatureFilter and parse the MRMFeatureFilter params
            featureFilter = pyopenms.MRMFeatureFilter()
            parameters = featureFilter.getParameters()
            smartpeak = smartPeak()
            parameters = smartpeak.updateParameters(
                parameters,
                MRMFeatureFilter_filter_params_I,
                )
            featureFilter.setParameters(parameters)

            # read in the parameters for the MRMFeatureQC
            featureQC = pyopenms.MRMFeatureQC()
            featureQCFile = pyopenms.MRMFeatureQCFile()
            featureQCFile.load(mrmfeatureqcs_csv_i.encode('utf-8'), featureQC)

            output_filtered = copy.copy(self.featureMap)
            featureFilter.FilterFeatureMap(
                output_filtered,
                featureQC,
                self.targeted)
        else:
            output_filtered = self.featureMap

        # select features
        if verbose_I:
            print("Selecting picked features")
        featureSelector = MRMFeatureSelector()
        if calibrators_csv_i is not None:
            smartpeak_i = smartPeak_i()
            smartpeak_i.read_csv(calibrators_csv_i, delimiter=',')
            calibrators = smartpeak_i.getData()
            smartpeak_i.clear_data()
        else:
            calibrators = []
        if MRMFeatureSelector_schedule_params_I:
            output_selected = featureSelector.schedule_MRMFeatures_qmip(
                features=output_filtered,
                tr_expected=calibrators,
                targeted=self.targeted,
                schedule_criteria=MRMFeatureSelector_schedule_params_I,
                score_weights=MRMFeatureSelector_select_params_I
            )
        elif MRMFeatureSelector_select_params_I:
            output_selected = featureSelector.select_MRMFeatures_score(
                output_filtered,
                MRMFeatureSelector_select_params_I)
            # output_selected = featureSelector.select_MRMFeatures_qmip(
            #     features = output_filtered,
            #     tr_expected = calibrators,
            #     select_criteria = MRMFeatureSelector_select_params_I,
            # )
        else:
            output_selected = output_filtered

        self.featureMap = output_selected

    def load_featureMap(
        self,
        filenames_I={},
        verbose_I=False
    ):
        """Load a FeatureMap

        Args:
            filenames_I (list): list of filename strings

        """
        if verbose_I:
            print("Loading FeatureMap")

        # Handle the filenames
        featureXML_i = None
        if 'featureXML_i'in filenames_I.keys():
            featureXML_i = filenames_I['featureXML_i']

        # Store outfile as featureXML
        featurexml = pyopenms.FeatureXMLFile()
        output = pyopenms.FeatureMap()
        if featureXML_i is not None:
            featurexml.load(featureXML_i.encode('utf-8'), output)

        self.featureMap = output

    def extract_metaData(self, verbose_I=False):
        """Extracts metadata from the chromatogram
        """
        if verbose_I:
            print("Extracting metadata")

        # initialize output variables
        filename = ''
        samplename = ''
        instrument = ''
        software = ''

        # filename
        loaded_file_path = self.chromatogram_map.getLoadedFilePath()
        if loaded_file_path is not None:
            filename = loaded_file_path.decode('utf-8').replace('file://', '')
        # filename = '''%s/%s''' %(
        #     chromatograms_mapped.getSourceFiles()[0].getPathToFile().decode('utf-8').replace('file://',''),
        #     chromatograms_mapped.getSourceFiles()[0].getNameOfFile().decode('utf-8'))

        # sample name
        mzml_id = self.chromatogram_map.getMetaValue(b'mzml_id')
        if mzml_id is not None:
            samplename_list = mzml_id.decode('utf-8').split('-')
            samplename = '-'.join(samplename_list[1:])

        # instrument
        instrument_name = self.chromatogram_map.getInstrument().getName()
        if instrument_name is not None:
            instrument = instrument_name.decode('utf-8')
            # software
            software_name = self.chromatogram_map.getInstrument().getSoftware().getName()
            if software_name is not None:
                software = software_name.decode('utf-8')

        self.meta_data = {
            "filename": filename,
            "sample_name": samplename,
            "instrument": instrument,
            "software": software
        }

    def store_featureMap(
        self,
        filenames_I={},
        verbose_I=False
    ):
        """Store FeatureMap as .xml and .csv

        Args:
            filenames_I (list): list of filename strings
        """
        if verbose_I:
            print("Storing FeatureMap")

        # Handle the filenames
        featureXML_o, feature_csv_o = None, None
        if 'featureXML_o'in filenames_I.keys():
            featureXML_o = filenames_I['featureXML_o']
        if 'feature_csv_o'in filenames_I.keys():
            feature_csv_o = filenames_I['feature_csv_o']

        # Store outfile as featureXML
        featurexml = pyopenms.FeatureXMLFile()
        if featureXML_o is not None:
            featurexml.store(featureXML_o.encode('utf-8'), self.featureMap)

        # Store the outfile as csv
        featurescsv = OpenSwathFeatureXMLToTSV()
        if feature_csv_o is not None:
            featurescsv.store(
                feature_csv_o, self.featureMap, self.targeted,
                run_id=self.meta_data['sample_name'],
                filename=self.meta_data['filename']
                )

    def load_validationData(
        self,
        filenames_I,
        ReferenceDataMethods_params_I={},
        verbose_I=False
    ):
        """Load the validation data from file or from a database

        Args:
            filenames_I (dict): dictionary of filenames
            ReferenceDataMethods_params_I (dict): dictionary of DB query parameters

        """
        if verbose_I:
            print("Loading validation data")

        smartpeak = smartPeak()

        # Handle the filenames
        referenceData_csv_i, db_ini_i = None, None
        if 'referenceData_csv_i'in filenames_I.keys():
            referenceData_csv_i = filenames_I['referenceData_csv_i']
        if 'db_ini_i'in filenames_I.keys():
            db_ini_i = filenames_I['db_ini_i']

        # Parse the input parameters
        ReferenceDataMethods_dict = {d['name']: smartpeak.parseString(
            d['value'], encode_str_I=False) for d in ReferenceDataMethods_params_I}
        experiment_ids_I = [],
        sample_names_I = [],
        sample_types_I = [],
        acquisition_methods_I = [],
        quantitation_method_ids_I = [],
        component_names_I = [],
        component_group_names_I = [],
        where_clause_I = '',
        used__I = True,
        experiment_limit_I = 10000,
        mqresultstable_limit_I = 1000000,
        # settings_filename_I = 'settings.ini',
        # data_filename_O = ''
        if "experiment_ids_I" in ReferenceDataMethods_dict.keys():
            experiment_ids_I = ReferenceDataMethods_dict["experiment_ids_I"]
        if "sample_names_I" in ReferenceDataMethods_dict.keys():
            sample_names_I = ReferenceDataMethods_dict["sample_names_I"]
        if "sample_types_I" in ReferenceDataMethods_dict.keys():
            sample_types_I = ReferenceDataMethods_dict["sample_types_I"]
        if "acquisition_methods_I" in ReferenceDataMethods_dict.keys():
            acquisition_methods_I = ReferenceDataMethods_dict["acquisition_methods_I"]
        if "quantitation_method_ids_I" in ReferenceDataMethods_dict.keys():
            quantitation_method_ids_I = ReferenceDataMethods_dict[
                "quantitation_method_ids_I"]
        if "component_names_I" in ReferenceDataMethods_dict.keys():
            component_names_I = ReferenceDataMethods_dict["component_names_I"]
        if "component_group_names_I" in ReferenceDataMethods_dict.keys():
            component_group_names_I = ReferenceDataMethods_dict["component_group_names_I"]
        if "where_clause_I" in ReferenceDataMethods_dict.keys():
            where_clause_I = ReferenceDataMethods_dict["where_clause_I"]
        if "used__I" in ReferenceDataMethods_dict.keys():
            used__I = ReferenceDataMethods_dict["used__I"]
        if "experiment_limit_I" in ReferenceDataMethods_dict.keys():
            experiment_limit_I = ReferenceDataMethods_dict["experiment_limit_I"]
        if "mqresultstable_limit_I" in ReferenceDataMethods_dict.keys():
            mqresultstable_limit_I = ReferenceDataMethods_dict["mqresultstable_limit_I"]
        # if "settings_filename_I" in ReferenceDataMethods_dict.keys():
        #     settings_filename_I = ReferenceDataMethods_dict["settings_filename_I"]
        # if "data_filename_O" in ReferenceDataMethods_dict.keys():
        #     data_filename_O = ReferenceDataMethods_dict["data_filename_O"]

        # read in the reference data
        reference_data = []
        if referenceData_csv_i is not None:
            smartpeak_i = smartPeak_i()
            smartpeak_i.read_csv(referenceData_csv_i)
            reference_data = smartpeak_i.getData()
            smartpeak_i.clear_data()
        elif db_ini_i is not None:
            referenceDataMethods = ReferenceDataMethods()
            reference_data = referenceDataMethods.getAndProcess_referenceData_samples(
                experiment_ids_I=experiment_ids_I,
                sample_names_I=sample_names_I,
                sample_types_I=sample_types_I,
                acquisition_methods_I=acquisition_methods_I,
                quantitation_method_ids_I=quantitation_method_ids_I,
                component_names_I=component_names_I,
                component_group_names_I=component_group_names_I,
                where_clause_I=where_clause_I,
                used__I=used__I,
                experiment_limit_I=experiment_limit_I,
                mqresultstable_limit_I=mqresultstable_limit_I,
                settings_filename_I=db_ini_i,
                data_filename_O=''
            )
        self.reference_data = reference_data

    def validate(
        self,
        MRMRFeatureValidator_params_I={},
        verbose_I=False
    ):
        """Validate the selected peaks agains reference data
        """
        if verbose_I:
            print("Validating features")

        # map the reference data
        if MRMRFeatureValidator_params_I:
            featureValidator = MRMFeatureValidator()
            features_mapped, validation_metrics = featureValidator.validate_MRMFeatures(
                reference_data=self.reference_data,
                features=self.featureMap,
                Tr_window=float(MRMRFeatureValidator_params_I[0]['value'])
                )
            self.featureMap = features_mapped
            self.validation_metrics = validation_metrics

    def export_featurePlots(
        self,
        filenames_I,
        FeaturePlotter_params_I={},
        verbose_I=False
    ):
        """Export plots of peaks with features annotated
        """
        if verbose_I:
            print("Plotting peaks with features")

        # Handle the filenames
        features_pdf_o = None
        if 'features_pdf_o'in filenames_I.keys():
            features_pdf_o = filenames_I['features_pdf_o']

        # export diagnostic plots
        if FeaturePlotter_params_I:
            featurePlotter = FeaturePlotter()
            featurePlotter.setParameters(FeaturePlotter_params_I)
            featurePlotter.plot_peaks(
                filename_I=features_pdf_o,
                transitions=self.targeted,
                chromatograms=self.chromatogram_map,
                features=self.featureMap
            )


## written by Sarah Betti 2019
## updated 5 November 2019

import numpy as np
from astropy import units as u
from astropy import constants as const

def get_flux_mass_conv(pcdist,T, wavelength=1100):

    '''
    PURPOSE
    --------
    Return mass conversion factor for total flux in Jy.

    Parameters
    --------
    pcdist: float in pc
        distance to object in pc
    T: float in Kelvin
        temperature of object
    wavelength: float in microns
        wavelength at which to find flux.  Can only be [350, 850, 1100] microns
        default is 1100 microns (1.1 mm)

    Returns
    --------
    m_XXum: float
        conversion factor to convert total flux to XX Jy

    Raises
    -------
    ValueError if wavelength is not 350, 850, 1100

    Examples
    --------
        m_350um = get_aztec_mass_conv(800, 18, 350)

    Comments
    --------
        written in IDL by Rob Gutermith
        converted to python by Sarah Betti 2019
    '''

    h   =   const.h.cgs.value     # g cm^2 / s OR  erg s
    k   =   const.k_B.cgs.value   # erg / K
    c   =   const.c.cgs.value     # cm /s
    d   =   pcdist *3.0857e18     # cm

    if wavelength == 350.:
        ### Ossenkopf & Henning 1994 #5 opacities; "OH5"
        kappa_350um     =   0.101                   # cm^2 / g  (includes gas-to-dust)

        ### Dunham et al. 2009 in prep.
        snu_350um       =   151.2*1.0*10**-23       # erg/cm^2/s/Hz
        snu_350um_alt1  =   81.3*1.0*10**-23        # erg/cm^2/s/Hz
        snu_350um_alt2  =   34.6*1.0*10**-23        # erg/cm^2/s/Hz

        nu_350um        =   (2.99792458e14)/350.    # Hz
        planck_350um    =   (2.*h*(nu_350um**3.)/(c**2.))/((np.exp((h*nu_350um)/(k*T)))-1.)

        m_350um         =   (((d**2.)*snu_350um)/(planck_350um*kappa_350um))/1.99e33
        m_350um_alt1    =   (((d**2.)*snu_350um_alt1)/(planck_350um*kappa_350um))/1.99e33
        m_350um_alt2    =   (((d**2.)*snu_350um_alt2)/(planck_350um*kappa_350um))/1.99e33

        return m_350um

    elif wavelength == 450.:
        ### Ossenkopf & Henning 1994 #5 opacities; "OH5"
        kappa_450um     =   0.0674               # cm^2 / g  (includes gas-to-dust)

        snu_450um       =   1.0*10**-23      # erg/cm^2/s/Hz

        nu_450um        =   (2.99792458e14)/450.    # Hz
        planck_450um    =   (2.*h*(nu_450um**3.)/(c**2.))/((np.exp((h*nu_450um)/(k*T)))-1.)

        m_450um         =   (((d**2.)*snu_450um)/(planck_450um*kappa_450um))/1.99e33

        return m_450um

    elif wavelength == 850.:
        ### Ossenkopf & Henning 1994 #5 opacities; "OH5"
        kappa_850um     =   0.0114                  # cm^2 / g  (includes gas-to-dust) (OH*4*, 845um)

        snu_850um       =   1.0*10**-23             # erg/cm^2/s/Hz

        nu_850um        =   (2.99792458e14)/850.    # Hz
        planck_850um    =   (2.*h*(nu_850um**3.)/(c**2.))/((np.exp((h*nu_850um)/(k*T)))-1.)

        m_850um         =   (((d**2.)*snu_850um)/(planck_850um*kappa_850um))/1.99e33

        return m_850um

    elif wavelength == 1100.:
        ### Ossenkopf & Henning 1994 #5 opacities; "OH5"
        #kappa_1100um   =   0.0114                  # cm^2 / g  (includes gas-to-dust) (OH*4*, 845um)
        kappa_1100um    =   0.0121                  # cm^2 / g  (includes gas-to-dust) (close enough?)
        #kappa_1100um   =   0.0069                  # cm^2 / g  (includes gas-to-dust) (OH*4*, 1.1mm)

        snu_1100um      =   1.0*10**-23             # erg/cm^2/s/Hz

        nu_1100um       =   (2.99792458e14)/1100.0  # Hz
        planck_1100um   =   (2.*h*(nu_1100um**3.)/(c**2.))/((np.exp((h*nu_1100um)/(k*T)))-1.)

        m_1100um        =   (((d**2.)*snu_1100um)/(planck_1100um*kappa_1100um))/1.99e33

        return m_1100um

    else:
        raise ValueError('wavelength does not exist.  Use either 350, 345, 850, 1100 microns instead.')


import json
import mock
import uuid

from django.test import TestCase
from django.utils import timezone
from facility_profile.models import SummaryLog
from morango.api.serializers import CertificateSerializer, BufferSerializer
from morango.certificates import Certificate, ScopeDefinition, Key
from morango.controller import MorangoProfileController
from morango.errors import CertificateSignatureInvalid
from morango.models import Buffer, SyncSession, TransferSession, InstanceIDModel
from morango.syncsession import NetworkSyncConnection, SyncClient

def mock_patch_decorator(func):

    def wrapper(*args, **kwargs):
        mock_object = mock.Mock(content=b"""{"id": "abc"}""", data={'signature': 'sig', 'client_fsic': '{}', 'server_fsic': '{}'})
        mock_object.json.return_value = {}
        with mock.patch.object(NetworkSyncConnection, '_request', return_value=mock_object):
            with mock.patch.object(Certificate, 'verify', return_value=True):
                    return func(*args, **kwargs)
    return wrapper

class NetworkSyncConnectionTestCase(TestCase):

    def setUp(self):
        self.profile = "facilitydata"

        self.root_scope_def = ScopeDefinition.objects.create(
            id="rootcert",
            profile=self.profile,
            version=1,
            primary_scope_param_key="mainpartition",
            description="Root cert for ${mainpartition}.",
            read_filter_template="",
            write_filter_template="",
            read_write_filter_template="${mainpartition}",
        )

        self.subset_scope_def = ScopeDefinition.objects.create(
            id="subcert",
            profile=self.profile,
            version=1,
            primary_scope_param_key="",
            description="Subset cert under ${mainpartition} for ${subpartition}.",
            read_filter_template="${mainpartition}",
            write_filter_template="${mainpartition}:${subpartition}",
            read_write_filter_template="",
        )

        self.root_cert = Certificate.generate_root_certificate(self.root_scope_def.id)

        self.subset_cert = Certificate(
            parent=self.root_cert,
            profile=self.profile,
            scope_definition=self.subset_scope_def,
            scope_version=self.subset_scope_def.version,
            scope_params=json.dumps({"mainpartition": self.root_cert.id, "subpartition": "abracadabra"}),
            private_key=Key(),
        )
        self.root_cert.sign_certificate(self.subset_cert)
        self.subset_cert.save()

        self.controller = MorangoProfileController('facilitydata')
        self.network_connection = self.controller.create_network_connection('127.0.0.1')

    @mock_patch_decorator
    def test_creating_sync_session_successful(self):
        self.assertEqual(SyncSession.objects.filter(active=True).count(), 0)
        NetworkSyncConnection._request.return_value.json.return_value = {'signature': 'sig', 'local_fsic': '{}'}
        self.network_connection.create_sync_session(self.subset_cert, self.root_cert)
        self.assertEqual(SyncSession.objects.filter(active=True).count(), 1)

    @mock_patch_decorator
    def test_creating_sync_session_cert_fails_to_verify(self):
        Certificate.verify.return_value = False
        with self.assertRaises(CertificateSignatureInvalid):
            self.network_connection.create_sync_session(self.subset_cert, self.root_cert)

    @mock_patch_decorator
    def test_get_remote_certs(self):
        # mock certs being returned by server
        certs = self.subset_cert.get_ancestors(include_self=True)
        cert_serialized = json.dumps(CertificateSerializer(certs, many=True).data)
        NetworkSyncConnection._request.return_value.json.return_value = json.loads(cert_serialized)

        # we want to see if the models are created (not saved) successfully
        remote_certs = self.network_connection.get_remote_certificates('abc')
        self.assertSetEqual(set(certs), set(remote_certs))

    @mock_patch_decorator
    def test_csr(self):
        # mock a "signed" cert being returned by server
        cert_serialized = json.dumps(CertificateSerializer(self.subset_cert).data)
        NetworkSyncConnection._request.return_value.json.return_value = json.loads(cert_serialized)
        self.subset_cert.delete()

        # we only want to make sure the "signed" cert is saved
        with mock.patch.object(Key, "get_private_key_string", return_value=self.subset_cert.private_key.get_private_key_string()):
            self.network_connection.certificate_signing_request(self.root_cert, '', '')
        self.assertTrue(Certificate.objects.filter(id=json.loads(cert_serialized)['id']).exists())

    @mock_patch_decorator
    def test_get_cert_chain(self):
        # mock a cert chain being returned by server
        certs = self.subset_cert.get_ancestors(include_self=True)
        original_cert_count = certs.count()
        cert_serialized = json.dumps(CertificateSerializer(certs, many=True).data)
        NetworkSyncConnection._request.return_value.json.return_value = json.loads(cert_serialized)
        Certificate.objects.all().delete()

        # we only want to make sure the cert chain is saved
        self.network_connection._get_certificate_chain(certs[1])
        self.assertEqual(Certificate.objects.count(), original_cert_count)

class SyncClientTestCase(TestCase):

    def setUp(self):
        session = SyncSession.objects.create(id=uuid.uuid4().hex, profile="facilitydata", last_activity_timestamp=timezone.now())
        transfer_session = TransferSession.objects.create(id=uuid.uuid4().hex, sync_session=session, filter='partition',
                                                          push=True, last_activity_timestamp=timezone.now(), records_total=3)
        conn = NetworkSyncConnection()
        self.syncclient = SyncClient(conn, session)
        self.syncclient.current_transfer_session = transfer_session
        self.chunk_size = 3
        InstanceIDModel.get_or_create_current_instance()

    def build_buffer_items(self, transfer_session, **kwargs):

        data = {
            "profile": kwargs.get("profile", 'facilitydata'),
            "serialized": kwargs.get("serialized", '{"test": 99}'),
            "deleted": kwargs.get("deleted", False),
            "last_saved_instance": kwargs.get("last_saved_instance", uuid.uuid4().hex),
            "last_saved_counter": kwargs.get("last_saved_counter", 179),
            "partition": kwargs.get("partition", 'partition'),
            "source_id": kwargs.get("source_id", uuid.uuid4().hex),
            "model_name": kwargs.get("model_name", "contentsummarylog"),
            "conflicting_serialized_data": kwargs.get("conflicting_serialized_data", ""),
            "model_uuid": kwargs.get("model_uuid", None),
            "transfer_session": transfer_session,
        }

        for i in range(self.chunk_size):
            data['source_id'] = uuid.uuid4().hex
            data["model_uuid"] = SummaryLog.compute_namespaced_id(data["partition"], data["source_id"], data["model_name"])
            Buffer.objects.create(**data)

        buffered_items = Buffer.objects.filter(transfer_session=self.syncclient.current_transfer_session)
        serialized_records = BufferSerializer(buffered_items, many=True)
        return json.dumps(serialized_records.data)

    @mock_patch_decorator
    def test_push_records(self):
        self.build_buffer_items(self.syncclient.current_transfer_session)
        self.assertEqual(self.syncclient.current_transfer_session.records_transferred, 0)
        self.syncclient._push_records(chunk_size=self.chunk_size)
        self.assertEqual(self.syncclient.current_transfer_session.records_transferred, self.chunk_size)

    @mock_patch_decorator
    def test_pull_records(self):
        resp = self.build_buffer_items(self.syncclient.current_transfer_session)
        NetworkSyncConnection._request.return_value.json.return_value = json.loads(resp)
        Buffer.objects.filter(transfer_session=self.syncclient.current_transfer_session).delete()
        self.assertEqual(Buffer.objects.filter(transfer_session=self.syncclient.current_transfer_session).count(), 0)
        self.assertEqual(self.syncclient.current_transfer_session.records_transferred, 0)
        self.syncclient._pull_records(chunk_size=self.chunk_size)
        self.assertEqual(Buffer.objects.filter(transfer_session=self.syncclient.current_transfer_session).count(), self.chunk_size)
        self.assertEqual(self.syncclient.current_transfer_session.records_transferred, self.chunk_size)

    @mock_patch_decorator
    def test_create_transfer_session_push(self):
        self.syncclient.current_transfer_session.active = False
        self.syncclient.current_transfer_session.save()
        self.assertEqual(TransferSession.objects.filter(active=True).count(), 0)
        self.syncclient._create_transfer_session(True, 'filter')
        self.assertEqual(TransferSession.objects.filter(active=True).count(), 1)

    @mock_patch_decorator
    def test_close_transfer_session_push(self):
        self.assertEqual(TransferSession.objects.filter(active=True).count(), 1)
        self.syncclient._close_transfer_session()
        self.assertEqual(TransferSession.objects.filter(active=True).count(), 0)

    @mock_patch_decorator
    def test_close_sync_session(self):
        self.assertEqual(SyncSession.objects.filter(active=True).count(), 1)
        self.syncclient._close_transfer_session()
        self.syncclient.close_sync_session()
        self.assertEqual(SyncSession.objects.filter(active=True).count(), 0)


from app import create_app, celery
from flask_script import Manager

my_app = create_app('default')
my_app.app_context().push()
manager = Manager(my_app)

if __name__ == '__main__':

    # Start app
    manager.run()


from selenium import webdriver
from time import sleep
import os
import datetime
import tweepy
import ctypes
import sys

# Matheus Eduardo
# github.com/eumts

# - Setting variables -

driver = webdriver.Firefox()
driver.get(" ") # Your dontpad link
sleep(3)

CONSUMER_KEY = " "
CONSUMER_SECRET = " "
ACCESS_KEY = " "
ACCESS_SECRET = " "

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)

user = api.me()
print("Logged as " + user.name)
print()

# Words only tweets

def fav_tweet_calor():
    print()
    print("=-=-=-=-=-=-=-=-=-=-=")
    print("Looking for tweets...")
    print("=-=-=-=-=-=-=-=-=-=-=")
    print()
    results = api.search(q="calor", tweet_mode = "extended") # lang="pt-br" geocode = "xx.xxxxxx,xx.xxxxxx,xkm"      q="" its the query, the word that the script will look for
    for result in reversed(results):
        arquivo_calor = open('curtidas_calor.txt','r')
        conteudo_calor = arquivo_calor.read()
        try:
            if (str(result.id) in conteudo_calor):
                print("We already retweeted the tweet of @" + result.user.screen_name)
            else:
                if (result.user.screen_name == "bot_calor"):
                    print("myself") # Trying to interact with my tweet
                else:
                    arquivo_calor.close()
                    print()
                    print(str(result.user.screen_name) + ' - ' + result.full_text, flush=True) # @Mtss_e - Nossa que calor!!!
                    print()
                    print()
                    print("Found", flush=True)
                    print("Liking and retweeting...", flush=True)
                    print()
                    arquivo_calor = open('curtidas_calor.txt','a')
                    arquivo_calor.write(str(result.id) + "\n")
                    api.create_favorite(result.id)
                    api.retweet(result.id)
                    arquivo_calor.close()
        except tweepy.TweepError as e:
            print(e.reason)

# Mentions only funcion

def fav_tweet_menção():
    print()
    print("=-=-=-=-=-=-=-=-=-=-=")
    print("Looking for mentions...")
    print("=-=-=-=-=-=-=-=-=-=-=")
    print()
    mentions = api.mentions_timeline(tweet_mode = 'extended', lang="pt-br")
    for mention in reversed(mentions):
        arquivo_mencao = open('curtidas_mencao.txt','r')
        conteudo_mencao = arquivo_mencao.read()
        try:
            if (str(mention.id) in conteudo_mencao):
                print("We already retweeted the tweet of @" + mention.user.screen_name)
            else:
                if (mention.user.screen_name == "bot_calor"):
                        print("myself")
                else:
                    arquivo_mencao.close()
                    print()
                    print(str(mention.user.screen_name) + ' - ' + mention.full_text, flush=True) # @Mtss_e - Caramba, esse @bot_calor tá bombando mesmo né!!!
                    print()
                    print()
                    print("Found", flush=True)
                    print("Liking and retweeting...", flush=True)
                    print()
                    arquivo_mencao = open('curtidas_mencao.txt','a')
                    arquivo_mencao.write(str(mention.id) + "\n")
                    api.create_favorite(mention.id)
                    api.retweet(mention.id)
                    arquivo_mencao.close()
        except tweepy.TweepError as e:
            print(e.reason)

# Checking dontpad function

def check():
    os.system('cls')
    driver.refresh()
    textarea = driver.find_element_by_xpath('//*[@id="text"]')
    text = textarea.text
    now = datetime.datetime.now()
    data = (now.strftime("[%d-%m-%Y  %H:%M:%S]"))
    if ("ON" in text):
        print()
        print("=--=-=-=-=-=-=-=-=-=")
        print("Calor_bot on")
        print("=--=-=-=-=-=-=-=-=-=")
        print()
        print(data)
        textarea.clear()
        textarea.send_keys('''ON
the bot is running, as your computer.
to change it and shut your pc down type the password:''')
        fav_tweet_calor()
        fav_tweet_menção()
        sleep(30)

    if ("OFF" in text):
        os.system('cls')
        print()
        print("=--=-=-=-=-=-=-=-=-=")
        print("Calor_bot off")
        print("=--=-=-=-=-=-=-=-=-=")
        print()
        print(data)
        textarea.clear()
        textarea.send_keys('''OFF
the bot isnt running, but your pc does.
to change it and shut your pc down type the password:''')

    if (" " in text): # Your password (could not have the words ON and OFF, I recommend using a numer sequence.)
        os.system('cls')
        print()
        print("=--=-=-=-=-=-=-=-=-=")
        print("Shutting your computer down...")
        print("=--=-=-=-=-=-=-=-=-=")
        print()
        print(data)
        textarea.clear()
        textarea.send_keys('''OFF
the bot isnt running, and your pc isnt running too.''')
        os.startfile(r".\shutdown.bat")

# Loop

while True:
    check()
    sleep(20)


import os
import numpy as np
import torch
from PIL import Image, ImageDraw
from tool import utils

# 定义路径
# root = r"D:\datasets"

"一级改动"
labelfile = r"D:\datasets\datasets10261\label_10261.txt"  # 读取的标签路径
"二级改动"
picpath = r"D:\datasets\datasets10261\jpg"  # 读取的图片路径
"一级改动"
savepath = r"D:\datasets\save_10261_20190726"
utils.makedir(savepath)
# 图片和标签的保存根目录
pic_savepath = os.path.join(savepath, "pic")
label_savepath = os.path.join(savepath, "label")
"define iou threshold"
positive_range = [0.65, 1]
part_range = [0.4, 0.6]
negative_range = [0, 0.05]
size_scale = 0.9
wh_scales = [0.05, 0.1, 0.2, 0.75, 0.95]
face_sizes = [12, 24, 48]
classification = ['negative', 'positive', 'part']

utils.makedir(pic_savepath)
utils.makedir(label_savepath)

# define 每张图片新生成图片的数量
each_pic_num = 10  # 1--144

def getconfidence(iou_value):
    '''

    :param iou_value:
    :return:
    '''
    if positive_range[0] < iou_value < positive_range[1]:
        confidence = 1
    elif part_range[0] < iou_value < part_range[1]:
        confidence = 2
    elif negative_range[0] < iou_value < negative_range[1]:
        confidence = 0
    else:
        return -1
    return confidence

def classify(iou_value):
    '''
    没用
    :param iou_value:
    :return:
    '''
    # confidence = 0
    if positive_range[0] < iou_value < positive_range[1]:
        confidence = 1
    elif part_range[0] < iou_value < part_range[1]:
        confidence = 2
    elif negative_range[0] < iou_value < negative_range[1]:
        confidence = 0
    else:
        return -1
    return {
        0: "negative",
        1: "positive",
        2: "part"
    }.get(confidence, "error")  # 不能加default=

def generatedataset(pass_used=False, treat_img=True, gen_label=True):
    pic_used_dict = {12: "12.txt", 24: "24.txt", 48: "48.txt"}

    # 新旧标签列表
    datalist_read = []
    # datalist_write = []

    with open(labelfile, 'r') as f:
        for i, line in enumerate(f.readlines()):
            # 第一行为标量行，不读
            if (i > 0):
                datalist_read.append(line.strip())

    ioulist = []
    # 定义三个样本的计数
    positive_num_ = 0
    negative_num_ = 0
    part_num_ = 0

    positive_num = 0
    negative_num = 0
    part_num = 0
    for face_size in face_sizes:
        datalist_write = []  # 注意datalist清空位置，注意计数
        '''
           将已生成的图片存起来，下次不再生成'''
        pic_used_txt = pic_used_dict.get(face_size)
        if pass_used:

            pic_used_list = []

            if os.path.exists(pic_used_txt):
                with open(pic_used_txt) as f:
                    for line in f.readlines():

                        if line[0].isdigit():
                            pic_used_list.append(line.strip().split()[0])
        elif os.path.exists(pic_used_txt):
            os.remove(pic_used_txt)

        "定义三个尺寸的文件保存路径"
        # positive_savepath = os.path.join(pic_savepath, str(face_size), "positive")
        # negative_savepath = os.path.join(pic_savepath, str(face_size), "negative")
        # part_savepath = os.path.join(pic_savepath, str(face_size), "part")
        # pic_savepathlist = [negative_savepath,positive_savepath,part_savepath]
        # 定义图片保存文件夹
        # pic_savedict = {
        #     0: os.path.join(pic_savepath, str(face_size), "negative"),
        #     1: os.path.join(pic_savepath, str(face_size), "positive"),
        #     2: os.path.join(pic_savepath, str(face_size), "part")
        # }
        '''
        定义每个尺寸图片保存文件夹和标签保存文件的dict
        图片要按照置信分类，标签不用'''
        save_dict = {
            "pic": {
                0: os.path.join(pic_savepath, str(face_size), "negative"),
                1: os.path.join(pic_savepath, str(face_size), "positive"),
                2: os.path.join(pic_savepath, str(face_size), "part")
            },
            "label": os.path.join(label_savepath, "label_{0}.txt".format(str(face_size)))
        }
        # positive part negative集中放置，这几个文件夹不用生成
        # for i in [0, 1, 2]:
        #     utils.makedir(save_dict['pic'][i])

        "遍历标签数据"
        for i, strdata in enumerate(datalist_read):
            "分隔每行数据"
            data = strdata.strip().split()
            if pass_used:
                if data[0] in pic_used_list:
                    continue

            # x,y,w,h = map(int,data[1],data[2],data[3],data[4])#这个写法不行
            # x1, y1, w, h, width, height = map(int, data[1:7])#本身可以实现数据的strip()
            # x1, y1, w, h, width, height = map(int, map(str.strip,data[1:7]))#不以逗号分隔了，改为空格
            x1, y1, x2, y2, w, h, width, height = map(int, data[1:9])
            "图片过滤"
            # if (x1 < 0 or y1 < 0 or w < 0 or h < 0 or max(w, h) < 40):
            #     continue
            "判断是否有小于0的元素，框的大小是否比40小，框是否在图片内"
            if any([d < 0 for d in map(int, data[1:9])]) or min(w, h) < 40 or max(w, h) > min(width, height):
                # print("false data: ", data)
                # 输出错误数据
                continue
            '''
            补方框'''
            # "判断框是否为近方框"
            # if w / (w + h) > 0.6 or w / (w + h) < 0.3:
            #     pass
            # else:
            #     continue

            # x1, y1, x2, y2, w, h, width, height = map(int, data[1:9])

            "计算右下角坐标x1,y1和中心点x0,y0"
            # x2, y2 = x1 + w, y1 + h
            # x0, y0 = x1 + w // 2, y1 + h // 2
            for j in range(len(wh_scales)):
                k = 0
                while k < each_pic_num:
                    '''
                    name是加载图片的名称
                    pic_name 是新生成图片的名称'''

                    name, offset, box, box_ = utils.getoffset(data, wh_scale=wh_scales[j], size_scale=size_scale)

                    iou_value = utils.iou(box, box_)

                    # 生成图片文件名
                    pic_name = "%s_%d_0%1d%02d.jpg" % (
                        name.split('.')[0], face_size, j, k)  # 文件名中第二个"_"后的第一个0代表正或部分样本，另外生成的负样本此处为1

                    # 生成图片路径
                    "置信"
                    confidence = getconfidence(iou_value)

                    "图片保存路径"
                    if confidence == -1:
                        continue
                    else:
                        ioulist.append(iou_value)
                        k += 1

                    pic_savedir = os.path.join(pic_savepath, str(face_size))
                    utils.makedir(pic_savedir)
                    pic_savefile = os.path.join(pic_savedir, pic_name)

                    "标签保存路径"
                    label_savefile = os.path.join(save_dict['label'])

                    "原图片路径"
                    pic_file = os.path.join(picpath, name)

                    if treat_img:
                        utils.imgTreat(pic_file, pic_savefile, box_, face_size)

                    datalist_write.append(utils.generateLabeldata(pic_name, confidence, offset))
                    # 三个样本计数

                    if positive_range[0] < iou_value < positive_range[1]:
                        positive_num_ += 1
                    elif part_range[0] < iou_value < part_range[1]:
                        part_num_ += 1
                    elif negative_range[0] < iou_value < negative_range[1]:
                        negative_num_ += 1
                if i%100 == 0:
                    total_num_ = positive_num_ + part_num_ + negative_num_
                    print("epoch", i, positive_num_, part_num_, negative_num_, total_num_)
                    if all([positive_num_ > 0, part_num_ > 0, negative_num_ > 0, total_num_ > 0]):
                        print("positive: %.3f, %.3f" % (positive_num_ / part_num_, positive_num_ / negative_num_))
                        print("ratio: %.3f, %.3f, %.3f" % (positive_num_ / total_num_, part_num_ / total_num_,
                              negative_num_ / total_num_))  # 临时查看一下数量
                    print("********************")

            if pass_used:
                with open(pic_used_txt, 'a') as pic_used_file:
                    print(data[0], file=pic_used_file)

        if len(datalist_write) != 0 and gen_label:
            utils.generateLabel(datalist_write, label_savefile, face_size)

    # 三个样本计数
    for i in ioulist:
        if positive_range[0] < i < positive_range[1]:
            positive_num += 1
        elif part_range[0] < i < part_range[1]:
            part_num += 1
        elif negative_range[0] < i < negative_range[1]:
            negative_num += 1
    total_num = positive_num + part_num + negative_num
    print(positive_num, part_num, negative_num, total_num)

if __name__ == '__main__':
    # 清空一下save文件夹
    # preclear(savepath)
    generatedataset(pass_used=False, gen_label=False, treat_img=False)
    # generatedataset(pass_used=False, gen_label=True, treat_img=True)
    # generatedataset(pass_used=True, gen_label=True, treat_img=True)
    print(1)


# MIT License
#
# Copyright (c) 2019 Onur Dundar onur.dundar1@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import json
import numpy as np
import logging

from .detection_base_ov import InferenceBase, InferenceConfig

class AgeGenderDetectionTypes:
    MTCNN = "mtcnn_age_gender"
    OPENMODELZOO = "omz_age_gender"

class AgeGenderConfig(InferenceConfig):
    ModelType = AgeGenderDetectionTypes.OPENMODELZOO

    def parse_json(self, json_file):
        try:
            logging.log(logging.INFO, "Loading JSON File {}".format(json_file))
            logging.log(logging.INFO, "Model Type {}".format(self.ModelType))

            with open(json_file) as json_file:
                data = json.load(json_file)

                self.ModelPath = data[self.ModelType]["model_path"]
                self.ModelName = data[self.ModelType]["model_name"]

                self.TargetDevice = data[self.ModelType]["target_device"]

                if data[self.ModelType]["async"] == "True":
                    self.Async = True

                self.RequestCount = int(data[self.ModelType]["request_count"])

                self.BatchSize = int(data[self.ModelType]["batch_size"])

                if data[self.ModelType]["cpu_extension"] == "True":
                    self.CpuExtension = True

                self.CpuExtensionPath = data[self.ModelType]["cpu_extension_path"]

                if data[self.ModelType]["dynamic_batch"] == "True":
                    self.DynamicBatch = True

                if data[self.ModelType]["limit_cpu_threads"] == "True":
                    self.LimitCPUThreads = True

                self.CPUThreadNum = int(data[self.ModelType]["number_of_cpu_threads"])

                if data[self.ModelType]["bind_cpu_threads"] == "True":
                    self.LimitCPUThreads = True

                self.CPUStream = data[self.ModelType]["cpu_stream"]

        except FileNotFoundError:
            logging.log(logging.ERROR, '{} FileNotFound'.format(json_file))
            exit(-1)

    def read_dict(self, data=None):
        """
        Used When JSON Already Parsed as Dict
        :return:
        """
        if data is None:
            data = dict()
        self.ModelPath = data[self.ModelType]["model_path"]
        self.ModelName = data[self.ModelType]["model_name"]

        self.TargetDevice = data[self.ModelType]["target_device"]

        if data[self.ModelType]["async"] == "True":
            self.Async = True

        self.RequestCount = int(data[self.ModelType]["request_count"])

        self.BatchSize = int(data[self.ModelType]["batch_size"])

        if data[self.ModelType]["cpu_extension"] == "True":
            self.CpuExtension = True

        self.CpuExtensionPath = data[self.ModelType]["cpu_extension_path"]

        if data[self.ModelType]["dynamic_batch"] == "True":
            self.DynamicBatch = True

        if data[self.ModelType]["limit_cpu_threads"] == "True":
            self.LimitCPUThreads = True

        self.CPUThreadNum = int(data[self.ModelType]["number_of_cpu_threads"])

        if data[self.ModelType]["bind_cpu_threads"] == "True":
            self.LimitCPUThreads = True

        self.CPUStream = data[self.ModelType]["cpu_stream"]

class MTCNNAgeGenderConfig(AgeGenderConfig):
    ModelType = AgeGenderDetectionTypes.MTCNN

class MTCNNAgeGenderDetection(InferenceBase):

    Config = MTCNNAgeGenderConfig()

    def get_age_gender_data(self, request_id=0):
        """
        Parse Output Data for Age-Gender Detection Model
        :param request_id:
        :return:
        """
        detection = self.OpenVinoExecutable.requests[request_id].outputs[self.OutputLayer]
        # Parse detection vector to get age and gender
        gender_vector = detection[:, 0:2].flatten()
        gender = int(np.argmax(gender_vector))

        gender_text = 'female'
        if gender == 1:
            gender_text = 'male'

        age_matrix = detection[:, 2:202].reshape((100, 2))
        ages = np.argmax(age_matrix, axis=1)
        age = int(sum(ages))

        return age, gender_text

class AgeGenderDetection(InferenceBase):

    Config = AgeGenderConfig()

    def get_age_gender_data(self, request_id=0):
        """
        Parse Output Data for Age-Gender Detection Model
        :param request_id:
        :return:
        """
        age = int(self.OpenVinoExecutable.requests[request_id].outputs["age_conv3"][0][0][0][0] * 100)
        genders = self.OpenVinoExecutable.requests[request_id].outputs["prob"]
        # Parse detection vector to get age and gender

        gender_text = 'female'
        if genders[0][0][0][0] < genders[0][1][0][0]:
            gender_text = 'male'

        return age, gender_text


import unittest

from helixbilling.test.logic.logic_test import LogicTestCase

class LogoutTestCase(LogicTestCase):
    def test_logout(self):
        from helixcore.test.logic import access_granted #@UnusedImport
        req = {'session_id': 's'}
        resp = self.logout(**req)
        self.check_response_ok(resp)

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