# -*- coding: utf-8 -*-
"""
This programs runs various classification models
on the moons data set. Each run results in a set
of PNG files displaying the result from the model
and parameters that were used. The PNG files are
saved in the system temp folder.

@author: Brian Munksgaard.
"""
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.datasets import make_moons
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import svm
from sklearn import linear_model
from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN

#
# Returns a string with parameters of the
# specified MLP model. Each parameter is
# separated by a newline character.
#
def get_mlp_legend(p_mlp_model):
    legend = "hl: " + str(p_mlp_model.hidden_layer_sizes)
    legend = legend + "\nmax_iter: " + str(p_mlp_model.max_iter)
    legend = legend + "\nbatch_size: " + str(p_mlp_model.batch_size)
    legend = legend + "\nrandom_state: " + str(p_mlp_model.random_state)
    legend = legend + "\nactivation: " + p_mlp_model.activation
    legend = legend + "\nalpha: " + str(p_mlp_model.alpha)
    return legend

#
# Returns a string with parameters of the
# specified SVC model. Each parameter is
# separated by a newline character.
#
def get_svc_legend(p_svc_model):
    legend = "kernel: " + p_svc_model.kernel
    legend = legend + "\ngamma: " + str(p_svc_model.gamma)
    legend = legend + "\nC: " + str(p_svc_model.C)
    legend = legend + "\ndegree: " + str(p_svc_model.degree)
    legend = legend + "\nrandom_state: " + str(p_svc_model.random_state)
    return legend

#
# Returns a string with parameters of the
# specified logistic model. Each parameter is
# separated by a newline character.
#
def get_log_legend(p_log_model):
    legend = "C: " + str(p_log_model.C)
    legend = legend + "\nmax_iter: " + str(p_log_model.max_iter)
    legend = legend + "\nrandom_state: " + str(p_log_model.random_state)
    return legend

#
# Returns a string with parameters of the
# specified KMEANS model. Each parameter is
# separated by a newline character.
#
def get_kmeans_legend(p_k_model):
    legend = "clusters: " + str(p_k_model.n_clusters)
    legend = legend + "\nmax_iter: " + str(p_k_model.max_iter)
    legend = legend + "\nrandom_state: " + str(p_k_model.random_state)
    return legend

#
# Split data into training & test and perform feature scaling.
#
def split_and_scale(x_data, y_data):
    # Split in train and test set
    x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.20)

    # Perform feature scaling.
    scaler = StandardScaler()
    scaler.fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)

    return x_train, x_test, y_train, y_test

#
# Displays a mesh plot with test data and predictions.
# Parameters:
#   p_title: The title of the plot.
#   p_legend: A text with the parameters used in the model.
#   p_x_test: All the test values for x.
#   p_y_test: All the test values for y.
#   p_model: The model itself.
#
def display_mesh(p_title, p_legend, p_x_test, p_y_test, p_model):
    h = .02  # Step size in th mesh.

    x_min, x_max = p_x_test[:, 0].min() - 1, p_x_test[:, 0].max() + 1
    y_min, y_max = p_x_test[:, 1].min() - 1, p_x_test[:, 1].max() + 1

    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    plt.subplot(1, 1, 1)
    plt.subplots_adjust(wspace=0.4, hspace=0.4)
    z = p_model.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    z = z.reshape(xx.shape)
    plt.contourf(xx, yy, z, cmap=plt.cm.coolwarm, alpha=0.8)

    # Plot the training points
    plt.scatter(p_x_test[:, 0], p_x_test[:, 1], c=p_y_test)

    # Title and model parameters.
    plt.title(p_title)
    x_pos = x_max / 2
    y_pos = y_min
    plt.text(x_pos, y_pos, p_legend, bbox=dict(boxstyle="square", facecolor="white"))

    # Save the figure in the system temp folder.
    filename = "/tmp/" + p_title + "-" + p_legend.replace("\n", ", ").replace(":", "->") + ".png"
    print("Filename: " + filename)
    plt.savefig(filename)

    # Execute display and save.
    plt.show()

#
# This procedure is used to train and display
# the results of an MLP model.
#
def train_and_display_mlp(p_mlp_model, x_data, y_data):
    x_train, x_test, y_train, y_test = split_and_scale(x_data, y_data)
    p_mlp_model.fit(x_train, y_train)
    display_mesh("Model type - MLP", get_mlp_legend(p_mlp_model), x_test, y_test, p_mlp_model)

#
# This procedure is used to train and display
# the results of a SVC model.
#
def train_and_display_svc(p_svc_model, x_data, y_data):
    x_train, x_test, y_train, y_test = split_and_scale(x_data, y_data)
    p_svc_model.fit(x_train, y_train)
    display_mesh("Model type - SVC", get_svc_legend(p_svc_model), x_test, y_test, p_svc_model)

#
# This procedure is used to train and display
# the results of a logistic regression model.
#
def train_and_display_log(p_log_model, x_data, y_data):
    x_train, x_test, y_train, y_test = split_and_scale(x_data, y_data)
    p_log_model.fit(x_train, y_train)
    display_mesh("Model type - LOG", get_log_legend(p_log_model), x_test, y_test, p_log_model)

#
# This procedure is used to train and display
# the results of a KMEANS model.
#
def train_and_display_kmeans(p_k_model, x_data, y_data):
    x_train, x_test, y_train, y_test = split_and_scale(x_data, y_data)
    p_k_model.fit(x_train)
    display_mesh("Model type - KMEANS", get_kmeans_legend(p_k_model), x_test, y_test, p_k_model)

# -------------------------------------------
# Generate moons 2d classification data set
# -------------------------------------------
X, y = make_moons(n_samples=1000, noise=0.1)

# Visualize the data set.
plt.figure()
colormap = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=colormap,edgecolor='black', s=20)

filename = "/tmp/" + "moons_data.png"
print("Filename: " + filename)
plt.savefig(filename)
plt.show()

#
# Now run the various models.
#

# ----------------------
# MLP models.
# ----------------------
#mlp = MLPClassifier(hidden_layer_sizes=(4, 4), max_iter=1000)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(8, 8), max_iter=1000)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(4, 4, 4, 4), max_iter=1000)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(8, 8, 8), max_iter=1000)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(8, 8, 8, 8), max_iter=1000)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(10, 10, 10, 10), max_iter=1000, alpha=0.01)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(10, 10, 10, 10), max_iter=1000, alpha=1.0)
#train_and_display_mlp(mlp, X, y)

#mlp = MLPClassifier(hidden_layer_sizes=(10, 10, 10, 10), max_iter=1000, alpha=1.0, activation='tanh')
#train_and_display_mlp(mlp, X, y)

# ----------------------------
# Logistic regression models.
# ----------------------------
#logreg = linear_model.LogisticRegression(C=1000)
#train_and_display_log(logreg, X, y)

# ----------------------------
# KMeans clustering models.
# ----------------------------
#kmeans = KMeans(n_clusters=4, random_state=0)
#train_and_display_kmeans(kmeans, X, y)

# ----------------------------
# SVC clustering models.
# ----------------------------
#svc = svm.SVC(kernel='linear', gamma=0.7, C=1)
#train_and_display_svc(svc, X, y)

svc = svm.SVC(kernel='poly', gamma=3.7, C=2, degree=5)
train_and_display_svc(svc, X, y)

# RBF.
# Vary C.
svc = svm.SVC(kernel='rbf', gamma=0.7, C=1)
train_and_display_svc(svc, X, y)

svc = svm.SVC(kernel='rbf', gamma=0.7, C=5)
train_and_display_svc(svc, X, y)

svc = svm.SVC(kernel='rbf', gamma=0.7, C=0.2)
train_and_display_svc(svc, X, y)

# Vary gamma.
svc = svm.SVC(kernel='rbf', gamma=0.3, C=1)
train_and_display_svc(svc, X, y)

svc = svm.SVC(kernel='rbf', gamma=2, C=1)
train_and_display_svc(svc, X, y)


import os
from unittest import TestCase
from models.item import ItemModel
from models.store import StoreModel
from db import reset_db, Session

class TestItem(TestCase):
    def setUp(self):
        reset_db()
        self.sess = Session()

    def tearDown(self):
        self.sess.close()

    def test_crud(self):
        store = StoreModel('testStore')
        item = ItemModel(name='test', price=12.99, store_id=1)
        self.assertEqual(self.sess.query(ItemModel).filter(ItemModel.name=='test').count(), 0)

        self.sess.add(store)
        self.sess.add(item)
        self.sess.commit()
        self.assertEqual(self.sess.query(ItemModel).filter(ItemModel.name=='test').count(), 1)

        self.sess.delete(item)
        self.sess.commit()
        self.assertEqual(self.sess.query(ItemModel).filter(ItemModel.name=='test').count(), 0)
        self.sess.delete(store)
        self.sess.commit()


from PyQt5.QtWidgets import *
from library.open_api import *

class OpenTest:
    def __init__(self):
        self.open_api = open_api()
        self.open_api.py_gubun = "collector"
        self.run()

    def run(self):
        # Part1. 기본적인 OpenAPI 메소드 활용 () -----------------------------------------------------------------------
        # koastudio -> 파일 -> OpenAPI 접속 -> 로그인(모의투자만 접속 가능)  ** 개발가이드 읽어보시면 도움이 됩니다. (조회 제한 정책 등)

        # *종목정보함수(koastudio 개발가이드/기타함수/종목정보관련함수 참고) : GetCodeListByMarket, GetMasterListedStockCnt ...
        # dynamicCall : OpenAPI 메소드를 호출 하기 위한 함수
        #               (PyQt5 패키지의 QAxContainer 모듈에 포함 - open_api.py 파일 상단의 from PyQt5.QAxContainer import *)
        # BSTR : 문자열로 인자 값을 보내라(Qstring)
        ex1 = self.open_api.dynamicCall('GetCodeListByMarket(QString)', '8')
        print(f"ex1 -- ETF 종목코드 리스트: {ex1}")

        ex2 = self.open_api.dynamicCall('GetMasterCodeName(QString)', '005930')
        print(f"ex2 -- 005930 종목명: {ex2}")

        ex3 = self.open_api.dynamicCall('GetMasterListedStockCnt(QString)', '005930')
        print(f"ex3 -- 삼성전자 상장 주식수: {ex3}")

        ex4 = self.open_api.dynamicCall('GetMasterConstruction(QString)', '005930')
        print(f"ex4 -- 삼성전자 감리 구분: {ex4}")

        ex5 = self.open_api.dynamicCall('GetMasterStockState(QString)', '005930')
        print(f"ex5 -- 삼성전자 증거금 비율, 거래정지여부, 관리종목여부 등(: {ex5}")

        # *테마관련함수(키움 OpenAPI+ 개발가이드 매뉴얼 참고 ** koastudio에는 가이드가 없음)
        # LONG : 정수형으로 인자 값을 보내라(int)
        ex6 = self.open_api.dynamicCall('GetThemeGroupList(int)', 1)
        print(f"ex6 -- 테마코드, 테마명 리스트: {ex6}")

        # 종목 코드에 알파벳이 붙어서 나오기 때문에 사용 시 가공이 필요함
        ex7 = self.open_api.dynamicCall('GetThemeGroupCode(QString)', '141')
        print(f"ex7 -- 141 테마코드(2차전지_소재)에 해당하는 종목코드: {ex7}")

        # get_theme_group_code 함수는 제공 되는 것이 아닌 사용자가 직접 만들어야함
        ex8 = self.open_api.get_theme_group_code('141')
        print(f"ex8 --  ex7 가공 버전: {ex8}")

        ex9 = self.open_api.get_theme_info()
        print(f"ex9 -- ex6, ex8 혼합 버전 (테마코드, 테마명 그리고 테마 그룹에 속하는 종목코드 리스트): {ex9}")

        # *특수함수(koastudio 개발가이드/기타함수/특수함수 참고)
        ex10 = self.open_api.KOA_Functions('GetMasterStockInfo', '005930')
        print(f"ex10 -- 주식종목 시장구분, 종목분류등 : {ex10}")
        # -----------------------------------------------------------------------------------------------------------

        # Part2. TR을 활용한 데이터 수집 -----------------------------------------------------
        # => CommRqData OpenAPI 메소드 활용
        # TR(Transaction) :  어떤 일을 하기위한 작업들의 모음. (서버에 데이터를 요청 -> 수신)
        print("ex11 -- 예수금 출력 ")
        # koastudio 좌측 하단 TR목록 / 'opw00001:예수금상세현황조회' 클릭 후 샘플 참고
        self.open_api.get_d2_deposit()

        print("ex12 -- 삼성전자 주가 데이터 ")
        # koastudio 좌측 하단 TR목록 / 'opt10081:주식일봉차트조회요청' 클릭 후 샘플 참고
        # get_total_data : 특정 종목의 1985년 이후 특정 날짜까지의 주가 데이터를 모두 가져오는 함수
        data = self.open_api.get_total_data('005930', '삼성전자', '20200424')
        print(data)

        print("ex13 -- 삼성전자 재정 데이터 ")
        # koastudio 좌측 하단 TR목록 / 'opt10001:주식기본정보요청' 클릭 후 샘플 참고
        data2 = self.open_api.get_stock_finance('005930')
        print(data2)
        # -----------------------------------------------------------------------------------------------------------

