import json,os,requests,threading

class B():
    data = {}
    all = []

def alist(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]

def th(list):
    for i in range(0,len(list)):
        threading.Thread(target=download, args=[list[i]]).start()

def openjson(path):
    with open(path,'r') as r:
        text = r.read()
    B.data = json.loads(text)
    B.data = B.data['libraries']

def arrangement(list):
    after = []
    for i in list:
        after.append([i['downloads']['artifact']['path'],i['downloads']['artifact']['url']])
        if 'classifiers' in i['downloads']:
            if 'javadoc' in i['downloads']['classifiers']:
                after.append([i['downloads']['classifiers']['javadoc']['path'], i['downloads']['classifiers']['javadoc']['url']])
            if 'natives-windows' in i['downloads']['classifiers']:
                after.append([i['downloads']['classifiers']['natives-windows']['path'], i['downloads']['classifiers']['natives-windows']['url']])

    return after

def download(list):
    #os.makedirs('libraries')

    for i in list:
        try:
            if not(os.path.isdir(os.path.split(i[0])[0])):
                os.makedirs(os.path.split(i[0])[0])
        except:
            if not(os.path.isdir(os.path.split(i[0])[0])):
                os.makedirs(os.path.split(i[0])[0])
        print('下载%s中...' % (os.path.split(i[0])[1]))
        while True:  #套个死循环，还下不出来就卡死算了......
            try:
                file = requests.get(url=i[1])
                with open(i[0], "wb") as code:
                    code.write(file.content)
                break
            except:
                pass
        print('下载完成！')

if __name__ == '__main__':
    print('MC本体json')
    path = input('>>>')
    #path = r'E:\mctest\.minecraft\versions\1.16.5\1.16.5.json'
    openjson(path)
    pAu = arrangement(B.data)
    work = alist(pAu,16)
    for i in work:
        B.all.append(i)
    th(B.all)
    #download(pAu)
    #os.makedirs('libraries')


# Patrick Flathers
# 9/26/14
# make a pong game in which the paddles are 80 by 20 pixels and (a and z) and (k and m)
#are the controls for the paddles

#9/28 Note to self
#have to convert to radians in order to use sin and cos in python

#9/29 Converted to radians,bouncing is still a little random due to cs1lib
#bouncing is reasonable though
#Next is to create range of where paddle is to know where the ball bounces.

#9/30 make local variables for the globals maybe
#request redraw and then sleep

from cs1lib import *
from time import time, sleep
from math import *

#screen bounds
screen_y = 400
screen_x = 400

#paddle Bounds
top_y = 0
bottom_y = 320

#ball position
ball_x = 200
ball_y = 200

#angle at which the ball travels
direction = 50

def background(): #makes the playing field, is a function for redrawing

    set_fill_color(0,0,0)
    draw_rectangle(0,0, screen_x, screen_y)

def paddles(x1,y1,x2,y2):#makes the paddles
    set_fill_color(0,.75,0)
    draw_rectangle(x1,y1,x2,y2)

def p1_up(p1_y1):#the following functions move the paddles
    if (p1_y1 <= top_y):
                return p1_y1
    else:
                p1_y1 -= 10
                return p1_y1

def p1_down(p1_y1):
    if (p1_y1 >= bottom_y):
                return p1_y1
    else:
                p1_y1 += 10
                return p1_y1

def p2_up(p2_y1):

    if (p2_y1 <= top_y):
                return p2_y1
    else:
               p2_y1 -= 10
               return p2_y1

def p2_down(p2_y1):

    if (p2_y1 >= bottom_y):
                return p2_y1

    else:
                p2_y1 += 10
                return p2_y1

def redraw_paddles(p1_x1,p1_y1,p1_width,p1_height,p2_x1,p2_y1,p2_width,p2_height):#allows the paddles to move

    background()
    paddles(p1_x1,p1_y1,p1_width,p1_height)
    paddles(p2_x1,p2_y1,p2_width,p2_height)

def redraw_ball(p1_y1, p2_y1): #moves the ball
    global ball_x
    global ball_y
    global direction

    speed = 10 # this determines how fast the ball moves

    if (ball_y <= 0): # these two if statements allow bouncing on the horizontal walls
        direction = (-1*direction) % 360

    elif (ball_y >= screen_y):
        direction = (-1*direction) % 360

    if (ball_x <= 20 and p1_y1 + 80 >= ball_y >= p1_y1):# these if statements allow the ball to bounce off the paddles

            bounce()

    elif (ball_x >= screen_x - 20 and p2_y1 + 80 >= ball_y >= p2_y1):

            bounce()

    elif (screen_x < ball_x or ball_x < 0): # this allows makes a loop so you can reset to a new match
            set_stroke_color(.75,0,0)
            draw_text("PRESS SPACEBAR TO RESTART", 100,100)
            set_stroke_color(0,0,0)
            while screen_x < ball_x or ball_x < 0:
                end_game()
                if (is_key_pressed(" ") ):
                     reset()
                     break

    direction_r = radians(direction) # sine and cosine need radians to work right

    ball_y +=-1 * speed * sin(direction_r)#new ball coordinates
    ball_x += speed * cos(direction_r)

    set_fill_color(.75,0,0)

    draw_circle(ball_x,ball_y,10)

def reset(): # resets the ball coordinates
    global direction
    global ball_x
    global ball_y
    background()
    ball_x = 200
    ball_y = 200

    direction = 50

def bounce (): # allows x bouncing

    global direction

    direction = (180 - direction) %360

def end_game():# closes the window
    if (is_key_pressed("q") == True):
        cs1_quit()

def main():#main :)
    global ball_x
    global ball_y
    p1_x1 = 0
    p1_y1 = 160
    p1_width = 20
    p1_height =  80

    p2_x1 = screen_x - 20
    p2_y1 = 160
    p2_width = screen_x
    p2_height =  80

    set_fill_color(.75,0,0)
    draw_circle(ball_x,ball_y,10)

    background()

    paddles(p1_x1,p1_y1,p1_width,p1_height)
    paddles(p2_x1,p2_y1,p2_width,p2_height)
    while not window_closed():

            set_stroke_color(.75,0,0)
            draw_text("PRESS SPACEBAR TO START", 100,100)
            set_stroke_color(0,0,0)

            end_game()
            if (is_key_pressed(" ") ):
                    reset()
                    break

    while not window_closed():
        if (is_key_pressed("a") or is_key_pressed("A")):
           p1_y1 = p1_up(p1_y1)
        elif(is_key_pressed("z") or is_key_pressed("Z")):
            p1_y1 = p1_down(p1_y1)
        if(is_key_pressed("k") or is_key_pressed("K")):
            p2_y1 = p2_up(p2_y1)
        elif(is_key_pressed("m") or is_key_pressed("M")):
            p2_y1 = p2_down(p2_y1)

        redraw_paddles(p1_x1,p1_y1,p1_width,p1_height,p2_x1,p2_y1,p2_width,p2_height)

        redraw_ball(p1_y1, p2_y1)
        sleep(.02)
        clear()
        end_game()
        request_redraw()
start_graphics(main, "Pong", 400,400)


from flask import Blueprint, request, jsonify
from url_shortener.core.url_core import save_url

api = Blueprint('api', __name__, url_prefix='/api/v1')

@api.route('/url', methods=['POST'])
def add_url():
    """
    Example of request: {"url": "http://some-url.com/bla-bla-bla"}
    Example of response: {"short_url": "http://yourdomain.com/6QFT8r8J"}
    """
    data = request.get_json(force=True)
    short_url = save_url(data['url'], domain=request.url_root)
    return jsonify({'short_url': short_url})


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 16 10:26:50 2019

@author: dell
"""

import webbrowser
import urllib.request
from bs4 import BeautifulSoup
import random
import re
import Project_1
import os
import random
import numpy as np
import csv
import requests

mx=np.argmax(Project_1.emoji)
mx
base='C:/Users\dell\Desktop\musicmood-master\musicmood-master\code\classify_lyrics\make_folder'
if(mx==2 or mx==3):
    pre='happy'
elif(mx==1 or mx==0):
    pre='sad'
x=random.randint(0,9)
#print(x)
path_dir=os.path.join(base,pre)
links = []
listr=os.listdir(path_dir)
print(len(listr))
for i in range(0,len(listr)):
    deep=open(os.path.join(path_dir,listr[i]),'r')
    s=""
    c=0
    for i in deep.readlines():
        c=c+1
        s+=i[:-1]+' '
    value1=s[0:100]
    links = []
    textToSearch = value1
    print(value1)
    query = urllib.parse.quote(textToSearch)
    url = "https://www.youtube.com/results?search_query=" + query
    response = urllib.request.urlopen(url)
    html = response.read()
    soup = BeautifulSoup(html, 'html.parser')

    for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
        links.append('https://www.youtube.com' + vid['href'])
    print(links[0])
    filesInChannel = [ links[0] ]
    def getStats(link):

        page = requests.get(link)
        likes = re.search("with (\d*.\d*.\d*)", page.text).group(1)
        title = re.search("property=\"og:title\" content=\"([^\n]*)", page.text).group(1)
        return (likes, title)

    for link in filesInChannel:
        stats = getStats(link)
        #print (stats[0].encode("utf-8") + " " + stats[1].encode("utf-8"))
        row = [stats[0],stats[1]]

    with open('abcde.csv', 'a') as csvFile:
        writer = csv.writer(csvFile)
        writer.writerow(row)
    csvFile.close()

a = []
max1        = -1
valuee = ''
with open('abcde.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        if (row):
            #print(row[0])
            if(type(row[0]) == int):
                print(row[0])
                if (row[0]>max1):
                    max1=row[0]
                    valuee =row[1]

            #a.append([row[0],row[1]])

#for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
 #   links.append('https://www.youtube.com' + vid['href'])
#print(links)

print("")
#print(value1)
#p = random.randint(0,len(links))
url1 = links[0]
filesInChannel = [
"https://www.youtube.com/watch?v=-Ox9MvottBI"
]
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open(url1)


import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV
import os
import sys
import pandas as pd

# Keras
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.wrappers.scikit_learn import KerasClassifier

import scipy.io as sio
import matplotlib.patches as patches
from PIL import Image, ImageDraw
import numpy as np
import cv2
import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
import torch
import matplotlib.pyplot as plt
import matplotlib

class YoloModel:
    def __init__(self, weights, config, coco):
        self.weights = weights
        self.config = config
        self.coco = coco
        self.output_layers = []
        self.net = self.load_model()
        self.classes = open(self.coco).read().split("\n")

    def load_model(self):
        net = cv2.dnn.readNetFromDarknet(self.config, self.weights)
        net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
        layer_names = net.getLayerNames()
        self.output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
        return net

    def transform_bounding_box(self,box, shape=(416,416)):

        box = box * np.array([shape[1], shape[0], shape[0], shape[1]]) #width, height, width, height => scale the image
        box = [int(box[0] - (box[2] / 2)), int(box[1] - (box[3] / 2)), int(box[2]), int(box[3])] #xmin, ymin, width, height
        return box

    def get_label_score(self,scores, label="person"):
        label_index = self.classes.index(label)
        return scores[label_index]

    def plot_boxes(self, img, boxes):
        fig,ax = plt.subplots(1)
        ax.imshow(img)#.permute(1, 2, 0))
        for box in boxes:
            xmin = box[0]
            ymin = box[1]
            w = box[2]
            h = box[3]
            color = (1,0,1,0.99)
            rect = matplotlib.patches.Rectangle((xmin,ymin),xmin+w,ymin+h,edgecolor=color,facecolor='none')
            ax.add_patch(rect)
        plt.show()

    def predict(self, img, label="person", threshold=0.70, nms_threshold=0.4,plot=True):
        img = cv2.resize(img, (416,416))
        blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=True)
        self.net.setInput(blob)

        feed_forward_output = self.net.forward(self.output_layers)
        classes = open(self.coco).read().split("\n")
        person_index = self.classes.index("person")

        boxes = []
        scores = []

        for output in feed_forward_output:
            for detection in output: #shape of detection is (85,1) => index 0-4 for bounding box, index 5-85 for scores for each class in coco.names
                label_score = self.get_label_score(detection[5:],label)
                box = detection[0:4] #center X, center Y, width, height

                box = self.transform_bounding_box(box, (416,416))
                if(label_score > threshold):
                    boxes.append([box[0],box[1],box[2],box[3]]) #read somewhere that NMSBoxes only takes lists, not np arrays
                    scores.append(float(label_score))

        non_overlap_indices = cv2.dnn.NMSBoxes(boxes, scores, threshold, nms_threshold)
        if(type(non_overlap_indices) is not tuple):
            top_boxes = [boxes[int(i)] for i in non_overlap_indices.flatten()]
            if(plot):
                self.plot_boxes(img,top_boxes)

            return {"boxes": top_boxes, "label_count":len(top_boxes) }
        else:
            return {"boxes": [], "label_count":0}

# yolo = YoloModel("yolov3.weights", "yolov3.cfg", "coco.names")
# test = cv2.imread("../crowd_images/mask1.jpg")#, cv2.COLOR_BGR2RGB)
# print(yolo.predict(test)["label_count"])

COVID_Face Mask Detection in Crowd_PetersonWilliam_Garimella_Taylor/models/yolo_model.py


"""
Problem Link: https://leetcode.com/problems/intersection-of-two-arrays/

