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

import csv
import nltk
import string
from collections import Counter
from collections import defaultdict as dd

PUNCS = list(string.punctuation) + ["``", "''"]
NO_PUNC = str.maketrans('', '', string.punctuation)

# read bible data
with open('bibid.csv', 'r', encoding= 'utf8') as infile:
    reader = csv.DictReader(infile)
    verses = [v for v in reader]

# tokenize
tc = Counter()
for v in verses:
    # remove tab
    if v['text'].startswith('<t />'):
        text = v['text'][5:]
    else:
        text = v['text']
    # tokenize
    tokens = nltk.word_tokenize(text)
    v['tokens'] = tokens
    tc.update(tk.translate(NO_PUNC) for tk in tokens if tk not in PUNCS)
lexicon = set(tc.keys())

# --------------------
# look for names
names = dd(set)
maybe_names = dd(set)
for vid, v in enumerate(verses):
    for tid, tk in enumerate(v['tokens']):
        token = tk.translate(NO_PUNC)  # remove punc
        if not token:
            continue
        elif token[0].isupper():
            # if it's the first token ...
            if token.upper() == token or tid == 0 or v['tokens'][tid - 1] == '``' or v['tokens'][tid - 1][-1] in '.!\'?':
                # check if the lowered is also in lexicon
                maybe_names[token].add(vid)
            else:
                names[token].add(vid)
print("Found names: {}".format(len(names)))
print("May be: {}".format(len(maybe_names)))
name_list = list(sorted(names))

# write to file
name_file = "name_freq.csv"
with open(name_file, "w") as outfile:
    writer = csv.writer(outfile, dialect='excel-tab', quoting=csv.QUOTE_MINIMAL)
    for word, freq in tc.most_common():
        if word in names:
            writer.writerow((word, freq))

print("Donezo! Namelist has been written to {}".format(name_file))


def f(n, a):
    nums = [4, 8, 15, 16, 23, 42]
    aux = [0] * 6

    for e in a:
        i = nums.index(e)
        if (i == 0):
            aux[i] += 1
        else:
            if (aux[i-1] > 0):
                aux[i-1] -= 1
                aux[i] += 1

    res = n - aux[5] * 6
    return res

n = int(input())
a = list(map(int, input().split()))
print(f(n, a))


# -*- coding:utf-8 -*-
# /user/bin/python

from django.conf import settings

def init_permission(request, username):
    """
    用户权限信息初始化，获取当前用户所有权限信息，并保存到Session中
    :param request:
    :param username:
    :return:
    """
    permission_list = username.user_role.filter(permissions__id__isnull=False).values(
        "permissions__id",
        "permissions__title",
        "permissions__url",
        "permissions__code",
        "permissions__group",
        "permissions__menu_gp_id",          # 组内菜单ID，Null表示是菜单
        "permissions__group_id",            # 权限的组ID
        "permissions__group__menu_id",      # 权限的组的菜单ID
        "permissions__group__menu__title"    # 权限的组的菜单名称
    ).distinct()
    print(permission_list)

    # 菜单相关
    permission_menu_list = []
    for item in permission_list:
        # is_menu = item["permissions__is_menu"]
        # if not is_menu:
        #     continue
        temp = {
            "id": item["permissions__id"],
           "title": item["permissions__title"],
           "url": item["permissions__url"],
           "menu_gp_id": item["permissions__menu_gp_id"],
           "menu_id": item["permissions__group__menu_id"],
           "menu_title": item["permissions__group__menu__title"],
        }
        print(temp)
        permission_menu_list.append(temp)
        request.session[settings.OO] = permission_menu_list

    # 权限相关
    permission_dict = {}
    for item in permission_list:
        group_id = item["permissions__group_id"]
        code = item["permissions__code"]
        url = item["permissions__url"]
        if group_id in permission_dict:
            permission_dict[group_id]["codes"].append(code)
            permission_dict[group_id]["urls"].append(url)
        else:
            permission_dict[group_id] = {
                "codes":[code, ],
                "urls":[url, ]
            }
    request.session[settings.XX] = permission_dict


"""
This file is where all other apis and stream functions are called
"""
from modules import api, stream, database, universe
import threading

def main():

    # These are api examples:
    print(api.instruments.getInstrument('AMD'))
    print(api.instruments.getInstrument('007903107'))
    print(api.movers.getMovers("$DJI", direction="up", change="percent"))
    print(api.optionChains.getOptionChain(symbol="AMD", contractType="CALL", includeQuotes="TRUE", range="ITM", toDate="2022-11-7"))
    print(api.priceHistory.getPriceHistory('AMD', periodType='day', period='1', frequencyType='minute', frequency='30', needExtendedHoursData='false'))
    print(api.quotes.getQuote('AMD'))
    print(api.quotes.getQuotes(['AMD', "INTC"]))
    stream.send(stream.levelOne.quote(["AMD", "INTC"], [0, 1, 2, 3, 4, 5, 8, 9, 24]))   #<--- you must ALWAYS send 0 in fields
    stream.send(stream.levelOne.futures(["/ES"], [0, 1, 2, 8])) #<-- futures/forex/options do not override orther types of subs.
    stream.send(stream.levelOne.forex(["EUR/USD"], [0, 1, 2, 8]))
    stream.send(stream.levelOne.option(["AMD"], [0, 2, 3, 8])) #Options dont work yet for some reason, maybe you can fix it?

    """
    # This is an example of a dynamically built order, It is commented out so you dont accidentally run it!
    ord = order.Order(orderType="LIMIT", session="NORMAL", duration="DAY", orderStrategyType="SINGLE", price=90)

    leg = order.Leg(instruction="Buy", quantity=1)
    ord.addLeg(leg)

    ins = order.Instrument(assetType="EQUITY", symbol="AMD", optionMultiplier="This is not included in compliation")
    leg.addInstrument(ins)

    ord.submit()

    # This is an example of a preset order, It is commented out so you dont accidentally run it!

    order.submit(order.presets.equity.buyLimited("AMD", 1, 90))

    # To cancel an order:

    accountsAndTrading.cancelOrder("Order Number")

    """
    """
    # if you want to enter your own commands while the stream is running
    prev = ""
    history = []
    while True:
        try:
            entered = input("[INPUT]: Enter something to execute:\n")
            if entered == "":
                exec(prev)
                history.append(prev)
            else:
                exec(entered)
                prev = entered
                history.append(entered)
            print("Succeeded")
        except Exception as error:
            print(error)
            print("There was an error in the command that you entered.")
    """

if __name__ == '__main__':
    api.initialize()  # checks tokens & loads variables
    if universe.preferences.usingDatabase: database.DBConnect() # database code is incomplete
    universe.threads.append(threading.Thread(target=api._checkTokensDaemon, daemon=True))  # thread that automatically updates tokens
    stream.startAutomatic(streamPreHours=False, streamAfterHours=False) # this starts the stream during market hours and stops it during non-market hours.
    #stream.startManual() # this starts the stream immediately (only use one type of stream start)
    universe.threads.append(threading.Thread(target=main)) # add the main thread to the list of threads to start
    for thread in universe.threads:
        thread.start()
    for thread in universe.threads:
        thread.join()


import math
import sys
def decrypt_key(key):
    key=str(key)
    decrypt_key=[""]*len(key)
    for i in key:
        decrypt_key[int(i)-1]=str(key.index(i)+1)
    key="".join(decrypt_key)
    return int(key)

def read_encipher_matrix(strings,columns):
    cipher=[]
    for col in range(columns):
        cipher_str=""
        for string in strings:
            if string==strings[len(strings)-1] and len(string)<col+1:
                continue
            else:
                cipher_str= cipher_str+string[col]
        cipher.append(cipher_str)
    return cipher

def encrypt_columnar(plaintext,columns=None,key=None,transposition_type=None):
    cipher=""
    if transposition_type=="keylessColumnar":
        crypt_str=[]
        for i in range(0,len(plaintext),columns):
            string=plaintext[i:i+columns]
            crypt_str.append(string)

        cipher=read_encipher_matrix(crypt_str,columns)
        cipher="".join(encrypt for encrypt in cipher)

    if transposition_type=="keyedColumnar":
        crypt_str=[]
        columns=len(str(key))
        key=str(key)
        for i in range(0,len(plaintext),columns):
            string=plaintext[i:i+columns]
            crypt_str.append(string)

        temp_crypt_str=['']*columns
        for i in range(0,columns):
            for string in crypt_str:
                if string == crypt_str[len(crypt_str)-1] and (len(string)<i+1):
                    break
                else:
                    temp_crypt_str[int(key[i])-1]+=string[i]
            #print(temp_crypt_str)
            cipher=''.join(temp_crypt_str)

    if transposition_type=="doubletranspostion":
        cipher=encrypt_columnar(plaintext,key=key,transposition_type="keyedColumnar")
        cipher=encrypt_columnar(cipher,key=key,transposition_type="keyedColumnar")

    return cipher.replace(" ","")

def decrypt_columnar(ciphertext,columns=None,key=None,transposition_type=None):
    plaintext=""
    if transposition_type=="keylessColumnar":
        rows=int(math.ceil(len(ciphertext)/columns))
        plain_str=[""]*rows
        index=0
        for col in range(columns):
            for row in range(rows):
                if (row == rows-1) and (len(plain_str[row])==(len(ciphertext)%columns)):
                    continue
                else:
                    plain_str[row]=plain_str[row]+ciphertext[index]
                    index+=1

        for string in plain_str:
            plaintext = plaintext + string

    if transposition_type=="keyedColumnar":
        plain_str=[]
        key=str(key)
        row_index=0
        col_index=0
        column=len(key)
        rows=int(math.ceil(len(ciphertext)/column))
        cipher_index=0
        temp_plain_str=['']*column
        for i in range(0,column):
            for j in range(rows):
                if int(key[i])>int(len(ciphertext)%column) and (j==rows-1):
                    continue
                else:
                    temp_plain_str[int(key[i])-1]+=ciphertext[cipher_index]
                    cipher_index+=1

        for i in range(0,rows):

            for string in  temp_plain_str:
                if string==temp_plain_str[len(temp_plain_str)-1] and i+1>len(string):
                    break
                else:
                    plaintext+=string[i]

    if transposition_type=="doubletranspostion":
        plaintext=decrypt_columnar(ciphertext,key=key,transposition_type="keyedColumnar")
        plaintext=decrypt_columnar(plaintext,key=key,transposition_type="keyedColumnar")

    return plaintext.replace(" ","")
def encryptRailFence(text, key):

    # create the matrix to cipher
    # plain text key = rows ,
    # length(text) = columns
    # filling the rail matrix
    # to distinguish filled
    # spaces from blank ones
    rail = [['\n' for i in range(len(text))]
                  for j in range(key)]

    # to find the direction
    dir_down = False
    row, col = 0, 0

    for i in range(len(text)):

        # check the direction of flow
        # reverse the direction if we've just
        # filled the top or bottom rail
        if (row == 0) or (row == key - 1):
            dir_down = not dir_down

        # fill the corresponding alphabet
        rail[row][col] = text[i]
        col += 1

        # find the next row using
        # direction flag
        if dir_down:
            row += 1
        else:
            row -= 1
    # now we can construct the cipher
    # using the rail matrix
    result = []
    for i in range(key):
        for j in range(len(text)):
            if rail[i][j] != '\n':
                result.append(rail[i][j])
    return("" . join(result))