if __name__ == "__main__":
    app = QApplication(sys.argv)
    OpenTest()


import os
import sys
import datetime
import traceback
import pandas as pd
import numpy as np
import utils
sys.path.append('/bearbin-data/home/sdou/gdrive/segpy/')
from segpy.reader import create_reader

UTC = datetime.timezone.utc

class PeekSegy:
    def __init__(self, file_segy):
        self.file_segy = file_segy
        with open(file_segy, 'rb') as f:
            try:
                self.segy_reader = create_reader(f)
            except UnicodeDecodeError as ue:
                try:
                    self.segy_reader = create_reader(f, encoding='cp037')
                except UnicodeDecodeError('encoding not deciphered: neither ascii nor cp037') as ue:
                    traceback.print_tb(ue.__traceback__)
                    sys.exit(1)

    @staticmethod
    def get_timestamp(file_segy):
        """
        Get time stamp from segy file name
        :param file_segy: full path of input segy file
        :return: time stamp as datetime object
        :how to use:
         PeekSegy.get_timestamp(file_segy)
        """
        filename = os.path.splitext(os.path.basename(file_segy))[0]
        # Select the longest string
        timestamp_str = max(filename.split('_'), key=len)
        dateformat = {12: '%y%m%d%H%M%S', 14:'%Y%m%d%H%M%S'}
        return datetime.datetime.strptime(
                    timestamp_str, dateformat[len(timestamp_str)]
                ).replace(tzinfo=UTC)

    @property
    def filesize(self):
        return utils.filesize(self.file_segy)

    @property
    def dt(self):
        file_header = self.segy_reader.binary_reel_header
        return 1.0e-6 * getattr(file_header, 'sample_interval')

    @property
    def shape(self):
        n_samples = self.segy_reader.max_num_trace_samples()
        n_traces = self.segy_reader.num_traces()
        return (n_samples, n_traces)

    @property
    def duration(self):
        return self.dt*(self.shape[0] - 1)

    @property
    def tb(self):
        trace_header = self.segy_reader.trace_header(0)
        th_list = ['year_recorded', 'day_of_year', 'hour_of_day',
                   'minute_of_hour', 'second_of_minute']
        year, doy, hour, min, sec = (
            getattr(trace_header, th)  for th in th_list
        )
        return (
            datetime.datetime(year=year, month=1, day=1, tzinfo=UTC) +
            datetime.timedelta(
                days=doy - 1, hours=hour, minutes=min, seconds=sec
            )
        )

    @property
    def te(self):
        return self.tb + datetime.timedelta(seconds=self.duration)

class SegyToSeis:
    def __init__(self, file_segy, tmin=0, tmax=None, trace_indexes=None):
        try:
            self.param = dict(tmin=tmin, tmax=tmax, trace_indexes=trace_indexes)
            self.segy = PeekSegy(file_segy)
            if tmin > self.segy.duration:
                raise ValueError('tmin larger than duration time')
            elif tmax and tmin >= tmax:
                raise ValueError('tmax must be larger than tmin')
        except ValueError as ve:
            traceback.print_tb(ve.__traceback__)
            sys.exit(1)

    def _t_index(self):
        i_tmin = 0
        i_tmax = self.segy.shape[0]
        t = self.segy.dt * np.arange(self.segy.shape[0])
        if self.param['tmin'] is not None:
            i_tmin = utils.nearest_index(t, self.param['tmin'])
        if self.param['tmax'] is not None:
            i_tmax = utils.nearest_index(t, self.param['tmax'])
        return (i_tmin, i_tmax)

    @property
    def trace_indexes(self):
        if self.param['trace_indexes'] is not None:
            return self.param['trace_indexes']
        else:
            return list(self.segy_reader.trace_indexes())

    @property
    def t(self):
        (i_min, i_max) = self._t_index()
        return self.segy.dt * np.arange(i_min, i_max)

    def _tb(self):
        tmin = self.param['tmin']
        if (tmin is not None) and (tmin > 0):
            return self.segy.tb + datetime.timedelta(seconds=tmin)
        else:
            return self.segy.tb

    def _te(self):
        tmax = self.param['tmax']
        if (tmax is not None) and (tmax < self.segy.duration):
            return self.segy.te - datetime.timedelta(
                seconds=(self.segy.duration - tmax)
            )
        else:
            return self.segy.te

    def _duration(self):
        return (self._te() - self._tb()).total_seconds()

    def _shape(self):
        return (len(self.t), len(self.trace_indexes))

    @property
    def dat(self):
        segy_reader = self.segy.segy_reader
        (i_tmin, i_tmax) = self._t_index()
        self.__dat = np.zeros(self._shape(), dtype=float)
        for i, trace_index in enumerate(self.trace_indexes):
            self.__dat[:, i] = segy_reader.trace_samples(
                trace_index, start=i_tmin, stop=i_tmax
            )
        return self.__dat

    @property
    def fh(self):
        return pd.Series(
            dict(tb=self._tb(), te=self._te(), dt=self.segy.dt,
                 duration=self._duration(),
                 n_samples=self._shape()[0], n_traces=self._shape()[1])
        )

    def to_Series(self):
        """
        Output bare-bone data as pandas Series to memory
        :return: pandas Series; fields = [fh, t, trace_indexes, dat]
        """
        return pd.Series(
            dict(fh=self.fh, t=self.t, trace_indexes=self.trace_indexes,
                 dat=self.dat)
        )

    def to_pickle(self, path):
        """
        Save bare-bone data as pickle
        :param path: full path of output pickle file
        :return: None
        """
        self.to_Series().to_pickle(path)
        return None


"""
    Find the largest circle that does not contain any duplicate tags
"""
from math import hypot

def solution(S, X, Y):
    radii = list(map(hypot, X, Y))
    tags = list(zip(radii, S))
    tags.sort()
    print (tags)

    found_tags = []
    found_circles = []
    for i in tags:
        if i[1] in found_tags:
            exclude = i[0]
            break
        found_tags.append(i[1])
        found_circles.append(i)

    final_circles = list(filter(lambda x: x[0] != exclude, found_circles))

    return len(final_circles)

def main():

    S = "ABDCA"
    X = [2, -1, -4, -3, 3]
    Y = [2, -2, 4, 1, -3]
    print (solution(S, X, Y))       # 3

    print('---')

    S = 'ABCDBE'
    X = [1, 2, 3, -3, 3, 4]
    Y = [1, 2, 3, -3, -3, 4]
    print (solution(S, X, Y))       # 2

    print('---')

    S = 'ACFHEF'            # E and the second F occur at the same radius, so E should be excluded
    X = [1, 2, 3, 4, 5, 5]  # it's tricky bc "E" sorts alphabetically before "F"
    Y = [1, 2, 3, 4, 5, 5]
    print(solution(S, X, Y))        # 4

if __name__ == '__main__':
    main()


import RPi.GPIO as GPIO
import time

dataPin = 18
sendOutPin = 23
clockPin = 24
resetPin = 25

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(dataPin, GPIO.OUT)
GPIO.setup(clockPin, GPIO.OUT)
GPIO.setup(sendOutPin, GPIO.OUT)
GPIO.setup(resetPin, GPIO.OUT)

numbers = [
    [0, 1, 0, 0, 0, 1, 0, 0],  # 0
    [1, 1, 1, 1, 0, 1, 0, 1],  # 1
    [1, 0, 0, 0, 1, 1, 0, 0],  # 2
    [1, 0, 1, 0, 0, 1, 0, 0],  # 3
    [0, 0, 1, 1, 0, 1, 0, 1],  # 4
    [0, 0, 1, 0, 0, 1, 1, 0],  # 5
    [0, 0, 0, 0, 0, 1, 1, 0],  # 6
    [1, 1, 1, 1, 0, 1, 0, 0],  # 7
    [0, 0, 0, 0, 0, 1, 0, 0],  # 8
    [0, 0, 1, 0, 0, 1, 0, 0],  # 9
    [1, 1, 1, 1, 1, 0, 1, 1],  # .
]

def set_pin_value(pin_number, value):
    GPIO.output(pin_number, value)
    time.sleep(0.01)

def add_data_to_register(value):
    set_pin_value(dataPin, value)
    set_pin_value(clockPin, GPIO.HIGH)
    set_pin_value(clockPin, GPIO.LOW)
    set_pin_value(dataPin, GPIO.LOW)

def send_data_out():
    set_pin_value(sendOutPin, GPIO.HIGH)
    set_pin_value(sendOutPin, GPIO.LOW)

def send_number(number):
    data = numbers[number]
    for x in data:
        add_data_to_register(x)
    send_data_out()

def clear_any_number():
    set_pin_value(resetPin, GPIO.LOW)
    set_pin_value(resetPin, GPIO.HIGH)
    data = [1, 1, 1, 1, 1, 1, 1, 1]
    for x in data:
        add_data_to_register(x)
    send_data_out()

def start():
    print('\n-----------------------------------------------------------------')
    print('Insert the number between 0 and 9 that you would like to display:')
    digit = input()
    print('Displaying number: ', digit)
    vector = numbers[digit]
    print('Using vector:', vector)
    send_number(digit)
    start()

clear_any_number()
start()


import csv
import http.client
import logging
import os
import redis
from zipfile import ZipFile
import schedule
import time

def get_bhavcopy():
    conn = http.client.HTTPSConnection("www.bseindia.com")
    payload = ''
    headers = {}
    try:
        conn.request("GET", "/download/BhavCopy/Equity/EQ290421_CSV.ZIP",
                     payload,
                     headers)
        res = conn.getresponse()
        data = res.read()
        with open('bhavcopy.zip', 'wb') as fd:
            fd.write(data)
        zf = ZipFile("./bhavcopy.zip")
        zf.extractall(path='extraction_bhavcopy')
        zf.close()
    except:
        return False
    return True

def poll():
    print('polling started')
    if get_bhavcopy():
        conn = redis.Redis('localhost')
        try:
            for (dirpath, dirnames, filenames) in os.walk(
                    'extraction_bhavcopy'):

                if filenames:
                    with open("/".join([dirpath, filenames[0]]),
                              mode='r') as infile:

                        for line in csv.DictReader(infile):
                            conn.hset("zerodha" + dict(line)['SC_NAME'].strip(),

                                      mapping=dict(line))

        except Exception as e:
            logging.exception(e)

if __name__ == '__main__':

    schedule.every().day.at("17:30").do(poll, 'It is 17:30')

    while True:
        schedule.run_pending()
        time.sleep(60*60)  # wait one minute


import os

from storybro.transcripts.transcript import Transcript
from storybro.utils import find_files

class DuplicateTranscriptError(Exception):
    def __init__(self, transcript_name):
        self.transcript_name = transcript_name

