if filter_values.get('period'):
  period_values = filter_values['period']
  if len(period_values.split(NEWLINE))>1:
    msgprint("You can view report only for one period. Please select only one value in period.")
    raise Exception
  else:
    period = period_values.split(NEWLINE)[0]

if filter_values.get('based_on'):
  based_on = filter_values['based_on']
  if len(based_on.split(NEWLINE)) > 1:
    msgprint("You can view report based on only one criteria. Please select only one value in Based On.")
    raise Exception
  else:
    based_on = based_on.split(NEWLINE)[0]

if not filter_values.get('fiscal_year'):
  msgprint("Please Select Fiscal Year")
  raise Exception
elif not filter_values.get('period'):
  msgprint("Please Select Period")
  raise Exception
elif not filter_values.get('based_on'):
  msgprint("Please Select the Criteria on which you want your report to be based")
  raise Exception

fiscal_year = filter_values.get('fiscal_year')

# get fiscal year start date and start month
# ---------------------------------------------------------
year_start_date = sql("select year_start_date,MONTH(year_start_date) from `tabFiscal Year` where name = %s",fiscal_year)
start_date = year_start_date and year_start_date[0][0] or ''
start_month = year_start_date and year_start_date[0][1] or ''
month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

# Add columns based on period
# --------------------------------
columns = []
# ================ Annual ======================
if period == 'Annual':
  columns.append(['Target','Currency','120px',''])
  columns.append(['Actual','Currency','120px',''])

# =========== Half Yearly ======================
elif period == 'Half Yearly':
  columns.append(['Target (H1)','Currency','120px','']) # first half
  columns.append(['Actual (H1)','Currency','120px','']) # first half
  if start_month == 1:  # this is case when fiscal year starts with JAN
    columns.append(['Target (H2)','Currency','120px',''])
    columns.append(['Actual (H2)','Currency','120px',''])
  else:  #this is case when fiscal year starts with other than JAN
    columns.append(['Target (H2)','Currency','120px',''])
    columns.append(['Actual (H2)','Currency','120px',''])

# ================ Quarterly ===================
elif period == 'Quarterly':
  length_1 = (len(month_name) - start_month + 1) / 3  #this gives the total no. of times we need to iterate for quarter
  val = length_1 % 4
  q_no = 1
  for i in range(length_1):
    value = 3*i + val
    columns.append(['Target (Q'+cstr(q_no)+')','Currency','120px',''])
    columns.append(['Actual (Q'+cstr(q_no)+')','Currency','120px',''])
    q_no += 1
  length_2 = (start_month - 1) / 3 #this gives the total no. of times we need to iterate for quarter (this is required only if fiscal year starts from april)
  for i in range(length_2):
    columns.append(['Target (Q'+cstr(q_no)+')','Currency','120px',''])
    columns.append(['Actual (Q'+cstr(q_no)+')','Currency','120px',''])
    q_no += 1;

# =============== Monthly ======================
elif period == 'Monthly':
  for i in range(start_month-1,len(month_name)):
    columns.append(['Target ('+month_name[i]+')','Currency','120px',''])
    columns.append(['Actual ('+month_name[i]+')','Currency','120px',''])

  for i  in range(start_month-1):
    columns.append(['Target('+month_name[i]+')','Currency','120px',''])
    columns.append(['Actual ('+month_name[i]+')','Currency','120px',''])

for c in columns:
  colnames.append(c[0])
  coltypes.append(c[1])
  colwidths.append(c[2])
  coloptions.append(c[3])
  col_idx[c[0]] = len(colnames)-1

condition = ' docstatus = 1 and fiscal_year = "'+fiscal_year+'"'

for r in res:
  query = ''

  # ================= Annual Report ===============
  if period == 'Annual':

    target = sql("select sum(target_amount) from `tabTarget Detail` where parent = %s and parenttype= 'Territory' and fiscal_year = %s ",(r[col_idx['ID']],fiscal_year))
    target = target and flt(target[0][0]) or 0
    r.append(target)

    so = sql("select sum(net_total) from `tab%s` where territory = '%s' and %s" % (based_on, r[col_idx['ID']],condition))
    so = so and flt(so[0][0]) or 0
    r.append(so)

  # ================= Half Yearly Report ===============
  elif period == 'Half Yearly':
    target = sql("select sum(target_amount) from `tabTarget Detail` where parent = %s and parenttype= 'Territory' and fiscal_year = %s",(r[col_idx['ID']],fiscal_year))
    target = target and flt(flt(target[0][0])/2) or 0
    r.append(target)

    query += ' MONTH(transaction_date) BETWEEN '+cstr(start_month)+' and '+cstr(start_month+5)
    so = sql("select sum(net_total) from `tab%s` where territory = '%s'  and %s and %s" % (based_on, r[col_idx['ID']],condition,query))
    so = so and flt(so[0][0]) or 0
    r.append(so)

    r.append(target)

    query =''
    query += 'MONTH(transaction_date) NOT BETWEEN '+cstr(start_month)+' and '+cstr(start_month+5)
    so = sql("select sum(net_total) from `tab%s` where territory = '%s'  and %s and %s" % (based_on, r[col_idx['ID']],condition,query))
    so = so and flt(so[0][0]) or 0
    r.append(so)
    query = ''

  # =============== Quarterly Report ==============
  elif period == 'Quarterly':
    query = ''
    length_1 = (len(month_name) - start_month + 1) / 3; #this gives the total no. of times we need to iterate for quarter
    val = length_1 % 4;
    for i in range(length_1):
      value = 3*i + val;
      query +='SUM(CASE WHEN MONTH(transaction_date) BETWEEN '+cstr(value+1)+' AND '+cstr(value+3)+' THEN net_total ELSE NULL END),'
    length_2 = (start_month - 1) / 3; #this gives the total no. of times we need to iterate for quarter (this is required only if fiscal year starts from april)
    for i in range(length_2):
      query += 'SUM(CASE WHEN MONTH(transaction_date) BETWEEN '+cstr(3*i+1)+' AND '+cstr(3*i+3)+' THEN net_total ELSE NULL END)';

    target = sql("select sum(target_amount) from `tabTarget Detail` where parent = %s and parenttype= 'Territory' and fiscal_year = %s",(r[col_idx['ID']],fiscal_year))
    target = target and flt(flt(target[0][0])/4) or 0

    so = sql("SELECT %s from `tab%s` where territory ='%s' and %s " %(query,based_on,r[col_idx['ID']],condition))
    i = 0
    length_l = 0
    for c in columns:
      if length_l == 0:
        r.append(target)
        length_l += 1
      else:
        so_total = so and flt(so[0][i]) or 0
        r.append(so_total)
        i +=1
        length_l = 0

  # ================ Monthly Report ===============
  elif period == 'Monthly':
    query =''
    target = sql("select sum(target_amount) from `tabTarget Detail` where parent = %s and parenttype= 'Territory' and fiscal_year = %s",(r[col_idx['ID']],fiscal_year))
    #msgprint(target)
    target = target and flt(flt(target[0][0])/12) or 0

    # for loop is required twice coz fiscal year starts from April (this will also work if fiscal year starts in January)
    for i in range(start_month-1,len(month_name)):
      query += 'SUM(CASE WHEN MONTH(transaction_date) = '+cstr(i+1)+' THEN net_total ELSE NULL END),'

    for i  in range(start_month-1):
      if i != (start_month-2):
        query += 'SUM(CASE WHEN MONTH(transaction_date) = '+cstr(i+1)+' THEN net_total ELSE NULL END),'
      else:
        query += 'SUM(CASE WHEN MONTH(transaction_date) = '+cstr(i+1)+' THEN net_total ELSE NULL END)';
    so = sql("SELECT %s from `tab%s` where territory ='%s' and %s " %(query,based_on,r[col_idx['ID']],condition))

    i = 0
    length_l = 0
    for c in columns:
      if length_l == 0:
        r.append(target)
        length_l += 1
      else:
        so_total = so and flt(so[0][i]) or 0
        r.append(so_total)
        i +=1
        length_l = 0


