# windows C:\Windows\System32\drivers\etc
# Mac and Linus /etc/hosts
# Only administration can access the hosts document

hosts_temp = 'hosts'
hosts_path = 'C:\Windows\System32\drivers\etc'
# hosts_path = 'C:\n Windows\System32\drivers\etc' \n a new row, sometimes it doesn't work
# hosts_path = r'C:\n Windows\System32\drivers\etc'  add a r - road

redirect = '127.0.0.1'
website_list = ['www.factbook.com','facebook.com']

import time
from datetime import datetime as dt

while True:
    if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 16):
        #print ('Working hours...')
        with open(hosts_path,'r+') as file:
            content = file.read()
            for website in website_list:
                if website in content:
                    pass
                else:
                    file.write(redirect + ' ' + website + '\n')
    else:
        with open(hosts_path,'r+') as file:
            content = file.readlines()
            # Circle back to the top, when truncate, it will take all the bottom part out
            file.seek(0)
            for line in content:
                if not any(website in line for website in website_list):
                    file.write(line)
            file.truncate()
    time.sleep(5)

Application 3 Create a Web Blocker/Websiteblocker.py


def solution_function(num1, *other_nums):
    """
    This python program solves the "List Replication" problem on
    HackerRank, but it stil needs to be translated to Racket.
    """

    for args in other_nums:

        for i in range(num1):
            print(args)

solution_function(3, 1, 2, 3, 4)


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/hugoruscitti/Aptana Studio 3 Workspace/quickdiagrams/quickdiagrams/gtkclient/drawing_thread.py
# Compiled at: 2011-01-24 11:46:37
import time
from threading import Thread
import gtk

class DrawingThread(Thread):
    """Representa el componente del programa que dibuja los diagramas.

    Este componente actua de manera desacoplada por medio de un hilo, para
    no interferir en la interfaz de usuario gtk."""

    def __init__(self, main, queue):
        Thread.__init__(self)
        self.queue = queue
        self.main = main

    def run(self):
        while True:
            task = self.queue.get()
            if task:
                task()
                self.queue.task_done()
            else:
                return


import os
import random
import inflect

path = '/scratch/aalibhai/LRS3_lip'
apath = '/scratch/aalibhai/pretrain'
corpus = '/home/aalibhai/Thesis/LipNet-CTCWBS/LRS3_corpus.txt'
train_list = '/home/aalibhai/Thesis/LipNet-CTCWBS/data/LRS3_train.txt'
val_list = '/home/aalibhai/Thesis/LipNet-CTCWBS/data/LRS3_val.txt'
vids = []
txt = []
vids_val = []
p = inflect.engine()
count = 0
print('making file list')
for speaker in os.listdir(path):
    for vid in os.listdir(os.path.join(path, speaker)):
        if len(os.listdir(os.path.join(path, speaker, vid))) > 0:
            lines= []
            with open(os.path.join(apath, speaker, vid + '.txt')) as f:
                lines = [line.strip().split(' ') for line in f.readlines()]
                lines = lines[4:]
                txt = [line[0].lstrip() for line in lines if float(line[2]) < 3.0]
                txt = list(filter(lambda s: not s.upper() in ['{NS}', '{LG}'], txt))
                for i, _ in enumerate(txt):
                    if not txt[i].isalpha():
                        txt[i] = p.number_to_words(txt[i]).upper().replace('-', ' ').replace(',', '')

                if len(txt) > 0 and sum(len(i) for i in txt) <= 75 and sum(len(i) for i in txt) > 0:
                    vids.append(os.path.join(speaker, vid))
        count += 1
        if count % 1000 == 0:
            print(count, end='\r')

count = 0

print('splitting train/validation')
num = range(len(vids)//10)
for i in num:
    vids_val.append(vids.pop(random.randrange(len(vids))))

print('writing train list')
count = 0
l = len(vids)
with open(train_list, 'w') as t:
    for file in vids:
        t.write(file + '\n')
        count += 1
        if count % 100000 == 0:
            print('{}/{}'.format(count, l), end='\r')

print('writing validation list')
count = 0
l = len(vids_val)
with open(val_list, 'w') as v:
    for file in vids_val:
        v.write(file + '\n')
        count += 1
        if count % 100000 == 0:
            print('{}/{}'.format(count, l), end='\r')


import unittest

import numpy as np
from .patch import find_patch_start_end_padding, extract_patch, find_masked_patch_center_indices, \
    sample_uniform_center_index, get_random_patch

class TestPatch(unittest.TestCase):
    def setUp(self):
        self.x = np.array([[1, 2, 2, 3],
                           [2, 3, 3, 2],
                           [2, 3, 5, 2],
                           [4, 2, 2, 5]])
        self.patch_size = np.array([4, 3])
        self.spatial_dims = [0, 1]

    def test_find_patch_start_end_padding(self):
        shape = np.array([3, 12, 13, 14])
        spatial_patch_size = np.array([4, 5, 7])
        spatial_center = np.array([1, 1, 1])
        start, end, padding = find_patch_start_end_padding(
            shape, spatial_center_idx=spatial_center,
            spatial_patch_size=spatial_patch_size, spatial_dims=[-3, -2, -1]
        )
        np.testing.assert_equal(start, [0, 0, 0, 0])
        np.testing.assert_equal(end, [3, 3, 4, 5])
        np.testing.assert_equal(padding, [[0, 0], [1, 0], [1, 0], [2, 0]])

    def test_extract_patch_no_padding(self):
        x = extract_patch(self.x, spatial_center_idx=[2, 2], spatial_patch_size=self.patch_size,
                          spatial_dims=self.spatial_dims)

        self.assertSequenceEqual(x.shape, (4, 3))
        np.testing.assert_array_equal(x, np.array([
            [2, 2, 3],
            [3, 3, 2],
            [3, 5, 2],
            [2, 2, 5]]
        ))

    def test_extract_padding(self):
        x = extract_patch(self.x, spatial_center_idx=np.array([3]), spatial_patch_size=np.array([3]),
                          spatial_dims=[-1], padding_values=np.array([[1], [2], [3], [4]]))

        self.assertSequenceEqual(x.shape, (4, 3))
        np.testing.assert_array_equal(x, np.array([
            [2, 3, 1],
            [3, 2, 2],
            [5, 2, 3],
            [2, 5, 4]
        ]))

    def test_get_uniform_center_index(self):
        for _ in range(1000):
            c = sample_uniform_center_index(np.array(self.x.shape), spatial_patch_size=self.patch_size,
                                            spatial_dims=self.spatial_dims)
            self.assertTrue((list(c) == [2, 1]) or (list(c) == [2, 2]))

    def test_get_conditional_center_indices(self):
        c = find_masked_patch_center_indices(self.x > 4, patch_size=np.array([2, 3]))
        np.testing.assert_array_equal(c, [[2, 2]])

class TestRandomPatch(unittest.TestCase):
    def test_no_spatial_dims(self):
        shape = (3, 4, 10)
        x = np.empty(shape)
        patch = get_random_patch(x, [2, 2])
        self.assertEqual(patch.shape, (3, 2, 2))

    def test_spatial_dims(self):
        shape = (3, 4, 10)
        x = np.empty(shape)
        patch = get_random_patch(x, [2, 2], spatial_dims=[0, 2])
        self.assertEqual(patch.shape, (2, 4, 2))


import torch
import torch.nn as nn

from ..base_module import BaseModule
from .feed_forward import FeedForward
from ..utils import get_subsequent_mask
from .multihead_attn import MultiHeadAttention

@BaseModule.register("transformer_block")
class TransformerBlock(BaseModule):
    def __init__(self, num_heads, num_features):
        """
        Args:
            num_heads (int): The number of heads.

            num_features (int): Dimensions of keys(K) features and query(Q) features.
        """

        super(TransformerBlock, self).__init__()

        self.num_heads = num_heads
        self.num_features = num_features

        self.attn = MultiHeadAttention(self.num_heads, self.num_features)
        self.attn_norm = nn.LayerNorm(num_features)

        self.ff = FeedForward(self.num_features, 4 * self.num_features)
        self.ff_norm = nn.LayerNorm(num_features)

    def forward(self, queries, keys, values, attn_mask=None, non_pad_mask=None):
        """
        Args:
            queries (torch.Tensor): Of shape (batch, query_len, query_dim),
                usually the last hidden state of a decoder.

            keys (torch.Tensor): Of shape (batch, key_len, key_dim), where compute
                weights according to query.

            values (torch.Tensor): Of shape (batch, value_len, value_dim),
                values information which query will `attened`. Each value should correspond
                to a key, which means that value_len should be equal to key_len.

            attn_mask (torch.Tensor): Of shape (batch, query_len, value_len), attention mask.

        Returns:
            output (torch.Tensor): Of shape (batch, query_len, value_dim or query_dim),
                weighted sum of values or projection of concatenation of [weighted_sum + query]

            weights (torch.Tensor): Of shape (batch, query_len, value_len),
                attention weights.
        """

        outputs, weights = self.attn(queries, keys, values, attn_mask)
        outputs = self.attn_norm(queries + outputs)

        # masked out padding values
        if non_pad_mask is not None:
            outputs *= non_pad_mask

        forwarded = self.ff(outputs)
        outputs = self.ff_norm(forwarded + outputs)

        # masked out padding values
        if non_pad_mask is not None:
            outputs *= non_pad_mask

        return outputs, weights

@BaseModule.register("transformer_decoder_block")
class TransformerDecoderBlock(BaseModule):
    def __init__(self, num_heads, num_features):
        """
        Args:
            num_heads (int): The number of heads.

            num_features (int): Dimensions of keys(K) features and query(Q) features.
        """

        super(TransformerDecoderBlock, self).__init__()
        self.num_heads = num_heads
        self.num_features = num_features

        self.self_attn = MultiHeadAttention(self.num_heads, self.num_features)
        self.self_norm = nn.LayerNorm(num_features)

        self.block = TransformerBlock(self.num_heads, self.num_features)

    def forward(self, queries, keys, values, attn_mask=None, non_pad_mask=None):
        """
        Args:
            queries (torch.Tensor): Of shape (batch, query_len, query_dim), this
                will be used for self-attention in first layer.

            keys (torch.Tensor): Of shape (batch, key_len, key_dim), where compute
                weights according to query, should be the outputs of encoder.

            values (torch.Tensor): Of shape (batch, value_len, value_dim),
                values information which query will `attened`. Each value should correspond
                to a key, which means that value_len should be equal to key_len,
                should be the outputs of encoder.

            attn_mask (torch.Tensor): Of shape (batch, query_len, value_len), attention mask.

        Returns:
            output (torch.Tensor): Of shape (batch, query_len, value_dim or query_dim),
                weighted sum of values or projection of concatenation of [weighted_sum + query]

            weights_list (list[torch.Tensor]): A list of weights, each shape is (batch, query_len, value_len),
                attention weights list.
        """
        attn_weights = []
        self_attn_mask = get_subsequent_mask(queries)
        if attn_mask is not None:
            self_attn_mask = attn_mask + self_attn_mask

        self_attnded, self_weights = self.self_attn(
            queries, queries, queries, self_attn_mask
        )

        # masked out padding values
        if non_pad_mask is not None:
            self_attnded *= non_pad_mask

        attn_weights.append(self_weights)
        self_attnded = self.self_norm(queries + self_attnded)

        outputs, weights = self.block(
            self_attnded, keys, values, attn_mask, non_pad_mask
        )
        attn_weights.append(weights)

        return outputs, attn_weights


'''
    Programa feito por Gabriel Valentin Tiburcio,
    aluno da graduação em Sistemas de Informação na UFU.
    Pesquisa "Exploração de Estratégias para Mineração de Expertise",
    orientada pela professora Maria Camila Nardini Barioni

    -----------------------------------------------------------------

    Programa realiza extração do Abstract(resumo) das pesquisas contidas
    nas páginas Web do IEEE, e extração das Keywords das pesquisas
    encontradas no BIBTEX dessa mesma página. Depois o conteúdo encontrado
    é armazenado em um arquivo denominado "registroIEEE.txt", onde será lido
    posteriormente pelo programa "03-CarregaBancoMySQL.py".

    O site segue o padrão desse exemplo a seguir:
        https://www.computer.org/csdl/proceedings-article/2011/iv/12OmNxzMnU0/12OmNxymo4x
'''

import re
import time

# Importar a classe para abrir o navegador e usar suas funções
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

# Importar a classe que contém as funções e aplicar um alias
from selenium.webdriver.support import expected_conditions as EC
# Importar classe para ajudar a localizar os elementos
from selenium.webdriver.common.by import By
# Importar a classe ActionChains responsável pelas manipulações
from selenium.webdriver.common.action_chains import ActionChains

# Função p/ extrair as keywords dos inproceedings
def pegakeywords(conteudo,titulo,autor,abstract,arq,url):
    aux1 = conteudo.replace('Keywords', '')
    aux1 = aux1.replace('\n','')
    lista_keywords = aux1.split(',')

    aux2 = abstract.replace('Abstract','')
    aux2 = aux2.replace('\n','')

    texto = []
    texto.append('Titulo{')
    texto.append(titulo)
    texto.append('}\n')
    texto.append('Autor{')
    texto.append(autor)
    texto.append('}\n')
    texto.append('Keywords{')
    for j in lista_keywords:
        k=j.rjust(0).strip()
        texto.append(k)
        texto.append(';')
    texto.append('}\n')
    texto.append('Abstract{')
    texto.append(aux2)
    texto.append('}\n')
    texto.append('URL{')
    texto.append(url)
    texto.append('}\n')

    arq.writelines(texto)

# Início do Programa
inicio = time.time()

arquivo = open('urls_IEEE-Article.txt', 'r')

# Lê linhas do arquivo.
conteudo = arquivo.readlines()
arquivo.close()

conteudo_separado_por_linha = []
titulo = []
autor = []
url = []

# Retira "\n" e "}" das linhas.
for linha in conteudo:
    linha = linha.replace('}\n', '')
    conteudo_separado_por_linha.append(linha)

for x in conteudo_separado_por_linha:
    if re.search('Titulo{', x, re.IGNORECASE):
        titulo.append(x.replace('Titulo{',''))
    elif re.search('Autor{', x, re.IGNORECASE):
        autor.append(x.replace('Autor{',''))
    elif re.search('URL{', x, re.IGNORECASE):
        url.append(x.replace('URL{',''))

# Caminhos p/ encontrar os Abstracts e Keywords dos Inproceedings no site do IEEE
caminho_abstract = '/html/body/csdl-app/div/csdl-periodical-details/div/csdl-periodical-article/div/div/div/div/div/csdl-article-full-text/div'
caminho_aux = '/html/body/csdl-app/div/csdl-periodical-details/div/csdl-periodical-article/div/div/div/div/div/csdl-periodical-article-metadata/div'

padrao_not_found_url_ieee = 'https://www.computer.org/csdl/not-found'

cont_inproceedings = 0 # contador de inproceedings
cont_not_found = 0 # contador de páginas não encontradas
cont_erro_pag = 0 # contador de páginas que não foi possível pegar o conteúdo
cont = 0

pag_erro = []
pag_not_found = []

arq = open('registro_IEEE-Article.txt', 'w')

for u in url:

    options = webdriver.ChromeOptions()
    options.add_argument('headless')
    options.add_argument('window-size=1920x1080')

    driver = webdriver.Chrome(chrome_options=options)
    wait = WebDriverWait(driver,30)

    driver.get(u)

    if padrao_not_found_url_ieee in driver.current_url:
        pag_not_found.append(u)
        cont_not_found+=1
        cont+=1
        driver.quit()
    else:
        try:
            elemento = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'csdl-app')))

            nav_pega_abstract = driver.find_element_by_xpath(caminho_abstract)

            abstract = nav_pega_abstract.text

            nav_para_keyword = driver.find_element(By.XPATH, caminho_aux)
            pre = nav_para_keyword.find_element_by_class_name('mt')

            pegakeywords(pre.text,titulo[cont],autor[cont],abstract,arq,u)

            driver.quit()
            cont_inproceedings+=1
            cont+=1
        except:
            pag_erro.append(u)
            cont_erro_pag+=1
            cont+=1
            driver.quit()

