import math
import os
import cv2
import pygame
import numpy as np
from pygame.locals import *

FPS = 60
ANIMATION_SPEED = 0.18
WIN_WIDTH = 223 * 2
WIN_HEIGHT = 382

# define reward
REWARD_SURVIVE = 1.0
REWARD_PASS = 0.1
REWARD_DEAD = 0.0

# define actions
ACTION_DO_NOTHING = 0
ACTION_FLAP = 1

# define state type
STATE_TYPE_NUMBERS = 0
STATE_TYPE_IMAGE = 1

def msec_to_frames(milliseconds, fps=FPS):
   return fps * milliseconds / 1000.0

def frame_to_msec(frames, fps=FPS):
	return 1000.0*frames/fps

# hidden bird, to generate input for model
class H_Bird:
	WIDTH = HEIGHT = 32
	SINK_SPEED = 0.05
	CLIMB_SPEED = 0.2
	CLIMB_DURATION = 333.3

	def __init__(self, x, y, msec_to_climb, raw_images):
		self.x, self.y = x, y
		self.msec_to_climb = msec_to_climb
		self._img_wingup, self._img_wingdown = raw_images
		self._mask_wingup = self._img_wingup[:,:,3:4]/255
		self._mask_wingdown = self._img_wingdown[:,:,3:4]/255
		self._img_wingup = self._img_wingup[:,:,0:3]
		self._img_wingdown = self._img_wingdown[:,:,0:3]
		self.speed = H_Bird.SINK_SPEED

	def flap(self):
		self.msec_to_climb = H_Bird.CLIMB_DURATION

	def update(self, delta_frames=1):
		delta_time = frame_to_msec(delta_frames)
		if self.msec_to_climb>0:
			frac_climb_done = 1 -  self.msec_to_climb/H_Bird.CLIMB_DURATION
			self.speed = -H_Bird.CLIMB_SPEED * (1 - math.cos(frac_climb_done * math.pi))
			self.y +=  self.speed * delta_time
			self.msec_to_climb -= delta_time
		else:
			self.speed = H_Bird.SINK_SPEED
			self.y += self.speed * delta_time

	def hit_edges(self):
		if self.y<0 or self.y + H_Bird.HEIGHT>WIN_HEIGHT:
			return True
		return False

	def get_image(self, clock_tick):
		if clock_tick%500>=250:
			return self._img_wingup
		else:
			return self._img_wingdown

	def get_mask(self, clock_tick):
		if clock_tick%500>=250:
			return self._mask_wingup
		else:
			return self._mask_wingdown
			image

	def draw(self, paper, clock_tick):
		start_x = int(self.x)
		end_x = start_x + H_Bird.WIDTH
		start_y = int(self.y)
		end_y = start_y + H_Bird.HEIGHT

		if start_y<0:
			mask = self.get_mask(clock_tick)[int(-start_y):H_Bird.HEIGHT]
			image = self.get_image(clock_tick)[int(-start_y):H_Bird.HEIGHT]
			end_y = int(end_y)
			start_y = 0
		elif end_y > WIN_HEIGHT:
			mask = self.get_mask(clock_tick)[0: int(WIN_HEIGHT-start_y)]
			image = self.get_image(clock_tick)[0: int(WIN_HEIGHT-start_y)]
			end_y = WIN_HEIGHT
			start_y = int(start_y)
		else:
			mask = self.get_mask(clock_tick)
			image = self.get_image(clock_tick)
			start_y = int(start_y)
			end_y = int(end_y)

		bird_img = image * mask + paper[start_y: end_y, start_x: end_x] * (1 - mask)
		paper[start_y: end_y, start_x: end_x] = bird_img

class H_PipePair:
	WIDTH = 80
	PIECE_HEIGHT = 32
	ADD_INTERVAL = 2500
	TOTAL_PIPE_BODY_PIECES = int(
		(WIN_HEIGHT -                  # fill window from top to bottom
		3 * H_Bird.HEIGHT -             # make room for bird to fit through
		3 * PIECE_HEIGHT) /  # 2 end pieces + 1 body piece
		PIECE_HEIGHT)

	def __init__(self, pipe_end_img, pipe_body_img, bottom_pieces=np.random.randint(1, TOTAL_PIPE_BODY_PIECES)):
		self.x = float(WIN_WIDTH - 1)
		self.score_counted = False

		self.image = np.zeros([WIN_HEIGHT, H_PipePair.WIDTH, 4], dtype=np.float32)
		self.bottom_pieces = bottom_pieces
		self.top_pieces = H_PipePair.TOTAL_PIPE_BODY_PIECES - self.bottom_pieces

		start_x = 0
		end_x = H_PipePair.WIDTH

		# bottom pieces
		for i in range(1, self.bottom_pieces+1):
			start_y = WIN_HEIGHT - i * H_PipePair.PIECE_HEIGHT
			end_y = start_y + H_PipePair.PIECE_HEIGHT
			self.image[start_y: end_y, start_x: end_x] = pipe_body_img

		start_y = WIN_HEIGHT - self.bottom_pieces * H_PipePair.PIECE_HEIGHT - H_PipePair.PIECE_HEIGHT
		end_y = start_y + H_PipePair.PIECE_HEIGHT
		self.image[start_y: end_y, start_x: end_x] = pipe_end_img

		# top pipe
		for i in range(self.top_pieces):
			start_y = i * H_PipePair.PIECE_HEIGHT
			end_y = start_y + H_PipePair.PIECE_HEIGHT
			self.image[start_y:end_y, start_x:end_x] = pipe_body_img
		top_pipe_end_y = self.top_pieces * PipePair.PIECE_HEIGHT
		start_y = self.top_pieces * H_PipePair.PIECE_HEIGHT
		end_y = start_y + H_PipePair.PIECE_HEIGHT
		self.image[start_y: end_y, start_x: end_x] = pipe_end_img

		# compensate for added end pieces
		self.top_pieces += 1
		self.bottom_pieces += 1

		self.mask = self.image[:,:,3:4]/255
		self.image = self.image[:,:,0:3]

	def collides_with(self, bird):
		bird_upper_left_corner = [bird.x, bird.y]
		if self.x<bird_upper_left_corner[0]<self.x + H_PipePair.WIDTH and bird_upper_left_corner[1]<self.top_pieces*H_PipePair.PIECE_HEIGHT:
			return True

		bird_upper_right_corner = [bird.x + H_Bird.WIDTH, bird.y]
		if self.x<bird_upper_right_corner[0]<self.x + H_PipePair.WIDTH and bird_upper_right_corner[1] < self.top_pieces*H_PipePair.PIECE_HEIGHT:
			return True

		bird_lower_left_corner = [bird.x, bird.y + H_Bird.HEIGHT]
		if self.x<bird_lower_left_corner[0]<self.x + H_PipePair.WIDTH and bird_lower_left_corner[1]>WIN_HEIGHT - self.bottom_pieces*H_PipePair.PIECE_HEIGHT:
			return True

		bird_lower_right_corner = [bird.x + H_Bird.WIDTH, bird.y + H_Bird.HEIGHT]
		if self.x<bird_lower_right_corner[0]<self.x + H_PipePair.WIDTH and bird_lower_right_corner[1]>WIN_HEIGHT - self.bottom_pieces*H_PipePair.PIECE_HEIGHT:
			return True

		return False

	def is_visible(self):
		return -H_PipePair.WIDTH< self.x < WIN_WIDTH

	def update(self, delta_frames=1):
		self.x -=ANIMATION_SPEED * frame_to_msec(delta_frames)

	def draw(self, paper):
		start_x = self.x
		end_x = start_x + H_PipePair.WIDTH
		if start_x<0:
			mask = self.mask[:, int(-start_x): H_PipePair.WIDTH]
			image = self.image[:, int(-start_x): H_PipePair.WIDTH]
			end_x = int(end_x)
			start_x = 0
		elif end_x>WIN_WIDTH:
			mask = self.mask[:, 0: int(WIN_WIDTH-start_x)]
			image = self.image[:, 0: int(WIN_WIDTH-start_x)]
			end_x = WIN_WIDTH
			start_x = int(start_x)
		else:
			mask = self.mask
			image = self.image
			start_x = int(start_x)
			end_x = int(end_x)
		pipe_img = image * mask + paper[:, start_x: end_x] * (1 - mask)
		paper[:, start_x: end_x] = pipe_img

# create environment without clock ticking
class QuickEnvironment:

	SCREENSHOT_INTERVAL = 100
	MAX_DURATION = 100000 # 100 secs

	def __init__(self, image_folder):
		self.images = self.load_raw_images(image_folder=image_folder)
		self.bird = H_Bird(50, int((WIN_HEIGHT - H_Bird.HEIGHT)/2), 2, [self.images['bird-wingup'], self.images['bird-wingdown']])
		self.clock_tick = 0
		self.frame_count = 0
		self.pipes = []
		self.background = np.concatenate([self.images['background'], self.images['background']], axis=1)
		self.background = self.background[:,:,0:3]

	def load_raw_images(self, image_folder='./cimages/'):
		def load_raw_image(img_file_name):
			img = np.float32(cv2.imread(image_folder + img_file_name, cv2.IMREAD_UNCHANGED))
			if img.shape[2]==3:
				img = np.concatenate([img, np.ones([img.shape[0], img.shape[1], 1], dtype=np.float32)*255], axis=2)
			return img

		return {
			'background':  load_raw_image('background.png'),
			'pipe-end': load_raw_image('pipe_end.png'),
			'pipe-body': load_raw_image('pipe_body.png'),
			'bird-wingup': load_raw_image('bird_wing_up.png'),
			'bird-wingdown': load_raw_image('bird_wing_down.png')}

	def reset(self, state_type=STATE_TYPE_NUMBERS):
		self.clock_tick = 0
		self.frame_count = 0
		self.bird.y = np.random.randint(low=WIN_HEIGHT/4, high=3*WIN_HEIGHT/4-H_Bird.HEIGHT)
		self.bird.msec_to_climb = 2
		self.pipes.clear()
		return self.get_state(state_type)

	def screenshot(self):
		paper = self.background.copy()
		self.bird.draw(paper, self.clock_tick)
		for pipe in self.pipes:
			pipe.draw(paper)
		return paper

	def get_duration(self):
		return frame_to_msec(self.frame_count)/1000

	def get_next_pipe_pos(self):
		pipe_pos = WIN_WIDTH
		for pipe in self.pipes:
			if pipe.x + H_PipePair.WIDTH > self.bird.x:
				pipe_pos = pipe.x
				break
		return pipe_pos

	def update(self, action=ACTION_DO_NOTHING):
		# increase clock tick
		self.clock_tick+=16.667 # 60 fps => clock tick += 1000/60
		t_action = action
		# init reward, done
		reward = REWARD_SURVIVE
		done = False

		n_frames = int(np.round(msec_to_frames(QuickEnvironment.SCREENSHOT_INTERVAL)))
		for i in range(n_frames):
			# after every 3000 ms add a piple
			if self.frame_count%msec_to_frames(PipePair.ADD_INTERVAL)==0:
				self.pipes.append(H_PipePair(self.images['pipe-end'], self.images['pipe-body'], np.random.randint(1, H_PipePair.TOTAL_PIPE_BODY_PIECES)))

			# update bird
			if t_action==ACTION_FLAP:
				self.bird.flap()
				t_action = ACTION_DO_NOTHING
			self.bird.update()

			# check bird state
			if self.bird.hit_edges():
				reward = REWARD_DEAD
				done = True
				return None, reward, done

			# update pipe
			for pipe in self.pipes:
				pipe.update()
				if pipe.collides_with(self.bird):
					reward = REWARD_DEAD
					done = True
					return None, reward, done

			# check if episode is too long
			if self.frame_count>msec_to_frames(QuickEnvironment.MAX_DURATION):
				reward = REWARD_SURVIVE
				done = True
				return None, reward, done

			if not self.pipes[0].is_visible():
				self.pipes.pop(0)

			# increase frame count
			self.frame_count += 1
		return self.get_state(), reward, done

	def get_state(self, state_type=STATE_TYPE_NUMBERS):
		if state_type==STATE_TYPE_NUMBERS:
			return [self.bird.y/191 - 1, self.bird.speed/H_Bird.SINK_SPEED, self.get_next_pipe_pos()/223 - 1]
		else: # this state type does not contain speed information
			return self.screenshot()

