#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt

def ft(t, f_t):
	F_f = np.fft.fft(f_t)*np.array([np.exp(1j*np.pi*i) for i in range(len(t))])
	f = np.fft.fftfreq(len(t))
	return f, F_f

if __name__ == '__main__':
	t = np.arange(-100, 100, 0.1)
	f_t = np.sinc(t)*np.sin(2*np.pi*1*t)
	f, F_f = ft(t, f_t)

	fig1 = plt.figure()
	ax1 = fig1.add_axes([0.1, 0.1, 0.8, 0.8])
	l1 = ax1.plot(t, f_t)
	ax1.set_xlim(-10, 10)
	ax1.set_ylim(-1, 1)

	fig2 = plt.figure()
	ax2 = fig2.add_axes([0.1, 0.1, 0.8, 0.8])
	l2 = ax2.plot(f, F_f.imag)
	ax2.set_xlim(-0.5, 0.5)
	ax2.set_ylim(-10, 10)
	plt.show()


from kafka import KafkaConsumer
from kafka import KafkaProducer
import thread
import tweepy
import nltk
import json
import math
import time
import threading
import sys
import pymongo
from vaderSentiment.vaderSentiment import sentiment as vaderSentiment

class TwitterFeed():
    def __init__(self,bad,good,spam,bad2,good2,spam2,bad3,good3,spam3,track,stop,user):
        db = pymongo.MongoClient()
        data = db.LOGIN.login.find_one({'_id':int(user)})
        consumer_key = data['consumer_key']
        consumer_secret= data['consumer_secret']
        access_token= data['access_token']
        access_token_secret= data['access_token_secret']
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token,access_token_secret)
        self.stream = tweepy.Stream(auth, TwitterListener(bad,good,spam,bad2,good2,spam2,bad3,good3,spam3,stop,user),timeout=259200)
        self.stream.filter(track=[track])

    def close(self):
        self.stream.disconnect()
        return True

class TwitterListener(tweepy.StreamListener):
    def __init__(self,bad,good,spam,bad2,good2,spam2,bad3,good3,spam3,stop,user):
        self.producer = KafkaProducer(bootstrap_servers='localhost:9092')
        self.topicbad = 'badtopic'
        self.topicgood = 'goodtopic'
        self.topicspam= 'spamtopic'
        self.bad = bad
        self.good= good
        self.spam = spam
        self.bad2 = bad2
        self.good2= good2
        self.spam2 = spam2
        self.bad3 = bad3
        self.good3= good3
        self.spam3 = spam3
        self.numstop = stop
        self.user = user

    def on_data(self, data):
        fil = open("meu.txt","a")
        stop=set(nltk.corpus.stopwords.words('english'))
        stop.update(['http','https','rt'])
        tweet = json.loads(data)
        if 'text' in tweet:
            texto = tweet['text'].encode('utf-8','ignore')
            self.numstop -=1
            tokens = nltk.word_tokenize(tweet['text'])
            guess = help(self.bad,self.good,self.spam,tweet['text'])
            guess2= help(self.bad2,self.good2,self.spam2,tweet['text'])
            guess3= help(self.bad3,self.good3,self.spam3,tweet['text'])
            totalguess = guess + guess2 + guess3
            #print('RealTweet: ' + texto + '\n')
            if totalguess>0: #and (guess2=='pos' or guess3=='pos'):
                #print('Positive: ' + texto + '\n')
                texto = self.user+'-'+texto
                self.producer.send(self.topicgood,texto)
                saveTweet('pos',tweet,texto,totalguess,self.user)
                saveLocation('pos',tweet,self.user)
                fil.write('POSITIVE:\n' + 'One Word:' + str(guess) + '\nTwo Word:' + str(guess2) + '\nThree Word:' + str(guess3) + '\nTotal guess:' +  str(totalguess) + '\n' + str(texto) + '\n\n\n')
                fil.close()
            elif totalguess<0: #and (guess2=='neg' or guess3=='neg'):
                #print('Negative: ' + texto + '\n')
                texto = self.user+'-'+texto
                self.producer.send(self.topicbad,texto)
                saveTweet('neg',tweet,texto,totalguess,self.user)
                saveLocation('neg',tweet,self.user)
                fil.write('NEGATIVE:\n' + 'One Word: ' + str(guess) + '\nTwo Word:' + str(guess2) + '\nThree Word:' + str(guess3) + '\nTotal guess:' + str(totalguess) + '\n' + str(texto) + '\n\n\n')
                fil.close()
            else:
                #print('Negative: ' + texto + '\n')
                texto = self.user+'-'+texto
                self.producer.send(self.topicbad,texto)
                saveTweet('spam',tweet,texto,totalguess,self.user)
                saveLocation('spam',tweet,self.user)
                fil.write('NEGATIVE:\n' + 'One Word: ' + str(guess) + '\nTwo Word:' + str(guess2) + '\nThree Word:' + str(guess3) + '\nTotal guess:' + str(totalguess) + '\n' + str(texto) + '\n\n\n')
                fil.close()

            vs = vaderSentiment(str(texto))
            contagemneg= vs['neg']
            contagempos= vs['pos']
            contagemspam=vs['neu']
            filo= open("vader.txt",'a')
            filo.write('Neg:' + str(contagemneg) + '\nPos:' + str(contagempos) + '\nNeutro:' + str(contagemspam) + '\nTotal guess:' + str(totalguess) + ' ' + str(texto) + '\n\n\n')
            filo.close()
            #elif (guess=='spam'): #and guess2=='spam' and guess3=='spam'):
                #print('Spam: ' + texto + '\n')
                #self.producer.send(self.topicspam,texto)
                #saveTweet('spam',tweet)
            #else:
                #print(guess + ':\n' + texto + '\n')
                #self.producer.send('drawtopic',texto)
                #saveTweet('spam',tweet)
            if self.numstop == 0:
                return False
        return True

def help(bad,good,spam,tweet):
        contagempos = 0.0
        contagemneg = 0.0
        contagemspam = 0.0
        contagem = 0.0

        texto = tweet.encode('ascii','ignore')
        stop=set(nltk.corpus.stopwords.words('english'))
        stop.update(['http','https','rt'])
        tokens = nltk.word_tokenize(tweet)

        for w in tokens:
            w = w.lower()
            w = w.encode('ascii', 'ignore')
            if w.isalpha() and w not in stop:
                if w in bad.keys():
                    contagemneg +=math.log10(bad[w])
                if w in good.keys():
                    contagempos +=math.log10(good[w])
        if contagempos == contagemneg:
            return 0
        if contagempos < contagemneg:
            if contagempos >= 0:
                return contagempos
            else:
                return (contagempos * -1)
        else:
            if contagemneg >= 0:
                return (contagemneg * -1)
            else:
                return contagemneg

def saveLocation(tipo,tweet,user):
    db = pymongo.MongoClient()
    board = db.BOARD
    if tweet['coordinates']:
        if tipo == 'pos':
            board.goodlocation.insert_one({"tweet":tweet['text'] , "created_at":tweet['created_at'] , "location": tweet['coordinates']})
        else:
            board.badlocation.insert_one({"tweet":tweet['text'] , "created_at":tweet['created_at'] , "location": tweet['coordinates']})

def saveTweet(tipo,tweet,texto,totalguess,user):
    db = pymongo.MongoClient()
    aux = 'BOARD'+user
    board = db[aux]
    id_str= tweet['user']['id']
    n_followers=tweet['user']['followers_count']
    location= tweet['user']['location']
    name = tweet['user']['name']
    friends_count = tweet['user']['friends_count']
    timenow = time.strftime("%Y-%m-%dT%H:%M:%S")

    if(tipo == 'pos'):
        board.goodcounter.update({"type":'contagem'},{"$inc": {'count':1}},upsert=True)
        auxcount = board.goodcounter.find_one()
        auxcount = auxcount["count"]
        board.goodinfo.insert_one({"_id": auxcount , "id_str":id_str , "n_followers":n_followers , "location": location , "name": name , "friends_count": friends_count, "time":timenow , "tweet":texto , "guess":totalguess})

    elif(tipo == 'neg'):
        board.badcounter.update({"type":'contagem'},{"$inc": {'count':1}},upsert=True)
        auxcount = board.badcounter.find_one()
        auxcount = auxcount["count"]
        board.badinfo.insert_one({"_id": auxcount , "id_str":id_str , "n_followers":n_followers , "location": location , "name": name , "friends_count": friends_count, "time":timenow, "tweet":texto , "guess":totalguess})

    elif(tipo == 'spam'):
        board.spamcounter.update({"type":'contagem'},{"$inc": {'count':1}},upsert=True)
        auxcount = board.spamcounter.find_one()
        auxcount = auxcount["count"]
        board.spaminfo.insert_one({"_id": auxcount , "id_str":id_str , "n_followers":n_followers , "location": location , "name": name , "friends_count": friends_count, "time":timenow, "tweet":texto, "guess":totalguess})

class StopThread(StopIteration): pass

threading.SystemExit = SystemExit, StopThread

class Thread2(threading.Thread):

    def stop(self):
        self.__stop = True

    def _bootstrap(self):
        if threading._trace_hook is not None:
            raise ValueError('Cannot run thread with tracing!')
        self.__stop = False
        sys.settrace(self.__trace)
        super()._bootstrap()

    def __trace(self, frame, event, arg):
        if self.__stop:
            raise StopThread()
        return self.__trace

class Learning():
    def __init__(self,bad,good,spam,bad2,good2,spam2,bad3,good3,spam3,tracker,stop,user):
        self.tracker = tracker
        self.feed= TwitterFeed
        self.t = Thread2(target=self.feed , args=(bad,good,spam,bad2,good2,spam2,bad3,good3,spam3,self.tracker,stop,user,))

    def start(self):
        self.t.start()
        #print 'start'
        self.t.join()
        #print 'end'
        return True

    def stop(self):
        self.t.stop()


from cgi import print_exception
import csv
import collections
from re import sub
import numpy as np
import itertools
import os
import pandas as pd
from openpyxl import load_workbook
import traceback

num_top_values = 10