class TranscriptManager:
    def __init__(self, root_path):
        self.root_path = root_path

    @property
    def transcripts(self):
        scripts = dict()

        for script_file in find_files(self.root_path):
            script_path = os.path.join(self.root_path, script_file)
            scripts[script_file] = Transcript.fromJSON(script_path)

        return scripts

    def new_transcript(self, name):
        script = self.transcripts.get(name)

        if script:
            raise DuplicateTranscriptError(name)

        script_path = os.path.join(self.root_path, name)

        return Transcript(script_path)


"""This module saves visual representations of binary sequences.

The sequences are done through constant addition.
"""
import matplotlib.pyplot as plt
from math import sqrt, log

def to_binary(num: int) -> str:
    """Convert int to string form of binary sequence"""
    return bin(num)[2:]

def binary_image(num: int, step=1):
    """Print a plot representing a simple sequence of binary numbers.

    Step corresponds to how much to add"""
    grid = []
    for i in range(num//step):
        grid.append([0]*int(log(num, 2)+1))
        for j, char in enumerate(to_binary(i*step)):
            grid[i][j] = float(char)
    fig1 = plt.figure(figsize=(25,100))
    plt.pcolormesh(grid[::-1], cmap='Greens')
    plt.axis('off')
    plt.show()
    plt.draw()
    fig1.savefig(f'binary_image_addition_range({num})_stepsize({step}).png', dpi=100)

# The visual pattern remains the same regardless of the stepsize you use.
# Evenly spaced sequences in binary seem to have a universal visual pattern.
binary_image(500, step=1)
binary_image(500*3, step=3)
binary_image(500*13, step=13)
binary_image(500*25, step=25)
binary_image(500*87, step=87)


import yaml
import io

import os, django

# from django.contrib.gis.geoip2.resources import Country

os.environ['DJANGO_SETTINGS_MODULE'] = 'portfolio_ecom.settings'
django.setup()
from core.models import Category, Country

# data = {
# 	'a list': [
# 		1,
# 		42,
# 		3.141,
# 		1337,
# 		'help',
# 		u'€'
# 	],
# 	'a string': 'bla',
# 	'another dict': {
# 		'foo': 'bar',
# 		'key': 'value',
# 		'the answer': 42
# 	}
# }

def write_data(data):
	"""
	:type data: dict
	"""
	with io.open('data.yaml', 'w', encoding='utf8') as outfile:
		try:
			yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True)
		except yaml.YAMLError as exc:
			print(exc)

# return dict
def get_file_data() -> dict:
	with open('data.yaml', 'r') as stream:
		try:
			return yaml.safe_load(stream)
		except yaml.YAMLError as exc:
			print(exc)

class CategoryData:
	name = 'core_category'

	@classmethod
	def tofile(cls, data: dict = None):
		arr = []
		for r in Category.objects.all():
			arr.append({
				'id': r.id,
				'name': r.name,
				'parent': r.parent,
				'is_selectable': r.is_selectable
			})
		write_data({cls.name: arr})

	@classmethod
	def todb(cls, data: dict = None):
		for i in get_file_data()[cls.name]:
			Category(**i).save()

class CountryData:
	name = 'core_country'

	@classmethod
	def tofile(cls, data: dict = None, override=False):
		if data is None:
			arr = []
			for r in Category.objects.all():
				arr.append({
					'id': r.id,
					'name': r.name,
				})
			write_data({cls.name: arr})
		else:
			yaml_data = get_file_data()
			if not override:  # if append
				if not cls.name in yaml_data:
					yaml_data[cls.name] = []
					for index, item in enumerate(data):
						item['id'] = index
						yaml_data[cls.name].append(item)
				else:
					max2 = 0
					if len(yaml_data[cls.name]) > 0:
						arr = []
						for item in yaml_data[cls.name]:
							arr.append(float(item['id']))
						max2 = max(arr) + 1

					for index, item in enumerate(data):
						item['id'] = index + max2
						yaml_data[cls.name].append(item)
			else:
				yaml_data[cls.name] = []
				for index, item in enumerate(data):
					item['id'] = index
					yaml_data[cls.name].append(item)

	@classmethod
	def todb(cls, data: list = None, override=False):
		if data is None:
			for i in get_file_data()[cls.name]:
				Country(**i).save()
		else:
			if not override:  # if append
				for i in data:
					if 'id' in i:
						raise Exception('can\'t override with id, remove the id')
					Country(**i).save()
			else:
				for i in data:
					if 'id' not in i:
						raise Exception('if you want to override you need id')
					el = Country.objects.get(id=i['id'])
					el.name = i['name']

# CategoryData.tofile()
# CategoryData.todb()

# test if exist replace else create yaml
# data = get_file_data()
# data['sambal'][2] = {
# 	'wow': '555555555555555',
# 	'wow111111111111': 'dam',
# 	'wow1111111112': 'dam',
# 	'wodddddddw3': 'dam',
# }
#
# write_data(data)

CountryData.todb([
	{'name': 'Sri Lanka', },
	{'name': 'India', },
	{'name': 'USA', },
	{'name': 'Germany', },
])


GATE_IMAGES = {
    'draft_files': {
        'ATL': 'atl.png',
        'APL': '',
        'ATR': 'atr.png',
        'APL': '',
        'BPL': '',
        'BPL': '',
        'CPL': '',
        'CPL': '',
        'DPL': '',
        'DPL': '',
        'EPL': '',
        'EPL': '',
        'FPL': '',
        'FPL': '',
    },
    'beam_files': {
        "Без опорной балки": 'xxx.png',
        "Опорная балка": 'xxx.png',
        "Продольная закладная": 'xxx.png',
        "Нестандарт": 'xxx.png'
    }
}


#--- Exercício 5 - Funções
#--- Crie uma função para calculo de raiz
#--- A função deve ter uma variável que deternine qual é o indice da raiz(raiz quadrada, raiz cubica...)
#--- Leia um número do console, armazene em uma variável e passe para a função
#--- Realize o calculo da raiz e armazene em uma segunda variável e retorne
#--- Imprima o resultado e uma mensagem usando f-string, fora da função

#Importa biblioteca e define função;
import math
def calcularRaiz(num, indice):
    raiz = float(math.pow(num, 1/indice))
    return raiz

#Entrada de parâmetros;
print(f"**      Cálculo de raiz        **")
num = float(input("Digite o valor: "))
indice = int(input("Determine o índice da raiz: "))

#chamada da função;
raiz = calcularRaiz(num, indice)
print(f"A raiz é {raiz:.2f}")


import glob
import os
import SimpleITK as sitk
import cv2
import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O(e.g. pd.read_csv)
import pydicom
import pydicom.uid
# tkinter is needed to before PIL.Image
from tkinter.filedialog import *
import PIL.Image as Image
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# from pydicom.pixel_data_handlers.gdcm_handler import get_pixeldata
from scipy import ndimage as ndi
from matplotlib import pyplot as plt
from skimage import measure

# from pylab import *

"""Hounsfield Unit = pixel_value * rescale_slope + rescale_intercept"""
join = os.path.join
path = "/home/sj/workspace/data/chao_data/Train_Sets/CT/1/DICOM_anon/"

have_numpy = True

try:
    import numpy
except ImportError:
    have_numpy = False
    raise

sys_is_little_endian = (sys.byteorder == 'little')

NumpySupportedTransferSyntaxes = [
    pydicom.uid.ExplicitVRLittleEndian,
    pydicom.uid.ImplicitVRLittleEndian,
    pydicom.uid.DeflatedExplicitVRLittleEndian,
    pydicom.uid.ExplicitVRBigEndian,
]

# 支持"传输"语法
def supports_transfer_syntax(dicom_dataset):
    return (dicom_dataset.file_meta.TransferSyntaxUID in
            NumpySupportedTransferSyntaxes)

def needs_to_convert_to_RGB(dicom_dataset):
    return False

def should_change_PhotometricInterpretation_to_RGB(dicom_dataset):
    return False

