import os, sys
directory = os.path.dirname(os.path.realpath('_file_'))
sys.path.append(os.path.join(directory, "code", "helpers"))

from overlap import add_water
from make_houses import maak_woningen, add_shortest_distances

sys.path.append(os.path.join(directory, "code", "algoritmes"))
from hillclimber import run_hillclimber
from simulated_annealing import run_simulated_annealing

number_of_houses = 20
AANTAL_EENSGEZIN = 0.6 * number_of_houses
AANTAL_BUNGALOW = 0.25 * number_of_houses
AANTAL_MAISON = 0.15 * number_of_houses

def random_algorithm(iterations, climbing_iterations, type):
    """Makes random map and runs hillclimber or simmulated annealing a number of times"""
    waters = []
    woningen = []
    Text = "Score, Run\n"
    text_name = "resultaten/output_" + type + ".txt"
    text_file = open(text_name, "w")
    text_file.write(Text)
    for i in range(iterations):
        print("Water wordt geplaatst")
        waters = add_water(woningen)
        print("Huizen worden geplaatst")
        woningen = maak_woningen(number_of_houses, AANTAL_MAISON, AANTAL_BUNGALOW, waters, woningen)
        woningen = add_shortest_distances(woningen, waters)
        while woningen == False:
            woningen = maak_woningen(number_of_houses, AANTAL_MAISON, AANTAL_BUNGALOW, waters, woningen)
            woningen = add_shortest_distances(woningen, waters)
        if waters != False:
            print("Run, hillclimber, run!")
            if type == "hillclimber":
                run_hillclimber(climbing_iterations, text_file, i, woningen, waters)
            if type == "simann":
                run_simulated_annealing(climbing_iterations, text_file, i, woningen, waters)
        print("Het random algoritme in combinatie met het hillclimber algoritme is " + str(i) + " gerund")
    text_file.close()


from PyQt5 import QtWidgets

from src.ui.commons.layout import set_lvbox, set_hbox
from src.ui.systemSettingsWindow.widgetsPath import WidgetsPath
from src.business.configuration.configSystem import ConfigSystem
from src.business.consoleThreadOutput import ConsoleThreadOutput

class SystemSettingsWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(SystemSettingsWindow, self).__init__(parent)
        self.s = parent
        self.cs = ConfigSystem()
        self.console = ConsoleThreadOutput()

        # Creating Widgets
        self.wp = WidgetsPath(self)
        self.button_ok = QtWidgets.QPushButton('Salvar', self)
        self.button_cancel = QtWidgets.QPushButton('Cancelar', self)

        # Setting Up
        self.button_settings()
        self.setting_up()

        self.filling_fields()

    def button_settings(self):
        self.button_cancel.clicked.connect(self.s.close)
        self.button_ok.clicked.connect(self.ok_button)

    def ok_button(self):
        try:
            self.saving_settings()
            self.console.raise_text("Configurações Salvas com sucesso!", 1)
        except:
            self.console.raise_text("Não foi possível salvar as configurações do sistema.", 1)
        finally:
            self.s.close()

    def setting_up(self):
        self.setLayout(set_lvbox(set_hbox(self.wp),
                                 set_hbox(self.button_ok, self.button_cancel, stretch2=1)))

    def saving_settings(self):
        info = self.wp.get_values()
        self.cs.set_site_settings(info[0], info[1], info[2], info[3], info[4])
        self.cs.save_settings()

    def filling_fields(self):
        info = self.cs.get_site_settings()
        self.wp.set_values(info[0], info[1], info[2], info[3], info[4])


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def load_data():
    titanic_data = pd.read_csv("titanic.csv")
    return titanic_data

def draw_plot(titanic):
    male_mask = titanic_data['Sex'] == 'male'
    males = titanic_data[male_mask]['Sex'].value_counts()
    females = titanic_data[~male_mask]['Sex'].value_counts()
    proportions = [males,females]
    plt.pie(proportions, labels = ['Males','Females'], colors = ['blue','red'], startangle=90, autopct='%1.1f%%', explode = (0.1, 0))
    plt.axis('equal')
    plt.title('Sex Proportion')
    plt.show()

def draw_historgram(titanic):
    fares = sorted(titanic['Fare'])
    plt.hist(fares, bins = 100, range=(0,600))
    plt.title("Fare Payed Histogram")
    plt.xlabel("Fare")
    plt.ylabel("Frequency")
    plt.show()

titanic = load_data()
draw_historgram(titanic)


"""
    for 变量名 in 列表/元组/字典:
        <循环体>
    备注：变量的值是一直在变化的；
"""
list = ['a','b','c','d']
for k in list:
    print(k)
#输出结果为：a b c d
for i in range(10):
    print(i) #输出0-9


import cloudkeeper.logging
import threading
from datetime import datetime, timezone
from cloudkeeper.graph import Graph
from cloudkeeper.baseplugin import BasePlugin
from cloudkeeper.baseresources import BaseCloud, BaseAccount, BaseRegion
from cloudkeeper_plugin_aws.resources import (
    AWSALBTargetGroup,
    AWSEC2KeyPair,
    AWSEC2NetworkAcl,
    AWSVPC,
)
from cloudkeeper.paralleltagger import ParallelTagger
from cloudkeeper.args import ArgumentParser
from cloudkeeper.event import (
    Event,
    EventType,
    add_event_listener,
    remove_event_listener,
)
from prometheus_client import Summary, Counter

log = cloudkeeper.logging.getLogger("cloudkeeper." + __name__)

metrics_ctime_tags = Counter(
    "cloudkeeper_plugin_tag_aws_ctime_tags_total",
    "Tag AWS ctime Plugin Number of ctime tags applied",
    ["cloud", "account", "region"],
)
metrics_tag_ctime = Summary(
    "cloudkeeper_plugin_tag_aws_ctime_tag_ctime_seconds",
    "Tag AWS ctime Plugin Time it took the tag_ctime() method",
)

class TagAWSCtimePlugin(BasePlugin):
    def __init__(self):
        super().__init__()
        self.name = "tag_aws_ctime"
        self.exit = threading.Event()
        self.run_lock = threading.Lock()
        if ArgumentParser.args.tag_aws_ctime:
            log.debug("AWS ctime Tagger plugin initializing")
            add_event_listener(EventType.SHUTDOWN, self.shutdown)
            add_event_listener(
                EventType.COLLECT_FINISH,
                self.aws_ctime_tagger,
                blocking=False,
                timeout=900,
            )
        else:
            self.exit.set()

    def __del__(self):
        remove_event_listener(EventType.COLLECT_FINISH, self.aws_ctime_tagger)
        remove_event_listener(EventType.SHUTDOWN, self.shutdown)

    def go(self):
        self.exit.wait()

    def aws_ctime_tagger(self, event: Event):
        if not self.run_lock.acquire(blocking=False):
            log.error("AWS ctime Tagger is already running")
            return

        graph = event.data
        log.info("AWS ctime Tagger called")
        try:
            self.tag_ctime(graph)
        except Exception:
            raise
        finally:
            self.run_lock.release()

    @metrics_tag_ctime.time()
    def tag_ctime(self, graph: Graph):
        now = datetime.utcnow().replace(tzinfo=timezone.utc).isoformat()
        pt = ParallelTagger(self.name)
        with graph.lock.read_access:
            for node in graph.nodes:
                if not isinstance(
                    node, (AWSALBTargetGroup, AWSEC2NetworkAcl, AWSEC2KeyPair, AWSVPC)
                ):
                    continue

                if "cloudkeeper:ctime" not in node.tags:
                    cloud = node.cloud(graph)
                    account = node.account(graph)
                    region = node.region(graph)
                    if (
                        not isinstance(cloud, BaseCloud)
                        or not isinstance(account, BaseAccount)
                        or not isinstance(region, BaseRegion)
                    ):
                        log.error(
                            (
                                f"Resource {node.resource_type} {node.dname} has no valid "
                                "cloud, account or region associated with it"
                            )
                        )
                        continue

                    log.debug(
                        (
                            f"Resource {node.resource_type} {node.dname} in cloud {cloud.name} account "
                            f"{account.dname} region {region.name} has no cloudkeeper:ctime tag "
                            f"- setting it because ctime is not available via the AWS API"
                        )
                    )
                    pt_key = f"{cloud.id}-{account.id}-{region.id}"
                    pt.add(node, "cloudkeeper:ctime", now, pt_key)
                    metrics_ctime_tags.labels(
                        cloud=cloud.name, account=account.dname, region=region.name
                    ).inc()
        pt.run()

    @staticmethod
    def add_args(arg_parser: ArgumentParser) -> None:
        arg_parser.add_argument(
            "--tag-aws-ctime",
            help="Tag AWS ctime (default: False)",
            dest="tag_aws_ctime",
            action="store_true",
            default=False,
        )

    def shutdown(self, event: Event):
        log.debug(
            f"Received event {event.event_type} - shutting down AWS ctime tagging plugin"
        )
        self.exit.set()


from django.shortcuts import render
from django.http import HttpResponse
from common.models import Users,Goods,Types
from django.core.paginator import Paginator
from PIL import Image
from datetime import datetime
import time,os
from django.db.models import Q
from django.shortcuts import redirect
from django.core.urlresolvers import reverse

#获取产品类别索引
def typelist(request):
	try:
		context={}
		utype=Types.objects.filter(pid=0)
		context['utype']=utype
		return context
	except Exception as info:
		context={'info':info}
		return render(request,"myweb/info.html",context)

#获取单品
def list(request,uid):
	try:
		context=typelist(request)
		ulist=Goods.objects.get(id=uid)
		utype=Types.objects.get(id=ulist.typeid)
		context['list']=ulist
		context['typename']=utype.name
		return render(request,"myweb/vipuser/list.html",context)
	except Exception as info:
		context={'info':info}
		return render(request,"myweb/info.html",context)
#喜欢点赞
def love(request,uid):
	try:
		context=typelist(request)
		ulist=Goods.objects.get(id=uid)
		ulist.clicknum+=1
		ulist.save()
		utype=Types.objects.get(id=ulist.typeid)
		context['list']=ulist
		context['typename']=utype.name
		return render(request,"myweb/vipuser/list.html",context)
	except Exception as info:
		context={'info':info}
		return render(request,"myweb/info.html",context)


from keras.models import Sequential
from keras.layers import Dense

class TensorFlowEstimator():

    def __init__(self, X_train, y_train):
        self.X_train = X_train
        self.y_train = y_train

    def fit(self):
        # Initialize the constructor
        model = Sequential()

        # Add an input layer
        model.add(Dense(12, activation='relu', input_shape=(self.X_train.shape[1],)))

        # Add one hidden layer
        model.add(Dense(8, activation='relu'))

        # Add an output layer
        model.add(Dense(1, activation='sigmoid'))

        # model fit
        model.compile(loss='binary_crossentropy',
                      optimizer='adam',
                      metrics=['accuracy'])

        return model.fit(self.X_train, self.y_train, epochs=20, batch_size=1, verbose=0)


import os
import sys
import time
import numpy as np
import imgaug

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools import mask as maskUtils

# Root directory of the project
ROOT_DIR = os.path.abspath("Mask_RCNN/")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import model as modellib, utils

class DatasetConfig(Config):
    NAME = "hake"
    IMAGES_PER_GPU = 2
    NUM_CLASSES = 1 + 1
    STEPS_PER_EPOCH = 100
    VALIDATION_STEPS = 1

