# 조작키
#       Down - Drop block faster
# Left/Right - Move block
#         Up - Rotate block clockwise
#     Escape - Quit game
#          P - Pause game
#     Return - Instant drop

from random import randrange as rand
import pygame, sys

pygame.mixer.init()

#배경음악 재생
pygame.mixer.music.load("Tetris_background.wav")
pygame.mixer.music.play(-1)

game_over_sound=pygame.mixer.Sound('gameoversound.wav')

# 기본 설정
cell_size = 30   # 크기
width =     10   # 가로
string =    22   # 세로
maxfps =    30

colors = [
( 0,  0,  0 ),
(255, 113, 113), # 빨 - ㅜ
(110, 227, 247), # 하늘 - s
(250, 237, 125), # 노랑 - z
(255, 178, 217), # 연분홍 - ㄴ
(103, 153, 255), # 파랑 - ㄴ(반대)
(188, 229, 92 ), # 연두 - 막대기
(183, 120, 255), # 연보라 - ㅁ
(53,  53,  53)   # 배경
]

tetris_shapes = [ # 모양
    [[1, 1, 1],   # ㅜ
     [0, 1, 0]],

    [[0, 2, 2],   # s
     [2, 2, 0]],

    [[3, 3, 0],   # z
     [0, 3, 3]],

    [[4, 0, 0],   # ㄴ
     [4, 4, 4]],

    [[0, 0, 5],   # ㄴ(반대)
     [5, 5, 5]],

    [[6, 6, 6, 6]],  # 막대기

    [[7, 7],      # ㅁ
     [7, 7]]
]

# 오른쪽으로 회전
def rotate_shape_right(shape):
    return [
        [ shape[y][x] for y in range(len(shape)) ]
        for x in range(len(shape[0]) - 1, -1, -1)
    ]

# 블록이 벽이나 땅에(혹은 가장 위에있는 블록) 부딪히는 경우
def check_overlapped(board, shape, offset):
    x_offset, y_offset = offset
    for cy, row in enumerate(shape):
        for cx, cell in enumerate(row):
            try:
                if cell and board[ cy + y_offset ][ cx + x_offset ]:
                    return True  # 충돌
            except IndexError:
                return True
    return False

# 한 줄이 모두 채워질 시 해당 줄을 지움
def erase_line(board, line):
    del board[line]
    return [[0 for i in range(width)]] + board

# 블록이 가장 아래블록에 닿을 경우
def update(mat1, mat2, mat2_off):
    x_offset, y_offset = mat2_off
    for cy, string in enumerate(mat2):
        for cx, val in enumerate(string):
            mat1[cy+y_offset-1][cx+x_offset] += val
    return mat1

# 벽과 겹쳐진 블록의 상태를 유지해줌
def make_newboard():
    board = [
        [ 0 for x in range(width) ]
        for y in range(string)
    ]
    board += [[ 1 for x in range(width)]]
    return board