# This function receives cipher-text
# and key and returns the original
# text after decryption
def decryptRailFence(cipher, key):

    # create the matrix to cipher
    # plain text key = rows ,
    # length(text) = columns
    # filling the rail matrix to
    # distinguish filled spaces
    # from blank ones
    rail = [['\n' for i in range(len(cipher))]
                  for j in range(key)]

    # to find the direction
    dir_down = None
    row, col = 0, 0

    # mark the places with '*'
    for i in range(len(cipher)):
        if row == 0:
            dir_down = True
        if row == key - 1:
            dir_down = False

        # place the marker
        rail[row][col] = '*'
        col += 1

        # find the next row
        # using direction flag
        if dir_down:
            row += 1
        else:
            row -= 1

    # now we can construct the
    # fill the rail matrix
    index = 0
    for i in range(key):
        for j in range(len(cipher)):
            if ((rail[i][j] == '*') and
               (index < len(cipher))):
                rail[i][j] = cipher[index]
                index += 1

    # now read the matrix in
    # zig-zag manner to construct
    # the resultant text
    result = []
    row, col = 0, 0
    for i in range(len(cipher)):

        # check the direction of flow
        if row == 0:
            dir_down = True
        if row == key-1:
            dir_down = False

        # place the marker
        if (rail[row][col] != '*'):
            result.append(rail[row][col])
            col += 1

        # find the next row using
        # direction flag
        if dir_down:
            row += 1
        else:
            row -= 1
    return("".join(result))

def encryptKeylessColumnar(plaintext,no_of_columns):
    encrypted_text=""
    if len(plaintext)%2!=0:
        if plaintext[len(plaintext)-1]=='Z':
            plaintext=plaintext+'Q'
        else:
            plaintext=plaintext+'Z'
    no_of_rows=int(len(plaintext)/no_of_columns)
    matrix=[['\n']*no_of_columns]*no_of_rows

    index=0
    for i in range(no_of_columns):
        for j in range(no_of_rows):

            matrix[i][j]=plaintext[index]
            index+=1

    print(matrix)

    for j in range(no_of_columns):
        for i in range(no_of_rows):
            encrypted_text=encrypted_text+matrix[j][i]

    return encrypted_text

def main():
    print("Welcome to Keyed and Keyless Transpostion Cipher: ")
    print("1. Keyless - Railfence Cipher (Encryption and Decryption_")
    print("2. Keyless - Columnar Cipher (Encryption and Decryption)")
    print("3. Keyed - Columnar Cipher (Encryption and Decryption)")
    print("4. Keyed -  Double Transposition (Ciphers Encryption and Decryption)")

    while(1):
        choice= int(input("Enter your Choice from above Ciphers:"))

        if choice == 1:
            plaintext=str(input(" Enter a Message for in Encryption : ")).upper().replace(" ","")
            rows=int(input(" Enter No. of Rows for Encrytion : "))
            encrypted_text=encryptRailFence(plaintext,rows)
            print("\n Encrypted Text : ",encrypted_text)
            decrypted_text=decryptRailFence(encrypted_text,rows)
            print("\n Decrypted Text : ",decrypted_text)

        elif choice == 2:
            plaintext=str(input(" Enter a Message for in Encryption : ")).upper().replace(" ","")
            columns=int(input(" Enter No. of Columns for Encrytion : "))
            encrypted_text=encrypt_columnar(plaintext,columns=columns,transposition_type="keylessColumnar")
            print("\n Encrypted Text : ",encrypted_text)
            decrypted_text=decrypt_columnar(encrypted_text,columns=columns,transposition_type="keylessColumnar")
            print("\n Decrypted Text : ",decrypted_text)

        elif choice == 3:

            plaintext= str(input(" Enter a Message for Encryption : ")).upper().replace(" ","")
            encryption_key=int(input(" Enter a key for Encryption : "))
            encrypted_text=encrypt_columnar(plaintext,key=encryption_key,transposition_type="keyedColumnar")
            decryption_key=decrypt_key(encryption_key)
            print("\n Encrypted Text : ",encrypted_text)
            decrypted_text=decrypt_columnar(encrypted_text,key=decryption_key,transposition_type="keyedColumnar")
            print("\n Decrypted Text: ",decrypted_text)
            #print("Decryption Key",decryption_key)

        elif choice == 4:

            plaintext= str(input(" Enter a Message for Encryption : ")).upper().replace(" ","")
            encryption_key=int(input(" Enter a key for Encryption : "))
            encrypted_text=encrypt_columnar(plaintext,key=encryption_key,transposition_type="doubletranspostion")
            decryption_key=decrypt_key(encryption_key)
            print("\n Encrypted Text : ",encrypted_text)
            decrypted_text=decrypt_columnar(encrypted_text,key=decryption_key,transposition_type="doubletranspostion")
            print("\n Decrypted Text: ",decrypted_text.replace(" ",""))
        else:
            print("Invalid Choice Exitinig the Cryptosystem.")
            sys.exit()

if __name__=="__main__":
    main()


"""
Program that reproduces the caesar cipher
It can encode a word with an amount of shift in the alphabet
and decode it with the same amount of shift in the
alphabet

For example : abcdefghijklmnopqrstuv
if we decide that the shift is 3, a will become the next 3 letters which is d, b will be e, etc.
To decode, we must use the same amount of shift and go back.
"""

# import the art module
import art

# prints the logo from the art module
print(art.logo)

print("Welcome To The Caesar Cipher ! ")

# alphabet list
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
            'y', 'z']
# boolean variable for the while loop
should_continue = True

# function with inputs to encode and decode a word
# start_text : text that we want to encode or decode
# shift_amount : number of time we shift
# cipher_direction : whether we want to encode or decode

def caesar(start_text, shift_amount, cipher_direction):

    # empty string to store the word
    end_text = ""

    # if the cipher_direction is decode : we will shift back, so we multiply the shift_amount by -1
    if cipher_direction == "decode":
        shift_amount *= -1

    # for loop : we will go through all the letters in the start_text
    for char in start_text:

        # we look if the character is not a symbol before entering the block of code
        # must be letter to execute the block of code in the if statement
        if char in alphabet:
            # we look for the index of the letter
            position = alphabet.index(char)
            new_position = position + shift_amount
            end_text += alphabet[new_position]
        # if the character is a symbol, we simply add it in the end_text
        else:
            end_text += char
    # print the text
    print(f"The {cipher_direction}d text is {end_text}")

while should_continue:
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n").lower()
    if direction == "encode" or direction == "decode":
        text = input("Type your message : ").lower()
        shift = int(input("Type the shift number : "))
        shift = shift % 26

        caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
        result = input("Type 'yes' if you want to continue. Otherwise type 'no'. ").lower()
        if result == "no":
            should_continue = False
            print("Thank you for using this program. ")

    # if the user writes anything else than "encode" and "decode"
    else:
        print("Error : Try again with an appropriate command")


from caluma.caluma_analytics.schema import (
    RemoveAnalyticsField,
    RemoveAnalyticsTable,
    SaveAnalyticsField,
    SaveAnalyticsTable,
)
from caluma.caluma_core.mutation import Mutation
from caluma.caluma_core.permissions import (
    BasePermission,
    object_permission_for,
    permission_for,
)
from caluma.caluma_form.schema import SaveDocument, SaveDocumentAnswer
from caluma.caluma_workflow.schema import (
    CancelCase,
    CompleteWorkItem,
    RedoWorkItem,
    ReopenCase,
    SaveCase,
)
from caluma.extensions.common import (
    get_cases_for_user,
    get_cases_for_user_by_circulation_invite,
)
from caluma.extensions.settings import settings

class MySAGWPermission(BasePermission):
    def _is_admin(self, info):
        groups = info.context.user.groups
        return "admin" in groups

    def _is_sagw(self, info):
        groups = info.context.user.groups
        return "sagw" in groups

    def _is_admin_or_sagw(self, info):
        return self._is_admin(info) or self._is_sagw(info)

    def _can_access_case(self, info, case):
        case_ids = get_cases_for_user(info.context.user)
        return str(case.pk) in case_ids

    def _is_own(self, info, instance):
        return instance.created_by_user == info.context.user.username

    @permission_for(Mutation)
    @object_permission_for(Mutation)
    def has_permission_fallback(self, mutation, info, instance=None):
        return self._is_admin_or_sagw(info) or (
            bool(instance) and self._is_own(info, instance)
        )

    def _get_case_for_doc(self, document):
        """
        Get case from document.

        Check for case at these locations in this order:
        1. case on the document
        2. case on the document.work_item
        3. case on the document.family
        4. case on the document.family.work_item
        """

        def case_lookup(obj, *lookups):
            for lookup in lookups:
                obj = getattr(obj, lookup, None)
            return obj

        case_lookups = [
            (document, "case"),
            (document, "work_item", "case"),
            (document.family, "case"),
            (document.family, "work_item", "case"),
        ]

        for lookup_path in case_lookups:
            case = case_lookup(*lookup_path)
            if case:
                return case.family

    @permission_for(SaveCase)
    @object_permission_for(SaveCase)
    @permission_for(SaveDocument)
    @object_permission_for(SaveDocument)
    @permission_for(SaveDocumentAnswer)
    def has_permission_for_save_case_save_document(self, mutation, info, obj=None):
        return True

    @object_permission_for(SaveDocumentAnswer)
    def has_permission_for_save_document_answer(self, mutation, info, answer):
        if (
            not answer.document.family.form.is_published
            and not getattr(answer.document.family, "case", False)
            and not getattr(answer.document.family, "work_item", False)
            and self._is_own(info, answer.document.family)
        ):
            # is floating row-document
            return True

        case = self._get_case_for_doc(answer.document)

        if self._is_admin(info):
            return True
        if self._is_sagw(info):
            work_item = (
                answer.document.family.work_item
                if hasattr(answer.document.family, "work_item")
                else case.work_items.filter(status="ready")
                .exclude(task__slug__in=settings.APPLICANT_TASK_SLUGS)
                .first()
            )
            if work_item or self._can_access_case(info, case):
                return True

        if not (
            self._can_access_case(info, case)
            or self._is_own(info, answer.document.family)
        ):
            return False

        work_item = (
            answer.document.family.work_item
            if hasattr(answer.document.family, "work_item")
            else case.work_items.filter(
                task__slug__in=["submit-document", "revise-document"],
                status="ready",
            ).first()
        )

        return work_item is not None and work_item.status == "ready"

    @permission_for(CompleteWorkItem)
    @permission_for(CancelCase)
    def has_permission_for_complete_work_item(self, mutation, info):
        return True

    @object_permission_for(CompleteWorkItem)
    def has_object_permission_for_complete_work_item(self, mutation, info, work_item):
        return (
            self._is_admin_or_sagw(info)
            or (
                (
                    self._can_access_case(info, work_item.case)
                    or self._is_own(info, work_item)
                )
                and work_item.task.slug in settings.APPLICANT_TASK_SLUGS
            )
            or (
                str(work_item.case.pk)
                in get_cases_for_user_by_circulation_invite(info.context.user)
                and work_item.task.slug in settings.CIRCULATION_TASK_SLUGS
            )
        )

    @object_permission_for(CancelCase)
    def has_permission_for_cancel_case(self, mutation, info, case):
        return self._is_admin_or_sagw(info) or (
            (self._can_access_case(info, case) or self._is_own(info, case))
            and case.work_items.filter(
                status="ready",
                task__slug="submit-document",
            ).exists()
        )

    @permission_for(RedoWorkItem)
    @object_permission_for(RedoWorkItem)
    @permission_for(ReopenCase)
    @object_permission_for(ReopenCase)
    def has_permission_for_redo_workitem_reopen_case(
        self,
        mutation,
        info,
        instance=None,
    ):
        return self._is_admin_or_sagw(info)

    @permission_for(RemoveAnalyticsField)
    @object_permission_for(RemoveAnalyticsField)
    @permission_for(RemoveAnalyticsTable)
    @object_permission_for(RemoveAnalyticsTable)
    @permission_for(SaveAnalyticsField)
    @object_permission_for(SaveAnalyticsField)
    @permission_for(SaveAnalyticsTable)
    @object_permission_for(SaveAnalyticsTable)
    def has_permission_for_analytics(self, mutation, info, _=None):
        return self._is_admin_or_sagw(info)


