##This script plots the frequency of alphanumeric characters in my plain text resume. My resume was copied and pasted from MS Word. I made no changes to it.
import re
from collections import Counter
#import plotly
#import plotly.graph_objs
import numpy as np
import matplotlib.pyplot as plt

with open('resumetext.txt', 'r') as resume:
    resumetext = resume.read().replace('\n', '') ##Reading my plain text resume

onlyalnum = re.sub(r'[^\w]', '', resumetext) ##Removing any symbols/whitespace
onlyalnum = onlyalnum.lower() ##Making all the letters lowercase

sortedcharacters = sorted(Counter(onlyalnum).items()) ##Counting and sorting the alphanumeric characters. The output does not include any non-exisiting characters.
#In this case '8' does not show up in my resume anywhere, so '8' will be missing in my graph

character, freq = zip(*sortedcharacters)

##I like Plotly it makes nicer graphs. But Matplotlib is much faster
indexes = np.arange(len(character))
plt.bar(indexes, freq)
plt.xticks(indexes + 0.5, character)
plt.title('Frequency of Alphanumeric Characters in my Resume')
plt.ylabel('Frequency')
plt.xlabel('Character \n 8 is not present in my resume so it is absent from the graph')
plt.show()


# -*- coding: utf-8 -*-
# 常態分佈測試

import sys
import tools
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from stock import stock
from stock import stock_daytrade

def test_profit():
    s = stock_daytrade("測試資料/台灣加權指數/TXF1-分鐘-成交價.csv")
    nday = 45
    good_mean = -100
    for nday in range(20, 21):
        data = np.array(s.close)
        data = (np.roll(data, -nday) - data) / data  # 報酬率
        data = data[nday:len(data) - nday - 1]

        num_bins = 50
        n, bins, patches = plt.hist(
            data, num_bins, normed=1, facecolor='blue', alpha=0.5)
        x, y = tools.normfun_ex(data)
        plt.plot(x, y, color='g', linewidth=1)
        plt.show()

        if(data.mean() * 100 > good_mean):
            good_mean = data.mean() * 100
            print(nday, data.mean() * 100, data.std() * 100)

        result = []
        for i in range(0, len(data), nday):
            result.append(data[i])
        n, bins, patches = plt.hist(
            result, num_bins, normed=1, facecolor='red', alpha=0.5)
        result = np.array(result)
        if(result.mean() * 100 > good_mean):
            good_mean = result.mean() * 100
            print(nday, result.mean() * 100, result.std() * 100)

def test_up_down():
    # 分別計算 上漲 和 下跌 的常態分佈 (分開來後，就沒有常態分佈了)
    up_mean = -100
    down_mean = -100
    for nday in range(30, 30):
        data = np.array(s.close)
        data = (np.roll(data, -nday) - data) / data
        data = data[nday:len(data) - nday - 1]

        up = []
        down = []
        for up_or_down in data:
            if(up_or_down > 0):
                up.append(up_or_down)
            elif(up_or_down < 0):
                down.append(up_or_down)

        up = np.array(up)
        down = np.array(down)
        num_bins = 50

        n, bins, patches = plt.hist(
            up, num_bins, normed=1, facecolor='red', alpha=0.5)
        n, bins, patches = plt.hist(
            down, num_bins, normed=1, facecolor='green', alpha=0.5)
        print(nday, up.mean() * 100, down.mean() * 100)

def test_ma_slope(nday=10):
    s = stock_daytrade("測試資料/台灣加權指數/TXF1-分鐘-成交價.csv")
    data = np.array(s.feature_MA_slope(nday))
    data = data[nday:len(data) - nday - 1]

    n, bins, patches = plt.hist(
        data, 50, normed=1, facecolor='red', alpha=0.5)
    x, y = tools.normfun_ex(data)
    print(nday, data.mean(), data.std())
    plt.plot(x, y, color='g', linewidth=1)
    plt.show()

def test_oneday():
    # 計算單一天的漲跌 (有常態分佈)
    # 長紅，長黑，是否為交易訊號？
    s = stock_daytrade("測試資料/台灣加權指數/TXF1-分鐘-成交價.csv")
    c = np.array(s.close)
    o = np.array(s.opened)
    data = c - o

    n, bins, patches = plt.hist(
        data, 500, normed=1, facecolor='red', alpha=0.5)
    x, y = tools.normfun_ex(data)
    print(data.mean(), data.std())
    plt.plot(x, y, color='g', linewidth=1)
    plt.show()

    # 找出訊號
    #up_signal_list = []
    #up_signal_date_list = []
    #down_signal_list = []
    #down_signal_date_list = []
    #
    #for index, data in enumerate(x):
    #    if(data > up_signal):
    #        up_signal_date_list.append(s.date[index])
    #        up_signal_list.append(c[index])
    #    if(data < down_signal):
    #        down_signal_date_list.append(s.date[index])
    #        down_signal_list.append(c[index])
    #
    #plt.plot(s.date, s.close)
    #plt.plot(up_signal_date_list, up_signal_list, "ro")
    #plt.plot(down_signal_date_list, down_signal_list, "go")
    #plt.show()

def test_BIAS():
    #計算乖離率, 有常態分佈
    nday = 50
    f_name = "測試資料/台股/HYGd.csv"
#    s = stock_daytrade(f_name)
    s = stock(f_name)
    c = np.array(s.close)
    o = np.array(s.opened)
    x = s.feature_BIAS(nday)
    x = x[nday:]
#    x = tools.time_filter(x, s.date,9*60,11*60)
    a, b = tools.normfun_ex(x)
    plt.plot(a, b, color='g', linewidth=1)
    plt.hist(x, 50, normed=1, facecolor='blue', alpha=0.5)
    plt.show()

    def fa(x):
        if x >0:
            return True
        else:
            return False
    def fb(x):
        if x >0:
            return False
        else:
            return True

    x1 = np.array(list(filter(fa,x)))
    x2 = np.array(list(filter(fb,x)))
    print(x1)

    a, b = tools.normfun_ex(x1)
    plt.plot(a, b, color='g', linewidth=1)
    plt.hist(x1, 50, normed=1, facecolor='blue', alpha=0.5)
    plt.show()
    a, b = tools.normfun_ex(x2)
    plt.plot(a, b, color='g', linewidth=1)
    plt.hist(x2, 50, normed=1, facecolor='blue', alpha=0.5)
    plt.show()

    # 如何定義漲多，跌深
    #sigma = 1  # 50% = 0.65，60% = 0.85，70% = 1.04，80%=1.3，90%=1.6
    #up_signal = x.mean() + sigma * x.std()
    #down_signal = x.mean() - sigma * x.std()
    #print(x.mean(), up_signal, down_signal)
    #sigma = 1.6  # 50% = 0.65，60% = 0.85，70% = 1.04，80%=1.3，90%=1.6
    #up_signal = x.mean() + sigma * x.std()
    #down_signal = x.mean() - sigma * x.std()
    #print(x.mean(), up_signal, down_signal)

def test_K():
    # K, D無常態分佈
    nday = 10
    s = stock("price_data.csv")
    x = s.feature_K()
    x = np.array(x[nday:])

    a, b = tools.normfun_ex(x)
    plt.plot(a, b, color='g', linewidth=1)
    plt.hist(x, 60, normed=1, facecolor='blue', alpha=0.5)
    plt.show()

def test():
    nday = 20
    s = stock("測試資料/台股/0056d.csv")
    x = s.feature_diff()[nday:]*100

    a, b = tools.normfun_ex(x)
    plt.plot(a, b, color='g', linewidth=1)
    plt.hist(x, 60, normed=1, facecolor='blue', alpha=0.5)
    plt.show()

def fn_twstock():
    from twstocka import Stock
    stock = Stock('2330')
    a= stock.fetch_from(2013, 11)
    b = stock.price

if __name__ == '__main__':
    #test_oneday()
    # test_profit()
    test_BIAS()
    #test()
    #test_ma_slope(60)
    # test_K()


#! /usr/bin/python

def approx_equal(x1, x2, tol):
    """
    Checkes whether two values agree, within a given error margin
    """

    return abs((x1-x2)/(x1+x2))<tol

def l1_norm(a1, a2):

    import numpy

    return sum(numpy.abs(numpy.array(a1)-numpy.array(a2))/len(a1))

def calc_celerity(v):

    import math

    return v/math.sqrt(1-v**2)

def consolidate(fname):

    import h5py
    import numpy

    f = h5py.File(fname)
    res = dict((key,numpy.array(f[key])) for key in f)
    f.close()
    return res

def main():

    import sys
    sys.path.append(os.environ['FUJIN_ROOT']+'/analytic')
    from ideal_gas_riemann_solver import RiemannProfile, Primitive

    #import numpy
    #rawd = numpy.loadtxt('res.txt')
    #p_f = rawd[0]
    #v_f = rawd[1]
    #c_f = calc_celerity(v_f)
    #p_dm = 20.7445
    #v_dm = 0.956038
    #c_dm = calc_celerity(v_dm)

    #f = open('gradesheet.txt','w')
    #f.write('Numeric pressure = '+str(p_f)+'\n')
    #f.write('Numeric celerity = '+str(c_f)+'\n')
    #f.write('Exact pressure = '+str(p_dm)+'\n')
    #f.write('Exact celerity = '+str(c_dm)+'\n')
    #f.write('Relative pressure difference = '+str((p_f-p_dm)/(p_f+p_dm))+'\n')
    #f.write('Relative celerity difference = '+str((c_f-c_dm)/(c_f+v_dm))+'\n')
    #f.close()

    final = consolidate('final.h5')
    exact_prof = RiemannProfile(
        Primitive(1.,1000.,0),
        Primitive(1.,0.1,0), 5./3.)
    ss_cord = (final['position']-0.5)/(final['time'][0])
    analytic = {}
    analytic['density'] = [exact_prof.calcPrimitive(ssc).d for ssc in ss_cord]
    analytic['pressure'] = [exact_prof.calcPrimitive(ssc).p for ssc in ss_cord]
    analytic['celerity'] = [exact_prof.calcPrimitive(ssc).w for ssc in ss_cord]

    l1_data = dict((key,l1_norm(analytic[key],final[key])) for key in analytic)

    #import pylab
    #for n,key in enumerate(['density','pressure','celerity']):
    #    pylab.subplot(3,1,n+1)
    #    pylab.plot(final['position'], final[key])
    #    pylab.plot(final['position'], analytic[key])
    #    pylab.ylabel(key)
    #pylab.xlabel('Position')
    #pylab.subplot(311)
    #pylab.title('time = '+str(final['time'][0]))
    #pylab.show()

    f = open('gradesheet.txt','w')
    for key in ['density','pressure','celerity']:
        f.write(str(l1_data[key])+'\n')
    f.close()

    return l1_data['pressure'] < 22 and l1_data['celerity'] < 0.37 and l1_data['density']<0.83

