# -*- coding: utf-8 -*-
import os
import os.path
import shutil
import time
import datetime
import string
import sys
import platform
import time
import signal
import imp

imp.reload(sys)

source_dir = os.getcwd()
go_dir = r"go"

is_public_Rel = True
ISOTIMEFORMAT = '%Y-%m-%d %X'

buildfiles = [
    # srcdir,buildname isRel isLinux is64Bit
    #[r"/guard","guard_Dbg",False,True,True],
    [r"/guard","guard",True,True,True],
    #[r"/guardCertZ","guardcert_Dbg",False,True,True],
    #[r"/guardCertZ","guardcert",True,True,True],
    #[r"/guardCtrl","guardctrl_Dbg",False,True,True],
    #[r"/guardCtrl","guardctrl",True,True,True],
]

def logtofile(str):
    type = sys.getfilesystemencoding()
    timeStr = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
    print("[" + timeStr + "] " + str)#.decode('utf-8').encode(type))
    #os.system("echo " + "["+ timeStr + "] " + str.decode('utf-8').encode(type) + " >> " + getLogPath())

def copyFiles(sourceDir,  targetDir):
    if sourceDir.find(".svn") > 0:
        return
    for file in os.listdir(sourceDir):
        sourceFile = os.path.join(sourceDir,  file)
        targetFile = os.path.join(targetDir,  file)
        if os.path.isfile(sourceFile):
            if not os.path.exists(targetDir):
                os.makedirs(targetDir)
            if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
                    open(targetFile, "wb").write(open(sourceFile, "rb").read())
        if os.path.isdir(sourceFile):
            First_Directory = False
            copyFiles(sourceFile, targetFile)

def moveFileto(sourceDir,  targetDir):
    shutil.copy(sourceDir,  targetDir)

def build_go(sourceDir,outName,is64Bit,isRel,isLinux):
    if is64Bit:
        os.environ["GOARCH"] = "amd64"
    else:
        os.environ["GOARCH"] = "386"

    if isLinux:
        os.environ["CGO_ENABLED"] = "0"
        os.environ["GOEXE"] = ""
        os.environ["GOOS"] = "linux"
    else:
        os.environ["CGO_ENABLED"] = "1"
        os.environ["GOEXE"] = ".exe"
        os.environ["GOOS"] = "windows"

    os.environ["CC"] = "gcc"
    os.environ["CXX"] = "g++"

    ccurdir = os.curdir
    os.chdir(sourceDir)

    goRunBuild = go_dir
    goRunBuild += " build -v"

    if isRel:
        goRunBuild += ' -ldflags \"-w -s\"'

    goRunBuild += " -o " + outName
    os.system(goRunBuild)
    os.chdir(ccurdir)
    logtofile(goRunBuild)

def build_all():
    for bfile in buildfiles:
        build_go(source_dir + bfile[0],bfile[1],bfile[4],bfile[2],bfile[3])

if  __name__ =="__main__":
    logtofile("star build all...")
    build_all()
    logtofile("release done!!!")


#!/usr/bin/python3

def check_tile(grid, N, M, r, c):
    tile = grid[r][c]
    # At least one of both must be true or the set is imposible
    return all(grid[r][j]<=tile for j in range(M)) or all(grid[i][c]<=tile for i in range(N))

def check(grid, N, M):

    # Search For tiles where is not the higher in one direction
    # Row Compatibility
    for i in range(N):
        for j in range(M):
            if not check_tile(grid, N, M, i,j):
                return "NO"
    return "YES"

def solve(fd):

    T = int(fd.readline().strip())

    for c in range(T):
        N, M = [ int(a) for a in fd.readline().strip().split() ]
        grid = [ [ int(a) for a in fd.readline().strip().split() ] for i in range(N) ]
        print("Case #" + str(c+1) + ": " + check(grid, N, M))

if __name__ == "__main__":
    import sys

    fd = open(sys.argv[1], "r")
    solve(fd)
    fd.close()


'''
@Date: 2020-08-04 14:28:07
@Author: lcx

@Description:
'''

# 不要子类化dict, 去子类化UserDict

# 多重继承
class A:
    def ping(self):
        print('ping:', self)

class B(A):
    def pong(self):
        print('pong:', self)

class C(A):
    def pong(self):
        print('PONG:', self)

class D(B, C):

    def ping(self):
        super().ping()
        print('post-ping:', self)

    def pingpong(self):
        self.ping()
        super().ping()
        self.pong()
        super().pong()
        C.pong(self)

if __name__ == "__main__":
    # 通过__mro__属性判断继承顺序
    print(D.__mro__)
    d = D()
    d.pingpong()
    d.ping()
    d.pong()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Blog',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(max_length=80)),
                ('summury', models.CharField(max_length=300)),
                ('data_time', models.DateTimeField(auto_now_add=True)),
                ('editor', models.CharField(max_length=80)),
                ('body', models.TextField()),
                ('counts', models.IntegerField(default=0)),
            ],
        ),
        migrations.CreateModel(
            name='BlogComment',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('commentator', models.CharField(max_length=80)),
                ('comment', models.TextField()),
                ('data_time', models.DateTimeField(auto_now_add=True)),
                ('link', models.ForeignKey(to='blog.Blog')),
            ],
        ),
    ]


# -*- coding: utf-8 -*-
# BSD 3-Clause License
#
# Copyright (c) 2017 xxxx
# All rights reserved.
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ============================================================================
from __future__ import division
"""
WiderFace evaluation code
author: wondervictor
mail: tianhengcheng@gmail.com
copyright@wondervictor
"""
import os
import tqdm
import pickle
import argparse
import numpy as np
from scipy.io import loadmat
from bbox import bbox_overlaps
#from IPython import embed

def get_gt_boxes(gt_dir):
    """ gt dir: (wider_face_val.mat, wider_easy_val.mat, wider_medium_val.mat, wider_hard_val.mat)"""
    gt_dir = os.path.abspath(os.path.dirname(os.getcwd())) + "/data/wider_face/" + gt_dir
    gt_mat = loadmat(os.path.join(gt_dir, 'wider_face_val.mat'))
    hard_mat = loadmat(os.path.join(gt_dir, 'wider_hard_val.mat'))
    medium_mat = loadmat(os.path.join(gt_dir, 'wider_medium_val.mat'))
    easy_mat = loadmat(os.path.join(gt_dir, 'wider_easy_val.mat'))

    facebox_list = gt_mat['face_bbx_list']
    event_list = gt_mat['event_list']
    file_list = gt_mat['file_list']

    hard_gt_list = hard_mat['gt_list']
    medium_gt_list = medium_mat['gt_list']
    easy_gt_list = easy_mat['gt_list']

    return facebox_list, event_list, file_list, hard_gt_list, medium_gt_list, easy_gt_list

def get_gt_boxes_from_txt(gt_path, cache_dir):

    cache_file = os.path.join(cache_dir, 'gt_cache.pkl')
    if os.path.exists(cache_file):
        f = open(cache_file, 'rb')
        boxes = pickle.load(f)
        f.close()
        return boxes

    f = open(gt_path, 'r')
    state = 0
    lines = f.readlines()
    lines = list(map(lambda x: x.rstrip('\r\n'), lines))
    boxes = {}
    f.close()
    current_boxes = []
    current_name = None
    for line in lines:
        if state == 0 and '--' in line:
            state = 1
            current_name = line
            continue
        if state == 1:
            state = 2
            continue

        if state == 2 and '--' in line:
            state = 1
            boxes[current_name] = np.array(current_boxes).astype('float32')
            current_name = line
            current_boxes = []
            continue

        if state == 2:
            box = [float(x) for x in line.split(' ')[:4]]
            current_boxes.append(box)
            continue

    f = open(cache_file, 'wb')
    pickle.dump(boxes, f)
    f.close()
    return boxes

def read_pred_file(filepath):

    with open(filepath, 'r') as f:
        lines = f.readlines()
        img_file = lines[0].rstrip('\n\r')
        lines = lines[2:]

    boxes = np.array(list(map(lambda x: [float(a) for a in x.rstrip('\r\n').split(' ')], lines))).astype('float')
    return img_file.split('/')[-1], boxes

def get_preds(pred_dir):
    events = os.listdir(pred_dir)
    boxes = dict()
    pbar = tqdm.tqdm(events)

    for event in pbar:
        pbar.set_description('Reading Predictions ')
        event_dir = os.path.join(pred_dir, event)
        event_images = os.listdir(event_dir)
        current_event = dict()

        for imgtxt in event_images:
           # print(imgtxt)
            imgname, _boxes = read_pred_file(os.path.join(event_dir, imgtxt))
            current_event[imgname.rstrip('.jpg')] = _boxes
        boxes[event] = current_event
    return boxes

def norm_score(pred):
    """ norm score
    pred {key: [[x1,y1,x2,y2,s]]}
    """

    max_score = 0
    min_score = 1

    for _, k in pred.items():
        for _, v in k.items():
            if len(v) == 0:
                continue
            _min = np.min(v[:, -1])
            _max = np.max(v[:, -1])
            max_score = max(_max, max_score)
            min_score = min(_min, min_score)

    diff = max_score - min_score
    for _, k in pred.items():
        for _, v in k.items():
            if len(v) == 0:
                continue
            v[:, -1] = (v[:, -1] - min_score)/diff

def image_eval(pred, gt, ignore, iou_thresh):
    """ single image evaluation
    pred: Nx5
    gt: Nx4
    ignore:
    """
    _pred = pred.copy()
    _gt = gt.copy()
    pred_recall = np.zeros(_pred.shape[0])
    recall_list = np.zeros(_gt.shape[0])
    proposal_list = np.ones(_pred.shape[0])

    _pred[:, 2] = _pred[:, 2] + _pred[:, 0]
    _pred[:, 3] = _pred[:, 3] + _pred[:, 1]
    _gt[:, 2] = _gt[:, 2] + _gt[:, 0]
    _gt[:, 3] = _gt[:, 3] + _gt[:, 1]

    overlaps = bbox_overlaps(_pred[:, :4], _gt)

    for h in range(_pred.shape[0]):

        gt_overlap = overlaps[h]
        max_overlap, max_idx = gt_overlap.max(), gt_overlap.argmax()
        if max_overlap >= iou_thresh:
            if ignore[max_idx] == 0:
                recall_list[max_idx] = -1
                proposal_list[h] = -1
            elif recall_list[max_idx] == 0:
                recall_list[max_idx] = 1

        r_keep_index = np.where(recall_list == 1)[0]
        pred_recall[h] = len(r_keep_index)
    return pred_recall, proposal_list

def img_pr_info(thresh_num, pred_info, proposal_list, pred_recall):
    pr_info = np.zeros((thresh_num, 2)).astype('float')
    for t in range(thresh_num):

        thresh = 1 - (t+1)/thresh_num
        r_index = np.where(pred_info[:, 4] >= thresh)[0]
        if len(r_index) == 0:
            pr_info[t, 0] = 0
            pr_info[t, 1] = 0
        else:
            r_index = r_index[-1]
            p_index = np.where(proposal_list[:r_index+1] == 1)[0]
            pr_info[t, 0] = len(p_index)
            pr_info[t, 1] = pred_recall[r_index]
    return pr_info

def dataset_pr_info(thresh_num, pr_curve, count_face):
    _pr_curve = np.zeros((thresh_num, 2))
    for i in range(thresh_num):
        _pr_curve[i, 0] = pr_curve[i, 1] / pr_curve[i, 0]
        _pr_curve[i, 1] = pr_curve[i, 1] / count_face
    return _pr_curve

