"""
Automatically segments an audio file based on silence.

Marks an utterance for speech and silence.  Then, via
markTranscriptForAnnotations, chunks those segments into small,
manageable chunks.  The code assumes that the speaker is
talking most of the time.

Probably only useful in limited circumstances.
"""

from os.path import join
import math

from praatio import tgio
from praatio import praat_scripts
from praatio.utilities import utils

def markTranscriptForAnnotations(tgFN, tierName, outputTGFN, proportion=1 / 5.0):
    """
    Prep a noisy silence annotation for an annotation task

    Voice activity detectors are liable to segment speech into very small
    chunks (fragments of speech and silence).  The point of this code is
    to segment a recording into larger units that could be used in a
    speech transcription task.

    Assumes the speaker is speaking for most of the recording.
    """
    tg = tgio.openTextgrid(tgFN)

    duration = tg.maxTimestamp
    numEntries = int(math.ceil(duration * proportion))
    entryList = tg.tierDict[tierName].entryList

    # Get all silent intervals
    entryList = [
        (stop - start, start, stop, label)
        for start, stop, label in entryList
        if label == "silent"
    ]

    # Remove silent intervals at the start or end of the file
    entryList = [entry for entry in entryList if entry[1] != 0 and entry[2] != duration]

    # Put longest intervals first
    entryList.sort(reverse=True)

    # Get the mid point of the longest n intervals and convert them
    # into intervals to be transcribed
    entryList = entryList[:numEntries]
    pointList = [start + ((stop - start) / 2.0) for _, start, stop, _ in entryList]
    pointList.sort()

    pointList = (
        [
            0.0,
        ]
        + pointList
        + [
            duration,
        ]
    )

    newEntryList = []
    for i in range(len(pointList) - 1):
        newEntryList.append((pointList[i], pointList[i + 1], "%d" % i))

    outputTG = tgio.Textgrid()
    tier = tgio.IntervalTier("toTranscribe", newEntryList, 0, duration)
    outputTG.addTier(tier)

    outputTG.save(outputTGFN)

def autoSegmentSpeech(praatEXE, inputWavPath, rawTGPath, finalTGPath):

    utils.makeDir(finalTGPath)

    praat_scripts.annotateSilences(praatEXE, inputWavPath, rawTGPath)

    for tgFN in utils.findFiles(rawTGPath, filterExt=".TextGrid"):
        markTranscriptForAnnotations(
            join(rawTGPath, tgFN), "silences", join(finalTGPath, tgFN)
        )

_praatEXE = r"C:\praat.exe"
_root = join(".", "files")
_inputWavPath = _root
_rawTGPath = join(_root, "silence_marked_textgrids")
_finalTGPath = join(_root, "ready-to-transcribe_textgrids")
autoSegmentSpeech(_praatEXE, _inputWavPath, _rawTGPath, _finalTGPath)


import sys

def getChildren(parent):
  grid = tuple(parent)
  ret = []
  r = len(grid)
  c = len(grid[0])
  if r == 0 or c == 0:
    return []
  for i in range(r):
    if all([grid[i][j] != "#" for j in range(c)]):
      A = grid[:i]
      B = grid[i + 1:]
      child = [A, B]
      ret.append(child)
  for j in range(c):
    if all([grid[i][j] != "#" for i in range(r)]):
      A = [grid[i][:j] for i in range(r)]
      B = [grid[i][j + 1:] for i in range(r)]
      child = [tuple(A), tuple(B)]
      ret.append(child)
  return ret

def NimSum(a, b):
  i = 0
  ans = 0
  while a + b > 0:
    ans += (2 ** i) * ((a + b) % 2)
    a //= 2
    b //= 2
    i += 1
  return ans

nim = {}
nim[()] = (0, 0)

def GetNim(grid):
  if grid not in nim:
    children = getChildren(grid)
    if len(children) == 0:
      nim[grid] = (0, 0)
    else:
      A = [0] * 100
      for child in children:
        a, x = GetNim(child[0])
        b, x = GetNim(child[1])
        c = NimSum(a ,b)
        if len(child[0]) == len(grid):
          A[c] += len(grid)
        else:
          A[c] += len(grid[0])
      nim[grid] = (A.index(0), A[0])
  return nim[grid]

for t in range(int(input())):
    r, c = map(int, input().split())
    grid = tuple([input() for _ in range(r)])
    _, ans = GetNim(grid)
    print("Case #{case}: {answer}".format(case=t + 1, answer=ans))


'''Creates all configuration that is used for DEAP in CPPN experiments'''

from deap import base, tools, algorithms, creator
from scoop import futures
import pickle

from FULL_CPPN_struct import Genotype
from FULL_CPPN_deaphelp import weightMutate, conMutate, nodeMutate, xover, actMutate
from FULL_CPPN_evaluation import evaluate_pic_scoop, evaluate_nov_pic

"""The below contains all of the deap configuration used for CPPN so that it can be
called and edited from a central location"""

# constants used for deap configuration
weights=(-1.0, -1.0)
NUM_IN=2
NUM_OUT=1
POP_SIZE=100

#create types needed for deap
creator.create("FitnessMulti", base.Fitness, weights=weights)
creator.create("Individual", Genotype, fitness=creator.FitnessMulti)

# initialize the toolbox
toolbox = base.Toolbox()

# register function to create individual in the toolbox
toolbox.register("individual", creator.Individual, NUM_IN, NUM_OUT)

# register function to create population in the toolbox
toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=POP_SIZE)

# register all functions needed for evolution in the toolbox
toolbox.register("evaluate", evaluate_pic_scoop)
toolbox.register("assign_fit", evaluate_nov_pic)
toolbox.register("select", tools.selNSGA2, k=POP_SIZE)
toolbox.register("mate", xover)
toolbox.register("weightMutate", weightMutate)
toolbox.register("connectionMutate", conMutate)
toolbox.register("nodeMutate", nodeMutate)
toolbox.register("activationMutate", actMutate)
toolbox.register("map", futures.map)

def get_tb():
	"""method to get the toolbox object that is needed for
	evolution - the toolbox is configured in this file so that
	the configuration is central"""

	return toolbox


from django.urls import path, re_path
from . import views

urlpatterns = [

    path('<course_id>/', views.QuizListView, name='quiz_index'),

    path('<course_id>/gradebook/', views.viewQuizScoresByCourse, name='course_progress'),

    re_path(r'^category/$',
        views.CategoriesListView.as_view(),
        name='quiz_category_list_all'),

    re_path(r'^category/(?P<category_name>[\w|\W-]+)/$',
        views.ViewQuizListByCategory.as_view(),
        name='quiz_category_list_matching'),

    re_path(r'^progress/$',
        views.QuizUserProgressView.as_view(),
        name='quiz_progress'),

    re_path(r'^marking/$',
        views.QuizMarkingList.as_view(),
        name='quiz_marking'),

    re_path(r'^marking/(?P<pk>[\d.]+)/$',
        views.QuizMarkingDetail.as_view(),
        name='quiz_marking_detail'),

    #  passes variable 'quiz_name' to quiz_take view
    re_path(r'^detail/(?P<slug>[\w-]+)/$',
        views.QuizDetailView.as_view(),
        name='quiz_start_page'),

    re_path(r'^(?P<quiz_name>[\w-]+)/take/$',
        views.QuizTake.as_view(),
        name='quiz_question'),
]


from pyquery import PyQuery as pq
import requests

@when(u'I view {url}')
def step_impl(context, url):
    context.response = requests.get(context.base_url + url)

@then(u'I should see a list of projects')
def step_impl(context):
    assert context.response.status_code == 200
    page = pq(context.response.text)
    projects = page("li.module-item")
    assert len(projects) == 14


from newsie.models import Article
from utils.dummy_fun import dummy_fun
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from joblib import dump, load
from sklearn.pipeline import Pipeline, TransformerMixin, FeatureUnion
from sklearn.multiclass import OneVsRestClassifier

def classify(article):
    #load the trained classifier
    pipe = load('model.joblib')

    #convert the article to a dictionary then DataFrame
    df = pd.DataFrame.from_records([article.to_dict()])

    #0th index will be the category with the highest probability
    category = pipe.predict(df)[0]

    #Find the probability of the most likely category
    probability = pipe.predict_proba(df)
    probability = max(probability[0])

    #If probability is greater than the threshold, return a category
    classification = category if probability >= .93 else ''

    if probability >= .93:
        print(f"Classified Article: '{article.title}' \n Class: {classification} -- {probability} \n")

    return classification

class DataFrameColumnExtracter(TransformerMixin):

    def __init__(self, column):
        self.column = column

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        return X[self.column]

def train(articles):
    #Support Vector Machine:
    clf = svm.SVC(
        kernel='linear', #our data is linearly separable
        probability=True #we need probability to be calculated
        )
    # cvec = CountVectorizer(
    #     tokenizer=dummy_fun, #function that does nothing
    #     preprocessor=dummy_fun #since we already preprocessed
    #     )
    tfidf = TfidfVectorizer(tokenizer=dummy_fun, preprocessor=dummy_fun)

    #Converts Article objects to dictionaries for use in DataFrames
    news_df = pd.DataFrame.from_records([art.to_dict() for art in articles])

    #Creates two columns (features):
    #1 'tokens', the tokens from the article's body
    #2 'url_tokens', the tokens from the article's url
    X = news_df.drop('category', 1)

    #Creates one column with the categories (labels)
    Y = news_df['category']

    X_train, X_test, y_train, y_test = train_test_split(
        X,
        Y,
        random_state = 42, #pseudo-random number generator for consistent results
        test_size=0.33 #splits 33% of data into test group
    )

    # # Logistic Regression:
    #     # Accuracy on train_test_split: .95
    #     # Accuracy on manually labeled data: .61

    # # SVM:
    #     # Accuracy on train_test_split data: .96
    #     # Accuracy on manually labeled data: .60
    # pipe = Pipeline([
    #     ('features', FeatureUnion([
    #             ('url_tokens', Pipeline([
    #                 #DataFrameColumnExtracter is a custom class
    #                 #used for selecting the column with the correct feature
    #                 ('selector', DataFrameColumnExtracter('url_tokens')),
    #                 ('vec', cvec) # Count vectorizer
    #             ])),
    #             ('article_tokens', Pipeline([
    #                 ('selector', DataFrameColumnExtracter('tokens')),
    #                 ('vec', tfidf) # Tf-idf vectorizer
    #             ]))
    #         ])),
    #     ('clf', OneVsRestClassifier(clf))
    # ])

    # Logistic Regression:
        # #Accuracy on train_test_split data: .95
        # #Accuracy on manually labeled data: .60

    # SVM:
        # #Accuracy on train_test_split data: .89
        # #Accuracy on manually labeled data: .73
    pipe = Pipeline([
        ('article_tokens', Pipeline([
                    ('selector', DataFrameColumnExtracter('tokens')),
                    ('vec', tfidf), # Tf-idf vectorizer
                    ('clf', clf)    # SVM Classifier
                ]))
    ])

    pipe.fit(X_train, y_train) #Trains the classifier
    dump(pipe, 'model.joblib') #Saves the classifier as a .joblib file

    print(pipe.score(X_test, y_test))
    return news_df