def school_usage_analysis():
    try:
        i = 0
        prices_dict = collections.defaultdict(list)
        volume_dict = collections.defaultdict(list)
        catalog_nums_dict = collections.defaultdict(list)
        descs_dict = collections.defaultdict(list)
        numbered_prices_dict = collections.defaultdict(list)
        numbered_volume_dict = collections.defaultdict(list)

        if "uploads" not in os.getcwd():
            os.chdir(os.path.join(os.getcwd(), "uploads"))
        data_file = os.listdir(os.getcwd())[0]
        sheet_name = load_workbook(data_file, read_only=True,
                                keep_links=False).sheetnames[0]
        rows = pd.read_excel(
            data_file, sheet_name=sheet_name).to_numpy()

        rows = pd.read_excel(
            data_file, sheet_name=sheet_name).to_numpy()

        rows = sorted(rows, key=lambda row: row[3])
        j = 0
        for r in rows:

            r = [str(i) for i in r]
            for i in range(len(r)):
                if r[i] == 'nan':
                    r[i] = ''

            part_number = r[0]
            if r[13] != '':
                prices_dict[r[3]].append(float(sub(r'[^\d.]', '', r[13])))
                volume_dict[r[3]].append(r[11])
                catalog_nums_dict[j].append(r[10])
                descs_dict[j].append(r[8])
                numbered_prices_dict[j] = (float(sub(r'[^\d.]', '', r[13])))
                numbered_volume_dict[j] = int(r[11])

                j += 1

        prices_idx = 0
        f = open('output=school_usage_analysis.csv', 'w', newline='')
        writer = csv.writer(f)

        for idx, school in enumerate(prices_dict):
            prices_arr_size = len(list(prices_dict.values())[idx])

            truncated_prices_dict = dict(itertools.islice(
                numbered_prices_dict.items(), prices_idx, prices_idx + prices_arr_size))
            sorted_tuples = sorted(truncated_prices_dict.items(),
                                key=lambda item: item[1], reverse=True)
            sorted_prices_dict = {k: v for k, v in sorted_tuples}

            truncated_volume_dict = dict(itertools.islice(
                numbered_volume_dict.items(), prices_idx, prices_idx + prices_arr_size))
            sorted_tuples = sorted(truncated_volume_dict.items(),
                                key=lambda item: item[1], reverse=True)
            sorted_volume_dict = {k: v for k, v in sorted_tuples}

            top_by_price_values = list(sorted_prices_dict.values())[:num_top_values]
            top_by_volume_values = list(sorted_volume_dict.values())[:num_top_values]
            top_by_price_keys = list(sorted_prices_dict.keys())[:num_top_values]
            # print(top_by_price_keys)
            top_by_volume_keys = list(sorted_volume_dict.keys())[:num_top_values]
            # print(top_by_volume_keys)

            writer.writerow([school])
            writer.writerow(["", "Top Purchased By Price"])
            for key in top_by_price_keys:
                writer.writerow(["", "", catalog_nums_dict[key][0],
                                ''.join(descs_dict[key]), numbered_prices_dict[key]])
            writer.writerow(["", "Top Purchased By Volume"])
            for key in top_by_volume_keys:
                writer.writerow(["", "", catalog_nums_dict[key][0],
                                ''.join(descs_dict[key]), numbered_volume_dict[key]])

                #     f.write("%s, %s, %s\n" % (str(catalog_nums_dict[key]),
                #                               str(descs_dict[key]), numbered_prices_dict[key]))
            writer.writerow([])
            prices_idx = prices_arr_size + prices_idx

        f.close()
        return "True"
    except Exception:
        return traceback.print_exc()


import json
import data_reader
import json_reader

class unit_json(object):
    def __init__(self):
        pass

    def Xlsx2json():
        reader = data_reader.XLSReader()
        datas = reader.read()
        # print(datas)
        df = datas.iloc[:525, :]
        df = df.reset_index()
        df.columns=['Time', 'value',  'value', 'value']
        df2 = df.iloc[:, :2]
        aa = df2.to_json(orient='records')

        return aa

    def json_write(aa):
        TempData = aa
        with open("cache_data/interface/TempData.json", "w") as f:
            json.dump(TempData, f)
        print("写入完成")

class mydata2json(object):
    def __init__(self):
        pass

    def CThome_json():
        filename = 'cache_data/interface/alarm_home.json'
        with open(filename, 'r', encoding='UTF-8') as f:
            aa = json.load(f)
        return aa

    def CTrm_json():
        filename = 'cache_data/interface/alarm_mes.json'
        with open(filename, 'r', encoding='UTF-8') as f:
            aa = json.load(f)
        pltdata = json_reader.pltdata()

        alarmitems = aa['alarm_mes_%s'%pltdata]
        return alarmitems

    def DA_original_data():
        # aa = unit_json.Xlsx2json()
        # unit_json.json_write(aa)
        filename = 'cache_data/interface/DA_original_data.json'
        with open(filename, 'r', encoding='UTF-8') as f:
            aa = json.load(f)
        return aa

# aa = mydata2json.DA_original_data()
# print(aa)


from base import async_insert, pipeline, clean_db
import getpass
import psycopg2
import threading
import time

def test_combine_table(pipeline, clean_db):
  pipeline.create_stream('s', x='int')
  pipeline.create_cv('combine_table',
                     'SELECT x::int, COUNT(*) FROM s GROUP BY x')

  values = [(i,) for i in xrange(1000)]
  pipeline.insert('s', ('x',), values)

  pipeline.execute('SELECT * INTO tmprel FROM combine_table_mrel')

  stop = False
  ninserts = [0]

  def insert():
    while not stop:
      pipeline.insert('s', ('x',), values)
      ninserts[0] += 1
      time.sleep(0.01)

  t = threading.Thread(target=insert)
  t.start()

  time.sleep(2)

  conn = psycopg2.connect('dbname=postgres user=%s host=localhost port=%s' %
                          (getpass.getuser(), pipeline.port))
  cur = conn.cursor()
  cur.execute("SELECT pipelinedb.combine_table('combine_table', 'tmprel')")
  conn.commit()
  conn.close()

  stop = True
  t.join()

  assert ninserts[0] > 0

  rows = list(pipeline.execute('SELECT count FROM combine_table'))
  assert len(rows) == 1000
  for row in rows:
    assert row[0] == ninserts[0] + 2

  pipeline.execute('DROP TABLE tmprel')

def test_combine_table_no_groups(pipeline, clean_db):
  pipeline.create_stream('s', x='int')
  pipeline.create_cv('no_groups', 'SELECT COUNT(*) FROM s')
  values = [(i,) for i in xrange(1000)]
  pipeline.insert('s', ('x',), values)

  pipeline.execute('SELECT * INTO tmprel FROM no_groups_mrel')
  pipeline.execute("SELECT pipelinedb.combine_table('no_groups', 'tmprel')")

  rows = pipeline.execute('SELECT count FROM no_groups')
  assert len(rows) == 1
  assert len(rows[0]) == 2
  assert rows[0][0] == 2000

def test_pipeline_flush(pipeline, clean_db):
  pipeline.execute('SET pipelinedb.stream_insert_level=async')
  pipeline.create_stream('s', x='int')

  pipeline.create_cv('flush', 'SELECT x, cq_sleep(0.01) FROM s')

  values = [(i,) for i in xrange(1000)]
  start = time.time()

  # This will take 0.01 * 1000 = 10s to process but return immediately since
  # inserts are async and values will fit in one batch.
  pipeline.insert('s', ('x',), values)
  insert_end = time.time()

  pipeline.execute('SELECT pipelinedb.flush()')
  flush_end = time.time()

  assert insert_end - start < 0.1
  assert flush_end - start > 10

  row = list(pipeline.execute('SELECT count(*) FROM flush'))[0]
  assert row[0] == 1000

  pipeline.execute('SET pipelinedb.stream_insert_level=sync_commit')


import sys
sys.path.append('chapter3')
import math

from scipy.stats import expon, norm, uniform
from robot import Robot

class RealRobot(Robot):
    def __init__(self, initial_pose, camera=None, color='black',
                 noise_per_meter=5, noise_std=math.pi / 60,
                 bias_rate_stds=(0.1, 0.1),
                 expected_stuck_time=1e100, expected_escape_time=1e-100,
                 expected_kidnap_time=1e100,
                 kidnap_x_range=(-5.0, 5.0),
                 kidnap_y_range=(-5.0, 5.0)):
        super(RealRobot, self).__init__(initial_pose, camera, 0.2, color)
        self._noise_pdf = expon(scale=1.0 / (1e-100 + noise_per_meter))
        self._distance_until_noise = self._noise_pdf.rvs()
        self._theta_noise = norm(scale=noise_std)
        self._vel_bias_rate = norm.rvs(loc=1.0, scale=bias_rate_stds[0])
        self._omega_bias_rate = norm.rvs(loc=1.0, scale=bias_rate_stds[1])

        self._stuck_pdf = expon(scale=expected_stuck_time)
        self._time_until_stuck = self._stuck_pdf.rvs()
        self._escape_pdf = expon(scale=expected_escape_time)
        self._time_until_escape = self._escape_pdf.rvs()
        self._is_stuck = False

        self._kidnap_pdf = expon(scale=expected_kidnap_time)
        self._time_until_kidnap = self._kidnap_pdf.rvs()
        range_x = kidnap_x_range
        range_y = kidnap_y_range
        self._kidnap_distribution = uniform(loc=(range_x[0], range_y[0], 0.0),
                                            scale=(range_x[1] - range_x[0], range_y[1] - range_y[0], 2 * math.pi))

    def one_step(self, ut, delta_t):
        self._pose = self._compute_pose(ut, delta_t)
        self._trajectory.append(self._pose)
        return self._camera.observe(self._pose) if self._camera else None

    def _compute_pose(self, ut, delta_t):
        vel, omega = self._add_bias(ut)
        vel, omega = self._check_stuck(vel, omega, delta_t)
        next_pose = self.transition_function(vel, omega, delta_t, self._pose)
        next_pose = self._add_noise(next_pose, vel, omega, delta_t)
        next_pose = self._kidnap(next_pose, delta_t)
        return next_pose

    def _add_bias(self, ut):
        vel, omega = ut
        return vel * self._vel_bias_rate, omega * self._omega_bias_rate

    def _add_noise(self, pose, vel, omega, delta_t):
        self._distance_until_noise -= abs(vel) * \
            delta_t + self._radius * abs(omega) * delta_t
        if self._distance_until_noise <= 0.0:
            self._distance_until_noise += self._noise_pdf.rvs()
            pose[2] += self._theta_noise.rvs()
        return pose

    def _check_stuck(self, vel, omega, delta_t):
        if self._is_stuck:
            self._time_until_escape -= delta_t
            if self._time_until_escape <= 0.0:
                self._time_until_escape += self._escape_pdf.rvs()
                self._is_stuck = False
        else:
            self._time_until_stuck -= delta_t
            if self._time_until_stuck <= 0.0:
                self._time_until_stuck += self._stuck_pdf.rvs()
                self._is_stuck = True
        return vel * (not self._is_stuck), omega * (not self._is_stuck)

    def _kidnap(self, pose, delta_t):
        self._time_until_kidnap -= delta_t
        if self._time_until_kidnap <= 0.0:
            self._time_until_kidnap += self._kidnap_pdf.rvs()
            return np.array(self._kidnap_distribution.rvs()).T
        else:
            return pose

if __name__ == "__main__":
    from world import World
    from fixed_input_agent import FixedInputAgent
    from ideal_robot import IdealRobot
    import numpy as np
    world = World(time_span=30, time_interval=0.1)

    unbiased_robot = IdealRobot(np.array([0.0, 0.0, 0.0]).T, color='gray')
    unbiased_agent = FixedInputAgent(
        unbiased_robot, vel=0.2, omega=10.0 / 180 * math.pi)
    world.append_agent(unbiased_agent)

    biased_robot = RealRobot(np.array([0.0, 0.0, 0.0]).T,
                             noise_per_meter=0.0, bias_rate_stds=(0.2, 0.2),
                             expected_stuck_time=1e1000000,
                             expected_escape_time=0.0,
                             color='red')
    biased_agent = FixedInputAgent(
        biased_robot, vel=0.2, omega=10.0 / 180 * math.pi)
    world.append_agent(biased_agent)

    stucked_robot = RealRobot(np.array([0.0, 0.0, 0.0]).T,
                              noise_per_meter=0.0, bias_rate_stds=(0.0, 0.0),
                              expected_stuck_time=10,
                              expected_escape_time=10,
                              color='blue')
    stucked_agent = FixedInputAgent(
        stucked_robot, vel=0.2, omega=10.0 / 180 * math.pi)
    world.append_agent(stucked_agent)

    kidnap_robot = RealRobot(np.array([0.0, 0.0, 0.0]).T,
                             noise_per_meter=0.0, bias_rate_stds=(0.0, 0.0),
                             expected_stuck_time=1e100,
                             expected_escape_time=1e-100,
                             expected_kidnap_time=5,
                             color='black')
    kidnap_agent = FixedInputAgent(
        kidnap_robot, vel=0.2, omega=10.0 / 180 * math.pi)
    world.append_agent(kidnap_agent)

    world.draw()


