<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# BotFWD - A telegram bot to forward random messages from a channel

"""
Usage:
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""

import random as rd
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, Job)
import logging

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
    update.message.reply_text('Olá! Eu sou o BotFWD e existo unicamente para'
                              ' causar o desconforto nas pessoas!')
    bot.send_message(chat_id=update.message.chat_id,
                     text="Meus comandos:\n"
                          "Iniciar o bot: /start\n"
                          "Mensagem aleatória: /random\n"
                          "Ajuda: /help")

def help(bot, update):
    update.message.reply_text('Sem ajuda malandro!')
    bot.send_message(chat_id=update.message.chat_id,
                     text="Quer ajudar a desenvolver o bot?\n"
                          "https://goo.gl/x3jDri")

def random(bot, update):
    # update.message.reply_text('Em manutenção :(')
    messageID = rd.randint(6, 175)
    try:
        bot.forwardMessage(update.message.chat_id, '@botFwdTeste', messageID)
        print("Success => message_id %d" % messageID)
    except:
        print("Error => message_id %d does not exist" % messageID)
        random(bot, update)

def debug(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     text="Toschi, para de tentar fazer merda de novo.")

   # print("Chat id %d" % update.message.chat_id)
   # print("Message id %d" % update.message.message_id)
   # for x in range(0, 175):
   #     try:
   #         # bot.forwardMessage(update.message.chat_id, '@ofwdnovo', messageID)
   #         bot.forwardMessage(update.message.chat_id, '@botFwdTeste', x)
   #         print("Success => message_id %d" % x)
   #     except:
   #         print("Error => message_id %d does not exist" % x)
   #         continue

def lero(bot, update):
    t0 = [  'Caros amigos, ',
            'Por outro lado, ',
            'Assim mesmo, ',
            'No entanto, não podemos esquecer que ',
            'Do mesmo modo, ',
            'A prática cotidiana prova que ',
            'Nunca é demais lembrar o peso e o significado destes problemas, uma vez que ',
            'As experiências acumuladas demonstram que ',
            'Acima de tudo, é fundamental ressaltar que ',
            'O incentivo ao avanço tecnológico, assim como ',
            'Não obstante, ',
            'Todas estas questões, devidamente ponderadas, levantam dúvidas sobre se ',
            'Pensando mais a longo prazo, ',
            'O que temos que ter sempre em mente é que ',
            'Ainda assim, existem dúvidas a respeito de como ',
            'Gostaria de enfatizar que ',
            'Todavia, ',
            'A nível organizacional, ',
            'O empenho em analisar ',
            'Percebemos, cada vez mais, que ',
            'No mundo atual, ',
            'É importante questionar o quanto ',
            'Neste sentido, ',
            'Evidentemente, ',
            'Por conseguinte, ',
            'É claro que ',
            'Podemos já vislumbrar o modo pelo qual ',
            'Desta maneira, ',
            'O cuidado em identificar pontos críticos n',
            'A certificação de metodologias que nos auxiliam a lidar com ' ]

    t1 = [  'a execução dos pontos do programa ',
            'a complexidade dos estudos efetuados ',
            'a contínua expansão de nossa atividade ',
            'a estrutura atual da organização ',
            'o novo modelo estrutural aqui preconizado ',
            'o desenvolvimento contínuo de distintas formas de atuação ',
            'a constante divulgação das informações ',
            'a consolidação das estruturas ',
            'a consulta aos diversos militantes ',
            'o início da atividade geral de formação de atitudes ',
            'o desafiador cenário globalizado ',
            'a mobilidade dos capitais internacionais ',
            'o fenômeno da Internet ',
            'a hegemonia do ambiente político ',
            'a expansão dos mercados mundiais ',
            'o aumento do diálogo entre os diferentes setores produtivos ',
            'a crescente influência da mídia ',
            'a necessidade de renovação processual ',
            'a competitividade nas transações comerciais ',
            'o surgimento do comércio virtual ',
            'a revolução dos costumes ',
            'o acompanhamento das preferências de consumo ',
            'o comprometimento entre as equipes ',
            'a determinação clara de objetivos ',
            'a adoção de políticas descentralizadoras ',
            'a valorização de fatores subjetivos ',
            'a percepção das dificuldades ',
            'o entendimento das metas propostas ',
            'o consenso sobre a necessidade de qualificação ',
            'o julgamento imparcial das eventualidades ' ]

    t2 = [  'nos obriga à análise ',
            'cumpre um papel essencial na formulação ',
            'exige a precisão e a definição ',
            'auxilia a preparação e a composição ',
            'garante a contribuição de um grupo importante na determinação ',
            'assume importantes posições no estabelecimento ',
            'facilita a criação ',
            'obstaculiza a apreciação da importância ',
            'oferece uma interessante oportunidade para verificação ',
            'acarreta um processo de reformulação e modernização ',
            'pode nos levar a considerar a reestruturação ',
            'representa uma abertura para a melhoria ',
            'ainda não demonstrou convincentemente que vai participar na mudança ',
            'talvez venha a ressaltar a relatividade ',
            'prepara-nos para enfrentar situações atípicas decorrentes ',
            'maximiza as possibilidades por conta ',
            'desafia a capacidade de equalização ',
            'agrega valor ao estabelecimento ',
            'é uma das consequências ',
            'promove a alavancagem ',
            'não pode mais se dissociar ',
            'possibilita uma melhor visão global ',
            'estimula a padronização ',
            'aponta para a melhoria ',
            'faz parte de um processo de gerenciamento ',
            'causa impacto indireto na reavaliação ',
            'apresenta tendências no sentido de aprovar a manutenção ',
            'estende o alcance e a importância ',
            'deve passar por modificações independentemente ',
            'afeta positivamente a correta previsão ' ]

    t3 = [  'das condições financeiras e administrativas exigidas.',
            'das diretrizes de desenvolvimento para o futuro.',
            'do sistema de participação geral.',
            'das posturas dos órgãos dirigentes com relação às suas atribuições.',
            'das novas proposições.',
            'das direções preferenciais no sentido do progresso.',
            'do sistema de formação de quadros que corresponde às necessidades.',
            'das condições inegavelmente apropriadas.',
            'dos índices pretendidos.',
            'das formas de ação.',
            'dos paradigmas corporativos.',
            'dos relacionamentos verticais entre as hierarquias.',
            'do processo de comunicação como um todo.',
            'dos métodos utilizados na avaliação de resultados.',
            'de todos os recursos funcionais envolvidos.',
            'dos níveis de motivação departamental.',
            'da gestão inovadora da qual fazemos parte.',
            'dos modos de operação convencionais.',
            'de alternativas às soluções ortodoxas.',
            'dos procedimentos normalmente adotados.',
            'dos conhecimentos estratégicos para atingir a excelência.',
            'do fluxo de informações.',
            'do levantamento das variáveis envolvidas.',
            'das diversas correntes de pensamento.',
            'do impacto na agilidade decisória.',
            'das regras de conduta normativas.',
            'do orçamento setorial.',
            'do retorno esperado a longo prazo.',
            'do investimento em reciclagem técnica.',
            'do remanejamento dos quadros funcionais.' ]

    leroLero = rd.choice(t0) + rd.choice(t1) + rd.choice(t2) + rd.choice(t3)
    bot.send_message(chat_id=update.message.chat_id, text=leroLero)

def wordGenerate(bot, update):
    consonants = 'bcdfghjlmnpqrstvxz'
    vowels = 'aeiou'
    syllables = rd.randint(2, 10)
    result = ''

    for i in range(syllables):
        consonant = rd.choice(consonants)
        this_vowels = vowels
        if consonant == 'q':
            consonant += 'u'
            this_vowels = 'aeio'
        result += consonant + rd.choice(this_vowels)
        if i > 0 and result[-2] in 'bp' and rd.randint(0, 5) == 0:
            result = result[:-2] + 'm' + result[-2:]
        if i > 0 and result[-2] in 'cglrst' and rd.randint(0, 5) == 0:
            result = result[:-2] + 'n' + result[-2:]

    return result

def word(bot, update):
    result = wordGenerate(bot, update)
    bot.send_message(chat_id=update.message.chat_id, text=result)

def aplicacaonumafrase(bot, update):
    word = wordGenerate(bot, update)

    with open('aplicacaonumafrase.txt') as f:
        frases = [l.rstrip('\n') for l in f]

    result = rd.choice(frases).format(word=word)

    bot.send_message(chat_id=update.message.chat_id, text=result)

def filme(bot, update):
    palavras = ['cu', 'pinto', 'anus', 'pipi', 'temer', 'caralho']

    with open('filme.txt') as f:
        frases = [l.rstrip('\n').lower() for l in f]

    result = rd.choice(frases).format(word=rd.choice(palavras))
	result = result.replace('ânuss', 'ânus') # caso de borda

    bot.send_message(chat_id=update.message.chat_id, text=result)

def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))

def main():
    # Create the EventHandler and pass it your bot's token.
    updater = Updater("390975324:AAG57sa1pBQ9Swk7ry-I4FJijWOc1XZYM5s")

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(CommandHandler("random", random))
    dp.add_handler(CommandHandler("fwd", random))
    dp.add_handler(CommandHandler("lero", lero))
    dp.add_handler(CommandHandler("lerolero", lero))
    dp.add_handler(CommandHandler("word", word))
    dp.add_handler(CommandHandler("palavra", word))
    dp.add_handler(CommandHandler("aplicacaonumafrase", aplicacaonumafrase))
    dp.add_handler(CommandHandler("frase", aplicacaonumafrase))
    dp.add_handler(CommandHandler("filme", filme))
    dp.add_handler(CommandHandler("debug", debug))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()
    print('==== BOT started ====')

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()<commit_msg>Tratamento de caso de borda do /filme e cleanup geral<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# BotFWD - A telegram bot to forward random messages from a channel

"""
Usage:
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""

import random as rd
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, Job)
import logging

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
    update.message.reply_text('Olá! Eu sou o BotFWD e existo unicamente para'
                              ' causar o desconforto nas pessoas!')
    bot.send_message(chat_id=update.message.chat_id,
                     text="Meus comandos:\n"
                          "Iniciar o bot: /start\n"
                          "Mensagem aleatória: /random\n"
                          "Ajuda: /help")

def help(bot, update):
    update.message.reply_text('Sem ajuda malandro!')
    bot.send_message(chat_id=update.message.chat_id,
                     text="Quer ajudar a desenvolver o bot?\n"
                          "https://goo.gl/x3jDri")