def voc_ap(rec, prec):

    # correct AP calculation
    # first append sentinel values at the end
    mrec = np.concatenate(([0.], rec, [1.]))
    mpre = np.concatenate(([0.], prec, [0.]))

    # compute the precision envelope
    for i in range(mpre.size - 1, 0, -1):
        mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])

    # to calculate area under PR curve, look for points
    # where X axis (recall) changes value
    i = np.where(mrec[1:] != mrec[:-1])[0]

    # and sum (\Delta recall) * prec
    ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
    return ap

def evaluation(pred, gt_path, all, iou_thresh=0.4):
    pred = get_preds(pred)
    norm_score(pred)
    facebox_list, event_list, file_list, hard_gt_list, medium_gt_list, easy_gt_list = get_gt_boxes(gt_path)
    event_num = len(event_list)
    thresh_num = 1000
    settings = ['easy', 'medium', 'hard']
    setting_gts = [easy_gt_list, medium_gt_list, hard_gt_list]

    if not all:
        aps = []
        for setting_id in range(3):
            # different setting
            gt_list = setting_gts[setting_id]
            count_face = 0
            pr_curve = np.zeros((thresh_num, 2)).astype('float')
            # [hard, medium, easy]
            pbar = tqdm.tqdm(range(event_num))  #  61
            error_count = 0
            for i in pbar:
                pbar.set_description('Processing {}'.format(settings[setting_id]))
                event_name = str(event_list[i][0][0])
                img_list = file_list[i][0]
                pred_list = pred[event_name]
                sub_gt_list = gt_list[i][0]
                print("shape of sub_gt_list is: ",sub_gt_list.shape)
                gt_bbx_list = facebox_list[i][0]

                for j in range(len(img_list)):
                    try:
                        pred_info = pred_list[str(img_list[j][0][0])]
                    except:
                        error_count+=1
                        continue

                    gt_boxes = gt_bbx_list[j][0].astype('float')
                    keep_index = sub_gt_list[j][0]
                    count_face += len(keep_index)
                    if len(gt_boxes) == 0 or len(pred_info) == 0:
                        continue
                    ignore = np.zeros(gt_boxes.shape[0])
                    if len(keep_index) != 0:
                        ignore[keep_index-1] = 1
                    pred_recall, proposal_list = image_eval(pred_info, gt_boxes, ignore, iou_thresh)

                    _img_pr_info = img_pr_info(thresh_num, pred_info, proposal_list, pred_recall)

                    pr_curve += _img_pr_info
            print("error_count is: ",error_count)
            pr_curve = dataset_pr_info(thresh_num, pr_curve, count_face)

            propose = pr_curve[:, 0]
            recall = pr_curve[:, 1]

            ap = voc_ap(recall, propose)
            aps.append(ap)

        print("==================== Results ====================")
        print("Easy   Val AP: {}".format(aps[0]))
        print("Medium Val AP: {}".format(aps[1]))
        print("Hard   Val AP: {}".format(aps[2]))
        print("=================================================")
    else:
        aps = []
        # different setting
        count_face = 0
        pr_curve = np.zeros((thresh_num, 2)).astype('float')  #  control calcultate how many samples
        # [hard, medium, easy]
        pbar = tqdm.tqdm(range(event_num))
        error_count = 0
        for i in pbar:
            pbar.set_description('Processing {}'.format("all"))
            # print("event_list is: ",event_list)
            event_name = str(event_list[i][0][0])  #  '0--Parade', '1--Handshaking'
            img_list = file_list[i][0]
            pred_list = pred[event_name]  #  每个文件夹的所有检测结果
            sub_gt_list = [ setting_gts[0][i][0], setting_gts[1][i][0], setting_gts[2][i][0] ]

            gt_bbx_list = facebox_list[i][0]
            for j in range(len(img_list)):
                try:
                    pred_info = pred_list[str(img_list[j][0][0])]  #   # str(img_list[j][0][0] 是每个folder下面的图片名字
                except:
                    error_count+=1
                    continue

                gt_boxes = gt_bbx_list[j][0].astype('float')
                temp_i = []
                for ii in range(3):
                    if len(sub_gt_list[ii][j][0])!=0:
                        temp_i.append(ii)
                if len(temp_i)!=0:
                    keep_index = np.concatenate(tuple([sub_gt_list[xx][j][0] for xx in temp_i]))
                else:
                    keep_index = []
                count_face += len(keep_index)

                if len(gt_boxes) == 0 or len(pred_info) == 0:
                    continue
                ignore = np.zeros(gt_boxes.shape[0]) # #  no ignore
                if len(keep_index) != 0:
                    ignore[keep_index-1] = 1
                pred_recall, proposal_list = image_eval(pred_info, gt_boxes, ignore, iou_thresh)

                _img_pr_info = img_pr_info(thresh_num, pred_info, proposal_list, pred_recall)

                pr_curve += _img_pr_info

        pr_curve = dataset_pr_info(thresh_num, pr_curve, count_face)

        propose = pr_curve[:, 0]
        recall = pr_curve[:, 1]

        ap = voc_ap(recall, propose)
        aps.append(ap)

        print("==================== Results ====================")
        print("All Val AP: {}".format(aps[0]))
        print("=================================================")

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--pred', default=os.path.abspath(os.path.dirname(os.getcwd())) + '/output/widerface')
    parser.add_argument('-g', '--gt', default='./ground_truth')
    parser.add_argument('--all', help='if test all together', action='store_true')

    args = parser.parse_args()
    evaluation(args.pred, args.gt, args.all)


import os

list_of_students = ['Maciek', 'Oskar', 'Franek']

def print_list():
    for student in list_of_students:
        print(student)

def add_student():
    student = input("Wpisz studenta którego chcesz dodac")
    list_of_students.append(student)

def add_at_number():
    number = int(input("Wpisz numer na liście studenta"))
    student = input("Wpisz nazwę studenta ")
    if number > 0:
        list_of_students.insert((number - 1), student)
    else:
        print('Musisz wpisac liczbę większą od 0 ')

while True:
    print("\n1.Wyświetl liste studentów \n2.Dodaj Studenta na końcu listy \n3.Dodaj studenta w odpowiednim miejscu listy ")
    try:
        what_to_do = int(input("Enter choice:"))
    except ValueError:
        print("Sorry, i dont understand use only numbers")
        continue
    if what_to_do == 1:
        print_list()
    if what_to_do == 2:
        add_student()
        print("student dodany")
        print_list()
    if what_to_do == 3:
        add_at_number()
        print("student dodany")
        print_list()


from django import forms
from django.forms.models import inlineformset_factory

from .models import Protocol, Institution, Topic

class InstitutionForm(forms.ModelForm):
    class Meta:
        model = Institution
        fields = ("name", )

class TopicForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        instance = super(TopicForm, self).save(commit=False)
        import ipdb; ipdb.set_trace()
        self.protocol = Protocol.objects.get(kwargs['protocol'])
        self.save_m2m()
        return instance

    class Meta:
        model = Topic
        exclude = ('protocol', 'attachment')
        fields = (
            "name",
            "voted_for",
            "voted_against",
            "voted_abstain",
            "statement", )

TopicFormSet = inlineformset_factory(Protocol, Topic, extra=2)

class ProtocolForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        return super(ProtocolForm, self).save()

    class Meta:
        model = Protocol
        fields = (
            "institution",
            "number",
            "scheduled_time",
            "absent",
            "attendents",
            "start_time",
            "additional",
            "quorum",
            "majority",
            "current_majority",
            "information", )


import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier

trainfile="CBIR_Proj.csv"
data=pd.read_csv(trainfile)
data.head()

df,dftest=train_test_split(data,test_size=0.2, random_state=42)
X=data[['major_axis','minor_axis','eccentricity','equiv_diameter','perimeter','area','mean','std','skewness','surf-location1','surf-location2','surf-metric','surf-scale','aspect_ratio']]
y=data.category
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X,y)

fi="CBIR_Proj_test12.csv"
testdf=pd.read_csv(fi)

Xfinal=testdf[['major_axis','minor_axis','eccentricity','equiv_diameter','perimeter','area','mean','std','skewness','surf-location1','surf-location2','surf-metric','surf-scale','aspect_ratio']]
out=pd.DataFrame()
out['Images']=testdf['Images']
out['category']=rfc.predict(Xfinal)

fileout="final_predict_rfc1.csv"
out[['Images','category']].to_csv(fileout,index=False)


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def lcm(a, b):
    return a // gcd(a, b) * b

N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [a//2 for a in A]

while A[0] % 2 == 0:
    for i in range(N):
        if A[i] % 2 != 0:
            print(0)
            exit()
        A[i] //= 2
    M //= 2

for i in range(N):
    if A[i] % 2 == 0:
        print(0)
        exit()

lcm_ = 1
for i in range(N):
    lcm_ = lcm(lcm_, A[i])
    if M < lcm_:
        print(0)
        exit()

print((M // lcm_ + 1) // 2)


from django.conf.urls import url
from django.conf.urls import patterns
from django.views.generic import ListView, DetailView
from visit.apps.places.models import *
from visit.apps.places.views import PlaceTypeDetailView

urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset=PlaceType.objects.all(),
            allow_empty=True,
            paginate_by=25,
            template_name="places/place_type_list.html",
            context_object_name="place_types",
        ),
        name='places_list'),
    url(r'^(?P<slug>[\w-]+)/$',
        PlaceTypeDetailView.as_view(
            template_name="places/place_type.html",
            model=PlaceType,
            context_object_name="place_type"
        ), name='place_type'),
    url(r'^(?P<slug>[\w-]+)/category/(?P<category_slug>[\w-]+)/(?P<category_id>\d+)/$',
        PlaceTypeDetailView.as_view(
            template_name="places/place_type.html",
            model=PlaceType,
            context_object_name="place_type"
        ), name='place_type'),
    url(r'^(?P<place_type_slug>[\w-]+)/(?P<slug>[\w-]+)/(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Place,
            template_name="places/place.html",
            context_object_name="place",
        ), name='place'),
)


# -*- coding: utf-8 -*-
"""
This module provides functionality for interpreting BCA assays.
"""

from __future__ import absolute_import, division

from collections import OrderedDict
import logging
import os
import re

from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np
import pandas as pd
from scipy.stats import linregress

import pyproteome as pyp

LOGGER = logging.getLogger("pyproteome.bca")
RE_ROW_COL = re.compile(r"([A-Z]+)(\d+)")

def _interpret_pos(pos):
    """
    Turn a position (i.e. "A1:B2") into integers for the row / column range.

    Parameters
    ----------
    pos : str

    Returns
    -------
    col_start : int
    col_end : int
    row_start : int
    row_end : int
    """
    lst = pos.split(":")

    m = RE_ROW_COL.match(lst[0])
    row_start, col_start = m.groups()
    col_start = int(col_start)

    if len(lst) > 1:
        m = RE_ROW_COL.match(lst[1])
        row_end, col_end = m.groups()
        col_end = int(col_end)
    else:
        row_end, col_end = None, None

    return col_start, col_end, row_start, row_end

def _next_chr(char):
    return chr(ord(char) + 1)