import sys
sys.stdin = open("4_회문2.txt","r")
1
def longest_palindrom(s):
    # 비교할 문자열의 길이를 지정 : 문자열의 제일 긴 길이부터 0까지 -1씩 줄여나감
    for i in range(len(s), 0, -1):
        # 짧아진 길이 만큼 앞뒤로 이동시키며 비교 : 줄인 길이 만큼 문자열의 시작점을 이동시키며 비교해야 한다.
        # 때문에 +1을 해준다.
        for j in range(len(s) - i + 1):
            # j는 부분 문자열의 시작 index, i는 부분 문자열의 길이
            if(s[j:j+i] == my_reverse(s[j:j+i])):
                return i
def my_max(arr):
    max1 = 0
    for i in range(len(arr)):
        if arr[i] > max1:
            max1=arr[i]
    return max1

def my_reverse(s):
    result = ""
    for i in range(len(s)-1, -1, -1):
        result += s[i]
    return result

for tc in range(10):
    tc = input()
    result = []
    answer = []
    for num in range(100):
        result += [input()]

    # 가로 회문찾기
    for i in range(100):
        a = longest_palindrom(result[i])
        answer.append(a)

    # 세로 회문 찾기
    c = []
    for i in range(100):
        b = ''
        for j in range(100):
            b += result[j][i]
        c.append(b)
    for i in range(100):
        a = longest_palindrom(c[i])
        answer.append(a)

    print(f"#{tc} {my_max(answer)}")


from dolfin import *
import random
import numpy as np

#Simulation and material parameters to be specified

# Flory-Huggins interaction parameters
chi_AB = 0.15
chi_AC = 0.15
chi_BC = 0.15
# Diffusion coefficient
D_AB = 1.e-10
D_AC = 1.e-10
D_BC = 1.e-10
# Chain length
N_A = 100
N_B = 100
N_C = 100
# Scaling choices
N_SCALE_OPTION = "N_A"
D_SCALE_OPTION = "D_AB"
# Concentration parameters
A_RAW = 0.1
B_RAW = 0.1
NOISE_MAGNITUDE = 0.03
# Time and space parameters
TIME_MAX = 200
DT = 0.1
N_CELLS = 30
DOMAIN_LENGTH = 400
theta_ch = 1.0

# Class representing the intial conditions
class InitialConditions(UserExpression):
    def __init__(self, **kwargs):
        random.seed(2 + MPI.rank(MPI.comm_world))
        super().__init__(**kwargs)

    def eval(self, values, x):
        values[0] = A_RAW + 2.0 * NOISE_MAGNITUDE * (0.5 - random.random())
        values[1] = B_RAW + 2.0 * NOISE_MAGNITUDE * (0.5 - random.random())
        values[2] = 0.0
        values[3] = 0.0
        values[4] = 0.0

    def value_shape(self):
        return (5,)

#Stuff for boundary conditions
class PeriodicBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and (near(x[0], 0.0))

    # Map RightBoundary to LeftBoundary
    def map(self, x, y):
        y[0] = x[0] - DOMAIN_LENGTH
        y[1] = x[1]

# Form compiler options
parameters["form_compiler"]["optimize"] = True
parameters["form_compiler"]["cpp_optimize"] = True

# Create mesh and build function space
mesh = RectangleMesh(
    Point(0.0, 0.0), Point(DOMAIN_LENGTH, DOMAIN_LENGTH), N_CELLS, N_CELLS
)

P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
ME = MixedElement([P1,P1,P1,P1,P1])
CH = FunctionSpace(mesh, ME, constrained_domain=PeriodicBoundary())

h_1, h_2, j_1, j_2, j_3 = TestFunctions(CH)

ch = Function(CH)
ch0 = Function(CH)

a, b, N_mu_AB, N_mu_AC, N_mu_BC = split(ch)
a0, b0, N_mu0_AB, N_mu0_AC, N_mu0_BC = split(ch0)

a = variable(a)
b = variable(b)

# Create intial conditions and interpolate
ch_init = InitialConditions(degree=1)
ch.interpolate(ch_init)
ch0.interpolate(ch_init)

N_scale_options = {"N_A": N_A, "N_B": N_B, "N_C": N_C}
N_SCALE = N_scale_options[N_SCALE_OPTION]

D_scale_options = {"D_AB": D_AB, "D_AC": D_AC, "D_BC": D_BC}
D_SCALE = D_scale_options[D_SCALE_OPTION]

kappa_AA = (2.0 / 3.0) * chi_AC
kappa_BB = (2.0 / 3.0) * chi_BC
kappa_AB = (1.0 / 3.0) * (chi_AC +  chi_BC - 1.0 * chi_AB)

N_kappa_AA = N_SCALE * kappa_AA
N_kappa_BB = N_SCALE * kappa_BB
N_kappa_AB = N_SCALE * kappa_AB

# "ordinary free energy" gradients
N_dgda = N_SCALE * (
    (1.0 / N_A) * (1.0 + ln(a)) + chi_AB * b + chi_AC * (1.0 - a - b)
)
N_dgdb = N_SCALE * (
    (1.0 / N_B) * (1.0 + ln(b)) + chi_AB * a + chi_BC * (1.0 - a - b)
)
N_dgdc = N_SCALE * (
    (1.0 / N_C) * (1.0 + ln(1.0 - a - b)) + chi_AC * a +  chi_BC * b
)

N_mu_AB_mid = (1.0 - theta_ch) * N_mu0_AB + theta_ch * N_mu_AB
N_mu_AC_mid = (1.0 - theta_ch) * N_mu0_AC + theta_ch * N_mu_AC
N_mu_BC_mid = (1.0 - theta_ch) * N_mu0_BC + theta_ch * N_mu_BC

# scale diffusivity
D_AB_ = D_AB / D_SCALE
D_AC_ = D_AC / D_SCALE
D_BC_ = D_BC / D_SCALE

dt = DT

# transport equations
F_a = (
    a * h_1
    - a0 * h_1
    + dt * a * b * D_AB_ * dot(grad(N_mu_AB_mid), grad(h_1))
    + dt * a * (1.0 - a - b) * D_AC_ * dot(grad(N_mu_AC_mid), grad(h_1))
)

F_b = (
    b * h_2
    - b0 * h_2
    - dt * a * b * D_AB_ * dot(grad(N_mu_AB_mid), grad(h_2))
    + dt * b * (1.0 - a - b) * D_BC_ * dot(grad(N_mu_BC_mid), grad(h_2))
)

# chemical potential equations
F_N_mu_AB = (
    N_mu_AB * j_1
    - N_dgda * j_1
    + N_dgdb * j_1
    - (N_kappa_AA - N_kappa_AB) * dot(grad(a), grad(j_1))
    + (N_kappa_BB - N_kappa_AB) * dot(grad(b), grad(j_1))
)

F_N_mu_AC = (
    N_mu_AC * j_2
    - N_dgda * j_2
    + N_dgdc * j_2
    - (N_kappa_AA * dot(grad(a), grad(j_2)))
    - (N_kappa_AB * dot(grad(b), grad(j_2)))
)

F_N_mu_BC = (
    N_mu_BC * j_3
    - N_dgdb * j_3
    + N_dgdc * j_3
    - N_kappa_BB * dot(grad(b), grad(j_3))
    - N_kappa_AB * dot(grad(a), grad(j_3))
)

F = (F_a + F_b + F_N_mu_AB + F_N_mu_AC + F_N_mu_BC)*dx(domain=mesh)

