import os, sys, ConfigParser
sys.path.append("../mojomail")
from mojomail import *

if __name__=="__main__":
  e=MojoMailer("/home/mojoarjun/configs/datamail.conf")
  e.logintomail()
  m=MojoMessage("/home/mojoarjun/configs/mm-GenericMessage.conf")
  p=e.getmessages("MOJOMAIL",sys.argv[1],sys.argv[2],m.getsubkey(),sys.argv[3])
  postlist=[]
  calllist=[]
  for item in p:
    if "Post" in item['$ATTACHMENT']:
      postlist.append(item['$ATTACHMENT'])
    if "Call" in item['$ATTACHMENT']:
      calllist.append(item['$ATTACHMENT'])
  finalposts=open(sys.argv[3]+"/CallReport-"+sys.argv[1]+"-to-"+sys.argv[2]+".csv","w")
  f=open(calllist[0],"rb")
  header=f.readlines()[0]
  finalposts.write(header)
  finallines=[]
  for logfile in calllist:
    p=open(logfile,"rb")
    lines=p.readlines()
    lines.remove(lines[0])
    finallines=finallines+lines
  for line in finallines:
    #print line
    finalposts.write(line)
  finalposts.close()

  '''
  #print m.getsubdict()
  #print m.getbodydict()
  subdict  = {'$TYPE': 'MoFoMail', '$META': 'TESTMESSAGE', '$TIMESTAMP': '2013-07-01 00:00:00', '$IDENTIFIER': '20922', '$PAYLOAD': '/home/mojoarjun/test.csv'}
  bodydict = {'$SERVERSIG': 'Hello Buddy, This is a fucking test', '$CONTENT': 'Donkeys Balls are big and strong \n Teri maa ke naurate kabhi nahi aate', '$RECIPIENT,': 'Buddy Boy', '$TIMESTAMP': '2013-07-01 00:00:00'}
  p=m.composemessage("arjunsdarkside@gmail.com",subdict,bodydict,"/home/mojoarjun/test.csv")
  e.sendmsg(p)
  #e.sendfile("/opt/mscodes.csv","swarahldev@gmail.com")
  '''


'''
	Define an BoardEvaluator class.
	Note: I wrote more than 100 lines if-elif-else statements to discuss every single
		possible scenarios that could occur. This portion of code looks ugly but it is
		quite necessary for the evaluation process.
'''
class BoardEvaluator(object):

	def __init__ (self):
		# self.POS is for adding weight to each intersetion
		# add weight of 7 to the center, 6 to the outer square, then
		# 5, 4, 3, 2, 1, at last 0 to the outermost square.
		self.POS = []
		for i in range(15):
			row = []
			for j in range(15):
				row.append( 7 - max(abs(i - 7), abs(j - 7)) )
			# row = [ (7 - max(abs(i - 7), abs(j - 7))) for j in range(15) ]
			self.POS.append(tuple(row))

		# different types of situations below
		self.cTwo = 1		# chong'er	2 stones in a row, 1 move to make a chongsan
		self.cThree = 2		# chongsan	3 stones in a row, 1 move to make a chongsi
		self.cFour = 3		# chongsi	4 stones in a row, 1 move(1 possible position) to make a 5
		self.two = 4		# huo'er	2 stones in a row, 1 move to make a huosan
		self.three = 5		# huosan	3 stones in a row, 1 move to make a huosi
		self.four = 6		# huosi		4 stones in a row, 1 move(2 possible positions) to make a 5
		self.five = 7		# huowu		5 stones in a row
		self.analyzed = 8		# has benn analyzed
		self.unanalyzed = 0			# has not been analyzed
		self.result = [ 0 for i in range(30) ]		# save current reslut of analyzation in a line
		self.line = [ 0 for i in range(30) ]		# current data in a line
		self.record = []			# result of analysis of whole board
									# format of each item in list is record[row][col][dir]
		for i in range(15):
			self.record.append([])
			self.record[i] = []
			for j in range(15):
				self.record[i].append([ 0, 0, 0, 0])
		self.count = []				# count of each situation: count[black/white][situation]
		for i in range(3):
			data = [ 0 for i in range(10) ]
			self.count.append(data)
		self.reset()

	# reset data
	def reset(self):
		unanalyzed = self.unanalyzed
		count = self.count
		for i in range(15):
			line = self.record[i]
			for j in range(15):
				line[j][0] = unanalyzed
				line[j][1] = unanalyzed
				line[j][2] = unanalyzed
				line[j][3] = unanalyzed
		for i in range(10):
			count[0][i] = 0
			count[1][i] = 0
			count[2][i] = 0
		return 0

	# analyze & evaluate board
	# return score based on analysis result
	def evaluate (self, board, turn):
		score = self.__evaluate(board, turn)
		count = self.count
		if score < -9000:
			if turn == 1:
				stone = 2
			elif turn == 2:
				stone = 1
			# print('evaluate: stone = ', stone)
			for i in range(10):
				if count[stone][i] > 0:
					score -= i
		elif score > 9000:
			if turn == 1:
				stone = 2
			elif turn == 2:
				stone = 1
			# print('evaluate: stone = ', stone)
			for i in range(10):
				if count[turn][i] > 0:
					score += i
		return score

	# analyze & evaluate board
	# in 4 directinos: horizontal, vertical, diagonal(left-hand or right-hand)
	# return score difference between players based on analysis result
	def __evaluate (self, board, turn):
		record = self.record
		count = self.count
		unanalyzed = self.unanalyzed
		analyzed = self.analyzed
		self.reset()
		# analysis in 4 directions
		for i in range(15):
			boardrow = board[i]
			recordrow = record[i]
			for j in range(15):
				if boardrow[j] != 0:
					# has not analyzed horizontally
					if recordrow[j][0] == unanalyzed:
						self.__analysis_horizon(board, i, j)
					# has not analyzed vertically
					if recordrow[j][1] == unanalyzed:
						self.__analysis_vertical(board, i, j)
					# has not analyzed left-hand diagonally
					if recordrow[j][2] == unanalyzed:
						self.__analysis_left(board, i, j)
					# has not analyzed right-hand diagonally
					if recordrow[j][3] == unanalyzed:
						self.__analysis_right(board, i, j)

		five = self.five
		four = self.four
		three =  self.three
		two = self.two
		cFour = self.cFour
		cThree = self.cThree
		cTwo = self.cTwo

		check = {}

		# for either white or black, calculated the number of occurences of different
		# situations (i.e., five, four, cFour, three, cThree, two, cTwo)
		for c in (five, four, cFour, three, cThree, two, cTwo):
			check[c] = 1
		# for each stone on the board
		for i in range(15):
			for j in range(15):
				stone = board[i][j]
				if stone != 0:
					# for 4 directions
					for k in range(4):
						ch = record[i][j][k]
						if ch in check:
							count[stone][ch] += 1

		# return score if there is a five
		black = 1
		white = 2
		# current turn is white
		if turn == white:
			if count[black][five]:
				return -9999
			elif count[white][five]:
				return 9999
		# current turn is black
		else:
			if count[white][five]:
				return -9999
			elif count[black][five]:
				return 9999

		# if there exist 2 chongsi, it's equivalent to 1 huosi
		if count[white][cFour] >= 2:
			count[white][four] += 1
		if count[black][cFour] >= 2:
			count[black][four] += 1

		# return score for specific situations
		wvalue = 0
		bvalue = 0
		win = 0
		# current turn is white
		if turn == white:
			if count[white][four] > 0:			# white huosi
				return 9990
			if count[white][cFour] > 0:			# white chongsi
				return 9980
			if count[black][four] > 0:			# black huosi
				return -9970
			if count[black][cFour] and count[black][three]:			# black chongsi & huosan
				return -9960
			if count[white][three] and count[black][cFour] == 0:	# white huosan & no black chongsi
				return 9950
			if	(count[black][three] > 1 and	# black >1 huosan &
				count[white][cFour] == 0 and	# no white chongsi &
				count[white][three] == 0 and	# no white huosan &
				count[white][cThree] == 0):		# no white chongsan
					return -9940

			if count[white][three] > 1:			# white >1 huosan
				wvalue += 2000
			elif count[white][three]:			# white 1 huosan
				wvalue += 200
			if count[black][three] > 1:			# black >1 huosan
				bvalue += 500
			elif count[black][three]:			# black 1 huosan
				bvalue += 100

			if count[white][cThree]:					# white chongsan
				wvalue += count[white][cThree] * 10
			if count[black][cThree]:					# black chongsan
				bvalue += count[black][cThree] * 10
			if count[white][two]:						# white huo'er
				wvalue += count[white][two] * 4
			if count[black][two]:						# black huo'er
				bvalue += count[black][two] * 4
			if count[white][cTwo]:						# white chong'er
				wvalue += count[white][cTwo]
			if count[black][cTwo]:						# black chong'er
				bvalue += count[black][cTwo]

		# current turn is black
		else:
			if count[black][four] > 0:			# black huosi
				return 9990
			if count[black][cFour] > 0:			# black chongsi
				return 9980
			if count[white][four] > 0:			# white huosi
				return -9970
			if count[white][cFour] and count[white][three]:			# white chongsi & huosan
				return -9960
			if count[black][three] and count[white][cFour] == 0:	# black huosan & no white chongsi
				return 9950
			if	(count[white][three] > 1 and	# white >1 huosan &
				count[black][cFour] == 0 and	# no black chongsi &
				count[black][three] == 0 and	# no black huosan &
				count[black][cThree] == 0):		# no black chongsan
					return -9940

			if count[black][three] > 1:			# black >1 huosan
				bvalue += 2000
			elif count[black][three]:			# black 1 huosan
				bvalue += 200
			if count[white][three] > 1:			# white >1 huosan
				wvalue += 500
			elif count[white][three]:			# white 1 huosan
				wvalue += 100

			if count[black][cThree]:					# black chongsan
				bvalue += count[black][cThree] * 10
			if count[white][cThree]:					# white chongsan
				wvalue += count[white][cThree] * 10
			if count[black][two]:						# black huo'er
				bvalue += count[black][two] * 4
			if count[white][two]:						# white huo'er
				wvalue += count[white][two] * 4
			if count[black][cTwo]:						# black chong'er
				bvalue += count[black][cTwo]
			if count[white][cTwo]:						# white chong'er
				wvalue += count[white][cTwo]

		# include weight for each intersection
		# add weight of 7 to the center, 6 to the outer square, then
		# 5, 4, 3, 2, 1, at last 0 to the outermost square.
		wc = 0
		bc = 0
		# for each intersection with a stone, add weight
		for i in range(15):
			for j in range(15):
				stone = board[i][j]
				if stone != 0:
					if stone == white:
						wc += self.POS[i][j]
					else:
						bc += self.POS[i][j]
		# add total weight to total score
		wvalue += wc
		bvalue += bc

		# return score differnece between players
		if turn == white:
			return wvalue - bvalue

		return bvalue - wvalue

	# anaylze horizontally
	def __analysis_horizon (self, board, i, j):
		line = self.line
		result = self.result
		record = self.record
		unanalyzed = self.unanalyzed
		# add each intersection in a row to line
		for x in range(15):
			line[x] = board[i][x]
		self.analysis_line(line, result, 15, j)
		for x in range(15):
			if result[x] != unanalyzed:
				record[i][x][0] = result[x]
		return record[i][j][0]

	# analyze vertically
	def __analysis_vertical (self, board, i, j):
		line = self.line
		result = self.result
		record = self.record
		unanalyzed = self.unanalyzed
		for x in range(15):
			line[x] = board[x][j]
		self.analysis_line(line, result, 15, i)
		for x in range(15):
			if result[x] != unanalyzed:
				record[x][j][1] = result[x]
		return record[i][j][1]

	# analyze left-hand diagonally
	def __analysis_left (self, board, i, j):
		line = self.line
		result = self.result
		record = self.record
		unanalyzed = self.unanalyzed
		if i < j:
			x, y = j - i, 0
		else:
			x, y = 0, i - j
		k = 0
		while k < 15:
			if x + k > 14 or y + k > 14:
				break
			line[k] = board[y + k][x + k]
			k += 1
		self.analysis_line(line, result, k, j - x)
		for s in range(k):
			if result[s] != unanalyzed:
				record[y + s][x + s][2] = result[s]
		return record[i][j][2]

	# analyzed right-hand diagonally
	def __analysis_right (self, board, i, j):
		line = self.line
		result = self.result
		record = self.record
		unanalyzed = self.unanalyzed
		if 14 - i < j:
			x, y, realnum = j - 14 + i, 14, 14 - i
		else:
			x, y, realnum = 0, i + j, j
		k = 0
		while k < 15:
			if x + k > 14 or y - k < 0:
				break
			line[k] = board[y - k][x + k]
			k += 1
		self.analysis_line(line, result, k, j - x)
		for s in range(k):
			if result[s] != unanalyzed:
				record[y - s][x + s][3] = result[s]
		return record[i][j][3]

	# analyze a line, find out different situations (i.e., five, four, three, etc)
	def analysis_line (self, line, record, num, pos):
		unanalyzed = self.unanalyzed
		analyzed = self.analyzed
		three = self.three
		cThree = self.cThree
		four = self.four
		cFour = self.cFour

		while len(line) < 30:
			line.append(15)
		while len(record) < 30:
			record.append(unanalyzed)

		for i in range(num, 30):
			line[i] = 15
		for i in range(num):
			record[i] = unanalyzed

		if num < 5:
			for i in range(num):
				record[i] = analyzed
			return 0
		stone = line[pos]
		inverse = (0, 2, 1)[stone]
		num -= 1
		xl = pos
		xr = pos
		# left border
		while xl > 0:
			if line[xl - 1] != stone:
				break
			xl -= 1
		# right border
		while xr < num:
			if line[xr + 1] != stone:
				break
			xr += 1
		left_range = xl
		right_range = xr
		# left border (not opponent's stone intersection)
		while left_range > 0:
			if line[left_range - 1] == inverse:
				break
			left_range -= 1
		# right border (not opponent's stone intersection)
		while right_range < num:
			if line[right_range + 1] == inverse:
				break
			right_range += 1

		# if the linear range is less than 5, return directly
		if right_range - left_range < 4:
			for k in range(left_range, right_range + 1):
				record[k] = analyzed
			return 0

		# set analyzed
		for k in range(xl, xr + 1):
			record[k] = analyzed

		srange = xr - xl

		# if 5 in a row
		if srange >= 4:
			record[pos] = self.five
			return self.five

		# if 4 in a row
		if srange == 3:
			leftfour = False
			# if space on the left
			if xl > 0:
				if line[xl - 1] == 0:
					# huo'si
					leftfour = True
			if xr < num:
				if line[xr + 1] == 0:
					if leftfour:
						# huo'si
						record[pos] = self.four
					else:
						# chognsi
						record[pos] = self.cFour
				else:
					if leftfour:
						# chongsi
						record[pos] = self.cFour
			else:
				if leftfour:
					# chongsi
					record[pos] = self.cFour
			return record[pos]

		# if 3 in a row
		if srange == 2:
			left3 = False
			# if space on the left
			if xl > 0:
				# if space on the left
				if line[xl - 1] == 0:
					if xl > 1 and line[xl - 2] == stone:
						record[xl] = cFour
						record[xl - 2] = analyzed
					else:
						left3 = True
				elif xr == num or line[xr + 1] != 0:
					return 0
			if xr < num:
				# if space on the right
				if line[xr + 1] == 0:
					if xr < num - 1 and line[xr + 2] == stone:
						# 11101 or 22202 is equivalent to chongsi
						record[xr] = cFour
						record[xr + 2] = analyzed
					elif left3:
						record[xr] = three
					else:
						record[xr] = cThree
				elif record[xl] == cFour:
					return record[xl]
				elif left3:
					record[pos] = cThree
			else:
				if record[xl] == cFour:
					return record[xl]
				if left3:
					record[pos] = cThree
			return record[pos]

		# if 2 in a row
		if srange == 1:
			left2 = False
			if xl > 2:
				# if space on the left
				if line[xl - 1] == 0:
					if line[xl - 2] == stone:
						if line[xl - 3] == stone:
							record[xl - 3] = analyzed
							record[xl - 2] = analyzed
							record[xl] = cFour
						elif line[xl - 3] == 0:
							record[xl - 2] = analyzed
							record[xl] = cThree
					else:
						left2 = True
			if xr < num:
				# if space on the right
				if line[xr + 1] == 0:
					if xr < num - 2 and line[xr + 2] == stone:
						if line[xr + 3] == stone:
							record[xr + 3] = analyzed
							record[xr + 2] = analyzed
							record[xr] = cFour
						elif line[xr + 3] == 0:
							record[xr + 2] = analyzed
							record[xr] = left2 and three or cThree
					else:
						if record[xl] == cFour:
							return record[xl]
						if record[xl] == cThree:
							record[xl] = three
							return record[xl]
						if left2:
							record[pos] = self.two
						else:
							record[pos] = self.cTwo
				else:
					if record[xl] == cFour:
						return record[xl]
					if left2:
						record[pos] = self.cTwo
			return record[pos]
		return 0