def get_pixeldata(dicom_dataset):
    """If NumPy is available, return an ndarray of the Pixel Data.
    Raises
    ------
    TypeError
        If there is no Pixel Data or not a supported data type.
    ImportError
        If NumPy isn't found
    NotImplementedError
        if the transfer syntax is not supported
    AttributeError
        if the decoded amount of data does not match the expected amount
    Returns
    -------
    numpy.ndarray
       The contents of the Pixel Data element (7FE0,0010) as an ndarray.
    """
    if (dicom_dataset.file_meta.TransferSyntaxUID not in
            NumpySupportedTransferSyntaxes):
        raise NotImplementedError("Pixel Data is compressed in a "
                                  "format pydicom does not yet handle. "
                                  "Cannot return array. Pydicom might "
                                  "be able to convert the pixel data "
                                  "using GDCM if it is installed.")

    if not have_numpy:
        msg = ("The Numpy package is required to use pixel_array, and "
               "numpy could not be imported.")
        raise ImportError(msg)
    if 'PixelData' not in dicom_dataset:
        raise TypeError("No pixel data found in this dataset.")

    # Make NumPy format code, e.g. "uint16", "int32" etc
    # from two pieces of info:
    # dicom_dataset.PixelRepresentation -- 0 for unsigned, 1 for signed;
    # dicom_dataset.BitsAllocated -- 8, 16, or 32
    if dicom_dataset.BitsAllocated == 1:
        # single bits are used for representation of binary data
        format_str = 'uint8'
    elif dicom_dataset.PixelRepresentation == 0:
        format_str = 'uint{}'.format(dicom_dataset.BitsAllocated)
    elif dicom_dataset.PixelRepresentation == 1:
        format_str = 'int{}'.format(dicom_dataset.BitsAllocated)
    else:
        format_str = 'bad_pixel_representation'
    try:
        numpy_dtype = numpy.dtype(format_str)
    except TypeError:
        msg = ("Data type not understood by NumPy: "
               "format='{}', PixelRepresentation={}, "
               "BitsAllocated={}".format(
            format_str,
            dicom_dataset.PixelRepresentation,
            dicom_dataset.BitsAllocated))
        raise TypeError(msg)

    if dicom_dataset.is_little_endian != sys_is_little_endian:
        numpy_dtype = numpy_dtype.newbyteorder('S')

    pixel_bytearray = dicom_dataset.PixelData

    if dicom_dataset.BitsAllocated == 1:
        # if single bits are used for binary representation, a uint8 array
        # has to be converted to a binary-valued array (that is 8 times bigger)
        try:
            pixel_array = numpy.unpackbits(
                numpy.frombuffer(pixel_bytearray, dtype='uint8'))
        except NotImplementedError:
            # PyPy2 does not implement numpy.unpackbits
            raise NotImplementedError(
                'Cannot handle BitsAllocated == 1 on this platform')
    else:
        pixel_array = numpy.frombuffer(pixel_bytearray, dtype=numpy_dtype)
    length_of_pixel_array = pixel_array.nbytes
    expected_length = dicom_dataset.Rows * dicom_dataset.Columns
    if ('NumberOfFrames' in dicom_dataset and
            dicom_dataset.NumberOfFrames > 1):
        expected_length *= dicom_dataset.NumberOfFrames
    if ('SamplesPerPixel' in dicom_dataset and
            dicom_dataset.SamplesPerPixel > 1):
        expected_length *= dicom_dataset.SamplesPerPixel
    if dicom_dataset.BitsAllocated > 8:
        expected_length *= (dicom_dataset.BitsAllocated // 8)
    padded_length = expected_length
    if expected_length & 1:
        padded_length += 1
    if length_of_pixel_array != padded_length:
        raise AttributeError(
            "Amount of pixel data %d does not "
            "match the expected data %d" %
            (length_of_pixel_array, padded_length))
    if expected_length != padded_length:
        pixel_array = pixel_array[:expected_length]
    if should_change_PhotometricInterpretation_to_RGB(dicom_dataset):
        dicom_dataset.PhotometricInterpretation = "RGB"
    if dicom_dataset.Modality.lower().find('ct') >= 0:  # CT图像需要得到其CT值图像
        pixel_array = pixel_array * dicom_dataset.RescaleSlope + dicom_dataset.RescaleIntercept  # 获得图像的CT值
    pixel_array = pixel_array.reshape(dicom_dataset.Rows, dicom_dataset.Columns * dicom_dataset.SamplesPerPixel)
    return pixel_array, dicom_dataset.Rows, dicom_dataset.Columns

def setDicomWinWidthWinCenter(img_data, winwidth, wincenter, rows, cols):
    # if can not set 'writeable = True', make a new np.array()
    img_temp = np.array(img_data)
    img_temp.flags.writeable = True
    min = (2 * wincenter - winwidth) / 2.0 + 0.5
    max = (2 * wincenter + winwidth) / 2.0 + 0.5
    dFactor = 255.0 / (max - min)

    for i in numpy.arange(rows):
        for j in numpy.arange(cols):
            img_temp[i, j] = int((img_temp[i, j] - min) * dFactor)

    min_index = img_temp < min
    img_temp[min_index] = 0
    max_index = img_temp > max
    img_temp[max_index] = 255

    return img_temp

def load_scan(path):
    # path_list = os.listdir(path)
    slices = [pydicom.read_file(join(path, s)) for s in os.listdir(path)]
    # slices is sorted by ImagePositionPatient
    slices.sort(key=lambda x: int(x.ImagePositionPatient[2]))
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
    for s in slices:
        s.SliceThickness = slice_thickness
    return slices

def multi_dcm(path, save_path):
    path_list = os.listdir(path)
    re_pattern = re.compile(r'(\d+)')
    path_list.sort(key=lambda x: int(re_pattern.findall(x)[0]))
    a = os.path.exists(save_path)
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    for s in path_list:
        dicom = pydicom.read_file(join(path, s))
        pixel_array, dicom.Rows, dicom.Columns = get_pixeldata(dicom)
        img_data = pixel_array
        winwidth = 500
        wincenter = 50
        rows = dicom.Rows
        cols = dicom.Columns
        dcm_temp = setDicomWinWidthWinCenter(img_data, winwidth, wincenter, rows, cols)
        # dcm_img = cv2.imshow('imgshow', dcm_temp)
        dcm_img = Image.fromarray(dcm_temp)
        dcm_img = dcm_img.convert('L')
        # dcm_img.show()
        save_file_path = save_path + '/{}.png'.format(s[:-4])
        dcm_img.save(save_file_path)

def get_pixels_hu(slices):
    image = np.stack([s.pixel_array for s in slices])
    # Convert to int16 (from sometimes int16)
    # Should be possible as values should always be low enough (<32k)
    image = image.astype(np.int16)
    # Set outside-of-scan pixels to 0
    # The intercept is usually -1024, so air is approximately 0
    image[image == -2000] = 0
    for slice_number in range(len(slices)):
        intercept = slices[slice_number].RescaleIntercept  # Intercept
        slope = slices[slice_number].RescaleIntercept
        if slope != 1:
            image[slice_number] = slope * image[slice_number].astype(np.float64)
            image[slice_number] = image[slice_number].astype(np.int16)
        image[slice_number] += np.int16(intercept)
    return np.array(image, dtype=np.int16)

def resample(image, scan, new_spaceing=[1, 1, 1]):
    # Determine current pixel spacing
    a = scan[0].PixelSpacing
    spacing = map(float, [scan[0].SliceThickness + scan[0].PixelSpacing[0]])
    spacing = np.array(list(spacing))
    resize_factor = spacing / new_spaceing
    new_real_shape = image.shape * resize_factor
    new_shape = np.round(new_real_shape)  # 返回浮点数x的四舍五入值。
    real_resize_factor = new_shape / image.shape
    new_spaceing = spacing / real_resize_factor
    image = ndi.interpolation.zoom(image, real_resize_factor, mode='nearest')
    return image, new_spaceing

def normalize(image):
    image = (image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
    image[image > 1] = 1.
    image[image < 0] = 0.
    return image

MIN_BOUND = -1000.0
MAX_BOUND = 400.0
# save_path = "/home/sj/workspace/data/chao_data/Train_Sets/CT/2/dcm2png/"
# multi_dcm(path, save_path)

train_path = '/home/sj/workspace/data/chao_data/Train_Sets/'
test_path = '/home/sj/workspace/data/chao_data/Test_Sets/'

def deal_chao(path, type):
    if type == 'CT':
        path = path + 'CT/'
        for case in os.listdir(path):
            dcm_data = path + case + '/DICOM_anon/'
            label_data = path + case + '/Ground/'
            save_path = path + case + '/CTdcm2png/'
            # for img in os.listdir(dcm_data):
            multi_dcm(dcm_data, save_path)
    elif type == 'MR':
        path = path + 'MR/'
        for case in os.listdir(path):
            dcm_data = path + case + '/T2SPIR/DICOM_anon/'
            label_data = path + case + '/T2SPIR/Ground/'
            save_path = path + case + '/T2SPIR/MRdcm2png/'
            # for img in os.listdir(dcm_data):
            multi_dcm(dcm_data, save_path)

deal_chao(train_path, 'MR')
deal_chao(test_path, 'MR')
# first_patient = load_scan(path)
# first_patient_pixels = get_pixels_hu(first_patient)
# plt.hist(first_patient_pixels.flatten(), bins=80, color='c')
# plt.xlabel("Housfield Units (HU)")
# plt.ylabel("Frequency")
# plt.show()

# pix_resampled, spacing = resample(first_patient_pixels, first_patient, [1, 1, 1])

def plot_3d(image, threshold=-300):
    # Position the scan upright,
    # so the head of the patient would be at the top facing the camera
    p = image.transpose(2, 1, 0)  # 将扫描件竖直放置
    verts, faces = measure.marching_cubes(p, threshold)  # Liner推进立方体算法来查找3D体积数据中的曲面。
    # faces = measure.marching_cubes(p, threshold)
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')
    # Fancy indexing: `verts[faces]` to generate a collection of triangles
    mesh = Poly3DCollection(verts[faces], alpha=0.1)  # 创建3Dpoly
    face_color = [0.5, 0.5, 1]
    mesh.set_facecolor(face_color)  # 设置颜色
    ax.add_collection3d(mesh)
    ax.set_xlim(0, p.shape[0])
    ax.set_ylim(0, p.shape[1])
    ax.set_zlim(0, p.shape[2])
    plt.show()

# 调用函数
# plot_3d(pix_resampled, 400)


"""Test model generation."""
import io
from textwrap import dedent

import numpy as np
import pandas as pd
import pytest

from respy.config import EXAMPLE_MODELS
from respy.likelihood import get_crit_func
from respy.pre_processing.model_checking import validate_options
from respy.pre_processing.model_processing import _convert_labels_in_formulas_to_codes
from respy.pre_processing.model_processing import _identify_relevant_covariates
from respy.pre_processing.model_processing import _parse_initial_and_max_experience
from respy.pre_processing.model_processing import process_params_and_options
from respy.tests.random_model import generate_random_model
from respy.tests.random_model import simulate_truncated_data
from respy.tests.utils import process_model_or_seed

@pytest.mark.parametrize("seed", range(5))
def test_generate_random_model(seed):
    """Test if random model specifications can be simulated and processed."""
    np.random.seed(seed)

    params, options = generate_random_model()

    df = simulate_truncated_data(params, options)

    crit_func = get_crit_func(params, options, df)

    crit_val = crit_func(params)

    assert isinstance(crit_val, float)

@pytest.mark.parametrize("model_or_seed", EXAMPLE_MODELS + list(range(10)))
def test_model_options(model_or_seed):
    _, options = process_model_or_seed(model_or_seed)

    _, options = process_params_and_options(_, options)

    validate_options(options)

@pytest.mark.parametrize("model_or_seed", EXAMPLE_MODELS + list(range(10)))
def test_sorting_of_type_probability_parameters(model_or_seed):
    # Set configuration for random models.
    n_types = np.random.randint(2, 5)
    n_type_covariates = np.random.randint(2, 4)

    params, options = process_model_or_seed(
        model_or_seed, n_types=n_types, n_type_covariates=n_type_covariates
    )

    optim_paras, options = process_params_and_options(params, options)

    # Update variables if not a random model, but an example model is tested.
    if isinstance(model_or_seed, str):
        n_types = optim_paras["n_types"]
        n_type_covariates = (
            None if n_types == 1 else len(optim_paras["type_covariates"])
        )

    if optim_paras["n_types"] > 1:
        # Resort type probability parameters.
        types = [f"type_{i}" for i in range(2, optim_paras["n_types"] + 1)]
        params.loc[types] = params.sort_index(ascending=False).loc[types]

        optim_paras_, _ = process_params_and_options(params, options)

        assert (optim_paras["type_prob"] == optim_paras_["type_prob"]).all()

def test_parse_initial_and_max_experience():
    """Test ensures that probabilities are transformed with logs and rest passes."""
    choices = ["a", "b"]

    options = {"n_periods": 10}
    optim_paras = {"choices_w_exp": choices, "choices": {"a": {}, "b": {}}}
    params = pd.DataFrame(
        {
            "category": [
                "initial_exp_a_0",
                "initial_exp_a_5",
                "initial_exp_b_0",
                "initial_exp_b_5",
                "maximum_exp",
            ],
            "name": ["probability"] * 2 + ["constant"] * 2 + ["b"],
            "value": [2, 2, np.log(2), np.log(2), 5],
        }
    ).set_index(["category", "name"])["value"]

    optim_paras = _parse_initial_and_max_experience(optim_paras, params, options)

    assert (
        optim_paras["choices"]["a"]["start"][0]
        == optim_paras["choices"]["a"]["start"][5]
    ).all()
    assert (
        optim_paras["choices"]["b"]["start"][0]
        == optim_paras["choices"]["b"]["start"][5]
    ).all()
    assert optim_paras["choices"]["a"]["max"] == options["n_periods"] - 1 + max(
        optim_paras["choices"]["a"]["start"]
    )
    assert optim_paras["choices"]["b"]["max"] == 5

@pytest.mark.parametrize("seed", list(range(10)))
def test_normalize_probabilities(seed):
    np.random.seed(seed)
    constraints = {"observables": [3]}
    params, options = generate_random_model(point_constr=constraints)
    optim_paras_1, _ = process_params_and_options(params, options)

    for group in ["initial_exp_edu", "observable_"]:
        mask = params.index.get_level_values(0).str.contains(group)
        params.loc[mask, "value"] = params.loc[mask, "value"].to_numpy() / 2

    optim_paras_2, _ = process_params_and_options(params, options)

    for key in optim_paras_1["choices"]["edu"]["start"]:
        np.testing.assert_array_almost_equal(
            optim_paras_1["choices"]["edu"]["start"][key],
            optim_paras_2["choices"]["edu"]["start"][key],
        )
    for level in optim_paras_1["observables"]["observable_0"]:
        np.testing.assert_array_almost_equal(
            optim_paras_1["observables"]["observable_0"][level],
            optim_paras_2["observables"]["observable_0"][level],
        )

def test_identify_relevant_covariates():
    params = pd.read_csv(
        io.StringIO(
            dedent(
                """
                category,name,value
                wage_a,constant,1
                nonpec_b,upper_upper,1
                wage_c,upper_upper_with_spacing_problem,1
                """
            )
        ),
        index_col=["category", "name"],
    )

    options = {
        "covariates": {
            "constant": "1",
            "nested_covariate": "2",
            "upper": "nested_covariate > 2",
            "upper_upper": "upper == 5",
            "unrelated_covariate": "2",
            "unrelated_covariate_upper": "unrelated_covariate",
            "upper_upper_with_spacing_problem": "upper>2",
        }
    }

    relevant_covariates = _identify_relevant_covariates(options, params)

    expected = {
        "covariates": {
            "constant": "1",
            "nested_covariate": "2",
            "upper": "nested_covariate > 2",
            "upper_upper": "upper == 5",
            "upper_upper_with_spacing_problem": "upper>2",
        }
    }

    assert expected == relevant_covariates

def test_convert_labels_in_covariates_to_codes():
    optim_paras = {
        "choices": ["fishing", "hammock"],
        "observables": {"fishing_grounds": ["poor", "rich"]},
        "choices_w_exp": ["fishing"],
    }

    options = {
        "covariates": {
            "rich_fishing_grounds": "fishing_grounds == 'rich'",
            "do_fishing": "choice == 'fishing'",
            "do_hammock": 'choice == "hammock"',
        },
        "core_state_space_filters": [],
        "inadmissible_states": {},
    }

    options = _convert_labels_in_formulas_to_codes(options, optim_paras)

    expected = {
        "rich_fishing_grounds": "fishing_grounds == 1",
        "do_fishing": "choice == 0",
        "do_hammock": "choice == 1",
    }

    assert options["covariates"] == expected


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/nodeconductor_saltstack/exchange/migrations/0007_user_additional_fields.py
# Compiled at: 2016-09-28 02:05:53
from __future__ import unicode_literals
from django.db import models, migrations
import nodeconductor_saltstack.exchange.validators

class Migration(migrations.Migration):
    dependencies = [
     ('exchange', '0006_group_manager_foreign_key')]
    operations = [
     migrations.AddField(model_name=b'user', name=b'company', field=models.CharField(max_length=255, blank=True), preserve_default=True),
     migrations.AddField(model_name=b'user', name=b'department', field=models.CharField(max_length=255, blank=True), preserve_default=True),
     migrations.AddField(model_name=b'user', name=b'manager', field=models.ForeignKey(blank=True, to=b'exchange.User', null=True), preserve_default=True),
     migrations.AddField(model_name=b'user', name=b'office', field=models.CharField(max_length=255, blank=True), preserve_default=True),
     migrations.AddField(model_name=b'user', name=b'phone', field=models.CharField(max_length=255, blank=True), preserve_default=True),
     migrations.AddField(model_name=b'user', name=b'title', field=models.CharField(max_length=255, blank=True), preserve_default=True),
     migrations.AlterField(model_name=b'exchangetenant', name=b'domain', field=models.CharField(max_length=255, validators=[nodeconductor_saltstack.exchange.validators.domain_validator]), preserve_default=True),
     migrations.AlterField(model_name=b'user', name=b'username', field=models.CharField(unique=True, max_length=255), preserve_default=True)]


import numpy as np
import healpy as hp
import h5py
import re
import os
import glob

if __name__ == "__main__":
    LOCAL = False

    # specify parameters for RHT data
    nchunks = 32
    startvel = 73
    stopvel  = 111

    # need to step through all R(n, v, theta) chunks
    nside = 1024
    npix = 12*nside**2
    stepsize = np.int(npix/nchunks)
    all_startpix = np.arange(0, npix, stepsize, dtype=np.int_)
    all_stoppix = all_startpix + stepsize

    # final data will be NHI(n, theta) with dimensions (npix, ntheta)
    ntheta = 165
    NHI_n_theta = np.zeros((npix, ntheta), np.float_)

    # need sum of I*R over all v and theta for denominator
    sum_I_R_n = np.zeros(npix)

    # step through each chunk of RHT data
    for _i, (startpix, stoppix) in enumerate(zip(all_startpix, all_stoppix)):
        print("beginning chunk {}: from {} to {}".format(_i, startpix, stoppix))

        # load RHT data
        rht_root = '../data/'
        rht_fn = rht_root + 'Rtheta_{}_{}_1_hpix{}_{}.h5'.format(startvel, stopvel, startpix, stoppix)

        with h5py.File(rht_fn, 'r') as rht_file:
            RHT_n_v_theta = rht_file['hthets_arr'][:] # needs colon indexer or will just be pointer #[startpix:stoppix]
            v_index_list = rht_file['hthets_arr'].attrs['velocities']
            nvels = len(v_index_list)

        # load HI intensity data I(n, v)
        if LOCAL:
            hi4pi_root = '/Users/Dropbox/HI4PI/'
        else:
            hi4pi_root = '/data/seclark/HI4PI/full_data/'
        HI_n_v = np.zeros((stoppix-startpix, nvels), np.float_)
        hi4pi_fn = hi4pi_root + 'HI4PI_120kms.h5'
        with h5py.File(hi4pi_fn, 'r') as f:
            for _i, _vel in enumerate(v_index_list):
                HI_n_v[:, _i] = f['survey'][startpix:stoppix, _vel]

        # Intensity-weighted RHT data
        I_weighted_RHT = RHT_n_v_theta * HI_n_v[:, :, np.newaxis]

        # sum over velocity axis
        R_I_n_theta = np.nansum(I_weighted_RHT, axis=1)
        NHI_n_theta[startpix:stoppix, :] = R_I_n_theta

        # sum over velocity and theta axes
        sum_I_R_n[startpix:stoppix] = np.nansum(R_I_n_theta, axis=1)

    # load NHI(n)
    if LOCAL:
        hi4pi_root = '/Users/susanclark/Dropbox/HI4PI/'
    else:
        hi4pi_root = '/data/seclark/HI4PI/full_data/'
    NHI = hp.fitsfunc.read_map(hi4pi_root+'HI4PI_NHI.fits')

    # compute Eq. 1
    NHI_over_denominator = NHI / sum_I_R_n
    final_NHI_n_theta = (NHI_over_denominator[:, np.newaxis] * NHI_n_theta)

    print("final_NHI_n_theta shape: {}".format(final_NHI_n_theta.shape))

    compress = False
    out_root = '../data/'
    NHI_n_theta_fn = out_root + 'NHI_n_theta_{}_{}_1.h5'.format(startvel, stopvel)
    with h5py.File(NHI_n_theta_fn, 'w') as f:
        if compress:
            dset = f.create_dataset(name='NHI_n_theta', data=final_NHI_n_theta, compression="gzip")
        else:
            dset = f.create_dataset(name='NHI_n_theta', data=final_NHI_n_theta)
        dset.attrs['velocities'] = v_index_list

    # sanity check: the sum along theta of this map should be NHI
    NHI_n_theta_thetsum = np.nansum(final_NHI_n_theta, axis=1)
    print("sum of difference: {}".format(np.nansum(NHI_n_theta_thetsum - NHI)))
    print("sum of NHI n theta: {}".format(np.nansum(NHI_n_theta_thetsum)))
    print("sum of NHI: {}".format(np.nansum(NHI)))


from django import template
from django.db import connection
from apps.products.dashboard import dictfetchall
from django.template import loader, Context, Template

register = template.Library()

@register.simple_tag
def lastNews():
    html = ''

    cursor = connection.cursor()
    cursor.execute('''SELECT `id`, `title`, `content`, `timestamp`, `image` FROM `news` WHERE `active` = 1 ORDER BY `timestamp` DESC LIMIT 9''')
    news = dictfetchall(cursor)

    if news:
        template = loader.get_template('_newsTpl.html')
        for item in news:
            html += template.render(Context(item))
    return html

@register.simple_tag
def tags():
    html = ''
    tag_list = list()

    cursor = connection.cursor()
    cursor.execute('''SELECT `tags` FROM `news` WHERE (`active` = 1 AND (`tags` != "" OR `tags` IS NOT NULL))''')
    tags = dictfetchall(cursor)

    if tags:
        for tag in tags:
            [tag_list.append(x.strip()) for x in tag['tags'].split(",") if x.strip() not in tag_list]

    if tag_list:
        tpl = Template('{{tag}}')
        html = '
'
        for t in tag_list:
            if t:
                html += tpl.render(Context({"tag": t}))
        html += '
'
    return html


#!/usr/bin/python3

import sys
import json
import sqlite3

import numpy as np
from randpool import RandPool

from entitygenerator import EntityGenerator, EntityElement, ArrayElement
from namegen import USCensusName
from addrgen import USAddress
from phonegen import PhoneElement
from gendergen import GenderElement
from natidgen import NationalIDElement
from dobgen import DOBElement
from tradegen import USCreditAccount

# TODO - Modifier functions, two types: pre- and post-serialization.
# pre-modifiers change the object before it is serialized.  This might
# be useful for introducing intentional errors, nicknames, modifying
# structure, etc.  The post-serialization modifiers can be used to
# create changes to the object which was just serialized so that it
# can be serialized again.  This should be an efficient way to create
# relationships (where large parts of the object might be reused), such
# as husband/wife, parent/child, roommates, or co-applicants (credit).

# TODO - include nicknames, name variations (maiden/married)

# TODO - need pre-output modifier functions.  This might randomly disrupt
# otherwise too-clean data.

# TODO - need post-output modifier functions.  These operate on the entity
# which was just output, and might be employed to reuse large portions of
# the original record.  This might be useful for generating a spouse or a
# parent/child (biological) relationship.  Once a post-output modifier has
# operated on an entity, we will have to serialize the modified object
# again in its modified state.

# TODO - generate trade (credit account) data

# TODO - email generator?

# TODO - when generating an array of addresses, it would be nice to be able
# to specify "generate local addresses".  It's unrealistic that a person would
# move large distances frequently.  Instead, pick addresses (based on a
# probability) where the leading 1, 2, or 3 digits of the zip code (for US
# addresses) are the same.

# TODO - create address history, with from/to dates.

# TODO - need better phone generator:
#        - Create a CDF for each zip code with area code frequencies
#        - do not use invalid exchanges or sequence numbers

def serialize_csv(e):
    fname = e['name'].get('first', '')
    mname = e['name'].get('middle', '')
    lname = e['name'].get('last', '')
    dob = e.get('dob', '')
    ssn = e.get('ssn', '')
    phone = e.get('phone', '')
    gender = e.get('gender', '')

    for addr in e['addrs']:
        s = '|'.join(( fname, mname, lname,
                       ssn, dob, phone, gender,
                       addr['street1'], addr['street2'], addr['street3'],
                       addr['city'], addr['state'], addr['postalcode']) )
        print(s)

    return

def serialize_json(e):
    print(json.dumps(e))
    return

def main(argv):
    if len(argv) > 2:
        raise Exception('Invalid number of arguments received')

    n = 10
    if len(argv) == 2:
        try:
            n = int(argv[1])
        except:
            raise Exception('Invalid command line argument: ' + argv[1])

    egen = EntityGenerator()

    gender = GenderElement(name = 'gender')
    egen.addElement(gender)

    dob = DOBElement(name = 'dob',
                     dt_format = '%Y%m%d')
    egen.addElement(dob)

    ssn = NationalIDElement(name = 'ssn',
                            useDashes = False)
    egen.addElement(ssn)

    name = USCensusName(name = 'name',
                        order = 'LFM',
                        pctMidName=0.7,
                        pctMidInitial=0.5,
                        pctFirstInitial=0.15,
                        params = { 'gender': '/gender' } )
    egen.addElement(name)

    # we're going to get a bit crazy here (because we can!).  Each entity is
    # going to get a block of addresses.  Each address will be generated by
    # the USAddress Entity class, and the number of addresses in each block
    # will be random (range  [1..5) ).  Each address will get a block of phone
    # numbers, and there will be exactly two phone numbers in each block.

    addr = USAddress()
    addrBlock = ArrayElement(name = 'addresses',
                             count_fn = EntityElement.count_rand_fn(max = 5,
                                                                    min = 1),
                             generator = addr)

    # Create a block of phone numbers
    phone = PhoneElement()
    phoneBlock = ArrayElement(name = 'phones',
                              count_fn = EntityElement.count_const_fn(2),
                              generator = phone)

    addr.addElement(phoneBlock)  # add the block of phones to each address...
    egen.addElement(addrBlock)   # ...and finally add the addresses to the entity.

    trade = USCreditAccount()
    tradeBlock = ArrayElement(name = 'trades',
                              count_fn = EntityElement.count_rand_fn(max=9,
                                                                     min=0),
                              generator = trade)
    egen.addElement(tradeBlock)

    # create a few entities
    serialize = serialize_json
    #serialize = serialize_csv
    for i in range(n):
        entity = egen.create()
        serialize(entity)

    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))