class InferenceConfig(DatasetConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

class Dataset(utils.Dataset):
    def load_dataset(self, image_path=None, annotations_file=None):
        coco = COCO(annotations_file)

        class_ids = sorted(coco.getCatIds())
        image_ids = list(coco.imgs.keys())

        # Add classes
        for i in class_ids:
            self.add_class("coco", i, coco.loadCats(i)[0]["name"])

        # Add images
        for i in image_ids:
            self.add_image(
                "coco", image_id=i,
                path=os.path.join(image_path, coco.imgs[i]['file_name']),
                width=coco.imgs[i]["width"],
                height=coco.imgs[i]["height"],
                annotations=coco.loadAnns(coco.getAnnIds(imgIds=[i], catIds=class_ids, iscrowd=None)))

    def load_mask(self, image_id):
        image_info = self.image_info[image_id]

        instance_masks = []
        class_ids = []
        annotations = self.image_info[image_id]["annotations"]
        # Build mask of shape [height, width, instance_count] and list
        # of class IDs that correspond to each channel of the mask.
        for annotation in annotations:
            class_id = self.map_source_class_id(
                "coco.{}".format(annotation['category_id']))
            if class_id:
                m = self.annToMask(annotation, image_info["height"],
                                   image_info["width"])
                # Some objects are so small that they're less than 1 pixel area
                # and end up rounded out. Skip those objects.
                if m.max() < 1:
                    continue
                # Is it a crowd? If so, use a negative class ID.
                if annotation['iscrowd']:
                    # Use negative class ID for crowds
                    class_id *= -1
                    # For crowd masks, annToMask() sometimes returns a mask
                    # smaller than the given dimensions. If so, resize it.
                    if m.shape[0] != image_info["height"] or m.shape[1] != image_info["width"]:
                        m = np.ones([image_info["height"], image_info["width"]], dtype=bool)
                instance_masks.append(m)
                class_ids.append(class_id)

        # Pack instance masks into an array
        if class_ids:
            mask = np.stack(instance_masks, axis=2).astype(np.bool)
            class_ids = np.array(class_ids, dtype=np.int32)
            return mask, class_ids
        else:
            # Call super class to return an empty mask
            return super(Dataset, self).load_mask(image_id)

    def image_reference(self, image_id):
        super(Dataset, self).image_reference(image_id)

    # The following two functions are from pycocotools with a few changes.

    def annToRLE(self, ann, height, width):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        segm = ann['segmentation']
        if isinstance(segm, list):
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = maskUtils.frPyObjects(segm, height, width)
            rle = maskUtils.merge(rles)
        elif isinstance(segm['counts'], list):
            # uncompressed RLE
            rle = maskUtils.frPyObjects(segm, height, width)
        else:
            # rle
            rle = ann['segmentation']
        return rle

    def annToMask(self, ann, height, width):
        """
        Convert annotation which can be polygons, uncompressed RLE, or RLE to binary mask.
        :return: binary mask (numpy 2D array)
        """
        rle = self.annToRLE(ann, height, width)
        m = maskUtils.decode(rle)
        return m


import time
from turtle import Screen
from player import Player
from car_manager import CarManager
from scoreboard import Scoreboard

screen = Screen()

screen.title("Turtle Crossing Game")
screen.setup(width=600, height=600)
screen.tracer(0)

player = Player()
car_manager = CarManager()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(player.up, "Up")

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)

    car_manager.create_car()
    car_manager.move_cars()

    # detect if turtle collides with car
    for car in car_manager.all_cars:
        if player.distance(car) < 20:
            game_is_on = False
            scoreboard.game_over()

    # detect when turtle hits top edge
    if player.ycor() > player.end_pos:
        player.reset_player()
        scoreboard.increase_level()
        car_manager.increase_speed()

screen.exitonclick()


#!/usr/bin/python3
""" Module for base class """

import cmd
import sys
import models
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.place import Place
from models.amenity import Amenity
from models.review import Review
import shlex

classes = {"Amenity": Amenity, "BaseModel": BaseModel, "City": City,
           "Place": Place, "Review": Review, "State": State, "User": User}

class HBNBCommand(cmd.Cmd):
    """ The command interpreter for Bnb """
    prompt = '(hbnb) '
    classes = {"BaseModel", "User", "State", "City", "Place",
               "Amenity", "Review"}

    def do_create(self, line):
        """ Creates an object of certain class """
        if not len(line):
            print("** class name missing **")
            return
        if line not in HBNBCommand.classes:
            print("** class doesn't exist **")
            return
        newObjects = eval(line)()
        print(newObjects.id)
        newObjects.save()

    def do_show(self, line):
        """
            Prints the string representation of an instance
            based on the class name and id
        """
        if not len(line):
            print("** class name missing **")
            return
        aux = line.split()
        if aux[0] not in HBNBCommand.classes:
            print("** class doesn't exist **")
            return
        if len(aux) < 2:
            print("** instance id missing **")
            return
        if aux[0] + '.' + aux[1] not in storage.all().keys():
            print("** no instance found **")
            return
        else:
            print(storage.all()[aux[0] + '.' + aux[1]])

    def do_destroy(self, line):
        """
            Destroy the string representation of an instance
            based on the class name and id
        """
        if not len(line):
            print("** class name missing **")
            return
        aux = line.split()
        if aux[0] not in HBNBCommand.classes:
            print("** class doesn't exist **")
            return
        if len(aux) < 2:
            print("** instance id missing **")
            return
        if aux[0] + '.' + aux[1] not in storage.all().keys():
            print("** no instance found **")
            return
        del storage.all()[aux[0] + '.' + aux[1]]
        storage.save()

    def do_all(self, line):
        """ Prints all the objects """
        aux = line.split()
        if not len(line):
            print([str(value) for key, value in storage.all().items()])
            return
        if aux[0] not in HBNBCommand.classes:
            print("** class doesn't exist **")
            return
        print([str(value) for key, value in storage.all().items()
              if aux[0] == type(value).__name__])

    def do_update(self, arg):
        """Update an instance based on class, attr, id, etc"""
        args = shlex.split(arg)
        integers = ["number_rooms", "number_bathrooms", "max_guest",
                    "price_by_night"]
        floats = ["latitude", "longitude"]

        if len(args) == 0:
            print("** class name missing **")
        elif args[0] in classes:
            if len(args) > 1:
                k = args[0] + "." + args[1]
                if k in models.storage.all():
                    if len(args) > 2:
                        if len(args) > 3:
                            if args[0] == "Place":
                                if args[2] in integers:
                                    try:
                                        args[3] = int(args[3])
                                    except:
                                        args[3] = 0
                                elif args[2] in floats:
                                    try:
                                        args[3] = float(args[3])
                                    except:
                                        args[3] = 0.0
                            setattr(models.storage.all()[k], args[2], args[3])
                            models.storage.all()[k].save()
                        else:
                            print("** value missing **")
                    else:
                        print("** attribute name missing **")
                else:
                    print("** no instance found **")
            else:
                print("** instance id missing **")
        else:
            print("** class doesn't exist **")

    def help_quit(self):
        """ Prints the help for the quit """
        print("Quit command to exit the program")

    def emptyline(self):
        """ Do nothing """
        pass

    def do_EOF(self, line):
        """ Get out of the cmd """
        print("")
        return True

    def do_quit(self, line):
        """ Exit the aplication """
        return True

    def default(self, line):
        """ Executes the process with the class name """
        lineSplited = line.split(".")
        className = lineSplited[0]
        command = lineSplited[1].split('(')[0]
        newLine = command + " " + className + " " + " "
        string = lineSplited[1].split('(')[1].replace(')', "").split(", ")
        argument = command + " " + className + " " + " " \
            .join(el.replace('"', "") for el in string)
        self.onecmd(argument)

if __name__ == '__main__':
    HBNBCommand().cmdloop()


'''
生成验证集二进制文件
'''
import RGBImageProcessor
import grayImageProcessor
import tensorflow as tf
import PIL as Image
import matplotlib.pyplot as plt
import os
#from torchvision import transforms

orig_picture = 'E:/study/DL/1(validation)'
gen_picture = 'E:/study/DL/1(validation)'
RecordFileName = "RGBImage_validation.tfrecords"

#t = grayImageProcessor.GaryIP(orig_picture, gen_picture)
t = RGBImageProcessor.RGBIP(orig_picture, gen_picture, RecordFileName)
print(t.classes)
print(t.num_samples)
os.system("pause")
t.create_record()
batch = t.read_and_decode()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())

with tf.Session() as sess:
    sess.run(init_op)
    coord = tf.train.Coordinator()
    therads = tf.train.start_queue_runners(coord = coord)

    for i in range(t.num_samples):
        example, lab = sess.run(batch)
        shape = tf.shape(example)
        print(sess.run(shape))
        t.show_tensor_to_image(example)
        print(lab)
    coord.request_stop()
    coord.join(therads)
    sess.close()


#!/usr/bin/env python3

"""
At the bottom of this page is a very long file called mystery.txt The lines of this file contain either the word UP or DOWN or a pair of numbers. UP and DOWN are instructions for a turtle to lift up or put down its tail. The pairs of numbers are some x,y coordinates. Write a program that reads the file mystery.txt and uses the turtle to draw the picture described by the commands and the set of points.
"""

import turtle

def get_data():
    data = open("mystery.txt", "r")
    values = []
    for line in data:
        line = line.rstrip()
        if line == "UP" or line == "DOWN":
            values.append(line)
        else:
            line = line.split()
            line[0] = int(line[0])
            line[1] = int(line[1])
            values.append(line)
    return values

def create_screen():
    wn = turtle.Screen()
    return wn

def create_turtle():
    tess = turtle.Turtle()
    return tess

def draw_mystery(t, acts):
    for act in acts:
        if act == "UP":
            t.penup()
        elif act == "DOWN":
            t.pendown()
        else:
            t.goto(act[0], act[1])

wn = create_screen()
draw_mystery(create_turtle(), get_data())
wn.exitonclick()


##############################################################################
# File for running Brunel on real data with default 2008 geometry, as defined in
#                                                        DDDB/Configuration.py
# Syntax is:
#
#   gaudirun.py $ESCHEROPTS/AlignOTCosmics.py $ESCHEROPTS/2008-Cosmic-Data.py
#
# or
#
#   $ESCHEROPTS/gaudiiter.py -n NUMITER -e NUMEVENTS $ESCHEROPTS/AlignOTCosmics.py $ESCHEROPTS/2008-Cosmic-Data.py
#
##############################################################################

from Configurables import ( Escher, TrackSys, RecSysConf )

from GaudiKernel.ProcessJobOptions import importOptions
importOptions('$STDOPTS/PreloadUnits.opts')

Escher().DataType     = "2008"
Escher().Kalman = True
Escher().Millepede = False
Escher().TrackContainer = "Rec/Track/Best" # Velo, VeloR, Long, Upstream, Downstream, Ttrack, Muon or Calo
Escher().EvtMax = 100000
Escher().SpecialData = [ "fieldOff", "cosmics"]
TrackSys().ExpertTracking += [ "noDrifttimes","kalmanSmoother" ]
TrackSys().TrackPatRecAlgorithms = [ "PatSeed" ]
TrackSys().TrackExtraInfoAlgorithms = ['']

RecSysConf().RecoSequence = ["TT","IT","OT","Tr"]