'''
Napišite razred Kalkulator, ki bo simuliral delovanje kalkulatorja. Kalkulator mora tudi beležiti koliko napak je prišlo pri samem izvajanju. Torej morebitne napake mora kalkulator predvideti in uporabniku povedati pri kateri operaciji je prišlo do napake ter opis same napake (prve ki se pojavi), vendar se kalkulator/program ne sme ustaviti. Kalkulator se lahko zaključi z vnešenim operatorjem "q". Podpira naj vsaj seštevanje "+", odštevanje "-", množenje "*", deljenje "/" in potenciranje "**". Ob koncu izvajanja, mora kalkulator izpisati koliko napak je naredil uporabnik (1 napaka se šteje za napako pri 1 izračunu --> torej če sta x in y narobe vnešena se to šteje samo za 1 napako).

Kaj vse mora nuditi kalkulator:
- pomoč, kateri operatorji so na voljo
- vnos operatorja ("input()")
- vnos števila x ("input()")
- vnos števila y ("input()")
- ob napaki izpiše katero operacija je bila mišljeno izvesti in opis napake ki jo vrne program
- izpis operacije (vsakič), tudi če pride do napakev obliki: "[x] [operator] [y] = rezultat" naprimer: "[3] [**] [3] = 27"
- ob napaki, naj za rezultat izpiše "None", naprimer: "[3] [***] [0] = None"
- vsako izvajanje naj bo ločeno s črto (poglej si primer izvajanja)

Input:
    calc = Kalkulator()
    calc.start()

Output:
    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator: *
    x = 6
    y = 6
    [6] [*] [6] = 36.0
    ---------------------------------------------------------------------------

    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator: 8*
    x = *
    y = *
    Prišlo je do napake pri <neznana operacija>: Operator je vnešen napačno
    [*] [8*] [*] = None
    ---------------------------------------------------------------------------

    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator:
    x =
    y =
    Prišlo je do napake pri <neznana operacija>: Operator je vnešen napačno
    [] [] [] = None
    ---------------------------------------------------------------------------

    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator: /
    x = asffds
    y =
    Prišlo je do napake pri <deljenje>: could not convert string to float: 'asffds'
    [asffds] [/] [] = None
    ---------------------------------------------------------------------------

    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator: -
    x = 3.589
    y = 7.98
    [3.589] [-] [7.98] = -4.391
    ---------------------------------------------------------------------------

    Operatorji:
            [+] --> seštevanje
            [-] --> odštevanje
            [*] --> množenje
            [/] --> deljenje
            [**] --> potenciranje
            [q] --> zaključi kalkulator
    Operator: q
    število napak v času delovanja: 3

'''
# Rešitev
class Kalkulator:
    def __init__(self):
        self.numOfMistakes = 0

    def start(self):
        while True:
            print("""\nOperatorji:
        [+] --> seštevanje
        [-] --> odštevanje
        [*] --> množenje
        [/] --> deljenje
        [^] --> potenciranje
        [q] --> zaključi kalkulator""")
            operator = input("Operator: ").strip()

            if operator == "q":
                self.izpisi_stevilo_napak()
                break

            x = input("x = ").strip()
            y = input("y = ").strip()

            rezultat = self.izracunaj(operator,x,y)

            print(f"[{x}] [{operator}] [{y}] = {rezultat}")
            print("---------------------------------------------------------------------------")

    def izracunaj(self,operator, num1, num2):
        operacije_dict = {"+":"seštevanje",
                         "-":"odštevanje",
                         "*":"množenje",
                         "/":"deljenje",
                         "^":"potenciranje"}
        try:
            if operator in operacije_dict:
                operacija = operacije_dict[operator]
            else:
                operacija = "neznana operacija"
                raise Exception("Operator je vnešen napačno")
            num1_f = float(num1)
            num2_f = float(num2)
            if operator == "+":
                return num1_f+num2_f
            elif operator == "-":
                return num1_f-num2_f
            elif operator == "*":
                return num1_f*num2_f
            elif operator == "/":
                return num1_f/num2_f
            elif operator == "^":
                return num1_f**num2_f
            else:
                raise Exception(f"Operacija ni bila možno zaznati iz opratorja <{operator}>")
        except Exception as e:
            self.numOfMistakes += 1
            print(f"Prišlo je do napake pri <{operacija}>: {e}")
            return None

    def izpisi_stevilo_napak(self):
        print(f"število napak v času delovanja: {self.numOfMistakes}")

calc = Kalkulator()
calc.start()

'''
Napišite funkcijo "izvedi_ukaze(ime_datoteke)", ki prejme kot atribut ime datoteke, v kateri so v vsaki vrstici napisani ukazi, ki jih mora ta funkcija interpretirati. Ukazi se nanašajo na izrisovanje obrob likov. Primer datoteke je datoteka "test.txt".

Oblika ukaza: "[Lik], [Kam_izpisati], [Velikost]"

Lik:
"trikotnik" --> izriše se naj obroba trikotnika
"kvadrat" --> izriše se naj obroba kvadrata

Kam_izpisati:
"terminal" --> izriše se naj v terminalu
"ime_nove_datoteke" --> odpre se naj datoteko z imenom ime_nove_datoteke in vanj tudi izriše

Velikost:
"stevilo_vrstic" --> koliko vrstic naj uporabi za izris (se upošteva tudi koliko je maksimalno znakov v vrstici --> brez '\n')

Ob začetku preverjanja ukaza, naj v terminal izpiše kakšen je prebrani ukaz (lahko kar v listu, kot je narejeno v primerih), nato pa ga naj izvede. Če pride pri izvajanju do napake, naj program v terminalu napiše, da je prišlo do napake ter opis same napake.

Za pomoč:
"split(znak)" --> razdeli string v list, kjer za razdelitev uporabi "znak" podan v oklepaju
"strip()" --> stringu obrže "prazne znake" kot so presledki, tabulatorji, ... Na čisto desni in levi strani stringa

Razultat izvajanja so tudi ustvarjene datoteke
Za primer datotek imate podani datoteki: "primer_kvadrat.txt" in "primer_trikotnik.txt"

Input:
    izvedi_ukaze("test.txt")

Output (terminal):
    ---------------------- izvajanje ukazov ----------------------

    ukaz: ['kvadrat', 'terminal', '10']
    **********
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    **********

    ukaz: ['kvadrat', 'kvadrat_datoteka.txt', '30']

    ukaz: ['trikotnik', 'terminal', 'asd']
        Ta ukaz ima napako: invalid literal for int() with base 10: 'asd'

    ukaz: ['trikotnik', 'terminal', '1']
    *

    ukaz: ['črtica', 'terminal', '80']
        Ta ukaz ima napako: Ukaz [črtica] neznan

    ukaz: ['trikotniknik', 'tr.txt', '-6']
        Ta ukaz ima napako: Ukaz [trikotniknik] neznan

    ukaz: ['trikotnik', 'terminal', '2']
    *
    **

    ukaz: ['trikotnik', 'trikotnik_datoteka.txt', '20']

    ukaz: ['trikotnik', 'terminal', '3']
    *
    **
    ***

    ukaz: ['trikotnik', 'terminal', '4']
    *
    **
    * *
    ****
    ---------------------- ukazi izvedeni ----------------------

Output (datoteke):
    #Preveri podane datoteke

'''
# Rešitev
def izvedi_ukaze(ime_datoteke):
    with open(ime_datoteke, "r", encoding="utf-8") as f:
        print("---------------------- izvajanje ukazov ----------------------")
        for line in f:
            command = [ x.strip() for x in line.strip().split(",")]
            print("\nukaz:",command)
            try:
                if command[0] == "trikotnik":
                    draw_triangle(command[1:])
                elif command[0] == "kvadrat":
                    draw_square(command[1:])
                else:
                    raise Exception(f"Ukaz [{command[0]}] neznan")
            except Exception as e:
                print(f"\tTa ukaz ima napako: {e}")
        print("---------------------- ukazi izvedeni ----------------------")

def draw_triangle(args):
    h = int(args[1])
    if args[0] == "terminal":
        for i in range(h):
            if i == h-1:
                print("*"*(i+1))
            else:
                print("*"+" "*max(0,(i-1))+("" if i==0 else "*"))
    else:
        with open(args[0], "w", encoding="utf-8") as f:
            for i in range(h):
                if i == h-1:
                    f.write("*"*(i+1)+"\n")
                else:
                    f.write("*"+" "*max(0,(i-1))+("\n" if i==0 else "*\n"))

def draw_square(args):
    size = int(args[1])
    if args[0] == "terminal":
        for i in range(size):
            if i==0 or i==size-1:
                print("*"*size)
            else:
                print("*"+" "*(size-2)+"*")
    else:
        with open(args[0], "w", encoding="utf-8") as f:
            for i in range(size):
                if i==0 or i==size-1:
                    f.write("*"*size+"\n")
                else:
                    f.write("*"+" "*(size-2)+"*\n")

izvedi_ukaze("test.txt")


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

'''
Calcule o tempo de uma viagem de carro. Pergunte a distância a percorrer e a velocidade média
esperada para a viagem.
'''
distancia = float(input('Distancia a percorrer: '))
vm = float(input('Velocidade média: '))

print('Tempo total: {:.2f}'.format(distancia / vm))

Python-para-zumbis/Começando com o básico/06.py


# ----------------Problem: draw a pine tree ---------------

'''
how tall is the tree : 5
    #
   ###
  #####
 #######
#########
    #
    #
    #
    #
'''

# get the numbers of rows for the tree
tree_height = input("How tall is the tree : ")

# Convert into an integer
tree_height = int(tree_height)
spaces = tree_height - 1

hashes = 1

stump_spaces = tree_height - 1

while tree_height != 0:
    # Print spaces
    # end="" means a newline won't be added
    for i in range(spaces):
        print(' ' , end= "")

    for i in range(hashes):
        print("#", end="")

    # Newline after each orw is printed
    print()

    spaces -=1

    hashes +=2

    tree_height -= 1

for i in range(stump_spaces):
    for j in range(stump_spaces):
        print(' ',end="")
    print("#")


from twisted.internet import reactor
from twisted.internet import defer
from twisted.internet import task
from kademlia.network import Server
from kademlia.log import Logger
import time
import copy
import sys
import signal
from twisted.python import log

log.startLogging(sys.stdout)

# time PYTHONPATH=. python examples/benbenchmark.py