class Bird(pygame.sprite.Sprite):

	WIDTH = H_Bird.WIDTH
	HEIGHT = H_Bird.HEIGHT
	SINK_SPEED = H_Bird.SINK_SPEED
	CLIMB_SPEED = H_Bird.CLIMB_SPEED
	CLIMB_DURATION = H_Bird.CLIMB_DURATION

	def __init__(self, x, y, msec_to_climb, images):
		super(Bird, self).__init__()
		self.x, self.y = x, y
		self.msec_to_climb = msec_to_climb
		self._img_wingup, self._img_wingdown = images
		self._mask_wingup = pygame.mask.from_surface(self._img_wingup)
		self._mask_wingdown = pygame.mask.from_surface(self._img_wingdown)
		self.speed = Bird.SINK_SPEED

	def update(self, delta_frames=1):
		delta_time = frame_to_msec(delta_frames)
		if self.msec_to_climb>0:
			frac_climb_done = 1 -  self.msec_to_climb/Bird.CLIMB_DURATION
			self.speed = -Bird.CLIMB_SPEED * (1 - math.cos(frac_climb_done * math.pi))
			self.y += self.speed * delta_time
			self.msec_to_climb -= delta_time
		else:
			self.speed = Bird.SINK_SPEED
			self.y += self.speed * delta_time

	def flap(self):
		self.msec_to_climb = Bird.CLIMB_DURATION-1

	@property
	def image(self):
		if pygame.time.get_ticks()%500>=250:
			return self._img_wingup
		else:
			return self._img_wingdown

	@property
	def mask(self):
		if pygame.time.get_ticks()%500>=250:
			return self._mask_wingup
		else:
			return self._mask_wingdown

	@property
	def rect(self):
		return Rect(self.x, self.y, Bird.WIDTH, Bird.HEIGHT)

class PipePair(pygame.sprite.Sprite):
	WIDTH = H_PipePair.WIDTH
	PIECE_HEIGHT = H_PipePair.PIECE_HEIGHT
	ADD_INTERVAL = H_PipePair.ADD_INTERVAL
	TOTAL_PIPE_BODY_PIECES = H_PipePair.TOTAL_PIPE_BODY_PIECES
	def __init__(self, pipe_end_img, pipe_body_img, bottom_pieces=np.random.randint(0, H_PipePair.TOTAL_PIPE_BODY_PIECES)):
		self.x = float(WIN_WIDTH - 1)
		self.score_counted = False

		self.image = pygame.Surface((PipePair.WIDTH, WIN_HEIGHT), SRCALPHA)
		self.image.convert()
		self.image.fill((0,0,0,0))

		total_pipe_body_pieces = int(
			(WIN_HEIGHT -                  # fill window from top to bottom
			 3 * Bird.HEIGHT -             # make room for bird to fit through
			 3 * PipePair.PIECE_HEIGHT) /  # 2 end pieces + 1 body piece
			PipePair.PIECE_HEIGHT          # to get number of pipe pieces
		)
		self.bottom_pieces = bottom_pieces
		self.top_pieces = total_pipe_body_pieces - self.bottom_pieces
		# bottom pieces
		for i in range(1, self.bottom_pieces+1):
			piece_pos = (0, WIN_HEIGHT - i*PipePair.PIECE_HEIGHT)
			self.image.blit(pipe_body_img, piece_pos)
		bottom_pipe_end_y = WIN_HEIGHT - self.bottom_pieces*PipePair.PIECE_HEIGHT
		bottom_end_piece_pos = (0, bottom_pipe_end_y - PipePair.PIECE_HEIGHT)
		self.image.blit(pipe_end_img, bottom_end_piece_pos)

		# top pipe
		for i in range(self.top_pieces):
			self.image.blit(pipe_body_img, (0, i * PipePair.PIECE_HEIGHT))
		top_pipe_end_y = self.top_pieces * PipePair.PIECE_HEIGHT
		self.image.blit(pipe_end_img, (0, top_pipe_end_y))

		# compensate for added end pieces
		self.top_pieces += 1
		self.bottom_pieces += 1

		# for collision detection
		self.mask = pygame.mask.from_surface(self.image)

	def is_visible(self):
		return -PipePair.WIDTH< self.x < WIN_WIDTH

	@property
	def rect(self):
		return Rect(self.x, 0, PipePair.WIDTH, PipePair.PIECE_HEIGHT)

	def update(self, delta_frames=1):
		self.x -=ANIMATION_SPEED * frame_to_msec(delta_frames)

	def collides_with(self, bird):
		bird_upper_left_corner = [bird.x, bird.y]

		if self.x<bird_upper_left_corner[0]<self.x + PipePair.WIDTH and bird_upper_left_corner[1]<self.top_pieces*PipePair.PIECE_HEIGHT:
			return True

		bird_upper_right_corner = [bird.x + Bird.WIDTH, bird.y]
		if self.x<bird_upper_right_corner[0]<self.x + PipePair.WIDTH and bird_upper_right_corner[1] < self.top_pieces*PipePair.PIECE_HEIGHT:
			return True

		bird_lower_left_corner = [bird.x, bird.y + Bird.HEIGHT]
		if self.x<bird_lower_left_corner[0]<self.x + PipePair.WIDTH and bird_lower_left_corner[1]>WIN_HEIGHT - self.bottom_pieces*PipePair.PIECE_HEIGHT:
			return True

		bird_lower_right_corner = [bird.x + Bird.WIDTH, bird.y + Bird.HEIGHT]
		if self.x<bird_lower_right_corner[0]<self.x + PipePair.WIDTH and bird_lower_right_corner[1]>WIN_HEIGHT - self.bottom_pieces*PipePair.PIECE_HEIGHT:
			return True

		return False

class Environment:

	SCREENSHOT_INTERVAL = QuickEnvironment.SCREENSHOT_INTERVAL

	def __init__(self, image_folder='./images/'):
		pygame.init()
		self.display_surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
		pygame.display.set_caption('Reinforcement Environment')
		self.images = self.load_images(image_folder=image_folder)
		self.clock = pygame.time.Clock()
		self.bird = Bird(
			50,
			int((WIN_HEIGHT - Bird.HEIGHT)/2),
			2,
			[self.images['bird-wingup'], self.images['bird-wingdown']])
		self.pipes = []

	def load_images(self, image_folder='./images/'):
		def load_image(img_file_name):
			img = pygame.image.load(image_folder + img_file_name)
			img.convert()
			return img

		return {
			'background':  load_image('background.png'),
			'pipe-end': load_image('pipe_end.png'),
			'pipe-body': load_image('pipe_body.png'),
			'bird-wingup': load_image('bird_wing_up.png'),
			'bird-wingdown': load_image('bird_wing_down.png')}

	def should_screenshot(self):
		if self.frame_count%msec_to_frames(Environment.SCREENSHOT_INTERVAL)==0:
			return True
		return False

	def screenshot(self):
		img = pygame.surfarray.array3d(self.display_surface)
		img = img.swapaxes(0,1)
		return img

	def reset(self):
		self.clock_tick = 0
		self.frame_count = 0
		self.bird.y = int((WIN_HEIGHT - H_Bird.HEIGHT)/2)
		self.bird.msec_to_climb = 2
		self.pipes.clear()

	def run(self):
		done = False
		paused = False
		frame_clock = 0
		while not done:
			self.clock.tick(FPS)
			clock_tick = pygame.time.get_ticks()

			# add pipe after every 3 secs
			if not (paused or frame_clock%msec_to_frames(PipePair.ADD_INTERVAL)):
				self.pipes.append(PipePair(self.images['pipe-end'], self.images['pipe-body'], np.random.randint(1, PipePair.TOTAL_PIPE_BODY_PIECES)))

			# handle events
			for e in pygame.event.get():
				if e.type==KEYUP:
					if e.key==K_ESCAPE:
						done=True
						break
					elif e.key==K_UP:
						self.bird.flap()
					elif e.key==K_p:
						paused = not paused
				elif e.type==QUIT:
					done=True
					break

			# do not do anything if paused
			if paused:
				continue

			if self.bird.y<=0 or self.bird.y>=WIN_HEIGHT-Bird.HEIGHT:
				done = True

			for x in [0, WIN_WIDTH/2]:
				self.display_surface.blit(self.images['background'], (x, 0))

			if len(self.pipes)>0 and not self.pipes[0].is_visible():
				self.pipes.pop(0)

			for pipe in self.pipes:
				pipe.update()
				self.display_surface.blit(pipe.image, pipe.rect)
				if pipe.collides_with(self.bird):
					done = True
					break

			self.bird.update()
			self.display_surface.blit(self.bird.image, self.bird.rect)

			# screenshot
			'''
			if not frame_clock%msec_to_frames(Environment.SCREENSHOT_INTERVAL):
				cv2.imwrite('test{:06d}.jpg'.format(frame_clock), self.screenshot())
			'''

			pygame.display.update()
			frame_clock+=1

		pygame.quit()

'''
env = Environment('./images/')
env.run()

env = QuickEnvironment('./images/')
env.reset()
done = False
count = 0
while not done:
	reward, done = env.update(action=np.random.randint(2))
	img = env.screenshot()
	cv2.imwrite('test{:06d}.jpg'.format(count), img)
	count += 1
	if count>1000:
		break
'''


"""
Implementazione vecchia alternativa a imdispatcher.py

Più prolissa, quindi da preferire l'altra.
C'e' l'opzione recursive in più, per ora.

    python imdispatcher.py <template-dir> <search-dir> <dst-dir> [--threshold] [--dry_run]
"""

# TODO: log ingombrante da togliere

from cv2 import error
from imfinder import bool_find, imread
from os import sys, walk
from os.path import abspath, basename, exists, isdir, join, split, splitext
import argparse
import datetime
import re
import shutil

WRONG_IMAGE_SIZE = 'wrong_image_size'
NON_MATCHING_PATHS = 'non-matching_path'
INVALID_IMAGES = 'invalid_image'
UNCLASSIFIED_IMAGES = 'unclassified_image'
DISPATCHED_IMAGES = 'dispatched_images'
TEMPLATE_MATCHING_ERROR = 'template_matching_error'

def move(src, dst, dry_run=False):
    assert exists(dst), 'dst="{}" does not exist'.format(dst)
    assert isdir(dst), 'dst="{}" is not a directory'.format(dst)
    src_dir_path, src_file_name = split(src)
    dst_file_name = src_file_name
    while True:
        dst_file_path = join(dst, dst_file_name)
        if exists(dst_file_path):
            name, ext = splitext(dst_file_name)
            dst_file_name = name + '+' + ext
        else:
            break

    if not dry_run:
        shutil.move(src, dst_file_path)

    return dst_file_path

def process_templates(templates_dirpath):
    print('process_templates', templates_dirpath)
    res = []
    for dirpath, dirs, files in walk(templates_dirpath):
        for file_name in files:
            if not file_name.startswith('template'):
                continue

            file_path = join(dirpath, file_name)
            template = imread(file_path, 0)
            assert template is not None, 'Invalid template image: {}'.format(file_path)
            label = dirpath[len(templates_dirpath):]
            res.append((file_path, template, label))
    return res

def log(start_datetime, file_path, result_flag):
    dt = start_datetime.isoformat()[:-7].replace(':', '-')
    file_name = u'image_dispatcher_{}_{}.log'.format(
        dt, result_flag)

    now_str = datetime.datetime.now().isoformat()
    with open(file_name, 'a') as fp:
        fp.write(u'{}: {}\n'.format(now_str, file_path.encode('utf-8')))