# 게임 실행
class TetrisApp(object):
    def __init__(self):
        pygame.init()
        pygame.key.set_repeat(250,25)
        # 게임 창의 너비
        self.width = cell_size*(width+6)
        self.rlim = cell_size*width
        # 게임 창의 높이
        self.height = cell_size*string
        # 배경의 격자무늬
        self.bground_grid = [[ 8 if x%2==y%2 else 0 for x in range(width)] for y in range(string)]
        # 게임화면 옆의 글씨 크기
        self.default_font =  pygame.font.Font(
            pygame.font.get_default_font(), 17)

        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.event.set_blocked(pygame.MOUSEMOTION) # 마우스의 움직임을 필요로하지 않기 때문에 이를 막음
        # 다음에 나올 블록을 보여줌
        self.next_block = tetris_shapes[rand(len(tetris_shapes))]
        self.init_game()

    # 새 블록을 소환
    def call_newblock(self):
        # 다음에 나올 예정이었던 블록을 현재 블록으로 옮김
        self.block = self.next_block[:]
        # 다음에 나올 블록을 새로 보여줌
        self.next_block = tetris_shapes[rand(len(tetris_shapes))]
        self.block_x = int(width / 2 - len(self.block[0])/2)
        self.block_y = 0

        # 벽과의 충돌을 확인
        if check_overlapped(self.board,
                           self.block,
                           (self.block_x, self.block_y)):
            self.gameover = True   # 부딪힐 시 게임 종료

    # 게임을 실행할 때 초기 설정(게임 시작)을 위해 불러오는 함수
    def init_game(self):
        self.board = make_newboard()
        self.call_newblock()
        self.level = 1
        self.score = 0
        self.lines = 0
        pygame.time.set_timer(pygame.USEREVENT+1, 1000)

    # 실행화면 오른쪽에 메세지 출력
    def print_message(self, message, top_left):
        x,y = top_left
        for line in message.splitlines():
            self.screen.blit(
                self.default_font.render(
                    line,
                    False,
                    (255,255,255),
                    (0,0,0)),
                (x,y))
            y+=14

    # 가운데에 메세지 출력
    def print_message_at_center(self, message):
        for i, line in enumerate(message.splitlines()):
            message_image =  self.default_font.render(line, False,
                (255,255,255), (0,0,0))

            msgim_center_x, msgim_center_y = message_image.get_size()
            msgim_center_x //= 2
            msgim_center_y //= 2

            self.screen.blit(message_image, (
              self.width // 2-msgim_center_x,
              self.height // 2-msgim_center_y+i*22))

    # 블록을 그림
    def draw(self, matrix, offset):
        x_offset, y_offset  = offset
        # 블록 데이터는 1차원이므로 for문을 이용해 0~배열의길이까지 반복
        for y, row in enumerate(matrix):
            for x, val in enumerate(row):
                if val:
                    # 색상은 colors[val]이고, pygame.Rect(1,2,3,4)에서
                    # 1, 2를 왼쪽으로 하는 (3,4)영역에 그려짐
                    pygame.draw.rect(
                        self.screen,
                        colors[val],
                        pygame.Rect(
                            (x_offset+x) *
                              cell_size,
                            (y_offset+y) *
                              cell_size,
                            cell_size,
                            cell_size),0)

    # 라인을 지울 시 레벨과 점수를 올림
    def if_clear(self, n):
        linescores = [0, 40, 100, 300, 1200]
        self.lines += n
        self.score += linescores[n] * self.level
        if self.lines >= self.level*6:
            self.level += 1
            newdelay = 1000-50*(self.level-1)
            newdelay = 100 if newdelay < 100 else newdelay
            pygame.time.set_timer(pygame.USEREVENT+1, newdelay)

    # 블록을 움직임
    def move_block(self, delta_x):
        if not self.gameover and not self.paused:
            new_x = self.block_x + delta_x
            if new_x < 0:
                new_x = 0
            if new_x > width - len(self.block[0]):
                new_x = width - len(self.block[0])
            # 키를 조작했을 때 겹치지 않을 때
            if not check_overlapped(self.board,
                                   self.block,
                                   (new_x, self.block_y)):
                self.block_x = new_x  # 키 조작을 유효로 함

    # 게임을 종료할 시
    def exit(self):
        self.print_message_at_center("Exiting...")
        pygame.mixer.music.stop()
        pygame.display.update()
        pygame.quit()
        sys.exit()

    # 블록을 떨어뜨릴 때
    def drop_block(self, manual):
        if not self.gameover and not self.paused:
            self.score += 1 if manual else 0
            self.block_y += 1
            # 바닥(가장 위에 있는 블록)에 닿았을 때
            if check_overlapped(self.board,
                               self.block,
                               (self.block_x, self.block_y)):
                self.board = update(
                  self.board,
                  self.block,
                  (self.block_x, self.block_y))
                self.call_newblock()
                clear_line = 0
                while True:
                    # 한 줄이 모두 채워지면 해당 줄을 지움
                    for i, row in enumerate(self.board[:-1]):
                        if 0 not in row:
                            self.board = erase_line(
                              self.board, i)
                            clear_line += 1
                            break
                    else:
                        break
                self.if_clear(clear_line)
                return True
        return False

    def insta_drop(self):
        if not self.gameover and not self.paused:
            while(not self.drop_block(True)):
                pass

    # 블록을 회전시킴
    def rotate_block(self):
        if not self.gameover and not self.paused:
            new_block = rotate_shape_right(self.block)
            # 블록을 회전시켰을 때 벽과 부딪히지 않는다면 실행
            if not check_overlapped(self.board,
                                   new_block,
                                   (self.block_x, self.block_y)):
                self.block = new_block

    # 게임을 멈췄을 시
    def pause(self):
        self.paused = not self.paused

    def game_start(self):
        if self.gameover:
            self.init_game()
            pygame.mixer.music.play(-1)
            self.gameover = False

    # 게임 실행
    def run_game(self):
        # 조작키
        key_actions = {
            'ESCAPE':   self.exit,
            'LEFT':     lambda:self.move_block(-1),
            'RIGHT':    lambda:self.move_block(+1),
            'DOWN':     lambda:self.drop_block(True),
            'UP':       self.rotate_block,
            'p':        self.pause,
            'SPACE':    self.game_start,
            'RETURN':   self.insta_drop
        }

        self.gameover = False
        self.paused = False

        dont_burn_my_cpu = pygame.time.Clock()
        while 1:
            self.screen.fill((0, 0, 0))

            # 게임 오버될 시 출력되는 문구
            if self.gameover:
                self.print_message_at_center("""Game Over!\nYour score: %d\nPress space to continue""" % self.score)
                pygame.mixer.music.stop()
                game_over_sound.play(1)

            else:
                # 게임 중지할 시 출력되는 문구
                if self.paused:
                    self.print_message_at_center("Paused")
                else:
                    pygame.draw.line(self.screen,
                        (255,255,255),
                        (self.rlim+1, 0),
                        (self.rlim+1, self.height-1))
                    # 다음 블록을 알려주는 문구
                    self.print_message("Next:", (
                        self.rlim+cell_size,
                        2))
                    # 게임 실행 화면 오른쪽에 출력되는 문구
                    self.print_message("Score: %d\n\nLevel: %d\
                        \nLines: %d" % (self.score, self.level, self.lines),
                        (self.rlim+cell_size, cell_size*5))
                    self.draw(self.bground_grid, (0,0))
                    self.draw(self.board, (0,0))
                    self.draw(self.block,
                        (self.block_x, self.block_y))
                    self.draw(self.next_block,
                        (width+1,2))
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.USEREVENT+1:
                    self.drop_block(False)
                elif event.type == pygame.QUIT:
                    self.exit()
                elif event.type == pygame.KEYDOWN:
                    for key in key_actions:
                        if event.key == eval("pygame.K_"
                        +key):
                            key_actions[key]()

            dont_burn_my_cpu.tick(maxfps)

if __name__ == '__main__':
    App = TetrisApp()
    App.run_game()


class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        res=0
        for c in list(s):
            res = res*26+ord(c)-64
        return res

s=Solution()
print (s.titleToNumber("AA"))


__author__ = 'Bobsleigh'

from hand import Hand
from card import Card
import pokertextfile
import numpy

class PokerStats:
    """Used to perform data analysis on a PokerStars hands text file.

    Attributes:
        file (PokerTextFile) : A PokerTextFile object to perform data analysis on.

    """
    def __init__(self):
        """Constructor for PokerStats.
        """
        pass

    def handsNet(self, handList):
        """Returns a list of the net gain (or loss, in negative) in chips for all the hands in the file.

        Returns:
            net_gain (list) : A list of the net gain of all the hands in the file. Losses are negative.
        """

        net_gain = []

        for hand in handList:
            net_gain.append(hand.get_net())

        return net_gain

    def handsPartialSum(self, handList):

        partialSum = []
        handsNet = self.handsNet(handList)
        lastHand = 0

        for netGain in handsNet:
            partialSum.append(netGain+lastHand)
            lastHand = netGain

        return partialSum

    def avgGain(self, handList):
        """Returns the average gain (positive) or loss (negative) of the hands in the file.

        Returns:
            avg (int) : average gain (positive) or loss (negative) of the hands in the file.
        """

        sum = 0
        for chips in self.handsNet(handList):
            sum += chips

        if sum == 0:
            return 0
        else:
            return sum

    def maxGain(self, handList):
        try:
            return numpy.amax(self.handsNet(handList))
        except Exception:
            return 0

    def maxLoss(self, handList):
        try:
            return numpy.amin(self.handsNet(handList))
        except Exception:
            return 0


def affineEncrypt(text, a, b):
    """encrypts the plaintext 'text', using an affine transformation key (a, b)
    INPUT:  text - plaintext as a string of letters
            a - integer satisfying gcd(a, 26) = 1.  Raises error if such is not the case
            b - integer

    OUTPUT: The encrypted message as a string of characters
    """
    c=26
    d=a
    while(c != 0):#while the remainder is not zero

        m=d//c#(a div c)

        a1=d#update the subtractor variable

        d=c#update the variable being subtracted

        c=a1-m*c#the Eulerian algorithm
    if(d!=1):
        return "The given key is invalid. The gcd(a,26) must be 1."
    else:
        output=""
        for i in text:
            p=int(letters2digits(i))
            encrypt=(a*p+b)%26
            digit=str(encrypt)
            if(len(digit)<2):
                digit="0"+digit
            encryption=digits2letters(digit)
            output=output+encryption

        return output
    pass

def letters2digits(letters):
    """converts a string of letters A-Z to a string of double digits without spaces in the range 00-25"""
    letter2digit = {"A" : "00", "B" : "01", "C" : "02", "D" : "03", "E" : "04",
                  "F" : "05", "G" : "06", "H" : "07", "I" : "08", "J" : "09",
                  "K" : "10", "L" : "11", "M" : "12", "N" : "13", "O" : "14",
                  "P" : "15", "Q" : "16", "R" : "17", "S" : "18", "T" : "19",
                  "U" : "20", "V" : "21", "W" : "22", "X" : "23", "Y" : "24",
                  "Z" : "25"}

    digits = ""  #initializing digits string
    letters = "".join(letters.split()) #removing whitespaces in the text

    for i in range(0, len(letters)):
        if(letters[i].isalpha()):
            letter = letters[i].upper()  #converting to uppercase
            digit = letter2digit[letter]
            digits += digit     # concatenating to the string of digits

    return digits

def digits2letters(digits):
    """converts a string of double digits without spaces in the range 00-25 to a string of letters A-Z"""
    letter2digit = {"A" : "00", "B" : "01", "C" : "02", "D" : "03", "E" : "04",
                  "F" : "05", "G" : "06", "H" : "07", "I" : "08", "J" : "09",
                  "K" : "10", "L" : "11", "M" : "12", "N" : "13", "O" : "14",
                  "P" : "15", "Q" : "16", "R" : "17", "S" : "18", "T" : "19",
                  "U" : "20", "V" : "21", "W" : "22", "X" : "23", "Y" : "24",
                  "Z" : "25"}

    digit2letter = dict((v,k) for k,v in letter2digit.items())  #creating a dictionary with keys and values exchanged

    letters = ""
    start = 0  #initializing starting index of first digit
    for i in range(0, len(digits), 2):
        digit = digits[start : start + 2]  # accessing the double digit
        letters += digit2letter[digit]     # concatenating to the string of letters
        start += 2                         # updating the starting index for next digit

    return letters

print(affineEncrypt("MEETYOUINTHEPARK", 1,3))


##
## Imprima los valores unicos e la columna _c4 de
## de la tabla tbl1 en mayusculas
##

import pandas as pd
import numpy as np

x1 = pd.read_csv('tbl1.tsv', sep='\t')
l= x1['_c4'].unique()
q04 = [x.upper() for x in l]
q04.sort()
print(q04)


"""walker class
"""

from random import randint

from core.base import BaseCanvas

class TradRdmWalker(BaseCanvas):

    x = 0
    y = 0
    # clr = ''

    def __init__(self, master=None, **kw):

        BaseCanvas.__init__(self, master, **kw)

        self.x = int(kw.get('width')/2)
        self.y = int(kw.get('height')/2)

    def build_scene(self):

        self.step()

        self.create_line(self.x, self.y, self.x+1, self.y+1, width=2, fill='black')

    def step(self):

        gen_x = randint(-1,1)
        gen_y = randint(-1,1)

        self.x += gen_x
        self.y += gen_y

        # versao 01
        # random_number = randint(0,3)
        #
        # if random_number == 0:
        #     self.x += 1
        #     self.clr = 'blue'
        #
        # elif random_number == 1:
        #     self.x -= 1
        #     self.clr = 'black'
        #
        # elif random_number == 2:
        #     self.y += 1
        #     self.clr = 'red'
        #
        # else:
        #     self.y -= 1
        #     self.clr = 'orange'


import time, sys
start_time = time.time()
anagramList = []
with open('lemmad.txt', 'rb') as file:
    for value in enumerate(file):
        w = value[1].strip('\r\n')
        if(sorted(w) == sorted(sys.argv[1])):
            anagramList.append(w)
print('Output (duration,list,of,anagrams,found): ' +
 str((time.time() - start_time) * 1000000) +
 ',' + ",".join(map(str,anagramList)))


loan = years = intrest = str()
while str(loan+years+intrest).isdigit()!=True:
    loan = input("Enter your initial loan: ")
    years = input("Enter the number of years: ")
    intrest = input("Enter the percentage of intrest %")
    if str(loan+years+intrest).isdigit()!=True:
        print("\nInccorect Input!")
loan, years, intrest = int(loan), int(years), int(intrest)
for i in range(int(years)):
    print("Year ",i+1,": £",int(loan),"x ",intrest,"%=£",int(loan/intrest),
    " Total=£",int(loan+(loan/intrest)),sep="")
    loan = int(loan+(loan/intrest))

Python/!PYTHON/PYTHONchallenges/3. Iteration (Unconditional loops)/task 4 solution 3.py


from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
from rest_framework import status
import os, shutil, requests, subprocess, time

def save_folder(input_path, storage_path, auth_header):
    headers = {'Authorization': auth_header}

    response = requests.get(url="https://api.cdrive.columbusecosystem.com/list/?path=" + input_path, headers=headers)
    drive_objects = response.json()['driveObjects']
    for dobj in drive_objects:
        if dobj['type'] == 'Folder':
            os.mkdir(storage_path + '/' + dobj['name'])
            save_folder(input_path + '/' + dobj['name'], storage_path + '/' + dobj['name'], auth_header)
        else:
            url = "https://api.cdrive.columbusecosystem.com/download/?path=" + input_path + '/' + dobj['name']
            download_url = requests.get(url=url, headers=headers).json()['download_url']
            response = requests.get(url=download_url)
            open(storage_path + '/' + dobj['name'], 'wb').write(response.content)

class StartExecutionView(APIView):
    parser_class = (JSONParser,)

    @csrf_exempt
    def post(self, request):
        input_path = request.data['input_path']
        output_path = request.data['output_path']

        auth_header = request.META['HTTP_AUTHORIZATION']

        glm_path = '/storage/glm'
        if os.path.exists(glm_path):
            shutil.rmtree(glm_path)
        os.mkdir(glm_path)
        save_folder(input_path, glm_path, auth_header)

        glm_out_path = glm_path + '/output'
        os.mkdir(glm_out_path)

        subprocess.call('/glm_build/glm', cwd=glm_path)
        time.sleep(2)

        for file_name in os.listdir(glm_out_path):
            file_path = glm_out_path + '/' + file_name
            f = open(file_path, 'rb')
            file_arg = {'file': (file_name, f), 'path': (None, output_path)}
            requests.post('https://api.cdrive.columbusecosystem.com/upload/', files=file_arg, headers={'Authorization': auth_header})
            f.close()

        return Response(status=status.HTTP_200_OK)

class ExecutionStatusView(APIView):
    parser_class = (JSONParser,)

    def get(self, request):
        return Response(status=status.HTTP_200_OK)

class ClientIdView(APIView):
    parser_class = (JSONParser,)

    def get(self, request):
        client_id = os.environ['COLUMBUS_CLIENT_ID']
        return Response({"client_id": client_id})

class AuthenticationTokenView(APIView):
    parser_class = (JSONParser,)

    @csrf_exempt
    def post(self, request, format=None):
        code = request.data['code']
        redirect_uri = request.data['redirect_uri']
        data = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': redirect_uri,
            'client_id': os.environ['COLUMBUS_CLIENT_ID'],
            'client_secret': os.environ['COLUMBUS_CLIENT_SECRET']
        }
        response = requests.post(url='http://authentication.columbusecosystem.com/o/token/', data=data)

        return Response(response.json(), status=response.status_code)


# -*- coding: utf-8 -*-
# vim:tabstop=4:shiftwidth=4:expandtab

from django.core.management.base import (
    BaseCommand as DjangoBaseCommand, CommandError)
from django.conf import settings

from pathlib import Path
import tempfile
import shutil
import logging
logger = logging.getLogger(__name__)

class BaseCommand(DjangoBaseCommand):

    def execute(self, *args, **options):
        # if options['delete']:
        #     pass
        try:
            super(BaseCommand, self).execute(*args, **options)

        except Exception as e:
            logger.exception(e)
            if settings.DEBUG:
                raise
            else:
                raise CommandError(e)

class TmpToFile(object):

    def __init__(self, _text, output_filename):
        self._text = _text
        self.output_filename = output_filename

    def write_file(self):
        with tempfile.TemporaryDirectory(
                suffix='_file', prefix='tmp_', dir='/var/tmp') as temp_dir:
            fname = self.write_str_into_file(temp_dir)
            shutil.move(fname, self.output_filename)

    def write_str_into_file(self, dir_name):
        with tempfile.NamedTemporaryFile(delete=False, dir=dir_name,) as temp:
            temp.write(self._text)
            return temp.name


import sys
import re
import os
import logging
import argparse
import time
import datetime
import json

from enum import Enum

from zmachine.interpreter import Story,Interpreter,OutputStream,OutputStreams,Memory,QuitException,\
                                 StoryFileException,InterpreterException,MemoryAccessException,\
                                 InputStreams,InputStream,RestartException
from zmachine.text import ZTextException
from zmachine.memory import BitArray,MemoryException
from zmachine.instructions import InstructionException

from pygame_terp import PygameUI
from generic_terp import STDOUTOutputStream,ConfigException,FileStreamEmptyException

SETTINGS = {'dimensions': (640,480),
            'char_dimensions': (80,30),
            'font_name': 'courier',
            'font_size': 12}

# How many zcode steps we take before checking for keypress
INPUT_BREAK_FREQUENCY=1000

class RunState(Enum):
    RUNNING                  = 0
    WAITING_TO_QUIT          = 1
    PROMPT_FOR_SAVE          = 2
    PROMPT_FOR_RESTORE       = 3

class Terp(object):
    def __init__(self,zmachine,story_filename,tracer=None):
        self.state = RunState.RUNNING
        self.zmachine = zmachine
        self.story_filename = story_filename
        self.tracer = tracer

    def run(self):
        if self.state != RunState.RUNNING:
            self.state = RunState.RUNNING

    def start_save(self):
        self.state = RunState.PROMPT_FOR_SAVE
        stream = self.zmachine.output_streams.get_screen_stream()
        self.zmachine.output_streams.get_screen_stream().print_str('Name of file for save (in %s)? ' %
                                            self.zmachine.save_handler.save_path)
        stream.flush()
        self.zmachine.input_streams.active_stream.readline()

    def handle_save(self,save_name):
        stream = self.zmachine.output_streams.get_screen_stream()
        message = self.zmachine.save_handler.save_to(save_name,self.zmachine)
        stream.print_str(message)
        stream.new_line()
        stream.flush()

        self.run()

    def start_restore(self):
        self.state = RunState.PROMPT_FOR_RESTORE
        self.zmachine.output_streams.get_screen_stream().print_str('Name of file for restore (in %s)? ' %
                                self.zmachine.save_handler.save_path)
        self.zmachine.output_streams.get_screen_stream().flush()
        self.zmachine.input_streams.active_stream.readline()

    def handle_restore(self,save_name):
        stream = self.zmachine.output_streams.get_screen_stream()
        message = self.zmachine.restore_handler.restore_from(save_name,self.zmachine)
        stream.print_str(message)
        stream.new_line()
        stream.flush()

        self.run()

    def wait_for_quit(self):
        self.state = RunState.WAITING_TO_QUIT
        self.zmachine.output_streams.get_screen_stream().print_str('\n[HIT ESC AGAIN TO QUIT]')
        self.zmachine.output_streams.get_screen_stream().flush()

    def idle(self,input_stream):
        """ Called if no key is pressed """
        if self.state == RunState.RUNNING:
            self.zmachine.step()

            if self.tracer:
                self.tracer.log_instruction(self.zmachine.last_instruction)

    def text_entered(self,text,input_stream,output_streams):
        if self.state in (RunState.RUNNING,RunState.PROMPT_FOR_SAVE,RunState.PROMPT_FOR_RESTORE):
            if self.state == RunState.PROMPT_FOR_SAVE:
                self.handle_save(text)
                input_stream.reset()
            elif self.state == RunState.PROMPT_FOR_RESTORE:
                self.handle_restore(text)
                input_stream.reset()
            else:
                output_streams.command_entered(text)
                output_streams.flush()
                if self.tracer:
                    self.tracer.start_command(text)
        elif self.state == RunState.WAITING_TO_QUIT:
            self.run()

class MainLoop(object):
    def __init__(self,zmachine,raw=False,commands_path=None,story_filename=None,tracer=None,seed=None,transcript_path=None,save_path=None):
        self.zmachine = zmachine
        self.curses_input_stream = None
        self.raw = raw
        self.commands_path = commands_path
        self.tracer = tracer
        self.seed=seed
        self.transcript_path=transcript_path
        self.save_path = save_path
        self.story_filename = story_filename

    def loop(self):
        ui=PygameUI(SETTINGS)

        if self.seed != None:
            self.zmachine.story.rng.enter_predictable_mode(int(self.seed))

        if self.raw:
            output_stream = STDOUTOutputStream()
        else:
            output_stream = ui

        self.zmachine.output_streams.set_screen_stream(output_stream)
        self.output_stream=output_stream

        if self.transcript_path:
            if not self.transcript_path.endswith('.transcript'):
                raise ConfigException('All transcripts must end with the .transcript extension')
            if os.path.isdir(self.transcript_path):
                raise ConfigException('Transcript path must be to a file that ends in .transcript')

            transcript_stream = FileOutputStream(self.transcript_path)
            self.zmachine.output_streams.set_transcript_stream(transcript_stream)
            transcript_stream.print_str('--- Game started at %s ----\n\n' % datetime.datetime.now())
            transcript_stream.flush()

            # Create a command transcript as well
            transcript_stream = FileOutputStream(self.transcript_path + '.commands')
            self.zmachine.output_streams.set_commands_stream(transcript_stream)
            transcript_stream.print_str('--- Game started at %s ----\n\n' % datetime.datetime.now())
            transcript_stream.flush()

        self.zmachine.input_streams.keyboard_stream = ui
        self.zmachine.input_streams.select_stream(InputStreams.KEYBOARD)

        # If provided with a command file, load it as the file stream and select it by default
        if self.commands_path:
            input_stream = FileInputStream(output_stream)
            input_stream.load_from_path(self.commands_path)
            self.zmachine.input_streams.command_file_stream =input_stream
            self.zmachine.input_streams.select_stream(InputStreams.FILE)

        terp = Terp(self.zmachine,self.story_filename,tracer=self.tracer)
        terp.run()
        ui.refresh()
        self.terp = terp

        #if self.save_path:
        #    self.zmachine.save_handler = TerpSaveHandler(terp,self.save_path)
        #    self.zmachine.restore_handler = TerpRestoreHandler(terp,self.save_path)

        counter = 0
        timer = 0
        while True:
            try:
                input_stream = self.zmachine.input_streams.active_stream

                if not ui.tick():
                    break

                if input_stream.entered_text_buffer:
                    terp.text_entered(input_stream.entered_text_buffer, input_stream, self.zmachine.output_streams)

                if not input_stream.waiting_for_line:
                    if terp.state == RunState.RUNNING:
                        terp.idle(input_stream)
            except (QuitException,RestartException) as e:
                self.zmachine.output_streams.flush()
                raise e
            except FileStreamEmptyException:
                self.zmachine.input_streams.select_stream(InputStreams.KEYBOARD)
            except Exception as e:
                raise Exception('Unhandled exception "%s" at PC 0x%04x [%s]' % (e,self.zmachine.pc,self.zmachine.last_instruction),e)

        # If pygame returns False, treat as a quit
        raise QuitException()

def load_zmachine(filename,restart_flags=None):
    with open(filename,'rb') as f:
        story = Story(f.read())
        outputs = OutputStreams(OutputStream(),OutputStream())
        inputs = InputStreams(InputStream(),InputStream())
        zmachine = Interpreter(story,outputs,inputs,None,None)
        zmachine.reset(restart_flags=restart_flags)
        zmachine.story.header.set_debug_mode()

    return zmachine

def start(path,commands_path,trace_file_path=None,seed=None,restart_flags=None,transcript_path=None,save_path=None):
    tracer = None
    if trace_file_path:
        tracer = Tracer()

    zmachine = load_zmachine(path,restart_flags)
    story_path, story_filename = os.path.split(path)
    loop = MainLoop(zmachine,
        story_filename=story_filename,
        commands_path=commands_path,
        tracer=tracer,
        seed=seed,
        transcript_path=transcript_path,
        save_path=save_path)

    loop.loop()

def main(*args):
    if sys.version_info[0] < 3:
        raise Exception("Moosezmachine requires Python 3.")

    parser = argparse.ArgumentParser()
    parser.add_argument('story',help='Story file to play')
    parser.add_argument('--raw',help='Output to with no curses',required=False,action='store_true')
    parser.add_argument('--commands_path',help='Path to optional command file',required=False)
    parser.add_argument('--save_path',help='Path to directory for saves. Will default to /tmp',required=False,default='/tmp')
    parser.add_argument('--transcript_path',help='Path for transcript. This will also activate transcript by default. A separate commands transcript will also automatically be created.',required=False)
    parser.add_argument('--seed',help='Optional seed for RNG',required=False)
    parser.add_argument('--trace_file',help='Path to file to which the terp will dump all instructions on exit',required=False)
    data = parser.parse_args()

    try:
        restart_flags = None # Per spec, when restarting, preserve bit 0 and bit 1 of flag 2 in header
        while True:
            try:
                start(data.story,
                    commands_path=data.commands_path,
                    trace_file_path=data.trace_file,
                    seed=data.seed,
                    transcript_path=data.transcript_path,
                    save_path=data.save_path,
                    restart_flags=restart_flags)
            except RestartException as e:
                restart_flags = e.restart_flags
    except QuitException:

        print("Thanks for playing!")
    except ConfigException as e:
        print(e)

if __name__ == "__main__":
    main()


import os
from datetime import datetime
import yaml

from dgp.lib.gravity_ingestor import read_at1a
from dgp.lib.trajectory_ingestor import import_trajectory
from dgp.lib.etc import align_frames
from dgp.lib.transform.transform_graphs import AirbornePost
from dgp.lib.transform.filters import detrend
from dgp.lib.plots import timeseries_gravity_diagnostic, mapplot_segment, read_meterconfig

# Runtime Options
write_out = True
make_plots = True
add_map_plots = False
diagnostic = True

# Read YAML config file
proj_path = os.path.abspath(os.path.dirname(__file__))
try:
    with open(os.path.join(proj_path, 'config_runtime.yaml'), 'r') as file:
        config = yaml.safe_load(file)
        print('Read YAML configuration file for {}'.format(config['flight']))
except Exception as e:
    print('Error reading the config file')
    # TODO What to do if exception is reached? Exit, or read in test data?
campaign = config['campaign']
flight = config['flight']
begin_line = datetime.strptime(config['begin_line'], '%Y-%m-%d %H:%M')
end_line = datetime.strptime(config['end_line'], '%Y-%m-%d %H:%M')
gravity_directory = config['gravity_dir']
gravity_file = config['gravity_file']
trajectory_source = config['trajectory_src']
trajectory_directory = config['trajectory_dir']
trajectory_file = config['trajectory_file']
gps_fields = config['gps_fields']
configdir = config['config_dir']
outdir = config['out_dir']
try:
    QC_segment = config['QC1']  #
    QC_plot = True
except KeyError:
    print('No QC Segments...')
    QC_plot = False
if trajectory_source == 'Waypoint':
    trajectory_engine = 'c'
    trajectory_delim = ','
else:
    trajectory_engine = 'python'
    trajectory_delim = '\s+'

# Load Data Files
print('\nImporting gravity')
gravity = read_at1a(os.path.join(gravity_directory, gravity_file), interp=True)
print("Gravity START: {}".format(gravity.index[0]))
print("Gravity END:   {}".format(gravity.index[-1]))
print('\nImporting trajectory')
trajectory = import_trajectory(os.path.join(trajectory_directory, trajectory_file),
                               columns=gps_fields, skiprows=1,
                               timeformat='hms', engine=trajectory_engine, sep=trajectory_delim)

# Read MeterProcessing file in Data Directory
config_file = os.path.join(configdir, 'DGS_config_files', 'MeterProcessing.ini')
k_factor = read_meterconfig(config_file, 'kfactor')
tie_gravity = read_meterconfig(config_file, 'TieGravity')
print('K-factor:    {}\nGravity-tie: {}\n'.format(k_factor, tie_gravity))

# Still Readings
#  TODO: Semi-automate or create GUI to get statics
first_static = read_meterconfig(config_file, 'PreStill')
second_static = read_meterconfig(config_file, 'PostStill')

# pre-processing prep
if not begin_line < end_line:
    print('Check your times.  Using start and end of gravity file instead.')
    begin_line = gravity.index[0]
    end_line = gravity.index[-1]
trajectory_full = trajectory[['long', 'lat']]
gravity = gravity[(begin_line <= gravity.index) & (gravity.index <= end_line)]
trajectory = trajectory[(begin_line <= trajectory.index) & (trajectory.index <= end_line)]

# Save a 'meter_gravity' column for diagnostic output
gravity['meter_gravity'] = gravity['gravity']

# align gravity and trajectory frames
gravity, trajectory = align_frames(gravity, trajectory)

# adjust for crossing the prime meridian
trajectory['long'] = trajectory['long'].where(trajectory['long'] > 0, trajectory['long'] + 360)

# de-drift
if not diagnostic:
    gravity['gravity'] = detrend(gravity['gravity'], first_static, second_static)

# adjust to absolute
offset = tie_gravity - k_factor * first_static
gravity['gravity'] += offset

print('\nProcessing')
g = AirbornePost(trajectory, gravity, begin_static=first_static, end_static=second_static)
results = g.execute()

if write_out:   # TODO: split this file up into a Diagnostic and Standard output
    import numpy as np
    import pandas as pd
    print('\nWriting Output to File')
    time = pd.Series(trajectory.index.astype(np.int64) / 10 ** 9,
                     index=trajectory.index, name='unix_time')
    columns = ['unixtime', 'lat', 'long', 'ell_ht',
               'eotvos_corr', 'kin_accel_corr', 'meter_grav',
               'lat_corr', 'fa_corr', 'total_corr',
               'abs_grav', 'FAA', 'FAA_LP']
    values = np.array([time.values, trajectory['lat'].values, trajectory['long'].values, trajectory['ell_ht'].values,
                       results['eotvos'].values, results['kin_accel'].values, gravity['meter_gravity'].values,
                       results['lat_corr'].values, results['fac'].values, results['total_corr'].values,
                       results['abs_grav'].values, results['corrected_grav'].values, results['filtered_grav'].values])
    #
    df = pd.DataFrame(data=values.T, columns=columns, index=time)   #pd.DatetimeIndex(gravity.index)
    df = df.apply(pd.to_numeric, errors='ignore')
    df.index = pd.to_datetime(trajectory.index)
    outfile = os.path.join(outdir, '{}_{}_{}_DGP.csv'.format(campaign, flight, str(begin_line.strftime('%Y%m%d_%H%Mz'))))
    df.to_csv(outfile)  # , na_rep=" ")

###########
# Real plots

if make_plots:
    print('\nPlotting')
    # Time-series Plot
    variables = ['meter_gravity', 'gravity', 'cross_accel', 'beam', 'temp']
    variable_units = ['mGal', 'mGal', 'mGal', ' ', 'C']
    plot_title = '{} {}: QC'.format(campaign, flight)
    plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_meter.png'.format(campaign, flight))
    timeseries_gravity_diagnostic(gravity, variables, variable_units, begin_line, end_line,
                                  plot_title, plot_file)

    variables = ['ell_ht', 'lat', 'long']
    variable_units = ['m', 'degrees', 'degrees']
    plot_title = '{} {}: PNT'.format(campaign, flight)
    plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_trajectory.png'.format(campaign, flight))
    timeseries_gravity_diagnostic(results['trajectory'], variables, variable_units, begin_line, end_line,
                                  plot_title, plot_file)

    variables = ['eotvos', 'lat_corr', 'fac', 'kin_accel', 'total_corr']
    variable_units = ['mGal', 'mGal', 'mGal', 'mGal', 'mGal']
    plot_title = '{} {}: Corrections'.format(campaign, flight)
    plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_corrections.png'.format(campaign, flight))
    timeseries_gravity_diagnostic(results, variables, variable_units, begin_line, end_line,
                                  plot_title, plot_file)

    variables = ['abs_grav', 'corrected_grav', 'filtered_grav']
    variable_units = ['mGal', 'mGal', 'mGal']
    plot_title = '{} {}: Gravity'.format(campaign, flight)
    plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_freeair.png'.format(campaign, flight))
    timeseries_gravity_diagnostic(results, variables, variable_units, begin_line, end_line,
                                  plot_title, plot_file)

    if QC_plot:
        # QC Segment Plot
        variables = ['filtered_grav', 'corrected_grav', 'abs_grav']
        variable_units = ['mGal', 'mGal', 'mGal', 'mGal']
        plot_title = '{} {}: Gravity (segment)'.format(campaign, flight)
        plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_freeair_segment.png'.format(campaign, flight))
        timeseries_gravity_diagnostic(results, variables, variable_units,
                                      QC_segment['start'], QC_segment['end'],
                                      plot_title, plot_file)

    if add_map_plots:
        plot_title = '{} {}: Gravity'.format(campaign, flight)
        plot_file = os.path.join(outdir, '{}_{}_DGP_QCplot_freeair_map.png'.format(campaign, flight))
        mapplot_segment(results, 'filtered_grav',
                        QC_segment['start'], QC_segment['end'],
                        'mGal', plot_title, plot_file)


import multiprocessing
from multiprocessing import Process
def testing():
      print("Works")
def square(n):
       print("The number squares to ",n**2)
def cube(n):
       print("The number cubes to ",n**3)

if __name__=="__main__":
   p1=Process(target=square,args=(7,))
   p2=Process(target=cube,args=(7,))
   p3=Process(target=testing)
   p1.start()
   p2.start()
   p3.start()
   p1.join()
   p2.join()
   p3.join()
   print("We're done")


#PSEUDOCODE

#two ways of implementing the neural network model
import torch.functional as F
import torch

class Net(torch.nn.Module):
    def __init__(self, D_in, H, D_out):
        super(Net, self).__init__()
        self.linear(D_in, H)
        self.linear(H, D_out)
    def forward(self, x):
        x = F.sigmoid(self.linear1(x))
        x = F.sigmoid(self.linear2(x))
        return x

x = torch.linspace(-3.0, 3.0, 0.1, requires_grad=True)
model = Net(1,2,1) #model with one input, one hidden layer with two hidden neurons, one output layer
#alternatively
model = torch.nn.Sequential(torch.nn.Linear(1,2), torch.nn.Sigmoid(), torch.nn.Linear(2,1), torch.nn.Sigmoid())

yhat = model(x) #prediction

#just another piece:
#when the output as here is logistic, as a creterion we are using the BCEloss
criterion = torch.nn.BCEloss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
train(dataset, model, criterion, train_loader, optimizer, epochs=1000)

4 Neural Networks/Simple Neural Networks.py


# http://www.spoj.com/problems/PT07Z/

def dfs(graph, start):
    stack = [(start, 0)]
    visited = set()
    max_node, max_distance = None, 0

    while len(stack):
        node, distance = stack.pop()
        visited.add(node)

        if distance > max_distance:
            max_node, max_distance = node, distance

        for child in graph[node]:
            if child not in visited:
                stack.append((child, distance + 1))

    return (max_node, max_distance)

def find_distance(graph):
    best, distance = dfs(graph, 1)
    node, result = dfs(graph, best)
    return result

def actions():
    # import os
    # with open(os.path.join(os.path.curdir, 'tests/0003_test_case.txt')) as f:
    #     while True:
    #         yield f.readline().split('\n')[0]
    while True:
        yield raw_input()

if __name__ == '__main__':
    go = actions()
    n = int(go.next())
    graph = {i: [] for i in range(1, n + 1)}

    for i in range(n - 1):
        left, right = map(int, go.next().split())
        graph[left].append(right)
        graph[right].append(left)

    print(find_distance(graph))


from jackster import Router
from jackster.funcs import html

def showN(req, res, nxt, n='0'):
    res.html(n)
    nxt()

app = (Router()
    .method(('GET', 'POST'), html('multimethod'))
    .use(r'^/number/(?P<n>\d+)', Router()
        .get(showN)
    )
)


"""
:Author: Rossi
:Date: 2016-02-21
"""
from operator import itemgetter
import numpy as np
from sklearn.grid_search import GridSearchCV

# Utility function to report best scores
def report(grid_scores, n_top=3):
    top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
    for i, score in enumerate(top_scores):
        print("Model with rank: {0}".format(i + 1))
        print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
              score.mean_validation_score,
              np.std(score.cv_validation_scores)))
        print("Parameters: {0}".format(score.parameters))
        print("")