# load a special database
#from Configurables import ( CondDB, CondDBAccessSvc )
#otGeom = CondDBAccessSvc( 'OTGeom' )
#otGeom.ConnectionString = 'sqlite_file:/afs/cern.ch/user/j/janos/dbase/OTDDDBCroissant.db/DDDB'
#CondDB().addLayer( otGeom )
#otCond = CondDBAccessSvc( 'OTCond' )
#otCond.ConnectionString = 'sqlite_file:/afs/cern.ch/user/j/janos/dbase/LHCBCOND_changes.db/LHCBCOND'
#CondDB().addLayer( otCond )

# configure the alignment
from Configurables import TAlignment
from TAlignment.Alignables import *
elements = Alignables()
elements.OTCFramesASide("TxTzRz")
elements.OTCFramesCSide("TxTzRz")
constraints = [
    "OT/T1X1C : OT/T1X1UCSide : Tx Ty Tz Rz",
    "OT/T1X1A : OT/T1X1UASide : Tx Ty Tz Rz",
    "OT/T3X2C : OT/T3X1UCSide : Tx Ty Tz Rz",
    "OT/T3X2A : OT/T3X1UASide : Tx Ty Tz Rz"
    ]
TAlignment().ElementsToAlign      = list(elements)
TAlignment().TrackLocation        = "Rec/Track/AlignTracks"
TAlignment().Constraints          = constraints
TAlignment().WriteCondSubDetList  = ['OT']

from Configurables import (PatSeeding, PatSeedingTool)
PatSeeding = PatSeeding("PatSeeding")
PatSeeding.addTool( PatSeedingTool("PatSeedingTool") )
PatSeeding.PatSeedingTool.MaxOTOccupancy         = 0.02
PatSeeding.PatSeedingTool.MaxITOccupancy         = 0.02
#PatSeeding.PatSeedingTool.CommonXFraction        = 0.
#PatSeeding.PatSeedingTool.MaxUsedFractPerRegion  = 0.
#PatSeeding.PatSeedingTool.MaxUsedFractITOT       = 0.
#PatSeeding.PatSeedingTool.MaxUsedFractLowQual    = 0.

# set up a track selection
from Configurables import (GaudiSequencer,TrackContainerCopy,TrackSelector,TrackMonitor)
trackFilterSeq = GaudiSequencer("TrackFilterSeq")
alignSelector  = TrackContainerCopy("AlignSelector",
                                    inputLocation = "Rec/Track/Best",
                                    outputLocation = "Rec/Track/AlignTracks")
alignSelector.addTool( TrackSelector("Selector", MaxChi2Cut = 5) )
trackFilterSeq.Members.append( alignSelector )
trackFilterSeq.Members.append( TrackMonitor("AlignTrackMonitor",
                                            TracksInContainer = "Rec/Track/AlignTracks") )

##############################################################################
# I/O datasets are defined in a separate file, see examples in 2008-TED_Data.py
##############################################################################


# Copyright 2014 Wind River, Inc.
# Copyright 2012 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
# Copyright (c) 2013-2017 Wind River Systems, Inc.
#

from django.core.urlresolvers import NoReverseMatch
from django.core.urlresolvers import reverse
from django.utils import html
from django.utils import safestring
from django.utils.translation import string_concat
from django.utils.translation import ugettext_lazy as _  # noqa
from django.utils.translation import ungettext_lazy

from horizon import exceptions
from horizon import tables

from openstack_dashboard import api
from openstack_dashboard.api import nova
from openstack_dashboard.dashboards.project.volumes.tables \
    import get_attachment_name
from openstack_dashboard.usage import quotas

from starlingx_dashboard.api import nova as stx_nova

DELETABLE_STATES = ("available", "error")

class DeleteServerGroup(tables.DeleteAction):
    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Delete Server Group",
            u"Delete Server Groups",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Deleted Server Group",
            u"Deleted Server Groups",
            count
        )

    def delete(self, request, obj_id):
        obj = self.table.get_object_by_id(obj_id)
        name = self.table.get_object_display(obj)

        try:
            stx_nova.server_group_delete(request, obj_id)
        except Exception:
            msg = _('Unable to delete group "%s" because it is not empty. '
                    'Either delete the member instances'
                    ' or remove them from the group.')
            exceptions.check_message(["group", "not", "empty."], msg % name)
            raise

    # maybe do a precheck to see if the group is empty first?
    def allowed(self, request, server_group=None):
        return True

class CreateServerGroup(tables.LinkAction):
    name = "create"
    verbose_name = _("Create Server Group")
    url = "horizon:admin:server_groups:create"
    classes = ("ajax-modal", "btn-create")
    icon = "plus"

    def allowed(self, request, volume=None):
        usages = quotas.tenant_quota_usages(request)
        if usages['server_groups']['available'] <= 0:
            if "disabled" not in self.classes:
                self.classes = [c for c in self.classes] + ['disabled']
                self.verbose_name = string_concat(self.verbose_name, ' ',
                                                  _("(Quota exceeded)"))
        else:
            self.verbose_name = _("Create Server Group")
            classes = [c for c in self.classes if c != "disabled"]
            self.classes = classes
        return True

class EditAttachments(tables.LinkAction):
    name = "attachments"
    verbose_name = _("Edit Attachments")
    url = "horizon:admin:server_groups:attach"
    classes = ("ajax-modal", "btn-edit")

    def allowed(self, request, server_group=None):
        return True  # volume.status in ("available", "in-use")

class CreateSnapshot(tables.LinkAction):
    name = "snapshots"
    verbose_name = _("Create Snapshot")
    url = "horizon:admin:server_groups:create_snapshot"
    classes = ("ajax-modal", "btn-camera")

    def allowed(self, request, server_group=None):
        return True  # server_group.status == "available"

class UpdateRow(tables.Row):
    ajax = True

    def get_data(self, request, server_group_id):
        server_group = stx_nova.server_group_get(request, server_group_id)
        if not server_group.name:
            server_group.name = server_group_id

        return server_group

def get_policies(server_group):
    policies = ', '.join(server_group.policies)
    return policies

def get_metadata(server_group):
    metadata_items = ['{}:{}'.format(x, y) for x, y in
                      server_group.metadata.items()]
    metadata = ', '.join(metadata_items)
    return metadata

def get_member_name(request, server_id):
    try:
        server = api.nova.server_get(request, server_id)
        name = server.name
    except Exception:
        name = None
        exceptions.handle(request, _("Unable to retrieve "
                                     "member information."))
    # try and get a URL
    try:
        url = reverse("horizon:admin:instances:detail", args=(server_id,))
        instance = '%s' % (url, html.escape(name))
    except NoReverseMatch:
        instance = name
    return instance

class ProjectNameColumn(tables.Column):
    """Customized column class

    Customized column class that does complex processing on the
    server group.
    """

    def get_raw_data(self, server_group):
        request = self.table.request
        project_id = getattr(server_group, 'project_id', None)
        try:
            tenant = api.keystone.tenant_get(request,
                                             project_id,
                                             admin=True)

            project_name = getattr(tenant, "name", None)
        except Exception:
            project_name = "(not found)"

        return project_name

class MemberColumn(tables.Column):
    """Customized column class

    Customized column class that does complex processing on the instances
    in a server group.  This was substantially copied
    from the volume equivalent.
    """

    def get_raw_data(self, server_group):
        request = self.table.request
        link = _('%(name)s (%(id)s)')
        members = []
        for member in server_group.members:
            member_id = member
            name = get_member_name(request, member)
            vals = {"name": name, "id": member_id}
            members.append(link % vals)
        return safestring.mark_safe(", ".join(members))

def get_server_group_type(server_group):
    return server_group.volume_type if server_group.volume_type != "None" \
        else None

class ServerGroupsFilterAction(tables.FilterAction):
    def filter(self, table, server_groups, filter_string):
        """Naive case-insensitive search."""
        q = filter_string.lower()
        return [group for group in server_groups
                if q in group.display_name.lower()]

class ServerGroupsTable(tables.DataTable):
    projectname = ProjectNameColumn("project_name",
                                    verbose_name=_("Project"))
    name = tables.Column("name",
                         verbose_name=_("Group Name"),
                         link="horizon:admin:server_groups:detail")
    policies = tables.Column(get_policies,
                             verbose_name=_("Policies"))
    members = MemberColumn("members",
                           verbose_name=_("Members"))
    metadata = tables.Column(get_metadata,
                             verbose_name=_("Metadata"))

    class Meta(object):
        name = "server_groups"
        verbose_name = _("Server Groups")
        row_class = UpdateRow
        table_actions = (
            CreateServerGroup, DeleteServerGroup, ServerGroupsFilterAction)
        row_actions = (DeleteServerGroup,)

    def get_object_display(self, obj):
        return obj.name

class DetachServerGroup(tables.BatchAction):
    name = "detach"
    action_type = 'danger'

    @staticmethod
    def action_present(count):
        return ungettext_lazy(
            u"Detach Server Group",
            u"Detached Server Groups",
            count
        )

    @staticmethod
    def action_past(count):
        return ungettext_lazy(
            u"Detaching Server Group",
            u"Detaching Server Groups",
            count
        )

    def action(self, request, obj_id):
        attachment = self.table.get_object_by_id(obj_id)
        api.nova.instance_server_group_detach(request,
                                              attachment.get('server_id',
                                                             None),
                                              obj_id)

    def get_success_url(self, request):
        return reverse('horizon:admin:server_groups:index')

class AttachedInstanceColumn(tables.Column):
    """Customized column class

    Customized column class that does complex processing on the attachments
    for a server group.
    """

    def get_raw_data(self, attachment):
        request = self.table.request
        return safestring.mark_safe(get_attachment_name(request, attachment,
                                                        True))

class AttachmentsTable(tables.DataTable):
    instance = AttachedInstanceColumn(get_member_name,
                                      verbose_name=_("Instance"))
    device = tables.Column("device",
                           verbose_name=_("Device"))

    def get_object_id(self, obj):
        return obj['id']

    def get_object_display(self, attachment):
        instance_name = get_attachment_name(self.request, attachment, True)
        vals = {"dev": attachment['device'],
                "instance_name": html.strip_tags(instance_name)}
        return _("%(dev)s on instance %(instance_name)s") % vals

    def get_object_by_id(self, obj_id):
        for obj in self.data:
            if self.get_object_id(obj) == obj_id:
                return obj
        raise ValueError('No match found for the id "%s".' % obj_id)

    class Meta(object):
        name = "attachments"
        verbose_name = _("Attachments")
        table_actions = (DetachServerGroup,)
        row_actions = (DetachServerGroup,)


# coding: utf-8

# import tkinter
from tkinter import *
from random import shuffle
import os
# import time  # sleep

SIDE = 6                   # кол-во элеметов в строке и столбце
QSIDE = (SIDE ** 2) // 2   # кол-во уникальных значений

prev = None

print('Memorizer')

def cmd():      # PEP-8
    x = 13
    print('Алиса, выпей чаю!')
    print(x)

def hide_all(btns):
    pass                # nop ;; no operation
    for i in range(SIDE):
        for j in range(SIDE):
            # btns[i][j].configure(text=' ? ')
            btns[i][j].configure(image=faq)

def hide_both(btns, i1, j1, i2, j2):
    # btns[i1][j1].configure(text=' ? ')
    # btns[i2][j2].configure(text=' ? ')
    btns[i1][j1].configure(image=faq)
    btns[i2][j2].configure(image=faq)