# Compute Jacobian
J = derivative(F,ch)

#Set up problem and solver
problem = NonlinearVariationalProblem(F,ch, J=J)
solver = NonlinearVariationalSolver(problem)
solver.parameters["newton_solver"]["linear_solver"] = "lu"
solver.parameters["newton_solver"]["relaxation_parameter"] = 0.5

# Output file
file_a = File("concentration_A.pvd", "compressed")
file_b = File("concentration_B.pvd", "compressed")

# Step in time
t = 0.0
timestep = 0

# output initial condition
file_a << (ch.split()[0], t)
file_b << (ch.split()[1], t)

while t < TIME_MAX:

    timestep += 1
    t += dt

    ch0.vector()[:] = ch.vector()
    solver.solve()

    if timestep % 100 == 0:
        file_a << (ch.split()[0], t)
        file_b << (ch.split()[1], t)


from __future__ import print_function
from __future__ import division

from itertools import chain
from collections import defaultdict
import sys
import math

__author__ = 'Sahand Saba'

def get_data():
    ratings = defaultdict(dict)
    for line in sys.stdin:
        rater, movie, rating = line.split('\t')
        rating = float(rating)
        ratings[rater][movie] = rating

    return ratings

def dot(u, v, uc, vc):
    return sum((x - uc) * (y - vc) for x, y in zip(u, v))

def pearson_similiarty(u, v):
    """Assume u and v are vectors of the same length at this point."""
    u_avg = sum(u) / len(u)
    v_avg = sum(v) / len(v)
    u_dot_v = dot(u, v, u_avg, v_avg)
    u_dot_u = dot(u, u, u_avg, u_avg)
    v_dot_v = dot(v, v, v_avg, v_avg)
    return u_dot_v / (math.sqrt(u_dot_u) * math.sqrt(v_dot_v))

def user_user_similarity(u, v, sim_func):
    u_vector = []
    v_vector = []
    for k in u:
        if k in v:
            u_vector.append(u[k])
            v_vector.append(v[k])

    return sim_func(u_vector, v_vector)

def get_rating(u, m, raters, sim, K=5):
    if u in raters[m]:
        return ratings[u][m]
    else:
        raters_to_use = sorted(raters[m], key=lambda v: sim[u, v], reverse=True)[:K]
        numerator = sum(sim[u, v] * ratings[v][m] for v in raters_to_use)
        denom = sum(sim[u, v] for v in raters_to_use)
        return numerator / denom

if __name__ == '__main__':
    ratings = get_data()
    users = ratings.keys()
    raters = defaultdict(set)
    for u, m in ratings.iteritems():
        for movie in m:
            raters[movie].add(u)

    sim_matrix = dict()
    for u in users:
        for v in users:
            sim = user_user_similarity(ratings[u],
                                       ratings[v],
                                       pearson_similiarty)
            sim_matrix[u, v] = sim

    print('Guessed ratings:')
    for u in users:
        for m, r in raters.iteritems():
            if u not in r:
                rating = get_rating(u, m, raters, sim_matrix)
                output = '{0}: {1} - {2:5.3f}'
                output = output.format(u, m, rating)
                print(output)


#!usr/bin/python2.7
# coding=utf-8

import os, re, sys, json
from bs4 import BeautifulSoup as parser
from datetime import datetime

def main(self, cookie, url, config):
	ask = raw_input('\n\033[0;92mMasukan nama \033[0;91m:\033[0;96m ')
	if ask.strip() == '':
		exit("\n\033[0;91mTidak boleh kosong.\033[0m")
	try:
		max = int(raw_input('\033[0;93mBerapa banyak? \033[0;97m{\033[0;94mMax \033[0;91m: \033[0;96m1000\033[0;97m} : '))
	except ValueError:
		exit("\n\033[0;91mTolol.\033[0m")
	if max == 0:
		exit("\n\033[0;91mTidak boleh kosong.\033[0m")

	url_search = url+'/search/people/?q='+ask

	statusStop = False
	output = 'dump/'+ask.replace(' ', '_')+'.json'.strip()
	id = []
	print('')

	while True:
		try:
			response = config.httpRequest(url_search, cookie).encode('utf-8')
			html = parser(response, 'html.parser')
			find = html.find_all('a')
			for i in find:
				name = i.find('div')
				if '+' in str(name) or name == None:
					continue
				else:
					full_name = str(name.text.encode('utf-8'))
					if 'profile.php?id=' in str(i):
						uid = re.findall(r'\?id=(.*?)&', str(i))
					else:
						uid = re.findall('/(.*?)\?refid=', str(i))
					if len(uid) == 1:
						id.append({'uid': uid[0], 'name': full_name})
					sys.stdout.write("\r\033[0;92m->\033[0;96m %s                                        \r\n\033[0;97m{\033[0;94m%s\033[0m} {\033[0;93m%s\033[0m} Jangan keluar selagi ngeDump id!."%(
						full_name, datetime.now().strftime('%H:%M:%S'), len(id)
					)); sys.stdout.flush()
					if len(id) == max or len(id) > max:
						statusStop = True
						break
			if statusStop == False:
				if 'Lihat Hasil Selanjutnya' in str(html):
					url_search = html.find('a', string='Lihat Hasil Selanjutnya')['href']
				else: break
			else: break
		except KeyboardInterrupt:
			print('\n\n\033[0;91mKeyInterrupt, berhenti!!\033[0m')
			break
	try:
		for filename in os.listdir('dump'):
			os.remove('dump/'+filename)
	except: pass
	print('\n\n\033[0;92mOutput \033[0;91m:\033[0;96m '+output)
	save = open(output, 'w')
	save.write(json.dumps(id))
	save.close()


# 1. Define an empty variable named "my_list".

my_list = None

# 2. Define an empty list with the variable name "your_list".

your_list = []

# 3. Convert "my_list" from NoneType to a list.
#       Then using a loop fill "my_list" with numbers 9 - 0.

my_list = []

loop = 9
while loop > -1:
    my_list.append(loop)
    loop = loop -1

print(my_list)

# 4. In the simplest way possible, sort "my_list" from least to greatest.

my_list.sort()
print(my_list)

# 5. Using "my_list" store only the even numbers into "your_list".

for shitwhatinthelist in my_list:
    # 3 / 2 = 1.5 | 0.5 == 0
    if shitwhatinthelist % 2 == 0:
        your_list.append(shitwhatinthelist)

print(your_list)


from mtranslate import translate
from bidi.algorithm import get_display
from hazm import Normalizer
import persian ,arabic_reshaper ,re ,os

def main():
    language = input ('Choose your translation type : \n \
                    english to persian --> 1 \n \
                    persian to english --> 2 \n \
                    Your choice : '
                    )
    input_list=['1','2']
    while language not in input_list :
        language = input ('Choose your tr type : \n \
                        english to persian --> 1 \n \
                        persian to english --> 2 \n \
                        Your choice : '
                        )
    if language == "1" :
        print ("English to Persian Translator .")
    else :
        print ("Persian to English Translator .")
    print ("For exit type 'exit0' .")
    file_needed = input (
                        "Do you want to write translations in a file [yes/any] : "
                        )
    while True :
        if language == "1" :
            sentenses = input ("Sentenses : ")
            if sentenses.lower() == "exit0" :
                exit(0)
            translation = translate (sentenses ,'fa' , 'en')
            translation = re.sub(r"(\.)\s+" , "، " ,translation)
            norm = Normalizer()
            translation = norm.normalize(translation)
            if file_needed.lower() == "yes" :
                txt = os.getcwd() + "tr-fa.txt"
                if not os.path.exists(txt) :
                    os.mknod(txt)
                with open ("tr-fa.txt" , "a") as file :
                    text = translation + "\n"
                    file.write(text)
            reshaped_text = arabic_reshaper.reshape(translation)
            bidi_alg = get_display(reshaped_text)
            bidi_alg = arabic_reshaper.reshape(bidi_alg)
            bidi_alg = persian.convert_en_numbers(bidi_alg)
            bidi_alg = persian.convert_ar_characters(bidi_alg)
            bidi_alg = bidi_alg.split("،")
            bidi_alg.reverse()
            for i in range(len(bidi_alg)) :
                print (bidi_alg[i])
            print (30 * '-' , "\n")
        else :
            sentenses = input ("Sentenses : ")
            if sentenses.lower() == "exit0" :
                exit(0)
            translation = translate (sentenses ,'en' , 'fa')
            if file_needed.lower() == "yes" :
                txt = os.getcwd() + "tr-en.txt"
                if not os.path.exists(txt) :
                    os.mknod(txt)
            with open ("tr-en.txt" , "a") as file :
                text = translation + "\n"
                file.write(text)
            print(translation)
            print (30 * '-' , "\n")