def grid_search(estimator, X, y, param_grid, n_top=3):
    grid_search = GridSearchCV(estimator, param_grid=param_grid)
    grid_search.fit(X, y)
    report(grid_search.grid_scores_, n_top)


from flask_restful import Resource
from flask import request, jsonify
from api.main import db
from main.models import SeismModel, SensorModel
from random import randint, uniform
import time
from flask_jwt_extended import jwt_required, get_jwt_identity

# -------------------------------------------------------------------------------------#
# Creamos la clase para verificar el recurso "Seism".
# -------------------------------------------------------------------------------------#

class VerifSeism(Resource):

    # Definimos "GET" para obtener un recurso de una coleccion y su "ID".
    def get(self, id):
        # Asignamos a la variable "verifseism" un seism traido de la db, si no existe, error 404.
        seism = db.session.query(SeismModel).get_or_404(id)

        if seism.verified:
            # Si el recurso existe, nos devolvera:
            return seism.to_json()

        # Sino, nos devuelve el error 403, Acceso denegado o Prohibido.
        else:
            return 'Access Denied', 403

# -------------------------------------------------------------------------------------#
# Creamos la clase para verificar la COLECCION de recursos "Seisms".
# -------------------------------------------------------------------------------------#

class VerifSeisms(Resource):

    # Luego definimos un "GET" para obtener la coleccion de seisms verificados.
    def get(self):

        # Definimos la variable "page" para decir cuantas paginas tendremos.
        page = 1
        # Definimos "perpage" para decir cuantos sensores mostrara cada pagina.
        perpage = 100
        # Aca analizamos los datos y filtramos los Seisms verificados de la coleccion,"verified == True".
        seisms = db.session.query(SeismModel).filter(SeismModel.verified == True)

        try:
            filter = request.get_json().items()

            # Definimos "for" para los filtros en las consultas...
            for key, value in filter:
                if key == "sensorId":
                    seisms = seisms.filter(SeismModel.sensorId == value)
                if key == "datetime":
                    seisms = seisms.filter(SeismModel.dt.like('%' + value + '%'))
                if key == "magnitude":
                    seisms = seisms.filter(SeismModel.magnitude == value)
                if key == "sensor.name":
                    seisms = seisms.join(SeismModel.sensor).filter(SensorModel.name.like('%' + value + '%'))

                # ORDENAMIENTO
                # Utilizamos sort_by para ordenar todo de mayor a menor.
                if key == "shortby":
                    if value == "datetime":
                        seisms = seisms.order_by(SeismModel.dt)
                    if value == "datime.desc":
                        seisms = seisms.order_by(SeismModel.dt.desc())
                    if value == "sensor.name":
                        seisms = seisms.join(SeismModel.sensor).orderby(SensorModel.name)
                    if value == "sensor.namedesc":
                        seisms = seisms.join(SeismModel.sensor).orderby(SensorModel.name.desc())

                # Definimos los if dentro de for para paginas y cantidad de sismos mostrados por pagina.
                if key == "page":
                    page = value
                if key == "perpage":
                    perpage = value

        except:
            pass

        # Alojamos en la variable seisms, todos los sismos verificados obtenidos de las paginas.
        seisms = seisms.paginate(page, perpage, True, 5000)

        # Con el return nos devolvera la coleccion de Seisms.
        return jsonify({'Verif-seisms': [seism.to_json() for seism in seisms.items]})