Given two arrays, write a function to compute their intersection.

Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:
Each element in the result must be unique.
The result can be in any order.
"""
class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        d1,d2 = {}, {}
        for n in nums1:
            d1[n] = 0
        for n in nums2:
            if n in d1:
                d2[n] = 0
        return d2.keys()


from pygame import mixer
from Voice import speakmodule
from actions.check_cmd import CheckCommand
import speech_recognition as sr
#from Ears.ears import Ears

class Brain():

	# def __init_(self):
	# 	self.print_welcome()

	def print_welcome(self):

		welcome_message =  """
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		+++++++++++++++++++++  Welcome to V.P.D.A  +++++++++++++++++++++
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		+                                                              +
		+           I am J.A.R.V.I.S, How can I help You!              +
		+                                                              +
		++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			"""
		print (welcome_message)

	def text_mode(self):
		self.print_welcome()
		msg="Ready For your Command"
		#speakmodule.speak([msg],len(msg[0:]),mixer)
		CC = CheckCommand()
		while 1:
			cmd = input("> ")
			print(cmd)
			task = CC.check(cmd)
			if task == True:
				print("What else i can do for you")
			if task == False:
				print("Task is Not Complete, Please Can U type it Again!")

	def voice_mode(self):
		self.print_welcome()
		#ears = Ears()
		CC = CheckCommand()

		while 1:
			#cmd = ears.listen()
			r = sr.Recognizer()
			with sr.Microphone() as source:
				audio = r.adjust_for_ambient_noise(source)
				print("Say something!")
				audio = r.listen(source)
				print(audio)

			try:
				s = (r.recognize_google(audio))
				message = (s.lower())
				print (message)
				task=CC.check(message)
				if task == True:
					print("What else i can do for you")
				if task == False:
					print("Task is Not Complete, Please Can U Say it Again!")

			except sr.UnknownValueError:
				print("$could not understand audio")
			except sr.RequestError as e:
				print("Could not request results$; {0}".format(e))

	def remote_mode(self):
		pass
		self.print_welcome()


import re

# 5) Write a Python program that matches a string that has an a followed by three 'b'
def text_match(text):
    patterns = 'ab{3}?'
    if re.search(patterns, text):
        return 'Found a match!'
    else:
        return 'Not matched!'

print(text_match("abbb"))
print(text_match("aabbbbbc"))


import random

#Define user_input_secret
user_input_secret = 0;
#Define computer guessing variable
computer_guess = 0
attempt = 1

#User number input loop
while user_input_secret < 1 or user_input_secret > 100:
    user_input_secret = input("Please type a number between 1 and 100: ")
    user_input_secret = int(user_input_secret)
    if user_input_secret < 1:
        print("The number you wrote is less than 1, please try again: ")
        user_input_secret = int(user_input_secret)
    elif user_input_secret > 100:
        print("The number you wrote is more than 100, please try again: ")
        user_input_secret = int(user_input_secret)
#Computer guess confirmation
input("Press any key for computer to try guessing your number.")

#Generate a random number and store it as a variable
# next_guess
computer_guess = random.randint(1, 100)
computer_min_guess = 1
computer_max_guess = 100
guess_status = 0
#Computer guessing loop
while guess_status == 0:
    if computer_guess == user_input_secret:
        print("Computer's attempt #" + str(attempt))
        print("The computer guessed " + str(computer_guess) + ", which is.. CORRECT")
        guess_status = 1
    elif computer_guess > user_input_secret:
        print("Computer's attempt #" + str(attempt))
        print("The computer guessed " + str(computer_guess) + ", which is.. more than the secret number")
        attempt += 1
        computer_max_guess = computer_guess - 1
        computer_guess = random.randint(computer_min_guess, computer_max_guess)
        input("Press any key for the computer to try again")
    elif computer_guess < user_input_secret:
        print("Computer's attempt #" + str(attempt))
        print("The computer guessed " + str(computer_guess) + ", which is.. less than the secret number")
        attempt += 1
        computer_min_guess = computer_guess + 1
        computer_guess = random.randint(computer_min_guess, computer_max_guess)
        input("Press any key for the computer to try again")


import os
import slack
import certifi
import ssl
from flask import Flask, request
from flask_cors import CORS, cross_origin
from flask_restful import Api, Resource

app = Flask(__name__)
api = Api(app)

CORS(app, support_credentials=True)

ssl_context = ssl.create_default_context(cafile=certifi.where())

SLACK_API_TOKEN = os.environ['SLACK_BOT_TOKEN']

client = slack.WebClient(token=SLACK_API_TOKEN, ssl=ssl_context)

def send_message_slack(fullname_arg: str, emailaddress_arg: str, cloud_pak_arg: str, gaia_arg: str, op_arg: str ):
    data = {
            'Full Name':fullname_arg,
            'Email Address':emailaddress_arg,
            'Cloud Pak':cloud_pak_arg,
            'GAIA':gaia_arg,
            'Opportunity Description':op_arg
        }
    client.chat_postMessage(channel="website-app",text=data)

class SendMessage(Resource):
    def post(self):
        data = request.json
        fullname: str = data['fullname']
        emailaddress: str = data['emailaddress']
        cloud_pak: str = data['cloudpakvalue']
        gaia: set = data['gaia']
        opportunity_description: str = data['requestdescription']
        try:
            send_message_slack(fullname, emailaddress, cloud_pak, gaia, opportunity_description)
            return True
        except Exception as identifier:
            return identifier

api.add_resource(SendMessage,'/api/v1/sendmessage/')


#!/usr/bin/env python3
import wikipedia
import geopandas as gpd
import pandas as pd
import numpy as np
import argparse
from pathlib import Path

# Set up command line arguments
parser = argparse.ArgumentParser(
    description='A script to gather popup and geometry information from Wikipedia for an input .csv list of assets')
parser.add_argument("input_file", default=None, type=str,
                    help="Input file with full path")
parser.add_argument("place_name", default=None, type=str,
                    help="The name of the city in which the assets are situated")

args = parser.parse_args()
input_file = args.input_file
place_name = args.place_name

# Define function to obtain coordinates for each asset. Handle exceptions for
# articles which do not have coordinate information should they arise.
# (Thanks to @AdamP for this one)
def getCoords(x):
    coords = ""
    try:
        coords = [wikipedia.WikipediaPage(title=x).coordinates]
    except:
        coords = np.nan
    return coords

# place_name = 'Bath'
# First read the csv of assets to a dataframe
df = pd.read_csv(input_file,
                 header=None, names=['assets'])

# Ensure data is type = string
df.assets = df.assets.astype(str)

# Pre-process the asset names for search. Append the place name to any asset
# that doesn't already have it to aid search.

df.loc[df['assets'].str.contains(place_name), 'append_place_name'] = df['assets']

df.loc[~df['assets'].str.contains(
    place_name), 'append_place_name'] = df['assets'] + ', ' + place_name

# Run a fuzzy search to return the actual Wikipedia article titles that should be searched against
df['suggested_article_title'] = [wikipedia.search(
    x, results=1, suggestion=False) for x in df['append_place_name']]

# Remove sqare brackets from results
df['suggested_article_title'] = df['suggested_article_title'].str.get(0)

# Re-construct the overall article URL and a hyperlink to be inserted at the end of the summary:
df['article_url'] = "https://en.wikipedia.org/wiki/" + \
    df['suggested_article_title'].astype(str).replace(' ', '_', regex=True)
df['article_hyperlink'] = "Read More"

# Obtain the summary paragraph for each article
df['summary'] = [wikipedia.summary(x) for x in df['suggested_article_title']]

# Append a read more link with the article URL to the end of the summary:
df['summary'] = df['summary'] + "\n\n" + df['article_hyperlink']

# Obtain the main image URL
df['image_url'] = [wikipedia.WikipediaPage(
    title=x).images[1] for x in df['suggested_article_title']]

# Obtain the coordinates of the asset
df['coordinates'] = [getCoords(x) for x in df['suggested_article_title']]

df['lat'] = df['coordinates'][0][0][0]
df['lon'] = df['coordinates'][0][0][1]

df['lat'] = df['lat'].astype(float)
df['lon'] = df['lon'].astype(float)

# Convert the dataframe to a geodataframe
gdf = gpd.GeoDataFrame(df[['assets', 'suggested_article_title', 'summary',
                           'image_url', 'lat', 'lon']], geometry=gpd.points_from_xy(df.lon, df.lat))

export_to_csv = gdf.to_csv(r'./data/export.csv')


import sys, socket
import urllib

try:
	rfc_number = int(sys.argv[1])

except(IndexError,ValueError):
	print('Must supply an RFC number as first argument')
	sys.exit(2)

# template = 'http://www.ietf.org/rfc/rfc{}.txt'
# url = template.format(rfc_number)
# rfc_raw = urllib.urlopen(url).read()
# rfc = rfc_raw.decode()
# print(rfc)

host = 'www.ietf.org'
port = 80
sock = socket.create_connection((host, port))
req = (
'GET /rfc/rfc{rfcnum}.txt HTTP/1.1\r\n'
'Host: {host}:{port}\r\n'
'User-Agent: Python {version}\r\n'
'Connection: close\r\n'
'\r\n'
)
req = req.format(
rfcnum=rfc_number,
host=host,
port=port,
version=sys.version_info[0]
)
sock.sendall(req.encode('ascii'))
rfc_raw = bytearray()
while True:
	buf = sock.recv(4096)
	if not len(buf):
		break
	rfc_raw += buf
	rfc = rfc_raw.decode('utf-8')
print(rfc)


import math

def parse_output_to_dict(out):
    print('__parse_out_to_dict__')
    print(out)
    setup = dict()
    results = []
    outputs = out\
        .replace('-\n', '')\
        .replace('-', '')\
        .replace('- ', '')\
        .replace('-', '')\
        .replace('\t', '')\
        .replace(' Start Peso Task Farm ', '')\
        .split('\n')
    try:
        count = 1
        for output in outputs:
            if count == 1:
                setup['dop'] = int(output.split(' ')[5])
            elif count == 2:
                setup['batch_size'] = int(output.split(' ')[3])
            elif count == 3:
                setup['buffer_size'] = int(output.split(' ')[3])
            elif output is not '':
                key, value = output.split(': ')
                results.append(int(value))
            count += 1
    except Exception as e:
        print('Caught an exception')
        print(e)
        print(output)
        raise

    return setup, results # results contains all measurements

def compute_interarrival_time(out, batch_size, dop):
    slice_to_reduce = out[dop * batch_size * (-1):]
    return math.ceil(sum(slice_to_reduce) / len(slice_to_reduce))

def check_if_deadline_has_been_missed(out, deadline):
    return any(sample > deadline for sample in out)


import json
import logging
import pytest
import urllib.parse

from server import app

LOG = logging.getLogger(__name__)

@pytest.fixture
def app_client():
    app_client = app.test_client()
    app_client.testing = True
    yield(app_client)

def test_root_endpoint(app_client):
    response = app_client.get('/')
    assert response.status_code == 200
    response.close()

def test_health_endpoint(app_client):
    result = json.loads(app_client.get('/v1/health').data)
    LOG.debug('/v1/health : ' + str(result))
    assert result['status'] == 'UP'

def test_calc_endpoint(app_client):
    url = '/v1/calculation?expr=' + urllib.parse.quote_plus('1 + 1')
    result = json.loads(app_client.get(url).data)
    LOG.debug(str(url) + ' : ' + str(result))
    assert result['result'] == 2


import numpy as np
from hdf5storage import loadmat
from os.path import join

import sys
imset = sys.argv[1].lower().capitalize()

DS_PATH = 'D:/datasets/processed/voc2012'
PRED_PATH = join(DS_PATH, 'Deeplab_Prediction', imset)
GT_PATH = join(DS_PATH, 'Truth', imset)

ds_info = loadmat(join(DS_PATH, 'dataset_info.mat'))
num_img = ds_info['num_'+imset.lower()]
nc = 21
class_names = ds_info['class_labels']

SM_FMT = '%s_%06d_softmax.mat'
SM_MAT_NAME = 'softmax_img'

PRED_FMT = '%s_%06d_prediction.mat'
PRED_MAT_NAME = 'pred_img'

GT_FMT = '%s_%06d_pixeltruth.mat'
GT_MAT_NAME = 'truth_img'

def load_sm_pred_gt(idx):
	global PRED_PATH, SM_FMT, SM_MAT_NAME, GT_PATH, GT_FMT, GT_MAT_NAME, imset
	sm = loadmat(join(PRED_PATH, SM_FMT % (imset, idx)))[SM_MAT_NAME].flatten()
	pred = loadmat(join(PRED_PATH, PRED_FMT % (imset, idx)))[PRED_MAT_NAME].flatten()
	gt = loadmat(join(GT_PATH, GT_FMT % (imset, idx)))[GT_MAT_NAME].flatten()
	return sm, pred, gt

acc_res = 0.1
hists = []
histlen = int(1./acc_res)

for _ in range(nc):
	correct_hist = np.zeros((histlen), dtype=np.uint64)
	count_hist = np.zeros_like(correct_hist)
	hists.append((correct_hist, count_hist))

for i in range(1, num_img+1):
	scores, pred, gt = load_sm_pred_gt(i)
	bins = np.floor(scores/acc_res).astype(np.uint8)

	for c in range(nc):
		if (gt==c).sum() == 0:
			continue

		gt_mask = gt[gt==c]
		pred_mask = pred[gt==c]
		bin_mask = bins[gt==c]

		for binno in np.unique(bin_mask):
			pred_bin = pred_mask[bin_mask==binno]
			gt_bin = gt_mask[bin_mask==binno]
			hists[c][0][min(binno, histlen-1)] += np.sum(pred_bin==gt_bin)
			hists[c][1][min(binno, histlen-1)] += np.sum(bin_mask==binno)

for i, (correct_hist, count_hist) in enumerate(hists):
	acc_hist = correct_hist.astype(np.float64)/np.maximum(1e-7, count_hist)
	class_name = class_names[i]

	import matplotlib.pyplot as plt
	fig, ax = plt.subplots()
	ax.bar(range(acc_hist.shape[0]), [v*acc_res for v in range(acc_hist.shape[0])], align='edge', color='r', alpha=0.5)
	ax.bar(range(acc_hist.shape[0]), acc_hist, align='edge')
	ax.set_xlabel('Confidence')
	ax.xaxis.set_ticks(np.linspace(0, acc_hist.shape[0], num=6))
	ax.set_xticklabels(['%.1f' % v for v in np.linspace(0.0, 1.0, num=6)])
	ax.set_ylabel('Accuracy')
	ax.yaxis.set_ticks(np.linspace(0.0, 1.0, num=6))
	ax.set_title('%s' % class_name)
	fig.savefig('acc_hists/%s_acc_hist.png' % class_name, bbox_inches='tight')
	plt.close(fig)


from itertools import permutations
from math import log10, floor

subn = lambda l,d: int(''.join(map(lambda i: l[i], range(d, d+3))))

if __name__ == '__main__':
	rules, res = [2,3,5,7,11,13,17], 0

	for perm in permutations('1406375289'):
		if not any([subn(perm, d+1) % rule for d,rule in enumerate(rules)]):
			res += int(''.join(perm))

	'''
	#this one is more efficient because we
	#suddenly stop if a rule is not followed
	for perm in permutations('1406375289'):
		for d,rule in enumerate(rules):
			if subn(perm, d+1) % rule: break
			d+=1

		if d==7:
			res += int(''.join(perm))
	'''

	print('Result:', res)


#importando el módulo pandas.
import pandas as pd

"""
Se leen los datos de un archivo Excel en un objeto Panda DataFrame.
Asimismo, pandas almacena datos de forma predeterminada en el DataFrame.
Luego se almacena este DataFrame en una variable llamada peliculas.
"""

rutaFileXls = 'https://github.com/luisguillermomolero/MisionTIC2022/blob/ff05b5f12ea60cf2f005309f011c1d48ab4725cb/movies.xls?raw=true'
#rutaFileXls = 'movies.xls' #ruta de la base de datos

def listaPeliculas(rutaFileXls: str)-> str:
    #if rutaFileXls.endswith('.xls'): #Condicion de entrada (True) si la cadena termina con el sufijo ".xls"
    #if not rutaFileXls.endswith('.xls'): #Condicion de entrada (True) si la cadena termina con el sufijo ".xls"
    if rutaFileXls.split('.')[-1] != 'xls': #Condicion de entrada (True) si la cadena termina con el sufijo ".xls"
        try:
            xlsx = pd.ExcelFile(rutaFileXls)    #Importar Bd Excel "movies.xls"
            registroPeliculas = []              #Inicializa la lista
            for hojas in xlsx.sheet_names:      #Concatenar todas las hojas de excel en un sola lista
                registroPeliculas.append(xlsx.parse(hojas))
            peliculas = pd.concat(registroPeliculas)    #Concatena todas las hojas de la bd excel en "registroPeliculas"
        except:
            print('Error al leer el archivo de datos.')
        #Se selecciona la columna 'Year' como indice y la columna 'Gross Earnings' para aplicar la fórmula de resumen
        subGrupoPeliculas = peliculas[['Year', 'Gross Earnings']]
        subGrupoPeliculas.head()
        #Con pivot_table indexamos 'Year'
        gananciaAno = subGrupoPeliculas.pivot_table(index=['Year'])
        gananciaAno.head()
        #Se importa la libreria matplotlib.pyplot
        import matplotlib.pyplot as plt
        gananciaAno.plot()
        #Se muestra la columna 'Net Earnings' [76 rows x 1 columns]
        print(gananciaAno)
        #print('\n Ganancia por Año')
        #Se muestra el resultado con la grafica
        #plt.show()

    else:
        print('Extensión inválida.')
    return 'Fin del registro'
print(listaPeliculas(rutaFileXls))


def isrotation(a, b):
    """ Determine if string a b rotation of string a """
    if len(a) != len(b):
        return False

    for i in range(0, len(a)):
        x = a[:i]
        y = a[i:]
        print(x+y, y+x)
        if y + x == b:
            return True

    return False

# x + y is always a
# if b is a rotation of a, for some x and y, x + y = a and y + x = b
# y + x is a substring of x + y + x + y, that is, b is a substring of a + a
# it is enough to check then if b is a substring of a + a
def isrotation2(a, b):
    if (a+a).find(b) != -1:
        return True
    return False


#!/usr/bin/env python

import os
import sys
sys.path.append('/Users/bennet/python/module-list')
from os.path import join as pathjoin
from prettytable import PrettyTable
from pprint import pprint

def read_inc_file(root, file, modules_dict):
    with open(pathjoin(root, file), 'r') as incfile:
        whatis_dict = {'Description': ''}
        app = ''
        line = incfile.readline()
        while line:
            if line[:4] == 'proc':
                app = line.split('::')[2]
            else:
                parts = line.split('"')
                if parts[0].strip() == 'module-whatis':
                    lpart = parts[1].partition(':')
                    if lpart[2]:
                        whatis_dict[lpart[0]] = lpart[2].strip()
                    else:
                        whatis_dict['Description'] = lpart[0]
            line = incfile.readline()
        if app:
            if app not in modules_dict:
                modules_dict[app] = {'versions':[]}
            modules_dict[app]['whatis'] = whatis_dict

def read_module_file(root, file, modules_dict):
    with open(pathjoin(root, file), 'r') as modulefile:
        app = ''
        version = ''
        line = modulefile.readline()
        while line and (not app or not version):
            parts = line.split()
            if (len(parts) >= 3) and (parts[0] == 'set'):
                if parts[1] == 'app':
                    app = parts[2]
                elif parts[1] == 'version':
                    version = parts[2]
            line = modulefile.readline()
        if app:
            if app not in modules_dict:
                modules_dict[app] = {'versions':[]}
            modules_dict[app]['versions'].append(version)

module_trees = ['/usr/cac/rhel6/Modules/modulefiles',
                '/usr/flux/software/rhel6/Modules/modulefiles',
                '/usr/cac/rhel6/lsa/Modules/modulefiles',
                '/usr/cac/rhel6/med/Modules/modulefiles',
                '/usr/cac/rhel6/sph/Modules/modulefiles',
                '/usr/flux/software/rhel6/isr/Modules/modulefiles']

# with open('/home/ajying/misc/module-list.html', 'w') as html:
# with open('/afs/umich.edu/group/f/flux/Public/html/module-list.html', 'w') as html:
#    html.write('\n')

for tree_path in module_trees:
    header = ['Module', 'Versions', 'Description']
    table = PrettyTable(header)
    table.align = 'l'
    modules_dict = {}
    for root, dirs, files in os.walk(tree_path):
        for file in files:
            if 'inc.tcl' in file:
                read_inc_file(root, file, modules_dict)
            elif 'version' not in file:
                read_module_file(root, file, modules_dict)
    for module, data in sorted(modules_dict.items()):
        versions = data['versions']
        if len(versions) == 0:
            versions.append('')
        versions.sort()
        desc_lines = []
        desc_line_len = 50
        desc = ''
        if 'whatis' in data:
            desc = data['whatis']['Description']
        while len(desc) > desc_line_len:
            cutoff = desc.rfind(' ', 0, desc_line_len)
            if cutoff > 0:
                desc_lines.append(desc[:cutoff])
                desc = desc[cutoff:]
        desc_lines.append(desc)
        table.add_row([module[:20], versions[0][:20], desc_lines[0]])
        count = 1
        while (count < len(versions) or count < len(desc_lines)):
            row = ['', '', '']
            if count < len(versions):
                row[1] = versions[count][:20]
            if count < len(desc_lines):
                row[2] = desc_lines[count]
            table.add_row(row)
            count += 1
        table.add_row(['', '', ''])

#     print(tree_path)
#     print(table)
#     print('')

#    with open('/home/ajying/misc/module-list.html', 'a') as html:
#     with open('/afs/umich.edu/group/f/flux/Public/html/module-list.html', 'a') as html:
#         html.write('<h2>' + tree_path + '</h2>')
#         html.write(table.get_html_string(format=True).encode('utf8'))
#         html.write('\n')


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

"""
Created on Feb 20 2017
@author: amourleey