def interpret_bca_assay(
    xls_path,
    samples,
    volumes=None,
    standards=None,
    n=3,
    data_row_start=27,
):
    u"""
    Load and interpret a plate reader output.

    Fits a curve to the standards, checks data sanity, and calculates sample
    concentrations.

    Parameters
    ----------
    xls_path : str
        Path to output of Tecan reader. File should be located in
        paths.BCA_ASSAY_DIR.
    samples : list of tuple of (str, float, str)
        Sample names, dilutions, and positions within the plate. Positions
        should be supplied as either the left index of the first of three
        aliquots (i.e. "A4") or a range indicating the starting and ending row
        and column (i.e. "A4:B6").
    volumes : dict of str, float or int or float
        Volumes of samples, in μL.
    standards : list of (tuple of int, float), optional
        Locations of standards in table. Defaults to the first n (below)
        columns, rows A-G, with concentrations 1 - 0.0625 mg / mL, followed by
        two blanks of PBS.
    n : int, optional
        Number of replicates to use for standards and samples (Defaults to 3).
    data_row_start : int, optional
        Starting row in excel file for absorbance table.

    Returns
    -------
    total_protein : dict of str, tuple of (float, float)
        Sample total protein contents. Supplies the mean and standard deviation
        for the calculated protein content (in μg)
    concentrations : dict of list of float
        Individual concentrations calculations.

    Raises
    ------

    Examples
    --------
    >>> from pyproteome import bca
    >>> total_protein, _ = bca.interpret_bca_assay(
    ...     "bsa-sample.xlsx",
    ...     samples=[
    ...         ("3157 Hippocampus", 10, "A4:B6"),
    ...         ("3157 Cortex", 10, "C4:D6"),
    ...         ("3146 Cerebellum", 10, "E4:F6"),
    ...         ("3146 Hippocampus", 10, "G4:H6"),
    ...     ],
    ...     volumes={
    ...         "3157 Hippocampus": 3000,
    ...         "3157 Cortex": 3000,
    ...         "3146 Cerebellum": 3000,
    ...         "3146 Hippocampus": 3000,
    ...     },
    ... )
    >>> cortex = total_protein["3157 Cortex"]
    >>> print("{:.2f} +/- {:.2f} ug".format(cortex[0], cortex[1])
    8094.51 +/- 340.66 ug
    """
    if volumes is None:
        volumes = {i[0]: 1000 for i in samples}
    elif isinstance(volumes, int) or isinstance(volumes, float):
        volumes = {i[0]: volumes for i in samples}

    xls_path = os.path.join(pyp.paths.BCA_ASSAY_DIR, xls_path)

    xls = pd.read_excel(
        xls_path,
        skiprows=(
            list(range(data_row_start)) +
            list(range(data_row_start + 9, data_row_start + 13))
        ),
        index_col=0,
    )
    # print(xls)

    if isinstance(xls, dict):
        assert len(xls) == 1
        xls = list(xls.values())[0]

    # Drop columns with NaN values
    # xls = xls.dropna(
    #     axis=1,
    #     how="any",
    # )

    std_start_col = xls.columns[0]

    if standards is None:
        standards = [
            ("A{}".format(std_start_col), 1.0),
            ("B{}".format(std_start_col), 0.5),
            ("C{}".format(std_start_col), 0.25),
            ("D{}".format(std_start_col), 0.125),
            ("E{}".format(std_start_col), 0.0625),
            ("F{}".format(std_start_col), 0),
            ("G{}".format(std_start_col), 0),
        ]

    # Extract out standards absorbances
    std_x = []
    std_y = []

    for pos, x in standards:
        col_start, col_end, row_start, row_end = _interpret_pos(pos)

        if col_end is None:
            col_end = col_start + 2

        if row_end is None:
            row_end = row_start

        row = row_start

        while row <= row_end:
            for col in range(col_start, col_end + 1):
                std_x.append(x)
                std_y.append(xls.loc[row][col])

            row = _next_chr(row)

    # Fit a linear regression model to standards
    slope, intercept, r, _, _ = linregress(std_x, std_y)

    # Plot these points on a graph
    f, ax = plt.subplots(figsize=(16, 12))
    ax.scatter(
        x=std_x,
        y=std_y,
        s=50,
        label="BSA Standards",
    )
    ax.plot(
        [0, max(std_x)],
        [intercept, intercept + slope * max(std_x)],
    )

    ax.set_title("BSA Standards $R^2$ = {:.3f}".format(r ** 2))
    ax.set_xlabel("Concentration (mg / mL)")
    ax.set_ylabel("Absorbance (AU)")

    # Sanity check standards
    if r ** 2 < 0.95:
        ax.legend(loc="lower right")
        raise Exception("R^2 of standards = {:.2f} (< 0.95)".format(r ** 2))

    # Extract out sample absorbances and concentrations
    absorbances = pyp.utils.DefaultOrderedDict(list)
    concentrations = pyp.utils.DefaultOrderedDict(list)
    total_protein = OrderedDict()

    for name, dilution, pos in samples:
        col_start, col_end, row_start, row_end = _interpret_pos(pos)

        if col_end is None:
            col_end = col_start + 3

        if row_end is None:
            row_end = row_start

        row = row_start

        while row <= row_end:
            for col in range(col_start, col_end + 1):
                absorbance = xls.loc[row][col]
                concentration = (absorbance - intercept) / slope
                concentrations[name].append(concentration * dilution)
                absorbances[(name, dilution)].append(absorbance)

            row = _next_chr(row)

    # Plot samples and show to user
    colors = cm.rainbow(np.linspace(0, 1, len(absorbances)))
    for color, kv in zip(colors, absorbances.items()):
        key, val = kv
        name, dilution = key
        y = np.array(val)
        x = (y - intercept) / slope
        ax.scatter(x, y, s=100, label=name, color=color, marker="*")

    ax.legend(loc="lower right")

    # Sanity check concentrations

    # Calculate total sample protein content
    for name, conc in concentrations.items():
        protein = np.array(conc) * volumes[name]
        total_protein[name] = (
            protein.mean(),
            protein.std(),
        )

    return total_protein, concentrations


from socketengine import client
from threading import Thread

import psutil
import socket
import time

def getHostIP():
	host = socket.gethostname()
	return host, socket.gethostbyname(host)

class reporter:
	def __init__(self, address='127.0.0.1', port=8080, start=True, pi=True, channel='reporter', updateTime=1):
		self.socket = None
		self.address = address
		self.port = port
		self.usingPi = pi
		self.channel = channel
		self.updateTime = updateTime
		if self.usingPi:
			from gpiozero import CPUTemperature
			self.getTemp = lambda: CPUTemperature()
		self.stopped = False
		if start:
			self.start()

	def start(self):
		self.startTime = time.time()
		self.socket = client(self.address, self.port).start()
		self.socket.write("connected", True)
		Thread(target=self._update, args=()).start()
		return self

	def _update(self):
		while True:
			if self.stopped:
				self.socket.close()
				return
			data = {}
			hostname, ip = getHostIP()
			cpu = psutil.cpu_percent()
			ram = dict(psutil.virtual_memory()._asdict())['percent']

			data = {
				'hostname': hostname,
				'ip':		ip,
				'cpu':		cpu,
				'ram':		ram,
				'time':		time.time(),
				'runtime':	(time.time()-self.startTime)
			}
			if self.usingPi:
				data['cpu_temp'] = self.getTemp()
			self.socket.write(self.channel, data)
			time.sleep(self.updateTime)

	def close(self):
		self.stopped = True


from algorithm.mobo.mobo import MOBO

'''
High-level algorithm specifications by providing spec.
'''

class DGEMO(MOBO):
    '''
    DGEMO [Luković et al. 2020].
    '''
    spec = {
        'surrogate': 'gp',
        'acquisition': 'identity',
        'solver': 'discovery',
        'selection': 'dgemo',
    }

class TSEMO(MOBO):
    '''
    TSEMO [Bradford et al. 2018].
    '''
    spec = {
        'surrogate': 'ts',
        'acquisition': 'identity',
        'solver': 'nsga2',
        'selection': 'hvi',
    }

class USEMO_EI(MOBO):
    '''
    USeMO [Belakaria and Deshwal 2020], using EI as acquisition.
    '''
    spec = {
        'surrogate': 'gp',
        'acquisition': 'ei',
        'solver': 'nsga2',
        'selection': 'uncertainty',
    }

class MOEAD_EGO(MOBO):
    '''
    MOEA/D-EGO [Zhang et al. 2009].
    '''
    spec = {
        'surrogate': 'gp',
        'acquisition': 'ei',
        'solver': 'moead',
        'selection': 'moead',
    }

class ParEGO(MOBO):
    '''
    ParEGO [Knowles et al. 2006].
    '''
    spec = {
        'surrogate': 'gp',
        'acquisition': 'ei',
        'solver': 'parego',
        'selection': 'random',
    }

'''
Define new algorithms here
'''

class Custom(MOBO):
    '''
    Fully customized algorithms which totally rely on user arguments to specify each component.
    '''
    spec = None

algos = {
    'dgemo': DGEMO,
    'tsemo': TSEMO,
    'usemo-ei': USEMO_EI,
    'moead-ego': MOEAD_EGO,
    'parego': ParEGO,
    'custom': Custom,
}

def get_algorithm(name):
    '''
    Get class of algorithm by name
    '''
    try:
        return algos[name]
    except:
        return None

def get_algorithm_list():
    '''
    Get names of available algorithms
    '''
    return list(algos.keys())


from pylab import *