# Definimos "POST" para agregar un sieism verificado a la coleccion.
def post(self):
    # Traemos la lista de todos los sensores y los alojamos en la variable "sensors".
    sensors = db.session.query(SensorModel).all()
    sensorsId = []

    # Utilizamos un FOR para recorrer el diccionario de sensors
    for sensor in sensors:
        sensorsId.append(sensor.id)
    if sensorsId:
        value_sensor = {'datetime': time.strftime(r"%Y-%m-%d %H:%M:%S", time.localtime()),
                        'depth': randint(5, 250), 'magnitude': round(uniform(2.0, 5.5), 1),
                        'latitude': uniform(-180, 180), 'longitude': uniform(-90, 90),
                        'verified': True, 'sensorId': sensorsId[randint(0, len(sensorsId) - 1)]
                        }

        seism = SeismModel.from_json(value_sensor)
        # Agregamos el seism a la db.
        db.session.add(seism)
        # Actualizamos la db.
        db.session.commit()

        # Retornamos el seism en formato json con el codigo "201 (Nuevo recurso Creado)"
        return seism.to_json(), 201
    else:
        # Sino nos devuelve el error "400 (Solicitud incorrecta)"
        return "No sensors found, can't create seism", 400

# -------------------------------------------------------------------------------------#
# Creamos la clase para trabajar sobre algun recurso "UnverifSeism" de la coleccion.
# -------------------------------------------------------------------------------------#