"""
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('data/images/meixi.png')
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
# img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.subplot(121),plt.imshow(img)
plt.title('BGR'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(img2)
plt.title('RGB'),plt.xticks([]),plt.yticks([])

plt.show()

cv2.imshow('bgr image', img)
cv2.imshow('rgb image', img2)
cv2.waitKey( 0 )
cv2.destroyAllWindows()


#!C:\\Users\\shivam Aangirasa N\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print("Content-Type: text/plain;charset=utf-8")
print()
l=[1,2,4,5,90,87]
for e in l:
    print(str(e*7))
k=open('sample.txt','w')


import constants
from PyQt5.QtWidgets import QPushButton, QLabel, QRadioButton, QGroupBox
from PyQt5.QtCore import pyqtSlot

class button(QPushButton):
    c = constants.constants()
    def __init__(self):
        super().__init__()
        self.setText("Nakarm")
        self.setDefault(False)

        self.clicked.connect(self.button_clicked)

        self.show()

    def button_clicked(self):
        self.make_label()

    def make_label(self):
        self.label = QLabel()#"lol")
        self.label.resize(200, 100)
        self.label.move(self.c.POS_X + 150, self.c.POS_Y + 120)

        self.label.show()


with open('input.txt', 'r') as f:
  lines = [l.strip() for l in f.readlines()]

drawn_nums = [int(n) for n in lines[0].split(',')]
print(drawn_nums)

cards = []
card = []
marked_cards = []
marked_card = []
for line in lines[2:]:
  if line == '':
    print('empty line')
    cards.append(card)
    card = []

    marked_cards.append(marked_card)
    marked_card = []
  else:
    row = [int(n) for n in line.split()]
    card.append(row)

    marked_card.append([False for _ in line.split()])
cards.append(card)
card = []
marked_cards.append(marked_card)
marked_card = []

print(cards)
print(marked_cards)

def mark_cards(cards, marked_cards, drawn_num):
  # new_marked_cards = []
  for i in range(len(cards)):
    for r in range(5):
      for c in range(5):
        if cards[i][r][c] == drawn_num:
          marked_cards[i][r][c] = True
  return marked_cards

def is_card_winning(marked_card):
  for r in range(5):
    row = marked_card[r]
    if all(row):
      return True
  for c in range(5):
    col = [marked_card[r][c] for r in range(5)]
    if all(col):
      return True
  return False

def get_winning_card(cards, marked_cards):
  for i in range(len(marked_cards)):
    if is_card_winning(marked_cards[i]):
      return cards[i], marked_cards[i]
  return None, None

def score_board(card, marked_card, drawn_num):
  s = 0
  for r in range(5):
    for c in range(5):
      if not marked_card[r][c]:
        s += card[r][c]
  return s * drawn_num

for drawn_num in drawn_nums:
  print(f'Bingo caller: {drawn_num}!')
  marked_cards = mark_cards(cards, marked_cards, drawn_num)
  # print(marked_cards)
  winning_card, winning_marked_card = get_winning_card(cards, marked_cards)
  if winning_card:
    print(f'I won!!')
    print(winning_card)
    print(winning_marked_card)
    score = score_board(winning_card, winning_marked_card, drawn_num)
    print(f'Score: {score}')
    break


import time
from videocore6.driver import Driver
from videocore6.assembler import qpu
import numpy as np

@qpu
def qpu_label_with_namespace(asm):

    mov(r0, 0)

    with namespace('ns1'):
        b(R.test, cond = 'always')
        nop()
        nop()
        nop()
        add(r0, r0, 10)
        L.test
        add(r0, r0, 1)

        with namespace('nested'):
            b(R.test, cond = 'always')
            nop()
            nop()
            nop()
            add(r0, r0, 10)
            L.test
            add(r0, r0, 1)

    with namespace('ns2'):
        b(R.test, cond = 'always')
        nop()
        nop()
        nop()
        add(r0, r0, 10)
        L.test
        add(r0, r0, 1)

    b(R.test, cond = 'always')
    nop()
    nop()
    nop()
    add(r0, r0, 10)
    L.test
    add(r0, r0, 1)

    with namespace('ns3'):
        b(R.test, cond = 'always')
        nop()
        nop()
        nop()
        add(r0, r0, 10)
        L.test
        add(r0, r0, 1)

    eidx(r1, sig = ldunifrf(rf2))
    shl(r1, r1, 2)

    mov(tmud, r0)
    add(tmua, rf2, r1)
    tmuwt()

    nop(sig = thrsw)
    nop(sig = thrsw)
    nop()
    nop()
    nop(sig = thrsw)
    nop()
    nop()
    nop()

def test_label_with_namespace():

    with Driver() as drv:

        code = drv.program(qpu_label_with_namespace)
        data = drv.alloc(16, dtype = 'uint32')
        unif = drv.alloc(1, dtype = 'uint32')

        data[:] = 1234

        unif[0] = data.addresses()[0]

        start = time.time()
        drv.execute(code, unif.addresses()[0])
        end = time.time()

        assert (data == 5).all()


import pytest
from sklearn.exceptions import NotFittedError

from feature_engine.imputation import DropMissingData

def test_detect_variables_with_na(df_na):
    # test case 1: automatically detect variables with missing data
    imputer = DropMissingData(missing_only=True, variables=None)
    X_transformed = imputer.fit_transform(df_na)
    # init params
    assert imputer.missing_only is True
    assert imputer.variables is None
    # fit params
    assert imputer.variables_ == ["Name", "City", "Studies", "Age", "Marks"]
    assert imputer.n_features_in_ == 6
    # transform outputs
    assert X_transformed.shape == (5, 6)
    assert X_transformed["Name"].shape[0] == 5
    assert X_transformed.isna().sum().sum() == 0

def test_selelct_all_variables_with_na_when_variables_is_none(df_na):
    imputer = DropMissingData(missing_only=False, variables=None)
    X_transformed = imputer.fit_transform(df_na)
    assert imputer.n_features_in_ == 6
    assert imputer.variables_ == ["Name", "City", "Studies", "Age", "Marks", "dob"]
    assert X_transformed.shape == (5, 6)
    assert X_transformed[imputer.variables_].isna().sum().sum() == 0

def test_detect_variables_with_na_in_variables_entered_by_user(df_na):
    imputer = DropMissingData(
        missing_only=True, variables=["City", "Studies", "Age", "dob"]
    )
    X_transformed = imputer.fit_transform(df_na)
    assert imputer.variables == ["City", "Studies", "Age", "dob"]
    assert imputer.variables_ == ["City", "Studies", "Age"]
    assert X_transformed.shape == (6, 6)

def test_return_na_data_method(df_na):
    imputer = DropMissingData(
        missing_only=True, variables=["City", "Studies", "Age", "dob"]
    )
    imputer.fit(df_na)
    X_nona = imputer.return_na_data(df_na)
    assert X_nona.shape == (2, 6)

def test_error_when_missing_only_not_bool():
    with pytest.raises(ValueError):
        DropMissingData(missing_only="missing_only")

def test_non_fitted_error(df_na):
    with pytest.raises(NotFittedError):
        imputer = DropMissingData()
        imputer.transform(df_na)


import uncertainpy as un
import chaospy as cp
import numpy as np
import scipy.integrate as integrate
import time
from tqdm import tqdm
from tqdm import tqdm_gui
import matplotlib.pyplot as plt

from uqtools import *

class MethodOfLines:

    def __init__(self,N,tmax):
        #variaveis de inicializacao
        self.N = N # number of ages
        self.xmin = 0
        self.xmax  = 50 # number of classes of ages (50)
        #self.agePt = np.linspace(0, ageFim, ageNpts)
        #self.ageCont = 0
        self.tmax = tmax # number of days for simulation (31)
        self.dt   = 0.01 # time step
        #self.tempoPt = np.linspace(0, tempoFim, tempoNpts)
        #self.tempoCont = 0
        self.Virus = []
        self.v0 = 6.4799*10**5
        self.Target = []
        self.t0 = 1.3*10**5
        #self.RNAt = 1
        self.RNAp = 1
        #self.RNAn = 0
        self.delta = 0.07
        self.beta = 5*10**-8
        self.theta = 1.2
        self.s     = 1.3*10**5
        self.d     = 0.01
        self.beta  = 5*10**-8
        self.c     = 19.0
        self.rho   = 12.0
        self.prm_alpha = 22
        self.mu    = 1
        self.initializeDomain()
        self.initializeVariables()
        self.initializeParams()

    def initializeDomain(self):
        self.dx = (self.xmax-self.xmin)/self.N
        self.x = np.arange(self.xmin-self.dx, self.xmax+(2*self.dx), self.dx)
        self.time = np.arange(0,self.tmax, self.dt)

    def Infected(self,x,args):
        v,t=args
        var = v*t*self.beta*np.exp(-self.delta*x)
        return var

    def Target(self):
        return self.s - self.d*self.target - self.v*self.target*self.beta

    def initializeVariables(self):
        # target cell
        self.t0 = self.t0+(self.s - self.d*self.t0 - self.v0*self.t0*self.beta)*self.dt
        self.target = self.t0
        # infected cell
        self.u0 = self.v0*self.t0*self.beta*np.exp(-self.delta*self.x)
        self.u = self.u0.copy()
        self.unp1 = self.u0.copy()
        # rna
        self.rna_0 = self.RNAp*np.exp(self.x)
        self.rna = self.rna_0.copy()
        self.unp2 = self.rna_0.copy()
        # virus
        res = integrate.quad(self.Infected,self.xmin,self.xmax,[self.v0,self.target])
        self.v0 = self.v0+(self.rho*res[0]-self.v0*self.c)*self.dt
        self.v = self.v0

    def initializeParams(self):
        self.nsteps = round(self.tmax/self.dt)
        self.alpha = self.dt/(2*self.dx)
        self.MEAN_PATS = [6.47991433, 6.42897983, 5.857277, 4.63766183, 4.08108333, 3.38275467, 3.18124267, 2.88121, 2.66053917]#, 2.54078967, 2.487822, 2.21939417, 1.90103167, 1.7158415]
        self.t_exp = [0.04, 0.09, 0.17, 0.34, 0.50, 1.00, 1.50, 2.98, 4.91]#, 6.92, 10.94, 13.96, 20.91, 27.98]

    def solve_and_plot(self):
        tc = 0

        for i in range(self.nsteps):
            plt.clf()

            # The Lax-Friedrichs scheme
            #for j in range(self.N):
            #    self.unp1[j] = self.u[j]-self.alpha*(self.u[j+1]-self.u[j-1])+(1/2)*(self.u[j+1]-2*self.u[j]+self.u[j-1])

            # finite differences
            for j in range(self.N):
                self.unp1 =  self.u[j]+(-self.delta*self.u[j]+self.alpha*self.u*(self.u[j]-self.u[j-1]))
                self.unp2 =  self.rna[j]+(self.prm_alpha*self.rna[j]-(self.mu+self.rho)*self.rna[j]+self.alpha*self.rna*(self.rna[j]-self.rna[j-1]))

            # infected cells
            self.u = self.unp1.copy()

            # rna
            self.rna = self.unp2.copy()

            # virus
            exported_virus = integrate.quad(self.Infected,self.xmin,self.xmax,[self.v,self.target])
            self.v = self.v+(self.dt*(self.rho*exported_virus[0]-self.c*self.v))
            self.Virus.append(self.v)
            # target cells
            self.target = self.target + self.dt*(self.s - self.d*self.target - self.v*self.target*self.beta)
            self.Target.append(self.target)

            #plt.plot(self.x,self.u,'bo-', label= "")
            #plt.ylabel('Infected cells')
            #plt.ylim(0.0,8.0)
            plt.plot(self.x,self.rna,'bo-', label= "")
            plt.xlabel('day')
            plt.ylabel('RNA')
            plt.legend(loc='upper left', prop={'size':13})
            #plt.pause(0.01)
            plt.grid(True)
            tc += self.dt

        plt.figure()
        plt.plot(self.time,self.Virus,'bo-', label= "")
        plt.plot(self.t_exp, self.MEAN_PATS, 'r', label='data', linewidth=4)
        plt.xlabel('day')
        plt.ylabel('HVC RNA log_{10}')
        plt.yscale('log')
        plt.grid(True)

        plt.figure()
        plt.plot(self.time,self.Target,'bo-', label= "")
        plt.xlabel('day')
        plt.ylabel('Target Cells')
        plt.yscale('log')
        plt.grid(True)

    '''
    def calcIntegral(I,Rp,Rt):
        soma = 0.0
        for a in range(0, ageNpts):
            soma = soma + I[a]*Rp[a]*Rt[a]
        return soma/float(ageNpts)
    '''

    #recebe como parametro os parametros estocasticos, individuos, e os valores experimentais
    def viralmodelfit(prms):

        [alpha, r, rho, epsilon_alpha, epsilon_r, delta] = prms
        #Inicializacao
        #parametros
        s     = 1.3*10**5
        d     = 0.01
        beta  = 5*10**-8
        c     = 19.0

        k     = 0.80 # coeficiente da funcao exponencial de atraso na exportacao de RNA positivo
        tau   = 0.50 # tempo de atraso para a exportacao de RNA positivo
        Rmax  = 50.0 # numero maximo de RNA negativo / complexo de replicacao (Rn)
        sigma = 4.0  # taxa de formacao de complexos de replicacao
        mu_t  = 0.8  # 0.8 no artigo # decaimento natural de Rt
        theta = 1.20 # taxa de disponibilidade para traducao
        mu_c  = 2.8  # 0.89 no artigo # decaimento natural de Rc e Rn
        epsilon_s = 0.998

        #variaveis
        I = np.zeros((tempoNpts, ageNpts))
        Rp = np.zeros((tempoNpts, ageNpts))
        Rt = np.zeros((tempoNpts, ageNpts))
        Rn = np.zeros((tempoNpts, ageNpts))
        V = np.zeros(tempoNpts)
        T = np.zeros(tempoNpts)

        def diferencasfinitas():
            #Solve
            kappa_t       = 1.0 #poi[3]
            kappa_c       = 1.0 #poi[4]

            for t in range(1, tempoNpts):

                T[t] = (s - d*T[t-1] - beta*V[t-1]*T[t-1])*deltaT + T[t-1]

                V[t] =  deltaT*((1 - epsilon_s)*rho*calcIntegral(I[t-1],Rp[t-1],Rt[t-1])
                - c*V[t-1]) + V[t-1]

                rho1 = 0.0

                Rp[t][0] = 0
                Rn[t][0] = 0
                Rt[t][0] = 1
                I[t][0]  = beta*V[t]*T[t]

                for a in range(1, ageNpts):

                    if(float(a*deltaA) < tau):
                        rho1 = 0
                    else:
                        rho1 = rho*(1 - np.exp(-k*(float(a*deltaA) - tau)))

                    I[t][a] = (-delta*I[t-1][a] - (I[t-1][a] - I[t-1][a-1])/deltaA)*deltaT + I[t-1][a]

                    Rn[t][a] = ((1 - epsilon_r)*r*Rp[t-1][a]*(1 - (Rn[t-1][a]/Rmax)) - kappa_c*mu_c*Rn[t-1][a]
                                - (Rn[t-1][a] - Rn[t-1][a-1])/(deltaA ))*deltaT + Rn[t-1][a]

                    Rp[t][a] = ((1 - epsilon_alpha)*alpha*Rn[t-1][a] + sigma*Rt[t-1][a] - theta*Rp[t-1][a]
                                - (1- epsilon_s)*rho1*Rp[t-1][a] - kappa_c*mu_c*Rp[t-1][a]
                                - (Rp[t-1][a] - Rp[t-1][a-1])/(deltaA))*deltaT + Rp[t-1][a]

                    Rt[t][a] = (theta*Rp[t-1][a] - sigma*Rt[t-1][a] - (1 - epsilon_s)*rho1*Rt[t-1][a] - kappa_t*mu_t*Rt[t-1][a]
                                - (Rt[t-1][a] - Rt[t-1][a-1])/(deltaA))*deltaT + Rt[t-1][a]
            return V

        #condicoes iniciais
        T0 = 1.3*10**5 #PAT83:
        V0 = 6.4799*10**5
        I0_t0 = beta*T0*V0

        Rt0_t0 = 1
        Rp0_t0 = 0
        Rn0_t0 = 0

        T[0] = T0
        V[0] = V0
        Rt[0][0] = Rt0_t0
        Rp[0][0] = Rp0_t0
        Rn[0][0] = Rn0_t0
        I[0][0] = I0_t0

        rho1 = 0.00
        # Condicao inicial para as EDPs
        for ageCont in range(1, ageNpts):
            I[0][ageCont] = (beta*T[0]*V[0]*np.exp(-deltaA*ageCont))*deltaA + I[0][ageCont-1]
            if ageCont*deltaA < tau:
                rho1 = 0
            else:
                rho1 = (1 - np.exp(-k*((ageCont*deltaA) - tau)))*rho
            Rn[0][ageCont] = (r*Rp[0][ageCont-1] - r*Rp[0][ageCont-1]*(Rn[0][ageCont-1]/Rmax)- mu_c*Rn[0][ageCont-1])*deltaA + Rn[0][ageCont-1]
            Rp[0][ageCont] = (alpha*Rn[0][ageCont-1]+sigma*Rt[0][ageCont-1]-theta*Rp[0][ageCont-1]-rho1*Rp[0][ageCont-1]- mu_c*Rp[0][ageCont-1])*deltaA + Rp[0][ageCont-1]
            Rt[0][ageCont] = (theta*Rp[0][ageCont-1] - sigma*Rt[0][ageCont-1]- rho1*Rt[0][ageCont-1] - mu_t*Rt[0][ageCont-1])*deltaA + Rt[0][ageCont-1]
        #Fim das condicoess iniciais----------------------------------------------+

        # Executa o metodo numerico para resolver
        # Precisa passar como parametro as condicoes iniciais
        V = diferencasfinitas()

        # Passa para a base log o resultado
        V_log = np.log10(V)

        return tempoPt, V_log

    ##############FIM MODELO################

    '''

    model = un.Model(
        run = viralmodelfit,
        labels=["Tempo (dias)",
            "Carga viral (log10)"]
    )

    epsilon_r = 0.3
    epsilon_alpha = 0.9
    delta = 0.07
    alpha = 22
    r = 11.1
    rho = 12.0

    # create pdf distributions
    min_bound = 0.9
    max_bound = 1.1

    pdf_alpha = cp.Uniform(alpha*min_bound,alpha*max_bound)
    pdf_r = cp.Uniform(r*min_bound,r*max_bound)
    pdf_rho = cp.Uniform(rho*min_bound,rho*max_bound)
    pdf_epsilon_alpha = cp.Uniform(epsilon_alpha*min_bound,epsilon_alpha*max_bound)
    pdf_epsilon_r = cp.Uniform(epsilon_r*min_bound,epsilon_r*max_bound)
    pdf_delta = cp.Uniform(delta*min_bound,delta*max_bound)

    #print('Criando distribuicoes normais')
    ##epsilon_r = cp.Normal(0.3,0.1)
    #epsilon_r = cp.Normal(0.3,0.001)
    ##epsilon_s = cp.Normal(0.998,0.001)
    ##epsilon_alpha = cp.Normal(0.92,0.002)
    #epsilon_alpha = cp.Normal(0.92,0.001)
    #delta = cp.Normal(0.07,0.01)
    #alpha = cp.Normal(22,0.001)
    #r = cp.Normal(11.1,0.1)
    #rho = cp.Normal(12.0,1.0)

    print('Distribuicoes criadas')

    dist = cp.J(pdf_alpha, pdf_r, pdf_rho, pdf_epsilon_alpha, pdf_epsilon_r)

    dist = cp.J(dist, pdf_delta)

    npar = len(dist)

    #grau da aproximacao polinomial
    degpc = 2
    ns = 10#3*P(degpc,npar)
    print("number of input parameters %d" % npar)
    print("polynomial degree %d" % degpc)
    print("min number of samples %d" % ns)

    evals_virus = []

    samples = dist.sample(ns,"L")
    k = 0
    lpts = range(ns)
    samp = samples.T
    print("evaluating samples: ")
    for i in tqdm(lpts,bar_format='{l_bar}{bar:20}{r_bar}{bar:-20b}'):
    #for s in samples.T:
        s = samp[k]
        k = k+1
        [sim_time,virus] = viralmodelfit(s)
        evals_virus.append(virus)

    evals_virus = np.array(evals_virus)

    plt.style.use('../estilo/PlotStyle.mplstyle')
    plt.close('all')

    output_path = './output_monte_carlo/'
    ext = '.png'
    itvl = 6
    caso = 'caso.'

    plt.figure('HCV RNA')
    #dadosViremiaLog10.plot.scatter(x='Day',y='Viral_load',color='m',label='Dados experimentais')
    plt.plot(t_exp, MEAN_PATS, 'o', label='data', linewidth=4)
    plt.xlim(0.0,tempoFim)
    #plt.ylim(0.0,8.0)
    plt.xlabel('day')
    plt.ylabel('HVC RNA log_{10}')
    plt.legend(loc='upper left', prop={'size':13})
    plt.grid(True)
    plt.title("Solution Virus")
    mean_virus= plot_confidence_interval(plt, sim_time, evals_virus, 'red', 'Modelo: Virus')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.savefig(output_path+caso+'_output_Virus'+ext)
    plt.show()
    '''

def main():
    #sim = LaxFriedrichs(100, 1.5)
    sim = MethodOfLines(50,5)
    sim.solve_and_plot()
    plt.show()

if __name__ == "__main__":
    main()

# alpha, r, rho, epsilon_alpha, epsilon_r, delta

'''

# define parameter dictionary
parameters = {"alpha": pdf_alpha,
        "r": pdf_r,
        "rho": pdf_rho,
        "epsilon_alpha": pdf_epsilon_alpha,
        "epsilon_r": pdf_epsilon_r,
        "delta": pdf_delta
            }

# set up UQ
UQ = un.UncertaintyQuantification(
    model=model,
    parameters=parameters
)

print('Inicio UQ --- ')
begin = time.perf_counter()
data = UQ.monte_carlo(nr_samples=1000)
end = time.perf_counter()
print(' --- Fim UQ')
tempo = end-begin
with open('tempo.txt', 'w') as file:
    file.write("Tempo de execução: "+str(tempo)+" segundos")

'''


#基于CNN，只使用最精细的那个先验知识，conv1d

import tensorflow as tf

class modelConfig(object):
    def __init__(self):
        self.EMBDDING_DIM = 128
        self.FACT_LEN = 30
        self.LAW_LEN = 30
        self.KS_LEN= 1

        self.FILTERS = 256
        self.KERNEL_SIZE = 5  # 卷积核尺寸

        self.LAYER_UNITS = 100
        self.NUM_CLASS = 2

        self.LEARNING_RATE = 0.001
        self.batch_size = 128
        self.num_epochs = 200
        self.save_per_batch = 10
        self.print_per_batch = 10
        self.dropout_keep_prob = 0.5

class CNN(object):
    def __init__(self, config):
        self.config = config
        self.input_x1 = tf.placeholder(tf.float32, [self.config.batch_size, self.config.FACT_LEN, self.config.EMBDDING_DIM],
                                        name='input_x1')
        self.input_x2 = tf.placeholder(tf.float32, [self.config.batch_size, self.config.LAW_LEN, self.config.EMBDDING_DIM],
                                        name='input_x2')
        self.input_ks = tf.placeholder(tf.float32, [self.config.batch_size, self.config.EMBDDING_DIM],name="input_ks")
        self.input_y = tf.placeholder(tf.int32, [self.config.batch_size, self.config.NUM_CLASS], name='input_y')
        self.keep_prob = tf.placeholder(tf.float32, name='keep_prob')

        self.cnn()
        return

    def cnn(self):
        new_x1 = self.gate(self.input_ks,self.input_x1)
        op1,op2 = self.conv(new_x1,self.input_x2)
        self.match(op1,op2)

    def gate(self, ks, inputx):
        with tf.name_scope("gate"):

            new_ks = tf.keras.backend.repeat(ks,self.config.FACT_LEN)

            weight_1 = tf.Variable(tf.random_normal([self.config.EMBDDING_DIM,self.config.EMBDDING_DIM],
                                                    stddev=0, seed=1),trainable=True, name='w1')
            weight_2 = tf.Variable(tf.random_normal([self.config.EMBDDING_DIM, self.config.EMBDDING_DIM],
                                                    stddev=0, seed=1),trainable=True, name='w2')

            new_inputx = tf.reshape(inputx, shape=[self.config.EMBDDING_DIM,self.config.FACT_LEN * self.config.batch_size])
            new_ks2 = tf.reshape(new_ks, shape=[self.config.EMBDDING_DIM,self.config.FACT_LEN * self.config.batch_size])

            kw = tf.sigmoid(tf.reshape(tf.matmul(weight_1, new_inputx) + tf.matmul(weight_2,new_ks2),shape=inputx.shape))

            one_array = tf.ones(shape=kw.shape)

            new_vector = (one_array - kw) * inputx + kw * new_ks
        return new_vector

    def conv(self,inputx,inputy):
        with tf.name_scope("conv"):
            conv1 = tf.layers.conv1d(inputx,filters=self.config.FILTERS,kernel_size=self.config.KERNEL_SIZE,name='conv1')
            op1 = tf.reduce_max(conv1, reduction_indices=[1], name='gmp1')

            conv2 = tf.layers.conv1d(inputy,filters=self.config.FILTERS,kernel_size=self.config.KERNEL_SIZE,name='conv2')
            op2 = tf.reduce_max(conv2, reduction_indices=[1], name='gmp2')

            return op1,op2

    def match(self,op1,op2):
        with tf.name_scope("match"):

            h = tf.concat([op1,op2],axis=1) #[batch,len1+len2]
            fc = tf.layers.dense(inputs=h, units= self.config.LAYER_UNITS, use_bias=True,
                            trainable=True, name="fc1")
            fc = tf.contrib.layers.dropout(fc, self.keep_prob)  # 根据比例keep_prob输出输入数据，最终返回一个张量
            fc = tf.nn.relu(fc)  # 激活函数，此时fc的维度是hidden_dim

            # 分类器
            self.logits = tf.layers.dense(fc, self.config.NUM_CLASS,
                                          name='fc2')  # 将fc从[batch_size,hidden_dim]映射到[batch_size,num_class]输出
            self.y_pred_cls = tf.argmax(tf.nn.softmax(self.logits), 1)  # 预测类别

        with tf.name_scope("optimize"):
            # 损失函数，交叉熵
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits,
                                                                    labels=self.input_y)  # 对logits进行softmax操作后，做交叉墒，输出的是一个向量
            self.loss = tf.reduce_mean(cross_entropy)  # 将交叉熵向量求和，即可得到交叉熵
            # 优化器
            self.optim = tf.train.AdamOptimizer(learning_rate=self.config.LEARNING_RATE).minimize(self.loss)

        with tf.name_scope("accuracy"):
            # 准确率
            correct_pred = tf.equal(tf.argmax(self.input_y, 1),self.y_pred_cls)  # 由于input_y也是onehot编码，因此，调用tf.argmax(self.input_y)得到的是1所在的下表
            self.acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


# This will be the main class to pull the data from CoinBase. This might not be the
# best way to get the information, but CoinBase is the current App that i use to
# buy/sell crypto already

from coinbase.wallet.client import Client

api_key_file = open(".coinbase_key", "r")
api_secret_file = open(".coinbase_secret", "r")

api_key = api_key_file.readline()
api_secret = api_secret_file.readline()

api_key_file.close()
api_secret_file.close()

client = Client(api_key, api_secret)

def getCryptoBuyPrice(coin, currency):
    response = client.get_buy_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getCryptoSellPrice(coin, currency):
    response = client.get_sell_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getCryptoSpotPrice(coin, currency):
    response = client.get_spot_price(currency_pair = "" + coin + "-" + currency)
    return response['amount']

def getTime():
    return client.get_time()

print(getCryptoBuyPrice("BTC", "USD"))
print(getCryptoSellPrice("BTC", "USD"))
print(getCryptoSpotPrice("BTC", "USD"))
print(getTime())


"""
Crawler to get results from specific query from github.com.