def random(bot, update):
    # update.message.reply_text('Em manutenção :(')
    messageID = rd.randint(6, 175)
    try:
        bot.forwardMessage(update.message.chat_id, '@botFwdTeste', messageID)
        print("Success => message_id %d" % messageID)
    except:
        print("Error => message_id %d does not exist" % messageID)
        random(bot, update)

def debug(bot, update):
    bot.send_message(chat_id=update.message.chat_id,
                     text="Toschi, para de tentar fazer merda de novo.")

   # print("Chat id %d" % update.message.chat_id)
   # print("Message id %d" % update.message.message_id)
   # for x in range(0, 175):
   #     try:
   #         # bot.forwardMessage(update.message.chat_id, '@ofwdnovo', messageID)
   #         bot.forwardMessage(update.message.chat_id, '@botFwdTeste', x)
   #         print("Success => message_id %d" % x)
   #     except:
   #         print("Error => message_id %d does not exist" % x)
   #         continue

def lero(bot, update):
    t0 = [  'Caros amigos, ',
            'Por outro lado, ',
            'Assim mesmo, ',
            'No entanto, não podemos esquecer que ',
            'Do mesmo modo, ',
            'A prática cotidiana prova que ',
            'Nunca é demais lembrar o peso e o significado destes problemas, uma vez que ',
            'As experiências acumuladas demonstram que ',
            'Acima de tudo, é fundamental ressaltar que ',
            'O incentivo ao avanço tecnológico, assim como ',
            'Não obstante, ',
            'Todas estas questões, devidamente ponderadas, levantam dúvidas sobre se ',
            'Pensando mais a longo prazo, ',
            'O que temos que ter sempre em mente é que ',
            'Ainda assim, existem dúvidas a respeito de como ',
            'Gostaria de enfatizar que ',
            'Todavia, ',
            'A nível organizacional, ',
            'O empenho em analisar ',
            'Percebemos, cada vez mais, que ',
            'No mundo atual, ',
            'É importante questionar o quanto ',
            'Neste sentido, ',
            'Evidentemente, ',
            'Por conseguinte, ',
            'É claro que ',
            'Podemos já vislumbrar o modo pelo qual ',
            'Desta maneira, ',
            'O cuidado em identificar pontos críticos n',
            'A certificação de metodologias que nos auxiliam a lidar com ' ]

    t1 = [  'a execução dos pontos do programa ',
            'a complexidade dos estudos efetuados ',
            'a contínua expansão de nossa atividade ',
            'a estrutura atual da organização ',
            'o novo modelo estrutural aqui preconizado ',
            'o desenvolvimento contínuo de distintas formas de atuação ',
            'a constante divulgação das informações ',
            'a consolidação das estruturas ',
            'a consulta aos diversos militantes ',
            'o início da atividade geral de formação de atitudes ',
            'o desafiador cenário globalizado ',
            'a mobilidade dos capitais internacionais ',
            'o fenômeno da Internet ',
            'a hegemonia do ambiente político ',
            'a expansão dos mercados mundiais ',
            'o aumento do diálogo entre os diferentes setores produtivos ',
            'a crescente influência da mídia ',
            'a necessidade de renovação processual ',
            'a competitividade nas transações comerciais ',
            'o surgimento do comércio virtual ',
            'a revolução dos costumes ',
            'o acompanhamento das preferências de consumo ',
            'o comprometimento entre as equipes ',
            'a determinação clara de objetivos ',
            'a adoção de políticas descentralizadoras ',
            'a valorização de fatores subjetivos ',
            'a percepção das dificuldades ',
            'o entendimento das metas propostas ',
            'o consenso sobre a necessidade de qualificação ',
            'o julgamento imparcial das eventualidades ' ]

    t2 = [  'nos obriga à análise ',
            'cumpre um papel essencial na formulação ',
            'exige a precisão e a definição ',
            'auxilia a preparação e a composição ',
            'garante a contribuição de um grupo importante na determinação ',
            'assume importantes posições no estabelecimento ',
            'facilita a criação ',
            'obstaculiza a apreciação da importância ',
            'oferece uma interessante oportunidade para verificação ',
            'acarreta um processo de reformulação e modernização ',
            'pode nos levar a considerar a reestruturação ',
            'representa uma abertura para a melhoria ',
            'ainda não demonstrou convincentemente que vai participar na mudança ',
            'talvez venha a ressaltar a relatividade ',
            'prepara-nos para enfrentar situações atípicas decorrentes ',
            'maximiza as possibilidades por conta ',
            'desafia a capacidade de equalização ',
            'agrega valor ao estabelecimento ',
            'é uma das consequências ',
            'promove a alavancagem ',
            'não pode mais se dissociar ',
            'possibilita uma melhor visão global ',
            'estimula a padronização ',
            'aponta para a melhoria ',
            'faz parte de um processo de gerenciamento ',
            'causa impacto indireto na reavaliação ',
            'apresenta tendências no sentido de aprovar a manutenção ',
            'estende o alcance e a importância ',
            'deve passar por modificações independentemente ',
            'afeta positivamente a correta previsão ' ]

    t3 = [  'das condições financeiras e administrativas exigidas.',
            'das diretrizes de desenvolvimento para o futuro.',
            'do sistema de participação geral.',
            'das posturas dos órgãos dirigentes com relação às suas atribuições.',
            'das novas proposições.',
            'das direções preferenciais no sentido do progresso.',
            'do sistema de formação de quadros que corresponde às necessidades.',
            'das condições inegavelmente apropriadas.',
            'dos índices pretendidos.',
            'das formas de ação.',
            'dos paradigmas corporativos.',
            'dos relacionamentos verticais entre as hierarquias.',
            'do processo de comunicação como um todo.',
            'dos métodos utilizados na avaliação de resultados.',
            'de todos os recursos funcionais envolvidos.',
            'dos níveis de motivação departamental.',
            'da gestão inovadora da qual fazemos parte.',
            'dos modos de operação convencionais.',
            'de alternativas às soluções ortodoxas.',
            'dos procedimentos normalmente adotados.',
            'dos conhecimentos estratégicos para atingir a excelência.',
            'do fluxo de informações.',
            'do levantamento das variáveis envolvidas.',
            'das diversas correntes de pensamento.',
            'do impacto na agilidade decisória.',
            'das regras de conduta normativas.',
            'do orçamento setorial.',
            'do retorno esperado a longo prazo.',
            'do investimento em reciclagem técnica.',
            'do remanejamento dos quadros funcionais.' ]

    leroLero = rd.choice(t0) + rd.choice(t1) + rd.choice(t2) + rd.choice(t3)
    bot.send_message(chat_id=update.message.chat_id, text=leroLero)

def wordGenerate(bot, update):
    consonants = 'bcdfghjlmnpqrstvxz'
    vowels = 'aeiou'
    syllables = rd.randint(2, 10)
    result = ''

    for i in range(syllables):
        consonant = rd.choice(consonants)
        this_vowels = vowels
        if consonant == 'q':
            consonant += 'u'
            this_vowels = 'aeio'
        result += consonant + rd.choice(this_vowels)
        if i > 0 and result[-2] in 'bp' and rd.randint(0, 5) == 0:
            result = result[:-2] + 'm' + result[-2:]
        if i > 0 and result[-2] in 'cglrst' and rd.randint(0, 5) == 0:
            result = result[:-2] + 'n' + result[-2:]

    return result

def word(bot, update):
    result = wordGenerate(bot, update)
    bot.send_message(chat_id=update.message.chat_id, text=result)

def aplicacaonumafrase(bot, update):
    word = wordGenerate(bot, update)

    with open('aplicacaonumafrase.txt') as f:
        frases = [l.rstrip('\n') for l in f]

    result = rd.choice(frases).format(word=word)

    bot.send_message(chat_id=update.message.chat_id, text=result)

def filme(bot, update):
    palavras = ['cu', 'pinto', 'ânus', 'pipi', 'temer', 'caralho']

    with open('filme.txt') as f:
        frases = [l.rstrip('\n').lower() for l in f]

    result = rd.choice(frases).format(word=rd.choice(palavras))
	result = result.replace('ânuss', 'ânus') # caso de borda

    bot.send_message(chat_id=update.message.chat_id, text=result)

def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))

def main():
    # Create the EventHandler and pass it your bot's token.
    updater = Updater("390975324:AAG57sa1pBQ9Swk7ry-I4FJijWOc1XZYM5s")

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(CommandHandler("random", random))
    dp.add_handler(CommandHandler("fwd", random))
    dp.add_handler(CommandHandler("lero", lero))
    dp.add_handler(CommandHandler("lerolero", lero))
    dp.add_handler(CommandHandler("word", word))
    dp.add_handler(CommandHandler("palavra", word))
    dp.add_handler(CommandHandler("aplicacaonumafrase", aplicacaonumafrase))
    dp.add_handler(CommandHandler("frase", aplicacaonumafrase))
    dp.add_handler(CommandHandler("filme", filme))
    dp.add_handler(CommandHandler("debug", debug))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()
    print('==== BOT started ====')

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()<|endoftext|>


<commit_before>import web
import jsonrpclib
import gettext

import os
import sys
import logging
import config

from time import sleep

# i18n directory.
localedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'i18n')

server = None #Ugly hack with globals, need to think of better way to store the variable

class ConnectionManager():

    def __init__(self, hostname, port):
        self.hostname = hostname
        self.port = port
        self.server = None

    def start_server(self):
        global server
        server = MiddleServer()

class MiddleServer():
    def __getattr__(self, name): # Google about overriding getattr in python
        server = jsonrpclib.Server('http://{}:{}'.format('localhost', 8070))
        method = server.__getattr__(name)
        return method
        #response = method(self, )
        #server.stop() #not sure if this is correct, testing required.
        #return response

class DeviceControl():

    def GET(self):
        global server
        game_info = {'state':server.__getattr__("get_game_state")(), 'name':'Museum'}
        time_left = server.get_time_left()
        devices = server.get_devices()
        return render.devices(game_info, devices)

    def POST(self):
        post_data = web.input()
        print(post_data)
        try:
            action = post_data['action']
        except KeyError:
            return False
        if action == 'get_devices':
            try:
                devices = server.get_devices()
            except: #TODO: monitor for exceptions as they appear
                raise
            else:
                return devices
        elif action == 'test_devices':
            try:
                #test_result = server.test_devices()
                test_result = MiddleServer.__getAttr__("test_devices")()
            except: #TODO: monitor for exceptions as they appear
                raise
            else:
                return devices
        else:
            pass #Maybe issue a warning?