# -*- mode: python -*-
# -*- coding: utf-8 -*-

##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2021 Emmanuel Viennet.  All rights reserved.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   Emmanuel Viennet      emmanuel.viennet@viennet.net
#
##############################################################################

"""Génération du bulletin en format JSON (beta, non completement testé)

"""
import datetime
import json

import app.scodoc.sco_utils as scu
import app.scodoc.notesdb as ndb
from app.scodoc import sco_abs
from app.scodoc import sco_cache
from app.scodoc import sco_edit_ue
from app.scodoc import sco_evaluations
from app.scodoc import sco_formsemestre
from app.scodoc import sco_groups
from app.scodoc import sco_photos
from app.scodoc import sco_preferences
from app.scodoc import sco_etud

# -------- Bulletin en JSON

def make_json_formsemestre_bulletinetud(
    formsemestre_id: int,
    etudid: int,
    xml_with_decisions=False,
    version="long",
    force_publishing=False,  # force publication meme si semestre non publie sur "portail"
) -> str:
    """Renvoie bulletin en chaine JSON"""

    d = formsemestre_bulletinetud_published_dict(
        formsemestre_id,
        etudid,
        force_publishing=force_publishing,
        xml_with_decisions=xml_with_decisions,
        version=version,
    )

    return json.dumps(d, cls=scu.ScoDocJSONEncoder)

