# -*- coding: UTF-8 -*-

import simplejson
from jukebox_core.tests.api import ApiTestBase

class ApiFavouritesTest(ApiTestBase):
    def testIndexEmpty(self):
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/favourites"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 0)

    def testAdd(self):
        artist = self.addArtist()
        song = self.addSong(artist)

        # check that song is not a favourite
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/songs"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)
        self.assertFalse(result["itemList"][0]["favourite"])

        # add to favourites
        response = self.httpPost(
            "/api/v1/favourites",
            {"id": song.id}
        )
        content = simplejson.loads(
            response.content
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(content["id"], song.id)

        # check favourites list
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/favourites"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)

        # check that song is a favourite
        result = simplejson.loads(
            self.httpGet(
                "/api/v1/songs"
            ).content
        )
        self.assertEquals(len(result["itemList"]), 1)
        self.assertEquals(result["itemList"][0]["id"], song.id)
        self.assertTrue(result["itemList"][0]["favourite"])


# Copyright 2014 Google Inc. All Rights Reserved.

# 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.

"""A logger for handling OpenHTF specific log mechanisms.

This file defines a logging.LoggerAdapter and logging.Handler, see below.

Any module can call logging.getLogger('htf.something') and it will by default
show up in the HTF output.
"""

import logging
import os
import re
import sys
import traceback
from datetime import datetime

import gflags

from openhtf.io import test_record

FLAGS = gflags.FLAGS

gflags.DEFINE_enum('verbosity',
                   'warning', ['debug', 'info', 'warning', 'error', 'critical'],
                   'Console verbosity level.')
gflags.DEFINE_boolean('quiet', False, '')
gflags.DEFINE_string('log_file', '', 'log files')
gflags.DEFINE_enum('log_level', 'warning', ['debug', 'info', 'warning', 'error',
                                            'critical'],
                   'Logging verbosity level.')

LOGGER_PREFIX = 'openhtf'

class MacAddressLogFilter(logging.Filter):  # pylint: disable=too-few-public-methods
  """A filter which redacts mac addresses if it sees one."""

  MAC_REPLACE_RE = re.compile(r"""
        ((?:[\dA-F]{2}:){3})       # 3-part prefix, f8:8f:ca means google
        (?:[\dA-F]{2}(:|\b)){3}    # the remaining octets
        """, re.IGNORECASE | re.VERBOSE)
  MAC_REPLACEMENT = r'\1<REDACTED>'

  def __init__(self):
    super(MacAddressLogFilter, self).__init__()

  def filter(self, record):
    if self.MAC_REPLACE_RE.search(record.getMessage()):
      # Update all the things to have no mac address in them
      record.msg = self.MAC_REPLACE_RE.sub(self.MAC_REPLACEMENT, record.msg)
      record.args = tuple([
          self.MAC_REPLACE_RE.sub(self.MAC_REPLACEMENT, str(arg))
          if isinstance(arg, basestring)
          else arg for arg in record.args])
    return True

# We use one shared instance of this, it has no internal state.
MAC_FILTER = MacAddressLogFilter()

class RecordHandler(logging.Handler):
  """A handler to save logs to an HTF TestRecord."""

  def __init__(self, test_record):
    super(RecordHandler, self).__init__(level=logging.DEBUG)
    self._test_record = test_record
    self.addFilter(MAC_FILTER)

  def emit(self, record):
    """Save a logging.LogRecord to our test record.

    LogRecords carry a significant amount of information with them including the
    logger name and level information.  This allows us to be a little clever
    about what we store so that filtering can occur on the client.

    Args:
      record: A logging.LogRecord to log.
    """
    message = record.getMessage()
    if record.exc_info:
      message += '\n' + ''.join(traceback.format_exception(
          *record.exc_info))
    message = message.decode('utf8', 'replace')

    log_record = test_record.LogRecord(
        record.levelno, record.name, os.path.basename(record.pathname),
        record.lineno, int(record.created * 1000), message
    )
    self._test_record.log_records.append(log_record)

def setup_logger():
  """Configure logging for OpenHTF based on command line flags."""
  logger = logging.getLogger('openhtf')
  logger.propagate = False
  logger.setLevel(logging.DEBUG)
  formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

  if FLAGS.log_file:
    try:
      cur_time = datetime.utcnow().strftime('%Y-%m-%d-%H:%M:%S.%f')[:-3]
      file_handler = logging.FileHandler(FLAGS.log_file + cur_time)
      file_handler.setFormatter(formatter)
      file_handler.setLevel(FLAGS.log_level.upper())
      file_handler.addFilter(MAC_FILTER)
      logger.addHandler(file_handler)
    except IOError as exception:
      print ('Failed to set up log file due to error: %s. '
             'Continuing anyway.' % exception)

  if not FLAGS.quiet:
    console_handler = logging.StreamHandler(stream=sys.stdout)
    console_handler.setFormatter(formatter)
    console_handler.setLevel(FLAGS.verbosity.upper())
    console_handler.addFilter(MAC_FILTER)
    logger.addHandler(console_handler)


import os
import sys
from getopt import getopt
import csv

import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

logger = logging.getLogger("prepare-ePrice")

##########################################################################
### usage
##########################################################################
def usage(msg):
   if msg=='0':
      print("=========================")
      print("ERRORE: %s" % 'Missing variabile in input')
      print("=========================")
#   print( "Usage: %s -i  input-file\n" % sys.argv[0]
   print("Usage: %s -f file in input" % sys.argv[0])
   print("Usage: %s -o file output" % sys.argv[0])
   print("Usage: %s -h help\n" % sys.argv[0])
   print("Example :-f  ePRICE_20160727.txt -o $ ePRICE_20160727-Cassandra.txt\n"%sys.argv[0])
   raise SystemExit

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

class DictCreation:
    """Class to prepare create dict from line"""

    type= 'ePrice_dict'

    def __init__(self,line):
        self.line=line
        self.dict_line={}
        self.dict_rearr={}

    def createDict(self):
        self.splitted=self.line.split(";")
        self.dict_line['code']=self.splitted[0].split("^")[0]
        for item in self.splitted[1:]:
            try:
                self.dict_line[item.split(":")[0]]=item.split(":")[1].strip("\n")
            except IndexError:
                print(item)
        return self.dict_line

class CheckFileds:
    """Class to check if there are missing fileds"""

    type= 'ePrice_fileds'

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

    def checkFileds_dict(self):
        self.Fields_list=['code','sesso']
        for field in self.Fields_list:
            if field not in self.dict_line:
                self.dict_line[field]=''
        return self.dict_line

def main(inFile,outFile):
    file_out=open(outFile, 'w',encoding='utf-8')
    first=0
    with open(inFile,'r', encoding='mac_roman') as file_in:
        for line in file_in:
            dict_line_ob=DictCreation(line)
            dict_line=dict_line_ob.createDict()
            dict_line_comp_ob=CheckFileds(dict_line)
            dict_line_comp=dict_line_comp_ob.checkFileds_dict()
            w = csv.DictWriter(file_out, dict_line_comp.keys())
            if first==0:
                w.writeheader()
                first=1
            w.writerow(dict_line_comp)

if __name__=="__main__":
   opts, args = getopt(sys.argv[1:], "f:o:h")
   opts = dict(opts)
   inFile=None
   outFile=None
   #for opt, arg in opts:
   if '-f' in opts:
      inFile=str(opts['-f'])
   if '-o' in opts:
      outFile=str(opts['-o'])
   if '-h' in opts:
      usage('msg')
   if ('-f' not in opts==True and '-o' not in opts==True and '-h' not in opts==True):
       usage('0')
   main(inFile,outFile)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from vizdoom import *
import itertools as it
import pickle
from random import sample, randint, random
from time import time, sleep
import numpy as np
import skimage.color, skimage.transform
import tensorflow as tf
from tqdm import trange

# 러닝레이트를 세팅합니다
learning_rate = 0.0025
discount_factor = 0.99

#epoch를 0으로하면 바로 화면을 볼 수 있습니다.
epochs = 0
learning_steps_per_epoch = 2000

#메모리 사이즈를 정합니다
replay_memory_size = 10000

# 배치사이즈 부분입니다
batch_size = 64

# 테스트할때 에피소드를 정합니다
test_episodes_per_epoch = 100

frame_repeat = 12
resolution = (30, 45)
episodes_to_watch = 10

#세이브 로드설정
save_model = False
load_model = True
skip_learning = False

# config파일과 세이브로드파일경로를 설정합니다.
config_file_path = "../../scenarios/rocket_basic.cfg"
model_savefile = "../../tmp/rocket.ckpt"

# 비즈둠의 환경을 설정합니다. config파일을 넣으면 액션과 환경을 자동으로 설정해줍니다.
def initialize_vizdoom(config_file_path):
    print("Initializing doom...")
    game = DoomGame()
    game.load_config(config_file_path)
    game.set_window_visible(True)
    game.set_mode(Mode.PLAYER)
    game.set_screen_format(ScreenFormat.GRAY8)
    game.set_screen_resolution(ScreenResolution.RES_640X480)
    game.init()
    print("Doom initialized.")
    return game

# Converts and down-samples the input image
def preprocess(img):
    img = skimage.transform.resize(img,resolution,mode='constant')
    img = img.astype(np.float32)
    return img

#메모리 저장용 함수

class ReplayMemory:
    def __init__(self, capacity):
        channels = 1
        state_shape = (capacity, resolution[0], resolution[1], channels)
        self.s1 = np.zeros(state_shape, dtype=np.float32)
        self.s2 = np.zeros(state_shape, dtype=np.float32)
        self.a = np.zeros(capacity, dtype=np.int32)
        self.r = np.zeros(capacity, dtype=np.float32)
        self.isterminal = np.zeros(capacity, dtype=np.float32)

        self.capacity = capacity
        self.size = 0
        self.pos = 0

    def add_transition(self, s1, action, s2, isterminal, reward):
        self.s1[self.pos, :, :, 0] = s1
        self.a[self.pos] = action
        if not isterminal:
            self.s2[self.pos, :, :, 0] = s2
        self.isterminal[self.pos] = isterminal
        self.r[self.pos] = reward

        self.pos = (self.pos + 1) % self.capacity
        self.size = min(self.size + 1, self.capacity)

    def get_sample(self, sample_size):
        i = sample(range(0, self.size), sample_size)
        return self.s1[i], self.a[i], self.s2[i], self.isterminal[i], self.r[i]

#네트워크 만들기
class DQN:
    def __init__(self,session, available_actions_count):
        self.imageIn = tf.placeholder(tf.float32, [None] + list(resolution) + [1], name="State")
        self.target_q_ = tf.placeholder(tf.float32, [None, available_actions_count], name="TargetQ")

        self.keep_prob=tf.placeholder(tf.float32)

        # 2개의convolutional 레이어 추가
        self.conv1 = tf.contrib.layers.convolution2d(self.imageIn, num_outputs=8, kernel_size=[6, 6], stride=[3, 3],
                                                activation_fn=tf.nn.relu,
                                                weights_initializer=tf.contrib.layers.xavier_initializer_conv2d(),
                                                biases_initializer=tf.constant_initializer(0.1))
        self.conv2 = tf.contrib.layers.convolution2d(self.conv1, num_outputs=8, kernel_size=[3, 3], stride=[2, 2],
                                                activation_fn=tf.nn.relu,
                                                weights_initializer=tf.contrib.layers.xavier_initializer_conv2d(),
                                                biases_initializer=tf.constant_initializer(0.1))
        self.conv2_flat = tf.contrib.layers.flatten(self.conv2)

        # Fully Connected 레이어입니다
        self.FC1 = tf.contrib.layers.fully_connected(self.conv2_flat, num_outputs=128, activation_fn=tf.nn.relu,
                                                weights_initializer=tf.contrib.layers.xavier_initializer(),
                                                biases_initializer=tf.constant_initializer(0.1))

        self.FC1=tf.nn.dropout(self.FC1,keep_prob=self.keep_prob)

        #액션의 갯수만큼 q아웃풋이 나옵니다.
        self.q = tf.contrib.layers.fully_connected(self.FC1, num_outputs=available_actions_count, activation_fn=None,
                                              weights_initializer=tf.contrib.layers.xavier_initializer(),
                                              biases_initializer=tf.constant_initializer(0.1))

        #q아웃풋에서 액션을 결정
        self.best_a = tf.argmax(self.q, 1)

        #loss를 계산합니다
        self.loss = tf.contrib.losses.mean_squared_error(self.q, self.target_q_)
        self.loss_summ = tf.summary.scalar('loss',self.loss)

        #Optimier과정
        self.optimizer = tf.train.RMSPropOptimizer(learning_rate)

        # loss를 줄입니다
        self.train_step = self.optimizer.minimize(self.loss)

    def function_learn(self,s1, target_q):
        self.feed_dict = {self.imageIn: s1, self.target_q_: target_q, self.keep_prob: 1}
        s,l, _ = session.run([summary,self.loss, self.train_step], feed_dict=self.feed_dict)
        writer.add_summary(s, global_step=epoch)
        return l

    def function_get_q_values(self,state):
        return session.run(self.q, feed_dict={self.imageIn: state, self.keep_prob: 1})

    def function_get_best_action(self,state):
        return session.run(self.best_a, feed_dict={self.imageIn: state, self.keep_prob: 1})

    def function_simple_get_best_action(self,state):
        return self.function_get_best_action(state.reshape([1, resolution[0], resolution[1], 1]))[0]

        #학습함수, Q값, 행동반환

#메모리에서 자료를 가져와 학습하는 함수입니다. 밑에 perform_learning_step에서 쓰입니다.
def learn_from_memory():

    # 배치사이즈만큼 차면 러닝을 시작합니다.
    if memory.size > batch_size:
        s1, a, s2, isterminal, r = memory.get_sample(batch_size)

        q2 = np.max(mainDQN.function_get_q_values(s2), axis=1)
        target_q = mainDQN.function_get_q_values(s1)

        # Q값을 계산하고 업데이트합니다
        target_q[np.arange(target_q.shape[0]), a] = r + discount_factor * (1 - isterminal) * q2
        mainDQN.function_learn(s1, target_q)

#학습을 수행하는 함수입니다.
def perform_learning_step(epoch):

    #입실론값을 설정합니다
    def exploration_rate(epoch):
        start_eps = 1.0
        end_eps = 0.1
        const_eps_epochs = 0.1 * epochs  # 10% of learning time
        eps_decay_epochs = 0.6 * epochs  # 60% of learning time

        if epoch < const_eps_epochs:
            return start_eps
        elif epoch < eps_decay_epochs:
            return start_eps - (epoch - const_eps_epochs) / \
                               (eps_decay_epochs - const_eps_epochs) * (start_eps - end_eps)
        else:
            return end_eps

    s1 = preprocess(game.get_state().screen_buffer)

    # 랜덤한 행동을 할지 결정합니다
    eps = exploration_rate(epoch)
    if random() <= eps:
        a = randint(0, len(actions) - 1)
    else:
        # 메인DQN에서 행동을 택합니다
        a = mainDQN.function_simple_get_best_action(s1)
    reward = game.make_action(actions[a], frame_repeat)

    isterminal = game.is_episode_finished()
    s2 = preprocess(game.get_state().screen_buffer) if not isterminal else None

    # 메모리에 넣어줍니다.
    memory.add_transition(s1, a, s2, isterminal, reward)
    #메모리에서 자료를 꺼내 러닝합니다
    learn_from_memory()

#메인 함수입니다.
if __name__ == '__main__':
    # 둠 초기설정
    game = initialize_vizdoom(config_file_path)

    # 가능한 버튼갯수, 액션을 받습니다.
    nOfAction = game.get_available_buttons_size()
    actions = [list(a) for a in it.product([0, 1], repeat=nOfAction)]

    #저장할 메모리를 만듭니다
    memory = ReplayMemory(replay_memory_size)

    #세션을 엽니다
    session = tf.Session()
    mainDQN = DQN(session,len(actions))
    saver = tf.train.Saver()

    summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter('./logs')
    writer.add_graph(session.graph)

    #모델 로드
    if load_model:
        print("Loading model from: ", model_savefile)
        saver.restore(session, model_savefile)
    else:
        init = tf.initialize_all_variables()
        session.run(init)
    print("Starting the training!")

    time_start = time()
    if not skip_learning:

        #epoch시작
        for epoch in range(epochs):
            print("\nEpoch %d\n-------" % (epoch + 1))
            train_episodes_finished = 0
            train_scores = []

            print("Training...")
            game.new_episode()

            #epoch당 반복되는 횟수를 받아 반복합니다
            for learning_step in trange(learning_steps_per_epoch):
                episode_buff=[]
                perform_learning_step(epoch)
                if game.is_episode_finished():
                    score = game.get_total_reward()
                    train_scores.append(score)
                    game.new_episode()
                    train_episodes_finished += 1

            print("%d training episodes played." % train_episodes_finished)

            train_scores = np.array(train_scores)

            print("Results: mean: %.1f±%.1f," % (train_scores.mean(), train_scores.std()),
                  "min: %.1f," % train_scores.min(), "max: %.1f," % train_scores.max())

            print("\nTesting...")

            test_episode = []
            test_scores = []
            for test_episode in trange(test_episodes_per_epoch):
                game.new_episode()
                while not game.is_episode_finished():
                    state = preprocess(game.get_state().screen_buffer)
                    best_action_index = mainDQN.function_simple_get_best_action(state)

                    game.make_action(actions[best_action_index], frame_repeat)
                r = game.get_total_reward()
                test_scores.append(r)

            test_scores = np.array(test_scores)
            print("Results: mean: %.1f±%.1f," % (
                test_scores.mean(), test_scores.std()), "min: %.1f" % test_scores.min(),
                  "max: %.1f" % test_scores.max())

            print("Saving the network weigths to:", model_savefile)
            saver.save(session, model_savefile)

            print("Total elapsed time: %.2f minutes" % ((time() - time_start) / 60.0))

    game.close()
    print("======================================")
    print("Training finished. It's time to watch!")

    # 최종 학습결과를 보여주는 부분입니다
    game.set_window_visible(True)
    game.set_mode(Mode.ASYNC_PLAYER)
    game.init()

    for _ in range(episodes_to_watch):
        game.new_episode()
        while not game.is_episode_finished():
            state = preprocess(game.get_state().screen_buffer)
            best_action_index = mainDQN.function_simple_get_best_action(state)

            game.set_action(actions[best_action_index])
            for _ in range(frame_repeat):
                game.advance_action()

        sleep(1.0)
        score = game.get_total_reward()
        print("Total score: ", score)


# -*- coding: utf-8 -*-
"""
Created on Wed Jan 28 12:14:18 2015

@author: dsing001
"""

import twitter
import json
import sys
import csv
import datetime
import pymongo
import sys
import re
import time
import HTMLParser
import itertools
import string
import collections
from collections import defaultdict
import numpy
import math

html_parser = HTMLParser.HTMLParser()

from pymongo import MongoClient

def cosine_sim(tfidf):
    dot_mat = numpy.dot(tfidf,numpy.transpose(tfidf))
    mag = numpy.divide(1,numpy.sqrt(numpy.sum(numpy.square(tfidf),axis=1)))
    sim_mat = numpy.multiply(numpy.transpose(numpy.multiply(dot_mat,mag)),mag)
    return sim_mat

def tf(wrds,doc_wrds,normalize=False):
    #create the matrix with tweets as rows and wrds as columns
    numofrows = len(doc_wrds)
    numofcols  = len(wrds)
    #print (numofrows,numofcols)
    tf_table = numpy.zeros(numofrows*numofcols).reshape(numofrows,numofcols)
    #print (type(wrds))
    for i,wrdsindoc in enumerate(doc_wrds):
        #print (i,wrds)
        for word in wrdsindoc:
            #print (word)
            colpos = wrds[word]
            tf_table[i][colpos]= tf_table[i][colpos] + 1

            #print (i,colpos,word)
    if normalize:
        tf_table_row_sums =numpy.sum(tf_table,axis=1)
        tf_table = numpy.transpose(numpy.divide(numpy.transpose(tf_table),tf_table_row_sums))

    return tf_table

        #break
def idf (tftbl):
    tftbl = tftbl > 0.1
    idf = numpy.sum(tftbl,axis=0)
    idf = numpy.log10(numpy.divide(tftbl.shape[0],idf))
    return (idf)

def tf_idf (wrds,doc_wrds,normalize= False):
    tftbl = tf(wrds,doc_wrds,normalize)
    idf_mat = idf(tftbl)
    # Now calculate the tf-idf value
    tfidf = numpy.multiply(tftbl,idf_mat)
    return (tfidf)

clt = MongoClient()
db = clt['task3']
coll = db.twt
cnt = 1
twt_ids = []
word_matrix = []
all_wrds = defaultdict(int)
htp = re.compile(r'(:|\\|//|\.|,|;|\||!|/|>|"|\'|\|=|-|£|$|€|“|”|\?|~|`|\)|\(|\+|\*|%)')
table = string.maketrans("","")

pos = 0
for twts in coll.find({"lang" : "en"},{'text' : 1}).limit(20):
    twt_txt = twts['text']
    twt_txt = html_parser.unescape(twt_txt).lower().replace('\n',' ').replace('\r\n',' ').replace('\t',' ').strip(' ')
    twt_txt = htp.sub(' ',twt_txt)
    #remove any non ascii charatcers
    twt_txt = twt_txt.encode('utf-8').decode('ascii','ignore')
    '''
    if cnt ==1:
        twt_txt = 'data mining is good than machine learning'
    if cnt ==2:
        twt_txt = 'machine learning  is good than data mining'
    print (twt_txt)
    '''
    wrds = twt_txt.split(' ')
    wrd = []
    for word in wrds:
        word=re.sub(r'\s+',' ',word)
        word= word.strip(' ')
        word = re.sub(r'\d+','',word)
        if len(word) > 1:
            if not(re.match(r'^http|@|#|rt|\s+|\n',word)):
                #word = word.translate(string.punctuation)
                wrd.append(word)
                all_wrds[word] = 1

    word_matrix.append(wrd)
    twt_ids.append(twts['_id'])
    cnt = cnt + 1
    #if cnt > 2:
    #    break

for i,wrd in enumerate(sorted(all_wrds.keys())):
    all_wrds[wrd] = i

#print (all_wrds)

tfidf = tf_idf(all_wrds,word_matrix)
#print (tfidf)
sim_mat = cosine_sim(tfidf)
#print ('sim')
#print (sim_mat)

#find the tweets which are similar to each other..


import numpy as np

from keras.models import Model, Sequential, load_model
from keras.layers import Dense, CuDNNLSTM, Input, Concatenate, Dropout
import keras

def load_model(location=None):

	if(location != None):
		model = keras.models.load_model(location)
		print("Loaded the model.")
		return model

	X = Input(shape = (10000, 6,))
	#X_gender = Input(shape = (1,))

	Y = CuDNNLSTM(16, name = 'fully_shared_LSTM_1', return_sequences = True)(X)
	Y = CuDNNLSTM(13, name = 'fully_shared_LSTM_2')(Y)

	Y = Dropout(rate = 3/13)(Y)

	#Y = Concatenate(axis = -1)([Y, X_gender])

	YR = Dense(8, activation = 'relu', name = 'regression_dense_1')(Y)
	YR = Dropout(rate = 2/8)(YR)

	YR = Dense(1, activation = None, name = 'HP_regression')(YR)

	YC = Dense(8, activation = 'relu', name = 'classification_dense_1')(Y)
	YC = Dropout(rate = 2/8)(YC)

	YC = Dense(5, activation = 'softmax', name = 'HP_classification')(YC)

	model = Model(inputs = X, outputs = [YR, YC])

	print("Created a new model.")

	return model


from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404, redirect
from .models import Post, Comment

def post_list(request):
    posts = Post.objects.all()
    search = request.GET.get('search', '')
    if search:
        posts = posts.filter(how_many=search)

    paginator = Paginator(posts, 3)
    page = request.GET.get('page')
    endPage = paginator.page_range
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    return render(request, 'post_list.html', {'posts':posts, 'search':search, 'endPage':endPage})

@login_required
def my_post_list(request):
    posts = Post.objects.filter(creator=request.user)
    return render(request, 'my_post_list.html', {'posts':posts})

@login_required
def post_detail(request, post_id):
    post_detail = get_object_or_404(Post, id=post_id)
    request_user = request.user
    comments = post_detail.comments.all()
    return render(request, 'post_detail.html',{'post':post_detail, 'user':request_user, 'comments':comments})

@login_required
def post_new(request):
    user = request.user
    if request.method == 'GET':
        return render(request,'post_new.html')
    elif request.method == 'POST' and user.is_authenticated:
        post= Post()
        post.title = request.POST['title'] # 제목저장
        post.text = request.POST['text'] # 본문저장
        post.how_many = request.POST['how_many'] # 몇명 저장
        post.creator = user #유저 정보 저장
        post.save()
        return redirect('/posts/detail/' + str(post.id))
    else:
        return render(request, 'login.html')

@login_required
def post_delete(request, post_id):
    post_to_delete = get_object_or_404(Post, pk=post_id)
    post_to_delete.delete()
    return redirect('/posts/myposts/')

@login_required
def post_update(request, post_id):
    user = request.user
    if request.method == 'GET':
        post = get_object_or_404(Post, id = post_id)
        return render(request,'post_new.html', {'post':post})
    elif request.method == 'POST':
        post = get_object_or_404(Post, id=post_id)
        post.title = request.POST['title']
        post.text = request.POST['text']
        post.how_many = request.POST['how_many']
        post.creator = request.user
        post.save()
        return redirect('/posts/detail/' + str(post.id))

@login_required
def post_comment_create(request, post_id):
    user = request.user
    if request.method=='POST' and user.is_authenticated:
        post = get_object_or_404(Post, id=post_id)
        comment = Comment()
        comment.post = post
        comment.creator = user
        comment.message = request.POST['message']
        comment.save()
        return redirect('/posts/detail/' + str(post.id))
    else:
        return render(request, 'login.html')

@login_required
def post_comment_delete(request, comment_id):
    comment_to_delete = get_object_or_404(Comment, id=comment_id)
    post_id = comment_to_delete.post.id
    comment_to_delete.delete()
    return redirect('/posts/detail/' + str(post_id))

def index_return(request):
    return render(request, 'index.html')

def develop(request):
    return render(request, 'developer.html')


from constants import *
from vk_parser import VkParser
from py2neo import Graph, Node, Relationship

def main():
    parser = VkParser(DRIVER_PATH)
    parser.login(LOGIN, PASSWORD)
    friends_ids = list(parser.get_friends_list(START_ID))
    edges = {START_ID: friends_ids}
    for id in friends_ids:
        edges[id] = []
        for friend_id in parser.get_friends_list(id):
            if friend_id in friends_ids:
                edges[id] += [friend_id]
    nodes = {}
    g = Graph()
    tx = g.begin()
    for id in friends_ids:
    	node = Node("Person", id=id)
    	nodes[id] = node
    for id in friends_ids:
    	for friend in nodes[id]:
    		edge = Relationship(nodes[id], "FRIENDSHIP", nodes[friend])
    		tx.create(edge)
    		tx.commit()

main()


#!/usr/bin/env python
import sys
sys.path.append('.')
from datetime import datetime

import pandas as pd
import yaml

from chartlib import CovidChart, DaysSinceNumReached

def chart_configs():
    return [
        {
            'name': 'jhu_country',
            'embed_id': 'country_vis',
            'gen': make_jhu_country_chart,
        },
        {
            'name': 'jhu_country_death',
            'embed_id': 'country_death_vis',
            'gen': make_jhu_country_death_chart,
        },
        {
            'name': 'jhu_state',
            'embed_id': 'state_vis',
            'gen': make_jhu_state_chart,
        },
        {
            'name': 'jhu_state_death',
            'embed_id': 'state_death_vis',
            'gen': make_jhu_state_death_chart,
        },
    ]

def make_jhu_country_chart() -> CovidChart:
    jhu_df = pd.read_csv('./data/jhu-data.csv')
    jhu_df = jhu_df[(jhu_df.Province_State.isnull()) & (jhu_df.Country_Region != 'China')]

    days_since = 50
    chart = CovidChart(
        jhu_df,
        groupcol='Country_Region',
        start_criterion=DaysSinceNumReached(days_since, 'Confirmed'),
        ycol='Confirmed',
        chart_type='country',
        xcol='Date',
        top_k_groups=20,
        quarantine_df='./data/quarantine-activity.csv'  # should have a column with same name as `groupcol`
    )

    # chart.set_colormap()
    chart.set_unfocused_opacity(0.05)
    chart = chart.set_ytitle('Number of Confirmed Cases (log)')
    chart = chart.set_xtitle('Days since {} Confirmed'.format(days_since))
    chart.set_width(600).set_height(400)
    chart.set_ydomain((days_since, 200000))
    chart.set_xdomain((0, 40))
    return chart

def make_jhu_country_death_chart() -> CovidChart:
    jhu_df = pd.read_csv("./data/jhu-data.csv")
    jhu_df = jhu_df.loc[(jhu_df.Country_Region != 'China') & jhu_df.Province_State.isnull()]

    chart = CovidChart(
        jhu_df,
        groupcol='Country_Region',
        start_criterion=DaysSinceNumReached(10, 'Deaths'),
        ycol='Deaths',
        xcol='Date',
        chart_type='country',
        top_k_groups=20,
        quarantine_df='./data/quarantine-activity.csv'  # should have a column with same name as `groupcol`
    )

    chart = chart.set_ytitle('Number of Deaths (log)')
    chart = chart.set_xtitle('Days since 10 Deaths')
    chart.set_width(600).set_height(400)
    chart.set_ydomain((10, 10000))
    chart.set_xdomain((0, 35)).compile()
    return chart

def make_jhu_state_death_chart() -> CovidChart:
    jhu_df = pd.read_csv('./data/jhu-data.csv')
    jhu_df = jhu_df.loc[(jhu_df.Country_Region == 'United States') & jhu_df.Province_State.notnull()]

    chart = CovidChart(
        jhu_df,
        groupcol='Province_State',
        start_criterion=DaysSinceNumReached(10, 'Deaths'),
        ycol='Deaths',
        xcol='Date',
        chart_type='usa',
        top_k_groups=20,
        quarantine_df='./data/quarantine-activity-us.csv' # should have a column with same name as `groupcol`
    )

    chart = chart.set_ytitle('Number of Deaths (log)')
    chart = chart.set_xtitle('Days since 10 Deaths')
    chart.set_width(600).set_height(400)
    chart.set_ydomain((10, 10000))
    chart.set_xdomain((0, 25)).compile()
    return chart

def make_jhu_state_chart() -> CovidChart:
    jhu_df = pd.read_csv('./data/jhu-data.csv')
    # grab us-specific
    jhu_df = jhu_df[(jhu_df.Country_Region == 'United States') & jhu_df.Province_State.notnull()]

    days_since = 20
    chart = CovidChart(
        jhu_df,
        groupcol='Province_State',
        start_criterion=DaysSinceNumReached(days_since, 'Confirmed'),
        ycol='Confirmed',
        chart_type='USA',
        xcol='Date',
        top_k_groups=20,
        quarantine_df='./data/quarantine-activity-US.csv'  # should have a column with same name as `groupcol`
    )
    # chart.set_colormap()
    chart.set_unfocused_opacity(0.05)
    chart = chart.set_ytitle('Number of Confirmed Cases (log)')
    chart = chart.set_xtitle('Days since {} Confirmed'.format(days_since))
    chart.set_width(600).set_height(400)
    chart.set_xdomain((0, 30)).set_ydomain((days_since, 100000))
    return chart

def make_jhu_selected_state_chart() -> CovidChart:
    jhu_df = pd.read_csv('./data/jhu-data.csv')
    # grab us-specific
    jhu_df = jhu_df[(jhu_df.Country_Region == 'United States') & jhu_df.Province_State.notnull()]
    # jhu_df[(nyt_df["state"]=="Illinois")|(nyt_df["state"]=="New York")| (nyt_df["state"]=="New Jersey")| (nyt_df["state"]=="Washington")| (nyt_df["state"]=="Michigan")]
    days_since = 20
    chart = CovidChart(
        jhu_df,
        groupcol='Province_State',
        start_criterion=DaysSinceNumReached(days_since, 'Confirmed'),
        ycol='Confirmed',
        chart_type='USA',
        xcol='Date',
        top_k_groups=20,
        quarantine_df='./data/quarantine-activity-US.csv'  # should have a column with same name as `groupcol`
    )
    # chart.set_colormap()
    chart.set_unfocused_opacity(0.05)
    chart = chart.set_ytitle('Number of Confirmed Cases (log)')
    chart = chart.set_xtitle('Days since {} Confirmed'.format(days_since))
    chart.set_width(250).set_height(400)
    chart.set_xdomain((0, 35)).set_ydomain((days_since, 100000))
    chart.set_title("States With Significant Rate Decreases")
    return chart

def export_charts(configs):
    for config in configs:
        name = config['name']
        chart = config['gen']()
        chart.export(f'./website/js/autogen/{name}.js', f'{name}')

def make_vega_embed_script(configs):
    script = """
function startVegaEmbedding() {{
  var embedOpt = {{"mode": "vega-lite"}};
  $(document).ready(function() {{
{embed_calls}
  }});
}};
    """
    embed_calls = []
    for config in configs:
        embed_calls.append(
            f'    vegaEmbed("#{config.get("embed_id", config["name"])}", {config["name"]}, embedOpt);'
        )
    embed_calls = '\n'.join(embed_calls)
    script = script.format(embed_calls=embed_calls)
    with open('./website/js/autogen/vega_embed.js', 'w') as f:
        f.write(script)

def make_jekyll_config(configs):
    with open('./website/_config.in.yml', 'r') as f:
        jekyll_config = yaml.load(f.read(), yaml.SafeLoader)
    jekyll_config['date_last_modified'] = datetime.now().strftime('%B %d, %Y')
    for config in configs:
        jekyll_config['footer_scripts'].append(f'js/autogen/{config["name"]}.js')
    with open('./website/_config.yml', 'w') as f:
        yaml.dump(jekyll_config, f)

def main():
    configs = chart_configs()
    export_charts(configs)
    make_vega_embed_script(configs)
    make_jekyll_config(configs)

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


'''
此部分由王俊垚完成
'''
from app.dao.getposition_dao import *
def put_resume(request,uid):
    user_id=uid
    resume_id=request.values['resume_id']
    position_id=request.values['position_id']
    result = None
    if user_id and resume_id and position_id:
        user={"position_id":position_id,"user_id":user_id,"resume_id":resume_id}
        result = putResume(user)
    return result

def showPut(uid):
    user_id=uid
    result=None
    if user_id:
        result=selectUserPut(user_id)
    return result


__author__ = 'joezhou'
import requests
import json
import time
from bs4 import BeautifulSoup

URL1 = 'http://chartapi.finance.yahoo.com/instrument/1.0/'
URL2 = '/chartdata;type=quote;range=1d/json'
class FLOW(object):
    def __init__(self, q):
        """
        Usage:
        from optionchain import OptionChain
        oc = OptionChain('NASDAQ:AAPL')
        # oc.calls
        # oc.puts
        """
        self.params = {
            'q': q,
            'output': 'json'
        }
        self.up = 0
        self.up_sum = 0.0
        self.sum = 0.0
        self.down = 0
        self.down_sum = 0.0

    def get_content(self, url, params):
        response = requests.get(url, params=params)
        #print (url, params)
        if response.status_code == 200:
            content_json = response.content.decode()
            content_json1 = content_json.split(' ', 1)[1]
            content_json1 = content_json1[:-1]
            data=json.loads(content_json1)
            #print(data['series'])
            pre_close = 0.0
            pre_sum = 0.0
            for item in data['series']:
                # print (item)
                # print (item['Timestamp'],item['high'],item['volume'])
                avg = (float(item['close']) + float(item['high']) + float(item['low'])) / 3
                if pre_close == 0.0:
                    next
                else:
                    if  avg> pre_close:
                        self.up_sum += pre_sum
                        self.up += 1
                    else:
                        self.down_sum += pre_sum
                        self.down += 1
                pre_close = avg
                pre_sum = avg* float(item['volume'])
            self.sum = self.up_sum - self.down_sum
        else:
            data = 0
        return data

if __name__ == '__main__':
    total_up=0
    total_down=0
    sum1=0.0
    #for key, value in list.items():
    tickers =['nmbl',
             'goog',
             'fb',
             'aapl',
             'tsla',
             'amzn',
             'gpro'
             ]
    for ticker in tickers:
        FLOW_URL=URL1+ticker+URL2
        oc = FLOW(ticker)
        data = oc.get_content(FLOW_URL, oc.params)
        print (ticker, oc.up, oc.down, "{:,.0f}".format(oc.sum), "percent {:,.2f}".format(oc.sum/(oc.up_sum+ oc.down_sum)))
        file_line= "\n"+time.strftime('%X %x') + " up= " + str(oc.up) + " down= " + str(oc.down) + " total= " + str(oc.sum)
        with open("nmbl_intraday.txt", "a") as myfile:
            myfile.write(file_line)
    myfile.close()


#!/usr/bin/env python3
import os
import json
from datetime import date, datetime, timedelta

import keyring
import myfitnesspal

MFP_START_DATE = date(2017, 1, 2)

os.chdir(os.path.dirname(os.path.realpath(__file__)))

def load_client(username):
    """Return mfp client for given username."""
    return myfitnesspal.Client(username)

def scrape_data(mfp, start_date=MFP_START_DATE, totals={}, weight={}):
    """Update and return totals dict starting from a given date.

    If no keyword args are given all the available data will be saved.
    totals has all the macro data, the 'entries' key is for the meal logs.
    """
    current_day = start_date
    while(current_day <= date.today()):
        print('Loading', current_day)
        data = mfp.get_date(current_day)
        day_str = current_day.isoformat()
        totals[day_str] = data.totals
        totals[day_str]['entries'] = {e.name: e.totals for e in data.entries}
        current_day += timedelta(days=1)
    new_weight = mfp.get_measurements('Weight', start_date)
    for w in reversed(new_weight):
        day_str = w.isoformat()
        weight[day_str] = new_weight[w]
    return totals, weight

def save_data(totals, weight):
    """Save totals and weight data to json."""
    with open('totals.json', 'w') as f:
        json.dump(totals, f, indent=4)
    with open('weight.json', 'w') as f:
        json.dump(weight, f, indent=4)

def load_data():
    """Return totals and weight json data."""
    with open('totals.json', 'r') as f:
        totals = json.load(f)
    with open('weight.json', 'r') as f:
        weight = json.load(f)
    return totals, weight

def run(all_dates=False):
    """Save data from mfp client for specific time frame."""
    mfp = load_client(keyring.get_password('mfp', 'user'))
    if all_dates:
        new_totals, new_weight = scrape_data(mfp)
    else:
        totals, weight = load_data()
        last_date = datetime.strptime(sorted(totals.keys())[-1],
                                      '%Y-%m-%d').date()
        from_date = last_date - timedelta(days=1)
        new_totals, new_weight = scrape_data(mfp, from_date, totals, weight)

    save_data(new_totals, new_weight)

def main():
    """Run the mfp scraper."""
    run(all_dates=False)
    print('Done')

if __name__ == '__main__':
    main()


import os
import sys
import re

from coloredfile import ColoredFile

def main():
    if (len(sys.argv)) < 4:
        print("Not enough cmdline args provided")
        print("USAGE: python highlighter.py SYNTAXFILE THEMEFILE SOURCEFILE")

    args = sys.argv[1:]

    try:
        syntaxfile = open(args[0], 'r')
        themefile = open(args[1], 'r')
        sourcefile = open(args[2], 'r')
    except FileNotFoundError as e:
        print(e)
        print("USAGE: python highlighter.py SYNTAXFILE THEMEFILE SOURCEFILE")

    syntaxes = {}
    colors = {}

    #read regexes from syntax file into syntaxes-dict
    syntax_regex = r'\"(.*)\": ([\w\-]+)$'
    for line in syntaxfile.readlines():
        if line == '\n':
            continue
        matches = re.match(syntax_regex, line)
        try:
            syntaxes[matches.group(2)] = matches.group(1)
        except Exception as e:
            print("something wrong with: " + line)
            print(e)
            sys.exit(1)

    #read color-codes from theme-file into colors-dict
    #read without newlines to avoid \n after 0;32 etc
    for line in themefile.read().splitlines():
        try:
            if line:    #to skip empty lines in file
                splitted = line.split(': ')
                colors[splitted[0]] = splitted[1]
        except Exception as e:
            print("something wrong with: " + line)
            print(e)
            sys.exit(1)

    check_syntax_colors(syntaxes, colors)

    lines = sourcefile.readlines()

    """if no newline at the end of the sourcefile, add one to be able to
        generalize the coloring algorithm"""
    if lines[-1][-1] != '\n':
        lines[-1] += '\n'

    coloredOutput = ColoredFile(lines)
    fileAsStr = coloredOutput.getAllCharsClean()   #without colors

    #Color the matching regex parts
    for syntax in syntaxes:
        matches = re.finditer(syntaxes[syntax], fileAsStr, re.MULTILINE)
        for m in matches:
            # if regex has capture group(s), color them
            if m and len(m.groups()) >= 1:
                for i in range(1, len(m.groups()) + 1):
                    coloredOutput.addColorAt(m.start(i), m.end(i) - 1, colors[syntax])
            #else color full match
            else:
                coloredOutput.addColorAt(m.start(), m.end() - 1, colors[syntax])

    coloredOutput.printWithFix()

    syntaxfile.close()
    themefile.close()
    sourcefile.close()

"""
Check if all the syntaxes has a specified color in the theme-file.
Will assign  a random color if it doesn't.
"""
def check_syntax_colors(syn, col):
    if all([x in col.keys() for x in syn.keys()]):
        return
    else:
        print("<Syntaxes and theme don't match. \n<Missing :" + str(syn.keys() - col.keys()))
        print("<Will add 'random' colors for the syntax-colors that are missing")
        input("Press enter to continue....")

        col_nr = 94  #start of the random colors

        #adding the missing colors
        for s in syn:
            if not s in col:
                col[s] = "0;"+str(col_nr)
                col_nr += 1

if __name__ == "__main__":
    main()


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

# from scrapy.selector import Selector
# from scrapy.http import  Request
# from scrapy.spiders import CrawlSpider, Rule
# from scrapy.loader import ItemLoader
# from scrapy.linkextractors import LinkExtractor
# from scrapy.linkextractors.sgml import SgmlLinkExtractor
# from PriceCrawler.items import PricecrawlerItem
#
# class priceSpider(CrawlSpider):
#
#     name = 'mininova'
#     allowed_domains = ['mininova.org']
#     start_urls = ['http://www.mininova.org/today', "http://www.mininova.org/yesterday/"]
#     rules = [Rule(LinkExtractor(allow=['/tor/\d+']), 'parse_torrent')]
#
#     def parse_torrent(self, response):
#         torrent = PricecrawlerItem()
#         torrent['url'] = response.url
#         torrent['name'] = response.xpath("//h1/text()").extract()
#         torrent['description'] = response.xpath("//div[@id='description']").extract()
#         torrent['size'] = response.xpath("//div[@id='specifications']/p[2]/text()[2]").extract()
#         return torrent

from PriceCrawler import lstData

import time
import logging
#from scrapy.selector import Selector
from scrapy.http import  Request
from scrapy.spiders import CrawlSpider, Rule
#from scrapy.loader import ItemLoader
from scrapy.linkextractors import LinkExtractor
from PriceCrawler.StoreItems import StoreItem

#from selenium import selenium
from selenium import webdriver
from selenium.selenium import selenium

logger = logging.getLogger(__name__)

class PriceSpider(CrawlSpider):
    name = "priceSpider"
    #delayed the download to
    download_delay = 1
    allowed_domains = [
                       "blog.csdn.net"
                       #"gouwu.sogou.com"
                       ]
    #print lstData.lstData().lst
#     start_urls = (
#         [('http://gouwu.sogou.com/shop?query=' + searchWord ) for searchWord in lstData.lstData().lst]
#     )
    start_urls = [
                  #"http://gouwu.sogou.com/shop?query=" + u'iPhone 6s 16GB'
                  "http://blog.csdn.net/u012150179/article/details/11749017"
                  ]

    logger.info("start_urls are %s", start_urls)

    rules = (
         # Extract links matching 'category.php' (but not matching 'subsection.php')
        # and follow links from them (since no callback means follow=True by default).
        #Rule(LinkExtractor(allow = '/detail/\d+\.html.+')),

        # Extract links matching 'item.php' and parse them with the spider's method parse_item
#         Rule(LinkExtractor(allow = '/shop\?query=.+', deny= '/shop\?classify=.+'), callback='parse_item'),
#             )
        Rule(LinkExtractor(), callback='parse_item'),
             )

#     link_extractor = {
#         #'page':  LinkExtractor(allow = '/detail/\d+\.html.+'),
#         'page_down':  LinkExtractor(allow = '/shop\?query=.+',),#restrict_xpaths = '//a[@class = "pagination-next"]'
#     }
#     _x_query = {
#         'title':   '//p[@class="title"]/a/@title',
#         'name':    '//span[@class="floatR hui61 mt1"]/text()',#//li[2]/a/div[@class="ruyitao-market-name ruyitao-market-name-hightlight"]/text()
#         'price'    :    '//span[@class="shopprice font17"]/text()', # 'price'    :    '//span[@class = "price"]/text()',
#     }

    def __init__(self):
        CrawlSpider.__init__(self)
        self.browser = webdriver.Firefox()
#     def parse(self, response):
#         pass

    def __del__(self):
        pass
        self.browser.close()

    def parse_item(self, response):
        #crawl all display page
#         for link in self.link_extractor['page_down'].extract_links(response):
#             yield Request(url = link.url, callback=self.parse)

        logger.info("Start browser")
        self.browser.get(response.url)
        #loading time interval
        logger.info("Loading time interval: 10s")
        time.sleep(10)
        #get the data and write it to scrapy items
        logger.info('Hi, this is an item page! %s', response.url)
        item = StoreItem()
        item['url'] = str(response.url)
        item['title'] = response.xpath('//div[@id="article_details"]/div/h1/span/a/text()').extract()
        item['name'] = response.xpath('///div[@id="article_details"]/div/h1/span/a/text()').extract()
        item['price'] = response.xpath('//div[@id="article_details"]/div/h1/span/a/text()').extract()
#         item['title'] = response.xpath('//p[@class="title"]/a/@title').extract()
#         item['name'] = response.xpath('//span[@class="floatR hui61 mt1"]/text()').extract()
#         item['price'] = response.xpath('//span[@class="shopprice font17"]/text()').extract()
        return item


'''
Author: Aaron Johnson
Date: 1 Mar 2014

A simple generator for the IBTL to gforth.
Expects a simple tree with brackets as the internal nodes.

'''
import parse
from tokens import *

# Symbol table - set by set_symbol_table, must be set to handle variables
symbols = {}
# Running stack of variables allocated thus far (for static scoping)
init_ids = []
# Running collection of all allocated variables, to put prior to main
# in gforth code. Note that this means the last variable defined at a specific
# level is the type of that variable.
# AKA, [let [[x bool]]] and [let [[x float]]] will conflict, and x will
# be a float as it is the last defined.
variables = []

def set_symbol_table(symt):
	''' Set the global symbol table externally '''
	global symbols
	symbols = symt

class SemanticException(Exception):
	''' Custom exception class for semantic errors '''
	def __init__(self, s):
		super(SemanticException, self).__init__(s)

class TokenGroup:
	''' Token class representing a bracketed group of tokens '''
	def __repr__(self):
		return 'grp'

class Gcode:
	''' Overarching generated gforth code class, represents final code '''
	def __init__(self, arg, ttype = None):
		self.ttypes = ['bool', 'int', 'float', 'string', None]
		self.code = ''
		if ttype in self.ttypes:
			self.ttype = ttype
		else:
			raise Warning('Unsupported type ' + ttype)
		self.token = None

		# Instante with another Gcode, a token, a string, or a list of the prior
		if isinstance(arg, Gcode):
			self.code = arg.code
			self.ttype = arg.ttype
			self.token = arg.token
		elif isinstance(arg, Token):
			self.ttype = self.get_type_from_token(arg)
			# Corrections to formats for gforth - must have 'e' for floating,
			# and special string formatting
			if self.ttype == 'float' and 'e' not in arg.tag.lower():
				arg.tag += 'e'
			if isinstance(arg, Tokens.String):
				# slice off leading quote
				self.code = Gcode(['s\\"', arg.tag[1:]]).code
			else:
				self.code = arg.tag
			self.token = arg
		elif isinstance(arg, str):
			self.code = arg
		elif isinstance(arg, list):
			self.code = ' '.join([Gcode(c).code for c in arg])
		else:
			raise Warning('Error: unsupported Gcode init parameter ' + str(arg))

	def __repr__(self):
		return self.code

	def get_type_from_token(self, token):
		if isinstance(token, Tokens.Keyword) and \
			isinstance(token.token_type, Tokens.Constant):
			return 'bool'
		elif isinstance(token, Tokens.Number):
			return token.type
		elif isinstance(token, Tokens.String):
			return 'string'
		return None

# Dictionaries for operators
# Note that 'pow' for integer exponents needs to be predefined in gforth,
# and so does 'not'
string_ops = {'+': '$+'}
bool_result_ops = set(('and', 'or', 'not', '<', '<=', '>', '>=', '!=', '='))
num_result_ops = set(('+', '-', '*', '/', '%', '^', 'sin', 'cos', 'tan'))
bopd = {'=':'=','<=':'<=','<':'<','>=':'>=','>':'>','!=':'<>','+':'+','-':'-',
		'*':'*','/':'/','%':'mod','^':'pow',':=':'!'}
fbopd = {'=':'f=','<=':'f<=','<':'f<','>=':'f>=','>':'f>','!=':'f<>','+':'f+',
		'-':'f-','*':'f*','/':'f/','%':'fmod','^':'f**',':=':'f!'}
uopd = {'-':Gcode(['-1', '*'])}
fuopd = {'-':Gcode(['-1e', 'f*']), 'sin':'fsin', 'cos':'fcos', 'tan':'ftan'}
bool_opd = {'and':'and', 'or':'or', 'not':'not'}

def error(e = '', t = None):
	s = e
	if t is not None and isinstance(t, Token):
		s += '\n at line {}, column {}'.format(t.linecol[0][0], t.linecol[0][1])
	raise SemanticException('Error: ' + s)

def replace_brackets(node):
	''' Replace brackets with indent notifier in tree '''
	for c in node.children:
		if parse.is_left_bracket(c.value):
			c.value = TokenGroup()
			replace_brackets(c)
	for c in node.children:
		if parse.is_right_bracket(c.value):
			c.delete_branch()

def variable(var, level, fetch = True):
	''' Fetch or assign a variable at a certain level '''
	if symbols[var.token.tag].init_levels:
		latest = symbols[var.token.tag].init_levels[-1]
		if fetch:
			suf = '@'
		else:
			suf = '!'
		pre = 'f' if latest['type'] == 'float' \
			else ('$' if latest['type'] == 'string' else '')
		presuf = pre + suf
		return Gcode([var.token.tag + str(latest['level']), presuf], \
			ttype = latest['type'])
	else:
		error('Variable not declared in this scope: ' + var.token.tag, var.token)

def switch_string_op_binary(op, c1, c2):
	''' Code for binary string operation; c1 and c2 are strings '''
	if op.tag not in string_ops:
		error('Operator "' + op.tag + '" is not a string operator', op)
	return Gcode([c1, c2, string_ops[op.tag]], ttype='string')

def switch_assignment(name, val, level):
	''' Code to assign to a variable correctly, or raise error if invalid
		assign 'val' to 'name' '''
	name = variable(name, level, fetch = False)
	if name.ttype == 'float' and val.ttype == 'int':
		val = Gcode([val, 's>f'], ttype='float')
	if name.ttype != val.ttype:
		erstr = 'type mismatch: cannot assign "{}" of type "{}" ' +\
			'to variable "{}" of ttype {}'\
			.format(val.code, val.ttype, name.code[:-1], name.ttype)
		error(erstr, name.token)
	return Gcode([val, name])

def switch_bool_op_unary(op, c1):
	''' Code for unary bool operation '''
	if c1.ttype != 'bool':
		error('Cannot use "{}" on non-{}, "{}"'\
			.format(op.tag, 'bool', c1.code), op)
	return Gcode([c1, bool_opd[op.tag]], ttype='bool')

def switch_bool_op_binary(op, c1, c2):
	''' Code for binary bool operation '''
	if c1.ttype != 'bool' or c2.ttype != 'bool':
		error('Cannot use "{}" on non-{}, "{}" and or "{}"'\
			.format(op.tag, 'bools', c1.code, c2.code), op)
	return Gcode([c1, c2, bool_opd[op.tag]], ttype='bool')

def switch_num_op_unary(op, c1):
	''' Code for unary number operation '''
	if not c1.ttype == 'float' and not c1.ttype == 'int':
		error('Cannot use "{}" on non-{}, "{}"'\
			.format(op.tag, 'number', c1.code), op)
	# Convert integer if needed
	# "-" is the only valid integer unary op - the others are sin, cos, etc.
	# Note that no number-unary op returns a non-number
	float_on = False if c1.ttype == 'int' and op.tag == '-' else True
	if float_on and c1.ttype == 'int':
		c1 = Gcode([c1, 's>f'], ttype='float')
	if float_on:
		return Gcode([c1, fuopd[op.tag]], ttype='float')
	else:
		return Gcode([c1, uopd[op.tag]], ttype='int')

def switch_num_op_binary(op, c1, c2):
	''' Code for binary number operation '''
	# c1 and c2 must be numbers
	if not all([x.ttype == 'float' or x.ttype == 'int' for x in [c1, c2]]):
		error('Cannot use "{}" on non-{}, "{}" and or "{}"'\
			.format(op.tag, 'numbers', c1.code, c2.code), op)
	# Convert integers to float as needed
	if c1.ttype == 'float' or c2.ttype == 'float':
		float_on = True
		if c1.ttype == 'int':
			c1 = Gcode([c1, 's>f'], ttype='float')
		elif c2.ttype == 'int':
			c2 = Gcode([c2, 's>f'], ttype='float')
	else:
		float_on = False

	# What is the result type of the binary operation - it could be boolean
	res_ttype = 'bool' if op.tag in bool_result_ops \
		else ('float' if float_on else 'int')

	if float_on:
		return Gcode([c1, c2, fbopd[op.tag]], ttype=res_ttype)
	else:
		return Gcode([c1, c2, bopd[op.tag]], ttype=res_ttype)

def switch_let(level, varlist):
	''' Code for let statements '''
	level -= 1 # This should be valid outside of [let ...]
	declares = varlist.children
	codes = []
	global init_ids
	# For each declaration, get its name and type, append them to the symbol
	# table and variable stack init_ids, and decide the correct gforth variable
	# declaration
	for declare in declares:
		name = declare.children[0].value
		typ = declare.children[1].value
		varname = name.tag + str(level)
		symbols[name.tag].init_levels.append({'level':level, 'type':typ.tag})
		if typ.tag == 'bool' or typ.tag == 'int':
			codes.append(Gcode(['variable', varname]))
		elif typ.tag == 'float':
			codes.append(Gcode(['fvariable', varname]))
		elif typ.tag == 'string':
			codes.append(Gcode(['variable', varname]))
		init_ids.append(symbols[name.tag])
	return Gcode(codes)

def switch_key_statement(fn, c1, c2 = None, c3 = None):
	''' Code for keyword statements if, while and stdout with max 3 children '''
	if all(x is not None for x in [c1, c2, c3]):
		if fn.tag =='if':
			return Gcode([c1, 'if', c2, 'else', c3, 'endif'])
		elif fn.tag == 'while':
			return Gcode(['begin', c1, 'while', c2, c3, 'repeat'])
	elif fn.tag == 'if':
		return Gcode([c1, 'if', c2, 'endif'])
	elif fn.tag == 'while':
		return Gcode(['begin', c1, 'while', c2, 'repeat'])
	elif fn.tag == 'stdout':
		if c1.ttype == 'int' or c1.ttype == 'bool':
			return Gcode([c1, '.'])
		elif c1.ttype == 'float':
			return Gcode([c1, 'f.'])
		elif c1.ttype == 'string':
			return Gcode([c1, 'type'])
		else:
			error('Unknown type for stdout ' + str(c1.ttype), fn)
	else:
		error('Unrecognized keyword statement', fn)

def switch_key_op(op, c1, c2 = None):
	''' Decide code path for keyword operators such as 'and' and 'sin' '''
	if c2 is None:
		if op.tag in bool_result_ops:
			return switch_bool_op_unary(op, c1)
		elif op.tag in num_result_ops:
			return switch_num_op_unary(op, c1)
	else:
		if op.tag in bool_result_ops:
			return switch_bool_op_binary(op, c1, c2)
		elif op.tag in num_result_ops:
			return switch_num_op_binary(op, c1, c2)
		else:
			error('Unrecognized keyword operator: ' + op.tag, op)

def code_op(children, level):
	''' Generate code for an operator and its children
		These are non-identifier operators such as '+' '''
	# Error on insufficient children
	if len(children) <= 1:
		error('Must have children for operator')
	else:
		op = children[0].value
	# Unary op
	if len(children) == 2:
		c1 = generate_gforth(children[1], level)
		if isinstance(c1.token, Tokens.Identifier):
			c1 = variable(c1, level, fetch = True)

		if c1.ttype is None:
			return Gcode([c1, op.tag])
		else:
			return switch_num_op_unary(op, c1)
	# Binary op
	elif len(children) == 3:
		# Special case for assignment - do not fetch variable, simply Gcode it
		# and let switch_assignment handle it
		if op.tag == ':=':
			if not isinstance(children[1].value, Tokens.Identifier):
				error('Cannot assign to non-identifier "{}"'\
					.format(children[1].value), op)
			c1 = Gcode(children[1].value)
			c2 = generate_gforth(children[2], level)
			return switch_assignment(c1, c2, level)

		# Generate code for the children and switch appropriately
		c1 = generate_gforth(children[1], level)
		c2 = generate_gforth(children[2], level)

		if c1.ttype is None or c2.ttype is None:
			return Gcode([c1, c2, op.tag])
		elif c1.ttype == 'string' and c2.ttype == 'string':
			return switch_string_op_binary(op, c1, c2)
		else:
			return switch_num_op_binary(op, c1, c2)
	# Should never happen after passing parser
	else:
		error('Operator branch without 2 or 3 children')

def code_key_statement(children, level):
	''' Generate code for a keyword statement and its children '''
	if len(children) <= 1:
		error('Must have children for keyword statement')
	key_stmt = children[0].value
	if len(children) == 2:
		# Special case of let, with 2 children (let and declarations group)
		if key_stmt.tag == 'let':
			global variables
			variables.append(switch_let(level, children[1]))
			return Gcode('')
		else:
			c1 = generate_gforth(children[1], level)
			return switch_key_statement(key_stmt, c1)
	elif len(children) == 3:
		c1 = generate_gforth(children[1], level)
		c2 = generate_gforth(children[2], level)
		return switch_key_statement(key_stmt, c1, c2)
	elif len(children) == 4:
		c1 = generate_gforth(children[1], level)
		c2 = generate_gforth(children[2], level)
		c3 = generate_gforth(children[3], level)
		return switch_key_statement(key_stmt, c1, c2, c3)
	elif len(children) >= 4:
		# We have a while statement to deal with! Simply wrap the whole lot
		cnlist = []
		for cn in children[1:]:
			cnlist.append(generate_gforth(cn, level))
		return Gcode(['begin', cnlist[0], 'while', cnlist[1:], 'repeat'])
	else:
		error('Keyword function without children')

def code_key_operator(children, level):
	''' Generate code for keyword operators like 'and' and 'sin' '''
	if len(children) < 1:
		error('Must have children for keyword operator')
	key_op = children[0].value
	if len(children) == 2:
		c1 = generate_gforth(children[1], level)
		return switch_key_op(key_op, c1)
	elif len(children) == 3:
		c1 = generate_gforth(children[1], level)
		c2 = generate_gforth(children[2], level)
		return switch_key_op(key_op, c1, c2)
	else:
		error('Keyword "' + key_op.tag + '" operator without 1 or 2 children', key_op)

def clear_decl_under(level):
	''' Upon leaving a token group, invalidate all declarations within it '''
	global init_ids
	if init_ids:
		last = init_ids[-1]
		while last.init_levels[-1]['level'] >= level:
			last.init_levels.pop()
			# Because it is by reference, the init_levels in the symbol table is popped
			last = init_ids.pop()
			if not init_ids:
				break
			last = init_ids[-1]

def generate_gforth(node, level):
	''' Generate gforth code recursively, core function '''

	# This node is a leaf - it is not root or a group, so return code on it
	if not isinstance(node.value, TokenGroup) and node.value != 'Root':
		if isinstance(node.value, Tokens.Identifier):
			return variable(Gcode(node.value), level, fetch = True)
		return Gcode(node.value)

	# This node is a token group with one child, return code on the single child
	elif isinstance(node.value, TokenGroup) and \
		 len(node.children) > 0 and \
	     not isinstance(node.children[0].value, TokenGroup) \
	     and len(node.children) == 1:
	    return generate_gforth(node.children[0], level + 1)

	# Multiple children nodes - decide what to do with them
	code = []
	for child in node.children:
		token = child.value
		# First check if the first node is an operator or keyword
		# Call the appropriate function if so, exit with a return
		if isinstance(token, Tokens.Operator):
			return code_op(node.children, level)
		elif isinstance(token, Tokens.Keyword):
			if isinstance(token.token_type, Tokens.Function):
				return code_key_statement(node.children, level)
			elif isinstance(token.token_type, Tokens.Operator):
				return code_key_operator(node.children, level)
			elif isinstance(token.token_type, Tokens.Constant):
				code.append(token)
			else:
				error('Unknown keyword used', token)
		# Now check and add the rest of the possibilities
		elif isinstance(token, Tokens.Identifier):
			code.append(variable(token, level))
		elif isinstance(token, Tokens.String):
			code.append(token)
		elif isinstance(token, Tokens.Number):
			code.append(token)
		# Recurse deeper if we reach a token group
		elif isinstance(token, TokenGroup):
			code.append(generate_gforth(child, level + 1))
			# back at this level, declarations below are to be eliminated
			clear_decl_under(level + 1)
		else:
			error('Unrecognized node', token)
	return Gcode(code)

def code_wrap(node):
	''' Wrap the code in a gforth function called 'main' since statements
		must be in functions '''
	code = generate_gforth(node, 0)
	global variables
	return Gcode([': pow 1 swap 0 u+do over * loop nip ; \n',
		          ': $! place ; \n',
		          ': $@ count ; \n',
		          ': not 0= ; \n',
		          ': $+ { a2 c2 } pad place pad count 2drop a2 c2 pad +place pad count ; \n',
		          Gcode(variables), '\n',
		          ': main\n',
		          code,
		          '\n; main bye'])


from easydict import EasyDict

from motorengine.base.aggregation import TopLevelAggregation

class Aggregation(TopLevelAggregation):
    async def fetch(self, alias=None):
        coll = self.queryset.coll(alias)
        results = []
        try:
            lst = await coll.aggregate(self.to_query(), cursor=False)
            for item in lst:
                self.fill_ids(item)
                results.append(EasyDict(item))
        except Exception as e:
            raise RuntimeError('Aggregation failed due to: {}'.format(str(e)))
        return results


"""Base class of PhonopyCalculation and Phono3pyCalculation."""

from aiida.engine import CalcJob
from aiida.common import CalcInfo, CodeInfo
from aiida.plugins import DataFactory
from aiida_phonopy.common.file_generators import get_BORN_txt

Dict = DataFactory("dict")
StructureData = DataFactory("structure")
ArrayData = DataFactory("array")
Float = DataFactory("float")
Bool = DataFactory("bool")

class BasePhonopyCalculation(CalcJob):
    """A basic plugin for calculating force constants using Phonopy.

    Requirement: the node should be able to import phonopy if NAC is used

    """

    _INPUT_NAC = "BORN"

    @classmethod
    def define(cls, spec):
        """Define inputs, outputs, and outline."""
        super().define(spec)
        spec.input("settings", valid_type=Dict, help="Phonopy parameters.")
        spec.input("structure", valid_type=StructureData, help="Unit cell structure.")
        spec.input("symmetry_tolerance", valid_type=Float, default=lambda: Float(1e-5))
        spec.input(
            "fc_only",
            valid_type=Bool,
            help="Only force constants are calculated.",
            default=lambda: Bool(False),
        )
        spec.input(
            "force_sets",
            valid_type=ArrayData,
            required=False,
            help="Sets of forces in supercells",
        )
        spec.input(
            "nac_params", valid_type=ArrayData, required=False, help="NAC parameters."
        )
        spec.input(
            "primitive",
            valid_type=StructureData,
            required=False,
            help="Primitive cell structure only necessary NAC is applied.",
        )
        spec.input(
            "dataset",
            valid_type=(Dict, ArrayData),
            required=False,
            help="Displacements and forces dataset.",
        )

    def prepare_for_submission(self, folder):
        """Prepare calcinfo."""
        self.logger.info("prepare_for_submission")

        # These three lists are updated in self._create_additional_files(folder)
        self._internal_retrieve_list = []
        self._additional_cmd_params = []
        self._calculation_cmd = []
        self._create_additional_files(folder)

        # ================= prepare the python input files =================

        # BORN
        if (
            not self.inputs.fc_only
            and "nac_params" in self.inputs
            and "primitive" in self.inputs
        ):
            born_txt = get_BORN_txt(
                self.inputs.nac_params,
                self.inputs.primitive,
                self.inputs.symmetry_tolerance,
            )
            with folder.open(self._INPUT_NAC, "w", encoding="utf8") as handle:
                handle.write(born_txt)
            for params in self._additional_cmd_params:
                params.append("--nac")

        self.inputs.metadata.options.withmpi = False

        # ============================ calcinfo ===============================

        local_copy_list = []
        remote_copy_list = []

        calcinfo = CalcInfo()
        calcinfo.uuid = self.uuid
        calcinfo.local_copy_list = local_copy_list
        calcinfo.remote_copy_list = remote_copy_list
        calcinfo.retrieve_list = self._internal_retrieve_list

        calcinfo.codes_info = []
        for i, (default_params, additional_params) in enumerate(
            zip(self._calculation_cmd, self._additional_cmd_params)
        ):
            codeinfo = CodeInfo()
            cmdline_params = default_params + additional_params
            codeinfo.cmdline_params = cmdline_params
            codeinfo.code_uuid = self.inputs.code.uuid
            codeinfo.withmpi = False
            calcinfo.codes_info.append(codeinfo)

        return calcinfo

    def _create_additional_files(self, folder):
        raise NotImplementedError()


from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
from nltk.tokenize import word_tokenize
import nltk
import sys
import numpy as np
import networkx as nx
import json

def read_review(file_name, business_id):
    file = open(file_name, "r")
    filedata = file.readlines()
    dataset = []

    for line in filedata:
        data = json.loads(line)
        if data['business_id'] == business_id:
            dataset.append(data['text'])

    return dataset

def cosine_similarity(i_sentence, j_sentence, stop_words):
    i_set = {w.lower() for w in i_sentence if not w in stop_words}
    j_set = {w.lower() for w in j_sentence if not w in stop_words}

    union_vector = list(i_set.union(j_set))
    vector_size = len(union_vector)
    i_vector, j_vector = np.zeros(vector_size), np.zeros(vector_size)

    for w in union_vector:
        if w in i_set:
            i_vector[union_vector.index(w)] += 1
        if w in j_set:
            j_vector[union_vector.index(w)] += 1

    return cosine_distance(i_vector, j_vector)

def similarity_matrix(review, stop_words):
    similarity_matrix = np.zeros((len(review), len(review)))

    for i in range(similarity_matrix.shape[0]):
        for j in range(similarity_matrix.shape[1]):
            similarity_matrix[i][j] = cosine_similarity(review[i], review[j], stop_words)

    return similarity_matrix

def main():
    file_name = sys.argv[1]
    business_id = sys.argv[2]

    stop_words = stopwords.words('english')

    review = read_review(file_name, business_id)
    similarity_martix = similarity_matrix(review, stop_words)
    similarity_graph  = nx.from_numpy_array(similarity_martix)

    page_rank = nx.pagerank(similarity_graph, alpha=0.9)
    most_similar_review_list = [(page_rank[i], sentence) for i,sentence in enumerate(review)]

    output_review = ""
    for i in range(2):
      output_review+=most_similar_review_list[i][1]

    print(output_review)

nltk.download('stopwords')
nltk.download('punkt')
#main("yelp_dataset/review.json", "iojTeSaoPuxm4WeCzDUA6w")
if __name__ == '__main__':
    main()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='testContent',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
                ('name', models.CharField(max_length=120, null=True, blank=True)),
                ('email', models.EmailField(max_length=75)),
                ('comment', models.CharField(max_length=1024, null=True, blank=True)),
                ('added', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]


import sublime, sublime_plugin, ctypes, json, sys, http.client

class GoldGitLogCommand(sublime_plugin.TextCommand):
    def run(self, edit=None):
        #fn = self.get_file_name()
        return self.run_log(self, '--', '')

    def log_panel_done(self, picked):
        if 0 > picked < len(self.results):
            return
        item = self.results[picked]
        self.view.run_command("gold_do_open_class", {"className": item})
        print(item)

    def run_log(self, follow, *args):
        conn = http.client.HTTPConnection('localhost:8082')
        conn.request("GET", "/aeWamManager/searchEntities/awt")
        resp = conn.getresponse()
        self.results = json.loads(resp.read().decode("ascii").replace('\r\n', '\n'))

        window = sublime.active_window()
        window.show_quick_panel(self.results, self.log_panel_done, sublime.MONOSPACE_FONT)


import numpy as np
import argparse
from System import System
import matplotlib.pyplot as plt
import time, os, sys

class queueLearn_time(object):
    _parameters = {'num_server': 0, 'num_class': 0, 'time_horizon': 0, 'num_tsk_per_usr': 0, 'mu': None,
                   'Lambda': 0, 'class_prob': None, 'reward_mat': None, 'V': 0, 'gamma': 0, 'verbose': False,
                   'continuous': False}

    def __init__(self, **kwargs):
        self._get_2_server_default_param()
        self._set_param(**kwargs)

        self.time_horizon = self._parameters['time_horizon']

        self.expect_reward = []
        self.num_client_time = []
        self.num_client_class = []

    def _get_2_server_default_param(self):
        # System parameters
        self._parameters['num_server'] = 2
        self._parameters['num_class'] = 2
        self._parameters['num_tsk_per_usr'] = 100
        self._parameters['mu'] = np.array([1, 1])
        self._parameters['Lambda'] = 1.2  # total system incoming task rate
        self._parameters['class_prob'] = np.array([0.5, 0.5])  # prior probability of each class of user
        self._parameters['reward_mat'] = np.array([[0.9, 0.1], [0.9, 0.3]])

        # algorithm parameters
        self._parameters['V'] = 21
        self._parameters['gamma'] = 1.1

    def _set_param(self, **kwargs):
        for key in kwargs:
            assert key in self._parameters, "Wrong kwarg {} given".format(key)
            self._parameters[key] = kwargs[key]
            # print('set {} to {}'.format(key, kwargs[key]))

    def run_simulation(self, myopic=False):
        # print(self._parameters)
        Operator = System(self._parameters)
        start_time = time.time()
        # TODO: make number of iterations configurable, and use multi-thread
        for t in range(self.time_horizon):
            Operator.addNewUser()
            if not myopic:
                Operator.update_client_UCB_est()

            Operator.compute_assignProb(myopic)
            Operator.process_task_at_server()
            Operator.incr_time_slot()
            Operator.update_expect_reward()
            self.expect_reward.append(Operator.expected_reward)
            self.num_client_time.append(Operator.num_clients)
            if not self._parameters['continuous']:
                self.num_client_class.append(Operator.num_class_client.copy())
            if t % 1000 == 0:
                print('{}%...'.format(t / self.time_horizon * 100))

        Operator.show_stats()

        print("--- %s seconds ---" % (time.time() - start_time))

    def plot_results(self, save_figure=False):
        """Plot simulation results"""
        fig1, axes = plt.subplots(2, 1, sharex=True)
        self._plot(axes[0], range(0, self.time_horizon), self.expect_reward, xlabel='time', ylabel='expected reward')
        self._plot(axes[1], range(0, self.time_horizon), self.num_client_time, xlabel='time', ylabel='number of total clients')

        if not self._parameters['continuous']:
            fig2 = plt.figure()
            ax = fig2.add_subplot(1, 1, 1)
            self._plot(ax, range(0, self.time_horizon), self.num_client_class, 'time', 'number of clients per class',
                       None, (['Class {}'.format(i) for i in range(self._parameters['num_class'])]))
        if save_figure:
            if not os.path.exists('Figures/'):
                os.mkdir('Figures/')
            figure_dir = 'Figures/'
            if not os.path.exists(figure_dir):
                os.mkdir(figure_dir)
            fig1.savefig(figure_dir+'ExpectedReward_time_classNum_'+str(self._parameters['num_class'])+'.png', bbox_inches='tight')
            fig2.savefig(figure_dir+'NumberClients_time_classNum_'+str(self._parameters['num_class'])+'.png', bbox_inches='tight')

    def _plot(self, ax, x_data, y_data, xlabel=None, ylabel=None, title=None, _legend=None):
        """ helper function for plotting"""
        ax.plot(x_data, y_data)
        ax.grid(which='major', axis='y', alpha=0.3)
        props = {'title': title, 'xlabel': xlabel, 'ylabel': ylabel}
        ax.set(**props)
        if _legend:
            plt.gca().legend(_legend)

def cmd_parser():
    parser = argparse.ArgumentParser(prog='QueueLearning', description='Run queueing with learning simulations.')
    parser.add_argument('-o', "--outfile", type=str, default='expect_reward.txt', help="insert output filename")

    parser.add_argument("-s", "--save", action='store_true', help="save simulation results/plots")
    parser.add_argument("-p", "--plot", action='store_true', help="show plots")
    parser.add_argument("-v", "--verbose", action='store_true', help="show real-time simulation details")
    parser.add_argument("--version", action='version', version='%(prog)s 1.0')
    parser.add_argument('-cont', '--continuous', action='store_true',
                        help='set payoff vectors to be continuous distributed (default: discrete)')
    parser.add_argument('-m', '--myopic', action='store_true',
                        help='use myopic matching strategy')
    parser.add_argument('-time', '--time_horizon', type=int, default=10000, help="time horizon for each simulation")
    parser.add_argument('-iter', '--iteration', type=int, default=1, help="number of total independent runs")
    args = parser.parse_args()

    return args

if __name__ == "__main__":
    args = cmd_parser()

    expect_reward_alg1_all = []

    for iteration in range(args.iteration):
        q1 = queueLearn_time(time_horizon=args.time_horizon, continuous=args.continuous, verbose=args.verbose)
        q1.run_simulation(myopic=False)
        expect_reward_alg1_all.append(q1.expect_reward)

    alg1_expect_reward_mean = np.mean(expect_reward_alg1_all, axis=0)

    if args.outfile:
        np.savetxt(args.outfile, alg1_expect_reward_mean, delimiter=",", fmt="%.3f")
    if args.plot:
        q1.plot_results(args.save)

    # for myopic matching simulation
    if args.myopic:
        expect_reward_myopic_all = []
        for iteration in range(args.iteration):
            q2 = queueLearn_time(time_horizon=args.time_horizon, continuous=args.continuous, verbose=args.verbose)
            q2.run_simulation(myopic=True)
            expect_reward_myopic_all.append(q2.expect_reward)
        myopic_expect_reward_mean = np.mean(expect_reward_myopic_all, axis=0)

        if args.outfile:
            np.savetxt(args.outfile, myopic_expect_reward_mean, delimiter=",", fmt="%.3f")
        if args.plot:
            q2.plot_results(args.save)

    plt.show()


import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

Migrate(app, db)

class OrdersModel(db.Model):
    # __tablename__ = "Orders"

    id = db.Column(db.Integer, primary_key=True)
    courier = db.Column(db.Integer)
    order_id = db.Column(db.Integer)
    start_loc = db.Column(db.String)
    end_loc = db.Column(db.String)
    price = db.Column(db.Float)
    comment = db.Column(db.String)

    def __init__(self, courier, order_id, start_loc, end_loc, price):
        self.courier = courier
        self.order_id = order_id
        self.start_loc = start_loc
        self.end_loc = end_loc
        self.price = price

    def __repr__(self):
        return f'შეკვეთა {self.order_id} მოაქვს კურიერის N {self.courier}'

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


# coding: utf-8
import socket

server = '172.16.1.1'
port = 20015

userid = 'test'
ip = '10.21.124.107'

def checksum(data):
    cs = 0x4e67c6a7
    for b in data:
        cs &= 0xffffffff
        if cs < 0x80000000:
            cs ^= ((cs >> 2) + (cs << 5) + b) & 0xffffffff
        else:
            cs ^= (((cs >> 2) | 0xC0000000) + (cs << 5) + b) & 0xffffffff
        # print(cs.to_bytes(4, 'big').hex().upper())
    return cs & 0x7fffffff

def make_content(userid, ip, mac, sp):
    data = b'test'                                  #user id
    data += bytes(26)                                #padding
    data += bytes(list(map(int, ip.split('.'))))    #ip
    data += mac.encode()                             # mac
    data += bytes(3)
    data += sp.to_bytes(1, 'little')                #sp
    data += bytes(1)
    data += checksum(data).to_bytes(4, 'little')
    return data

def connect(mac, sp):
    data = make_content(userid, ip, mac, sp)
    print(data.hex())
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.sendto(data, (server, port))
    # print(s.recv(1024).hex())
    s.close()

if __name__ == '__main__':
    #  mac = '74:D0:2B:4B:8E:33'
    mac = 'a0:1d:48:d5:12:33'
    sp = 3

    connect(mac, sp)


#-*-coding:utf-8-*-
#!/usr/bin/env python3

from pathlib import Path
import os
from algorithms.attention_sac import AttentionSAC
from utils.env_wrappers import SubprocVecEnv, DummyVecEnv
from utils.buffer import ReplayBuffer
from utils.make_env import make_env
import numpy as np
from tensorboardX import SummaryWriter
from torch.autograd import Variable
import torch
import gfootball.env as football_env
from gym.spaces import Box, Discrete

# disable logger casting warning
import gym
gym.logger.set_level(40)

def make_state(obs):
    state = []

    for p in range(11):
        o = obs[p]
        tempList = []
        # activate and designated
        if o['active'] == p:
            tempList.append(1)
        else:
            tempList.append(0)

        if o['designated'] == p:
            tempList.append(1)
        else:
            tempList.append(0)

        # home team
        tempList.append(o['left_team_tired_factor'][p])
        tempList.append(o['left_team'][p][0])
        tempList.append(o['left_team'][p][1])

        for l in range(len(o['left_team'])):
            if l == p: continue
            else:
                tempList.append(o['left_team'][l][0])
                tempList.append(o['left_team'][l][1])

        tempList.append(o['left_team_direction'][p][0])
        tempList.append(o['left_team_direction'][p][1])

        for l in range(len(o['left_team_direction'])):
            if l == p: continue
            else:
                tempList.append(o['left_team_direction'][l][0])
                tempList.append(o['left_team_direction'][l][1])

        # side team
        for r in o['right_team']:
            tempList.append(r[0])
            tempList.append(r[1])
        for r in o['right_team_direction']:
            tempList.append(r[0])
            tempList.append(r[1])

        # ball
        tempList.append(o['ball'][0])
        tempList.append(o['ball'][1])
        tempList.append(o['ball'][2])
        tempList.append(o['ball_rotation'][0])
        tempList.append(o['ball_rotation'][1])
        tempList.append(o['ball_rotation'][2])
        tempList.append(o['ball_direction'][0])
        tempList.append(o['ball_direction'][1])
        tempList.append(o['ball_direction'][2])
        tempList.append(o['ball_owned_team'])

        # etc
        tempList.append(o['score'][0])
        tempList.append(o['score'][1])
        tempList.append(o['game_mode'])

        state.append(np.array(tempList))

    state = np.array(state)
    return state

def make_parallel_env(n_rollout_threads, seed):
    def get_env_fn(rank):
        def init_env():
            # (** EDITED **) Google Football Env
            # Gym Env 설정 함수를 변경
            env = football_env.create_environment(
                env_name=config["academy_scenario"],
                rewards=config["scoring"],
                render=config["render_mode"],
                number_of_left_players_agent_controls=config["num_to_control"],
                representation='raw')

            env.seed(seed + rank * 1000)
            np.random.seed(seed + rank * 1000)
            return env
        return init_env
    if n_rollout_threads == 1:
        return DummyVecEnv([get_env_fn(0)])
    else:
        return SubprocVecEnv([get_env_fn(i) for i in range(n_rollout_threads)])

def run(config):
    model_dir = Path('./models') / config["env_id"] / config["model_name"]
    if not model_dir.exists():
        run_num = 1
    else:
        exst_run_nums = [int(str(folder.name).split('run')[1]) for folder in
                         model_dir.iterdir() if
                         str(folder.name).startswith('run')]
        if len(exst_run_nums) == 0:
            run_num = 1
        else:
            run_num = max(exst_run_nums) + 1
    curr_run = 'run%i' % run_num
    run_dir = model_dir / curr_run
    log_dir = run_dir / 'logs'
    os.makedirs(log_dir)
    logger = SummaryWriter(str(log_dir))

    torch.manual_seed(run_num)
    np.random.seed(run_num)
    env = make_parallel_env(config["n_rollout_threads"], run_num)
    model = AttentionSAC.init_from_env(env,
                                       tau=config["tau"],
                                       pi_lr=config["pi_lr"],
                                       q_lr=config["q_lr"],
                                       gamma=config["gamma"],
                                       pol_hidden_dim=config["pol_hidden_dim"],
                                       critic_hidden_dim=config["critic_hidden_dim"],
                                       attend_heads=config["attend_heads"],
                                       reward_scale=config["reward_scale"])
    #model = AttentionSAC.init_from_save("./models/football/MAAC3/run2/model.pt", True)
    # (** EDITED **) Set Replay Buffer
    # env.action_space, env.observation_space 의 shape를 iteration을 통해 버퍼 설정
    replay_buffer = ReplayBuffer(config["buffer_length"], model.nagents,
                                 [104 for _ in range(model.nagents)],
                                 [19 for _ in range(model.nagents)])
    t = 0
    for ep_i in range(0, config["n_episodes"], config["n_rollout_threads"]):
        print("Episodes %i-%i of %i" % (ep_i + 1,
                                        ep_i + 1 + config["n_rollout_threads"],
                                        config["n_episodes"]))

        obs = env.reset()
        obs = np.array([make_state(o) for o in obs])
        model.prep_rollouts(device='cpu')

        for et_i in range(config["episode_length"]):
            print("episode : {} | step : {}".format(ep_i, et_i), end='\r')
            # rearrange observations to be per agent, and convert to torch Variable
            torch_obs = [Variable(torch.Tensor(
                np.vstack(obs[:, i])), requires_grad=False) for i in range(model.nagents)]
            # get actions as torch Variables
            torch_agent_actions = model.step(torch_obs, explore=True)
            # convert actions to numpy arrays
            agent_actions = [ac.data.numpy() for ac in torch_agent_actions]
            # rearrange actions to be per environment
            actions = [[ac[i] for ac in agent_actions]
                       for i in range(config["n_rollout_threads"])]

            # Reform Actions list to fit on Football Env
            # Google Football 환경은 액션 리스트 (one hot encoded)가 아닌 정수값을 받음
            actions_list = [[np.argmax(b) for b in a] for a in actions]

            # Step
            next_obs, rewards, dones, infos = env.step(actions_list)
            next_obs = np.array([make_state(n_o) for n_o in next_obs])

            # Prevention of divergence
            # 안해주면 발산해서 학습 불가 (NaN)
            rewards = rewards - 0.000001

            # Reform Done Flag list
            # replay buffer에 알맞도록 done 리스트 재구성
            dones = (np.array([dones for _ in range(model.nagents)])).T

            replay_buffer.push(obs, agent_actions, rewards, next_obs, dones)
            obs = next_obs
            t += config["n_rollout_threads"]
            if (len(replay_buffer) >= config["batch_size"] and (t % config["steps_per_update"]) < config["n_rollout_threads"]):
                if config["use_gpu"]:
                    model.prep_training(device='gpu')
                else:
                    model.prep_training(device='cpu')
                for u_i in range(config["num_updates"]):
                    sample = replay_buffer.sample(
                        config["batch_size"], to_gpu=config["use_gpu"])
                    model.update_critic(sample, logger=logger)
                    model.update_policies(sample, logger=logger)
                    model.update_all_targets()
                model.prep_rollouts(device='cpu')
        ep_rews = replay_buffer.get_average_rewards(
            config["episode_length"] * config["n_rollout_threads"])
        for a_i, a_ep_rew in enumerate(ep_rews):
            logger.add_scalar('agent%i/mean_episode_rewards' % a_i,
                              a_ep_rew * config["episode_length"], ep_i)

        if ep_i % config["save_interval"] < config["n_rollout_threads"]:
            model.prep_rollouts(device='cpu')
            os.makedirs(run_dir / 'incremental', exist_ok=True)
            model.save(run_dir / 'incremental' /
                       ('model_ep%i.pt' % (ep_i + 1)))
            model.save(run_dir / 'model.pt')

    model.save(run_dir / 'model.pt')
    env.close()
    logger.export_scalars_to_json(str(log_dir / 'summary.json'))
    logger.close()

if __name__ == '__main__':
    config = dict()

    config["env_id"] = "football"
    config["model_name"] = "MAAC11"
    config["n_rollout_threads"] = 2
    config["buffer_length"] = int(1e6)
    config["n_episodes"] = 100000000
    config["episode_length"] = 400
    config["steps_per_update"] = 100
    config["num_updates"] = 4
    config["batch_size"] = 4096
    config["save_interval"] = 1000
    config["pol_hidden_dim"] = 128
    config["critic_hidden_dim"] = 128
    config["attend_heads"] = 4
    config["pi_lr"] = 0.001
    config["q_lr"] = 0.001
    config["tau"] = 0.001
    config["gamma"] = 0.99
    config["reward_scale"] = 100.0
    config["use_gpu"] = True

    # Google Football Configure Flags
    config["academy_scenario"] = "academy_single_goal_versus_lazy"
    config["scoring"] = "scoring,checkpoints"
    config["render_mode"] = False
    config["num_to_control"] = 11

    run(config)


from ABC_code import *

class ABC:
    """
    ABC simulator class
    args:
        -'acc': desired acceptance rate
        -'model_object': model object
        -'distance_object': distance object
        -'G0': reference graph to fit for posterior
        -'eps': numerical cutoff value (if applicable)
            -uses acceptance rate if eps == None (default)
        -'hamiltonian': hamiltonian function
            -h(d,eps) is a function of distance d and cutoff scale eps
            -uses hard thresholding hamiltonian by default
    """

    def __init__(self,acc,model_object,distance_object,eps='None',hamiltonian='None',**hamiltonian_kwargs):
        self.acc = acc
        self.model = model_object
        self.distance = distance_object
        self.G0 = self.distance.G0
        self.eps = eps
        self.hamiltonian = hamiltonian
        self.hamiltonian_kwargs = hamiltonian_kwargs

        self.posterior = dict.fromkeys(self.model.free_param_names)
        self.acceptance_rate = 'None'

    def get_eps_from_acc(self,max_runs='None',tol='None',eps_lower_init='None',eps_upper_init='None',\
                         window_size='None',num_sims='None'):
        """
        finds epsilon value that gives approximately the acceptance rate 'acc'
            for the given distance object
        stores this as self.eps
        takes moving average of acceptance rate and tries to get within 'tol' of 'acc'
        updates guess for epsilon by repeatedly bisecting an interval
        args:
            -'eps_init': initial guess for epsilon
            -'max_runs': number of runs algorithm allowed to run for if acc_current
                    never within 'tol' of 'acc'
            -'tol': tolerance of error for accepting acc_current
            -'eps_lower/upper_init': lower/upper cutoff for epsilon bisection
        """

        eps_currents = []
        for _ in range(num_sims):
            acc_current,run_count = 100,0
            eps_upper = eps_upper_init
            eps_lower = eps_lower_init
            proposal_results = [False]*window_size
            while (run_count < max_runs):

                eps_current = np.random.rand()*(eps_upper-eps_lower) + eps_lower

                for w in range(window_size):

                    G_prop = self.model.generate_graph()
                    params_prop = self.model.free_parameters

                    dist = self.distance.evaluate_unnormalized(G_prop)

                    if np.log(np.random.rand()) < -self.hamiltonian(dist,eps_current,**self.hamiltonian_kwargs):
                        proposal_results[w] = True
                    else:
                        proposal_results[w] = False

                acc_current = Counter(proposal_results)[True]/window_size

                if abs(acc_current-self.acc) > tol:
                    if acc_current < self.acc:
                        eps_lower = eps_current
                    else:
                        eps_upper = eps_current
                    eps_current = (eps_upper + eps_lower)/2

                else:
                    break

                run_count += 1
            eps_currents.append(eps_current)

        self.eps = np.mean(eps_currents)

    def run_sims(self,n_runs='None'):
        """
        runs ABC with soft thresholding (hard thresholding imposed by the default hamiltonian h_hard_cutoff)
        n_runs is desired number of runs (proposals) for the simulation
        sets self.accepted_params and self.acceptance_rate
        """

        accepted_params = []
        proposal_results = []
        run_count = 0
        while (run_count < n_runs):

            G_prop = self.model.generate_graph()
            params_prop = copy.deepcopy(self.model.free_parameters)

            dist = self.distance.evaluate_unnormalized(G_prop)

            if np.log(np.random.rand()) < -self.hamiltonian(dist,self.eps,**self.hamiltonian_kwargs):
                proposal_results.append(True)
                accepted_params.append(params_prop)
            else:
                proposal_results.append(False)

            run_count += 1

        params = self.model.free_param_names
        posterior_dict = dict.fromkeys(params)
        for p in params:
            posterior_dict[p] = [res[p] for res in accepted_params]
        self.posterior = posterior_dict
        self.acceptance_rate = Counter(proposal_results)[True]/n_runs

    def save_to_file(self,filepath,**save_kwargs):
        """
        write to csv with column names as parameter names and column values as the
            sampled values of that parameter
        """
        try:
            df = pd.DataFrame.from_dict(self.posterior)
            df.to_csv(filepath,**save_kwargs)
        except:
            print('No posterior data to write!')


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: wangfeng
# @Date:   2022-03-30 19:48:29
# @Last Modified by:   wangfeng
# @Last Modified time: 2022-03-30 23:40:32

from typing import List

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
    	if not digits: return []
    	phones = {
    		'2': {'a','b','c'},
    		'3': {'e','d','f'},
    		'4': {'g','h','i'},
    		'5': {'j','k','l'},
    		'6': {'m','n','o'},
    		'7': {'p','q','r', 's'},
    		'8': {'t','u','v'},
    		'9': {'w','x','y', 'z'}
    	}

    	res = []

    	def backtrack(conbination, nextdigit):
    		if not len(nextdigit):
    			res.append(conbination)
    		else:
    			for letter in phones[nextdigit[0]]:
    				backtrack(conbination + letter, nextdigit[1:])

    	backtrack('', digits)
    	return res

s = Solution()
print(s.letterCombinations(''))