if __name__=='__main__':

    import os

    os.system('rm -rf test_passed.res test_failed.res')
    if main():
        os.system('touch test_passed.res')
    else:
        os.system('touch test_failed.res')


from django.conf.urls import url, include
# from django.contrib.auth import views as auth_views

from . import views

app_name = 'feeder'
urlpatterns = [
	url(r'^$', views.LoginView, name='login'),
	url(r'^login/$', views.LoginView, name='log_in'),
	url(r'^register/$', views.RegisterView, name='register'),
	url(r'^index/$', views.IndexView, name='index'),
	url(r'^logout/$', views.Logout, name='logout'),
	url(r'^tokensignin/$', views.TokenVerify, name='gtoken'),
	url(r'^studentlogin/$', views.StudentLogin, name='studentlogin'),

]


"""The model of user profiles."""

def put(conn, sch, user_id, obj):
    """To set the user profiles."""
    with conn.cursor() as cur:
        cur.execute(SQL_SELECT_USER_COUNT.format(sch), (user_id,))
        row = cur.fetchone()
        if not row[0]:
            return None
    prof_names = []
    for prof in obj:
        prof = validate_prof(prof)
        prof['user_id'] = user_id
        added = False
        with conn.cursor() as cur:
            cur.execute(SQL_INSERT_PROF.format(sch), prof)
            added = cur.rowcount == 1
        for group_id in (prof['privs'] or []):
            with conn.cursor() as cur:
                cur.execute(
                    SQL_INSERT_PROF_PRIV.format(sch),
                    {**prof, **{'group_id': group_id}}
                )
        with conn.cursor() as cur:
            cur.execute(
                SQL_DELETE_PROF_PRIVS_OUTDATED.format(sch),
                {**prof, **{'groups': tuple(prof['privs'] or [''])}}
            )
        prop_names = []
        props_updated = False
        for prop in (prof['props'] or []):
            prop = validate_prop(prop)
            prop['user_id'] = user_id
            prop['prof_name'] = prof['name']
            if prop['value']:
                prop_names.append(prop['name'])
                with conn.cursor() as cur:
                    cur.execute(SQL_INSERT_PROF_PROP.format(sch), prop)
                    if cur.rowcount:
                        props_updated = True
                    else:
                        with conn.cursor() as cur:
                            cur.execute(SQL_UPDATE_PROF_PROP.format(sch), prop)
                            if cur.rowcount:
                                props_updated = True
                for group_id in (prop['privs'] or []):
                    with conn.cursor() as cur:
                        cur.execute(
                            SQL_INSERT_PROF_PROP_PRIV.format(sch),
                            {**prop, **{'group_id': group_id}}
                        )
                with conn.cursor() as cur:
                    cur.execute(
                        SQL_DELETE_PROF_PROP_PRIVS_OUTDATED.format(sch),
                        {**prop, **{'groups': tuple(prop['privs'] or [''])}}
                    )
        if prop_names:
            prof_names.append(prof['name'])
            with conn.cursor() as cur:
                cur.execute(SQL_DELETE_PROF_PROPS_OUTDATED.format(sch), {
                    'user_id': user_id,
                    'prof_name': prof['name'],
                    'names': tuple(prop_names or ['']),
                })
        if not added:
            if props_updated:
                with conn.cursor() as cur:
                    cur.execute(SQL_UPDATE_PROF.format(sch), prof)
            else:
                with conn.cursor() as cur:
                    cur.execute(SQL_UPDATE_PROF_IF_MODIFIED.format(sch), prof)
    with conn.cursor() as cur:
        cur.execute(SQL_DELETE_PROFS_OUTDATED.format(sch), {
            'user_id': user_id,
            'names': tuple(prof_names or ['']),
        })
    return get_all(conn, sch, (user_id,))

def get_all(conn, sch, user_id):
    """To get the user profiles."""
    with conn.cursor() as cur:
        cur.execute(SQL_SELECT_USER_COUNT.format(sch), (user_id,))
        row = cur.fetchone()
        if not row[0]:
            return None
    profs = []
    with conn.cursor() as cur:
        cur.execute(SQL_SELECT_PROFS.format(sch), (user_id,))
        for prof in cur.fetchall():
            profs.append({
                'name': prof[0],
                'note': prof[1],
                'main': prof[2],
                'ver': prof[3],
                'created_at': prof[4],
                'updated_at': prof[5],
                'privs': [],
                'props': [],
            })
    for prof in profs:
        with conn.cursor() as cur:
            cur.execute(SQL_SELECT_PROF_PRIVS.format(sch), {
                'user_id': user_id,
                'name': prof['name'],
            })
            for priv in cur.fetchall():
                prof['privs'].append(priv[0])
        with conn.cursor() as cur:
            cur.execute(SQL_SELECT_PROF_PROPS.format(sch), {
                'user_id': user_id,
                'prof_name': prof['name'],
            })
            for prop in cur.fetchall():
                prof['props'].append({
                    'name': prop[0],
                    'value': prop[1],
                    'ver': prop[2],
                    'created_at': prop[3],
                    'updated_at': prop[4],
                    'privs': [],
                })
        for prop in prof['props']:
            with conn.cursor() as cur:
                cur.execute(SQL_SELECT_PROF_PROP_PRIVS.format(sch), {
                    'user_id': user_id,
                    'prof_name': prof['name'],
                    'name': prop['name'],
                })
                for ret in cur.fetchall():
                    prop['privs'].append(ret[0])
    return profs

def get(conn, sch, user_id, groups):
    """To get the user profiles."""
    with conn.cursor() as cur:
        cur.execute(SQL_SELECT_USER_COUNT.format(sch), (user_id,))
        row = cur.fetchone()
        if not row[0]:
            return None
    profs = []
    with conn.cursor() as cur:
        cur.execute(SQL_SELECT_PROFS_ALLOWED.format(sch), {
            'user_id': user_id,
            'groups': tuple(groups or ['']),
        })
        for prof in cur.fetchall():
            profs.append({
                'name': prof[0],
                'note': prof[1],
                'main': prof[2],
                'ver': prof[3],
                'created_at': prof[4],
                'updated_at': prof[5],
                'props': [],
            })
    for prof in profs:
        with conn.cursor() as cur:
            cur.execute(SQL_SELECT_PROF_PROPS_ALLOWED.format(sch), {
                'user_id': user_id,
                'prof_name': prof['name'],
                'groups': tuple(groups or ['']),
            })
            for prop in cur.fetchall():
                prof['props'].append({
                    'name': prop[0],
                    'value': prop[1],
                    'ver': prop[2],
                    'created_at': prop[3],
                    'updated_at': prop[4],
                })
    return profs

def validate_prof(obj):
    """To validate the value profiles """
    ret = {**obj}
    ret['name'] = ret['name'].strip()
    if 'note' not in ret:
        ret['note'] = ''
    else:
        ret['note'] = (ret['note'] or '').strip()
    if 'main' not in ret:
        ret['main'] = False
    elif not ret['main']:
        ret['main'] = False
    else:
        ret['main'] = True
    if 'privs' not in ret:
        ret['privs'] = []
    if 'props' not in ret:
        ret['props'] = []
    return ret

def validate_prop(obj):
    """To validate the value of properties """
    ret = {**obj}
    ret['name'] = ret['name'].strip()
    if 'value' not in ret:
        ret['value'] = ''
    else:
        ret['value'] = (ret['value'] or '').strip()
    if 'privs' not in ret:
        ret['privs'] = []
    return ret

SQL_SELECT_USER_COUNT = """
SELECT COUNT(*) FROM {0}.USERS
 WHERE id = %s
"""

SQL_DELETE_PROFS_OUTDATED = """
DELETE FROM {0}.profs
 WHERE user_id = %(user_id)s
   AND name NOT IN %(names)s
"""

SQL_DELETE_PROF_PROPS_OUTDATED = """
DELETE FROM {0}.prof_props
 WHERE user_id = %(user_id)s
   AND prof_name = %(prof_name)s
   AND name NOT IN %(names)s
"""

SQL_INSERT_PROF = """
INSERT INTO {0}.profs (
       user_id
     , name
     , note
     , main
) VALUES (
       %(user_id)s
     , %(name)s
     , %(note)s
     , %(main)s
) ON CONFLICT DO NOTHING
"""

SQL_UPDATE_PROF = """
UPDATE {0}.profs
   SET note = %(note)s
     , main = %(main)s
     , ver = ver + 1
     , updated_at = (NOW() AT TIME ZONE 'UTC')
 WHERE user_id = %(user_id)s
   AND name = %(name)s
   AND ver = %(ver)s
"""

SQL_UPDATE_PROF_IF_MODIFIED = """
UPDATE {0}.profs
   SET note = %(note)s
     , main = %(main)s
     , ver = ver + 1
     , updated_at = (NOW() AT TIME ZONE 'UTC')
 WHERE user_id = %(user_id)s
   AND name = %(name)s
   AND ver = %(ver)s
   AND (note <> %(note)s
     OR main <> %(main)s)
"""

SQL_INSERT_PROF_PRIV = """
INSERT INTO {0}.prof_privs (
       user_id
     , name
     , group_id
)
SELECT
       %(user_id)s
     , %(name)s
     , %(group_id)s
 WHERE NOT EXISTS (
        SELECT group_id
          FROM {0}.prof_privs
         WHERE user_id = %(user_id)s
           AND name = %(name)s
           AND group_id = %(group_id)s
       )
   AND EXISTS (
        SELECT id
          FROM {0}.groups
         WHERE id = %(group_id)s
       )
"""

SQL_DELETE_PROF_PRIVS_OUTDATED = """
DELETE FROM {0}.prof_privs
 WHERE user_id = %(user_id)s
   AND name = %(name)s
   AND group_id NOT IN %(groups)s
"""

SQL_INSERT_PROF_PROP = """
INSERT INTO {0}.prof_props (
       user_id
     , prof_name
     , name
     , value
) VALUES (
       %(user_id)s
     , %(prof_name)s
     , %(name)s
     , %(value)s
) ON CONFLICT DO NOTHING
"""

SQL_UPDATE_PROF_PROP = """
UPDATE {0}.prof_props
   SET value = %(value)s
     , ver = ver + 1
     , updated_at = (NOW() AT TIME ZONE 'UTC')
 WHERE user_id = %(user_id)s
   AND prof_name = %(prof_name)s
   AND name = %(name)s
   AND ver = %(ver)s
   AND value <> %(value)s
"""