def main(templates_dirpath, root, dst,
        matching_threshold=0.9,
        dry_run=False,
        recursive=False,
        file_path_filter='jpg$|png$',
        image_size=None,
        log_file_path='log.txt'):

    res = {
        DISPATCHED_IMAGES: [],
        INVALID_IMAGES: [],
        NON_MATCHING_PATHS: [],
        UNCLASSIFIED_IMAGES: [],
        WRONG_IMAGE_SIZE: [],
    }

    if image_size:
        image_shape = tuple(reversed(image_size))

    root = abspath(root)
    dst = abspath(dst)
    file_path_compiled_regexp = re.compile(file_path_filter, re.IGNORECASE)
    n_dirs = n_files = n_images = n_filtered_images = n_dispatched = 0

    templates_data = process_templates(templates_dirpath)
    print('{} templates found'.format(len(templates_data)))

    start_datetime = datetime.datetime.now()

    try:
        for dirpath, dirs, files in walk(root, recursive=recursive):
            if dirpath == dst or dirpath == templates_dirpath:
                # we are entering in the dst dir! skip please
                print('SKIPPING dst or templates directory', dirpath)
                continue
            if basename(dirpath).startswith('dispatched'):
                print('skip', dispatched)
                continue

            print('{:,} dirs/{:,} files: {}'.format(n_dirs, n_files, dirpath))
            n_dirs += 1
            for file_name in files:
                n_files += 1
                file_path = join(dirpath, file_name)
                if not re.search(file_path_compiled_regexp, file_path):
                    #res[NON_MATCHING_PATHS].append(file_path)
                    log(start_datetime, file_path, NON_MATCHING_PATHS)
                    continue

                # Read image to classify
                image = imread(file_path, 0)

                if image is None:
                    #res[INVALID_IMAGES].append(file_path)
                    log(start_datetime, file_path, INVALID_IMAGES)
                    #print('   invalid image')
                    continue

                n_images += 1
                if image_size and image.shape != image_shape:
                    log(start_datetime, file_path, WRONG_IMAGE_SIZE)
                    continue

                n_filtered_images += 1

                dispatched = False
                for template_path, template, dest in templates_data:
                    # Template matching
                    # print('{} vs {} ({})'.format(template_path, file_path, matching_threshold))
                    try:
                        r = bool_find(template, image, threshold=matching_threshold)
                    except error as err:
                        log(start_datetime, file_path, TEMPLATE_MATCHING_ERROR)
                        print('Template matching error')
                        print(err)
                        r = False

                    if r:
                        dt_str = datetime.datetime.now().isoformat()[:-7]
                        dst_dir = join(dst, dest)

                        dst_file_path = move(file_path, dst_dir, dry_run=dry_run)

                        dispatched = True
                        n_dispatched += dispatched

                        res[DISPATCHED_IMAGES].append((file_path, dst_file_path, basename(template_path)))
                        msg = u'{}: "{}" contains "{}": >> {}'.format(
                            dt_str, file_path, basename(template_path), dst_dir)
                        print(msg)
                        print('{:,} moved/{:,} candidates/{:,} images/{:,} files'.format(
                            n_dispatched, n_filtered_images, n_images, n_files))

                        with open(log_file_path, 'a') as fp:
                            fp.write(u'{}\n'.format(msg))

                        log(start_datetime, file_path, DISPATCHED_IMAGES)

                        break

                if not dispatched:
                    log(start_datetime, file_path, UNCLASSIFIED_IMAGES)
                    sys.stdout.write('.')
                    sys.stdout.flush()
                    #res[UNCLASSIFIED_IMAGES].append(file_path)

    except KeyboardInterrupt:
        print('Interrupted by user.')

    return res

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Image dispatcher')
    parser.add_argument('query', help='query image path to search for')
    parser.add_argument('root', default=u'.',
                        help='root directory to search in [default=%(default)s]')
    parser.add_argument('dst', default=u'../dispatched-images',
                        help='directory to move images into [default="%(default)s"]')
    parser.add_argument('--threshold', type=float, default=0.9,
                        help='template matching threshold [default=%(default)s]')
    parser.add_argument('--recursive', action='store_true', default=False,
                        help='search recursively inside `root` [default=%(default)s]')
    parser.add_argument('--dry_run', action='store_true', default=False,
                        help='do not actually move files [default=%(default)s]')
    parser.add_argument('--logfile', default='log.txt',
                        help='log file path [default=%(default)s]')
    parser.add_argument('--log', dest='loglevel', default='DEBUG',
                        help='Set the logging level [default=%(default)s]')
    args = parser.parse_args()
    print(args)
    results = main(args.query, args.root, args.dst, matching_threshold=args.threshold,
        recursive=args.recursive, dry_run=args.dry_run, log_file_path=args.logfile)

    print('{:,} images dispatched'.format(len(results[DISPATCHED_IMAGES])))

    sys.exit(0)


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

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.platypus.tables import Table, TableStyle
from reportlab.platypus.flowables import Image
import cStringIO as StringIO

class PDFBase(object):
	def __init__(self, recipient, invoicenum, imagedir, currency):
		self.pdfdata = StringIO.StringIO()
		self.canvas = Canvas(self.pdfdata)

		self.recipient = recipient
		self.invoicenum = invoicenum
		self.imagedir = imagedir or '.'
		self.currency = currency or '€'

		self.preview = False

		self.canvas.setTitle("PostgreSQL Europe Invoice #%s" % self.invoicenum)
		self.canvas.setSubject("PostgreSQL Europe Invoice #%s" % self.invoicenum)
		self.canvas.setAuthor("PostgreSQL Europe")
		self.canvas._doc.info.producer = "PostgreSQL Europe Invoicing System"

	def trimstring(self, s, maxlen, fontname, fontsize):
		while len(s) > 5:
			if self.canvas.stringWidth(s, fontname, fontsize) <= maxlen:
				return s
			s = s[:len(s)-2]
		return s

	def textlines(self, t, lines):
		for l in lines.splitlines():
			t.textLine(l)

	def _pageheader(self):
		if self.preview:
			t = self.canvas.beginText()
			t.setTextOrigin(6*cm, 4*cm)
			t.setFont("Times-Italic", 70)
			t.setFillColorRGB(0.9,0.9,0.9)
			t.textLine("PREVIEW PREVIEW")
			self.canvas.rotate(45)
			self.canvas.drawText(t)
			self.canvas.rotate(-45)

		im = Image("%s/PostgreSQL_logo.1color_blue.300x300.png" % self.imagedir, width=3*cm, height=3*cm)
		im.drawOn(self.canvas, 2*cm, 25*cm)
		t = self.canvas.beginText()
		t.setFillColorRGB(0,0,0,0)
		t.setFont("Times-Roman", 10)
		t.setTextOrigin(6*cm, 27.5*cm)
		self.textlines(t,"""PostgreSQL Europe
Carpeaux Diem
13, rue du Square Carpeaux
75018 PARIS
France
""")
		self.canvas.drawText(t)

		t = self.canvas.beginText()
		t.setTextOrigin(2*cm, 23*cm)
		t.setFont("Times-Roman", 10)
		t.textLine("")
		self.textlines(t, """
Your contact: Guillaume Lelarge
Function: PostgreSQL Europe Treasurer
E-mail: treasurer@postgresql.eu
""")
		self.canvas.drawText(t)

		t = self.canvas.beginText()
		t.setTextOrigin(11*cm, 23*cm)
		t.setFont("Times-Italic", 11)
		t.textLine("To:")
		t.setFont("Times-Roman", 11)
		self.textlines(t, self.recipient)
		self.canvas.drawText(t)

		p = self.canvas.beginPath()
		p.moveTo(2*cm, 18.9*cm)
		p.lineTo(19*cm, 18.9*cm)
		self.canvas.drawPath(p)

class PDFInvoice(PDFBase):
	def __init__(self, recipient, invoicedate, duedate, invoicenum=None, imagedir=None, currency='€', preview=False, receipt=False, bankinfo=True):
		self.invoicedate = invoicedate
		self.duedate = duedate
		self.preview = preview
		self.receipt = receipt
		self.bankinfo = bankinfo
		self.rows = []

		if self.receipt:
			# Never include bank info on receipts
			self.bankinfo = False

		super(PDFInvoice, self).__init__(recipient, invoicenum, imagedir, currency)

	def addrow(self, title, cost, count=1):
		self.rows.append((title, cost, count,))

	def save(self):
		# We can fit 15 rows on one page. We might want to do something
		# cute to avoid a single row on it's own page in the future, but
		# for now, just split it evenly.
		for pagenum in range(0, (len(self.rows)-1)/15+1):
			self._pageheader()
			islastpage = (pagenum == (len(self.rows)-1)/15)

			if len(self.rows) > 15:
				suffix = " (page %s/%s)" % (pagenum+1, len(self.rows)/15+1)
			else:
				suffix = ''

			# Center between 2 and 19 is 10.5
			if self.invoicenum:
				if self.receipt:
					self.canvas.drawCentredString(10.5*cm,19*cm, "RECEIPT FOR INVOICE NUMBER %s%s" % (self.invoicenum, suffix))
				else:
					self.canvas.drawCentredString(10.5*cm,19*cm, "INVOICE NUMBER %s - %s%s" % (self.invoicenum, self.invoicedate.strftime("%B %d, %Y"),suffix))
			else:
				self.canvas.drawCentredString(10.5*cm,19*cm, "RECEIPT - %s%s" % (self.invoicedate.strftime("%B %d, %Y"), suffix))

			if pagenum == 0:
				tbldata = [["Item", "Price", "Count", "Amount"], ]
			else:
				tbldata = [["Item - continued from page %s" % pagenum, "Price", "count", "amount"], ]

			tbldata.extend([(self.trimstring(title, 10.5*cm, "Times-Roman", 10),
							 "%.2f %s" % (cost, self.currency),
							 count,
							 "%.2f %s" % ((cost * count), self.currency))
							for title,cost, count in self.rows[pagenum*15:(pagenum+1)*15]])
			style = [
					('BACKGROUND',(0,0),(3,0),colors.lightgrey),
					('ALIGN',(1,0),(3,-1),'RIGHT'),
					('LINEBELOW',(0,0),(-1,0), 2, colors.black),
					('OUTLINE', (0,0), (-1, -1), 1, colors.black),
				]
			if islastpage:
				tbldata.append(['','','Total',"%.2f %s" % (sum([cost*count for title,cost,count in self.rows]),self.currency)])
				style.append(('LINEABOVE', (-2,-1), (-1, -1), 2, colors.black))
			else:
				tbldata.append(['          Continued on page %s' % (pagenum + 2), '', '', ''])
				style.append(('ALIGN', (0, -1), (-1, -1), 'CENTER'))
				style.append(('FONT', (0, -1), (-1, -1), 'Times-Italic'))

			t = Table(tbldata, [10.5*cm, 2.5*cm, 1.5*cm, 2.5*cm])
			t.setStyle(TableStyle(style))
			w,h = t.wrapOn(self.canvas,10*cm,10*cm)
			t.drawOn(self.canvas, 2*cm, 18*cm-h)

			if self.receipt:
				self.canvas.drawCentredString(10.5*cm,17.3*cm-h, "This invoice was paid %s" % self.duedate.strftime("%B %d, %Y"))
			else:
				self.canvas.drawCentredString(10.5*cm,17.3*cm-h, "This invoice is due: %s" % self.duedate.strftime("%B %d, %Y"))

			t = self.canvas.beginText()
			t.setTextOrigin(2*cm, 5*cm)
			t.setFont("Times-Italic", 10)
			t.textLine("PostgreSQL Europe is a French non-profit under the French 1901 Law. The association is not VAT registered.")
			t.textLine("")

			if islastpage and self.bankinfo:
				t.setFont("Times-Bold", 10)
				t.textLine("Bank references / Références bancaires / Bankverbindungen / Referencias bancarias")

				t.setFont("Times-Roman", 8)
				self.textlines(t, """CCM PARIS 1-2 LOUVRE MONTORGUEIL
28 RUE ETIENNE MARCEL
75002 PARIS
FRANCE
IBAN: FR76 1027 8060 3100 0205 2290 114
BIC: CMCIFR2A
""")

			self.canvas.drawText(t)

			# Finish this page off, and optionally loop to another one
			self.canvas.showPage()

		# Last page is finished, flush the PDF output
		self.canvas.save()

		return self.pdfdata

