from imutils.video import VideoStream
from imutils.video import FPS
import imutils
import cv2,os,urllib.request,pickle
import numpy as np
from django.conf import settings
import os

# from . import train_model
# # load our serialized face detector model from disk
# protoPath = os.path.sep.join([settings.BASE_DIR, "face_detection_model\\deploy.prototxt"])
# modelPath = os.path.sep.join([settings.BASE_DIR,"face_detection_model\\res10_300x300_ssd_iter_140000.caffemodel"])
# detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
# # load our serialized face embedding model from disk
# embedder = cv2.dnn.readNetFromTorch(os.path.join(settings.BASE_DIR,'face_detection_model/openface_nn4.small2.v1.t7'))
# # load the actual face recognition model along with the label encoder
# recognizer = os.path.sep.join([settings.BASE_DIR, "output\\recognizer.pickle"])
# recognizer = pickle.loads(open(recognizer, "rb").read())
# le = os.path.sep.join([settings.BASE_DIR, "output\\le.pickle"])
# le = pickle.loads(open(le, "rb").read())
# dataset = os.path.sep.join([settings.BASE_DIR, "dataset"])
# user_list = [ f.name for f in os.scandir(dataset) if f.is_dir() ]

import mediapipe as mp
import math
import random
import time

#face_mesh object
mp_face_mesh = mp.solutions.face_mesh

#face_mesh object
mp_face_mesh = mp.solutions.face_mesh

#left eye and right eye indices
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386, 385, 384, 398]
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160, 161, 246]

#left and right iris indices
RIGHT_IRIS = [474, 475, 476, 477]
LEFT_IRIS = [469, 470, 471, 472]

rightEyeRL = [263] #right eye right most landmark
rightEyeLL = [362] #right eye left most landmark
lefteyeRL = [133] #left eye right most landmark
lefteyeLL = [33] #left eye left most landmark

#calculate the distance
def euclidean_distance(point1, point2):
    x1, y1 = point1.ravel()
    x2, y2 = point2.ravel()
    distance = math.sqrt((x2-x1)**2 + (y2-y1)**2)
    return distance

iris_positions = []
#find the position
def iris_position(iris_center, right_point, left_point):
    center_to_right_distance = euclidean_distance(iris_center, right_point)
    total_distance = euclidean_distance(right_point, left_point)
    ratio = center_to_right_distance/total_distance
    iris_position = ""
    if ratio <= 0.434:
        iris_position = "right"
        iris_positions.append("right")
    elif ratio > 0.434 and ratio <= 0.50:
        iris_position = "center"
        iris_positions.append("center")
    else:
        iris_position = "left"
        iris_positions.append("left")
    print(ratio)
    return iris_positions, iris_position, ratio

x_axisRange = 1920
y_axisRange = 1080
randomX=[]
randomY=[]

def randomNumber():
    again = True
    while (again):
        rx = random.randint(0, x_axisRange)
        ry = random.randint(0, y_axisRange)
        if ((rx not in randomX) and (ry not in randomY) and ((rx < 300) or (rx > 1700) or (rx > 900 and rx < 1020))):
            randomX.append(rx)
            randomY.append(ry)
            again = False
    return rx, ry

circlePositions = []
def getCirclePosition(x,y):
    cir_position = ""
    if (700 <= x <= 1219):
        cir_position = "center"
        circlePositions.append("center")
    elif (0 <= x <= 699):
        cir_position = "left"
        circlePositions.append("left")
    else:
        cir_position = "right"
        circlePositions.append("right")
    return cir_position

class FaceDetect(object):
    def __init__(self):

        self.cap = cv2.VideoCapture(0)
        self.rx1, self.ry1 = randomNumber()
        self.rx2, self.ry2 = randomNumber()
        self.rx3, self.ry3 = randomNumber()
        self.rx4, self.ry4 = randomNumber()
        self.rx5, self.ry5 = randomNumber()
        self.circleMatchCounter = 0
        self.loopEnd = False
        self.circleMatches = 0
        self.wrongAnswers = 0
        self.maxCount = 20
        self.maxWrong = 50
        self.outputText = ""

        # self.background = cv2.imread('C:\\Users\\Dell\\OneDrive\\Desktop\\FYP\\WebApp\\LivenessDetectionApp\\LivenessDetection\\background.png')
        self.background = cv2.imread('static/images/background_new.png')

        self.background = cv2.resize(self.background, (1920, 1080))
        self.face_mesh = mp_face_mesh.FaceMesh(
            max_num_faces=1,
            refine_landmarks=True,
            min_detection_confidence=0.5,
            min_tracking_confidence=0.5
        )

    def __del__(self):
        self.cap.release()
        cv2.destroyAllWindows()

    def get_frame(self):
        ret, frame = self.cap.read()
        if not ret:
            self.loopEnd = True  ### break
        #flip it so that its more like a mirror
        #1 -> mirror image
        frame = cv2.flip(frame, 1)

        frame = cv2.resize(frame, (1920, 1080))
        mask = np.zeros_like(frame)
        height, width, _ = frame.shape
        cv2.ellipse(
            mask,
            (int(width/2), int(height/2)),
            (int(width/5), int(height/5)),
            0, 0, 360 , (255, 255,255), -1
            )

        mask = cv2.bitwise_not(mask)
        masked_background = cv2.bitwise_and(self.background, mask)
        masked_frame = cv2.bitwise_and(frame, cv2.bitwise_not(mask))
        output = cv2.bitwise_or(masked_background, masked_frame)
        #it creates the image in bgr but mediapipe requires in RGB.
        rgb_frame = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
        img_h, img_w = output.shape[:2]
        results = self.face_mesh.process(rgb_frame)
        cv2.putText(output, self.outputText, (760, 570), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 2)
        if (results.multi_face_landmarks):
            #print(results.multi_face_landmarks[0].landmark)
            #get the pixel coordinate by multiplying the landmarks x and y with the frame width and height
            mesh_points = np.array([np.multiply([p.x, p.y], [img_w, img_h]).astype(int) for p in results.multi_face_landmarks[0].landmark])

            #478 landmark points in it (due to the refine_landmarks)
            #print(mesh_points.shape)

            #draw the eye shape around the detected one
            #cv2.polylines(frame, [mesh_points[LEFT_EYE]], True, (0, 0,255), 1, cv2.LINE_AA)
            #cv2.polylines(frame, [mesh_points[RIGHT_EYE]], True, (0, 0,255), 1, cv2.LINE_AA)

            #draw circles for iris - detected eyes
            (l_cx, l_cy), l_radius = cv2.minEnclosingCircle(mesh_points[LEFT_IRIS])
            (r_cx, r_cy), r_radius = cv2.minEnclosingCircle(mesh_points[RIGHT_IRIS])
            center_left = np.array([l_cx, l_cy], dtype=np.int32)
            center_right = np.array([r_cx, r_cy], dtype=np.int32)
            cv2.circle(output, center_left, int(l_radius), (255, 0, 255), 1, cv2.LINE_AA)
            cv2.circle(output, center_right, int(r_radius), (255, 0, 255), 1, cv2.LINE_AA)
            cv2.circle(output, mesh_points[rightEyeRL[0]], 3, (255, 255 , 255), -1, cv2.LINE_AA)
            cv2.circle(output, mesh_points[rightEyeLL[0]], 3, (0, 255, 255), -1, cv2.LINE_AA)
            cv2.circle(output, mesh_points[lefteyeRL[0]], 3, (255, 255 , 255), -1, cv2.LINE_AA)
            cv2.circle(output, mesh_points[lefteyeLL[0]], 3, (0, 255, 255), -1, cv2.LINE_AA)

            iris_array, iris_pos, ratio = iris_position(center_right, mesh_points[rightEyeRL[0]], mesh_points[rightEyeLL[0]])
            #iris_pos1, ratio1 = iris_position(center_left, mesh_points[lefteyeRL[0]], mesh_points[lefteyeLL[0]])
            print(iris_pos)

            if(self.circleMatchCounter == 0):
                cv2.circle(output, (self.rx1, self.ry1), radius=0, color=(255,0,0), thickness=20)
                circlePosition = getCirclePosition(self.rx1,self.ry1)
                if(iris_pos == circlePosition):
                    self.circleMatches += 1
                    print("circle 1 (" , self.rx1, self.ry1 , ") matched", self.circleMatches)
                else:
                    self.wrongAnswers += 1

            elif(self.circleMatchCounter == 1):
                cv2.circle(output, (self.rx2, self.ry2), radius=0, color=(255,0,0), thickness=20)
                circlePosition = getCirclePosition(self.rx2,self.ry2)
                if(iris_pos == circlePosition):
                    self.circleMatches += 1
                    print("circle 2 (" , self.rx2, self.ry2 , ") matched", self.circleMatches)
                else:
                    self.wrongAnswers += 1

            elif(self.circleMatchCounter == 2):
                cv2.circle(output, (self.rx3, self.ry3), radius=0, color=(255,0,0), thickness=20)
                circlePosition = getCirclePosition(self.rx3,self.ry3)
                if(iris_pos == circlePosition):
                    self.circleMatches += 1
                    print("circle 3 (" , self.rx3, self.ry3 , ") matched", self.circleMatches)
                else:
                    self.wrongAnswers += 1

            elif(self.circleMatchCounter == 3):
                cv2.circle(output, (self.rx4, self.ry4), radius=0, color=(255,0,0), thickness=20)
                circlePosition = getCirclePosition(self.rx4,self.ry4)
                if(iris_pos == circlePosition):
                    self.circleMatches += 1
                    print("circle 4 (" , self.rx4, self.ry4 , ") matched", self.circleMatches)
                else:
                    self.wrongAnswers += 1

            elif(self.circleMatchCounter == 4):
                cv2.circle(output, (self.rx5, self.ry5), radius=0, color=(255,0,0), thickness=20)
                circlePosition = getCirclePosition(self.rx5,self.ry5)
                if(iris_pos == circlePosition):
                    self.circleMatches += 1
                    print("circle 5 (" , self.rx5, self.ry5 , ") matched", self.circleMatches)
                else:
                    self.wrongAnswers += 1

            elif(self.circleMatchCounter == 5):
                self.outputText = "Real Person"
                print("Real person!")
                self.loopEnd = True
                ### break

            if (self.circleMatches > self.maxCount):
                self.circleMatches = 0
                self.wrongAnswers = 0
                self.circleMatchCounter = self.circleMatchCounter + 1
            elif (self.wrongAnswers > self.maxWrong):
                self.loopEnd = True
                self.outputText = "Fake Person"
                print('Fakeeeeee!!!!!!!')

        ret1, jpeg = cv2.imencode('.jpg', output)
        return jpeg.tobytes(), self.loopEnd
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.loopEnd = True ### break

# class FaceDetect(object):
# 	def __init__(self):
# 		# initialize the video stream, then allow the camera sensor to warm up
# 		self.vs = VideoStream(src=0).start()
# 		# start the FPS throughput estimator
# 		self.fps = FPS().start()

# 	def __del__(self):
# 		cv2.destroyAllWindows()

# 	def get_frame(self):
# 		# grab the frame from the threaded video stream
# 		frame = self.vs.read()
# 		frame = cv2.flip(frame,1)

# 		# resize the frame to have a width of 600 pixels (while
# 		# maintaining the aspect ratio), and then grab the image
# 		# dimensions
# 		frame = imutils.resize(frame, width=600)
# 		(h, w) = frame.shape[:2]

# 		# construct a blob from the image
# 		imageBlob = cv2.dnn.blobFromImage(
# 			cv2.resize(frame, (300, 300)), 1.0, (300, 300),
# 			(104.0, 177.0, 123.0), swapRB=False, crop=False)