if __name__ == "__main__":
    main()


import sys
import uuid
sys.path.append('./')
sys.path.append('../')
import redis
from CiriGateway import CiriGatewayServer
conn=redis.Redis()
guid=''
def SendMessageToGateway(myMessage):
    global guid
    guid=uuid.uuid1().__str__()
    cfg=CiriGatewayServer.GetAllConfig()
    serverListenTo=cfg['listenerQueue']
    consumedMessage=conn.publish(channel=serverListenTo,message=myMessage+":"+guid)
    return myMessage,guid
def SubscribeMessageFromServer(callback):
    cfg=CiriGatewayServer.GetAllConfig()
    serverPublishTo=cfg['senderTopic']
    sub=conn.pubsub()
    sub.subscribe(serverPublishTo)
    while(True):
        for message in sub.listen():
            if(message['type']=='message'):
                myMessage=message['data']
                import CiriUtility.Utility
                messageGuid=CiriUtility.Utility.GetMessageGUID(myMessage.decode('utf-8'))
                global guid
                if(messageGuid==guid and callback!=None):
                    callback(CiriUtility.Utility.GetMessageContent(message['data'].decode('utf-8')),guid)
                    #print(CiriUtility.Utility.GetMessageContent(message['data'].decode('utf-8')))


from modules.module_base import ModuleBase
import requests
from lxml import html
from urllib.parse import urljoin

class Module9GAG(ModuleBase):
    def __init__(self, bot):
        ModuleBase.__init__(self, bot)
        self.name = "Module9GAG"

    def getImage(self, chat):
        try:
            response = requests.get("http://9gag.com/random/")
            parsed_body = html.fromstring(response.text)

            image = parsed_body.xpath('//img[@class="badge-item-img"]/@src')
            imageAlt = parsed_body.xpath('//img[@class="badge-item-img"]/@alt')

            # Convert any relative urls to absolute urls
            image = urljoin(response.url, image[0])

            self.bot.sendPhotoUrl(chat, image, imageAlt)
        except Exception as e:
            self.logger.exception("9gag error", exc_info=True)
            self.bot.sendMessage("9gag error", chat)

    def notify_command(self, message_id, from_attr, date, chat, commandName, commandStr):
        if commandName == "9gag":
            self.getImage(chat["id"])

    def get_commands(self):
        return [
            ("9gag", "Get 9gag random image"),
        ]


from scrapy import Spider
from scrapy.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.linkextractors import LinkExtractor
import scrapy
from scrapy.spidermiddlewares.httperror import HttpError
from twisted.internet.error import DNSLookupError
from twisted.internet.error import TimeoutError, TCPTimedOutError
from UW_Madison.items import UwMadisonItem

# CrawlSpider
class Madison_courses( scrapy.Spider ):

    name = 'uw_madison_final'

    allowed_domains = ['wisc.edu']

    start_urls = [

            "http://guide.wisc.edu/courses/acct_i_s/",
            #"http://guide.wisc.edu/courses/zoology/",

    ]

    def  start_requests( self ):

        for u in self.start_urls:

            yield scrapy.Request( u, callback = self.parse_httpbin,
                    errback = self.errback_httpbin,
                    dont_filter = True )

    def parse_httpbin( self, response ):

        self.logger.info("Got successful response {}".format(response.url) )
        courses = response.css('span.courseblockcode::text').extract()
        #prerequisites  = response.css('p.courseblockextra.noindent.clearfix > span.cbextra-data > .bubblelink::text').extract()
        #prerequisites = response.xpath('//div[@class="cb-extras"]/p/span[@class="cbextra-data"]/a/@title').extract()

        prerequisites = response.xpath('//div[@class="cb-extras"]/p/span[@class="cbextra-data"]/a[1]/@title[1]').extract()

        title = response.css('div.sc_sccoursedescs > div.courseblock > p.courseblocktitle > strong::text').extract()
        unit = response.css('.courseblockcredits::text').extract()
        description =  response.css('.courseblockdesc::text').extract()

        final_courses = []
        final_prerequisites = []
        final_title = []
        final_unit = []
        final_description = []

        for course_set in courses:

            if course_set == '\n' or course_set == ' ':
                continue
            final_courses.append(course_set)

        for prerequisites_set in prerequisites:

            if prerequisites_set == '\n' or prerequisites_set == ' ' :
                continue
            final_prerequisites.append(prerequisites_set)

        for title_set in title:

            if title_set == '\n' or title_set == ' ':
                continue
            final_title.append(title_set)

        for unit_set in unit:

            if unit_set == '\n' or unit_set == ' ':
                continue
            final_unit.append(unit_set)

        for description_set in description:

            if description_set == '\n' or description_set == ' ':
                continue
            final_description.append(description_set)

        course_size = len(final_courses)
        items = []
        track_index = 0

        print("Course_Size:", course_size )
        print("prerequisites_size", len(final_prerequisites))
        print("title_size", len(final_title))
        print("Unit_size", len(final_unit))
        print("description", len(final_description))

        while track_index < course_size:

            print("Track_index", track_index )

            item = UwMadisonItem()

            #courses = response.css('span.courseblockcode::text').extract()[ track_index ].strip()
            #title = response.css('div.sc_sccoursedescs > div.courseblock > p.courseblocktitle > strong::text').extract()[ track_index ].strip()
            #unit = response.css('.courseblockcredits::text').extract()[ track_index ].strip()
            #description =  response.css('.courseblockdesc::text').extract()[ track_index ].strip()

            item['course'] = final_courses[ track_index ]
            item['title'] = final_title[ track_index ]
            item['unit'] = final_unit[ track_index ]
            item['description'] = final_description[ track_index ]

            try:

                item['prerequisites'] = final_prerequisites[ track_index ]

            except IndexError:

                item['prerequisites'] = 'None'

            items.append(item)
            track_index += 1

        return items

    def errback_httpbin( self, failure):

        # log all failures
        self.logger.error(repr(failure))

        # in case you want to do something special for some errors,
        # you may need the failure's type:

        if failure.check(HttpError):
            # These exception come from HttpError spider middleware
            # you can get the non-200 response
            response = failure.value.response
            self.logger.error("HttpError on %s", response.url )

        elif failure.check(DNSLookupError):
            # This is the original request
            request = failure.request
            self.logger.error('DNSLookupError on %s', request.url )

        elif failure.check(TimeoutError, TCPTimeOutError ):

            request = failure.request
            self.logger.error('TimeoutError on %s', request.url)


#
# Pyserini: Reproducible IR research with sparse and dense representations
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
from pyserini.search.lucene.irst import LuceneIrstSearcher
from pyserini.search.lucene.ltr._base import SpacyTextParser
from typing import List
from transformers import AutoTokenizer
from pyserini.analysis import Analyzer, get_lucene_analyzer
from tqdm import tqdm