class GameControl():

    def GET(self):

        print "#"
#        try:
#            print "##"
#            #game_info = {'state':server.get_game_state(), 'name':'Museum'}
#            game_info = {'state':MiddleServer.get_game_state(), 'name':'Museum'}
#        except:
#            print "###"
#            sleep(1)
#            pass

        print "#!"
        game_info = {'state':server.__getattr__("get_game_state"), 'name':'Museum'}
        #game_info = {'state':server.get_game_state(), 'name':'Museum'}
        time_left = server.__getattr__("get_time_left")
        steps = server.__getattr__("get_steps")()
        #Modifying steps for nice UI output
        step_num_dict = {}
        for index, step in enumerate(steps):
            step['number'] = index + 1 #Assigning numbers to steps for the UI
            step_num_dict[step['name']] = step['number'] #Making step_name:step_number mappings
        for index, step in enumerate(steps): #Doing a second pass to ensure all the mappings we need are there
            step['stepnum_that_enable'] = [str(step_num_dict[step_name]) for step_name in step['steps_that_enable'] if step_name != 'start'] #An exception: 'start' step is not an actual step
            if not step['stepnum_that_enable']:
                 step['stepnum_that_enable'] = ["Start"]
        return render.game(game_info, time_left, steps)

    def POST(self):
        post_data = web.input()
        print(post_data)
        try:
            action = post_data['action']
        except KeyError:
            return False
        if action == 'finish_step':
            try:
                step_id = post_data["step_name"]
                step_index = step_id[5:]
                step_index = int(step_index)-1
                steps = server.get_steps()
                step_name = steps[step_index]['name']
            except KeyError:
                raise
            try:
                server.finish_step(step_name)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'enable_step':
            try:
                step_id = post_data["step_name"]
                step_index = step_id[5:]
                step_index = int(step_index)-1
                steps = server.__getattr__("get_steps")
                step_name = steps[step_index]['name']
            except KeyError:
                raise

            try:
                server.enable_step("", step_name)
            except:
                raise

            #try:
            #    server.enable_step(step_name)
            #except: #TODO: monitor for exceptions as they appear
            #    raise
        elif action == 'start':
            try:
                len_minutes = post_data["minutes"] #Length, for now only minutes are supported
            except KeyError:
                len_minutes = 60
            try:
                p_count = post_data["p_count"] #Participant count
            except KeyError:
                p_count = 1
            try:
                server.start_game(p_count, len_minutes*60)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'stop':
            try:
                server.stop_game()
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'get_time':
            try:
                return {"time_left":server.get_time_left()}
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'decrease_t':
            try:
                seconds = post_data["seconds"]
            except KeyError:
                seconds = 0
            try:
                server.decrease_time(seconds)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'increase_t':
            try:
                seconds = post_data["seconds"]
            except KeyError:
                seconds = 0
            try:
                server.increase_time(seconds)
            except: #TODO: monitor for exceptions as they appear
                raise
        else:
            pass #Maybe issue a warning?

"""actions = {
"Stress test":server.stress_test,
}"""

if __name__ == "__main__":
    urls = (
        '/', 'GameControl',
        '/steps', 'GameControl'
        #'/devices', 'DeviceControl'
    )
    app = web.application(urls, globals())
    #gettext.install('messages', localedir, unicode=True)
    #gettext.translation('messages', localedir, languages=['lv_LV']).install(True)
    #render = web.template.render('templates/', globals={'_': _})
    render = web.template.render('templates/')
    app.internalerror = web.debugerror
    conn_manager = ConnectionManager('localhost', 8070)
    print("Starting server...")
    conn_manager.start_server()
    print("Starting game...")
    app.run()
<commit_msg>server WIP<commit_after>import web
import jsonrpclib
import gettext

import os
import sys
import logging
import config

from time import sleep

# i18n directory.
localedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'i18n')

server = None #Ugly hack with globals, need to think of better way to store the variable

class ConnectionManager():

    def __init__(self, hostname, port):
        self.hostname = hostname
        self.port = port
        self.server = None

    def start_server(self):
        global server
        server = MiddleServer()

class MiddleServer():
    def __getattr__(self, name): # Google about overriding getattr in python
        server = jsonrpclib.Server('http://{}:{}'.format('localhost', 8070))
        method = server.__getattr__(name)
        return method
        #response = method(self, )
        #server.stop() #not sure if this is correct, testing required.
        #return response

class DeviceControl():

    def GET(self):
        global server
        game_info = {'state':server.__getattr__("get_game_state")(), 'name':'Museum'}
        time_left = server.get_time_left()
        devices = server.get_devices()
        return render.devices(game_info, devices)

    def POST(self):
        post_data = web.input()
        print(post_data)
        try:
            action = post_data['action']
        except KeyError:
            return False
        if action == 'get_devices':
            try:
                devices = server.get_devices()
            except: #TODO: monitor for exceptions as they appear
                raise
            else:
                return devices
        elif action == 'test_devices':
            try:
                #test_result = server.test_devices()
                test_result = MiddleServer.__getAttr__("test_devices")()
            except: #TODO: monitor for exceptions as they appear
                raise
            else:
                return devices
        else:
            pass #Maybe issue a warning?

class GameControl():

    def GET(self):

        print "#"
#        try:
#            print "##"
#            #game_info = {'state':server.get_game_state(), 'name':'Museum'}
#            game_info = {'state':MiddleServer.get_game_state(), 'name':'Museum'}
#        except:
#            print "###"
#            sleep(1)
#            pass

        print "#!"
        game_info = {'state':server.__getattr__("get_game_state"), 'name':'Museum'}
        #game_info = {'state':server.get_game_state(), 'name':'Museum'}
        time_left = server.__getattr__("get_time_left")
        steps = server.__getattr__("get_steps")()
        #Modifying steps for nice UI output
        step_num_dict = {}
        for index, step in enumerate(steps):
            step['number'] = index + 1 #Assigning numbers to steps for the UI
            step_num_dict[step['name']] = step['number'] #Making step_name:step_number mappings
        for index, step in enumerate(steps): #Doing a second pass to ensure all the mappings we need are there
            step['stepnum_that_enable'] = [str(step_num_dict[step_name]) for step_name in step['steps_that_enable'] if step_name != 'start'] #An exception: 'start' step is not an actual step
            if not step['stepnum_that_enable']:
                 step['stepnum_that_enable'] = ["Start"]

        print "3####
        for i in game_info: print i
        return render.game(game_info, time_left, steps) #game info State is failing, check what's in there

    def POST(self):
        post_data = web.input()
        print(post_data)
        try:
            action = post_data['action']
        except KeyError:
            return False
        if action == 'finish_step':
            try:
                step_id = post_data["step_name"]
                step_index = step_id[5:]
                step_index = int(step_index)-1
                steps = server.get_steps()
                step_name = steps[step_index]['name']
            except KeyError:
                raise
            try:
                server.finish_step(step_name)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'enable_step':
            try:
                step_id = post_data["step_name"]
                step_index = step_id[5:]
                step_index = int(step_index)-1
                steps = server.__getattr__("get_steps")
                step_name = steps[step_index]['name']
            except KeyError:
                raise

            try:
                server.enable_step("", step_name)
            except:
                raise

            #try:
            #    server.enable_step(step_name)
            #except: #TODO: monitor for exceptions as they appear
            #    raise
        elif action == 'start':
            try:
                len_minutes = post_data["minutes"] #Length, for now only minutes are supported
            except KeyError:
                len_minutes = 60
            try:
                p_count = post_data["p_count"] #Participant count
            except KeyError:
                p_count = 1
            try:
                server.start_game(p_count, len_minutes*60)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'stop':
            try:
                server.stop_game()
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'get_time':
            try:
                return {"time_left":server.get_time_left()}
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'decrease_t':
            try:
                seconds = post_data["seconds"]
            except KeyError:
                seconds = 0
            try:
                server.decrease_time(seconds)
            except: #TODO: monitor for exceptions as they appear
                raise
        elif action == 'increase_t':
            try:
                seconds = post_data["seconds"]
            except KeyError:
                seconds = 0
            try:
                server.increase_time(seconds)
            except: #TODO: monitor for exceptions as they appear
                raise
        else:
            pass #Maybe issue a warning?

"""actions = {
"Stress test":server.stress_test,
}"""

if __name__ == "__main__":
    urls = (
        '/', 'GameControl',
        '/steps', 'GameControl'
        #'/devices', 'DeviceControl'
    )
    app = web.application(urls, globals())
    #gettext.install('messages', localedir, unicode=True)
    #gettext.translation('messages', localedir, languages=['lv_LV']).install(True)
    #render = web.template.render('templates/', globals={'_': _})
    render = web.template.render('templates/')
    app.internalerror = web.debugerror
    conn_manager = ConnectionManager('localhost', 8070)
    print("Starting server...")
    conn_manager.start_server()
    print("Starting game...")
    app.run()
<|endoftext|>


<commit_before>from ctypes import *
import ctypes.util

def load_fs():
    path = ctypes.util.find_library("fluidsynth")
    print "[fs] Using %s for FluidSynth library" % path
    libfs = cdll.LoadLibrary(path)

    # Argtypes
    libfs.fluid_settings_setint.argtypes = [c_void_p, c_char_p, c_int]
    libfs.fluid_settings_setnum.argtypes = [c_void_p, c_char_p, c_double]
    libfs.fluid_settings_setstr.argtypes = [c_void_p, c_char_p, c_char_p]

    return libfs

class FSSettings(object):
    def __init__(self, libfs):
        self.libfs = libfs
        self.settings = self.libfs.new_fluid_settings()

    def set_float(self, key, value):
        self.libfs.fluid_settings_setnum(self.settings, key, value)

    def set_int(self, key, value):
        self.libfs.fluid_settings_setint(self.settings, key, value)

    def set_string(self, key, value):
        self.libfs.fluid_settings_setstr(self.settings, key, value)