arq.close()

if(len(pag_erro)>0):
    arq = open('PaginasComErroIEEE.txt', 'w')
    texto = []
    texto.append('Páginas que não foi possível pegar o conteúdo\n')
    for j in pag_erro:
        texto.append(j)
        texto.append('\n')
    texto.append('Páginas com ERRO 404 Not Found\n')
    for k in pag_not_found:
        texto.append(k)
        texto.append('\n')
    arq.writelines(texto)
    arq.close()

print('\n---------------------------- INFORMACOES ADICIONAIS ---------------------------')
print("\nNUMERO DE INPROCEEDINGS MINERADOS: ",cont_inproceedings)
print("\nNUMERO DE PAGINAS NAO ENCONTRADAS: ",cont_not_found)
print("\nNUMERO DE PAGINAS COM ERRO DE EXTRAÇÃO DE CONTEUDO: ",cont_erro_pag)
fim = time.time()
print("\nTEMPO DE EXECUCAO: ",fim - inicio,"s")


import tempfile
import os

import pytest

from totosca import template

def test_adaptor_init():
    # Setup
    temp = tempfile.NamedTemporaryFile(dir=os.getcwd(), mode='w', suffix=".py")
    temp.write("class Adaptor():\n\tpass")
    temp.seek(0)
    temp_name = temp.name.replace(os.getcwd(),'').strip('/py').strip('.')

    # Test for a couple of exceptions
    with pytest.raises(ModuleNotFoundError):
        template.adaptor_init(temp_name + 'x.Adaptor')

    with pytest.raises(AttributeError):
        template.adaptor_init(temp_name + '.Adaptorx')

    # Test everything works
    try:
        adaptor = template.adaptor_init(temp_name + '.Adaptor')
    except Exception:
        pytest.fail("Could not init the adaptor")

    # Teardown
    temp.close()

def test_write_new_tosca():
    # Setup
    temp_in = tempfile.NamedTemporaryFile(dir=os.getcwd(), mode='w',)
    temp_out = tempfile.NamedTemporaryFile(dir=os.getcwd(), mode='w+')
    temp_in.write("my_var: <MYVAR>")
    temp_in.seek(0)

    # All working as expected
    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVAR": "12345"})
    temp_out.seek(0)
    assert "my_var: 12345\n" in temp_out.readline()
    assert "This TOSCA template has been filled" in temp_out.readline()
    assert "!KEYNOTFOUND error" not in temp_out.read()

    # Few tests for exceptional cases
    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVAR": None})
    temp_out.seek(0)
    assert "!KEYNOTFOUND\n" in temp_out.readline()
    assert "!KEYNOTFOUND error" in temp_out.read()

    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVAR": [1, 2, 3]})
    temp_out.seek(0)
    assert "[1, 2, 3]\n" in temp_out.readline()

    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVAR": {"ENV": "A"}})
    temp_out.seek(0)
    assert "{'ENV': 'A'}\n" in temp_out.readline()

    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVAR": 123})
    temp_out.seek(0)
    assert "123\n" in temp_out.readline()

    template.write_new_tosca(temp_in.name, temp_out.name, {"MYVXX": "12345"})
    temp_out.seek(0)
    assert "<MYVAR>\n" in temp_out.readline()

    # Tear down
    temp_in.close()
    temp_out.close()


import urllib.request as req

imgUrl = "https://cafeptthumb-phinf.pstatic.net/MjAyMDEwMzFfNTcg/MDAxNjA0MTQ2Mzg2NDM0.-4DLkT_jvdE20K-G2d6gWiUVDY2poOzm5NCvGUXiSocg.STeRWgTKj7fTu4Wi8sQFA9Y9YevMyIOXS3uh5g7kpcAg.JPEG/20201031%EF%BC%BF211200.jpg?type=w1600"
htmlUrl = 'https://www.naver.com'

savePath1 = 'iphone12.jpg'
savepath2 = 'main.html'

file1, header1 = req.urlretrieve(imgUrl,savePath1)
file2, header2 = req.urlretrieve(htmlUrl,savepath2)

#예외처리
# try:
#     file1, header1 = req.urlretrieve(imgUrl,savePath1)
#     file2, header2 = req.urlretrieve(htmlUrl,savepath2)
# except Exception as e:
#     print('Download success')
#     print(e)
# else:
#     #Header정보 출력
#     print(header1)
#     print('-------------------------')
#     print(header2)
#     #다운로드된 파일 정보
#     print('FileName1 {}'.format(file1))
#     print('FileName2 {}'.format(file2))
#     print()

print('Dwonload sucess')


from rest_framework_nested import routers
from django.conf.urls import url
from .views import *

router = routers.SimpleRouter()
router.register(r'admin/users',
    viewset=AdminUserProfileViewSet,
    base_name='admin/user')

router.register(r'admin/pending_registrations',
    viewset=AdminPendingRegistrationViewSet,
    base_name='admin/pending-registration')

router.register(r'admin/registrations',
    viewset=AdminRegistrationViewSet,
    base_name='admin/registration')

router.register(r'admin/courts',
    viewset=AdminCourtViewSet,
    base_name='admin/court')

router.register(r'admin/tournaments',
    viewset=AdminTournamentViewSet,
    base_name='admin/tournament')

router.register(r'admin/groups',
    viewset=AdminGroupViewSet,
    base_name='admin/groups')

router.register(r'admin/ungrouped_registrations',
    viewset=AdminUngroupedRegistrationViewSet,
    base_name='admin/ungrouped-registrations')

router.register(r'admin/categories',
    viewset=AdminCategoriesViewSet,
    base_name='admin/category')

router.register(r'admin/active_registrations',
    viewset=AdminListActiveRegistrationViewSet,
    base_name='admin/active-registration')

# Read this for nested-routers: https://github.com/alanjds/drf-nested-routers

# Scoped to a specific category
# /tournaments/tournament_pk/...
categories_router = routers.NestedSimpleRouter(router, r'admin/categories', lookup='category')

categories_router.register(r'groups',
    viewset=AdminGroupViewSet,
    base_name='admin/category-group')

categories_router.register(r'ungrouped_registrations',
    viewset=AdminUngroupedRegistrationViewSet,
    base_name='admin/category-ungrouped-registration')

categories_router.register(r'knock_offs',
    viewset=AdminKnockOffMatchesViewSet,
    base_name='admin/category-knock-off-match')

categories_router.register(r'knock_offs_pairs',
    viewset=AdminKnockOffClassedPairs,
    base_name='admin/category-knock-off-pairs')

categories_router.register(r'pending_registrations',
    viewset=AdminPendingRegistrationViewSet,
    base_name='admin/category-pending-registration')

categories_router.register(r'registrations',
    viewset=AdminRegistrationViewSet,
    base_name='admin/category-registrations')