SQL_INSERT_PROF_PROP_PRIV = """
INSERT INTO {0}.prof_prop_privs (
       user_id
     , prof_name
     , name
     , group_id
)
SELECT
       %(user_id)s
     , %(prof_name)s
     , %(name)s
     , %(group_id)s
 WHERE NOT EXISTS (
        SELECT group_id
          FROM {0}.prof_prop_privs
         WHERE user_id = %(user_id)s
           AND prof_name = %(prof_name)s
           AND name = %(name)s
           AND group_id = %(group_id)s
       )
   AND EXISTS (
        SELECT id
          FROM {0}.groups
         WHERE id = %(group_id)s
       )
"""

SQL_DELETE_PROF_PROP_PRIVS_OUTDATED = """
DELETE FROM {0}.prof_prop_privs
 WHERE user_id = %(user_id)s
   AND prof_name = %(prof_name)s
   AND name = %(name)s
   AND group_id NOT IN %(groups)s
"""

SQL_SELECT_PROFS = """
SELECT name
     , note
     , main
     , ver
     , created_at
     , updated_at
  FROM {0}.profs
 WHERE user_id = %s
 ORDER BY name
"""

SQL_SELECT_PROF_PRIVS = """
SELECT group_id
  FROM {0}.prof_privs
 WHERE user_id = %(user_id)s
   AND name = %(name)s
 ORDER BY group_id
"""

SQL_SELECT_PROF_PROPS = """
SELECT name
     , value
     , ver
     , created_at
     , updated_at
  FROM {0}.prof_props
 WHERE user_id = %(user_id)s
   AND prof_name = %(prof_name)s
 ORDER BY name
"""

SQL_SELECT_PROF_PROP_PRIVS = """
SELECT group_id
  FROM {0}.prof_prop_privs
 WHERE user_id = %(user_id)s
   AND prof_name = %(prof_name)s
   AND name = %(name)s
 ORDER BY group_id
"""

SQL_SELECT_PROFS_ALLOWED = """
SELECT p.name
     , p.note
     , p.main
     , p.ver
     , p.created_at
     , p.updated_at
  FROM {0}.profs p
  JOIN {0}.prof_privs v
    ON p.user_id = v.user_id
   AND p.name = v.name
 WHERE p.user_id = %(user_id)s
   AND v.group_id in %(groups)s
 ORDER BY p.name
"""

SQL_SELECT_PROF_PROPS_ALLOWED = """
SELECT p.name
     , p.value
     , p.ver
     , p.created_at
     , p.updated_at
  FROM {0}.prof_props p
  JOIN {0}.prof_prop_privs v
    ON p.user_id = v.user_id
   AND p.prof_name = v.prof_name
   AND p.name = v.name
 WHERE p.user_id = %(user_id)s
   AND p.prof_name = %(prof_name)s
   AND v.group_id in %(groups)s
 ORDER BY p.name
"""


import glob
import os
import ntpath
import json
from ripe.atlas.cousteau import AtlasResultsRequest

failed_measurements = list()

for filepath in glob.iglob('measurements/*.json'):
    file_name = os.path.splitext(ntpath.basename(filepath))[0]
    folder_name = "measurements/data/{}/".format(file_name)
    os.makedirs(os.path.dirname(folder_name), exist_ok=True)
    with open(filepath) as f:
        data = json.load(f)
    for k, v in data.items():
        folder_name = "measurements/data/{}/{}/".format(file_name, k)
        os.makedirs(os.path.dirname(folder_name), exist_ok=True)
        for elt in v:
            try:
                for measurement in elt['measurement_id']:
                    kwargs = {"msm_id": measurement}
                    (is_success, results) = AtlasResultsRequest(**kwargs).create()
                    try:
                        with open("measurements/data/{}/{}/{}.json".format(file_name, k, measurement), "w") as f:
                            json.dump(results, f, indent=4, sort_keys=True)
                    except Exception as e:
                        failed_measurements.append(measurement)
            except KeyError:
                pass

with open('measurements-failed-to-download.txt', 'w') as f:
    for elt in failed_measurements:
        f.write("%s\n" % elt)


# -*- coding:utf-8 -*-
from django.contrib import admin

from .models import Translation

class TranslationAdmin(admin.ModelAdmin):
    """ To manage Translations in Django-Admin """

    def channel_name(self, obj):
        return unicode(obj.news.channel)

    channel_name.admin_order_field = 'news__channel__name'

    list_display = (
        'title',
        'language',
        'status',
        'slug',
        'creation_date',
        'publish_date',
        'channel_name',
    )

    fieldsets = (
        (None, {'fields': ('language',)}),
        ('Contenu', {'fields': ('title', 'subtitle', 'text', 'id_original')}),
        ('Etat', {'fields': (
            'status',
            'last_modification_date',
            'publish_date',
            'trash_date',
            'delete_date',
            'order',)}),
        (u'Publication différée', {'fields': (
            'delayed_publication_date',
            'delayed_publication_time',
            'delayed_publication_position'
        )})
    )

    search_fields = ('title', 'news__channel__name')
    list_filter = ('status', 'language')
    ordering = ('title',)

admin.site.register(Translation, TranslationAdmin)


#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2013-2014  Hans Ulrich Frink
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

"""MYAllPeople Report"""

#------------------------------------------------------------------------
#
# python modules
#
#------------------------------------------------------------------------

#------------------------------------------------------------------------
#
# gramps modules
#
#------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from gramps.gen.plug.menu import (FilterOption, PlaceListOption,
                                  EnumeratedListOption, BooleanOption)
from gramps.gen.plug.report import Report
from gramps.gen.plug.report import MenuReportOptions
from gramps.gen.plug.report import stdoptions
from gramps.gen.plug.docgen import (IndexMark, FontStyle, ParagraphStyle,
                                    TableStyle, TableCellStyle,
                                    FONT_SANS_SERIF, FONT_SERIF,
                                    INDEX_TYPE_TOC, PARA_ALIGN_CENTER)
from gramps.gen.sort import Sort
from gramps.gen.utils.location import get_location_list
from gramps.gen.display.place import displayer as place_displayer
from gramps.gen.lib import PlaceType
from gramps.gen.errors import ReportError

class MYAllPeopleReport(Report):
    """
    MYAllPeople Report class
    """
    def __init__(self, database, options, user):
        """
        Create the PlaceReport object produces the Place report.

        The arguments are:

        database        - the GRAMPS database instance
        options         - instance of the Options class for this report
        user            - instance of a gen.user.User class

        This report needs the following parameters (class variables)
        that come in the options class.

        places          - List of places to report on.
        center          - Center of report, person or event
        incl_private    - Whether to include private data
        name_format     - Preferred format to display names

        """

        Report.__init__(self, database, options, user)

        self._user = user
        menu = options.menu

        self.set_locale(menu.get_option_by_name('trans').get_value())

        self.sort = Sort(self.database)

        self.person_handles=[h for h in self.database.iter_person_handles()]
        self.family_handles=[h for h in self.database.iter_family_handles()]
        self.place_handles=[h for h in self.database.iter_place_handles()]
        self.citation_handles=[h for h in self.database.iter_citation_handles()]
        self.source_handles=[h for h in self.database.iter_source_handles()]
        self.note_handles=[h for h in self.database.iter_note_handles()]
        self.media_handles=[h for h in self.database.iter_media_handles()]
        self.repository_handles=[h for h in self.database.iter_repository_handles()]
        self.event_handles=[h for h in self.database.iter_event_handles()]

        print("Länge", len(self.person_handles))
