import os
import math
import json
import librosa

from settings import (
    SAMPLE_RATE,
    NUM_MFCC,
    N_FTT,
    HOP_LENGTH,
    NUM_SEGMENTS,
    DURATION,
)

DATASET_PATH = "data\\archive\\Data\\genres_original" # loaded using the GTZAN Music Genre Classification dataset at https://www.kaggle.com/datasets/andradaolteanu/gtzan-dataset-music-genre-classification
JSON_PATH = "data\\data.json"

SAMPLES_PER_TRACK = SAMPLE_RATE * DURATION

def dump_mfccs_to_json(dataset_path=None):
    """
    Processes test data as MFCCs and labels
    """
    dataset_path = dataset_path if dataset_path is not None else DATASET_PATH
    data = {
        "mapping": [],
        "mfcc": [],
        "labels" : [],
    }
    samples_per_segment = int(SAMPLES_PER_TRACK/NUM_SEGMENTS)
    expected_mfcc = math.ceil(samples_per_segment/HOP_LENGTH)
    for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
        if dirpath is not dataset_path:
            dirpath_components = dirpath.split("\\")
            label = dirpath_components[-1]
            data["mapping"].append(label)
            print(f"Processing: {label}")

            for f in filenames:
                file_path = os.path.join(dirpath, f)
                signal, sr = librosa.load(file_path, sr=SAMPLE_RATE)

                for s in range(NUM_SEGMENTS):
                    start_sample = samples_per_segment * s
                    finish_sample = start_sample + samples_per_segment
                    mfcc = librosa.feature.mfcc(signal[start_sample:finish_sample], sr=sr, n_fft=N_FTT, n_mfcc=NUM_MFCC, hop_length=HOP_LENGTH)
                    mfcc = mfcc.T
                    if len(mfcc) == expected_mfcc:
                        data["mfcc"].append(mfcc.tolist())
                        data["labels"].append(i-1)
                        print(f"{file_path}, segment:{s+1}")

    with open(JSON_PATH, "w") as fp:
        json.dump(data, fp, indent=4)

if __name__ == "__main__":
    dump_mfccs_to_json()


from fastapi import Body, FastAPI
from pydantic import BaseModel
from typing import Annotated

from enum import Enum

app = FastAPI()

class ModelName(str, Enum):
    afs = "afs"
    har = "har1"

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()

fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]

@app.post("/items/create_item/")
async def create_items(item: Item):
    item_dict = item.model_dump()
    if item.tax:
        price_with_tax = item.price + item.tax
        item_dict.update({"price with tax": price_with_tax})
    return item_dict

@app.get("/")
async def home():
    return {"Data": "Test"}

@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
    return fake_items_db[skip: skip + limit]

@app.put("/add_items/{item_id}")
async def add_item(item_id: int, item: Item):
    return {"item_id": item_id, **item.model_dump()}

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Annotated[Item, Body(examples={"name": "foo", "description": "cool item", "price": "24", "tax": 3})]):
    result = {"item_id": item_id, "item": item}
    return result

@app.get("/models/{model_name}")
async def get_model(model_name: ModelName):
    if model_name is ModelName.afs:
        return {"model_name": model_name, "message": 1}
    if model_name.value == "har":
        return {"model_name": model_name, "message": 2}
    return {"model_name": model_name, "message": -1}

@app.get("/files/{file_path:path}")
async def read_file(file_path: str):
    return {"file_path": file_path}


from urllib2 import urlopen, HTTPError
from django.template.defaultfilters import slugify
from django.core.files.base import ContentFile
from django.db import transaction, IntegrityError

from item.models import Item, Link
from movie.models import Movie, Actor, Director, Genre
from decorators.retry import retry

class LoadMovie():
    """
    This manager inserts a movie into the database along with its
    corresponding genres, actors, and directors.
    """
    exists = False

    def __init__(self, title, imdb_id, runtime,
                 synopsis, theater_date, keywords):
        """
        Inserts the movie into the database if it doesn't already
        exist in the database.
        """
        try:
            self.movie, self.created = Movie.objects.get_or_create(
                title=title,
                imdb_id=imdb_id,
                runtime=runtime,
                synopsis=synopsis,
                theater_date=theater_date,
                keywords = keywords,
                url=slugify(title)
            )
        except IntegrityError:
            print('TRANSACTION FAILED ON MOVIE INSERT: Rolling back now...')
            transaction.rollback()

    def insert_genres(self, genres):
        """
        Inserts the genres for the movie.
        """
        genre_list = []
        try:
            for g in genres:
                genre, created = Genre.objects.get_or_create(
                    name=g, url=slugify(g))
                genre_list.append(genre)
            self.movie.genre.add(*genre_list)
        except IntegrityError:
            print('TRANSACTION FAILED ON GENRE INSERT: Rolling back now...')
            transaction.rollback()

    def insert_actors(self, actors):
        """
        Inserts the actors for the movie.
        """
        actor_list = []
        try:
            for a in actors:
                actor, created = Actor.objects.get_or_create(
                    name=a, url=slugify(a))
                actor_list.append(actor)
            self.movie.actors.add(*actor_list)
        except IntegrityError:
            print('TRANSACTION FAILED ON ACTOR INSERT: Rolling back now...')
            transaction.rollback()

    def insert_directors(self, directors):
        """
        Inserts the directors for the movie.
        """
        director_list = []
        try:
            for d in directors:
                director, created = Director.objects.get_or_create(
                    name=d, url=slugify(d))
                director_list.append(director)
            self.movie.directors.add(*director_list)
        except IntegrityError:
            print('TRANSACTION FAILED ON DIRECTOR INSERT: Rolling back now...')
            transaction.rollback()

    @retry(HTTPError)
    def insert_image(self, url):
        """
        Inserts the image for the movie.
        """
        try:
            if 'default.jpg' in self.movie.image.url or self.created:
                image = urlopen(url, timeout=15)
                self.movie.image.save(
                    self.movie.url+u'.jpg',
                    ContentFile(image.read())
                )
        except IntegrityError:
            print('TRANSACTION FAILED ON IMAGE INSERT: Rolling back now...')
            transaction.rollback()

    def insert_trailer(self, url):
        """
        Inserts the trailer as a link.
        """
        try:
            Link.objects.get_or_create(
                item=self.movie.item,
                partner="YouTube",
                url=url
            )
        except IntegrityError:
            print('TRANSACTION FAILED ON TRAILER INSERT: Rolling back now...')
            transaction.rollback()


# -*- coding: utf-8 -*-
"""
Created on Thu Jul  1 10:10:45 2021

@author: 82106
"""

import cv2
import os
import sys

if not os.path.exists('result'):
    os.makedirs('result')

capture = cv2.VideoCapture(1)

if not capture.isOpened():
    print('Camera open failed!')
    sys.exit()

'''
frameWidth = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(capture.get(cv2.CAP_PROP_FRMAE_HEIGHT))
frameSize = (frameWidth, frameHeight)
print('frame size : {}'.format(frameSize))
'''

capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

count = 1

while True:
    ret, frame = capture.read()

    if not ret:
        print('Frame read error!')
        sys.exit()

    cv2.imshow('frame', frame)

    key = cv2.waitKey(1)
    if key == ord('s'):
        print('Screenshot saved!')
        cv2.imwrite('result/screenshot{}.png'.format(count), frame, params=[cv2.IMWRITE_PNG_COMPRESSION, 0])
        count += 1

    elif key == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()