def change(btns, xs, i, j):
    global prev

    # btns[i][j].configure(text='{:>3}'.format(xs[i * SIDE + j]))
    btns[i][j].configure(image=imgs[i * SIDE + j])
    if prev:
        if xs[i * SIDE + j] != xs[prev[0] * SIDE + prev[1]]:
            main_window.after(1000, hide_both, btns, prev[0], prev[1], i, j)
        prev = None
    else:
        prev = (i, j)

# def <lambda> (ii=i, jj=j):
#  return change(buttons, ls, ii, jj)

main_window = Tk()
main_window.title('Запоминалка')

faq = PhotoImage(file='FAQ.gif')

# ls = []
# for i in range(1, 100):
#     ls.append(i)

# Создаем список имён файлов картинок
images = [os.path.join('gif', f) for f in os.listdir('gif')]

shuffle(images)     # перемешиваем список
images = images[:QSIDE] * 2     # берём срез от списка и размножаем его
shuffle(images)                  # ещё раз перемешаем
print(images)

# Создаем список картинок на основании имён файлов
imgs = [PhotoImage(file=f) for f in images]

ls = [i for i in range(1, 100)]     # что происходит см. выше
shuffle(ls)     # перемешиваем список
ls = ls[:QSIDE] * 2     # берём срез от списка и размножаем его
shuffle(ls)             # ещё раз перемешаем
print(ls)

buttons = []
for i in range(SIDE):
    buttons.append([])
    for j in range(SIDE):
        x = ls[i * SIDE + j]
        button = Button(main_window, # text=' ? ',
                                # text='{:>3}'.format(x),
                                image=imgs[i * SIDE + j],
                                font=('Courier New', 12, 'normal'),
                                relief=FLAT,
                                command=lambda ii=i, jj=j: change(buttons, images, ii, jj))
        buttons[i].append(button)
        # button.pack()
        button.grid(row=i, column=j)

main_window.after(2000, hide_all, buttons)
main_window.mainloop()


### This is the main code file f my telescope control software.

# Thanks to:
# https://git.nexlab.net/astronomy/skylived/tree/bd59190026d9d95b39983f8a0106a7e17023aee8/DecraDB/xephemdb
# https://github.com/Alex-Broughton/StarAtlas
# I'll do proper credits eventually.

import sys # for updatable print for status messages

# Raspberry Pi GPIO access
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Global variables
import config
from config import *

# Welcome message
print ("\n\x1b[1;37;42m ====== DIY GoTo Telesope Control ===== " + TColors.normal + " \n")

from load_observers import *
#config.observers = gather_observers(config.observers_file)
#config.observer, config.observer_name = set_observer(config.observer_name)

from load_landmarks import *
#config.landmarks = gather_landmarks(config.landmarks_file)

from backup_location import * # CRITICAL: before set_functions
from set_functions import * # CRITICAL: after backup_location

from ephem_wrapper import * # here: for printing names of stars

#from move_function import * # not directly
#from move_1_step import * # not directly
#from move_1_deg import * # not directly

# Operating modes
from track_mode import *
from go_to_mode import *
from manual_drive_mode import *
from scan_sky_mode import *

##################

def do_nothing(): pass # passthrough nethtod for menu

def quit_nicely():
	GPIO.cleanup()
	print (TColors.italic + "\nGood riddance!\n" + TColors.normal)

def show_options():
	print (TColors.bold + "\nAvailable options:" + TColors.normal)
	print (TColors.orange_on_black + "0:" + TColors.normal + " Quit nicely")
	print (TColors.orange_on_black + "1-4:" + TColors.blue_on_black + " Operating modes: " + TColors.normal + "track " + TColors.orange_on_black + "(1)" + TColors.normal + ", move " + TColors.orange_on_black + "(2)" + TColors.normal + ", manual drive " + TColors.orange_on_black + "(3)" + TColors.normal + " , scan sky area " + TColors.orange_on_black + "(4)" + TColors.normal)
	print (TColors.orange_on_black + "5-9:" + TColors.blue_on_black + " Set: " + TColors.normal + "speed " + TColors.orange_on_black + "(5)" + TColors.normal + ", microstepping " + TColors.orange_on_black + "(6)" + TColors.normal + ", track refresh interval " + TColors.orange_on_black + "(7)" + TColors.normal + ", observer " + TColors.orange_on_black + "(8)" + TColors.normal + ", current coordinates " + TColors.orange_on_black + "(9)" + TColors.normal)
	print (TColors.orange_on_black + "10:" + TColors.normal + " Recover last recoded coordinates")
	print (TColors.orange_on_black + "18 19 20:" + TColors.blue_on_black + " Print: " + TColors.normal + "observer " + TColors.orange_on_black + "(18)" + TColors.normal + ", current coordinates " + TColors.orange_on_black + "(19)" + TColors.normal + ", status " + TColors.orange_on_black + "(20)" + TColors.normal)
	print (TColors.orange_on_black + "21-23:" + TColors.blue_on_black + " Print: " + TColors.normal + "named stars " + TColors.orange_on_black + "(21)" + TColors.normal + ", all available stars in YBS " + TColors.orange_on_black + "(22)" + TColors.normal + ", landmakrs " + TColors.orange_on_black + "(23)" + TColors.normal)
	print (TColors.orange_on_black + "31:" + TColors.normal + " Make fake star")

def switch_main(option):
	switcher = {
		0: quit_nicely,

		1: track,
		2: go_to_location,
		3: manual_drive,
		4: scan_sky,

		5: set_speed,
		6: set_microstepping,
		7: set_track_refresh_interval,
		8: set_observer,
		9: set_location,
		10: recover_last_location,

		18: print_observer,
		19: print_location,
		20: print_status,

		21: print_named_stars,
		22: print_available_stars,
		23: print_landmarks,

		31: make_fake_star,

		99: show_options, # hidden
		100: do_nothing # hidden
	}
	func = switcher.get(option, show_options)
	func()

################

def main():

	#sys.stdout.write("\r")
	sys.stdout.write(TColors.italic + "Loading observers ... " + TColors.normal)
	sys.stdout.flush()
	config.observers = gather_observers(config.observers_file)
	config.observer, config.observer_name = set_observer(config.observer_name)

	sys.stdout.write("\r")
	sys.stdout.write(TColors.italic + "Loading landmarks ... " + TColors.normal)
	sys.stdout.flush()
	config.landmarks = gather_landmarks(config.landmarks_file)

	sys.stdout.write("\r")
	sys.stdout.write(TColors.italic + "Loading celestial bodies into ephem ... " + TColors.normal)
	sys.stdout.flush()

	# named stars from YBS
	#ephem_wrapper.list_of_named_stars = open("named_stars.txt", "r").readlines()
	#ephem_wrapper.list_of_named_stars = [star[:-1].lower() for star in list_of_named_stars]

	# named stars in an other catalog
	#ephem_wrapper.list_of_stars_YBS = open("YBS2.txt", "r").readlines()
	#ephem_wrapper.list_of_stars_YBS = [star[:-1].lower() for star in list_of_stars_YBS]

	sys.stdout.write("\r")
	sys.stdout.write("                                                                          \n")
	sys.stdout.flush()

	#print("\n")
	recover_last_location()
	config.fake_star = make_fake_star(0, 0) # defined in config.py as None

	# main loop:
	option = 100 # default, do_nothing()
	while option != 0:
		switch_main(int(option)) # first tine: do_nothing()
		print('\n')

		# next step
		show_options()
		try:
			option = input(TColors.italic + "What is my purpose? " + TColors.normal).strip()
		except KeyboardInterrupt as err: # accept Ctrl+c
			print(TColors.red_on_black + "  [ keybord interrupt ... ]  " + TColors.normal)
			quit_nicely()
			return()

		if not option.isnumeric():
			print("Please choose one of the listed options! ")
			option = 100 # do_nothing()
			#continue
		option = int(option)

	#GPIO.cleanup()
	quit_nicely()
	return(0)

#print("before if")
if __name__ == '__main__':
	main()

#GPIO.cleanup()
#quit_nicely()


from gazpacho import get, Soup
import pandas as pd

url = 'https://datatau.net/'
html = get(url)
soup = Soup(html)
trs = soup.find('tr', {'class': 'athing'})

def parse_metadata(tr):
    attributes = tr.find('a', {'class': 'storylink'}).attrs
    link = attributes['href']
    title = attributes['title']
    return title, link

links = [parse_metadata(tr) for tr in trs]
scores = soup.find('span', {'class': 'score'})
scores = [int(s.text.split(' ')[0]) for s in scores]

df = pd.DataFrame(links, columns=['title', 'link'])
scores = pd.DataFrame(scores, columns=['upvotes'])

df = pd.concat([df, scores], axis=1)
df


import cv2
import numpy as np