class BenchmarkClient(object):
    def __init__(self, server, loops):
        self.verified_values = 0
        self.key_vals_to_store = [("a key %d" % i, "some value %s" % i) for i in range(0, loops)]
        self.failed_value_names = []
        self.server = server
        self.loops = loops
        self.log = Logger(system=self)

    @defer.inlineCallbacks
    def store_many(self, kv_list):
        """Stores all value v against key k for k, v in kv_list """
        self.log.debug("Attempting to store %s (key, val) pairs" % len(kv_list))
        dfrs = []
        for k, v in kv_list:
            dfrs.append(self.write_key_val(k, v))
        dl =  defer.DeferredList(dfrs, consumeErrors=True)
        res = yield dl
        writes_dispatched = len([succeeded  for succeeded, result in res if succeeded])
        write_failure_results = [result  for _succeeded, result in res if not succeeded]
        self.log.debug("%s write failures : %s" % (len(write_failure_results), write_failure_results))
        defer.returnValue(writes_dispatched)

    @defer.inlineCallbacks
    def read_and_verify_many(self, kv_list):
        """Attempts to read all v stored against key k for k, v in kv_list - and
        keeps score if expected values are missing."""
        self.log.debug("Attempting to read and verify %s (key, val) pairs" % len(kv_list))
        dfrs = []
        self.verified_values = 0
        self.failed_value_names = []
        for k, v in kv_list:
            dfrs.append(self.read_key_val(k, v))
        dl =  defer.DeferredList(dfrs, consumeErrors=True)
        res = yield dl
        read_requests_dispatched = len([succeeded  for succeeded, result in res if succeeded])
        defer.returnValue(read_requests_dispatched)

    @defer.inlineCallbacks
    def retry_writes_and_reads(self):
        for retry in range(1, 6):
            yield self._sleep(6)
            self.log.debug("***************** Retrying failures ******************")
            self.log.debug("Retry %s: Failed to retrieve previously stored values for %s items: %s " % (retry, len(self.failed_value_names), self.failed_value_names))
            # self.log.debug("***************** Pinging ******************")
            # Ugly ugly hack
            #pingres = yield self.server.protocol.ping(('127.0.0.1', 8468), self.server.protocol.sourceNode.id)
            # self.log.debug("ping sent... : %s " % str(pingres))
            # bootstrapres = yield self.server.bootstrap([('127.0.0.1', 8468)])
            # self.log.debug("bootstrap sent... : %s " % str(bootstrapres))

            yield self._sleep(6)
            self.log.debug("***************** Retrying writes which failed verification ******************")
            failed_value_names_copy = copy.copy(self.failed_value_names)
            writes_dispatched = yield self.store_many(failed_value_names_copy)
            self.log.debug("Successfully dispatched %s (repeat)  write requests" % writes_dispatched)

            yield self._sleep(5)
            self.log.debug("***************** Retrying read ******************")
            read_requests_dispatched = yield self.read_and_verify_many(failed_value_names_copy)

            self.log.debug("Successfully dispatched %s (repeated) read requests" % read_requests_dispatched)
            self.log.debug("***************** Retrying summary ******************")
            self.log.debug("After write/read retry cycle %s, still failed to retrieve previously stored values for %s items: %s " % (retry, len(self.failed_value_names), self.failed_value_names))

            #self.log.debug("Fetching dump....")
            #dumpres = yield self.server.dump()
            #self.log.debug("Finally, dump result is : %s" %(dumpres,))

    @defer.inlineCallbacks
    def benchmark(self, _result):
        bw = time.time()
        writes_dispatched = yield self.store_many(self.key_vals_to_store)
        aw = time.time()
        self.log.debug("Successfully dispatched %s write requests" % writes_dispatched)

        read_requests_dispatched = yield self.read_and_verify_many(self.key_vals_to_store)
        ar = time.time()
        self.log.debug("Successfully dispatched %s read requests" % read_requests_dispatched)
        self.log.debug("%s of %s writes dispatched; took %s seconds = %s ops/sec" % (writes_dispatched, self.loops, aw - bw,  writes_dispatched / (1.0 * aw - bw)  ))
        self.log.debug("%s of %s reads dispatched; took %s seconds = %s ops/sec  (verified values: %s; failed values %s)" % (read_requests_dispatched, self.loops, ar - aw, self.loops / (1.0 * ar - aw), self.verified_values, len(self.failed_value_names)))

        if self.failed_value_names:
            yield self.retry_writes_and_reads()
        else:
            self.log.debug("All values verified successfully")
        self.log.debug("In the end %s values stored on this client node" % len(self.server.storage.data))

        reactor.stop()

    @defer.inlineCallbacks
    def write_key_val(self, key, val):
        yield self.server.set(key, val)

    @defer.inlineCallbacks
    def read_key_val(self, key, expected_value):
        result = yield self.server.get(key)
        if result == expected_value:
            self.verified_values = self.verified_values + 1
        else:
            self.log.debug( "Unexpected value read(k='%s'): got v='%s'; expected='%s'" % (key, result, expected_value))
            self.failed_value_names.append((key, expected_value))
        defer.returnValue(result)

    @defer.inlineCallbacks
    def _sleep(self, seconds):
        yield task.deferLater(reactor, seconds, lambda: True)

if __name__ == '__main__':
    n_transactions = 5
    if len(sys.argv) > 1:
        n_transactions = int(sys.argv[1])

    def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        reactor.callFromThread(reactor.stop)
    signal.signal(signal.SIGINT, signal_handler)
    server = Server()
    client = BenchmarkClient(server, n_transactions)
    server.listen(8880)
    bootstrap_addrs = [("127.0.0.1", port ) for port in [8881, 8882, 8883]]

    server.bootstrap(bootstrap_addrs, fixed_supernode_addrs=bootstrap_addrs).addCallback(client.benchmark)
    reactor.run()


import hypergan as hg
import tensorflow as tf
from hypergan.skip_connections import SkipConnections

class SkipConnectionsTest(tf.test.TestCase):
    def test_get_none(self):
        sc = SkipConnections()
        self.assertIsNone(sc.get('none'))

    def test_get(self):
        sc = SkipConnections()
        a = tf.zeros([1,2,3])
        b = tf.zeros([3,2,1])

        sc.set('layer_filter', a)
        sc.set('layer_filter', b)
        self.assertEqual(a, sc.get('layer_filter', a.get_shape()))
        self.assertEqual(b, sc.get('layer_filter', b.get_shape()))

    def test_get_closest(self):
        sc = SkipConnections()
        b = tf.zeros([1,2,2])
        a = tf.zeros([1,4,4])

        sc.set('layer_filter', b)
        sc.set('layer_filter', a)
        self.assertEqual(a, sc.get_closest('layer_filter', a.get_shape()))
        self.assertEqual(b, sc.get_closest('layer_filter', b.get_shape()))
        self.assertEqual(a, sc.get_closest('layer_filter', [1,3,3]))
        self.assertEqual(b, sc.get_closest('layer_filter', [1,1,1]))
        self.assertEqual(None, sc.get_closest('layer_filter', [1,5,5]))

    def test_array(self):
        sc = SkipConnections()
        a = tf.zeros([1,2,3])
        b = tf.zeros([3,2,1])

        self.assertEqual([], sc.get_array('layer_filter', a.get_shape()))
        sc.set('layer_filter', a)
        self.assertEqual([a], sc.get_array('layer_filter', a.get_shape()))
        sc.set('layer_filter', a)
        self.assertEqual([a, a], sc.get_array('layer_filter', a.get_shape()))

    def test_get_shapes(self):
        sc = SkipConnections()
        a = tf.zeros([1,2,3])
        b = tf.zeros([3,2,1])

        sc.set('layer_filter', a)
        sc.set('layer_filter', b)
        shapes = sc.get_shapes('layer_filter')
        print("SHAPES", shapes)
        self.assertEqual(len(shapes), 2)
        self.assertEqual(shapes[0], [1,2,3])
        self.assertEqual(shapes[1], [3,2,1])
        self.assertEqual(sc.get_shapes('non-existant'), None)

    def test_clear(self):
        sc = SkipConnections()
        a = tf.zeros([1,2,3])
        b = tf.zeros([3,2,1])

        sc.set('layer_filter', a)
        sc.set('layer_filter', b)
        sc.clear('layer_filter', shape=a.get_shape())
        print("C?LAEAR", sc.connections)
        self.assertEqual(None, sc.get('layer_filter', a.get_shape()))
        self.assertEqual([], sc.get_array('layer_filter', a.get_shape()))
        self.assertEqual(b, sc.get('layer_filter', b.get_shape()))

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


def persistence(n): #multiplies the individual digits given as n
    result = 1
    while n != 0:
        rem = n % 10
        result = result * rem
        n = n // 10
    return result #retruns single digit product

counter = 0
num = int(input("give integer: "))
if len(str(num)) == 1:
    print(counter)

else:
    newnum = persistence(num)
    counter += 1
    while True:
        if len(str(newnum)) > 1:
            newnum = persistence(newnum)
            counter += 1

        else:
            print(counter)

            break


import os
import glob
import shutil
import zipfile
import requests

from ..dicom_factory import is_dicom, dicom_reader

ROOT = os.path.join(os.path.dirname(__file__), 'data')

def test_dicom(tmpdir):

    # We can't include the data in the repository because of the license

    zip_filename = tmpdir.join('BRAINIX.zip').strpath

    data = requests.get('http://www.osirix-viewer.com/datasets/DATA/BRAINIX.zip')

    with open(zip_filename, 'wb') as f:
        f.write(data.content)

    zf = zipfile.ZipFile(zip_filename)
    zf.extractall(tmpdir.strpath)

    base_dir = os.path.join(tmpdir.strpath, 'BRAINIX', 'BRAINIX')

    # Because the paths contain unicode, and extractall can mess with it,
    # we get the next directory level programmatically
    new_base_dir = glob.glob(os.path.join(base_dir, '*'))[0]
    base_dir = os.path.join(base_dir, 'datasets')

    # We now rename it to avoid issues
    os.rename(new_base_dir, base_dir)

    assert not is_dicom(base_dir)
    assert is_dicom(os.path.join(base_dir, 'T1-3D-FFE-C - 801'))
    assert is_dicom(os.path.join(base_dir, 'T1-3D-FFE-C - 801', 'IM-0001-0054.dcm'))

    data = dicom_reader(os.path.join(base_dir, 'T1-3D-FFE-C - 801'))[0]
    assert data.shape == (100, 256, 256)

    data = dicom_reader(os.path.join(base_dir, 'T1-3D-FFE-C - 801', 'IM-0001-0054.dcm'))[0]
    assert data.shape == (256, 256)

    hete_dir = os.path.join(tmpdir.strpath, 'heterogeneous')

    os.mkdir(hete_dir)

    shutil.copy2(os.path.join(base_dir, 'T1-3D-FFE-C - 801', 'IM-0001-0054.dcm'),
                 os.path.join(hete_dir, 'file1.dcm'))

    shutil.copy2(os.path.join(base_dir, 'sT2W-FLAIR - 401', 'IM-0001-0017.dcm'),
                 os.path.join(hete_dir, 'file2.dcm'))

    datasets = dicom_reader(hete_dir)
    assert len(datasets) == 2
    assert datasets[0].label == 'file1'
    assert datasets[0].shape == (256, 256)
    assert datasets[1].label == 'file2'
    assert datasets[1].shape == (288, 288)


"""
MicroService Transferor configuration file.
"""

import socket
from WMCore.Configuration import Configuration

# globals
HOST = socket.gethostname().lower()
BASE_URL = "@@BASE_URL@@"
DBS_INS = "@@DBS_INS@@"
LOG_REPORTER = "reqmgr2ms"
ROOTDIR = __file__.rsplit('/', 3)[0]

if BASE_URL == "https://cmsweb.cern.ch":
    RUCIO_ACCT = "production"
else:
    RUCIO_ACCT="wmagent_testing"

config = Configuration()

main = config.section_("main")
srv = main.section_("server")
srv.thread_pool = 30
main.application = "ms-transferor"
main.port = 8247  # main application port it listens on
main.index = 'data' # Configuration requires index attribute

# Security configuration
main.authz_defaults = {"role": None, "group": None, "site": None}
#set default logging (prevent duplicate)
main.log_screen = True

sec = main.section_("tools").section_("cms_auth")
sec.key_file = "%s/auth/wmcore-auth/header-auth-key" % ROOTDIR

# this is where the application will be mounted, where the REST API
# is reachable and this features in CMS web frontend rewrite rules
app = config.section_(main.application)
app.admin = "cms-service-webtools@cern.ch"
app.description = "CMS Workload Management MicroServices"
app.title = "CMS MicroService Transferor"

# define different views for our application
views = config.section_("views")
# web UI interface
ui = views.section_('web') # was section 'ui'
ui.object = 'WMCore.MicroService.WebGui.FrontPage.FrontPage'
ui.static = ROOTDIR