# (fonction séparée: n'utilise pas formsemestre_bulletinetud_dict()
#   pour simplifier le code, mais attention a la maintenance !)
#
def formsemestre_bulletinetud_published_dict(
    formsemestre_id,
    etudid,
    force_publishing=False,
    xml_nodate=False,
    xml_with_decisions=False,  # inclue les decisions même si non publiées
    version="long",
):
    """Dictionnaire representant les informations _publiees_ du bulletin de notes
    Utilisé pour JSON, devrait l'être aussi pour XML. (todo)
    """
    from app.scodoc import sco_bulletins

    d = {}

    sem = sco_formsemestre.get_formsemestre(formsemestre_id)
    if (not sem["bul_hide_xml"]) or force_publishing:
        published = 1
    else:
        published = 0
    if xml_nodate:
        docdate = ""
    else:
        docdate = datetime.datetime.now().isoformat()

    el = {
        "etudid": etudid,
        "formsemestre_id": formsemestre_id,
        "date": docdate,
        "publie": published,
        "etapes": sem["etapes"],
    }
    # backward compat:
    if sem["etapes"]:
        el["etape_apo"] = sem["etapes"][0] or ""
        n = 2
        for et in sem["etapes"][1:]:
            el["etape_apo" + str(n)] = et or ""
            n += 1
    d.update(**el)

    # Infos sur l'etudiant
    etudinfo = sco_etud.get_etud_info(etudid=etudid, filled=True)[0]

    d["etudiant"] = dict(
        etudid=etudid,
        code_nip=etudinfo["code_nip"],
        code_ine=etudinfo["code_ine"],
        nom=scu.quote_xml_attr(etudinfo["nom"]),
        prenom=scu.quote_xml_attr(etudinfo["prenom"]),
        civilite=scu.quote_xml_attr(etudinfo["civilite_str"]),
        photo_url=scu.quote_xml_attr(sco_photos.etud_photo_url(etudinfo, fast=True)),
        email=scu.quote_xml_attr(etudinfo["email"]),
        emailperso=scu.quote_xml_attr(etudinfo["emailperso"]),
    )
    d["etudiant"]["sexe"] = d["etudiant"]["civilite"]  # backward compat for our clients
    # Disponible pour publication ?
    if not published:
        return d  # stop !

    # Groupes:
    partitions = sco_groups.get_partitions_list(formsemestre_id, with_default=False)
    partitions_etud_groups = {}  # { partition_id : { etudid : group } }
    for partition in partitions:
        pid = partition["partition_id"]
        partitions_etud_groups[pid] = sco_groups.get_etud_groups_in_partition(pid)

    nt = sco_cache.NotesTableCache.get(formsemestre_id)  # > toutes notes
    ues = nt.get_ues()
    modimpls = nt.get_modimpls()
    nbetuds = len(nt.rangs)
    mg = scu.fmt_note(nt.get_etud_moy_gen(etudid))
    if (
        nt.get_moduleimpls_attente()
        or sco_preferences.get_preference("bul_show_rangs", formsemestre_id) == 0
    ):
        # n'affiche pas le rang sur le bulletin s'il y a des
        # notes en attente dans ce semestre
        rang = ""
        rang_gr = {}
        ninscrits_gr = {}
    else:
        rang = str(nt.get_etud_rang(etudid))
        rang_gr, ninscrits_gr, gr_name = sco_bulletins.get_etud_rangs_groups(
            etudid, formsemestre_id, partitions, partitions_etud_groups, nt
        )

    d["note"] = dict(
        value=mg,
        min=scu.fmt_note(nt.moy_min),
        max=scu.fmt_note(nt.moy_max),
        moy=scu.fmt_note(nt.moy_moy),
    )
    d["rang"] = dict(value=rang, ninscrits=nbetuds)
    d["rang_group"] = []
    if rang_gr:
        for partition in partitions:
            d["rang_group"].append(
                dict(
                    group_type=partition["partition_name"],
                    group_name=gr_name[partition["partition_id"]],
                    value=rang_gr[partition["partition_id"]],
                    ninscrits=ninscrits_gr[partition["partition_id"]],
                )
            )

    d["note_max"] = dict(value=20)  # notes toujours sur 20
    d["bonus_sport_culture"] = dict(value=nt.bonus[etudid])

    # Liste les UE / modules /evals
    d["ue"] = []
    d["ue_capitalisee"] = []
    for ue in ues:
        ue_status = nt.get_etud_ue_status(etudid, ue["ue_id"])
        if ue["ects"] is None:
            ects_txt = ""
        else:
            ects_txt = f"{ue['ects']:2.3g}"
        u = dict(
            id=ue["ue_id"],
            numero=scu.quote_xml_attr(ue["numero"]),
            acronyme=scu.quote_xml_attr(ue["acronyme"]),
            titre=scu.quote_xml_attr(ue["titre"]),
            note=dict(
                value=scu.fmt_note(ue_status["cur_moy_ue"]),
                min=scu.fmt_note(ue["min"]),
                max=scu.fmt_note(ue["max"]),
                moy=scu.fmt_note(
                    ue["moy"]
                ),  # CM : ajout pour faire apparaitre la moyenne des UE
            ),
            rang=str(nt.ue_rangs[ue["ue_id"]][0][etudid]),
            effectif=str(nt.ue_rangs[ue["ue_id"]][1]),
            ects=ects_txt,
            code_apogee=scu.quote_xml_attr(ue["code_apogee"]),
        )
        d["ue"].append(u)
        u["module"] = []
        # Liste les modules de l'UE
        ue_modimpls = [mod for mod in modimpls if mod["module"]["ue_id"] == ue["ue_id"]]
        for modimpl in ue_modimpls:
            mod_moy = scu.fmt_note(
                nt.get_etud_mod_moy(modimpl["moduleimpl_id"], etudid)
            )
            if mod_moy == "NI":  # ne mentionne pas les modules ou n'est pas inscrit
                continue
            mod = modimpl["module"]
            # if mod['ects'] is None:
            #    ects = ''
            # else:
            #    ects = str(mod['ects'])
            modstat = nt.get_mod_stats(modimpl["moduleimpl_id"])

            m = dict(
                id=modimpl["moduleimpl_id"],
                code=mod["code"],
                coefficient=mod["coefficient"],
                numero=mod["numero"],
                titre=scu.quote_xml_attr(mod["titre"]),
                abbrev=scu.quote_xml_attr(mod["abbrev"]),
                # ects=ects, ects des modules maintenant inutilisés
                note=dict(value=mod_moy),
                code_apogee=scu.quote_xml_attr(mod["code_apogee"]),
            )
            m["note"].update(modstat)
            for k in ("min", "max", "moy"):  # formatte toutes les notes
                m["note"][k] = scu.fmt_note(m["note"][k])

            u["module"].append(m)
            if sco_preferences.get_preference("bul_show_mod_rangs", formsemestre_id):
                m["rang"] = dict(
                    value=nt.mod_rangs[modimpl["moduleimpl_id"]][0][etudid]
                )
                m["effectif"] = dict(value=nt.mod_rangs[modimpl["moduleimpl_id"]][1])

            # --- notes de chaque eval:
            evals = nt.get_evals_in_mod(modimpl["moduleimpl_id"])
            m["evaluation"] = []
            if version != "short":
                for e in evals:
                    if e["visibulletin"] or version == "long":
                        val = e["notes"].get(etudid, {"value": "NP"})[
                            "value"
                        ]  # NA si etud demissionnaire
                        val = scu.fmt_note(val, note_max=e["note_max"])
                        m["evaluation"].append(
                            dict(
                                jour=ndb.DateDMYtoISO(e["jour"], null_is_empty=True),
                                heure_debut=ndb.TimetoISO8601(
                                    e["heure_debut"], null_is_empty=True
                                ),
                                heure_fin=ndb.TimetoISO8601(
                                    e["heure_fin"], null_is_empty=True
                                ),
                                coefficient=e["coefficient"],
                                evaluation_type=e["evaluation_type"],
                                evaluation_id=e[
                                    "evaluation_id"
                                ],  # CM : ajout pour permettre de faire le lien sur les bulletins en ligne avec l'évaluation
                                description=scu.quote_xml_attr(e["description"]),
                                note=val,
                            )
                        )
                # Evaluations incomplètes ou futures:
                complete_eval_ids = set([e["evaluation_id"] for e in evals])
                if sco_preferences.get_preference(
                    "bul_show_all_evals", formsemestre_id
                ):
                    all_evals = sco_evaluations.do_evaluation_list(
                        args={"moduleimpl_id": modimpl["moduleimpl_id"]}
                    )
                    all_evals.reverse()  # plus ancienne d'abord
                    for e in all_evals:
                        if e["evaluation_id"] not in complete_eval_ids:
                            m["evaluation"].append(
                                dict(
                                    jour=ndb.DateDMYtoISO(
                                        e["jour"], null_is_empty=True
                                    ),
                                    heure_debut=ndb.TimetoISO8601(
                                        e["heure_debut"], null_is_empty=True
                                    ),
                                    heure_fin=ndb.TimetoISO8601(
                                        e["heure_fin"], null_is_empty=True
                                    ),
                                    coefficient=e["coefficient"],
                                    description=scu.quote_xml_attr(e["description"]),
                                    incomplete="1",
                                )
                            )

        # UE capitalisee (listee seulement si meilleure que l'UE courante)
        if ue_status["is_capitalized"]:
            try:
                ects_txt = str(int(ue_status["ue"].get("ects", "")))
            except:
                ects_txt = ""
            d["ue_capitalisee"].append(
                dict(
                    id=ue["ue_id"],
                    numero=scu.quote_xml_attr(ue["numero"]),
                    acronyme=scu.quote_xml_attr(ue["acronyme"]),
                    titre=scu.quote_xml_attr(ue["titre"]),
                    note=scu.fmt_note(ue_status["moy"]),
                    coefficient_ue=scu.fmt_note(ue_status["coef_ue"]),
                    date_capitalisation=ndb.DateDMYtoISO(ue_status["event_date"]),
                    ects=ects_txt,
                )
            )

    # --- Absences
    if sco_preferences.get_preference("bul_show_abs", formsemestre_id):
        nbabs, nbabsjust = sco_abs.get_abs_count(etudid, sem)
        d["absences"] = dict(nbabs=nbabs, nbabsjust=nbabsjust)

    # --- Decision Jury
    if (
        sco_preferences.get_preference("bul_show_decision", formsemestre_id)
        or xml_with_decisions
    ):
        infos, dpv = sco_bulletins.etud_descr_situation_semestre(
            etudid,
            formsemestre_id,
            format="xml",
            show_uevalid=sco_preferences.get_preference(
                "bul_show_uevalid", formsemestre_id
            ),
        )
        d["situation"] = scu.quote_xml_attr(infos["situation"])
        if dpv:
            decision = dpv["decisions"][0]
            etat = decision["etat"]
            if decision["decision_sem"]:
                code = decision["decision_sem"]["code"]
            else:
                code = ""

            d["decision"] = dict(code=code, etat=etat)
            if (
                decision["decision_sem"]
                and "compense_formsemestre_id" in decision["decision_sem"]
            ):
                d["decision"]["compense_formsemestre_id"] = decision["decision_sem"][
                    "compense_formsemestre_id"
                ]

            d["decision_ue"] = []
            if decision[
                "decisions_ue"
            ]:  # and sco_preferences.get_preference( 'bul_show_uevalid', formsemestre_id): always publish (car utile pour export Apogee)
                for ue_id in decision["decisions_ue"].keys():
                    ue = sco_edit_ue.ue_list({"ue_id": ue_id})[0]
                    d["decision_ue"].append(
                        dict(
                            ue_id=ue["ue_id"],
                            numero=scu.quote_xml_attr(ue["numero"]),
                            acronyme=scu.quote_xml_attr(ue["acronyme"]),
                            titre=scu.quote_xml_attr(ue["titre"]),
                            code=decision["decisions_ue"][ue_id]["code"],
                            ects=scu.quote_xml_attr(ue["ects"] or ""),
                        )
                    )
            d["autorisation_inscription"] = []
            for aut in decision["autorisations"]:
                d["autorisation_inscription"].append(
                    dict(semestre_id=aut["semestre_id"])
                )
        else:
            d["decision"] = dict(code="", etat="DEM")

    # --- Appreciations
    cnx = ndb.GetDBConnexion()
    apprecs = sco_etud.appreciations_list(
        cnx, args={"etudid": etudid, "formsemestre_id": formsemestre_id}
    )
    d["appreciation"] = []
    for app in apprecs:
        d["appreciation"].append(
            dict(
                comment=scu.quote_xml_attr(app["comment"]),
                date=ndb.DateDMYtoISO(app["date"]),
            )
        )

    #
    return d


# _*_ encoding: utf-8 _*_
from django.urls import path, include
from rest_framework.routers import DefaultRouter

from app import views

__author__ = 'nzb'
__date__ = '2021/2/20 12:59'
__doc__ = ''

router = DefaultRouter()

router.register('dynamic_chart', views.DynamicView, 'dynamic_chart')     # 动态图表

urlpatterns = [
    path('', include(router.urls)),
]


from torch import nn
from algorithms.DeepSVDD.SVDD import Model_first_stage, Model_second_stage
from options import args
if args.dataset == 'psm':
    from datasets.psm_moon import client_datasets_non_iid, client_datasets_iid
if args.dataset == 'smap':
    from datasets.smap_moon import client_datasets_non_iid, client_datasets_iid
if args.dataset == 'smd':
    from datasets.smd_moon import client_datasets_non_iid, client_datasets_iid
import warnings

warnings.filterwarnings("ignore")

config_svdd = {
        "epochs": 100,
        "iid": True,
        "stage": "first",
}

config_stage_2 = {
        "stage": "second",
}

if config_svdd["iid"]:
    client_datasets = client_datasets_iid
else:
    client_datasets = client_datasets_non_iid

def switch_config():
    for key in config_stage_2.keys():
        config_svdd[key] = config_stage_2[key]

def load_model(state_dict) -> nn.Module:

    if config_svdd["stage"] == "first":
        model = Model_first_stage()
    elif config_svdd["stage"] == "second":
        model = Model_second_stage()
    else:
        raise NotImplementedError
    model.load_state_dict(state_dict)
    return model


import codecs
from bs4 import BeautifulSoup
import sys
import os
import re
import xlsxwriter

# Converts pdf file to html file using pdfminer
def ConvertToHtml(fileName):
	os.popen("pdf2txt.py -o " + "output.html " + fileName )
	html = codecs.open("output.html", 'r')
	return html
# Extract EpicNo from html file
# Epic no has font = AAAAAF+Arial; font-size:10px and star with ANB or GLT or UP
def ParseEpicFromHtml(html):
	soup = BeautifulSoup(html,'lxml')
	style = soup.find_all('span', {'style':'font-family: AAAAAC+Arial; font-size:10px'})
	pattern = re.compile('^[ANB|UP|GLT]')

	workbook = xlsxwriter.Workbook('data.xlsx')
	worksheet = workbook.add_worksheet()
	worksheet.set_column('A:A', 20)
	worksheet.set_column('B:B', 20)
	worksheet.set_column('C:C', 20)
	worksheet.set_column('D:D', 22)
	worksheet.set_column('E:E', 27)
	worksheet.write('A1', 'EPIC NO')
	worksheet.write('B1', 'Name')
	worksheet.write('C1', 'Name (Hindi)')
	worksheet.write('D1', "Father/Husband's Name")
	worksheet.write('E1', "Father/Husband's Name(Hindi)")
	worksheet.write('F1', 'Age')
	worksheet.write('G1', 'Gender')

	i = 1
	for epic in style:
		if pattern.match(str(epic.text)):
			i = i + 1
			worksheet.write('A'+ str(i), str(epic.text).rsplit(' ')[0])
	workbook.close()
	return 1

if __name__ == "__main__":
	fileName = sys.argv[1]
	print("Converting PDF to html")
	html = ConvertToHtml(fileName)
	print("Parsing Epic No from html")
	ParseEpicFromHtml(html)
	os.remove("output.html")


from .constants import code
import mimetypes
import posixpath
import os

def guess_type(path):
    extensions_map = mimetypes.types_map.copy()
    extensions_map.update({
        '': 'application/octet-stream',  # Default
        '.py': 'text/plain',
        '.c': 'text/plain',
        '.h': 'text/plain'
    })
    base, ext = posixpath.splitext(path)
    if ext in extensions_map:
        return extensions_map[ext]
    ext = ext.lower()
    if ext in extensions_map:
        return extensions_map[ext]
    else:
        return extensions_map['']