g_version=1.0
g_ibig=640000
g_small=1.0e-16
g_big  =1.0e+21

g_name_len_max=255
g_xdir=1
g_ydir=2

file_in = 0
flie_out = 0
CHUNK_LEFT   =1
CHUNK_RIGHT  =2
CHUNK_BOTTOM =3
CHUNK_TOP    =4
EXTERNAL_FACE=-1

FIELD_DENSITY0   = 0
FIELD_DENSITY1   = 1
FIELD_ENERGY0    = 2
FIELD_ENERGY1    = 3
FIELD_PRESSURE   = 4
FIELD_VISCOSITY  = 5
FIELD_SOUNDSPEED = 6
FIELD_XVEL0      = 7
FIELD_XVEL1      = 8
FIELD_YVEL0      = 9
FIELD_YVEL1      =10
FIELD_VOL_FLUX_X =11
FIELD_VOL_FLUX_Y =12
FIELD_MASS_FLUX_X=13
FIELD_MASS_FLUX_Y=14
NUM_FIELDS       =15

CELL_DATA     = 1
VERTEX_DATA   = 2
X_FACE_DATA   = 3
y_FACE_DATA   = 4

SOUND = 1
X_VEL = 2
Y_VEL = 3
DIVERG= 4

g_rect=1
g_circ=2
g_point=3

parallel={
        "boss": False,
        "parallel": False,
        "task": 1,
        "max_task": 1,
        "boss_task": 1
        }


'''
el proposito de la decomposicion es solucionar un problema
diviendolo en varias partes.

este caso el cuerpo y las acciones de una persona
'''
class persona:
    def __init__(self, nombre, apellido, edad):
        self.nombre = nombre
        self.apellido = apellido
        self.edad = edad
        self.redSocial = redSocial(True, True)

    def datos(self):
        print(f'''
        nombre: {self.nombre}
        apellido: {self.apellido}
        edad: {self.edad}
        ''')

class redSocial:
    def __init__(self, tieneRS, estado):
        self.tieneRS = tieneRS
        self.estado = estado
        self.numeroSeguidores = 0
        self.seguidores = list()
        self.fechaRegistro = str()

    def agregarSeguidor(self, usuario):
        self.numeroSeguidores +=1
        self.seguidores.append(usuario)


'''

It is the method used to calculate threshold for smaller region.
Here, the threshold is not global for every pixel.
If the lightning condition varies from point to point, we use adaptive thresholding.

'''

import cv2 as cv

img = cv.imread('standard_test_images/sudoku.jpg', 0)
img = cv.resize(img, (512, 512))
_, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2) # 11 is block size: size of neigh. area
                                                                                # 2 IS CONST. TO BE SUB. FROM MEAN
th3 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)

gray = cv.copyMakeBorder(img, 20, 20, 20, 20, cv.BORDER_REPLICATE)
th4 = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]

cv.imshow("image", img)
cv.imshow("Thresh Binary", th1)
cv.imshow("ADAPTIVE_THRESH_MEAN", th2)
cv.imshow("ADAPTIVE_THRESH_GAUSSIAN", th3)
cv.imshow("Naya", th4)

cv.waitKey(0)
cv.destroyAllWindows()


import tweepy # EASIEST Twitter API
import requests
import ast
from keys import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET, YODA_KEY

# Tweepy authentication
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

# mentions_timeline will return all the tweets you are mentioned in
# count = 1 will only grab the most recent one
mentions = api.mentions_timeline(count=1)

# Let's get rid of the tag so we only have the sentence we want to translate
mention = str(mentions[0].text).replace("@CodesCarla ", "")

#Everything we need for the Yoda Translator API
#https://rapidapi.com/orthosie/api/yoda-translator
url = "https://yodish.p.rapidapi.com/yoda.json"

querystring = {"text":mention}

payload = ""
headers = {
    'x-rapidapi-host': "yodish.p.rapidapi.com",
    'x-rapidapi-key': YODA_KEY ,
    'content-type': "application/x-www-form-urlencoded"
    }
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

#Parsing the response
strResp=response.text.encode('ascii', 'ignore')
dictResp = ast.literal_eval(strResp)
print(dictResp["contents"]["translated"])

#Posting the translated tweet
api.update_status(dictResp["contents"]["translated"])


#!/usr/bin/python

# Ejercicio 5

import math # Para usar sus funciones

fichero = open('notas.txt','r')
notas = []

for line in fichero: # Añadimos las notas a la lista
    linea_separada = line.split('')
    notas.append(linea_separada[3])

fichero.clase()

print('La nota máxima fue ',max(notas),' y la mínima fue ',min(notas))
print('La nota media fue ',sum(notas)/len(notas))


"""
Задача 10: На столе лежат n монеток. Некоторые из них лежат вверх
решкой, а некоторые – гербом. Определите минимальное число
монеток, которые нужно перевернуть, чтобы все монетки были
повернуты вверх одной и той же стороной. Выведите минимальное
количество монет, которые нужно перевернуть.

5 -> 1 0 1 1 0
2
"""
import os
import random
os.system('cls')

def user_input():  # безопасный ввод
    while True:
        try:
            # пока будут волшебные числа, после
            number = int(input("Введите число от 1 до 1000: "))
            # поправлю на передаваемый в функцию элемент
            if not number in range(1, 1001):
                raise ValueError
            return number

        except ValueError:
            print('Вводите только числа от 1 до 1000')

def random_list(n):  # Список случайных чисел, очень не хотелось их вбивать руками))
    N = []
    for _ in range(n):
        N.append(random.randint(0, 1))
    return N

def print_zero_or_one_counter(N):  # не удержался и тоже выделил в функцию)
    zero_counter = one_counter = 0
    for i in N:
        if i == 0:
            zero_counter += 1
        else:
            one_counter += 1
    print(min(one_counter, zero_counter))

n = user_input()
N = random_list(n)
print(N)
print_zero_or_one_counter(N)

# print(*[(s if (s <= n // 2) else (n - s)) for arr in [[int(x) for x in input().split()]] for s in [sum(arr)] for n in [int(len(arr))]])
# это мой ребёнок написал мне на вырост решение в одну строку) буду разбирать, надо соответствовать)


from __future__ import print_function

from math import sqrt, ceil, isinf, floor, log
from pqsec import optimize_attack, svp_classical, svp_quantum, svp_plausible, primal_cost, dual_cost
from failure_prob_pke import heuristic_failure_prob_pke, exact_failure_prob_pke
from approx_distr import approximate_dgauss
from discrete_distr import pdf_product, dgauss, sym_binomial, distr_to_str, nonnegative_half, bits_needed_to_sample
from renyi import opt_renyi_bound
import argparse
import progressbar  # install using pip install progressbar2

def minimize_sum(n):
  """Finds a pair of integers so that their product is at least n and the sum is minimal.

  Args:
     n: An integer.

  Returns:
    A pair of integers.
  """
  opt_k1, opt_k2 = 1, n
  k1 = int(floor(sqrt(n))) + 1
  while k1 > 0 and k1 + n * 1. / k1 < opt_k1 + opt_k2:
    k2 = int(ceil(n * 1. / k1))
    if k1 + k2 < opt_k1 + opt_k2:
      opt_k1, opt_k2 = k1, k2
    k1 -= 1
  return opt_k1, opt_k2

def estimate_cost(q, n, max_m, s, attacks):
  """Finds attack costs (classical, quantum, plausible quantum) for an LWE instance.

  Args:
    q: LWE modulus.
    n: LWE dimension.
    max_m: maximum number of samples
    s: standard deviation of the error distribution
    attacks: A vector specifying types of attacks to consider (svp_classical,
        svp_quantum, svp_plausible)

  Returns:
    A triple of log_2 of the costs of three attacks: classical, quantum, and
    plausible quantum.
    Infinity if analysis is not run.
  """
  r = [float('inf')] * 3
  for cost in [primal_cost, dual_cost]:
    for i, attack in enumerate(attacks):
      _, _, c = optimize_attack(q, n, max_m, s, cost, attack)
      r[i] = min(r[i], c)
  return r

def find_opt_distr(sigma, samples, ubits, cost_cl, cost_pq, cost_pp):
  """Finds an optimal distribution approximating rounded continuous Gaussian.

  Args:
    sigma: The standard deviation of the target (rounded) Gaussian.
    samples: The total number of samples drawn by both parties combined.
    ubits: The bound on the number of uniform bits required for sampling.
    cost_cl, cost_pq, cost_pp: Estimated costs of the rounded Gaussian.

  Returns:
    Four-tuple consisting of the distribution and the cost triplet.
  """
  cost_cl_opt, d, _ = approximate_dgauss(
      sigma, samples, cost_cl, None, ubits, quiet=True)

  sym_d = pdf_product(d, {+1: .5, -1: .5})

  dg = dgauss(sigma)

  _, cost_pq_opt = opt_renyi_bound(-cost_pq * log(2), sym_d, dg, samples)
  _, cost_pp_opt = opt_renyi_bound(-cost_pp * log(2), sym_d, dg, samples)

  return [
      sym_d, cost_cl_opt, -cost_pq_opt / log(2), -cost_pp_opt / log(2)
  ]