class PDFRefund(PDFBase):
	def __init__(self, recipient, invoicedate, refunddate, invoicenum, invoiceamount, refundamount, imagedir, currency):
		self.recipient = recipient
		self.invoicedate = invoicedate
		self.refunddate = refunddate
		self.invoiceamount = invoiceamount
		self.refundamount = refundamount

		super(PDFRefund, self).__init__(recipient, invoicenum, imagedir, currency)

	def save(self):
		self._pageheader()

		self.canvas.drawCentredString(10.5*cm,19*cm, "REFUND NOTE FOR INVOICE NUMBER {0}".format(self.invoicenum))

		tbldata = [
			["Item", "Amount"],
			["Invoice total amount", "{0:.2f} {1}".format(self.invoiceamount, self.currency)],
			["Refunded amount", "-{0:.2f} {1}".format(self.refundamount, self.currency)],
			["", "{0:.2f} {1}".format(self.invoiceamount-self.refundamount, self.currency)],
		]
		style = [
			('BACKGROUND',(0,0),(1,0),colors.lightgrey),
			('ALIGN',(1,0),(1,-1),'RIGHT'),
			('LINEBELOW',(0,0),(-1,0), 2, colors.black),
			('OUTLINE', (0,0), (-1, -1), 1, colors.black),
			('LINEABOVE', (-1,-1), (-1,-1), 2, colors.black),
		]

		t = Table(tbldata, [10.5*cm, 2.5*cm, 1.5*cm, 2.5*cm])
		t.setStyle(TableStyle(style))
		w,h = t.wrapOn(self.canvas,10*cm,10*cm)
		t.drawOn(self.canvas, 2*cm, 18*cm-h)

		self.canvas.drawCentredString(10.5*cm, 17.3*cm-h, "This refund was issued {0}".format(self.refunddate.strftime("%B %d, %Y")))

		t = self.canvas.beginText()
		t.setTextOrigin(2*cm, 5*cm)
		t.setFont("Times-Italic", 10)
		t.textLine("PostgreSQL Europe is a French non-profit under the French 1901 Law. The association is not VAT registered.")
		t.textLine("")

		self.canvas.showPage()
		self.canvas.save()

		return self.pdfdata


import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# 创建两个变量占位符
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

# 创建一个乘法操作，把input1和input2相乘
new_value = tf.multiply(input1, input2)

#使用这种写法，在运行完毕后，会话自动关闭
with tf.Session() as sess:

    # 打印new_value,在运算时，用feed设置两个输入的值
    print(sess.run(new_value, feed_dict={input1:23.0, input2:11.0}))


"""Compare two version numbers version1 and version2.
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.

The . character does not represent a decimal point and is used to separate number sequences.

For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth
second-level revision of the second first-level revision.

You may assume the default revision number for each level of a version number to be 0. For
example, version number 3.4 has a revision number of 3 and 4 for its first and second level
revision number. Its third and fourth level revision number are both 0.

Example 1:
Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:
Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:
Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Example 4:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”

Example 5:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its
third level revision number is default to "0"

Note:
Version strings are composed of numeric strings separated by dots . and this numeric strings may
have leading zeroes.

Version strings do not start or end with dots, and they will not be two consecutive dots.
"""

class Solution:
    from itertools import zip_longest as zpl

    def compareVersion(self, version1: 'str', version2: 'str') -> 'int':
        version1_nums = version1.split('.')
        version1_nums = [int(x) for x in version1_nums]
        version2_nums = version2.split('.')
        version2_nums = [int(x) for x in version2_nums]

        for v1, v2 in self.zpl(version1_nums, version2_nums, fillvalue=0):
            if v1 > v2:
                return 1
            if v1 < v2:
                return -1
        return 0

inps = [
    ('0.1', '1.1', -1),
    ('1.0.1', '1', 1),
    ('7.5.2.4', '7.5.3', -1),
    ('1.01', '1.001', 0),
    ('1.0', '1.0.0', 0),
    ('0', '00', 0),
    ('01', '0001', 0),
    ('0.1', '0.1', 0),
    ('1.2.3.4.5', '1.2.3.4.5', 0),
    ('1.2.3.4.6', '1.2.3.4.5', 1),
    ('1.2.3.4.5', '1.2.3.4.6', -1),
    ('1.2.3.4.5', '2.2.3.4.5', -1),
]

sol = Solution()
for v1, v2, exp in inps:
    print(f"Comparing {v1} and {v2} and asserting...")
    assert exp == sol.compareVersion(v1, v2)


import logging

from django.conf import settings
from backend.libs.client import Client
from backend.libs.client import ClientError

DOMAIN_NAME = 'mg.taman-siswa.com'
MAILGUN_API_URL = f'https://api.mailgun.net/v3/{DOMAIN_NAME}'

logger = logging.getLogger('MAILGUN')

class MailgunClient(Client):

    def __init__(self, *args, **kwargs):
        return super().__init__(MAILGUN_API_URL, auth=('api', settings.MAILGUN_API_KEY), *args, **kwargs)

    def get_headers(self, original_headers={}):
        return original_headers

class MailgunManager:
    client = MailgunClient()

    def send_message(self,
                      sender_name,
                      sender_address,
                      recipient_address,
                      subject,
                      content,
                      bcc=[],
                      cc=[],
                      attachments=[],
                      track_open=False,
                      extra_variables={},
                      tag=None,):

        files = []
        for filename, file_handle in attachments:
            files.append(("attachment", (filename, file_handle.read())))

        sender = "%s <%s@%s>" % (sender_name, sender_address, DOMAIN_NAME)

        parameters = {
            "sender": sender,
            "from": sender,
            "to": recipient_address,
            "subject": subject,
            "html": content,
            "o:tracking": "yes" if track_open else "no",
            "o:tracking-clicks": "no",
        }

        for key in extra_variables.keys():
            parameters["v:%s" % key] = extra_variables[key]

        if tag:
            parameters["o:tag"] = tag

        try:
            response = self.client.post(
                '/messages', data=parameters, files=files)
            logger.info(
                msg=f'Mailgun Success: {sender} -> {recipient_address}')
            return response
        except ClientError as ce:
            logger.error(
                msg=f'Mailgun Error: {sender} -> {recipient_address} {str(ce)}')
            raise ce
        except Exception as e:
            logger.error(msg=f'Exception Error: {str(ce)}')
            raise e


import logging
from functools import lru_cache
from typing import Iterator, List, Optional, Tuple

from numpy.random import Generator

from .models import DefaultTemplate, DocumentPlanNode, Message, Template
from .pipeline import NLGPipelineComponent
from .registry import Registry

log = logging.getLogger("root")

# If we're starting a new paragraph and haven't mentioned the location for more than this number of
# messages, say it again (if possible), even if it's not changed
LOC_IF_NOT_SINCE = 6

class TemplateSelector(NLGPipelineComponent):
    """
    Adds a matching Template to each Message in the DocumentPlan.

    """

    def run(
        self,
        registry: Registry,
        random: Generator,
        language: str,
        document_plan: DocumentPlanNode,
        all_messages: List[Message],
    ) -> Tuple[DocumentPlanNode]:
        """
        Run this pipeline component.
        """
        if log.isEnabledFor(logging.DEBUG):
            document_plan.print_tree()

        templates = registry.get("templates")[language.lower()]

        template_checker = TemplateMessageChecker(templates, all_messages)
        log.info("Selecting templates from {} templates".format(len(templates)))
        self._recurse(random, language, document_plan, all_messages, template_checker, None)

        return (document_plan,)

    def _recurse(
        self,
        random: Generator,
        language: str,
        this: DocumentPlanNode,
        all_messages: List[Message],
        template_checker: "TemplateMessageChecker",
        context: Optional[Message],
    ) -> Optional[Message]:
        """
        Recursively works through the tree, adding Templates to Messages.
        """
        # Check all children of this root
        for child in this.children:
            if isinstance(child, Message):
                templates = list(template_checker.all_templates_for_message(child))
                if len(templates) == 0:
                    # If there are no templates, something's gone horribly wrong
                    # The document planner should have made sure this didn't happen, but the only thing we can
                    # at this point is skip the fact
                    log.error("Found no templates to express {}".format(child))
                    raise Exception("No template for message {}".format(child))
                else:
                    random.shuffle(templates)
                    template = templates[0]
                    self._add_template_to_message(child, template, all_messages)
                    context = child
            else:
                # This child is NOT a message and we should just recurse
                context = self._recurse(random, language, child, all_messages, template_checker, context)
        return context

    @staticmethod
    def _add_template_to_message(message: Message, template_original: Template, all_messages: List[Message]) -> None:
        """
        Adds a matching template to a message, also adding the facts used by the template to the message.

        :param message: The message to be fitted with a template
        :param template_original: The template to be added to the message.
        :param all_messages: Other available messages, some of which will be needed to match possible secondary rules
               in the template.
        :return: Nothing
        """
        template = template_original.copy()
        used_facts = template.fill(message, all_messages)
        if used_facts:
            log.debug("Successfully linked template to message")
        else:
            log.error(
                "Chosen template '{}' for fact '{}' could not be used! "
                "Falling back to default templates".format(template.display_template(), message.main_fact)
            )
            template = DefaultTemplate("")
        message.template = template
        message.facts = used_facts

class TemplateMessageChecker(object):
    """
    Doesn't actually fill in templates, but just checks, for a given message (and a list of other available messages),
    whether there is a template that can be used to realise it.

    Init with templates taken from the registry for the relevant language.

    The checks are cached on message and location_required.

    """

    def __init__(self, templates: List[Template], all_messages: List[Message]) -> None:
        self.all_messages = all_messages
        self.templates = templates
        self._cache = {}

    @lru_cache(maxsize=1024)
    def exists_template_for_message(self, message: Message) -> bool:
        """
        Check for templates that apply to the given message. To make things faster, we don't try to find
        all available templates, but return as soon as we find one.
        """
        try:
            # Try getting the first template
            next(self.all_templates_for_message(message))
        except StopIteration:
            # No template found at all
            return False
        return True

    def all_templates_for_message(self, message: Message) -> Iterator[Template]:
        for template in self.templates:
            # See if the template can express this message (with the help of the other available messages)
            if template.check(message, self.all_messages):
                # Got a matching template: this message can be expressed
                yield template