class Response():
    def __init__(self, method, writer):
        self.__writer = writer

        self.method = method
        # response header dictionary
        self.headers = {}
        # response body (in bytes)
        self.body = b''

        self.code = 404

    def __writeline(self, str):
        self.__writer.writelines([str.encode()])

    def __writelines(self, strs):
        self.__writer.writelines([s.encode() for s in strs])

    def __write_header(self):
        self.__writeline("HTTP/1.1 %d %s\r\n" % (self.code, code[self.code]))
        for k, v in self.headers.items():
            self.__writeline("%s: %s\r\n" % (k, v))
        self.__writeline("\r\n")

    def __write_body(self):
        self.__writer.writelines([self.body])

    def set_status(self, code):
        self.code = code

    def set_header(self, key, value):
        self.headers[key] = value

    def set_body(self, body):
        self.body = body

    def html(self, body):
        self.set_status(200)
        self.set_header('Content-Type', 'text/html;charset=utf-8')
        self.set_body(body.encode("utf-8"))

    async def send_file(self, path, start=None, end=None):
        size = os.path.getsize(path)
        file = open(path, 'rb')
        if start is None and end is None:
            print("Send file %s with size %d" % (path, size))
            self.set_header('Content-Type', guess_type(path))
            self.set_header('Content-Length', str(size))
            self.set_body(file.read())
        else:
            if start is None:
                start = 0
            if end is None:
                end = size - 1
            self.set_status(206)
            self.set_header('Accept-Ranges', 'bytes')
            self.set_header('Content-Range', 'bytes %d-%d/%d' % (start, end, size)),
            if start > end:
                raise Exception("Invalid range specified")
            else:
                file.seek(start)
                self.set_body(file.read(end-start+1))
        file.close()

    async def end(self):
        self.__write_header()
        if self.method == 'HEAD':
            pass
        else:
            self.__write_body()
        await self.__writer.drain()
        self.__writer.close()


import numpy as np
import pandas as pd
import mestim as M
from tabulate import tabulate
from numpy import linalg as la
from numpy import random
from scipy.stats import genextreme
import matplotlib.pyplot as plt
from scipy.stats import norm

def sim_data(N=500, beta=[1,1], sigma=1, error_type='normal'):
    beta=np.array(beta).reshape(-1,1)
    x =  random.normal(size=(N, beta.shape[0]))
    x[:,0]=np.ones((N))
    u=sigma*sim_error(N, x, error_type) # error has mean 0 and std.dev sigma
    ys = x @ beta + u
    y = np.maximum(ys, 0)
    return y, ys, x, u

def sim_error(N, x, error_type='normal'):
    # Generate errors
    if error_type=='normal':
        u=random.normal(0, 1, size=(N,1))
    elif error_type=='log-normal':
        u=np.exp(random.normal(0, 1, size=(N,1)));
    elif error_type=='uniform':
        u=random.random(size=(N,1));
    elif error_type=='het':
        u=(0.2*x[:,1]*x[:,1]).reshape(-1,1)*random.normal(0, 1, size=(N,1))
    elif error_type=='mix_sym':
        # Parameter for mixture of two normals
        p=0.5                       # Mixing parameter
        mu1=-2;mu2=2               # Means
        se1=1; se2=1;               # Standard deviation
        u_u=random.random(size=(N,1))
        u1=random.normal(size=(N,1))
        u2=random.normal(size=(N,1))
        u=(mu1+se1*u1)*(u_u<=p) + (mu2+se2*u2)*(u_u>p)
    elif error_type=='mix_asym':
        # Parameter for mixture of two normals
        p=0.5                       # Mixing parameter
        mu1=-2;mu2=2               # Means
        se1=2; se2=1;               # Standard deviation
        u_u=random.random(size=(N,1))
        u1=random.normal(size=(N,1))
        u2=random.normal(size=(N,1))
        u=(mu1+se1*u1)*(u_u<=p) + (mu2+se2*u2)*(u_u>p)

    u=u-np.mean(u);
    u=u/np.std(u);
    return u

def Q_tobit(theta, y, x, out='Q'): # sample objective function and derivatives for tobit
    N, K = x.shape
    theta=np.array(theta).reshape(K+1,1)
    beta=theta[:-1,:]
    sigma=theta[-1,:]
    Phi_i=norm.cdf(x@beta/sigma)
    Phi_i=np.minimum(np.maximum(Phi_i,1e-15),1-1e-15)

    ll_i = 1*(y == 0)*np.log(1-Phi_i)  -  1*(y > 0)*((y-x@beta)**2/(2*sigma**2) + np.log(sigma**2)/2)
    if out=='Q': return -np.mean(ll_i)

    s_i=s_i_tobit(beta, sigma, y, x, Phi_i) # computes NxK array with scores
    if out=='s_i': return s_i  # Return s_i
    if out=='dQ':  return -np.mean(s_i, axis=0);  # Return dQ: array of size K derivative of sample objective function

def s_i_tobit(beta, sigma, y, x, Phi_i):
    # derivatives
    phi_i=norm.pdf(x@beta/sigma)
    phi_i=np.minimum(np.maximum(phi_i,1e-15),1-1e-15)
    s_i_beta= 1/sigma**2*((y > 0)*(y-x@beta) - 1*(y == 0)*sigma*phi_i/(1-Phi_i))*x
    s_i_sigma =  1*(y == 0)/(1-Phi_i)*phi_i*x@beta/(sigma**2)
    s_i_sigma =  1*(y == 0)*phi_i/(1-Phi_i)*x@beta/(sigma**2)
    s_i_sigma += 1*(y > 0)*((y-x@beta)**2/(sigma**3) -1/(sigma))
    s_i=np.append(s_i_beta, s_i_sigma.reshape(-1,1),  axis=1)
    return s_i

def tobit(y, x, cov_type='Ainv',theta0=None, deriv=1, quiet=False):
    # Objective function and derivatives for
    Qfun     = lambda theta, out:  Q_tobit(theta, y, x, out)
    N, K, xvars=labels(x)
    if theta0 is None:
        theta0=np.zeros((K+1,1))
        b=la.inv(x.T@x)@x.T@y;
        theta0[0:-1,:]=b
        theta0[-1,:]=np.sqrt(np.mean((y-x@b)**2))

    res=M.estimation(Qfun, theta0, deriv, cov_type, parnames=xvars)
    res.update(dict(zip(['yvar', 'xvars', 'N','K', 'n'], ['y', xvars, N, K, N])))
    if quiet==False:
        print('Tobit model')
        print('Fractions of observations that are censored: ', np.mean(1*(y==0)))
        print('Initial log-likelihood', -Qfun(theta0, 'Q'))
        print('Initial gradient\n', -Qfun(theta0, 'dQ'))
        print_output(res)
    return res

def scatter_ols(x,y,ys=None):
    x=x[:,1].reshape(-1,1)

    plt.figure(figsize=(10,8))
    plt.scatter(x[y==0], y[y==0], s=0.4, label='y=0')
    plt.scatter(x[y>0], y[y>0],  s=0.4, label='y>0')
    if not ys is None:
        plt.scatter(x[ys<0], ys[ys<0], s=0.4, label='y<0')
    a, b = np.polyfit(x[:,0], y[:,0], 1)
    plt.plot(x, a*x + b, label='OLS (observed, y)')
    a, b = np.polyfit(x[y>0], y[y>0], 1)
    plt.plot(x, a*x + b, label='OLS (truncated, y>0)')
    if not ys is None:
        a, b = np.polyfit(x[:,0], ys[:,0], 1)
        plt.plot(x, a*x + b, label='OLS (latent variable, y*)')
    plt.legend(loc="upper left", prop={'size': 15})
    plt.show()

def condmean(beta, sigma, y, x):
    xb=x@beta                   # E(y*|x)
    Phi = norm.cdf(xb/sigma)    # P(y=0|x)
    phi = norm.pdf(xb/sigma)
    inv_mills = sigma*phi/Phi   # inverse mills ratio
    Ey_trunc= xb + inv_mills    # E[y|x,y>0]
    Ey= xb*Phi + sigma*phi      # E[y|x]
    return xb, Phi, phi, inv_mills, Ey_trunc, Ey

def scatter_condmean(beta, sigma, x,y, ys=None):
    y=y[x[:,1].argsort()]
    x=x[x[:,1].argsort()]
    x1=x[:,1].reshape(-1,1)
    beta=np.array(beta).reshape(-1,1)

    xb, Phi, phi, inv_mills, Ey_trunc, Ey = condmean(beta, sigma, y, x)

    plt.figure(figsize=(10,8))
    plt.scatter(x1[y==0], y[y==0], s=0.4, label='y=0')
    plt.scatter(x1[y>0], y[y>0],  s=0.4, label='y>0')
    if not ys is None:
        plt.scatter(x1[ys<0], ys[ys<0], s=0.4, label='y<0')
    plt.plot(x1, xb, label='E(y*|x)=x*beta')
    a, b = np.polyfit(x1[y>0], y[y>0], 1)
    plt.plot(x1, Ey, label='E(y|x)')
    plt.plot(x1, Ey_trunc, label='E(y|x, y>0)')
    plt.legend(loc="upper left", prop={'size': 15})
    plt.show()

def labels(x):
    # labels and dimensions for plotting
    N, K = x.shape
    xvars=['x' + str(i)  for i in range(K+1)]
    xvars[-1]='sigma'
    return N, K, xvars

def print_output(res, cols=['parnames','theta_hat', 'se', 't-values', 'jac']):
    print('Dep. var. :', res['yvar'], '\n')

    table=({k:res[k] for k in cols})
    print(tabulate(table, headers="keys",floatfmt="10.5f"))
    # print('\n# of groups:      :', res['n'])
    print('# of observations :', res['N'])
    print('# log-likelihood. :', - res['Q']*res['n'], '\n')
    print ('Iteration info: %d iterations, %d evaluations of objective, and %d evaluations of gradients'
        % (res.nit,res.nfev, res.njev))
    print(f"Elapsed time: {res['time']:0.4f} seconds")
    print('')


# 导入需要库
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras.layers import Conv2D
from tensorflow.python.keras.layers import MaxPooling2D
from tensorflow.python.keras.layers import Activation
from tensorflow.python.keras.layers import Flatten
from tensorflow.python.keras.layers import Dropout
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.regularizers import l2
from tensorflow.python.keras import backend as K