########DEFINE A VARIABLE CLASS#########
class PyCommandemClass():
	########DEFINE A DEFAULT CONSTRUCTOR#########
	def __init__(self,**kwargs):
		####update parameters with the specified ones
		items=kwargs.items();
		for k in xrange(len(items)):
			self[items[k][0]]=items[k][1];
		####add specific or from all paramaters
		self.setFromSharedAttributes(**{'Attributes':[]});
		####monitoring init#####################
		#self.initMonitoredObject();

	################################
	####SET SHORTCUTS TO SYSTEMS
	"""
	def setSystemAliases(self,**kwargs):
		##build the queries
		AliasQueries=self.getAliasQueries(**
									{
										#'locals':locals
										'StructureName':"self.__dict__",
										'Aliases':
										#shortcuts to VariableArray Parent Objects
										[
											{
												'Name':'_Sensem',
												'PathArray':[
																'NetParent',
																'SimulatedSensomChild',
																{
																	'GroupName':'Sensems',
																	'TargetedAttributes':{'SensoredPopulemName':self['CommandedPopulemName']}
																}
															]
											}
										]
									}
								);
		##build the aliases
		for AliasQuery in AliasQueries:
			exec(AliasQuery);
	"""
	################################
	##INIT VARIABLEARRAYS
	def initVariableArrays(self,**kwargs):

		"""
		print('WTF');
		plug=reduce(self.parsePathArray,['NetParent','SimulatedSensomChild',
																			{
																				'GroupName':'Sensems',
																				'TargetedAttributes':{'SensoredPopulemName':self['CommandedPopulemName']}
																			},0,'ID'],self.__dict__)
		"""

		"""
		di=[self.getSystemAliasQuery(**{
										'SystomName':SystomAttributes[0],
										'SystomTag':'Simulated',
										'TargetedAttributes':SystomAttributes[1]
										}) for SystomAttributes in [
																		['Sensom',{'SensoredPopulemName':self['CommandedPopulemName']}]
																	]];

		print('AAA',di)

		AliasQueries=self.getAliasQueries(**{'Aliases':[
															{
																'Name':'_Sensem',
																#'PathArray':['NetParent','SimulatedSensomChild','Sensems','Units',0]
																'PathArray':['NetParent','SimulatedSensomChild',
																			{
																				'GroupName':'Sensems',
																				'TargetedAttributes':{'SensoredPopulemName':self['CommandedPopulemName']}
																			},0,'ID']
															}
														]
											});
		"""

		AliasQueries=self.getAliasQueries(**{'Aliases':[self.getSystemAliasQuery(**{
										'SystomName':SystomAttributes[0],
										'SystomTag':'Simulated',
										'TargetedAttributes':SystomAttributes[1]
										}) for SystomAttributes in [
																		['Sensom',{'SensoredPopulemName':self['CommandedPopulemName']}]
																	]]
											});

		print(AliasQueries)
		##set the Aliases
		for AliasQuery in AliasQueries:
			exec(AliasQuery);
		print(self['_Sensem']);

		##set an Alias for the simulated sensem
		self['_Sensem']=self['NetParent']['SimulatedSensomChild'].getChildFromTargetedAttributes(**{
											'TargetedAttributes':
											{
												'SensoredPopulemName':self['CommandedPopulemName'],
											},
											'GroupName':'Sensems'
										}
									)[0];

		##set Local Parameters
		SensorsNumber=self['_Sensem']['SensorsNumber'];
		MonitoredSensorsIdx=self['_Sensem']['MonitoredSensorsIdx'];

		##init VariableArrays
		self['VariableArrays']={
						'SharedAttributes':{},
						'Units':
						{
							'Commands':sys.modules['PyVariableArrayClass'].PyVariableArrayClass(**
								{
									'InstantaneousValues':np.zeros(SensorsNumber,dtype=float),
									'MonitoredVariablesIdx':MonitoredSensorsIdx,
									'Color':sys.modules['PyTools'].repeatMotif(sys.modules['PyTools'].getSpecificColors()[0],len(MonitoredSensorsIdx))
								})
						}
					 };

	################################
	####BUILD SHORTCUTS
	def setShortCuts(self,**kwargs):
		##build the queries
		AliasQueries=self.getSelfAliasQueries(**
									{
										#'locals':locals
										'StructureName':"self.__dict__",
										'Aliases':
										#shortcuts to VariableArray Parent Objects
										[
											{'AliasName':Alias[0],'AliasedVariablePathArray':Alias[1]} for Alias in [
															['_Runom',['NetParent','SimulatedRunomChild']],
															#['Commandom',['SimulatedCommandomChild']],
															#['_Sensom',['NetParent','SimulatedSensomChild']]
															#['Populems',['SimulatedPopulomChild','Populems']]
														]
														+
														[
															[VariablePathArray[0],VariablePathArray[1]+['VariableArrays','Units',VariablePathArray[0][1:]]] for VariablePathArray in [
																				['_Commands',[]]
															#					[[],'V'],
															#					[[],'MuExt'],
															#					[['SimulatedInputChild'],'Gamma'],
															#					[['NetParent','SimulatedCommandomChild'],'c']
															#					]
																	]
														]
										]
									}
								);
		##build the aliases
		for AliasQuery in AliasQueries:
			exec(AliasQuery);

	"""
	########INIT GLOBAL VARIABLE CONTROLLERS FOR A SPECIFIC SIMULATION
	def initSimulation(self,**kwargs):
		pass;
	"""

	##############################
	def runOneStepTime(self,**kwargs):
		getattr(self,'set'+self['Name'])(**kwargs);

	########SET COMMANDS METHODS#############################################
	def setAbsoluteCrenelom(self,**kwargs):
		if self['NetParent']['Time']>=self['StepStartTime'] and self['NetParent']['Time']<=self['StepStopTime']:
			self['_Commands']['InstantaneousValues']=self['StepValue0']*np.ones(self['_Sensom']['SensorsNumber'],dtype=float);
		else:
			self['_Commands']['InstantaneousValues']=self['StepValue0']*np.zeros(self['_Sensom']['SensorsNumber'],dtype=float);

	def setThresholdCrenelom(self,**kwargs):
		#compute the level of command equal to the Vrest-VThre amplitude
		if self.hasKeyAttr(self,'StepValue0')==False or self['StepValue0']==None:
			if self.hasKeyAttr(self['NetParent']['SimulatedPopulomeChild'],'SimulatedPopulationChild'):
				self['StepValue0']=(mean(self['NetParent']['SimulatedPopulomeChild']['SimulatedPopulationChild']['VThreshold'])-self['NetParent']['PopulomeChild']['PopulationChild']['VRest'])/self['NetParent']['SimulatedPopulomeChild']['PopulationChild']['NeuronsNumber'];
				print(self['c0'])
			elif self.hasKeyAttr(self['NetParent']['PopulomeChild'],'PopulationObjects'):
				self['StepValue0']=mean(self['NetParent']['PopulomeChild']['PopulationObjects'][0]['VThreshold'])-self['NetParent']['PopulomeChild']['PopulationChild']['VRest'];

		#compute for any time t
		self['Time']=self['NetParent']['SimulatedRunChild']['VariableArrays']['Units']['Time']['InstantaneousValues'];
		if self['NetParent']['Time']>=self['StepStartTime'] and self['NetParent']['Time']<=self['StepStopTime']:
			self['c']['InstantaneousValues']=self['StepValue0']*np.ones(self['SensorsNumber'],dtype=float);
		else:
			self['c']['InstantaneousValues']=self['StepValue0']*np.zeros(self['SensorsNumber'],dtype=float);

	def setOscillatom(self,**kwargs):
		self['Time']=self['NetParent']['SimulatedRunChild']['VariableArrays']['Units']['Time']['InstantaneousValues'];
		for i in xrange(kwargs['SensorsNumber']):
			self['c']['InstantaneousValues'][i]=self['StepValue0']+kwargs['c1']*np.sin(2.*pi*kwargs['f0']*0.001*self.t+kwargs['phi']);

	def setVariable(self,**kwargs):
		Time=self['NetParent']['SimulatedRunChild']['VariableArrays']['Units']['Time']['InstantaneousValues'];
		if Time==self['NetParent']['SimulatedRunChild']['t0']:
			self.cRan=np.zeros((kwargs['SensorsNumber'],self['TimeStepsNumber']),dtype=float);
			for SensorIdx in xrange(kwargs['SensorsNumber']):
				self.cRan[SensorIdx,0]=self['StepValue0'];
				for TimeIdx in xrange(self['TimeStepsNumber']):
					self.cRan[SensorIdx,TimeIdx]=self.cRan[SensorIdx,TimeIdx-1]+0.1*(-0.5+scipy.stats.uniform.rvs(size=1));
					self.cRan[SensorIdx,TimeIdx]=min(self.cRan[SensorIdx,TimeIdxk],self['StepValue0']+kwargs['deltac']);
					self.cRan[SensorIdx,TimeIdx]=max(self.cRan[SensorIdx,TimeIdx],self['StepValue0']-kwargs['deltac']);
				#filtC=PyToolsClass.smooth(self.cRan[i,:]/max(self.cRan[i,:]),10.);
				filtC=PyToolsClass.smooth(self.cRan[SensorIdx,:],10.);
				self.cRan[SensorIdx,:]=abs(np.concatenate((array([0. for u in xrange(-len(filtC)+self['TimeStepsNumber'])]),filtC)));
		self['_Commands']['InstantaneousValues']=self.cRan[:,self.indxTime];


# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 13:40:21 2018