class UnverifSeism(Resource):

    @jwt_required
    # Definimos "GET" para obtener un "UnverifSeism" con su id de la coleccion.
    def get(self, id):
        # Asignamos a la variable "seism" un seism traido de la db, si no existe, error 404.
        seism = db.session.query(SeismModel).get_or_404(id)

        # Si el seism no esta verificado entonces:
        if not seism.verified:

            # Nos devuelve los datos del "seism".
            return seism.to_json()

        else:
            # En caso de no existir, nos devuelve un error 403 "Prohibido (el servidor se niega a devolver el contenido)".
            return "Denied Access", 403

    @jwt_required
    # Ahora para modifcar un recurso de la coleccion definimos un "PUT".
    def put(self, id):

        # Trae la coleccion de seism y la almacena en la variable seism.
        seism = db.session.query(SeismModel).get_or_404(id)
        # Extraemos los seism no verif a modificar de la info consultada, en formato json y guarda en "data".
        data = request.get_json().items()

        # si seism no esta verificado.
        if not seism.verified:
            # Utilizamos un FOR para recorrer el diccionario y evalua el contenido alojado en data.
            for key, value in data:
                setattr(seism, key, value)

            # Realiza la operacion
            try:
                # Agregamos un nuevo seism
                db.session.add(seism)
                # Actualiza la db.
                db.session.commit()
                # Nos devuelve el seism modificado con un codigo "201" (EXITO!).
                return seism.to_json(), 201

            # Si algo pasa o ocurre mal...
            except Exception as error:
                # Nos retorna el error "400 (Solicitud incorrecta)"
                return str(error), 400
        else:
            # Si no, nos devuelve el error "403 (Acceso denegado o Prohibido)"
            return "Denied Access", 403

    @jwt_required
    # Por ultimo definimos un "delete" para borrar un seism no verificado de la coleccion.
    def delete(self, id):
        # Traemos de la coleccion de seisms un seism y lo alojamos en la variable, si no existe, error 404.
        seism = db.session.query(SeismModel).get_or_404(id)

        # Si el seism no esta verificado.
        if not seism.verified:
            # Deleteamos el seism y actualizamos la db.
            db.session.delete(seism)
            db.session.commit()

            # Y nos devuelve el cod. 204 con el mensaje:
            return "Unverif seism delete", 204
        # Si no.
        else:
            # Si no, nos devuelve el error "403 (Acceso denegado o Prohibido)"
            return "Denied Access", 403

# -------------------------------------------------------------------------------------#
# Creamos la clase para VER la COLECCION de recursos "Seism UnVerified" osea no verificados.
# -------------------------------------------------------------------------------------#