from flask import Flask
'''
当访问路径是以下任何一个的时候：
    127.0.0.1:5000/
    127.0.0.1:5000/index
    127.0.0.1:5000/数字
    127.0.0.1:5000/index/数字
将程序交给 index()视图处理
根据数字的传递情况给出响应
    如果传递数字到路由中：
    响应：您当前看到的页数为：xxx
    如果没有传递数字到路由中：
        响应：您当前看的页数为：1
'''
app = Flask(__name__)

@app.route('/')
@app.route('/index')
@app.route('/<int:number>')
@app.route('/index/<int:number>')
def show(number=1):
    return "您当前看到的页数为%d"%number

if __name__ == '__main__':
    app.run(debug=True)


from constraint import Constraint

class BaseConstraint(Constraint):
	def __init__(self, order, machine):
		Constraint.__init__(self)
		self.order = order
		self.machine = machine

class MachineBreaksConstraint(BaseConstraint):
	def __call__(self, variables, domains, assignments, forwardcheck = False):
		enterTime = assignments[variables[0]]
		orderRange = range(enterTime,
						   enterTime + self.order.recipe[self.machine.name])

		for t in self.machine.setOfBreaks():
			for t2 in orderRange:
				if t == t2:
					return False
		return True


from flask import render_template,session,redirect,url_for,current_app,abort,flash,request,make_response
from flask_login import login_required,current_user
from datetime import datetime
from config import config
from . import main
from .forms import PostForm,EditProfileForm,EditProfileAdminForm,CommentForm
from .. import db
from ..models import User,Permission,Role,Post,Follow,Comment
from ..email import send_mail
from ..decorators import admin_required,permission_required