@author: Administrator
"""

import urllib.request as r
url='http://api.openweathermap.org/data/2.5/forecast?q=mianyang,cn&mode=json&lang=zh_cn&&APPID=6a67ed641c0fda8b69715c43518b6996&units=metric'
data=r.urlopen(url).read().decode('utf-8')
import json
data=json.loads(data)
##########中文版第一问

for x in range(0,20,2):
    a=data["city"]['name']
    b=data["list"][x]['dt_txt']
    c=data["list"][x]["main"]["temp"]
    d=data["list"][x]["weather"][0]["description"]
    e=data["list"][x]["main"]["pressure"]
    f=data["list"][x]["main"]["temp_max"]
    g=data["list"][x]["main"]["temp_min"]
    print("{},{},天气是：{}".format(a,b,d))
    if d=="大雨":
        print("请带伞！")
    elif d=="中雨":
        print("请带伞！")
    elif d=="小雨":
        print("请带伞！")
    else:
        print("玩开心~")

for i in range(10):
    if i==1:
        print("◐"*7)
    elif i==2:
        print("☞****你****☜")
    elif i==3:
        print("☞****的****☜")
    elif i==4:
        print("☞****贴****☜")
    elif i==5:
        print("☞****心****☜")
    elif i==6:
        print("☞****小****☜")
    elif i==7:
        print("☞****管****☜")
    elif i==8:
        print("☞****家****☜")
    elif i==9:
        print("◐"*7)

练习七：全球天气未来3天.py

练习七：全球天气未来3天.py


'''
Check finance statistics by Requests.

Update log: (date / version / author : comments)
2018-02-15 / 1.0.0 / Du Jiang : Creation
                                Support Yahoo Finance stock

Notes:
1. It requires 3rd parth python lib (at least): requests.
2. When using Selenium, the webdriver.Firefox.get() does not return, always timeout.
3. Those page load detection methods are all not working.
'''

from concurrent.futures import ThreadPoolExecutor
import csv
import getopt
from http import HTTPStatus
import json
import string
import sys
from time import localtime, strftime, time

from bs4 import BeautifulSoup
import requests

# Global variables.
# The value can be updated by command line options.
__data_type = None
__inventory_info_file_path = None
__result_output_file_path = None
__concurrent_max_workers = 5

__Constants = None

class Constants_Base(object):
    '''
    Use a class to keep constant variables.
    '''

    WAIT_PAGE_LOAD_MAX_TRY = 10

    RESULT = "Result"
    ERROR = "Error"

    RESULT_OK = "Ok"
    RESULT_ERROR = "Error"

    START_TIME = "Start time"
    STOP_TIME = "Stop time"

    INVENTORIES = "Inventories"
    RECORDS_NUMBER = "Records number"
    RECORD = "Record"
    INVENTORY_DATA = "Inventory data"

    URL = "URL"
    STATUS_CODE = "Status code"

class Constants_YahooStock(Constants_Base):
    API_URL = "https://finance.yahoo.com/quote/{0}/key-statistics"

    COLUMN_NAME = "Name"
    COLUMN_EXCHANGE = "Exchange"
    COLUMN_TICKER = "Ticker"

    SECTION_STOCK_INFO = "Stock info"
    STOCK_INFO_NAME = "Name"
    STOCK_INFO_EXCHANGE = "Exchange"
    STOCK_INFO_TICKER = "Ticker"

    SECTION_MARKET_INFO = "Market info"

def check_url(url):
    '''
    Use requests to get URL, and return HTTP status code.

    @param url: A string of URL.
    @return: HTTP response status code, or None if request failed.
    @return: Parsed HTTP response, or None if request failed.
    '''

    print("url =", url)
    status_code = None
    parsed_data = None

    for i in range(0, __Constants.WAIT_PAGE_LOAD_MAX_TRY):
        try:
            headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"}
            response = requests.get(url, headers = headers, timeout = 5)
            # print("response =", response)
            print("response.status_code =", response.status_code)

            if response.history:
                status_code = HTTPStatus.OK
                print("response.status_code (Due to redirected) =", status_code)
            else:
                status_code = response.status_code
                response.raise_for_status()

            # There is non-printable characters for Yahoo page.
            # print("text_data =\n", ascii(http_response.text))
            # There is error while parasing yahoo page.
            # print("json_data =\n", http_response.json())

            parsed_data = BeautifulSoup(response.text, "lxml-xml")
            # print("parsed_data =\n", parsed_data)

            if __data_type == 0:
                try:
                    stock_name_info_section = parsed_data.find(
                        "h1", {"data-reactid": 7})
                    if not stock_name_info_section:
                        raise
                except Exception:
                    raise Exception("Cannot find stock_name_info_section.")

            print("Check url: Count {0}: url = {1}".format(
                i, url))

            # It is ok, no need retry.
            break
        except Exception as e:
            print("Check url: Count {0}: Exception = {1}".format(
                i, e))
            # If reach max try.
            if (i + 1) == __Constants.WAIT_PAGE_LOAD_MAX_TRY:
                raise e

    return status_code, parsed_data

def parse_get_data_yahoo_stock(parsed_http_response, results):
    '''
    Analyse HTTP response data.

    @param parsed_http_response : Parsed HTTP response.
    @param results : Dict with return results.
    '''

    try:
        results[__Constants.SECTION_STOCK_INFO] = {}

        # The return object from find() is class 'bs4.element.Tag'.
        try:
            stock_name_info_section = parsed_http_response.find(
                "h1", {"data-reactid": 7})

            if not stock_name_info_section:
                raise

            # The stock name may contain non-printable characters.
            # print("stock_name_info_section =", stock_name_info_section)
        except Exception:
            raise Exception("Cannot find stock_name_info_section.")

        # Remove possible non-printable characters.
        stock_name_info_text = ''.join(
            [x for x in stock_name_info_section.get_text().strip() if x in string.printable])

        stock_name_info = stock_name_info_text.rsplit(" ", 1)
        print("stock_name_info =", stock_name_info)
        results[__Constants.SECTION_STOCK_INFO][__Constants.STOCK_INFO_NAME] = stock_name_info[0]
        ticker_exchange_info = stock_name_info[1][1:-1].split(".")
        print("ticker_exchange_info =", ticker_exchange_info)
        results[__Constants.SECTION_STOCK_INFO][__Constants.STOCK_INFO_TICKER] = ticker_exchange_info[0]
        if len(ticker_exchange_info) > 1:
            results[__Constants.SECTION_STOCK_INFO][__Constants.STOCK_INFO_EXCHANGE] = ticker_exchange_info[1]
        else:
            results[__Constants.SECTION_STOCK_INFO][__Constants.STOCK_INFO_EXCHANGE] = ""

        results[__Constants.SECTION_MARKET_INFO] = {}

        try:
            misc_info_section = parsed_http_response.find(
                "div", {"id": "Main"})

            if not misc_info_section:
                raise

            # print("misc_info_section =", misc_info_section)
        except Exception:
            raise Exception("Cannot find misc_info_section.")

        try:
            table_entry_sections = misc_info_section.find_all("tr")

            if not table_entry_sections:
                raise

            # print("table_entry_sections =", table_entry_sections)
            print("table_entry_sections[] =", len(table_entry_sections))
        except Exception:
            raise Exception("Cannot find table_entry_sections.")

        for table_entry_section in table_entry_sections:
            # print("table_entry_section =", table_entry_section.get_text())

            try:
                key_value_sections = table_entry_section.find_all("td")
                # print("key_value_sections[] =", len(key_value_sections))

                if key_value_sections:
                    # key_section = key_value_sections[0].find("span")
                    # key = key_section.get_text().strip()
                    key = key_value_sections[0].get_text().strip()
                    if key[-1].isdigit():
                        key = key[:-2]
                    value = None

                    if len(key_value_sections) > 1:
                        value = key_value_sections[1].get_text().strip()
                        if (value == "-") or (value.startswith("N/A")):
                            value = ""

                        print("key, value = {0}, {1}".format(key, value))
                        results[__Constants.SECTION_MARKET_INFO][key] = value
            except Exception:
                raise Exception("Cannot find key_value_sections.")

        print("Get stock data: ok.")
        print("-" * 40)
    except Exception as e:
        print("Get stock data: Exception = {0}".format(e))
        raise e

def inspect_inventory(record):
    '''
    Inspect inventory info page.

    @param record: [Stock name, Exchange, Ticker]
    @return : Dict with return results.
    '''

    results = {}

    if __data_type == 0:
        parse_get_data = parse_get_data_yahoo_stock

        stock_info_name, stock_info_exchange, stock_info_ticker = record
        stock_info_name = stock_info_name.strip()
        stock_info_exchange = stock_info_exchange.strip()
        stock_info_ticker = stock_info_ticker.strip()
        print("stock_info_name =", stock_info_name)
        print("stock_info_exchange =", stock_info_exchange)
        print("stock_info_ticker =", stock_info_ticker)
        inventory_id = stock_info_ticker

        results[stock_info_ticker] = {}
        result = results[stock_info_ticker]

        result[__Constants.RECORD] = {}
        result[__Constants.RECORD][__Constants.COLUMN_NAME] = stock_info_name
        result[__Constants.RECORD][__Constants.COLUMN_EXCHANGE] = stock_info_exchange
        result[__Constants.RECORD][__Constants.COLUMN_TICKER] = stock_info_ticker

        if stock_info_exchange:
            url = __Constants.API_URL.format("{0}.{1}".format(
                stock_info_ticker, stock_info_exchange))
        else:
            url = __Constants.API_URL.format(stock_info_ticker)

    print("url =", url)
    result[__Constants.URL] = url

    time_str = strftime("%Y-%m-%d %H:%M:%S", localtime(time()))
    print("Start time =", time_str)
    result[__Constants.START_TIME] = time_str

    try:
        status_code, parsed_http_response = check_url(url)
        if status_code != HTTPStatus.OK:
            raise Exception("Get '{0}' failed with status code {1}.".format(url,
                                                                            status_code))

        # Parse and get data.
        result[__Constants.INVENTORY_DATA] = {}
        parse_get_data(parsed_http_response,
                       result[__Constants.INVENTORY_DATA])
        print("-" * 40)

        print("Inspect inventory <{0}>: ok.".format(inventory_id))
        result[__Constants.RESULT] = __Constants.RESULT_OK
    except Exception as e:
        print("Inspect inventory <{0}> Exception = {1}".format(
            inventory_id, e))
        result[__Constants.RESULT] = __Constants.RESULT_ERROR
        result[__Constants.ERROR] = repr(e)

    time_str = strftime("%Y-%m-%d %H:%M:%S", localtime(time()))
    print("Stop time =", time_str)
    result[__Constants.STOP_TIME] = time_str

    return results

def process_inventory_list():
    '''
    Get a list of inventory info from a config file.
    Inspect each of them.

    @return: Dict with return results.
    '''

    global __Constants

    if __data_type == 0:
        __Constants = Constants_YahooStock

    results = {}

    print("-" * 100)
    time_str = strftime("%Y-%m-%d %H:%M:%S", localtime(time()))
    print("Start time =", time_str)
    results[__Constants.START_TIME] = time_str

    try:
        results[__Constants.INVENTORIES] = {}

        # Open input file.
        with open(__inventory_info_file_path) as record_file:
            print('record_file =', record_file)
            cin = csv.reader(record_file)
            # Get all records.
            records = [line for line in cin]
            # But not header line.
            records.pop(0)
            print("records =", records)
            results[__Constants.RECORDS_NUMBER] = len(records)
        print("-" * 80)

        # Inspect inventory concurrently.
        with ThreadPoolExecutor(max_workers = __concurrent_max_workers) as executor:
            # Wait for result to return.
            for record, result in zip(records, executor.map(inspect_inventory, records)):
                results[__Constants.INVENTORIES].update(result)

        print("-" * 80)
        print("Process inventory list: ok.")
    except Exception as e:
        print("Process inventory list: Exception = {0}".format(e))

    time_str = strftime("%Y-%m-%d %H:%M:%S", localtime(time()))
    print("Stop time =", time_str)
    results[__Constants.STOP_TIME] = time_str

    print("Results =", results)
    print("-" * 100)

    # If given __result_output_file_path, output to file; otherwise, output to
    # screen.
    if __result_output_file_path:
        try:
            # Open output file.
            with open(__result_output_file_path, "w") as result_file:
                print('result_file =', result_file)
                # Output file as JSON format.
                json.dump(results, result_file, indent = 4, sort_keys = True)
        except Exception as e:
            print("Output process results: Exception = {0}".format(e))
    else:
        # Output screen as JSON format.
        print(json.dumps(results, indent = 4, sort_keys = True))

    print("-" * 100)

    return results

def usage():
    print('''
Check finance data by Requests.

Usage:
-h
-d <DataType> -i <file path> [-o <file path>] [-c <Number>]

Options:
-h : Show help.
-d <DataType> : Finance data type. Compulsory, Value [0: Yahoo Finance stock].
-i <file path> : Environment info file path (CSV). Compulsory.
-o <file path> : Result output file path (JSON). Optional, output to screen by default.
-c <Number> : Concurrent max workers to process records. Optional, Value [1, 10], 5 by default.

Notes:
Inventory info file format sample (With header line):
1. Yahoo stock
Name,Exchange,Ticker
''')

def main(argv):
    '''
    Pass input arguments from command line to method.

    @param argv: A list of arguments
    '''

    global __data_type
    global __inventory_info_file_path
    global __result_output_file_path
    global __concurrent_max_workers

    print("argv =", argv)

    __show_usage = False
    __exit_code = 0
    __error_message = None

    # If no any option.
    if not argv:
        __show_usage = True

    # Parse command line.
    if not __show_usage:
        try:
            opts, args = getopt.getopt(argv, "hd:i:o:c:")
            print("opts =", opts)
            print("args =", args)
        except Exception as e:
            # There would be getopt.GetoptError.
            print("Parse command line: Exception = {0}".format(e))
            __show_usage, __exit_code, __error_message = True, -1, "Wrong command line option."

    # Check and parse each option.
    if not __show_usage:
        try:
            for opt, arg in opts:
                if opt == "-h":
                    __show_usage, __exit_code = True, 0
                elif opt == "-d":
                    __data_type = int(arg)
                elif opt == "-i":
                    __inventory_info_file_path = arg
                elif opt == "-o":
                    __result_output_file_path = arg
                elif opt == "-c":
                    __concurrent_max_workers = int(arg)
                else:
                    __show_usage, __exit_code, __error_message = True, -\
                        2, "Unknown command line option."
        except Exception as e:
            print("Parse command options: Exception = {0}".format(e))
            __show_usage, __exit_code, __error_message = True, -\
                3, "Wrong value for command line option."

    print("show_usage =", __show_usage)
    print("data_type =", __data_type)
    print("inventory_info_file_path =", __inventory_info_file_path)
    print("result_output_file_path", __result_output_file_path)
    print("concurrent_max_workers =", __concurrent_max_workers)

    # Check options are valid.
    if not __show_usage:
        if (__data_type is None) or (__inventory_info_file_path is None):
            __show_usage, __exit_code, __error_message = True, -\
                4, "Missing compulsory command line option."
        elif (__data_type < 0) or (__data_type > 1):
            __show_usage, __exit_code, __error_message = True, -5, "Wrong value for -d."
        elif (__concurrent_max_workers < 1) or (__concurrent_max_workers > 10):
            __show_usage, __exit_code, __error_message = True, -6, "Wrong value for -c."

    if not __show_usage:
        process_inventory_list()
    else:
        print("__exit_code =", __exit_code)
        if __error_message:
            print("__error_message =", __error_message)
        print("")
        usage()
        sys.exit(__exit_code)

if __name__ == '__main__':
    main(sys.argv[1:])


class Solution:
    def balancedString(self, s):
        import collections
        count = collections.Counter(s)
        n = len(s)
        ans = n
        lo, hi = 0, 0
        while hi < n:
            count[s[hi]] -= 1
            while lo < n and count['Q'] <= n // 4 and count['W'] <= n // 4 and count['E'] <= n // 4 and count['R'] <= n // 4:
                ans = min(ans, hi - lo + 1)
                count[s[lo]] += 1
                lo += 1
            hi += 1
        return ans


"""Dados um numero positivo n e um asequencia de n inteiros positivos,
determinar o comprimento de um segmento de comprimento maximo"""

lista = []
while True:
    n = int(input('Digite um numero: '))
    if n < 0: break
    lista.append(n)

print('A sequencia é {}'.format(lista))

con, qt, maior = [0, 1, 1]
while True:
    if con == len(lista) - 2: break
    if lista[con] < lista[con + 1]:
        qt = qt + 1
        if qt > maior:
            maior = qt
    else: qt = 1

    con = con + 1
print('O segmento maximo é ', maior)


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 08 13:28:01 2017

@author: Long
"""
import numpy as np