def normalize(scores: List[float]):
    low = min(scores)
    high = max(scores)
    width = high - low
    if width != 0:
        return [(s-low)/width for s in scores]
    else:
        return scores

def query_loader(data):
    queries = {}
    nlp = SpacyTextParser('en_core_web_sm', keep_only_alpha_num=True, lower_case=True)
    analyzer = Analyzer(get_lucene_analyzer())
    bert_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
    inp_file = open(data)
    ln = 0
    for line in tqdm(inp_file):
        ln += 1
        line = line.strip()
        if not line:
            continue
        fields = line.split('\t')
        if len(fields) != 2:
            print('Misformated line %d ignoring:' % ln)
            print(line.replace('\t', '<field delimiter>'))
            continue
        did, query = fields
        query_lemmas, query_unlemm = nlp.proc_text(query)
        analyzed = analyzer.analyze(query)
        for token in analyzed:
            if ' ' in token:
                print(analyzed)
        query_toks = query_lemmas.split()
        if len(query_toks) >= 0:
            query = {"raw" : query,
                "text": query_lemmas,
                "text_unlemm": query_unlemm,
                "analyzed": ' '.join(analyzed),
                "text_bert_tok": ' '.join(bert_tokenizer.tokenize(query.lower()))}
            queries[did] = query

        if ln % 10000 == 0:
            print('Processed %d queries' % ln)

    print('Processed %d queries' % ln)
    return queries

def baseline_loader(base_path: str):
    result_dic = {}
    with open(base_path, 'r') as f:
        for line in f:
            tokens = line.strip().split()
            t = tokens[0]
            doc_id = tokens[2]
            score = float(tokens[-2])
            if t in result_dic.keys():
                result_dic[t][0].append(doc_id)
                result_dic[t][1].append(score)
            else:
                result_dic[t] = [[doc_id], [score]]

    return result_dic

def sort_dual_list(pred: List[float], docs: List[str]):
    zipped_lists = zip(pred, docs)
    sorted_pairs = sorted(zipped_lists)

    tuples = zip(*sorted_pairs)
    pred, docs = [list(tuple) for tuple in tuples]

    pred.reverse()
    docs.reverse()
    return pred, docs

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='use ibm model 1 feature to rerank the base run file')
    parser.add_argument('--tag', type=str, default="ibm",
                        metavar="tag_name", help='tag name for resulting Qrun')
    parser.add_argument('--base-path', type=str, required=False,
                        metavar="path_to_base_run", help='path to base run')
    parser.add_argument('--tran-path', type=str, default="../ibm/ibm_model/text_bert_tok_raw",
                        metavar="directory_path", help='directory path to source.vcb target.vcb and Transtable bin file')
    parser.add_argument('--topics', type=str, help='path to query topics', required=True)
    parser.add_argument('--index', type=str, default="../ibm/index-msmarco-passage-ltr-20210519-e25e33f",
                        metavar="path_to_lucene_index", help='path to lucene index folder')
    parser.add_argument('--output', type=str, default="./ibm/runs/result-colbert-test-alpha0.3.txt",
                        metavar="path_to_reranked_run", help='the path to store reranked run file')
    parser.add_argument('--field-name', type=str, default="text_bert_tok",
                        metavar="type of field", help='type of field used for training')
    parser.add_argument('--alpha', type=float, default="0.3",
                        metavar="type of field", help='interpolation weight')
    parser.add_argument('--num-threads', type=int, default="12",
                        metavar="num_of_threads", help='number of threads to use')
    parser.add_argument('--max-sim', default=False, action="store_true",
                        help='whether we use max sim operator or avg instead')
    parser.add_argument('--hits', type=int, metavar='number of hits generated in runfile',
                        required=False, default=1000, help="Number of hits.")
    args = parser.parse_args()

    print('Using max sim operator or not:', args.max_sim)

    f = open(args.output, 'w')

    reranker = LuceneIrstSearcher(
        args.tran_path, args.index, args.field_name)
    queries = query_loader(args.topics)
    i = 0
    for topic in queries.keys():
        if i % 100 == 0:
            print(f'Reranking {i}')
        query_text_field = queries[topic][args.field_name]
        query_text = queries[topic]['raw']
        if args.base_path:
            baseline_dic = baseline_loader(args.base_path)
            docids, rank_scores, base_scores = reranker.rerank(
                query_text, query_text_field, baseline_dic[topic], args.max_sim)
        else:
            docids, rank_scores, base_scores = reranker.search(
                query_text, query_text_field, args.hits, args.max_sim)
        ibm_scores = normalize([p for p in rank_scores])
        base_scores = normalize([p for p in base_scores])

        interpolated_scores = [a * args.alpha + b * (1-args.alpha) for a, b in zip(base_scores, ibm_scores)]

        preds, docs = sort_dual_list(interpolated_scores, docids)
        i = i+1
        for index, (score, doc_id) in enumerate(zip(preds, docs)):
            rank = index + 1
            f.write(f'{topic} Q0 {doc_id} {rank} {score} {args.tag}\n')
    f.close()


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/bacpypes/task.py
# Compiled at: 2018-06-20 21:27:44
"""
Task
"""
import sys
from time import time as _time
from heapq import heapify, heappush, heappop
import itertools
from .singleton import SingletonLogging
from .debugging import DebugContents, Logging, ModuleLogger, bacpypes_debugging
_debug = 0
_log = ModuleLogger(globals())
_task_manager = None
_unscheduled_tasks = []
if sys.platform in ('linux2', 'darwin'):
    from .event import WaitableEvent

    class _Trigger(WaitableEvent, Logging):

        def handle_read(self):
            if _debug:
                _Trigger._debug('handle_read')
            data = self.recv(1)
            if _debug:
                _Trigger._debug('    - data: %r', data)

else:
    _Trigger = None

class _Task(DebugContents, Logging):
    _debug_contents = ('taskTime', 'isScheduled')

    def __init__(self):
        self.taskTime = None
        self.isScheduled = False
        return

    def install_task(self, when=None, delta=None):
        global _task_manager
        global _unscheduled_tasks
        if when is None and delta is not None:
            if not _task_manager:
                raise RuntimeError('no task manager')
            when = _task_manager.get_time() + delta
        if when is None:
            when = self.taskTime
        if when is None:
            raise RuntimeError("schedule missing, use zero for 'now'")
        self.taskTime = when
        if not _task_manager:
            _unscheduled_tasks.append(self)
        else:
            _task_manager.install_task(self)
        return

    def process_task(self):
        raise RuntimeError('process_task must be overridden')

    def suspend_task(self):
        if not _task_manager:
            _unscheduled_tasks.remove(self)
        else:
            _task_manager.suspend_task(self)

    def resume_task(self):
        _task_manager.resume_task(self)

    def __lt__(self, other):
        return id(self) < id(other)

class OneShotTask(_Task):

    def __init__(self, when=None):
        _Task.__init__(self)
        self.taskTime = when

class OneShotDeleteTask(_Task):

    def __init__(self, when=None):
        _Task.__init__(self)
        self.taskTime = when

@bacpypes_debugging
def OneShotFunction(fn, *args, **kwargs):

    class OneShotFunctionTask(OneShotDeleteTask):

        def process_task(self):
            OneShotFunction._debug('process_task %r %s %s', fn, repr(args), repr(kwargs))
            fn(*args, **kwargs)

    task = OneShotFunctionTask()
    if not _task_manager:
        _unscheduled_tasks.append(task)
    else:
        task.install_task(delta=0)
    return task

def FunctionTask(fn, *args, **kwargs):
    _log.debug('FunctionTask %r %r %r', fn, args, kwargs)

    class _FunctionTask(OneShotDeleteTask):

        def process_task(self):
            _log.debug('process_task (%r %r %r)', fn, args, kwargs)
            fn(*args, **kwargs)

    task = _FunctionTask()
    _log.debug('    - task: %r', task)
    return task