class FS(object):
    def __init__(self):
        self.libfs = load_fs()

        self.settings = FSSettings(self.libfs)
        self.settings.set_string("synth.chorus.active", "no")
        self.settings.set_string("synth.reverb.active", "no")

        self.synth = self.libfs.new_fluid_synth(self.settings.settings)

        self.sfd = {}

    def __del__(self):
        self.destroy()

    def destroy(self):
        for sf in self.sfd:
            self.unloadsf(sf)

        self.libfs.delete_fluid_synth(self.synth)
        self.libfs.delete_fluid_settings(self.settings.settings)

    def loadsf(self, sf):
        if sf in self.sfd:
            id = self.libfs.fluid_synth_sfreload(self.synth, self.sfd[sf])
        else:
            id = self.libfs.fluid_synth_sfload(self.synth, sf, True)
            self.sfd[sf] = id
        if id == -1:
            print "[fs] Error loading soundfont %s" % sf
        else:
            print "[fs] Soundfont %s loaded successfully" % sf

    def unloadsf(self, sf):
        if sf in self.sfd:
            if self.libfs.fluid_synth_sfunload(self.synth, self.sfd[sf],
                    True):
                print "[fs] Error unloading soundfont %s" % sf
            else:
                print "[fs] Soundfont %s unloaded successfully" % sf
        else:
            print "[fs] Soundfont %s was never loaded" % sf
<commit_msg>Automated test.<commit_after>#!/usr/bin/env python

from ctypes import *
import ctypes.util

def load_fs():
    path = ctypes.util.find_library("fluidsynth")
    print "[fs] Using %s for FluidSynth library" % path
    libfs = cdll.LoadLibrary(path)

    # Argtypes
    libfs.fluid_settings_setint.argtypes = [c_void_p, c_char_p, c_int]
    libfs.fluid_settings_setnum.argtypes = [c_void_p, c_char_p, c_double]
    libfs.fluid_settings_setstr.argtypes = [c_void_p, c_char_p, c_char_p]

    return libfs

class FSSettings(object):
    def __init__(self, libfs):
        self.libfs = libfs
        self.settings = self.libfs.new_fluid_settings()

    def set_float(self, key, value):
        self.libfs.fluid_settings_setnum(self.settings, key, value)

    def set_int(self, key, value):
        self.libfs.fluid_settings_setint(self.settings, key, value)

    def set_string(self, key, value):
        self.libfs.fluid_settings_setstr(self.settings, key, value)

class FS(object):
    def __init__(self):
        self.libfs = load_fs()

        self.settings = FSSettings(self.libfs)
        self.settings.set_string("synth.chorus.active", "no")
        self.settings.set_string("synth.reverb.active", "no")

        self.synth = self.libfs.new_fluid_synth(self.settings.settings)

        self.sfd = {}

    def __del__(self):
        self.destroy()

    def destroy(self):
        for sf in self.sfd:
            self.unloadsf(sf)

        self.libfs.delete_fluid_synth(self.synth)
        self.libfs.delete_fluid_settings(self.settings.settings)

    def loadsf(self, sf):
        if sf in self.sfd:
            id = self.libfs.fluid_synth_sfreload(self.synth, self.sfd[sf])
        else:
            id = self.libfs.fluid_synth_sfload(self.synth, sf, True)
            self.sfd[sf] = id
        if id == -1:
            print "[fs] Error loading soundfont %s" % sf
        else:
            print "[fs] Soundfont %s loaded successfully" % sf

    def unloadsf(self, sf):
        if sf in self.sfd:
            if self.libfs.fluid_synth_sfunload(self.synth, self.sfd[sf],
                    True):
                print "[fs] Error unloading soundfont %s" % sf
            else:
                print "[fs] Soundfont %s unloaded successfully" % sf
        else:
            print "[fs] Soundfont %s was never loaded" % sf

def test():
    fs = FS()
    fs.loadsf("double.sf2")
    del fs

test()
<|endoftext|>


<commit_before>#!/usr/bin/env python
import asyncio
import logging
import sys

from discord.ext.commands import when_mentioned_or
import yaml

from bot import BeattieBot

try:
    import uvloop
except ImportError:
    pass
else:
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

with open('config/config.yaml') as file:
    config = yaml.load(file)

self_bot = 'self' in sys.argv

if self_bot:
    token = config['self']
    bot = BeattieBot('self>', self_bot=True)
else:
    token = config['token']
    if config['debug']:
        prefixes = [config['test_prefix']]
    else:
        prefixes = config['prefixes']
    bot = BeattieBot(when_mentioned_or(*prefixes))

extensions = ('cat', 'default', 'eddb', 'japanese', 'osu', 'nsfw', 'repl',
              'rpg', 'stats', 'wolfram', 'xkcd')

for extension in extensions:
    try:
        bot.load_extension(extension)
    except Exception as e:
        print(f'Failed to load extension {extension}\n{type(e).__name__}: {e}')