# REST interface
data = views.section_('data')
data.object = 'WMCore.MicroService.Service.RestApiHub.RestApiHub'
data.manager = 'WMCore.MicroService.Unified.MSManager.MSManager'
data.reqmgr2Url = "%s/reqmgr2" % BASE_URL
data.readOnly = True
data.verbose = True
data.interval = 600
data.services = ['transferor']
data.rucioAccount = RUCIO_ACCT
data.phedexUrl = "https://cmsweb.cern.ch/phedex/datasvc/json/prod"
data.toAddr = ["cms-comp-ops-workflow-team@cern.ch"]
data.warningTransferThreshold = 100. * (1000 ** 4) # 100 TB (terabyte)
# if private_vm, just fallback to preprod DBS
if DBS_INS == "private_vm":
    data.dbsUrl = "https://cmsweb-testbed.cern.ch/dbs/int/global/DBSReader"
else:
    data.dbsUrl = "%s/dbs/%s/global/DBSReader" % (BASE_URL, DBS_INS)


import os
import difflib

from qisys import ui
import qisys.command
import qisys.worktree
import qibuild.build
import qibuild.build_config
import qibuild.deps
import qibuild.project

class BuildWorkTree(qisys.worktree.WorkTreeObserver):
    """ Stores a list of projects that can be built using CMake

    """
    def __init__(self, worktree):
        self.worktree = worktree
        self.root = self.worktree.root
        self.build_config = qibuild.build_config.CMakeBuildConfig(self)
        self.build_projects = list()
        self._load_build_projects()
        worktree.register(self)

    @property
    def dot_qi(self):
        return self.worktree.dot_qi

    @property
    def qibuild_cfg(self):
        return self.build_config.qibuild_cfg

    @property
    def qibuild_xml(self):
        """ Path to <worktree>/.qi/qibuild.xml
        Will be created if it does not exist

        """
        config_path = os.path.join(self.dot_qi, "qibuild.xml")
        if not os.path.exists(config_path):
            with open(config_path, "w") as fp:
                fp.write("<qibuild />\n")
        return config_path

    @property
    def toolchain(self):
        """ The toolchain to use """
        return self.build_config.toolchain

    @property
    def default_config(self):
        """ The default config to use """
        return self.build_config.default_config

    def get_build_project(self, name, raises=True):
        """ Get a :py:class:`.BuildProject` given its name """
        for build_project in self.build_projects:
            if build_project.name == name:
                return build_project
        if raises:
            mess = ui.did_you_mean("No such qibuild project: %s" % name,
                                   name, [x.name for x in self.build_projects])
            raise BuildWorkTreeError(mess)

    def on_project_added(self, project):
        """ Called when a new project has been registered """
        self._load_build_projects()

    def on_project_removed(self, project):
        """ Called when a build project has been removed """
        self._load_build_projects()

    def on_project_moved(self, project):
        self._load_build_projects()

    def _load_build_projects(self):
        """ Create BuildProject for every buildable project in the
        worktree

        """
        self.build_projects = list()
        for wt_project in self.worktree.projects:
            build_project = new_build_project(self, wt_project)
            if build_project:
                self.check_unique_name(build_project)
                self.build_projects.append(build_project)

    def configure_build_profile(self, name, flags):
        """ Configure a build profile for the worktree """
        qibuild.profile.configure_build_profile(self.qibuild_xml,
                                                name, flags)

    def remove_build_profile(self, name):
        """ Remove a build profile for this worktree """
        qibuild.profile.remove_build_profile(self.qibuild_xml, name)

    def set_default_config(self, name):
        """ Set the default toolchain for this worktree """
        tree = qisys.qixml.read(self.qibuild_xml)
        root = tree.getroot()
        defaults = root.find("defaults")
        if defaults is None:
            defaults = qisys.qixml.etree.Element("defaults")
            root.append(defaults)
        defaults.set("config", name)
        qisys.qixml.write(tree, self.qibuild_xml)

    def set_active_config(self, active_config):
        """ Set the config to use for this worktree
        Should match a toolchain name

        """
        self.build_config.set_active_config(active_config)

    def check_unique_name(self, new_project):
        for project in self.build_projects:
            if project.name == new_project.name:
                raise Exception("""\
Found two projects with the same name ({project.name})
In:
* {project.path}
* {new_project.path}
""".format(project=project, new_project=new_project))

def new_build_project(build_worktree, project):
    """ Cerate a new BuildProject from a worktree project.
    Return None if there is no BuildProject here

    """
    if not os.path.exists(project.qiproject_xml):
        return None
    tree = qisys.qixml.read(project.qiproject_xml)
    root = tree.getroot()
    if root.get("version") == "3":
        qibuild_elem = root.find("qibuild")
        if qibuild_elem is None:
            return None
    else:
        # qibuild2 used to check for a CMakeLists.txt
        cmake_lists = os.path.join(project.path, "CMakeLists.txt")
        if not os.path.exists(cmake_lists):
            return None
        qibuild_elem = root

    name = qibuild_elem.get("name")
    if not name:
        return None

    build_project = qibuild.project.BuildProject(build_worktree, project)
    build_project.name = name
    build_project.version = qibuild_elem.get("version", "0.1")
    qibuild.deps.read_deps_from_xml(build_project, qibuild_elem)

    return build_project

class BuildWorkTreeError(Exception):
    pass

class BadProjectConfig(Exception):
    def __str__(self):
        return """
Incorrect configuration detected for project in {0}
{1}
""".format(*self.args)


"""
Problem:

Given a list of numbers, create an algorithm that arranges them in order to form the largest possible integer.

Example:

Input = [10, 7, 76, 415]
Output = 77641510
"""

class ModifiedInt:
    # helper class for the sort operation (requires __lt__ dunder method)

    def __init__(self, val):
        if type(val) != int:
            raise TypeError
        self.val = str(val)

    def __lt__(self, other):
        # returns the number with shorter length or has a smaller more significant digit
        if type(other) == ModifiedInt:
            if self.val == other.val:
                return False

            for c1, c2 in zip(self.val, other.val):
                if c1 > c2:
                    return False
                elif c1 < c2:
                    return True

            if len(self.val) > len(other.val):
                return True
        return False

def get_largest(arr):
    arr = list(map(ModifiedInt, arr))
    arr.sort(reverse=True)
    return int("".join(map(lambda x: x.val, arr)))

# DRIVER CODE
print(get_largest([10, 7, 76, 415]))


import os
import sys
import django
import requests
import argparse
import json
from decouple import config
from typing import NoReturn
from abc import ABC, abstractmethod

from tweepy.error import TweepError
from tweepy import OAuthHandler

class Activity:
    _protocol: str = "https:/"
    _host: str = "api.twitter.com"
    _version: str = "1.1"
    _product: str = "account_activity"
    _auth: OAuthHandler = OAuthHandler(
        config('TWITTER_BOT_CONSUMER_KEY'), config('TWITTER_BOT_CONSUMER_SECRET')
    )
    _auth.set_access_token(
        config('TWITTER_BOT_ACCESS_TOKEN'), config('TWITTER_BOT_ACCESS_TOKEN_SECRET')
    )

    def api(self, method: str, endpoint: str, data: dict = None) -> json:
        """
        :param method: GET or POST
        :param endpoint: API Endpoint to be specified by user
        :param data: POST Request payload parameter
        :return: json
        """
        try:
            with requests.Session() as r:
                response = r.request(
                    url="/".join(
                        [
                            self._protocol,
                            self._host,
                            self._version,
                            self._product,
                            endpoint,
                        ]
                    ),
                    method=method,
                    auth=self._auth.apply_auth(),
                    data=data,
                )
                return response
        except TweepError:
            raise

    def register_webhook(self, callback_url: str) -> json:
        try:
            return self.api(
                method="POST",
                endpoint=f"all/{config('TWITTER_ENV_NAME')}/webhooks.json",
                data={"url": callback_url},
            ).json()
        except Exception as e:
            raise e

    def refresh(self, webhook_id: str) -> NoReturn:
        """Refreshes CRC for the provided webhook_id.
        """
        try:
            return self.api(
                method="PUT",
                endpoint=f"all/{config('TWITTER_ENV_NAME')}/webhooks/{webhook_id}.json",
            )
        except Exception as e:
            raise e

    def delete (self, webhook_id: str) -> NoReturn:
        """Removes the webhook from the provided webhook_id.
        """
        try:
            return self.api(
                method="DELETE",
                endpoint=f"all/{config('TWITTER_ENV_NAME')}/webhooks/{webhook_id}.json",
            )
        except Exception as e:
            raise e

    def subscribe(self) -> NoReturn:
        try:
            return self.api(
                method="POST",
                endpoint=f"all/{config('TWITTER_ENV_NAME')}/subscriptions.json",
            )
        except Exception:
            raise

    def webhooks(self) -> json:
        """Returns all environments, webhook URLs and their statuses for the authenticating app.
        Only one webhook URL can be registered to each environment.
        """
        try:
            return self.api(method="GET", endpoint=f"all/webhooks.json").json()
        except Exception as e:
            raise e

if __name__ == "__main__":
    activity = Activity()
    # activity.delete('1363218664019947520')
    # print(activity.webhooks())
    res = activity.register_webhook(
        callback_url=config('TWITTER_AUTH_WEBHOOK_URL')
    )
    print(activity.subscribe())


import logging
import re

# noinspection PyPackageRequirements
from telegram.ext import (
    CommandHandler,
    MessageHandler,
    ConversationHandler,
    CallbackContext,
    Filters
)
# noinspection PyPackageRequirements
from telegram import ChatAction, Update

from bot import stickersbot
from bot.strings import Strings
from bot.database.base import session_scope
from bot.database.models.pack import Pack
from bot.sticker import StickerFile
import bot.sticker.error as error
from ..conversation_statuses import Status
from ..fallback_commands import cancel_command
from ..fallback_commands import STANDARD_CANCEL_COMMANDS
from ..stickers.add import on_static_sticker_receive
from ..stickers.add import on_animated_sticker_receive
from ..stickers.add import on_bad_static_sticker_receive
from ..stickers.add import on_bad_animated_sticker_receive
from ...customfilters import CustomFilters
from ...utils import decorators
from ...utils import utils

logger = logging.getLogger(__name__)

@decorators.action(ChatAction.TYPING)
@decorators.restricted
@decorators.failwithmessage
@decorators.logconversation
def on_create_static_pack_command(update: Update, context: CallbackContext):
    logger.info('/create')

    context.user_data['pack'] = dict(animated=False)

    update.message.reply_text(Strings.PACK_CREATION_STATIC_WAITING_TITLE)

    return Status.WAITING_TITLE

@decorators.action(ChatAction.TYPING)
@decorators.restricted
@decorators.failwithmessage
@decorators.logconversation
def on_create_animated_pack_command(update: Update, context: CallbackContext):
    logger.info('/createanimated')

    context.user_data['pack'] = dict(animated=True)

    update.message.reply_text(Strings.PACK_CREATION_ANIMATED_WAITING_TITLE)

    return Status.WAITING_TITLE