from SVM_data import dataStructure
import SVM_functions as SVMfunc

dataMat, labelMat = SVMfunc.loadDataSet('testSetRBF.txt') # 获取数据坐标和标签

#C = float(raw_input('Input the value of C: '))
#toler = float(raw_input('Input the value of toler: ')) # 输入容错率
#maxIter = float(raw_input('Input the value of maxIter: ')) # 输入最大迭代次数
dataMat = np.mat(dataMat)
labelMat = np.mat(labelMat).transpose()
C = 200
toler = 0.0001
maxIter = 10000

data = dataStructure(dataMat, labelMat, C, toler,
                     kernelInfo = ('rbf',1.3))

"""以下相当于原代码的smoP函数"""
iteration = 0 # 记录迭代次数
entireSet = True # 为True时外循环遍历所有样本点，否则遍历满足0 < alpha < C的样本点
# 注：满足0 < alpha < C的样本点即支持向量
alphaPairsChanged = 0 # 标记：为0时表示遍历的样本点都满足KKT条件

while((iteration < maxIter) and ((alphaPairsChanged > 0) or (entireSet))):
    """当迭代次数小于设定的最大迭代次数"""
    """且还没有使所有样本点都满足KKT条件时执行while循环迭代"""
    alphaPairsChanged = 0
    if entireSet:
        """外循环遍历所有样本点来选择alpha对中的第一个alpha"""
        for i in range(data.dataShape):
            alphaPairsChanged += SVMfunc.innerL(i, data)
        iteration += 1
    else:
        """外循环遍历所有支持向量"""
        SV_List = np.nonzero((data.alphas.A > 0) * (data.alphas.A < C))[0]
        for i in SV_List:
            alphaPairsChanged += SVMfunc.innerL(i, data)
        iteration += 1

    if entireSet:
        entireSet = False
    elif (alphaPairsChanged == 0):
        entireSet = True
"""以上相当于原代码的smoP函数"""
# 求出对偶问题最优解
alphas = data.alphas
b = float(data.b)

svInd = np.nonzero(alphas.A>0)[0] # 找出非0的alpha的索引，从而得到所需要的支持向量
sVs = dataMat[svInd] # 支持向量的坐标
labelSV = labelMat[svInd] # 支持向量的类标签
print("there are %d Support Vectors" % np.shape(sVs)[0])

import SVM_data as SVMdata
m,n = np.shape(dataMat)
errorCount = 0
for i in range(m):
    kernelEval = SVMdata.kernelTrans(sVs, dataMat[i,:], ('rbf',1.3))
    predict = kernelEval.T * np.multiply(labelSV,alphas[svInd]) + b
    if np.sign(predict) != np.sign(labelMat[i]):
        errorCount += 1

Machine Learning in Action/5_SVM/SMO_KernelSVM.py


# -*- coding: utf-8 -*-
import hashlib

def get_key_from_path_and_site(path, site_id):
    '''
    cache key has to be < 250 chars to avoid memcache.Client.MemcachedKeyLengthError
    The best algoritm is SHA-224 whose output (224 chars) respects this limitations
    '''
    key = '{}_{}'.format(path, site_id)
    key = hashlib.sha224(key.encode('utf-8')).hexdigest()
    return key


from collections import deque
from copy import copy, deepcopy
from datetime import timedelta
import math

from nio.block.base import Block
from nio.properties import TimeDeltaProperty, VersionProperty
from nio.modules.scheduler.job import Job

class Stagger(Block):

    version = VersionProperty("1.0.1")
    period = TimeDeltaProperty(title='Period', default={"seconds": 1})
    min_interval = TimeDeltaProperty(
        title='Minimum Interval',
        advanced=True,
        default={"microseconds": 100000})

    def process_signals(self, signals, input_id=None):
        stagger_period = self._get_stagger_period(len(signals))
        self.logger.debug("{} signals received, notifying every {}".format(
            len(signals), stagger_period))

        # Launch the notification mechanism in a new thread so that it can
        # sleep between notifications
        stagger_data = StaggerData(
            stagger_period,
            math.ceil(self.period() / stagger_period),
            signals,
            self.notify_signals,
            self.logger,
        )
        stagger_data.start_notify()

    def _get_stagger_period(self, num_signals):
        """ Returns the stagger period based on a number of signals """
        return max(self.period() / num_signals, self.min_interval())

class StaggerData(object):

    """ A class containing an interval and a stack of signals to notify """

    def __init__(self, interval, num_groups, signals, notify_signals, logger):
        self.interval = interval
        self.num_groups = num_groups
        try:
            self.signals = deepcopy(signals)
        except:
            self.signals = copy(signals)
        self.notify_signals = notify_signals
        self.logger = logger
        self._build_deque()

    def start_notify(self):
        # Notify the first signals right away
        self._notify()
        # Repeat notify every interval until cancelled
        self._notify_job = Job(
            self._notify,
            timedelta(seconds=self.interval.total_seconds()),
            True)

    def _notify(self):
        if not self.signals_deque:
            self._notify_job.cancel()
        else:
            sigs_out = self.signals_deque.popleft()
            self.logger.debug("Notifying {} signals".format(len(sigs_out)))
            self.notify_signals(sigs_out)

    def _build_deque(self):
        """ Build the stack of signals based on number of groups we want """
        self.signals_deque = deque()
        signals_included = 0
        signals_per_group = len(self.signals) / self.num_groups
        for group_num in range(self.num_groups):

            # How many we should have after this iteration
            total_expected = (group_num + 1) * signals_per_group

            # Take what we expect to have and subtract the number of signals
            # we already have to determine how many to add for this iteration.
            # Round the number to space out uneven intervals
            signals_this_time = round(total_expected - signals_included)

            # Make sure to account for the signals we just added
            signals_included += signals_this_time

            # Build a list of signals for this interval and push it on to the
            # stack - pop the signals off the original list
            signals_to_include = list()
            for sig in range(signals_this_time):
                signals_to_include.append(self.signals.pop(0))
            self.signals_deque.append(signals_to_include)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ecnet/utils/server_utils.py
# v.3.0.1
# Developed in 2019 by Travis Kessler <travis.j.kessler@gmail.com>
#
# Contains functions used by ecnet.Server
#

# Stdlib imports
from os import path
from pickle import dump as pdump, load as pload

# 3rd party imports
from numpy import asarray
from yaml import dump, load

# ECNet imports
from ecnet.utils.error_utils import calc_rmse, calc_mean_abs_error,\
    calc_med_abs_error, calc_r2
from ecnet.models.mlp import MultilayerPerceptron

def default_config():
    '''Returns default NN architecture/learning parameters'''

    return {
        'epochs': 10000,
        'learning_rate': 0.001,
        'beta_1': 0.9,
        'beta_2': 0.999,
        'epsilon': 0.0000001,
        'decay': 0.0,
        'hidden_layers': [
            [32, 'relu'],
            [32, 'relu']
        ],
        'output_activation': 'linear'
    }

def get_candidate_path(prj, pool, candidate=None, model=False, p_best=False):
    '''Get path to various states of model.h5 files

    Args:
        prj (str): project name
        pool (int): pool number
        candidate (int): candidate number
        model (bool): if True, appends `model.h5` to path
        p_best (bool): if True, removes candidate folder from path

    Returns:
        str: path
    '''

    if model:
        return path.join(
            prj,
            'pool_{}'.format(pool),
            'candidate_{}'.format(candidate),
            'model.h5'
        )
    elif p_best:
        return path.join(
            prj,
            'pool_{}'.format(pool),
            'model.h5'
        )
    else:
        return path.join(
            prj,
            'pool_{}'.format(pool),
            'candidate_{}'.format(candidate)
        )

def get_error(y_hat, y, fn):
    '''Obtains specified error for supplied results/targets

    Args:
        y_hat (numpy.array): predicted data
        y (numpy.array): target data
        fn (str): error function; `rmse`, `mean_abs_error`, `med_abs_error`,
            `r2`

    Returns:
        float: error
    '''

    if fn == 'rmse':
        return calc_rmse(y_hat, y)
    elif fn == 'r2':
        return calc_r2(y_hat, y)
    elif fn == 'mean_abs_error':
        return calc_mean_abs_error(y_hat, y)
    elif fn == 'med_abs_error':
        return calc_med_abs_error(y_hat, y)
    else:
        raise ValueError('Unknown error function: {}'.format(fn))

def get_x(sets, dset):
    '''Obtains input values for specified data set

    Args:
        sets (ecnet.utils.data_utils.PackagedData): all sets
        dset (str): `learn`, `valid`, `train`, `test`, None (all sets)

    Returns:
        numpy.array: input data for specified set
    '''

    if dset == 'test':
        return sets.test_x
    elif dset == 'valid':
        return sets.valid_x
    elif dset == 'learn':
        return sets.learn_x
    elif dset == 'train':
        x_vals = [list(val) for val in sets.learn_x]
        x_vals.extend([list(val) for val in sets.valid_x])
        return asarray(x_vals)
    elif dset is None:
        x_vals = [list(val) for val in sets.learn_x]
        x_vals.extend([list(val) for val in sets.valid_x])
        x_vals.extend([list(val) for val in sets.test_x])
        return asarray(x_vals)
    else:
        raise ValueError('Unknown dset argument: {}'.format(dset))

def get_y(sets, dset):
    '''Obtains target values for specified data set

    Args:
        sets (ecnet.utils.data_utils.PackagedData): all sets
        dset (str): `learn`, `valid`, `train`, `test`, None (all sets)

    Returns:
        numpy.array: target data for specified set
    '''

    if dset == 'test':
        return sets.test_y
    elif dset == 'valid':
        return sets.valid_y
    elif dset == 'learn':
        return sets.learn_y
    elif dset == 'train':
        y_vals = [val for val in sets.learn_y]
        y_vals.extend([val for val in sets.valid_y])
        return y_vals
    elif dset is None:
        y_vals = [val for val in sets.learn_y]
        y_vals.extend([val for val in sets.valid_y])
        y_vals.extend([val for val in sets.test_y])
        return y_vals
    else:
        raise ValueError('Unknown dset argument: {}'.format(dset))

def open_config(filename):
    '''Returns contents of YML model configuration file

    Args:
        filename (str): path to YML configuration file

    Returns:
        dict: variable names and values
    '''

    with open(filename, 'r') as cf_file:
        return load(cf_file)

def open_df(filename):
    '''Opens pickled DataFrame object

    Args:
        filename (str): path to pickled file

    Returns:
        DataFrame: opened DataFrame
    '''

    with open(filename, 'rb') as data_file:
        return pload(data_file)