# 		# # apply OpenCV's deep learning-based face detector to localize
# 		# # faces in the input image
# 		# detector.setInput(imageBlob)
# 		# detections = detector.forward()

# 		# # loop over the detections
# 		# for i in range(0, detections.shape[2]):
# 		# 	# extract the confidence (i.e., probability) associated with
# 		# 	# the prediction
# 		# 	confidence = detections[0, 0, i, 2]

# 		# 	# filter out weak detections
# 		# 	if confidence > 0.5:
# 		# 		# compute the (x, y)-coordinates of the bounding box for
# 		# 		# the face
# 		# 		box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# 		# 		(startX, startY, endX, endY) = box.astype("int")

# 		# 		# extract the face ROI
# 		# 		face = frame[startY:endY, startX:endX]
# 		# 		(fH, fW) = face.shape[:2]

# 		# 		# ensure the face width and height are sufficiently large
# 		# 		if fW < 20 or fH < 20:
# 		# 			continue

# 		# 		# construct a blob for the face ROI, then pass the blob
# 		# 		# through our face embedding model to obtain the 128-d
# 		# 		# quantification of the face
# 		# 		faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,
# 		# 			(96, 96), (0, 0, 0), swapRB=True, crop=False)
# 		# 		embedder.setInput(faceBlob)
# 		# 		vec = embedder.forward()

# 		# 		# perform classification to recognize the face
# 		# 		preds = recognizer.predict_proba(vec)[0]
# 		# 		j = np.argmax(preds)
# 		# 		proba = preds[j]
# 		# 		name = le.classes_[j]

# 		# 		# draw the bounding box of the face along with the
# 		# 		# associated probability
# 		# 		text = "{}: {:.2f}%".format(name, proba * 100)
# 		# 		y = startY - 10 if startY - 10 > 10 else startY + 10
# 		# 		cv2.rectangle(frame, (startX, startY), (endX, endY),
# 		# 			(0, 0, 255), 2)
# 		# 		cv2.putText(frame, text, (startX, y),
# 		# 			cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

# 		# update the FPS counter
# 		self.fps.update()
# 		ret, jpeg = cv2.imencode('.jpg', frame)
# 		return jpeg.tobytes()


g = lambda: [*map(int, input().split())]

from collections import deque
N = int(input())
Map = []
x, y = 0, 0
for _ in range(N):
    l = g()
    Map.append(l)
    if 9 in l:
        x, y = _, l.index(9)
        Map[x][y] = 0

size, eat, ans = 2, 0, 0
dq = deque([(x, y, ans)])
visited = [bytearray(N) for _ in range(N)]
pts = None, None
while True:
    while dq:
        x, y, t = dq.popleft()
        visited[x][y] = 1
        for a, b in ((x-1, y), (x, y-1), (x, y+1), (x+1, y)):
            if 0 <= a < N and 0 <= b < N and visited[a][b] == 0 and Map[a][b] <= size:
                visited[a][b] = 1
                if 0 < Map[a][b] < size:
                    if pts == (None, None):
                        pts = (a, b)
                        ans = t+1
                    elif t+1 == ans:
                        if (a, b) < pts:
                            pts = (a, b)
                    else:
                        break
                else:
                    dq.append((a, b, t+1))

    if pts[0] == None:
        break
    a, b = pts
    pts = (None, None)
    dq = deque([(a, b, ans)])
    visited = [bytearray(N) for _ in range(N)]

    Map[a][b] = 0
    eat += 1
    if eat == size:
        size += 1
        eat = 0
print(ans)


import numpy as np
import scipy.io as sio
import tensorflow as tf
import matplotlib.pyplot as plt
import random

#loading data

filename0='ex4data1.mat'
datadict0=sio.loadmat(filename0)
X=np.array(datadict0['X'])
y=np.array(datadict0['y'])
m,n=np.shape(X)

#structuring the neural network

hid_layer=500; #number of hidden units
num_out=10; #number of output units

#dividing the training,cross validation and the test sets

X_train=np.zeros((4000,400))
X_test=np.zeros((1000,400))
y_train=np.zeros((4000,1))
y_test=np.zeros((1000,1))
for i in range(0,10):
    X_train[400*i:400*(i+1),:]=X[500*i:500*i+400,:]
    y_train[400*i:400*(i+1),0]=y[500*i:500*i+400,0]
    X_test[100*i:100*(i+1),:]=X[500*i+400:500*(i+1),:]
    y_test[100*i:100*(i+1),0]=y[500*i+400:500*(i+1),0]
Y_train=np.zeros((4000,num_out))
Y_test=np.zeros((1000,num_out))
for i in range(1,num_out+1):
    l0=np.array(y_train==i,dtype=np.int32)
    Y_train[:,i-1]=l0[:,0]
    l1=np.array(y_test==i,dtype=np.int32)
    Y_test[:,i-1]=l1[:,0]

#randomly initializing the parameters

t0=np.random.random((hid_layer,n))
t1=np.random.random((num_out,hid_layer))
t2=np.random.random((hid_layer,1))
t3=np.random.random((num_out,1))
Theta1=(np.array(t0,dtype=np.float32)*(0.6)-np.ones((hid_layer,n),dtype=np.float32)*0.3).T
Theta2=(np.array(t1,dtype=np.float32)*(0.6)-np.ones((num_out,hid_layer),dtype=np.float32)*0.3).T
Theta1_=(np.array(t2,dtype=np.float32)*(0.6)-np.ones((hid_layer,1),dtype=np.float32)*0.3).T
Theta2_=(np.array(t3,dtype=np.float32)*(0.6)-np.ones((num_out,1),dtype=np.float32)*0.3).T

#defining important parameters and variables to use as tensors

x=tf.placeholder(tf.float32,[None,n])
y0=tf.placeholder(tf.int32,[None,num_out])
w1=tf.Variable(Theta1,tf.float32)
w2=tf.Variable(Theta2,tf.float32)
w10=tf.Variable(Theta1_,tf.float32)
w20=tf.Variable(Theta2_,tf.float32)

#finding the activation values

def activation(x0,w01,w02,w010,w020):
    a1=tf.add(tf.matmul(x0,w01),tf.matmul(tf.ones((tf.shape(x)[0],1)),w010))
    a2=tf.nn.sigmoid(a1)
    a3=tf.nn.sigmoid(tf.add(tf.matmul(a2,w02),tf.matmul(tf.ones((tf.shape(x)[0],1)),w020)))
    return a3

#optimizing the parameters

max_iter=2500
c=np.zeros((max_iter,1))
Y=activation(x,w1,w2,w10,w20)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Y,labels=y0))
train=tf.train.AdamOptimizer().minimize(cost)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
costf=np.zeros((max_iter,1))
for i in range(0,max_iter):
    print('iteration',i+1)
    sess.run(train,{x:X_train,y0:Y_train})
    costf[i][0]=sess.run(cost,{x:X_train,y0:Y_train})
print('The value of cost function after optimizing the parameters is :',costf[max_iter-1,0])
Dict={'w1':w1,'w10':w10,'w2':w2,'w20':w20}
filemat='ex3data1.mat'
sio.savemat(filemat,Dict)    #saving the learned parameters to a mat file
p=np.array(range(1,max_iter+1))
plt.plot(p,costf)
plt.xlabel('Number of iterations')
plt.ylabel('Value of Cost Function')
plt.title('The variation of the cost function with the number of iterations')
plt.show()

#calculating the accuracy

filename1='ex3data1.mat'
Dict=sio.loadmat(filename1)
w1=tf.constant(Dict['w1'],tf.float32)
w1_=tf.constant(Dict['w10'],tf.float32)
w2=tf.constant(Dict['w2'],tf.float32)
w2_=tf.constant(Dict['w20'],tf.float32)
z0=np.array(sess.run(Y,feed_dict={x:X_test,y0:Y_test}))
z=np.array([np.max(z0,axis=1)]).T
pred=np.zeros((np.shape(z0)[0],1),dtype=np.int32)
for i in range(0,np.shape(z0)[0]):
    for j in range(0,np.shape(z0)[1]):
        if(z[i][0]==z0[i][j]):
            pred[i,0]=j+1
pred=tf.Variable(pred,tf.int32)
init=tf.global_variables_initializer()
sess.run(init)
y_tc=tf.placeholder(tf.int32,[None,1])
accuracy=tf.reduce_mean(tf.cast(tf.equal(pred,y_tc),tf.float32))
print('The accuracy on the test set is:',sess.run(accuracy,feed_dict={x:X_test,y0:Y_test,y_tc:y_test}))

#predicting the values with the learned parameters

for i in range(0,10):
    w=random.randint(0,999)
    print('The prediction from the learned parameters is (10 is corresponding to 0)',sess.run(pred[w,0]))
    X=np.reshape(X_test[w,:],(20,20))
    plt.imshow(X,cmap=plt.cm.gray_r,interpolation="nearest")
    plt.show()


import datetime
import random

start_year = datetime.date(2001, 1, 1)

intro = '''Birthday Paradox, by Al Sweigart al@inventwithpython.com
     The Birthday Paradox shows us that in a group of N people, the odds
     that two of them have matching birthdays is surprisingly large.
     This program does a Monte Carlo simulation (that is, repeated random simulations)
     to explore this concept.
     (It's not actually a paradox, it's just a surprising result.)'''

def get_Birthdays(number_of_birthdays):
    birthday_list = []
    for j in range(number_of_birthdays):
        # generating days to add on the start year
        random_number_of_days = datetime.timedelta(random.randint(0, 365))
        created_bd = start_year + random_number_of_days
        birthday_list.append(created_bd)
    return birthday_list

def get_Match(bds):
    if len(bds) == len(set(bds)):  # Looking for similar birthdays
        return None
    else:
        # comparing each birthday within the created list
        for a, bd1 in enumerate(bds):
            for b, bd2 in enumerate(bds[a + 1:]):
                if bd1 == bd2:
                    return bd1

print(intro)
Months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
while True:  # Keep asking until the user enters a valid amount
    print('How many birthdays do you want me to Generate? (max 100) ')
    ans = input('>>  ')
    if ans.isdecimal() and (0 < int(ans) <= 100):
        numBdays = int(ans)
        break

print()
print(f'Here are {numBdays} Birthdays')
birthdays = get_Birthdays(numBdays)

for i, birthday in enumerate(birthdays):
    if i != 0:
        print(', ', end='')
        month_name = Months[birthday.month - 1]
        date_text = f'{month_name, birthday.day}'
        print(date_text, end='')
print()
print()

# Determine if there are two birthdays that match
match = get_Match(birthdays)
print('In this simulation, ', end='')
if match is not None:
    # getting the bd month of th match
    month_name = Months[match.month - 1]
    date_text = f'{month_name, match.day}'
    print(f'Multiple people have a birthday on {date_text}')
else:
    print('No matching Birthdays where found')

# Generating 100,000 simulations
print(f'Generating {numBdays} birthdays 100,000 times')
input('Press Enter to Begin')
simMatch = 0  # How many simulations had matching birthdays in them
for i in range(100000):
    if i % 10000 == 0:
        print(f'{i} simulations run....')
    birthdays = get_Birthdays(numBdays)
    if get_Match(birthdays) is not None:
        simMatch = simMatch + 1
print(f'100000 simulations run.....')
probability = round(simMatch / 100_000 * 100, 2)
print('Out of 100,000 simulations of', numBdays, 'people, there was a')
print('matching birthday in that group', simMatch, 'times. This means')
print('that', numBdays, 'people have a', probability, '% chance of')
print('having a matching birthday in their group.')
print('That\'s probably more than you would think!')
print(simMatch)


from ac.pre import get_yaml
from ac.compare import compare
from v1.baidu import baidu
from v2.user import user
from v2.download import download