"""Script evaluates multimodal disambiguation using GT labels.

Expected JSON format:

[
    "dialog_id": <dialog_id>,
    "predictions": [
        {
            "turn_id": <turn_id>,
            "disambiguation_label": <bool>,
        }
        ...
    ]
    ...
]

Author(s): Satwik Kottur
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import argparse
import json

import numpy as np

def evaluate_disambiguation(gt_labels, model_results):
    """Evaluates disambiguation using golden labels and model predictions.

    Args:
        gt_labels: Ground truth labels.
        model_results: Generated labels.
    """
    gt_label_pool = {ii["dialogue_idx"]: ii for ii in gt_labels["dialogue_data"]}

    predictions = []
    num_evaluations = 0
    for model_datum in model_results:
        dialog_id = model_datum["dialog_id"]
        for round_datum in model_datum["predictions"]:
            round_id = round_datum["turn_id"]
            predicted_label = round_datum["disambiguation_label"]
            gt_datum = gt_label_pool[dialog_id]["dialogue"][round_id]

            if not "disambiguation_label" in gt_datum:
                continue
            # assert "disambiguation_label" in gt_datum, "Turn not to be evaluated!"
            gt_label = gt_datum["disambiguation_label"]
            predictions.append(gt_label == predicted_label)

    print(f"# Instances evaluated: {len(predictions)}")
    return np.mean(predictions), np.std(predictions) / np.sqrt(len(predictions))

def main(args):
    print("Reading: {}".format(args["data_json_path"]))
    with open(args["data_json_path"], "r") as file_id:
        gt_labels = json.load(file_id)
    print("Reading: {}".format(args["model_result_path"]))
    with open(args["model_result_path"], "r") as file_id:
        model_results = json.load(file_id)

    accuracy, accuracy_std_err = evaluate_disambiguation(gt_labels, model_results)
    print(f"Disambiguation Accuracy: {accuracy:.3f} +- {accuracy_std_err:.3f}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Disambiguation Evaluation")
    parser.add_argument(
        "--data_json_path",
        default="data/simmc2_disambiguate_dstc10_devtest.json",
        help="Data with gold label for disambiguation",
    )
    parser.add_argument(
        "--model_result_path",
        default=None,
        help="Disambiguation labels generated by the model",
    )
    try:
        parsed_args = vars(parser.parse_args())
    except (IOError) as msg:
        parser.error(str(msg))
    main(parsed_args)


from bs4 import BeautifulSoup
import urllib2
import time
import json

filehandle = urllib2.urlopen('http://www3.tesouro.gov.br/tesouro_direto/consulta_titulos/consultatitulos.asp')
soup = BeautifulSoup(filehandle.read())

quotes = {}

currentTime = time.strftime("%Y-%m-%d")

for tr in soup.find_all('tr') :
	columns = tr.find_all('td')
	if len(columns) > 2:
		if 'class' in columns[0].attrs and columns[0]['class'][0] == 'listing0' :
			tituloName = columns[0].contents[0]
			tituloPrice = columns[5].contents[0].replace('R$ ', '').replace('.', '').replace(',', '.')
			quotes[tituloName] = tituloPrice

quotesJson = json.dumps(quotes)
print(quotesJson)


import argparse
import socket
import struct
import threading
import sys
import logging
import time
import urllib.request, urllib.error, urllib.parse
ENCODING = 'utf-8'

def get_args(argv=None):
    #parse arguments and send to main function

    parser = argparse.ArgumentParser(description="ANONSERVER")
    parser.add_argument('-p', type=int, required=True, help='Port')
    parser.add_argument('-l', type=str, required=True, help='logFile')
    parser.add_argument('-w', type=str, required=True, help='website')
    args = parser.parse_args()
    log_file = args.l
    server_port = args.p
    website = args.w
    return server_port, log_file, website

def handshake(sock):
    #perform handshake with client

    #receive first packet
    data, addr = sock.recvfrom(1024)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: N SYN: Y FIN: N")

    #send packet
    sendData = createPacket(sequence_number = 100, ack_number = recvData[0] + 1, ack = 'Y', syn = 'Y', fin = 'N', payload = b"")
    logging.info(f"SEND: Sequence: {100} Acknowlegment: {recvData[0] + 1} Flags: ACK: Y SYN: Y FIN: N")
    send_packet(sock, sendData, addr)

    #recieve second packet, handshake complete
    data, addr = sock.recvfrom(1024)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: Y SYN: N FIN: N")
    print("Handshake Complete")
    logging.info(f"HANDSHAKE COMPLETE")

    return recvData[0], recvData[1], recvData[2], recvPayload, addr

def get_bit(num, i):
    #returns true if bit is 1, false if bit is 0

    return (num & (1 << i)) != 0

def print_bits(num):
    #prints number in 32 bit binary form

    print("{0:032b}".format(num))

def clear_bit(num, i):
    #sets selected bit to 0

    mask = ~(1 << i)
    return (num & mask)

def update_bit(num, i, bit):
    #updates selected bit based on user choice

    mask = ~(1 << i)
    return (num & mask) | (bit << i)

def createPacket(**kwargs):
    #creates packet to send to client

    num = 0
    s_n = kwargs['sequence_number']
    a_n = kwargs['ack_number']
    payload = kwargs['payload']
    if(kwargs['ack'] == 'Y'):
        num = update_bit(num, 2, 1)
    if(kwargs['syn'] == 'Y'):
        num = update_bit(num, 1, 1)
    if(kwargs['fin'] == 'Y'):
        num = update_bit(num, 0, 1)
    data = struct.pack('!I', s_n)
    data += struct.pack('!I', a_n)
    data += struct.pack('!I', num)
    data += struct.pack('!I', len(payload)) + payload

    return data

def send_packet(sock, data, server_addr):
    #sends packet to client

    sock.sendto(data, server_addr)

def downloadWebPage(webpage):
    #read contents of webpage and store in variable

    response = urllib.request.urlopen(webpage)
    webContent = response.read()

    return webContent

def sendFile(sock, s_n, a_n, flags, webContent, addr):
    #send file to client one piece at a time

    #open file we created in main function and send to client
    f = open('savedPage.html', 'rb')
    data = f.read(512)

    while(data):

        #send piece
        send_data = createPacket(sequence_number = a_n, ack_number = s_n + 1, ack = 'Y', syn = 'N', fin = "N", payload = data)
        if(sock.sendto(send_data, addr)):
            logging.info(f"SEND: Sequence: {a_n} Acknowlegment: {s_n + 1} Flags: ACK: Y SYN: N FIN: N")

            #receive acknowlegement that previous piece was received
            data, addr = sock.recvfrom(512)
            recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
            s_n = recvData[0]
            a_n = recvData[1]
            flags = recvData[2]
            logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: Y SYN: N FIN: N")
            data = f.read(512)

    print("Data Sent")
    #send fin packet to client
    send_data = createPacket(sequence_number = a_n, ack_number = s_n + 1, ack = 'N', syn = 'N', fin = 'Y', payload = b'')
    logging.info(f"SEND: Sequence: {a_n} Acknowlegment: {s_n + 1} Flags: ACK: N SYN: N FIN: Y")
    sock.sendto(send_data, addr)

    #receive fin packet from client
    data, addr = sock.recvfrom(512)
    recvData, recvPayload = struct.unpack('!IIII', data[:16]), data[16:]
    logging.info(f"RECV: Sequence: {recvData[0]} Acknowlegment: {recvData[1]} Flags: ACK: N SYN: N FIN: Y")
    flags = recvData[2]

    if(get_bit(flags, 0)):
        f.close()

if __name__=='__main__':
    #parse arguments, store into variables
    port, log_location, website = get_args(sys.argv[1:])
    print("Port: {}, Log location: {}, Website: {}".format(port, log_location, website))

    #logging configuration
    logging.basicConfig(filename=log_location, filemode='w', format='%(asctime)s - %(levelname)s - %(funcName)s - %(message)s', level=logging.INFO)
    logging.info("Starting ANONSERVER")
    logging.info(f"Server Port = {port}, Logfile = {log_location}, website = {website}")

    #store webpage into a file
    webpage = "http://" + website
    webContent = downloadWebPage(webpage)
    f = open('savedPage.html', 'wb')
    f.write(webContent)
    f.close()

    #grab host IP address
    IP = socket.gethostbyname(socket.gethostname())
    print(IP)
    SERVER_ADDR = (IP, port)

    #create and bind socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(SERVER_ADDR)

    while True:

        #perform handshake
        s_n, a_n, flags, payload, addr = handshake(sock)

        #Send the file to the client
        sendFile(sock, s_n, a_n, flags, webContent, addr)


# 13_assert.py

# 使示例示意assert語句 的用法
def get_age():
	a = int(input("請輸入年齡"))
	# 以下語句會在a不在 [0,140]時觸發AssertionError錯誤
	assert 0 <= a <= 140, '年齡不再合法的範圍內'
	return a

try :
	age = get_age()
except AssertionError as e:
	print("錯誤原因是", e)
	age = 0
print('年齡是：', age)


#concepts : connected nodes in graph, adjencylist , non-connected nodes,dfs

def dfs(node,graph,visited,temp=[]):
    temp.append(node)
    visited[node] = True

    for n in graph[node]:
        if visited[n] == False:
            t = dfs(n,graph,visited,temp)

    return temp

def maxfund(n,pairs,fund):
    graph = {}
    for i in range(1,n+1):
        graph[i] = []
    for u,v in pairs:
        graph[u].append(v)
        graph[v].append(u)
    print(graph)
    connected = []
    visited = {}
    for node in graph:
        visited[node] = False
    print(visited)
    for node in graph:
        if visited[node] == False:
            temp = []
            node_grp = dfs(node,graph,visited,temp)
            #print(node_grp)
            connected.append(node_grp)

    print(connected)
    max_fund = []
    for grp in connected:
        summ = 0
        for p in grp:
            summ = summ + fund[p-1]
        max_fund.append(summ)

    print(max_fund)

    return max(max_fund)

if __name__ == '__main__':
    n = int(input())
    fund = list(map(int,input().split()))
    p = int(input())
    pairs = []
    for i in range(p):
        pair = tuple(map(int,input().split()))
        pairs.append(pair)

    result = maxfund(n,pairs,fund)
    print(result)


from datetime import date, timedelta

class ParameterService:

    def __init__(self,default_date_gap=10):
        self.default_date_gap = default_date_gap
        self.default_ticker = ""

    def init_params(self,date_gap=None):
        if date_gap is None:
                date_gap = self.default_date_gap
        return self.default_ticker , date.today() - timedelta(days=date_gap), date.today()

    def process_params(self,request):
        from_date = request.form.get('from_date')
        till_date = request.form.get('till_date')
        tickers = request.form.get('tickers')
        if tickers is not None:
            for separator in ":,+":
                if tickers.find(separator) >= 0:
                    tickers = tickers.split(separator)
                    break
            else:
                tickers = [tickers]

        return tickers, from_date, till_date

    def get_param(self,request,param):
        return request.form.get(param)

    def init_option_controller_params(self):
        underlyingPrice = 14.00
        days = 21
        volatility = 76.5
        interest = 1.5
        dividend = 0.0
        return underlyingPrice,days,volatility,interest,dividend

    def process_options_params(self,request):
        underlyingPrice = float(request.form.get('underlyingPrice'))
        days = int(request.form.get('days'))
        volatility = float(request.form.get('volatility'))
        interest = float(request.form.get('interest'))
        dividend = float(request.form.get('dividend'))
        return underlyingPrice,  days, volatility, interest, dividend

    def process_options_params_ajax(self,request):
        print(request.args)
        optionSide = request.args.get('optionSide')
        underlyingPrice = float(request.args.get('underlyingPrice'))
        exercisePrice = float(request.args.get('exercisePrice'))
        days = int(request.args.get('days'))
        targetPrice = float(request.args.get('targetPrice'))
        interest = float(request.args.get('interest'))
        dividend = float(request.args.get('dividend'))
        return optionSide,underlyingPrice, exercisePrice, days, targetPrice, interest, dividend


"""Initializer helpers for HomematicIP fake server."""
from asynctest import MagicMock, Mock, patch
from homematicip.aio.auth import AsyncAuth
from homematicip.aio.connection import AsyncConnection
from homematicip.aio.home import AsyncHome
import pytest

from homeassistant import config_entries
from homeassistant.components.homematicip_cloud import (
    DOMAIN as HMIPC_DOMAIN,
    async_setup as hmip_async_setup,
    const as hmipc,
    hap as hmip_hap,
)
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from .helper import AUTH_TOKEN, HAPID, HAPPIN, HomeTemplate

from tests.common import MockConfigEntry, mock_coro

@pytest.fixture(name="mock_connection")
def mock_connection_fixture() -> AsyncConnection:
    """Return a mocked connection."""
    connection = MagicMock(spec=AsyncConnection)

    def _rest_call_side_effect(path, body=None):
        return path, body

    connection._restCall.side_effect = (  # pylint: disable=protected-access
        _rest_call_side_effect
    )
    connection.api_call.return_value = mock_coro(True)
    connection.init.side_effect = mock_coro(True)

    return connection

@pytest.fixture(name="hmip_config_entry")
def hmip_config_entry_fixture() -> config_entries.ConfigEntry:
    """Create a mock config entriy for homematic ip cloud."""
    entry_data = {
        hmipc.HMIPC_HAPID: HAPID,
        hmipc.HMIPC_AUTHTOKEN: AUTH_TOKEN,
        hmipc.HMIPC_NAME: "",
        hmipc.HMIPC_PIN: HAPPIN,
    }
    config_entry = MockConfigEntry(
        version=1,
        domain=HMIPC_DOMAIN,
        title=HAPID,
        data=entry_data,
        source="import",
        connection_class=config_entries.CONN_CLASS_CLOUD_PUSH,
        system_options={"disable_new_entities": False},
    )

    return config_entry

@pytest.fixture(name="default_mock_home")
def default_mock_home_fixture(mock_connection) -> AsyncHome:
    """Create a fake homematic async home."""
    return HomeTemplate(connection=mock_connection).init_home().get_async_home_mock()

@pytest.fixture(name="default_mock_hap")
async def default_mock_hap_fixture(
    hass: HomeAssistantType, mock_connection, hmip_config_entry
) -> hmip_hap.HomematicipHAP:
    """Create a mocked homematic access point."""
    return await get_mock_hap(hass, mock_connection, hmip_config_entry)

async def get_mock_hap(
    hass: HomeAssistantType,
    mock_connection,
    hmip_config_entry: config_entries.ConfigEntry,
) -> hmip_hap.HomematicipHAP:
    """Create a mocked homematic access point."""
    hass.config.components.add(HMIPC_DOMAIN)
    hap = hmip_hap.HomematicipHAP(hass, hmip_config_entry)
    home_name = hmip_config_entry.data["name"]
    mock_home = (
        HomeTemplate(connection=mock_connection, home_name=home_name)
        .init_home()
        .get_async_home_mock()
    )
    with patch.object(hap, "get_hap", return_value=mock_coro(mock_home)):
        assert await hap.async_setup()
    mock_home.on_update(hap.async_update)
    mock_home.on_create(hap.async_create_entity)

    hass.data[HMIPC_DOMAIN] = {HAPID: hap}

    await hass.async_block_till_done()

    return hap

@pytest.fixture(name="hmip_config")
def hmip_config_fixture() -> ConfigType:
    """Create a config for homematic ip cloud."""

    entry_data = {
        hmipc.HMIPC_HAPID: HAPID,
        hmipc.HMIPC_AUTHTOKEN: AUTH_TOKEN,
        hmipc.HMIPC_NAME: "",
        hmipc.HMIPC_PIN: HAPPIN,
    }

    return {HMIPC_DOMAIN: [entry_data]}

@pytest.fixture(name="dummy_config")
def dummy_config_fixture() -> ConfigType:
    """Create a dummy config."""
    return {"blabla": None}

@pytest.fixture(name="mock_hap_with_service")
async def mock_hap_with_service_fixture(
    hass: HomeAssistantType, default_mock_hap, dummy_config
) -> hmip_hap.HomematicipHAP:
    """Create a fake homematic access point with hass services."""
    await hmip_async_setup(hass, dummy_config)
    await hass.async_block_till_done()
    hass.data[HMIPC_DOMAIN] = {HAPID: default_mock_hap}
    return default_mock_hap

@pytest.fixture(name="simple_mock_home")
def simple_mock_home_fixture() -> AsyncHome:
    """Return a simple AsyncHome Mock."""
    return Mock(
        spec=AsyncHome,
        devices=[],
        groups=[],
        location=Mock(),
        weather=Mock(create=True),
        id=42,
        dutyCycle=88,
        connected=True,
    )

@pytest.fixture(name="simple_mock_auth")
def simple_mock_auth_fixture() -> AsyncAuth:
    """Return a simple AsyncAuth Mock."""
    return Mock(spec=AsyncAuth, pin=HAPPIN, create=True)


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
import torchvision.models as models
from torch.autograd.gradcheck import zero_gradients
from PIL import Image
import sys
import os

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img_num = 200

def read_origin_data(input_dir):
    print('start reading data...')
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize(mean, std)])
    img_list = []
    for i in range(img_num):
        img = Image.open(os.path.join(input_dir, (3 - len(str(i))) * '0' + str(i) + '.png'))
        img = transform(img).numpy()
        img_list.append(img)
        print('img', i, 'done', end = '\r', flush = True)
    img_list = np.array(img_list)
    data = torch.FloatTensor(img_list)
    loader = DataLoader(data, batch_size = 1, shuffle = False, num_workers = 2)
    print('data read...')
    return loader

def fgsm_attack(image, epsilon, data_grad):
    sign_data_grad = data_grad.sign()
    perturbed_image = image + epsilon  *sign_data_grad
    perturbed_image = torch.clamp(perturbed_image, 0, 1)
    return perturbed_image

def plot_attack(x, num, output_dir):
#     x = np.moveaxis(x, 0, -1)
#     print(x.shape)
    x.save(os.path.join(output_dir, (3 - len(str(num))) * '0' + str(num) + '.png'))
#     new_x = np.clip((x + 1) * 255 / 2, 0, 255)
#     result = Image.fromarray(new_x.astype(np.uint8))
#     result.save('/content/ans_'+ (3 - len(str(num))) * '0' + str(num) + '.png')
def inverse_transform(t):
    return t.mul(torch.FloatTensor(std).view(3, 1, 1)).add(torch.FloatTensor(mean).view(3, 1, 1))

if __name__ == "__main__":
    model = models.resnet50(pretrained=True)
    model.eval()
    img_num = 200
    epsilon = 0.08
    data_loader = read_origin_data(sys.argv[1])
    criterion = nn.CrossEntropyLoss()
    x_adv = []
    L_inf_sum = 0.
    cnt = 0
    acc_num = 0
    for data in data_loader:
        data.requires_grad = True
        zero_gradients(data)
        output = model(data)
        init_pred = output.max(1, keepdim=True)[1]
        loss = criterion(output, init_pred[0])
        loss.backward()
        perturbed_data = data + epsilon * data.grad.sign_()
        output = model(perturbed_data)
        final_pred = output.max(1, keepdim=True)[1]
        perturbed_data = inverse_transform(perturbed_data)
        origin_data = inverse_transform(data)
        adv = perturbed_data.squeeze().detach().numpy()
        origin = origin_data.squeeze().detach().numpy()
        L_inf = np.max(adv - origin)
        L_inf = abs(L_inf) * 255
        L_inf_sum += L_inf
        new_img = torch.clamp(perturbed_data.squeeze(), 0, 1)
        img_trans = transforms.Compose([transforms.ToPILImage()])
        new_img = img_trans(new_img)
        plot_attack(new_img, cnt, sys.argv[2])
        print('\nimg num   :', cnt)
        print('previous  :', init_pred.item())
        print('after     :', final_pred.item())
#         print('real label:', labels[cnt].item())
        print('L infinity:', L_inf)
        if init_pred.item() != final_pred.item():
            acc_num += 1
        cnt += 1
        if cnt == 200:
            break
    print('====================================================================')
    print('acc rate  :', acc_num / cnt)
    print('L infinity:', L_inf_sum / cnt)


class BreadthFirstSearchTree(object):
    def __init__(self):
        self.nodes = {}

    def add_node(self, new_node):
        print("\nAttempting to Add node:")
        print(new_node)

        for layer_key in self.nodes.keys():
            for node_index, node in enumerate(self.nodes[layer_key]):
                if node.vertex.vertex_id == new_node.vertex.vertex_id:
                    print("new_node found within dictionary of nodes.")
                    print("previous node: ")
                    print(node)
                    # vertex has exists within searchtree already, see if new_node
                    # has a shorter route.  If so replace.  If not, ignore node.
                    if new_node.minimum_route < node.minimum_route:
                        print("new_node < node. Adding")
                        self.nodes[layer_key][node_index] = deepcopy(new_node)
                        return True
                    else:
                        print("new_node > node.  Ignoring")
                        return False

        print("new_node not found within dictionary of nodes.")
        # If node does not exist, it is minimum route node by default.
        # If new_node's layer has been created
        if new_node.layer in self.nodes.keys():
            print("new_node's layer is not new.  Appending.")
            # Append the new_node to the dictionary's list at layer L
            self.nodes[str(new_node.layer)].append(new_node)
        else:
            print("new_node's layer is new.  Starting new list.")
            # else, create a new list for layer L's nodes
            self.nodes[str(new_node.layer)] = [new_node]

        return True

    def node_in_tree(self, node):
        for layer_key in self.nodes.keys():
            if node in self.nodes[layer_key]:
                return True
        return False

    def vertex_in_tree(self, vertex):
        for layer_key in self.nodes.keys():
            for node in self.nodes[layer_key]:
                if node.vertex.vertex_id == vertex.vertex_id:
                    return True
        return False

    def display(self):
        for layer_key in self.nodes.keys():
            print()
            print("===  Layer: ", layer_key, "===")
            for node in self.nodes[layer_key]:
                print(node)
                print()

    def plot(self):
        x = list([])
        y = list([])
        plots = list([])

        # Iterate over vertices, retrieving x and y coordinates
        for layer_key in self.nodes.keys():
            for node in self.nodes[layer_key]:
                x.append(node.vertex.x)
                y.append(node.vertex.y)

        # Show the graph with a legend
        plt.legend(loc=2, fontsize='small')
        plt.show()

    class Node(object):
        def __init__(self, node_id, vertex, layer, minimum_route):
            self.node_id = node_id
            self.vertex = vertex
            self.layer = layer
            self.adjacent_nodes = list([])
            self.minimum_route = deepcopy(minimum_route)

        def __str__(self):
            string = "node_id: " + str(self.node_id) + "\nvertex: " + str(self.vertex) + "\nlayer: " + self.layer + "\nadjacent_nodes [vertex_ids]: "

            string += "["

            for node in self.adjacent_nodes:
                string += str(node.vertex.vertex_id) + ","

            string += "]"

            string += "\nminimum_route:" + str(self.minimum_route)

            return string

        def __eq__(self, other):
            return self.vertex.vertex_id == other.vertex.vertex_id

class DepthFirstSearchStack(object):
    def __init__(self):
        self.node_stack = list([])
        self.finished_nodes = list([])
        self.finished_vertices = list([])

    def __str__(self):
        string = "==== Depth First Search Stack ===="
        string += "\nNode Stack: \n"
        for node in self.node_stack:
            string += str(node) + "\n"
        string += "\nFinished Nodes: \n"
        for node in self.finished_nodes:
            string += str(node) + "\n"
        string += "\nFinished Vertices: \n"
        for vertex in self.finished_vertices:
            string += str(vertex) + "\n"
        return string

    def get_unfinished_adjacent_vertices(self, adjacent_vertices):
        return [vertex for vertex in adjacent_vertices if vertex not in self.finished_vertices]

    def push(self, vertex, current_route):
        matching_vertices = [node.vertex for node in self.node_stack if node.vertex == vertex]

        if vertex not in matching_vertices:
            node = DepthFirstSearchStack.Node(vertex.vertex_id, vertex, current_route)
            self.node_stack.append(node)

    def get_path_to_finished_vertex_id(self, vertex_id):
        return [node.minimum_route for node in self.finished_nodes if node.vertex.vertex_id == vertex_id][0]

    def pop(self):
        top = self.node_stack[-1]
        self.node_stack.remove(top)
        return top

    def node_complete(self, node):
        print("Node marked as complete:" + str(node))

        self.finished_nodes.append(node)
        self.finished_vertices.append(node.vertex)
        node.finished = True

    class Node(object):
        def __init__(self, node_id, vertex, minimum_route):
            self.node_id = node_id
            self.vertex = vertex
            self.minimum_route = deepcopy(minimum_route)
            self.finished = False

        def __str__(self):
            return "node_id: " + str(self.node_id) + " finished: " + str(self.finished) + "\nvertex: " + str(self.vertex) + "\nminimum_route: " + str(self.minimum_route)


import pandas as pd
import numpy as np
import datetime as dt

def calculate_ema(values: [dt.date, np.float]):
	"""This returns the value of EMA indicator for the last date provided.
	    Returns the date for which the indicator was calculated and it's value"""
	n = len(values)
	if n == 0:
		raise Exception("Cannot make average of dis.")
	alpha = 2 / (n + 1)
	factor = 1
	numerator = 0
	denominator = 0
	for i in reversed(range(0, n)):
		numerator += values[i][1] * factor
		denominator += factor
		factor *= (1 - alpha)
	return values[-1][0], numerator / denominator