@decorators.action(ChatAction.TYPING)
@decorators.failwithmessage
@decorators.logconversation
def on_pack_title_receive(update: Update, context: CallbackContext):
    logger.info('received possible pack title')

    if len(update.message.text) > 64:
        logger.info('pack title too long: %s', update.message.text)
        update.message.reply_text(Strings.PACK_TITLE_TOO_LONG)
        # do not change the user status and let him send another title
        return Status.WAITING_TITLE

    if '\n' in update.message.text:
        logger.info('pack title contains newline character')
        update.message.reply_text(Strings.PACK_TITLE_CONTAINS_NEWLINES)
        # do not change the user status and let him send another title
        return Status.WAITING_TITLE

    logger.info('pack title is valid')

    context.user_data['pack']['title'] = update.message.text

    # max len of a pack name = 64 - "_by_botusername", final string always added by the API
    max_name_len = 64 - (len(context.bot.username) + 4)

    text = Strings.PACK_CREATION_WAITING_NAME.format(update.message.text, max_name_len)
    update.message.reply_html(text)

    return Status.WAITING_NAME

@decorators.action(ChatAction.TYPING)
@decorators.failwithmessage
@decorators.logconversation
def on_pack_name_receive(update: Update, context: CallbackContext):
    logger.info('received possible pack name (link)')
    logger.debug('user_data: %s', context.user_data)

    candidate_name = update.message.text
    max_name_len = 64 - (len(context.bot.username) + 4)
    if len(candidate_name) > max_name_len:
        logger.info('pack name too long (%d/%d)', len(candidate_name), max_name_len)
        update.message.reply_text(Strings.PACK_NAME_TOO_LONG.format(len(update.message.text), max_name_len))
        # do not change the user status and let him send another name
        return Status.WAITING_NAME

    if not re.search(r'^[a-z](?!__)\w+$', candidate_name, re.IGNORECASE):
        logger.info('pack name not valid: %s', update.message.text)
        update.message.reply_html(Strings.PACK_NAME_INVALID)
        # do not change the user status and let him send another name
        return Status.WAITING_NAME

    name_already_used = False
    with session_scope() as session:
        # https://stackoverflow.com/a/34112760
        if session.query(Pack).filter(Pack.user_id==update.effective_user.id, Pack.name==candidate_name).first() is not None:
            logger.info('pack name already saved: %s', candidate_name)
            name_already_used = True

    if name_already_used:
        update.message.reply_text(Strings.PACK_NAME_DUPLICATE)
        # do not change the user status and let him send another name
        return Status.WAITING_NAME

    logger.info('valid pack name: %s', candidate_name)

    context.user_data['pack']['name'] = candidate_name

    if context.user_data['pack']['animated']:
        text = Strings.PACK_CREATION_WAITING_FIRST_ANIMATED_STICKER
    else:
        text = Strings.PACK_CREATION_WAITING_FIRST_STATIC_STICKER

    update.message.reply_text(text)

    return Status.WAITING_FIRST_STICKER

@decorators.action(ChatAction.TYPING)
@decorators.failwithmessage
@decorators.logconversation
def on_bad_first_static_sticker_receive(update: Update, _):
    logger.info('user sent an animated sticker instead of a static one')

    update.message.reply_text(Strings.ADD_STICKER_EXPECTING_STATIC)

    return Status.WAITING_FIRST_STICKER

@decorators.action(ChatAction.TYPING)
@decorators.failwithmessage
@decorators.logconversation
def on_first_sticker_receive(update: Update, context: CallbackContext):
    logger.info('first sticker of the pack received')
    logger.debug('user_data: %s', context.user_data)

    animated_pack = context.user_data['pack']['animated']
    if update.message.document:
        animated_sticker = False
    else:
        animated_sticker = update.message.sticker.is_animated

    if animated_pack and not animated_sticker:
        logger.info('invalid sticker: static sticker for an animated pack')
        update.message.reply_text(Strings.ADD_STICKER_EXPECTING_ANIMATED)
        return Status.WAITING_FIRST_STICKER
    elif not animated_pack and animated_sticker:
        logger.info('invalid sticker: animated sticker for a static pack')
        update.message.reply_text(Strings.ADD_STICKER_EXPECTING_STATIC)
        return Status.WAITING_FIRST_STICKER
    else:
        logger.info('sticker type ok (animated pack: %s, animated sticker: %s)', animated_pack, animated_sticker)

    title, name = context.user_data['pack'].get('title', None), context.user_data['pack'].get('name', None)
    if not title or not name:
        logger.error('pack title or name missing (title: %s, name: %s)', title, name)
        update.message.reply_text(Strings.PACK_CREATION_FIRST_STICKER_PACK_DATA_MISSING)

        context.user_data.pop('pack', None)  # remove temp info

        return ConversationHandler.END

    full_name = '{}_by_{}'.format(name, context.bot.username)

    sticker = StickerFile(update.message.sticker or update.message.document, caption=update.message.caption)
    sticker.download(prepare_png=True)

    try:
        logger.debug('executing API request...')
        request_payload = dict(
            user_id=update.effective_user.id,
            title=title,
            name=full_name,
            emojis=sticker.emoji,
        )

        if animated_pack:
            request_payload['tgs_sticker'] = sticker.tgs_input_file
        else:
            # we need to use an input file becase a tempfile.SpooledTemporaryFile has a 'name' attribute which
            # makes python-telegram-bot retrieve the file's path using os (https://github.com/python-telegram-bot/python-telegram-bot/blob/2a3169a22f7227834dd05a35f90306375136e41a/telegram/files/inputfile.py#L58)
            # to populate the 'filename' attribute, which would result an exception since it is
            # a byte object. That means we have to do it ourself by  creating the InputFile and
            # assigning it a custom 'filename'
            request_payload['png_sticker'] = sticker.png_input_file

        StickerFile.create_set(bot=context.bot, **request_payload)
    except (error.PackInvalid, error.NameInvalid, error.NameAlreadyOccupied) as e:
        logger.error('Telegram error while creating stickers pack: %s', e.message)
        if isinstance(e, error.NameAlreadyOccupied):
            # there's already a pack with that link
            update.message.reply_html(Strings.PACK_CREATION_ERROR_DUPLICATE_NAME.format(utils.name2link(full_name)))
        elif isinstance(e, (error.PackInvalid, error.NameInvalid)):
            update.message.reply_text(Strings.PACK_CREATION_ERROR_INVALID_NAME)

        context.user_data['pack'].pop('name', None)  # remove pack name
        sticker.close()

        return Status.WAITING_NAME  # do not continue, wait for another name
    except error.InvalidAnimatedSticker as e:
        logger.error('Telegram error while creating animated pack: %s', e.message)
        update.message.reply_html(Strings.ADD_STICKER_INVALID_ANIMATED, quote=True, disable_web_page_preview=True)

        return Status.WAITING_FIRST_STICKER
    except error.UnknwonError as e:
        logger.error('Unknown error while creating the pack: %s', e.message)
        update.message.reply_html(Strings.PACK_CREATION_ERROR_GENERIC.format(e.message))

        context.user_data.pop('pack', None)  # remove temp data
        sticker.close()

        return ConversationHandler.END  # do not continue, end the conversation
    else:
        # success

        pack_row = Pack(user_id=update.effective_user.id, name=full_name, title=title, is_animated=animated_pack)
        with session_scope() as session:
            session.add(pack_row)

        # db.save_pack(update.effective_user.id, full_name, title)
        pack_link = utils.name2link(full_name)
        update.message.reply_html(Strings.PACK_CREATION_PACK_CREATED.format(pack_link))

        sticker.close()  # remove sticker files

        context.user_data['pack']['name'] = full_name
        # do not remove temporary data (user_data['pack']) because we are still adding stickers

        # wait for other stickers
        if animated_pack:
            return Status.WAITING_ANIMATED_STICKERS
        else:
            return Status.WAITING_STATIC_STICKERS

stickersbot.add_handler(ConversationHandler(
    name='pack_creation',
    persistent=True,
    entry_points=[
        CommandHandler(['create', 'new', 'n'], on_create_static_pack_command),
        CommandHandler(['createanimated', 'newanimated', 'na'], on_create_animated_pack_command)
    ],
    states={
        Status.WAITING_TITLE: [MessageHandler(Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), on_pack_title_receive)],
        Status.WAITING_NAME: [MessageHandler(Filters.text & ~Filters.command(STANDARD_CANCEL_COMMANDS), on_pack_name_receive)],
        Status.WAITING_FIRST_STICKER: [
            MessageHandler(  # this handler is shared by both static and animated stickers
                Filters.sticker | Filters.document.category('image/png'),
                on_first_sticker_receive
            )
        ],
        Status.WAITING_STATIC_STICKERS: [
            MessageHandler(
                CustomFilters.static_sticker | Filters.document.category('image/png'),
                on_static_sticker_receive
            ),
            MessageHandler(CustomFilters.animated_sticker, on_bad_static_sticker_receive),
        ],
        Status.WAITING_ANIMATED_STICKERS: [
            MessageHandler(CustomFilters.animated_sticker, on_animated_sticker_receive),
            MessageHandler(
                CustomFilters.static_sticker | Filters.document.category('image/png'),
                on_bad_animated_sticker_receive
            ),
        ]
    },
    fallbacks=[CommandHandler(STANDARD_CANCEL_COMMANDS, cancel_command)]
))


import numpy as np
from rlberry.agents.features import FeatureMap
from rlberry.envs.finite import GridWorld
from rlberry.stats import AgentStats, plot_episode_rewards,\
    compare_policies
from rlberry.agents.dynprog import ValueIterationAgent
from rlberry.agents.linear import LSVIUCBAgent
from rlberry.agents.kovi.kovi2 import KOVIAgent

from rlberry.utils.jit_setup import numba_jit

# Define environment
env = GridWorld(nrows=2, ncols=4, walls=(), success_probability=1.0)

# Create pd kernel
# @numba_jit
def pd_kernel(state1, action1, state2=None, action2=None, type='gaussian'):
    if state2 is None and action2 is None:
        state2, action2 = state1, action1

    if type == 'gaussian':
        sigma = 1.0
        # return np.exp(- np.linalg.norm(state1 - state2)**2 / (2 * sigma)) * (action1 == action2)
        return np.exp(- (state1 - state2) ** 2 / (2 * sigma)) * (action1 == action2)
    else:
        return

params = {'n_episodes': 500,
          'pd_kernel_fn': pd_kernel,
          'horizon': 10,
          'bonus_scale_factor': 0.01,
          'gamma': 0.99
          }

params_greedy = {'n_episodes': 500,
                 'pd_kernel_fn': pd_kernel,
                 'horizon': 10,
                 'bonus_scale_factor': 0.0,
                 'gamma': 0.99
                 }

params_oracle = {
                    'horizon': 10,
                    'gamma': 0.99
                    }

stats = AgentStats(KOVIAgent,
                   env,
                   eval_horizon=10,
                   init_kwargs=params,
                   n_fit=1)

stats_random = AgentStats(KOVIAgent,
                          env,
                          eval_horizon=10,
                          init_kwargs=params_greedy,
                          n_fit=1,
                          agent_name='LSVI-random-expl')

oracle_stats = AgentStats(ValueIterationAgent,
                          env,
                          eval_horizon=10,
                          init_kwargs=params_oracle,
                          n_fit=1)

plot_episode_rewards([stats, stats_random], cumulative=True, show=False)
compare_policies([stats, stats_random, oracle_stats], show=True)


import json
import urllib
import webapp2

from google.appengine.api import mail
from google.appengine.api import urlfetch

from src import common
from src.handlers.base import BaseHandler
from src.jinja import JINJA_ENVIRONMENT
from src.models.app_settings import AppSettings