@bacpypes_debugging
class RecurringTask(_Task):
    _debug_contents = ('taskInterval', 'taskIntervalOffset')

    def __init__(self, interval=None, offset=None):
        if _debug:
            RecurringTask._debug('__init__ interval=%r offset=%r', interval, offset)
        _Task.__init__(self)
        self.taskInterval = interval
        self.taskIntervalOffset = offset

    def install_task(self, interval=None, offset=None):
        if _debug:
            RecurringTask._debug('install_task interval=%r offset=%r', interval, offset)
        if interval is not None:
            self.taskInterval = interval
        if offset is not None:
            self.taskIntervalOffset = offset
        if self.taskInterval is None:
            raise RuntimeError('interval unset, use ctor or install_task parameter')
        if self.taskInterval <= 0.0:
            raise RuntimeError('interval must be greater than zero')
        if not _task_manager:
            if _debug:
                RecurringTask._debug('    - no task manager')
            _unscheduled_tasks.append(self)
        else:
            now = _task_manager.get_time() + 1e-06
            interval = self.taskInterval / 1000.0
            if self.taskIntervalOffset:
                offset = self.taskIntervalOffset / 1000.0
            else:
                offset = 0.0
            if _debug:
                RecurringTask._debug('    - now, interval, offset: %r, %r, %r', now, interval, offset)
            self.taskTime = now - offset + interval - (now - offset) % interval + offset
            if _debug:
                RecurringTask._debug('    - task time: %r', self.taskTime)
            _task_manager.install_task(self)
        return

@bacpypes_debugging
def RecurringFunctionTask(interval, fn, *args, **kwargs):
    if _debug:
        RecurringFunctionTask._debug('RecurringFunctionTask %r %r %r', fn, args, kwargs)

    class _RecurringFunctionTask(RecurringTask):

        def __init__(self, interval):
            RecurringTask.__init__(self, interval)

        def process_task(self):
            if _debug:
                RecurringFunctionTask._debug('process_task %r %r %r', fn, args, kwargs)
            fn(*args, **kwargs)

    task = _RecurringFunctionTask(interval)
    if _debug:
        RecurringFunctionTask._debug('    - task: %r', task)
    return task

@bacpypes_debugging
def recurring_function(interval, offset=None):

    def recurring_function_decorator(fn):

        class _RecurringFunctionTask(RecurringTask):

            def process_task(self):
                if _debug:
                    recurring_function._debug('process_task %r', fn)
                fn()

            def __call__(self, *args, **kwargs):
                fn(*args, **kwargs)

        task = _RecurringFunctionTask(interval, offset)
        task.install_task()
        return task

    return recurring_function_decorator

class TaskManager(SingletonLogging):

    def __init__(self):
        global _task_manager
        if _debug:
            TaskManager._debug('__init__')
        self.tasks = []
        if _Trigger:
            self.trigger = _Trigger()
        else:
            self.trigger = None
        _task_manager = self
        self.counter = itertools.count()
        if _unscheduled_tasks:
            for task in _unscheduled_tasks:
                task.install_task()

        return

    def get_time(self):
        if _debug:
            TaskManager._debug('get_time')
        return _time()

    def install_task(self, task):
        if _debug:
            TaskManager._debug('install_task %r @ %r', task, task.taskTime)
        if task.taskTime is None:
            raise RuntimeError('task time is None')
        if task.isScheduled:
            self.suspend_task(task)
        heappush(self.tasks, (task.taskTime, next(self.counter), task))
        if _debug:
            TaskManager._debug('    - tasks: %r', self.tasks)
        task.isScheduled = True
        if self.trigger:
            self.trigger.set()
        return

    def suspend_task(self, task):
        if _debug:
            TaskManager._debug('suspend_task %r', task)
        for i, (when, n, curtask) in enumerate(self.tasks):
            if task is curtask:
                if _debug:
                    TaskManager._debug('    - task found')
                del self.tasks[i]
                task.isScheduled = False
                heapify(self.tasks)
                break
        else:
            if _debug:
                TaskManager._debug('    - task not found')
            if self.trigger:
                self.trigger.set()

    def resume_task(self, task):
        if _debug:
            TaskManager._debug('resume_task %r', task)
        self.install_task(task)

    def get_next_task(self):
        """get the next task if there's one that should be processed,
        and return how long it will be until the next one should be
        processed."""
        if _debug:
            TaskManager._debug('get_next_task')
        now = _time()
        task = None
        delta = None
        if self.tasks:
            when, n, nxttask = self.tasks[0]
            if when <= now:
                heappop(self.tasks)
                task = nxttask
                task.isScheduled = False
                if self.tasks:
                    when, n, nxttask = self.tasks[0]
                    delta = max(when - now, 0.0)
            else:
                delta = when - now
        return (task, delta)

    def process_task(self, task):
        if _debug:
            TaskManager._debug('process_task %r', task)
        task.process_task()
        if isinstance(task, RecurringTask):
            task.install_task()
        elif isinstance(task, OneShotDeleteTask):
            del task


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import requests
from bs4 import BeautifulSoup

def get_soup (url):
    page = requests.get(url).content.decode()
    soup = BeautifulSoup(page,'html.parser')
    return  soup

def get_nexturl (soup):
    pages = soup.find('div', {'class': 'page'}).find_all('a')
    url_exist = pages[-1].text
    if url_exist == '下一页':
        url = pages[-1].get('href')
        print('加载下一页：'+ url)

        return url
    else:
        return None

url = 'http://www.qizuang.com/city/'
soup = get_soup(url)
A_url = soup.find('div',{'class':'p2 clearfix'}).find_all('div',{'class':'con1 acti'})[4].find_all('div',{'class':'span1'})[0].find_all('li')
for url in A_url :

    url = url.find('a').get('href')
    url = url + '/company/?p=1'
            #print(url)
            #每个城市的公司
    try:
        while url:
             print(url)
             soup = get_soup(url)  # 已经获取到每个城市某一页的soup
             city_name = soup.find('div',{'class':'city'}).find('strong').text #获取城市名称
             #print(city_name)
             companies = soup.find('ul', {'class': 'gongslist'}).find_all('li')

             for company in companies:
                 u = company.find('a', {'class': 'company-link'}).get('href')
                 compage = get_soup(u)

                 name = compage.find('h1',{'class':'com-tit'}).text#获取公司名称

                 infos = compage.find('ul',{'class':'c-tab-send'})#获取公司信息：地址，电话1，电话2
                 att = infos.find('li', {'class': 'att'}).text
                 tel = infos.find('li',{'class':'tel'}).text
                 mob = infos.find('li',{'class':'mob'}).text
                 list = [name,att,mob,tel,]
                 print(list)

                 file_name = '/Users/zhongyi/Desktop/d/'+ city_name + '.csv'
                 f = open(file_name, 'a+', encoding='utf-8')
                 for info in list:
                     f.write(info)
                     f.write(',')
                 f.write('\n')
                 f.close()

             url = get_nexturl(soup)  # 获取下一页的url

    except AttributeError  :
        pass


import pandas

csvCredits = pandas.read_csv("credits.csv", low_memory= False)
csvFilms = pandas.read_csv("movies_metadata.csv", low_memory= False)

mergedCsv = pandas.merge(left=csvFilms, right=csvCredits, how='left', on='id')

for index in range(0, len(mergedCsv["cast"])):
    if pandas.isnull(mergedCsv["cast"][index]):
        mergedCsv["cast"][index] = "[]"

for index in range(0, len(mergedCsv["crew"])):
    if pandas.isnull(mergedCsv["crew"][index]):
        mergedCsv["crew"][index] = "[]"