def calculate_ema_range(date_values: [(dt.date, np.float)], samples_per_result: int):
	"""Returns a list of tuples of EMA indicator value and date."""
	max_index = len(date_values) - samples_per_result
	results = []
	for d in range(max_index):
		date_value = calculate_ema(date_values[0 + d: d + samples_per_result])
		results.append(date_value)
	return results


import io, os
import re
from google.cloud import vision
from google.cloud import storage
from google.protobuf import json_format

def async_detect_document(gcs_source_uri, gcs_destination_uri):

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'ServiceAccountToken.json'
    client = vision.ImageAnnotatorClient()

    batch_size = 100
    mime_type = 'application/pdf'
    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)

    # gcs_source_uri = 'gs://filename'
    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(gcs_source=gcs_source, mime_type=mime_type)

    # gcs_destination_uri = 'gs://fileDestination '
    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(gcs_destination=gcs_destination, batch_size=batch_size)

    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature], input_config=input_config, output_config=output_config)

    operation = client.async_batch_annotate_files(requests=[async_request])
    operation.result(timeout=5000)

    storage_client = storage.Client()
    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)
    bucket = storage_client.get_bucket(bucket_name)

    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files:')
    for blob in blob_list:
        print(blob.name)

    output = blob_list[0]
    json_string = output.download_as_string()
    response = json_format.Parse(
        json_string, vision.types.AnnotateFileResponse())

    response = json_format.Parse(
            json_string, vision.types.AnnotateFileResponse())
    first_page_response = response.responses[0]
    annotation = first_page_response.full_text_annotation

    print(u'Full text:')
    print(annotation.text)