import logging
from pathlib import Path
import synthtool as s
import subprocess
from synthtool.languages import php
from synthtool import _tracked_paths

logging.basicConfig(level=logging.DEBUG)

src = Path(f"../{php.STAGING_DIR}/Vision").resolve()
dest = Path().resolve()

# Added so that we can pass copy_excludes in the owlbot_main() call
_tracked_paths.add(src)

# Exclude partial veneer files.
php.owlbot_main(
    src=src,
    dest=dest,
    copy_excludes=[
        src / "*/src/V1/ProductSearchClient.php",
        src / "*/src/V1/ImageAnnotatorClient.php",
    ]
)

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# V1 is GA, so remove @experimental tags
s.replace(
    'src/V1/**/*Client.php',
    r'^(\s+\*\n)?\s+\*\s@experimental\n',
    '')

# Change the wording for the deprecation warning.
s.replace(
    'src/*/*_*.php',
    r'will be removed in the next major release',
    'will be removed in a future release')

### [START] protoc backwards compatibility fixes

# roll back to private properties.
s.replace(
    "src/**/V*/**/*.php",
    r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$",
    r"""Generated from protobuf field \1
     */
    private $""")

# prevent proto messages from being marked final
s.replace(
    "src/**/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/**/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

### [END] protoc backwards compatibility fixes

# fix relative cloud.google.com links
s.replace(
    "src/**/V*/**/*.php",
    r"(.{0,})\]\((/.{0,})\)",
    r"\1](https://cloud.google.com\2)"
)

# format generated clients
subprocess.run([
    'npx',
    '-y',
    '-p',
    '@prettier/plugin-php@^0.16',
    'prettier',
    '**/Gapic/*',
    '--write',
    '--parser=php',
    '--single-quote',
    '--print-width=80'])


from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy()
db.init_app(app)

@app.errorhandler(404)
def error_404(error):
    return render_template('error/404.html'), 404

from project.views import example

app.register_blueprint(example.mod)


from django.shortcuts import render,redirect
from userApp.forms import RegisterForm
# Create your views here.

def register(request):
    #只有post时，才表示用户提交了注册了信息
    #从get或者post中获取next的参数值
    #get中；next通过/?next=value
    #post 通过表单传递
    redirect_to = request.POST.get('next',request.GET.get('next',''))

    if request.method=='POST':
        #request.POST是一个类字典数据结构，记录了了用户提交的注册信息
        #实例化
        form = RegisterForm(request.POST)
        if form.is_valid():
            #如果数据提交的合法
            form.save()
            if redirect_to:
                return redirect(redirect_to)
            else:
                return redirect('/')
    else:
        #当请求不是post时，标明用户正在访问注册页面，返回一个空的表单给用户
        form = RegisterForm()
    #如果用户正在访问注册页面， 则则渲染一个空的注册表单
    #如果用户通过通过表单传递信息，但是数据验证不合法，则渲染一个常有错误信息的表单
    #将记录用户注册前页面的redirect_to传递给模板，以维持next参数在整个注册过程中的传递
    return render(request,'userApp/register.html',context={'form':form,'next':redirect_to})

def loginIndex(request):
    return render(request,'userApp/loginIndex.html')


import sys
import argparse

import pandas as pd
import yaml
import pickle

sys.path.append('.')

from src.model.HGBR import HGBR
from src.utils.experiment_utils import load_experiment_from_file, save_prediction_to_file, create_folder

def train_hgbr(config, datafile, destination):

    create_folder(destination)

    targets = config['hyperparameters'].keys()

    x_train, _, x_test_set, y_train, _, _ = load_experiment_from_file(datafile)
    models = {}
    print(x_train.shape)
    for t in targets:
        conf = config['hyperparameters'][t]
        print(conf)
        model = HGBR(**conf, input_scaler=None, output_scaler=None)
        print(y_train[[t]].head())
        model.fit(x_train, y_train[[t]].values)
        models[t] = model

    all_predictions = {}
    for sims in x_test_set.keys():
        pred_dict = {}
        for t in targets:
            pred_dict[t] = models[t].predict(x_test_set[sims]).flatten()
        prediction = pd.DataFrame.from_dict(pred_dict)
        all_predictions[sims] = prediction

    pickle.dump(models, open('{}/model.pkl'.format(destination), 'wb'))
    save_prediction_to_file('{}/prediction.h5'.format(destination), all_predictions)

    return all_predictions

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Raw data parser')
    parser.add_argument('--config_experiment', '-c',
                        dest='experimentconfig',
                        metavar='string',
                        help='path to model config file',
                        default='')

    args = parser.parse_args()

    if args.experimentconfig == '':
        print('Please provide an experiment configfile.')
        exit(1)

    configfile = args.experimentconfig

    with open(configfile, 'r') as f:
        config = yaml.safe_load(f)

    with open(config['dataconfig'], 'r') as f:
        dataconfig = yaml.safe_load(f)

    with open(config['modelconfig'], 'r') as f:
        modelconfig = yaml.safe_load(f)

    for sp in dataconfig['species']:
        experiment_data = 'data/experiment/{}/{}.h5'.format(dataconfig['experiment_name'], sp)
        save_results_to = '{}/{}/{}'.format(config['destination_folder'], config['experiment_name'], sp)
        train_hgbr(modelconfig, experiment_data, save_results_to)


import random

import cv2
import os
import numpy as np
import time
#from playsound import playsound

folderPath = 'frames'
mylist = os.listdir(folderPath)
graphic = [cv2.imread(f'{folderPath}/{imPath}') for imPath in mylist]
green = graphic[0];
red = graphic[1];
kill = graphic[2];
winner = graphic[3];
intro = graphic[4];

cv2.imshow('Squid Game', cv2.resize(intro, (0, 0), fx=0.69, fy=0.69))
cv2.waitKey(1)
while True:
    cv2.imshow('Squid Game',
               cv2.resize(intro, (0, 0), fx=0.69, fy=0.69))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
TIMER_MAX =45
TIMER =TIMER_MAX
maxMove = 6500000
font = cv2.FONT_HERSHEY_SIMPLEX
cap = cv2.VideoCapture(0)
frameHeight = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frameWidth = cap.get(cv2.CAP_PROP_FRAME_WIDTH)

win =False
prev=time.time()
showFrame = cv2.resize(green,(0,0),fx=0.69,fy=0.69)
isgreen=True

while cap.isOpened() and TIMER>=0:
    if isgreen and (cv2.waitKey(10) & 0xFF == ord('w')):
        win = True
        break
    ret,frame = cap.read()
    cv2.putText(showFrame,str(TIMER),(50,50),font,1,
                (0,int(255*(TIMER)/TIMER_MAX),int(255*(TIMER_MAX-TIMER)/TIMER_MAX)),4,cv2.LINE_AA)


from setuptools import find_packages, setup

VERSION = "0.2.0"
DESCRIPTION = "A Pytorch & lightning  Wrapper, for rapid prototyping"

with open("README.md", encoding="utf-8") as f:
    long_description = f.read()

with open("requirements.txt") as f:
    requirements = f.readlines()

setup(
    name="torchood",
    version=VERSION,
    author="Anant Gupta",
    author_email="anantgupta129@gmail.com",
    description=DESCRIPTION,
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/anantgupta129/TorcHood",
    packages=find_packages(exclude=["*tutorials*", "*notebooks*"]),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.8",
    install_requires=requirements,
)


#!/usr/bin/env python3

#This is the main code which will run the inferior threads.

from ppadb.client import Client as AdbClient
import time
from bs4 import BeautifulSoup
import urllib.request
from IPython.display import HTML
import smtplib
import facilities_mon as fm
import pressure_mon as pm
import json
import time
import threading
import sys
import tools

#Initializing starting variables.
print("Initializing settings...")

json_file_path_1="/home/eliade/Desktop/Annealing_Alarming/Python_Grafana/conf_files/main_conf.json"
json_file_path_2="/home/eliade/Desktop/Annealing_Alarming/Python_Grafana/conf_files/facilities_conf.json"
json_file_path_3="/home/eliade/Desktop/Annealing_Alarming/Python_Grafana/conf_files/vacuum_conf.json"
threads = []
stop = False

#Opening json file. Otherwise email.
try:
    with open(json_file_path_1,"r") as file:
        json_conf_1 = json.load(file)
    time.sleep(0.5)
    with open(json_file_path_2,"r") as file2:
    	json_conf_2=json.load(file2)
    time.sleep(0.5)
    with open(json_file_path_3,"r") as file3:
    	json_conf_3=json.load(file3)
    time.sleep(0.5)

except Exception as e:
    print("Cannot open json file. Reason: "+str(e))
    stop = True
    tools.SendEmail_mem()

time.sleep(1)

#Loading important values from json configuration file.
host =json_conf_1["InfluxDB"]["host"]
port =json_conf_1["InfluxDB"]["port"]
database = json_conf_1["InfluxDB"]["database"]
measurement = json_conf_1["InfluxDB"]["msr_name"]
url = json_conf_1["Call-Monitor_Variables"]["url"]
timeout = json_conf_1["Call-Monitor_Variables"]["timeout"]
thp = json_conf_1["Call-Monitor_Variables"]["thp"]
tht = json_conf_1["Call-Monitor_Variables"]["tht"]

time.sleep(1)

#Starting threads
try:

    x=threading.Thread(target=fm.Facilities_Monitoring,args = (host, port, database,url, timeout, lambda:stop,), name="Power_Out_Check")
    x.daemon=True
    threads.append(x)

    y=threading.Thread(target=pm.VacAlert, args =(thp, tht, host, port, database, measurement,  lambda: stop,), name="Vac_Alarm_Monitor")
    y.daemon=True
    threads.append(y)

    #Starting defined threads
    for i in threads:
        i.start()
        print("Starting "+str(i.name)+".\n")

    #Entering infinite loop
    while(True):
        time.sleep(30)

#If error encountered (example keyboard interrupt) code initializes closing procedures.
except:
    print("Sending STOP signal.")

    stop=True

    #Waiting for threads to successfully end.
    time.sleep(1)

    #When script ends, values get reset.
    print("Completing ending script protocols!")

    json_conf_2["Call-Monitor_Variables"]["UFT"]=0
    json_conf_2["Call-Monitor_Variables"]["UFM"]=0
    json_conf_3["Call-Monitor_Variables"]["PF"]=0
    json_conf_3["Call-Monitor_Variables"]["TF"]=0
    json_conf_2["Call-Monitor_Variables"]["CN"]=0
    json_conf_2["Call-Monitor_Variables"]["SEU"]=0

    with open(json_file_path_1,"w") as file:
        json.dump(json_conf_1, file,indent=2)
    with open(json_file_path_2,"w") as file:
        json.dump(json_conf_2, file,indent=2)
    with open(json_file_path_3,"w") as file:
        json.dump(json_conf_3, file,indent=2)

    time.sleep(1)

    print("Main Finishing Execution.")

    #Exiting program.
    sys.exit()


import datetime

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.sensors.bash import BashSensor
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.operators.python import PythonOperator

default_args = {"owner": "airflow"}
connection_id = 'dwh'
default_end_time = '2999-12-31 23:59:59'

create_stg_inventory_sql = """
CREATE TABLE IF NOT EXISTS stg_inventory (
    product_id VARCHAR,
    amount DECIMAL,
    inventory_date DATE
);
"""

create_fact_inventory_snapshot_sql = """
CREATE TABLE IF NOT EXISTS fact_inventory_snapshot (
    product_id VARCHAR,
    amount DECIMAL,
    snapshot_date_id INT
);
"""

trans_fact_inventory_snapshot_sql = """

    DELETE FROM fact_inventory_snapshot WHERE snapshot_date_id = to_char(DATE('{{ ds }}') - INTERVAL '1 day', 'YYYYMMDD')::integer;

    INSERT INTO fact_inventory_snapshot(product_id, amount, snapshot_date_id)
    SELECT * FROM fact_inventory_snapshot
    WHERE snapshot_date_id = to_char(DATE('{{ ds }}') - INTERVAL '2 day', 'YYYYMMDD')::integer
    AND product_id NOT IN (
    SELECT DISTINCT product_id FROM stg_inventory WHERE inventory_date = DATE('{{ ds }}') - INTERVAL '1 day')

    UNION ALL
    SELECT
    product_id,
    amount,
    to_char(DATE('{{ ds }}') - INTERVAL '1 day', 'YYYYMMDD')::integer AS snapshot_date_id
    FROM stg_inventory WHERE inventory_date = DATE('{{ ds }}') - INTERVAL '1 day';
"""

def load_csv_to_postgres(table_name, **kwargs):
    csv_filepath = kwargs['csv_filepath']
    connecion = PostgresHook(postgres_conn_id=connection_id)
    connecion.copy_expert(f"COPY {table_name} FROM STDIN DELIMITER ',' CSV HEADER", csv_filepath)
    return table_name

with DAG(
        dag_id="inventory_batch_pipeline",
        start_date=datetime.datetime(2020, 1, 1),
        schedule_interval="@once",
        default_args=default_args,
        catchup=False,
) as dag:

    create_stg_inventory_table = PostgresOperator(
        task_id="create_stg_inventory_table",
        postgres_conn_id=connection_id,
        sql=create_stg_inventory_sql,
    )

    load_inventory_to_stg_inventory_table = PythonOperator(
        task_id='load_inventory_to_stg_inventory_table',
        python_callable=load_csv_to_postgres,
        op_kwargs={
            'csv_filepath': "/data/raw/inventory_{{ ds }}.csv",
            'table_name': 'stg_inventory'
        },
    )

    create_fact_inventory_snapshot_table = PostgresOperator(
        task_id="create_fact_inventory_snapshot_table",
        postgres_conn_id=connection_id,
        sql=create_fact_inventory_snapshot_sql,
    )

    transform_fact_inventory_snapshot_table = PostgresOperator(
        task_id="transform_fact_inventory_snapshot_table",
        postgres_conn_id=connection_id,
        sql=trans_fact_inventory_snapshot_sql,
    )

    create_stg_inventory_table >> load_inventory_to_stg_inventory_table >> create_fact_inventory_snapshot_table >> transform_fact_inventory_snapshot_table


import gensim.models as g
import codecs
import numpy
import numpy as np

model_path = './data/zhiwiki_news.doc2vec'
start_alpha = 0.01
infer_epoch = 1000
docvec_size = 192

def simlarityCalu(vector1, vector2):
    vector1Mod = np.sqrt(vector1.dot(vector1))
    vector2Mod = np.sqrt(vector2.dot(vector2))
    if vector2Mod != 0 and vector1Mod != 0:
        simlarity = (vector1.dot(vector2)) / (vector1Mod * vector2Mod)
    else:
        simlarity = 0
    return simlarity

def doc2vec(file_name, model):
    import jieba
    doc = [w for x in codecs.open(file_name, 'r', 'utf-8').readlines() for w in jieba.cut(x.strip())]
    doc_vec_all = model.infer_vector(doc, alpha=start_alpha, steps=infer_epoch)
    return doc_vec_all

if __name__ == '__main__':
    model = g.Doc2Vec.load(model_path)
    p1 = './data/P1.txt'
    p2 = './data/P2.txt'
    P1_doc2vec = doc2vec(p1, model)
    P2_doc2vec = doc2vec(p2, model)
    print(simlarityCalu(P1_doc2vec, P2_doc2vec))


from django.urls import path
from .views import BasketListView, CreateCategory, CreateProduct, CreateShop, CreateTag, DeleteShop, PanelView, ProductListView, ShopDetailView, ShopListView, UpdateShop, BasketDetailView, ChartView
from .api_views import BasketActive, BasketVerify, BasketVerifyView, CartItemAddView, CartItemDelView, CartItemView, ProductAPIList, ShopAPIList, CartItemAddOneView, CartItemReduceOneView

urlpatterns = [
    path('panel/', PanelView.as_view(), name='panel'),
    path('create_shop/', CreateShop.as_view(), name='create_shop'),
    path('create_product/<int:pk>', CreateProduct.as_view(), name='create_product'),
    path('create_category/', CreateCategory.as_view(), name='create_category'),
    path('create_tag/', CreateTag.as_view(), name='create_tag'),
    path('shop_list/', ShopListView.as_view(), name='shop-list'),
    path('Product_list/<int:pk>', ProductListView.as_view(), name='product-list'),
    path('basket_list/<str:name>/', BasketListView.as_view(), name='basket-list'),
    path('shop_detail/<int:pk>', ShopDetailView.as_view(), name='shop-detail'),
    path('basket_detail/<int:pk>', BasketDetailView.as_view(), name='basket-detail'),
    path('update_shop/<int:pk>', UpdateShop.as_view(), name='update_shop'),
    path('delete_shop/<int:pk>', DeleteShop.as_view(), name='delete_shop'),
    path('chart/<int:pk>', ChartView.as_view(), name='chart'),
    # api
    path('api/shop_list/', ShopAPIList.as_view(), name='shop_api_list'),
    path('api/shop_list/<int:pk>',
         ProductAPIList.as_view(), name='product_api_list'),
    path('api/basket_act/', BasketActive.as_view(), name='active'),
    path('api/basket_vrf/', BasketVerify.as_view(), name='verify'),
    path('api/cart_item/', CartItemView.as_view(), name='cart_item'),
    path('api/cart_add/', CartItemAddView.as_view(), name='cart_add'),
    path('api/cart_del/<int:pk>', CartItemDelView.as_view(), name='cart_del'),
    path('api/add_one/<int:pk>/', CartItemAddOneView.as_view(), name='add_cart'),
    path('api/reduce_one/<int:pk>/',
         CartItemReduceOneView.as_view(), name='reduce_cart'),
    path('api/verify/<int:pk>/', BasketVerifyView.as_view(), name='verify'),

]


'''Regional EDA'''
from dataclasses import dataclass
import pandas as pd
from overrides import overrides
from tqdm import tqdm
from src.core.base.base_analysis import AnalysisBase
from src.core.io import config as hc

class Analysis(AnalysisBase):
    '''Regional EDA'''
    @dataclass
    class RequiredParams:
        '''test parameters'''
        graph_style: str = 'fdp'
        code_of_interest: str = '49318'
        provider_header: str = hc.PR_ID

    class StateInformation:
        '''Exploratory statistics for each state'''
        def __init__(self, state):
            self.state: str = state
            self.item_stats = []
            self.provider_stats = []
            self.patient_stats = []
            self.provider_episode_stats = []
            self.claims_per_decision_provider = []
            self.episodes_per_decision_provider = []

    def __init__(self, logger, params, year):
        core_cols = ["EventID", hc.ITEM, hc.GL, hc.PR_ID, hc.RPR_ID, hc.PR_SP, hc.DATE]
        self.FINAL_COLS = core_cols + ["NSPR"]
        self.INITIAL_COLS = core_cols + [hc.VALID]
        super().__init__(logger, params, year)

    def create_eda_boxplots(self, states):
        '''make boxplots from the gathered data for all states'''
        labels = ["Nation"] + \
            [self.code_converter.convert_state_num(x) for x in range(1, 6)]

        item_stats = [si.item_stats for si in states]
        provider_stats = [si.provider_stats for si in states]
        patient_stats = [si.patient_stats for si in states]
        provider_episode_stats = [si.provider_episode_stats for si in states]
        claims_per_decision_provider = [si.claims_per_decision_provider for si in states]
        episodes_per_decision_provider = [si.episodes_per_decision_provider for si in states]
        self.plots.create_boxplot_group(item_stats, labels, "Claims per item", "claims_items")
        self.plots.create_boxplot_group(provider_stats, labels, "Claims per provider", "claims_providers")
        self.plots.create_boxplot_group(patient_stats, labels, "Claims per episode", "claims_episodes")
        self.plots.create_boxplot_group(provider_episode_stats, labels, "Episodes per provider", "episodes_providers")
        self.plots.create_boxplot_group(claims_per_decision_provider, labels, "Claims per decision-making provider", "d_claims_providers")
        self.plots.create_boxplot_group(episodes_per_decision_provider, labels, "Episodes per decision-making provider", "d_episodes_providers")

    def get_exploratory_stats(self, data, region):
        '''EDA'''
        rp = self.required_params
        state_info = self.StateInformation(region)
        self.log(f"Descriptive stats for {region}")
        self.log(f"{len(data)} claims")
        self.log(f"{len(data[hc.ITEM].unique())} items claimed")
        self.log(f"{len(data[hc.PR_ID].unique())} providers")
        self.log(f"{len(data['EventID'].unique())} patients")
        no_providers_of_interest = len(data.loc[data[hc.ITEM] == rp.code_of_interest, hc.PR_ID].unique())
        self.log(f"{no_providers_of_interest} decision-making providers for {region}")

        provider_episodes = []
        for _, g in data.groupby(hc.PR_ID):
            episodes = len(g['EventID'].unique())
            provider_episodes.append(episodes)

        for (description, header, filename, collection) in [
                ("Claims per item", hc.ITEM, hc.ITEM, state_info.item_stats),
                ("Claims per provider", hc.PR_ID, "provider", state_info.provider_stats),
                ("Claims per episode", "EventID", "episode", state_info.patient_stats),
                ("Episodes per provider", "provider_episodes", "provider_episodes", state_info.provider_episode_stats)
        ]:
            top_file = self.logger.get_file_path(f'top_{filename}_{region}.csv')
            if header == "provider_episodes":
                top_selection = pd.Series(provider_episodes).value_counts()
            else:
                top_selection = data[header].value_counts()

            top_code_counts = top_selection.values.tolist()
            for x in top_code_counts:
                collection.append(x)

            self.log(f"{description} in {region}")
            self.log(f"{top_selection.describe()}")

            if description == hc.ITEM:
                top_codes = top_selection.index.tolist()
                self.code_converter.write_mbs_codes_to_csv(
                    top_codes, top_file, [top_code_counts], ["No of occurrences"])

        for _, claims in data.groupby(rp.provider_header):
            patients = claims['EventID'].unique()
            state_info.episodes_per_decision_provider.append(len(patients))
            state_info.claims_per_decision_provider.append(len(claims))

        df = pd.DataFrame(state_info.episodes_per_decision_provider)
        self.log(f"Episodes per surgical provider in {region}")
        self.log(df.describe())

        return state_info

    @overrides
    def run_test(self) -> None:
        self.log("Running test")
        state_statistics = []
        state_information = self.get_exploratory_stats(self.data, "nation")
        state_statistics.append(state_information)
        # data = self.test_tools.exclude_multiple_states(self.data).groupby(hc.GL)
        data = self.data.groupby(hc.GL)
        for state, state_data in tqdm(data):
            state_information = self.get_exploratory_stats(state_data, self.code_converter.convert_state_num(state))
            state_statistics.append(state_information)

        self.create_eda_boxplots(state_statistics)


"""Added table to hold starting lineup data

Revision ID: 7c469ed02f7c
Revises: 515800272b10
Create Date: 2019-02-22 20:55:13.118327

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '7c469ed02f7c'
down_revision = '515800272b10'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('starting_lineup',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('week', sa.Integer(), nullable=True),
    sa.Column('position', sa.Integer(), nullable=False),
    sa.Column('car_number', sa.Integer(), nullable=False),
    sa.Column('driver', sa.String(length=30), nullable=False),
    sa.Column('team', sa.String(length=30), nullable=False),
    sa.ForeignKeyConstraint(['week'], ['event.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('starting_lineup')
    # ### end Alembic commands ###


#!python3
# -*- coding: utf-8 -*-
# usage : get_lc.py [uid]
# date  : 2020-03-09
# update: 2020-03-19 Add 'mp3_download'
# update: 2020-03-25 Add 'file_parsing'
# update: 2020-05-12 Mod 'mp3_download'
import sys
import re
import requests
from bs4 import BeautifulSoup

def main():
    if len(sys.argv) < 2:
        print('usage : get_lc.py <date>')
        sys.exit()

    questionNum = sys.argv[1]
    lc_url = 'https://www.hackers.co.kr/?c=s_toeic/toeic_study/dlc&front=dailytoeic&category=LC&result=Y&uid='
    lc_url = lc_url + questionNum

    solve_info = {
        'r': 'hackers',
        'm': 'contents',
        'a': 'dailytoeic/solve',
        # 'uid': '6246',
        'category': 'LC',
        'totaluser': '2712',
        'answer': 'Y',
        'event_pop': '',
        'part[]': 'p2_1',
        'p2_1_a': 'A',
        'p2_1_ua': 'A',
        'part[]': 'p4_1',
        'p4_1_a': 'A',
        'p4_1_ua': 'A',
        'part[]': 'p4_2',
        'p4_2_a': 'A',
        'p4_2_ua': 'A',
        'part[]': 'p4_3',
        'p4_3_a': 'A',
        'p4_3_ua': 'A'
    }
    solve_info['uid'] = questionNum

    fileName = questionNum + '_LC.txt'
    f = open(fileName, 'w', -1, 'utf-8')

    with requests.Session() as s:
        req = s.post(lc_url, data=solve_info)
        html = req.text
        soup = BeautifulSoup(html, 'html.parser')
        # solveStrings = soup.select('[class="td_font type1"]')
        # solveStrings = soup.select('.MsoNormal')
        solveStrings = soup.select('.ico_part')
        # solveStrings = soup.select('td')
        # solveStrings = soup.select('.ico_part > table > tr > td > strong')

        for solvStr in solveStrings:
            # print(solvStr.text)
            f.write(solvStr.text)
            # print(solvStr.text.encode('utf-8'))

    f.close()
    print('[+] Make: ' + fileName)
    file_parsing(questionNum)
    mp3_download(html)

def mp3_download(html):
    p = re.compile(".*file:.*mp3'")
    lines = p.findall(html)

    for line in lines:
        line = line.strip()
        url = line.split('\'')[1]
        mp3file = url.split('/')[-1]

        with open(mp3file, 'wb') as mp3:
                response = requests.get(url)
                mp3.write(response.content)
                print('[+] Down: ' + mp3file)

def file_parsing(questionNum):
    inputFile = questionNum + '_LC.txt'
    outputFile = questionNum + '_LC_parsing.txt'
    qestion_URL = 'https://www.hackers.co.kr/?c=s_toeic/toeic_study/dlc&front=dailytoeic&category=LC&uid=' + questionNum + '\n'

    f = open(inputFile, 'r', -1, 'utf-8')
    lines = f.readlines()
    f.close()

    f = open(outputFile, 'w', -1, 'utf-8')
    f.write(qestion_URL+'\n')

    writeFlag = False

    for line in lines:
        line = line.strip()
        if line == '':
            continue

        if ' 발음' in line:
            writeFlag = True
        elif '반복재생' in line:
            writeFlag = False
        elif '해  석' in line:
            writeFlag = True
        elif '· 총 참여자수' in line:
            writeFlag = False

        if writeFlag == True:
            f.write(line+'\n')

    print('[+] Make: ' + outputFile)
    f.close()

if __name__=="__main__":
    main()


# We'll start by importing the DAG object
from datetime import timedelta, datetime
from pathlib import Path

from airflow import DAG
# We need to import the operators used in our tasks
from airflow.operators.python_operator import PythonOperator
# We then import the days_ago function
from airflow.utils.dates import days_ago

# other libs
import pandas as pd
import sqlite3
import os
import requests
from bs4 import BeautifulSoup
import shutil
# get dag directory path
dag_path = os.getcwd()

# data scraping from inshort
def scrape_data(exec_date):
	try:
		print(f"Ingesting data for date: {exec_date}")
		date = datetime.strptime(exec_date, '%Y-%m-%d %H')
		file_date_path = f"{date.strftime('%Y-%m-%d')}/{date.hour}"
		print(f"File date path:- {file_date_path}")

		# News Types
		seed_urls = ['https://inshorts.com/en/read/technology',
		             'https://inshorts.com/en/read/sports',
		             'https://inshorts.com/en/read/world',
		             'https://inshorts.com/en/read/politics',
		             'https://inshorts.com/en/read/entertainment',
		             'https://inshorts.com/en/read/automobile',
		             'https://inshorts.com/en/read/science',
		             'https://inshorts.com/en/read/world']

		news_data = []

		# Collecting data
		for url in seed_urls:
		    news_category = url.split('/')[-1]
		    data = requests.get(url)
		    soup = BeautifulSoup(data.content, 'html.parser')
		    news_articles = [{'news_headline': headline.find('span',
		                                                        attrs={"itemprop": "headline"}).string,
		                        'news_article': article.find('div',
		                                                    attrs={"itemprop": "articleBody"}).string,
		                        'news_category': news_category}

		                        for headline, article in
		                            zip(soup.find_all('div',
		                                            class_=["news-card-title news-right-box"]),
		                                soup.find_all('div',
		                                            class_=["news-card-content news-right-box"]))
		                    ]
		    news_data.extend(news_articles)

		# Creating dataframe
		df =  pd.DataFrame(news_data)
		df = df[['news_headline', 'news_article', 'news_category']]
		print(df.head())

		# load processed data
		output_dir = Path(f'{dag_path}/scraped_data/{file_date_path}')
		output_dir.mkdir(parents=True, exist_ok=True)
		# scraped_data/2021-08-15/12/2021-08-15_12.csv
		df.to_csv(output_dir / f"{file_date_path}.csv".replace("/", "_"), index=False)

	except:
		print("No data scraped")

# Data pre-processing
def data_processing(exec_date):
	# path to read the file
	file_loc_path = f"{dag_path}/scraped_data"
	print(f"pre-preocssing  data for date: {exec_date}")

	# date and time extracting from the datetime module
	date = datetime.strptime(exec_date, '%Y-%m-%d %H')
	file_date_path = f"{date.strftime('%Y-%m-%d')}/{date.hour}"
	name_of_the_file = file_date_path.replace("/","_")

	try:
		# actively scraped data path
		current_scrap_data_path = f"{dag_path}/scraped_data/{file_date_path}/{file_date_path.replace('/', '_')}.csv"

		# current data path
		current_df_data = pd.read_csv(current_scrap_data_path,index_col=None)

		# append data path
		merger_data_path = f"{dag_path}/final_data/final_data.csv"

		# append the scrape data
		current_df_data.to_csv(merger_data_path, mode='a', index=False, header=False)

		# To archived the data we scraped
		# archived_data_path = f"{dag_path}/archived_data/"
		# archived_data_file_name = f"{archived_data_path}/{name_of_the_file}_data"
		# shutil.make_archive(archived_data_file_name, 'zip', archived_data_path)
	except:
		print("Their is some issue related to final data saving")

# Initializing the default arguments that we'll pass to our DAG
default_args = {
    'owner': 'airflow',
    'start_date': datetime(2022, 2, 9)

}

# Creation of DAG object
ingestion_dag = DAG(
    'scraping_ingestion',
    default_args=default_args,
    description='Scrape data from inshorts',
    schedule_interval = '@hourly',
    catchup=False,
)

# Scrape the data from Inshort-News-Web-App
scrape_data = PythonOperator(
    task_id='srape_data',
    python_callable=scrape_data,
    op_args=["{{ ds }} {{ execution_date.hour }}"],
    dag=ingestion_dag,
)

# After Scraping the latest news we save the data
# in a scraped_data folder and within it with date and
# hourly time basis
# Ex:- scraped_data/2022-02-10/6/2022-02-10_6.csv

data_processing = PythonOperator(
    task_id='data_processing',
    python_callable=data_processing,
    op_args=["{{ ds }} {{ execution_date.hour }}"],
    dag=ingestion_dag,
)

# Task priority
scrape_data >> data_processing


from unittest.mock import patch

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from core.entities.company import CompanyEntity
from infra.api import BaseView

class MockController:
    def create_object(self, params):
        pass

    def detail_object(self, object_id):
        pass

    def update_object(self, params):
        pass

class BaseViewAPITestCase(APITestCase):
    def setUp(self):
        self.view = BaseView()
        self.view.controller = MockController()

    def test_dispatch_with_invalid_model(self):
        url = reverse('create', kwargs={'model': 'invalid'})
        response = self.client.post(url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_create_company(self):
        url = reverse('create', kwargs={'model': 'company'})
        data = {
            "street": "Street",
            "city": "City",
            "country": "Country",
            "cnpj": "74422843000112",
        }

        with patch(
            "app.controller.CompanyController.create_object",
            return_value=CompanyEntity(
                **{
                    "street": "Street",
                    "city": "City",
                    "country": "Country",
                    "cnpj": "74422843000112",
                }
            ),
        ), patch(
            "rest_framework.permissions.IsAdminUser.has_permission",
            return_value=True,
        ):
            response = self.client.post(url, data)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        data = response.data
        print(data['error'])
        self.assertTrue(data['success'])
        self.assertIsNone(data['error'])
        self.assertEqual(
            data['data'],
            {
                'city': 'City',
                'cnpj': '74422843000112',
                'country': 'Country',
                'created_at': None,
                'id': None,
                'is_active': True,
                'street': 'Street',
                'updated_at': None,
            },
        )


# Reference: https://github.com/prateekralhan/qrcode-generator-streamlit/blob/main/app.py

import os
import streamlit as st
import segno
import time
from datetime import datetime
import base64

# create a title
st.title("âœ¨QR Code Generator ðŸš€")
url = st.text_input("Enter your URL please ðŸ‘‡")

# create a timestamp to store and read your QR codes
current_ts = datetime.now().strftime("%d_%m_%Y_%H_%M_%S")

# create a def that wil take the url and generate an animated qr code
def generate_animated_qrcode():
    print("to be updated")

if url is not None and url != "":
    with st.spinner('Generating QR Code... ðŸ’«'):
        time.sleep(2.5)
    generate_animated_qrcode()

    # open the animated qr code image and load it to your streamlit app
    # reference: https://discuss.streamlit.io/t/how-to-show-local-gif-image/3408/3
    #file_path = "https://github.com/shaq31415926/practical-experience-in-digital-media/blob/08de407f471f967891fbe1a73bbe06177fca6243/02Lecture/images/my_animated_qrcode_working.gif?raw=true"
    file_path = "images/my_animated_qrcode_working.gif"
    file_ = open(file_path, "rb")
    contents = file_.read()
    data_url = base64.b64encode(contents).decode("utf-8")
    file_.close()
    st.markdown(
        f'<img src="data:image/gif;base64,{data_url}" alt="gif">',
        unsafe_allow_html=True,
    )


import os
import torch
import onnx

from .logger import get_logger

LOGGER = get_logger("utils/export")
DEVICE = os.getenv("DEVICE")

def export_model_to_onnx(model, input_height: int, input_width: int, export_path: str) -> None:
    model.eval()
    x = torch.randn(1, 3, input_height, input_width, requires_grad=True)
    torch.onnx.export(
        model,
        x.to(DEVICE),
        export_path,
        export_params=True,
        opset_version=10,
        do_constant_folding=True,
        input_names=["input"],
        output_names=["output"],
    )
    LOGGER.info("Exported the model: %s.", export_path)

def check_model_is_valid(model_path: str) -> None:
    onnx_model = onnx.load(model_path)
    onnx.checker.check_model(onnx_model)
    LOGGER.info("Checked onnx model at %s.", model_path)


import sys
from pyserini.search import SimpleSearcher
from pyserini import analysis
from evaluate.calculate_score import evaluation

CUSTOMIZE_BM25 = True
QUERY_EXPANSION = False

# Customized parameters for BM25
BM25_k1 = 0.7
BM25_b = 0.32

# Customized parameters for QE_BM25
QE_BM25_k1 = 0.75
QE_BM25_b = 0.28

without_stem_QE_BM25_k1 = 0.54
without_stem_QE_BM25_b = 0.32

# Parameters for RM3 query_expansion
RM3_fb_terms = 5 # number of expansion terms.
RM3_fb_docs = 10 # number of expansion documents.
RM3_original_query_weight = 0.9 # weight to assign to the original query.

searcher = SimpleSearcher('indexes/')
# searcher.set_bm25(BM25_k1, BM25_b)
# searcher.set_rm3(RM3_fb_terms, RM3_fb_docs, RM3_original_query_weight, True)
searcher_ner = SimpleSearcher('indexes_ner')

searcher_1 = SimpleSearcher('indexes/')
searcher_1.set_bm25(BM25_k1, BM25_b)

searcher_2 = SimpleSearcher('indexes_krovetz/')
analyzer_2 = analysis.get_lucene_analyzer(stemmer='krovetz')
searcher_2.set_analyzer(analyzer_2)

searcher_3 = SimpleSearcher('indexes_without_stemming/')
analyzer_3 = analysis.get_lucene_analyzer(stemming=False)
searcher_3.set_analyzer(analyzer_3)

searcher_4 = SimpleSearcher('indexes/')
searcher_4.set_rm3(RM3_fb_terms, RM3_fb_docs, RM3_original_query_weight, False)

searcher_5 = SimpleSearcher('indexes/')
searcher_5.set_bm25(QE_BM25_k1, QE_BM25_b)
searcher_5.set_rm3(RM3_fb_terms, RM3_fb_docs, RM3_original_query_weight, False)

searcher_6 = SimpleSearcher('indexes_without_stemming/')
analyzer_6 = analysis.get_lucene_analyzer(stemming=False)
searcher_6.set_analyzer(analyzer_6)
searcher_6.set_bm25(without_stem_QE_BM25_k1, without_stem_QE_BM25_b)
searcher_6.set_rm3(RM3_fb_terms, RM3_fb_docs, RM3_original_query_weight, False)

# Customized parameters for QE_BM25 using indexes from doubling title and adding keyword
BM25_k1_new = 0.5
BM25_b_new = 0.37

searcher_7 = SimpleSearcher('indexes_without_stemming_new/')
analyzer_7 = analysis.get_lucene_analyzer(stemming=False)
searcher_7.set_analyzer(analyzer_7)
searcher_7.set_bm25(BM25_k1_new, BM25_b_new)

# Customized parameters for QE_BM25 using indexes from doubling title and adding keyword
QE_BM25_k1_new = 0.53
QE_BM25_b_new = 0.45
searcher_8 = SimpleSearcher('indexes_without_stemming_new/')
analyzer_8 = analysis.get_lucene_analyzer(stemming=False)
searcher_8.set_analyzer(analyzer_8)
searcher_8.set_bm25(QE_BM25_k1_new, QE_BM25_b_new)
searcher_8.set_rm3(RM3_fb_terms, RM3_fb_docs, RM3_original_query_weight, False)

annotation = './evaluate/Annotations.csv'

print("Default:")
evaluation(searcher, annotation)
print("-------------------------------------------------------------")
print("BM25 with customized parameters:")
print(f'k1:{BM25_k1}, b:{BM25_b}')
evaluation(searcher_1, annotation)
# print("-------------------------------------------------------------")
# print("With NER:")
# evaluation(searcher_ner, annotation)

print("-------------------------------------------------------------")
print("BM25 with krovetz stemmer:")
evaluation(searcher_2, annotation)

print("-------------------------------------------------------------")
print("BM25 with no stemmer:")
evaluation(searcher_3, annotation)

print("-------------------------------------------------------------")
print("Enable RM3 query expansion with default BM25 parameter:")
evaluation(searcher_4, annotation)

print("-------------------------------------------------------------")
print("Enable RM3 query expansion with customized BM25 parameter:")
print(f'k1:{QE_BM25_k1}, b:{QE_BM25_b}')
evaluation(searcher_5, annotation)

print("-------------------------------------------------------------")
print("Without stemming, but with RM3 query expansion and customized BM25 parameters:")
print(f'k1:{without_stem_QE_BM25_k1}, b:{without_stem_QE_BM25_b}')
evaluation(searcher_6, annotation)

print("-------------------------------------------------------------")
print("Without stemming and RM3 query expansion, but with customized BM25 parameter, and weighted scoring scheme:")
print(f'k1:{BM25_k1_new}, b:{BM25_b_new}')
evaluation(searcher_7, annotation)

print("-------------------------------------------------------------")
print("Without stemming, but with RM3 query expansion, customized BM25 parameter, and weighted scoring scheme:")
print(f'k1:{QE_BM25_k1_new}, b:{QE_BM25_b_new}')
evaluation(searcher_8, annotation)


from django.contrib import admin
from django.db.models import Count
from django.utils.translation import ugettext_lazy as _

from openzaak.utils.admin import (
    EditInlineAdminMixin,
    ListObjectActionsAdminMixin,
    UUIDAdminMixin,
    link_to_related_objects,
)

from ..models import InformatieObjectType, ZaakTypeInformatieObjectType
from .filters import GeldigheidFilter
from .forms import ZaakTypeInformatieObjectTypeAdminForm
from .mixins import (
    CatalogusContextAdminMixin,
    GeldigheidAdminMixin,
    PublishAdminMixin,
    ReadOnlyPublishedMixin,
    ReadOnlyPublishedParentMixin,
    SideEffectsMixin,
)

@admin.register(ZaakTypeInformatieObjectType)
class ZaakTypeInformatieObjectTypeAdmin(
    CatalogusContextAdminMixin,
    ReadOnlyPublishedParentMixin,
    UUIDAdminMixin,
    admin.ModelAdmin,
):
    model = ZaakTypeInformatieObjectType
    form = ZaakTypeInformatieObjectTypeAdminForm

    # List
    list_display = ("zaaktype", "informatieobjecttype", "statustype", "volgnummer")
    list_filter = (
        "zaaktype",
        "informatieobjecttype",
        "richting",
    )
    search_fields = (
        "uuid",
        "volgnummer",
        "zaaktype__uuid",
        "informatieobjecttype__uuid",
    )
    ordering = ("zaaktype", "informatieobjecttype", "volgnummer")

    # Detail
    fieldsets = (
        (_("Algemeen"), {"fields": ("volgnummer", "richting",)},),
        (_("Relaties"), {"fields": ("zaaktype", "informatieobjecttype", "statustype")}),
    )
    raw_id_fields = ("zaaktype", "informatieobjecttype", "statustype")

    def get_concept(self, obj):
        if not obj:
            return True
        return obj.zaaktype.concept or obj.informatieobjecttype.concept

class ZaakTypeInformatieObjectTypeInline(EditInlineAdminMixin, admin.TabularInline):
    model = ZaakTypeInformatieObjectType
    fields = ZaakTypeInformatieObjectTypeAdmin.list_display

@admin.register(InformatieObjectType)
class InformatieObjectTypeAdmin(
    ReadOnlyPublishedMixin,
    ListObjectActionsAdminMixin,
    UUIDAdminMixin,
    CatalogusContextAdminMixin,
    GeldigheidAdminMixin,
    PublishAdminMixin,
    SideEffectsMixin,
    admin.ModelAdmin,
):
    list_display = (
        "omschrijving",
        "catalogus",
        "vertrouwelijkheidaanduiding",
        "num_zaaktypen",
        "is_published",
    )
    list_filter = (
        GeldigheidFilter,
        "concept",
        "catalogus",
        "vertrouwelijkheidaanduiding",
    )
    search_fields = (
        "uuid",
        "omschrijving",
    )
    ordering = ("catalogus", "omschrijving")
    raw_id_fields = ("catalogus",)

    # Details
    fieldsets = (
        (
            _("Algemeen"),
            {
                "fields": (
                    "informatieobjectcategorie",
                    "omschrijving",
                    "vertrouwelijkheidaanduiding",
                    "trefwoord",
                    "uuid",
                )
            },
        ),
        (
            _("Omschrijving generiek"),
            {
                "fields": (
                    "omschrijving_generiek_informatieobjecttype",
                    "omschrijving_generiek_definitie",
                    "omschrijving_generiek_herkomst",
                    "omschrijving_generiek_hierarchie",
                    "omschrijving_generiek_opmerking",
                )
            },
        ),
        (_("Relaties"), {"fields": ("catalogus",)}),
    )
    inlines = (ZaakTypeInformatieObjectTypeInline,)  # zaaktypes

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.annotate(num_zaaktypen=Count("zaaktypen"))

    def get_object_actions(self, obj):
        return (link_to_related_objects(ZaakTypeInformatieObjectType, obj),)

    def num_zaaktypen(self, obj) -> int:
        return obj.num_zaaktypen

    num_zaaktypen.short_description = _("# zaaktypen")


"""
Unit Tests Class and Decorator
"""

from typing import Callable

from extensions_colorama import colorize, GREEN, RED

_TOTAL_TESTS: int = 0
_OK_TESTS   : int = 0
_ERROR_TESTS: int = 0

def unittest(f: Callable) -> Callable:
    assert(f is not None)
    global _ERROR_TESTS, _OK_TESTS, _TOTAL_TESTS
    _TOTAL_TESTS += 1
    try:
        f()
        print(colorize(f.__name__ + ": ok", fg=GREEN))
        _OK_TESTS += 1
    except AssertionError:
        _ERROR_TESTS += 1
        print(colorize(f.__name__ + ": ERROR", fg=RED))
    return f


from dataclasses import dataclass
from header import *
import struct

@dataclass
class Question():

    url : str
    query_type : int

    def build_question(self):
        query_packet = "".encode()
        for i in self.url.split("."):
            encoded = i.encode()
            bytelen = len(i)
            query_packet += struct.pack(f"! {bytelen+1}B", bytelen, *encoded)
        query_packet += struct.pack(
            ">B2H", 0, self.query_type, 1 # End of String,# Query Type,# Query Class
        )
        return query_packet

    def parse_question(self,data):
        self.url=self.parse_hostname(data)
        self.query_type=self.parse_query_type(data)
        query_class = self.parse_query_class(data)
        return f"\nDomain : {self.url}      Query Type :  {query_class}    Query Class :  {self.query_type}"

    def parse_hostname(self, data_recieved):
        url = []
        chunk_size = data_recieved[12]
        index = 13
        while chunk_size != 0:
            for i in range(chunk_size):
                url.append(data_recieved[index])
                index += 1
            url.append(46)
            chunk_size = data_recieved[index]
            index += 1
        hostname = ""
        for i in url:
            hostname += chr(i)
        return hostname

    def parse_query_type(self, data_recieved):
        index = 12
        while data_recieved[index] != 0:
            index += 1
        index += 1
        query_type = ""
        query_type_list = list(data_recieved[index : index + 2])
        index += 2
        if (query_type_list[0] * 10 + query_type_list[1] * 1) == 1:
            query_type += "  A   "
        return query_type

    def parse_query_class(self,data_recieved):
        index = 12
        while data_recieved[index] != 0:
            index += 1
        index += 3
        query_class = ""
        class_list = list(data_recieved[index : index + 2])
        if (class_list[0] * 10 + class_list[1] * 1) == 1:
            query_class += " IN "
        return query_class


import sys
from itertools import permutations

n = [1, 2, 3, 4, 5, 6, 7, 8, 9]

num = list(permutations(n, 3))	# 순열로 3개씩 뽑음

t = int(sys.stdin.readline())
for _ in range(t):
    test, s, b = map(int, sys.stdin.readline().split())
    test = list(str(test))
    removed_cnt = 0     # 배열에서 제거된 튜플 개수

    # num : 3개 리스트
    leng = len(num)
    for i in range(leng):
        s_cnt = b_cnt = 0   # 스트 개수, 볼 개수 0 초기화
        i -= removed_cnt

        for j in range(3):
            test[j] = int(test[j])
            if test[j] in num[i]:
                if j == num[i].index(test[j]):
                    s_cnt += 1
                else:
                    b_cnt += 1

        if s_cnt != s or b_cnt != b:
            num.remove(num[i])      # 스트 개수, 볼 개수 다르면 배열에서 제거
            removed_cnt += 1

print(len(num))


import datetime as dt
import numpy as np
import pandas as pd

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect

from flask import Flask, jsonify

#Setup
engine=create_engine("sqlite:///hawaii.sqlite")
base=automap_base()
    #reflect tables
base.prepare(engine, reflect=True)
measurement=base.classes.measurement
station=base.classes.station
    #start session
session=Session(engine)

    #flask
app=Flask(__name__)

#routes

@app.route("/") #home
def welcome():
    return(
        f"Hawaii Climate Analysis App
"
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/start/end"
    )

@app.route("/api/v1.0/precipitation") #precipitation page
def precipitation():
    last_year=dt.date(2017,8,23) - dt.timedelta(days=365)
    precipitation= session.query(measurement.date,measurement.prcp).filter(measurement.date >= last_year).all()
    precip_dict={date:prcp for date, prcp in precipitation}
    return jsonify(precip_dict)

@app.route("/api/v1.0/stations") #station page
def stations():
    stations_query=session.query(station.station).all()
    stations_list=list(np.ravel(stations_query))
    return jsonify(stations_list=stations_list)
@app.route("/api/v1.0/tobs") #temperature page
def temps():
    last_year=dt.date(2017,8,23) - dt.timedelta(days=365)
    results = session.query(measurement.tobs).filter(measurement.station == 'USC00519281').filter(measurement.date >= last_year).all()
    temps_list=list(np.ravel(results))
    return jsonify(temps_list=temps_list)

@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def stats(start=None,end=None):
    #select statement
    select=[func.min(measurement.tobs), func.max(measurement.tobs), func.avg(measurement.tobs)]

    if not end:
        results1=session.query(*select).filter(measurement.date >= start).all()
        temps_list1=list(np.ravel(results1))
        return jsonify(temps_list1)

    results2= session.query(*select).filter(measurement.date >=start).filter(measurement.date <= end).all()
    temps_list2=list(np.ravel(results2))
    return jsonify(temps_list2=temps_list2)

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


try:
    from flask_bcrypt import Bcrypt
    from flask_sqlalchemy import SQLAlchemy
    from flask import Flask
    import os
    import logging
except Exception as e:
    print("Could not import module in my_website package")

#get the root logger
logger = logging.getLogger()

#create a formatter object
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s : %(message)s')

# add console handler to the root logger
consoleHanlder = logging.StreamHandler()
consoleHanlder.setFormatter(log_formatter)
logger.addHandler(consoleHanlder)
logger.setLevel(logging.INFO)

#add file handler to the root logger
fileHandler = logging.FileHandler("logs.log")
fileHandler.setFormatter(log_formatter)
logger.addHandler(fileHandler)
logger.setLevel(logging.DEBUG)

# creating object of Flask class named app then setting SECRET_KEY
app = Flask(__name__)

# write first log in record.log
app.logger.debug("App object created of flask class.")

#setting up app config for SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config["SECRET_KEY"] = os.environ.get('SECRET_KEY')
db = SQLAlchemy(app)
app.logger.info("Object created of SQLAlchemy class.")

#binding Bcrypt to mathing plain text to hash value
bcrypt = Bcrypt(app)

# #config app for email server
# app.config['MAIL_SERVER'] = 'smtp.gmail.com'
# app.config['MAIL_PORT']   = 465
# app.config['MAIL_USERNAME'] = os.environ.get('COMPANY_EMAIL')
# app.config['MAIL_PASSWORD'] = ''
# app.config['MAIL_USE_TLS']  = False
# app.config['MAIL_USE_SSL']  = True


from dataclasses import dataclass
import random
import numpy as np
from Framework.task import AbstractTask

@dataclass
class ShuffleSamplesTask(AbstractTask):
    sample_var_name: str
    labels_var_name: str

    def process(self, data_store):
        """Shuffle the samples and store them back in the datastore"""
        samples = data_store[self.sample_var_name]
        labels = data_store[self.labels_var_name]
        sample_arr = list(zip(samples, labels))

        random.shuffle(sample_arr)

        samples, labels = zip(*sample_arr)
        data_store[self.sample_var_name] = np.array(samples)
        data_store[self.labels_var_name] = np.array(labels)

        print(f"Shuffled {self.sample_var_name} and {self.labels_var_name}")


import numpy as np
import scipy.special as scs
import matplotlib.pyplot as plt

N=[5,50,500,5000]
a=13.37995764
b=12.13615216

def f(x,n):
    return x**(a+n-1)*(1-x)**(b-1)/scs.beta(a+n,b)

fig,ax=plt.subplots(nrows=2,ncols=2)
i=1
for n in N:
    plt.subplot(2,2,i)
    i+=1
    x=np.linspace(0,1,1000)
    f_x=f(x,n)
    p=0
    for j in range(500,999):
        p=p+0.001*f(x[j],n)
    plt.plot(x,f_x)
    plt.title('n='+str(n)+',  P(P>1/2)='+str(round(p,3)))

plt.show()


# -*- coding: utf-8 -*-
# @Time : 2021/10/18 下午1:55
# @Author: yl
# @File: dataset.py

import os

import torch
import torch.utils.data as data
from typing import Any, Callable, List, Optional, Tuple

from torchvision.datasets import ImageFolder
from PIL import Image

from typing import Any, Callable, cast, Dict, List, Optional, Tuple

# Copying from torchvision
class VisionDataset(data.Dataset):
    _repr_indent = 4

    def __init__(
            self,
            root: str,
            transforms: Optional[Callable] = None,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
    ) -> None:
        if isinstance(root, torch._six.string_classes):
            root = os.path.expanduser(root)
        self.root = root

        has_transforms = transforms is not None
        has_separate_transform = transform is not None or target_transform is not None
        if has_transforms and has_separate_transform:
            raise ValueError("Only transforms or transform/target_transform can "
                             "be passed as argument")

        # for backwards-compatibility
        self.transform = transform
        self.target_transform = target_transform

        if has_separate_transform:
            transforms = StandardTransform(transform, target_transform)
        self.transforms = transforms

    def __getitem__(self, index: int) -> Any:
        raise NotImplementedError

    def __len__(self) -> int:
        raise NotImplementedError

    def __repr__(self) -> str:
        head = "Dataset " + self.__class__.__name__
        body = ["Number of datapoints: {}".format(self.__len__())]
        if self.root is not None:
            body.append("Root location: {}".format(self.root))
        body += self.extra_repr().splitlines()
        if hasattr(self, "transforms") and self.transforms is not None:
            body += [repr(self.transforms)]
        lines = [head] + [" " * self._repr_indent + line for line in body]
        return '\n'.join(lines)

    def _format_transform_repr(self, transform: Callable, head: str) -> List[str]:
        lines = transform.__repr__().splitlines()
        return (["{}{}".format(head, lines[0])] +
                ["{}{}".format(" " * len(head), line) for line in lines[1:]])

    def extra_repr(self) -> str:
        return ""

class StandardTransform(object):
    def __init__(self, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None) -> None:
        self.transform = transform
        self.target_transform = target_transform

    def __call__(self, input: Any, target: Any) -> Tuple[Any, Any]:
        if self.transform is not None:
            input = self.transform(input)
        if self.target_transform is not None:
            target = self.target_transform(target)
        return input, target

    def _format_transform_repr(self, transform: Callable, head: str) -> List[str]:
        lines = transform.__repr__().splitlines()
        return (["{}{}".format(head, lines[0])] +
                ["{}{}".format(" " * len(head), line) for line in lines[1:]])

    def __repr__(self) -> str:
        body = [self.__class__.__name__]
        if self.transform is not None:
            body += self._format_transform_repr(self.transform,
                                                "Transform: ")
        if self.target_transform is not None:
            body += self._format_transform_repr(self.target_transform,
                                                "Target transform: ")

        return '\n'.join(body)

IMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')

def has_file_allowed_extension(filename: str, extensions: Tuple[str, ...]) -> bool:
    """Checks if a file is an allowed extension.

    Args:
        filename (string): path to a file
        extensions (tuple of strings): extensions to consider (lowercase)

    Returns:
        bool: True if the filename ends with one of given extensions
    """
    return filename.lower().endswith(extensions)

def is_image_file(filename: str) -> bool:
    """Checks if a file is an allowed image extension.

    Args:
        filename (string): path to a file

    Returns:
        bool: True if the filename ends with a known image extension
    """
    return has_file_allowed_extension(filename, IMG_EXTENSIONS)

def make_dataset(
        directory: str,
        class_to_idx: Dict[str, int],
        extensions: Optional[Tuple[str, ...]] = None,
        is_valid_file: Optional[Callable[[str], bool]] = None,
) -> List[Tuple[str, int]]:
    """Generates a list of samples of a form (path_to_sample, class).

    Args:
        directory (str): root dataset directory
        class_to_idx (Dict[str, int]): dictionary mapping class name to class index
        extensions (optional): A list of allowed extensions.
            Either extensions or is_valid_file should be passed. Defaults to None.
        is_valid_file (optional): A function that takes path of a file
            and checks if the file is a valid file
            (used to check of corrupt files) both extensions and
            is_valid_file should not be passed. Defaults to None.

    Raises:
        ValueError: In case ``extensions`` and ``is_valid_file`` are None or both are not None.

    Returns:
        List[Tuple[str, int]]: samples of a form (path_to_sample, class)
    """
    instances = []
    directory = os.path.expanduser(directory)
    both_none = extensions is None and is_valid_file is None
    both_something = extensions is not None and is_valid_file is not None
    if both_none or both_something:
        raise ValueError("Both extensions and is_valid_file cannot be None or not None at the same time")
    if extensions is not None:
        def is_valid_file(x: str) -> bool:
            return has_file_allowed_extension(x, cast(Tuple[str, ...], extensions))
    is_valid_file = cast(Callable[[str], bool], is_valid_file)
    for target_class in sorted(class_to_idx.keys()):
        class_index = class_to_idx[target_class]
        target_dir = os.path.join(directory, target_class)
        if not os.path.isdir(target_dir):
            continue
        for root, _, fnames in sorted(os.walk(target_dir, followlinks=True)):
            for fname in sorted(fnames):
                path = os.path.join(root, fname)
                if is_valid_file(path):
                    item = path, class_index
                    instances.append(item)
    return instances

def pil_loader(path: str) -> Image.Image:
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
        return img.convert('RGB')

# TODO: specify the return type
def accimage_loader(path: str) -> Any:
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)

def default_loader(path: str) -> Any:
    from torchvision import get_image_backend
    if get_image_backend() == 'accimage':
        return accimage_loader(path)
    else:
        return pil_loader(path)

class DatasetFolder(VisionDataset):
    """A generic data loader where the samples are arranged in this way: ::

        root/class_x/xxx.ext
        root/class_x/xxy.ext
        root/class_x/[...]/xxz.ext

        root/class_y/123.ext
        root/class_y/nsdf3.ext
        root/class_y/[...]/asd932_.ext

    Args:
        root (string): Root directory path.
        loader (callable): A function to load a sample given its path.
        extensions (tuple[string]): A list of allowed extensions.
            both extensions and is_valid_file should not be passed.
        transform (callable, optional): A function/transform that takes in
            a sample and returns a transformed version.
            E.g, ``transforms.RandomCrop`` for images.
        target_transform (callable, optional): A function/transform that takes
            in the target and transforms it.
        is_valid_file (callable, optional): A function that takes path of a file
            and check if the file is a valid file (used to check of corrupt files)
            both extensions and is_valid_file should not be passed.

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        samples (list): List of (sample path, class_index) tuples
        targets (list): The class_index value for each image in the dataset
    """

    def __init__(
            self,
            root: str,
            loader: Callable[[str], Any],
            extensions: Optional[Tuple[str, ...]] = None,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
            is_valid_file: Optional[Callable[[str], bool]] = None,
    ) -> None:
        super(DatasetFolder, self).__init__(root, transform=transform,
                                            target_transform=target_transform)
        classes, class_to_idx = self._find_classes(self.root)
        samples = self.make_dataset(self.root, class_to_idx, extensions, is_valid_file)
        if len(samples) == 0:
            msg = "Found 0 files in subfolders of: {}\n".format(self.root)
            if extensions is not None:
                msg += "Supported extensions are: {}".format(",".join(extensions))
            raise RuntimeError(msg)

        self.loader = loader
        self.extensions = extensions

        # 0xx_name is a class, when save in class variable, only name are preserved.

        # self.classes = classes
        self.classes = ['_'.join(cls.split('_')[1:]) if '_' in cls else cls for cls in classes]
        self.class_to_idx = class_to_idx
        self.samples = samples
        self.targets = [s[1] for s in samples]

    @staticmethod
    def make_dataset(
            directory: str,
            class_to_idx: Dict[str, int],
            extensions: Optional[Tuple[str, ...]] = None,
            is_valid_file: Optional[Callable[[str], bool]] = None,
    ) -> List[Tuple[str, int]]:
        return make_dataset(directory, class_to_idx, extensions=extensions, is_valid_file=is_valid_file)

    def _find_classes(self, dir: str) -> Tuple[List[str], Dict[str, int]]:
        """
        Finds the class folders in a dataset.

        Args:
            dir (string): Root directory path.

        Returns:
            tuple: (classes, class_to_idx) where classes are relative to (dir), and class_to_idx is a dictionary.

        Ensures:
            No class is a subdirectory of another.
        """
        classes = [d.name for d in os.scandir(dir) if d.is_dir()]
        classes.sort()
        unique_classes = [s.split('_')[0] for s in classes]
        unique_classes = list(set(unique_classes))
        unique_classes.sort()
        unique_classes_to_idx = {unique_classes[i]: i for i in range(len(unique_classes))}
        # class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
        class_to_idx = {}
        j = 0
        k = 0
        main_classes = []
        for i in range(len(classes)):
            if not unique_classes[j] in classes[i]:
                j += 1
                k = 0
            class_to_idx[classes[i]] = unique_classes_to_idx[unique_classes[j]]
            k += 1
            if k == 1:  # the next class name
                tmp = classes[i]
                main_classes.append(classes[i])
            else:  # k >1, same class with different directory, keep the smallest class name
                tmp = classes[i] if len(classes[i]) < len(tmp) else tmp
                main_classes[-1] = tmp

        return main_classes, class_to_idx

    def __getitem__(self, index: int) -> Tuple[Any, Any]:
        """
        Args:
            index (int): Index

        Returns:
            tuple: (sample, target) where target is class_index of the target class.
        """
        path, target = self.samples[index]
        sample = self.loader(path)
        if self.transform is not None:
            sample = self.transform(sample)
        if self.target_transform is not None:
            target = self.target_transform(target)

        return sample, target

    def __len__(self) -> int:
        return len(self.samples)

class ImageFolder(DatasetFolder):
    """A generic data loader where the images are arranged in this way: ::

        root/dog/xxx.png
        root/dog/xxy.png
        root/dog/[...]/xxz.png

        root/cat/123.png
        root/cat/nsdf3.png
        root/cat/[...]/asd932_.png

    Args:
        root (string): Root directory path.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        loader (callable, optional): A function to load an image given its path.
        is_valid_file (callable, optional): A function that takes path of an Image file
            and check if the file is a valid file (used to check of corrupt files)

     Attributes:
        classes (list): List of the class names sorted alphabetically.
        class_to_idx (dict): Dict with items (class_name, class_index).
        imgs (list): List of (image path, class_index) tuples
    """

    def __init__(
            self,
            root: str,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
            loader: Callable[[str], Any] = default_loader,
            is_valid_file: Optional[Callable[[str], bool]] = None,
    ):
        super(ImageFolder, self).__init__(root, loader, IMG_EXTENSIONS if is_valid_file is None else None,
                                          transform=transform,
                                          target_transform=target_transform,
                                          is_valid_file=is_valid_file)
        self.imgs = self.samples


from typing import List

# 枚举法, 超时
class Solution_1:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        self.res = 0
        def get_list(length, cur):
            if length == 0:
                if sum([a * b for a, b in zip(cur, nums)]) == S:
                    self.res += 1
                return
            get_list(length-1, cur + [1])
            get_list(length-1, cur + [-1])
        get_list(len(nums), [])
        return self.res

# 0, 1背包
class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        target = sum(nums) + S
        if target % 2 != 0:
            return 0
        target = target // 2
        if target > sum(nums):
            return 0
        dp = [0] * (target + 1)
        dp[0] = 1
        for num in nums:
            for i in range(target, num-1, -1):
                dp[i] += dp[i-num]
            print(dp)
        return dp[target]

nums = [1,2,7,9,981]
S = 1000000000
s = Solution()
print(s.findTargetSumWays(nums, S))


import pytest

from .crud_helper import CrudHelper
from .fixtures import TravelFactory, RoleFactory, PersonFactory, CompletePersonFactory

class TestTravel(CrudHelper):
    @pytest.yield_fixture(autouse=True)
    def prep_types(self, db_session):
        # Initial permission check gets very unhappy if reviewer role is missing
        self.user = PersonFactory(roles=[RoleFactory(name='funding_reviewer'), RoleFactory(name='organiser')])
        db_session.commit()
        yield

    def test_permissions(self, app, db_session):
        # Must have BOTH org and funding_reviewer
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'])
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'], bad_roles=['organiser'])
        CrudHelper.test_permissions(self, app, db_session, good_roles=['organiser', 'funding_reviewer'], bad_roles=['funding_reviewer'])

    def test_new(self, app, db_session):
        data = {
                # NOTE: No facility to enter the flight details
                "origin_airport" : "Here",
                "destination_airport" : "There",
               }

        CrudHelper.test_new(self, app, db_session, data=data, user=self.user)

    def test_view(self, app, db_session):
        target = TravelFactory(person = CompletePersonFactory())
        db_session.commit()
        # NOTE: No facility to view the flight details
        expected = [target.person.fullname, target.origin_airport, target.destination_airport]
        CrudHelper.test_view(self, app, db_session, target=target, expected=expected, user=self.user)

    def test_index(self, app, db_session):
        groups = [TravelFactory(person=CompletePersonFactory()) for i in range(10)]
        db_session.commit()
        entries = { s.id : [str(s.id), s.person.fullname, s.origin_airport, s.destination_airport] for s in groups }

        CrudHelper.test_index(self, app, db_session, entries = entries, user=self.user)

    def test_edit(self, app, db_session):
        target = TravelFactory()
        db_session.commit()

        initial_values = {
                "origin_airport" : target.origin_airport,
                "destination_airport" : target.destination_airport,
                }

        new_values = {
                # NOTE: No facility to enter the flight details
                "origin_airport" : "Here",
                "destination_airport" : "There",
               }

        CrudHelper.test_edit(self, app, db_session, initial_values=initial_values, new_values=new_values, target=target, user=self.user)

    def test_delete(self, app, db_session):
        CrudHelper.test_delete(self, app, db_session, user=self.user)