img = cv2.imread('spider.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray,100,200)
contours,hierarchy = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

img_contours = cv2.drawContours(img,contours,-1,(0,255,0),3)

cv2.imshow('original',img)
cv2.imshow('canny',canny)
cv2.imshow('contours',img_contours)
cv2.waitKey(0)
cv2.destroyAllWindows()


"""
observables.py
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import correlate
from math import ceil, pi, sin
from itertools import product

from pilot.utils import cached_property, requires, NotDefinedForField, bootstrap_sample
from pilot.fields import requires_topology

class requires_spins(requires):
    attributes = ("has_spins",)
    exception = NotDefinedForField
    message = "Not a spin system"

class observable:
    def __init__(self, func):
        self.func = func
        self._name = func.__name__.replace("_", " ")
        self.__doc__ = func.__doc__

    def __get__(self, instance, owner):
        sample = self.func(instance)

        # Take mean and standard deviation over final dimension
        value = sample.mean(axis=-1)
        error = sample.std(axis=-1)

        return value, error

def autocorrelation(chain):
    chain_shifted = chain - chain.mean()
    auto = correlate(chain_shifted, chain_shifted, mode="same")

    t0 = auto.size // 2  # this is true for mode="same"
    return auto[t0:] / auto[t0]  # normalise and take +ve shifts

def optimal_window(integrated, mult=2.0, eps=1e-6):

    # Exponential autocorrelation
    with np.errstate(invalid="ignore", divide="ignore"):
        exponential = np.clip(
            np.nan_to_num(mult / np.log((2 * integrated + 1) / (2 * integrated - 1))),
            a_min=eps,
            a_max=None,
        )

    # Infer ensemble size, assuming correlation mode was 'same'!!!
    n_t = integrated.shape[-1]
    ensemble_size = n_t * 2

    # Window func, we want the minimum
    t_sep = np.arange(1, n_t + 1)
    window_func = np.exp(-t_sep / exponential) - exponential / np.sqrt(
        t_sep * ensemble_size
    )

    return np.argmax((window_func[..., 1:] < 0), axis=-1)

class Observables:
    tables = [
        "two_point_scalars",
        "spin_observables",
        "action_moments",
        "correlation_length",
        "topological_observables",
        "zero_momentum_correlator",
        "effective_pole_mass",
        "exponential_correlation_length",
        "two_point_correlator",
        "two_point_correlator_integrated_autocorrelation",
    ]
    figures = [
        "zero_momentum_correlator",
        "effective_pole_mass",
        "two_point_correlator",
        "two_point_correlator_series",
        "two_point_correlator_autocorrelation",
        "topological_charge_series",
        "topological_charge_autocorrelation",
    ]

    def __init__(self, ensemble, bootstrap_sample_size):
        self.ensemble = ensemble
        self.bootstrap_sample_size = bootstrap_sample_size

        self.volume = ensemble.lattice.volume

        self.has_topology = ensemble.has_topology
        self.has_spins = ensemble.has_spins

    def __str__(self):
        out = "\n"
        for table in self.tables:
            try:
                df = getattr(self, "table_" + table).to_string()
                header = table.replace("_", " ")
                line = "".join(["-" for char in table])
                out += f"{header}\n{line}\n{df}\n\n"
            except NotDefinedForField:
                pass

        return out

    @cached_property
    def _two_point_correlator(self):
        return self.ensemble.boot_two_point_correlator(self.bootstrap_sample_size)

    @cached_property
    def _zero_momentum_correlator(self):
        return 0.5 * (
            self._two_point_correlator.mean(axis=0)
            + self._two_point_correlator.mean(axis=1)
        )

    @cached_property
    def _effective_pole_mass(self):
        inner_indices = np.arange(
            1, self._zero_momentum_correlator.shape[0] - 1, dtype=int
        )
        return np.arccosh(
            (
                self._zero_momentum_correlator[inner_indices - 1]
                + self._zero_momentum_correlator[inner_indices + 1]
            )
            / (2 * self._zero_momentum_correlator[inner_indices])
        )

    @cached_property
    def _two_point_correlator_series(self):
        return self.ensemble.vol_avg_two_point_correlator

    @cached_property
    def _auto_two_point_correlator(self):
        return np.array(
            [
                autocorrelation(self._two_point_correlator_series[i])
                for i in range(self._two_point_correlator_series.shape[0])
            ]
        )

    @cached_property
    def _iauto_two_point_correlator(self):
        return np.cumsum(self._auto_two_point_correlator, axis=-1) - 0.5

    @cached_property
    def _optimal_window_two_point_correlator(self):
        return optimal_window(self._iauto_two_point_correlator)

    @observable
    def two_point_correlator(self):
        return self._two_point_correlator

    @observable
    def susceptibility(self):
        return self._two_point_correlator.sum(axis=(0, 1))

    @observable
    def ising_energy(self):
        return (self._two_point_correlator[1, 0] + self._two_point_correlator[0, 1]) / 2

    @observable
    def second_moment_correlation_length(self):
        d1, d2 = np.array(self.ensemble.lattice.dimensions)
        x1 = np.arange(-d1 // 2 + 1, d1 // 2 + 1)
        x2 = np.arange(-d2 // 2 + 1, d1 // 2 + 1)
        m1, m2 = np.meshgrid(x1, x2)
        m_sq = np.roll(m1 ** 2 + m2 ** 2, (-d1 // 2 + 1, -d2 // 2 + 1), (0, 1)).reshape(
            d1, d2, 1
        )
        second_moment = (m_sq * self._two_point_correlator).sum(
            axis=(0, 1)
        ) / self._two_point_correlator.sum(axis=(0, 1))
        return second_moment / 4

    @observable
    def low_momentum_correlation_length_a(self):
        # NOTE: assume both dimensions same length
        L = self.ensemble.lattice.dimensions[0]
        kernel = np.cos(2 * pi / L * np.arange(L)).reshape(L, 1, 1)

        g_tilde_00 = self._two_point_correlator.sum(axis=(0, 1))
        g_tilde_10 = (self._two_point_correlator * kernel).sum(axis=(0, 1))

        return ((L ** 2) / (4 * pi ** 2)) * (1 - g_tilde_10 / g_tilde_00)

    @observable
    def low_momentum_correlation_length_b(self):
        L = self.ensemble.lattice.dimensions[0]
        kernel = np.cos(2 * pi / L * np.arange(L)).reshape(L, 1, 1)

        g_tilde_00 = self._two_point_correlator.sum(axis=(0, 1))
        g_tilde_10 = (self._two_point_correlator * kernel).sum(axis=(0, 1))

        return (g_tilde_00 / g_tilde_10 - 1) / (4 * sin(pi / L) ** 2)

    @observable
    def zero_momentum_correlator(self):
        return self._zero_momentum_correlator

    @observable
    def effective_pole_mass(self):
        return self._effective_pole_mass

    @observable
    def exponential_correlation_length(self):
        return (1 / self._effective_pole_mass) ** 2

    @property
    def weighted_mean_exp_correlation_length(self):
        values, errors = self.exponential_correlation_length
        # positive, non-zero shifts only
        T = len(values)
        values = values[: T // 2 + 1]
        errors = errors[: T // 2 + 1]

        weights = 1 / errors
        mean = np.sum(values * weights) / np.sum(weights)
        error = 1 / np.sum(weights)  # standard error on the mean
        return mean, error

    @property
    def table_two_point_correlator(self):
        value, error = self.two_point_correlator
        return pd.concat(
            [pd.DataFrame(value), pd.DataFrame(error)], keys=["value", "error"],
        )

    @property
    def table_correlation_length(self):
        keys = [
            "second_moment_correlation_length",
            "low_momentum_correlation_length_a",
            "low_momentum_correlation_length_b",
            "weighted_mean_exp_correlation_length",
        ]
        return pd.DataFrame(
            [getattr(self, key) for key in keys],
            index=[key.replace("_", " ") for key in keys],
            columns=["value", "error"],
        )

    @property
    def table_two_point_scalars(self):
        keys = [
            "ising_energy",
            "susceptibility",
        ]
        return pd.DataFrame(
            [getattr(self, key) for key in keys],
            index=[key.replace("_", " ") for key in keys],
            columns=["value", "error"],
        )

    @property
    def table_zero_momentum_correlator(self):
        return pd.DataFrame(self.zero_momentum_correlator, index=["value", "error"]).T

    @property
    def table_effective_pole_mass(self):
        return pd.DataFrame(self.effective_pole_mass, index=["value", "error"]).T

    @property
    def table_exponential_correlation_length(self):

        return pd.DataFrame(
            self.exponential_correlation_length, index=["value", "error"]
        ).T

    @property
    def table_two_point_correlator_integrated_autocorrelation(self):
        shape_out = self._iauto_two_point_correlator.shape[:-1]
        w_opt = self._optimal_window_two_point_correlator
        values_out = np.empty(shape_out)
        for tup in product(*[range(size) for size in shape_out]):
            index = tup + (w_opt[tup],)
            values_out[tup] = self._iauto_two_point_correlator[index]
        return pd.DataFrame(values_out)

    @property
    def plot_two_point_correlator(self):
        # NOTE: Annoyingly seems to sort things in alphabetical order
        iterator = self.table_two_point_correlator.groupby(level=0)
        fig, axes = plt.subplots(1, 2, sharex=True, sharey=True)
        for ax, (key, df) in zip(reversed(axes), iterator):
            img = ax.imshow(df)
            plt.colorbar(img, ax=ax, fraction=0.046, pad=0.04)
            ax.xaxis.tick_top()
            ax.set_title(key)
        fig.tight_layout()
        return fig

    @property
    def plot_zero_momentum_correlator(self):
        df = self.table_zero_momentum_correlator
        return df["value"].plot(
            x=df.index.values, yerr=df["error"], logy=True, title="zero momentum correlator"
        )

    @property
    def plot_effective_pole_mass(self):
        df = self.table_effective_pole_mass
        return df["value"].plot(
            x=df.index.values, yerr=df["error"], title="effective pole mass"
        )

    @property
    def plot_two_point_correlator_series(self):
        fig, ax = plt.subplots(1)
        ax.set_title("Two point correlator series")
        ax.set_xlabel("$t$")
        ax.set_ylabel("$G(x; t)$")
        for i in range(min(10, self._two_point_correlator_series.shape[0])):
            ax.plot(
                self._two_point_correlator_series[i],
                linewidth=0.5,
                label=f"$x =$ (0, {i+1})",
            )
        ax.legend()
        fig.tight_layout()
        return fig

    @property
    def plot_two_point_correlator_autocorrelation(self):
        auto_to_plot = self._auto_two_point_correlator
        integrated_to_plot = self._iauto_two_point_correlator
        lines_to_plot = self._optimal_window_two_point_correlator
        cut = max(10, 2 * np.max(lines_to_plot))

        fig, ax1 = plt.subplots(1)
        ax2 = ax1.twinx()
        ax1.set_title("$G(x; t)$ autocorrelation")
        ax1.set_xlabel("$\delta t$")
        ax1.set_ylabel("$\Gamma_G(\delta t)$")
        ax2.set_ylabel("$\sum \Gamma_G(\delta t)$")

        for i in range(min(10, auto_to_plot.shape[0])):
            color = next(ax1._get_lines.prop_cycler)["color"]
            ax1.plot(auto_to_plot[i, :cut], linestyle=":", linewidth=0.5, color=color)
            ax1.axvline(lines_to_plot[i], linestyle="--", linewidth=0.5, color=color)
            ax2.plot(
                integrated_to_plot[i, :cut],
                linestyle="-",
                linewidth=0.5,
                color=color,
                label=f"$x =$ (0, {i+1})",
            )

        ax1.set_xlim(left=0)
        ax1.set_ylim(top=1)
        ax2.set_ylim(bottom=0.5)

        ax2.legend()
        fig.tight_layout()
        return fig

    # ----------------------------------------------------------------------------- #
    #                               Spin observables                                #
    # ----------------------------------------------------------------------------- #
    @cached_property
    @requires_spins
    def _hamiltonian(self):
        return bootstrap_sample(self.ensemble.hamiltonian, self.bootstrap_sample_size)

    @cached_property
    @requires_spins
    def _magnetisation_sq(self):
        return bootstrap_sample(
            self.ensemble.magnetisation_sq, self.bootstrap_sample_size
        )

    @observable
    @requires_spins
    def energy_density(self):
        return self._hamiltonian.mean(axis=-1) / self.volume

    @observable
    def magnetic_susceptibility(self):
        return self._magnetisation_sq.mean(axis=-1) / self.volume

    @observable
    @requires_spins
    def heat_capacity(self):
        return self.ensemble.beta ** 2 * self._hamiltonian.var(axis=-1) / self.volume

    @property
    def table_spin_observables(self):
        keys = [
            "energy_density",
            "magnetic_susceptibility",
            "heat_capacity",
        ]
        return pd.DataFrame(
            [getattr(self, key) for key in keys],
            index=[key.replace("_", " ") for key in keys],
            columns=["value", "error"],
        )

    # ----------------------------------------------------------------------------- #
    #                    Observables involving the O(N) action                      #
    # ----------------------------------------------------------------------------- #
    @cached_property
    def _action(self):
        return bootstrap_sample(self.ensemble.action, self.bootstrap_sample_size)

    @observable
    def energy_density_v2(self):
        # Should be 2E_spin + 4
        return 2 * self._action.mean(axis=-1) / (self.ensemble.beta * self.volume)

    @observable
    def heat_capacity_v2(self):
        return self._action.var(axis=-1) / self.volume

    @property
    def table_action_moments(self):
        keys = ["energy_density_v2", "heat_capacity_v2"]
        return pd.DataFrame(
            [getattr(self, key) for key in keys],
            index=[key.replace("_", " ") for key in keys],
            columns=["value", "error"],
        )

    # ----------------------------------------------------------------------------- #
    #                           Topological observables                             #
    # ----------------------------------------------------------------------------- #
    @cached_property
    @requires_topology
    def _topological_charge_series(self):
        return self.ensemble.topological_charge

    @cached_property
    @requires_topology
    def _topological_charge(self):
        return bootstrap_sample(
            self._topological_charge_series, self.bootstrap_sample_size
        )

    @cached_property
    @requires_topology
    def _auto_topological_charge(self):
        return autocorrelation(self._topological_charge_series)

    @cached_property
    def _iauto_topological_charge(self):
        return np.cumsum(self._auto_topological_charge) - 0.5

    @cached_property
    def _optimal_window_topological_charge(self):
        return optimal_window(self._iauto_topological_charge)

    @observable
    def topological_charge(self):
        return self._topological_charge.mean(axis=-1)

    @observable
    def topological_susceptibility(self):
        return self._topological_charge.var(axis=-1) / self.volume

    @property
    def table_topological_observables(self):
        keys = ["topological_charge", "topological_susceptibility"]
        return pd.DataFrame(
            [getattr(self, key) for key in keys],
            index=[key.replace("_", " ") for key in keys],
            columns=["value", "error"],
        )

    @property
    def plot_topological_charge_series(self):
        fig, ax = plt.subplots(1)
        ax.set_title("Topological charge series")
        ax.set_xlabel("$t$")
        ax.set_ylabel("$Q(t)$")
        ax.plot(self._topological_charge_series, linewidth=1)
        fig.tight_layout()
        return fig

    @property
    def plot_topological_charge_autocorrelation(self):
        auto_to_plot = self._auto_topological_charge
        integrated_to_plot = self._iauto_topological_charge
        line = self._optimal_window_topological_charge
        cut = max(10, 2 * line)

        fig, ax1 = plt.subplots(1)
        ax2 = ax1.twinx()
        ax1.set_title("$Q$ autocorrelation")
        ax1.set_xlabel("$\delta t$")
        ax1.set_ylabel("$\Gamma_Q(\delta t)$")
        ax2.set_ylabel("$\sum \Gamma_Q(\delta t)$")

        ax1.plot(auto_to_plot[:cut], linestyle=":")
        ax2.plot(integrated_to_plot[:cut], linestyle="-")
        ax1.axvline(line, linestyle="--")
        ax2.annotate(
            fr"$\tau_Q = ${integrated_to_plot[line]:.3g}",
            xy=(0.05, 0.05),
            xycoords="axes fraction",
        )

        ax1.set_xlim(left=0)
        ax1.set_ylim(top=1)
        ax2.set_ylim(bottom=0.5)

        fig.tight_layout()
        return fig


'''
剑指 Offer 33. 二叉搜索树的后序遍历序列
输入一个整数数组，判断该数组是不是某二叉搜索树的后序遍历结果。
如果是则返回 true，否则返回 false。假设输入的数组的任意两个数字都互不相同。
'''

def verifyPostorder(self, postorder: list) -> bool:
    if not postorder: return True
    root = postorder[-1]
    left = 0
    for i in range(len(postorder)):
        if(postorder[i]<root):
            left += 1
        else:
            break
    for j in range(left,len(postorder)-1):
        if(postorder[j] < root):
            return False
    return self.verifyPostorder(postorder[0:left]) and self.verifyPostorder(postorder[left:len(postorder)-1])


import pickle
import datetime

import THESIS2019.utils.to_lexicon as lex
from THESIS2019.utils.base_words import *

# import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
# import matplotlib.pyplot as plt
from collections import defaultdict

import dit
from dit.divergences import jensen_shannon_divergence
import THESIS2019.utils.get_articles as get

# get the topic distribution for a document set (corpus)
def total_topic_distribution(model, corpus):
    dist = defaultdict(lambda:0)
    for doc in corpus:
        topics = model[doc]
        for topic,prob in topics:
            dist[topic] += prob
    # normalize
    s = sum(dist.values())
    for d in dist:
        dist[d] /= s
    return dist

def to_js_distro(distro):
    ids, probs = list(distro.keys()), list(distro.values())
    js_distro = dit.ScalarDistribution(ids, probs)
    return js_distro

def compare_topic_distributions(distros):
	js_distros = []
	for distro in distros:
		js_distros.append(to_js_distro(distro))
	return jensen_shannon_divergence(js_distros)

def graph_distribution(dist):
    x, y = dist.keys(), dist.values()
    plt.bar(x, y, alpha=0.5)
    plt.xlabel('Topic number')
    plt.ylabel('Proportion in document set')
    plt.title('Topic distribution')

    plt.show()

if __name__ == '__main__':
	# path = "/Users/ninawang/Thesis/remote/THESIS2019/example_data/NYT-OPINION2012-2013-processed/"
	# start = datetime.datetime(2012, 6, 1)
	# end = datetime.datetime(2013, 7, 1)
	# articles = lex.get_articles_from_filepath(path,start,end)

	# # notebook to find consistency over time of topic models using LDA

	datapath = "/Users/ninawang/Thesis/remote/THESIS2019/example_data_1000/"
	outlets = ['BREITBART','NATIONALREVIEW','FOX',
			'WASHINGTONEXAMINER','REUTERS','NPR',
			'NYT', 'MSN','CNN','SLATE']
	articles = get.get_articles_outlets(datapath,outlets,2012,filter_date=False)
	for outlet, article in articles.items():
		print("%s has %d articles" %(outlet, len(article)))

	arts, dictionary, corpus, model = lex.LDA(articles, LEFT_WORDS+RIGHT_WORDS,num_topics=30,load=False)

	# SAVE
	with open("all_model2012.pkl","wb") as f:
		pickle.dump(model, f)
	with open("all_articles2012.pkl","wb") as f:
		pickle.dump(arts, f)
	with open("all_dictionary2012.pkl","wb") as f:
		pickle.dump(dictionary, f)
	with open("all_corpus2012.pkl","wb") as f:
		pickle.dump(corpus, f)

	# LOAD
	# path = "/Users/ninawang/Thesis/remote/THESIS2019/Jupyter Notebooks/"
	# with open(path+"prelim_model2012.pkl","rb") as f:
	# 	model = pickle.load(f)
	# with open(path+"prelim_dictionary2012.pkl","rb") as f:
	# 	dictionary = pickle.load(f)
	# with open(path+"prelim_articles2012.pkl","rb") as f:
	# 	articles = pickle.load(f)
	# with open(path+"prelim_corpus2012.pkl","rb") as f:
	# 	corpus = pickle.load(f)

	# arts = {"2012":[],"2013":[]}
	# for i,article in enumerate(articles):
	# 	if article.date_publish is not None:
	# 		arts[str(article.date_publish.year)].append(corpus[i])

	# dist2012 = total_topic_distribution(model, arts["2012"])
	# dist2013 = total_topic_distribution(model, arts["2013"])

	# jsd = compare_topic_distributions(dist2012, dist2013)
	# print("JS Divergence of topic: %f"%(jsd))


from django.db import models
from django.utils.translation import gettext_lazy as _
from simple_history.models import HistoricalRecords

READER = 'Lectura en voz alta'
MUSICIAN = 'Interpretación musical'
SOUND_ENGINEER = 'Ingeniería de sonido'
PRODUCTION = 'Producción'
DIRECTION = 'Dirección'
POST_PRODUCTION = 'Post-producción'
AUX = 'Auxiliar de sonido'
CONTRIBUTOR = 'Contribuidor'
PUBLISHER = 'Publicador'
COMPOSER = 'Composición'
TRANSLATOR = 'Traducción'

ENTITY_WORK_ROLE = (
    (COMPOSER, COMPOSER),
    (READER, READER),
    (MUSICIAN, MUSICIAN),
    (SOUND_ENGINEER, SOUND_ENGINEER),
    (PRODUCTION, PRODUCTION),
    (DIRECTION, DIRECTION),
    (POST_PRODUCTION, POST_PRODUCTION),
    (AUX, AUX),
    (CONTRIBUTOR, CONTRIBUTOR),
    (PUBLISHER, PUBLISHER),
    (TRANSLATOR, PUBLISHER),
)

class EntityToWorkRel(models.Model):
    from_entity = models.ForeignKey('Entity', verbose_name=_('From entity'), on_delete=models.CASCADE,
                                    db_column='from_entity', related_name='ew_from_model')
    to_work = models.ForeignKey('Work', verbose_name=_('To recording'), on_delete=models.CASCADE,
                                db_column='to_work', related_name='ew_to_model')

    relationship = models.CharField(max_length=256, verbose_name=_('Role'), choices=ENTITY_WORK_ROLE, default=READER)
    instrument = models.CharField(max_length=256, verbose_name=_('Instrument'), blank=True, null=True)

    # Arbitrary additional information
    commentary = models.TextField(verbose_name=_('Additional commentary'), blank=True, null=True)
    history = HistoricalRecords()

    class Meta:
        managed = True
        db_table = 'poet_entity_to_work_rel'
        verbose_name = _('Entity to recording relationship')


"""

Write a function that repeats the shorter string until it is equal to the
length of the longer string.

### Examples

    lengthen("abcdefg", "ab") ➞ "abababa"

    lengthen("ingenius", "forest") ➞ "forestfo"

    lengthen("clap", "skipping") ➞ "clapclap"

### Notes

  * Both strings will differ in length.
  * Both strings will contain at least one character.
  * Either of the two strings could be the shortest in length.

"""

def lengthen(s1, s2):
  n1 = len(s1)
  n2 = len(s2)
  res = ""
  if n1 < n2:
    res = ( (int(n2 / n1) + 1) * s1)[ : n2]
  else:
    res = ( (int(n1 / n2) + 1) * s2)[ : n1]
  return res


from kubernetes import client,config
from packtivity.asyncbackends import PacktivityProxyBase
from packtivity.syncbackends import packconfig, build_job, publish
from packtivity.statecontexts import load_state
from packtivity import prepublish_default
import logging
import uuid
import os

log = logging.getLogger(__name__)

class KubeProxy(PacktivityProxyBase):
    def __init__(self, job_id, spec, pars, state):
        self.job_id = job_id
        self.spec = spec
        self.pars = pars
        self.state = state

    def proxyname(self):
        return 'KubeProxy'

    def details(self):
        return {
            'job_id': self.job_id,
            'spec': self.spec,
            'pars':self.pars,
            'state':self.state.json()
        }

    @classmethod
    def fromJSON(cls,data):
        return cls(
            data['proxydetails']['job_id'],
            data['proxydetails']['spec'],
            data['proxydetails']['pars'],
            load_state(data['proxydetails']['state'])
        )

class KubeBackend(object):
    def __init__(self,kubeconfigloc = None):
        config.load_kube_config(kubeconfigloc or os.path.join(os.environ['HOME'],'.kube/config'))
        self.config = packconfig()

    def prepublish(self, spec, parameters, state):
        return None

    def submit(self, spec, parameters, state, metadata = None):
        job = build_job(spec['process'], parameters, self.config)

        image   = spec['environment']['image']
        tag     = spec['environment']['imagetag']

        interpreter = 'sh' if 'command' in job else job['interpreter']
        script = job['command'] if 'command' in job else job['script']
        jobspec = job_spec(interpreter, script, image, tag, state)

        jobid = jobspec['metadata']['name']

        thejob = client.V1Job(
            api_version = jobspec['apiVersion'],
            kind = jobspec['kind'],
            metadata = jobspec['metadata'],
            spec = jobspec['spec']
        )

        log.info(thejob)
        client.BatchV1Api().create_namespaced_job('default',thejob)

        log.info('submitted job: %s', jobid)
        return KubeProxy(
            job_id = jobid,
            spec = spec,
            pars = parameters,
            state = state
        )

    def result(self, resultproxy):
        return prepublish_default(
            resultproxy.spec,resultproxy.pars, resultproxy.state
        )

    def ready(self, resultproxy):
        jobstatus = client.BatchV1Api().read_namespaced_job(resultproxy.job_id,'default').status
        return jobstatus.failed or jobstatus.succeeded

    def successful(self, resultproxy):
        jobstatus = client.BatchV1Api().read_namespaced_job(resultproxy.job_id,'default').status
        return jobstatus.succeeded

    def fail_info(self, resultproxy):
        pass

def state_binds(state):
    container_mounts = []
    volumes = []

    log.info('mountspec: %s', state.mountspec)

    for i,path in enumerate(state.readwrite + state.readonly):
        container_mounts.append({
            "name": "cephfs",
            "mountPath": path,
            "subPath": path.lstrip('/')
        })

    volumes.append({
        "name": "cephfs".format(i),
    })
    volumes[0].update(state.mountspec)
    return container_mounts, volumes

def job_spec(interpreter,script,image,imagetag,state):
    wrapped_script = '''cat << EOF | {interpreter}\n{script}\nEOF'''.format(
        interpreter = interpreter,
        script = script)

    job_uuid = str(uuid.uuid4())

    container_mounts, volumes = state_binds(state)

    spec = {
      "apiVersion": "batch/v1",
      "kind": "Job",
      "spec": {
        "template": {
          "spec": {
            "restartPolicy": "Never",
            "containers": [
              {
                "image": ':'.join([image,imagetag]),
                "command": [
                  "sh",
                  "-c",
                  wrapped_script
                ],
                "volumeMounts": container_mounts,
                "name": job_uuid
              }
            ],
            "volumes": volumes
          },
          "metadata": {
            "name": job_uuid
          }
        }
      },
      "metadata": {
        "name": job_uuid
      }
    }
    return spec


class MyCircularDeque:
    # constructor
    def __init__(self, k):
        self._data = [None] * k
        self._maxSize = k;
        self._size = 0;
        self._front = 0
        self._back = k-1

    # insertion methods
    def insertFront(self, x):
        if self._size == self._maxSize:  # queue is full
            return False
        self._data.insert(0, x)
        self._size += 1
        return True
    def insertLast(self, x):
        if self._size == self._maxSize:  # queue is full
            return False
        self._data[self._size] = x
        self._size += 1
        return True

    # deletion methods
    def deleteFront(self):
        if self._size == 0:  # queue is empty
            return False
        self._data.pop(0)
        self._data.append(None)
        self._size -= 1
        return True
    def deleteLast(self):
        if self._size == 0:  # queue is empty
            return False
        self._data[self._size - 1] = [None]
        self._size -= 1
        return True

    # get methods
    def getFront(self):
        if self._size == 0:  # queue is empty
            return -1
        return self._data[0]
    def getRear(self):
        if self._size == 0:  # queue is empty
            return -1
        return self._data[self._size - 1]

    # Check if Deque is full or empty
    def isEmpty(self):
        return self._size == 0
    def isFull(self):
        return not self._size == 0

if __name__ == '__main__':
    circularDeque = MyCircularDeque(3)
    print(circularDeque.insertLast(1))
    print(circularDeque.insertLast(2))
    print(circularDeque.insertFront(3))
    print(circularDeque.insertFront(4))
    print(circularDeque.getRear())
    print(circularDeque.isFull())
    print(circularDeque.deleteLast())
    print(circularDeque.insertFront(4))
    print(circularDeque.getFront())


import sys, os

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from UcsBase import ManagedObject
sys.path.remove(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

class EquipmentFexPowerSummary(ManagedObject):
	def __init__(self):
		ManagedObject.__init__(self,"EquipmentFexPowerSummary")

	@staticmethod
	def ClassId():
		return "equipmentFexPowerSummary"

	_MODULE_POWER = "ModulePower"
	_MODULE_POWER_AVG = "ModulePowerAvg"
	_MODULE_POWER_MAX = "ModulePowerMax"
	_MODULE_POWER_MIN = "ModulePowerMin"
	AVAILABLE_POWER = "AvailablePower"
	AVAILABLE_POWER_AVG = "AvailablePowerAvg"
	AVAILABLE_POWER_MAX = "AvailablePowerMax"
	AVAILABLE_POWER_MIN = "AvailablePowerMin"
	DN = "Dn"
	INTERVALS = "Intervals"
	RESERVED_POWER = "ReservedPower"
	RESERVED_POWER_AVG = "ReservedPowerAvg"
	RESERVED_POWER_MAX = "ReservedPowerMax"
	RESERVED_POWER_MIN = "ReservedPowerMin"
	RN = "Rn"
	STATUS = "Status"
	SUSPECT = "Suspect"
	THRESHOLDED = "Thresholded"
	TIME_COLLECTED = "TimeCollected"
	TOTAL_POWER = "TotalPower"
	TOTAL_POWER_AVG = "TotalPowerAvg"
	TOTAL_POWER_MAX = "TotalPowerMax"
	TOTAL_POWER_MIN = "TotalPowerMin"
	UPDATE = "Update"

	CONST_SUSPECT_FALSE = "false"
	CONST_SUSPECT_NO = "no"
	CONST_SUSPECT_TRUE = "true"
	CONST_SUSPECT_YES = "yes"


from django.conf.urls import patterns, include, url
from django.contrib import admin
from calc import views

urlpatterns = [
    url(r'(-?\d+)([+])(\d+)', views.suma, name="suma"),
    url(r'(-?\d+)([-])(\d+)', views.resta, name="resta"),
    url(r'(-?\d+)([*])(\d+)', views.mult, name="mult"),
    url(r'(-?\d+)([/])(\d+)', views.div, name="div"),
    url(r'(.*)', views.error, name="error")
]


# Dash Application
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output, State

# Data Viz
import plotly.graph_objs as go
from plotly.subplots import make_subplots

# Data Source
import yfinance as yf
#import pickle5 as pickle
import pickle
import pandas as pd

# Global Variable: Ticker List
#with open("data/tickers.pickle", "rb") as f:
#with open("data/twse_otc_id.pickle", "rb") as f:
with open("data/steady_growth.pickle", "rb") as f:
    TICKER_LIST = pickle.load(f)

class Asset:
    """Class to initialize the stock, given a ticker, period and interval"""
    def __init__(self, ticker, period='1y', interval='1d'):
        self.ticker = ticker.upper()
        self.period = period
        self.interval = interval

    def __repr__(self):
        return f"Ticker: {self.ticker}, Period: {self.period}, Interval: {self.interval}"

    def get_info(self):
        """Uses yfinance to get information about the ticker
        returns a dictionary filled with at-point information about the ticker"""
        ticker_info = yf.Ticker(self.ticker).info
        return ticker_info

    def get_data(self):
        """Uses yfinance to get data, returns a Pandas DataFrame object
        Index: Date
        Columns: Open, High, Low, Close, Adj Close, Volume
        """
        try:
            self.data = yf.download(
                tickers=self.ticker,
                period=self.period,
                interval=self.interval)
            return self.data
        except Exception as e:
            return e

class Indicator:
    """Class that takes in a pandas dataframe object and adds trading indicators"""

    def __init__(self, data):
        self.data = data

    def BollingerBands(self, width=1.96, window=21):
        """Default period set at 21 days"""
        self.data['BB Middle'] = self.data['Close'].rolling(window=window).mean()
        self.data['BB Upper'] = self.data['BB Middle'] + width * self.data['Close'].rolling(window=window).std()
        self.data['BB Lower'] = self.data['BB Middle'] - width * self.data['Close'].rolling(window=window).std()

        return self.data

    def MovingAverage(self, period=50):
        """Default is set to 50 days simple moving average"""
        self.ma_period = period
        self.data[f'MA{period}'] = self.data['Close'].rolling(period).mean()

        return self.data

    def MACD(self, period1=12, period2=26, period3=9):
        """Default is set to 12 and 26 exponential moving average for macd
        9 period units for signal"""
        # ewm = exponential weighted mean from pandas
        ema1 = self.data['Close'].ewm(span=period1, adjust=False).mean()
        ema2 = self.data['Close'].ewm(span=period2, adjust=False).mean()
        macd_line = ema1 - ema2
        macd_signal = self.data['Close'].ewm(span=period3, adjust=False).mean()

        self.data['MACD'] = macd_line
        self.data['MACD Signal'] = macd_signal
        self.data['MACD Histogram'] = macd_line - macd_signal

        return self.data

    def RSI(self, period=14):
        """Relative Strength Index (RSI) is an oscillator that measures the rate of change in price
        0 <= RSI <= 100 | if RSI > 70, overbought | if RSI < 30, oversold"""
        # difference in price from previous step
        delta = self.data['Adj Close'].diff()
        up, down = delta.copy(), delta.copy()
        up[up < 0] = 0
        down[down > 0] = 0

        _gain = up.ewm(com=(period - 1), min_periods=period).mean()
        _loss = down.abs().ewm(com=(period - 1), min_periods=period).mean()
        RS = _gain / _loss

        self.data['RSI'] = (100 - (100 / (1 + RS)))
        return self.data

"""
Custom Candlestick Colors
https://plotly.com/python/candlestick-charts/
"""
"""
設定顏色
https://waynestalk.com/python-candlesticks/
"""

class Plotter:
    """Plotter class that takes in a Pandas dataframe object
    returns a Plotly graphic object compatible with Dash apps"""

    def __init__(self, data):
        self.data = data

    def plotCandlestick(self):
        chart = go.Candlestick(
            x=list(self.data.index), open=list(self.data['Open']),
            high=list(self.data['High']), low=list(self.data['Low']),
            close=list(self.data['Close']), name="Candlestick",
            increasing_line_color= 'red', decreasing_line_color= 'green',
            visible=True)
        return chart

    def plotRSI(self):
        chart = go.Scatter(
            x=list(self.data.index), y=list(self.data['RSI']),
            line=dict(color='pink', width=1.5), name="RSI")
        return chart

    def plotSMA(self, period=50):
        chart = go.Scatter(
            x=list(self.data.index), y=list(self.data[f'MA{period}']),
            line=dict(color='orange', width=1.3), name=f'SMA{period}')
        return chart

    def plotBBUpper(self):
        chart = go.Scatter(
            x=list(self.data.index), y=list(self.data['BB Upper']),
            line=dict(color='white', width=1.0), name="Bollinger Upper Band")
        return chart

    def plotBBLower(self):
        chart = go.Scatter(
            x=list(self.data.index), y=list(self.data['BB Lower']),
            line=dict(color='white', width=1.0), name="Bollinger Lower Band")
        return chart

    def plotVolume(self):
        chart = go.Bar(
            x=list(self.data.index), y=list(self.data['Volume']),
            marker_color='lightgrey', name="Volume")
        return chart

class Layout:
    """Handles the layout and front-facing app of Dash application"""

    @classmethod
    def dash_layout(self):
        layout = html.Div(
            children=[
                html.Div(
                    className='row',
                    children=[
                        # Left Tab: Inputs
                        html.Div(
                            className='three columns div-user-controls',
                            children=[

                                # Title
                                html.H1(
                                    'STOCK CHART ANALYZER',
                                    style={'text-align': 'center', 'font-size': '25px'}),
                                html.Br(),
                                html.P('Select Asset:',
                                       style={'font-size': '17px'}),

                                # Dropdown Input
                                dcc.Dropdown(
                                    id="ticker",
                                    options=[
                                        {
                                            "label": str(TICKER_LIST[i]),
                                            "value": str(TICKER_LIST[i]),
                                        }
                                        for i in range(len(TICKER_LIST))
                                    ],
                                    searchable=True,
                                    value="TSLA",
                                    placeholder="Enter Stock Ticker",
                                    style={'color': '#FFFFFF'}
                                ),
                                html.Br(),

                                # Plot Button
                                html.Button(
                                    'PLOT',
                                    style={'color': '#FDFDFD'},
                                    id='plot_button',
                                    n_clicks=1
                                ),
                                html.Br(), html.Br(),

                                # Table info
                                html.P('Summary Statistics:',
                                       style={'font-size': '17px'}),
                                dt.DataTable(
                                    id='info',
                                    data=[],
                                    columns=[{'id': c, 'name': c}
                                             for c in ['Metric', 'Value']],
                                    style_cell={
                                        'textAlign': 'left',
                                        'color': 'black',
                                        'font-size': '15px'},
                                ),

                                # Credits
                                html.Br(), html.Br(), html.Br(), html.Br(),
                                html.Br(), html.Br(), html.Br(), html.Br(),
                                html.P('Frok from Jericho Villareal',
                                       style={'font-size': '17px'}
                                       ),
                                html.P('MIDS W200: '),
                                html.P('~ to the moon! ~',
                                       style={'font-size': '12px'}),
                                html.P('#HODL #DiamondHands #StonksOnlyGoUp',
                                       style={'font-size': '12px'})
                            ]
                        ),
                        # Right Tab: Graphs
                        html.Div(
                            className='nine columns div-for-charts bg-grey',
                            children=[
                                dcc.Graph(
                                    id='graph',
                                    config={'displayModeBar': False},
                                    animate=True
                                )
                            ]
                        )
                    ]
                )
            ]
        )
        return layout

class App:
    """Instantiates a Dash app that is already layouted using the Layout class"""

    @classmethod
    def init_app(cls):

        # Initialize Dash Application
        app = dash.Dash(__name__)
        app.config.suppress_callback_exceptions = True

        # Set up Dash Layout
        layout = Layout().dash_layout()
        app.layout = layout

        return app

class Engine:
    """Engine superclass that calls on:
    - App: initiates a dash application with a Layout object
    - Asset: calls on particular stock ticker from TICKER_LIST
        - get_info(): summary statistics about the stock
        - get_data(): historical price/volume data
    - Indicator: adds various signals and indicator to the stock
        - Bollinger Bands, RSI, MACD
    - Plotter: plots various indicators using Plotly

    This class is responsible for callbacks which enable the app to update lower-level objects
    """

    @classmethod
    def run(cls):
        app = App().init_app()  # important to initialize main Dash app layout

        # Update charts
        @app.callback(
            # Output
            Output("graph", "figure"),
            # Input
            [Input("plot_button", "n_clicks")],
            # State
            [State("ticker", "value")]
        )
        def update_graph(n_clicks, ticker):

            if ticker not in TICKER_LIST:
                raise Exception("Invalid Ticker!")
            else:
                pass

            if n_clicks >= 1:

                # initialize Asset object
                asset = Asset(ticker, period='1y', interval='1d')
                asset_info = asset.get_info()  # Information about the Company
                asset_df = asset.get_data()    # Historical price data

                # Check in terminal for n_clicks and status
                print(f'\nTicker: {ticker} | Clicks: {n_clicks}\n')
                print("Program is running...")

                # add Indicator objects
                asset_df = Indicator(asset_df).BollingerBands()
                asset_df = Indicator(asset_df).MovingAverage()
                asset_df = Indicator(asset_df).RSI()
                asset_df = Indicator(asset_df).MACD()

                # initialize Plotter trace objects
                candlestick = Plotter(asset_df).plotCandlestick()
                sma = Plotter(asset_df).plotSMA()
                bb_upper = Plotter(asset_df).plotBBUpper()
                bb_lower = Plotter(asset_df).plotBBLower()
                rsi = Plotter(asset_df).plotRSI()
                volume = Plotter(asset_df).plotVolume()

                # initialize Dash plot layout,
                figure = make_subplots(
                    shared_xaxes=True, vertical_spacing=0.1,
                    rows=4, cols=1,                 # Partition layout in 4, row-wise
                    specs=[[{"rowspan": 2}],        # first chart will take up rows 1 and 2
                        [None],
                        [{}],                       # second chart will take up row 3
                        [{}]],                      # third chart will take up row 4
                    print_grid=False)

                figure.append_trace(candlestick, 1, 1)    # Plots in rows 1 and 2
                figure.append_trace(sma, 1, 1)
                figure.append_trace(bb_upper, 1, 1)
                figure.append_trace(bb_lower, 1, 1)
                figure.append_trace(rsi, 3, 1)            # Plot in row 2
                figure.append_trace(volume, 4, 1)         # Plot in row 3

                figure.update_xaxes(rangeslider_visible=False)
                #figure.update_yaxes(title_text="Price (USD)", row=1, col=1)
                figure.update_yaxes(title_text="Price (NTD)", row=1, col=1)
                figure.update_yaxes(title_text="RSI", row=3, col=1)
                figure.update_yaxes(title_text="Volume", row=4, col=1)

                figure['layout'].update(
                    height=950, autosize=True, template='plotly_dark',
                    title={

                        'text': f'{asset_info["longName"]} | Last Price: ${asset_df.iloc[-1]["Close"]:.2f}',
                        'font': {'color': 'white', 'size': 30}
                    }
                )

            return figure

        # Update Table
        @app.callback(
            # Output
            [Output("info", "columns"), Output("info", "data")],
            # Input
            [Input("plot_button", "n_clicks")],
            # State
            [State("ticker", "value")]
        )
        def update_table(n_clicks, ticker):
            """Updates the metrics found on asset info"""

            if ticker not in TICKER_LIST:
                raise Exception("Invalid Ticker!")
            else:
                pass

            metric_ls = ["longName", "sector", "industry",
                        "marketCap", "previousClose", "dayHigh", "dayLow"]
            if n_clicks >= 1:
                asset = Asset(ticker, period='1y', interval='1d')
                asset_info = asset.get_info()

                data = [{'Metric': i, 'Value': j}
                        for i, j in asset_info.items() if i in metric_ls]
                data = pd.DataFrame.from_dict(data)
                data = data.reindex([6, 1, 0, 3, 2, 5, 4])
                data = data.to_dict('records')
                columns = [{'id': c, 'name': c} for c in ['Metric', 'Value']]

            return columns, data

        return app

if __name__ == '__main__':
    app = Engine().run()
    app.run_server(debug=True, port=8000, use_reloader=True)
    print("End of Program")


from model import SbertModel
from pytorch_lightning import Trainer
from data import SbertDataModule
import hparams
from pytorch_lightning import loggers as pl_loggers

'''
How sbert_sub is trained.
For approximately 3000 steps, the discriminator is trained without the generator. This is because the generator is
initialized in a good position while the discriminator is not. Done by adjusting the frequency to 1, 0 in model.py
After this, the model trains as normal. Saving every epoch is suggested. On epoch end swords validation scores are saved
'''

model = SbertModel(alpha=hparams.ALPHA)
# model = SbertModel.load_from_checkpoint('sbert_sub.ckpt')
model.alpha = hparams.ALPHA

summary_data = SbertDataModule('data', batch_size=hparams.BATCH_SIZE)

tb_logger = pl_loggers.TensorBoardLogger('logs/')

trainer = Trainer(
    gpus=[1],
    accelerator='dp',
    auto_select_gpus=True,
    max_epochs=hparams.MAX_EPOCHS,
    deterministic=True,
    logger=tb_logger,
)

trainer.fit(model, summary_data)
# trainer.save_checkpoint("sbert_sub.ckpt")


#!/usr/bin/python
# coding: utf-8
"""
"""

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        self.say1()

    def say1(self):
        for i in range(10):
            time.sleep(1)
            print('hh', i)

class MyThread1(threading.Thread):
    def run(self):
        self.say1()

    def say1(self):
        for i in range(10):
            time.sleep(1)
            print('zz', i)

if __name__ == '__main__':
    t1 = MyThread()
    t1.start()  # 调用start时, 调用的是run方法
    t2 = MyThread1()
    t2.start()

"""
 |  run(self)
 |      Method representing the thread's activity.
 |
 |      You may override this method in a subclass. The standard run() method
 |      invokes the callable object passed to the object's constructor as the
 |      target argument, if any, with sequential and keyword arguments taken
 |      from the args and kwargs arguments, respectively.

"""

"""
start(self)
    Start the thread's activity.

    It must be called at most once per thread object. It arranges for the
    object's run() method to be invoked in a separate thread of control.

    This method will raise a RuntimeError if called more than once on the
"""

多任务/线程/继承Thread类完成多线程.py


import time,sys,os
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse, Gather
from directline import DirectLineAPI as DL
from utils.twilio import buildTwilioResponse

app = Flask(__name__)
converations = {}

@app.route("/", methods=['GET'])
def index():
    return "Twilio-Docker-Bot"

@app.route("/answer", methods=['GET', 'POST'])
def answer_call():
    caller = request.form.get('Caller')
    bot = DL(caller);
    converations[caller] = bot
    return str(buildTwilioResponse("Hello! welcome to NTT service desk. How may I help?"))

@app.route("/gather", methods=['POST'])
def gather():
    user_speech = request.form.get('SpeechResult',None)
    input_digits = request.form.get('Digits', None)
    data = input_digits if input_digits else user_speech
    caller = request.form.get('Caller')
    bot = converations[caller]
    bot._lastAccessed = time.time()
    bot.send(data)
    print(str(time.time()) + ' - ' + caller + ': ' + data)
    return str(buildTwilioResponse(text=None, waitForResponse=True));

@app.route("/gather/dtmf", methods=['POST'])
def gather_dtmf():
    pass

@app.route("/realtimevoice", methods=['get'])
def realTimeVoice():
    print(request.values);
    return str(request.values)

@app.route('/wait/<ntry>', methods=['POST'])
def waitForBotRespsonse(ntry=1):
    caller = request.form.get('Caller')
    bot = converations[caller]
    if (int(ntry)>7):
        return str(buildTwilioResponse('Sorry I could not reply from the application', hangUp=True))
    msg = bot.get()
    bot._lastAccessed = time.time()
    loop = time.time()
    if not msg:
        while time.time() - loop <= 10:
            try:
                msg=bot.get()
                if msg:
                    break
                time.sleep(.5)
            except Exception as e:
                print(str(e))
                pass
    bot._lastAccessed = time.time()
    if not msg:
        resp = VoiceResponse()
        url = '/wait/' + str(int(ntry)+1)
        resp.redirect(url, method='POST');
        return str(resp)
    else:
        return str(buildTwilioResponse(msg.get('msgs'), msg.get('endOfConversation'), msg.get('waitForResponse'),
                    msg.get('routeToMainMenu'), hangUp=msg.get('hangUp'), dtmf=msg.get('dtmf')))

if __name__ == "__main__":
    app.run('0.0.0.0', port=80,debug=True)