def resave_df(old, new):
    '''Resaves picked DataFrame to new location

    Args:
        old (str): path to existing file
        new (str): path to new file
    '''

    save_df(open_df(old), new)

def resave_model(old, new):
    '''Resaves .h5 model file

    Args:
        old (str): path to existing file
        new (str): path to new file
    '''

    model = MultilayerPerceptron(filename=old)
    model.load()
    model.save(new)

def train_model(sets, vars, eval_set, eval_fn, retrain=False,
                filename='model.h5', validate=True, save=True):
    '''Trains neural network

    Args:
        sets (ecnet.utils.data_utils.PackagedData): data sets
        vars (dict): Server._vars variables
        eval_set (str): set used to evaluate the model; `learn`, `valid`,
            `train`, `test`, None (all sets)
        eval_fn (str): error function to evaluate the model; `rmse`,
            `mean_abs_error`, `med_abs_error`
        retrain (bool): if True, opens model for additional training
        filename (str): path to .h5 model file, loads from this if retraining
        save (bool): if True, saves model to `filename`

    Returns:
        float: error of evaluated set
    '''

    model = MultilayerPerceptron(filename=filename)
    if retrain:
        model.load()
    else:
        for idx, layer in enumerate(vars['hidden_layers']):
            if idx == 0:
                model.add_layer(layer[0], layer[1], len(sets.learn_x[0]))
            else:
                model.add_layer(layer[0], layer[1])
        model.add_layer(len(sets.learn_y[0]), vars['output_activation'])
    if validate:
        model.fit(
            sets.learn_x,
            sets.learn_y,
            sets.valid_x,
            sets.valid_y,
            epochs=vars['epochs'],
            lr=vars['learning_rate'],
            beta_1=vars['beta_1'],
            beta_2=vars['beta_2'],
            epsilon=vars['epsilon'],
            decay=vars['decay']
        )
    else:
        model.fit(
            sets.learn_x,
            sets.learn_y,
            epochs=vars['epochs'],
            lr=vars['learning_rate'],
            beta_1=vars['beta_1'],
            beta_2=vars['beta_2'],
            epsilon=vars['epsilon'],
            decay=vars['decay']
        )
    if save:
        model.save()
    return get_error(
        model.use(get_x(sets, eval_set)),
        get_y(sets, eval_set),
        eval_fn
    )

def save_config(vars, filename):
    '''Saves model architecture/learning variables to YML file

    Args:
        filename (str): path to save location
    '''

    with open(filename, 'w') as cf_file:
        dump(vars, cf_file, default_flow_style=False, explicit_start=True)
    cf_file.close()

def save_df(df, filename):
    '''Saves DataFrame to pickled file

    Args:
        filename (str): path to save location
    '''

    with open(filename, 'wb') as data_file:
        pdump(df, data_file)
    data_file.close()

def use_model(sets, dset, filename='model.h5'):
    '''Uses existing model to predict data

    Args:
        sets (ecnet.utils.data_utils.PackagedData): data sets
        dset (str): set to predict for; `learn`, `valid`, `train`, `test`,
            None (all sets)
        filename (str): path to existing .h5 model file

    Returns:
        numpy.array: predicted data
    '''

    model = MultilayerPerceptron(filename=filename)
    model.load()
    return model.use(get_x(sets, dset))


from API.Utility.Util import Util
from API.Utility import UtilConst
from API.MenuBar.Options.Options import Options
from API.MenuBar.Options.OptionsConst import OptionsConst
from API.MenuBar.Options.Preferences.Miscellaneous.MiscellaneousConst import MiscellaneousConst
from API.SimulationPanel.EventList.EventListConst import EventListConst
from API.SimulationPanel.EventListFilters.EventListFilters import EventListFilters
from API.SimulationPanel.PlayControls.PlayControlsConst import PlayControlsConst
from API.MenuBar.Options.Preferences.PreferencesConst import PreferencesConst

util = Util()
options = Options()
eventListFilters = EventListFilters()

def main():
    util.init()
    util.open("UI13.pkt", UtilConst.UI_TEST )
    util.speedUpConvergence()
    editOptionsSetting()
    checkpoint1()
    resetOptionsSetting()

def editOptionsSetting():
    options.selectOptionsItem(OptionsConst.PREFERENCES)
    util.clickTab(PreferencesConst.TAB_BAR, PreferencesConst.MISCELLANEOUS)
    util.clickButton(MiscellaneousConst.AUTO_VIEW_PREVIOUS_EVENTS)
    util.close(OptionsConst.OPTIONS_DIALOG)

def checkpoint1():
    util.clickOnSimulation()
    util.clickButton(EventListConst.RESET_SIMULATION)
    for i in range(0, 8):
        util.clickButton(PlayControlsConst.CAPTURE_FORWARD)
    snooze(10)
    if (object.exists(PlayControlsConst.BUFFER_FULL_DIALOG_LABEL)):
        test.fail("Buffer window found")
    else:
        test.passes("Buffer window not found")

def resetOptionsSetting():
    options.selectOptionsItem(OptionsConst.PREFERENCES)
    util.clickTab(PreferencesConst.TAB_BAR, PreferencesConst.MISCELLANEOUS)
    util.clickButton(MiscellaneousConst.PROMPT)
    util.close(OptionsConst.OPTIONS_DIALOG)


class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if len(grid) == 0 or len(grid[0]) == 0:
            return 0
        toX = len(grid[0]) - 1
        toY = len(grid) - 1
        cache = [[None for j in range(toX + 1)] for i in range(toY + 1)]

        return self.dfs(grid, 0, 0, toX, toY, cache)

    def dfs(self, grid, x, y, toX, toY, cache):
        if x > toX or y > toY:
            return 65536
        if x == toX and y == toY:
            return grid[toY][toX]

        ans = cache[y][x]
        if ans is not None:
            return ans
        minp = min(self.dfs(grid, x + 1, y, toX, toY, cache), self.dfs(grid, x, y + 1, toX, toY, cache))
        ans = grid[y][x] + minp
        cache[y][x] = ans

        return ans


from argparse import ArgumentParser
from web import start_api_server
from shell import start_shell

parser = ArgumentParser()
parser.add_argument('mode', help='which mode to start', choices=['web', 'shell'])

if __name__ == '__main__':
    args = parser.parse_args()

    dict(
        web=start_api_server,
        shell=start_shell
    ).get(args.mode)()


import argparse
import sys
import math
parser = argparse.ArgumentParser()
parser.add_argument("--type", type=str, help='It indicates the type of payment: "annuity" or "diff"')
parser.add_argument("--periods", type=float, help="takes the count of months")
parser.add_argument("--principal", type=float, help="takes the amount borrowed or amount to be borrowed")
parser.add_argument("--interest", type=float, help="takes the interest charged without the percentage symbol")
parser.add_argument("--payment", type=float, help="takes the monthly payment amount")

args = parser.parse_args()
types = args.type
periods = args.periods
interest = args.interest
month_pay = args.payment
principal = args.principal

if types not in ["annuity", "diff"]:
    print("Incorrect parameters")
    sys.exit()
elif len(sys.argv) != 5:
    print("Incorrect parameters")
    sys.exit()
elif types is None:
    print("Incorrect parameters")
    sys.exit
elif periods is not None and periods != abs(periods):
    print("Incorrect parameters")
    sys.exit()
elif month_pay is not None and month_pay != abs(month_pay):
    print("Incorrect parameters")
    sys.exit()
elif principal is not None and principal != abs(principal):
    print("Incorrect parameters")
    sys.exit()
elif interest is not None and interest != abs(interest):
    print("Incorrect parameters")
    sys.exit()
elif types == "diff" and month_pay is not None:
    if principal is None or periods is None:
        print("Incorrect parameters")
        sys.exit()
elif types == "diff" and month_pay is None:
    i = interest / (12 * 100)
    m = 0
    total = 0
    while m < periods:
        m += 1
        diff_pay = (principal / periods) + (i * (principal - ((principal * (m - 1)) / periods)))
        total += math.ceil(diff_pay)
        print(f"Month {m}: paid out {math.ceil(diff_pay)}")
    print(f"\nOverpayment = {int(total - principal)}")
    sys.exit()
elif types == "annuity" and month_pay is None:
    i = interest / (12 * 100)
    month_pay = principal * ((i * ((1 + i)**periods)) / (((1 + i)**periods) - 1))
    anuit_pay = math.ceil(month_pay)
    overpayment = (anuit_pay * periods) - principal
    print(f"Your annuity payment = {anuit_pay}!")
    print(f"Overpayment = {int(overpayment)}")
    sys.exit()
elif types == "annuity" and principal is None:
    i = interest / (12 * 100)
    principal = month_pay / ((i * ((1 + i)**periods)) / (((1 + i)**periods) - 1))
    p = math.floor(principal)
    over_p = (month_pay * periods) - p
    print(f"Your credit principal = {p}!")
    print(f"Overpayment = {int(over_p)}")
    sys.exit()
elif types == "annuity" and periods is None:
    i = interest / (12 * 100)
    periods = (math.log(month_pay / (month_pay - (i * principal)))) / (math.log(1 + i))
    year = math.ceil(periods / 12)
    over_p = (year * 12 * month_pay) - principal
    if year > 1:
        print(f"You need {year} years to repay this credit!")
    elif year == 1 or year == 0:
        print(f"You need {year} year to repay this credit!")
    print(f"Overpayment = {int(over_p)}")
    sys.exit()
else:
    print("Incorrect parameters")
    sys.exit()


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#  Filename      greatest.py
#  Author        Kristopher Chambers
#  Updated       2016-02-01
#  Status        Complete
#
#  Description   A solution to the Greatest Element problem
#                from RosettaCode.org, which can be found online at:
#
#                 http://rosettacode.org/wiki/Greatest_element_of_a_list
#
#                Python already has the max() function to do this,
#                but using the in-built function would be defeat
#                the purpose of the exercise.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#  A synopsis of the original challenge is below:
#
#  Create a function that returns the maximum value in a provided set of
#  values, where the number of values may not be known until runtime.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
"""Solve the Greatest Element problem."""

def greatest_element(lst: list) -> any:
    """Return the greatest element in a list."""
    greatest = lst[0]
    for el in lst[1:]:
        if el > greatest:
            greatest = el
    return greatest

def run_test():
    """Find the greatest element in a list of ten elements."""
    print("The greatest element in a list of [1..10] is",
          greatest_element(range(1, 11)))

if __name__ == '__main__':
    run_test()