def ContactHandler(contact_email, template_path, subject_prefix):
  class Handler(BaseHandler):
    def get(self):
      template = JINJA_ENVIRONMENT.get_template(template_path)
      self.response.write(template.render({
          'c': common.Common(self),
          'result': 'success' if self.request.get('success') else '',
      }))

    def post(self):
      template = JINJA_ENVIRONMENT.get_template(template_path)
      app_settings = AppSettings.Get()
      if app_settings.recaptcha_secret_key:
        payload = {
            'secret': app_settings.recaptcha_secret_key,
            'response': self.request.get('g-recaptcha-response'),
            'remoteip': self.request.remote_addr,
        }
        fetch = urlfetch.fetch(
            url='https://www.google.com/recaptcha/api/siteverify',
            payload=urllib.urlencode(payload),
            method=urlfetch.POST)
        result = json.loads(fetch.content)
        if not result['success']:
          self.response.write(template.render({
              'c': common.Common(self),
              'result': 'failure',
          }))
          return
      subject = '[%s] Contact form -- %s' % (subject_prefix, self.request.get('name'))
      if self.request.get('wcaid'):
        body = 'WCA ID: %s\n%s' % (self.request.get('wcaid'),
                                   self.request.get('contact-message'))
      else:
        body = self.request.get('contact-message')
      mail.send_mail(sender=contact_email,
                     to=contact_email,
                     cc=self.request.get('from-address'),
                     subject=subject,
                     body=body)
      self.redirect(self.request.url + '?success=1')

  return Handler


#!/usr/bin/env python
# Wazuh Ruleset Update

# v3.0.0 2016/12/23
# Created by Wazuh, Inc. <info@wazuh.com>.
# jesus@wazuh.com
# This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2

# Requirements:
#  Python 2.6 or newer
#  Wazuh
#  root privileges
# http://wazuh-documentation.readthedocs.io/en/latest/wazuh_ruleset.html

import os
import sys
import contextlib
from glob import glob
from zipfile import ZipFile
from getopt import GetoptError, getopt
from signal import signal, SIGINT
from datetime import datetime, date
from pwd import getpwnam
from grp import getgrnam
from shutil import copyfile, copytree, rmtree
from re import sub, search
from filecmp import cmp
from time import time
from json import dumps

try:
    from urllib2 import urlopen, URLError, HTTPError
except:
    from urllib.request import urlopen  # Python 3

class RulesetLogger:
    O_STDOUT = 1
    O_FILE = 2
    O_ALL = 3

    def __init__(self, tag, filename=None, flag=3, debug=False):
        self.tag = tag
        self.flag = flag
        self.debug_mode = debug
        try:
            self.logger = open(filename, 'a')
        except:
            print("Error opening log '{0}'".format(filename))
            sys.exit(1)

    def log(self, msg):
        if self.flag == RulesetLogger.O_ALL or self.flag == RulesetLogger.O_STDOUT:
            print(msg)

        if self.flag == RulesetLogger.O_ALL or self.flag == RulesetLogger.O_FILE:
            self.__write_file(msg)

    def debug(self, msg):
        if self.debug_mode:
            print("DEBUG: {0}".format(msg))

    def __write_file(self, message):
        timestamp = datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S')
        new_msg = sub('[\n\t]', '', message)
        log = "{0} [{1}]: {2}".format(self.tag, timestamp, new_msg)
        self.logger.write("{0}\n".format(log))

    def __del__(self):
        self.logger.close()

# Aux functions
def chown(path, uid, gid):
    os.chown(path, uid, gid)

    if os.path.isdir(path):
        for item in os.listdir(path):
            itempath = os.path.join(path, item)
            if os.path.isfile(itempath):
                os.chown(itempath, uid, gid)
            elif os.path.isdir(itempath):
                chown(itempath, uid, gid)

def chmod(path, mode):
    os.chmod(path, mode)

    if os.path.isdir(path):
        for item in os.listdir(path):
            itempath = os.path.join(path, item)
            if os.path.isfile(itempath):
                os.chmod(itempath, mode)
            elif os.path.isdir(itempath):
                chmod(itempath, mode)

def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)
        chown(path, root_uid, ossec_gid)
        chmod(path, file_permissions)

def rename(src, dst):
    os.rename(src, dst)
    chown(dst, root_uid, ossec_gid)
    chmod(dst, file_permissions)

def copy(src, dst, executable=False):
    if os.path.isfile(src):
        copyfile(src, dst)
    else:
        copytree(src, dst)

    chown(dst, root_uid, ossec_gid)
    if executable:
        chmod(dst, file_permissions_x)
    else:
        chmod(dst, file_permissions)

def rm(path):
    if os.path.exists(path):
        rmtree(path)

def signal_handler(n_signal, frame):
    sys.exit(1)

def get_stdin(msg):
    try:
        stdin = raw_input(msg)
    except:
        # Python 3
        stdin = input(msg)
    return stdin

def regex_in_file(regex, filepath):
    with open(filepath) as f:
        m = search(regex, f.read())
        if m:
            return True
        else:
            return False

def compare_files(file1, file2):
    """
    :param file1: File to be compared
    :param file2: File to be compared
    :return: Returns true if file1 is equal to file2, and false otherwise. Also returns false if file2 does not exist.
    """

    if os.path.isfile(file1) and os.path.isfile(file2):
        same = cmp(file1, file2)
    else:
        same = False

    return same

def exit(code, msg=None):
    logger.log("ERROR: {0}".format(msg))

    if arguments['json']:
        json_res = {'error': code}
        msg_res = msg.replace('\t', '').replace('\n', '')
        if code == 0:
            json_res['data'] = msg_res
        else:
            json_res['message'] = msg_res

        print(dumps(json_res))

    sys.exit(code)

# Functions
def get_branch():
    git_wazuh_ruleset = "./.git/HEAD"
    git_wazuh = "../../../.git/HEAD"
    branch_file = None

    if os.path.isfile('VERSION') and os.path.isfile(git_wazuh_ruleset):  # wazuh-ruleset
        branch_file = git_wazuh_ruleset
    elif os.path.isfile('RULESET_VERSION') and os.path.isfile(git_wazuh):  # wazuh
        branch_file = git_wazuh

    try:
        with open(branch_file, "r") as f:
            lines = f.readlines()

        current_branch = lines[0].split('/')[-1].strip()
    except:
        current_branch = 'stable'  # standalone script

    return current_branch

def get_ossec_version():
    init_file = "{0}/etc/ossec-init.conf".format(ossec_path)

    try:
        ossec_v = "old"
        is_wazuh = False

        f_ossec = open(init_file)
        for line in f_ossec.readlines():
            line_lower = line.lower()

            if "wazuh_version=\"" in line_lower:
                is_wazuh = True
                ossec_v = line.strip("\n").split("=")[1]
                break
            elif "name=\"wazuh\"" in line_lower:
                is_wazuh = True
            elif "version=\"" in line_lower:
                ossec_v = line.strip("\n").split("=")[1]

        f_ossec.close()

        return is_wazuh, ossec_v
    except:
        exit(2, "Reading '{0}'.".format(init_file))

def get_ruleset_version():
    try:
        f_version = open(ossec_ruleset_version_path)
        rs_version = f_version.read().strip("\n").split("=")[1][2:-1]
        f_version.close()
    except:
        rs_version = "N/A"

    return rs_version

def get_new_ruleset(source):
    mkdir(update_downloads)
    rm(update_ruleset)

    if source == 'download':
        branch = get_branch()  # 'stable' 'master' 'development'
        url_ruleset = "https://github.com/wazuh/wazuh-ruleset/archive/{0}.zip".format(branch)
        ruleset_zip = "{0}/ruleset.zip".format(update_downloads)

        # Download
        try:
            f_url = urlopen(url_ruleset)
            with open(ruleset_zip, "wb") as f_local:
                f_local.write(f_url.read())
        except HTTPError as e:
            exit(2, "\tHTTP Error {0}: {1}".format(e.code, url_ruleset))
        except URLError as e:
            exit(2, "\tURL Error - {0}: {1}".format(e.reason, url_ruleset))
        except Exception as e:
            exit(2, "\tDownload Error:{0}.\nExit.".format(e))

        # Extract
        try:
            with contextlib.closing(ZipFile(ruleset_zip)) as z:
                z.extractall(update_downloads)
        except Exception as e:
            exit(2, "\tError extracting file '{0}': {1}.".format(ruleset_zip, e))

        # Rename
        rename("{0}/wazuh-ruleset-{1}".format(update_downloads, branch), update_ruleset)

    else:
        # New ruleset
        if not os.path.exists(source):
            exit(2, "Directory doest not exist: '{0}'.\nExit.".format(source))

        # Copy
        copy(source, update_ruleset)

        # Check new ruleset
        check_files = ["rootchecks", "rules", "decoders", "VERSION"]
        for cf in check_files:
            if not os.path.exists("{0}/{1}".format(update_ruleset, cf)):
                exit(2, "'{0}' doest not exist at '{1}'.\nExit.".format(cf, source))

    # Update main directory
    copy("{0}/VERSION".format(update_ruleset), ossec_ruleset_version_path)
    copy("{0}/update_ruleset.py".format(update_ruleset), ossec_update_script, executable=True)

    return get_ruleset_version()

def get_ruleset_to_update(no_checks=False):
    ruleset_update = {"rules": [], "decoders": [], "rootchecks": []}
    restart_ossec_needed = False

    for item in ruleset_update.keys():
        if item == 'rules':
            src = "{0}/*.xml".format(update_rules)
            dst = ossec_rules
        elif item == 'decoders':
            src = "{0}/*.xml".format(update_decoders)
            dst = ossec_decoders
        elif item == 'rootchecks':
            src = "{0}/*.txt".format(update_rootchecks)
            dst = ossec_rootchecks

        for src_item in glob(src):
            filename = src_item.split("/")[-1]

            if no_checks:
                ruleset_update[item].append(filename)
                restart_ossec_needed = True
            else:  # Compare: New / Changed files
                dst_item = "{0}/{1}".format(dst, filename)
                item_equal = compare_files(src_item, dst_item)

                if not item_equal:
                    if arguments['debug']:
                        logger.debug("\t{0} '{1}' == {0} '{2}'?\t{3}.".format(item, src_item, dst_item, item_equal))
                    ruleset_update[item].append(filename)

                    if item != 'rootchecks':
                        restart_ossec_needed = True
                    else:
                        # Restart ossec just if the rootcheck is enabled in ossec.conf
                        # The script does not activate the rootchecks.
                        if regex_in_file("\s*<.+>.*{0}</.+>".format(filename), ossec_conf):
                            restart_ossec_needed = True

    # Save ruleset
    ruleset_update["rules"] = sorted(ruleset_update["rules"])
    ruleset_update["decoders"] = sorted(ruleset_update["decoders"])
    ruleset_update["rootchecks"] = sorted(ruleset_update["rootchecks"])

    return ruleset_update, restart_ossec_needed

def upgrade_ruleset(ruleset):

    rm(update_backups)
    mkdir(update_backups)
    mkdir(update_backups_rules)
    mkdir(update_backups_decoders)
    mkdir(update_backups_rootchecks)

    for item in ruleset.keys():
        if not ruleset[item]:
            logger.log("You already have the latest version of {0}.".format(item))
            continue

        if item == 'rules':
            src = update_rules
            dst = ossec_rules
            backup = update_backups_rules
        elif item == 'decoders':
            src = update_decoders
            dst = ossec_decoders
            backup = update_backups_decoders
        elif item == 'rootchecks':
            src = update_rootchecks
            dst = ossec_rootchecks
            backup = update_backups_rootchecks

        logger.log("\nThe following {0} will be updated:".format(item))
        for filename in ruleset[item]:
            logger.log("\t{0}".format(filename))
            src_file = "{0}/{1}".format(src, filename)
            dst_file = "{0}/{1}".format(dst, filename)
            dst_backup = "{0}/{1}".format(backup, filename)
            if os.path.exists(dst_file):
                copy(dst_file, dst_backup)
            copy(src_file, dst_file)