files = 'file_name'
source_bucket = 'gs://cambridgefive_pdfs/'
destinations_bucket = 'gs://cambridgefive_pdfs/'
i = 0
while i < len(files):
#     async_detect_document(files[i], destinations[i])
    async_detect_document(source_bucket + files[i], source_bucket + files[i] + '.txt ')
    print(files[i])
    i+= 1


import networkx as nx
import pandas as pd
import pickle
import numpy as np
from gae.preprocessing import mask_test_edges_directed, mask_test_edges

RANDOM_SEED = 0
NetWorks=['twitter', 'gplus', 'hamster', 'advogato']
#NetWorks=['twitter', 'gplus']
for network in NetWorks:
    network_adj = pickle.load(open('./data/{}/{}-adj.pkl'.format(network, network), 'rb'))

    #FRAC_EDGES_HIDDEN = [0.15, 0.3, 0.45]
    FRAC_EDGES_HIDDEN = [0.15]
    TRAIN_TEST_SPLIT_DIR = './train-test-splits/'

    # Generate 1 train/test split for each frac_edges_hidden setting
    for frac_hidden in FRAC_EDGES_HIDDEN:
        val_frac = 0.05
        test_frac = frac_hidden - val_frac

        # Set random seed
        np.random.seed(RANDOM_SEED)

        # Generate train_test_split:
        # (adj_train, train_edges, train_edges_false, val_edges, val_edges_false, test_edges, test_edges_false)
        if network in ['hamster', 'jazz', 'karate']:
            train_test_split = mask_test_edges(network_adj,
                                               test_frac=test_frac, val_frac=val_frac,
                                               verbose=True)
        else:
            train_test_split = mask_test_edges_directed(network_adj,
                                                        test_frac=test_frac, val_frac=val_frac,
                                                        verbose=True, prevent_disconnect=False,
                                                        false_edge_sampling='random')

        # Save split
        file_name = TRAIN_TEST_SPLIT_DIR + '{}-{}-hidden.pkl'.format(network, frac_hidden)
        with open(file_name, 'wb') as f:
            pickle.dump(train_test_split, f, protocol=2)


# --- Day 3: ####### ---
#
#   Part 1: Count trees hit down path through tiled hillside
#   Part 2: Count trees hit down multiple paths
#

import sys
sys.path.append("..")

from AoCCommon.InputToArray import InputToArray

a = InputToArray()

mydata = a.array

testinput =\
"""
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"""

treerows = testinput.split('\n')
treerows = [x for x in treerows if x]

treecoords = []

def part1():

    for i,row in enumerate(mydata):
        for j in range(len(row)):
            if row[j] == "#":
                treecoords.append((i,j))

    mylocation = [0,0]

    print(treecoords)

    width = len(mydata[0])
    hits = 0

    for i in range(len(mydata)):
        mylocation[0] += 1
        mylocation[1] += 3
        if (mylocation[0], mylocation[1]%width) in treecoords:
            hits += 1

    print(hits)

def part2():
    for i, row in enumerate(mydata):
        for j in range(len(row)):
            if row[j] == "#":
                treecoords.append((i, j))

    width = len(mydata[0])
    paths = [[1,1],[1,3],[1,5],[1,7],[2,1]]
    hits = [0 for x in paths]

    for i,path in enumerate(paths):
        mylocation = [0, 0]
        while mylocation[0] < len(mydata):
            mylocation[0] += path[0]
            mylocation[1] += path[1]
            if (mylocation[0], mylocation[1] % width) in treecoords:
                hits[i] += 1

    print(hits[0]*hits[1]*hits[2]*hits[3]*hits[4])

part2()


import argparse

from calculadora.calculator import Calculator

if __name__ == '__main__':
    parse = argparse.ArgumentParser()
    parse.add_argument('operation', type=str, help='mathematical operation you want to do')
    parse.add_argument('num1', type=int, help='first parameter to use in your operation')
    parse.add_argument('num2', type=int, help='second parameter to use in your operation')

    arguments = parse.parse_args()

    calculator = Calculator()

    expression = calculator.get_expression(arguments.num1, arguments.num2, arguments.operation)
    result = calculator.calculate(arguments.num1, arguments.num2, arguments.operation)

    print(f'The result  is: {expression} = {str(result)}')


import random
import time

hidden_number = random.randint(0, 10)
start = time.time()

print('The computer randomly chose a number from 0 to 10.')
print('Try to guess it as fast a possible!')
print(hidden_number)
guess = int(input('>>> '))
while guess != hidden_number:
   print('Try again!')
   guess = (input('>>> '))

total_time = time.time() - start

print('Well, it took you {:.2f}s'.format(total_time))


from django.core.urlresolvers import reverse_lazy

from siscoban.core.mixins.views import GenericListView, GenericCreateView, GenericUpdateView, GenericDeleteView
from ..models.pay_scale import PayScale
from ..forms.pay_scale import PayScaleForm