class AlexNet:
    @staticmethod
    def build(width, height, depth, classes, reg=0.0002):
        # 初始化模型
        model = Sequential()
        inputShape = (height, width, depth)
        chanDim = -1

        if K.image_data_format() == "channel_first":
            inputShape = (depth, height, width)
            chanDim = -1

        # 定义网路中的第一个层
        model.add(Conv2D(96, (11, 11), strides=(4, 4), input_shape=inputShape, padding="same", kernel_regularizer=l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Dropout(0.25))

        # Block #2: second CONV => RELU => POOL layer set
        model.add(Conv2D(256, (5, 5), padding="same",
        kernel_regularizer = l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
        model.add(Dropout(0.25))

        # Block #3: CONV => RELU => CONV => RELU => CONV => RELU
        model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer = l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(384, (3, 3), padding="same",
        kernel_regularizer = l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(256, (3, 3), padding="same",
        kernel_regularizer = l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
        model.add(Dropout(0.25))

        # Block #4: first set of FC => RELU layers
        model.add(Flatten())
        model.add(Dense(4096, kernel_regularizer=l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dropout(0.5))
        # Block #5: second set of FC => RELU layers
        model.add(Dense(4096, kernel_regularizer=l2(reg)))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dropout(0.5))

        # softmax classifier
        model.add(Dense(classes, kernel_regularizer=l2(reg)))
        model.add(Activation("softmax"))
        # return the constructed network architecture
        return model


#!/usr/bin/python3
"""Rectangle Class"""

from models.base import Base

class Rectangle(Base):
    """Rectangle class inherits from Base"""
    def __init__(self, width, height, x=0, y=0, id=None):
        """Init method for Rectangle"""
        self.width = width
        self.height = height
        self.x = x
        self.y = y
        super().__init__(id)

    @property
    def width(self):
        """Return Width"""
        return self.__width

    @width.setter
    def width(self, value):
        """Set width and check errors"""
        if type(value) is not int:
            raise TypeError('width must be an integer')
        if value <= 0:
            raise ValueError('width must be > 0')
        self.__width = value

    @property
    def x(self):
        """Return x"""
        return self.__x

    @x.setter
    def x(self, value):
        """Set x and check errors"""
        if type(value) is not int:
            raise TypeError('x must be an integer')
        if value < 0:
            raise ValueError('x must be >= 0')
        self.__x = value

    @property
    def y(self):
        """Return y"""
        return self.__y

    @y.setter
    def y(self, value):
        """Set y and check errors"""
        if type(value) is not int:
            raise TypeError('y must be an integer')
        if value < 0:
            raise ValueError('y must be >= 0')
        self.__y = value

    @property
    def height(self):
        """Return height"""
        return self.__height

    @height.setter
    def height(self, value):
        """Set height and check errors"""
        if type(value) is not int:
            raise TypeError('height must be an integer')
        if value <= 0:
            raise ValueError('height must be > 0')
        self.__height = value

    def area(self):
        """Return area"""
        return self.__height * self.__width

    def display(self):
        """Print rectangle with # and using x, y"""
        print("\n" * self.__y, end="")
        for i in range(self.__height):
            print(" " * self.__x, end="")
            print("#" * self.__width)

    def __str__(self):
        """Str method"""
        m = "[Rectangle] ({}) {}/{} - {}/{}"
        m = m.format(self.id, self.__x, self.__y, self.__width, self.__height)
        return m

    def update(self, *args, **kwargs):
        """Assigns and arg with key to atributte"""
        my_list = [None, None, None, None, None]

        for i in range(len(args)):
            my_list[i] = args[i]

        if my_list[0] is not None:
            self.id = args[0]
        elif 'id' in kwargs:
            self.id = kwargs['id']
        if my_list[1] is not None:
            self.__width = args[1]
        elif 'width' in kwargs:
            self.__width = kwargs['width']
        if my_list[2] is not None:
            self.__height = args[2]
        elif 'height' in kwargs:
            self.__height = kwargs['height']
        if my_list[3] is not None:
            self.__x = args[3]
        elif 'x' in kwargs:
            self.__x = kwargs['x']
        if my_list[4] is not None:
            self.__y = args[4]
        elif 'y' in kwargs:
            self.__y = kwargs['y']

    def to_dictionary(self):
        """
        Return the dictionary representation
        of Rectangle
        """
        new_dict = {}

        for key in self.__dict__.keys():
            if 'width' in key:
                new_dict['width'] = self.__dict__[key]
            elif 'height' in key:
                new_dict['height'] = self.__dict__[key]
            elif 'x' in key:
                new_dict['x'] = self.__dict__[key]
            elif 'y' in key:
                new_dict['y'] = self.__dict__[key]
            elif 'id' in key:
                new_dict['id'] = self.__dict__[key]
        return new_dict


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def local_contrast_norm(image,radius=9):
    """
    image: torch.Tensor , .shape => (1,channels,height,width)

    radius: Gaussian filter size (int), odd
    """
    if radius%2 == 0:
        radius += 1
    def get_gaussian_filter(kernel_shape):
        x = np.zeros(kernel_shape, dtype='float64')

        def gauss(x, y, sigma=2.0):
            Z = 2 * np.pi * sigma ** 2
            return  1. / Z * np.exp(-(x ** 2 + y ** 2) / (2. * sigma ** 2))

        mid = np.floor(kernel_shape[-1] / 2.)
        for kernel_idx in range(0, kernel_shape[1]):
            for i in range(0, kernel_shape[2]):
                for j in range(0, kernel_shape[3]):
                    x[0, kernel_idx, i, j] = gauss(i - mid, j - mid)

        return x / np.sum(x)

    n,c,h,w = image.shape[0],image.shape[1],image.shape[2],image.shape[3]

    gaussian_filter = torch.Tensor(get_gaussian_filter((1,c,radius,radius))).to(device)
    filtered_out = F.conv2d(image,gaussian_filter,padding=radius-1)
    mid = int(np.floor(gaussian_filter.shape[2] / 2.))
    ### Subtractive Normalization
    centered_image = image - filtered_out[:,:,mid:-mid,mid:-mid]

    ## Variance Calc
    sum_sqr_image = F.conv2d(centered_image.pow(2),gaussian_filter,padding=radius-1)
    s_deviation = sum_sqr_image[:,:,mid:-mid,mid:-mid].sqrt()
    per_img_mean = s_deviation.mean()

    ## Divisive Normalization
    divisor = np.maximum(per_img_mean.cpu().detach().numpy(),s_deviation.cpu().detach().numpy())
    divisor = np.maximum(divisor, 1e-4)
    new_image = centered_image / torch.Tensor(divisor).to(device)
    return new_image

class LocalContrastNorm(nn.Module):
    def __init__(self, radius = 9):
        super(LocalContrastNorm, self).__init__()
        self.radius = radius

    def forward(self, x):
        x = local_contrast_norm(x, radius = self.radius)
        return x


import pm4py

def execute_script():
    log = pm4py.read_xes("../tests/input_data/receipt.xes")
    var_paths_durs = pm4py.get_variants_paths_duration(log)
    print(var_paths_durs.columns)

    # gets for each variant the average times (between all the cases of the given variant)
    # between the activities 'Confirmation of receipt' and 'T02 Check confirmation of receipt'
    df1 = var_paths_durs[(var_paths_durs["concept:name"] == "Confirmation of receipt") & (var_paths_durs["concept:name_2"] == "T02 Check confirmation of receipt")]
    print(df1[["@@variant_column", "@@flow_time"]])

    # gets the paths which are repeated in some variants, along with the number of variants for which they are repeated
    df2 = var_paths_durs[var_paths_durs["@@cumulative_occ_path_column"] == 1]
    print(df2.groupby(["concept:name", "concept:name_2"]).size())

if __name__ == "__main__":
    execute_script()


import numpy as np
import torch

import argparse
import utils.texture_synthesis_tools as tools
import nets.vgg16_texture as net

parser = argparse.ArgumentParser()
parser.add_argument("--input_width", type=int, default=256, help="width of input image")
parser.add_argument("--input_height", type=int, default=256, help="height of input image")
parser.add_argument("--input_file", type=str, default="./data/texture_data/earth.jpg", help="input ground truth image url")
parser.add_argument("--output_file", type=str, default="./Texture_processed.jpg", help="output ground truth processed image name")
parser.add_argument("--output_path", type=str, default="./data/texture_data", help="output synthesised image url")
parser.add_argument("--epochs", type=int, default=10000, help="numbers epochs of training")
parser.add_argument("--lr", type=float, default="0.001", help="adam: learning rate")
parser.add_argument("--output_dir", type=str, default="./results/Texture_opt/", help="Output directory of texture synthesis")
parser.add_argument("--before_opt_filename", type=str, default="Texture_0_before.jpg", help="Output file name before texture synthesis")
parser.add_argument("--after_opt_filename", type=str, default="Texture_0_after.jpg", help="Output file name after texture synthesis")
opt = parser.parse_args()

# Training process of texture synthesis

def loss_func(m, texture_opt, noise_opt):
    # This function takes initial ground truth texture image and noise data
    # Output the loss for a given layer by using Gram matrix
    loss_ = 0
    for i in range(len(m)):
        texture_filters = torch.squeeze(texture_opt[m[i][0]], 0)
        texture_filters_ = torch.reshape(texture_filters, shape=(
        texture_filters.shape[1] * texture_filters.shape[2], texture_filters.shape[0]))

        #Gram Matrix computation
        gram_matrix_texture = torch.matmul(texture_filters_.T, texture_filters_)
        noise_filters = torch.squeeze(noise_opt[m[i][0]], 0)
        noise_filters_= torch.reshape(noise_filters,
                                   shape=(noise_filters.shape[1] * noise_filters.shape[2], noise_filters.shape[0]))
        gram_matrix_noise = torch.matmul(torch.transpose(noise_filters_,1,0), noise_filters_)
        denominator = (4 * torch.tensor(texture_filters_.shape[1]) * torch.tensor(
            texture_filters_.shape[0]))

        # Calculate loss function of all layers
        loss = m[i][1] * (torch.sum(torch.square(torch.subtract(gram_matrix_texture, gram_matrix_noise))) / denominator.float())
        loss_ = loss_ + loss
    return loss_

def texture_train(input_filename, processed_path, processed_filename, m, eps, op_dir, initial_filename, final_filename):

    # Load vgg16 net
    vgg16 = net.Vgg16()
    if cuda:
        vgg16 = vgg16.cuda()

    # Read and pre-process training image
    texture_data = tools.resize_img(input_filename, opt.input_width, opt.input_height, processed_path,processed_filename, device)

    texture_outputs = tools.compute_layer_output(texture_data, vgg16)

    # data normalizaiton parameters
    # Generate random initial noise data

    random = np.random.rand(texture_data.shape[0],texture_data.shape[1],texture_data.shape[2],texture_data.shape[3])
    # normalization
    random[:, 0, :, :] = random[:, 0, :, :] - mean[0]
    random[:, 1, :, :] = random[:, 1, :, :] - mean[1]
    random[:, 2, :, :] = random[:, 2, :, :] - mean[2]
    random = Tensor(random)
    # Feed to vgg16
    vgg16.forward(random)

    # Optimizer
    optimizer = torch.optim.Adam([random.requires_grad_()], lr=opt.lr)

    for i in range(eps):
        vgg16.forward(random)
        noise_layers_list = dict(
            {0: vgg16.conv1_1, 1: vgg16.conv1_2, 2: vgg16.pool1, 3: vgg16.conv2_1, 4: vgg16.conv2_2, 5: vgg16.pool2,
             6: vgg16.conv3_1,
             7: vgg16.conv3_2,
             8: vgg16.conv3_3, 9: vgg16.pool3, 10: vgg16.conv4_1, 11: vgg16.conv4_2, 12: vgg16.conv4_3, 13: vgg16.pool4,
             14: vgg16.conv5_1, 15: vgg16.conv5_2,
             16: vgg16.conv5_3, 17: vgg16.pool5})
        loss = loss_func(m, texture_outputs, noise_layers_list)
        optimizer.zero_grad()
        loss.backward(retain_graph=True)
        optimizer.step()
        print("Epoch:{}/{} Loss:{}.".format(i,eps,loss))

        if(i%499 == 0):
            final_noise = random
            final_filename_ = final_filename+str(i)+".jpg"
            final_noise_ = tools.post_process_and_display(final_noise, op_dir, final_filename_,input_file = 0)
            tools.match_his_output(processed_path+processed_filename, final_noise_,opt.output_path)
    #initial_noise = tools.post_process_and_display(random, op_dir, initial_filename,save_file=True)
    return final_noise_

# if gpu available
cuda = True if torch.cuda.is_available() else False
Tensor = torch.cuda.FloatTensor if cuda else torch.Tensor
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# VGG RGB normalization colour value
mean = [0.5, 0.5, 0.5]
# processing layers
m = [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1)]

# Start training
texture_train(opt.input_file,opt.output_path,opt.output_file,m, opt.epochs,opt.output_dir,
              opt.before_opt_filename,opt.after_opt_filename)


import torch
import torch.nn as nn  # Building the model

class Net(nn.Module):
    def __init__(self, in_channels=3):
        super().__init__()

        self.normalize = nn.BatchNorm2d(in_channels)  # Layer will be frozen without learnable parameters
        self.normalize.reset_parameters()
        self.normalize.eval()

        self.set_normalization()

    def set_normalization(self, mean=[0.0, 0.0, 0.0], std=[1.0, 1.0, 1.0]):
        self.normalize.weight.requires_grad = False  # gamma
        self.normalize.bias.requires_grad = False   # beta
        self.normalize.running_mean.requires_grad = False  # stat, mean
        self.normalize.running_var.requires_grad = False  # stat, variance

        self.normalize.weight = torch.nn.Parameter( torch.ones(3), requires_grad=False )
        self.normalize.bias = torch.nn.Parameter( torch.zeros(3), requires_grad=False )
        self.normalize.running_mean = torch.nn.Parameter( torch.tensor(mean), requires_grad=False )
        self.normalize.running_var = torch.nn.Parameter( torch.tensor([x**2 for x in std]), requires_grad=False )

        print("\nweight :", self.normalize.weight)
        print("bias :", self.normalize.bias)
        print("running_mean :", self.normalize.running_mean)
        print("running_var :", self.normalize.running_var)

    def forward(self, x):
        print("pre :", x)
        x = self.normalize(x)
        print("post :", x)
        return x

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

input_size = 2
in_channels = 3

model = Net(in_channels)

model.set_normalization(mean=[0.5, 0.4, 0.3], std=[0.1, 0.2, 0.3])

x = torch.rand((1, in_channels, input_size, input_size)).to(device)

model = model.to(device)

y = model(x)


# soundtrigger.py

from twilio.rest import Client
from config import *

def SoundDetection(text):
    config = load_json('config.json')
    trigger_words_values = config['user_made_sounds']['trigger_words'].values()
    sound_pattern_values = config['user_made_sounds']['sound_pattern'].values()
    if any(value in text for value in trigger_words_values) or any(value in text for value in sound_pattern_values):
        return 1
    elif "stop" in text:
        return 2
    else:
        return 0

# This code is adapted from https://www.twilio.com/docs/libraries/python
def SoundTriggering(signal):
    if signal == 1:
        twilio_details = load_json('twilio_account_details.json')
        account_sid = twilio_details['account_sid']
        auth_token = twilio_details['auth_token']
        twilio_number = twilio_details['twilio_number']
        my_phone_number = twilio_details['my_phone_number']

        client = Client(account_sid, auth_token)

        message = client.messages.create(
            body="I need help!",
            from_=twilio_number,
            to=my_phone_number
        )
        print(message)
    else:
        return
    return


import argparse
import torch.nn
from model import U2NET
from model import U2NETP

import onnx
import numpy as np
import onnxsim  # pip install onnx-simplifier
import onnxruntime as ort
import numpy as np

class Alpha_U2Net(torch.nn.Module):
    def __init__(self, weight_file):
        super().__init__()
        self.model = U2NET(3, 1)
        self.model.load_state_dict(torch.load(model_path, map_location='cpu'))
        self.model.eval()

    def forward(self, x):
        y = self.model(x)
        return y[0]

class Alpha_U2Netp(torch.nn.Module):
    def __init__(self, weight_file):
        super().__init__()
        self.model = U2NETP(3, 1)
        self.model.load_state_dict(torch.load(model_path, map_location='cpu'))
        self.model.eval()

    def forward(self, x):
        y = self.model(x)
        return y[0]
"""
example:
python alpha_export.py --net=u2net --weights=saved_models/u2net/u2net.pth
python alpha_export.py --net=u2netp --weights=saved_models/u2netp/u2netp.pth
"""
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--net', type=str, default='u2net', help='net type')
    parser.add_argument('--weights', type=str, default='saved_models/u2net/u2net.pth', help='net path')
    opt = parser.parse_args()

    net = ''
    image_input_shape = [1, 3, 320, 320]
    image_input = torch.autograd.Variable(torch.randn(image_input_shape))
    input_names = ["images"]
    output_names = ["output"]
    dynamic_axes = {"images": {0: "batch_size"}, "output": {0: "batch_size"}}

    net = opt.net
    if net=='u2net':  # for u2net.pt
        net_name = "u2net"
        onnx_name = net_name + ".onnx"
        model_path = opt.weights
        # model = U2NET(3, 1)
        # model.load_state_dict(torch.load(model_path, map_location='cpu'))
        # model.eval()
        u2net = Alpha_U2Net(model_path)
        torch.onnx.export(u2net, image_input, "saved_models/onnx/" + onnx_name,
                        verbose=True,
                        input_names=input_names,
                        output_names=output_names,
                        opset_version=11,  # try  opset_version=9
                        training=False,
                        dynamic_axes=dynamic_axes)
        # # simplify onnx
        # print("-------------------------- simplify onnx --------------------------")
        # onnx_model = onnx.load("saved_models/onnx/" + onnx_name)
        # # model_simp, check = onnxsim.simplify(onnx_model)
        # model_simp, check = onnxsim.simplify(onnx_model,
        #     check_n=0,
        #     input_shapes={'images':[-1, 3, 320, 320]},

        #     dynamic_input_shape=True)

        # assert check, "Simplified ONNX model could not be validated"
        # onnx_simplify_name = net_name + "_simplify.onnx"
        # onnx.save(model_simp, "saved_models/onnx/" + onnx_simplify_name)
        # # check onnx
        # print("-------------------------- check onnx --------------------------")
        # onnx_simplify = onnx.load("saved_models/onnx/" + onnx_simplify_name)
        # onnx.checker.check_model(onnx_simplify)
        # print(onnx.helper.printable_graph(onnx_simplify.graph))

        # print("-------------------------- inference onnx --------------------------")
        # outputs = infer_onnx("saved_models/onnx/" + onnx_name, input_names, image_input_shape)
        # print(outputs[0][0][0][0][0:10])

        # print("-------------------------- inference onnx(simplify) --------------------------")
        # outputs_simp = infer_onnx("saved_models/onnx/" + onnx_simplify_name, input_names, image_input_shape)
        # print(outputs[0][0][0][0][0:10])

    elif net=='u2netp':  # for u2netp.pt
        model_path = opt.weights
        # model = U2NETP(3, 1)
        # model.load_state_dict(torch.load(model_path, map_location='cpu'))
        # model.eval()
        u2netp = Alpha_U2Netp(model_path)
        torch.onnx.export(u2netp, image_input, "saved_models/onnx/u2netp.onnx",
                        verbose=True,
                        input_names=input_names,
                        output_names=output_names,
                        opset_version=11,
                        training=False,
                        dynamic_axes=dynamic_axes)

    print()


import functools
import logging

from cachetools import LFUCache, LRUCache, RRCache, TTLCache
import cachetools.keys as keys

from db_hooks.errors import UnrecognizedCacheImplementationError

logger = logging.getLogger(__name__)

CACHE_CLS_BY_NAME = {
    cls.__name__: cls for cls in [LFUCache, LRUCache, RRCache, TTLCache]
}

def create_cache(cache_config):
    if cache_config.type not in CACHE_CLS_BY_NAME:
        raise UnrecognizedCacheImplementationError(cache_config.type, CACHE_CLS_BY_NAME)

    logger.info(
        "Creating connection cache of type {}({})...".format(
            cache_config.type, cache_config.kwargs
        )
    )

    cls = CACHE_CLS_BY_NAME[cache_config.type]
    return cls(**cache_config.kwargs)

class CacheManager:
    def __init__(self, key=keys.hashkey, enabled=True):
        self._cache = None
        self._enabled = enabled
        self._key = key

    def set_cache(self, cache):
        logger.info("Cache set to {}.".format(cache))
        self._cache = cache

    def has_cache(self):
        return self._cache is not None

    def clear_cache(self):
        logger.info("Cache has been cleared.")
        self._cache = None

    def disable_cache(self):
        logger.info("Cache has been disabled.")
        self._enabled = False

    def enable_cache(self):
        logger.info("Cache has been enabled.")
        self._enabled = True

    def cached(self, fn):
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            if not self._enabled:
                return fn(*args, **kwargs)

            k = self._key(*args, **kwargs)

            if self._cache:
                try:
                    return self._cache[k]
                except KeyError:
                    pass

            v = fn(*args, **kwargs)

            if not self._cache:
                return v

            try:
                self._cache[k] = v
            except ValueError:
                pass

            return v

        return wrapper


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

import unittest
from uiautomatorplug.android import device as d

class SystemAppTest(unittest.TestCase):
    def setUp(self):
        """
        called before  each test method start.
        """
        d.wakeup()
        for i in xrange(3): d.press('back')
        d.press('home')
        d.press('home')
        for i in xrange(8): d.press('left')

    def tearDown(self):
        """
        called after each test method end or exception occur.
        """
        for i in xrange(8): d.press('back')
        d.press('home')
        d.press('home')

    def testLaunchAndExitAppStore(self):
        """
        launch  app store and exit
       """
        assert d(text="应用商店").exists, 'App Store icon not found!'
        d(text="应用商店").sibling(className='android.view.View').click.wait()
        assert d(resourceId='com.xiaomi.mitv.appstore:id/tv_tab_recommend', text='推荐').wait.exists(timeout=10000), 'launch App Store failed!'
        d.press('back')
        assert d(text="应用商店").wait.exists(timeout=10000), 'exit from App Store failed!'
        #d(resourceId="com.xiaomi.mitv.appstore:id/title_chinese_textview",text='应 用商店').wait.exists()

    def testLaunchAndExitCloudGallery(self):
        """
        launch  app store and exit
       """
        for i in xrange(8):
            d.press('right')
        assert d(text="云相册").exists, 'Cloud Gallery icon not found!'
        d(text="云相册").click.wait()
        #gallery_check_png =  os.path.join(PIC_DIR, 'cloudgallery_sub_1.png')
        d.expect('cloudgallery_sub_1.png', timeout=20000)
        d.press('back')
        assert d(text="云相册").wait.exists(timeout=10000), 'exit from cloud gallery failed!'
        #d(resourceId="com.xiaomi.mitv.appstore:id/title_chinese_textview",text='应 用商店').wait.exists()

    def  testLaunchAndExitFM(self):
        for i in xrange(8):
            d.press('right')
        assert d(text="网络电台").exists, 'FM app icon not found!'
        d(text="网络电台").click.wait()
        assert d(resourceId='com.xiaomi.mimusic:id/play_btn').child(className='android.widget.ImageView').wait.exists(timeout=10000), 'Open FM failed!'
        d.sleep(10)
        d.press('back')
        d.sleep(2)
        d.press('back')
        assert d(text="网络电台").wait.exists(timeout=10000), 'exit from FM failed!'

    def  testLaunchAndExitVideo(self):
        assert d(text="在线影视").exists, 'Online Video icon not found!'
        d(text="在线影视").sibling(className='android.view.View').click.wait()
        #menu_hot_highlight_focus_png =  os.path.join(PIC_DIR, 'menu_hot_highlight_focus.png')
        #menu_hot_highlight_nofocus_png  =  os.path.join(PIC_DIR, 'menu_hot_highlight_nofocus.png')
        if  d.find('menu_hot_highlight_focus.png', timeout=50) :
            pass
        elif  d.find('menu_hot_highlight_nofocus.png', timeout=50):
            pass
        else:
            assert False, 'open online video failed!'
        d.press('back')
        assert d(text="在线影视").wait.exists(timeout=10000), 'exit from online video failed!'

    def testLaunchAndExitGameCenter(self):
        """
        launch  app store and exit
       """
        assert d(text="游戏中心").exists, 'Game Center icon not found!'
        d(text="游戏中心").sibling(className='android.view.View').click.wait()
        #assert d(className='android.widget.FrameLayout').child(text="热门精选").wait.exists(timeout=20000), 'Launch Game Center failed!'
        assert d(resourceId='android:id/content').child(text="推荐").wait.exists(timeout=20000), 'Launch Game Center failed!'
        d.press('home')
        assert d(text="游戏中心").wait.exists(timeout=10000), 'exit from Game Center failed!'

    def testLaunchAndExitSetting(self):
        """
        launch  app store and exit
       """
        for i in xrange(4):
            d.press('right')
        assert d(text="小米电视设置").exists, 'MiTv Setting icon not found!'
        d(text="小米电视设置").click.wait()
        assert d(text="帐户与安全").wait.exists(timeout=5000), 'launch MiTv setting failed!'
        assert d(text="图像与声音").wait.exists(timeout=5000), 'launch MiTv setting failed!'
        assert d(text="关于").wait.exists(timeout=5000), 'open MiTv setting failed!'
        d.press('back')
        assert d(text="小米电视设置").wait.exists(timeout=10000), 'exit from MiTv Setting failed!'

    def testLaunchAndExitMiracast(self):
        for i in xrange(4):
            d.press('right')
        assert d(text="无线显示").exists, 'Miracast icon not found!'
        d(text="无线显示").click.wait()
        d.press('back')
        assert d(text="无线显示").wait.exists(timeout=10000), 'exit from Miracast failed!'

    def testLaunchAndExitMediaExplorer(self):
        for i in xrange(6):
            d.press('right')
        assert d(text="高清播放器").exists, 'Media Explorer icon not found!'
        d(text="高清播放器").click.wait()
        assert d(resourceId='com.xiaomi.mitv.mediaexplorer:id/dev', text='设备').wait.exists(timeout=5000), 'launch Media Explorer failed!'
        assert d(resourceId='com.xiaomi.mitv.mediaexplorer:id/video', text="视频").wait.exists(timeout=5000), 'launch Media Explorer failed!'
        d.press('back')
        d.sleep(3)
        d.press('back')
        assert d(text="高清播放器").wait.exists(timeout=10000), 'exit from Media Explorer failed!'

    def testWifiOpenAndClose(self):
        d.start_activity('--activity-clear-task', component='com.android.settings/.Settings')
        d(text="无线和网络").wait.exists(timeout=2000)
        #d(className="android.widget.ListView", resourceId="android:id/list").child(text='WLAN').click.wait()
        #d(text="WLAN").click.wait()
        d.press('enter')
        d(resourceId='android:id/action_bar').child(text='WLAN').wait.exists(timeout=3000)
        if d(className='android.widget.Switch', text='打开').exists:
            d.press('right')
            d.sleep(2)
            d.press('up')
            d.sleep(2)
            for i in xrange(10):
                d.press('enter')
                assert d(resourceId='android:id/action_bar').child(text='关闭').wait.exists(timeout=3000), 'wifi close failed!'
                d.press('enter')
                assert d(resourceId='android:id/action_bar').child(text='打开').wait.exists(timeout=3000), 'wifi open failed!'
                assert d(resourceId='android:id/list').child(text='已连接').wait.exists(timeout=20000), 'wifi connect failed!'
        elif d(className='android.widget.Switch', text='关闭').exists:
            d.press('right')
            d.sleep(2)
            d.press('up')
            d.sleep(2)
            for i in xrange(10):
                d.press('enter')
                assert d(resourceId='android:id/action_bar').child(text='打开').wait.exists(timeout=3000), 'wifi close failed!'
                assert d(resourceId='android:id/list').child(text='已连接').wait.exists(timeout=20000), 'wifi connect failed!'
                d.press('enter')
                assert d(resourceId='android:id/action_bar').child(text='打开').wait.exists(timeout=3000), 'wifi open failed!'


"""
Problem Description
You are given a 2D integer matrix A, make all the elements in a row or column zero if the A[i][j] = 0. Specifically, make entire ith row and jth column zero.

Problem Constraints
1 <= A.size() <= 103
1 <= A[i].size() <= 103
0 <= A[i][j] <= 103

Input Format
First argument is a vector of vector of integers.(2D matrix).

Output Format
Return a vector of vector after doing required operations.

Example Input
Input 1:
[1,2,3,4]
[5,6,7,0]
[9,2,0,4]

Example Output
Output 1:
[1,2,0,0]
[0,0,0,0]
[0,0,0,0]

Example Explanation
Explanation 1:
A[2][4] = A[3][3] = 0, so make 2nd row, 3rd row, 3rd column and 4th column zero.

"""
A = [[1,2,3,4],
     [5,6,7,0],
     [9,2,0,4]]
def solve(self, A):
    rows = len(A)
    columns = len(A[0])
    rowsArray = []
    columnsArray = []
    for i in range(0, rows * columns):
        row = i // columns
        col = i % columns
        if A[row][col] == 0:
            rowsArray.append(row)
            columnsArray.append(col)
    for i in range(len(A)):
        for j in range(len(columnsArray)):
            value = columnsArray[j]
            A[i][value] = 0
    for i in range(len(A[0])):
        for j in range(len(rowsArray)):
            value = rowsArray[j]
            A[value][i] = 0
    print(A)
solve("data", A)


# Databricks notebook source
from pyspark import SparkConf, SparkContext
conf = SparkConf().setAppName('flatmap')
sc = SparkContext.getOrCreate(conf=conf)
rdd = sc.textFile('./FileStore/tables/sample.txt')

# COMMAND ----------

print(rdd.collect())

# COMMAND ----------

rdd2 = rdd.filter(lambda x: x != '1 8 3 48')
print(rdd2.collect())

# Out[4]: ['1 15 8 4', '8 6 44 2', '57 1 44 5']

# COMMAND ----------

def fltr(x):
    if sum([int(el) for el in x.split(' ')]) <= 28:
        return False
    else:
        return True

def fltr2(x):
    return False if sum([int(el) for el in x.split(' ')]) <= 28 else True

rdd3 = rdd.filter(fltr)
print(rdd3.collect())

# Out[30]: ['8 6 44 2', '1 8 3 48', '57 1 44 5']

# COMMAND ----------


import os
while True:
    command = input()
    if command == 'End':
        break
    tokens = command.split('-')
    action = tokens[0]
    if action == 'Create':
        f_name = tokens[1]
        with open(f_name, 'w') as fh:
            pass
    elif action == 'Add':
        f_name = tokens[1]
        content = f'{tokens[2]}\n'
        with open(f_name, 'a') as fh:
            fh.write(content)
    elif action == 'Replace':
        f_name = tokens[1]
        old = tokens[2]
        new = tokens[3]
        try:
            with open(f_name, 'a') as fh:
                f_name.replace(old, new)
        except FileNotFoundError:
            print('An error occurred')
    elif action == 'Delete':
        f_name = tokens[1]
        if os.path.exists(f_name):
            os.remove(f_name)
        else:
            print('An error occurred')

File Handling Exerscise/3. File Manipulator.py


from __future__ import print_function
import os.path
import pytz
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import datetime
import speech
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
MONTHS = ["january","february","march","april","may","june","july","august","september","october","november","december"]
DAYS = ["monday","tuesday", "wednesday", "thursday", "friday","saturday","sunday"]
DAY_EXT = ["rd", "th", "st", "nd"]

"""
Look up the Google Calendar API and follow the steps to set it up
"""

def authenticate(): #Authenticating the user's Google Calendar using Calendar API.
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('calendar', 'v3', credentials=creds)
    return service

def get_events(day, service): #Accesses and tells the event the user has on the particular day
    date = datetime.datetime.combine(day, datetime.datetime.min.time())
    end_date = datetime.datetime.combine(day, datetime.datetime.max.time())
    utc = pytz.UTC
    date = date.astimezone(utc)
    end_date = end_date.astimezone(utc)

    events_result = service.events().list(calendarId='primary', timeMin=date.isoformat(), timeMax=end_date.isoformat(),
                                        singleEvents=True,
                                        orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        speech.speech('No upcoming events found.')
    else:
        speech.speech(f"You have {len(events)} events on this day.")

        for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            print(start, event['summary'])
            start_time = str(start.split("T")[1].split("-")[0])  # get the hour the event starts
            if int(start_time.split(":")[0]) < 12:  # if the event is in the morning
                start_time = start_time = str(int(start_time.split(":")[0])) + str(int(start_time.split(":")[1])) +  "a.m."
            else:
                start_time = str(int(start_time.split(":")[0])-12) + start_time.split(":")[1]  # convert 24 hour time to regular
                start_time = start_time + "pm"

            speech.speech(event["summary"] + " at " + start_time)

def date(text):
    text = text.lower()
    today = datetime.date.today()

    if text.count("today") > 0:
        return today
    day=-1
    day_week = -1
    month = -1
    year = today.year

    for word in text.split():
        if word in MONTHS:
            month = MONTHS.index(word)+1
        elif word in DAYS:
            day_week = DAYS.index(word)
        elif word.isdigit():
            day = int(word)
        else:
            for ext in DAY_EXT:
                found = word.find(ext)
                if found>0:
                    try:
                        day = int(word[:found])
                    except:
                        pass

    if month < today.month and month != -1:
        year = year+1
    if day < today.day and month == -1 and day != -1:
        month = month+1
    if month == -1 and day == -1 and day_week != -1:
        current_day_week = today.weekday()
        diff = day_week - current_day_week
        if diff < 0:
            diff+=7
            if text.count("next")>= 1:
                diff+=7
        return today+ datetime.timedelta(diff)
    return datetime.date(month=month, day = day, year=year)


def test():
	w = world()
	print(w)
	w.setGravity( vec3(0,-9.81,0) )
	b = body( w )
	m = mass()
	m.setSphere( 2500.0, 0.05 )
	b.setMass( m )
	b.setPosition( vec3(0,2,0) )
	b.addForce( vec3(0, 200, 0) )
	b.addTorque( vec3(10,0,0) )
	b.setRotation( quat(0.5, 0.5, 0, 0) )
	time = 0.0
	dt = 0.04
	while time < 0.5:
		print('simulating....')
		w.step(dt)
		v = b.getPosition()
		print(v)
		if v[1] < 0.0:
			print("hit ground")
			b.addForce( vec3(0,1000,0) )
		q = b.getRotation()
		print(q)
		print('linear vel:')
		vel = b.getLinearVel()
		print( vel )
		print('angular vel:')
		avel = b.getAngularVel()
		print( avel )
		time += dt

def main():
	test()

main()


from __future__ import unicode_literals

import errno
import io
import json
import os
import re
import shutil
import traceback

from .compat import compat_getenv
from .utils import (
    expand_path,
    write_json_file,
)

class Cache(object):
    def __init__(self, ydl):
        self._ydl = ydl

    def _get_root_dir(self):
        res = self._ydl.params.get('cachedir')
        if res is None:
            cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
            res = os.path.join(cache_root, 'youtube-dlc')
        return expand_path(res)

    def _get_cache_fn(self, section, key, dtype):
        assert re.match(r'^[a-zA-Z0-9_.-]+$', section), \
            'invalid section %r' % section
        assert re.match(r'^[a-zA-Z0-9_.-]+$', key), 'invalid key %r' % key
        return os.path.join(
            self._get_root_dir(), section, '%s.%s' % (key, dtype))

    @property
    def enabled(self):
        return self._ydl.params.get('cachedir') is not False

    def store(self, section, key, data, dtype='json'):
        assert dtype in ('json',)

        if not self.enabled:
            return

        fn = self._get_cache_fn(section, key, dtype)
        try:
            try:
                os.makedirs(os.path.dirname(fn))
            except OSError as ose:
                if ose.errno != errno.EEXIST:
                    raise
            write_json_file(data, fn)
        except Exception:
            tb = traceback.format_exc()
            self._ydl.report_warning(
                'Writing cache to %r failed: %s' % (fn, tb))

    def load(self, section, key, dtype='json', default=None):
        assert dtype in ('json',)

        if not self.enabled:
            return default

        cache_fn = self._get_cache_fn(section, key, dtype)
        try:
            try:
                with io.open(cache_fn, 'r', encoding='utf-8') as cachef:
                    return json.load(cachef)
            except ValueError:
                try:
                    file_size = os.path.getsize(cache_fn)
                except (OSError, IOError) as oe:
                    file_size = str(oe)
                self._ydl.report_warning(
                    'Cache retrieval from %s failed (%s)' % (cache_fn, file_size))
        except IOError:
            pass  # No cache available

        return default

    def remove(self):
        if not self.enabled:
            self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)')
            return

        cachedir = self._get_root_dir()
        if not any((term in cachedir) for term in ('cache', 'tmp')):
            raise Exception('Not removing directory %s - this does not look like a cache dir' % cachedir)

        self._ydl.to_screen(
            'Removing cache dir %s .' % cachedir, skip_eol=True)
        if os.path.exists(cachedir):
            self._ydl.to_screen('.', skip_eol=True)
            shutil.rmtree(cachedir)
        self._ydl.to_screen('.')


from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
import Expert
import pprint
import sys

expert = None
ans_list = None

class UpWidget(QWebEngineView):
    '''结果框部件
    '''
    def __init__(self, father):
        super().__init__(father)

        with open('./css/up.html', 'r') as f:
            self.html_head = f.read()
        self.html_tail = ''

        self.html_append = '''

                                            {name}

                                            身高：{height} m
体重：{weight} kg

                                        {intro}

'''

        self.init_UI()

    def init_UI(self):
        self.setHtml( self.html_head
                    + self.html_append.format(url='https://media.52poke.com/wiki/a/a6/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%9B%BE%E9%89%B4_Pt.png',
                                              name='宝可梦图鉴',
                                              height='???',
                                              weight='???',
                                              intro='按工具栏搜索键 [开始/重新] 搜索；按记录键 [增添/覆盖] 宝可梦信息到图鉴中。')
                    + self.html_tail)

    def showPoke(self, url, name, height, weight, intro):
        url = "https://media.52poke.com/wiki/b/bb/Dream_%E8%83%86%E6%80%AF%E7%90%83_Sprite.png" if url is None else url
        height = "???" if height is None else str(height)
        weight = "???" if weight is None else str(weight)
        intro = "没有详细资料..." if intro is None or intro == '' else intro
        self.setHtml( self.html_head
                    + self.html_append.format(url=url, name=name, height=height, weight=weight, intro=intro)
                    + self.html_tail)

class DownWidget(QWebEngineView):
    '''询问框部件
    '''
    def __init__(self, father):
        super().__init__(father)

        with open('./css/down.html', 'r') as f:
            self.html_head = f.read()
        self.html_tail = '
'

        self.ques_head = '''

                                        '''
        self.ques_tail = '''

'''
        self.answer_head = '''

                                            '''
        self.answer_tail = '''

'''

        self.append_html = ''
        self.init_UI()

    def init_UI(self):
        self.append_html = ''
        self.setHtml(self.html_head + self.html_tail)

    def addAns(self, msg):
        self.append_html += self.answer_head + msg + self.answer_tail
        self.__refresh()

    def addQues(self, msg):
        self.append_html += self.ques_head + msg + self.ques_tail
        self.__refresh()

    def __refresh(self):
        self.setHtml(self.html_head + self.append_html + self.html_tail)

class MainWidget(QWidget):
    '''主部件

    负责安排各部件的布局
    '''
    def __init__(self, father):
        super().__init__(father)
        self.up_widget = UpWidget(self)
        self.down_widget = DownWidget(self)
        self.sld = QSlider(Qt.Horizontal, self)
        self.current_key = ''
        self.current_val = ''
        self.turn_num = 0
        self.isJudge = True
        self.isDone = True
        self.initUI()

    def clean_all(self):
        self.sld.setValue(0)
        self.turn_num = 0
        self.isJudge = False
        self.isDone = False

    def initUI(self):
        # 布局
        vbox = QVBoxLayout()
        vbox.addWidget(self.up_widget, stretch=5)
        vbox.addWidget(self.down_widget, stretch=5)

        hbox = QHBoxLayout()
        submitButton = QPushButton()
        submitButton.setStyleSheet( "QPushButton{width:25px; height:25px; border-image: url(./image/ok.ico)}"
                                    "QPushButton:hover{border-image: url(./image/ok_h.ico)}"
                                    "QPushButton:pressed{border-image: url(./image/ok_h.ico)}")
        submitButton.clicked.connect(self.submit)

        self.sld.setFocusPolicy(Qt.NoFocus)
        # self.sld.valueChanged[int].connect(self.changeValue)

        hbox.addStretch(1)
        hbox.addWidget(self.sld, stretch=12)
        hbox.setSpacing(20)
        hbox.addWidget(submitButton)
        hbox.addStretch(1)

        vbox.addLayout(hbox)
        self.setLayout(vbox)
        self.show()

    def submit(self):
        '''循环问答
        '''
        if self.isDone: # 任务结束状态
            return

        self.turn_num += 1
        val = self.sld.value()
        cf = val / 100 * 2 - 1
        print(f'用户确信度:{cf}')
        if self.isJudge: # 评判状态
            self.judge(cf)
            return

        global expert
        ans = expert.get_language_ans(cf)
        self.down_widget.addAns(ans)
        expert.update_pokemons_cf(self.current_key, self.current_val, cf)
        if not expert.enough(self.turn_num):
            next_ques, self.current_key, self.current_val = expert.get_next_question()
            self.down_widget.addQues(next_ques)
        else:
            global ans_list
            ans_list = expert.get_ans_list()
            self.down_widget.addQues('特征收集完毕')
            # 展示宝可梦
            self.refresh_ans()
            self.isJudge = True

    def refresh_ans(self):
        global ans_list
        # 展示宝可梦
        if ans_list is None or len(ans_list) == 0:
            self.down_widget.addQues("图鉴中没有这只宝可梦...快记录新精灵吧～")
            self.isDone = True
            return
        else:
            ans_cf, name, url, height, weight, intro = ans_list[0]
            self.up_widget.showPoke(url, name, height, weight, intro)
            self.display_track(name, ans_cf)
            self.down_widget.addQues('是这只宝可梦吗？')

    def display_track(self, name, cf):
        global expert
        trace_list = list()
        trace_list.append(f'{name} {round(cf, 3)}')
        for judgement, delta in expert.get_track_box(name):
            prefix = '+' if delta >= 0 else ''
            suffix = ''
            trace_list.append(f'{judgement}  {prefix}{round(delta, 3)}{suffix}')
        self.down_widget.addQues('
'.join(trace_list))

    def judge(self, cf):
        global ans_list
        if cf > 0:
            self.down_widget.addQues("搜索完成～")
            self.isDone = True
        elif ans_list is None or len(ans_list) <= 1:
            self.down_widget.addQues("图鉴中没有这只宝可梦...快记录新精灵吧～")
            self.isDone = True
        else:
            ans_list.pop(0)
            self.refresh_ans()

class MainWin(QMainWindow):
    '''主界面
    '''
    def __init__(self):
        super().__init__()
        # 初始化主窗口
        self.main_widget = MainWidget(self)
        self.setCentralWidget(self.main_widget)
        self.adder = AdderWidget()
        global expert
        expert = Expert.Expert()
        self.initUI()

    def initUI(self):
        # 基本信息
        self.setGeometry(480, 160, 345, 555)
        self.setWindowTitle('宝可梦图鉴')
        self.setStyleSheet("QMainWindow {background: #EF2929;}")

        # 重新搜索
        searchAct = QAction(QIcon('./image/search.ico'), 'Search a pokemon', self)
        searchAct.setShortcut('Ctrl+S')
        searchAct.triggered.connect(self.search)
        self.toolbar = self.addToolBar('Search a pokemon') # 直接退出
        self.toolbar.addAction(searchAct)

        # 增加图鉴
        addAct = QAction(QIcon('./image/add.ico'), 'Add a pokemon', self)
        addAct.setShortcut('Ctrl+A')
        addAct.triggered.connect(self.add)
        self.toolbar = self.addToolBar('Add a pokemon') # 直接退出
        self.toolbar.addAction(addAct)

        self.show()

    def search(self):
        # 清空界面 重置专家
        self.main_widget.up_widget.init_UI()
        self.main_widget.down_widget.init_UI()
        self.main_widget.clean_all()
        global expert, ans_list
        del expert
        expert = Expert.Expert()
        ans_list = None
        # 第一个问题
        ques, self.main_widget.current_key, self.main_widget.current_val = expert.get_next_question()
        self.main_widget.down_widget.addQues(ques)

    def add(self):
        self.adder.show()
        pass

class AdderWidget(QWidget):
    '''记录界面

    增添新宝可梦信息到数据库中
    '''
    def __init__(self):
        super().__init__()

        self.init_UI()

    def init_UI(self):

        self.setWindowTitle('Record New Pokemon')
        self.setGeometry(450, 260, 400, 350)
        self.setStyleSheet("AdderWidget{background-color:#404040;}")

        self.nameLine = QLineEdit(self)
        self.nameLine.setPlaceholderText('宝可梦名字')
        self.colorLine = QLineEdit(self)
        self.colorLine.setPlaceholderText('整体颜色(多种用空格隔开)')
        self.eyeLine = QLineEdit(self)
        self.eyeLine.setPlaceholderText('眼睛颜色(多种用空格隔开)')
        self.earLine = QLineEdit(self)
        self.earLine.setPlaceholderText('耳朵颜色(多种用空格隔开)')
        self.limbLine = QLineEdit(self)
        self.limbLine.setPlaceholderText('手和脚的个数')
        self.wingLine = QLineEdit(self)
        self.wingLine.setPlaceholderText('翅膀个数')
        self.habitatLine = QLineEdit(self)
        self.habitatLine.setPlaceholderText('栖息地(多种用空格隔开)')
        self.detailLine = QLineEdit(self)
        self.detailLine.setPlaceholderText('细节特点')
        self.superClassLine = QLineEdit(self)
        self.superClassLine.setPlaceholderText('所属类')
        self.urlLine = QLineEdit(self)
        self.urlLine.setPlaceholderText('图片源(网址)')
        self.introLine = QLineEdit(self)
        self.introLine.setPlaceholderText('简介')
        self.heightLine = QLineEdit(self)
        self.heightLine.setPlaceholderText('身高(m)')
        self.weightLine = QLineEdit(self)
        self.weightLine.setPlaceholderText('体重(kg)')
        self.Btn = QPushButton('写入', self)
        self.Btn.clicked.connect(self.writeInRepo)

        hbox1 = QHBoxLayout()
        hbox1.addWidget(self.nameLine)
        hbox1.addWidget(self.heightLine)
        hbox1.addWidget(self.weightLine)

        hbox2 = QHBoxLayout()
        hbox2.addWidget(self.limbLine)
        hbox2.addWidget(self.wingLine)
        hbox2.addWidget(self.superClassLine)

        hbox3 = QHBoxLayout()
        hbox3.addWidget(self.colorLine)
        hbox3.addWidget(self.habitatLine)

        hbox4 = QHBoxLayout()
        hbox4.addWidget(self.eyeLine)
        hbox4.addWidget(self.earLine)

        hbox5 = QHBoxLayout()
        hbox5.addWidget(self.detailLine)
        hbox5.addWidget(self.urlLine)

        hbox6 = QHBoxLayout()
        hbox6.addWidget(self.introLine)
        hbox6.addWidget(self.Btn)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)
        vbox.addLayout(hbox3)
        vbox.addLayout(hbox4)
        vbox.addLayout(hbox5)
        vbox.addLayout(hbox6)

        self.setLayout(vbox)

    def writeInRepo(self):
        '''name color eye-color ear-color limb_num wing_num habitat detail super_class url intro height weight
        '''
        if self.nameLine.text() == '':
            QMessageBox.warning(self, "Warning", "宝可梦名字不能为空")
            return
        global expert
        expert.update_pokemon([ self.nameLine.text(), self.colorLine.text(), self.eyeLine.text(),
                                self.earLine.text(), self.limbLine.text(), self.wingLine.text(),
                                self.habitatLine.text(), self.detailLine.text(), self.superClassLine.text(),
                                self.urlLine.text(), self.introLine.text(), self.heightLine.text(),
                                self.weightLine.text()])
        QMessageBox.information(self, "success", "写入成功")
        self.close()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    PokemonMap = MainWin()
    app.exec_()


import matplotlib.pyplot as plt
import numpy as np

list1 = np.random.rand(500)
list2 = np.random.rand(500)

list1x = np.random.normal(0, 0.01, 500)
list2y = np.random.normal(0, 0.01, 500)

plt.scatter(list1, list2)
plt.show()
plt.scatter(list1x, list2y)
plt.show()