urlpatterns = [
    url(r'^admin/groups/(?P<pk>[0-9]+)/scores/$', AdminGroupScoresViewSet.as_view({'get': 'retrieve', 'post': 'update'})),
    url(r'^admin/current_tournaments_and_categories/$', AdminGetCurrentTournamentAndCategory.as_view(), name='admin/current-tournaments-and-categories'),
    url(r'^admin/available_courts_for_group/(?P<category_pk>[0-9]+)/$', AdminListNonAttributedCourtForGroups.as_view(), name='admin/available-courts-for-group'),
    url(r'^admin/list_users/(?P<user_type>[a-z]+)/$', AdminListUsers.as_view(), name='admin/list-users'),
    url(r'^admin/pending_registrations_matching/$', AdminPendingRegistrationsMatching.as_view(), name='admin/pending-registrations-matching'),
    url(r'^admin/full_tournaments/$', AdminFullTournamentView.as_view(), name='admin/full-tournament'),
    url(r'^admin/tournaments_and_categories_typology/$', AdminGetTournamentAndCategoryTypology.as_view(), name='admin/tournaments-and-categories-typology'),
    url(r'^admin/pending_registrations/(?P<pk>[0-9]+)/make_solo/$', AdminPendingRegistrationsSoloView.as_view()),
    url(r'^admin/email_players_for_date/$', AdminEmailPlayersForDateView.as_view(), name='admin/email-players-for-date'),
    url(r'^admin/email_court_owners/$', AdminEmailCourtOwnersView.as_view(), name='admin/email-court-owners'),
    url(r'^admin/automatic_group_creation_for_category/(?P<category_pk>[0-9]+)/$', AdminAutomaticGroupCreationForCategoryView.as_view(), name='admin/automatic-group-creation-for-category'),
    url(r'^admin/list_court_available_for_day/(?P<category_pk>[0-9]+)/$', AdminListCourtAvailableForDayView.as_view(), name='admin/list-court-available-for-day'),
    url(r'^admin/pdf_for_group/(?P<group_pk>[0-9]+)/$', AdminPdfForGroupView.as_view(), name='admin/pdf-for-group'),
    url(r'^admin/courts_not_confirmed_yet/$', AdminCourtsNotConfirmedYet.as_view(), name='admin/courts-not-confirmed-yet'),
    url(r'^admin/generate_csv_addresses_for_user_type/(?P<user_type>[a-z]+)/$', GenerateCSVWithAddressesForUserType.as_view(), name='admin/generate-csv-addresses-for-user-type'),
    url(r'^admin/automatic_pending_registration_matching_for_category/(?P<category_pk>[0-9]+)/$', AutomaticPendingRegistrationMatchingForCategory.as_view(), name='admin/automatic-pending-registration-matching-for-category'),
    url(r'^admin/managed_categories_for_admin/$', GetManagedCategoriesForAdmin.as_view(), name='admin/managed-categories-for-admin'),
    url(r'^admin/summary_ordered_extras_and_registrations/$', GetSummaryOrderedExtrasAndRegistrations.as_view(), name='admin/summary-ordered-extras-and-registrations'),
    url(r'^admin/close_current_tournament/$', CloseCurrentTournament.as_view(), name='admin/close-current-tournament'),
    url(r'^admin/automatic_knock_off_generation/(?P<category_pk>[0-9]+)/$', AutomaticKnockOffGeneration.as_view(), name='admin/automatic-knock-off-generation'),
    url(r'^admin/change_manager_category/(?P<category_pk>[0-9]+)/$', ModifyCategoryManager.as_view(), name='admin/change-manager-category'),
    url(r'^admin/generate_csv_summary_tournament/$', GenerateCSVCurrentSummaryTournament.as_view(), name='admin/generate-csv-summary-tournament'),
]

admin_urls = router.urls + urlpatterns + categories_router.urls


import pickle
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt

species_codes = {1: 'Pst', 2: 'Pst', 3: 'Pgr', 4: 'Pgr', 6: 'Pst',
                 7: 'Aru', 9: 'Aru', 11: 'Aru', 12: 'Pst', 14: 'Pst',
                 17: 'Pst', 19: 'Pst', 21: 'Aru', 22: 'Pgr', 24: 'Aru',
                 26: 'Aru', 27: 'Pgr', 28: 'Aru', 29: 'Aru', 31: 'Bpa',
                 32: 'Bpa', 34: 'Pgr', 35: 'Bpa', 38: 'Bpa', 41: 'Bpa'}
species_codes = [y+'_'+str(x) for x, y in species_codes.items()]
species_name_dict = {'Aru': ['Red maple', 'Acer rubrum'],
                     'Bpa': ['Paper birch', 'Betula papyrifera'],
                     'Pgr': ['Bigtooth aspen', 'Populus grandidentata'],
                     'Pst': ['White pine', 'Pinus strobus']}

df = None
for species_code in species_codes:
    if not os.path.exists('../../Data/UMB_trace/Gil_v2/test/{}.pickle'\
                          .format(species_code)):
        continue
    ts = pickle.load(open('../../Data/UMB_trace/Gil_v2/test/{}.pickle'\
                          .format(species_code), 'rb'))['c']
    temp = pd.DataFrame(ts, columns=['c'])
    temp['species_code'] = species_code
    temp['Species'] = species_name_dict[species_code[:3]][0]
    if df is None:
        df = temp
    else:
        df = pd.concat([df, temp])
df = df.sort_values(by=['species_code'])

# figure
plt.figure(figsize=(20, 10))
sns.set(font_scale=2)
g = sns.violinplot(x='species_code', y='c', hue='Species', data=df,\
                cut=0, dodge=False)
g.set(xticklabels=[])
g.set(xlabel=None)
g.tick_params(bottom=False)
g.set_ylabel(ylabel='$c$')
g.set(ylim=(0, 40))
g.legend(title='Species', loc='upper center', title_fontsize=25, ncol=4)
fig = g.get_figure()


#--------------Start of Imports--------------
from sty import fg, bg, ef, rs, RgbFg

import os

def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

# create
checklist = list()

#--------------Start Methods--------------
def user_input(prompt):
    user_input = input(prompt)
    print(fg.rs+bg.rs)
    return user_input

# (P)rint All the items in the list
def list_all_items():
    print(bg.green, fg.red, "**--List All Items--**", fg.rs+bg.rs)
    if len(checklist) == 0: print("No Items in List")
    else:
        index = 0
        for list_item in checklist:
            print("{}: {}".format(index+1, list_item))
            index += 1
    print()

# (L)ook at an item in the list
def read(index):
# Catch for Index Out of Bounds Error
    try:
        checklist[index-1]
    except IndexError:
        return(fg.red+bg.black+ "Error Index: " + str(index) + " out of bounds \n"+ fg.rs + bg.rs)
    return(fg.da_magenta+bg.blue + checklist[index-1] +" is in position "+ str(index) + fg.rs+bg.rs + "\n")

# (A)dd an item to the list
def add(item):
    checklist.append(item)

# (D)estroy item in the list
def destroy(index):
    # Catch for Index Out of Bounds Error
    try:
        destroy = checklist.pop(index-1)
    except IndexError:
        return(fg.red+bg.black+ "Error Index: " + str(index) + " out of bounds \n"+ fg.rs + bg.rs)
    print("Item: " + destroy + " @ " + str(index) + " has been Deleted")
    return True

# (U)pdate an item in the list
def update(index, item):
    # Catch for Index Out of Bounds Error
    try:
        checklist[index-1] = item
    except IndexError:
        return(fg.red+bg.black+ "Error Index: " + str(index) + " out of bounds \n"+ fg.rs + bg.rs)
    print("Item: " + str(index) + " has been updated to: " + checklist[index-1])
    return True

# (M)ark the indexed item as complete
def mark_completed(index):
    # Catch for Index Out of Bounds Error
    try:
       update(index-1, "√ " + checklist[index-1])
       list_all_items()
    except IndexError:
        return(fg.red+bg.black+ "Error Index: " + str(index) + " out of bounds \n"+ fg.rs + bg.rs)
        print("Item: " + str(index) + " has been updated to: " + checklist[index-1])
    return True

# (R)emove a mark from an item
def remove_mark(index):
    try:
        checklist[index-1].replace("√ ", "")
    except ValueError:
        print(bg.black, fg.red, "Item not Checked", fg.rs+bg.rs)
    #except AttributeError:
    #    call_security("some_list not quacking like a list!")
    # ^Insperation from https://stackoverflow.com/questions/4915920/how-to-delete-an-item-in-a-list-if-it-exists
    except IndexError:
        return(fg.red+bg.black+ "Error Index: " + str(index) + " out of bounds \n"+ fg.rs + bg.rs)
        print("Item: " + str(index) + " has been updated to: " + checklist[index-1])
    list_all_items()
    return True

def select(function_code):
    # Print all items
    if function_code.upper() == "P":
        list_all_items()
        return True

    # Read item
    elif function_code.upper() == "L":
        item_index = user_input("Index Number? ")

        print(read(int(item_index)))
        return True

    # Add item
    elif function_code.upper() == "A":
        item = user_input("Input item: ")
        add(item)
        return True

    # delete item
    elif function_code.upper() == "D":
        item_index = user_input("Index Number? ")
        destroy(int(item_index))
        return True

    #update item
    elif function_code.upper() == "U":
        item_index = int(user_input("Index Number? "))
        replacement = user_input("What do you want to update it to: ")
        update(item_index, replacement)
        return True

    #Mark Completed
    elif function_code.upper() == "M":
        item_index = int(user_input("Index Number? "))
        mark_completed(item_index)
        return True

    #Remove mark
    elif function_code.upper() == "R":
        item_index = int(user_input("Index Number? "))
        remove_mark(item_index)
        return True

    # Quit Selector
    elif function_code.upper() == "Q":
        return False

    elif function_code.upper() == "C":
        clear()
        return True

    # Catch all
    else:
        print(bg.black, fg.red, "Unknown Option", fg.rs+bg.rs)
        return True
#--------------End Methods--------------

#--------------Start of Display/Testing--------------
# test Function
"""def test():
    print(bg.green, fg.red, "Create and Display a new List", fg.rs+bg.rs)
    #create demo list
    add("purple sox")
    add("red cloak")
    add("Bananas")
    #display demo list
    print(fg.purple, read(0))
    print(fg.red, read(1))
    print(fg.yellow, read(2))
    print(fg.rs+bg.rs)
    # Update demo list
    update(0, "purple socks")
    destroy(2)
    print(bg.green, fg.red, "Update the First Item, Remove the Third Item, and the Display the List again", fg.rs+bg.rs)
    print(fg.purple, read(0))
    print(fg.red, read(1))
    print(fg.rs+bg.rs)
    #Demo adding a check
    print(bg.green, fg.red, "**--Add a Check Mark for Completion--**", fg.rs+bg.rs)
    mark_completed(int(user_input("Enter an Index: ")))
    #Demo Removing the Check
    print(bg.green, fg.red, "**--Remove a Check Mark if not Complete--**")
    print(fg.rs+bg.rs)
    remove_mark(int(user_input("Enter the same Index: ")))
"""
# display/Run
#print(bg.green, fg.red, "**--Testing--**", fg.rs+bg.rs)
#test()

print(bg.green, fg.red, "Run through selector", fg.rs+bg.rs)
running = True
while running:
    print("Your Options Are: \n" +
        fg.green, "(P)rint the entire list, \n" +
        fg.blue, "(L)ook at an Item by it's Index value, \n" +
        fg.red, "(A)dd an Item, \n" +
        fg.cyan, "(D)estroy an Item, \n" +
        fg.yellow, "(U)ppdate an Item, \n" +
        fg.rs+bg.rs, "(M)ark an Item complete, \n" +
        fg.blue, "(R)emove a check mark, or\n" +
        fg.yellow, "(Q)uit. \n" +
        bg.green, fg.red, "Your Choice is: "+ fg.rs+bg.rs)
    selection = user_input("")
    running = select(selection)