mergedCsv.to_csv("movies_metadata.csv", index=False)


class Circle:

    def __init__(self, sequence, max_times):
        self.sequence = sequence
        self.max_time = max_times
        self.count = 0
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.count < self.max_time:
            result = self.sequence[self.index]
            if self.index >= len(self.sequence)-1:
                self.index = 0
            else:
                self.index += 1
            self.count += 1
            return result
        else:
            raise StopIteration


import matplotlib.pyplot as plt
import os
import pickle
import datetime
import pandas
import numpy as np

STAGE_AI = {-1: -1, 0: 97, 1: 85, 2: 70, 3: 50, 4: 30, 5: 20, 6: 10, 7: 0}

class ReportAI(object):
    def __init__(self, path_to_report):
        wb = pandas.read_csv(path_to_report, delimiter=';').dropna(axis=0, how='all')

        self.doctor = wb["Doctor"][0] if not pandas.isnull(wb["Doctor"][0]) else "Unknown"
        self.diagnosis = wb["Diagnosis"][0] if not pandas.isnull(wb["Diagnosis"][0]) else "Unknown"
        self.electrodes_position = wb["Position"][0] if not pandas.isnull(wb["Position"][0]) else "Unknown"
        self.medicines = medicineCounter(wb["Medicine"])
        self.comment = wb["Overall comment"][0] if not pandas.isnull(wb["Overall comment"][0]) else "No comments"

        time_stage = timeFormat(wb[["Time", "Stage"]], os.path.basename(path_to_report))
        self.time = time_stage[0] + np.arange(len(time_stage) - 1)
        self.stages = time_stage[1:]  # The first element in time_stage is the first second of a record
        self.date = time_stage[0]  # The first element in time_stage is the first second of a record

        record_number = os.path.basename(path_to_report).split('_')
        self.name = [i for i in record_number if i[0:3] == "rec"][0]

    def fragmentAI(self, start_second, end_second):
        if self.time[0] > start_second or end_second > self.time[-1]:
            return None
        start = start_second - self.time[0]
        time_delta = end_second - start_second
        average_ai = 0
        for i in range(time_delta):
            if self.stages[start + i] == -1:  # if found at artifact - this fragment is artifacted
                return -1  # return artifacted ai
            average_ai += self.stages[start + i]
        average_ai /= time_delta
        return round(average_ai, 1)

    def saveToPickle(self, path_to_save="Z:\\Tetervak\\Reports\\reports 2.0\\reports_pickle"):
        pickle_output = open(os.path.join(path_to_save, "{}.{}".format(self.name, '.pickle')), 'wb')
        pickle.dump(self, pickle_output)
        pickle_output.close()

class FragmentList(object):
    def __init__(self, path_to_fragments):
        self.fragments = []
        with open(path_to_fragments, 'r') as file:
            for line in file:
                self.fragments.append(line.replace('\n', '').replace(';', ''))

        self.time_step = 300
        if '(' in self.fragments[0]:
            self.time_step = self.fragments[0].split('(')[-1].split(')')[0].split('-')  # xxx(90-120)xxx
            self.time_step = int(self.time_step[-1]) - int(self.time_step[0])  # 120 - 90 = 30

    def fragments2pickle(self, path_to_save="Z:\\Tetervak\\Reports\\reports 2.0\\fragments_pickle"):
        pickle_output = open("{}\\{}_second_fragments.pickle".format(path_to_save, self.time_step), 'wb')
        pickle.dump(self, pickle_output)
        pickle_output.close()

    def rec_list(self, record_name):  # Returns start seconds of all fragments for the record
        return sorted([matName2Time(f) for f in self.fragments if f[:6] == record_name and
                       len(f.split('_')) == 3])  # return list for the record without records 000_1, 000_2

class BaredReport(object):
    def __init__(self, report, fragment_list):
        # Report information
        self.record_name = report.name
        self.fragment_times = fragment_list.rec_list(self.record_name)  # list of fragments start seconds
        self.time_step = fragment_list.time_step
        self.wakefulness_level = 100

        # Calculating bar values
        self.ai_values = []
        self.time_values = []
        for i in range(len(self.fragment_times)):
            if self.fragment_times[i] < report.time[0]:
                continue
            elif self.fragment_times[i] > report.time[-1]:
                break
            else:
                start_index = report.time.index(self.fragment_times[i])
                ai = 0
                step = fragment_list.time_step
                if self.fragment_times[i] + step > report.time[-1]:
                    step = report.time[-1] - self.fragment_times[i]
                for k in range(step):
                    ai += report.stages[start_index + k]
                self.ai_values.append(ai / step)
                self.time_values.append(self.fragment_times[i] + int(step/2))

        """
        TESTING VALUES
        """
        self.time_values = [self.time_values[0] + i * 30 for i in range(50)]
        self.ai_values = 5*[i + 1 for i in range(10)]
        """"""

        """
        Figure parameters
        """
        # Axis values
        self.bars_on_plot = 15
        self.ticks_on_plot = 8
        self.x_min = self.time_values[0] - self.time_step/2
        self.x_max = self.time_values[-1] + self.time_step/2
        if len(self.time_values) > self.bars_on_plot:
            self.x_max = self.time_values[self.bars_on_plot - 1] + self.time_step/2
        self.y_min = -5
        self.y_max = 110
        self.pan_x = None

    def updateTicks(self):
        self.x_ticks_names = []
        self.x_ticks = []
        tick_per_value = int(round(self.bars_on_plot / self.ticks_on_plot))
        for i in range(len(self.time_values)):
            if i % tick_per_value == 0:
                self.x_ticks.append(self.time_values[i])
                self.x_ticks_names.append(str(datetime.datetime.fromtimestamp(self.time_values[i]).time()))

def reportTime(string_time: str, report_path: str):  # convert string to time in the given report
    year = int("20" + report_path[-6:-4])
    month = int(report_path[-8:-6])
    day = int(report_path[-10:-8])
    point = ':'  # default delimiter
    for char in string_time:
        if char == '.':
            point = '.'
            break
        elif char == '-':
            point = '-'
            break
        elif char == '\\':
            point = '\\'
            break
        elif char == '/':
            point = '/'
            break
    t = string_time.split(point)

    if (len(t) == 1):  # if there are no delimiters in the time
        t = t[0]
        if (len(t) == 3) or (len(t) == 4):  # if time 12:33:00 in format 1233
            return datetime.datetime(year, month, day, int(t[:-2]), int(t[-2:]), 00)
        if (len(t) == 5) or (len(t) == 6):
            return datetime.datetime(year, month, day, int(t[:-4]), int(t[-4:-2]),
                                     int(t[-2:]))  # if time 12:33:00 in format 123300
        return ' '.join(["Wrong time format", ''.join(t)])
    elif (len(t) == 2):  # if there is one delimiter in the time
        if (len(t[0]) == 2 or len(t[0]) == 1) and len(t[1]) == 2:
            return datetime.datetime(year, month, day, int(t[0]), int(t[1]), 00)
        return ' '.join(["Wrong time format", ''.join(t)])
    elif (len(t) == 3):  # if there are two delimiters in the time
        if (len(t[0]) == 2 or len(t[0]) == 1) and len(t[1]) == 2 and len(t[2]) == 2:
            return datetime.datetime(year, month, day, int(t[0]), int(t[1]), int(t[2]))
        return ' '.join(["Wrong time format", ''.join(t)])
    return ' '.join(["Wrong time format", ''.join(t)])