__External modules__: `requests`, `random`, `random`, `bs4`, `json`

`Compatible with Python3.8 or higher`
"""
import requests
from random import randint
from bs4 import BeautifulSoup
import json

def get_random_proxy(*args):
	"""Function to wait process thread responsible for calculation the solutions.

	Args:
		proxies (array): _Optional_ array of `str` with proxies

	Returns:
		proxy (str): a random proxy
	"""
	if not args:
		http_proxies = [
			'200.68.49.184:8080',
			'119.252.160.165:3128',
			'94.180.249.187:38051',
			'62.210.69.176:5566' ]
	else:
		http_proxies = args[0]

	n = randint(0,len(http_proxies)-1)

	return http_proxies[n]

def process_input(crawler_input):
	"""Function process the input, making sure it is valid.

	Args:
		crawler_input (dict): `dict` with `keywords`, `proxies` and `type`

	Returns:
		input_dict (dict): a sanatised `dict` with the inputs for the crawler
	"""
	try:
		keywords = crawler_input['keywords']
	except KeyError:
		print('No keywords found')
		quit()

	try:
		proxy = get_random_proxy(crawler_input['proxies'])
	except KeyError:
		print('No proxies found, using existing ones')
		proxy = get_random_proxy()

	try:
		query_type = crawler_input['type']
	except KeyError:
		print('No type found')
		quit()

	if query_type not in ['Repositories','Issues', 'Wikis']:
		print('Invalid type')
		quit()

	input_dict = {
		'keywords': keywords,
		'proxy': proxy,
		'type': query_type
	}

	return input_dict

def parse_keywords(keywords):
	"""Function to convert the keywords `array` in to a `str` that will be used for the crawler query

	Args:
		keywords (array): `array` with `keywords`

	Returns:
		formated_keyword (str): `str` with the concatenated keywords using + between them
	"""
	q = ''
	for i in range(len(keywords)):
		q += f'{keywords[i]}+'
	return q[0:-1]

def get_results_page(input_dict):
	"""Function to retrieve the first 10 results for the specified input

	Args:
		input_dict (dict): `dict` with crawler input

	Returns:
		page (str): `str` with the html content of the page
	"""
	q = parse_keywords(input_dict['keywords'])
	proxy_dict = {'http': f'http://{input_dict["proxy"]}'}
	search_type = input_dict['type']

	r = requests.get(f'https://github.com/search?q={q}&type={search_type}', proxies=proxy_dict)

	return r.text

def results_parser(page):
	"""Function to parse the result page

	Args:
		page (str): `str` with html content

	Returns:
		results_dict (dict): `dict` with the results from the crawler
	"""
	results_dict = {}

	soup_page = BeautifulSoup(page, 'html.parser')
	results = soup_page.findAll('div', {'class':'f4 text-normal'})

	for result in results:
		temp = result.find('a').get('data-hydro-click')
		json_input = json.loads(temp)
		payload = json_input['payload']
		results_dict[payload['result_position']] = payload

	return results_dict

def languages_parser(page):
	"""Function to parse the languages in a repository page

	Args:
		page (str): `str` with html content

	Returns:
		language_stats (dict): `dict` with the language status of the repository
	"""
	language_stats = {}

	soup_page = BeautifulSoup(page, 'html.parser')
	languages = soup_page.findAll('li', {'class': 'd-inline'})
	for language in languages:
		temp = language.find('span',{'class': 'text-gray-dark text-bold mr-1'})
		lang = temp.get_text()
		value = temp.findNext('span').get_text()
		language_stats[lang] = float(value.strip('%'))

	return language_stats

def get_urls(results_dict):
	"""Function to get the url of each result in the results `dict`

	Args:
		results_dict (dict): `dict` with all the results

	Returns:
		url (array): `array` with the urls of returned by the crawler
	"""
	urls = []
	for result in results_dict:
		url = results_dict[result]['result']['url']
		urls.append({"url":url})
	return urls

def get_repo_details(url,proxy):
	"""Function to get the details of a specific repository

	Args:
		url (dict): `dict` with all the results format {'url': url_str}\n
		proxy(str): `str` with the proxy to be used

	Returns:
		repo_details (dict): `dict` with the repository details
	"""
	temp = url['url'].replace('https://github.com/','')
	owner = temp[0:temp.find("/")]

	proxies = {'http': f'http://{proxy}'}

	r = requests.get(url['url'],proxies=proxies)

	language_stats = languages_parser(r.text)
	return {'owner': owner, 'language_stats': language_stats }

def crawler(crawler_input):
	"""Function to crawl the webpage using the inputs given by the user

	Args:
		crawler_input (dict): `dict` with the crawler input

	Returns:
		details (json): an indented json object with all the crawler information
	"""
	c_input = process_input(crawler_input)
	text = get_results_page(c_input)
	results_dict = results_parser(text)
	urls = get_urls(results_dict)

	if c_input['type'] == 'Repositories':
		for url in urls:
			url['extra'] = get_repo_details(url,c_input['proxy'])

	return json.dumps(urls,indent=1)

def memory_usage(method,args):
	"""Function to check the memory usage

	Args:
		method (function): `function` to be evaluated\n
		args (args): `args` to be used on the evalutated method

	Returns:
		__Nonen__
	"""
	import tracemalloc
	tracemalloc.start()

	method(args)

	current, peak = tracemalloc.get_traced_memory()
	print(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
	tracemalloc.stop()

if __name__ == '__main__':
	crawler_input = {
		'keywords': [
			'recursion',
			# 'rickfernandes',
			# 'css',
			# 'rick',
			# 'otrisus'
		],
		'proxies': [
			'194.126.37.94:8080',
			'13.78.125.167:8080'
		],
		'type': 'Repositories'
	}

	print(crawler(crawler_input))

	# memory_usage(crawler,crawler_input)


from time import sleep
from picamera import PiCamera

cam = PiCamera()
cam.start_preview()

for i in range(5):
	sleep(5)
	cam.capture('/home/pi/Pictures/image%s.jpg' % i)
	print ("photo number ", i , " taken")
cam.stop_preview()


import requests

class MyRequest(object):

    def __init__(self, data):
        self.method = data.get('request_type').strip()
        self.url = data.get('request_url').strip()
        self.params = {}
        for dic in data.get('request_params'):
            if dic.get('execute') == 1:
                self.params[dic['key']] = dic['value']
        self.headers = {}
        for dic in data.get('header_params'):
            if dic.get('execute') == 1:
                self.headers[dic['key']] = dic['value']

    def start(self):
        if self.method == 'get':
            response = requests.request(self.method, self.url, headers=self.headers, params=self.params, timeout=30)
        else:
            response = requests.request(self.method, self.url, headers=self.headers, data=self.params, timeout=30)

        return response


def rhs(u,t):
    N = len(u) - 1
    rhs = zeros(N+1)
    rhs[0] = dsdt(t)
    for i in range(1, N):
        rhs[i] = (beta/dx**2)*(u[i+1] - 2*u[i] + u[i-1]) + g(x[i], t)
    rhs[N] = (beta/dx**2)*(2*u[N-1] + 2*dx*dudx(t) - 2*u[N]) + g(x[N], t)
    return rhs

def u_exact(x,t):
    return (3*t + 2)*(x - L)

def dudx(t):
    return 0

def s(t):
    return 323

def dsdt(t):
    return 0

def g(x, t):
    return 0

from numpy import linspace,zeros
import time

dt = 0.00034375

L = 0.5
beta = 8.2E-5
N = 40
x = linspace(0, L, N+1)
dx = x[1] - x[0]
u = zeros(N+1)
U_0 = zeros(N+1)
U_0[0] = s(0)
U_0[1:] = 283

#t0 = time.process_time
from ode_system_FE import ode_FE
u, t = ode_FE(rhs, U_0, dt, T=1*60*60)
#t1 = time.process_time
#print('CPU time: %.1fs' % (t1 - t0))

# Make movie
import os
os.system('rm tmp_*.png')
import matplotlib.pyplot as plt
plt.ion()
y = u[0,:]
lines = plt.plot(x, y)
plt.axis([x[0], x[-1], 273, s(0)+10])
plt.xlabel('x')
plt.ylabel('u(x,t)')
counter = 0
# Plot each of the first 100 frames, then increase speed by 10x
change_speed = 100
for i in range(0, u.shape[0]):
    print(t[i])
    plot = True if i <= change_speed else i % 10 == 0
    lines[0].set_ydata(u[i,:])
    if i > change_speed:
        plt.legend(['t=%.0f 10x' % t[i]])
    else:
        plt.legend(['t=%.0f' % t[i]])
    plt.draw()
    if plot:
        plt.savefig('tmp_%04d.png' % counter)
        counter += 1


from random import random

from pygame.math import Vector2

from logic import Game, Player

def level_1():
    g = Game()
    g.add_player(Player(position=Vector2(0.2, 0.5),
                        movement=Vector2(0, 0.002*random())))
    g.add_player(Player(position=Vector2(0.8, 0.5),
                        movement=Vector2(0, 0.002*random())))
    return g


import sys
sys.path.insert(0, '../')

from core.siamese import Siamese

model = Siamese('mobilenet_like', input_shape=(672, 896, 3), embedding_size=128)
model.load_weights('trained/final_weights.h5')

model.make_embeddings('../data/train', 'train.csv', mappings_filename='../data/meta/whales_to_idx_mapping.npy', batch_size=25)
#model.load_embeddings('trained/embeddings.pkl')

model.predict('../data/train', 'val.csv')
#model.predict('../data/train', 'train.csv')


"""tango_with_django_project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include