def find_binomial_cost(sigma, samples, cost_cl, cost_pq, cost_pp):
  """Estimates the cost of replacing a rounded Gaussian with a binomial.

  Args:
    sigma: The standard deviation of the Gaussian.
    samples: The total number of samples drawn by Alice and Bob.
    cost_cl, cost_pq, cost_pp: Estimated costs of the rounded Gaussian.

  Returns:
    Four-tuple consisting of the distribution and the cost triplet.
  """

  dg = dgauss(sigma)
  # The binomial is defined as B(2*z, .5) - z.
  sb = sym_binomial(2 * sigma**2)

  _, cost_cl_binomial = opt_renyi_bound(-cost_cl * log(2), sb, dg, samples)
  _, cost_pq_binomial = opt_renyi_bound(-cost_pq * log(2), sb, dg, samples)
  _, cost_pp_binomial = opt_renyi_bound(-cost_pp * log(2), sb, dg, samples)

  return [
      sb, -cost_cl_binomial / log(2), -cost_pq_binomial / log(2),
      -cost_pp_binomial / log(2)
  ]

def minimize_bandwidth(classical_lb,
                       quantum_lb,
                       plausible_lb,
                       ubits,
                       sigmas,
                       prob_of_failure=128,
                       agree_bits=256):
  """Searches the parameter space to minimize bandwidth. Prints to stdout.

  Args:
    classical_lb: A lower bound on classical security (None if undefined).
    quantum_lb: A lower bound on quantum security (None if undefined).
    plausible_lb: A conservative lower bound on security (None if undefined).
    ubits: The bound on the number of uniform bits required for sampling.
    sigmas: The list of sigmas to explore.
    prob_of_failure: Minimally acceptable probability of failure.
    agree_bits: Target key length.
  """

  def check_costs(cost_cl, cost_pq, cost_pp):
    """Checks whether the costs satisfy lower bounds.

    Args:
      cost_cl, cost_pq, cost_pp: Costs as binary logarithms.

    Returns:
      True if lower bounds are satisfied.
    """
    return ((classical_lb is None or cost_cl >= classical_lb) and
            (quantum_lb is None or cost_pq >= quantum_lb) and
            (plausible_lb is None or cost_pp >= plausible_lb))

  def print_intermediate_result(opt_d, qlog, n, w, m_bar, n_bar, sigma,
                                bandwidth, heuristic_pr_failure,
                                actual_pr_failure, costs_gauss, costs_opt):
    """A printer helper.
    """
    print(distr_to_str(nonnegative_half(opt_d)))
    print('q = 2**{}, n = {}, w = {}, mbar x nbar = {} x {}, sigma = {:.3f}:'.format(
        qlog, n, w, m_bar, n_bar, sigma), end='')
    print('bandwidth = {:.2f} kB,'.format(bandwidth / (8. * 1000))),

    print('heuristic Pr of failure = {:.1f}, '
          'actual Pr of failure = {:.1f},'.format(
              log(heuristic_pr_failure) / log(2),  # can be 0
              log(actual_pr_failure) / log(2)),    # can be 0
          end='')

    formatted_costs = ', '.join(
        '{:.1f}'.format(c) for c in costs_gauss if not isinf(c))
    print('security = [{}],'.format(formatted_costs), end='')

    formatted_costs_opt = ', '.join(
        '{:.1f}'.format(c) for c in costs_opt if not isinf(c))
    print('security after reduction = [{}],'.format(formatted_costs_opt), end='')

    bits = bits_needed_to_sample(opt_d)
    print('distribution on [{}, {}], {} bits to sample'.format(
        min(opt_d.iterkeys()), max(opt_d.iterkeys()), bits))

    print("Parameters for print_tables_pke.py: 'sigma': {:.3f}, 'n': {}, "
           "'m_bar':{}, 'n_bar':{}, 'q': {}, 'B': {}, 'key_len': {}, "
           "'rand_bits': {}, 'sec_base': {:.0f}".format(sigma, n, m_bar, n_bar,
                                                        qlog, w, agree_bits,
                                                        bits, min(costs_gauss)))

  # Main body of minimize_bandwidth starts here.

  attacks = [svp_classical]
  if quantum_lb is not None:
    attacks.append(svp_quantum)
  if plausible_lb is not None:
    attacks.append(svp_plausible)

  qlog_list = range(8, 17)  # + [24, 32]
  n_list = range(240, 1400, 16)

  min_bandwidth_bits = 8 * 60000  # Upper bound on the bandwidth in bits.

  for qlog in qlog_list:
    print('q = 2**{}'.format(qlog))
    for n in progressbar.progressbar(n_list):
      prev_k = None  # number of columns already considered
      # The number of bits to extract; can range between 1 and be as large as
      # the modulus.
      w_list = range(1, 1 + qlog)
      for w in w_list:
        # number of coordinates needed to achieve agree_bits
        required_coordinates = (agree_bits + w - 1) / w
        # number of rows, columns
        m_bar, n_bar = minimize_sum(required_coordinates)
        if (m_bar, n_bar) == prev_k:
          # Nothing to do: w is larger than before but the number
          # of columns is the same.
          continue
        prev_k = (m_bar, n_bar)

        max_m = n + max(m_bar, n_bar)
        bandwidth = (m_bar + n_bar) * qlog * n
        if bandwidth > min_bandwidth_bits:
          continue

        for sigma in sigmas:
          # Quick-and-dirty check on the probability of failure
          heuristic_pr_failure = heuristic_failure_prob_pke(
              2**qlog, n, sigma, w, agree_bits)
          if heuristic_pr_failure > 2**-prob_of_failure:  # very loose bound
            continue

          samples = 2 * (m_bar + n_bar) * n + required_coordinates

          # Attack complexity of a single instantiation of the LWE problem.
          costs_gauss_single_shot = estimate_cost(2**qlog, n, max_m, sigma,
                                                  attacks)
          # Taking into account that there are m_bar + n_bar simultaneous instances that all must hold.
          costs_gauss = [
              c - log(m_bar + n_bar) / log(2)
              for c in costs_gauss_single_shot
          ]

          # Checking upper bounds on costs, before trying to find an optimal
          # approximation.
          if not check_costs(*costs_gauss):
            continue

          [opt_d, cost_cl_opt, cost_pq_opt, cost_pp_opt] = find_opt_distr(
              sigma, samples, ubits, *costs_gauss)

          costs_opt = [cost_cl_opt, cost_pq_opt, cost_pp_opt]

          if not check_costs(*costs_opt):
            continue

          actual_pr_failure = exact_failure_prob_pke(opt_d, 2**qlog, n, w,
                                                     agree_bits)
          if actual_pr_failure > 2**-prob_of_failure:
            continue

          min_bandwidth_bits = bandwidth
          print_intermediate_result(opt_d, qlog, n, w, m_bar, n_bar, sigma,
                                    bandwidth, heuristic_pr_failure,
                                    actual_pr_failure, costs_gauss, costs_opt)

  return

def main():
  sigmas = [2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7,
            2.75, 2.8, 3.]
  small_sigmas = [1.35, 1.4, 1.45]

  parser = argparse.ArgumentParser(description='Sweep parameter space.')
  parser.add_argument(
      '-level', type=int, nargs='?', required=False, help='Level')
  args = parser.parse_args()

  if args.level is None or args.level == 1:
    print('### NIST Level 1 ###')
    minimize_bandwidth(143, 85, 85, 16, sigmas, 128, agree_bits=128)
    print()

  if args.level is None or args.level == 3:
    print('### NIST Level 3 ###')
    minimize_bandwidth(207, 116.5, 116.5, 16, sigmas, 128, agree_bits=192)
    print()

  if args.level is None or args.level == 5:
    print("### NIST Level 5 ###")
    minimize_bandwidth(272, 149, 149, 16, small_sigmas, prob_of_failure=250, agree_bits=256)
    print()

if __name__ == '__main__':
  main()


import os.path
import re
import struct
from enum import Enum
import pydicom
import numpy as np
import tifffile
from scipy.ndimage import zoom

from common import rescale

pattern = re.compile(r'/[0-9]{3,}/', re.ASCII)

SRC_PATH = r'./../../BrowlerEngine/Assets/raw/'
OUT_PATH = r'./../../BrowlerEngine/Assets/DataSets'
OUT_EXT = r'.dat'

DATASET_NAME = r'VHF-Head'

SAMPLE_DIM = (1, 1, 1)  # x, y, z
ROTATE_X = 1
ROTATE_Y = 2

class DATA_SET_TYPE(Enum):
    TIFF_SLICES = 0
    DICOM_SLICES = 1

TYPE = DATA_SET_TYPE.DICOM_SLICES

def load_tiff_list(file_list):
    return tifffile.imread(file_list).astype(np.float32)

def load_dicom_list(file_list):
    zDim = len(file_list)
    arr = None
    for i, p in enumerate(file_list):
        d = pydicom.read_file(p)
        if arr is None:
            arr = np.ndarray((zDim, *d.pixel_array.shape), dtype=np.float32)
        arr[i, :, :] = d.pixel_array

    return arr

load_functions = {
    DATA_SET_TYPE.TIFF_SLICES: load_tiff_list,
    DATA_SET_TYPE.DICOM_SLICES: load_dicom_list,
}

def main():
    folder = os.path.join(SRC_PATH, DATASET_NAME)
    assert os.path.isdir(folder)
    for dirpath, dirnames, filenames in os.walk(folder):

        files, ext = zip(*sorted((os.path.join(dirpath, f), ext) for f, ext in (os.path.splitext(filename) for filename in filenames) if len(f) > 2 and f[-3:].isnumeric()))
        assert all(ext[i] == ext[i+1] for i in range(len(ext) - 1)), "Not the same file extension"
        files = [file + e for file, e, in zip(files, ext)]
        arr = load_functions[TYPE](files)  # type: np.ndarray

        dims = np.array(list(reversed(SAMPLE_DIM)))
        min_dim = dims.min(axis=None)
        scale = dims / min_dim
        max = arr.max(axis=None)
        min = arr.min(axis=None)
        if not all(dims[i] == dims[i+1] for i in range(len(dims) - 1)):
            res = np.ndarray((scale * np.array(arr.shape)).astype(np.int32), dtype=np.float32)
            zoom(arr, scale, output=res)
        else:
            res = arr
        np.subtract(res, min, out=res)
        np.divide(res, max - min, out=res)
        rescale(res)
        res[0, :, :] = 0
        res[:, 0, :] = 0
        res[:, :, 0] = 0
        res[-1, :, :] = 0
        res[:, -1, :] = 0
        res[:, :, -1] = 0

        if ROTATE_X:
            res = np.rot90(res, ROTATE_X, (0, 1))

        if ROTATE_Y:
            res = np.rot90(res, ROTATE_Y, (0, 2))

        out_path = os.path.join(OUT_PATH, DATASET_NAME + OUT_EXT)
        with open(out_path, 'wb') as out:
            out.write(struct.pack('<H', res.shape[2]))
            out.write(struct.pack('<H', res.shape[1]))
            out.write(struct.pack('<H', res.shape[0]))

            out.write(np.ascontiguousarray(res, np.dtype('<i2')).tobytes())

        print("Written to {}".format(out_path))

if __name__ == '__main__':
    main()


from pyroute2 import IPRoute, netns, NetNS, netlink
import socket
import logging
import time