def matName2Time(matfile_name: str) -> [datetime.datetime, int]:  # convert Mat file's name to date and time
    start_second = 0
    if '(' in matfile_name:
        time_delta = matfile_name.split('(')[1].split(')')[
            0]  # this will be the number of seconds since the fragment beginning
        start_second = int(time_delta.split('-')[0])  # if time delta looks like 100-120

    name = matfile_name.split('_')
    if '(' in name[-1]:
        name[-1] = name[-1].split('(')[0]

    dt = int((datetime.datetime(int(name[-2][0:4]), int(name[-2][4:6]), int(name[-2][6:8]),  # YYYY, MM, DD
               int(name[-1][:2]), int(name[-1][3:5]),  # HH, MM, SS
               int(name[-1][6:8])) + datetime.timedelta(seconds = start_second)).timestamp())
    return dt

def medicineCounter(med_table):
    med_cells = [cell for cell in med_table if not pandas.isnull(cell)]
    medicines = []
    for cell in med_cells:
        for med in cell.split(','):
            if med not in medicines:
                medicines.append(med.replace(' ', ''))
    return ', '.join(medicines)

def timeFormat(time_stamps, file_name):
    date = file_name.split('_')[-1]
    second_stamps = []
    for t in time_stamps["Time"]:
        time = t.split(':')
        second_stamps.append(
            int(datetime.datetime(int("20" + date[4:6]), int(date[2:4]), int(date[0:2]),  # YYYY, MM, DD
                                  int(time[0]), int(time[1]), int(time[2])).timestamp()))  # HH, MM, SS

    if second_stamps != sorted(second_stamps):
        print("Wrong time order")
        exit(33)

    stages = []
    for stage in time_stamps["Stage"]:
        stages.append(stage)

    sec_stage = [second_stamps[0]]  # the first element is a start second
    for i in range(len(second_stamps) - 1):
        step = int(second_stamps[i + 1] - second_stamps[i])
        for k in range(step):
            sec_stage.append(STAGE_AI[stages[i]])  # append stage for each second
    return sec_stage

def fragmentUpdating(path_to_txts = "Z:\\Tetervak\\Reports\\reports 2.0\\fragments_txt",
                   path_to_pickles = "Z:\\Tetervak\\Reports\\reports 2.0\\fragments_pickle"):
    # Find all pickle fragments
    pickle_fragments = [f.split('_')[0] for f in os.listdir(path_to_pickles)
                   if os.path.isfile(os.path.join(path_to_pickles, f))]
    txt_fragments = [f for f in os.listdir(path_to_txts)
                   if os.path.isfile(os.path.join(path_to_txts, f))]
    # Find all fragments lists which don't have a pickle version
    for f in txt_fragments:
        if f.split('_')[0] not in pickle_fragments:
            FragmentList(os.path.join(path_to_txts, f)).fragments2pickle()

def reportUpdating(path_to_csvs = "Z:\\Tetervak\\Reports\\reports 2.0\\reports_csv",
                   path_to_pickles = "Z:\\Tetervak\\Reports\\reports 2.0\\reports_pickle"):
    # Find all pickle reports
    pickle_reports = [f.split('.')[0] for f in os.listdir(path_to_pickles)
                   if os.path.isfile(os.path.join(path_to_pickles, f))]
    csv_reports = [f for f in os.listdir(path_to_csvs)
                   if os.path.isfile(os.path.join(path_to_csvs, f))]

    # Find all completed reports which don't have an txt version
    for f in csv_reports:
        if f.split('_')[0] not in pickle_reports:
            ReportAI(os.path.join(path_to_csvs, f)).saveToPickle()

def onClick(event, axis, report):
    if event.xdata is not None and event.ydata is not None and event.button == 1:
        for i in range(len(report.time_values)):
            if abs(event.xdata - report.time_values[i]) <= report.time_step/2:
                report.ai_values[i] = int(event.ydata)
        axis.clear()
        drawBars(axis, report)
        axis.figure.canvas.draw()
    if event.xdata is not None and event.ydata is not None and event.button == 3:
        if True:
            report.pan_x = event.xdata

def onScroll(event, axis, report):
    if event.button == "up":
        report.bars_on_plot += 1
        report.x_max += report.time_step
        axis.clear()
        drawBars(axis, report)
        axis.figure.canvas.draw()
    if event.button == "down":
        report.bars_on_plot -= 1
        report.x_max -= report.time_step
        axis.clear()
        drawBars(axis, report)
        axis.figure.canvas.draw()

def onMovement(event, axis, report):
    if event.xdata is not None and event.ydata is not None and event.button == 1:
        for i in range(len(report.time_values)):
            if abs(event.xdata - report.time_values[i]) <= report.time_step/2:
                report.ai_values[i] = int(event.ydata)
        axis.clear()
        drawBars(axis, report)
        axis.figure.canvas.draw()
    elif event.xdata is not None and event.ydata is not None and event.button == 3 and report.pan_x is not None:
        report.x_max -= event.xdata - report.pan_x
        report.x_min -= event.xdata - report.pan_x
        if report.x_min < report.time_values[0]  - report.time_step/2:
            report.x_min = report.time_values[0]  - report.time_step/2
            report.x_max = report.time_values[0] + report.time_step / 2 + report.time_step * report.bars_on_plot
        if report.x_max > report.time_values[-1]  + report.time_step/2:
            report.x_max = report.time_values[-1] + report.time_step / 2
            report.x_min = report.time_values[-1] - report.time_step / 2 - report.time_step * report.bars_on_plot

        axis.clear()
        drawBars(axis, report)
        axis.figure.canvas.draw()

def drawBars(axis, report):
    report.updateTicks()
    start_bar = 0 # Find the first visible bar
    while(report.time_values[start_bar] < report.x_min):
        start_bar += 1
    last_bar = start_bar + report.bars_on_plot if len(report.time_values) > start_bar + report.bars_on_plot \
        else len(report.time_values) - 1 # Find the last visible bar

    for i in range(start_bar, last_bar): # Add value for the each visible bar
        axis.text(report.time_values[i], report.ai_values[i] + 2, "{}".format(report.ai_values[i]), ha='center')

    axis.plot([report.time_values[start_bar] - report.time_step, report.time_values[last_bar] + report.time_step],
              [report.wakefulness_level, report.wakefulness_level], color = 'red', linestyle='--', dashes=(5, 10))

    bar_list = axis.bar(report.time_values[start_bar: last_bar],
             report.ai_values[start_bar: last_bar],
             width = report.time_step - 1)

    for bar in bar_list:
        if bar._height < 0:
            bar.set_color('r')
        elif bar._height > 100:
            bar.set_color('y')

    axis.set_xticks(report.x_ticks)
    axis.set_xticklabels(report.x_ticks_names)
    axis.axis([report.x_min, report.x_max,
               report.y_min, report.y_max])
    axis.set_title(report.record_name)

if __name__ == "__main__":
    fragmentUpdating()
    reportUpdating()

    path_to_reports = "Z:\\Tetervak\\Reports\\reports 2.0\\reports_pickle"
    path_to_fragments = "Z:\\Tetervak\\Reports\\reports 2.0\\fragments_pickle"

    reports = [pickle.load(open(os.path.join(path_to_reports, pck), 'rb')) for pck in os.listdir(path_to_reports)
               if os.path.isfile(os.path.join(path_to_reports, pck))]
    fragments = [pickle.load(open(os.path.join(path_to_fragments, pck), 'rb')) for pck in os.listdir(path_to_fragments)
               if os.path.isfile(os.path.join(path_to_fragments, pck))]

    test_report = reports[0]
    test_fragment = fragments[0]

    test_bar_report = BaredReport(test_report, test_fragment)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    fig.canvas.mpl_connect('button_press_event', lambda event: onClick(event, axis=ax, report=test_bar_report))
    fig.canvas.mpl_connect('scroll_event', lambda event: onScroll(event, axis=ax, report=test_bar_report))
    fig.canvas.mpl_connect('motion_notify_event', lambda event: onMovement(event, axis=ax, report=test_bar_report))
    drawBars(ax, test_bar_report)
    plt.show()