def restore_backups():
    for src in [update_backups_rules, update_backups_decoders, update_backups_rootchecks]:
        type_item = src.split('/')[-1]
        if type_item == 'rules':
            dst = ossec_rules
        elif type_item == 'decoders':
            dst = ossec_decoders
        elif type_item == 'rootchecks':
            dst = ossec_rootchecks

        backups_items = os.listdir(src)

        if backups_items:
            logger.log("\t{0}:".format(type_item))
        else:
            logger.log("\t{0}: Empty".format(type_item))

        for backup_item in backups_items:
            logger.log("\t\t{0}".format(backup_item))
            src_file = "{0}/{1}".format(src, backup_item)
            dst_file = "{0}/{1}".format(dst, backup_item)
            copy(src_file, dst_file)

def main():
    status = {'restart_required': False, 'restarted': False, 'success': False, 'msg': ""}

    # Previous checks
    is_wazuh, ossec_version = get_ossec_version()

    if not is_wazuh:
        exit(2, "OSSEC {0} detected. This script only supports Wazuh v2.0 or newer.".format(ossec_version))
    else:
        if float(ossec_version[2:5]) < 2.0:
            exit(2, "Wazuh {0} detected. This script only supports Wazuh v2.0 or newer.".format(ossec_version))

    # Main
    logger.log("### Wazuh ruleset ###")

    if arguments['backups']:
        logger.log("Restoring ruleset backup:")
        restore_backups()
        status['restart_required'] = True
        status['msg'] = "\nBackup restored successfully"
    else:
        # Get ruleset
        status['old_version'] = get_ruleset_version()
        status['new_version'] = get_new_ruleset(arguments['source'])
        ruleset_to_update, status['restart_required'] = get_ruleset_to_update(arguments['force'])

        # Update
        if not ruleset_to_update['rules'] and not ruleset_to_update['decoders'] and not ruleset_to_update['rootchecks']:
            status['msg'] = "\nYou already have the latest version of ruleset."
        else:
            upgrade_ruleset(ruleset_to_update)
            status['msg'] = "\nRuleset {0} updated to {1} successfully".format(status['old_version'], status['new_version'])

        rm(update_downloads)

    # Restart & messages
    if status['restart_required']:
        logger.log("\nOSSEC requires a restart to apply changes.")

        ans_restart = 'n'
        if arguments['restart'] == 'ask':
            ans_restart = get_stdin("Do you want to restart OSSEC now? [y/N]: ")
        else:
            ans_restart = 'n' if not arguments['restart'] else 'y'

        if ans_restart == "y" or ans_restart == "Y":
            no_output = ""
            if arguments['json']:
                no_output = " > /dev/null 2>&1"

            ret = 0
            ret = os.system("{0}/bin/ossec-control restart{1}".format(ossec_path, no_output))
            if ret != 0:
                exit(2, "OSSEC restart failed")
            status['restarted'] = True
        else:
            logger.log("Do not forget to restart OSSEC to apply changes.")

    logger.log(status['msg'])

    status['success'] = True

    if arguments['json']:
        print(dumps({'error': 0, 'data': status}))

def usage():
    msg = """
    Update ruleset v3.0.0
    Github repository: https://github.com/wazuh/wazuh-ruleset
    Full documentation: http://documentation.wazuh.com/en/latest/wazuh_ruleset.html

    Usage: ./update_ruleset.py                  # Update Decoders, Rules and Rootchecks
           ./update_ruleset.py -b               # Restore last backup

    Restart:
    \t-r, --restart       Restart OSSEC when required.
    \t-R, --no-restart    Do not restart OSSEC when required.

    Backups:
    \t-b , --backups      Restore last backup.

    Additional Params:
    \t-f, --force-update  Force to update the ruleset. By default, only it is updated the new/changed decoders/rules/rootchecks.
    \t-o, --ossec-path    Set OSSEC path. Default: '/var/ossec'
    \t-s, --source        Select ruleset source path (instead of download it).
    \t-j, --json          JSON output. It should be used with '-s' or '-S' argument.
    \t-d, --debug         Debug mode.
    """
    print(msg)

if __name__ == "__main__":

    if os.geteuid() != 0:
        print("You need root privileges to run this script. Please try again, using 'sudo'. Exiting.")
        sys.exit(1)

    file_permissions = 0o640
    file_permissions_x = 0o740
    try:
        root_uid = getpwnam("root").pw_uid
        ossec_gid = getgrnam("ossec").gr_gid
    except:
        sys.exit(1)

    # Arguments
    arguments = {'ossec_path': '/var/ossec', 'source': 'download', 'restart': 'ask', 'backups': False, 'force': False, 'debug': False, 'json': False}
    restart_args = 0

    try:
        opts, args = getopt(sys.argv[1:], "s:o:brRfdjh", ["backups", "source=", "ossec_path=", "restart", "no-restart", "force-update", "debug", "json", "help"])
        if len(opts) > 6:
            print("Incorrect number of arguments.\nTry './update_ruleset.py --help' for more information.")
            sys.exit(1)
    except GetoptError as err:
        print(str(err) + "\n" + "Try './update_ruleset.py --help' for more information.")
        sys.exit(1)

    for o, a in opts:
        if o in ("-b", "--backups"):
            arguments['backups'] = True
        elif o in ("-s", "--source"):
            arguments['source'] = a
        elif o in ("-o", "--ossec-path"):
            arguments['ossec_path'] = a
            if not os.path.exists(arguments['ossec_path']):
                print("ERROR: '{0}' does not exist.".format(arguments['ossec_path']))
                sys.exit(1)
        elif o in ("-r", "--restart"):
            arguments['restart'] = True
            restart_args += 1
        elif o in ("-R", "--no-restart"):
            arguments['restart'] = False
            restart_args += 1
        elif o in ("-f", "--force-update"):
            arguments['force'] = True
        elif o in ("-d", "--debug"):
            arguments['debug'] = True
        elif o in ("-j", "--json"):
            arguments['json'] = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit(0)
        else:
            usage()
            sys.exit(1)

    if restart_args > 1:
        print("Bad arguments combination.\nTry './update_ruleset.py --help' for more information.")
        sys.exit(1)

    # Capture Cntrl + C
    signal(SIGINT, signal_handler)

    ossec_path = arguments['ossec_path']
    ossec_ruleset_log = "{0}/logs/ruleset.log".format(ossec_path)
    ossec_conf = "{0}/etc/ossec.conf".format(ossec_path)
    ossec_rootchecks = "{0}/etc/shared".format(ossec_path)
    ossec_update_script = "{0}/bin/update_ruleset.py".format(ossec_path)
    ossec_ruleset = "{0}/ruleset".format(ossec_path)
    ossec_rules = "{0}/rules".format(ossec_ruleset)
    ossec_decoders = "{0}/decoders".format(ossec_ruleset)
    ossec_ruleset_version_path = "{0}/VERSION".format(ossec_ruleset)

    update_downloads = "{0}/tmp/ruleset/downloads".format(ossec_path)
    update_ruleset = "{0}/wazuh-ruleset".format(update_downloads)
    update_rules = "{0}/rules".format(update_ruleset)
    update_decoders = "{0}/decoders".format(update_ruleset)
    update_rootchecks = "{0}/rootchecks".format(update_ruleset)
    update_backups = "{0}/tmp/ruleset/backups".format(ossec_path)
    update_backups_decoders = "{0}/decoders".format(update_backups)
    update_backups_rules = "{0}/rules".format(update_backups)
    update_backups_rootchecks = "{0}/rootchecks".format(update_backups)

    if arguments['json']:
        logger = RulesetLogger(tag="Wazuh-Ruleset", filename=ossec_ruleset_log, flag=RulesetLogger.O_FILE, debug=arguments['debug'])
    else:
        logger = RulesetLogger(tag="Wazuh-Ruleset", filename=ossec_ruleset_log, flag=RulesetLogger.O_ALL, debug=arguments['debug'])

    logger.debug("Arguments: {0}".format(arguments))

    try:
        main()
    except Exception as e:
        exit(2, "Unkown: {0}.\nExiting.".format(e))


# torch
import torch
import torch.distributed as dist
from torch.nn.utils import parameters_to_vector, vector_to_parameters
from torch import from_numpy

# python lib
import numpy as np
import random, gym
from copy import deepcopy
# from file
from src.util_actor import generateTransitionParallel, computePrioritiesParallel
from src.numba.util_actor import selectActionBatch
from src.util import action_type
from src.EnvSet import EnvSet

# Quality of life
from src.nn.torch.NN import NN_11, NN_17