from registration.backends.simple.views import RegistrationView

class MyRegView(RegistrationView):
    def get_success_url(self, user=None):
        return '/forms/'

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    #bellow related to rango app

    # url(r'^$', views.index, name='index'),
    # url(r'^rango/', include('rango.urls')),
    url(r'^accounts/register/$', MyRegView.as_view(),name='registration_register'),
    url(r'^accounts/', include('registration.backends.simple.urls')),
    # url(r'^conversations/', include('conversations.urls')),
    url(r'^forms/', include('forms.urls')),
    # url(r'^analise/', include('analise.urls')),
]

urlpatterns += [
    url(r'^api-auth/', include('rest_framework.urls',
                               namespace='rest_framework')),
]


# from ROOT import *
import ROOT
import glob

ch = ROOT.TChain("mytree")
MyFileNames12 = glob.glob('../data/data.root')
for fName in MyFileNames12:
    ch.Add(fName)

print("Adding chain done", ch.GetNtrees(), 'files ')

hBmas1 = ROOT.TH1F('hBmas1', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas2 = ROOT.TH1F('hBmas2', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas3 = ROOT.TH1F('hBmas3', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas4 = ROOT.TH1F('hBmas4', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas5 = ROOT.TH1F('hBmas5', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas6 = ROOT.TH1F('hBmas6', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas7 = ROOT.TH1F('hBmas7', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas8 = ROOT.TH1F('hBmas8', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas9 = ROOT.TH1F('hBmas9', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas10 = ROOT.TH1F('hBmas10', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)
hBmas11 = ROOT.TH1F('hBmas11', 'B_mass', 100, 6.2751 - 0.4751, 6.2751 + 0.6249)

cuts_cos = [0.985, 0.99, 0.995, 0.999, 0.9995, 0.9999, 0.99995]
cuts_sign = [3, 5, 7, 10, 15, 20, 25]
cuts_prob = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
cuts_K1_pt = [1, 1.5, 2, 2.5, 3, 4, 6, 10]
cuts_dr = [0.1, 0.3, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2]

cut_jpsi = 'JPSI_mass_Cmumu > 3.04 && JPSI_mass_Cmumu < 3.15'
cut_B_pt = ' && B_pt_Cjp > 8'

cut_sign = ' && B_pvdistsignif2_Cjp > ' + str(cuts_sign[1])
cut_K1_pt = ' && K1_pt > ' + str(cuts_K1_pt[4])
cut_prob = ' && B_vtxprob_Cjp > ' + str(cuts_prob[2])
cut_cos = ' && B_pvcos2_Cjp > ' + str(cuts_cos[3])
# cut_dr = ' && DRpiJ < ' + str(cuts_dr[0])

cut = cut_jpsi + cut_B_pt + cut_sign + cut_K1_pt + cut_prob + cut_cos

cuts = []

# for i in cuts_cos:
#     cuts.append(cut + ' && B_pvcos2_Cjp > ' + str(i))

# for i in cuts_sign:
#     cuts.append(cut + ' && B_pvdistsignif2_Cjp > ' + str(i))

# for i in cuts_prob:
#     cuts.append(cut + ' && B_vtxprob_Cjp > ' + str(i))

# for i in cuts_K1_pt:
#     cuts.append(cut + ' && K1_pt > ' + str(i))

for i in cuts_dr:
    cuts.append(cut + ' && DRpiJ < ' + str(i))

ch.Draw('B_mass_Cjp >> hBmas1', cut)
ch.Draw('B_mass_Cjp >> hBmas2', cuts[1])
ch.Draw('B_mass_Cjp >> hBmas3', cuts[2])
ch.Draw('B_mass_Cjp >> hBmas4', cuts[3])
ch.Draw('B_mass_Cjp >> hBmas5', cuts[4])
ch.Draw('B_mass_Cjp >> hBmas6', cuts[5])
ch.Draw('B_mass_Cjp >> hBmas7', cuts[6])
ch.Draw('B_mass_Cjp >> hBmas8', cuts[7])
ch.Draw('B_mass_Cjp >> hBmas9', cuts[8])
ch.Draw('B_mass_Cjp >> hBmas10', cuts[9])
# ch.Draw('B_mass_Cjp >> hBmas11', cuts[10])

ff = ROOT.TFile("../data/B_cut_dr.root", "recreate")

hBmas1.Write()
hBmas2.Write()
hBmas3.Write()
hBmas4.Write()
hBmas5.Write()
hBmas6.Write()
hBmas7.Write()
hBmas8.Write()
hBmas9.Write()
hBmas10.Write()
# hBmas11.Write()

ff.Close()


# coding:utf-8
import os

from scalr_manage.version import __version__
from scalr_manage.library.match_version.target import MatchVersionTarget

from scalr_manage.test.util import BaseWrapperTestCase

class MatchVersionTestCase(BaseWrapperTestCase):
    def setUp(self):
        super(MatchVersionTestCase, self).setUp()

        self.target = MatchVersionTarget()
        self.target.register(self.parser)
        os.makedirs(os.path.dirname(self.solo_json_path))

    def _helper_check_exit_code(self, args, exit_code):
        try:
            ns = self.parser.parse_args(args)
            self.target.__call__(ns, self.ui, self.tokgen)
        except SystemExit as e:
            self.assertEqual(exit_code, e.code)
        else:
            self.fail("SystemExit wasn't raised!")

    def test_match_version(self):
        # Get our current major version to prepare suitable test strings
        major, minor, trailing = __version__.split(".", 2)
        major, minor = int(major), int(minor)

        for version, exit_code in [
            (__version__, 0),

            # Leading / trailing rubbish
            ("\n" + __version__, 0),
            (" " + __version__, 0),
            (__version__ + "\n", 0),
            (__version__ + " ", 0),

            # Different major
            (".".join([str(major+1), str(minor), trailing]), 1),
            (".".join([str(major+1), str(minor+1), trailing]), 1),
            (".".join([str(major+1), str(max(0, minor-1)), trailing]), 1),

            # Different major
            (".".join([str(major-1), str(minor), trailing]), 1),
            (".".join([str(major-1), str(minor+1), trailing]), 1),
            (".".join([str(major-1), str(max(0, minor-1)), trailing]), 1),

            # Same major
            (".".join([str(major), str(minor), trailing]), 0),
            (".".join([str(major), str(minor+1), trailing]), 0),
            (".".join([str(major), str(max(0, minor-1)), trailing]), 0),

            # Rubbish - test error handling
            ("...", 1),
            ("a.b.c", 1),
            ("zzz", 1),
            ("f", 1),
        ]:
            cnf = os.path.join(self.work_dir, version + ".json")

            with open(cnf, "w") as f:
                f.write("{}")

            with open(cnf + ".version", "w") as f:
                f.write(version)

            self._helper_check_exit_code(["--configuration", cnf], exit_code)

    def test_no_version(self):
        self._helper_check_exit_code([], 1)

    def test_no_configuration(self):
        with open(self.solo_json_path + ".version", "w") as f:
            f.write(__version__)
        self._helper_check_exit_code([], 1)

    def test_broken_configuration(self):
        with open(self.solo_json_path, "w") as f:
            f.write("{")
        with open(self.solo_json_path + ".version", "w") as f:
            f.write(__version__)
        self._helper_check_exit_code([], 1)


"""The Epsilon module."""

import time
import random
import logging

log = logging.getLogger(__name__)

class Worker(object):
    """The Worker class."""

    def __init__(self):
        log.info("Initializing the Worker object")

        self.data = {}
        self.data["fibonacci"] = None
        self.data["collatz"] = []

    def _fib(self, n):
        a, b = 0, 1
        log.debug("a is %d, b is %d", a, b)
        for _ in range(n):
            time.sleep(0.1)
            a, b = b, a + b
            log.debug("a is %d, b is %d", a, b)
        return a

    def _collatz(self, n):
        log.debug("n is %d", n)
        while True:
            time.sleep(0.1)
            if n == 1:
                log.debug("n is %d", n)
                break
            if n % 2 == 0:
                n = n // 2
                log.debug("n is %d", n)
                yield n
            else:
                n = n * 3 + 1
                log.debug("n is %d", n)
                yield n
        yield n

    def compute(self):
        """Perform some computation."""
        log.info("Started computing ...")

        seed = random.randint(10, 19)
        log.info("The seed is %d", seed)

        fib = self._fib(seed)
        log.info("The %d-th Fibonacci number is %d", seed, fib)
        self.data["fibonacci"] = fib

        for c in self._collatz(fib):
            self.data["collatz"].append(c)


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

"""
甩手工具从淘宝网上爬下来的宝贝图片，是用数字命名的。如1, 10, 12, 2, 3。
这样图片放置顺序是不对的。
解决方法是，将数字转化为字母，如1-->a，2-->b，10-->j。
"""

import os
import re
from functools import partial

pattern = re.compile(r'(\d+)')
for path, dirs, files in os.walk(os.getcwd()):
    join = partial(os.path.join, path)
    for oldname in files:
        newname = pattern.sub(lambda r: chr(96+int(r.group(1))), oldname)
        if oldname != newname:
            os.rename(join(oldname), join(newname))


"""An empty class used to throw helpful exceptions when a search client isn't configured."""

from django.core.exceptions import ImproperlyConfigured

class Client(object):
    """Throws exceptions on attempts to use this class."""

    def __getattr__(self, _):
        raise ImproperlyConfigured(
            "Search client not configured. Start with settings.ENABLE_SEARCHIFY")


import socket
import pyaudio
import numpy as np
import wave
import math
import struct

# Socket
HOST = "192.168.1.218"
PORT = 10010

# Audio
p = pyaudio.PyAudio()
CHUNK = 1024 * 4
FORMAT = pyaudio.paInt16
CHANNELS = 4
RATE = 44100
cRATE = 22050

#record channel
fm, f0, f1, f2, f3 = [], [], [], [], []

def save(name, channels, rate, frames):
    wf = wave.open(name, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(2)
    wf.setframerate(rate)
    wf.writeframes(b''.join(frames))
    wf.close()

def sig(num):
    return 1/(1 + math.exp(-10 * num))

def amplitude(block):
    count = len(block)/2
    count = count if count != 0 else 1
    format = "%dh"%(count)
    shorts = struct.unpack(format, block)

    sum_squares = 0.0
    for sample in shorts:
        n = sample * (1.0/32768.0)
        sum_squares += n**2

    # return 100 * (sum_squares/count) ** 0.5
    return int(2 * (100 * sig((sum_squares/count) ** 0.5) - 50))

def avgfreq(block):
        result = np.fromstring(block, dtype=np.int16)
        w = np.fft.fft(result)
        freqs = np.abs((np.fft.fftfreq(len(w))*44100))
        w = np.abs(w)
        indices = np.argsort(w)[int(len(w)*0.99):]
        print(indices)
        bin_const = 10
        indices = indices[len(indices) % bin_const:]
        w = w[indices]
        freqs = freqs[indices]
        w = w[np.argsort(freqs)]
        freqs = np.sort(freqs)
        w = np.reshape(w, (-1, bin_const)).sum(axis=1)

        if len(freqs) == 0:
            return None

        freqs = np.average(np.reshape(freqs, (-1, bin_const)), axis=1)
        w = w / np.sum(w)
        freqdict = dict(zip(freqs, w))
        wsum = sum(w)
        wsum = wsum if wsum != 0 else 1
        out = 0
        for i in range(len(w)):
            out += w[i] * freqs[i] / wsum
        return 0 if math.isnan(float(out)) else int(out)

with socket.socket() as server_socket:
    server_socket.bind((HOST, PORT))
    server_socket.listen(1)
    conn, address = server_socket.accept()
    print("Connection from " + address[0] + ":" + str(address[1]))
    data = conn.recv(8192)

    while data != "":
        try:
            data = conn.recv(8192)

            channels = np.fromstring(data, dtype='int16')
            c0 = channels[0::8]
            c1 = channels[1::8]
            c2 = channels[2::8]
            c3 = channels[3::8]

            fm.append(data)
            f0.append(c0.tostring())
            f1.append(c1.tostring())
            f2.append(c2.tostring())
            f3.append(c3.tostring())

            print(amplitude(f0[-1]), avgfreq(f0[-1]), "\t", amplitude(f1[-1]), avgfreq(f1[-1]), "\t", amplitude(f2[-1]), avgfreq(f2[-1]), "\t", amplitude(f3[-1]), avgfreq(f3[-1]))
        except (socket.error, KeyboardInterrupt) as e:
            print("Client Disconnected")

            save("combinedpost.wav", 4, RATE, fm)

            save("post0.wav", 1, cRATE, f0)
            save("post1.wav", 1, cRATE, f1)
            save("post2.wav", 1, cRATE, f2)
            save("post3.wav", 1, cRATE, f3)

            print("Done Saving")
            break


from linked_lists import node,ll_start

def add():
    value = input("Enter the value of element:")
    if start.head == None:
        start.head=node(value)
        return

    loop = start.head
    while loop.next:
        loop=loop.next
    added=node(value)
    loop.next=added

def remove():
    value = start.head.data
    start.head = start.head.next
    print(f"Removing from top of queue with value:{value}")

if __name__ == '__main__':
    start = ll_start()

    add()
    add()
    add()

    start.print_list()
    remove()
    start.print_list()


#!/usr/bin/python
#-----------------------------------------------------------
#  Extracts GB Users with point totals for 0-1000 range
#-----------------------------------------------------------

import csv
import requests
from bs4 import BeautifulSoup
import time
import lxml

timestr = time.strftime("%Y-%m-%d-%H"+"00")  # Sets date string used in filename

import sys			# This section corrects for
#reload(sys)			# UNICODE characters and converts
#sys.setdefaultencoding('utf-8') # to ASCII

url = 'http://www.kcgasprices.com/Member_pts_leader.aspx?range=2000'
response = requests.get(url, timeout=5)
html = response.content

#soup = BeautifulSoup(lxml)
soup = BeautifulSoup(html, 'html.parser')

table = soup.find('div', attrs={'class': 'Wrapper'})

table = soup.find('table', attrs={'style': 'width: 100%;'})

# <table style="width: 100%;">

list_of_rows = []
for row in table.findAll('tr'):
    list_of_cells = []
    for cell in row.findAll('td'):
        text = cell.text.replace(' ', '')
        list_of_cells.append(text)
    list_of_rows.append(list_of_cells)

filename = "/home/juren/Projects/GB/Extended/GB-2K-"+timestr+".csv"
outfile = open(filename, "w")
writer = csv.writer(outfile)
writer.writerow(["Rank", "Name", "Location", "Points"])
writer.writerows(list_of_rows)

outfile.close()

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

##import csv
##with open(filename, 'rt') as inp, open('corrected.csv', 'wt') as out:
##    writer = csv.writer(out)
##    for row in csv.reader(inp):
##        if row[1] != "2001":
##            writer.writerow(row)

import pandas
import locale

print (filename)

locale.setlocale(locale.LC_NUMERIC, '')

df = pandas.read_csv(filename)

df1 = df['Points'].apply(locale.atoi)

del df['Points']

Name = df['Name']

Rank = df['Rank']

Location = df['Location']

df2 = pandas.concat([Name,Rank,Location,df1],axis=1)

df2.columns = ['Name','Rank','Location','Points']

df2 = df2.set_index('Name')

df2.to_csv(filename)


#!/usr/bin/env python3

from glob import glob
import os
import subprocess

def main():
    files = glob(f"src{os.sep}**{os.sep}*.adoc", recursive=True)
    files.sort()
    print("The following files will be built:")
    print(files)
    content = str()
    for file in files:
        with open(file) as f:
            content += f.read()
        content += "\n\n"
    completed = subprocess.run(["asciidoctor", "-r", "asciidoctor-pdf", "-r", "asciidoctor-mathematical", "-d", "book",
                                "-b", "pdf", "-B", "build", "-"], input=bytes(content, "utf-8"), stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    with open("book.pdf", mode="wb") as f:
        f.write(completed.stdout)

if __name__ == "__main__":
    main()


import pickle
import base64
from django_redis import get_redis_connection

def merge_cart_cookie_to_redis(request, user, response):
    """
    合并请求用户的购物车数据，将未登录保存在cookie里的保存到redis中
    :param request: 用户的请求对象
    :param user: 当前登录的用户
    :param response: 响应对象，用于清楚购物车cookie
    :return:
    """
    cookie_cart = request.COOKIES.get('carts')
    if cookie_cart is not None:
        return response

    cookie_cart = pickle.loads(base64.b64decode(cookie_cart.encode()))
    redis_conn = get_redis_connection('carts')
    pl = redis_conn.pipeline()
    for electron_id, data in cookie_cart.items():
        # 修改商品数量
        pl.hset("cart_%s" % user.id, electron_id, data['count'])
    pl.execute()

    # 4. 清除cookie中的购物车
    response.delete_cookie('carts')

    return response


"""
Exercise 3: Encapsulate the code below in a function named count, and generalise it
so that it accepts the string and letter as arguments.

word = 'banana'
count = 0
for letter in word:
   if letter == 'a':
       count = count + 1
print(count)
"""

# Define count
def count(string, chosen_letter):
    """
    Counts the number of times a letter appears in a word.
    Example input:
        word = banana
        chosen_letter = a
    Example output:
        3
    """
    counter = 0
    for letter in string:
        if letter == chosen_letter:
            counter += 1
    return counter

# Word and letter inputs from user
word = input("Enter string: ")
chosen_letter = input("Enter letter you want to count: ")

# Ensure word and chosen_letter are not digits
if word.isdigit() or chosen_letter.isdigit():
    print("Input must not be a number")
    quit()

# Ensure letter is one character only
if len(chosen_letter) != 1:
    print("Input of letter must be a single character")
    quit()

# Put output into variable and print
letter_count = count(word, chosen_letter)
print(letter_count)


import socket

players = []

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 9090))

stop_server = False
print('Server is on')
while not stop_server:
    try:
        data, addr = sock.recvfrom(1024)
        if addr not in players:
            players.append(addr)
        if data.decode() == 'Hello':
            print(addr, 'is connected')

        for player in players:
            if addr!= player:
                sock.sendto(data, player)

    except:
        print('Server is off')
        stop_server = True
sock.close()


import time

from library.dbmanager import DbManager
from models.base import Base

class CustomerModel(Base):
    def __init__(self):
        super().__init__()
        self.db = DbManager()
        self.table = 'customer'

    def add_customer_model(self, data):
        sql = self.db.sql_crud_generator(table=self.table,
                                         datas=data,
                                         option='insert')
        last_id = self.db.insert(sql)
        if last_id:
            return last_id
        else:
            return False

    def check_customer_isvalid(self, customer_type, mobile, name, uid=''):
        usr_info = self.get_info_by_id(customer_type, mobile, name, uid)
        if usr_info:
            return 0 == usr_info.get('status')
        else:
            return False

    def get_info_by_id(self, customer_type, mobile, name, uid=''):
        uid = str(uid)
        if len(uid) > 0:
            filters = {'id': uid}
        else:
            filters = {'customer_type': customer_type, 'mobile': mobile} if int(customer_type) == 2 else {
                'customer_type': customer_type, 'name': name}

        sql = self.db.sql_crud_generator(table=self.table, datas=['*'],
                                         option='query',
                                         filters=filters, limit=1)
        row = self.db.fetchone(sql)
        if row:
            return row
        else:
            return dict()

    def get_info_list(self, page, page_size, filters={'status': 0}):
        page = int(page)
        page_size = int(page_size)
        sql = self.db.sql_crud_generator(table=self.table,
                                         datas=['id', 'customer_type', 'invest_status', 'name', 'business_contact',
                                                'salesrep'],
                                         option='query', extension={"order": [{"value": "mtime", "desc": True}]},
                                         limit=[(page - 1) * page_size, page * page_size], filters=filters)
        rows = self.db.fetchall(sql)
        if rows:
            return rows
        else:
            return False

    def get_info_total_by_filters(self, filters):
        sql = self.db.sql_crud_generator(table=self.table, datas=['id'],
                                         option='query', filters=filters)
        rows = self.db.fetchall(sql)
        if rows:
            return rows
        else:
            return []

    def del_by_id(self, uid, admin_id):
        mtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        sql = self.db.sql_crud_generator(table=self.table, datas={'status': 1, 'user_id': admin_id, 'mtime': mtime},
                                         option='update', filters={'id': uid})
        row = self.db.execute(sql)
        if row:
            return row
        else:
            return False

    def update_by_id(self, uid, datas):
        datas['mtime'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        sql = self.db.sql_crud_generator(table=self.table, datas=datas,
                                         option='update', filters={'id': uid})
        row = self.db.execute(sql)
        if row:
            return row
        else:
            return False


import matplotlib.pyplot as plt
import matplotlib.animation as ani
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
def animate(i):
    file=open("sample.txt","r+").read()
    print(file)
    ldata=file.split("\n")
    xdata=[]
    ydata=[]
    for x in ldata:
        if len(x)>1:
            temp=x.split(" ")
            xdata.append(temp[0])
            ydata.append(temp[1])
    ax1.clear()
    ax1.plot(xdata,ydata)
anim=ani.FuncAnimation(fig,animate,interval=1000)


#!/usr/bin/env python3

from sched import scheduler
import time
import yaml
import tweepy
from postgres import Postgres
from os import environ
from telegram.ext import Updater

import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

with open('config.yml', 'r') as ymlfile:
    cfg = yaml.load(ymlfile)

db = Postgres(environ.get("DATABASE_URL"))

twitter_auth = tweepy.OAuthHandler(environ.get("TWITTER_CONSUMER_KEY"), environ.get("TWITTER_CONSUMER_SECRET"))
twitter_auth.set_access_token(environ.get("TWITTER_ACCESS_TOKEN"), environ.get("TWITTER_ACCESS_TOKEN_SECRET"))
twitter = tweepy.API(twitter_auth)

def read_last_twit_from_db() -> str:
    return db.one("SELECT last_twit_id FROM twitlegram WHERE id=1")

def write_last_twit_to_db(twit: str):
    db.run("INSERT INTO twitlegram (id, last_twit_id)"
           " VALUES (1, %(last_twit_id)s)"
           " ON CONFLICT (id) DO"
           " UPDATE SET last_twit_id = excluded.last_twit_id;", {"last_twit_id": twit})

def post_twit(bot, job):
    bot.send_message(chat_id=cfg['chat_id'], text=job.context)

def fetch_latest_twits(queue):
    logger.info('Fetching...')
    last_twit_id: str = read_last_twit_from_db()

    if last_twit_id:
        twits = twitter.user_timeline(screen_name=cfg['screen_name'], since_id=last_twit_id)
    else:
        twits = twitter.user_timeline(screen_name=cfg['screen_name'], count=1)

    if twits:
        write_last_twit_to_db(twits[0].id_str)

    when: int = 0
    for twit in reversed(twits):
        logger.info(twit.text)
        queue.run_once(post_twit, ++when, twit.text)

def periodic(sched, interval, action, actionargs=()):
    sched.enter(interval, 1, periodic, (sched, interval, action, actionargs))
    action(*actionargs)

def main():
    db.run("CREATE TABLE IF NOT EXISTS twitlegram ("
           "id INTEGER NOT NULL UNIQUE,"
           "last_twit_id VARCHAR(100));")

    upd = Updater(environ.get("TELEGRAM_BOT_TOKEN"))
    queue = upd.job_queue
    upd.start_polling()
    sched = scheduler(time.time, time.sleep)
    periodic(sched, cfg['fetch_interval'], fetch_latest_twits, [queue])
    sched.run()

if __name__ == '__main__':
    main()


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

import scrapy
import re
from urllib.parse import urlparse

from cl1024.items import Cl1024AsianCensoredOriginalItem

class Cl1024AsianCensoredOriginalSpider(scrapy.Spider):
    name = "Cl1024AsianCensoredOriginal"
    allowed_domains = ["t66y.com", "rmdown.com"]
    start_urls = ["http://t66y.com/thread0806.php?fid=15"]

    unicode_next_page = u'\u4e0b\u4e00\u9801'
    linkviidii_info = re.compile(r"viidii")
    imgtwist_com = re.compile(r"imagetwist")
    imgchili_net = re.compile(r"imgchili")
    imgad = re.compile(
        r"51688\.cc"
        "|wi\.to"
        "|sinaimg\.cn"
        "|dioimg\.net"
        "|dio889\.net"
        "|hotavxxx\.com"
        )

    def parse(self, response):
        #links in

, there's also sticky threads.
        thread_hrefs = (
            response.selector.xpath("//h3/a[contains(@href,'htm_data/15')]/@href"))
        for thread_href in thread_hrefs:
            thread_url = response.urljoin(thread_href.extract())
            if thread_url != "http://t66y.com/htm_data/15/0907/341557.html":
            #give an item page to resolve by parse_thread.
                yield scrapy.Request(thread_url, callback=self.parse_thread)
                #for test
                #break

        next_page_href = response.selector.xpath("//a[text()='%s']/@href" % self.unicode_next_page)[0]
        next_page_url = response.urljoin(next_page_href.extract())

        #give the next list page to parse.
        #for test
        #yield scrapy.Request(next_page_url, callback=self.parse)

    def parse_thread(self, response):
        item = Cl1024AsianCensoredOriginalItem()
        item["t_url"] = response.url
        item["t_date"] = urlparse(item["t_url"])[2].split("/")[3]
        post_time =(
            response.selector.xpath("//div[contains(@class,'tipad')]/text()")[1].extract())
        post_time = (post_time.split("|"))[0]
        post_time = post_time.split(":",1)[1].strip()
        item["t_time"] = post_time
        item["t_id"] = (
            response.selector.xpath("//input[@name='tid']/@value")[0].extract())
        t_title = response.selector.xpath("string(//title)")[0].extract()
        #remove site info in title
        item["t_title"] = t_title[:-28].strip()
        item["t_author"] = response.selector.xpath(
            "//th[contains(@class,'r_two')]/b/text()")[0].extract()
        item["t_category"] = "asian_censored_original"

        t_allimg_list = []
        for link in response.selector.xpath("//img/@src").extract():
            if self.imgad.search(link):
                continue
            if self.imgchili_net.search(link):
                t_allimg_list.append(link.replace("http://t", "http://i"))
            elif self.imgtwist_com.search(link):
                t_allimg_list.append(link.replace("/th/", "/i/"))
            else:
                t_allimg_list.append(link)

        #for test
        #item["image_urls"] = []
        item["image_urls"] = list(set(t_allimg_list))

        #Get download links.
        item["t_download_list"] = []
        item["torrent_urls"] = []
        download_hrefs = response.selector.xpath(
            "//a[contains(@href,'rmdown')]/@href")

        for download_href in download_hrefs:
            download_url = response.urljoin(download_href.extract())
            item["t_download_list"].append(download_url)

        for download_url in item["t_download_list"]:
            referer_headers = {
                "Referer":download_url
            }
            if self.linkviidii_info.search(download_url):
                download_url = download_url.replace("http://www.viidii.info/?", "")
                download_url = download_url.replace("______",".")[0:-2]

                yield scrapy.Request(
                download_url,
                headers = referer_headers,
                meta = {"item":item},
                callback=self.parse_torrent)

    def parse_torrent(self, response):
        ref = response.selector.xpath("//input[@name='ref']/@value")[0].extract()
        reff = response.selector.xpath("//input[@name='reff']/@value")[0].extract()
        torrent_url = (
            "http://www.rmdown.com/download.php?ref=%s&reff=%s&submit=download"
            % (ref, reff))

        item = response.meta["item"]
        item["torrent_urls"].append(torrent_url)
        yield item