#-------------- End of Display/Testing--------------


#I never thought it took me so long to finish the course
import codecademylib
import pandas as pd

df = pd.read_csv('employees.csv')

#If the worker worked more than 40 hours
#The exceeding part should multiply by 1.5
total_earned = lambda row: (row.hourly_wage * 40) + ((row.hourly_wage * 1.5) * (row.hours_worked - 40)) \
	if row.hours_worked > 40 \
  else row.hourly_wage * row.hours_worked

df['total_earned'] = df.apply(total_earned, axis = 1)

print(df)

#Rename columns
import codecademylib
import pandas as pd

df = pd.read_csv('imdb.csv')

# Rename columns here
df.columns = ['ID', 'Title', 'Category', 'Year Released', 'Rating']
print(df)

import codecademylib
import pandas as pd
#Rename column II
df = pd.read_csv('imdb.csv')

# Rename columns here
df.rename(columns = {
  'name': 'movie_title'},
  inplace = True)
print(df)

import codecademylib
import pandas as pd

orders = pd.read_csv('shoefly.csv')

print(orders.head(5))

orders['shoe_source'] = orders.shoe_material.apply(lambda x: \
                        	'animal' if x == 'leather'else 'vegan')

#Create salution for specific customers
orders['salutation'] = orders.apply(lambda row: \
                                    'Dear Mr. ' + row['last_name']
                                    if row['gender'] == 'male'
                                    else 'Dear Ms. ' + row['last_name'],
                                    axis=1)

#I do want to finish this session
#But it might be impossible in a few days
#Hope finish it in a few days
#Petal Power Inventory Project
import codecademylib
import pandas as pd

inventory = pd.read_csv('inventory.csv')
print(inventory.head(10))

staten_island = inventory.head(10)
product_request = staten_island.product_description

seed_request = inventory[(inventory.location == "Brooklyn") & (inventory.product_type == "seeds") ]

#Remember you can use lambda function to do logic operation
inventory['in_stock'] = inventory.apply(lambda row: True if row.quantity > 0 else False, axis = 1)

inventory['total_value'] = inventory.apply(lambda row: row.price * row.quantity, axis = 1)

combine_lambda = lambda row: \
  '{} - {}'.format(row.product_type, row.product_description)
inventory['full_description'] = inventory.apply(combine_lambda, axis = 1)
print(inventory.head(10))


#Hangman game with OOP
import random as rnd
word=[]
class Hangman():

    def __init__(self):
        print("-----HANGMAN GAME-----")

    def RndWord(self):
        words=["peace","faith","funny","success","health","luck","friendship"]
        i=rnd.randint(0,6)
        #selects a random word from the 'words' list.
        RWord=[]
        RWord[:0]=words[i]
        #turns the string word to a list
        return RWord

    def First_Print(self, w):
        print("\t")
        for i in range(len(w)):
            word.append("__")
            print(word[i],end=" ")
            #adds '__'char into the empty word list as many as the word's letters, and print word list to the screen.
        print()

    def Control(self,w):
        for i in range(3):
            letter=input("guess a letter! : ")
            letter=letter.strip()
            for j in range(len(w)):
                 if letter==w[j]:
                      word[j]=letter
                    #if the word contains the guessed letter, it insert the letter into the 'word' list
                 else:continue
            self.Print(w)
            selected=input("Do you want to guess the word?(y/n)")
            if(selected=="y" or selected=="Y"):
                self.Guess(w)
                break
            else:continue
        print("Game Over!")

    def Guess(self,w):
        str=""
        str=str.join(w)
        #convert the list to string again
        guess=input("Enter your guess: ")
        guess=guess.strip()
        if guess==str:
            print("\tYou guess it right! Congrets!")

        else:
            print(f"\tYou guess it wrong.The word was '{str}'")
    def Print(self,w):
        print("\t")
        for i in range(len(w)):
            print(word[i],end=" ")
        print()

game=Hangman()
rndword=game.RndWord()
game.First_Print(rndword)
game.Control(rndword)


# for loop is used to reduce the number of repeated commands
# to draw a square you need to go forward and turn right for 4 times
# for loop has range 4 to repeat the actions that are indented
# for loops can be used to loop inside an array for each iteration
# every time the for loop is excuted the x value goes up by 1 starting by 0 (0,1,2,3)
# the x never reaches the value 4... but loops 4 times because it started at 0... (start = 0 -> 1 -> 2 -> end = 3)
import turtle

turtle1 = turtle.Turtle()

turtle1.fillcolor("blue")

turtle1.begin_fill()

for x in range(4):
    turtle1.forward(200)
    turtle1.right(90)

turtle1.end_fill()

turtle.done()


import functions
import python_files

result = python_files.list_directory('.\\', 'py')
print(result)
functions.counting_lines_of_code(result)
functions.checking_connections_between_files(result)

functions.checking_weight_of_connections_between_files(result)

function_list1 = functions.checking_connections_between_functions(result)
function_list2 = functions.checking_weight_of_connections_between_functions(result, function_list1)
function_list3=functions.checking_functions_origins(result,function_list1)#lista z zrodlem danej funkcji

functions.write_to_file_fun_data(function_list1, function_list2)

x = functions.counting_repetitive_lines('connect_weight.txt')

weight_list = list(x)

with open('graf.txt', 'a') as f:
    for el in weight_list:
        f.write(str(el))
        f.write('\n')
    f.write("dane")
with open('function_weight.txt', 'a') as f:
    for el in function_list1:
        f.write(el)
        f.write('\n')
    f.write("dane")
    f.write('\n')
with open('function_weight.txt', 'a') as f:
    for el in range(int(len(function_list1)/2)):
        f.write(str(0))
        f.write('\n')
    f.write("dane")
    f.write('\n')
modul_list = functions.searching_for_used_modules(result)
functions.checking_connections_between_modules(result, modul_list)
with open('fun_graf_modul.txt','w') as f:
    f.write('FUNCTION_WEIGHT')
    f.write('\n')
    with open('function_weight.txt','r') as fw:
        for line in fw:
            f.write(line)
    fw.close()
    f.write("FUNCTION_ORIGIN")
    f.write('\n')
    for el in function_list3:
        f.write(el)
        f.write('\n')
    f.write('MODUL_LIST')
    f.write('\n')
    with open('modul.txt','r') as m:
        for line in m:
            f.write(line)
    m.close()
    f.write('GRAF')
    f.write('\n')
    with open('graf.txt','r') as g:
        for line in g:
            f.write(line)
    g.close()
f.close()


from pages.main_page import MainPage
from pages.login_page import LoginPage
from pages.basket_page import BasketPage
import pytest