class SimpleNetlink(object):
    def __init__(self, namespace=None):
        self.ipr = IPRoute()
        self._log = logging.getLogger("SimpleNetlink")
        self._current_namespace = namespace
        self._previous_namespace_instance = None
        self._previous_namespace = None
        self._log.level = logging.DEBUG
        self._supported_virtual_interface_types = ["ipvlan", "tagged"]

    def reset(self):
        self._current_namespace = None
        self._previous_namespace_instance = None
        self._previous_namespace = None
        if self.ipr:
            self.ipr.close()
        self.ipr = IPRoute()

    def get_interface_index(self, ifname):
        res = self.ipr.link_lookup(ifname=ifname)
        if len(res) == 1:
            return res[0]
        else:
            if len(res) == 0:
                raise ValueError(
                    f"no result found for {ifname} in namespace {self.get_current_namespace_name()}"
                )
            else:
                self._log.error(
                    f"multiple results found for {ifname}: {res} -> returning first"
                )
                return res[0]

    def create_namespace(self, namespace):
        ns = netns.create(namespace)

        self.set_current_namespace(namespace)
        idx = self.get_interface_index("lo")
        self.ipr.link("set", index=idx, state="up")

        self.restore_previous_namespace()

    def set_current_namespace(self, namespace):
        if not namespace:
            if self._current_namespace:
                self._log.info(f"close {self._current_namespace}")
                self.ipr.close()
            self._previous_namespace = self._current_namespace
            self.ipr = IPRoute()
            self._current_namespace = namespace
        elif namespace not in self.get_namespaces():
            self._log.debug(
                f"{namespace} does not exist, implicitly creating namespace {namespace}"
            )
            self.create_namespace(namespace)
        if namespace:
            self._previous_namespace = self._current_namespace
            if self.ipr:
                self.ipr.close()
            self.ipr = NetNS(namespace)
            self._current_namespace = namespace
        self._log.debug(
            f"switched namespace from {self._previous_namespace} to {self._current_namespace}"
        )
        return True

    def restore_previous_namespace(self):
        tmp_i = self.ipr
        tmp = self._current_namespace
        self.ipr = self._previous_namespace_instance
        self._current_namespace = self._previous_namespace
        self._previous_namespace_instance = tmp_i
        self._previous_namespace = tmp
        self._log.debug(f"restored previous namespace {self._current_namespace}")
        return True

    def delete_namespace(self, namespace):
        if namespace in self.get_namespaces():
            self.set_current_namespace(namespace)
            for (
                interface_name,
                interface_cfg,
            ) in self.get_network_interfaces_info().items():
                for ipv4 in interface_cfg.get("ipv4", []):
                    self.interface_delete_ipv4(interface_name, ipv4["prefix"])
                for ipv6 in interface_cfg.get("ipv6", []):
                    self.interface_delete_ipv4(interface_name, ipv6["prefix"])
            self.ipr.close()
            self.ipr.remove()
            self.restore_previous_namespace()

            # ns = NetNS(namespace)

            # ns.close()
            # ns.remove()

            self._log.debug(f"removed namespace {namespace}")
            if namespace == self._current_namespace:
                self.set_current_namespace(None)
            time.sleep(
                0.1
            )  # give kernel some time, this workarounds various 'already existing' problams
        else:
            self._log.debug(
                f"cannot remove non existing namespace {namespace} -> ignoring request"
            )

    def get_current_namespace_name(self):
        return self._current_namespace

    def get_namespaces(self):
        return list(netns.listnetns())

    def find_interface_in_all_namespaces(self, interface_name):
        idx = None
        namespace = self.get_current_namespace_name()
        try:
            self.set_current_namespace(None)
            idx = self.get_interface_index(interface_name)
            namespace = self.get_current_namespace_name()
            self.restore_previous_namespace()
        except ValueError:
            for namespace in self.get_namespaces():
                self._log.debug(f"switching namespace to {namespace}")
                self.set_current_namespace(namespace)
                try:
                    idx = self.get_interface_index(interface_name)
                    break
                except:
                    pass
                self.restore_previous_namespace()
        if idx:
            self._log.debug(
                f"found interface {interface_name} in namespace {namespace} with index {idx}"
            )
        else:
            self._log.debug(f"cannot find interface {interface_name} in any namespace")
        return (namespace, idx)

    def __create_tagged(self, interface_name, options={}, **kwargs):
        if kwargs.get("parent_interface"):
            (base_namespace, base_idx) = self.find_interface_in_all_namespaces(
                kwargs.get("parent_interface")
            )
            self._log.debug(
                f"found parent_interface {kwargs.get('parent_interface')} in namespace {base_namespace}"
            )
            if not kwargs.get("vlan_id"):
                raise ValueError(
                    "vlan_id not specified -> cannot create tagged vlan interface without"
                )
            else:
                self._log.debug(
                    f"creating tagged interface {interface_name} with tag on base_interface"
                )

            self.set_current_namespace(base_namespace)

            self.ipr.link(
                "add",
                ifname=interface_name,
                kind="vlan",
                link=base_idx,
                vlan_id=int(kwargs.get("vlan_id")),
                **options,
            )
            idx = self.get_interface_index(interface_name)
            namespace = kwargs.get("namespace")
            if namespace:
                if kwargs.get("namespace") not in self.get_namespaces():
                    self.create_namespace(namespace)
                self.ipr.link("set", index=idx, net_ns_fd=namespace)
                self.set_current_namespace(namespace)
                idx = self.get_interface_index(interface_name)
            else:
                self.ipr.link("set", index=idx, net_ns_pid=1)
            return (namespace, idx)
        else:
            raise ValueError(
                f"parent_interface not specified for vlan interface {interface_name}"
            )

    def __create_ipvlan(self, interface_name, options={}, **kwargs):
        ipvlan_modes = {
            "l2": 0,
            "l3": 1,
            "l3s": 2,
        }
        if kwargs.get("parent_interface"):
            (base_namespace, base_idx) = self.find_interface_in_all_namespaces(
                kwargs.get("parent_interface")
            )
            self._log.debug(f"found parent_interface in namespace {base_namespace}")
            self.set_current_namespace(base_namespace)

            self.ipr.link(
                "add",
                ifname=interface_name,
                kind="ipvlan",
                link=base_idx,
                ipvlan_mode=ipvlan_modes[
                    "l2"
                ],  # l2 mode so arp can be handled from namespace
                **options,
            )
            idx = self.get_interface_index(interface_name)
            namespace = kwargs.get("namespace")
            if namespace:
                self.set_current_namespace(namespace)
                self.set_current_namespace(base_namespace)
                self.ipr.link("set", index=idx, net_ns_fd=kwargs.get("namespace"))
                self.set_current_namespace(namespace)
            else:
                self.ipr.link("set", index=idx, net_ns_pid=1)
                self.set_current_namespace(None)
            idx = self.get_interface_index(interface_name)
            return (namespace, idx)
        else:
            raise ValueError(
                f"parent_interface not specified for ipvlan interface {interface_name}"
            )

    def create_interface(self, interface_name, **kwargs):
        options = {}
        if kwargs.get("mtu"):
            options["mtu"] = kwargs.get("mtu")

        f = getattr(self, f"_SimpleNetlink__create_{kwargs.get('type')}")
        if f:
            (namespace, idx) = f(interface_name, options, **kwargs)
            if kwargs.get("link_state", "").lower() == "down":
                self.ipr.link("set", index=idx, state="down")
            else:
                self._log.debug(
                    f"enabling interface {interface_name} in namespace {namespace}"
                )
                self.ipr.link("set", index=idx, state="up")
            return (namespace, idx)
        else:
            raise ValueError(f"type {kwargs.get('type')} not implemented")

    def _resolve_interface_type_by_index(self, idx):
        info = {}
        for attr in self.ipr.link("get", index=idx)[0]["attrs"]:
            if attr[0] == "IFLA_LINKINFO":
                for inner_attr in attr[1]["attrs"]:
                    if inner_attr[0] == "IFLA_INFO_KIND":
                        info["type"] = inner_attr[1]
                        if info["type"] == "vlan":
                            info["type"] = "tagged"
                    if info["type"] == "tagged" and inner_attr[0] == "IFLA_INFO_DATA":
                        for ii in inner_attr[1]["attrs"]:
                            if ii[0] == "IFLA_VLAN_ID":
                                info["vlan_id"] = ii[1]
                break
        return info

    def get_interface_mtu(self):
        for attr in self.ipr.link("get", index=idx)[0]["attrs"]:
            if attr[0] == "IFLA_MTU":
                return attr[1]

    def get_interface_ip(self, interface, **kwargs):
        namespace, idx = self.find_interface_in_all_namespaces(interface)
        self.set_current_namespace(namespace)
        retval = {"ipv4": [], "ipv6": []}

        for entry in self.ipr.get_addr(index=idx):
            for attr in entry["attrs"]:
                if attr[0] == "IFA_ADDRESS":
                    if entry["family"] == 2:
                        retval["ipv4"].append(f"{attr[1]}/{entry['prefixlen']}")
                    if entry["family"] == 10:
                        retval["ipv6"].append(f"{attr[1]}/{entry['prefixlen']}")
                break

        return retval

    def get_interface_ipv4(self, interface):
        return self.get_interface_ip(interface)["ipv4"]

    def get_interface_ipv6(self, interface):
        return self.get_interface_ip(interface)["ipv6"]

    def ensure_interface_exists(self, interface, **kwargs):
        self.reset()
        namespace, idx = self.find_interface_in_all_namespaces(interface)
        self.set_current_namespace(namespace)

        if idx:
            if kwargs.get("namespace") != namespace:
                self._log.debug(
                    f'interface is in namespace {namespace} -> moving to {kwargs.get("namespace")}'
                )

                if kwargs.get("namespace"):
                    self.set_current_namespace(kwargs.get("namespace"))
                    self.set_current_namespace(namespace)
                    self.ipr.link("set", index=idx, net_ns_fd=kwargs.get("namespace"))
                    self.set_current_namespace(kwargs.get("namespace"))
                    self.interface_up(interface)
                else:
                    self.set_current_namespace(namespace)
                    self.ipr.link("set", index=idx, net_ns_pid=1)
                    self.set_current_namespace(None)
                    self.interface_up(interface)

            interface_info = self._resolve_interface_type_by_index(idx)
            if kwargs.get("type"):
                if interface_info.get("type") != kwargs.get("type"):
                    self.delete_interface()
                    raise ValueError(
                        "Cannot change interface type. please delete the interface and recreate with new configuration"
                    )

            if kwargs.get("mtu"):
                self.ipr.link("set", index=idx, mtu=kwargs.get("mtu"))

            if kwargs.get("link_state") in ["up", "down"]:
                if kwargs.get("link_state") == "up":
                    self.interface_up(interface)
                else:
                    self.interface_down(interface)
            elif kwargs.get("link_state") == None:
                pass
            else:
                raise ValueError(
                    f"{kwargs['link_state']} is not a valid link state. Valid link_state values up,down"
                )

        else:
            if kwargs.get("type") in self._supported_virtual_interface_types:
                self._log.debug(
                    f'interface type of {interface} is virtual interface of type {kwargs.get("type")} which does not exist -> creating'
                )
                namespace, idx = self.create_interface(interface, **kwargs)
            else:
                raise ValueError(
                    f"either physical interface just doesn't exist (typo?) or virtual type {kwargs.get('type')} is not supported"
                )

        v4_diff_list = self.get_interface_ipv4(interface)

        for ipv4_config_item in kwargs.get("ipv4", []):
            self.interface_add_ipv4(interface, ipv4_config_item)
            if ipv4_config_item in v4_diff_list:
                v4_diff_list.remove(ipv4_config_item)
        if v4_diff_list:
            self._log.debug(
                f"interface {interface} has dangling ipv4 addresses {v4_diff_list}"
            )
            for ip in v4_diff_list:
                self.interface_delete_ipv4(interface, ip)

        return (namespace, idx)

    def interface_add_ipv4(self, interface_name, prefix):
        idx = self.get_interface_index(interface_name)
        if not idx:
            raise ValueError(
                f"interface {interface_name} not found in namespace {self._current_namespace}"
            )
        address, prefix_len = prefix.strip().split("/")
        prefix_len = int(prefix_len)
        try:
            self.ipr.addr("add", index=idx, address=address, prefixlen=prefix_len)
        except netlink.exceptions.NetlinkError as e:
            if e.code == 98 or e.code == 17:
                self._log.debug(
                    f"prefix {prefix} already in use in namespace {self._current_namespace} -> ignoring request"
                )
                self._log.debug(e)
                return True
            else:
                raise (e)
        self._log.debug(
            f"setting ipv4_address {prefix} on {interface_name} in namespace {self._current_namespace}"
        )
        return True

    def interface_delete_ipv4(self, interface_name, prefix):
        idx = self.get_interface_index(interface_name)
        if not idx:
            raise ValueError(
                f"interface {interface_name} not found in namespace {self._current_namespace}"
            )
        address, prefix_len = prefix.strip().split("/")
        prefix_len = int(prefix_len)
        try:
            self.ipr.addr("del", index=idx, address=address, prefixlen=prefix_len)
        except netlink.exceptions.NetlinkError as e:
            if e.code == 98 or e.code == 17:
                self._log.debug(
                    f"prefix {prefix} already in use in namespace {self._current_namespace} -> ignoring request"
                )
                self._log.debug(e)
                return True
            else:
                raise (e)
        self._log.debug(
            f"setting ipv4_address {prefix} on {interface_name} in namespace {self._current_namespace}"
        )
        return True

    def interface_up(self, interface_name):
        idx = self.get_interface_index(interface_name)
        if not idx:
            raise ValueError(
                f"interface {interface_name} not found in namespace {self._current_namespace}"
            )
        self.ipr.link("set", index=idx, state="up")

    def interface_down(self, interface_name):
        idx = self.get_interface_index(interface_name)
        if not idx:
            raise ValueError(
                f"interface {interface_name} not found in namespace {self._current_namespace}"
            )
        self.ipr.link("set", index=idx, state="down")

    def get_routes(self):
        retval = {"static": {}, "dynamic": {}, "local": {}}
        for route in self.ipr.route("show", type=1):
            if route.get_attr("RTA_GATEWAY"):
                dest = route.get_attr("RTA_DST")
                if dest:
                    dest = f"{dest}/{route.get('dst_len')}"
                else:
                    dest = "default"
                if dest not in retval["static"]:
                    retval["static"][dest] = []
                retval["static"][dest].append(route.get_attr("RTA_GATEWAY"))
            elif route.get_attr("RTA_PREFSRC"):
                dest = f"{route.get_attr('RTA_DST')}/{route.get('dst_len')}"
                if dest not in retval["local"]:
                    retval["local"][dest] = []
                retval["local"][dest].append(f"{route.get_attr('RTA_PREFSRC')}")
            else:
                raise ValueError(
                    f"Never come here, if so something is really wrong. {route}"
                )
        return retval

    def add_route(self, prefix, nexthop):
        try:
            self.ipr.route("add", gateway=nexthop, dst=prefix)
            self._log.debug(
                f"added route {prefix} via {nexthop} in namespace {self._current_namespace}"
            )
        except netlink.exceptions.NetlinkError as e:
            if e.code == 17:
                self._log.debug(
                    f"route {prefix} via {nexthop} in namespace {self._current_namespace} exists -> ignoring"
                )
                pass
            else:
                raise (e)

    def delete_route(self, prefix, nexthop):
        try:
            self.ipr.route("del", gateway=nexthop, dst=prefix)
        except netlink.exceptions.NetlinkError as e:
            if e.code == 3:
                self._log.debug(
                    f"route {prefix} via {nexthop} in namespace {self._current_namespace} does not exist -> ignoring request to delete"
                )
            else:
                raise (e)

    def get_network_interfaces_info(self):
        results = {}
        for link in self.ipr.get_links():
            interface_name=link.get_attr("IFLA_IFNAME")
            interface_type=None
            additional_parameters={}
            if link.get_attr("IFLA_LINKINFO"):
                if link.get_attr("IFLA_LINKINFO").get_attr("IFLA_INFO_KIND")=="vlan":
                    additional_parameters["type"] = "tagged"
                    additional_parameters["vlan_id"] = link.get_attr("IFLA_LINKINFO").get_attr("IFLA_INFO_DATA").get_attr("IFLA_VLAN_ID")
                    additional_parameters["parent_interface"] = self.ipr.link('get', index=link.get_attr("IFLA_LINK"))[0].get_attr("IFLA_IFNAME")

            ipv4 = []
            ipv6 = []
            for addr in self.ipr.get_addr(
                family=socket.AF_INET, label=interface_name
            ):
                ipv4.append(
                    {
                        "prefix_length": addr["prefixlen"],
                        "address": addr.get_attr("IFA_ADDRESS"),
                        "prefix": f'{addr.get_attr("IFA_ADDRESS")}/{addr["prefixlen"]}',
                    }
                )
            for addr in self.ipr.get_addr(
                family=socket.AF_INET6, label=interface_name
            ):
                ipv6.append(
                    {
                        "prefix_length": addr["prefixlen"],
                        "address": addr.get_attr("IFA_ADDRESS"),
                        "prefix": f'{addr.get_attr("IFA_ADDRESS")}/{addr["prefixlen"]}',
                    }
                )
            results[interface_name] = {
                "link_state": link["state"].lower(),
                "oper_state": link.get_attr("IFLA_OPERSTATE").lower(),
                "mac_address": link.get_attr("IFLA_ADDRESS", "None").lower(),
                "mtu": link.get_attr("IFLA_MTU"),
                "ipv4": ipv4,
                "ipv6": ipv6,
                **additional_parameters
            }
        return results