@main.route('/',methods = ['GET','POST'])
def index():
    form = PostForm()
    if form.validate_on_submit() and current_user.can(Permission.WRITE_ARTICLES):
        post = Post(body = form.body.data,author = current_user._get_current_object())
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type = int )
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    pagination = query.order_by(Post.timestamp.desc()).paginate(page,per_page = current_app.config['FLASKY_POSTS_PER_PAGE'],error_out = False)
    posts = pagination.items
    return render_template('index.html',form = form,posts = posts,pagination = pagination,show_followed = show_followed)

@main.route('/all')
@login_required
def show_all():
    resp = make_response(redirect(url_for('.index')))
    resp.set_cookie('show_followed','',max_age = 30*24*60*60)
    return resp

@main.route('/followed')
@login_required
def show_followed():
    resp = make_response(redirect(url_for('.index')))
    resp.set_cookie('show_followed','1',max_age = 30*24*60*60)
    return resp

@main.route('/admin')
@login_required
@admin_required
def for_admin_only():
    return 'For administrators!'

@main.route('/moderator')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def for_moderator_only():
    return 'For comment moderators!'

@main.route('/user/<username>')
def user(username):
    user = User.query.filter_by(username = username).first()
    if user is None:
        abort(404)
    page = request.args.get('page',1,type = int)
    pagination = user.posts.order_by(Post.timestamp.desc()).paginate(page,per_page = current_app.config['FLASKY_POSTS_PER_PAGE'],error_out = False)
    posts = pagination.items
    return render_template('user.html',user = user,posts = posts,pagination = pagination)

@main.route('/edit-profile',methods = ['GET','POST'])
@login_required
def edit_profile():
    form = EditProfileForm()
    if form.validate_on_submit():
        current_user.name = form.name.data
        current_user.location = form.location.data
        current_user.about_me = form.about_me.data
        db.session.add(current_user._get_current_object())
        db.session.commit()
        flash('你的资料已经提交')
        return redirect(url_for('.user',username = current_user.username))
    form.name.data = current_user.name
    form.location.data = current_user.location
    form.about_me.data = current_user.about_me
    return render_template('edit_profile.html',form = form )

@main.route('/edit-profile/<int:id>', methods = ['GET','POST'])
@login_required
@admin_required
def edit_profile_admin(id):
    user = User.query.get_or_404(id)
    form = EditProfileAdminForm(user = user)
    if form.validate_on_submit():
        user.email = form.email.data
        user.username = form.username.data
        user.confirmed = form.confirmed.data
        user.role = Role.query.get(form.role.data)
        user.name = form.name.data
        user.location = form.location.data
        user.about_me = form.about_me.data
        db.session.add(user)
        db.session.commit()
        flash('资料已经更新')
        return redirect(url_for('.user',username = user.username))
    form.email.data = user.email
    form.username.data = user.username
    form.confirmed.data = user.confirmed
    form.role.data = user.role_id
    form.name.data = user.name
    form.location.data = user.location
    form.about_me.data = user.about_me
    return render_template('edit_profile.html',form = form,user = user)

@main.route('/post/<int:id>',methods = ['GET','POST'])
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(body = form.body.data,post = post,author = current_user._get_current_object())
        db.session.add(comment)
        db.session.commit()
        flash('你的评论已经发布')
        return redirect(url_for('.post',id = post.id,page = -1))
    page = request.args.get('page',1,type = int)
    if page == -1:
        page = (post.comments.count() - 1) / current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
    pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page,per_page = current_app.config['FLASKY_COMMENTS_PER_PAGE'],error_out = False)
    comments = pagination.items
    return render_template('post.html',posts = [post],form = form, comments = comments,pagination = pagination)

@main.route('/edit/<int:id>',methods = ['GET','POST'])
@login_required
def edit(id):
    form = PostForm()
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        db.session.commit()
        flash('你的文章已经更新')
        return redirect(url_for('.post',id = post.id))
    form.body.data = post.body
    return render_template('edit_post.html',form = form)

@main.route('/follow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def follow(username):
    user = User.query.filter_by(username = username).first()
    if user is None:
        flash('此用户不存在')
        return redirect(url_for('.index'))
    if current_user.is_following(user):
        flash('你已经关注这个用户.')
        return redirect(url_for('.user',username = username))
    current_user.follow(user)
    db.session.commit()
    flash('你关注了%s.' % username)
    return redirect(url_for('.user',username = username))

@main.route('/unfollow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def unfollow(username):
    user = User.query.filter_by(username = username).first()
    if user is None:
        flash('此用户不存在')
        return redirect(url_for('.index'))
    if not current_user.is_following(user):
        flash('你没有关注这个用户')
        return redirect(url_for('.user',username = username))
    current_user.unfollow(user)
    db.session.commit()
    flash('取消关注%s.' % username)
    return redirect(url_for('.user',username = username))

@main.route('/followers/<username>')
def followers(username):
    user = User.query.filter_by(username = username).first()
    if user is None:
        flash('此用户不存在.')
        return redirect(url_for('.index'))
    page = request.args.get('page',1,type = int)
    pagination = user.followers.paginate(page,per_page = current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],error_out = False)
    follows = [{'user':item.follower,'timestamp':item.timestamp} for item in pagination.items]
    return render_template('followers.html',user = user,pagination = pagination, follows = follows,title = '的粉丝 ',endpoint = '.followers')

@main.route('/followed_by/<username>')
def followed_by(username):
    user = User.query.filter_by(username = username).first()
    if user is None:
        flash('此用户不存在.')
        return redirect(url_for('.index'))
    page = request.args.get('page',1,type = int)
    pagination = user.followed.paginate(page,per_page = current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],error_out = False)
    follows = [{'user':item.followed,'timestamp':item.timestamp} for item in pagination.items]
    return render_template('followers.html',user = user,pagination = pagination, follows = follows,title = '的关注',endpoint = '.followed_by')

# @main.route('/moderate')
# @login_required
# @permission_required(Permission.MODERATE_COMMENTS)
# def moderate():
#     page = request.args.get('page',1,type = int)
#     pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(page,per_page = current_app.config['FLASKY_COMMENTS_PER_PAGE'],error_out = False)
#     comments = pagination.items
#     return render_template('moderate.html',pagination = pagination,page = page, comments = comments)

@main.route('/moderate/enable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_enable(id):
    comment = Comment.query.get_or_404(id)
    comment.disabled = False
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('.post',id = comment.post_id))