class UnverifSeisms(Resource):

    @jwt_required
    # Aca definimos un "GET" para obtener la coleccion de UnverifSeisms.
    def get(self):

        # Definimos la variable "page" para decir cuantas paginas tendremos.
        page = 1
        # Definimos "perpage" para decir cuantos sismos mostrara cada pagina.
        perpage = 100

        # Traemos la coleccion de seisms, pero filtramos los seism verificados.
        filters = request.get_json().items()
        seisms = db.session.query(SeismModel).filter(SeismModel.verified == False)
        current_user_id = get_jwt_identity()
        seisms = seisms.join(SeismModel.sensor).filter(SensorModel.userId == current_user_id)

        # Utilizamos un FOR para recorrer el diccionario y evalua el contenido alojado en filters.
        for key, value in filters:
            # Utilizamos condicionales para filtrar por partes...
            if key == "sensorId":
                seisms = seisms.filter(SeismModel.sensorId == value)

            # ORDENAMIENTO
            # Utilizamos sort_by para ordenar todo de mayor a menor.
            if key == "sort_by":
                if value == "datime":
                    seisms = seisms.order_by(SeismModel.dt)
                # Se agrega datetime.desc para realizar el ordenamiento y se almacena en la variable.
                if value == "datetime.desc":
                    seisms = seisms.order_by(SeismModel.dt.desc())

            # Definimos los if dentro de for para la paginacion de sismos no verificados mostrados por pagina.
            if key == "page":
                page = value
            if key == "per_page":
                perpage = value

        # Alojamos en la variable seisms, todos los sismos obtenidos de las paginas.
        seisms = seisms.paginate(page, perpage, True, 100)

        # Nos devuelve la coleccion con los seisms no verificados filtrados.
        return jsonify({"Unverif-seisms": [seism.to_json() for seism in seisms.items]})

    @jwt_required
    # Definimos "POST" para agregar un seisms no verificado a la coleccion.
    def post(self):

        # Traemos la coleccion de seisms completa y la alojamos en la variable, si no existe, error 404.
        sensors = db.session.query(SensorModel).all()
        sensorlist = []

        # Utilizamos un FOR para recorrer el diccionario y evalua el contenido alojado en sensors.
        for sensor in sensors:
            sensorlist.append(sensor.id)
        if sensorlist:
            value_sensor = {
                'datetime': time.strftime(r'%Y-%m-%d %H:%M:%S', time.localtime()), 'depth': randint(5, 250),
                'magnitude': round(uniform(2.0, 5.5), 1), 'latitude': uniform(-180, 180), 'longitude': uniform(-90, 90),
                'verified': False, 'sensorId': sensorlist[randint(0, len(sensorlist) - 1)]
            }

            seism = SeismModel.from_json(value_sensor)
            # Agregamos el seism a la db.
            db.session.add(seism)
            # Actualizamos la db.
            db.session.commit()

            # Retornamos el seism en formato json con el codigo "201 (Nuevo recurso Creado)"
            return seism.to_json(), 201
        else:
            # Sino nos devuelve el error "400 (Solicitud incorrecta)"
            return 'Sensors not found, cant create seism', 400


import xxhash
import os
import sys
from pathos.multiprocessing import cpu_count, ProcessingPool as Pool
import collections
from pprint import pprint
import hashlib
import argparse
import shutil

CHUNK = 1024*1024*4  #4 MB
PROCESSES = cpu_count()

def stream_file(file):
    sum = xxhash.xxh64()
    with open(file, 'rb') as to_sum:
        file_chunk = to_sum.read(CHUNK)
        while file_chunk:
            sum.update(file_chunk)
            file_chunk = to_sum.read(CHUNK)
    return sum.digest(), file

def get_files(path, file_ext=None):
    paths = []
    for folder, subfolders, files in os.walk(path):
        if file_ext is None:
            [os.path.join(folder, x) for x in files]
        else:
            paths += [os.path.join(folder, x) for x in files if os.path.splitext(x)[-1] == file_ext]
    return paths

def get_most_common(counts, duplicates, n):
    return [(duplicates[digest[0]], digest[1]) for digest in counts.most_common(n)]

def extreme_check(common):  # Only takes common I'm lazy
    for results in common:
        print("****************")
        for file in results[0]:
            print(file)
            print(hashlib.md5(open(file, 'rb').read()).hexdigest())

def build_statistics(sums):
    duplicates = {}
    print("Building counts..")
    counts = collections.Counter([x[0] for x in sums])
    print("Building duplicates list...")
    # TODO make this SPEEDY
    for digest, file in sums:
        if duplicates.get(digest) is None:
            duplicates[digest] = [file]
        else:
            duplicates[digest].append(file)
    return counts, duplicates

def build_digests(paths):
    print("Starting {0} Processes...".format(PROCESSES))
    print("Building digests on {0} files...".format(len(paths)))
    pool = Pool(PROCESSES)
    digests = pool.map(stream_file, paths)  # PROBABLY SPEEDY
    return digests

def copy(unique_dict, dest, sim=False):
    print("Copying to {0}...".format(dest))
    if sim:
        def copy_single(file):
            print("Moving {0} to {1}".format(file, dest))
    else:
        def copy_single(file):
            shutil.copy(file, dest)
        os.makedirs(dest, exist_ok=True)
    pool = Pool(PROCESSES)
    pool.map(copy_single, unique_dict.values())

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--directory')
    parser.add_argument('-f', '--fileext', default='.nef')
    parser.add_argument('-c', '--copy')
    parser.add_argument('-s', '--sim', default=False, action='store_true')
    parser.add_argument('i', '--interact', default=False, action='store_true')
    args = parser.parse_args()
    paths = get_files(args.directory, file_ext=args.fileext)
    digests = build_digests(paths)
    counts, duplicates = build_statistics(digests)
    common = get_most_common(counts, duplicates, 5)
    unique = dict(digests)
    if args.interact:
        import code
        code.interact(local=locals())
    if args.copy is not None:
        copy(unique, args.copy, sim=args.sim)
    if common:
        pprint(common)
    else:
        print("No duplicates")


from acab.abstract.data.contexts import Contexts
from acab.abstract.rule.transform import TransformOp
from acab.abstract.rule.rule import Rule
from acab.abstract.rule.action import ActionOp

class LayerRunRules(TransformOp):

    def __init__(self):
        super(LayerRunRules, self).__init__()

    def __call__(self, rules, data=None, engine=None):
        if not isinstance(rules, list):
            rules = [rules]
        assert(all([isinstance(x, Rule) for x in rules]))

        rule_results = []
        for x in rules:
            rule_results += x(ctxs=[data], engine=engine)

        return rule_results

class LayerRunAgenda(TransformOp):

    def __call__(self, agenda, *params, data=None, engine=None):
        if data is None:
            data = {}
        # rebind passed in parameters from the caller (ie: layer),
        # to the agenda's parameters
        rebound = Contexts.rebind_across_contexts(agenda._params,
                                                  params, data)

        return agenda(ctxs=[rebound], engine=engine)

class LayerPerform(ActionOp):

    def __call__(self, proposals, data=None, engine=None):
        for x,y in proposals:
            y._action(ctxs=[x], engine=engine)


"""
It's going to create a video out of a list of PNGs. How it's going to do it is: write a bunch of images,
and then use ffmpeg to convert it to a video. I'm really not happy about this, but I can improve it
later...
"""

import numpy as np
from PIL import Image
import os
import torch
from torch.autograd import Variable

def smooth_transition_to_noise_vector(starting_noise, ending_noise, num_points):
    if num_points < 2:
        raise ValueError("Num Points must be greater than 2")
    end_result = []
    for i in range(0, num_points):
        frac = i / (num_points - 1)
        new_noise = (frac * ending_noise) + (1.0 - frac) * (starting_noise)
        end_result.append(new_noise)
    return np.asarray(end_result)

def make_gif_from_numpy(starting_noise, ending_noise, num_points, generator, gif_dir, iter_number):
    print("Making gif from numpy")
    noise_vectors = smooth_transition_to_noise_vector(starting_noise, ending_noise, num_points)
    noise_vectors_v = Variable(torch.Tensor(noise_vectors))
    output_images = generator(noise_vectors_v).data.cpu().numpy()
    frame_dir = os.path.join(gif_dir, 'frames')
    os.makedirs(frame_dir, exist_ok=True)
    for (i, image) in enumerate(output_images):
        save_path = os.path.join(frame_dir, "gif_frame_{}.png".format(str(i).zfill(3)))
        image = (255.99 * image).astype('uint8')
        pil_image = Image.fromarray(image.reshape(28, 28))
        pil_image = pil_image.convert('RGB')
        pil_image.save(save_path)
    file_path = os.path.join(frame_dir, "gif_frame_%03d.png")
    output_path = os.path.join(gif_dir, "output_{}.gif".format(iter_number))
    try:
        os.remove(output_path)
    except FileNotFoundError:
        pass
    os.system("ffmpeg -i {} {} ".format(file_path, output_path))
    for f in os.listdir(frame_dir):
        if f.startswith('gif_frame_'):
            filename = os.path.join(frame_dir, f)
            os.remove(filename)

# if __name__ == '__main__':
#     print(smooth_transition_to_noise_vector(0, 1, 11))
#     print(smooth_transition_to_noise_vector(np.asarray([0.0, 1.0]), np.asarray([1.0, 0.0]), 11))

# import images2gif
#
#
#
#

#
#
# if __name__ == '__main__':
#     num_points = 10
#     for i in range(0, num_points):
#         frac = i / (num_points - 1)
        # print(frac)


from tkinter import *
from os.path import *