if self_bot:
    logger = logging.getLogger('discord')
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler(
        filename='discord.log', encoding='utf-8', mode='w')
    handler.setFormatter(
        logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
    logger.addHandler(handler)
else:
    logger = logging.getLogger('discord')
    logger.setLevel(logging.CRITICAL)
bot.logger = logger

bot.run(token, bot=not self_bot)
<commit_msg>Correct which logger is which<commit_after>#!/usr/bin/env python
import asyncio
import logging
import sys

from discord.ext.commands import when_mentioned_or
import yaml

from bot import BeattieBot

try:
    import uvloop
except ImportError:
    pass
else:
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

with open('config/config.yaml') as file:
    config = yaml.load(file)

self_bot = 'self' in sys.argv

if self_bot:
    token = config['self']
    bot = BeattieBot('self>', self_bot=True)
else:
    token = config['token']
    if config['debug']:
        prefixes = [config['test_prefix']]
    else:
        prefixes = config['prefixes']
    bot = BeattieBot(when_mentioned_or(*prefixes))

extensions = ('cat', 'default', 'eddb', 'japanese', 'osu', 'nsfw', 'repl',
              'rpg', 'stats', 'wolfram', 'xkcd')

for extension in extensions:
    try:
        bot.load_extension(extension)
    except Exception as e:
        print(f'Failed to load extension {extension}\n{type(e).__name__}: {e}')

if self_bot:
    logger = logging.getLogger('discord')
    logger.setLevel(logging.CRITICAL)
else:
    logger = logging.getLogger('discord')
    logger.setLevel(logging.DEBUG)
    handler = logging.FileHandler(
        filename='discord.log', encoding='utf-8', mode='w')
    handler.setFormatter(
        logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
    logger.addHandler(handler)
bot.logger = logger

bot.run(token, bot=not self_bot)
<|endoftext|>


<commit_before>import os, sys
import requests
import json
import random

# Set some initial constants
manifest_location = "http://1.webseed.robertsspaceindustries.com/FileIndex/sc-alpha-2.0.0/"
BUILD_RANGE = 10

##########################################################3##
#               ---- latest_version() ----                  #
# Description: 						    #
# Function searches the current directory for *.json files  #
# It returns the highest number it finds		    #
#############################################################
def latest_version(json_num = 0):
	dir_contents = [f for f in os.listdir('.') if os.path.isfile(f)]
	for item in dir_contents:
		last_json = 0
		if item.split('.')[-1] == 'json':
			json_num = item.split('.')[0]
			if last_json < json_num:
				last_json = json_num
		else:
			continue
	return json_num;

item_num = int(latest_version())

print "Found Latest: " + str(item_num)

for i in range (item_num + 1,item_num + BUILD_RANGE + 1):
	try:
		manifest_name = str(i) + ".json"

		# Print the latest json that we're trying to test for
		print 'Trying: {}\r'.format(manifest_name)
		manifest_response = requests.get(manifest_location + manifest_name)
		manifest_response.raise_for_status()

		# If it finds the file, doesn't throw an exception and writes the file to disk
		print "Writing: " + manifest_name
		manifest_fh = open(manifest_name, "w")
		manifest_fh.write(manifest_response.read())
		manifest_fh.close()
		item_num = manifest_name
	except Exception as e:
		if(r.status_code==404):
			print('%s: Page could not be found.' % e.reason)
		if(r.status_code>=500):
			print ('%s: Server error [%s]' % (e.reason,r.status_code))

# Open up and parse JSON data
json_fh = open(str(item_num) + ".json", "r")
parsed_json = json.load(json_fh)

# Setting constants based on JSON data
base_webseed_url = random.choice(parsed_json["webseed_urls"])
key_prefix = parsed_json["key_prefix"]

print "Using: {}".format(item_num)

#for file_name in parsed_json["file_list"]:
#	list = []
#	file_url = base_webseed_url + "/" + key_prefix + "/" + file_name
#	if "/" in file_name:
#		for item in file_name.split('/'):
#			list.append(item)
#		file_name = list.pop(-1)
#		file_path = os.path.join(*list)
#		if not os.path.isdir(file_path):
#			try:
#				os.makedirs(file_path)
#			except OSError:
#				print "Error creating directory: " + file_path
#
#	file = os.path.join(file_path, file_name)
#	# Begin Downloading Part
#
#	print("Downloading: %s" % file)
#
#	response = requests.get(file_url)
#
#	if os.path.isfile(file):
#		print("File Exists - Updating")
#
#	with open(file, "w") as fh:
#	    if not response.ok:
#	        print('File download was unsuccessful')
#	    else:
#	    	for block in response.iter_content(1024):
#	        fh.write(block)
<commit_msg>Added version_compare() method to better compare double digit version schemes<commit_after>import os, sys
import requests
import json
import random

# Set some initial constants
manifest_location = "http://1.webseed.robertsspaceindustries.com/FileIndex/sc-alpha-2.0.0/"
BUILD_RANGE = 10

##########################################################3##
#               ---- latest_version() ----                  #
# Description: 						    #
# Function searches the current directory for *.json files  #
# It returns the highest number it finds		    #
#############################################################
def latest_version(json_num = 0):
	dir_contents = [f for f in os.listdir('.') if os.path.isfile(f)]
	for item in dir_contents:
		last_json = 0
		if item.split('.')[-1] == 'json':
			json_num = item.split('.')[0]
			if last_json < json_num:
				last_json = json_num
		else:
			continue
	return int(json_num);

#############################################################
#               ---- version_compare() ----                 #
# Description:												#
# Function compares two versions by each portion of the		#
# version number. This allows double digit parts to be 		#
# compared individually, such as 2.11.0, which would be a 	#
# newer version than 2.8.0									#
#############################################################
def version_compare(num_check):
	num_split = num_check.split('.')
	try:
		latest_num_list=latest_num.split('.')
		if(isinstance(latest_num_list,list)):
			for ind,part in enumerate(num_split):
				if(part>latest_num_list[ind]):
					latest_num_list=num_split
				elif(part==latest_num_list[ind]):
					continue
				else:
					break
		else:
			latest_num=num_check
	except:
		latest_num=num_check
	return latest_num

item_num = latest_version()

print "Found Latest: " + str(item_num)

for i in range (item_num + 1,item_num + BUILD_RANGE + 1):
	try:
		manifest_name = str(i) + ".json"

		# Print the latest json that we're trying to test for
		print 'Trying: {}\r'.format(manifest_name)
		manifest_response = requests.get(manifest_location + manifest_name)
		manifest_response.raise_for_status()

		# If it finds the file, doesn't throw an exception and writes the file to disk
		print "Writing: " + manifest_name
		manifest_fh = open(manifest_name, "w")
		manifest_fh.write(manifest_response.read())
		manifest_fh.close()
		item_num = manifest_name
	except Exception as e:
		if(r.status_code==404):
			print('%s: Page could not be found.' % e.reason)
		if(r.status_code>=500):
			print ('%s: Server error [%s]' % (e.reason,r.status_code))

# Open up and parse JSON data
json_fh = open(str(item_num) + ".json", "r")
parsed_json = json.load(json_fh)

# Setting constants based on JSON data
base_webseed_url = random.choice(parsed_json["webseed_urls"])
key_prefix = parsed_json["key_prefix"]

print "Using: {}".format(item_num)

#for file_name in parsed_json["file_list"]:
#	list = []
#	file_url = base_webseed_url + "/" + key_prefix + "/" + file_name
#	if "/" in file_name:
#		for item in file_name.split('/'):
#			list.append(item)
#		file_name = list.pop(-1)
#		file_path = os.path.join(*list)
#		if not os.path.isdir(file_path):
#			try:
#				os.makedirs(file_path)
#			except OSError:
#				print "Error creating directory: " + file_path
#
#	file = os.path.join(file_path, file_name)
#	# Begin Downloading Part
#
#	print("Downloading: %s" % file)
#
#	response = requests.get(file_url)
#
#	if os.path.isfile(file):
#		print("File Exists - Updating")
#
#	with open(file, "w") as fh:
#	    if not response.ok:
#	        print('File download was unsuccessful')
#	    else:
#	    	for block in response.iter_content(1024):
#	        fh.write(block)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime

"""
37. Реализовать магазин, который продает 3 вида товара.
Программа должна показывать, сколько осталось в магазине
каждого товара и какова прибыль на текущий момент продавца
по каждому товару и всего (Прибыль = доход - себестоимость товара).
"""

class RetailShop:
    """
    Super mega retail shop
    """

    class OutOfGoods(Exception):
        def __init__(self, message):
            self.message = message

    def __init__(self):
        self.total_income = 0
        self.storage_facilities = None
        self.invoices = []
        self.customers = []

    def __str__(self):
        items_dict = {}
        if self.storage_facilities:
            for item in self.storage_facilities.get_items():
                items_dict[item.category] = items_dict.get(item.category, 0) + 1

        invoice_dict = {}
        for invoice in self.invoices:
            invoice_dict[invoice.product.category] = invoice_dict.get(invoice.product.category, 0) + invoice.income

        output = ""
        output += "Customers: %i\n" % len(self.customers)
        output += "Total income: %i\n" % self.total_income
        output += "Income per category:\n"
        for inv in invoice_dict:
            output += "    %s: %i\n" % (inv, invoice_dict[inv])
        output += "Goods left:\n"
        for item in items_dict:
            output += "    %s: %i\n" % (item, items_dict[item])

        return output

    def set_store(self, store):
        self.storage_facilities = store

    def add_custromer(self, customer):
        self.customers.append(customer)

    def add_invoice(self, invoice):
        self.invoices.append(invoice)

    def sel(self, customer_id, item_id):
        items = self.storage_facilities.get_items()
        if len(items) < 1:
            raise RetailShop.OutOfGoods("Unfortunately, no products left")

        try:
            invoice = Invoice(self.customers[customer_id], items[item_id])
        except IndexError:
            raise RetailShop.OutOfGoods("Unfortunately, product with id %i are no longer in stock" % item_id)

        self.add_invoice(invoice)
        self.total_income += invoice.income
        self.storage_facilities.rm_item(items[item_id])

class Customer:
    """
    Common or garden customer
    """
    def __init__(self, login, email):
        self.login = login
        self.email = email

class Invoice:
    """
    A list of goods sent, with a statement of the sum due for these.
    """

    ADDED_VALUE = 20  # in percent
    __invoice_number = 1

    def __init__(self, customer, product):
        Invoice.__invoice_number += 1
        self.number = Invoice.__invoice_number
        self.customer = customer
        self.product = product
        self.cost_price = product.cost_price
        self.date = datetime.datetime.now()
        self.price = (self.cost_price * Invoice.ADDED_VALUE/100) + self.cost_price
        self.income = self.price - self.cost_price

class Store:
    """
    Accumulate products.
    """
    def __init__(self, location, operating_hours, space):
        self.location = location
        self.operating_hours = operating_hours
        self.space = space
        self._items = []

    def __str__(self):
        return "%i %s" % (len(self._items), str(self._items))

    def add_items(self, items):
        self._items.extend(items)

    def get_items(self):
        return self._items

    def rm_item(self, item):
        self._items.remove(item)

class Product:
    """
    Father of all assortment
    """
    def __init__(self, category, cost_price, brand, title):
        self.cost_price = cost_price
        # clothing, books, hobby
        self.category = category
        self.brand = brand
        self.title = title

class Clothing(Product):
    """
    Category clothing.
    """

    def __init__(self, brand, cost_price, size, gender, title):
        super().__init__("clothing", cost_price, brand, title)
        self.size = size
        self.gender = gender

class Book(Product):
    """
    Category for books
    """

    def __init__(self, brand, cost_price, authors, title):
        super().__init__("books", cost_price, brand, title)
        self.authors = authors

class Hobby(Product):
    """
    Various hobby staff
    """

    def __init__(self, brand, cost_price, title):
        super().__init__("hobby", cost_price, brand, title)

<commit_msg>simplify exeption handling<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import datetime

"""
37. Реализовать магазин, который продает 3 вида товара.
Программа должна показывать, сколько осталось в магазине
каждого товара и какова прибыль на текущий момент продавца
по каждому товару и всего (Прибыль = доход - себестоимость товара).
"""

class RetailShop:
    """
    Super mega retail shop
    """

    class OutOfGoods(Exception):
        pass

    def __init__(self):
        self.total_income = 0
        self.storage_facilities = None
        self.invoices = []
        self.customers = []

    def __str__(self):
        items_dict = {}
        if self.storage_facilities:
            for item in self.storage_facilities.get_items():
                items_dict[item.category] = items_dict.get(item.category, 0) + 1

        invoice_dict = {}
        for invoice in self.invoices:
            invoice_dict[invoice.product.category] = invoice_dict.get(invoice.product.category, 0) + invoice.income

        output = ""
        output += "Customers: %i\n" % len(self.customers)
        output += "Total income: %i\n" % self.total_income
        output += "Income per category:\n"
        for inv in invoice_dict:
            output += "    %s: %i\n" % (inv, invoice_dict[inv])
        output += "Goods left:\n"
        for item in items_dict:
            output += "    %s: %i\n" % (item, items_dict[item])

        return output

    def set_store(self, store):
        self.storage_facilities = store

    def add_custromer(self, customer):
        self.customers.append(customer)

    def add_invoice(self, invoice):
        self.invoices.append(invoice)

    def sel(self, customer_id, item_id):
        items = self.storage_facilities.get_items()
        if len(items) < 1:
            raise RetailShop.OutOfGoods("Unfortunately, no products left")

        try:
            invoice = Invoice(self.customers[customer_id], items[item_id])
        except IndexError:
            raise RetailShop.OutOfGoods("Unfortunately, product with id %i are no longer in stock" % item_id)

        self.add_invoice(invoice)
        self.total_income += invoice.income
        self.storage_facilities.rm_item(items[item_id])

class Customer:
    """
    Common or garden customer
    """
    def __init__(self, login, email):
        self.login = login
        self.email = email

class Invoice:
    """
    A list of goods sent, with a statement of the sum due for these.
    """

    ADDED_VALUE = 20  # in percent
    __invoice_number = 1

    def __init__(self, customer, product):
        Invoice.__invoice_number += 1
        self.number = Invoice.__invoice_number
        self.customer = customer
        self.product = product
        self.cost_price = product.cost_price
        self.date = datetime.datetime.now()
        self.price = (self.cost_price * Invoice.ADDED_VALUE/100) + self.cost_price
        self.income = self.price - self.cost_price

class Store:
    """
    Accumulate products.
    """
    def __init__(self, location, operating_hours, space):
        self.location = location
        self.operating_hours = operating_hours
        self.space = space
        self._items = []

    def __str__(self):
        return "%i %s" % (len(self._items), str(self._items))

    def add_items(self, items):
        self._items.extend(items)

    def get_items(self):
        return self._items

    def rm_item(self, item):
        self._items.remove(item)

class Product:
    """
    Father of all assortment
    """
    def __init__(self, category, cost_price, brand, title):
        self.cost_price = cost_price
        # clothing, books, hobby
        self.category = category
        self.brand = brand
        self.title = title

class Clothing(Product):
    """
    Category clothing.
    """

    def __init__(self, brand, cost_price, size, gender, title):
        super().__init__("clothing", cost_price, brand, title)
        self.size = size
        self.gender = gender

class Book(Product):
    """
    Category for books
    """

    def __init__(self, brand, cost_price, authors, title):
        super().__init__("books", cost_price, brand, title)
        self.authors = authors

class Hobby(Product):
    """
    Various hobby staff
    """

    def __init__(self, brand, cost_price, title):
        super().__init__("hobby", cost_price, brand, title)

<|endoftext|>


<commit_before># Tell the script what modules to use
from netmiko import ConnectHandler
from datetime import datetime
import time

# Variables
ip_addr01 = '10.31.1.101'
ip_addr02 = '10.31.1.102'
ip_addr03 = '10.31.1.103'
ip_addr04 = '10.31.1.104'
ip_addr05 = '10.31.1.105'
ip_addr06 = '10.31.1.106'
ip_addr07 = '10.31.1.107'
ip_addr08 = '10.31.1.108'
ip_addr09 = '10.31.1.109'
ip_addr10 = '10.31.1.110'
ip_addr11 = '10.31.1.111'
ip_addr12 = '10.31.1.112'
ip_addr13 = '10.31.1.113'
ip_addr14 = '10.31.1.114'
ip_addr15 = '10.31.1.115'
ip_addr16 = '10.31.1.116'
ip_addr17 = '10.31.1.117'
ip_addr18 = '10.31.1.118'
ip_addr19 = '10.31.1.119'
ip_addr20 = '10.31.1.120'
ip_addr21 = '10.31.1.121'
ip_addr22 = '10.31.1.122'
ip_addr23 = '10.31.1.123'
ip_addr24 = '10.31.1.124'
ip_addr25 = '10.31.1.125'
ip_addr26 = '10.31.1.126'
ip_addr27 = '10.31.1.127'
ip_addr28 = '10.31.1.128'
ip_addr29 = '10.31.1.129'
ip_addr30 = '10.31.1.130'
ip_addr31 = '10.31.1.131'
ip_addr32 = '10.31.1.132'
ip_addr33 = '10.31.1.133'
ip_addr34 = '10.31.1.134'
ip_addr35 = '10.31.1.135'

# Create a dictionary for the devices
hp1 = {
'device_type': 'hp_procurve',
'ip': ip_addr01,
'username': "",
'password': "",
}

hp2 = {
'device_type': 'hp_procurve',
'ip': ip_addr02,
'username': "",
'password': "",
}

hp3 = {
'device_type': 'hp_procurve',
'ip': ip_addr03,
'username': "",
'password': "",
}

hp4 = {
'device_type': 'hp_procurve',
'ip': ip_addr04,
'username': "",
'password': "",
}

hp5 = {
'device_type': 'hp_procurve',
'ip': ip_addr05,
'username': "",
'password': "",
}

hp6 = {
'device_type': 'hp_procurve',
'ip': ip_addr06,
'username': "",
'password': "",
}

hp7 = {
'device_type': 'hp_procurve',
'ip': ip_addr07,
'username': "",
'password': "",
}

hp8 = {
'device_type': 'hp_procurve',
'ip': ip_addr08,
'username': "",
'password': "",
}

hp9 = {
'device_type': 'hp_procurve',
'ip': ip_addr09,
'username': "",
'password': "",
}

hp10 = {
'device_type': 'hp_procurve',
'ip': ip_addr10,
'username': "",
'password': "",
}

hp11 = {
'device_type': 'hp_procurve',
'ip': ip_addr11,
'username': "",
'password': "",
}

hp12 = {
'device_type': 'hp_procurve',
'ip': ip_addr12,
'username': "",
'password': "",
}

hp13 = {
'device_type': 'hp_procurve',
'ip': ip_addr13,
'username': "",
'password': "",
}

hp14 = {
'device_type': 'hp_procurve',
'ip': ip_addr14,
'username': "",
'password': "",
}

hp15 = {
'device_type': 'hp_procurve',
'ip': ip_addr15,
'username': "",
'password': "",
}

hp16 = {
'device_type': 'hp_procurve',
'ip': ip_addr16,
'username': "",
'password': "",
}

hp17 = {
'device_type': 'hp_procurve',
'ip': ip_addr17,
'username': "",
'password': "",
}

hp18 = {
'device_type': 'hp_procurve',
'ip': ip_addr18,
'username': "",
'password': "",
}

hp19 = {
'device_type': 'hp_procurve',
'ip': ip_addr19,
'username': "",
'password': "",
}

hp20 = {
'device_type': 'hp_procurve',
'ip': ip_addr20,
'username': "",
'password': "",
}

hp21 = {
'device_type': 'hp_procurve',
'ip': ip_addr21,
'username': "",
'password': "",
}

hp22 = {
'device_type': 'hp_procurve',
'ip': ip_addr22,
'username': "",
'password': "",
}

hp23 = {
'device_type': 'hp_procurve',
'ip': ip_addr23,
'username': "",
'password': "",
}

hp24 = {
'device_type': 'hp_procurve',
'ip': ip_addr24,
'username': "",
'password': "",
}

hp25 = {
'device_type': 'hp_procurve',
'ip': ip_addr25,
'username': '',
'password': '',
}

hp26 = {
'device_type': 'hp_procurve',
'ip': ip_addr26,
'username': "",
'password': "",
}

hp27 = {
'device_type': 'hp_procurve',
'ip': ip_addr27,
'username': "",
'password': "",
}

hp28 = {
'device_type': 'hp_procurve',
'ip': ip_addr28,
'username': "",
'password': "",
}

hp29 = {
'device_type': 'hp_procurve',
'ip': ip_addr29,
'username': "",
'password': "",
}

hp30 = {
'device_type': 'hp_procurve',
'ip': ip_addr30,
'username': "",
'password': "",
}

hp31 = {
'device_type': 'hp_procurve',
'ip': ip_addr31,
'username': "",
'password': "",
}

hp32 = {
'device_type': 'hp_procurve',
'ip': ip_addr32,
'username': "",
'password': "",
}

hp33 = {
'device_type': 'hp_procurve',
'ip': ip_addr33,
'username': "",
'password': "",
}

hp34 = {
'device_type': 'hp_procurve',
'ip': ip_addr34,
'username': "",
'password': "",
}

hp35 = {
'device_type': 'hp_procurve',
'ip': ip_addr35,
'username': "",
'password': "",
}

# Group dictionary of devices into a list
all_devices = [hp1, hp2, hp3, hp4, hp5, hp6, hp7, hp8, hp9, hp10, hp11, hp12, hp13, hp14, hp15, hp16, hp17, hp18, hp19, hp20, hp21, hp22, hp23, hp24, hp25, hp26, hp27, hp28, hp29, hp30, hp31, hp32, hp33, hp34, hp35]

# Grabs the current date and time and prints it at the start of the output
start_time = datetime.now()
print ("Start Time = {}".format(start_time))

# Takes the list of "all_devices" and assigns a name (a_device) to each dictionary item.
# This is so the "For Loop" can iterate over each device in the dictionary
# The For Loop will SSH to each device in the list and execute the commands below, in order.
for a_device in all_devices:
	net_connect = ConnectHandler(**a_device)
	print("----------------------------------------BEGIN {0}----------------------------------------".format(a_device['ip']))
	print ("\n>>>>>>>>> Starting Firmware Update on - {0} <<<<<<<<<".format(a_device['ip']))
	output = net_connect.send_command("copy tftp flash IP FIRMWARE pri")
	time.sleep(1)
	output += net_connect.send_command("y")
	print ("\n\nWaiting 1 Minute for the Firmware to Download and Install")
	time.sleep(60)
	shflash = net_connect.send_command("show flash")
	time.sleep(1)
	print ("\n")
	print (shflash)
	print ("\n\n>>>>>>>>> Firmware Update is Complete - {0} <<<<<<<<<".format(a_device['ip']))
	print ("\n\n>>>>>>>>> Creating Self-Signed Certificate <<<<<<<<<")
	time.sleep(1)
	cert1 = net_connect.send_command("config")
	time.sleep(1)
	cert1 += net_connect.send_command('crypto pki enroll-self-signed certificate-name cert1 key-type rsa key-size 2048 subject common-name CERT country US org CERT org-unit IT locality CERT state WA Valid-start 7/21/2016 valid-end 7/21/2036')
	time.sleep(5)
	cert = net_connect.send_command("show crypto pki local-certificate")
	print ("\n")
	print (cert)
	print ("\n\n>>>>>>>>> Finished Creating Self-Signed Certificate <<<<<<<<<")
	print ("\n\n>>>>>>>>> Setting Config from TFTP Server as Startup-Config on - {0} <<<<<<<<<".format(a_device['ip']))
	print ("\n!!!DEVICE WILL REBOOT!!!")
	copy = net_connect.send_command("copy tftp startup-config IP {0}.txt detail".format(a_device['ip']))
	time.sleep(1)
	copy += net_connect.send_command("y")
	time.sleep(1)
	print ("\n\n>>>>>>>>> Device is Rebooting - {0} <<<<<<<<<".format(a_device['ip']))
	print ("----------------------------------------END {0}----------------------------------------".format(a_device['ip']))

# Prints the total time it took to complete the script
end_time = datetime.now()
total_time = end_time - start_time
print ("Total Time = {}".format(total_time))
<commit_msg>Delete AIO.py<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python3

from progressbar import ProgressBar
from pretty import *
import argparse
import lasagne
import theano
import theano.tensor as T
import pickle
import numpy
import time

import os
import os.path

parser = argparse.ArgumentParser()
parser.add_argument('tagged', help='path to directory containing prepared files')
parser.add_argument('-r', '--reserve', type=float, help='percentage of samples to reserve for validation and testing', default=10)
parser.add_argument('-m', '--momentum', type=float, help='momentum', default=0.9)
parser.add_argument('-b', '--batchsize', type=int, help='size of each mini batch', default=256)
parser.add_argument('-s', '--batch-stop', type=int, help='stop after this many batches each epoch', default=0)
parser.add_argument('-e', '--epoch-stop', type=int, help='stop after this many epochs', default=0)
parser.add_argument('-c', '--cache', type=argparse.FileType('ab+'), help='store/resume network state in/from this file', default=None)
parser.add_argument('-p', '--plot', type=argparse.FileType('ab+'), help='plot network performance to this png file', default=None)
parser.add_argument('-v', '--verbose', action='count')
args = parser.parse_args()

section("Setup")
task("Loading data")
y_train = numpy.memmap(os.path.join(args.tagged, "train.labels.db"), dtype=numpy.int32, mode='r')
X_train = numpy.memmap(os.path.join(args.tagged, "train.images.db"), dtype=numpy.float32, mode='r', shape=(len(y_train), 3, 128, 128))
cats = numpy.max(y_train)+1

# use X% of data for validation, and X% for testing
reserved = int(args.reserve * len(y_train) / 100.0)
X_train, X_val, X_test = X_train[:-2*reserved], X_train[-2*reserved:-reserved], X_train[-reserved:]
y_train, y_val, y_test = y_train[:-2*reserved], y_train[-2*reserved:-reserved], y_train[-reserved:]

task("Building model and compiling functions")
# create Theano variables for input and target minibatch
learning_rates = numpy.logspace(-2, -4, 60, dtype=theano.config.floatX)
learning_rates_var = theano.shared(learning_rates)
learning_rate = theano.shared(learning_rates[0] * (1-args.momentum)) # see https://lasagne.readthedocs.org/en/latest/modules/updates.html#lasagne.updates.nesterov_momentum
epochi = T.iscalar('e')
input_var = T.tensor4('X')
target_var = T.ivector('y')

# create a small convolutional neural network
from lasagne.nonlinearities import leaky_rectify, softmax
network = lasagne.layers.InputLayer((None, 3, 128, 128), input_var)
# 1st
network = lasagne.layers.Conv2DLayer(network, 64, (8, 8), stride=2,
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 2nd
network = lasagne.layers.Conv2DLayer(network, 96, (5, 5), stride=1, pad='same',
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 3rd
network = lasagne.layers.Conv2DLayer(network, 128, (3, 3), stride=1, pad='same',
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 4th
network = lasagne.layers.DenseLayer(network, 512, nonlinearity=leaky_rectify)
network = lasagne.layers.DropoutLayer(network)
# 5th
network = lasagne.layers.DenseLayer(network, cats, nonlinearity=softmax)

# create loss function
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean() + 1e-4 * lasagne.regularization.regularize_network_params(
        network, lasagne.regularization.l2)

# create parameter update expressions
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=learning_rate, momentum=args.momentum)
updates[learning_rate] = learning_rates_var[epochi] * (1-args.momentum)

# compile training function that updates parameters and returns training loss
train_fn = theano.function([epochi, input_var, target_var], loss, updates=updates)

# Create a loss expression for validation/testing. The crucial difference here
# is that we do a deterministic forward pass through the network, disabling
# dropout layers.
test_prediction = lasagne.layers.get_output(network, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,
                                                        target_var)
test_loss = test_loss.mean()
# As a bonus, also create an expression for the classification accuracy:
test_1_acc = T.mean(lasagne.objectives.categorical_accuracy(test_prediction, target_var))
test_5_acc = T.mean(T.any(T.eq(T.argsort(test_prediction, axis=1)[:, -5:], target_var.dimshuffle(0, 'x')), axis=1), dtype=theano.config.floatX)

# compile a second function computing the validation loss and accuracy:
val_fn = theano.function([input_var, target_var], [test_loss, test_1_acc, test_5_acc])

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = numpy.arange(len(inputs))
        numpy.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]

training = []
validation = []
end = len(learning_rates)
if args.epoch_stop != 0:
    end = args.epoch_stop

if args.plot is not None:
    args.plot.close()
    import matplotlib
    matplotlib.use('Agg') # avoid the need for X

def replot():
    if args.plot is None:
        return

    global training
    global validation
    if len(validation) == 0:
        return

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(style="ticks", color_codes=True)

    fig = plt.figure()
    ax_loss = fig.add_subplot(1, 2, 1)
    ax_err = fig.add_subplot(1, 2, 2)

    # styles
    ax_loss.grid(True)
    ax_err.grid(True)
    #ax_loss.set_yscale('log')
    #ax_err.set_yscale('log')

    # limits
    global end
    ax_loss.set_xlim(0, end)
    ax_err.set_xlim(0, end)
    ax_err.set_ylim(0, 1)
    #ax_err.set_ylim(1e-5, 1)

    # plot loss
    ax_loss.plot(range(1, end+1), [dp[0] for dp in training], 'b')
    ax_loss.plot(range(1, end+1), [dp[0] for dp in validation], 'r--')
    ax_loss.legend(['Training loss', 'Validation loss'])
    ax_loss.set_title('Model loss')

    # plot error
    ax_err.plot(range(1, end+1), [1-dp[1] for dp in training], 'b')
    ax_err.plot(range(1, end+1), [1-dp[2] for dp in training], 'r')
    ax_err.plot(range(1, end+1), [1-dp[1] for dp in validation], 'y--')
    ax_err.plot(range(1, end+1), [1-dp[2] for dp in validation], 'm--')
    ax_err.legend(['Training exact', 'Training top 5', 'Validation exact', 'Validation top 5'])
    ax_err.set_title('Match error')

    import tempfile
    with tempfile.NamedTemporaryFile(delete=False, dir=os.path.dirname(args.plot.name)) as fp:
        plt.savefig(fp, format='png')
        fp.close()
        os.rename(fp.name, args.plot.name)

start = 0
if args.cache is not None:
    try:
        section("Restoring state")
        args.cache.seek(0)
        task("Loading parameters")
        state = pickle.load(args.cache)
        task("Loading epoch information")
        epoch = pickle.load(args.cache)
        training = pickle.load(args.cache)
        validation = pickle.load(args.cache)
        task("Restoring parameter values")
        for p, v in zip(params, state):
            p.set_value(v)
        learning_rate.set_value(learning_rates[epoch] * (1-args.momentum))
        subtask("Resuming at epoch {}".format(epoch+1))
        start = epoch+1
    except EOFError:
        task("No model state stored; starting afresh")

section("Training")
# Finally, launch the training loop.
for epoch in range(start, end):
    replot()

    task("Starting training epoch {}".format(epoch))
    start_time = time.time()

    # In each epoch, we do a pass over minibatches of the training data:
    train_loss = 0
    train_batches = len(range(0, len(X_train) - args.batchsize + 1, args.batchsize))
    p = ProgressBar(max_value = train_batches).start()
    i = 1
    for batch in iterate_minibatches(X_train, y_train, args.batchsize, shuffle=True):
        train_loss += train_fn(i-1, batch[0], batch[1])
        p.update(i)
        i = i+1
        if args.batch_stop != 0 and i > args.batch_stop:
            p.update(train_batches)
            break

    subtask("Doing forward pass on training data (size: {})".format(len(X_train)))
    p = ProgressBar(max_value = train_batches).start()
    i = 1
    train_acc1 = 0
    train_acc5 = 0
    for batch in iterate_minibatches(X_train, y_train, args.batchsize, shuffle=False):
        _, acc1, acc5 = val_fn(batch[0], batch[1])
        p.update(i)
        train_acc1 += acc1
        train_acc5 += acc5
        i = i+1

    subtask("Doing forward pass on validation data (size: {})".format(len(X_val)))
    # Also do a validation data forward pass
    val_loss = 0
    val_acc1 = 0
    val_acc5 = 0
    val_batches = len(range(0, len(X_val) - args.batchsize + 1, args.batchsize))
    p = ProgressBar(max_value = val_batches).start()
    i = 1
    for batch in iterate_minibatches(X_val, y_val, args.batchsize, shuffle=False):
        loss, acc1, acc5 = val_fn(batch[0], batch[1])
        val_loss += loss
        val_acc1 += acc1
        val_acc5 += acc5
        p.update(i)
        i = i+1

    # record performance
    training.append((train_loss/train_batches, train_acc1/train_batches, train_acc5/train_batches))
    validation.append((val_loss/val_batches, val_acc1/val_batches, val_acc5/val_batches))

    # store model state
    if args.cache is not None:
        subtask("Storing trained parameters")
        args.cache.seek(0)
        args.cache.truncate()
        pickle.dump([p.get_value() for p in params], args.cache)
        pickle.dump(epoch, args.cache)
        pickle.dump(training, args.cache)
        pickle.dump(validation, args.cache)

    # Then we print the results for this epoch:
    subtask("Epoch results: {:.2f}%/{:.2f}% (t5acc, v5acc)".format(
        training[-1][2] * 100,
        validation[-1][2] * 100,
    ))

replot()

section("Evaluation")
# After training, we compute and print the test error:
task("Evaluating performance on test data set")
test_loss = 0
test_acc1 = 0
test_acc5 = 0
test_batches = len(range(0, len(X_test) - args.batchsize + 1, args.batchsize))
p = ProgressBar(max_value = test_batches).start()
i = 1
for batch in iterate_minibatches(X_test, y_test, args.batchsize, shuffle=False):
    loss, acc1, acc5 = val_fn(batch[0], batch[1])
    test_loss += loss
    test_acc1 += acc1
    test_acc5 += acc5
    p.update(i)
    i = i+1

print(colored(" ==> Final results: {:.6f} loss, {:.2f}% top-1 accuracy, {:.2f}% top-5 accuracy <== ".format(
    test_loss / test_batches,
    test_acc1 / test_batches * 100,
    test_acc5 / test_batches * 100
), "green", attrs=["bold"]))
<commit_msg>Plot with correcter x-axis<commit_after>#!/usr/bin/env python3

from progressbar import ProgressBar
from pretty import *
import argparse
import lasagne
import theano
import theano.tensor as T
import pickle
import numpy
import time

import os
import os.path

parser = argparse.ArgumentParser()
parser.add_argument('tagged', help='path to directory containing prepared files')
parser.add_argument('-r', '--reserve', type=float, help='percentage of samples to reserve for validation and testing', default=10)
parser.add_argument('-m', '--momentum', type=float, help='momentum', default=0.9)
parser.add_argument('-b', '--batchsize', type=int, help='size of each mini batch', default=256)
parser.add_argument('-s', '--batch-stop', type=int, help='stop after this many batches each epoch', default=0)
parser.add_argument('-e', '--epoch-stop', type=int, help='stop after this many epochs', default=0)
parser.add_argument('-c', '--cache', type=argparse.FileType('ab+'), help='store/resume network state in/from this file', default=None)
parser.add_argument('-p', '--plot', type=argparse.FileType('ab+'), help='plot network performance to this png file', default=None)
parser.add_argument('-v', '--verbose', action='count')
args = parser.parse_args()

section("Setup")
task("Loading data")
y_train = numpy.memmap(os.path.join(args.tagged, "train.labels.db"), dtype=numpy.int32, mode='r')
X_train = numpy.memmap(os.path.join(args.tagged, "train.images.db"), dtype=numpy.float32, mode='r', shape=(len(y_train), 3, 128, 128))
cats = numpy.max(y_train)+1

# use X% of data for validation, and X% for testing
reserved = int(args.reserve * len(y_train) / 100.0)
X_train, X_val, X_test = X_train[:-2*reserved], X_train[-2*reserved:-reserved], X_train[-reserved:]
y_train, y_val, y_test = y_train[:-2*reserved], y_train[-2*reserved:-reserved], y_train[-reserved:]

task("Building model and compiling functions")
# create Theano variables for input and target minibatch
learning_rates = numpy.logspace(-2, -4, 60, dtype=theano.config.floatX)
learning_rates_var = theano.shared(learning_rates)
learning_rate = theano.shared(learning_rates[0] * (1-args.momentum)) # see https://lasagne.readthedocs.org/en/latest/modules/updates.html#lasagne.updates.nesterov_momentum
epochi = T.iscalar('e')
input_var = T.tensor4('X')
target_var = T.ivector('y')

# create a small convolutional neural network
from lasagne.nonlinearities import leaky_rectify, softmax
network = lasagne.layers.InputLayer((None, 3, 128, 128), input_var)
# 1st
network = lasagne.layers.Conv2DLayer(network, 64, (8, 8), stride=2,
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 2nd
network = lasagne.layers.Conv2DLayer(network, 96, (5, 5), stride=1, pad='same',
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 3rd
network = lasagne.layers.Conv2DLayer(network, 128, (3, 3), stride=1, pad='same',
                                     nonlinearity=leaky_rectify)
network = lasagne.layers.MaxPool2DLayer(network, (3, 3), stride=2)
# 4th
network = lasagne.layers.DenseLayer(network, 512, nonlinearity=leaky_rectify)
network = lasagne.layers.DropoutLayer(network)
# 5th
network = lasagne.layers.DenseLayer(network, cats, nonlinearity=softmax)

# create loss function
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean() + 1e-4 * lasagne.regularization.regularize_network_params(
        network, lasagne.regularization.l2)

# create parameter update expressions
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=learning_rate, momentum=args.momentum)
updates[learning_rate] = learning_rates_var[epochi] * (1-args.momentum)

# compile training function that updates parameters and returns training loss
train_fn = theano.function([epochi, input_var, target_var], loss, updates=updates)

# Create a loss expression for validation/testing. The crucial difference here
# is that we do a deterministic forward pass through the network, disabling
# dropout layers.
test_prediction = lasagne.layers.get_output(network, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,
                                                        target_var)
test_loss = test_loss.mean()
# As a bonus, also create an expression for the classification accuracy:
test_1_acc = T.mean(lasagne.objectives.categorical_accuracy(test_prediction, target_var))
test_5_acc = T.mean(T.any(T.eq(T.argsort(test_prediction, axis=1)[:, -5:], target_var.dimshuffle(0, 'x')), axis=1), dtype=theano.config.floatX)

# compile a second function computing the validation loss and accuracy:
val_fn = theano.function([input_var, target_var], [test_loss, test_1_acc, test_5_acc])

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = numpy.arange(len(inputs))
        numpy.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]

training = []
validation = []
end = len(learning_rates)
if args.epoch_stop != 0:
    end = args.epoch_stop

if args.plot is not None:
    args.plot.close()
    import matplotlib
    matplotlib.use('Agg') # avoid the need for X

def replot():
    if args.plot is None:
        return

    global training
    global validation
    if len(validation) == 0:
        return

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(style="ticks", color_codes=True)

    fig = plt.figure()
    ax_loss = fig.add_subplot(1, 2, 1)
    ax_err = fig.add_subplot(1, 2, 2)

    # styles
    ax_loss.grid(True)
    ax_err.grid(True)
    #ax_loss.set_yscale('log')
    #ax_err.set_yscale('log')

    # limits
    global end
    ax_loss.set_xlim(0, end)
    ax_err.set_xlim(0, end)
    ax_err.set_ylim(0, 1)
    #ax_err.set_ylim(1e-5, 1)

    # plot loss
    xend = len(training)+1
    ax_loss.plot(range(1, xend), [dp[0] for dp in training], 'b')
    ax_loss.plot(range(1, xend), [dp[0] for dp in validation], 'r--')
    ax_loss.legend(['Training loss', 'Validation loss'])
    ax_loss.set_title('Model loss')

    # plot error
    ax_err.plot(range(1, xend), [1-dp[1] for dp in training], 'b')
    ax_err.plot(range(1, xend), [1-dp[2] for dp in training], 'r')
    ax_err.plot(range(1, xend), [1-dp[1] for dp in validation], 'y--')
    ax_err.plot(range(1, xend), [1-dp[2] for dp in validation], 'm--')
    ax_err.legend(['Training exact', 'Training top 5', 'Validation exact', 'Validation top 5'])
    ax_err.set_title('Match error')

    import tempfile
    with tempfile.NamedTemporaryFile(delete=False, dir=os.path.dirname(args.plot.name)) as fp:
        plt.savefig(fp, format='png')
        fp.close()
        os.rename(fp.name, args.plot.name)

start = 0
if args.cache is not None:
    try:
        section("Restoring state")
        args.cache.seek(0)
        task("Loading parameters")
        state = pickle.load(args.cache)
        task("Loading epoch information")
        epoch = pickle.load(args.cache)
        training = pickle.load(args.cache)
        validation = pickle.load(args.cache)
        task("Restoring parameter values")
        for p, v in zip(params, state):
            p.set_value(v)
        learning_rate.set_value(learning_rates[epoch] * (1-args.momentum))
        subtask("Resuming at epoch {}".format(epoch+1))
        start = epoch+1
    except EOFError:
        task("No model state stored; starting afresh")

section("Training")
# Finally, launch the training loop.
for epoch in range(start, end):
    replot()

    task("Starting training epoch {}".format(epoch))
    start_time = time.time()

    # In each epoch, we do a pass over minibatches of the training data:
    train_loss = 0
    train_batches = len(range(0, len(X_train) - args.batchsize + 1, args.batchsize))
    p = ProgressBar(max_value = train_batches).start()
    i = 1
    for batch in iterate_minibatches(X_train, y_train, args.batchsize, shuffle=True):
        train_loss += train_fn(i-1, batch[0], batch[1])
        p.update(i)
        i = i+1
        if args.batch_stop != 0 and i > args.batch_stop:
            p.update(train_batches)
            break

    subtask("Doing forward pass on training data (size: {})".format(len(X_train)))
    p = ProgressBar(max_value = train_batches).start()
    i = 1
    train_acc1 = 0
    train_acc5 = 0
    for batch in iterate_minibatches(X_train, y_train, args.batchsize, shuffle=False):
        _, acc1, acc5 = val_fn(batch[0], batch[1])
        p.update(i)
        train_acc1 += acc1
        train_acc5 += acc5
        i = i+1

    subtask("Doing forward pass on validation data (size: {})".format(len(X_val)))
    # Also do a validation data forward pass
    val_loss = 0
    val_acc1 = 0
    val_acc5 = 0
    val_batches = len(range(0, len(X_val) - args.batchsize + 1, args.batchsize))
    p = ProgressBar(max_value = val_batches).start()
    i = 1
    for batch in iterate_minibatches(X_val, y_val, args.batchsize, shuffle=False):
        loss, acc1, acc5 = val_fn(batch[0], batch[1])
        val_loss += loss
        val_acc1 += acc1
        val_acc5 += acc5
        p.update(i)
        i = i+1

    # record performance
    training.append((train_loss/train_batches, train_acc1/train_batches, train_acc5/train_batches))
    validation.append((val_loss/val_batches, val_acc1/val_batches, val_acc5/val_batches))

    # store model state
    if args.cache is not None:
        subtask("Storing trained parameters")
        args.cache.seek(0)
        args.cache.truncate()
        pickle.dump([p.get_value() for p in params], args.cache)
        pickle.dump(epoch, args.cache)
        pickle.dump(training, args.cache)
        pickle.dump(validation, args.cache)

    # Then we print the results for this epoch:
    subtask("Epoch results: {:.2f}%/{:.2f}% (t5acc, v5acc)".format(
        training[-1][2] * 100,
        validation[-1][2] * 100,
    ))

replot()

section("Evaluation")
# After training, we compute and print the test error:
task("Evaluating performance on test data set")
test_loss = 0
test_acc1 = 0
test_acc5 = 0
test_batches = len(range(0, len(X_test) - args.batchsize + 1, args.batchsize))
p = ProgressBar(max_value = test_batches).start()
i = 1
for batch in iterate_minibatches(X_test, y_test, args.batchsize, shuffle=False):
    loss, acc1, acc5 = val_fn(batch[0], batch[1])
    test_loss += loss
    test_acc1 += acc1
    test_acc5 += acc5
    p.update(i)
    i = i+1

print(colored(" ==> Final results: {:.6f} loss, {:.2f}% top-1 accuracy, {:.2f}% top-5 accuracy <== ".format(
    test_loss / test_batches,
    test_acc1 / test_batches * 100,
    test_acc5 / test_batches * 100
), "green", attrs=["bold"]))
<|endoftext|>


<commit_before>from common.bounty import *
from common.peers import *
from common import settings

def main():
    settings.setup()
    print "settings are:"
    print settings.config
    if settings.config.get('server') is not True:
        initializePeerConnections()
    else:
        listen()

if __name__ == "__main__":
    main()
<commit_msg>Concurrent listening<commit_after>from common.bounty import *
from common.peers import *
from common import settings
from time import sleep

def main():
    settings.setup()
    print "settings are:"
    print settings.config
    ear = listener(1,44565)
    ear.daemon = True
    ear.start()
    initializePeerConnections()

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>import sys, getopt

def usage():
    print("Usage: python main.py [-h|help] [-u|username=<args>] [-p|password=<args>]")

def main(argv):
    username = ""
    password = ""

    try:
        opts, args = getopt.getopt(argv, "hu:p:", ["help", "username=", "password="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-u", "--username"):
            username = arg
        elif opt in ("-p", "--password"):
            password = arg
        else:
            assert False, "unhandled option"

    print("Your ntu username is: " + username)
    print("Your ntu password is: " + password)

if __name__ == "__main__":
    main(sys.argv[1:])
<commit_msg>add execution of ntu_login() in main commented<commit_after>import sys, getopt
from ntudownloader import ntu_login

def usage():
    print("Usage: python main.py [-h|help] [-u|username=<args>] [-p|password=<args>]")

def main(argv):
    username = ""
    password = ""

    try:
        opts, args = getopt.getopt(argv, "hu:p:", ["help", "username=", "password="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-u", "--username"):
            username = arg
        elif opt in ("-p", "--password"):
            password = arg
        else:
            assert False, "unhandled option"

    # ntu_login(username, password)
    print("Your ntu username is: " + username)
    print("Your ntu password is: " + password)

if __name__ == "__main__":
    main(sys.argv[1:])
<|endoftext|>