class PayScaleListView(GenericListView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_list.html'
    title = 'Nova Tabela de remuneração'
    subtitle = 'Por estados'
    descriptive_title = 'Nova Tabela de remuneração por estado'

    def get_context_data(self):
        context = super(PayScaleListView, self).get_context_data()
        ufs = PayScale.objects.values('uf').all().distinct()
        context['ufs'] = ufs
        context['tables'] = PayScale.objects.all().order_by('-uf')

        try:
            tables = []
            tables_uf = []
            u = None
            for uf in ufs:
                if u is not None and u != uf['uf']:
                    tables.append(tables_uf)
                    tables_uf = []
                u = uf['uf']
                tables_uf += PayScale.objects.filter(uf=u)
                if u != uf['uf']:
                    tables.append(tables_uf)
                    tables_uf = []
            tables.append(tables_uf)
            context['tables'] = tables
        except:
            pass

        return context

class PayScaleCreateView(GenericCreateView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_detail.html'
    form_class = PayScaleForm
    success_url = reverse_lazy('financial:pay_scale_list')
    success_message = "Tabela de remuneração criada com sucesso"
    title = 'Nova Tabela de remuneração'
    subtitle = 'Cadastrar nova tabela de remuneração'
    descriptive_title = 'Cadastro da nova tabela de remuneração'

class PayScaleUpdateView(GenericUpdateView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    template_name = 'pages/pay_scale/pay_scale_detail.html'
    form_class = PayScaleForm
    success_url = reverse_lazy('financial:pay_scale_list')
    success_message = "Tabela de remuneração atualizada com sucesso"
    title = 'Nova Tabela de remuneração'
    subtitle = 'Editar nova tabela de remuneração'
    descriptive_title = 'Edição da nova tabela de remuneração'

class PayScaleDeleteView(GenericDeleteView):
    permission_required = 'global_permissions.diretoria'
    model = PayScale
    success_url = reverse_lazy('financial:pay_scale_list')
    title = 'Nova Tabela de remuneração'
    subtitle = 'Remover nova tabela de remuneração'
    descriptive_title = 'Remoção da nova tabela de remuneração'


import json
import requests
from requests.api import get, head

def get_octopus_resource(uri, headers, skip_count = 0):
    items = []
    skip_querystring = ""

    if '?' in uri:
        skip_querystring = '&skip='
    else:
        skip_querystring = '?skip='

    response = requests.get((uri + skip_querystring + str(skip_count)), headers=headers)
    response.raise_for_status()

    # Get results of API call
    results = json.loads(response.content.decode('utf-8'))

    # Store results
    if 'Items' in results.keys():
        items += results['Items']

        # Check to see if there are more results
        if (len(results['Items']) > 0) and (len(results['Items']) == results['ItemsPerPage']):
            skip_count += results['ItemsPerPage']
            items += get_octopus_resource(uri, headers, skip_count)

    else:
        return results

    # return results
    return items

# Define Octopus server variables
octopus_server_uri = 'https://YourURL/api'
octopus_api_key = 'API-YourAPIKey'
headers = {'X-Octopus-ApiKey': octopus_api_key}
project_name = "MyProject"
environment_name_list = ['Environment', 'List']
tenant_tag = 'TENANT TAG TO FILTER ON'  #Format = [Tenant Tag Set Name]/[Tenant Tag] "Tenant Type/Customer"
max_number_tenants = 1
tenants_updated = 0
space_name = 'Default'
what_if = False

# Get space
uri = '{0}/spaces'.format(octopus_server_uri)
spaces = get_octopus_resource(uri, headers)
space = next((x for x in spaces if x['Name'] == space_name), None)

# Get project
uri = '{0}/{1}/projects'.format(octopus_server_uri, space['Id'])
projects = get_octopus_resource(uri, headers)
project = next((x for x in projects if x['Name'] == project_name), None)

# Get environments
environments = []
uri = '{0}/{1}/environments'.format(octopus_server_uri, space['Id'])
all_environments = get_octopus_resource(uri, headers)
for environment_name in environment_name_list:
    environment = next((x for x in all_environments if x['Name'] == environment_name), None)
    environments.append(environment['Id'])

# Get tenants by tag
uri = '{0}/{1}/tenants?tags={2}'.format(octopus_server_uri, space['Id'], tenant_tag)
tenants = get_octopus_resource(uri, headers)

# Loop through tenants
for tenant in tenants:
    tenant_updated = False

    if tenant['ProjectEnvironments'] == None or len(tenant['ProjectEnvironments']) == 0:
        projectEnvironments = {
            project['Id']: environments
        }

        # Attach to tenant
        tenant['ProjectEnvironments'] = projectEnvironments
        tenant_updated = True
    else:
        # Get current project environments
        projectEnvironments = tenant['ProjectEnvironments']

        # Loop through environments
        for environment in environments:
            #print (projectEnvironments[project['Id']])
            if environment not in projectEnvironments[project['Id']]:
                projectEnvironments[project['Id']].append(environment)

        tenant['ProjectEnvironments'] = projectEnvironments
        tenant_updated = True

    if tenant_updated:
        if what_if:
            print(tenant)
        else:
            uri = '{0}/{1}/tenants/{2}'.format(octopus_server_uri, space['Id'], tenant['Id'])
            response = requests.put(uri, headers=headers, json=tenant)
            response.raise_for_status

        tenants_updated = tenants_updated + 1

    if tenants_updated == max_number_tenants:
        break


from json import loads
from os import getenv
from urllib import request
from datetime import datetime, timedelta
from time import strftime

from discord import Embed
from discord.ext import commands

class Weather(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.api_calls = 0
        self.last_update = datetime.now() - timedelta(minutes=10)

    def generateQuery(self):
        # define query parameters
        api_url = 'https://api.darksky.net/forecast/'
        key = getenv('DARK_SKY_SECRET')
        latitude = '28.4196'
        longitude = '-81.2937'
        exclude = 'minutely,hourly,daily,flags'
        language = 'en'

        # generate query string
        query = f'{api_url}{key}/{latitude},{longitude}?exclude={exclude}&lang={language}'

        return query

    def rateLimit(self):
        delta = datetime.now() - self.last_update
        interval = timedelta(minutes=10)

        if self.api_calls >= 200:
            return False
        if delta < interval:
            return False

        return True

    def getResponse(self, update: bool, query: str):
        response = request.urlopen(query)
        self.api_calls = int(response.getheader('X-Forecast-API-Calls'))
        return response

    def parseResponse(self, response):
        json_data = loads(response.read())

        # f = open('logs.json', 'w')
        # print(json_data, file=f)
        # f.close()

        self.last_update = datetime.fromtimestamp(json_data['currently']['time'])

        properties = ['summary', 'icon', 'apparentTemperature', 'temperature', 'precipProbability']
        data = []

        for x in properties:
            data.append(json_data['currently'][x])

        data.append(self.last_update.strftime(r'%b %d, %I:%M %p'))

        return data

    def colorizer(self, icon: str):
        switch = {
            'clear-day': 0xFFD600,
            'clear-night': 0x90A4AE,
            'rain': 0x2962FF,
            'snow': 0xFFFFFF,
            'sleet': 0xB3E5FC,
            'wind': 0xBDBDBD,
            'fog': 0x9E9E9E,
            'cloudy': 0x757575,
            'partly-cloudy-day': 0xb29400,
            'partly-cloudy-night': 0x647e8b,
            'hail': 0x00B0FF,
            'thunderstorm': 0xFF8F00,
            'tornado': 0x42424
        }

        return switch.get(icon, 0x2ecc71)

    def generateEmbed(self, data):
        summary = data[0]
        color = self.colorizer(data[1])

        weather_embed = Embed(title='Orlando', description=summary, color=color)
        # weather_embed.set_thumbnail(url="...")

        names = ['Feels Like', 'Actual', 'Precipitation Probability']
        fields = list(zip(names, data[2:-1]))

        for n, v in fields:
            weather_embed.add_field(name=n, value=round(v))

        weather_embed.set_footer(text=f"Powered by Dark Sky ◉ Updated: {data[-1]}")

        return weather_embed

    @commands.command()
    async def weather(self, ctx):
        query = self.generateQuery()
        update = self.rateLimit()

        if update:
            response = self.getResponse(update, query)
            self.cache = self.parseResponse(response)

        weather_embed = self.generateEmbed(self.cache)

        await ctx.send(embed=weather_embed)


import random
import time
import os
def jogar():
    clear = lambda: os.system('cls')
    # clear()
    # APRESENTAÇÃO
    bva = "***************************************"
    bvt = "*  BEM VINDO AO JOGO DA ADIVINHAÇÃO!  *"
    lbv = [bva, bvt, bva, "\n\n\n\n"]
    t = 1
    while t <= 3:
        for percorrel in range(0, len(lbv)):
            print(lbv[percorrel])
        t += 1
        time.sleep(1)
    time.sleep(1)
    # VARIÁVEIS PARA DEFINIR DIFICULDADE DO JOGO
    dificuldade = ""
    nivel = 0
    # FERRAMENTAS PARA CRIAÇÃO DO RANKING DO JOGO
    ranking_f = [["Zé", 950], ["Maria", 900], ["Pedro", 850]]
    ranking_m = [["Gabriel", 951], ["Fernanda", 901], ["Bruna", 851]]
    ranking_d = [["Ana", 972], ["Vinícius", 962], ["Rebecca", 952]]
    ranking = []
    la = "\n***************************************"
    lt = "\n---------------------------------------"
    lt2 = "---------------------------------------"
    la1 = "  TOP 3 NÍVEL %s\n  1º: %s - %d PONTOS                   "
    la2 = "  2º: %s - %d PONTOS                   \n  3º: %s - %d PONTOS                   "
    # MENU INICIAL
    m = 0
    clear()
    while m == 0:
        print("***************************************")
        print("*  BEM VINDO AO JOGO DA ADIVINHAÇÃO!  *")
        print("***************************************\n")
        print("||||||||||||||   MENU    ||||||||||||||")
        print("\n  1 - RANKING\n  2 - INICIAR\n  3 - SAIR")
        menu = int(input("\nESCOLHA UMA OPÇÃO: "))
        if menu == 1:
            clear()
            # SISTEMA DE CONSULTA DE RANKING
            print("||||||||||||||  RANKING  ||||||||||||||")
            c = 0
            # ESCOLHA DO NÍVEL A SER APRESENTADO
            while c == 0:
                print("\nEscolha um nível de dificuldade :\n\n(1) FÁCIL  (2) MÉDIO  (3) DIFÍCIL  (4) MENU")
                nivel = int(input("\nNÍVEL: "))
                if nivel == 1:
                    ranking = ranking_f
                    dificuldade = "FÁCIL"
                    clear()
                elif nivel == 2:
                    ranking = ranking_m
                    dificuldade = "MÉDIO"
                    clear()
                elif nivel == 3:
                    ranking = ranking_d
                    dificuldade = "DIFÍCIL"
                    clear()
                elif nivel == 4:
                    clear()
                    c = 1
                    break
                else:
                    # EXCEÇÃO PARA CAPTAR ERROS
                    print("Escolha um número de 1 a 4 !!!".upper())
                # EXIBIÇÃO DO RANKING COM VARIÁVEIS CONTENDO A FORMATAÇÃO
                print("||||||||||||||  RANKING  ||||||||||||||")
                print(la, lt)
                print(la1 % (dificuldade, ranking[0][0], ranking[0][1]))
                print(la2 % (ranking[1][0], ranking[1][1], ranking[2][0], ranking[2][1]))
                print(lt2, la)
        elif menu == 2:
            g = 0
            # VARIÁVEIS PARA DEFINIR DIFICULDADE DO JOGO
            dificuldade = ""
            nivel = 0
            # RANKING
            ranking = []
            while g == 0:
                # LIMPA A TELA
                clear()
                # CRIAÇÃO DO NÚMERO SECRETO COM RANDINT
                numero_secreto = random.randint(1, 100)
                # CAPTAÇÃO NOME E CRIAÇÃO VARIÁVEIS DE TENTATIVAS E PONTUAÇÃO
                nome = input("\nDIGITE SEU NOME: ")
                tentativas = 0
                pontos = 1000
                cont_tentativas = 0
                l_chutes = []
                # ESCOLHA DA DIFICULDADE
                print("\nEscolha um nível de dificuldade %s:" % nome)
                print("(1) FÁCIL  (2) MÉDIO  (3) DIFÍCIL\n(1) 20x -- (2) 10x -- (3) 5x")
                a = 0
                while a == 0:
                    nivel = int(input("NÍVEL: "))
                    if nivel == 1:
                        tentativas = 20
                        a = 1
                        ranking = ranking_f
                        dificuldade = "FÁCIL"
                        x = 21
                    elif nivel == 2:
                        tentativas = 10
                        a = 1
                        ranking = ranking_m
                        dificuldade = "MÉDIO"
                        x = 11
                    elif nivel == 3:
                        tentativas = 5
                        a = 1
                        ranking = ranking_d
                        dificuldade = "DIFÍCIL"
                        x = 6
                    else:
                        # EXCEÇÃO PARA CAPTAR ERROS
                        print("Escolha um número de 1 a 3 !!!".upper())
                # EXIBIÇÃO DOS PONTOS INICIAIS
                clear()
                print("R: ", numero_secreto)
                print("INÍCIO - USE 0 PARA VOLTAR AO MENU PRINCIPAL")
                print("\nPONTOS: ", pontos)
                # FOR PARA OS CHUTES DO USUÁRIO
                for tentativas in range(tentativas, 0, -1):
                    print("\nVocê tem %d tentativas !" % tentativas)
                    chute = int(input("\nDigite o seu número: "))
                    print("\nVocê chutou:", chute)
                    if chute == 0:
                        print("\nSAINDO E VOLTANDO AO MENU PRINCIPAL...")
                        time.sleep(2)
                        g = 1
                        break
                    if chute < 1 or chute > 100:
                        # EXCEÇÃO PARA CAPTAR ERROS
                        print("Você deve escolher um número entre 1 e 100 !")
                        l_chutes.append(chute)
                        continue
                    if numero_secreto == chute:
                        print("\nVocê acertou !")
                        l_chutes.append(chute)
                        cont_tentativas += 1
                        time.sleep(2)
                        clear()
                        break
                    elif chute > numero_secreto:
                        print("\nVocê chutou um número maior que a resposta !")
                        l_chutes.append(chute)
                    else:
                        print("\nVocê chutou um número menor que a resposta !")
                        l_chutes.append(chute)
                    # CÁLCULO DOS PONTOS
                    pontos_perdidos = abs(numero_secreto - chute)
                    pontos -= pontos_perdidos
                    cont_tentativas = x - tentativas
                if chute == 0:
                    clear()
                    break
                # RESUMO DA PARTIDA
                print("|||||||||  RESUMO DA PARTIDA  |||||||||")
                print("\nRESPOSTA: %d" % numero_secreto)
                print("NOME: %s\nPONTUAÇÃO FINAL: %d\nTENTATIVAS O: %d" % (nome, pontos, cont_tentativas))
                print("TENTATIVAS N: %d\nCHUTES : %s" % (len(l_chutes), l_chutes))
                print("\nVERIFICANDO RANKING...")
                time.sleep(3)
                # VERIFICAÇÃO E ATUALIZAÇÃO DE NOVO TOP 3 E QUEBRA DE RECORDE
                if pontos > ranking[0][1]:
                    print("\nPARABÉNS VOCÊ ENTROU NO TOP 3 !")
                    print("\n|||||||||||  NOVO  RECORDE  |||||||||||")
                    print("\nVOCÊ FICOU EM 1º LUGAR !")
                    ranking[2][0] = ranking[1][0]
                    ranking[2][1] = ranking[1][1]
                    ranking[1][0] = ranking[0][0]
                    ranking[1][1] = ranking[0][1]
                    ranking[0][0] = nome
                    ranking[0][1] = pontos
                elif pontos > ranking[1][1]:
                    print("\nPARABÉNS VOCÊ ENTROU NO TOP 3 !")
                    print("\nVOCÊ FICOU EM 2º LUGAR !")
                    ranking[2][0] = ranking[1][0]
                    ranking[2][1] = ranking[1][1]
                    ranking[1][0] = nome
                    ranking[1][1] = pontos
                elif pontos > ranking[2][1]:
                    print("\nPARABÉNS VOCÊ ENTROU NO TOP 3 !")
                    print("\nVOCÊ FICOU EM 3º LUGAR !")
                    ranking[2][0] = nome
                    ranking[2][1] = pontos
                else:
                    dif = ranking[2][1] - pontos
                    print("\nSEM QUEBRA DE RECORDES!")
                    print("\nVOCÊ NÃO ENTROU NO TOP 3 POR %s PONTOS" % dif)
                print(la, lt)
                print(la1 % (dificuldade, ranking[0][0], ranking[0][1]))
                print(la2 % (ranking[1][0], ranking[1][1], ranking[2][0], ranking[2][1]))
                print(lt2, la)
                if nivel == 1:
                    ranking_f = ranking
                elif nivel == 2:
                    ranking_m = ranking
                else:
                    ranking_d = ranking
                b = 0
                while b == 0:
                    print("\nDESEJA JOGAR NOVAMENTE ?")
                    resp = int(input("SIM = 1  |||  NÃO = 0: "))
                    if resp == 1:
                        clear()
                        g = 0
                        b = 1
                    elif resp == 0:
                        print("\nVOLTANDO AO MENU PRINCIPAL...")
                        time.sleep(1)
                        clear()
                        g = 1
                        b = 1
                    else:
                        print("ESCOLHA ENTRE SIM(1) E NÃO(0) !!!")
        # OPÇÃO SAIR
        elif menu == 3:
            print("\nENCERRANDO...")
            m = 1
            time.sleep(1)
            clear()
        else:
            # EXCEÇÃO PARA CAPTAR ERROS
            print("Escolha um número de 1 a 3 !!!".upper())

    time.sleep(1)

if(__name__ == "__main__"):
    jogar()