def v1():
    print('搜索关键字：'+config['keyword']+'\n搜索页数：'+str(config['pages'])+'\n输出大于'+str(
        config['sim_limit'])+'%的结果\n使用搜索引擎：'+config['site']+'\n---------------\n')
    if config['site'] == 'baidu':
        baidu(config)

def v2():
    print('比赛编号：'+str(config['contest_id'])+'\n搜索页数：'+str(config['pages'])+'页\n使用搜索引擎'+config['site']+'\n相似度阀值：'+str(
        config['sim_limit'])+'%\n最多等待：'+str(config['time_out'])+'s\n----------------------------------------')
    download()
    user()
    compare()
    print('完成！请打开result.txt查看结果')

if __name__ == '__main__':
    config = get_yaml()
    if config['mode'] == 1:
        v1()
    elif config['mode'] == 2:
        v2()
    else:
        print('模式不合法！')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu

# Note that while the regex tester test Python 2.7 regex idioms,
# the code base itself has been made to be run on host with Python 2.6

import re
import sys
import os
import webbrowser
import json

from pprint import pformat
from operator import or_

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
RUN_REGEX_SCRIPT = os.path.join(ROOT_DIR, 'run_regex.py')

try:
    import bottle
    import regex_tester
    ROOT_DIR = os.path.dirname(os.path.abspath(regex_tester.__file__))
    bottle.TEMPLATE_PATH.insert(0, os.path.join(ROOT_DIR, 'views'))
except ImportError:
    sys.path.insert(0, os.path.join(ROOT_DIR, 'libs'))
    import bottle

STATIC_FILES_ROOT = os.path.join(ROOT_DIR, 'static')

from clize import clize
from bottle import request, run, view, static_file, Bottle

from parallel import worker

DEBUG = False
TIMEOUT = 5
STATIC_FILES_ROOT = os.path.join(ROOT_DIR, 'static')
ENCODING = sys.stdout.encoding or 'utf-8'