import requests, json, re, xlsxwriter
import traceback
class yilong():
    def yilong_comment(info, city_name):
        count = 0
        for hotel_id, hotel_name, review_count in info:  # 对于每一家酒店
            count += 1
            print('now I am get the ' + str(count) + ' th page,the hotel\'s name is ' + str(hotel_name))
            # print(hotel_id)
            # print(hotel_name)
            # print(review_count)
            username = []
            content = []
            scoretotal = []
            commentcount = []  # 返回的json中写的
            userrank = []  # 用户等级
            time = []  # "commentDateTime":"2017-07-07 用户评论时间
            travelType = []  # 旅行方式
            roomTypeName = []
            for page_index in range(1, int(int(review_count) / 100) + 2, 1):
                url = 'http://m.elong.com/hotel/api/morereviewnew?hotelid=' + str(
                    hotel_id) + '&commenttype=1&pagesize=100&pageindex=' + str(page_index) + '&mainId=0&esdnum=9196917'

                headers = {
                    'Accept': '*/*',
                    'Accept-Encoding': 'gzip, deflate, sdch',
                    'Accept-Language': 'zh-CN,zh;q=0.8',
                    'Connection': 'keep-alive',
                    'Cookie': 'CookieGuid=1a8880aa-4f0d-4b0f-8c97-e52d2d178573; SessionGuid=00c6ddec-64ee-4776-9a14-6057a49270a8; Esid=8e852714-7af7-48ff-94e4-36d28f924d11; \
                    s_eVar44=sgbrandzone; CitySearchHistory=0101%23%E5%8C%97%E4%BA%AC%E5%B8%82%23beijing%23; com.eLong.CommonService.OrderFromCookieInfo=Status=1&Orderfromtype=\
                    5&Isusefparam=0&Pkid=51023&Parentid=4600&Coefficient=0.0&Makecomefrom=0&Cookiesdays=0&Savecookies=0&Priority=9000; ShHotel=CityID=0101&CityNameCN=%E5%8C%97%E4\
                    %BA%AC%E5%B8%82&CityName=%E5%8C%97%E4%BA%AC%E5%B8%82&OutDate=2017-07-09&CityNameEN=beijing&InDate=2017-07-08; s_cc=true; SHBrowseHotel=cn=50101077%2C%2C%2C%2C%\
                    2C%2C%3B90914560%2C%2C%2C%2C%2C%2C%3B&; s_visit=1; s_sq=elongcom%3D%2526pid%253Dhotel.elong.com%25252F50101077%25252F%2526pidt%253D1%2526oid%253Djavascript%252\
                    53Avoid(0)%2526ot%253DA; H5CookieId=8a00bc2f-380c-4463-88ba-4e45e837e7c3; businessLine=hotel; H5Channel=mnoreferseo%2CSEO; H5SessionId=2581D347C4A64C3EDC07969968\
                    C74FFB; cityid=0101; route=7fbf8090634af19a2246b39009778d36; indate=2017-07-07; outdate=2017-07-08; H5ff4detail=""; _fid=j4tefvsr-355b-483f-affb-7db312781cd9; pag\
                    e_time=1499401737010%2C1499401742149%2C1499401757112%2C1499401847772%2C1499401928546%2C1499402089717%2C1499402354577%2C1499402364922%2C1499404022346; _RF1=171.113.\
                    115.200; _RSG=FB4MGt0eO8BlNwlmg1V0KA; _RGUID=b218a00d-d058-42cf-a500-7c1ab0bfa5ef',
                    'Host': 'm.elong.com',
                    'Referer': 'http://m.elong.com/hotel/50101077/review/good.html',
                    'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36',
                    'X-Requested-With': 'XMLHttpRequest'
                }
                try:
                    page = requests.get(url=url, headers=headers, timeout=15)
                    #json_ = json.loads(page.text)
                    json_ = page.json()
                    commentcount = json_["commentCount"]
                    json_ =json_["comments"]
                    username = json_['userName']
                    content=json_['content']
                    scoretotal = json_['commentScoreTotal']

                    userrank = json_['userRank']
                    time = json_['commentDateTime']
                    travelType = json_['travelType']
                    roomTypeName = json_['roomTypeName']
                except:
                    print(traceback.format_exc())
                    page = requests.get(url=url, headers=headers,timeout = 15)
                    try:
                        json_ = json.loads(page.text)["comments"]
                        for data in json_:
                            username.append(data.get('userName'))
                            content.append(data.get('content'))
                            scoretotal.append(data.get('commentScoreTotal'))
                            commentcount.append(data.get('commentCount'))
                            userrank.append(data.get('userRank'))
                            time.append(data.get('commentDateTime'))
                            travelType.append(data.get('travelType'))
                            roomTypeName.append(data.get('roomTypeName'))
                    except:
                        print(traceback.format_exc())
                        print('出错，出错URL为：' + str(url))
                        # 建议使用xlwrite
                        """wbk = xlwt.Workbook()  # 将结果写入到EXCEL
            sheet1 = wbk.add_sheet('content', cell_overwrite_ok=True)
            title = ['用户名', '评论', '评分', '总数', '用户等级', '评论时间', '旅行方式']
            for i in range(0,7):
                sheet1.write(0,i,title[i])
            for index in range(2, len(username)+2):
                try:
                    sheet1.write(index, 0, username[index+1])  # write（行,列,写入的内容）
                except:
                    sheet1.write(index, 0, "")
                try:
                    sheet1.write(index, 1, content[index+1])
                except:
                    sheet1.write(index, 1, "")
                try:
                    sheet1.write(index, 2, scoretotal[index+1])
                except:
                    sheet1.write(index, 2, "")
                try:
                    sheet1.write(index, 3, commentcount[index+1])
                except:
                    sheet1.write(index, 3, "")
                try:
                    sheet1.write(index, 4, userrank[index+1])
                except:
                    sheet1.write(index, 4, "")
                try:
                    sheet1.write(index, 5, time[index+1])
                except:
                    sheet1.write(index, 5, "")
                try:
                    sheet1.write(index, 6, travelType[index+1])
                except:
                    sheet1.write(index, 6, "")
                try:
                    sheet1.write(index, 7, roomTypeName[index+1])
                except:
                    sheet1.write(index, 7, "")"""
            try:
                save_path = 'D:\\' + str(city_name) + '9\\' + str(hotel_name) + '_' + str(hotel_id) + '_' + str(len(username)) + '&' + str(review_count) + '.xlsx'
                comm_wk = xlsxwriter.Workbook(save_path)
                print('writing to excle...')
                sheet1 = comm_wk.add_worksheet('评论')
                title = ['用户名', '评论', '评分', '总数', '用户等级', '评论时间', '旅行方式', '房屋类型']
                sheet1.write_row('A1', title)
                row = 1
                sheet1.write_column(row, 0, username)
                sheet1.write_column(row, 1, content)
                sheet1.write_column(row, 2, scoretotal)
                sheet1.write_column(row, 3, commentcount)
                sheet1.write_column(row, 4, userrank)
                sheet1.write_column(row, 5, time)
                sheet1.write_column(row, 6, travelType)
                sheet1.write_column(row, 7, roomTypeName)
                comm_wk.close()
                print('writing to excle Done!')
            except:
                print(traceback.format_exc())
            """try:
                save_path = 'D:\\'+str(city_name)+'\\' + str(hotel_name)+'_'+str(hotel_id)+'_'+str(len(username))+'&'+str(review_count) + '.xls'
                wbk.save(save_path)"""

    def get_hotel(citys):
        # 获取酒店总数

        for city_name, city_id in citys:
            url = 'http://hotel.elong.com/' + str(city_name)
            page = requests.get(url)
            mod = r'id="hotelCount">(.*?)</span>家酒店满足条件'
            hotel_count = re.compile(mod).findall(page.text)
            print(hotel_count[0])

        hotel_page_headers = {
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, sdch',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Connection': 'keep-alive',
            'Cookie': 'CookieGuid=1a8880aa-4f0d-4b0f-8c97-e52d2d178573; SessionGuid=00c6ddec-64ee-4776-9a14-6057a49270a8; Esid=8e852714-7af7-48ff-94e4-36d28f924d11; \
                s_eVar44=sgbrandzone; CitySearchHistory=0101%23%E5%8C%97%E4%BA%AC%E5%B8%82%23beijing%23; com.eLong.CommonService.OrderFromCookieInfo=Status=1&Orderfromty\
                pe=5&Isusefparam=0&Pkid=51023&Parentid=4600&Coefficient=0.0&Makecomefrom=0&Cookiesdays=0&Savecookies=0&Priority=9000; ShHotel=CityID=0101&CityNameCN=%E5%\
                8C%97%E4%BA%AC%E5%B8%82&CityName=%E5%8C%97%E4%BA%AC%E5%B8%82&OutDate=2017-07-09&CityNameEN=beijing&InDate=2017-07-08; s_cc=true; SHBrowseHotel=cn=5010107\
                7%2C%2C%2C%2C%2C%2C%3B90914560%2C%2C%2C%2C%2C%2C%3B&; s_visit=1; s_sq=elongcom%3D%2526pid%253Dhotel.elong.com%25252F50101077%25252F%2526pidt%253D1%2526oi\
                d%253Djavascript%25253Avoid(0)%2526ot%253DA; H5CookieId=8a00bc2f-380c-4463-88ba-4e45e837e7c3; businessLine=hotel; H5Channel=mnoreferseo%2CSEO; H5SessionId\
                =2581D347C4A64C3EDC07969968C74FFB; route=7fbf8090634af19a2246b39009778d36; _fid=j4tefvsr-355b-483f-affb-7db312781cd9; page_time=1499401737010%2C149940174214\
                9%2C1499401757112%2C1499401847772%2C1499401928546%2C1499402089717%2C1499402354577%2C1499402364922%2C1499404022346%2C1499404467066; _RF1=171.113.115.200; _R\
                SG=FB4MGt0eO8BlNwlmg1V0KA; _RGUID=b218a00d-d058-42cf-a500-7c1ab0bfa5ef; indate=2017-07-07; outdate=2017-07-08; cityid=0101; H5ff4list=""',
            'Host': 'm.elong.com',
            'Referer': 'http://m.elong.com/hotel/0101/nlist/',
            'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }
        info = []
        hotel_count = ['0']
        for city_name, city_id in citys:  # 每一个城市的
            page_count = 0
            for index in range(0, int(int(hotel_count[0]) / 20) + 2, 1):  # 每一家酒店  爬取各个酒店的id、name 和评论数
                hotel_page_url = 'http://m.elong.com/hotel/api/list?city=' + str(city_id) + '&pageindex=' + str(index) + '&indate=2017-07-08&outdate=2017-07-09'
                hotel_page = requests.get(url=hotel_page_url, headers=hotel_page_headers)
                page_count += 1
                print('正在爬取' + str(city_name) + '第' + str(page_count) + '页的酒店信息')
                hotel_datas = json.loads(hotel_page.text)['hotelList']
                try:
                    for hotel_data in hotel_datas:
                        mode = r'hotel/(.*?)/#'
                        hotel_url = hotel_data.get('detailPageUrl')
                        _id = re.compile(mode).findall(hotel_url)
                        count = hotel_data.get('totalCommentCount')
                        name = hotel_data.get('hotelName')
                        t = (_id[0], name, count)
                        info.append(t)
                except:
                    pass
                    print('出错：' + hotel_page_url)
        print(info)
        print(len(info))
        return info

    citys = [[('beijing', '0101')], [('shanghai', '0201')], [('guangzhou', '2001')], [('shenzhen', '2003')]]
    for city in citys:
        info = get_hotel(city)
        yilong_comment(info, city_name=city[0][0])

a = yilong()


from django.core.exceptions import ObjectDoesNotExist
from apitest.runrecord.models import *

def get_runrecord_info(run_number):
    try:
        runRecord = RunRecord.objects.get(run_number=run_number)
    except ObjectDoesNotExist:
        return None

    runrecord_info = runRecord.to_json()
    result_list = runRecord.get_all_result()
    return runrecord_info,result_list

# 运行结果详情
def get_result_info(result_id):
    try:
        rs = Result.objects.get(id=result_id)
    except ObjectDoesNotExist:
        return None
    return rs.to_json()

# 删除运行记录
def runrecord_del(run_number):
    rr = RunRecord.objects.filter(run_number=run_number)
    rs = Result.objects.filter(run_number=run_number)
    rs.delete()
    rr.delete()


def total_amount(amount, percentage, duration):
    total = amount
    if amount is None or percentage is None or duration is None:
        return None
    try:
        for _ in range(duration):
            interest = total * (percentage / 100)
            total = total + interest
    except TypeError:
        return None
    return total