class QuestionForm:

    def __init__(self, root, title):

        formatted_title = title + '\n' + ("-"*60)
        error_msg = StringVar()
        # error_msg.set('')

        root.geometry("600x800")

        data = [] # define this, was thinking about using it as a means to organzie information

        # main title
        header = Label(root, text=formatted_title, bg="black", fg="white", width=28, height=2)
        header.pack(fill=X)

        # error message
        error = Label(root, textvariable=error_msg, bg="black", fg="red", width=28)
        error.pack(fill=X)

        # creates 'frame' to seperate the title from questions
        frame = Frame(root)
        frame.pack()

        ########################################
        ########################################
        ########################################

        #--------------------------------------------

        Label(frame).grid(row=0, column=0)

        # First Name Label
        Label(frame, text="First Name: ").grid(row=1, column=0, sticky=E)
        # First Name Entry
        first_name_entry = Text(frame, height=1, width=25)
        first_name_entry.grid(row=1, column=1)
        first_name_entry.focus_set()

        Label(frame).grid(row=2, column=0)

        #--------------------------------------------

        # Last Name Label
        Label(frame, text="Last Name: ").grid(row=3, column=0, sticky=E)
        # Last Name Entry
        last_name_entry = Text(frame, height=1, width=25)
        last_name_entry.grid(row=3, column=1)

        Label(frame).grid(row=4, column=0)

        # THIS IS OUT OF ORDER ON PURPOSE BECAUSE THE FUNCTION CALL ISN'T DESIGNED THE BEST
        #--------------------------------------------

        Label(frame, text="Other: ").grid(row=6, column=0, sticky=E)
        # Other Location Entry
        other_location_entry = Text(frame, height=1, width=25, bg='light grey', state=DISABLED)
        other_location_entry.grid(row=6, column=1)

        def otherCheck(location):
            if location == "Other":
                other_location_entry.config(bg='white')
                other_location_entry.config(state=NORMAL)
            else:
                other_location_entry.config(bg='light grey')
                other_location_entry.config(state=DISABLED)

        #--------------------------------------------

        def checkLocation():
            if location_var.get() == 'Other':
                if len(other_location_entry.get("1.0", "end-1c")) < 1:
                    error_msg.set("*ERROR: Location Required!*")
                    return False
                else:
                    error_msg.set("")
                    return True
            else:
                error_msg.set("")
                return True

        # OUT OF ORDER ON PURPOSE, SEE ABOVE COMMENT
        # Location Label
        Label(frame, text="Location: ").grid(row=5, column=0, sticky=E)
        location_var = StringVar(root)

        location_choices = ['Main Office',
                            'Bay State',
                            'Wahconah',
                            'Byron Weston',
                            'CTC',
                            'Human Resources',
                            'Old Berkshire',
                            'Transportation',
                            'Pioneer',
                            'Other']

        location_choices.sort()

        location_menu = OptionMenu(frame, location_var, *location_choices, command=otherCheck)

        location_menu.config(bg = "LIGHT GREY")
        location_var.set('Main Office') # set the default option
        location_menu.grid(row =5, column =1, sticky="ew")
        location_menu.update()

        #--------------------------------------------
        Label(frame).grid(row=7, column=0)

        # Applications  Label
        Label(frame, text="Applications: ").grid(row=8, column=0)

        # Applications Entry
        apps_entry = Text(frame, height=3, width=40)
        apps_entry.grid(row=8, column=1)
        Label(frame).grid(row=9, column=0)

        #--------------------------------------------
        # Computer Admin(s) Label
        Label(frame, text="Admin User(s): ").grid(row=15, column=0, sticky=E)
        # Computer Admins(s) Entry
        admins_entry = Text(frame, height=3, width=40)
        admins_entry.grid(row=15, column=1)
        Label(frame).grid(row=16, column=0)

        #--------------------------------------------
        # Printer(s) Label
        Label(frame, text="Printer(s): ").grid(row=17, column=0, sticky=E)
        # Printer(s) Entry
        printers_entry = Text(frame, height=3, width=40)
        printers_entry.grid(row=17, column=1)
        Label(frame).grid(row=18, column=0)

        #--------------------------------------------

        # Notes Label
        Label(frame, text="Notes: ").grid(row=19, column=0, sticky=E)
        # Notes Entry
        notes_entry = Text(frame, height=5, width=40)
        notes_entry.grid(row=19, column=1)

        #--------------------------------------------
        def saveFile(fileName, data):
            # this is saving all output files to the 'output' directory within the program
            fullPath = join('output/', fileName + ".txt")
            # 'w+' implements the creation of a new file (ie. You are WRITING to a NEW file)
            with open(fullPath, "w+") as text_file:
                text_file.write(data)

            error.config(fg="green")
            error_msg.set("*SUCCESS: Text File Created.*")

        #--------------------------------------------
        # THIS IS WHERE THE MAGIC HAPPENS
        def submitData(self):
            fileName = ""
            # NAME = ""
            # LOCATION = ""

            if checkName() and checkLocation():
                NAME = last_name_entry.get("1.0", "end-1c")
                LOCATION = location_var.get()
                fileName += getLocation(LOCATION)
                temp = NAME.split(" ")
                fileName += "_" + temp[0]

                # print(fileName)
            data = "NAME:\n-----------------\n%s %s\n\n\nLOCATION:\n-----------------\n%s\n\n\nAPPLICATIONS:\n-----------------\n%s\n\n\nADMIN USER(S):\n-----------------\n%s\n\n\nPRINTER(S):\n-----------------\n%s\n\n\nNOTES:\n-----------------\n%s" % (first_name_entry.get("1.0", "end-1c"), last_name_entry.get("1.0", "end-1c"), LOCATION, apps_entry.get("1.0", "end-1c"), admins_entry.get("1.0", "end-1c"), printers_entry.get("1.0", "end-1c"), notes_entry.get("1.0", "end-1c"))

            saveFile(fileName, data)

        #--------------------------------------------

        # submit Button
        submit = Button(root, text="Submit", fg="black")
        submit.pack(side = BOTTOM, padx=20, pady=20)
        submit.bind("<Button-1>", submitData)
        #Test

        #--------------------------------------------

        #--------------------------------------------

        # would like to look into cleaning these conditional IF statements up
        def getLocation(location):
            if location == 'Main Office':
                return 'MO'
            elif location == 'Bay State':
                return 'BS'
            elif location == 'Wahconah':
                return 'WAH'
            elif location == 'Byron Weston':
                return 'BW'
            elif location == 'CTC':
                return 'CTC'
            elif location == 'Human Resources':
                return 'HR'
            elif location == 'Old Berkshire':
                return 'OB'
            elif location == 'Transportation':
                return 'TRP'
            elif location == 'Pioneer':
                return 'PIO'
            elif location == 'Other':
                return other_location_entry.get("1.0", "end-1c")

        #--------------------------------------------

        def checkName():
            if len(first_name_entry.get("1.0", "end-1c")) < 1:
                error.config(fg="red")
                error_msg.set("*ERROR: First Name Required!*")
                return False
            elif len(last_name_entry.get("1.0", "end-1c")) < 1:
                error.config(fg="red")
                error_msg.set("*ERROR: Last Name Required!*")
                return False
            else:
                error_msg.set("")
                return True

        #--------------------------------------------


import os
import shutil
import tensorflow as tf
from detext.train import optimization

tmp_out_dir = "/tmp/detext-optimization-test"

class OptimizationTest(tf.test.TestCase):

    def _cleanUp(self, tmp_out_dir):
        if os.path.exists(tmp_out_dir):
            shutil.rmtree(tmp_out_dir, ignore_errors=True)

    def test_adam(self):
        with self.test_session() as sess:
            w = tf.get_variable(
                "w",
                shape=[3],
                initializer=tf.constant_initializer([0.1, -0.2, -0.1]))
            x = tf.constant([0.4, 0.2, -0.5])
            loss = tf.reduce_mean(tf.square(x - w))
            tvars = tf.trainable_variables()
            grads = tf.gradients(loss, tvars)
            global_step = tf.train.get_or_create_global_step()
            optimizer = optimization.AdamWeightDecayOptimizer(learning_rate=0.2)
            train_op = optimizer.apply_gradients(zip(grads, tvars), global_step)
            init_op = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())
            sess.run(init_op)
            for _ in range(100):
                sess.run(train_op)
            w_np = sess.run(w)
            print(w_np.flat)
            self.assertAllClose(w_np.flat, [0.4, 0.2, -0.5], rtol=1e-2, atol=1e-2)

    def test_lamb(self):
        with self.test_session() as sess:
            w = tf.get_variable(
                "w",
                shape=[3],
                initializer=tf.constant_initializer([0.1, -0.2, -0.1]))
            x = tf.constant([0.4, 0.2, -0.5])
            loss = tf.reduce_mean(tf.square(x - w))
            tvars = tf.trainable_variables()
            grads = tf.gradients(loss, tvars)
            global_step = tf.train.get_or_create_global_step()
            optimizer = optimization.LAMBOptimizer(learning_rate=0.02, weight_decay_rate=0.01)
            train_op = optimizer.apply_gradients(zip(grads, tvars), global_step)
            init_op = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())
            sess.run(init_op)
            for _ in range(200):
                sess.run(train_op)
            w_np = sess.run(w)
            self.assertAllClose(w_np.flat, [0.4, 0.2, -0.5], rtol=1e-2, atol=1e-2)

    def test_different_lr(self):
        """Test the usage of different learning rate."""
        with self.test_session() as sess:
            bert_w = tf.get_variable(
                "bert/w",
                shape=[3],
                initializer=tf.constant_initializer([0.1, 0.1, 0.1]))
            non_bert_w = tf.get_variable(
                "w",
                shape=[3],
                initializer=tf.constant_initializer([0.1, 0.1, 0.1]))
            x = tf.constant([1.0, 2.0, 3.0])
            loss = tf.reduce_mean(tf.square(x - bert_w - non_bert_w))

            hparams = tf.contrib.training.HParams(
                learning_rate=0.001,
                num_train_steps=100,
                num_warmup_steps=0,
                lr_bert=0.00001,
                optimizer="bert_adam",
                use_horovod=False,
                ftr_ext='cnn',
                out_dir=tmp_out_dir
            )
            if not tf.gfile.Exists(hparams.out_dir):
                tf.gfile.MakeDirs(hparams.out_dir)
            train_op, _, _ = optimization.create_optimizer(hparams, loss)

            init_op = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())
            sess.run(init_op)
            sess.run(train_op)
            bert_w_v, non_bert_w_v = sess.run((bert_w, non_bert_w))
            print(bert_w_v, non_bert_w_v)
            # The difference of weight values (gradient) reflects the learning arte difference
            self.assertAllClose((bert_w_v - 0.1) / (non_bert_w_v - 0.1), [0.01, 0.01, 0.01], rtol=1e-2, atol=1e-2)
            self._cleanUp(tmp_out_dir)

if __name__ == "__main__":
    tf.test.main()


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

import random, math
import numpy as np
import codecs
import json
from collections import defaultdict

import torch

from xslu.utils import process_sent, process_word

import xslu.Constants as Constants

def seq2extend_ids(lis, word2idx):
    ids = []
    oovs = []
    for w in lis:
        if w in word2idx:
            ids.append(word2idx[w])
        else:
            if w not in oovs:
                oovs.append(w)
            oov_num = oovs.index(w)
            ids.append(len(word2idx) + oov_num)
    return ids, oovs

def value2ids(lis, word2idx):
    ids = []
    for w in lis:
        if w in word2idx:
            ids.append(word2idx[w])
        else:
            ids.append(Constants.UNK)
    return ids

def value2extend_ids(lis, word2idx, oovs):
    ids = []
    for w in lis:
        if w in word2idx:
            ids.append(word2idx[w])
        else:
            if w in oovs:
                ids.append(len(word2idx) + oovs.index(w))
            else:
                ids.append(Constants.UNK)
    return ids