@main.route('/moderate/disable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_disable(id):
    comment = Comment.query.get_or_404(id)
    comment.disabled = True
    db.session.add(comment)
    db.session.commit()
    return redirect(url_for('.post',id = comment.post_id))


import pandas
import sys
from google.cloud import firestore
import math

pre_column_names = [
    "Form inserted",
    "* Are you?",
    "* How old are you?",
    "* What Province are you contacting us from?",
    "On a scale of 0 to 7, how upset are you right now right now?",
    "siteLanguage",
    "chat id"
]

post_column_names = [
    "chatid",
    "2. On a scale of 0 to 7, how upset are you right now?"
]

if __name__ == '__main__':
    pre_file_loc = sys.argv[1]
    pre_sheet_num = sys.argv[2]
    post_file_loc = sys.argv[3]
    post_sheet_num = sys.argv[4]
    pre_sheet = pandas.read_excel(pre_file_loc, int(pre_sheet_num))
    post_sheet = pandas.read_excel(post_file_loc, int(post_sheet_num))
    db = firestore.Client('kids-help-phone-dash')
    the_datas = []
    nums = {}
    for pre_name in pre_column_names:
        the_datas.append(pre_sheet[pre_name])
    post_datas = []
    for post_name in post_column_names:
        post_datas.append(post_sheet[post_name])

    tot_lt_5 = 0
    tot_6_to_10 = 0
    tot_11_to_15 = 0
    tot_16_to_20 = 0
    tot_21_plus = 0
    for i in range(len(the_datas[0])):
        call_id = the_datas[6][i]
        gender = the_datas[1][i]
        if gender == 'I identify differently than the above':
            gender = 'Other'

        age = str(the_datas[2][i]).split(' ')[0]
        if age == 'Over':
            age = '21'
        age = int(age)
        ind = post_datas[0][post_datas[0] == call_id]
        if ind.size == 0:
            print('No after call log for: ' + str(call_id))
            continue
        print(ind)
        ind = ind.index[0]
        print(ind)
        if age in nums.keys():
            nums[age] = nums[age]+1
        else:
            nums[age] = 1
        data = {
            'date': the_datas[0][i],
            'gender': gender,
            'age': age,
            'province': the_datas[3][i],
            'language': the_datas[5][i],
            'before_rating': the_datas[4][i],
            'after_rating': post_datas[1][ind]
        }
        db.collection(u'call-stats').document(str(call_id)).set(data)
    print(nums)
    total = 0
    for key,val in nums.items():
        total += val
    print(total)


from autoPyTorch.utils.config.config_option import ConfigOption
from autoPyTorch.utils.benchmarking.benchmark_pipeline import ForRun, ForAutoNetConfig, ForInstance
from autoPyTorch.utils.benchmarking.benchmark_pipeline.prepare_result_folder import get_run_result_dir
import os
import logging
import traceback

class CollectInstanceTrajectories(ForInstance):
    def fit(self, pipeline_config, run_id_range):
        instances = self.get_instances(pipeline_config, instance_slice=self.parse_slice(pipeline_config["instance_slice"]))

        result_trajectories = dict()
        result_optimize_metrics = set()

        for instance in instances:
            try:
                pipeline_result = self.sub_pipeline.fit_pipeline(pipeline_config=pipeline_config, instance=instance, run_id_range=run_id_range)

                # merge the trajectories into one dict
                instance_trajectories = pipeline_result["trajectories"]
                optimize_metrics = pipeline_result["optimize_metrics"]

                for metric, config_trajectories in instance_trajectories.items():
                    if metric not in result_trajectories:
                        result_trajectories[metric] = dict()
                    for config, run_trajectories in config_trajectories.items():
                        if config not in result_trajectories[metric]:
                            result_trajectories[metric][config] = dict()
                        result_trajectories[metric][config][instance] = run_trajectories
                result_optimize_metrics |= optimize_metrics

            except Exception as e:
                print(e)
                traceback.print_exc()
        return {"trajectories": result_trajectories,
                "optimize_metrics": result_optimize_metrics}

class CollectAutoNetConfigTrajectories(ForAutoNetConfig):
    def fit(self, pipeline_config, instance, run_id_range):
        logging.getLogger('benchmark').info('Collecting data for dataset ' + instance)

        result_trajectories = dict()
        result_optimize_metrics = set()

        # iterate over all configs
        for config_file in self.get_config_files(pipeline_config):
            autonet_config_name = os.path.basename(config_file).split(".")[0]
            pipeline_result = self.sub_pipeline.fit_pipeline(pipeline_config=pipeline_config,
                                                             instance=instance,
                                                             run_id_range=run_id_range,
                                                             autonet_config_file=config_file)

            # merge the trajectories into one dict
            config_trajectories = pipeline_result["trajectories"]
            optimize_metrics = pipeline_result["optimize_metrics"]

            for metric, run_trajectories in config_trajectories.items():
                if metric not in result_trajectories:
                    result_trajectories[metric] = dict()
                result_trajectories[metric][autonet_config_name] = run_trajectories

            result_optimize_metrics |= optimize_metrics
        return {"trajectories": result_trajectories,
                "optimize_metrics": result_optimize_metrics}

class CollectRunTrajectories(ForRun):
    def fit(self, pipeline_config, instance, run_id_range, autonet_config_file):
        logging.getLogger('benchmark').info('Collecting data for autonet config ' + autonet_config_file)

        result_trajectories = dict()
        optimize_metrics = set()

        run_number_range = self.parse_range(pipeline_config['run_number_range'], pipeline_config['num_runs'])
        instance_result_dir = os.path.abspath(os.path.join(get_run_result_dir(pipeline_config, instance, autonet_config_file, "0", "0"), ".."))
        if not os.path.exists(instance_result_dir):
            logging.getLogger('benchmark').warn("Skipping %s because it no results exist" % instance_result_dir)
            return {"trajectories": result_trajectories, "optimize_metrics": optimize_metrics}
        run_result_dirs = next(os.walk(instance_result_dir))[1]

        # iterate over all run_numbers and run_ids
        for run_result_dir in run_result_dirs:
            run_id, run_number = parse_run_folder_name(run_result_dir)
            run_result_dir = get_run_result_dir(pipeline_config, instance, autonet_config_file, run_id, run_number)
            if (run_id_range is not None and run_id not in run_id_range) or run_number not in run_number_range:
                continue

            run_result_dir = get_run_result_dir(pipeline_config, instance, autonet_config_file, run_id, run_number)
            if not os.path.exists(run_result_dir):
                logging.getLogger('benchmark').debug("Skipping " + run_result_dir + "because it does not exist")
                continue
            pipeline_result = self.sub_pipeline.fit_pipeline(pipeline_config=pipeline_config,
                                                                instance=instance,
                                                                run_number=run_number,
                                                                run_id=run_id,
                                                                autonet_config_file=autonet_config_file,
                                                                run_result_dir=run_result_dir)
            run_trajectories = pipeline_result["trajectories"]
            optimize_metric = pipeline_result["optimize_metric"]

            # merge the trajectories into one dict
            for metric, trajectory in run_trajectories.items():
                if metric not in result_trajectories:
                    result_trajectories[metric] = list()
                result_trajectories[metric].append(trajectory)

            if optimize_metric is not None:
                optimize_metrics |= set([optimize_metric])
        return {"trajectories": result_trajectories, "optimize_metrics": optimize_metrics}

def parse_run_folder_name(run_folder_name):
    assert run_folder_name.startswith("run_")
    run_folder_name = run_folder_name[4:].split("_")
    run_id = int(run_folder_name[0])
    run_number = int(run_folder_name[1])
    return run_id, run_number


alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def limpar(texto):
    lista=[]
    for char in texto:
        char = char.lower()
        if(char.isalpha() == True):
            lista.append(char)
    return lista

def frequencia(texto):
    freq={}
    for char in texto:
        if(char in freq):
            freq[char]=freq[char]+1
        else:
            freq[char]=1
    return freq

def Ecifra_cesar(texto,chave=3):
    cifrado = ""
    for char in texto:
        char = char.upper()
        if(char in alfabeto):
            letra = alfabeto.find(char)+chave
            if letra >= 26:
                letra -= 26
            cifrado += alfabeto[letra]
    return(cifrado)

def Dcifra_cesar(texto,chave=3):
    decrypt = ""
    for char in texto:
        char = char.upper()
        if(char in alfabeto):
            letra = alfabeto.find(char)-chave
            decrypt += alfabeto[letra]
    decrypt = decrypt.lower()
    return(decrypt)


import requests
from bs4 import BeautifulSoup

def GetFeaturedArticleDate(articleName):
	articleName = articleName.split('.')[0]
	FeaturedDate = ''
	months = {
	'January': '01',
	'February': '02',
	'March': '03',
	'April': '04',
	'May' : '05',
	'June': '06',
	'July': '07',
	'August': '08',
	'September': '09',
	'October': '10',
	'November': '11',
	'December': '12',
	}

	url = 'https://en.wikipedia.org/wiki/Talk:' + articleName
	headers = {
		'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Mobile Safari/537.36'
	}

	r = requests.get(url, headers=headers)

	if r.status_code == 200:
		html = r.text
		soup = BeautifulSoup(html, 'lxml')

		temp = ''
		for link in soup.find_all('tr', attrs=False):
			if len(link) == 3:
				if 'Featured' in link.get_text() and 'Promoted' in link.get_text():
					temp = link.get_text()
					break

		if temp == '':
			for link in soup.find_all('tr', attrs=False):
				if len(link) == 3:
					if 'Featured' in link.get_text() and 'Kept' in link.get_text():
						temp = link.get_text()
						break

		c = 0
		for i in temp:
			if i.isupper() and c!=0:
				temp = temp[:c]
				break
			c += 1

		temp = temp.split(' ')
		if len(temp[1][:-1]) < 2:
			FeaturedDate += temp[-1] + months[temp[0]] + '0' + temp[1][:-1]
		else:
			FeaturedDate += temp[-1] + months[temp[0]] + temp[1][:-1]
		return int(FeaturedDate)

	else:
		print('Something went wrong!', articleName)

print(GetFeaturedArticleDate('Woolly mammoth')) #yyyymmdd


# Imports
from flask import Flask, render_template, redirect, jsonify
from featured_img_scrape import get_featured_image
from mars_facts import get_mars_facts
from mars_hemisphere_pic_scrape import get_image
from mission_to_mars import get_title, get_body
import PyMongo

# Create the App

app = Flask(__name__)

# Establish Mongo connection
mongodb = PyMongo(app, uri='http://127.0.0.1:5000/')

# Home route

@app.route("/")
def marsinfo():
    cerberus = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/cerberus_enhanced'
    schiaparelli = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/schiaparelli_enhanced'
    syrtis_major = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/syrtis_major_enhanced'
    valles_marineris = 'https://astrogeology.usgs.gov/search/map/Mars/Viking/valles_marineris_enhanced'
    featured_image = get_featured_image()
    mars_facts = get_mars_facts()
    title = get_title()
    body = get_body()
    cerberus_info = get_image(cerberus)
    schiaparelli_info = get_image(schiaparelli)
    syrtis_major_info = get_image(syrtis_major)
    valles_marineris_info = get_image(valles_marineris)
    return render_template("index.html",
    featured_image = featured_image,
    mars_facts = mars_facts,
    title = title,
    body = body,
    cerberuspic = cerberus_info,
    schiaparellipic = schiaparelli_info,
    syrtis_majorpic = syrtis_major_info,
    valles_marinerispic = valles_marineris_info
     )

@app.route("/scrape")
def scrape():
    featured_image = get_featured_image()
    mars_facts = get_mars_facts()
    title = get_title()
    body = get_body()
    mongodb.db.collection.update({}, featured_image, mars_facts, title, body, upsert=True)
    return redirect("/")

if __name__ == "__main__":
    app.run(debug=True)


# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Top-level presubmit script for Skia.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into gcl.
"""

def _CommonChecks(input_api, output_api):
  """Presubmit checks common to upload and commit."""
  results = []
  sources = lambda x: (x.LocalPath().endswith('.h') or
                       x.LocalPath().endswith('.gypi') or
                       x.LocalPath().endswith('.gyp') or
                       x.LocalPath().endswith('.py') or
                       x.LocalPath().endswith('.sh') or
                       x.LocalPath().endswith('.cpp'))
  results.extend(
      input_api.canned_checks.CheckChangeHasOnlyOneEol(
          input_api, output_api, source_file_filter=sources))
  return results

def CheckChangeOnUpload(input_api, output_api):
  """Presubmit checks for the change on upload.

  The following are the presubmit checks:
  * Check change has one and only one EOL.
  """
  results = []
  results.extend(_CommonChecks(input_api, output_api))
  return results

def CheckChangeOnCommit(input_api, output_api):
  """Presubmit checks for the change on commit.

  The following are the presubmit checks:
  * Check change has one and only one EOL.
  * Ensures that the Skia tree is not closed in
    http://skia-tree-status.appspot.com/
  """
  results = []
  results.extend(_CommonChecks(input_api, output_api))
  results.extend(
      input_api.canned_checks.CheckTreeIsOpen(
          input_api, output_api, json_url=(
              'http://skia-tree-status.appspot.com/banner-status?format=json')))
  return results


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = u'Nicolas L\u0153uillet'
SITENAME = u'wallabag: a self hostable application for saving web pages'
SITEURL = "http://localhost:8000"
PATH = 'content'

#WARNING_MESSAGE = u'2016/04/03: wallabag v2 available! Download it here.'

TIMEZONE = 'Europe/Paris'

DEFAULT_LANG = u'en'
DISPLAY_PAGES_ON_MENU = True
ARTICLE_URL = '{slug}'

DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives', 'search')
SITEMAP_SAVE_AS = 'sitemap.xml'

DEFAULT_PAGINATION = 6
INDEX_SAVE_AS = 'blog_index.html'
SUMMARY_MAX_LENGTH = None
THEME = "pelican-bootstrap3"
BOOTSTRAP_THEME = 'cosmo'
CUSTOM_CSS = 'extra/css/custom.css'
RECENT_POST_COUNT = 3

HIDE_SIDEBAR = True

BOOTSTRAP_NAVBAR_INVERSE = True
USE_PAGER = True

FAVICON = 'images/favicon.ico'

DISPLAY_CATEGORIES_ON_MENU = False

DISPLAY_TAGS_ON_SIDEBAR = False

MIT_LICENSE = True

HIDE_SITENAME = True

DISPLAY_RECENT_POSTS_ON_SIDEBAR = True

# Revoir la 404 pour insérer quelques liens

ARTICLE_URL = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}'
ARTICLE_SAVE_AS = 'blog/{date:%Y}/{date:%m}/{date:%d}/{slug}.html'

STATIC_PATHS = [
    'extra',
    'images',
    'extra/custom.css',
    ]

TEMPLATE_PAGES = {'extra/404.html': '404.html'}

PATH = 'content'
ARTICLE_PATHS = ['posts']

EXTRA_PATH_METADATA = {
    'extra/c89a2a203e9a6045dd4df81b6ca20289.txt': {'path': 'c89a2a203e9a6045dd4df81b6ca20289.txt'},
    'extra/googlef9834aed73023328.html': {'path': 'googlef9834aed73023328.html'},
    'extra/CNAME': {'path': 'CNAME'},
    'extra/custom.css': {'path': 'static/custom.css'}
}

READERS = {'html': None}

DISPLAY_ARTICLE_INFO_ON_INDEX = True

PLUGIN_PATHS = ['plugins']
PLUGINS = ['share_post']


import operator
import thread
import datetime
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.mail import EmailMultiAlternatives
from django.core.mail import send_mail
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect, render_to_response
from django.template import RequestContext
from django.template.loader import render_to_string
from forms import *
from models import *
from tagging.models import Tag
from tagging.models import TaggedItem
from django.contrib.contenttypes.generic import generic_inlineformset_factory

# Create your views here.

def lista_foro(request):
    discusiones = Foros.objects.all().order_by('-creacion')

    return render_to_response('index.html', locals(),
    	           context_instance=RequestContext(request))

@login_required
def ver_foro(request, foro_id):
    discusion = get_object_or_404(Foros, id=foro_id)

    if request.method == 'POST':
        form = AporteForm(request.POST)

        if form.is_valid():
            form_uncommited = form.save(commit=False)
            form_uncommited.user = request.user
            form_uncommited.foro = discusion
            form_uncommited.save()

            return HttpResponseRedirect('/foros/ver/%d' % discusion.id)
    else:
        form = AporteForm()
    return render_to_response('foros/ver_foro.html', RequestContext(request, locals()))

@login_required
def comentario_foro(request, aporte_id):
    aporte = get_object_or_404(Aportes, id=aporte_id)

    if request.method == 'POST':
        form = ComentarioForm(request.POST)
        if form.is_valid():
            form1_uncommited = form.save(commit=False)
            form1_uncommited.usuario = request.user
            form1_uncommited.aporte = aporte
            form1_uncommited.save()
            return HttpResponseRedirect('/foros/ver/%d' % aporte.foro_id)
    else:
        form = ComentarioForm()
    return render_to_response('foros/comentario.html', RequestContext(request, locals()))

@login_required
def crear_foro(request):
    if request.method == 'POST':
        form = ForosForm(request.POST)
        form2 = ImagenForm(request.POST, request.FILES)
        form3 = DocumentoForm(request.POST, request.FILES)
        form4 = VideoForm(request.POST)
        form5 = AudioForm(request.POST, request.FILES)

        if form.is_valid() and form2.is_valid() and form3.is_valid() and form4.is_valid() and form5.is_valid():
            form_uncommited = form.save(commit=False)
            form_uncommited.contraparte = request.user
            form_uncommited.save()

            form2_uncommitd = form2.save(commit=False)
            form2_uncommitd.content_object = form_uncommited
            form2_uncommitd.save()

            form3_uncommitd = form3.save(commit=False)
            form3_uncommitd.content_object = form_uncommited
            form3_uncommitd.save()

            form4_uncommitd = form4.save(commit=False)
            form4_uncommitd.content_object = form_uncommited
            form4_uncommitd.save()

            form5_uncommitd = form5.save(commit=False)
            form5_uncommitd.content_object = form_uncommited
            form5_uncommitd.save()

            return HttpResponseRedirect('/foros')

    else:
        form = ForosForm()
        form2 = ImagenForm()
        form3 = DocumentoForm()
        form4 = VideoForm()
        form5 = AudioForm()
    return render_to_response('foros/crear_foro.html', RequestContext(request, locals()))

@login_required
def editar_foro(request, id):
    foro = get_object_or_404(Foros, id=id)
    ForoImgFormSet = generic_inlineformset_factory(Imagen, extra=5, max_num=5)
    ForoDocuFormSet = generic_inlineformset_factory(Documentos, extra=5, max_num=5)
    ForoVideoFormSet = generic_inlineformset_factory(Videos, extra=5, max_num=5)
    ForoAudioFormSet = generic_inlineformset_factory(Audios, extra=5, max_num=5)
    form2 = ForoImgFormSet(instance=foro)
    form3 = ForoDocuFormSet(instance=foro)
    form4 = ForoVideoFormSet(instance=foro)
    form5 = ForoAudioFormSet(instance=foro)

    if not foro.contraparte == request.user and not request.user.is_superuser:
        return HttpResponse("Usted no puede editar este Foro")

    if request.method == 'POST':
        form = ForosForm(request.POST, instance=foro)
        form2 = ForoImgFormSet(data=request.POST, files=request.FILES, instance=foro)
        form3 = ForoDocuFormSet(data=request.POST, files=request.FILES, instance=foro)
        form4 = ForoVideoFormSet(data=request.POST, files=request.FILES, instance=foro)
        form5 = ForoAudioFormSet(data=request.POST, files=request.FILES, instance=foro)

        if form.is_valid() and form2.is_valid() and form3.is_valid() and form4.is_valid() and form5.is_valid():
            form_uncommited = form.save(commit=False)
            form_uncommited.contraparte = request.user
            form_uncommited.save()

            form2.save()
            form3.save()
            form4.save()
            form5.save()
            return HttpResponseRedirect('/foros')

    else:
        form = ForosForm(instance=foro)
        form2 = ForoImgFormSet(instance=foro)
        form3 = ForoDocuFormSet(instance=foro)
        form4 = ForoVideoFormSet(instance=foro)
        form5 = ForoAudioFormSet(instance=foro)

    return render_to_response('foros/crear_foro.html', RequestContext(request, locals()))

@login_required
def borrar_foro(request, id):
    foro = get_object_or_404(Foros, id=id)
    if foro.user == request.user or request.user.is_superuser:
        foro.delete()
        return redirect('/foros')
    else:
        return redirect('/')

@login_required
def perfil(request):
    return render_to_response('registration/perfil.html', RequestContext(request, locals()))


import unittest
from DLL import DLL, DLLNode, DLLError, reverse

class TestProject1(unittest.TestCase):

    def test_insert_front_back(self):
        lst = DLL()
        lst.insert_front(1)
        lst.insert_front(2)
        lst.insert_front(3)
        lst.insert_back(4)
        lst.insert_back(5)

        assert lst.head.value == 3
        assert lst.tail.value == 5

        forward, backward = [], []
        head = lst.head
        while head is not None:
            forward.append(head.value)
            head = head.next
        tail = lst.tail
        while tail is not None:
            backward.append(tail.value)
            tail = tail.prev

        assert forward == [3, 2, 1, 4, 5]
        assert backward == [5, 4, 1, 2, 3]

    def test_accessors(self):
        lst = DLL()

        assert lst.is_empty()
        assert lst.size == 0

        for _ in range(5):
            lst.insert_front(1)

        assert not lst.is_empty()
        assert lst.size == 5

        for _ in range(3):
            lst.delete_front()

        assert not lst.is_empty()
        assert lst.size == 2

    def test_delete_front_back(self):
        def condense(linkedlist):
            res = list()
            node = linkedlist.head
            while node is not None:
                res.append(node.value)
                node = node.next
            return res

        lst = DLL()
        inserts = [1, 4, 0, 10, 3, 7, 9]

        for x in inserts:
            lst.insert_back(x)

        lst.delete_front()
        inserts.pop(0)

        assert inserts == condense(lst)

        lst.delete_back()
        inserts.pop()

        assert inserts == condense(lst)

    def test_delete_value_all(self):
        def condense(linkedlist):
            res = list()
            node = linkedlist.head
            while node is not None:
                res.append(node.value)
                node = node.next
            return res

        lst = DLL()
        insert = [1, 2, 3, 4, 5, 6, 1, 1, 2, 4, 1, 9]

        for i in insert:
            lst.insert_back(i)

        lst.delete_value(1)
        assert condense(lst) == insert[1:]

        lst.delete_all(1)
        assert condense(lst) == [2, 3, 4, 5, 6, 2, 4, 9]

    def test_find_first_last_all(self):
        lst = DLL()
        inserts = [9, 16, 5, 58, 32, 1, 4, 58, 67, 2, 4]

        for i in inserts:
            lst.insert_back(i)

        first = lst.find_first(32)

        assert first.value == 32
        assert first.next.value == 1
        assert first.prev.value == 58

        last = lst.find_last(2)

        assert last.value == 2
        assert last.next.value == 4
        assert last.prev.value == 67

        list_of_58s = lst.find_all(58)

        assert len(list_of_58s) == 2
        for i in list_of_58s:
            assert i.value == 58

        first = list_of_58s[0]
        second = list_of_58s[1]

        assert first.next.value == 32
        assert first.prev.value == 5

        assert second.next.value == 67
        assert second.prev.value == 4

    def test_count_sum(self):
        lst = DLL()
        inserts = [1, 3, 1, 4, 5, 6, 1, 3, 8]

        for i in inserts:
            lst.insert_back(i)

        assert lst.count(1) == 3
        assert lst.sum() == 32

    def test_reverse(self):
        def condense(linkedlist):
            res = list()
            node = linkedlist.head
            while node is not None:
                res.append(node.value)
                node = node.next
            return res

        lst = DLL()
        inserts = [1, 2, 3, 4, 5]

        for i in inserts:
            lst.insert_back(i)

        reverse(lst)

        assert condense(lst) == [5, 4, 3, 2, 1]

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


vogal = ['a','e','i','o','u','A','E','I','O','U']
vetor = ['a','a','b','c','i','k','o','p','q','u']

aux=1
soma =0
cons = []

for i in vetor:
    if i not in vogal:
        soma=soma+aux
        cons.append(i)

print("total de consoantes: %i" %soma)
print(cons)


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^login/$', views.login_user, name='login_user'),
    url(r'^logout/$', views.logout_user, name='logout_user'),
    url(r'^register/$', views.register, name='register'),
    url(r'^register_user/$', views.register_user, name='register_user'),
    url(r'^map/$', views.map, name='map'),
    url(r'^recommend/$', views.recommend, name='recommend'),
    url(r'^settings/$', views.settings, name='settings'),
    url(r'^test/$', views.test, name='test'),
]


from aocd import get_data

data = sorted(list(map(int, get_data(day=10, year=2020).splitlines())))
data.append(data[-1] + 3)
data.insert(0, 0)
ones = threes = 0

for i in range(1, len(data)):
	if data[i] - data[i - 1] == 1:
		ones += 1
	elif data[i] - data[i - 1] == 3:
		threes += 1

print(ones * threes)

# Find number of possible ways to go through the interval, including first and last position
# [0, 1] = 1
# [0, 1, 2] = 2 = [0, 2] + [0, 1, 2]
# [0, 1, 2, 3] = 4 = [0, 1, 2, 3] + [0, 1, 3] + [0, 2, 3] + [0, 3]
# [0, 1, 2, 4] = 3 = [0, 1, 4] + [0, 2, 4] + [0, 1, 2, 4]
def ways(interval):
	if len(interval) == 2:
		return 1
	elif interval[2] - interval[0] <= 3:
		return ways(interval[1:]) + ways([interval[0]] + interval[2:])
	else:
		return ways(interval[1:])

total = 1
mandatory = 0
for i in range(1, len(data) - 1):
	# Find mandatory numbers that must be in all permutations
	if data[i+1] - data[i-1] > 3:
		# Calculate ways to travel between mandatory numbers and multiply possibilities
		total *= ways(data[mandatory:i+1])
		mandatory = i
print(total)


import asyncio
import time
from asyncPool import *

async def 工作任务(i):
    await asyncio.sleep(1)
    return i

async def thread_example3(i):
    time.sleep(1)
    await asyncio.sleep(0.1)
    return i

def 工作任务4(i):
    time.sleep(0.01)
    return i

def 接受结果(future):
    result = future.result()
    print('返回值: ', result)

def demo():
    # 任务组， 最大协程数
    pool = 协程池(协程数量=10, 线程池数量=10)

    # 插入任务任务
    for i in range(1000):
        # print(i)
        # 非阻塞协程
        pool.投递任务(工作任务, i, 回调函数=接受结果)
        # 阻塞协程
        # pool.投递任务2(工作任务4, i, 回调函数=接受结果)

    print("等待子线程结束1...")

    # 停止子线程
    pool.释放线程()

    print("等待子线程结束2...")

    # 等待
    pool.等待()

    print("等待子线程结束3...")

    #
    # # 进度条使用
    # while True:
    #     value = pool.task_progress(99)
    #     print("value: ", value)
    #
    #     # 获取线程数
    #     print("running: ", pool.running)
    #
    #     if value == 100:
    #         break
    #
    #     # 获取任务
    #     # print(pool.get_task())
    #     time.sleep(0.5)
    #
    # print("等待子线程结束4...")

if __name__ == '__main__':
    start_time = time.time()
    demo()
    end_time = time.time()
    print("run time: ", end_time - start_time)