@pytest.mark.login_quest
class TestLoginFromMainPage:
    def test_guest_can_go_to_login_page(self, browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.go_to_login_page()
        login_page = LoginPage(browser, browser.current_url)
        # login_page.open()
        login_page.should_be_login_page()

    def test_guest_should_see_login_link(self, browser):
        link = "http://selenium1py.pythonanywhere.com/"
        page = MainPage(browser, link)
        page.open()
        page.should_be_login_link()

def test_guest_cant_see_product_in_basket_opened_from_main_page(browser):
    link = "http://selenium1py.pythonanywhere.com/"
    main_page = MainPage(browser, url=link)
    main_page.open()
    main_page.go_to_shop_cart()
    basket_page = BasketPage(browser, url=browser.current_url, timeout=0)
    basket_page.should_not_be_products_in_basket()
    basket_page.should_be_message_empty_basket()


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import statsmodels.api as sm
import os.path as osp # makes it so we can substitute forward slash for backslash minimizing character escape headaches
import sys
import statsmodels.formula.api as smf

def fix_rate(data):
    rate = data.rstrip('%')
    rate = float(rate) / 100
    return rate

def getfile(mypath, filename, url): # filename should be passed in with forward slashes

	# See if we have the file locally.  If so use it.  If not go get it. Otherwise stop
	try:
		df = pd.read_csv(osp.normpath(mypath + filename)) #  index_col=0 - This index statement causes an error when we try to read saved file for some reason?
		return df
	except IOError:
		try:
			df = pd.read_csv(url, index_col=0)
			df.to_csv(osp.normpath(mypath + filename), header=True, index=False)
			return df
		except:
			print("Sorry - Can't find the input file on disk or online or couldn't write it once found")
			sys.exit(1) # stop processing

def clean_data(raw_data):
	raw_data.dropna(inplace=True) # drop the na rows
	raw_data['New.Rate'] = [fix_rate(data) for index, data in raw_data['Interest.Rate'].iteritems()]
	raw_data['dtir'] = [fix_rate(data) for index, data in raw_data['Debt.To.Income.Ratio'].iteritems()]
	raw_data['FICO'] = map(lambda x: int(x.split('-')[0]), raw_data['FICO.Range']) # clean fico range and get score #ask Kyle why this runs so slow

	print("Shape after clean is {}".format(raw_data.shape))
	return raw_data

def enhance_data(df):
	df['annual_inc'] = df['Monthly.Income'] * 12
	df['Rate'] = df['New.Rate'] # seems silly to have to do this but smf chokes on Interest.Rate - No field named Interest?
	print("Shape after enhance is {}".format(df.shape))
	print("printing head after enhance ******************************")
	print(df.head())
	return df

# def minimize_data(df):  # Thought I would try limiting this to just the fields required in the model - did not help
# 	df = df[['annual_inc','Rate']]
# 	# df1 = df[['a','b']]
# 	return df

def linreg_A(df):  #modifying this to refelct the multiple regression exercise
	#Use income (annual_inc) to model interest rates (int_rate).
	intrate = df['Rate']
	annualinc = df['annual_inc']

	# intrate is dependent variable
	y = np.matrix(intrate).transpose()

	# A single independent variable
	x = np.matrix(annualinc).transpose()  ######## error?

	X = sm.add_constant(x)

	linmodel_A = sm.OLS(y,X).fit()
	return linmodel_A

def linreg_B(df):

	# Build the model in statsmodels api
	# copy data and separate predictors and response
	X = df.copy()
	y = X.pop('Rate')

	linmodel_B = smf.ols(formula='Rate ~ annual_inc', data=df).fit()
	return linmodel_B

def linreg_C(df):
	# Build the model in statsmodels api
	# copy data and separate predictors and response
	X = df.copy()
	y = X.pop('Rate')

	linmodel_C = smf.ols(formula='annual_inc ~ Rate', data=df).fit() # flipped the variables to see if results made more sense.
	return linmodel_C

def linreg_D(df):
# trying to overfit

	intrate = df['Interest.Rate']
	loanamt = df['Amount.Requested']
	fico = df['FICO']
	dtir = df['dtir']
	ocl = df['Open.CREDIT.Lines']
	rcb = df['Revolving.CREDIT.Balance']
	i6m = df['Inquiries.in.the.Last.6.Months']
	ai = df['annual_inc']
	# Build the model in statsmodels api
	# copy data and separate predictors and response
	X = df.copy()
	y = X.pop('Rate')

	linmodel_D = smf.ols(formula='Rate ~ FICO + loanamt + ocl + rcb + i6m + ai', data=df).fit() # flipped the variables to see if results made more sense.
	return linmodel_D

def analyze_it():
	mypath = 'C:/Users/bob071988/thinkful/projects/ds-lesson3/'
	filename = 'loansData.csv'
	url = 'https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv'

	df = getfile(mypath, filename, url)
	df = clean_data(df)
	df = enhance_data(df)
	# df = minimize_data(df)

	linmodel_A = linreg_A(df)
	# linmodel_B = linreg_B(df)
	# linmodel_C = linreg_C(df)
	# linmodel_D = linreg_D(df)

	print(linmodel_A.summary())
	# print(linmodel_B.summary())
	# print(linmodel_C.summary())
	# print(linmodel_D.summary())

	# return(df, linmodel_A, linmodel_B, linmodel_C)
	# modifying return to just one model and df
	return(df, linmodel_A)

def main():
    analyze_it()

if __name__ == "__main__":
    main()

### stats model predict vs numpy predict


import sys
sys.stdin = open('eval_input.txt')

# 시간 복잡도 O(n^2)

N = int(input())
nums = list(map(int, input().split()))
counter = [1]*N

for i in range(1, N):
    for j in range(i):
        if nums[i] > nums[j]:
            counter[i] = max(counter[i], counter[j]+1)

print(counter)
print(max(counter))


import pandas as pd

# Importando os Dados

fatec_operacao = pd.read_excel("../dados/alterados/fatec_opr.xlsx")
'''
fatec_movimento = pd.read_csv("../Dados/Complementares/fatec_mvt.csv", sep = '|', encoding='UTF-8', header = 1)
fatec_pagamento = pd.read_csv("../Dados/Complementares/fatec_pgt.csv", sep = '|', encoding='UTF-8', header = 1)

# Retirando as colunas desnecessárias

fatec_operacao_colunas = fatec_operacao.columns[1:len(fatec_operacao.columns) - 1]
fatec_movimento_colunas = fatec_movimento.columns[1:len(fatec_movimento.columns) - 1]
fatec_pagamento_colunas = fatec_pagamento.columns[1:len(fatec_pagamento.columns) - 1]

# Armazenando o nome de uma coluna de cada tabela numa váriavel

#coluna_fatec_operacao = fatec_operacao.columns[1]
coluna_fatec_movimento = fatec_movimento.columns[1]
coluna_fatec_pagamento = fatec_pagamento.columns[1]

# Armazenando o nome de uma coluna de cada tabela numa váriavel

fatec_operacao = pd.DataFrame(fatec_operacao, columns = fatec_operacao_colunas)
fatec_movimento = pd.DataFrame(fatec_movimento, columns = fatec_movimento_colunas)
fatec_pagamento = pd.DataFrame(fatec_pagamento, columns = fatec_pagamento_colunas)

# Redefinindo o nome de cada coluna

#fatec_operacao.columns = ['id_opr_cad_pos', 'doc_cli', 'tip_cli', 'qtd_pcl', 'dat_vct_ult_pcl', 'vlr_ctrd_fta_tfm', 'sdo_ddr_tfm', 'vlr_ctrd', 'id_ult_rss_opr', 'id_mdl', 'cod_mdl', 'id_fnt']
fatec_movimento.columns = ['id_opr_cad_pos', 'id_mvt_cad_pos', 'dat_vct', 'qtd_pcl_vnc', 'qtd_pcl_pgr', 'vlr_tot_fat_tfm',	'vlr_min_fat_tfm', 'vlr_pcl_tfm', 'tip_mvt', 'prd']
fatec_pagamento.columns = ['id_opr_cad_pos', 'id_mvt_cad_pos', 'id_pgt_cad_pos', 'dat_pgt', 'dat_vct_tfm', 'vlr_pgt_tfm', 'cod_mdl', 'cmd']

# Retirando os valores nulos entre as quebras de página:

#fatec_operacao = fatec_operacao[(pd.isna(fatec_operacao['id_opr_cad_pos']) != True)]
fatec_movimento = fatec_movimento[(pd.isna(fatec_movimento['id_opr_cad_pos']) != True)]
fatec_pagamento = fatec_pagamento[(pd.isna(fatec_pagamento['id_opr_cad_pos']) != True)]

# Usando a váriavel anterior para retirar as quebrar de página:

#fatec_operacao = fatec_operacao[(fatec_operacao['id_opr_cad_pos'] != coluna_fatec_operacao)]
fatec_movimento = fatec_movimento[(fatec_movimento['id_opr_cad_pos'] != coluna_fatec_movimento)]
fatec_pagamento = fatec_pagamento[(fatec_pagamento['id_opr_cad_pos'] != coluna_fatec_pagamento)]
'''

fontes_ids = list(int(float(str(i).strip())) for i in list(fatec_operacao['id_fnt']))
fatec_operacao['id_fnt'].update(pd.Series(fontes_ids))

#Removendo espaços dos campos das tabelas

def limpa_espacos():
    for coluna in fatec_operacao:
        coluna_temporaria = list()
        for campo in fatec_operacao[coluna]:
            if type(campo) == str:
                coluna_temporaria.append(campo.strip())
            else:
                coluna_temporaria.append(campo)
        fatec_operacao[coluna].update(pd.Series(coluna_temporaria))

limpa_espacos()

#Função que calcula a quantidade total de campos para um determinada fonte
def campos_totais_opr(fonte):
    return fatec_operacao.query(f"id_fnt == {fonte}")['id_fnt'].count() * fatec_operacao.shape[1]

#Função que calcula quantos campos nulos existem por conta da modalidade D01 e C01, as quais são as únicas que
#precisam da coluna 'sdo_ddr_tfm'
def nulos_permitidos(fonte):
    return fatec_operacao.query(f"sdo_ddr_tfm == 'NULL' and cod_mdl not in ['D01', 'C01'] and id_fnt == {fonte}")['sdo_ddr_tfm'].count()

#Função que retorna a porcentagem de campos nulos de acordo com a fonte, na tabela de operação
def completude_opr(fonte):
    campos_nulos = 0
    for coluna in fatec_operacao.columns:
        campos_nulos += fatec_operacao.query(f"{coluna} == 'NULL' and id_fnt == {fonte}")['id_fnt'].count()

    nulos_totais = campos_nulos - nulos_permitidos(fonte)
    return 100 - ((nulos_totais / campos_totais_opr(fonte)) * 100)

#Função que integra as 3 funções anteriores, retornando uma lista com a fonte e a porcentagem de sua respectiva
#completude dos dados
def completude_fontes_operacao():
    indice_fontes = set(list(int(float(str(i).strip())) for i in list(fatec_operacao['id_fnt'])))
    matriz_completude = list()
    for fonte in indice_fontes:
        porcentagem = completude_opr(fonte)
        matriz_completude.append([porcentagem, fonte])
    return matriz_completude.sort(reverse = True)

Entrega 5/SPC Produto - Protótipo v1/UDA Brasil/indicadores/completude.py


from django.http import HttpResponse
from django.template import loader

import requests
from bs4 import BeautifulSoup

from wordcloud import WordCloud, STOPWORDS
from konlpy.tag import Okt
from collections import Counter
import matplotlib.pyplot as plt

# url_list, 텍스트 파일 생성(쓰기) 및 url_list 100개 저장
f = open('url.txt', 'w')
page_counter = 1
url_count = 0

for i in range(1, 6):
    html = requests.get(f'http://www.dailypop.kr/news/articleList.html?page={i}&total=168&sc_section_code'
                        '=&sc_sub_section_code=&sc_serial_code=&sc_area=A&sc_level=&sc_article_type=&sc_view_level'
                        '=&sc_sdate=&sc_edate=&sc_serial_number=&sc_word=%EC%A7%91%EC%BD%95')
    soup = BeautifulSoup(html.content, 'html.parser')
    url_list = soup.find(class_='article-list-content text-left').find_all('a')

    for link in url_list:
        if url_count == 99:
            f.write('http://www.dailypop.kr/'+link.get('href'))
        else:
            f.write(f'http://www.dailypop.kr/'+link.get('href')+'\n')
        url_count += 1
f.close()

# url_list read, 텍스트 파일 읽기 및 각 기사(url)의 내용들을 한 변수에 저장
stringTypeContent = ''
with open('url.txt', 'r') as file:
    for line in file:
        line = line.strip('\n')
        html = requests.get(line)
        soup = BeautifulSoup(html.content, 'html.parser')
        body = soup.find(id='article-view-content-div').find_all('p')
        spwords = set(STOPWORDS)
        for i, e in enumerate(body):
            stringTypeContent += e.get_text()
file.close()

# 워드클라우드 설정 및 키워드 빈도별로 분류
wc = WordCloud(font_path='/Library/Fonts/HMKMMAG.TTF', background_color='white',
               width=500, height=500, max_words=20, max_font_size=100, stopwords=spwords)
okt = Okt()
noun = okt.nouns(stringTypeContent)
count = Counter(noun)
noun_list = count.most_common()

# stopwords 키워드 제거(워드클라우드 분석에 도움안되는 한 글자의 키워드들 제거)
temp = []
for i, noun in enumerate(dict(noun_list)):
    if noun == '이' or noun == '위' or noun == '를' or noun == '등' or noun == '것' or noun == '로' or noun == '수':
        temp.append(noun_list[i])
for i, e in enumerate(temp):
    noun_list.remove(temp[i])

# 워드 클라우드 생성
wc.generate_from_frequencies(dict(noun_list))
wc.to_file('./wc.png')
plt.figure(figsize=(10, 8))
plt.imshow(wc)
plt.axis('off')
plt.show()
print('success')

def index(request):
    template = loader.get_template('news/index.html')

    return HttpResponse(template.render('exit', request))


def permutation_unique(nums):
    def backtrack(lis, visited, nums, temp):
        if len(temp) == len(nums):
            lis.append(temp[:])
            print("Ans: {0}".format(lis))

        for i in range(0, len(nums)):
            if visited[i] == True:
                continue
            if i > 0 and nums[i] == nums[i - 1] and visited[i - 1] == False:
                continue

            temp.append(nums[i])
            visited[i] = True
            print(temp)
            print("Visited: {0}".format(visited))
            backtrack(lis, visited, nums, temp)
            # backtrack
            temp.pop()
            visited[i] = False
            print("Backtrack: {0}".format(temp))
            print("Visited: {0}".format(visited))

    nums.sort()
    lis = []
    visited = [False for i in range(len(nums))]
    backtrack(lis, visited, nums, [])
    return lis

print(permutation_unique([1,1,2]))


# Author: Real
# CreateTime 2018/9/6-11:42
# IDE: PyCharm

import socket

client = socket.socket()

HOST = 'localhost'
PORT = 8001
client.connect((HOST,PORT))

while True:
    msg_to_server = \
        bytes(input('please input the message which you want to send to server').strip(),encoding='utf-8')
    client.sendall(msg_to_server)
    data = client.recv(1024)

    print('the data we received from server is',repr(data)) #repr是格式化输出

client.close()


# 129. Sum Root to Leaf Numbers

# code: Approach: keep multipling by 10 when we see a child and then add the val of the child node.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumNumbers(self, root: TreeNode) -> int:
        if not root:
            return 0

        stk = [(root, root.val)]
        res = 0

        while stk:
            curr, sum = stk.pop()
            if curr.left:
                stk.append((curr.left, sum*10 + curr.left.val))
            if curr.right:
                stk.append((curr.right, sum*10+ curr.right.val))

            if not curr.left and not curr.right:
                res+=sum

        return res

# Time complexity: O(H)  Height of the tree.
# Space complexity: O(H)
# Accepted on Leetcode: Yes.


"""
Run FIMO to scan motif over FASTA sequences
"""
import pathlib
import shlex
import subprocess
from concurrent.futures import ProcessPoolExecutor, as_completed

import pandas as pd

from .utilities import split_meme_motif_file

"""
Run FIMO to scan motif over FASTA sequences
"""

def _fimo_runner(motif_path, fasta_path, output_path, path_to_fimo='',
                 raw_score_thresh=6, raw_p_value_thresh=5e-4, is_genome_fasta=False,
                 top_n=300000):
    """Run fimo for a single motif over single fasta file"""
    if path_to_fimo != '':
        path_to_fimo = path_to_fimo.rstrip('/') + '/'
    cmd = f'{path_to_fimo}fimo --text --skip-matched-sequence --thresh {raw_p_value_thresh} {motif_path} {fasta_path}'

    tmp_path = str(output_path) + "tmp"
    with open(tmp_path, 'w') as out:
        p = subprocess.Popen(shlex.split(cmd),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             encoding='utf8')
        first = True
        while True:
            line = p.stdout.readline()
            if line == '':
                break
            if first:
                first = False
                out.write(line)
                continue
            score = line.split('\t')[6]
            # when scan very small motifs, p-value is not very sig, but score can be different.
            if float(score) > raw_score_thresh:
                out.write(line)
        p.terminate()
        if p.returncode is not None and p.returncode != 0:
            raise OSError(f'{cmd} returncode {p.returncode}')

    try:
        fimo_out = pd.read_csv(tmp_path, sep='\t')
        # same as sort -k1,1 -k2,2n, so in bedtools intersect, we can use --sorted
        fimo_out = fimo_out.sort_values(by=['sequence_name', 'start']).reset_index(drop=True)
        """
        # p-value correction and filtering
        # this is not very useful when scan large scale regions.

        reject, pvals_corrected, alphacSidak, alphacBonf = multipletests(
            fimo_out['p-value'],
            alpha=alpha,
            method=correct_method,
            is_sorted=False,
            returnsorted=False)
        print(f'{motif_path}, alpha={alpha}, N motif total={reject.size}, '
              f'N motif remain={reject.sum()}, alphacBonf={alphacBonf}')
        fimo_out = fimo_out[reject].copy()
        """

    except pd.errors.EmptyDataError:
        # if no result in tmp_path
        return ''

    if not is_genome_fasta:
        # the fasta file is not whole genome fasta, get real genome position from it
        chrom_start = fimo_out['sequence_name'].apply(lambda i: i.split('-')[0])
        final_df = pd.DataFrame(chrom_start.str.split(':').tolist(),
                                columns=['sequence_name', 'region_start'])
        final_df['region_start'] = final_df['region_start'].astype(int)
        # Calculate the real genome position. fimo_out['start'] is relative to final_df['region_start']
        final_df['start'] = final_df['region_start'] + fimo_out['start']
        final_df['stop'] = final_df['region_start'] + fimo_out['stop']
        final_df['strand'] = fimo_out['strand']
        final_df['motif_id'] = fimo_out['motif_id']
        final_df['score'] = fimo_out['score']
        final_df['p-value'] = fimo_out['p-value']
    else:
        final_df = fimo_out

    # motif stats are not saved
    final_df = final_df.sort_values('score', ascending=False).iloc[:min(final_df.shape[0], top_n), :]
    print(f'{motif_path}, N motif total={final_df.shape[0]}')

    col_order = ['sequence_name', 'start', 'stop', 'motif_id', 'strand']
    final_df.loc[:, col_order].to_hdf(output_path, key='data')

    subprocess.run(['rm', '-f', tmp_path], check=True)
    return output_path

def _scan_motif_over_fasta(meme_motif_file, fasta_path, cpu, output_dir, path_to_fimo='',
                           raw_score_thresh=7, raw_p_value_thresh=5e-4, top_n=300000,
                           is_genome_fasta=False):
    if isinstance(meme_motif_file, str):
        meme_motif_file = [meme_motif_file]

    output_dir = pathlib.Path(output_dir).resolve()
    output_dir.mkdir(exist_ok=True, parents=True)

    motif_file_records = split_meme_motif_file(meme_motif_file, output_dir)
    print(f'{len(motif_file_records)} motifs to count.')

    with ProcessPoolExecutor(cpu) as executor:
        futures = {}
        for uid, name, motif_file_path in motif_file_records:
            future = executor.submit(_fimo_runner,
                                     path_to_fimo=path_to_fimo,
                                     motif_path=motif_file_path,
                                     fasta_path=fasta_path,
                                     output_path=output_dir / (pathlib.Path(motif_file_path).name[:-5] + '.bed.hdf'),
                                     raw_score_thresh=raw_score_thresh,
                                     raw_p_value_thresh=raw_p_value_thresh,
                                     is_genome_fasta=is_genome_fasta,
                                     top_n=top_n)
            futures[future] = (uid, name, motif_file_path)

        n = 0
        motif_records = []
        for future in as_completed(futures):
            output_path = future.result()
            uid, name, motif_file_path = futures[future]
            if output_path == '':
                # fimo return nothing, the parameter is too stringent for the motif.
                # by default parms, this rarely happens (2 in 1800).
                # Usually its the motif too small so p value is large.
                print(f'Motif {uid} {name} do not have any match under current settings.')
                continue
            motif_records.append((uid, name, str(motif_file_path), str(output_path)))
            n += 1
            if n % 100 == 0:
                print(f'Finish count {n} motifs.')

    lookup_table = pd.DataFrame(motif_records,
                                columns=['uid', 'name', 'motif_file_path', 'output_file_path']).set_index('uid')

    lookup_table.to_hdf(output_dir / f'LOOKUP_TABLE.hdf', key='data')
    return

def _aggregate_motif_beds(bed_file_paths, output_path, cpu=1, sort_mem_gbs=1):
    output_path = str(pathlib.Path(output_path).resolve())

    temp_bed = output_path + '.tmp_input.bed'
    with open(temp_bed, 'w') as temp_f:
        for bed_file_path in bed_file_paths:
            bed_df = pd.read_hdf(bed_file_path)[['sequence_name', 'start', 'stop', 'motif_id', 'strand']]
            csv_string = bed_df.to_csv(sep='\t', header=None, index=None)
            temp_f.write(csv_string)

    subprocess.run(shlex.split(f'sort -k1,1 -k2,2n --parallel={cpu} -S {sort_mem_gbs}G {temp_bed} -o {output_path}'),
                   stderr=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf8', check=True)
    subprocess.run(shlex.split(f'rm -f {temp_bed}'))
    return output_path


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

"""'Brain-calc' game logic."""

import random
from operator import add, mul, sub

RULES = 'What is the result of the expression?'
OPERATIONS = (
    ('+', add),
    ('-', sub),
    ('*', mul),
)

def get_round_data():  # noqa: WPS210
    """Generate data for game.

    Returns:
        tuple: expression and current answer
    """
    operator, operation = random.choice(OPERATIONS)  # noqa: S311
    operand1 = random.randint(1, 100)  # noqa: S311
    operand2 = random.randint(1, 100)  # noqa: S311
    expression = '{num1} {operator} {num2}'.format(
        num1=operand1,
        operator=operator,
        num2=operand2,
    )
    answer = operation(operand1, operand2)

    return (expression, str(answer))


def rec(n,l,prod):
	if(l == 0):
		prod = prod * n[0]
		return prod
	else:
		prod = prod * n[l]
		return rec(n,l-1,prod)

def iter(n):
	prod = 1
	l = len(n)
	for x in range(0,l):
		prod=prod * n[x]
	return prod

n = [5,7,9,6]
l =len(n)-1
prod = 1
print ("iteration")
print(iter(n))
print("recursion")
print(rec(n,l,prod))


from .constants import RotationType, Axis
from .auxiliary_methods import intersect, set_to_decimal

DEFAULT_NUMBER_OF_DECIMALS = 3
START_POSITION = [0, 0, 0]

class Item:
    def __init__(self, name, width, height, depth, rotate):
        self.name = name
        self.width = width
        self.height = height
        self.depth = depth
        self.rotate = rotate
        self.rotation_type = 0
        self.position = START_POSITION
        self.number_of_decimals = DEFAULT_NUMBER_OF_DECIMALS

    def format_numbers(self, number_of_decimals):
        self.width = set_to_decimal(self.width, number_of_decimals)
        self.height = set_to_decimal(self.height, number_of_decimals)
        self.depth = set_to_decimal(self.depth, number_of_decimals)
        self.number_of_decimals = number_of_decimals

    def get_volume(self):
        return set_to_decimal(
            self.width * self.height * self.depth, self.number_of_decimals
        )

    def get_dimension(self):
        if self.rotation_type == RotationType.RT_WHD:
            dimension = [self.width, self.height, self.depth]
        elif self.rotation_type == RotationType.RT_HWD:
            dimension = [self.height, self.width, self.depth]
        elif self.rotation_type == RotationType.RT_HDW:
            dimension = [self.height, self.depth, self.width]
        elif self.rotation_type == RotationType.RT_DHW:
            dimension = [self.depth, self.height, self.width]
        elif self.rotation_type == RotationType.RT_DWH:
            dimension = [self.depth, self.width, self.height]
        elif self.rotation_type == RotationType.RT_WDH:
            dimension = [self.width, self.depth, self.height]
        else:
            dimension = []

        return dimension

class Bin:
    def __init__(self, name, width, height, depth):
        self.name = name
        self.width = width
        self.height = height
        self.depth = depth
        self.items = []
        self.number_of_decimals = DEFAULT_NUMBER_OF_DECIMALS

    def format_numbers(self, number_of_decimals):
        self.width = set_to_decimal(self.width, number_of_decimals)
        self.height = set_to_decimal(self.height, number_of_decimals)
        self.depth = set_to_decimal(self.depth, number_of_decimals)
        self.number_of_decimals = number_of_decimals

    def get_volume(self):
        return set_to_decimal(
            self.width * self.height * self.depth, self.number_of_decimals
        )

    def put_item(self, item, pivot):
        fit = False
        valid_item_position = item.position
        item.position = pivot

        for i in range(0, len(RotationType.ALL) if item.rotate else 1):
            item.rotation_type = i
            dimension = item.get_dimension()
            if (
                self.width < pivot[0] + dimension[0] or
                self.height < pivot[1] + dimension[1] or
                self.depth < pivot[2] + dimension[2]
            ):
                continue

            fit = True

            for current_item_in_bin in self.items:
                if intersect(current_item_in_bin, item):
                    fit = False
                    break

            if fit:
                self.items.append(item)

            if not fit:
                item.position = valid_item_position

            return fit

        if not fit:
            item.position = valid_item_position

        return fit

def pack_to_bin(bin, item):
    fitted = False

    if not bin.items:
        response = bin.put_item(item, START_POSITION)
        if not response:
            bin.unfitted_items.append(item)
        return True

    for axis in range(0, 3):
        items_in_bin = bin.items
        for ib in items_in_bin:
            pivot = [0, 0, 0]
            w, h, d = ib.get_dimension()
            if ib.rotate:
                if axis == Axis.WIDTH:
                    pivot = [
                        ib.position[0] + w,
                        ib.position[1],
                        ib.position[2]
                    ]
                elif axis == Axis.HEIGHT:
                    pivot = [
                        ib.position[0],
                        ib.position[1] + h,
                        ib.position[2]
                    ]
                elif axis == Axis.DEPTH:
                    pivot = [
                        ib.position[0],
                        ib.position[1],
                        ib.position[2] + d
                    ]
            if bin.put_item(item, pivot):
                fitted = True
                break
        if fitted:
            break
    return fitted

def pack(binx, biny, binz, items, return_bins=False, number_of_decimals=DEFAULT_NUMBER_OF_DECIMALS):
    bin = Bin("", binx, binz, biny)
    bin.format_numbers(number_of_decimals)
    bins = [bin]
    bins_idxs = []

    for item in items:
        item.format_numbers(number_of_decimals)

    item_idx = 0
    for bin in bins:
        items_in_bin = []
        for i in range(item_idx, len(items)):
            fitted = pack_to_bin(bin, items[i])
            if not fitted:
                bins.append(Bin("", bins[0].width, bins[0].height, bins[0].depth))
                break
            items_in_bin.append(items[i].name)
            item_idx += 1
        bins_idxs.append(items_in_bin)

    return bins if return_bins else bins_idxs


import matplotlib.pyplot as plt
from matplotlib import animation

audio_file = '/Users/bzamecnik/Documents/harmoneye-labs/harmoneye/data/wav/c-scale-piano-mono.wav'

fig = plt.figure(figsize=(5,4))
# 350 frames = 25 fps * 14 sec
ch_resampled = resample(ch_from_file[:,:108], 350, axis=0)
vmin, vmax = ch_resampled.min(), ch_resampled.max(),
def animate(nframe):
    cla()
    imshow(ch_resampled[nframe].reshape(-1,12),
         cmap=plt.get_cmap('jet'), interpolation='nearest',
               vmin=vmin, vmax=vmax)

anim = animation.FuncAnimation(fig, animate, frames=ch_resampled.shape[0])
anim_file = 'c-scale-piano-chroma-grid.gif'
anim.save(anim_file, writer='imagemagick', fps=25);

#%%sh
#ffmpeg -i anim_file -i audio_file -c:v libx264 -c:a libmp3lame -b:a 128k c-scale-piano-chroma-grid.mp4


"""Created All Basic Models: User Model, Team Model, Team User Model, Match Model, Location Model, Game Model, Invite Model and Comment Model.

Revision ID: 4c5d252e146d
Revises:
Create Date: 2021-04-23 06:39:08.022544

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "4c5d252e146d"
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "games",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("game_name", sa.String(), nullable=False),
        sa.Column("game_type", sa.String(), nullable=False),
        sa.Column("game_description", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "locations",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("location_name", sa.String(), nullable=False),
        sa.Column("location_phone", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("location_name"),
    )
    op.create_table(
        "users",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("nickname", sa.String(), nullable=False),
        sa.Column("first_name", sa.String(), nullable=False),
        sa.Column("last_name", sa.String(), nullable=False),
        sa.Column("email", sa.String(), nullable=False),
        sa.Column("password_hash", sa.String(), nullable=False),
        sa.Column("biography", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
    )
    op.create_index(op.f("ix_users_created_at"), "users", ["created_at"], unique=False)
    op.create_table(
        "teams",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("team_name", sa.String(), nullable=False),
        sa.Column("team_description", sa.String(), nullable=False),
        sa.Column("team_created_date", sa.DateTime(), nullable=True),
        sa.Column("owner_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_teams_team_created_date"), "teams", ["team_created_date"], unique=False
    )
    op.create_table(
        "invite_users",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("team_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["team_id"],
            ["teams.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "matches",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("date", sa.String(), nullable=False),
        sa.Column("match_register_date", sa.DateTime(), nullable=True),
        sa.Column("match_winner_id", sa.Integer(), nullable=True),
        sa.Column("team_id_1", sa.Integer(), nullable=True),
        sa.Column("team_id_2", sa.Integer(), nullable=True),
        sa.Column("game_id", sa.Integer(), nullable=True),
        sa.Column("location_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["game_id"],
            ["games.id"],
        ),
        sa.ForeignKeyConstraint(
            ["location_id"],
            ["locations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["match_winner_id"],
            ["teams.id"],
        ),
        sa.ForeignKeyConstraint(
            ["team_id_1"],
            ["teams.id"],
        ),
        sa.ForeignKeyConstraint(
            ["team_id_2"],
            ["teams.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_matches_match_register_date"),
        "matches",
        ["match_register_date"],
        unique=False,
    )
    op.create_table(
        "team_users",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("team_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["team_id"],
            ["teams.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "comments",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("text", sa.String(length=140), nullable=False),
        sa.Column("author", sa.String(length=32), nullable=False),
        sa.Column("timestamp", sa.DateTime(), nullable=True),
        sa.Column("match_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["match_id"],
            ["matches.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_comments_timestamp"), "comments", ["timestamp"], unique=False
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_comments_timestamp"), table_name="comments")
    op.drop_table("comments")
    op.drop_table("team_users")
    op.drop_index(op.f("ix_matches_match_register_date"), table_name="matches")
    op.drop_table("matches")
    op.drop_table("invite_users")
    op.drop_index(op.f("ix_teams_team_created_date"), table_name="teams")
    op.drop_table("teams")
    op.drop_index(op.f("ix_users_created_at"), table_name="users")
    op.drop_table("users")
    op.drop_table("locations")
    op.drop_table("games")
    # ### end Alembic commands ###


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

import codecs

# part 1
# name = raw_input("What's your name?\n")  # input
# age = raw_input("How old are you?\n")
#
# print("Your name is: " + name)
# print("You are " + age + " years old.")
#
# after_ten = int(age) + 10
# print("You will be " + str(after_ten) + " years old after ten years.")

# part 2
name = "张三"
unicode_str = unicode(name, encoding='utf-8')
print(unicode_str.encode('utf=8'))

# file coding
# 打开文件使用codecs

codecs.open('filename', encoding='utf8')

# Python 中如何避免中文是乱码
# 这个问题是一个具有很强操作性的问题。我这里有一个经验总结，分享一下，供参考：
#
# 首先，提倡使用 utf-8 编码方案，因为它跨平台不错。
#
# 经验一：在开头声明：
# 经验二：遇到字符（节）串，立刻转化为 unicode，不要用 str()，直接使用 unicode()
# unicode_str = unicode('中文', encoding='utf-8')
# print unicode_str.encode('utf-8')

# 经验三：如果对文件操作，打开文件的时候，最好用 codecs.open，替代 open(这个后面会讲到，先放在这里)
# import codecs
# codecs.open('filename', encoding='utf8')


import numpy as np

# plots FFT
def doFFT(t,x,doplot=False):
  N = np.float(np.shape(x)[0]);

  Fs = np.float(np.shape(t)[0])
  xdft = np.fft.fft(x);
  xdft = xdft[1:N/2+1];
  #print xdft
  psdx = (1/(Fs*N))*np.abs(xdft)**2;
  psdx *=2   #[[2:end-1);
  dt = t[-1]-t[0]
  freq = np.linspace(0,Fs/(2*dt),N/2.)
  if(doplot):
    import matplotlib.pylab as plt
    plt.plot(freq,np.log10(psdx)); #grid on;
    plt.title('Power spectral density');
    plt.xlabel('Frequency (Hz)'); plt.ylabel('Power/Frequency (dB/Hz)');

  return freq,psdx

def freq(yt,t,doplot=False,oldstuff=True):
  if oldstuff:
    tss = 100
    idx = np.where(t > tss)
    cA1 = np.ndarray.flatten(yt[idx,8])
    ts = t[idx]
  else:
    cA1 = yt
    ts = t

  cA1m = cA1-np.mean(cA1)
  f,p = doFFT(ts,cA1m,doplot=doplot)
  return f,p

# validate
def doit():
  Fs = 1000; # frames per sec
  sec = 10   # sec
  t = np.linspace(0,sec-1/Fs,sec*Fs)
  v = 100. # Hz
  x = np.cos(2*pi*v*t)+1*0.01*np.random.randn(np.shape(t)[0]);

  doFFT(t,x,doplot=True)

#print t


import os

import graphviz
import matplotlib
import matplotlib.pyplot as plt
import mglearn
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer, make_moons
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.tree import export_graphviz

from common.task import Task

matplotlib.rc('font', family="NanumBarunGothic")

def plot_feature_importances_cancer(model):
    n_features = cancer.data.shape[1]
    plt.barh(range(n_features), model.feature_importances_, align='center')
    plt.yticks(np.arange(n_features), cancer.feature_names)
    plt.xlabel("특성 중요도")
    plt.ylabel("특성")
    plt.ylim(-1, n_features)
    plt.show()

with Task("결정 트리"):
    plt.figure(figsize=(5, 5))
    mglearn.plots.plot_animal_tree()
    plt.show()

    cancer = load_breast_cancer()
    X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
    tree = DecisionTreeClassifier()
    tree.fit(X_train, y_train)
    print("DecisionTree : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        tree.score(X_train, y_train), tree.score(X_test, y_test)))

    tree = DecisionTreeClassifier(max_depth=4, random_state=0)
    tree.fit(X_train, y_train)
    print("DecisionTree : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        tree.score(X_train, y_train), tree.score(X_test, y_test)))

    plt.figure(figsize=(20, 12))
    export_graphviz(tree, out_file="tree.dot", class_names=["악성", "양성"],
                    feature_names=cancer.feature_names, impurity=False, filled=True)
    dot = graphviz.Source(open("tree.dot").read())
    dot.format = 'pdf'
    dot.render(filename='tmp')

    print("특성 중요도: {}".format(tree.feature_importances_))

    plot_feature_importances_cancer(tree)

    mglearn.plots.plot_tree_not_monotone()
    plt.show()

    ram_prices = pd.read_csv(os.path.join(mglearn.datasets.DATA_PATH, "ram_price.csv"))
    plt.semilogy(ram_prices.date, ram_prices.price)
    plt.xlabel("년")
    plt.ylabel("가격($/Mbyte)")
    plt.show()

    data_train = ram_prices[ram_prices.date < 2000]
    data_test = ram_prices[ram_prices.date >= 2000]

    X_train = data_train.date[:, np.newaxis]
    y_train = np.log(data_train.price)

    tree = DecisionTreeRegressor().fit(X_train, y_train)
    linear_reg = LinearRegression().fit(X_train, y_train)

    X_all = ram_prices.date[:, np.newaxis]

    pred_tree = tree.predict(X_all)
    pred_lr = linear_reg.predict(X_all)

    price_tree = np.exp(pred_tree)
    price_lr = np.exp(pred_lr)

    plt.semilogy(data_train.date, data_train.price, label="훈련 데이터")
    plt.semilogy(data_test.date, data_test.price, label="테스트 데이터")
    plt.semilogy(ram_prices.date, price_tree, label="트리 예측")
    plt.semilogy(ram_prices.date, price_lr, label="선형 회귀 예측")
    plt.legend()
    plt.show()

with Task("랜덤 포레스트"):
    print()
    print("[[Moons 데이터셋]]")
    X, y = make_moons(n_samples=100, noise=0.25, random_state=3)
    X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
    forest = RandomForestClassifier(n_estimators=5, random_state=2)
    forest.fit(X_train, y_train)
    _, axes = plt.subplots(2, 3, figsize=(20, 10))
    for i, (ax, tree) in enumerate(zip(axes.ravel(), forest.estimators_)):
        ax.set_title("트리 {}".format(i))
        mglearn.plots.plot_tree_partition(X, y, tree, ax=ax)
    mglearn.plots.plot_2d_separator(forest, X, fill=True, ax=axes[-1, -1], alpha=.4)
    axes[-1, -1].set_title("랜덤 포레스트")
    mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
    plt.show()
    print("RandomForest : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        forest.score(X_train, y_train), forest.score(X_test, y_test)))

    print()
    print("[[Breast Cancer 데이터셋]]")
    cancer = load_breast_cancer()
    X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=0)
    forest = RandomForestClassifier(n_estimators=5, random_state=0)
    forest.fit(X_train, y_train)
    print("RandomForest : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        forest.score(X_train, y_train), forest.score(X_test, y_test)))

    plot_feature_importances_cancer(forest)

with Task("그래디언트 부스팅"):
    print()
    print("[[Breast Cancer 데이터셋]]")
    cancer = load_breast_cancer()
    X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=0)
    gbrt = GradientBoostingClassifier(random_state=0)
    gbrt.fit(X_train, y_train)
    print("GradientBoosting : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        gbrt.score(X_train, y_train), gbrt.score(X_test, y_test)))
    gbrt = GradientBoostingClassifier(random_state=0, max_depth=1)
    gbrt.fit(X_train, y_train)
    print("GradientBoosting : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        gbrt.score(X_train, y_train), gbrt.score(X_test, y_test)))
    gbrt = GradientBoostingClassifier(random_state=0, learning_rate=0.01)
    gbrt.fit(X_train, y_train)
    print("GradientBoosting : 훈련 세트 점수: {:.3f} / 테스트 세트 점수: {:.3f}".format(
        gbrt.score(X_train, y_train), gbrt.score(X_test, y_test)))
    gbrt = GradientBoostingClassifier(random_state=0, max_depth=1)
    gbrt.fit(X_train, y_train)

    plot_feature_importances_cancer(forest)


#!/usr/bin/env python

"""
  Author: Adam White, Mohammad M. Ajallooeian
  Purpose: for use of Reinforcement learning course University of Alberta Fall 2017

  env *ignores* actions: rewards are all random
"""

from utils import rand_norm, rand_in_range, rand_un
import numpy as np

this_reward_observation = (None, None, None) # this_reward_observation: (floating point, NumPy array, Boolean)
numActions = 10
distributions = None

def env_init():
    global this_reward_observation
    local_observation = np.zeros(0) # An empty NumPy array
    createDistribution()

    this_reward_observation = (0.0, local_observation, False)

def env_start(): # returns NumPy array

    return this_reward_observation[1]

def env_step(this_action): # returns (floating point, NumPy array, Boolean), this_action: NumPy array
    global this_reward_observation

    the_reward = rand_norm(distributions[int(this_action)], 1.0) # rewards drawn from (0, 1) Gaussian
    this_reward_observation = (the_reward, this_reward_observation[1], False)
    return this_reward_observation

def env_cleanup():
    #
    return

def env_message(inMessage): # returns string, inMessage: string
    if inMessage == "what is your name?":
        return "my name is skeleton_environment!"
    elif inMessage == "get optimal action":
        highestqValue = findHighestValue()
        return highestqValue

    # else
    return "I don't know how to respond to your message"

def createDistribution(): # create distributions contains mean of each distribution
    global distributions, numActions
    i  =0
    distributions = []
    while i<10:
        distribution = rand_norm(0.0,1.0)
        distributions.append(distribution)
        i+=1
    return

def findHighestValue(): #return the largest q*(a) 's index
    global distributions
    max = distributions[0]
    for distribution in distributions:
        if max < distribution :
            max = distribution
    return distributions.index(max)


from base64 import b64encode
from hashlib import sha1
import itertools as it

from psycopg2 import ProgrammingError
from psycopg2.errorcodes import UNDEFINED_TABLE

from eplasty.ctx import get_session
from eplasty.result import Result
from eplasty.lazy import LazyQuery

from eplasty.object.meta import ObjectMeta
from eplasty.object.exc import NotFound, TooManyFound
from eplasty.object.const import (
    NEW, MODIFIED, ORPHANED, DELETED, UPDATED, UNCHANGED
)
from eplasty.util import prepare_col
from eplasty.query import SelectQuery

class Object(object, metaclass=ObjectMeta):
    """Parent class for all eplasty Object classes"""

    def __init__(self, **kwargs):
        if self._abstract:
            raise NotImplementedError('Abstract class')

        field_names = [
            field.name for field in it.chain(self.fields, self.inh_fields)
        ]
        self._status = NEW
        self._flushed = False
        for k, v in kwargs.items():
            if k not in field_names:
                raise TypeError('Class {0} has no attribute {1}'.format(
                    type(self).__name__, k
                ))
            setattr(self, k, v)

    def __new__(cls, *args, **kwargs):
        self = super(Object, cls).__new__(cls)
        self._current = {}
        self._initial = {}
        self.session = None
        self.temporary = []
        return self

    @property
    def _diff(self):
        r = []
        for f in self.fields:
            n = f.name
            if self._initial[n] != self._current[n]:
                r.append ((f, (self._initial[n], self._current[n])))
        return r

    def _flush_new(self, session, cursor):
        col_names = []
        col_values = []
        for f in it.chain(self.fields, self.inh_fields):
            cvals = f.get_c_vals(self._current)
            for cname, cval in cvals.items():
                col_names.append('"{0}"'.format(cname))
                col_values.append(cval)
        # print cursor.mogrify(
        #     'INSERT INTO {0} ({1}) VALUES ({2}) RETURNING {3}'.format(
        #         type(self).__table_name__, ', '.join(col_names),
        #         ', '.join(['%s'] * len(col_names)),
        #         ','.join(type(self).__pk__)
        #     ),
        #     col_values
        # )
        cursor.execute(
            'INSERT INTO {0} ({1}) VALUES ({2}) RETURNING {3}'.format(
                type(self).__table_name__, ', '.join(col_names),
                ', '.join(['%s'] * len(col_names)),
                ','.join(type(self).__pk__)
            ),
            col_values
        )
        new_pk_val = cursor.fetchone()
        for pk_val in zip(self.__pk__, new_pk_val):
            col, val = pk_val
            self._current[col] = val

        self._initial = self._current.copy()

    def _flush_modified(self, session, cursor):
        diff = self._diff
        col_names = []
        col_values = []

        for f, (was, is_) in diff: #@UnusedVariable
            cvals = f.get_c_vals(self._current)
            for c, v in cvals.items():
                col_names.append('"{0}" = %s'.format(c))
                col_values.append(v)

        if not col_names: #Might happend when modified field has no columns
            return

        pk = self._current['id']
        col_names = ', '.join(col_names)

        cursor.execute(
            'UPDATE {0} SET {1} WHERE id = %s'.format(
                self.__table_name__, col_names
            ), col_values + [pk]
        )

    def _do_delete(self, session, cursor):
        """Performs the actual deletion from database"""
        #for field in self.fields:
        #    if field.before_delete is not None:
        #        field.before_delete(self, session, cursor)
        cursor.execute(
            'DELETE FROM {0} WHERE {1} = %s'.format(
                self.__table_name__,
                prepare_col(type(self).get_pk()),
            ),
            [self.get_pk_value()],
        )
        for field in self.fields:
            if field.after_delete is not None:
                field.after_delete(self, session, cursor)

    def set_orphan_status(self):
        """Sets the status to ORPHANED if it makes sense"""
        if self._status != DELETED:
            self._status = ORPHANED

    def check_orphan_status(self):
        """Checks if the object is still an orphan and deletes the orphan status
        appropriately"""
        for f in self.fields:
            if f.orphaned:
                self.set_orphan_status()
                return
        if self._status == ORPHANED:
            self._status = MODIFIED

    def delete(self):
        """Marks this object as deleted"""
        self._status = DELETED

    def flush(self, session, cursor):
        """
Flush this object to database using given cursor
        """
        if self._status == NEW:
            self._flush_new(session, cursor)
        elif self._status == MODIFIED:
            self._flush_modified(session, cursor)
        elif self._status in [DELETED, ORPHANED]:
            self._do_delete(session, cursor)

    @classmethod
    def _get_conditions(cls, *args, **kwargs):
        """Reformats args and kwargs to a ``Condition`` object"""
        from eplasty import conditions as cond
        args = list(args)
        # for k, v in kwargs.items():
        #     args.append(cond.Equals(k, v))

        if not args:
            return cond.All()

        return cond.And(*args)

    @classmethod
    def _get_query(
        cls, condition, order, fields=None, limit=None, offset=None
    ):
        """Returns tuple to be executed for this class and given
        ``condition``"""
        if fields is None:
            columns = cls.columns
        else:
            columns = sum((getattr(cls, field).columns for field in fields), [])
        return SelectQuery(
            cls.__table_name__, columns = columns, condition = condition,
            order = order, limit=limit, offset=offset,
        ).render()

    @classmethod
    def get(cls, id = None, session = None, *args, **kwargs):
        from eplasty import conditions as cond
        args = list(args)
        session = get_session(session)
        cached =  session.find_cached(cls.__table_name__, id)
        if cached:
            return cached
        if isinstance(id, cond.Condition):
            args.append(id)
        elif id is not None:
            args.append(cond.Equals(cls.get_pk(), id))

        cursor = session.cursor()

        condition = cls._get_conditions(*args, **kwargs)
        order = kwargs.get('order', [])
        try:
            cursor.execute(*cls._get_query(condition, order))
        except ProgrammingError as err:
            if err.pgcode == UNDEFINED_TABLE:
                session._rollback(True)
                raise NotFound("Didn't find anything")
            else:
                raise

        if cursor.rowcount == 1:
            r = cls.hydrate(cursor.fetchone(), session)
            session.add(r)
            return r
        elif cursor.rowcount == 0:
            raise NotFound("Didn't find anything")
        else:
            raise TooManyFound("Found more than one row")

    @classmethod
    def find(cls, *args, **kwargs):
        """
        Execute the query on this object class and return a ``Result`` object.
        """
        s = kwargs.pop('session', None)
        session = get_session(s)
        tmp_cursor = session.cursor()
        condition = cls._get_conditions(*args, **kwargs)

        order = kwargs.pop('order', [])
        fields = kwargs.pop('fields', None)
        limit = kwargs.pop('limit', None)
        offset = kwargs.pop('offset', None)
        query = cls._get_query(condition, order, fields, limit, offset)
        query_hash = b64encode(sha1(tmp_cursor.mogrify(*query)).digest())
        cursor = session.cursor()
        try:
            cursor.execute(*query)
        except ProgrammingError as err:
            if err.pgcode == UNDEFINED_TABLE:
                session._rollback(True)
                return []
            else:
                raise

        return Result(session, cursor, cls, fields)

    @classmethod
    def get_pk(cls):
        """Get the tuple of columns acting as pk"""
        return tuple((c for c in cls.columns if c.name in cls.__pk__))

    def get_pk_value(self):
        """Get the tuple of values for the pk or None"""
        try:
            return tuple((self._current[k] for k in type(self).__pk__))
        except KeyError:
            return None

    def _has_unflushed_dependencies(self):
        """Tells if there are some objects that should be flushed before this
        one"""
        result = []
        for f in self.fields:
            for d in f.get_dependencies(self._current):
                if (
                    not isinstance(d, LazyQuery) and
                    (d._status == NEW and not d._flushed)
                ):
                    result.append(d)
        return result or False

    def bind_session(self, session):
        """Sets the current session"""
        self.session = session
        for f in self.fields:
            f.bind_session(self, session)
        session.add(*self.temporary)
        self.temporary = []

    def add(self, *os):
        """Adds an object to this object's session or to a temporary storage
        (to be added when adding this one"""
        if self.session is not None:
            self.session.add(*os)
        else:
            self.temporary += os

    def touch(self):
        """Set status to modified if applicable"""
        if self._status in [UNCHANGED, UPDATED]:
            self._status = MODIFIED