class DADataset(object):

    def __init__(self, filename, memory, cuda, epoch_shuffle):
        self.filename = filename
        self.memory = memory
        self.cuda = cuda
        self.epoch_shuffle = epoch_shuffle
        self.datas = self.read_file(filename)
        self.data_len = len(self.datas)

        self.idx = 0
        self.indices = list(range(self.data_len))
        self.reset()

    @staticmethod
    def read_file(filename):
        with codecs.open(filename, 'r') as f:
            lines = f.readlines()
        lines = [line.split('\t<=>\t') for line in lines]
        datas = []
        for line in lines:
            utterance = line[0]
            triple = line[1]
            class_string = line[2]
            enc_lis = process_sent(class_string)
            dec_lis = process_sent(utterance)
            if len(enc_lis) > 0 and len(dec_lis) > 0:
                datas.append((utterance, class_string, triple))
        return datas

    @staticmethod
    def judge_utt_label(utterance, triple, class_string, memory, cuda):

        classes = triple.strip().split(';')
        new_string = class_string
        for cls in classes:
            lis = cls.strip().split('-', 2)
            if len(lis) == 3 and lis[2] in utterance:
                if random.random() > 0.5:
                    for word in lis[2].strip().split():
                        new_string = new_string.replace(word, 'unk')
        if new_string == class_string:
            return None
        else:
            lis = process_sent(new_string)
            word2idx = memory['enc2idx']
            ids = [word2idx[w] if w in word2idx else Constants.UNK for w in lis]
            data = torch.tensor(ids).view(1, -1)
            if cuda:
                data = data.cuda()
            return data

    @staticmethod
    def data_info(string, memory, cuda):

        lis = process_sent(string)
        if len(lis) == 0:
            raise Exception("Input string can not be empty string")

        word2idx = memory['enc2idx']
        ids = [word2idx[w] if w in word2idx else Constants.UNK for w in lis]
        data = torch.tensor(ids).view(1, -1)

        word2idx = memory['dec2idx']
        ids, oov_list = seq2extend_ids(lis, word2idx)
        enc_batch_extend_vocab_idx = torch.tensor(ids).view(1, -1)

        if len(oov_list) == 0:
            extra_zeros = None
        else:
            extra_zeros = torch.zeros((1, len(oov_list)))

        if cuda:
            data = data.cuda()
            enc_batch_extend_vocab_idx = enc_batch_extend_vocab_idx.cuda()
            if extra_zeros is not None:
                extra_zeros = extra_zeros.cuda()

        return data, None, extra_zeros, enc_batch_extend_vocab_idx, oov_list

    @staticmethod
    def label_info(string, memory, enc_oov_list, cuda):

        lis = process_sent(string)

        word2idx = memory['dec2idx']

        inp_ids = value2ids(lis, word2idx)
        out_ids = value2extend_ids(lis, word2idx, enc_oov_list)
        inp_ids = [Constants.BOS] + inp_ids
        out_ids = out_ids + [Constants.EOS]
        inp_ids = torch.tensor(inp_ids).view(1, -1)
        out_ids = torch.tensor(out_ids)

        if cuda:
            inp_ids = inp_ids.cuda()
            out_ids = out_ids.cuda()

        return inp_ids, out_ids

    def reset(self):
        self.idx = 0
        if self.epoch_shuffle:
            random.shuffle(self.indices)

    def __len__(self):
        return self.data_len

    def __iter__(self):
        return self

    def __next__(self):
        if self.idx >= self.data_len:
            self.reset()
            raise StopIteration

        utterance, class_string, triple = self.datas[self.indices[self.idx]]
        self.idx += 1

        enc_data, enc_length, extra_zeros, enc_batch_extend_vocab_idx, oov_list = \
                self.data_info(class_string, self.memory, self.cuda)
        dec_inp, dec_out = self.label_info(utterance, self.memory, oov_list, self.cuda)

        if self.epoch_shuffle:
            pad_data = self.judge_utt_label(utterance, triple, class_string, self.memory, self.cuda)
            if pad_data is not None:
                enc_data = pad_data

        return enc_data, enc_length, extra_zeros, enc_batch_extend_vocab_idx, oov_list, \
                dec_inp, dec_out


from commands import icommand

from log import whorkovlogger as LOGGER

logger = LOGGER.get_logger(__name__)

def get_cmd_class():
    return PingCmd()

class PingCmd(icommand.WhorkovCmd):
    def __init__(self):
        super(PingCmd, self).__init__()
        self.cmd_string_long = "ping"
        self.cmd_string_short = "p"

    async def execute_cmd(self, arg_str, message):
        await message.channel.send("pong!")


srcPts = np.array(  [[  -39,    73,  515],
                     [   52,  -132,  580],
                     [   49,   155,  500],
                     [   43,   185,  640],
                     [ -195,   100,  520],
                     [    3,    29,  505],
                     [  -23,   -31,  495],
                     [ -195,   -74,  500],
                     [  -25,  -140,  570]],
                     np.float32)

dstPts = np.array(  [[    1,   -15,   15],
                     [  -10,   -10,   10],
                     [    5,   -10,   15],
                     [    5,   -10,    6],
                     [    1,   -25,   15],
                     [   -2,   -13,   16],
                     [   -5,   -15,   15],
                     [   -7,   -25,   15],
                     [   11,   -14,   10]],
                     np.float32)

print("METHOD 1")
ret, M, mask = cv2.estimateAffine3D(srcPts, dstPts)
M = np.float32([M[0], M[1], M[2], [0 ,0, 0, 1]])

print("Matrix\n", M)
transformFnCv = lambda x: (M * np.vstack((np.matrix(x).reshape(3, 1), 1)))[0:3, :]
print(transformFnCv((-27,    65,  515)))
print(transformFnCv((52, -132, 580)))
print(transformFnCv((40, 95, 430)))

print(transformFnCv((-135, -86, 740)))

print("METHOD 2")
def solveAffine( src, dst):
    p1, p2, p3, p4 = (src[0], src[1], src[2], src[3])
    s1, s2, s3, s4 = (dst[0], dst[1], dst[2], dst[3])

    x = np.transpose(np.matrix([p1, p2, p3, p4]))
    y = np.transpose(np.matrix([s1, s2, s3, s4]))

    # add ones on the bottom of x and y
    x = np.vstack((x,[1,1,1,1]))
    y = np.vstack((y,[1,1,1,1]))

    # solve for A2
    A2 = y * x.I
    print(A2)
    # return function that takes input x and transforms it
    # don't need to return the 4th row as it is
    return lambda x: (A2 * np.vstack((np.matrix(x).reshape(3,1),1)))[0:3,:]

transformFn = solveAffine(srcPts, dstPts)

# print(np.matrix(dstPts[0]).T - transformFn(srcPts[0]))

print(transformFn((-27,    65,  515)))
print(transformFn((52, -132, 580)))
print(transformFn((40, 95, 430)))


import threading
from functools import lru_cache

from basic_library import rwlock
from basic_library import xdata
from basic_library import xfunctions
from basic_library import xlogging

_logger = xlogging.getLogger(__name__)

_storage_reference_manager = None
_storage_reference_manager_locker = threading.Lock()

class StorageReferenceManager(object):
    """快照存储引用管理器

    使用中（含逻辑上将要使用）的快照存储进行统一管理
    提供查询接口，支持业务逻辑获知 某快照 或 某文件 当前是否被引用
    引用有两类：1. 读取 2. 写入
    """

    @staticmethod
    def get_storage_reference_manager():
        global _storage_reference_manager

        if _storage_reference_manager is None:
            with _storage_reference_manager_locker:
                if _storage_reference_manager is None:
                    _storage_reference_manager = StorageReferenceManager()
        return _storage_reference_manager

    class Record(object):
        def __init__(self, storage_info):
            self.storage_ident = storage_info['disk_snapshot_storage_ident']
            self.storage_path = storage_info['image_path']
            self.timestamp = xfunctions.current_timestamp()

        def __str__(self):
            return f'{xfunctions.humanize_timestamp(self.timestamp)}|{self.storage_path}|{self.storage_ident}'

        def __repr__(self):
            return self.__str__()

    def __init__(self):
        self.reading_record_dict = dict()
        self.rr_locker = rwlock.RWLockWrite()
        self.writing_record_dict = dict()
        self.wr_locker = rwlock.RWLockWrite()

    def add_reading_record(self, caller_name: str, storage_info_list: list):
        assert caller_name
        with self.rr_locker.gen_wlock():
            assert caller_name not in self.reading_record_dict
            self.reading_record_dict[caller_name] = [self.Record(storage_info) for storage_info in storage_info_list]
            self.is_storage_using.cache_clear()

    def remove_reading_record(self, caller_name: str):
        assert caller_name
        with self.rr_locker.gen_wlock():
            if self.reading_record_dict.pop(caller_name, None):
                self.is_storage_using.cache_clear()

    def add_writing_record(self, caller_name: str, storage_info: dict):
        assert caller_name
        with self.wr_locker.gen_wlock():
            assert caller_name not in self.writing_record_dict
            for record in self.writing_record_dict.values():
                if record.storage_path == storage_info['image_path']:
                    xlogging.raise_and_logging_error(
                        '快照镜像文件正在写入中', f'repeat add writing storage ref : {record}',
                        print_args=False, exception_class=xdata.StorageReferenceRepeated)
            self.writing_record_dict[caller_name] = self.Record(storage_info)
            self.is_storage_using.cache_clear()
            self.is_storage_writing.cache_clear()

    def remove_writing_record(self, caller_name: str):
        assert caller_name
        with self.wr_locker.gen_wlock():
            if self.writing_record_dict.pop(caller_name, None):
                self.is_storage_using.cache_clear()
                self.is_storage_writing.cache_clear()

    @lru_cache(None)
    def is_storage_using(self, storage_ident):
        with self.rr_locker.gen_rlock():
            for record_list in self.reading_record_dict.values():
                for record in record_list:
                    if record.storage_ident == storage_ident:
                        return True
        with self.wr_locker.gen_rlock():
            for record in self.writing_record_dict.values():
                if record.storage_ident == storage_ident:
                    return True
        return False

    @lru_cache(None)
    def is_storage_writing(self, storage_path):
        with self.wr_locker.gen_rlock():
            for record in self.writing_record_dict.values():
                if record.storage_path == storage_path:
                    return True
        return False