CODES = {
    'search': """re.search(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
    'split': """re.split(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
    'findall': """re.findall(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
    'match': """re.match(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
    'sub': """re.sub(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s%(is_raw)s'''%(replace)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
    'subn': """re.subn(%(is_unicode)s%(is_raw)s'''%(regex)s''', %(is_unicode)s%(is_raw)s'''%(replace)s''', %(is_unicode)s'''%(text)s'''%(flags_param)s)""",
}

FLAGS = dict((flag, getattr(re, flag)) for flag in (
    'IGNORECASE',
    'LOCALE',
    'MULTILINE',
    'DOTALL',
    'UNICODE',
    'VERBOSE'
))

DEFAULT = {
        'regex': '',
        'text': '',
        'replace': '',
        'is_unicode': '',
        'error': '',
        'code': '',
        'match': None,
        'function': 'search',
        'is_unicode': 'u',
        'is_raw': 'r',
        'markers': '[]',
        'flags_param': '',
        'group': (),
        'groupdict': {}
    }

@worker(timeout=TIMEOUT)
def run_regex(message):
    """
        Apply regex on text and return the results as a dictionary
    """

    regex = message['regex']
    function = message.get('function', 'search')
    replace = message.get('replace', '')
    isunicode = message.get('isunicode', False)
    flags = int(message.get('flags', 0))
    text = message.get('text', None)

    if isunicode:
        text = text.decode(ENCODING)
        regex = regex.decode(ENCODING)
        replace = regex.decode(ENCODING)

    pattern = re.compile(regex, flags=flags)

    try:
        if 'sub' in function:
            result = getattr(re, function)(pattern, replace, text)
        else:
            result = getattr(re, function)(pattern, text)
    except re.error as e:
        if text:
            raise e
        sys.exit(u'Error in your regular expression: %s' % e)

    out = {'match': bool(result)}

    if result:
        if hasattr(result, 'group'):
            out['group'] = pformat(result.group())
            out['groupdict'] = pformat(result.groupdict())
            markers = result.span()
        else:
            markers = (m.span() for m in re.finditer(regex, text, flags=flags))
            markers = (x for boundaries in markers for x in boundaries)

        out['markers'] = json.dumps(tuple(markers))

    return out

# todo: allow arguments functions surch as maxsplit for split

app = Bottle()

@app.get('/')
@view('index')
def index():
    ctx = dict(DEFAULT)
    ctx.update(dict((flag, False) for flag in FLAGS))
    return ctx

@app.post('/')
@view('index')
def index():

    forms = request.forms
    ctx = dict(DEFAULT)
    regex = forms.get('regex', '').decode('utf-8')
    text = forms.get('text', '').decode('utf-8')
    replace = forms.get('replace', '')
    is_unicode =  'u' * bool(forms.get('is_unicode', False))
    is_raw = 'r' * bool(forms.get('is_raw', False))
    function = forms.get('function', 'search')

    set_flags = dict((flag, bool(forms.get(flag))) for flag in FLAGS)
    ctx.update(set_flags)
    set_flags = set_flags.items()
    flags = reduce(or_, (FLAGS[flag] for flag, on in set_flags if on), 0)

    if function not in CODES:
        error = u'This function is not supported'

    ctx.update(locals())

    if regex.strip() and text.strip():

        if "'''" in regex:
            ctx['error'] = u"""
                        Regex containing ''' (triple single quotes) are
                        not supported
                            """
            return ctx

        if not is_raw:
            regex = regex.decode('unicode_escape')

        elif regex.endswith('\\'):
            ctx['error'] = u"""
                        Raw string litterals ending with '\\' are not supported
                        in Python
                           """
            return ctx

        app.regex_process.put(ctx)

        try:
            res = app.regex_process.get(True)
        except run_regex.TimeoutError:
            ctx['error'] = u"""
                                This regex took too much time to process. If
                                you want to experiment with heavy regexes,
                                download the stand alone version of the regex
                                tester, set the timeout to None and run it
                                on your machine.
                            """
        except Exception as e:
            ctx['error'] = unicode(e)
        else:
            if isinstance(res, Exception):
                ctx['error'] = unicode(res)
            else:
                ctx.update(res)

        if flags != 0:
            ctx['flags_param'] = ", flags=" + '|'.join('re.' + flag for flag, on in set_flags if on)
        ctx['code'] = CODES[function] % ctx

    return ctx

@app.route('/static/<filename>')
def server_static(filename):
    return static_file(filename, root=STATIC_FILES_ROOT)

@app.route('/download/<filename:path>')
def download(filename):
    return static_file(filename, root=STATIC_FILES_ROOT, download=filename)

@clize
def main(host='localhost', port=9999, debug=DEBUG, timeout=TIMEOUT):
    try:
        app.regex_process = run_regex.start()
        if debug:
            bottle.debug(True)
            run(app, host=host, port=port, reloader=True)
        else:
            webbrowser.open('http://%s:%s/' % (host, port))
            run(app, host=host, port=port)
    finally:
        app.regex_process.stop()

if __name__ == "__main__":
    main(*sys.argv)


from os import rename
from tkinter import *
from tkinter import filedialog
import subprocess
import shutil

def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/",
                                          title = "Select a File",
                                          filetypes = (("Image files",
                                                        "*.png*"), ))
    filename=str(filename).replace("/","\\")
    print(filename)
    #os.rename(filename,"C:\\Users\\zmana\\Desktop\\BEStuff\\MAINTEST.png")
    shutil.copy(filename,"C:\\Users\\zmana\\Desktop\\BEStuff\\Uploads\\1.png")

    #subprocess.run(["xcopy",filename,"C:\\Users\\zmana\\Desktop\\BEStuff\\Uploads\\"])

def colorit():
    subprocess.run([ "python","PATH TO MODEL FILE" ])

    #   RUNS THE MAIN MODEL CODE
    pass

root  =  Tk()
root.configure(background="Light Gray")
root.geometry("1400x700+10+20")
root.minsize(1400,700)
root.maxsize(1400,700)

canvas = Canvas(root, width = 1400, height = 700)
canvas.pack()
img = PhotoImage(file="C:\\Users\\zmana\\Desktop\\BEStuff\\im1.png")
canvas.create_image(0,0, anchor=NW, image=img)

b2 = Button(root,text="Colour It",font=("Comic Sans MS",14),activebackground='sky blue',padx=10,pady=10,relief=SUNKEN,justify=CENTER,background="#ccdde3",command=colorit)
b2.place(x=600,y=600,height=60,width=200)

b3=Button(root,text="+",font=("Comic Sans MS",26),activebackground='sky blue',padx=10,pady=10,relief=SUNKEN,justify=CENTER,height=200,command=browseFiles)
b3.place(x=200,y=50,width=512,height=512)

im2  = PhotoImage(file="C:\\Users\\zmana\\Desktop\\BEStuff\\img2.png")
#b4=Button(root,text="",font=("Comic Sans MS",9),activebackground='sky blue',padx=10,pady=10,background="White", relief=SUNKEN,justify=CENTER,height=200)
#b4.place(x=700,y=50,width=512,height=512)

canvas2 = Canvas(root, width = 512, height = 512,background="White")
canvas2.place(x=700,y=50)

canvas2.create_image(0,0, anchor=NW, image=im2)

root.mainloop()


#! -*- coding:utf-8 -*-
"""
@author: chengbo
@software: PyCharm
@file: save_model.py
@time: 2022/10/27 17:10
"""
from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np

# import torchvision
# from torchvision import datasets, models, transforms
# import matplotlib.pyplot as plt
import time
import os
import copy

# 定义模型
class TheModelClass(nn.Module):
    def __init__(self):
        super(TheModelClass, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# 初始化模型
model = TheModelClass()

# 初始化优化器
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# 打印模型的 state_dict
print("Model's state_dict:")
for param_tensor in model.state_dict():
    print(param_tensor, "\t", model.state_dict()[param_tensor].size())

# 打印优化器的 state_dict
print("Optimizer's state_dict:")
for var_name in optimizer.state_dict():
    print(var_name, "\t", optimizer.state_dict()[var_name])

torch.save(model, "model.pt")

model = torch.load("model.pt")

torch.onnx.export()


#!/usr/bin/env python3
from collections import Counter

def main():
    S = input()
    N = len(S)
    T = [0] * (N+1)

    for i in range(N-1,-1,-1):
        T[i] = (T[i+1] + int(S[i]) * pow(10,N-i,2019)) % 2019
    l = Counter(T)
    ans = 0
    for v in l.values():
        ans += v * (v-1) // 2
    print(ans)

if __name__ == "__main__":
    main()


from fastapi import APIRouter ,status , File, UploadFile
from pydantic import BaseModel, Field
from fastapi.responses import JSONResponse
from app.service import merchant
from typing import Union
import json
from app.models import merchant as merchant_model

router = APIRouter()
merchant_service = merchant.MerchantService()

@router.post("/")
async def CreateMerchant(merchant: merchant_model.CreateMerchantForm):
    return await merchant_service.CreateMerchant(merchant)

@router.get("/{merchant_id}")
async def GetOneMerchant(merchant_id: int):
    return await merchant_service.GetOneMerchant(merchant_id)

@router.get("/")
async def GetAllMerchantPaginated(page_number: int, page_size: int):
    return await merchant_service.GetAllMerchantPaginated(page_number, page_size)

@router.put("/{merchant_id}")
async def UpdateMerchant(merchant_id: int, merchant: merchant_model.CreateMerchantForm):
    return await merchant_service.UpdateMerchant(merchant_id, merchant)

@router.delete("/{merchant_id}")
async def DeleteMerchant(merchant_id: int):
    return await merchant_service.DeleteMerchant(merchant_id)


#!/usr/bin/env python3

from classical import *

cd = CD()
cd.title = 'Olympic Music Festival 2012 - Aug 25th & 26th (2)'
cd.artists = 'Olympic Music Festival Artists'

p = Piece(
        "Dialogues for Violin and Cello",
        "Michael Brown",
        ["Arnaud Sussmann", "Nicholas Canellakis"],
        [Movement(1, "Dialogues for Violin and Cello")]
        )
cd.pieces.append(p)

p = Piece(
        "String Sextet No. 2",
        "Johannes Brahms",
        ["Arnold Sussmann", "Jessica Lee", "Alan Iglitzin",
            "Tien-Hsin Cindy Wu", "Nicholas Canellackis", "Patrick Jee"],
        [
            Movement(1, "Allegro non troppo"),
            Movement(2, "Scherzo - Allegro non troppo"),
            Movement(3, "Poco adagio"),
            Movement(4, "Poco allegro")
            ]
        )
cd.pieces.append(p)
tagit(cd)


'''
Default: useful variables for the base page templates.
'''

from ipware import get_client_ip
from ivatar.settings import IVATAR_VERSION, SITE_NAME, MAX_PHOTO_SIZE
from ivatar.settings import BASE_URL, SECURE_BASE_URL
from ivatar.settings import MAX_NUM_UNCONFIRMED_EMAILS

def basepage(request):
    '''
    Our contextprocessor adds additional context variables
    in order to be used in the templates
    '''
    context = {}
    if 'openid_identifier' in request.GET:
        context['openid_identifier'] = \
            request.GET['openid_identifier']  # pragma: no cover
    client_ip = get_client_ip(request)[0]
    context['client_ip'] = client_ip
    context['ivatar_version'] = IVATAR_VERSION
    context['site_name'] = SITE_NAME
    context['site_url'] = request.build_absolute_uri('/')[:-1]
    context['max_file_size'] = MAX_PHOTO_SIZE
    context['BASE_URL'] = BASE_URL
    context['SECURE_BASE_URL'] = SECURE_BASE_URL
    context['max_emails'] = False
    if request.user:
        if not request.user.is_anonymous:
            unconfirmed = request.user.unconfirmedemail_set.count()
            if unconfirmed >= MAX_NUM_UNCONFIRMED_EMAILS:
                context['max_emails'] = True

    return context


from nltk.stem.snowball import SnowballStemmer
import sys
import os,glob
import re
import math
import pathlib
import random

def prediction(w_t,mail,classType):
	#Spam = 1 and Ham =-1
	accuracy = 0
	for text in mail:
		#Calculate prediction
		sum = output(text,w_t)
		#If pos+ve ==> spam else if neg-ve ==> ham
		if (sum==1 and classType == "spam"):
			accuracy+=1
		elif (sum==-1 and classType == "ham"):

			accuracy+=1
	return float(accuracy)/len(mail)

def output(text,w_t):
	output = w_t["BIAS"]
	for word in text:
		if word in w_t:
			output+=(text[word]*w_t[word])
	perceptron = -1
	if (output>0):
		perceptron = 1
	return perceptron

#Update weight vector
def updateWeight(w_t,ham,spam,rate,iterations):
	#Set class type where ham = -1 and spam = 1

	#Mix the two in order to avoid skewing to one side
	j = max(len(ham),len(spam))

	for i in range(j):
		if i<len(ham):
			text = ham[i]
			o = output(text,w_t)
			for word in text:
				if word in w_t:
					change = rate*text[word]*(-1-o)
					w_t[word]+= change
		if i<len(spam):
			text = spam[i]
			o = output(text,w_t)
			for word in text:
				if word in w_t:
					change = rate*text[word]*(1-o)
					w_t[word]+= change

	if (iterations>1):
		w_t = updateWeight(w_t,ham,spam,rate,iterations-1)
	return w_t

#0 Weight vector
def initialWeight(hamemails,spamemails):
	wVector = {}
	wVector["BIAS"] = 1

	for text in hamemails:
		for word in text:
			if not word in wVector:
				wVector[word] = random.uniform(-0.1,0.1)

	for text in spamemails:
		for word in text:
			if not word in wVector:
				wVector[word] = random.uniform(-0.1,0.1)

	return wVector

def featureTables(hamemails,spamemails):
	spamtable = []
	hamtable = []

	for text in hamemails:
		textCount = {}
		for word in text:
			if not word in textCount:
				textCount[word] = 1
			else:
				textCount[word] += 1
		hamtable.append(textCount)

	for text in spamemails:
		textCount = {}
		for word in text:
			if not word in textCount:
				textCount[word] = 1
			else:
				textCount[word] += 1
		spamtable.append(textCount)

	return hamtable,spamtable

def stopwords():
	stopwordList =[]
	with open("stopwords.txt",'r') as File:
		for line in File:
			for word in line.split():
				stopwordList.append(word.lower())

	return stopwordList

def reading(folderpath,stopBool):
	#Open file
	mail =[]
	stemmer = SnowballStemmer("english")
	stopwordList = stopwords()

	#Get clean text files from the folder
	for filename in glob.glob(os.path.join(folderpath,'*.txt')):
		with open(filename,'r',errors='ignore') as f:
			text = []
			for line in f.readlines():
				for word in line.split():
					word = word.lower()
					cleanword = re.sub(r'\W','',word)
					stemcleanword = stemmer.stem(cleanword)
					if(not stopBool):
						if (stemcleanword != ''):
							text.append(stemcleanword)
					else:
						if ((stemcleanword != '') and (not word in stopwordList)):
							text.append(stemcleanword)

			mail.append(text)

	return mail

def main():
	#Requires 2 folders in current directory:
		#one folder called train with ham+spam folders
		#one folder called test with ham+spam folders

	#Get path
	path = str(pathlib.Path().absolute())

	#Read in training data
	train_hamemails = reading(path+'/train/ham',False)
	train_spamemails = reading(path+'/train/spam',False)
	hamtable,spamtable = featureTables(train_hamemails,train_spamemails)

	#Read in test data w/stopwords
	test_hamemails = reading(path+'/test/ham',False)
	test_spamemails = reading(path+'/test/spam',False)
	hamtableTest,spamtableTest = featureTables(test_hamemails,test_spamemails)

	#TEST w/o STOPWORDS
	#Get probability of each text for each class
	test_hamemailsSTOP = reading(path+'/test/ham',True)
	test_spamemailsSTOP = reading(path+'/test/spam',True)
	hamtableTestS,spamtableTestS = featureTables(test_hamemailsSTOP,test_spamemailsSTOP)

	print("Rate\tIterations\tTraining\t\t Test_with_stopwords\t Test_without_stopwords")

	rates = [0.005,0.02,0.05,0.06,0.08]
	iterations = [4,5,6,7]
	for r in rates:
		for i in iterations:
			weightVector = initialWeight(train_hamemails,train_spamemails)
			weightVector = updateWeight(weightVector,hamtable,spamtable,r,i)
			train_result = (prediction(weightVector,hamtable,"ham")+prediction(weightVector,spamtable,"spam"))/2
			test_result =(prediction(weightVector,hamtableTest,"ham")+prediction(weightVector,spamtableTest,"spam"))/2
			testSTOP_result =(prediction(weightVector,hamtableTestS,"ham")+prediction(weightVector,spamtableTestS,"spam"))/2
			print(r,'\t',i,'\t\t',train_result,'\t',test_result,'\t',testSTOP_result)

main()


#!/usr/bin/env python

from bs4 import BeautifulSoup as bs
import requests

url_va = 'http://historical.elections.virginia.gov/elections/search/year_from:1924/year_to:2016/office_id:1/stage:General'

req_va = requests.get(url_va)
html_va = req_va.content
soup = bs(html_va,'html.parser')

tags = soup.find_all('tr','election_item')

ELECTION_ID=[]
for t in tags:
    year = t.td.text
    year2 = t['id'][-5:]
    i=[year, year2]
    ELECTION_ID.append(i)
    print(year, year2)

with open('ELECTION_ID','w') as ELECTION_ID_file:
    for line in ELECTION_ID:
        ELECTION_ID_file.write(line[0] + ' ' + line[1])
        print(line[0],line[1])


from functions import simpleseive
from time import clock as now

def pb47():
    n = 0
    n1 = []
    n2 = []
    n3 = []
    print(now())
    primes = simpleseive(200000)
    print(now())
    while n < 200000:
        m = n
        n4 = []
        for p in primes:
            #print(m,p)
            if p > m:
                break
            if m % p == 0:
                while m % p == 0:
                    m = m / p
                    #print(m,p)
                n4.append(p)
                if len(n4) == 5:
                    break
        if len(n4) == len(n3) == len(n2) == len(n1)== 4:
            print(n-3,n-2,n-1,n,n*(n-1)*(n-2)*(n-3))
            return
        #print(n1,n2,n3,n4)
        n1,n2,n3 = n2 + [],n3 + [],n4 + []
        n += 1
    print(now())
pb47()


import os,face_recognition,pickle

path = 'C:/xampp/htdocs/website/login/pytest/'

imgtoverify=face_recognition.load_image_file(path+'my.jpg')
toverify = face_recognition.face_encodings(imgtoverify)[0]

db_encs = os.listdir(path+'encodings/')
print(db_encs)

img_num = 1
names = []
person = 'Unknown'

def most_common(lst):
    return max(set(lst), key=lst.count)

for db_enc in db_encs:
	if db_enc.endswith('.enc'):
		enc_file = open(path+'encodings/'+db_enc,'rb')
		enc = pickle.load(enc_file)
		enc_file.close()
		result = face_recognition.compare_faces([enc], toverify, tolerance=0.4)
		print(result)
		name = db_enc[:(db_enc.index('.')-1)]
		if result[0] == True:
			names.append(name)

print (names)
#most common name
if names:
	person = most_common(names)
	if(person!='Unknown'):
		print('photo matched as '+person)
else:
	print('no match found')


import numpy as np
import torch
from torch import nn
from functools import reduce
from operator import mul

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

# nc = 3  # Number of channels in the training images. For color images this is 3
nz = 100  # Size of z latent vector (i.e. size of generator input)
# ngf = 64  # Size of feature maps in generator
# ndf = 64  # Size of feature maps in discriminator

# Create batch of latent vectors that we will use to visualize
#  the progression of the generator
fixed_noise = torch.randn(20, nz, 1, 1, device=device)

class Reshape(nn.Module):
    def __init__(self, *args):
        """
        Запоминает размерности, в которые при проходе
        вперед будут переводиться все объекты.
        Например,
            input = torch.zeros(100, 196)
            reshape_layer = Reshape(1, 14, 14)
            reshape_layer(input)
        возвращает тензор размерности (100, 1, 14, 14).
            input = torch.zeros(100, 1, 14, 14)
            reshape_layer = Reshape(-1)
            reshape_layer(input)
        наоборот вернет тензор размерности (100, 196).
        """
        super(type(self), self).__init__()
        self.dims = args

    def forward(self, input):
        """
        Возвращает тензор с измененными размерностями объектов.
        """
        return input.view(input.size(0), *self.dims)

# custom weights initialization called on netG and netD
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        torch.nn.init.normal_(m.weight, 0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        torch.nn.init.normal_(m.weight, 1.0, 0.02)
        torch.nn.init.zeros_(m.bias)

class Generator(nn.Module):
    def __init__(self, ngpu=1):
        super(Generator, self).__init__()
        self.ngpu = ngpu
        self.main = nn.Sequential(
            Reshape(nz, 1, 1),
            nn.ConvTranspose2d(nz, 128, 4, 1, 0, 0, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.ConvTranspose2d(128, 64, 3, 2, 1, 0, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.ConvTranspose2d(64, 1, 3, 2, 1, 1, bias=False),
            Reshape(-1),
            nn.Sigmoid()
        )
        '''
        self.main = nn.Sequential(
            # input is Z, going into a convolution
            nn.ConvTranspose2d(     nz, ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (nc) x 64 x 64
        )
        '''

    def get_params(self):
        params = []
        for name, module in self.named_modules():
            if len(module._parameters) != 0:
                params.append(module._parameters['weight'].data.view(-1))
                try:
                    params.append(module._parameters['bias'].data.view(-1))
                except:
                    pass
        return torch.cat(params)

    def set_params(self, flat_params):
        # Restore original shapes
        offset = 0
        for module in self.modules():
            if len(module._parameters) != 0:
                weight_shape = module._parameters['weight'].size()
                weight_flat_size = reduce(mul, weight_shape, 1)
                module._parameters['weight'].data = flat_params[
                                                    offset:offset + weight_flat_size].view(*weight_shape)
                try:
                    bias_shape = module._parameters['bias'].size()
                    bias_flat_size = reduce(mul, bias_shape, 1)
                    module._parameters['bias'].data = flat_params[
                                                      offset + weight_flat_size:offset + weight_flat_size + bias_flat_size].view(
                        *bias_shape)
                except:
                    bias_flat_size = 0
                offset += weight_flat_size + bias_flat_size

    def get_params_size(self):
        return self.get_params().size(0)

    def generate_noise(self, num_samples):
        """
        Генерирует сэмплы из априорного распределения на z.
        Возвращаемое значение: Tensor, матрица размера num_samples x d.
        """
        z = np.random.multivariate_normal(mean=np.zeros(self.d),
                                          cov=np.eye(self.d),
                                          size=num_samples)
        z = torch.Tensor(z)
        if next(self.parameters()).is_cuda:
            z = z.cuda()
        return z

    def generate_samples(self, num_samples):
        """
        Генерирует сэмплы из индуцируемого моделью распределения на объекты x.
        Возвращаемое значение: Tensor, матрица размера num_samples x D.
        """
        # z = np.random.multivariate_normal(mean=np.zeros(nz),
        #                                  cov=np.eye(nz),
        #                                  size=num_samples)
        # noise = torch.Tensor(z)
        # samples = self.main(noise)
        samples = self.main(fixed_noise)
        # return torch.Tensor(samples)
        return samples

    def forward(self, input):
        if input.is_cuda and self.ngpu > 1:
            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
        else:
            output = self.main(input)
        return output

class Discriminator(nn.Module):
    def __init__(self, ngpu=1):
        super(Discriminator, self).__init__()
        self.ngpu = ngpu
        self.main = nn.Sequential(
            Reshape(1, 14, 14),
            nn.Conv2d(1, 64, 3, 2, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.LeakyReLU(0.2),
            nn.Conv2d(64, 32, 3, 2, 0, bias=False),
            nn.BatchNorm2d(32),
            nn.LeakyReLU(0.2),
            nn.Conv2d(32, 16, 3, 1, 0, bias=False),
            nn.BatchNorm2d(16),
            nn.LeakyReLU(),
            Reshape(-1),
            nn.Linear(16, 1),
            nn.Sigmoid()
        )
        '''
        self.main = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
            nn.Sigmoid()
        )
        '''

    def get_params(self):
        params = []
        for name, module in self.named_modules():
            if len(module._parameters) != 0:
                params.append(module._parameters['weight'].data.view(-1))
                try:
                    params.append(module._parameters['bias'].data.view(-1))
                except:
                    pass
        return torch.cat(params)

    def set_params(self, flat_params):
        # Restore original shapes
        offset = 0
        for module in self.modules():
            if len(module._parameters) != 0:
                weight_shape = module._parameters['weight'].size()
                weight_flat_size = reduce(mul, weight_shape, 1)
                module._parameters['weight'].data = flat_params[
                                                    offset:offset + weight_flat_size].view(*weight_shape)
                try:
                    bias_shape = module._parameters['bias'].size()
                    bias_flat_size = reduce(mul, bias_shape, 1)
                    module._parameters['bias'].data = flat_params[
                                                      offset + weight_flat_size:offset + weight_flat_size + bias_flat_size].view(
                        *bias_shape)
                except:
                    bias_flat_size = 0
                offset += weight_flat_size + bias_flat_size

    def get_params_size(self):
        return self.get_params().size(0)

    def forward(self, input):
        if input.is_cuda and self.ngpu > 1:
            output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
        else:
            output = self.main(input)

        return output.view(-1, 1).squeeze(1)


class Map:
  '''Creates a map object.'''

  _instance = None
  _initialized = False

  def __new__(cls):
    '''Creates an instance of the class if one hasn't already been created.'''
    if (cls._instance is None):
      cls._instance = super().__new__(cls)
    return cls._instance

  def __init__(self):
    '''Creates an empty map and revealed map list. If a map object has already been created.'''
    if not Map._initialized:
      self._map = []
      self._revealed = []
      Map._initialized = True

  def load_map(self, map_num):
    '''Loads the map based on what level the player is at and resets the revealed map to all False.'''
    #Reads the given "map.txt" and creates a 2D list based on the contents of its individual lines
    if (map_num == 1):
      mapFile = open("map1.txt")
    elif (map_num == 2):
      mapFile = open("map2.txt")
    elif (map_num == 3):
      mapFile = open("map3.txt")
    mapList = mapFile.readlines()
    self._map = [[] for x in range(5)]
    for x in mapList:
      x.replace("\n", "")
      self._map[0].append(x[0])
      self._map[1].append(x[1])
      self._map[2].append(x[2])
      self._map[3].append(x[3])
      self._map[4].append(x[4])
    #Creates a 2D list of the same size, but filled with booleans initially set to False.
    self._revealed = [[False for i in range(5)] for j in range(5)]

  def __getitem__(self, row):
    '''Returns the specified row from the map.'''
    pass

  def __len__(self):
    '''Returns the number of rows in the map list.'''
    return len(self._revealed)

  def show_map(self, loc):
    '''Displays the map to the hero indicating their position and what has/hasn't been explored yet.'''
    #Denotes the hero's position with "*"
    row = loc[0]
    column = loc[1]
    self._revealed[row][column] = "*"

    #Replaces True values with corresponding room.
    lineReset = 0
    for x in range(5):
      for y in range(5):
        if (self._revealed[x][y] == True):
          self._revealed[x][y] = self._map[x][y]
    #Replaces False values with "x."
    for x in self._revealed:
      for y in x:
        if (y == False):
          y = "x"
        print(y, end=" ")
        lineReset += 1
        if (lineReset == 5):
          print("")
          lineReset = 0
    return ""

  def reveal(self, loc):
    '''Changes the specified position in the revealed map to True.'''
    row = loc[0]
    column = loc[1]
    self._revealed[row][column] = True

  def remove_at_loc(self, loc):
    '''Replaces the specified position with an "n."'''
    row = loc[0]
    column = loc[1]
    self._map[row][column] = "n"


def es_primo(n):
    'Calcula si un numero entero es primo o no '
    for i in range (2, int((n+ 1) ** 0.5)):
        if n % i == 0:
            return False
    return True

def filtrar_primos(numeros):
    '''
    Recibe una lista de numeros y devuelve una nueva lista con
    los numeros primos.
    '''
    listafinal= []
    for i in numeros:
        if es_primo(i):
            listafinal += [i]
    return listafinal

def promedio(numeros):
    ' Recibe una lista de numeros y devuelve el promedio'
    suma = 0
    cont = 0
    for num in numeros:
        suma += 1
        cont += 1
    return suma / cont

def factorial_de_numeros(numeros):
    '''
    Recibe una lista de numeros y devuelve una lista  con
    el factorial de cada numero
    '''
    res = []
    for i in numeros:
        res.append(factorial(i))
    return res

def factorial(n):
    'Funcion aux para calcular el factorial.'
    fact = 1
    for i in range(1, n):
        fact = fact * i
    return fact


N = int(input())
bodys = [tuple(map(int, input().split())) for i in [0] * N]
ranks = [1] * N
for i in range(N - 1):
    for j in range(i + 1, N):
        if bodys[i][0] > bodys[j][0] and bodys[i][1] > bodys[j][1]:
            ranks[j] += 1
        elif bodys[i][0] < bodys[j][0] and bodys[i][1] < bodys[j][1]:
            ranks[i] += 1

for rank in ranks:
    print(rank)


from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

PROXY = "88.198.43.234:3128" # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

chrome = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
chrome.get("https://www.google.com/search?q=what+is+my+ip&rlz=1C1ONGR_enUS928US928&oq=what+is+my+ip&aqs=chrome..69i57.2590j0j4&sourceid=chrome&ie=UTF-8")


#
# Purpose: Classify logs wrt their source (eg. Java, Apache, Nagios, etc.)
# Requires: sklearn, numpy, argparse
#

import os
import glob
import argparse
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn import svm, naive_bayes, linear_model, tree, ensemble, neighbors, semi_supervised, neural_network, discriminant_analysis
from sklearn.externals import joblib

parser = argparse.ArgumentParser()
parser.add_argument('--train_data_dir', type=str, default='data/train/laptop',
                    help='data directory containing training logs')
parser.add_argument('--test_data_dir', type=str, default='data/test/laptop',
                    help='data directory containing training logs')
parser.add_argument('--save_dir', type=str, default='save',
                    help='directory to store and load training models')
args = parser.parse_args()

def train(algorithm, training_feature_data, training_target_data):
    model = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', algorithm)])
    model.fit(training_feature_data,training_target_data)
    return model

def predict(model, new_docs):
    predicted = model.predict(new_docs['data'])
    accuracy = np.mean(predicted == new_docs['type'])
    return accuracy

def create_log_dict(logfile_path):
    log_collection = {}
    logfiles = glob.glob(logfile_path + "/*.log") # Get list of log files
    for logfile in logfiles:
        file_handle = open(logfile, "r")
        filedata_array = file_handle.read().split('\n')
        file_handle.close()
        # Remove empty lines
        for line in filedata_array:
            if len(line) == 0:
                del filedata_array[filedata_array.index(line)]
        # Add log file data and type
        if log_collection.has_key('data'):
            log_collection['data'] = log_collection['data'] + filedata_array
            # numerise log type for each line
            temp_types = [logfiles.index(logfile)] * len(filedata_array)
            log_collection['type'] = log_collection['type'] + temp_types # Add log type array
        # Cater for first time iteration
        else:
            log_collection['data'] = filedata_array
            temp_types = [logfiles.index(logfile)] * len(filedata_array)
            log_collection['type'] = temp_types

    return log_collection

def report(clf_type,accuracy):
    print("\033[1m" + clf_type + "\033[0m\033[92m")
    print("Success rate: " + str(round(accuracy * 100,2)) + "%\n")
    print

def save_model(algorithm, model):
    # Create directory to save models if it doesn't exist
    if not os.path.exists(args.save_dir):
        os.makedirs(args.save_dir)
    save_file = str(algorithm).split('(')[0] + '.pkl'
    joblib.dump(model, args.save_dir + "/" + save_file)

algorithms = [
#    svm.SVC(kernel='linear', C = 1.0),   # QUITE SLOW
    linear_model.SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, random_state=42, max_iter=5, tol=None),
    naive_bayes.MultinomialNB(),
    naive_bayes.BernoulliNB(),
    tree.DecisionTreeClassifier(max_depth=1000),
    tree.ExtraTreeClassifier(),
    ensemble.ExtraTreesClassifier(),
    svm.LinearSVC(),
#    linear_model.LogisticRegressionCV(multi_class='multinomial'),   # A BIT SLOW
#    neural_network.MLPClassifier(),   # VERY SLOW
    neighbors.NearestCentroid(),
    ensemble.RandomForestClassifier(),
    linear_model.RidgeClassifier(),
]

train_log_collection = create_log_dict(args.train_data_dir)
test_log_collection = create_log_dict(args.test_data_dir)

print("Training log collection => " + str(len(train_log_collection['data'])) + " data entries")
print("Testing log collection => " + str(len(test_log_collection['data'])) + " data entries")
print

for algorithm in algorithms:
    model = train(algorithm, train_log_collection['data'], train_log_collection['type'])
    accuracy = predict(model,test_log_collection)
    report((str(algorithm).split('(')[0]),accuracy)
    save_model(algorithm, model)


import paddle
import paddle.nn as nn

from .. import PretrainedModel, register_base_model

__all__ = [
    'ErnieMModel', 'ErnieMPretrainedModel', 'ErnieMForSequenceClassification',
    'ErnieMForTokenClassification', 'ErnieMForQuestionAnswering'
]

class ErnieMEmbeddings(nn.Layer):
    r"""
    Include embeddings from word, position.
    """

    def __init__(self,
                 vocab_size,
                 hidden_size=768,
                 hidden_dropout_prob=0.1,
                 max_position_embeddings=514):
        super(ErnieMEmbeddings, self).__init__()

        self.word_embeddings = nn.Embedding(vocab_size, hidden_size)
        self.position_embeddings = nn.Embedding(max_position_embeddings,
                                                hidden_size)
        self.layer_norm = nn.LayerNorm(hidden_size)
        self.dropout = nn.Dropout(hidden_dropout_prob)

    def forward(self, input_ids, position_ids=None):
        if position_ids is None:
            ones = paddle.ones_like(input_ids, dtype="int64")
            seq_length = paddle.cumsum(ones, axis=1)
            position_ids = seq_length - ones
        position_ids += 2
        position_ids.stop_gradient = True
        input_embeddings = self.word_embeddings(input_ids)
        position_embeddings = self.position_embeddings(position_ids)
        embeddings = input_embeddings + position_embeddings
        embeddings = self.layer_norm(embeddings)
        embeddings = self.dropout(embeddings)
        return embeddings

class ErnieMPooler(nn.Layer):
    def __init__(self, hidden_size):
        super(ErnieMPooler, self).__init__()
        self.dense = nn.Linear(hidden_size, hidden_size)
        self.activation = nn.Tanh()

    def forward(self, hidden_states):
        # We "pool" the model by simply taking the hidden state corresponding
        # to the first token.
        first_token_tensor = hidden_states[:, 0]
        pooled_output = self.dense(first_token_tensor)
        pooled_output = self.activation(pooled_output)
        return pooled_output

class ErnieMPretrainedModel(PretrainedModel):
    r"""
    An abstract class for pretrained ERNIE-M models. It provides ERNIE-M related
    `model_config_file`, `pretrained_init_configuration`, `resource_files_names`,
    `pretrained_resource_files_map`, `base_model_prefix` for downloading and
    loading pretrained models.
    Refer to :class:`~paddlenlp.transformers.model_utils.PretrainedModel` for more details.

    """

    model_config_file = "model_config.json"
    pretrained_init_configuration = {
        "ernie-m-base": {
            "attention_probs_dropout_prob": 0.1,
            "hidden_act": "gelu",
            "hidden_dropout_prob": 0.1,
            "hidden_size": 768,
            "initializer_range": 0.02,
            "max_position_embeddings": 514,
            "num_attention_heads": 12,
            "num_hidden_layers": 12,
            "vocab_size": 250002,
            "pad_token_id": 1
        },
        "ernie-m-large": {
            "attention_probs_dropout_prob": 0.1,
            "hidden_act": "gelu",
            "hidden_dropout_prob": 0.1,
            "hidden_size": 1024,
            "initializer_range": 0.02,
            "max_position_embeddings": 514,
            "num_attention_heads": 16,
            "num_hidden_layers": 24,
            "vocab_size": 250002,
            "pad_token_id": 1
        }
    }
    resource_files_names = {"model_state": "model_state.pdparams"}
    pretrained_resource_files_map = {
        "model_state": {
            "ernie-m-base":
            "https://paddlenlp.bj.bcebos.com/models/transformers/ernie_m/ernie_m_base.pdparams",
            "ernie-m-large":
            "https://paddlenlp.bj.bcebos.com/models/transformers/ernie_m/ernie_m_large.pdparams",
        }
    }
    base_model_prefix = "ernie_m"

    def init_weights(self, layer):
        """ Initialization hook """
        if isinstance(layer, (nn.Linear, nn.Embedding)):
            # only support dygraph, use truncated_normal and make it inplace
            # and configurable later
            if isinstance(layer.weight, paddle.Tensor):
                layer.weight.set_value(
                    paddle.tensor.normal(
                        mean=0.0,
                        std=self.initializer_range
                        if hasattr(self, "initializer_range") else
                        self.ernie_m.config["initializer_range"],
                        shape=layer.weight.shape))

@register_base_model
class ErnieMModel(ErnieMPretrainedModel):
    r"""
    The bare ERNIE-M Model transformer outputting raw hidden-states.

    This model inherits from :class:`~paddlenlp.transformers.model_utils.PretrainedModel`.
    Refer to the superclass documentation for the generic methods.

    This model is also a Paddle `paddle.nn.Layer <https://www.paddlepaddle.org.cn/documentation
    /docs/en/api/paddle/fluid/dygraph/layers/Layer_en.html>`__ subclass. Use it as a regular Paddle Layer
    and refer to the Paddle documentation for all matter related to general usage and behavior.

    Args:
        vocab_size (int):
            Vocabulary size of `inputs_ids` in `ErnieMModel`. Also is the vocab size of token embedding matrix.
            Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling `ErnieMModel`.
        hidden_size (int, optional):
            Dimensionality of the embedding layer, encoder layers and pooler layer. Defaults to `768`.
        num_hidden_layers (int, optional):
            Number of hidden layers in the Transformer encoder. Defaults to `12`.
        num_attention_heads (int, optional):
            Number of attention heads for each attention layer in the Transformer encoder.
            Defaults to `12`.
        intermediate_size (int, optional):
            Dimensionality of the feed-forward (ff) layer in the encoder. Input tensors
            to ff layers are firstly projected from `hidden_size` to `intermediate_size`,
            and then projected back to `hidden_size`. Typically `intermediate_size` is larger than `hidden_size`.
            Defaults to `3072`.
        hidden_act (str, optional):
            The non-linear activation function in the feed-forward layer.
            ``"gelu"``, ``"relu"`` and any other paddle supported activation functions
            are supported. Defaults to `"gelu"`.
        hidden_dropout_prob (float, optional):
            The dropout probability for all fully connected layers in the embeddings and encoder.
            Defaults to `0.1`.
        attention_probs_dropout_prob (float, optional):
            The dropout probability used in MultiHeadAttention in all encoder layers to drop some attention target.
            Defaults to `0.1`.
        max_position_embeddings (int, optional):
            The maximum value of the dimensionality of position encoding, which dictates the maximum supported length of an input
            sequence. Defaults to `512`.
        type_vocab_size (int, optional):
            The vocabulary size of the `token_type_ids`.
            Defaults to `2`.
        initializer_range (float, optional):
            The standard deviation of the normal initializer for initializing all weight matrices.
            Defaults to `0.02`.

            .. note::
                A normal_initializer initializes weight matrices as normal distributions.
                See :meth:`ErnieMPretrainedModel._init_weights()` for how weights are initialized in `ErnieMModel`.

        pad_token_id(int, optional):
            The index of padding token in the token vocabulary.
            Defaults to `1`.

    """

    def __init__(self,
                 vocab_size,
                 hidden_size=768,
                 num_hidden_layers=12,
                 num_attention_heads=12,
                 hidden_act="gelu",
                 hidden_dropout_prob=0.1,
                 attention_probs_dropout_prob=0.1,
                 max_position_embeddings=514,
                 initializer_range=0.02,
                 pad_token_id=1):
        super(ErnieMModel, self).__init__()
        self.pad_token_id = pad_token_id
        self.initializer_range = initializer_range
        self.embeddings = ErnieMEmbeddings(vocab_size, hidden_size,
                                           hidden_dropout_prob,
                                           max_position_embeddings)
        encoder_layer = nn.TransformerEncoderLayer(
            hidden_size,
            num_attention_heads,
            dim_feedforward=4 * hidden_size,
            dropout=hidden_dropout_prob,
            activation=hidden_act,
            attn_dropout=attention_probs_dropout_prob,
            act_dropout=0,
            normalize_before=False)
        self.encoder = nn.TransformerEncoder(encoder_layer, num_hidden_layers)
        self.pooler = ErnieMPooler(hidden_size)
        self.apply(self.init_weights)

    def forward(self, input_ids, position_ids=None, attention_mask=None):
        r"""
        Args:
            input_ids (Tensor):
                Indices of input sequence tokens in the vocabulary. They are
                numerical representations of tokens that build the input sequence.
                It's data type should be `int64` and has a shape of [batch_size, sequence_length].
            position_ids (Tensor, optional):
                Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0,
                max_position_embeddings - 1]``.
                Shape as `[batch_size, num_tokens]` and dtype as int64. Defaults to `None`.
            attention_mask (Tensor, optional):
                Mask used in multi-head attention to avoid performing attention on to some unwanted positions,
                usually the paddings or the subsequent positions.
                Its data type can be int, float and bool.
                When the data type is bool, the `masked` tokens have `False` values and the others have `True` values.
                When the data type is int, the `masked` tokens have `0` values and the others have `1` values.
                When the data type is float, the `masked` tokens have `-INF` values and the others have `0` values.
                It is a tensor with shape broadcasted to `[batch_size, num_attention_heads, sequence_length, sequence_length]`.
                For example, its shape can be  [batch_size, sequence_length], [batch_size, sequence_length, sequence_length],
                [batch_size, num_attention_heads, sequence_length, sequence_length].
                Defaults to `None`, which means nothing needed to be prevented attention to.

        Returns:
            tuple: Returns tuple (``sequence_output``, ``pooled_output``).

            With the fields:

            - `sequence_output` (Tensor):
                Sequence of hidden-states at the last layer of the model.
                It's data type should be float32 and its shape is [batch_size, sequence_length, hidden_size].

            - `pooled_output` (Tensor):
                The output of first token (`[CLS]`) in sequence.
                We "pool" the model by simply taking the hidden state corresponding to the first token.
                Its data type should be float32 and its shape is [batch_size, hidden_size].

        Example:
            .. code-block::

                import paddle
                from paddlenlp.transformers import ErnieMModel, ErnieMTokenizer

                tokenizer = ErnieMModel.from_pretrained('ernie-m-base')
                model = ErnieMTokenizer.from_pretrained('ernie-m-base')

                inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
                inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
                sequence_output, pooled_output = model(**inputs)

        """
        if attention_mask is None:
            attention_mask = paddle.unsqueeze(
                (input_ids == 0).astype(self.pooler.dense.weight.dtype) * -1e9,
                axis=[1, 2])
        # For 2D attention_mask from tokenizer
        elif attention_mask.ndim == 2:
            attention_mask = paddle.unsqueeze(
                attention_mask, axis=[1, 2]).astype(paddle.get_default_dtype())
            attention_mask = (1.0 - attention_mask) * -1e9
        attention_mask.stop_gradient = True
        embedding_output = self.embeddings(
            input_ids=input_ids, position_ids=position_ids)
        encoder_outputs = self.encoder(embedding_output, attention_mask)
        sequence_output = encoder_outputs
        pooled_output = self.pooler(sequence_output)
        return sequence_output, pooled_output

class ErnieMForSequenceClassification(ErnieMPretrainedModel):
    r"""
    Ernie-M Model with a linear layer on top of the output layer,
    designed for sequence classification/regression tasks like GLUE tasks.

    Args:
        ernie (ErnieMModel):
            An instance of `paddlenlp.transformers.ErnieMModel`.
        num_classes (int, optional):
            The number of classes. Default to `2`.
        dropout (float, optional):
            The dropout probability for output of ERNIE-M.
            If None, use the same value as `hidden_dropout_prob`
            of `paddlenlp.transformers.ErnieMModel` instance. Defaults to `None`.
    """

    def __init__(self, ernie_m, num_classes=2, dropout=None):
        super(ErnieMForSequenceClassification, self).__init__()
        self.num_classes = num_classes
        self.ernie_m = ernie_m  # allow ernie_m to be config
        self.dropout = nn.Dropout(dropout if dropout is not None else
                                  self.ernie_m.config["hidden_dropout_prob"])
        self.classifier = nn.Linear(self.ernie_m.config["hidden_size"],
                                    num_classes)
        self.apply(self.init_weights)

    def forward(self, input_ids, position_ids=None, attention_mask=None):
        r"""
        Args:
            input_ids (Tensor):
                See :class:`ErnieMModel`.
            token_type_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            position_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            attention_mask (Tensor, optional):
                See :class:`ErnieMModel`.

        Returns:
            Tensor: Returns tensor `logits`, a tensor of the input text classification logits.
            Shape as `[batch_size, num_classes]` and dtype as float32.

        Example:
            .. code-block::

                import paddle
                from paddlenlp.transformers import ErnieMForSequenceClassification, ErnieMTokenizer

                tokenizer = ErnieMTokenizer.from_pretrained('ernie-m-base')
                model = ErnieMForSequenceClassification.from_pretrained('ernie-m-base')

                inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
                inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
                logits = model(**inputs)

        """
        _, pooled_output = self.ernie_m(
            input_ids,
            position_ids=position_ids,
            attention_mask=attention_mask)

        pooled_output = self.dropout(pooled_output)
        logits = self.classifier(pooled_output)
        return logits

class ErnieMForQuestionAnswering(ErnieMPretrainedModel):
    """
    Ernie-M Model with a linear layer on top of the hidden-states
    output to compute `span_start_logits` and `span_end_logits`,
    designed for question-answering tasks like SQuAD.

    Args:
        ernie (`ErnieMModel`):
            An instance of `ErnieMModel`.
    """

    def __init__(self, ernie_m):
        super(ErnieMForQuestionAnswering, self).__init__()
        self.ernie_m = ernie_m  # allow ernie_m to be config
        self.classifier = nn.Linear(self.ernie_m.config["hidden_size"], 2)
        self.apply(self.init_weights)

    def forward(self, input_ids, position_ids=None, attention_mask=None):
        r"""
        Args:
            input_ids (Tensor):
                See :class:`ErnieMModel`.
            token_type_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            position_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            attention_mask (Tensor, optional):
                See :class:`ErnieMModel`.

        Returns:
            tuple: Returns tuple (`start_logits`, `end_logits`).

            With the fields:

            - `start_logits` (Tensor):
                A tensor of the input token classification logits, indicates the start position of the labelled span.
                Its data type should be float32 and its shape is [batch_size, sequence_length].

            - `end_logits` (Tensor):
                A tensor of the input token classification logits, indicates the end position of the labelled span.
                Its data type should be float32 and its shape is [batch_size, sequence_length].

        Example:
            .. code-block::

                import paddle
                from paddlenlp.transformers import ErnieMForQuestionAnswering, ErnieMTokenizer

                tokenizer = ErnieMTokenizer.from_pretrained('ernie-m-base')
                model = ErnieMForQuestionAnswering.from_pretrained('ernie-m-base')

                inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
                inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
                logits = model(**inputs)
        """

        sequence_output, _ = self.ernie_m(
            input_ids,
            position_ids=position_ids,
            attention_mask=attention_mask)

        logits = self.classifier(sequence_output)
        logits = paddle.transpose(logits, perm=[2, 0, 1])
        start_logits, end_logits = paddle.unstack(x=logits, axis=0)

        return start_logits, end_logits

class ErnieMForTokenClassification(ErnieMPretrainedModel):
    r"""
    ERNIE-M Model with a linear layer on top of the hidden-states output layer,
    designed for token classification tasks like NER tasks.

    Args:
        ernie (`ErnieMModel`):
            An instance of `ErnieMModel`.
        num_classes (int, optional):
            The number of classes. Defaults to `2`.
        dropout (float, optional):
            The dropout probability for output of ERNIE-M.
            If None, use the same value as `hidden_dropout_prob`
            of `ErnieMModel` instance `ernie_m`. Defaults to `None`.
    """

    def __init__(self, ernie_m, num_classes=2, dropout=None):
        super(ErnieMForTokenClassification, self).__init__()
        self.num_classes = num_classes
        self.ernie_m = ernie_m  # allow ernie_m to be config
        self.dropout = nn.Dropout(dropout if dropout is not None else
                                  self.ernie_m.config["hidden_dropout_prob"])
        self.classifier = nn.Linear(self.ernie_m.config["hidden_size"],
                                    num_classes)
        self.apply(self.init_weights)

    def forward(self, input_ids, position_ids=None, attention_mask=None):
        r"""
        Args:
            input_ids (Tensor):
                See :class:`ErnieMModel`.
            token_type_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            position_ids (Tensor, optional):
                See :class:`ErnieMModel`.
            attention_mask (Tensor, optional):
                See :class:`ErnieMModel`.

        Returns:
            Tensor: Returns tensor `logits`, a tensor of the input token classification logits.
            Shape as `[batch_size, sequence_length, num_classes]` and dtype as `float32`.

        Example:
            .. code-block::

                import paddle
                from paddlenlp.transformers import ErnieMForTokenClassification, ErnieMTokenizer

                tokenizer = ErnieMTokenizer.from_pretrained('ernie-m-base')
                model = ErnieMForTokenClassification.from_pretrained('ernie-m-base')

                inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!")
                inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()}
                logits = model(**inputs)
        """
        sequence_output, _ = self.ernie_m(
            input_ids,
            position_ids=position_ids,
            attention_mask=attention_mask)

        sequence_output = self.dropout(sequence_output)
        logits = self.classifier(sequence_output)
        return logits


# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import xgboost as xgb
from params import col_nuls,col_less_10, RANDOM_STATE, params_xgb

if __name__ == "__main__":
	# test = pd.concat([pd.read_pickle('data/aggregation/test.pkl', compression='gzip'),
	# 				pd.read_pickle('data/cross/test.pkl', compression='gzip'),
	# 				pd.read_pickle('data/double_cross/test.pkl', compression='gzip')],axis=1)
	# ids_test = pd.read_pickle('data/drop_duplicates/ids_test.pkl', compression='gzip')

	train = pd.concat([pd.read_pickle('data/aggregation/train.pkl', compression='gzip'),
					pd.read_pickle('data/cross/train.pkl', compression='gzip'),
					pd.read_pickle('data/double_cross/train.pkl', compression='gzip')],axis=1)

	#ids_train = pd.read_pickle('data/drop_duplicates/ids_train.pkl', compression='gzip')
	target = pd.read_pickle('data/drop_duplicates/target.pkl', compression='gzip')

	# train = train[list(set(train.columns.values) - set(col_nuls) - set(col_less_10))]
	train = train[list(set(train.columns.values))]

	xgb_train = xgb.DMatrix(train, label=target)
	cv_res = xgb.cv(params_xgb, xgb_train, num_boost_round=1500, nfold=5, stratified=True, )
	num_boost_best = np.argmax(cv_res['test-auc-mean'])
	print (np.argmax(cv_res['test-auc-mean']), np.max(cv_res['test-auc-mean']))
#988 0.7000424
#1052 0.7017934 + id


from statistics import mean
from enum import Enum

from calaldees.attribute_packer import AttributePackerMixin

from . import RGBLight
from ._base import BaseDevice

GLOBOS = Enum('Globo', ('none', 'cross', 'dots', 'crescent_moon', 'target', 'triangle', 'square', 'stars'))

_mean = lambda a, b: mean((a, b))

AttributePackerMixin.AttributeEncoders['globo'] = AttributePackerMixin.AttributeEncoder(
    lambda value: value.value,
    lambda value: GLOBOS(value),
    'B',
)

class EffectRGBLight(RGBLight):

    def __init__(self, *args, x=0.5, y=0.5, globo=GLOBOS.none, globo_rotation=0, device_attributes=(), **kwargs):
        self.x = x
        self.y = y
        self.globo = globo
        self.globo_rotation = globo_rotation
        super().__init__(*args, device_attributes=(
            BaseDevice.DeviceAttribute(name='x', default=x, and_func=_mean, packer_type='onebyte'),
            BaseDevice.DeviceAttribute(name='y', default=y, and_func=_mean, packer_type='onebyte'),
            BaseDevice.DeviceAttribute(name='globo', default=globo, and_func=lambda a, b: a, packer_type='globo'),
            BaseDevice.DeviceAttribute(name='globo_rotation', default=globo_rotation, and_func=_mean, packer_type='plusminusonebyte'),
        ) + device_attributes, **kwargs)

    def __copy__(self):
        return EffectRGBLight(**self.todict())

    def _and_(light1, light2):
        for light in (light1, light2):
            assert isinstance(light, EffectRGBLight)
        RGBLight._and_(light1, light2)


#!/usr/bin/python3

import numpy as np
import math
from matplotlib import pyplot as plt

def cca_show(img, lbl_img, lbl_img_final, cca_list, color='black'):
    '''
    input:
      + img: binary image
      + lbl_img: pre labeled image
      + lbl_img_final: 2nd labeled image
      + cca_list: for annotation
    '''
    INVALID = np.amax(lbl_img)+1
    lbl_img += 1
    lbl_img[lbl_img==INVALID] = 0
    lbl_img_final +=1
    lbl_img_final[lbl_img_final==INVALID]=0

    plt.figure(figsize=(9, 3.5))
    plt.subplot(131)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
    plt.subplot(132)
    plt.imshow(lbl_img, cmap='nipy_spectral')
    plt.axis('off')
    ax = plt.subplot(133)
    plt.imshow(lbl_img_final, cmap='nipy_spectral')
    plt.axis('off')
    # annotation
    font = {'family': 'serif',
            'color':  'white',
            'weight': 'bold',
            'size': 16,
            }
    for grp in cca_list:
        xc,yc,rot, a,b, lbl = grp
        cog = plt.Circle((xc,yc), 0.5, color=color)
        ax.add_patch(cog)
        # plt.text(xc, yc, r'$%d$'%(lbl), fontdict=font)
        # draw arrow
        fc = 'r'
        ec = fc
        rad = rot*math.pi/180.0
        y_scale, x_scale = math.sin(rad)*b, math.cos(rad)*b
        arr = plt.arrow(xc,yc,x_scale,y_scale,
                        fc=fc, ec=ec, width = 1)
        ax.add_patch(arr)
        # plt.text(xc+x_scale, yc+y_scale, r'$%.1f$'%(rot), fontdict=font)
        plt.text(xc-2, yc-4, r'$%.1f$'%(rot), fontdict=font)

    plt.tight_layout()
    plt.show()

def show_img(img, cmap='gray'):
    iscolor = False if len(img.shape)== 2 else True
    if iscolor:
        plt.imshow(img)
    else:
        plt.imshow(img, cmap=cmap)
    plt.show()


from random import randint
from time import clock

##========================================================
class State(object):
    pass

class Walk(State):
    def Execute(self):
        print("The cat in walking!")

class Sleep(State):
    def Execute(self):
        print("The cat is sleeping")

class Meow(State):
    def Execute(self):
        print("The cat is meowing..")

##========================================================
class Transition(object):
    def __init__(self,toState):
        self.toState = toState

    def Execute(self):
        print("Transitioning...")

##========================================================
class SimpleFSM(object):
    def __init__(self,char):
        self.char = char
        self.states = {}
        self.transitions = {}
        self.curState = None
        self.trans = None
        # self.prevState = None

    def SetState(self, stateName):
        self.curState = self.states[stateName]

    def Transition (self,transName):
        self.trans = self.transitions[transName]

    def Execute(self):
        if(self.trans):
            self.trans.Execute()
            self.SetState(self.trans.toState)
            self.trans = None
        self.curState.Execute()
##========================================================

class Char(object):
    def __init__(self):
        self.FSM = SimpleFSM(self)
        self.Walk = True

##========================================================

if __name__ == "__main__":
    cat = Char()

    cat.FSM.states["Walk"] = Walk()
    cat.FSM.states["Sleep"] = Sleep()
    cat.FSM.states["Meow"] = Meow()
    cat.FSM.transitions["toWalk"] = Transition("Walk")
    cat.FSM.transitions["toSleep"] = Transition("Sleep")
    cat.FSM.transitions["toMeow"] = Transition("Meow")

    cat.FSM.SetState("Walk")

    for i in range(10):
        startTime = clock()
        timeInterval = .5
        randomInteger = randint (1,3)

        while (startTime + timeInterval > clock()):
            pass
        if (randint(0,2)):
            cat.FSM.Transition("toSleep")
            cat.Meow = False
            cat.Walk = False
        elif (randint(3,4)):
            cat.FSM.Transition("toMeow")
            cat.Sleep = False
            cat.Walk = False
        else:
            #Mangler å få til toWalk og toMeow
            cat.FSM.Transition("toWalk")
            cat.Walk = True
        cat.FSM.Execute()

Python/Arbeid fra youtube-Trevor Payne/robotCat.py


import socket
import configparser
import sys
import random
import time
import select
from functions import parse_ports, create_table, INFINITY, process_update, send_update, print_routing_table, extract_fields

def main(argv):

    loop = True

    try:
        configFile = argv[1]
    except (IndexError):
        return 'Missing file name'

    try:
        config = configparser.ConfigParser()
        config.read(configFile);

        router_id = int(config['ROUTER']['routerid'])
        input_ports_str = config['ROUTER']['inputports']
        outputs_str = config['ROUTER']['outputports']
        timeout = int(config['TIMER']['timeout'])
        update = int(config['TIMER']['update'])

        if router_id < 1 or router_id > 64000:
            raise ValueError('Router id is not in the specified range')

        input_ports = []
        input_ports_list = input_ports_str.split(',')
        for port in input_ports_list:
            input_port = int(port.strip())
            input_ports.append(input_port)

        parse_ports(input_ports)

        output_ports = []
        output_costs = []
        output_routerids = []
        outputs = outputs_str.split(',')
        for output in outputs:
            output_info = output.split('-')
            output_ports.append(int(output_info[0]))
            output_costs.append(int(output_info[1]))
            output_routerids.append(int(output_info[2]))

        parse_ports(output_ports)

        for i in range(0, len(output_ports)):
            for j in range(0, len(input_ports)):
                if output_ports[i] == input_ports[j]:
                    raise ValueError('A pair of input/output ports are equal')

        if timeout / update != 6:
            raise ValueError('Incorrect timeout/update timer ratio')

    except ValueError as e:
        print('ERROR: ' + str(e))
        sys.exit()

    in_socks = []
    for port in input_ports:
        host = socket.gethostname()
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((host, port))
        in_socks.append(sock)

    out_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    routing_table = create_table(output_costs, output_routerids, output_ports, timeout)
    print_routing_table(routing_table)

    while loop == True:

        time_before = time.time()

        #if triggered_updates_table.length > 0:
            #triggered_update(triggered_updates_table)

        offset = random.randrange(-200, 200, 1)
        update_timer = update * (1 + (offset / 1000))

        readables, writables, exceptionables = select.select(in_socks, [], [], update_timer)

        if readables:
            for sock in readables:
                print("Hello received packet thankyou")
                packet = sock.recv(2**16)
                entries, source_router = extract_fields(packet)
                process_update(routing_table, entries, source_router, output_ports)

        time_after = time.time()

        for i in range(0, len(routing_table)):
            entry = routing_table[i]

            if entry.timeout != 420:
                entry.timeout -= time_after - time_before
                if entry.timeout >= timeout:
                    entry.cost = INFINITY
                    entry.timeout = 420
                    entry.change_flag = 1
                    #triggered_update_table.append(entry)
            else:
                entry.garbage_timer -= time_after - time_before
                if entry.garbage_timer <= 0:
                    delete_route(entry)

        send_update(routing_table, output_ports, output_routerids, out_sock, router_id)
        print_routing_table(routing_table)

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


import click
from zipline.data.bundles import register
from zipline.data import bundles as bundles_module
from cn_zipline.bundles.tdx_bundle import tdx_bundle
import pandas as pd
import os
from cn_stock_holidays.zipline.default_calendar import register_calendar

@click.group()
def main():
    pass

@main.command()
@click.option(
    '-b',
    '--bundle',
    default='quantopian-quandl',
    metavar='BUNDLE-NAME',
    show_default=True,
    help='The data bundle to ingest.',
)
@click.option(
    '--assets-version',
    type=int,
    multiple=True,
    help='Version of the assets db to which to downgrade.',
)
@click.option(
    '--show-progress/--no-show-progress',
    default=True,
    help='Print progress information to the terminal.'
)
def ingest(bundle, assets_version, show_progress):
    if bundle == 'tdx':
        register('tdx', tdx_bundle, 'SHSZ')
    bundles_module.ingest(bundle,
                          os.environ,
                          pd.Timestamp.utcnow(),
                          assets_version,
                          show_progress,
                          )

def register_tdx():
    register('tdx', tdx_bundle, 'SHSZ')

if __name__ == '__main__':
    register('tdx1', tdx_bundle, 'SHSZ')
    bundles_module.ingest('tdx1',
                          os.environ,
                          pd.Timestamp.utcnow(),
                          show_progress=True,
                          )
    # main()


import pandas as pd
import jax
import jax.numpy as np
import os
import pickle as pickle
import numpy as onp
import bisect
import json
import itertools
from itertools import chain, combinations

from funcs_LNC import *

def readMeta(folder, file):
    meta = pd.read_csv(folder+file, sep="#", nrows=19, header=None)
    meta = [el.replace(" ","").split("=",2) for el in meta[1].tolist()]
    meta  = {el[0]:el[1] for el in meta}
    meta["latents"] = int(meta["latents"])
    meta["confounder"] = int(meta["confounder"])
    meta["proxy"] = int(meta["proxy"])
    meta["noisesd"] = float(meta["noisesd"])
    meta["independent"] = meta["independent"]=="TRUE"
    meta["noiseproxy"] = int(meta["noiseproxy"])
    meta["ncl"] = int(meta["ncl"])
    meta["size"] = int(meta["size"])
    meta["ix"] = [int(el) for el in meta["ix"].replace("c(","").replace(")","").replace(" ","").split(",")]
    meta["iy"] = [int(el) for el in meta["iy"].replace("c(","").replace(")","").replace(" ","").split(",")]
    meta["ic"] = int(meta["ic"])
    meta["causal_coeff"] = float(meta["causal_coeff"])
    meta["coefx"] = [float(el) for el in meta["coefx"].replace("c(","").replace(")","").replace(" ","").split(",")]
    meta["coefy"] = [float(el) for el in meta["coefy"].replace("c(","").replace(")","").replace(" ","").split(",")]
    return meta

def readFile(folder, file):
    meta = readMeta(folder, file)
    num_proxies = meta["proxy"]
    num_latents = meta["latents"]

    data = pd.read_csv(folder+file, skiprows=19)
    zcols = ["Z."+str(i) for i in range(1,num_latents+1)]
    ucols = ["U."+str(i) for i in range(1,num_proxies+1)]
    x= np.array(data["X"])[:,None]
    x_std = np.std(x)
    x = stdrze(x)
    y= np.array(data["Y"])[:,None]
    y_std = np.std(y)
    y = stdrze(y)
    Z= np.array(onp.apply_along_axis(stdrze, 0, data[zcols]))
    U= np.array(onp.apply_along_axis(stdrze,0, data[ucols]))
    idx_x = np.array(meta["ix"]) #np.array([7, 1, 2])
    idx_y = np.array(meta["iy"]) #np.array([0, 6, 5])
    idx_c = np.array([meta["ic"]]) ##np.array([9])
    idxs = (idx_x, idx_y, idx_c)
    beta_real = np.array(meta["causal_coeff"])#/y_std*x_std)
    stds = (x_std, y_std)
    return x, y, Z, U, meta, idxs, beta_real, stds

def getDataSetTab(repos, pars):
    files = os.listdir(repos)
    files = list(set(files).difference(set(["log.txt"])))
    parsList = [pars[k] for k in pars.keys()]
    combos = [list(it) for it in itertools.product(*parsList)]
    combos = [[combos[j][i] for j in range(len(combos))] for i in range(len(combos[0]))]
    combos = {k: v for k, v in zip(pars.keys(), combos)}

    aux = {"fileNames": files}
    aux = pd.DataFrame.from_dict(aux)
    datasetTab2 = {"fileNames": [f for f in files for i in range(len(combos["lambda"]))]}

    for par in pars.keys():
        datasetTab2[par] = [p for f in files for p in combos[par]]

    datasetTab2 = pd.DataFrame.from_dict(datasetTab2)
    datasetTab2 = datasetTab2.merge(aux, on="fileNames", sort=True)
    datasetTab2["job"] = onp.arange(1, datasetTab2.shape[0]+1)
    return datasetTab2

def readGetMsrs(folder, file, job):
    if int(job) % 5000 == 0:
        print("job: ", job)
    pathFile = folder+file+"_"+job+".pkl"
    if not os.path.isfile(pathFile):
        return None
    res = pickle5.load( open(pathFile, "rb" ) )
    n = res["Z"]["path"]["loss"].shape[0]-1
    df = {k:[res["Z"]["path"][k][n,0,0]] for k in res["Z"]["path"].keys()}
    msrs = pd.DataFrame(df)

    def convPars(x):
        if type(x) == onp.ndarray:
            res = onp.round(x, 6)[0]
        else:
            res = x
        return res

    parsJob = res["pars"]
    parsJob = {k: convPars(parsJob[k]) for k in parsJob.keys()}
    for k in parsJob.keys():
        msrs[k] = parsJob[k]

    dataInfo = res["dataInfo"]
    for k in dataInfo.keys():
        msrs[k] = dataInfo[k]

    keepMeta = ["rep","latents","confounder","proxy","dist","distsd","noise","noisesd","independent","noiseproxy","ncl","size"]
    dataInfo = res["meta"]
    for k in keepMeta:
        msrs[k] = dataInfo[k]

    return msrs


import spacy
nlp = spacy.load('en_core_web_lg')

txt = " ".join(open('./apple_earnings.txt','r').readlines())
doc = nlp(txt)
sentences = [{'text':str(d), 'labels':[]} for d in doc.sents]

import pandas as pd
import json

with open('e_sentences.json', 'w+') as csvfile:
    #import pdb; pdb.set_trace()
    for s in sentences:
        csvfile.write(json.dumps(s)+"\n")
    #csvfile.write(json.dumps(sentences))
        #print(s)
        #import pdb; pdb.set_trace()
        #csvfile.write(json.dump(s))

# import csv
# with open('e_sentences.csv', 'w', newline='') as csvfile:
#     spamwriter = csv.writer(csvfile,
#                            )
#     spamwriter.writerow(sentences)