#        self.person_handles.sort(key=self.sort.by_place_title_key)

    def write_report(self):
        """
        The routine that actually creates the report.
        At this point, the document is opened and ready for writing.
        """

        # Write the title line. Set in INDEX marker so that this section will be
        # identified as a major category if this is included in a Book report.

        title = self._("all persons Report")
        mark = IndexMark(title, INDEX_TYPE_TOC, 1)
        self.doc.start_paragraph("PLC-ReportTitle")
        self.doc.write_text(title, mark)
        self.doc.end_paragraph()

        self.doc.start_table("PersonTable", "PLC-EventTable")
        column_titles = [_("LNr"), _("handle"), _("Person")]

        self.doc.start_row()
        for title in column_titles:
            self.doc.start_cell("PLC-Details")
            self.doc.start_paragraph("PLC-Details")
            self.doc.write_text(title)
            self.doc.end_paragraph()
            self.doc.end_cell()
        self.doc.end_row()

        self.__write_all_persons()

    def __write_all_persons(self):
        """
        This procedure writes out each of the selected places.
        """
        person_nbr = 1
        print(len(self.person_handles))

        with self._user.progress(_("Person Report"),
                                  _("Generating report"),
                                  len(self.person_handles)) as step:

            for handle in self.person_handles:
                self.__write_person(handle, person_nbr)
                person_nbr += 1
                # increment progress bar
                step()

        family_nbr = 1
        print(len(self.family_handles))
        with self._user.progress(_("Family Report"),
                                  _("Generating report"),
                                  len(self.family_handles)) as step:

            for handle in self.family_handles:
                self.__write_family(handle, family_nbr)
                family_nbr += 1
                # increment progress bar
                step()

        source_nbr = 1
        print(len(self.source_handles))
        with self._user.progress(_("Source Report"),
                                  _("Generating report"),
                                  len(self.source_handles)) as step:

            for handle in self.source_handles:
                self.__write_source(handle, source_nbr)
                source_nbr += 1
                # increment progress bar
                step()

        citation_nbr = 1
        print(len(self.citation_handles))
        with self._user.progress(_("Citation Report"),
                                  _("Generating report"),
                                  len(self.citation_handles)) as step:

            for handle in self.citation_handles:
                self.__write_citation(handle, citation_nbr)
                citation_nbr += 1
                # increment progress bar
                step()

        place_nbr = 1
        print(len(self.place_handles))
        with self._user.progress(_("Place Report"),
                                  _("Generating report"),
                                  len(self.place_handles)) as step:

            for handle in self.place_handles:
                self.__write_place(handle, place_nbr)
                place_nbr += 1
                # increment progress bar
                step()

        repository_nbr = 1
        print(len(self.repository_handles))
        with self._user.progress(_("Repository Report"),
                                  _("Generating report"),
                                  len(self.repository_handles)) as step:

            for handle in self.repository_handles:
                self.__write_repository(handle, repository_nbr)
                repository_nbr += 1
                # increment progress bar
                step()

        media_nbr = 1
        print(len(self.media_handles))
        with self._user.progress(_("Media Report"),
                                  _("Generating report"),
                                  len(self.media_handles)) as step:

            for handle in self.media_handles:
                self.__write_media(handle, media_nbr)
                media_nbr += 1
                # increment progress bar
                step()

        event_nbr = 1
        print(len(self.event_handles))
        with self._user.progress(_("Event Report"),
                                  _("Generating report"),
                                  len(self.event_handles)) as step:

            for handle in self.event_handles:
                self.__write_event(handle, event_nbr)
                event_nbr += 1
                # increment progress bar
                step()

        note_nbr = 1
        print(len(self.note_handles))
        with self._user.progress(_("Note Report"),
                                  _("Generating report"),
                                  len(self.note_handles)) as step:

            for handle in self.note_handles:
                self.__write_note(handle, note_nbr)
                note_nbr += 1
                # increment progress bar
                step()

    def __write_person(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            person = self.database.get_person_from_handle(handle)
            self.__write_row(nbr, handle, person)
        except:
            person = "NOT FOUND"
            self.__write_row(nbr, handle, person)

    def __write_family(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            family = self.database.get_family_from_handle(handle)
            self.__write_row(nbr, handle, family)
        except:
            family = "NOT FOUND"
            self.__write_row(nbr, handle, family)

    def __write_source(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            source = self.database.get_source_from_handle(handle)
            self.__write_row(nbr, handle, source)
        except:
            source = "NOT FOUND"
            self.__write_row(nbr, handle, source)

    def __write_citation(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            citation = self.database.get_citation_from_handle(handle)
            self.__write_row(nbr, handle, citation)
        except:
            citation = "NOT FOUND"
            self.__write_row(nbr, handle, citation)

    def __write_place(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            place = self.database.get_place_from_handle(handle)
            self.__write_row(nbr, handle, place)
        except:
            place = "NOT FOUND"
            self.__write_row(nbr, handle, place)

    def __write_note(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            note = self.database.get_note_from_handle(handle)
            self.__write_row(nbr, handle, note)
        except:
            note = "NOT FOUND"
            self.__write_row(nbr, handle, note)

    def __write_media(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            media = self.database.get_media_from_handle(handle)
            self.__write_row(nbr, handle, media)
        except:
            media = "NOT FOUND"
            self.__write_row(nbr, handle, media)

    def __write_event(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            event = self.database.get_event_from_handle(handle)
            self.__write_row(nbr, handle, event)
        except:
            event = "NOT FOUND"
            self.__write_row(nbr, handle, event)

    def __write_repository(self, handle, nbr):
        """
        This procedure writes out the details of a single place
        """
        try:
            repository = self.database.get_repository_from_handle(handle)
            self.__write_row(nbr, handle, repository)
        except:
            repository = "NOT FOUND"
            self.__write_row(nbr, handle, repository)

    def __write_row(self, nbr, handle, text):

        self.doc.start_row()

        self.doc.start_cell("PLC-Cell")
        self.doc.start_paragraph("PLC-Details")
        self.doc.write_text("%s " % nbr)
        self.doc.end_paragraph()
        self.doc.end_cell()

        self.doc.start_cell("PLC-Cell")
        self.doc.start_paragraph("PLC-Details")
        self.doc.write_text("%s " % handle)
        self.doc.end_paragraph()
        self.doc.end_cell()

        self.doc.start_cell("PLC-Cell")
        self.doc.start_paragraph("PLC-Details")
        self.doc.write_text("%s " % text)
        self.doc.end_paragraph()
        self.doc.end_cell()
        self.doc.end_row()

#------------------------------------------------------------------------
#
# MYPlaceOptions
#
#------------------------------------------------------------------------
class MYAllPeopleOptions(MenuReportOptions):

    """
    Defines options and provides handling interface.
    """

    def __init__(self, name, dbase):
        MenuReportOptions.__init__(self, name, dbase)

    def add_menu_options(self, menu):
        """
        Add options to the menu for the place report.
        """
        category_name = _("Report Options")

        stdoptions.add_localization_option(menu, category_name)

    def make_default_style(self, default_style):
        """
        Make the default output style for the Place report.
        """
        self.default_style = default_style
        self.__report_title_style()
        self.__place_title_style()
        self.__place_details_style()
        self.__column_title_style()
        self.__section_style()
        self.__event_table_style()
        self.__details_style()
        self.__cell_style()
        self.__table_column_style()

    def __report_title_style(self):
        """
        Define the style used for the report title
        """
        font = FontStyle()
        font.set(face=FONT_SANS_SERIF, size=16, bold=1)
        para = ParagraphStyle()
        para.set_font(font)
        para.set_header_level(1)
        para.set_top_margin(0.25)
        para.set_bottom_margin(0.25)
        para.set_alignment(PARA_ALIGN_CENTER)
        para.set_description(_('The style used for the title of the report.'))
        self.default_style.add_paragraph_style("PLC-ReportTitle", para)

    def __place_title_style(self):
        """
        Define the style used for the place title
        """
        font = FontStyle()
        font.set(face=FONT_SERIF, size=12, italic=0, bold=1)
        para = ParagraphStyle()
        para.set_font(font)
        para.set(first_indent=-1.5, lmargin=1.5)
        para.set_top_margin(0.75)
        para.set_bottom_margin(0.25)
        para.set_description(_('The style used for place title.'))
        self.default_style.add_paragraph_style("PLC-PlaceTitle", para)

    def __place_details_style(self):
        """
        Define the style used for the place details
        """
        font = FontStyle()
        font.set(face=FONT_SERIF, size=10)
        para = ParagraphStyle()
        para.set_font(font)
        para.set(first_indent=0.0, lmargin=1.5)
        para.set_description(_('The style used for place details.'))
        self.default_style.add_paragraph_style("PLC-PlaceDetails", para)

    def __column_title_style(self):
        """
        Define the style used for the event table column title
        """
        font = FontStyle()
        font.set(face=FONT_SERIF, size=10, bold=1)
        para = ParagraphStyle()
        para.set_font(font)
        para.set(first_indent=0.0, lmargin=0.0)
        para.set_description(_('The style used for a column title.'))
        self.default_style.add_paragraph_style("PLC-ColumnTitle", para)

    def __section_style(self):
        """
        Define the style used for each section
        """
        font = FontStyle()
        font.set(face=FONT_SERIF, size=10, italic=0, bold=0)
        para = ParagraphStyle()
        para.set_font(font)
        para.set(first_indent=-1.5, lmargin=1.5)
        para.set_top_margin(0.5)
        para.set_bottom_margin(0.25)
        para.set_description(_('The style used for each section.'))
        self.default_style.add_paragraph_style("PLC-Section", para)

    def __event_table_style(self):
        """
        Define the style used for event table
        """
        table = TableStyle()
        table.set_width(100)
        table.set_columns(5)
        table.set_column_width(0, 15)
        table.set_column_width(1, 15)
        table.set_column_width(2, 35)
        table.set_column_width(3, 20)
        table.set_column_width(4, 25)
        self.default_style.add_table_style("PLC-EventTable", table)
        table.set_width(100)
        table.set_columns(5)
        table.set_column_width(0, 35)
        table.set_column_width(1, 15)
        table.set_column_width(2, 25)
        table.set_column_width(3, 20)
        table.set_column_width(4, 25)
        self.default_style.add_table_style("PLC-PersonTable", table)
        table.set_width(100)
        table.set_columns(3)
        table.set_column_width(0, 20)
        table.set_column_width(1, 70)
        table.set_column_width(2, 10)
        self.default_style.add_table_style("PLC-LitTable", table)

    def __details_style(self):
        """
        Define the style used for person and event details
        """
        font = FontStyle()
        font.set(face=FONT_SERIF, size=10)
        para = ParagraphStyle()
        para.set_font(font)
        para.set_description(_('The style used for event and person details.'))
        self.default_style.add_paragraph_style("PLC-Details", para)

    def __cell_style(self):
        """
        Define the style used for cells in the event table
        """
        cell = TableCellStyle()
        self.default_style.add_cell_style("PLC-Cell", cell)

    def __table_column_style(self):
        """
        Define the style used for event table columns
        """
        cell = TableCellStyle()
        cell.set_bottom_border(1)
        self.default_style.add_cell_style('PLC-TableColumn', cell)


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

"""

@author: alexyang

@contact: alex.yang0326@gmail.com

@file: rel_rnn.py

@time: 2018/4/6 10:53

@desc: learn a relation matching score function v(r, q) that measures the similarity between the question & the relation

"""

import os
import logging
import random
import numpy as np
import tensorflow as tf

class RelRank(object):
    def __init__(self, config, sess, word_embeddings):
        self.n_words = config.n_words
        self.n_relations = config.n_relations
        self.embedding_size = config.embedding_size
        self.n_layer = config.n_layer
        self.hidden_size = config.hidden_size
        self.margin = config.margin
        self.batch_size = config.batch_size
        self.max_sequence_len = config.max_sequence_len
        self.n_epoch = config.n_epoch
        self.early_stopping_step = config.early_stopping_step
        self.embedding_trainable = config.embedding_trainable   # whether to fine tune word embeddings

        self.model_path = config.model_path
        if not os.path.exists(self.model_path):
            os.makedirs(self.model_path)
        self.model_name = config.model_name

        self.sess = sess

        self.loss = None
        self.pos_similarity = None
        self.neg_similarity = None
        self.train_step = None

        self.stopping_step = 0

        self.best_loss = 128000.0
        self.best_epoch = 0

        # define placeholder for input
        self.question_ids = tf.placeholder(tf.int32, [None, self.max_sequence_len], name='question_ids')
        self.sequence_len = tf.placeholder(tf.int32, name='sequence_len')
        self.pos_rel_ids = tf.placeholder(tf.int32, name='pos_rel_ids')     # positive relations
        self.neg_rel_ids = tf.placeholder(tf.int32, name='neg_rel_ids')     # negative relations
        self.gru_keep_prob = tf.placeholder(tf.float32, name='gru_keep_prob')

        # embedding layer
        self.word_embeddings = tf.Variable(initial_value=word_embeddings, name='word_embeddings', dtype=tf.float32,
                                           trainable=self.embedding_trainable)
        self.relation_embeddings = tf.Variable(tf.random_uniform([self.n_relations, self.embedding_size], -0.1, 0.1),
                                               name='relation_embeddings')

        self.pos_rel_embed = tf.nn.embedding_lookup(self.relation_embeddings, self.pos_rel_ids)
        self.neg_rel_embed = tf.nn.embedding_lookup(self.relation_embeddings, self.neg_rel_ids)
        self.question_word_embed = tf.nn.embedding_lookup(self.word_embeddings, self.question_ids)

        if not os.path.exists('log'):
            os.makedirs('log')
        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s %(filename)s[line: %(lineno)d] %(levelname)s %(message)s',
                            datefmt='%a, %b %d %H:%M:%S', filename='./log/train.log', filemode='a')
        logging.info(config)

    def single_gru_cell(self):
        return tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.GRUCell(self.hidden_size))

    def multi_gru_cell(self):
        stack_gru = [self.single_gru_cell() for _ in range(self.n_layer)]
        return tf.nn.rnn_cell.MultiRNNCell(stack_gru)

    def build_model(self):
        # bigru layer
        cell_fw = self.multi_gru_cell()
        cell_bw = self.multi_gru_cell()
        _, ((_, fw_state), (_, bw_state)) = tf.nn.bidirectional_dynamic_rnn(cell_fw=cell_fw,
                                                                            cell_bw=cell_bw,
                                                                            inputs=self.question_word_embed,
                                                                            sequence_length=self.sequence_len,
                                                                            dtype=tf.float32)
        question_gru = tf.concat([fw_state, bw_state], axis=-1)     # [batch_size, 2*hidden_size]

        # linear layer to project final hidden state of BiGRU to the same vector as entity embedding
        w_linear = tf.Variable(tf.truncated_normal([2*self.hidden_size, self.embedding_size], stddev=0.01),
                               name='w_linear')
        b_linear = tf.Variable(tf.constant(0.1, shape=[self.embedding_size]), name='b_linear')
        question_embed = tf.matmul(question_gru, w_linear) + b_linear

        # similarity layer
        self.pos_similarity = self.cosine_sim(question_embed, self.pos_rel_embed, name='pos_similarity')
        self.neg_similarity = self.cosine_sim(question_embed, self.neg_rel_embed, name='neg_similarity')

        # triplet loss
        # triplet_loss = tf.nn.relu(self.margin - self.pos_similarity + self.neg_similarity)
        # pos_triplets = tf.cast(tf.greater(triplet_loss, 1e-16), tf.float32)
        # num_pos_triplets = tf.reduce_sum(pos_triplets)
        # self.loss = tf.reduce_sum(triplet_loss) / (num_pos_triplets + 1e-16)
        self.loss = tf.reduce_mean(tf.nn.relu(self.margin - self.pos_similarity + self.neg_similarity))
        self.train_step = tf.train.AdamOptimizer().minimize(self.loss)

    def train(self, train_data, valid_data, valid_data_metric):
        self.sess.run(tf.global_variables_initializer())
        self.sess.run(tf.local_variables_initializer())

        saver = tf.train.Saver(max_to_keep=self.n_epoch)

        question_ids, sequence_len, pos_rel_ids, neg_rel_ids = train_data

        loops = int((len(question_ids) + self.batch_size - 1) / self.batch_size)
        for epoch in range(self.n_epoch):
            avg_loss = 0.0

            # shuffle data matters!
            shuffle_index = list(range(question_ids.shape[0]))
            random.shuffle(shuffle_index)

            for i in range(loops):
                feed_dict = {self.question_ids: question_ids[shuffle_index[i: i + self.batch_size]],
                             self.sequence_len: sequence_len[shuffle_index[i: i + self.batch_size]],
                             self.pos_rel_ids: pos_rel_ids[shuffle_index[i: i + self.batch_size]],
                             self.neg_rel_ids: neg_rel_ids[shuffle_index[i: i + self.batch_size]],
                             self.gru_keep_prob: 0.5}
                _, loss = self.sess.run([self.train_step, self.loss], feed_dict=feed_dict)
                if loss == 0.0:
                    break
                avg_loss += loss

            avg_loss /= loops

            logging.info('%s %d train_loss: %f' % (self.model_name, epoch, avg_loss))
            print('%s %d train_loss: %f' % (self.model_name, epoch, avg_loss))

            saver.save(sess=self.sess, save_path=os.path.join(self.model_path, self.model_name), global_step=epoch)

            valid_loss = self.valid(valid_data)
            self.compute_accuracy(valid_data_metric, 'valid_acc')
            if valid_loss < self.best_loss:
                self.best_loss = valid_loss
                self.best_epoch = epoch
                self.stopping_step = 0
            else:
                self.stopping_step += 1
            if self.stopping_step >= self.early_stopping_step:
                print('%s early stopping is trigger at epoch: %d' % (self.model_name, epoch))
                logging.info('%s early stopping is trigger at epoch: %d' % (self.model_name, epoch))
                break

        print('%s best epoch: %d, best loss: %f' % (self.model_name, self.best_epoch, self.best_loss))
        logging.info('%s best epoch: %d, best loss: %f' % (self.model_name, self.best_epoch, self.best_loss))

        self.only_save_best_epoch(self.model_path, self.model_name, self.best_epoch)

    def valid(self, valid_data):
        question_ids, sequence_len, pos_rel_ids, neg_rel_ids = valid_data
        # restrict the amount of data to be fed to avoid OOM error
        feed_dict = {self.question_ids: question_ids[:25600],
                     self.sequence_len: sequence_len[:25600],
                     self.pos_rel_ids: pos_rel_ids[:25600],
                     self.neg_rel_ids: neg_rel_ids[:25600],
                     self.gru_keep_prob: 1.0}
        loss = self.sess.run(self.loss, feed_dict=feed_dict)

        logging.info('%s valid_loss: %f' % (self.model_name, loss))
        print('%s valid_loss: %f' % (self.model_name, loss))

        return loss

    def compute_accuracy(self, data_metric, data_type):
        """"compute top k hits accuracy"""
        q_word_ids, q_seq_len, q_pos_rel_ids, cand_rel_ids = data_metric
        top1 = top3 = top5 = top10 = 0

        data_size = min(len(q_word_ids), 1000)
        for i in range(data_size):
            score = {}

            # compute score for each candidate relation
            q_word_ids_npy = np.zeros([1, 60])
            q_word_ids_npy[0, :len(q_word_ids[i])] = q_word_ids[i]
            mul_q_word_ids_npy = np.tile(q_word_ids_npy, (len(cand_rel_ids[i]), 1))

            mul_q_seq_len = np.tile(q_seq_len[i:i + 1], len(cand_rel_ids[i]))
            feed_dict = {self.question_ids: mul_q_word_ids_npy,
                         self.sequence_len: mul_q_seq_len,
                         self.pos_rel_ids: cand_rel_ids[i],
                         self.gru_keep_prob: 1.0
                         }
            similarity = self.sess.run(self.pos_similarity, feed_dict=feed_dict)

            for j in range(len(cand_rel_ids[i])):
                score[cand_rel_ids[i][j]] = similarity[j]

            # rank by similarity score
            sorted_score = sorted(score.items(), key=lambda x:x[1], reverse=True)
            sorted_rel = [score[0] for score in sorted_score]

            if q_pos_rel_ids[i] in sorted_rel[:1]:
                top1 += 1
            if q_pos_rel_ids[i] in sorted_rel[:3]:
                top3 += 1
            if q_pos_rel_ids[i] in sorted_rel[:5]:
                top5 += 1
            if q_pos_rel_ids[i] in sorted_rel[:10]:
                top10 += 1
        print('%s %s: hits@1: %f hits@3: %f hits@5: %f hits@10: %f' %
              (self.model_name, data_type, top1/data_size, top3/data_size,
               top5/data_size, top10/data_size))
        logging.info('%s %s: hits@1: %f hits@3: %f hits@5: %f hits@10: %f' %
              (self.model_name, data_type, top1/data_size, top3/data_size,
               top5/data_size, top10/data_size))

    @staticmethod
    def cosine_sim(a, b, name='cosine_sim'):
        a_norm = tf.nn.l2_normalize(a, axis=-1)
        b_norm = tf.nn.l2_normalize(b, axis=-1)
        return tf.reduce_sum(tf.multiply(a_norm, b_norm), axis=-1, name=name)

    @staticmethod
    def only_save_best_epoch(model_path, model_name, best_epoch):
        data_suffix = '.data-00000-of-00001'
        data_name = model_name + '-' + str(best_epoch) + data_suffix

        index_suffix = '.index'
        index_name = model_name + '-' + str(best_epoch) + index_suffix

        meta_suffix = '.meta'
        meta_name = model_name + '-' + str(best_epoch) + meta_suffix

        for file in os.listdir(model_path):
            if file.startswith(model_name):
                if file == data_name or file == index_name or file == meta_name:
                    continue
                elif file.endswith(data_suffix) or file.endswith(index_suffix) or file.endswith(meta_suffix):
                    os.remove(os.path.join(model_path, file))


condicion_1= 0
condicion_2 =1

if(condicion_1 and condicion_2) or condicion_2:
    print("Es verdad")
else:
    print("No es verdad")
print()

variable1= 1
variable2= 0

if (variable1 and variable2) or variable2:
    print("Es verdad")
elif variable1:
    print("Solo se cumple la condicion 1")
else:
    print("Ninguna fue verdad es verdad")


# ----------------------------------------------------------------------------
# DiSCUS - plain example file
# http://code.google.com/p/discus/
# Angel Goni-Moreno - www.angelgm.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------------------------------------------------

###############################################################################
### This exapmle simulates a bacterial strain growing in emtpy space. Thus, the
### walls are defined as NONE. As well as the program (genetic circuit = NONE
###############################################################################

from Imports.definitions import *
from Imports.cell_class import *
from discus import *

screenview = 300 			### Scren size
screen = pygame.display.set_mode((screenview, screenview))
clock = pygame.time.Clock()
space = pymunk.Space()
bacs = [] #array containing list of bacteria object.
walls = None

### Place a cell in the world

points = [(0, -width), (0, width), (length,width), (length, -width)]

#specify location of the cell in the middle of the screen view
r_y = round(screenview/2)
r_x = round(screenview/2)

#specify original cell orientation randomly
r_angle = random.randint(0,360)

bac_shape = add_bac(space,r_x,r_y, math.radians(r_angle), points)
b = bacteria(bac_shape,None,[0,0],[bac_shape.body.position.x, bac_shape.body.position.y], 0, False, False,1,0)
#add bacteria object to the list of bacteria
bacs.append(b)

def main():
    discus(screenview, screen, clock, bacs, space, walls) #simulator function
if __name__ == '__main__':
    main()


#!/usr/bin/env python

"""update Microsoft software on Mac OSX"""

# File: ratom/microsoft.py
# Version: 1.0.0
# Date: 2016-05-25
# Author: qtfkwk <qtfkwk+ratom@gmail.com>
# Copyright: (C) 2016 by qtfkwk
# License: BSD 2-Clause License (https://opensource.org/licenses/BSD-2-Clause)

from common import *

_f = '/Library/Application Support/Microsoft/MAU2.0/Microsoft AutoUpdate.app'

def check():
    """check if can update Microsoft software on Mac OSX"""
    return os.path.isdir(_f)

def main(argv=None, cfg=None):
    """update Microsoft software on Mac OSX"""
    if cfg == None:
        cfg = args(argv)
    log = logging.getLogger('ratom')
    log.info('microsoft: started')
    if not check():
        log.info('microsoft: failed check')
        return
    c = 'open -W %s' % _f.replace(' ', r'\ ')
    section('Microsoft', c, dryrun=cfg['dryrun'])
    log.info('microsoft: finished')

if __name__ == '__main__':
    main()


import logging

from sqlalchemy import (
    create_engine,
    Table, Column, Integer, String, DateTime, Boolean
)
from sqlalchemy.schema import MetaData

class SQLAlchemy:

    def __init__(self, dsn):
        engine = create_engine(dsn)
        self._metadata = MetaData(engine)

    def create_all_tables(self):
        logging.debug("Creating all DB tables")
        self._metadata.create_all()

    def configure_mappings(self):
        logging.debug("Configuring DB mappings")
        user = Table('users', self._metadata,
            Column('id', Integer, primary_key=True),
            Column('email', String(254), nullable=False),  # compliant with RFCs 3696 and 5321
            Column('password', String(128), nullable=False),
            Column('last_login', DateTime()),
            Column('created_at', DateTime()),
            Column('is_active', Boolean()),
        )
        sessions = Table('sessions', self._metadata,
            Column('id', String(40), primary_key=True),
            Column('data', String(262144), nullable=False),  # approx. 1 mb
            Column('expires_at', DateTime(), nullable=False),
        )
        return self._metadata


#
# Simple example scene with a moving obstacle
#
from manta import *
import os

GUI = True
save = True

dim = 2
res = 128
gs  = vec3(res,res, 1 if dim==2 else res )
s   = Solver(name='main', gridSize = gs, dim=dim)

# allocate grids
flags   = s.create(FlagGrid)
vel     = s.create(MACGrid)
density = s.create(RealGrid)
pressure = s.create(RealGrid)
obsVel  = s.create(MACGrid)

bWidth = 1
flags.initDomain(boundaryWidth=bWidth)
flags.fillGrid()
setOpenBound(flags, bWidth,'yY',FlagOutflow|FlagEmpty)

source = s.create(Cylinder, center=gs*vec3(0.5,0.1,0.5), radius=res*0.14, z=gs*vec3(0, 0.02, 0))

# init obstacle properties
obsPos = vec3(0.5,0.5,0.5)
obsSize = 0.1
obsVel.setConst(Vec3(0.))
obsVel.setBound(value=Vec3(0.), boundaryWidth=bWidth+1) # make sure walls are static
obs = Sphere( parent=s, center=gs*obsPos, radius=res*obsSize)
phiObs = obs.computeLevelset()
setObstacleFlags(flags=flags, phiObs=phiObs)
flags.fillGrid()

#################################### save to a file
if save:
    simNo    = 1000  # start ID
    basePath = os.path.dirname(__file__) + '/../../data/'
    folderNo = simNo
    pathaddition = 'circlePlume128x128_%04d/' % folderNo
    while os.path.exists(basePath + pathaddition):
        folderNo += 1
        pathaddition = 'circlePlume128x128_%04d/' % folderNo

    simPath = basePath + pathaddition
    print("Using output dir '%s'" % simPath)
    simNo = folderNo
    os.makedirs(simPath)

if (GUI):
    gui = Gui()
    gui.show( True )
    #gui.pause()

#main loop
for t in range(150):
    if t<100:
        source.applyToGrid(grid=density, value=1)

    mantaMsg('\nFrame %i' % (s.frame))

    advectSemiLagrange(flags=flags, vel=vel, grid=density, order=2)
    advectSemiLagrange(flags=flags, vel=vel, grid=vel,     order=2)
    resetOutflow(flags=flags,real=density)

    setWallBcs(flags=flags, vel=vel, phiObs=phiObs, obvel=obsVel)
    addBuoyancy(density=density, vel=vel, gravity=vec3(0,-4e-3,0), flags=flags)
    solvePressure(flags=flags, vel=vel, pressure=pressure)

    ################################# write divergence and pressure to a file
    if save:
        writeDivergence(vel, simPath + 'div_vel_%04d.uni' % (t) )
        pressure.save(simPath + 'pressure_%04d.uni' % (t))
        flags.save(simPath + 'flags_%04d.uni' % (t))

    s.step()


import sys
sys.path.insert(0, './spiders/py-scripts')

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector

from mongo_access import insert_image
from utils import valid_ext

class MySpider(CrawlSpider):
    name = 'bonjour_madame'
    allowed_domains = ['dites.bonjourmadame.fr']
    start_urls = ['http://dites.bonjourmadame.fr/']

    custom_settings = {
        "AUTOTHROTTLE_ENABLED":True,
        "AUTOTHROTTLE_START_DELAY":0.1,
        "AUTOTHROTTLE_MAX_DELAY":0.3,
        "AUTOTHROTTLE_TARGET_CONCURRENCY":10
    }

    rules = (
        Rule(LinkExtractor(), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        hxs = Selector(response)
        # this xpath selector will only select the full sized images and ignore the mini.
        imgurls = hxs.xpath("//div[contains(@class, 'photo post')]/a/img/@src").extract()
        imgurls = list(map(lambda x: x.split('?')[0], imgurls))

        for imgurl in imgurls :
            if not(valid_ext(imgurl)):
                continue
            image_obj = {
                "image_url" : imgurl,
                "webpage_url" : response.url
            }
            insert_image(image_obj)


# -*- coding: utf-8 -*-
# Copyright (c) 2020, Sachin Mane and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from latte.utils.background.job import background
from latte.utils.background.job import SUCCESS
from latte.utils.background.job import FAILURE
from latte.job.background_jobs.job_fixer import Helper
from latte.job.enums.job_status import Job_status

class Job(Document):

	@background(identity= lambda self: self.name)
	def execute(self):
		self.lock_for_update()
		doc = frappe.get_doc(self.ref_doctype, self.ref_docname)
		getattr(doc, self.method)(doc, __job__=False)

	def update_status(self,status):
		super().update_status(status)
		if status in { Job_status.SUCCESS.value, Job_status.FAILURE.value}:
			self.db_set("ended_on", frappe.utils.now_datetime())
		if status == Job_status.PROCESSING.value:
			self.increment_retries()
		frappe.db.commit()

	def increment_retries(self):
		self.db_set("retry_count", self.retry_count+1)

	def add_job_run_as_child(self, job_run_doc):
		job_run_doc.parent = self.name
		job_run_doc.parenttype = self.doctype
		job_run_doc.parentfield = "job_run"
		job_run_doc.db_update()

	def get_job_status_from_job_run_status(self, job_run_status):
		if job_run_status == SUCCESS:
			return Job_status.SUCCESS.value
		else:
			if self.retry_count >= self.max_retries:
				return Job_status.FAILURE.value
			else:
				return Job_status.PENDING.value

	def lock_for_update(self):
		sql_query_to_lock = "SELECT name FROM `tabJob` where name=%(name)s FOR UPDATE NOWAIT"
		frappe.db.sql(sql_query_to_lock, {
			'name': self.name,
		})

	@staticmethod
	def update_job_status(job_run_doc):
		job_run_status = job_run_doc.db_get("status")
		if job_run_status in [SUCCESS, FAILURE] and job_run_doc.ident:
			if frappe.db.exists("Job", job_run_doc.ident):
				job = frappe.get_doc("Job", job_run_doc.ident)
				job.update_status(job.get_job_status_from_job_run_status(job_run_status))
				job.add_job_run_as_child(job_run_doc)

@frappe.whitelist()
def retry(name):
	frappe.only_for('System Manager')
	frappe.get_doc('Job', name).execute()

def update_job_on_job_run_status_change(doc, event):
	Job.update_job_status(doc)

def on_doctype_update():
	frappe.db.add_index("Job", ["ref_doctype", "ref_docname"])


import pandas as pd
import matplotlib.pyplot as plt
import os
import pylab as pl
import numpy as np
from functools import reduce
from math import ceil,floor
from collections import Counter

def createAllCertainFormatFileList(filePath, fileFormat):
    filenameList = [os.path.join(filePath, relativeFilename) for relativeFilename in os.listdir(filePath)
                    if os.path.isfile(os.path.join(filePath, relativeFilename))
                    if os.path.splitext(relativeFilename)[1] in fileFormat]
    return filenameList

def calculateMidlineRatioAcrossByNoise(trajectory,bean1Grid,bean2Grid,noise,action):
    # trajectory的第n个点是被噪声影响的点，index的n-1点被噪声影响，index的n-2点产生噪声
    midlineList=list()
    noise.sort()
    for noisePoint in noise:
        pacmanGrid=trajectory[noisePoint]
        midlineFlag = np.linalg.norm(np.array(pacmanGrid) - np.array(bean1Grid), ord=1) == np.linalg.norm(
            np.array(pacmanGrid) - np.array(bean2Grid), ord=1)
        if not midlineFlag:
            noisePointToAnalyse=noisePoint
            break
        else:
            noisePointToAnalyse=999
    if noisePointToAnalyse!=999:
        for step in range(noisePointToAnalyse,len(trajectory)):
            pacmanGrid=trajectory[step]
            midlineFlag=np.linalg.norm(np.array(pacmanGrid) - np.array(bean1Grid), ord=1)== np.linalg.norm(np.array(pacmanGrid) - np.array(bean2Grid), ord=1)
            if midlineFlag:
                if step+1 not in noise:
                    midlineList.append(pacmanGrid)
                else:
                    realPacmanGrid=np.array(trajectory[step-1])+np.array(action[step-1])
                    if  np.linalg.norm(np.array(realPacmanGrid) - np.array(bean1Grid), ord=1)== np.linalg.norm(np.array(realPacmanGrid) - np.array(bean2Grid), ord=1):
                        midlineList.append(pacmanGrid)
        return midlineList
    else:
        return None

if __name__ == "__main__":
    resultsPath = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) + '/Results/max'
    fileFormat = '.csv'
    resultsFilenameList = createAllCertainFormatFileList(resultsPath, fileFormat)
    resultsDataFrameList = [pd.read_csv(file) for file in resultsFilenameList]
    avoidCommitmentRatioList=list()
    for results in resultsDataFrameList:
        participantAvoidCommitmentRatioList=list()
        trialNumber=results.shape[0]
        for i in range(trialNumber):
            # trajectory的第n个点是被噪声影响的点，index的n-1点被噪声影响，index的n-2点产生噪声
            bean1Grid=[results.iat[i,2],results.iat[i,3]]
            bean2Grid=[results.iat[i,4],results.iat[i,5]]
            playerGrid=[results.iat[i,5],results.iat[i,7]]
            noiseList=eval(results.iat[i,11])
            actionList=eval(results.iat[i,10])
            trajectory=eval(results.iat[i,9])
            if len(noiseList)>0:
                midline=calculateMidlineRatioAcrossByNoise(trajectory,bean1Grid,bean2Grid,noiseList,actionList)
                if midline is not None:
                    participantAvoidCommitmentRatioList.append(len(midline)/len(trajectory))
        if len(participantAvoidCommitmentRatioList)!=0:
            avoidCommitmentRatioList.append(np.mean(np.array(participantAvoidCommitmentRatioList)))
    averageAvoidCommitmentRatio = np.mean(np.array(avoidCommitmentRatioList))
    stdAvoidCommitmentRatio = np.std(np.array(avoidCommitmentRatioList))/np.sqrt(len(avoidCommitmentRatioList)-1)
    print(averageAvoidCommitmentRatio)
    print(stdAvoidCommitmentRatio)


import os
import threading
import time

from networktables import NetworkTables
from PIL import Image
from PIL.ImageColor import getcolor, getrgb
from PIL.ImageOps import grayscale

from StreamDeck.DeviceManager import DeviceManager
from StreamDeck.ImageHelpers import PILHelper

ASSETS_PATH = os.path.join(os.path.dirname(__file__), "assets")

def image_tint(src, tint="#ffffff"):  # From https://stackoverflow.com/a/12310820
    if src.mode not in ["RGB", "RGBA"]:
        raise TypeError("Unsupported source image mode: {}".format(src.mode))
    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")
    if not tl:
        tl = 1
    tl = float(tl)
    sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb))
    luts = (
        tuple(map(lambda lr: int(lr * sr + 0.5), range(256)))
        + tuple(map(lambda lg: int(lg * sg + 0.5), range(256)))
        + tuple(map(lambda lb: int(lb * sb + 0.5), range(256)))
    )
    l = grayscale(src)
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))
    else:
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))

        luts += tuple(range(256))

    return Image.merge(*merge_args).point(luts)

ASSETS_PATH = os.path.join(os.path.dirname(__file__), "icons")

def render_key_image(deck, icon_filename):

    image = PILHelper.create_image(deck)

    icon = Image.open(ASSETS_PATH + "/" + icon_filename).convert("RGBA")
    icon.thumbnail((image.width, image.height - 20), Image.LANCZOS)
    icon_pos = ((image.width - icon.width) // 2, 0)
    image.paste(icon, icon_pos, icon)

    return image

def key_change_callback(deck, key, state):
    if state:
        button = buttons[key]
        button.set(deck)

class Button:
    def __init__(self, key):
        self.key = key
        self.active = False

    def set(self, deck):
        sd.putBoolean(f"Action/{self.key}", True)
        self.update(deck)

    def update(self, deck):
        x = sd.getBoolean(f"Status/{self.key}", False)
        y = sd.getBoolean(f"Action/{self.key}", False)
        icon_array = sd.getStringArray("Icons", [])
        name = icon_array[self.key]
        image = None
        if x:
            image = render_key_image(deck, name + "/active.png")
        else:
            image = render_key_image(deck, name + "/inactive.png")
        if y:
            image = image_tint(image, tint="#882020")
        image = PILHelper.to_native_format(deck, image)
        deck.set_key_image(self.key, image)

# As a client to connect to a robot
NetworkTables.initialize(server="10.11.89.2")
# NetworkTables.initialize(server="127.0.0.1")
time.sleep(3)

sd = NetworkTables.getTable("StreamDeck/0")
# a = [
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
#     "default",
# ]
# sd.putStringArray("Icons", a)

buttons = []

for i in range(0, 15):
    sd.putBoolean(f"Action/{i}", False)
    sd.putBoolean(f"Status/{i}", False)
    button = Button(i)
    buttons.append(button)

deck = DeviceManager().enumerate()[0]
deck.open()
deck.reset()
print(
    "Opened '{}' device (serial number: '{}')".format(
        deck.deck_type(), deck.get_serial_number()
    )
)

# Set initial screen brightness to 30%.
deck.set_brightness(30)
# Set initial key images.
# for key in range(deck.key_count()):
#    update_key_image(deck, key, False)

# Register callback function for when a key state changes.
deck.set_key_callback(key_change_callback)

while True:
    for button in buttons:
        button.update(deck)

# Wait until all application threads have terminated (for this example,
# this is when all deck handles are closed).
for t in threading.enumerate():
    if t is threading.currentThread():
        continue
    if t.is_alive():
        t.join()


# This code gets two lists (listA, listB) and create a third one (listAB) anternating between the first and second lists

listA = []              # First List
listB = []              # Second List
listAB = []             # Third list

for x in range(10):
    value = float(input("Enter Values To The List A:"))
    listA.append(value)             # Add input to the list A
print("List A Complete\n")          # Tell user list is complete

for y in range(10):
    value = float(input("Enter Values To The List B:"))
    listB.append(value)             # Add input to the list B
print("List B Complete\n")          # Tell user list is complete

for z in range(10):
    listAB.append(listA[z])         # Add the values interleaved
    listAB.append(listB[z])         # Add the values interleaved

print(listAB, "= Interleaved."
              " Alternating Between First and Second List")


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

# In[ ]:

get_ipython().magic(u'tensorflow_version 1.x')
from google.colab.patches import cv2_imshow
import cv2

# In[ ]:

# install the mtcnn package
get_ipython().system(u'pip install mtcnn')

# In[ ]:

import mtcnn
mtcnn_model = mtcnn.MTCNN()

# In[ ]:

# download an image
get_ipython().system(u'curl -o pixney_faces.jpeg https://i.pinimg.com/originals/cf/04/14/cf0414f971aa50c87140ed49223da33b.jpg')
disneypixarfaces = cv2.imread('pixney_faces.jpeg')
cv2_imshow(disneypixarfaces)

# In[ ]:

def recognize_faces(new_img):
  faces = mtcnn_model.detect_faces(new_img)
  for i in range(0, len(faces)):
      x, y, w, h = faces[i]['box']
      # draw rectangle
      cv2.rectangle(new_img, (x, y), (x+w, y+h), (255, 0, 0), 1)
  return new_img

# In[ ]:

img = disneypixarfaces
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
final_img = recognize_faces(img)
cv2_imshow(final_img[:,:,::-1])

# In[ ]:

# bonus: how to download and play video in colab
# Download sample video
get_ipython().system(u'curl -o sample.mp4 https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4')
from IPython.display import HTML
from base64 import b64encode
mp4 = open('sample.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
      <source src="%s" type="video/mp4">
</video>
""" % data_url)


from __future__ import absolute_import, print_function, unicode_literals

import json

import requests
import aiohttp

from simplifiedpytrends import exceptions

class TrendReq(object):
    """
    Google Trends API
    """

    GET_METHOD = 'get'
    POST_METHOD = 'post'

    GENERAL_URL = 'https://trends.google.com/trends/api/explore'
    INTEREST_OVER_TIME_URL = 'https://trends.google.com/trends/api/widgetdata/multiline'
    INTEREST_BY_REGION_URL = 'https://trends.google.com/trends/api/widgetdata/comparedgeo'
    RELATED_QUERIES_URL = 'https://trends.google.com/trends/api/widgetdata/relatedsearches'
    TRENDING_SEARCHES_URL = 'https://trends.google.com/trends/hottrends/hotItems'
    TOP_CHARTS_URL = 'https://trends.google.com/trends/topcharts/chart'
    SUGGESTIONS_URL = 'https://trends.google.com/trends/api/autocomplete/'
    CATEGORIES_URL = 'https://trends.google.com/trends/api/explore/pickers/category'

    def __init__(self, hl='en-US', tz=360, geo='', proxies=''):
        """
        Initialize default values for params
        """
        # google rate limit
        self.google_rl = 'You have reached your quota limit. Please try again later.'
        self.results = None
        self.aiohttp_session = None

        # set user defined options used globally
        self.tz = tz
        self.hl = hl
        self.geo = geo
        self.kw_list = list()
        self.proxies = proxies #add a proxy option
        self.cookies = dict(filter(
            lambda i: i[0] == 'NID',
            requests.get(
                'https://trends.google.com/?geo={geo}'.format(geo=hl[-2:])
            ).cookies.items()
        ))

        # intialize widget payloads
        self.token_payload = dict()
        self.interest_over_time_widget = dict()
        self.interest_by_region_widget = dict()
        self.related_topics_widget_list = list()
        self.related_queries_widget_list = list()

    @staticmethod
    def _handle_req_response(response, resp_text, trim_chars=0):
        # check if the response contains json and throw an exception otherwise
        # Google mostly sends 'application/json' in the Content-Type header,
        # but occasionally it sends 'application/javascript
        # and sometimes even 'text/javascript
        if 'application/json' in response.headers['Content-Type'] or \
                'application/javascript' in response.headers['Content-Type'] or \
                'text/javascript' in response.headers['Content-Type']:

            # trim initial characters
            # some responses start with garbage characters, like ")]}',"
            # these have to be cleaned before being passed to the json parser
            content = resp_text[trim_chars:]

            # parse json
            return json.loads(content)
        else:
            # this is often the case when the amount of keywords in the payload for the IP
            # is not allowed by Google
            status = response.status if hasattr(response, "status") else response.status_code
            raise exceptions.ResponseError('The request failed: Google returned a '
                                           'response with code {0}.'.format(status), response=response)

    def _get_data(self, url, method=GET_METHOD, trim_chars=0, **kwargs):
        """Send a request to Google and return the JSON response as a Python object

        :param url: the url to which the request will be sent
        :param method: the HTTP method ('get' or 'post')
        :param trim_chars: how many characters should be trimmed off the beginning of the content of the response
            before this is passed to the JSON parser
        :param kwargs: any extra key arguments passed to the request builder (usually query parameters or data)
        :return:
        """
        s = requests.session()
        s.headers.update({'accept-language': self.hl})
        if self.proxies != '':
            s.proxies.update(self.proxies)
        if method == TrendReq.POST_METHOD:
            response = s.post(url, cookies=self.cookies, **kwargs)
        else:
            response = s.get(url, cookies=self.cookies, **kwargs)

        return TrendReq._handle_req_response(response, response.text, trim_chars=trim_chars)

    async def _async_get_data(self, url, method=GET_METHOD, trim_chars=0, aiohttp_session: aiohttp.ClientSession = None,
                              **kwargs):
        """Send a request to Google and return the JSON response as a Python object

        :param url: the url to which the request will be sent
        :param method: the HTTP method ('get' or 'post')
        :param trim_chars: how many characters should be trimmed off the beginning of the content of the response
            before this is passed to the JSON parser
        :param kwargs: any extra key arguments passed to the request builder (usually query parameters or data)
        :return:
        """
        aiohttp_session = aiohttp_session or self.aiohttp_session
        headers = {'accept-language': self.hl}
        if method == TrendReq.POST_METHOD:
            async with aiohttp_session.post(url, cookies=self.cookies, headers=headers, **kwargs) as response:
                return TrendReq._handle_req_response(response, await response.text(), trim_chars=trim_chars)
        else:
            async with aiohttp_session.get(url, cookies=self.cookies, headers=headers, **kwargs) as response:
                return TrendReq._handle_req_response(response, await response.text(), trim_chars=trim_chars)

    def _init_payload(self, kw_list, cat, timeframe, geo, gprop):
        """Create the payload for related queries, interest over time and interest by region"""
        self.kw_list = kw_list
        self.geo = geo
        self.token_payload = {
            'hl': self.hl,
            'tz': self.tz,
            'req': {'comparisonItem': [], 'category': cat, 'property': gprop}
        }

        # build out json for each keyword
        for kw in self.kw_list:
            keyword_payload = {'keyword': kw, 'time': timeframe, 'geo': self.geo}
            self.token_payload['req']['comparisonItem'].append(keyword_payload)
        # requests will mangle this if it is not a string
        self.token_payload['req'] = json.dumps(self.token_payload['req'])
        return

    async def async_build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='', gprop=''):
        """Makes request to Google to get API tokens for interest over time, interest by region and related queries"""
        self._init_payload(kw_list, cat, timeframe, geo, gprop)
        # make the token request and parse the returned json
        widget_dict = (await self._async_get_data(
            url=TrendReq.GENERAL_URL,
            method=TrendReq.GET_METHOD,
            params=self.token_payload,
            trim_chars=4,
        ))['widgets']
        self._tokens(widget_dict)
        return

    def build_payload(self, kw_list, cat=0, timeframe='today 5-y', geo='', gprop=''):
        """Makes request to Google to get API tokens for interest over time, interest by region and related queries"""
        self._init_payload(kw_list, cat, timeframe, geo, gprop)
        # make the token request and parse the returned json
        widget_dict = self._get_data(
            url=TrendReq.GENERAL_URL,
            method=TrendReq.GET_METHOD,
            params=self.token_payload,
            trim_chars=4,
        )['widgets']
        self._tokens(widget_dict)
        return

    def _tokens(self, widget_dict):
        # order of the json matters...
        first_region_token = True
        # clear self.related_queries_widget_list and self.related_topics_widget_list
        # of old keywords'widgets
        self.related_queries_widget_list[:] = []
        self.related_topics_widget_list[:] = []
        # assign requests
        for widget in widget_dict:
            if widget['id'] == 'TIMESERIES':
                self.interest_over_time_widget = widget
            if widget['id'] == 'GEO_MAP' and first_region_token:
                self.interest_by_region_widget = widget
                first_region_token = False
            # response for each term, put into a list
            if 'RELATED_TOPICS' in widget['id']:
                self.related_topics_widget_list.append(widget)
            if 'RELATED_QUERIES' in widget['id']:
                self.related_queries_widget_list.append(widget)
        return

    @staticmethod
    def _handle_interest_over_time(req_json):
        result_list = []
        timestamp_key = "timestamp"
        data_key = "data"

        for req_data in req_json['default']['timelineData']:
            result_list.append({
                timestamp_key: float(req_data["time"]),
                data_key: float(req_data["value"][0])
            })

        # sort by time
        sorted_result = sorted(result_list, key=lambda a: a[timestamp_key])

        return sorted_result

    def _get_over_time_payload(self):
        return {
            # convert to string as requests will mangle
            'req': json.dumps(self.interest_over_time_widget['request']),
            'token': self.interest_over_time_widget['token'],
            'tz': self.tz
        }

    # returns a list of dicts containing {"timestamp", "data"}
    def interest_over_time(self):
        """Request data from Google's Interest Over Time section and return a dataframe"""

        over_time_payload = self._get_over_time_payload()

        # make the request and parse the returned json
        req_json = self._get_data(
            url=TrendReq.INTEREST_OVER_TIME_URL,
            method=TrendReq.GET_METHOD,
            trim_chars=5,
            params=over_time_payload,
        )
        return TrendReq._handle_interest_over_time(req_json)

    # returns a list of dicts containing {"timestamp", "data"}
    async def async_interest_over_time(self):
        """Request data from Google's Interest Over Time section and return a dataframe"""

        over_time_payload = self._get_over_time_payload()

        # make the request and parse the returned json
        req_json = await self._async_get_data(
            url=TrendReq.INTEREST_OVER_TIME_URL,
            method=TrendReq.GET_METHOD,
            trim_chars=5,
            params=over_time_payload,
        )
        return TrendReq._handle_interest_over_time(req_json)


#!usr/bin/env python3
# 08/09/2020 SET

'''
3. Modifique o exercício anterior para que um nome de
animal seja lido e escrito no local adequado.
'''

substantivo = input("Digite um substantivo: ") #Sem quebra de linha pra ficar mais legível na execução
adjetivo = input("Digite um adjetivo:")
verbo = input("Digite um verbo: ")
animal = input("Digite um animal: ")

#Abaixo o espaço após o "O" e antes de "sobre" não são necessários,
#visto que é automaticamente adicionado pela vírgula de delimitação.
print("O",substantivo, adjetivo, verbo, "sobre o", animal, "marrom preguiçoso")


'''
1. You are given a number n and a number m separated by line-break representing the length and breadth of a n * m floor.
2. You've an infinite supply of m * 1 tiles.
3. You are required to calculate and print the number of ways floor can be tiled using tiles.

Input Format:

A number n
A number m

Sample Input:

39
16

Sample Output:

61

https://www.youtube.com/watch?v=_c_R-uIi-zU&list=PL-Jc9J83PIiG8fE6rj9F5a6uyQ5WPdqKy&index=27

'''

def tiling(n, m):

    dp = [0] * (n+1)

    for i in range(1, n+1):

        if(i < m):
            dp[i] = 1

        elif(i == m):
            dp[i] = 2
        else:
            dp[i] = dp[i-1] + dp[i-m]

    return dp[n]

if __name__ == "__main__":

    n = 39
    m = 16
    print(tiling(n, m))


from flask import Flask, render_template, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from os import environ
import requests
import random
import json

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = environ.get('dbURL') or 'mysql+mysqlconnector://root@localhost:3306/box'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 299}

db = SQLAlchemy(app)
CORS(app)

class Box(db.Model):
    __tablename__ = 'box'

    boxid = db.Column(db.Integer, primary_key=True)
    box_contents = db.Column(db.String(400))
    no_of_points = db.Column(db.Integer,nullable=False)
    box_latitude = db.Column(db.String(100),nullable=False)
    box_longitude = db.Column(db.String(100),nullable=False)
    planted_by_username = db.Column(db.String(300),nullable=False)
    is_opened = db.Column(db.String(1),nullable=False)

    def json(self):
        return {
            'boxid': self.boxid,
            'box_contents': self.box_contents,
            'no_of_points': self.no_of_points,
            'box_latitude': self.box_latitude,
            'box_longitude': self.box_longitude,
            'planted_by_username': self.planted_by_username,
            'is_opened': self.is_opened
        }

# Obtains a list of Boxes that are Unopened to display on the map
# http://(micro_name):(port no.)/(route)

@app.route("/getBoxes")
def getBoxes():
    try:
        boxes = Box.query.filter_by(is_opened='N')
        if not boxes:
            return jsonify({
                "code": 404,
                "message": "There are no boxes on the map at all!"
            }),404
        box_array = []
        for box in boxes:
            box_array.append(box.json())

        return jsonify({
            "code": 200,
            "boxes": box_array
        }), 200

    except Exception as e:
        return jsonify({
            "code": 500,
            "message": "An error occurred while retrieving boxes: " + str(e)
        })

@app.route("/", methods=["POST"])
def create_box():
    # Format should be { username , latitude, longitude }
    plant_username = request.json.get('username')
    latitude = request.json.get('latitude')
    longitude = request.json.get('longitude')
    # randomly generate number of points and rewards (MAY NEED TO EDIT)
    no_of_points = random.randint(1,300)
    no_of_prize = random.randint(0,1)
    prize_list = ['$20 Gongcha Voucher','$5 Capitaland Voucher','$3 Li-Ho Voucher','$1 Popular Voucher','$10 GrabVoucher']
    box_prizes = []
    for i in range(0,no_of_prize):
        index = random.randint(0,len(prize_list)-1)
        box_prizes.append(prize_list[index])
    box_prizes_string = ','.join(box_prizes)
    if box_prizes_string == "":
        box_prizes_string = None
    box = Box(box_contents=box_prizes_string,no_of_points=no_of_points,box_latitude=latitude,box_longitude=longitude,planted_by_username=plant_username,is_opened = 'N')

    try:
        db.session.add(box)
        db.session.commit()
    except Exception as e:
        return jsonify(
            {
                "code": 500,
                "message": "An error occurred while creating the box. " + str(e)
            }
        ),500

    return jsonify(
        {
            "code": 201,
            "message": "Your box has been deployed successfully"
        }
    ),201

@app.route("/search")
def find_box():
    query_params = request.args
    #print(query_params)
    latitude = str(query_params['latitude'])
    longitude = str(query_params['longitude'])
    # BOX SEARCH takes in the first 5 digits of latitude and longitude in order to check if the user is approximately near the box
    box = Box.query.filter(Box.box_latitude.like(latitude[0:5] + "%"),Box.box_longitude.like(longitude[0:5] + "%"),Box.is_opened.like("N")).first()
    try:
        if box != None:
            return jsonify(
                {
                    "code": 200,
                    "result": box.json()
                }
            ),200
        else:
            return jsonify(
                {
                    "code": 404,
                    "message": "There is no box nearby."
                }
            ),404
    except Exception as e:
        return jsonify(
            {
                "code": 500,
                "message": "An error occurred while searching for a box: " + str(e)
            }
        ),500

# When opening box, change is_opened to 'Y'
@app.route('/open',methods=['PUT'])
def openBox():
    # JSON TO PASS THROUGH { boxid }
    try:
        boxid = request.json.get('boxid')
        box_details = Box.query.filter_by(boxid=boxid).first()
        box_details.is_opened = 'Y'
        db.session.commit()
        return jsonify({
            "code": 200,
            "message": "Box status updated to open successfully."
        }),200
    except Exception as e:
        return jsonify({
            "code":500,
            "message":"An error occurred while updating box status: " + str(e)
        }),500

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


import unittest

def solve(arr, k):
	if arr is None or len(arr) == 1:
		return arr
	if k < 1:
		return None
	processed = []
	start, mid, end = None, None, None
	for idx, elm in enumerate(arr):
		if elm.isalpha() and mid is None:
			mid = idx
			if elm.isalpha() and start is None:
				start = idx
		if elm == ' ':
			end = idx
			mid = None
		if idx - start == k:
			processed.append(arr[start:end])
			start = mid
			mid = None
			end = None
	if end is None:
		processed.append(arr[start:len(arr)])
	return processed

class Test(unittest.TestCase):

	data = [(('the quick brown fox jumps over the lazy dog', 10), ['the quick', 'brown fox', 'jumps over', 'the lazy', 'dog'])]

	def test(self):
		for case, expected in self.data:
			actual = solve(case[0], case[1])
			self.assertEquals(actual, expected)

if __name__ == '__main__':
#	unittest.main()
	data = [(('the quick brown fox jumps over the lazy dog', 10), ['the quick', 'brown fox', 'jumps over', 'the lazy', 'dog'])]
	print(solve(data[0][0][0], data[0][0][1]))