#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@AUTHOR:Joselyn Zhao
@CONTACT:zhaojing17@foxmail.com
@HOME_PAGE:joselynzhao.top
@SOFTWERE:PyCharm
@FILE:main.py
@TIME:2019/6/13 10:32
@DES:
'''

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
old_v = tf.logging.get_verbosity()
tf.logging.set_verbosity(tf.logging.ERROR)
from tensorflow.examples.tutorials.mnist import input_data
from Lenet import  *
from PIL import Image

mnist = input_data.read_data_sets('../../../data/mnist', one_hot=True)
x_test = np.reshape(mnist.test.images, [-1, 28, 28, 1])
x_test = np.pad(x_test, ((0, 0), (2, 2), (2, 2), (0, 0)),
                'constant')  # print("Updated Image Shape: {}".format(X_train[0].shape))
tf.logging.set_verbosity(old_v)

iteratons = 1000
batch_size = 64
ma = 0
sigma = 0.1
lr = 0.01

def get_sample100(label):
    sample100_x=[]
    sample100_y=[]
    count = 0
    for i in range(len(mnist.test.images)):
        if mnist.test.labels[i][label]==1:
            count+=1
            sample100_y.append(mnist.test.labels[i])
            sample100_x.append(mnist.test.images[i])
        if count>=100:
            break
    return sample100_x,sample100_y

def train_lenet(lenet):
    with tf.Session() as sess:  #这个session需要关闭么？
        sess.run(tf.global_variables_initializer())

        tf.summary.image("input",lenet.x,3)
        merged_summary = tf.summary.merge_all()

        writer = tf.summary.FileWriter("LOGDIR/4/",sess.graph)  # 保存到不同的路径下
        # writer.add_graph(sess.graph)
        for ii in range(iteratons):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            batch_xs = np.reshape(batch_xs,[-1,28,28,1])
            batch_xs = np.pad(batch_xs,((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
            sess.run(lenet.train_step,feed_dict ={lenet.x:batch_xs,lenet.y_:batch_ys})
            if ii % 50 == 1:
                acc,s = sess.run([lenet.accuracy,merged_summary],feed_dict ={lenet.x:x_test,lenet.y_:mnist.test.labels})
                writer.add_summary(s,ii)
                print("%5d: accuracy is: %4f" % (ii, acc))
        sample100_x,sample100_y = get_sample100(4) #随便选了一个label 输入0-9的值
        sample100_x = np.reshape(sample100_x,[-1,28,28,1])
        sample100_x = np.pad(sample100_x, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')
        x_min = tf.reduce_min(lenet.fc2)
        x_max = tf.reduce_max(lenet.fc2)
        fc2 = (lenet.fc2 - x_min) / (x_max - x_min)
        fc2 = sess.run(fc2,feed_dict={lenet.x:sample100_x,lenet.y_:sample100_y})
        plt.imshow(fc2)
        plt.show()

        print('[accuracy,loss]:', sess.run([lenet.accuracy], feed_dict={lenet.x:x_test,lenet.y_:mnist.test.labels}))

if __name__ =="__main__":
    act = "sigmoid"
    lenet = Lenet(ma,sigma,lr,act)
    train_lenet(lenet)


import pytesseract
import PIL
from os import system
import re
system("tesseract -l")

class workout:
    reps = 0
    exercise_name = ""

def compile_text_to_workouts(text):
    workouts = []
    num = 0

    for word in text:
        new_workout = workout()
        if word.isdigit():
            new_workout.reps = word
            num+=1
        while num < len(text) and not text[num].isdigit() :
            new_workout.exercise_name +=  " " + str(text[num])
            num +=1
        if not new_workout.reps == 0 or not new_workout.exercise_name == "":
            workouts.append(new_workout)

    return workouts

####MAIN:###############################################################

letters = (pytesseract.image_to_string(r'../GetFit/workout_routine1.png'))
print(letters)
sentence = re.findall(r'\w+', letters) ##turns letters into words and makes list
print(sentence)
compile_text_to_workouts(sentence) ###turns into actual workout routine


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

import xml.etree.ElementTree as ET
from xml.etree import ElementTree as etree
from xml.dom import minidom
import untangle

def xml_generator(input_filename, input_foldername, exif_list, root_path):
    root = ET.Element('annotation')

    source = ET.SubElement(root, 'source')
    image_date = ET.SubElement(source, 'date')
    image_date.text = str(exif_list[0])
    folder_name = ET.SubElement(source, 'folder')
    folder_name.text = input_foldername
    file_name = ET.SubElement(source, 'filename')
    file_name.text = input_filename

    gpsinfo = ET.SubElement(root, 'gpsinfo')
    gps_altitude = ET.SubElement(gpsinfo, 'GPSAltitude')
    gps_altitude.text = str(exif_list[1])
    gps_latitude = ET.SubElement(gpsinfo, 'GPSLatitude')
    gps_latitude.text = str(exif_list[2])
    gps_latitude_ref = ET.SubElement(gpsinfo, 'GPSLatitudeRef')
    gps_latitude_ref.text = str(exif_list[3])
    gps_longitude = ET.SubElement(gpsinfo, 'GPSLongitude')
    gps_longitude.text = str(exif_list[4])
    gps_longitude_ref = ET.SubElement(gpsinfo, 'GPSLongitudeRef')
    gps_longitude_ref.text = str(exif_list[5])

    '''
    There should be position annotation inside 'object' tag
    '''
    #ann_obj = ET.SubElement(root, 'object')

    xml_string = etree.tostring(root)
    tree = minidom.parseString(xml_string)
    xml_string = tree.toxml()

    save_path = '%s/ob_%s/%s.xml' % (root_path, input_foldername, input_filename[:-4])

    f=open(save_path,'wb')
    f.write(tree.toprettyxml(encoding='utf-8'))
    f.close()

def xml_parsing(input_xml_file):
    obj = untangle.parse(input_xml_file)

    date_time = obj.annotation.source.date.cdata
    GPSAltitude = obj.annotation.gpsinfo.GPSAltitude.cdata
    GPSLatitude = obj.annotation.gpsinfo.GPSLatitude.cdata
    GPSLatitudeRef = obj.annotation.gpsinfo.GPSLatitudeRef.cdata
    GPSLongitude = obj.annotation.gpsinfo.GPSLongitude.cdata
    GPSLongitudeRef = obj.annotation.gpsinfo.GPSLongitudeRef.cdata

    xml_info_keys = ['Date', 'GPSAltitude', 'GPSLatitude', 'GPSLatitudeRef', 'GPSLongitude', 'GPSLongitudeRef']
    xml_info_value = [date_time, GPSAltitude, GPSLatitude, GPSLatitudeRef, GPSLongitude, GPSLongitudeRef]
    xml_info_dict = dict(zip(xml_info_keys, xml_info_value))
    return xml_info_dict

#im = '/Users/xiang/ml_ann/ann_tools_eric/dataset/ob_curr/00001.xml'
#xml_parsing(im)


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        d={}
        while headA:
            d[headA] = 1
            headA = headA.next

        while headB:
            if headB in d:
                return headB
            headB=headB.next

# the idea is to traverse list A and store the address/reference to each node
# in a hash set. Then check every node bi in list B: if bi appears in the hash set,
# then bi is the intersection node.

# I did not realize that the hash set can be created like this

Linked List/Intersection of Two Linked Lists/solution.py


#for X in range(1,10):
    #print(X)

#for char in "cofee":
        #print(char * 10)

#for num in range (0,20,2):#if you start with odd nums it will print odd(1,20,2)
    #print(num)

#times = input("how many times do i have to tell you? ")
#times = int(times)
#for time in range(times) :
   # print ("clean up your room!")

#for num in range(1,21):
   #if num ==4 or num ==13:
       #print(f"{num} is unlucky")
   #elif num %  2==0:
      #print(f"{num} is even")
  # else:
      #print(f"{num} is odd")

#for num in range(1,21):
   #if num ==4 or num ==13:
       #print(f"{num} is unlucky")
   #elif num %  2==0:
      #print(f"{num} is even")
   #else:
      #state()
      #print(f"{num} is odd")

#while loop
#msg = input("whats your password?")
#while msg != "bananas":
    #print("wrong!")
    #msg = input("whats your password?")
#print("correct!")

#num =1
#while num < 11:
    #print(num)
    #num += 1

#for num in range(1,11) :
    #print(" \U0001f600" * num)

#times = 1
#while times < 11:
    #print("       \U0001f600" * times)
    #times += 1

#msg = input("say something: ")
#while msg != "stop copying me":
    #print(msg)
    #msg = input()
#print("you win!")

while True:
    command = input("type  'exit' to exit:")
    if (command == "exit"):
        break

times = int(input("how many times do i have to tell yah?"))
for time in range(times):
    print("claen your room!")
    if time >= 3:
        print("do you even listen anymore")
        break


from __future__ import annotations

from io import BufferedIOBase, BytesIO
from typing import List, Optional

from helper import (
    byte_to_int,
    encode_varstr,
    hash160,
    int_to_byte,
    int_to_little_endian,
    little_endian_to_int,
    read_varint,
    sha256,
)
from op import (
    decode_num,
    encode_minimal_num,
    is_number_op_code,
    number_to_op_code,
    op_code_to_number,
    OP_0,
    OP_CHECKLOCKTIMEVERIFY,
    OP_CHECKMULTISIG,
    OP_CHECKMULTISIGVERIFY,
    OP_CHECKSEQUENCEVERIFY,
    OP_CHECKSIG,
    OP_CHECKSIGVERIFY,
    OP_DROP,
    OP_DUP,
    OP_EQUAL,
    OP_EQUALVERIFY,
    OP_FROMALTSTACK,
    OP_HASH160,
    OP_IF,
    OP_NOTIF,
    OP_PUSHDATA1,
    OP_PUSHDATA2,
    OP_TOALTSTACK,
    OP_VERIFY,
    OP_CODE_NAMES,
    OP_CODE_FUNCTIONS,
)
from timelock import Locktime, Sequence
from witness import Witness

class Script(list):
    def __add__(self, other: Script) -> Script:
        return self.__class__(super().__add__(other))

    def __radd__(self, other: Script) -> Script:
        o = self.__class__(other)
        return o + self

    def __new__(cls,
                commands: Optional[List[Union(bytes, str)]] = None) -> Script:
        if commands is None:
            commands = []
        for current in commands:
            if type(current) not in (bytes, ):
                raise ValueError(
                    f'Every command should be bytes or str, got {current} instead'
                )
        return super().__new__(cls, commands)

    def __repr__(self) -> str:
        result = ''
        for current in self:
            if OP_CODE_NAMES.get(current):
                result += f'{OP_CODE_NAMES[current]} '
            elif type(current) == str:
                result += f'<{current}> '
            else:
                result += f'{current.hex()} '
        return result

    @classmethod
    def parse(cls, s: BufferedIOBase) -> Script:
        # get the length of the entire field
        length = read_varint(s)
        # initialize the commands array
        commands = []
        # initialize the number of bytes we've read to 0
        count = 0
        # loop until we've read length bytes
        while count < length:
            # get the current byte
            current = s.read(1)
            # increment the bytes we've read
            count += 1
            # convert the current byte to an integer
            current_int = current[0]
            # if the current byte is between 1 and 75 inclusive
            if current_int <= 75:
                # add the next n bytes as a command
                commands.append(s.read(current_int))
                count += current_int
            elif current == OP_PUSHDATA1:
                # op_pushdata1
                data_length = byte_to_int(s.read(1))
                commands.append(s.read(data_length))
                count += data_length + 1
            elif current == OP_PUSHDATA2:
                # op_pushdata2
                data_length = little_endian_to_int(s.read(2))
                commands.append(s.read(data_length))
                count += data_length + 2
            else:
                # add the command to the list of commands
                commands.append(current)
        if count != length:
            raise SyntaxError(f'parsing script failed {commands}')
        return cls(commands)

    def miniscript(self):
        from miniscript import MiniScript
        return MiniScript.from_script(Script(self[:]))

    def is_locktime_locked(self) -> bool:
        '''Returns whether the script starts with
        <locktime> OP_CLTV OP_DROP'''
        return len(self) >= 3 and \
            (is_number_op_code(self[0]) or len(self[0]) > 1) and \
            self[1] == OP_CHECKLOCKTIMEVERIFY and self[2] == OP_DROP

    def is_multisig(self) -> bool:
        '''Returns whether the script follows the
        OP_k <pubkey1>...<pubkeyn> OP_n OP_CHECKMULTISIG pattern'''
        if self[-1] != OP_CHECKMULTISIG:
            return False
        if not is_number_op_code(self[-2]):
            return False
        n = op_code_to_number(self[-2])
        if len(self) < n + 3:
            return False
        for current in self[-n - 2:-2]:
            if len(current) != 33:
                return False
        if not is_number_op_code(self[-n - 3]):
            return False
        k = op_code_to_number(self[-n - 3])
        if k < 1 or k > 15:
            return False
        if n < k or n > 15:
            return False
        return True

    def is_multisig_timelock(self) -> bool:
        '''Returns whether the script follows the
        <locktime> OP_CLTV/OP_CSV OP_DROP OP_k <pubkey1>...<pubkeyn> OP_n OP_CHECKMULTISIG pattern'''
        return (self.is_sequence_locked() or self.is_locktime_locked()) and \
            self.is_multisig()

    def is_p2pkh(self) -> bool:
        '''Returns whether the script follows the
        OP_DUP OP_HASH160 <20 byte hash> OP_EQUALVERIFY OP_CHECKSIG pattern.'''
        # there should be exactly 5 commands
        # OP_DUP, OP_HASH160, 20-byte hash, OP_EQUALVERIFY, OP_CHECKSIG
        return len(self) == 5 and self[0] == OP_DUP and self[1] == OP_HASH160 \
            and len(self[2]) == 20 and self[3] == OP_EQUALVERIFY \
            and self[4] == OP_CHECKSIG

    def is_p2sh(self) -> bool:
        '''Returns whether the script follows the
        OP_HASH160 <20 byte hash> OP_EQUAL pattern.'''
        # there should be exactly 3 commands
        # OP_HASH160, 20-byte hash, OP_EQUAL
        return len(self) == 3 and self[0] == OP_HASH160 and len(self[1]) == 20 \
            and self[2] == OP_EQUAL

    def is_p2wpkh(self) -> bool:
        '''Returns whether the script follows the
        OP_0 <20 byte hash> pattern.'''
        return len(self) == 2 and self[0] == OP_0 and len(self[1]) == 20

    def is_p2wsh(self) -> bool:
        '''Returns whether the script follows the
        OP_0 <32 byte hash> pattern.'''
        return len(self) == 2 and self[0] == OP_0 and len(self[1]) == 32

    def is_segwit(self) -> bool:
        return self.is_p2wpkh() or self.is_p2wsh()

    def is_sequence_locked(self) -> bool:
        '''Returns whether the script starts with
        <sequence> OP_CSV OP_DROP'''
        return len(self) >= 3 and \
            (is_number_op_code(self[0]) or len(self[0]) > 1) and \
            self[1] == OP_CHECKSEQUENCEVERIFY and self[2] == OP_DROP

    def is_timelock(self) -> bool:
        '''Returns whether the script follows the
        locktime OP_CLTV OP_DROP <pubkey> OP_CHECKSIG pattern'''
        return (self.is_sequence_locked() or self.is_locktime_locked()) and \
            len(self) == 5 and len(self[3]) == 33 and self[4] == OP_CHECKSIG

    def pubkeys(self) -> List[bytes]:
        pubkeys = []
        for item in self:
            if len(item) == 33 and item[0] in (2, 3):
                pubkeys.append(item)
        return pubkeys

    def raw_serialize(self) -> bytes:
        # initialize what we'll send back
        result = b''
        # go through each command
        for current in self:
            if current == OP_0:
                result += int_to_byte(0)
            elif OP_CODE_NAMES.get(current) is None:
                # this is an element
                # get the length in bytes
                length = len(current)
                # for large lengths, we have to use a pushdata op code
                if length < 75:
                    # turn the length into a single byte integer
                    result += int_to_byte(length)
                elif length > 75 and length < 0x100:
                    # 76 is pushdata1
                    result += OP_PUSHDATA1
                    result += int_to_byte(length)
                elif length >= 0x100 and length <= 520:
                    # 77 is pushdata2
                    result += OP_PUSHDATA2
                    result += int_to_little_endian(length, 2)
                else:
                    raise ValueError('too long a command')
            result += current
        return result

    def serialize(self) -> bytes:
        return encode_varstr(self.raw_serialize())

class ScriptPubKey(Script):
    '''Represents a ScriptPubKey in a transaction'''
    @classmethod
    def parse(cls, s: BufferedIOBase) -> ScriptPubKey:
        script_pubkey = super().parse(s)
        if script_pubkey.is_p2pkh():
            return PKHScriptPubKey.from_hash(script_pubkey[2])
        elif script_pubkey.is_p2sh():
            return SHScriptPubKey.from_hash(script_pubkey[1])
        elif script_pubkey.is_p2wpkh():
            return WPKHScriptPubKey.from_hash(script_pubkey[1])
        elif script_pubkey.is_p2wsh():
            return WSHScriptPubKey.from_hash(script_pubkey[1])
        else:
            return script_pubkey

    def redeem_script(self) -> RedeemScript:
        '''Convert this ScriptPubKey to its RedeemScript equivalent'''
        return RedeemScript(self)

class PKHScriptPubKey(ScriptPubKey):
    @classmethod
    def from_hash(cls, h160: bytes) -> PKHScriptPubKey:
        if len(h160) != 20:
            raise TypeError('h160 should be 20 bytes')
        return cls([OP_DUP, OP_HASH160, h160, OP_EQUALVERIFY, OP_CHECKSIG])

    def hash160(self) -> bytes:
        return self[2]

class SHScriptPubKey(ScriptPubKey):
    @classmethod
    def from_hash(cls, h160: bytes) -> SHScriptPubKey:
        if len(h160) != 20:
            raise TypeError('h160 should be 20 bytes')
        return cls([OP_HASH160, h160, OP_EQUAL])

    def hash160(self) -> bytes:
        return self[1]

class RedeemScript(Script):
    '''Subclass that represents a RedeemScript for p2sh'''
    def hash160(self) -> bytes:
        '''Returns the hash160 of the serialization of the RedeemScript'''
        return hash160(self.raw_serialize())

    def script_pubkey(self) -> SHScriptPubKey:
        '''Returns the ScriptPubKey that this RedeemScript corresponds to'''
        return SHScriptPubKey.from_hash(self.hash160())

class SegwitPubKey(ScriptPubKey):
    def hash(self) -> bytes:
        return self[1]

class WPKHScriptPubKey(SegwitPubKey):
    @classmethod
    def from_hash(cls, h160: bytes) -> WPKHScriptPubKey:
        if len(h160) != 20:
            raise TypeError('h160 should be 20 bytes')
        return cls([OP_0, h160])

class WSHScriptPubKey(SegwitPubKey):
    @classmethod
    def from_hash(cls, s256: bytes) -> WSHScriptPubKey:
        if len(s256) != 32:
            raise TypeError('s256 should be 32 bytes')
        return cls([OP_0, s256])

class WitnessScript(Script):
    '''Subclass that represents a WitnessScript for p2wsh'''
    def redeem_script(self) -> RedeemScript:
        return self.script_pubkey().redeem_script()

    def script_pubkey(self) -> WSHScriptPubKey:
        '''Generates the ScriptPubKey for p2wsh'''
        # get the sha256 of the current script
        # return new p2wsh script using p2wsh_script
        return WSHScriptPubKey.from_hash(self.sha256())

    def sha256(self) -> bytes:
        '''Returns the sha256 of the raw serialization for witness program'''
        return sha256(self.raw_serialize())

class MultiSigScript(Script):
    @classmethod
    def from_pubkeys(cls, k: int, sec_pubkeys: List[bytes]) -> MultiSigScript:
        n = len(sec_pubkeys)
        if k == 0 or k > n:
            raise ValueError(f'cannot do {k} of {n} keys')
        return cls([
            number_to_op_code(k), *sorted(sec_pubkeys),
            number_to_op_code(n), OP_CHECKMULTISIG
        ])

class MultiSigRedeemScript(RedeemScript, MultiSigScript):
    pass

class MultiSigWitnessScript(WitnessScript, MultiSigScript):
    pass

class TimelockScript(Script):
    @classmethod
    def from_time(cls,
                  locktime: Optional[Locktime] = None,
                  sequence: Optional[Sequence] = None) -> List[bytes]:
        if locktime is not None:
            return [
                encode_minimal_num(locktime), OP_CHECKLOCKTIMEVERIFY, OP_DROP
            ]
        elif sequence is not None:
            return [
                encode_minimal_num(sequence), OP_CHECKSEQUENCEVERIFY, OP_DROP
            ]
        else:
            raise ValueError('locktime or sequence required')

class SingleSigTimelockScript(TimelockScript):
    @classmethod
    def from_pubkey_time(
            cls,
            sec: bytes,
            locktime: Optional[Locktime] = None,
            sequence: Optional[Sequence] = None) -> SingleSigTimelockScript:
        script = cls.from_time(locktime, sequence) + [sec, OP_CHECKSIG]
        return cls(script)

class SingleSigTimelockRedeemScript(RedeemScript, SingleSigTimelockScript):
    pass

class SingleSigTimelockWitnessScript(WitnessScript, SingleSigTimelockScript):
    pass

class MultiSigTimelockScript(TimelockScript, MultiSigScript):
    @classmethod
    def from_pubkeys_time(
            cls,
            k: int,
            sec_pubkeys: List[bytes],
            locktime: Optional[Locktime] = None,
            sequence: Optional[Sequence] = None) -> MultiSigTimelockScript:
        script = cls.from_time(locktime, sequence) + cls.from_pubkeys(
            k, sec_pubkeys)
        return cls(script)

class MultiSigTimelockRedeemScript(RedeemScript, MultiSigTimelockScript):
    pass

class MultiSigTimelockWitnessScript(WitnessScript, MultiSigTimelockScript):
    pass


vertices = []

arestas = []

matriz = []

class Grafo:
    def __init__(self, no, noAux, prioridade):

        self.no = no
        self.noAux = noAux
        self.prioridade = prioridade

grafo = open('arquivomatriz.txt', 'r')

for i in grafo:

    linha = i.split()

    arestas.append(Grafo(int(linha[0]), int(linha[1]), int(linha[2])))

grafo.close()

def Inserir(vector):

    inserido = False

    for i in range( len(vertices) ):

        if (vector == vertices[i]):

            inserido = True
            break

    return inserido

for i in range( len(arestas) ):

    if(not Inserir(arestas[i].no)):

        vertices.append(arestas[i].no)

    if(not Inserir(arestas[i].noAux)):

        vertices.append(arestas[i].noAux)
vertices = sorted(vertices)

for i in range( len(vertices) ):  #Preenche matriz com 0's

    linha = []

    for j in range( len(vertices) ):

        linha.append(0)

    matriz.append(linha)

for i in range( len(arestas) ):   # matriz adjacente

    matriz[arestas[i].no][arestas[i].noAux] = arestas[i].prioridade
    matriz[arestas[i].noAux][arestas[i].no] = arestas[i].prioridade

print()
print("Matriz Adja: ")

for i in range( len(matriz) ):

    print(matriz[i])

print()

print("O grau de cada vértice é: ")

for i in range( len(matriz) ):

    g = 0

    for j in range( len(matriz[i]) ):

        if(matriz[i][j] != 0):

            g += 1

    print('grau do {}: {}'.format(i,g) )


import matplotlib.pyplot as plt
# from mpl_toolkits.axes_grid1 import ImageGrid
# import numpy as np
from os import listdir
from os import chdir
from os import path
from PIL import Image
# import matplotlib.gridspec as gridspec
import argparse

parser = argparse.ArgumentParser(description="generate plot for report")
parser.add_argument("--input_dir", required=True, help="Input ROS bag.")
parser.add_argument("--rows", required=True, help="numer of rows in figure")
parser.add_argument("--cols", required=True, help="number of columns in figure")

args = parser.parse_args()

# chdir('/Volumes/macOS Big Sur/Users/pmvanderburg/matplotlib_test/')
chdir(args.input_dir)
files = listdir(args.input_dir)
files.sort()

for i, f in enumerate(files):
    if f!='.DS_Store':
        print(i,f)
    else:
        del files[i]

images = [Image.open(f) for f in files]
print(len(images))
max_rows = 7
max_cols = 3
# max_rows = 3
# max_cols = 2

methods=['Input image',
         '640x480 N+FT',
         '832x256 K+FT',
         '640x480 N',
         '832x256 N',
         '640x480 K',
         '832x256 K']

fig, axes = plt.subplots(nrows=7, ncols=3, figsize=(9,10),sharex=True, sharey=True)
for idx, image in enumerate(images):
    # print(files[idx])
    print(idx)
    row = idx % max_rows
    col = idx // max_rows
    print(row,' row')
    print(col,' col')
    # if col>0:
    # axes[row, col].axis("off")
    axes[row,col].spines['bottom'].set_color('#ffffff')
    axes[row,col].spines['top'].set_color('#ffffff')
    axes[row,col].spines['right'].set_color('#ffffff')
    axes[row,col].spines['left'].set_color('#ffffff')

    if image.size==(1280, 720):
        image = image.resize((640,480))

    axes[row, col].imshow(image, cmap="gray", aspect="auto")

    axes[row, 0].set_ylabel(methods[row])

plt.subplots_adjust(wspace=.05, hspace=.05)
plt.xticks([])
plt.yticks([])
# fig.savefig(path.join)
plt.show()


# coding: utf-8

# In[1]:

from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Normalizer
from sklearn import metrics

from sklearn.cluster import KMeans, MiniBatchKMeans

import sys
from time import time

import numpy as np

# In[2]:

df = pd.read_csv('lyrics.csv')
df.head(10)

# In[3]:

df['lyrics'].replace('', np.nan, inplace=True)
df.dropna(subset=['lyrics'], inplace=True)
ind_drop = df[df['genre'].apply(lambda x: x.startswith('Other'))].index
df = df.drop(ind_drop)

# In[4]:

ind_drop = df[df['genre'].apply(lambda x: x.startswith('Not Available'))].index
df = df.drop(ind_drop)

# In[5]:

ind_drop = df[df['lyrics'].apply(lambda x: x.startswith('INSTRUMENTAL'))].index
df = df.drop(ind_drop)
df.drop(columns=['index'])

ind_drop = df[df['lyrics'].apply(lambda x: x.startswith('instrumental'))].index
df = df.drop(ind_drop)
df.drop(columns=['index'])

# In[6]:

genre=df['genre'].values
lyrics=df['lyrics'].values
true_k = len(np.unique(genre))
print(np.unique(genre), "The total number of genres are", true_k)

#shaping:
lyrics = np.array(lyrics)[:,None]
print(lyrics.shape)
genre = np.array(genre)[:,None]
print(genre.shape)

# In[7]:

data = np.append(lyrics,genre,axis=1)
data.shape
print(data)

# In[8]:

np.random.shuffle(data)

data_test = data[10001:20001,]
data = data[:10000,]

# In[9]:

data_lyrics=data[:,0]
data_genre=data[:,1]

data_lyrics_test = data_test[:,0]
data_genre_test = data_test[:,1]

# print(data_lyrics)
# print(data_genre.shape)

# In[10]:

vectorizer = TfidfVectorizer(
                max_df=0.75, # max doc freq (as a fraction) of any word to include in the vocabulary
                min_df=0.3,   # min doc freq (as doc counts) of any word to include in the vocabulary
                max_features=10000,           # max number of words in the vocabulary
                stop_words='english',         # remove English stopwords
                use_idf=True )

# In[11]:

labels={'Country':1, 'Electronic':2, 'Folk':3, 'Hip-Hop':4, 'Indie':5, 'Jazz':6,
       'Metal':7, 'Pop':8, 'R&B':9, 'Rock':10}

print("Extracting features from the training dataset using a sparse vectorizer")
t0 = time()
vectorizer.fit(data_lyrics)

X = vectorizer.transform(data_lyrics)
Y = [labels[i] for i in data_genre]

X_test = vectorizer.transform(data_lyrics_test)
Y_test = [labels[i] for i in data_genre_test]

n_features = X.shape[1]
print("done in %fs" % (time() - t0))
print("n_samples: %d, n_features: %d" % X.shape)

# In[12]:

doc_ind = 1  # Index of an example document
xi = X[doc_ind,:].todense()
term_ind = xi.argsort()[:, ::-1]
xi_sort = xi[0,term_ind]
terms = vectorizer.get_feature_names()

for i in range(n_features):
    term = terms[term_ind[0,i]]
    tfidf = xi[0,term_ind[0,i]]
    print('{0:20s} {1:f} '.format(term, tfidf))

# In[13]:

km = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1,
                verbose=True)

# In[14]:

print("Clustering sparse data with %s" % km)
t0 = time()
km.fit(X)
print("done in %0.3fs" % (time() - t0))
print()

# In[15]:

order_centroids = km.cluster_centers_.argsort()[:, ::-1]
for i in range(true_k):
    print("Cluster %d:" % i, end='')
    for ind in order_centroids[i, :10]:
        print(' %s' % terms[ind], end='')
    print()

# In[16]:

labels={'Country':1, 'Electronic':2, 'Folk':3, 'Hip-Hop':4, 'Indie':5, 'Jazz':6,
       'Metal':7, 'Pop':8, 'R&B':9, 'Rock':10}
print(labels.values)
# genre_names
# data_genre
genre_labels=[]
#print(genre_labels.shape)
for j,i in enumerate(data_genre):
    x=labels[i]
    #print(x)
    np.append(genre_labels,x)
    genre_labels.append(x)
#print(genre_labels)

# In[17]:

print((Y_test == km.predict(X_test)).sum() / len(Y_test))

# In[18]:

labelkm = km.labels_
print(labelkm.shape)
print(type(labelkm))

# In[19]:

#print(data_genre)
labelkm = km.labels_
from sklearn.metrics import confusion_matrix
C = confusion_matrix(genre_labels,labelkm)

Csum = np.sum(C,axis=0)
Cnorm = C / Csum[None,:]
print(Cnorm)
print(np.array_str(C, precision=3, suppress_small=True))
plt.imshow(C, interpolation='none')
plt.colorbar()


import unittest

import requests_mock

from alertaclient.api import Client

class PermissionTestCase(unittest.TestCase):

    def setUp(self):
        self.client = Client()

        self.perm = """
            {
              "id": "584f38f4-b44e-4d87-9b61-c106d21bcc7a",
              "permission": {
                "href": "http://localhost:8080/perm/584f38f4-b44e-4d87-9b61-c106d21bcc7a",
                "id": "584f38f4-b44e-4d87-9b61-c106d21bcc7a",
                "match": "websys",
                "scopes": [
                  "admin:users",
                  "admin:keys",
                  "write"
                ]
              },
              "status": "ok"
            }
        """

    @requests_mock.mock()
    def test_permission(self, m):
        m.post('http://localhost:8080/perm', text=self.perm)
        perm = self.client.create_perm(role='websys', scopes=['admin:users', 'admin:keys', 'write'])
        self.assertEqual(perm.match, 'websys')
        self.assertEqual(sorted(perm.scopes), sorted(['admin:users', 'admin:keys', 'write']))


# OO style
import matplotlib.pyplot as plt

data_uas = [['Bejo', 70],
            ['Tejo', 83],
            ['Cecep', 62],
            ['Wati', 74],
            ['Karti', 71]
            ]

fig, ax = plt.subplots()
table = plt.table(cellText = data_uas, loc = 'center')
table.set_fontsize(14)
table.scale(.5, 2) # ukuran kolom, baris
ax.axis(False)
plt.title('OO Style')
plt.show()


'''
$Id: gvars.py 44 2010-10-11 11:24:33Z goffer.looney@gmail.com $
'''

from datetime import datetime, date
from models import Global_Var
from django.conf import settings
from django.core.cache import cache

'''  see gsettings.models.Global_Var_Type '''
VAR_TYPE_STRING     = 1
VAR_TYPE_INT        = 2
VAR_TYPE_FLOAT      = 3
VAR_TYPE_DATETIME   = 4
VAR_TYPE_DATE       = 5

def get_value(name, category_name=None):
    ''' Returns the value of a variable from the cache, or the database if the cache is empty/expired/disabled.
        Returns None if variable is not found.
    '''
    ret = None
    vars = __get_vars_from_cache()
    if vars is not None:
        if name in vars:
            ret = vars[name]
            if category_name is None:
                # pick the first category
                for i in ret:
                    ret = ret[i]
                    break
            else:
                if category_name in ret:
                    ret = ret[category_name]
    else:
        ret = get_value_db(name, category_name)
    return ret

def get_value_db(name, category_name=None):
    ''' Returns the value of a variable from the database.
        Never tries the cache.
        Returns None if the variable is not found.'''
    ret = None
    var = Global_Var.objects.filter(name=name)
    if category_name is not None:
        var = var.filter(global_var_category__name=category_name)
    if var.count():
        var = var[0]
        ret = __get_cast_var(var)
    return ret

def cache_values(force_reload = False):
    raise Exception('cache_values() is deprecated. GSettings caching uses your projects cache settings (see CACHE_BACKEND)')

''' --------------- INTERNAL FUNCTIONS -------------- '''

def __get_vars():
    ''' Returns a dictionary with all the gsettings variable values.
        Variables are read from the cache (if caching is enabled AND cache is not empty AND not expired)
        or the database (otherwise).
        Repopulate the cache if necessary.
    '''
    ret = __get_vars_from_cache()
    if ret is None: ret = __get_vars_from_db()
    return ret

def __get_vars_from_cache():
    ''' Returns a dictionary with all the gsettings variable values.
        Variables are read from the cache. If the cache is expired or empty, read from the DB and fill the cache.
        If caching is disabled, returns None.
    '''
    ret = None
    if __is_cache_enabled():
        ret = cache.get('gsettings.vars', None)
        if ret is None:
            # get all the data from the DB to a dictionary
            ret = __get_vars_from_db()
            timeout = getattr(settings, 'GSETTINGS_CACHE_TIMEOUT', None)
            cache.set('gsettings.vars', ret, timeout)
    return ret

def __get_vars_from_db():
    ''' Returns a dictionary with all the gsettings variable values.
        Variables are read from the database. It neither read from nor write to the cache.
    '''
    ret = {}
    for var in Global_Var.objects.all():
        if var.name not in ret:
            ret[var.name] = {}
        ret[var.name][var.global_var_category.name] = __get_cast_var(var)
    return ret

def __get_cast_var(var):
    ''' Returns the value of a variable. it is cast into the proper python type. '''
    return __get_cast_value(var.value, var.global_var_type.id)

def __get_cast_value(value, typeid):
    ''' Returns the value of a variable. it is cast into the proper python type. '''
    ret = value
    try:
        if typeid == VAR_TYPE_INT: ret = int(ret)
        if typeid == VAR_TYPE_FLOAT: ret = float(ret)
        if typeid == VAR_TYPE_DATE:
            parts = ret.split('-')
            ret = date(int(parts[0]), int(parts[1]), int(parts[2]))
        if typeid == VAR_TYPE_DATETIME:

            parts = ret.split(' ')
            parts_date = parts[0].split('-')
            parts_time = parts[1].split(':')
            ret = datetime(int(parts_date[0]), int(parts_date[1]), int(parts_date[2]), int(parts_time[0]), int(parts_time[1]), int(parts_time[2]))
    except:
        raise ValueError('Invalid format.')
    return ret

def __get_string_from_value(value):
    ''' Returns a string from a python value.
        The format of this string is compatible with database format for global variables. '''
    ret = value
    type_name = value.__class__.__name__
    if type_name == 'int':
        str(ret)
    if type_name == 'float':
        str(ret)
    if type_name == 'date':
        ret.__str__()
    if type_name == 'datetime':
        ret.__str__()
    return ret

def __is_cache_enabled():
    ''' Returns  True if caching is enabled in the project settings. '''
    return (getattr(settings, 'CACHE_BACKEND', None) is not None)

#''' --------------- CACHING -------------- '''
#from django.core.signals import request_started
#
#def __on_request_started(sender, **kwargs):
#    # Clear the existing cache so if the database has been modified
#    # by a previous request we don't return stale values.
##    global cache
##    cache = None
##    print 'cache = None'
##    from django.conf import settings
##    if getattr(settings, 'GSETTINGS_AUTO_CACHE', False):
##        cache_values()
#    pass
#
#request_started.connect(__on_request_started)


from datetime import datetime

import pandas as pd
import numpy as np
import quandl
from arctic.exceptions import NoDataFoundException

from .base import BaseBacktestObject
from ..utils import keys
from ..utils.config import AdagioConfig
from ..utils.const import (FutureContractMonth, Denominator, PriceSkipDates,
                           ReturnSkipDates, FuturesInfo, RETURN_KEY_PRIORITY,
                           VOLUME_KEY_PRIORITY)
from ..utils.date import date_shift
from ..utils.logging import get_logger
from ..utils.mongo import get_library
from ..utils.quandl import futures_contract_name, futures_contract_month, year

logger = get_logger(name=__name__)

class QuandlGeneric(BaseBacktestObject):
    def __init__(self, **backtest_params):
        super(QuandlGeneric, self).__init__(**backtest_params)

class GenericInstrument(BaseBacktestObject):
    def __init__(self, **backtest_params):
        super(GenericInstrument, self).__init__(**backtest_params)

class TrueFXInstrument(BaseBacktestObject):
    def __init__(self, **backtest_params):
        super(TrueFXInstrument, self).__init__(**backtest_params)

class QuandlFutures(BaseBacktestObject):
    def __init__(self, **backtest_params):
        super(QuandlFutures, self).__init__(**backtest_params)
        self.data = None
        self.roll_date = None
        self.position = None
        self.is_expired = False

    def __repr__(self):
        return '{}({})'.format(self.__class__.__name__,
                               self[keys.quandl_ticker])

    @property
    def name(self):
        """ Return quandl ticker """
        return self[keys.quandl_ticker]

    @property
    def contract_name(self):
        """ Return contract name such as ES for S&P500 mini futures """
        return futures_contract_name(self[keys.quandl_ticker])

    @property
    def contract_month_code(self):
        """ Return month string representing the delivery month """
        return futures_contract_month(self[keys.quandl_ticker])

    @property
    def contract_month(self):
        """ Return delivery month """
        return FutureContractMonth[self.contract_month_code].value

    @property
    def contract_month_dt(self):
        """ Return the beginning of the month of delivery """
        return datetime(self.year, self.contract_month, 1).date()

    @property
    def year(self):
        """ Return delivery year """
        return year(self[keys.quandl_ticker])

    @property
    def price_for_return(self):
        """ Return a series for returns """
        return self.data[self.get_return_key()]

    @property
    def has_volume(self):
        """ Return if data contains volume information """
        try:
            _ = self.get_volume_key()
            return True
        except ValueError:
            return False

    def get_final_positions(self):
        """ Return final position (adjusted by signals etc.)
        Trading lags are already applied so that the final return can be
        calculated by final_position * returns.
        """
        return self.position.prod(axis=1).rename('final_position')

    def get_final_gross_returns(self):
        """ Return final gross returns for its contract using final_positions
        Any slippage is not deducted.
        """
        return ((self.calc_return() * self.get_final_positions())
                .rename('final_gross_returns'))

    def get_final_net_returns(self):
        """ Return final net returns for its contract using final_positions

        Cost = tick size * slippage * trade amount / price_t
        trade amount is proportional to the trading size and is at least 1.0
        every time we trade.
        """
        # trade_amount = 1 if there is a transaction
        final_positions = self.get_final_positions()
        trade_amount = (final_positions.diff()
                        .shift(-1)  # trading lag is already added to positions
                        .fillna(0.0)
                        .abs())

        # initial entry if the position is held from start
        start_date = self.data.index[0]
        trade_amount[start_date] = abs(final_positions[start_date])

        cost = (self.price_for_return.pow(-1)
                .replace([np.inf, -np.inf], np.nan)
                .fillna(method='pad')
                .mul(self[keys.tick_size] * self[keys.slippage])
                .mul(trade_amount))
        return ((self.get_final_gross_returns() - cost)
                .rename('final_net_returns'))

    def get_final_returns(self, is_gross=True):
        if is_gross:
            return self.get_final_gross_returns()
        else:
            return self.get_final_net_returns()

    def backtest(self, start_date, end_date, *args, **kwargs):
        """ Get data from Quandl and clean it. Positions are calculated
        according to start_date and end_date (both including).

        :param start_date:
        :param end_date:
        :return:
        """
        logger.info('Run layers: {}'.format(self))

        # load data
        self.data = self.load_data()
        self.data = self.clean_data()
        self.roll_date = end_date

        # determine base positions based on the roll date
        logger.debug('Determining base positions')
        self.position = pd.DataFrame(0.0, columns=['base'],
                                     index=self.data.index)
        self.position.loc[slice(start_date, end_date), 'base'] = 1.0
        self[keys.start_date] = start_date
        self[keys.end_date] = end_date

    def check_if_expired(self, data):
        """ Check if the contract is expired """
        if data.index[-1] >= self.last_trade_date():
            # if data contains the last trade date
            self.is_expired = True
        else:
            today = datetime.today()
            if data.index[-1] < date_shift(today, '-1y'):
                # if data is very old the contract is assumed to be expired
                self.is_expired = True

    def load_from_quandl(self):
        """ Download data from quandl """
        logger.debug('Downloading data from Quandl')
        data = quandl.get(self[keys.quandl_ticker],
                          api_key=AdagioConfig.quandl_token)
        self.check_if_expired(data)
        return data

    def to_mongo(self, library, data):
        """ Save data to MongoDB """
        logger.debug('Pushing data to MongoDB')
        library.write(self[keys.quandl_ticker], data)

    def load_data(self):
        """ Load data either from MongoDB stored locally """

        library = get_library(keys.quandl_contract)
        item = library.read(self[keys.quandl_ticker])
        data = item.data
        self.check_if_expired(data)
        return data

    def update_database(self):
        """ Update local database by checking Quandl if they have the latest
        data """
        library = get_library(keys.quandl_contract)
        try:
            item = library.read(self[keys.quandl_ticker])
            data = item.data
            self.check_if_expired(data)

            if not self.is_expired or self[keys.force_download]:
                # re-download data. Quandl might have more recent data
                data = self.load_from_quandl()
                self.to_mongo(library, data)
            else:
                logger.debug('Load data from MongoDB')

        except NoDataFoundException:
            # if not found in MongoDB, then it tries to get data from Quandl
            data = self.load_from_quandl()
            self.to_mongo(library, data)

    def get_date(self, shift_string):
        """ Shift date from the delivery month-begin """
        return date_shift(self.contract_month_dt, shift_string)

    def last_trade_date(self):
        return self.get_date(self[keys.last_trade_date])

    def first_notice_date(self):
        if self[keys.first_notice_date] is None:
            raise ValueError('{} not found.'.format(keys.first_notice_date))
        return self.get_date(self[keys.first_notice_date])

    def get_roll_base_date(self):
        """ Return roll base date from which the actual roll date is
        calculated. This is either first notice date or last trade date. """
        if self[keys.first_notice_date] is not None:
            first_notice_date = self.first_notice_date()
            last_trade_date = self.last_trade_date()
            return min(first_notice_date, last_trade_date)
        else:
            return self.last_trade_date()

    def get_roll_date(self, roll_rule):
        """ Return roll date

        If there is no first notice date, then the roll date is X-days before
        its last trade date
        If the contract has a setting for first notice date, then the roll date
        is min(X-days before last trade date, X-days before first notice date)
        """
        return date_shift(self.get_roll_base_date(), roll_rule)

    def clean_data(self):
        """ Clean erroneous dates """
        cleaned_data = self.data

        if self[keys.lo_ticker] in PriceSkipDates.__members__.keys():
            dates = PriceSkipDates[self[keys.lo_ticker]].value
            skip_dates = pd.DatetimeIndex(dates)
            to_nan = [t for t in skip_dates if t in cleaned_data.index]
            cleaned_data.loc[to_nan] = None
            cleaned_data = (cleaned_data
                            .fillna(method='pad')
                            .fillna(method='bfill'))

        # remove when all prices are zero or negative for some reason
        flgs = self.price_for_return > 0
        cleaned_data = (cleaned_data.where(flgs).fillna(method='pad')
                        .fillna(method='bfill'))

        if self[keys.lo_ticker] in __fut_clean_func__.keys():
            cleaned_data = __fut_clean_func__[self[keys.lo_ticker]](
                cleaned_data)
        return cleaned_data

    def _trim_data(self):
        """ Trim underlying data based on the backtest period """
        period = slice(self[keys.backtest_start_date],
                       self[keys.backtest_end_date])
        self.data = self.data.loc[period, :]
        self.position = self.position.loc[period, :]

    def get_return_key(self):
        """ Return a column name used to be used for calculating returns """
        for return_key in RETURN_KEY_PRIORITY:
            if return_key in self.data.keys():
                return return_key
        raise ValueError('No return key found. Data contains {}'
                         .format(self.data.keys()))

    def get_volume_key(self):
        """ Return a column name used to be used for calculating returns """
        for return_key in VOLUME_KEY_PRIORITY:
            if return_key in self.data.keys():
                return return_key
        raise ValueError('No volume key found. Data contains {}'
                         .format(self.data.keys()))

    def calc_return(self):
        """ Calculate returns and clean it if necessary """
        return_raw = self._calc_return_raw()

        if self[keys.lo_ticker] in [i.name for i in ReturnSkipDates]:
            fix_date = ReturnSkipDates[self[keys.lo_ticker]].value
            for d in fix_date:
                if d in return_raw.index:
                    return_raw[d] = 0.0

        return_raw = self.convert_return_ccy(return_raw)

        return return_raw

    def _calc_return_raw(self):
        """ Return raw returns according to the denominator.
        (Fully-collateralised returns)

        If denominator is not specified, returns are just percentage changes.
        Other denominators follow the market conventions and/or the data format.
        """
        if self[keys.denominator] is None:
            base_price = self._get_base_price()
        else:
            if self[keys.denominator] == Denominator.GOVT_FUT.value:
                base_price = self._get_base_price(100.0)
            elif self[keys.denominator] == Denominator.MM_FUT.value:
                base_price = self._get_base_price(100.0 * 0.25)
            else:
                raise ValueError("{} is not a valid denominator."
                                 .format(self[keys.denominator]))
        return self.price_for_return.diff().div(base_price.shift()).fillna(0)

    def _get_base_price(self, constant=None):
        """ Get base price series that will be used as a denominator
        for return calculation """
        if constant is not None:
            base_price = pd.Series(constant, index=self.position.index)
        else:
            final_position = self.get_final_positions()
            position_change = (final_position
                               .shift(-1)
                               .fillna(method='pad')
                               .diff()
                               .abs()
                               .pipe(np.sign))
            # initial position
            position_change = (position_change
                               .fillna(final_position.abs().pipe(np.sign)))

            base_price = (self.price_for_return
                          .where(position_change == 1)
                          .fillna(method='pad'))

        # base_return is later shifted by 1 period when multiplied by
        # price change
        return base_price

    def convert_return_ccy(self, returns):
        """ Convert returns series into the backtest currency

        This calculation assumes that the position is fully-collateralised
        and the initial collateral is fully fx-hedged.

        :param returns: returns series measured in the contract currency
        :return:
        """

        if self[keys.contract_ccy] == self[keys.backtest_ccy]:
            return returns
        else:
            library = get_library(keys.fx_rates)
            symbols = library.list_symbols(regex=self[keys.contract_ccy])

            if len(symbols) > 1:
                raise ValueError('Multiple fx rates found')

            fx_rates = library.read(symbols[0])
            fx_rates = fx_rates.data

            if fx_rates.name == '{}/{}'.format(self[keys.contract_ccy],
                                               self[keys.backtest_ccy]):
                # use fx rates as is
                pass
            elif fx_rates.name == '{}/{}'.format(self[keys.backtest_ccy],
                                                 self[keys.contract_ccy]):
                fx_rates = fx_rates.pow(-1)

            fx_adj = (fx_rates
                      .reindex(returns.index)
                      .fillna(method='pad')
                      .pct_change()
                      .fillna(0)
                      .add(1.0)
                      )
            return (returns * fx_adj).rename(returns.name)

def _clean_jgb_prices(df):
    df[:'2018-01-18'] *= 0.1
    return df

__fut_clean_func__ = {
    FuturesInfo.SGX_JB.name: _clean_jgb_prices
}


from   _PMA              import PMA
import _PMA.V_Mailbox

class Filter_Mailbox (PMA._V_Mailbox_) :
    """A mailbox which does not contain real messages but presents a filtered
       view of a mailbox (or a virtual mailbox).
    """

    def __init__ (self, name, matcher, mailbox, prefix = None, ** ckw) :
        self.mailboxes = (mailbox, )
        self.__super.__init__ \
            ( name     = name
            , prefix   = mailbox.qname
            , root     = mailbox.root
            )
        if not isinstance (matcher, PMA._Matcher_) :
            matcher    = PMA.Matcher (matcher, ** ckw)
        self._matcher  = matcher
    # end def __init__

    def _eligible (self, messages) :
        return self._matcher.filter (* messages)
    # end def _eligible

# end class Filter_Mailbox

"""
from _PMA                  import PMA
import _PMA.Mailbox
import _PMA.Filter_Mailbox
import _PMA.Matcher
import _PMA.Office

mb1 = PMA.Mailbox    ("/home/glueck/PMA/TTTech/planung")
mbs = PMA.Maildir    ("/home/glueck/PMA/D/inbox")
vmb = PMA.V_Mailbox  ("inboxes", (mbs, ))
fmb = vmb.add_filter_mailbox ("PGR", "'groessinger' in sender_name.lower ()")
fmb.messages
m   = mbs.messages [58]
"""

if __name__ != "__main__" :
    PMA._Export ("*")
### __END__ PMA.Filter_Mailbox


import re
import io

keywords=["int","void","main","print"]

operators = { '=': 'Assignment Operator','+': 'Additon Operator', '-' : 'Substraction Operator', '/' : 'Division Operator', '*': 'Multiplication Operator'}
optr_keys = operators.keys()

symbols = {';':'semi_colon','{' : 'left_brace', '}':'right_brace', '(':'left_parenthesis',')':'right_parenthesis' ,'[':'left_sqbracket',']':'right_sqbracket'}
symbol_keys = symbols.keys()

the_ch = " "
the_line = 1
token_list = []
error = []

#get the next character from the input file
def next_ch():

    global the_ch, the_line
    the_ch = input_file.read(1)
    if the_ch == '\n':
        the_line += 1
    return the_ch

#handle identifiers and numbers
def identifier_or_number(line_no):

    text = ""
    while the_ch.isalnum() or the_ch == '_' or the_ch =='.':
        text += the_ch
        next_ch()

    if len(text) == 0:
        error_msg = "Unrecognized character "+the_ch+" found in line : "+str(line_no)
        error.append(error_msg)
        next_ch()
        return '' , '' , ''

    elif text in keywords:
        token_list.append(text)
        return line_no, text, "Keyword"

    elif text in re.findall('[_a-zA-Z][_a-zA-Z0-9]*',text):
        token_list.append('id')
        return line_no , text , 'Identifier'

    elif text in re.findall('[0-9]+[.]?[0-9]*',text):
        token_list.append('num')
        return line_no , text , 'Number'

    elif text not in re.findall('[_a-zA-Z ][_a-zA-Z0-9 ]*',text):
        error_msg=text+" is an invalid identifier found in line : "+str(line_no)
        error.append(error_msg)
        return '','',''

#return the next token type
def getToken():

    while the_ch.isspace():
        next_ch()

    line_no = the_line

    if len(the_ch) == 0:
        token_list.append('$')
        return line_no, '$' , 'Enf_of_input'

    elif the_ch in symbol_keys:
        token = the_ch
        token_list.append(token)
        sym = symbols[token]
        next_ch()
        return line_no, token , sym

    elif the_ch in optr_keys:
        token = the_ch
        token_list.append(token)
        opr = operators[token]
        next_ch()
        return line_no, token , opr

    else:
        return identifier_or_number(line_no)

#opening input file
f = open("input.txt", "r")
i = f.read()

program = re.sub('//.*?\n|/\*.*?\*/', '', i, flags=re.S) #removes all comment lines from input file
input_file = io.StringIO(program) #converting string to file object

print("\nOutput of Lexical Analyser\n--------------------------\n")
print("%5s %7s %9s" % ("Line No.", "Token","Meaning"))
print("----------------------------")

while True:
    t = getToken()
    line  = t[0]
    token = t[1]
    meaning  = t[2]

    if token != '' and token != '$':
        print("%5s  %9s  %-15s" % (line, token, meaning))
    elif token == '$':
        break

#display error msg if found any
if len(error) != 0:
    print("\n\n-------------",len(error),"ERROR FOUND ---------------\n")
    for msg in error:
        print(msg)
    print("\n-------------------------------------------")
    exit(0)

print("\nThere are total",line,"lines in program")
print("Tokens:",token_list)


from api.models import Term
from sqlite3 import Error
from api.services.connection import create_connection
import sqlite3
import json

def obtener_correcciones():
    lista = {'correcciones':[]}
    conn = create_connection('db.sqlite3')
    cur = conn.cursor()
    cur.execute("SELECT * FROM api_correcciones")

    rows = cur.fetchall()

    for row in rows:
        correccion = {
            'id': row[0],
            'fecha': row[1],
            'fk_defensa': row[2]
        }
        lista['correcciones'].append(correccion)
    return lista

def crear_correccion(correccion):
    conn = create_connection('db.sqlite3')
    cur = conn.cursor()

    print(correccion)

    sql = "SELECT id FROM api_defensa WHERE codigo='{0}'".format(correccion['defensa'])
    cur.execute(sql)
    row = cur.fetchall()
    defensa = list(row[0])
    print(defensa)

    sql = "INSERT INTO api_correcciones(fecha,fk_defensa_id) VALUES ('{0}',{1})".format(correccion['fecha'],defensa[0])
    cur.execute(sql)
    conn.commit()

def actualizar_correccion(correccion):
    conn = create_connection('db.sqlite3')
    cur = conn.cursor()
    sql = "UPDATE api_correcciones SET fecha='{0}' WHERE id={1}".format(correccion['fecha'], correccion['id'])
    cur.execute(sql)
    conn.commit()

def eliminar_correccion(id):
    conn = create_connection('db.sqlite3')
    cur = conn.cursor()
    sql = "DELETE FROM api_correcciones WHERE id={0}".format(id)
    cur.execute(sql)
    conn.commit()


class Solution:
    def numUniqueEmails(self, emails):
        s = set()
        for email in emails:
            email_list = email.split("@")
            local_name = email_list[0].split("+")[0].replace(".", "")
            email = local_name+"@"+email_list[1]
            print(email)
            s.add(email)
        return len(s)

emails_list = ["test.email+alex@leetcode.com",
               "test.e.mail+bob.cathy@leetcode.com",
               "testemail+david@lee.tcode.com"]
print(Solution().numUniqueEmails(emails_list))


#!/usr/bin/env python3
import numpy as np
import urllib.request
import cv2
import binascii
import lorem
import math
import matplotlib.pyplot as plt

def encode_as_binary_array(msg):
    """Encode a message as a binary string."""
    msg = msg.encode("utf-8")
    msg = msg.hex()
    msg = [msg[i:i + 2] for i in range(0, len(msg), 2)]
    msg = [bin(int(el, base=16))[2:] for el in msg]
    msg = ["0" * (8 - len(el)) + el for el in msg]
    return "".join(msg)

def decode_from_binary_array(array):
    """Decode a binary string to utf8."""
    array = [array[i:i+8] for i in range(0, len(array), 8)]
    if len(array[-1]) != 8:
        array[-1] = array[-1] + "0" * (8 - len(array[-1]))
    array = [hex(int(el, 2))[2:].zfill(2) for el in array]
    array = "".join(array)
    result = binascii.unhexlify(array)
    return result.decode("utf-8", errors="replace")

def hide_message(image, message, nbits=1):
    """Hide a message in an image (LSB).

    nbits: number of least significant bits
    """
    nbits = clamp(nbits, 1, 8)
    shape = image.shape
    image = np.copy(image).flatten()
    if len(message) > len(image) * nbits:
        raise ValueError("Message is to long :(")

    chunks = [message[i:i + nbits] for i in range(0, len(message), nbits)]
    for i, chunk in enumerate(chunks):
        byte = str(bin(image[i]))[2:].zfill(8)
        new_byte = byte[:-nbits] + chunk
        image[i] = int(new_byte, 2)

    return image.reshape(shape)

def clamp(n, minn, maxn):
    """Clamp the n value to be in range (minn, maxn)."""
    return max(min(maxn, n), minn)

def reveal_message(image, nbits=1, length=0):
    """Reveal the hidden message.

    nbits: number of least significant bits
    length: length of the message in bits.
    """
    nbits = clamp(nbits, 1, 8)
    shape = image.shape
    image = np.copy(image).flatten()
    length_in_pixels = math.ceil(length/nbits)
    if len(image) < length_in_pixels or length_in_pixels <= 0:
        length_in_pixels = len(image)

    message = ""
    i = 0
    while i < length_in_pixels:
        byte = str(bin(image[i]))[2:].zfill(8)
        message += byte[-nbits:]
        i += 1

    mod = length % -nbits
    if mod != 0:
        message = message[:mod]
    return message

print("Downloading image!")
path = 'https://picsum.photos/500/500'
resp = urllib.request.urlopen(path)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
print("Image downloaded!")

message = lorem.text()*1000
secret = encode_as_binary_array(message)

resultImageRow1 = None
resultImageRow2 = None

nbitsList = range(1, 9)
nbitsMSE = []
for nbits in nbitsList:
    print(nbits)
    imageSecret = hide_message(image, secret[:int(image.size*0.8)], nbits)
    mse = ((imageSecret - image)**2).mean()
    nbitsMSE.append(mse)

    if nbits <= 4:
        resultImageRow1 = imageSecret if resultImageRow1 is None else np.hstack(
            [resultImageRow1, imageSecret])
    else:
        resultImageRow2 = imageSecret if resultImageRow2 is None else np.hstack(
            [resultImageRow2, imageSecret])

plt.plot(nbitsList, nbitsMSE)
plt.xlabel('nbits')
plt.ylabel('MSE')

cv2.namedWindow("Result", cv2.WINDOW_NORMAL)

cv2.imshow('Result', np.vstack([resultImageRow1, resultImageRow2]))
cv2.imwrite('ex2_encoded.png', np.vstack([resultImageRow1, resultImageRow2]))
cv2.waitKey(1)
plt.savefig('ex2_plot.png')
plt.show()
cv2.waitKey()

# Dla nbits=7,8 MSE zmalał, ponieważ widoczna jest większa część bazowego obrazu
# - wiadomość zapisano na mniejszej liczbie plkseli