import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn import datasets, preprocessing, feature_selection
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

from tqdm import tqdm

class SOM:
    def __init__(self, input_dim, dim=10, sigma=None, learning_rate=0.1, tay2=1000):
        self.dim = dim  # общее количество нейронов
        self.input_dim = input_dim  # размерность входного пространства
        self.sigma = sigma if sigma is not None else dim / 2
        self.learning_rate = learning_rate
        self.tay1 = 1000 / np.log(self.sigma)
        # минимальное значение сигма на шаге 1000 (определяем по формуле 3)
        self.minsigma = self.sigma * np.exp(-1000 / (1000 / np.log(self.sigma)))
        self.tay2 = tay2
        self.w = np.random.rand(dim * dim, input_dim)
        # матрица позиций всех нейронов, для определения латерального расстояния
        self.positions = np.argwhere(np.ones((dim, dim)) == 1)
        self.n = 1

    # Formula 1
    def _win_neuron(self, input_vector):
        distance = np.sqrt(np.sum(np.square(input_vector - self.w), axis=1))
        return np.argmin(distance)

    # Расстояния
    def _dist(self, win_index):
        d = np.sqrt(np.sum(
            np.square(self.positions - [win_index // self.dim, win_index - win_index // self.dim * self.dim]),
            axis=1))
        return d

    # Формула топологической окрестности
    def _topol(self, d):
        sigma = self.minsigma if self.n > 1000 else self.sigma * np.exp(-self.n / self.tay1)
        return np.exp(- np.square(d) / (2 * sigma * sigma))

    # Изменение весов
    def _change_weights(self, x, tnh):
        lr = self.learning_rate * np.exp(-self.n / self.tay2)
        minlr = 0.01
        lr = minlr if lr <= minlr else lr
        delta = np.transpose(lr * tnh * np.transpose(x - self.w))
        self.w = self.w + delta

    def _one_train(self, x):
        c = self._win_neuron(x)
        coop_dist = self._dist(c)
        t = self._topol(coop_dist)
        self._change_weights(x, t)
        self.n = self.n + 1

    def training(self, data, max_iteration=10000, show_stage=False, stage_step=5000):
        for i in tqdm(range(max_iteration)):
            for j in range(0, len(data)):
                x = data[j]
                self._one_train(x)
                if show_stage and i % stage_step == 0:
                    self.heatmap()

    def heatmap(self):
        size = self.w.shape[1]
        if np.sqrt(size) % 1 == 0:
            nrows = int(np.sqrt(size))
        else:
            nrows = int(np.sqrt(size)) + 1
        ncols = int(np.sqrt(size))
        f, axs = plt.subplots(nrows, ncols)
        axs = np.array(axs)
        for ax, w in zip(axs.reshape(-1), self.w.transpose()):
            grid = np.reshape(w, (self.dim, self.dim))
            sns.heatmap(grid, ax=ax)
        neurons = np.sum(self.w, axis=1)
        grid = np.reshape(neurons, (self.dim, self.dim))
        plt.figure()
        sns.heatmap(grid)
        plt.show()

    def create_map(self, data, y):
        classes = set(y)
        neurons = np.full((self.dim, self.dim), np.nan)
        annot = np.chararray((self.dim, self.dim), itemsize=len(classes) * 2 - 1, unicode=True)
        annot[:] = ''
        for k, i in enumerate(data):
            r = self._win_neuron(i)
            x_coord = r // self.dim
            y_coord = r - r // self.dim * self.dim
            if np.isnan(neurons[x_coord][y_coord]):
                neurons[x_coord][y_coord] = y[k]
                annot[x_coord][y_coord] = y[k]
            else:
                neurons[x_coord][y_coord] = -1
                annot[x_coord][y_coord] = -1
        non_class = np.where(neurons == -1)
        neurons[non_class] = np.nan
        annot[non_class] = np.nan

        notnan_index = np.where(np.isfinite(neurons))
        notnan_neurons = neurons[notnan_index]
        plt.figure(dpi=100)
        plt.hexbin(notnan_index[0], notnan_index[1], notnan_neurons, gridsize=self.dim)

        plt.show()

def dataset_processing(dataset, dim: int = 40, epoch: int = 1000, **kwargs):
    x, y = dataset.data, dataset.target
    x_normalized = preprocessing.normalize(x, norm='l2')

    x_normalized = StandardScaler().fit_transform(x)
    pca = PCA(n_components=8)
    X_pca = pca.fit_transform(x_normalized)

    s = SOM(len(X_pca[0]), dim)
    s.training(X_pca, max_iteration=epoch, **kwargs)
    s.create_map(X_pca, y=y)

def main():
    models = [
        {
            'dataset': datasets.load_iris(),
            'dim': 25,
            'epoch': 10000
        },
        {
            'dataset': datasets.load_breast_cancer(),
            'dim': 50,
            'epoch': 10000
        },
        {
            'dataset': datasets.load_digits(),  # Очень долго считается
            'dim': 43,
            'epoch': 400
        },
        {
            'dataset': datasets.load_wine(),
            'dim': 40,
            'epoch': 100000
        },
    ]
    # for m in models[-1]:
    dataset_processing(**models[-2])

if __name__ == '__main__':
    main()


### Link:
# https://github.com/doocs/leetcode/blob/main/solution/0100-0199/0112.Path%20Sum/README.md

### Description
# 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum
# 判断该树中是否存在 根节点到叶子节点 的路径，这条路径上所有节点值相加等于目标和 targetSum

### Example
# 输入：root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
# 输出：true

# 输入：root = [1,2,3], targetSum = 5
# 输出：false

# 输入：root = [1,2], targetSum = 0
# 输出：false

### Solution
# 递归求解：递归询问树的子节点是否能满足条件即可

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def hasPathSum(self, root, sum):
        def dfs(root, sum):
            if root is None:
                return False
            if root.val == sum and root.left is None and root.right is None:  # Leaf Node
                return True
            return dfs(root.left, sum-root.val) or dfs(root.right, sum-root.val)  # True or False: True
        return dfs(root, sum)

root=TreeNode(val=5, \
              left=TreeNode(val=4,\
                            left=TreeNode(val=11, left=TreeNode(val=7, left=None, right=None),\
                            right=TreeNode(val=2, left=None, right=None)), right=None),\
              right=TreeNode(val=8,\
                             left=TreeNode(val=13, left=None, right=None),\
                             right=TreeNode(val=4, left=None, right=TreeNode(val=1, left=None, right=None))))

print(Solution().hasPathSum(root, 22))


################################################################################
# Author: Miguel Castilho
# Date: 02/26/2021
# This program will create a triangle
# by using loops and the hastag symbol
###############################################################################

lines = int(input('Enter the number of lines: '))

#This is the loop that will output the necessary figure
for r in range(lines):
    for c in range(r+1):
        spaces = " " * r

    print("#" + spaces + "#")
    print('', end = '')


# -*- coding: utf-8 -*-
"""
Created on Fri Aug  2 17:50:36 2019

@author: daniel
"""

# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 17:48:41 2016

@author: daniel
"""

from neuron import h
import d1msn as msn
#import iMSN
import spillover_experiment as pe
import pickle
import parameters as p
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import json
import scipy.signal as ss

# --- 1. Create a cell and other useful stuff
dMSN_library = 'D1_71bestFit_updRheob.pkl'
iMSN_library = 'D2_34bestFit_updRheob.pkl'
with open(dMSN_library, 'rb') as f:
    model_sets  = pickle.load(f, encoding="latin1")

cell_ID = 34
#cell_ID = 1
variables = model_sets[cell_ID]['variables']
#cell = iMSN.iMSN(variables = variables)
cell = msn.MSN(variables = variables)

for d in p.input_dends:
    cell.dendlist[d].nseg *=5

print(cell.max_dist())

for sec in cell.dendlist:
    print(sec.name(), "%f, %f, %f, d = %.2f" % (h.distance(1, sec = sec),
                                      h.distance(0, sec = sec),
                                      h.distance(1, sec = sec) - h.distance(0, sec = sec),
                                      sec.diam))

for sec in cell.somalist:
    print(sec.name(), "%f, %f, %f, d = %.2f" % (h.distance(1, sec = sec),
                                      h.distance(0, sec = sec),
                                      h.distance(1, sec = sec) - h.distance(0, sec = sec),
                                      sec.diam))

# --- 2. Insert stimulation to cell

#independent_dends = [3, 5, 8, 12, 15, 22, 26, 35, 41, 47, 53, 57]
dend_record_list = [22] #[3,4,9,10,21,22,24,26,35,36,51,52]
dend_stim_list = []#[3,4,9,10,35,36]
plateau_cluster_list = [22]

plateau_cluster_size = np.arange(1,31,1)

vs = []
vspine = []
vd = []
legend = []

max_vs = []
max_vspine = []
max_vd = []
g_nmda = []
i_nmda = []

sns.set(font_scale = 1.0)
sns.set_style('whitegrid')
fig_vs = plt.figure();
fig_vspine = plt.figure();
fig_vd = plt.figure();

ax_vs = fig_vs.add_subplot(111); ax_vs.set_ylabel('Vs (mV)'); ax_vs.set_xlabel('t (ms)')
ax_vspine = fig_vspine.add_subplot(111); ax_vspine.set_ylabel('Vspine (mV)'); ax_vspine.set_xlabel('t (ms)')
ax_vd = fig_vd.add_subplot(111); ax_vd.set_ylabel('Vd (mV)'); ax_vd.set_xlabel('t (ms)')
colors = sns.color_palette("coolwarm", plateau_cluster_size.max())

add_spine = 0
on_spine = 1

cell.insert_spines(plateau_cluster_list, p.cluster_start_pos, p.cluster_end_pos, num_spines = p.plateau_cluster_size_max)

sns.set_style("ticks")
for num_syns in plateau_cluster_size:

    ex = pe.Spillover_Experiment('record_ca', cell)
    ex.insert_synapses('noise_SPN')
    ex.insert_synapses('my_spillover', plateau_cluster_list, deterministic = 0,
                       num_syns = num_syns, add_spine = add_spine, on_spine = on_spine)

    ex.set_up_recording(dend_record_list)
    ex.simulate()
    tv = ex.tv.to_python()
    t = ex.t.to_python()
    legend.append("%d syns" % num_syns)

    if add_spine == 1 or on_spine == 1:
        vspine.append(ex.vspine[0].to_python())
        max_vspine.append(max(ex.vspine[0]))
        ax_vspine.plot(tv, ex.vspine[0].to_python(), color = colors[num_syns-1])

    vd.append(ex.vdlist[0].to_python())
    max_vd.append(max(ex.vdlist[0]))

    max_vs.append(max(ex.vs))
    vs.append(ex.vs.to_python())

    cell.esyn = []
    ex.estim = []
    ex.enc = []
    for s in cell.spines:
        s.syn_on = 0
    cell.isyn = []
    ex.istim = []
    ex.inc = []
#
vs_indices = []; vs_widths = []
vd_indices = []; vd_widths = []
vspine_indices = []; vspine_wid = []
for v in vs:
    vs_indices.append(ss.find_peaks(v))
    vs_widths.append(ss.peak_widths(v, (vs_indices[-1])[0], rel_height = 0.15))

for v in vd:
    vd_indices.append(ss.find_peaks(v))
    vd_widths.append(ss.peak_widths(v, (vd_indices[-1])[0] ,rel_height = 0.15))

for i in range(0, len(vs)):
    if add_spine ==0 and on_spine ==0:
        ax_vd.plot(tv, vd[i]);
    ax_vs.plot(tv, vs[i], color = colors[i]); ax_vs.set_title("weight = %.2f, Cdur_factor = %d" % (p.weight, p.eCdur_factor))
    ax_vd.plot(tv, vd[i], color = colors[i]); ax_vs.set_title("weight = %.2f, Cdur_factor = %d" % (p.weight, p.eCdur_factor))
ax_vd.set_title("weight = %.2f, Cdur_factor = %d" % (p.weight, p.eCdur_factor))

sns.despine()
res_dict = {'t': t,
            'vs': vs,
            'vspine': vspine}
to_save = json.dumps(res_dict)
#filename = './results/data_spillover_steep.dat'
#with open(filename,'w', encoding = 'utf-8') as f:
#    json.dump(to_save, f)

plt.show()


import numpy as np
from pytest import approx
from scipy.optimize import minimize

from rosen.example import rosen, rosen_der

def test_example():
    x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2])
    result = minimize(rosen, x0, method="BFGS", jac=rosen_der, options={"disp": True})
    optimized_params = result.x
    assert approx(optimized_params.tolist()) == [
        1.00000004,
        1.0000001,
        1.00000021,
        1.00000044,
        1.00000092,
    ]


"""Classed and methods for comparing expected and published items in DBs"""

class DBAssert:

    @classmethod
    def count_of_types(cls, dbcon, queried_type, expected, **kwargs):
        """Queries 'dbcon' and counts documents of type 'queried_type'

            Args:
                dbcon (AvalonMongoDB)
                queried_type (str): type of document ("asset", "version"...)
                expected (int): number of documents found
                any number of additional keyword arguments

                special handling of argument additional_args (dict)
                    with additional args like
                    {"context.subset": "XXX"}
        """
        args = {"type": queried_type}
        for key, val in kwargs.items():
            if key == "additional_args":
                args.update(val)
            else:
                args[key] = val

        no_of_docs = dbcon.count_documents(args)

        msg = None
        args.pop("type")
        detail_str = " "
        if args:
            detail_str = " with '{}'".format(args)

        if expected != no_of_docs:
            msg = "Not expected no of '{}'{}."\
                  "Expected {}, found {}".format(queried_type,
                                                 detail_str,
                                                 expected, no_of_docs)

        status = "successful"
        if msg:
            status = "failed"

        print("Comparing count of {}{} {}".format(queried_type,
                                                  detail_str,
                                                  status))

        return msg


from datetime import date, datetime

"""
a = date.today()
print(a.today())
print(a.day)
print(a.month)
print(a.year)
b = date(2000, 5, 29)
c = a - b
print(c)
"""
a = datetime.now()
print(a.today())
b = datetime(2000, 5, 29, 17, 43, 22)
c = a - b
print(c)


from flask import render_template, Blueprint, request, session
from app.spotify_api.spotify_handler import SpotifyHandler

select_blueprint = Blueprint('select_bp', __name__, template_folder='templates')
spotify_handler = SpotifyHandler()

@select_blueprint.route("/select_tracks", methods=['GET', 'POST'])
def select_tracks():
    authorization_header = session['authorization_header']

    def extract_letters(string):
        return ''.join([letter for letter in string if not letter.isdigit()])

    if request.method == 'GET':
        # -------- Get user's name, id, and set session --------
        profile_data = spotify_handler.get_user_profile_data(authorization_header)
        user_display_name, user_id = profile_data['display_name'], profile_data['id']
        session['user_id'], session['user_display_name'] = user_id, user_display_name

        # -------- Get user playlist data --------
        playlist_data = spotify_handler.get_user_playlist_data(authorization_header, user_id)

        return render_template('select_tracks.html',
                               user_display_name=user_display_name,
                               playlists_data=playlist_data,
                               func=extract_letters)

    return render_template('select_tracks.html')


import argparse
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch

from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from PIL import Image
from collections import OrderedDict

import json
import math

def main():
    pass

    arguments = get_args()

    data_dir = arguments.data_directory
    save_dir = arguments.save_dir
    arch = arguments.arch
    learning_rate = arguments.learning_rate
    epochs = arguments.epochs
    hidden_layers = arguments.hidden_units
    gpu = arguments.gpu
    outputs = arguments.outputs

    if gpu:
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    else:
        device = torch.device("cpu")

    train_dir = data_dir + '/train'
    valid_dir = data_dir + '/valid'
    test_dir = data_dir + '/test'
    dataloaders, image_datasets  = load_datasets(train_dir, valid_dir, test_dir)

    model = build_model(nn_type=arch, outputs = outputs, hidden_layers = hidden_layers)
    model.to(device)

    criterion = nn.NLLLoss()
    optimizer = optim.Adam(model.classifier.parameters(), lr=learning_rate)

    print('Training start')
    train(model, device, criterion, optimizer, dataloaders['train'], dataloaders['test'], epochs=epochs, print_every = 20)
    print('Training finish')

    save_checkpoint(model, arch, hidden_layers,  image_datasets, model_name = save_dir)

def load_datasets(train_dir, valid_dir, test_dir):

    noramlize = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))

    data_transforms = {
        'train': transforms.Compose([transforms.RandomRotation(30),
                                         transforms.RandomHorizontalFlip(),
                                         transforms.RandomResizedCrop(224),
                                         transforms.ToTensor(),
                                         noramlize]),
        'valid': transforms.Compose([transforms.Resize(255),
                                         transforms.CenterCrop(224),
                                         transforms.ToTensor(),
                                         noramlize]),
        'test': transforms.Compose([transforms.Resize(255),
                                         transforms.CenterCrop(224),
                                         transforms.ToTensor(),
                                         noramlize])
    }

    image_datasets = {
        'train': datasets.ImageFolder(train_dir, transform=data_transforms['train']),
        'valid': datasets.ImageFolder(valid_dir, transform=data_transforms['valid']),
        'test':  datasets.ImageFolder(test_dir, transform=data_transforms['test'])
    }

    dataloaders = {
        'train': torch.utils.data.DataLoader(image_datasets['train'], batch_size=64, shuffle=True),
        'valid': torch.utils.data.DataLoader(image_datasets['valid'], batch_size=32, shuffle=True),
        'test':  torch.utils.data.DataLoader(image_datasets['test'], batch_size=32, shuffle=True)
    }

    return dataloaders, image_datasets

def get_in_features(model):

    for item in  model.classifier:
        if type(item) == torch.nn.modules.linear.Linear:
            return item.in_features

def build_model(nn_type='vgg16', outputs = 102, hidden_layers = [1000]):

    model = getattr(models, nn_type)(pretrained=True)
    print(model)

    in_features = get_in_features(model)

    hidden_layers = [in_features] + hidden_layers

    j = len(hidden_layers) - 1
    i = 0

    #hidden layers go here
    layers = []
    while j > i:
        layers.append((f'fc{i}', nn.Linear(hidden_layers[i], hidden_layers[i+1])))
        layers.append((f'relu{i}', nn.ReLU()))
        layers.append((f'dropout{i}', nn.Dropout()))
        i+=1

    #final layer of the network
    layers.append(('fc_last', nn.Linear(hidden_layers[-1], outputs)))
    layers.append(('output', nn.LogSoftmax(dim=1)))

    classifier = nn.Sequential(OrderedDict(layers))

    # Replace classifier
    model.classifier = classifier

    return model

def validate(model, device, criterion, testing_set):

    test_loss = 0
    accuracy = 0
    model.eval()

    with torch.no_grad():
        for inputs, labels in testing_set:

            inputs, labels = inputs.to(device), labels.to(device)
            output = model.forward(inputs)
            batch_loss = criterion(output, labels)
            test_loss += batch_loss.item()

            ps = torch.exp(output)
            top_p, top_class = ps.topk(1, dim=1)
            equals = top_class == labels.view(*top_class.shape)
            accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

    model.train()
    return test_loss/len(testing_set), accuracy/len(testing_set)

def train(model, device, criterion, optimizer, traning_set, testing_set, epochs=5, print_every = 20):

    steps = 0
    running_loss = 0

    for epoch in range(epochs):
        for inputs, labels in traning_set:
            steps += 1
            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            logps = model.forward(inputs)
            loss = criterion(logps, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if steps % print_every == 0:
                test_loss, accuracy = validate(model, device, criterion, testing_set)

                print(f"Epoch {epoch+1}/{epochs}.. "
                  f"Train loss: {running_loss/print_every:.3f}.. "
                  f"Test loss: {test_loss:.3f}.. "
                  f"Test accuracy: {accuracy:.3f}")

                running_loss = 0

def save_checkpoint(model, arch, hidden_layers,  image_datasets, model_name = "checkpoint.pt"):

    model.class_to_idx = image_datasets['train'].class_to_idx

    checkpoint = {
        'arch' : arch,
        'class_to_idx' : model.class_to_idx,
        'state_dict' : model.state_dict(),
        'hidden_layers' : hidden_layers
    }

    torch.save(checkpoint, model_name)
    print('saving checkpoint as: {}'.format(model_name))

def get_args():
    """
        Get arguments from command line
    """
    parser = argparse.ArgumentParser()

    parser.add_argument("--data_directory", type=str, default = 'flowers', help="data directory containing training and testing data")
    parser.add_argument("--save_dir", type=str, default="checkpoint.pt" ,help="directory where to save trained model and hyperparameters")
    parser.add_argument("--arch", type=str, default="vgg16", help="pre-trained model: vgg16, alexnet")
    parser.add_argument("--epochs", type=int, default=3, help="number of epochs to train model")
    parser.add_argument("--hidden_units", type=list, default=[768, 384], help="list of hidden layers")
    parser.add_argument("--learning_rate", type=float, default=0.001, help="learning rate")
    parser.add_argument("--gpu", type=bool, default=True, help="use GPU or CPU to train model: True = GPU, False = CPU")
    parser.add_argument("--outputs", type=int, default=102, help="enter output size")

    return parser.parse_args()

if __name__ == "__main__":
    main()


from typing import List
import xdoctest
import xdoctest.doctest_part
from _typeshed import Incomplete

INDENT_RE: Incomplete
NEED_16806_WORKAROUND: Incomplete

class DoctestParser:
    simulate_repl: bool

    def __init__(self, simulate_repl: bool = False) -> None:
        ...

    def parse(
            self,
            string: str,
            info: dict | None = None
    ) -> List[xdoctest.doctest_part.DoctestPart]:
        ...


#API Client - Enterprise DevOps

import requests

ip_address  = input("Please enter you IP Address: ")

url = f"http://ipwho.is/{ip_address}"

get = requests.get(url)

if get.status_code == 200:
    data = get.json()
    print(data)
else:
    print(f"Error: {get.status_code}")


import numpy as np
import os

def batch(inputs, max_sequence_length=None):
    """
    :param inputs: list of sentences (integer lists)
    :param max_sequence_length: integer specifying how large should `max_time` dimension be.
                                If None, maximum sequence length would be used
    :return: inputs_time_major: input sentences transformed into time-major matrix
                                (shape [max_time, batch_size]) padded with 0s
             sequence_lengths: batch-sized list of integers specifying amount of active time steps in each input sequence
    """
    sequence_lengths = [len(seq) for seq in inputs]
    if max_sequence_length is None:
        max_sequence_length = max(sequence_lengths)
    batch_size = len(inputs)
    inputs_time_major = np.zeros(shape=[batch_size, max_sequence_length], dtype=np.int32)

    for i,seq in enumerate(inputs):
        for j,word in enumerate(seq):
            inputs_time_major[i,j]=word

    # [batch_size, max_time] -> [max_time, batch_size]
    inputs_time_major = inputs_time_major.swapaxes(0,1)
    return inputs_time_major, sequence_lengths

def random_sequences(length_from, length_to, vocab_lower, vocab_upper, batch_size):
    """
        Generates batches of random integer sequences,
        sequence length in [length_from, length_to],
        vocabulary in [vocab_lower, vocab_upper]
    """
    if length_from > length_to:
        raise ValueError('length_from > length_to')
    def random_length():
        if length_from==length_to:
            return length_from
        else:
            return np.random.randint(length_from,length_to+1)
    while True:
        yield [np.random.randint(low=vocab_lower, high=vocab_upper, size=random_length()).tolist()
           for _ in range(batch_size)]

def load_data(path):
    input_file = os.path.join(path)
    with open(input_file, "r", encoding='utf-8', errors='ignore') as f:
        data = f.read()

    return data

def extract_vocab(data):
    special_words = ['<pad>', '<unk>', '<s>',  '<\s>']
    set_words = set([word for line in data.split('\n') for word in line.split()])
    int_to_vocab = {word_i: word for word_i, word in enumerate(special_words + list(set_words))}
    vocab_to_int = {word: word_i for word_i, word in int_to_vocab.items()}
    return int_to_vocab, vocab_to_int

def pad_id_sequences(source_ids, source_vocab_to_int, target_ids, target_vocab_to_int, sequence_length):
    new_source_ids = [list(reversed(sentence + [source_vocab_to_int['<pad>']] * (sequence_length - len(sentence)))) \
                      for sentence in source_ids]
    new_target_ids = [sentence + [target_vocab_to_int['<pad>']] * (sequence_length - len(sentence)) \
                      for sentence in target_ids]

    return new_source_ids, new_target_ids

def batch_data(source, target, batch_size):
    """
    Batch source and target together
    """
    for batch_i in range(0, len(source)//batch_size):
        start_i = batch_i * batch_size
        source_batch = source[start_i:start_i + batch_size]
        target_batch = target[start_i:start_i + batch_size]
        yield source_batch, target_batch


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
from urllib import request
from scrapy.pipelines.images import ImagesPipeline

from download import settings

# class DownloadPipeline(object):
#     def __init__(self):
#         self.path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'image')
#         if not os.path.exists(self.path):
#             os.mkdir(self.path)
#
#     def process_item(self, item, spider):
#         title = item['title']
#         urls = item['image_urls']
#
#         title_path = os.path.join(self.path, title)
#         if not os.path.exists(title_path):
#             os.mkdir(title_path)
#         for url in urls:
#             image_name = url.split('_')[-1]
#             request.urlretrieve(url, os.path.join(title_path, image_name))
#         return item

class BMWImagesPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        # 这个方法是发送请求时调用的，其实就是用来发送请求的。
        request_objs = super(BMWImagesPipeline, self).get_media_requests(item, info)
        for request_obj in request_objs:
            request_obj.item = item  # 这里是为了获取request对象的item属性，供下面的函数调用
        return request_objs

    def file_path(self, request, response=None, info=None):
        # 这个方法是在请求完成时，存储文件时调用的。
        path = super(BMWImagesPipeline, self).file_path(request, response, info)
        title = request.item.get('title')
        images_store = settings.IMAGES_STORE
        title_path = os.path.join(images_store, title)
        if not title_path:
            os.mkdir(title_path)
        image_name = path.replace('full/', '')
        image_path = os.path.join(title_path, image_name)
        return image_path