def actor(rank, world_size, args):
    """ An actor that performs actions in an environment.

    Params
    ======
    rank:       (int) rank of the actor in a multiprocessing setting.
    world_size: (int) total number of actors and learners.
    args:       (dict) training specific parameters
    {
        train_steps:                (int) number of training steps
        , max_actions_per_episode:  (int) number of actions before
                                    the episode is cut short
        , update_policy:            (int) (depricated) number of
                                    steps until updating policy
        , size_local_memory_buffer: (int) size of the local replay buffer
        , min_qubit_errors:         (int) minumum number of qbit
                                    errors on the toric code
        , model:                    (Class torch.nn) model to make predictions
        , model_config:             (dict)
        {
            system_size:        (int) size of the toric grid.
            , number_of_actions (int)
        }
        , env:                      (String) environment to act in
        , env_config                (dict)
        {
            size:               (int)
            , min_qbit_errors   (int)
            , p_error           (float)
        }
        , device:                   (String) {"cpu", "cuda"} device to
                                    operate whenever possible
        , epsilon:                  (float) probability of selecting a
                                    random action
        , n_step:                   (int) n-step learning
        , con_learner:              (multiprocessing.Connection) connection
                                    where new weights are received and termination
        , transition_queue:         (multiprocessing.Queue) SimpleQueue where
                                    transitions are sent to replay buffer
    }

    """
    # queues
    con_learner = args["con_learner"]
    transition_queue_to_memory = args["transition_queue_to_memory"]

    no_envs         = args["no_envs"]
    device          = args["device"]
    discount_factor = args["discount_factor"]
    epsilon         = np.array(args["epsilon"])

    # env and env params
    env = gym.make(args["env"], config=args["env_config"])
    envs = EnvSet(env, no_envs)

    size = env.system_size

    transition_type = np.dtype([('perspective', (np.int, (2,size,size))),
                                ('action', action_type),
                                ('reward', np.float),
                                ('next_perspective', (np.int, (2,size,size))),
                                ('terminal',np.bool)])

    no_actions = int(env.action_space.high[-1])
    grid_shift = int(size/2)

    # startup
    state = envs.resetAll()
    steps_per_episode = np.zeros(no_envs)

    # Local buffer of fixed size to store transitions before sending.
    size_local_memory_buffer    = args["size_local_memory_buffer"] + 1
    local_buffer_T              = np.empty((no_envs, size_local_memory_buffer), dtype=transition_type)  # Transtions
    local_buffer_A              = np.empty((no_envs, size_local_memory_buffer, 4), dtype=np.int)        # A values
    local_buffer_Q              = np.empty((no_envs, size_local_memory_buffer), dtype=(np.float, 3))    # Q values
    local_buffer_R              = np.empty((no_envs, size_local_memory_buffer))                         # R values
    buffer_idx                  = 0

    # set network to eval mode
    NN = args["model"]
    if NN == NN_11 or NN == NN_17:
        NN_config = args["model_config"]
        model = NN(NN_config["system_size"], NN_config["number_of_actions"], args["device"])
    else:
        model = NN()

    # Get initial network params
    weights = None
    while weights == None:
        msg, weights = con_learner.recv() # blocking op
        if msg != "weights":
            weights = None

    # load weights
    vector_to_parameters(weights, model.parameters())

    model.to(device)
    model.eval()

    # main loop over training steps
    while True:

        if con_learner.poll():
            msg, weights = con_learner.recv()

            if msg == "weights":
                vector_to_parameters(weights, model.parameters())

            elif msg == "prep_terminate":
                con_learner.send("ok")
                break

        steps_per_episode += 1

        # select action using epsilon greedy policy
        action, q_values = selectActionBatch(number_of_actions=no_actions,
                                                epsilon=epsilon,
                                                grid_shift=grid_shift,
                                                toric_size = size,
                                                state = state,
                                                model = model,
                                                device = device)
        next_state, reward, terminal_state, _ = envs.step(action)

        transition = generateTransitionParallel(action,
                                                reward,
                                                state,
                                                next_state,
                                                terminal_state,
                                                grid_shift,
                                                transition_type)

        local_buffer_T[:, buffer_idx] = transition
        local_buffer_A[:, buffer_idx] = action
        local_buffer_Q[:, buffer_idx] = q_values
        local_buffer_R[:, buffer_idx] = reward
        buffer_idx += 1

        # If buffer full, send transitions
        if buffer_idx >= size_local_memory_buffer:
            priorities = computePrioritiesParallel(local_buffer_A[:,:-1],
                                                   local_buffer_R[:,:-1],
                                                   local_buffer_Q[:,:-1],
                                                   np.roll(local_buffer_Q, -1, axis=1)[:,:-1],
                                                   discount_factor)

            to_send = [*zip(local_buffer_T[:,:-1].flatten(), priorities.flatten())]

            # send buffer to learner
            transition_queue_to_memory.put(to_send)
            buffer_idx = 0

        too_many_steps = steps_per_episode > args["max_actions_per_episode"]
        if np.any(terminal_state) or np.any(too_many_steps):

            # Reset terminal envs
            idx = np.argwhere(np.logical_or(terminal_state, too_many_steps)).flatten()
            reset_states = envs.resetTerminalEnvs(idx)

            # Reset n_step buffers
            next_state[idx]        = reset_states
            steps_per_episode[idx] = 0

        state = next_state

    # ready to terminate
    while True:
        msg, _ = con_learner.recv()
        if msg == "terminate":
            transition_queue_to_memory.close()
            con_learner.send("ok")
            break

# def actor(rank, world_size, args):
#     """ An actor that performs actions in an environment.

#     Params
#     ======
#     rank:       (int) rank of the actor in a multiprocessing setting.
#     world_size: (int) total number of actors and learners.
#     args:       (dict) training specific parameters
#     {
#         train_steps:                (int) number of training steps
#         , max_actions_per_episode:  (int) number of actions before
#                                     the episode is cut short
#         , update_policy:            (int) (depricated) number of
#                                     steps until updating policy
#         , size_local_memory_buffer: (int) size of the local replay buffer
#         , min_qubit_errors:         (int) minumum number of qbit
#                                     errors on the toric code
#         , model:                    (Class torch.nn) model to make predictions
#         , model_config:             (dict)
#         {
#             system_size:        (int) size of the toric grid.
#             , number_of_actions (int)
#         }
#         , env:                      (String) environment to act in
#         , env_config                (dict)
#         {
#             size:               (int)
#             , min_qbit_errors   (int)
#             , p_error           (float)
#         }
#         , device:                   (String) {"cpu", "cuda"} device to
#                                     operate whenever possible
#         , epsilon:                  (float) probability of selecting a
#                                     random action
#         , beta:                     (float) parameter to determin the
#                                     level of compensation for bias when
#                                     computing priorities
#         , n_step:                   (int) n-step learning
#         , con_learner:              (multiprocessing.Connection) connection
#                                     where new weights are received and termination
#         , transition_queue:         (multiprocessing.Queue) SimpleQueue where
#                                     transitions are sent to replay buffer
#     }

#     """

#     # queues
#     con_learner = args["con_learner"]
#     transition_queue_to_memory = args["transition_queue_to_memory"]

#     device = args["device"]

#     discount_factor = args["discount_factor"]

#     # local buffer of fixed size to store transitions before sending
#     n_step                      = args["n_step"]
#     size_local_memory_buffer    = args["size_local_memory_buffer"] + n_step
#     local_buffer_T              = [None] * size_local_memory_buffer # Transtions
#     local_buffer_Q              = [None] * size_local_memory_buffer # Q values
#     buffer_idx                  = 0
#     n_step_S                    = [None] * n_step # State
#     n_step_A                    = [None] * n_step # Actions
#     n_step_Q                    = [None] * n_step # Q values
#     n_step_R                    = [0   ] * n_step # Rewards
#     n_step_idx                  = 0 # index

#     # set network to eval mode
#     NN = args["model"]
#     if NN == NN_11 or NN == NN_17:
#         NN_config = args["model_config"]
#         model = NN(NN_config["system_size"], NN_config["number_of_actions"], args["device"])
#     else:
#         model = NN()

#     model.to(device)
#     model.eval()

#     # env and env params
#     env = gym.make(args["env"], config=args["env_config"])

#     no_actions = int(env.action_space.high[-1])
#     grid_shift = int(env.system_size/2)

#     # Get initial network params
#     weights = None
#     while weights == None:
#         msg, weights = con_learner.recv() # blocking op
#         if msg != "weights":
#             weights = None

#     # load weights
#     vector_to_parameters(weights, model.parameters())

#     # init counters
#     steps_counter = 0
#     update_counter = 1
#     local_memory_index = 0

#     # startup
#     state = env.reset()
#     steps_per_episode = 0

#     # main loop over training steps
#     while True:

#         if con_learner.poll():
#             msg, weights = con_learner.recv()

#             if msg == "weights":
#                 vector_to_parameters(weights, model.parameters())

#             elif msg == "prep_terminate":
#                 con_learner.send("ok")
#                 break

#         steps_per_episode += 1
#         # select action using epsilon greedy policy
#         action, q_values = selectAction(number_of_actions=no_actions,
#                                         epsilon=args["epsilon"],
#                                         grid_shift=grid_shift,
#                                         toric_size = env.system_size,
#                                         state = state,
#                                         model = model,
#                                         device = device)

#         next_state, reward, terminal_state, _ = env.step(action)

#         n_step_A[n_step_idx] = action
#         n_step_S[n_step_idx] = state # Keep in mind that next state is not saved until next iteration
#         n_step_Q[n_step_idx] = q_values
#         n_step_R[n_step_idx] = 0
#         n_step_R = updateRewards(n_step_R, n_step_idx, reward, n_step, discount_factor) # Remember to 0 as well!

#         if not (None in n_step_A):
#             transition = generateTransition(n_step_A[n_step_idx-n_step],
#                                             n_step_R[n_step_idx-n_step],
#                                             grid_shift,
#                                             n_step_S[n_step_idx-n_step],
#                                             next_state,
#                                             terminal_state
#                                             )
#             local_buffer_T[buffer_idx] = transition
#             local_buffer_Q[buffer_idx] = n_step_Q[n_step_idx-n_step]
#             buffer_idx += 1

#         n_step_idx = (n_step_idx+1) % n_step

#         if buffer_idx >= size_local_memory_buffer:

#             # disregard latest transition since it has no next state to compute priority for
#             priorities = computePriorities( local_buffer_T[:-n_step],
#                                             local_buffer_Q[:-n_step],
#                                             np.roll(local_buffer_Q, -n_step, axis=0)[:-n_step],
#                                             discount_factor**n_step)

#             to_send = [*zip(local_buffer_T[:-n_step], priorities)]

#             # send buffer to learner
#             transition_queue_to_memory.put(to_send)
#             buffer_idx = 0

#         if terminal_state or steps_per_episode > args["max_actions_per_episode"]:
#             # Reset n_step buffers
#             n_step_S        = [None] * n_step
#             n_step_A        = [None] * n_step
#             n_step_R        = [0   ] * n_step

#             # reset env
#             state = env.reset()
#             steps_per_episode = 0
#         else:
#             state = next_state

#     # ready to terminate
#     while True:
#         msg, _ = con_learner.recv()
#         if msg == "terminate":
#             transition_queue_to_memory.close()
#             con_learner.send("ok")
#             break


# import re
# import cloudinary
# import cloudinary.uploader
# import cloudinary.api
from flask import Flask, render_template, request, jsonify
import numpy as np
from test import Preprocess
from melodygenerator import MelodyGenerator
from flask import Flask, send_from_directory

app = Flask(__name__)
app.url_map.strict_slashes = False

@app.route("/upload", methods=["GET", "POST"])
def upload_file():
    #   cloudinary.config(cloud_name = os.getenv('CLOUD_NAME'), api_key=os.getenv('API_KEY'),
    #     api_secret=os.getenv('API_SECRET'))
    upload_result = None
    if request.method == "GET":
        return render_template('index.html')

    if request.method == "POST":
        print('-----')
        file_to_upload = request.files['file']
        file_to_upload.save(f'{file_to_upload.filename}')
        app.logger.info('%s file_to_upload', file_to_upload)
        mg = MelodyGenerator()
        obj = Preprocess()
        seed = obj.getString(file_to_upload.filename)
        print(f' in class -> {seed}')
        #seed2 = "67 _ 67 _ 67 _ _ 65 64 _ 64 _ 64 _ _"
        #seed = "67 _ _ _ _ _ 65 _ 64 _ 62 _ 60 _ _ _ 67 _ 67 _ 67 _ _ 65 64 _ 64 _ 64 _ _ 67 _ _ _ _ _ 65 _ 64 _ 62 _ 60 _ _ _ 67 _ 67 _ 67 _ _ 65 64 _ 64 _ 64 _ _"
        melody = mg.generate_melody(seed, 500, 64, 0.3)
        mg.save_melody(melody, file_name='midd.mid')
        print(type(mg))
        return send_from_directory('', path='midd.mid', as_attachment=True)
        # if file_to_upload:
        #     #   upload_result = cloudinary.uploader.upload(file_to_upload)
        #     # app.logger.info(upload_result)
        #     upload_result = cloudinary.uploader.upload(file_to_upload)
        #     return jsonify(True)

# ===============================================================================================
if __name__ == '__main__':
    app.run(debug=True)


'''
4. Utilizar los valores booleanos de cada lista (penúltimo campo) para mostrar en pantalla el nombre del
auto correspondiente, en caso de que este valor sea True.
La solución debe estar dentro del programa listas_cuatro.py
'''

from velocidad import promedio

auto1 = ['Mazda RX4', 21.0, 6, False, 4]
auto2 = ['Merc 240D', 24.4, 4, True, 2]
auto3 = ['Merc 280', 19.2, 6, True, 4]
auto4 = ['Valiant', 18.1, 6, True, 1]
auto5 = ['Merc 450SL', 17.3, 8, False, 3]
auto6 = ['Toyota Corolla', 33.9, 4, True, 1]

lista_autos = [auto1,auto2,auto3,auto4,auto5,auto6]

for autos in lista_autos:
	if autos[-2] is True:
		print(autos[0])