#!/usr/bin/env python3
#
# This either:
# * Creates a placeholder VPC and private hosted zone or
# * Associates a VPC with the hosted zone and deletes the placeholder
#
# Note: This assumes that a VPC may exist with the same name as the hosted zone
#

import argparse
import subprocess
import sys
import logging
import time

logging.basicConfig(level=logging.DEBUG)

INSTALL="install"
DELETE="delete"
AWS="aws"       # path to the AWS CLI binary

def main():
    parser = argparse.ArgumentParser(description='Creates/updates hosted zones for kops.')
    parser.add_argument(dest='mode', choices=[INSTALL, DELETE], help='Mode to run in')
    parser.add_argument(dest='hosted_zone_name', help='Hosted zone to create/update (a domain name)')
    parser.add_argument('--vpc-region', help='Region to create the VPC in (when running in install mode)')

    args = parser.parse_args()
    return run(mode=args.mode, hosted_zone_name=args.hosted_zone_name, vpc_region=args.vpc_region)

def run(mode, hosted_zone_name, vpc_region):
    if mode == INSTALL:
        return install(hosted_zone_name=hosted_zone_name, vpc_region=vpc_region)
    elif mode == DELETE:
        return delete(hosted_zone_name=hosted_zone_name)

def delete(hosted_zone_name):
    """
    Deletes a hosted zone (used when deleting the kapp)
    :param hosted_zone_name:
    """
    if not hosted_zone_name.endswith('.'):
        hosted_zone_name += '.'

    hosted_zone_id = _get_hosted_zone_id(hosted_zone_name)
    logging.info("Hosted zone ID is: '%s'" % hosted_zone_id)

    if hosted_zone_id:
        _delete_hosted_zone(hosted_zone_id)

    placeholder_vpc_name = "placeholder %s" % hosted_zone_name
    vpc_id = _get_vpc_by_name(placeholder_vpc_name)
    if vpc_id:
        _delete_vpc(vpc_id)

def install(hosted_zone_name, vpc_region):
    """
    Creates/updates a hosted zone and its associated VPC and creates/deletes a placeholder VPC accordingly
    :param hosted_zone_name:
    :param vpc_region:
    """
    if not hosted_zone_name.endswith('.'):
        hosted_zone_name += '.'

    hosted_zone_id = _get_hosted_zone_id(hosted_zone_name)
    logging.info("Hosted zone ID is: '%s'" % hosted_zone_id)

    placeholder_vpc_name = "placeholder %s" % hosted_zone_name

    if not hosted_zone_id:
        logging.info("Hosted zone doesn't exist... Will create it.")
        hosted_zone_id = _create_hosted_zone(hosted_zone_name=hosted_zone_name,
                                             placeholder_vpc_name=placeholder_vpc_name,
                                             vpc_region=vpc_region)

    # see if the main (non-placeholder) vpc exists
    vpc_name = hosted_zone_name.rstrip('.')
    vpc_id = _get_vpc_by_name(vpc_name)

    if not vpc_id:
        logging.info("No main VPC exists called '%s'. Nothing else to do." % vpc_name)
        return

    # if it does exist, we need to associate it with the hosted zone if it's not already,
    # and delete the placeholder if it exists
    if _is_vpc_associated_with_hosted_zone(vpc_id=vpc_id, hosted_zone_id=hosted_zone_id):
        logging.info("Hosted zone '%s' is already associated with VPC '%s' (ID='%s')" %
                     (hosted_zone_id, vpc_name, vpc_id))
    else:
        # associate the vpc with the hosted zone
        _associate_vpc_with_hosted_zone(vpc_id=vpc_id,
                                        hosted_zone_id=hosted_zone_id,
                                        vpc_region=vpc_region)

    placeholder_vpc_id = _get_vpc_by_name(placeholder_vpc_name)
    if placeholder_vpc_id:
        # dissociate and delete the placeholder VPC
        if _is_vpc_associated_with_hosted_zone(placeholder_vpc_id, hosted_zone_id):
            _dissociate_vpc_from_hosted_zone(vpc_id=placeholder_vpc_id,
                                             hosted_zone_id=hosted_zone_id,
                                             vpc_region=vpc_region)

        logging.info("Deleting placeholder VPC '%s'" % placeholder_vpc_id)
        _delete_vpc(placeholder_vpc_id)
    else:
        logging.info("Couldn't find a placeholder VPC called '%s'. Assume it's already been deleted." % placeholder_vpc_name)

def _dissociate_vpc_from_hosted_zone(vpc_id, hosted_zone_id, vpc_region):
    """
    Dissociates a VPC from a hosted zone
    :param vpc_id:
    :param hosted_zone_id:
    :param vpc_region:
    """
    logging.info("Dissociating VPC '%s' with hosted zone '%s'" % (vpc_id, hosted_zone_id))
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'disassociate-vpc-from-hosted-zone',
                                  '--hosted-zone-id', hosted_zone_id,
                                  '--vpc', 'VPCRegion=%s,VPCId=%s' % (vpc_region, vpc_id)],
                            capture_output=True)
    logging.debug('result=%s' % result)

    if not result.returncode == 0:
        raise RuntimeError("Failed to dissociate VPC '%s' from hosted zone '%s': %s" % (
            vpc_id, hosted_zone_id, result))

def _associate_vpc_with_hosted_zone(vpc_id, hosted_zone_id, vpc_region):
    """
    Associate a VPC with a hosted zone
    :param vpc_id:
    :param hosted_zone_id:
    :param vpc_region:
    """
    logging.info("Associating VPC '%s' with hosted zone '%s'" % (vpc_id, hosted_zone_id))
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'associate-vpc-with-hosted-zone',
                                  '--hosted-zone-id', hosted_zone_id,
                                  '--vpc', 'VPCRegion=%s,VPCId=%s' % (vpc_region, vpc_id)],
                            capture_output=True)
    logging.debug('result=%s' % result)

    if not result.returncode == 0:
        raise RuntimeError("Failed to associate VPC '%s' with hosted zone '%s': %s" % (
            vpc_id, hosted_zone_id, result))

def _is_vpc_associated_with_hosted_zone(vpc_id, hosted_zone_id):
    """
    Checks whether a VPC is associated with a hosted zone
    :param vpc_id:
    :param hosted_zone_id:
    :return: boolean
    """
    logging.info("Checking whether VPC '%s' is associated with hosted zone '%s'" % (vpc_id, hosted_zone_id))
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'get-hosted-zone',
                                  '--id', hosted_zone_id,
                                  '--query', 'VPCs[?VPCId == `%s`].VPCId | [0]' % vpc_id],
                            capture_output=True)

    if not result.returncode == 0:
        raise RuntimeError("Failed to check whether VPC '%s' is associated with hosted zone '%s': %s" % (
            vpc_id, hosted_zone_id, result))

    logging.debug('result=%s' % result)
    result = result.stdout.decode("utf-8").strip()
    return result == vpc_id

def _get_vpc_by_name(vpc_name):
    """
    Returns the ID of a VPC by name
    :param vpc_name:
    :return: VPC ID
    """
    logging.info("Searching for VPC '%s'" % vpc_name)
    result = subprocess.run(args=[AWS, '--output', 'text', 'ec2', 'describe-vpcs',
                                  '--filters', 'Name=tag:Name,Values=%s' % vpc_name,
                                  '--query', 'Vpcs[*].VpcId | [0]'],
                            capture_output=True)
    logging.debug('result=%s' % result)

    if not result.returncode == 0:
        raise RuntimeError("Failed to get VPC '%s': %s" % (vpc_name, result))

    result = result.stdout.decode("utf-8").strip()
    if result == 'None':
        result = None

    logging.debug("Returning result '%s'" % result)

    return result

def _delete_vpc(vpc_id):
    """
    Deletes a VPC by name
    :param vpc_id:
    """
    logging.info("Deleting VPC '%s'" % vpc_id)
    result = subprocess.run(args=[AWS, '--output', 'text', 'ec2', 'delete-vpc',
                                  '--vpc-id', vpc_id],
                            capture_output=True)
    if not result.returncode == 0:
        raise RuntimeError("Failed to delete VPC '%s': %s" % (vpc_id, result))

def _create_vpc(vpc_name):
    """
    Creates a VPC and tags it with the given name
    :param vpc_name:
    :return:
    """
    logging.info("Creating a VPC called '%s'" % vpc_name)
    result = subprocess.run(args=[AWS, '--output', 'text', 'ec2', 'create-vpc',
                                  '--cidr-block', '10.0.0.0/28',        # this is a temporary placeholder so we can use anything
                                  '--query', 'Vpc.VpcId'],
                            capture_output=True)
    if not result.returncode == 0:
        raise RuntimeError("Failed to create VPC '%s': %s" % (vpc_name, result))

    logging.debug('result=%s' % result)

    vpc_id = result.stdout.decode("utf-8").strip()
    if vpc_id == 'None':
        vpc_id = None

    logging.info("Tagging VPC '%s'" % vpc_name)
    result = subprocess.run(args=[AWS, '--output', 'text', 'ec2', 'create-tags', '--resources', vpc_id,
                                  '--tags', 'Key=Name,Value=%s' % vpc_name],
                            capture_output=False)

    if not result.returncode == 0:
        raise RuntimeError("Failed to tag VPC '%s': %s" % (vpc_name, result))

    logging.info("VPC '%s' (ID=%s) created and tagged" % (vpc_name, vpc_id))

    return vpc_id

def _delete_hosted_zone(hosted_zone_id):
    """
    Delete a hosted zone
    :param hosted_zone_id:
    """
    logging.info("Deleting hosted zone '%s'" % hosted_zone_id)
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'delete-hosted-zone',
                                  '--id', hosted_zone_id],
                            capture_output=True)

    if not result.returncode == 0:
        raise RuntimeError("Failed to delete hosted zone '%s': %s" % (hosted_zone_id, result))

def _create_hosted_zone(hosted_zone_name, placeholder_vpc_name, vpc_region):
    """
    Creates a hosted zone and returns its ID
    :param hosted_zone_name:
    :param placeholder_vpc_name: Name of the placeholder VPC to create if it doesn't already exist
    :param vpc_name: Region to create the VPC in
    :return: The hosted zone ID
    """
    placeholder_vpc_id = _get_vpc_by_name(placeholder_vpc_name)
    if not placeholder_vpc_id:
        placeholder_vpc_id = _create_vpc(placeholder_vpc_name)

    logging.info("Creating private hosted zone '%s' for placeholder VPC %s in region %s" % (
        hosted_zone_name, placeholder_vpc_id, vpc_region))
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'create-hosted-zone',
                                  '--caller-reference', str(time.time()),
                                  '--vpc', "VPCRegion=%s,VPCId=%s" % (vpc_region, placeholder_vpc_id),
                                  '--name', hosted_zone_name, '--hosted-zone-config', 'PrivateZone=true',
                                  '--query', 'HostedZone.Id'],
                            capture_output=True)

    if not result.returncode == 0:
        raise RuntimeError("Failed to create hosted zone '%s': %s" % (hosted_zone_name, result))

    logging.debug('result=%s' % result)
    hosted_zone_id = result.stdout.decode("utf-8").strip()
    if hosted_zone_id == 'None':
        hosted_zone_id = None

    return hosted_zone_id

def _get_hosted_zone_id(hosted_zone_name):
    """
    Returns the ID of a hosted zone if it exists
    :param hosted_zone_name:
    :return:
    """
    logging.info("Searching for hosted zone '%s'" % hosted_zone_name)
    result = subprocess.run(args=[AWS, '--output', 'text', 'route53', 'list-hosted-zones', '--query',
                             'HostedZones[?Name == `%s` && Config.PrivateZone == `true`].Id | [0]' % hosted_zone_name],
                            capture_output=True)

    if not result.returncode == 0:
        raise RuntimeError("Failed to create VPC '%s': %s" % (hosted_zone_name, result))

    logging.debug('result=%s' % result)
    result = result.stdout.decode("utf-8").strip()
    if result == 'None':
        result = None

    logging.debug("Returning result '%s'" % result)

    return result

if __name__ == "__main__":
    sys.exit(main())


import os
def initialize():
    tmppath = "tmp"
    reportpath="reports"
    isTmpExist = os.path.exists(tmppath)
    isReportExist = os.path.exists(reportpath)
    # check if tmp folder exist
    if not isTmpExist:
        # if not create
        os.makedirs(tmppath)
    else:
        # if exist clear tmp files
        for f in os.listdir(tmppath):
            os.remove(os.path.join(tmppath, f))
    # check if reports folder exist
    if not isReportExist:
        os.makedirs(reportpath)


"""
Speak the time when the touch-sensitive pin is touched

Hook piezo speaker (high impedence) to a DAC pin, which on
the ESP32 DEVKIT 1 is either GPIO25 or GPIO26 (DAC1 and DAC2)

Where do voices come from?
https://evolution.voxeo.com/library/audio/prompts/numbers/index.jsp

"""

import machine
import math
import time
import connect
import ntptime
import machine

connect.do_connect(verbose=True)
ntptime.settime()

rtc = machine.RTC()

speakerpin = machine.Pin(25)

dac = machine.DAC(speakerpin)

def dacspeedtest():
    nomadcfreq = 8000
    duration = 1
    nomperiod_us = 1000000 // nomadcfreq
    nomaudiofreq = 1500
    bytes= bytearray([int(128 + 120*math.sin(math.pi * i * nomaudiofreq/nomadcfreq))
                      for i in range(int(nomadcfreq * duration))])

    for i in range(10):
        for b in bytes:
            dac.write(b)
            time.sleep_us(nomperiod_us)


"""
Main file to run all the operations of automatic processing
in FARO SCENE software.
"""

from operations.start_operations import InitialProcedures

class MainApp:
    """ Docstring Placeholder """
    def run_operations(self):
        """ Docstring Placeholder """
        # Create an instance of the StartOperations class
        operations_instance = InitialProcedures()

        # Launches the program and gives the user a choice on which process option to select.
        operations_instance.startup()

        # Checks that Scene is open, if not, opens it.
        operations_instance.open_scene()

        # Clears the Update and Project transfer popup boxes.
        operations_instance.clear_popup_menus()

        # Gives the user the option to add a new project.
        processed_folder_path = operations_instance.set_folders()

        # Set the default folder that will be used in SCENE
        operations_instance.set_project_folder(processed_folder_path)

        # Import a project using the project transfer wizard
        # operations_instance.project_transfer()

# Create an instance of MainApp
app = MainApp()

# Run the operations
app.run_operations()


import os, glob, importlib
from types import ModuleType

def load_modules(repo='dl_utils', submodule='io', allows=['hdf5', 'dicom', 'wrapper']):
    """
    Method to compile submodule from all available libraries

    """
    ROOT = os.environ.get('{}_ROOT'.format(repo).upper(), '.')

    is_hidden = lambda x : x.startswith('_')
    is_module = lambda x, module : isinstance(getattr(module, x), ModuleType) and x not in allows

    inits = glob.glob('{}/{}/*/{}/__init__.py'.format(ROOT, repo, submodule))
    inits = [i.replace(ROOT, '') for i in inits]

    LOAD_FUNCS = {}
    SAVE_FUNCS = {}

    for init in inits:

        # --- Get path to module
        path = '.'.join(init.split('/')[1:-1])

        # --- Import module
        module = importlib.import_module(path)
        names = [n for n in module.__dict__ if not is_hidden(n) and not is_module(n, module)]
        globals().update({n: getattr(module, n) for n in names})

        # --- Record LOAD_FUNCS / SAVE_FUNCS
        for n in module.__dict__:

            if hasattr(globals().get(n, None), 'LOAD_FUNCS'):
                LOAD_FUNCS.update(getattr(globals()[n], 'LOAD_FUNCS'))

            if hasattr(globals().get(n, None), 'SAVE_FUNCS'):
                SAVE_FUNCS.update(getattr(globals()[n], 'SAVE_FUNCS'))

    # ==========================================================
    # FILEIO
    # ==========================================================

    # --- Load module
    fileio = importlib.import_module('dl_utils.core.io.fileio')

    # --- Register load funcs
    fileio.LOAD_FUNCS.update(LOAD_FUNCS)
    fileio.SAVE_FUNCS.update(SAVE_FUNCS)

    # --- Update io namespace
    globals()['load'] = fileio.load
    globals()['save'] = fileio.save
    globals()['LOAD_FUNCS'] = fileio.LOAD_FUNCS
    globals()['SAVE_FUNCS'] = fileio.SAVE_FUNCS

# --- Load modules
load_modules()

# --- Remove imports from namespace
for name in ['os', 'glob', 'importlib', 'ModuleType', 'load_modules', 'name']:
    globals().pop(name)


""" Linked records REST views
"""
import json
import logging

from rest_framework import status
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView

from core_linked_records_app.components.blob.api import get_blob_by_pid
from core_linked_records_app.components.data.api import get_data_by_pid
from core_linked_records_app.rest.data.renderers.data_html_user_renderer import (
    DataHtmlUserRenderer,
)
from core_linked_records_app.rest.data.renderers.data_xml_renderer import (
    DataXmlRenderer,
)
from core_linked_records_app.utils.exceptions import (
    InvalidPrefixError,
    InvalidRecordError,
)
from core_linked_records_app.utils.pid import split_prefix_from_record
from core_linked_records_app.utils.providers import ProviderManager
from core_main_app.access_control.exceptions import AccessControlError
from core_main_app.commons.exceptions import DoesNotExist
from core_main_app.rest.data.serializers import DataSerializer
from core_main_app.utils.file import get_file_http_response

logger = logging.getLogger(__name__)

class ProviderRecordView(APIView):
    """Provider Record View"""

    parser_classes = (JSONParser,)
    renderer_classes = (DataHtmlUserRenderer, JSONRenderer, DataXmlRenderer)

    def __init__(self, **kwargs):
        self.provider_manager = ProviderManager()
        super().__init__(**kwargs)

    def post(self, request, provider, record):
        """Create a handle record

        Args:
            request:
            provider:
            record:

        Returns:
        """
        try:
            try:
                prefix, record = split_prefix_from_record(record)
            except InvalidPrefixError as invalid_prefix_error:
                return Response(
                    {
                        "record": record,
                        "url": request.build_absolute_uri("?"),
                        "message": str(invalid_prefix_error),
                    },
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                )
            except InvalidRecordError as invalid_record_error:
                return Response(
                    {
                        "record": record,
                        "url": request.build_absolute_uri("?"),
                        "message": str(invalid_record_error),
                    },
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                )

            id_provider = self.provider_manager.get(provider)
            provider_response = id_provider.create(prefix, record)

            provider_content = json.loads(provider_response.content)
            return Response(
                provider_content, status=provider_response.status_code
            )
        except Exception as exc:  # pylint: disable=broad-except
            return Response(
                {
                    "record": record,
                    "url": request.build_absolute_uri("?"),
                    "message": str(exc),
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )

    def put(
        self,
        request,  # noqa, pylint: disable=unused-argument
        provider,
        record,
    ):
        """Update the value of a given handle record

        Args:
            request:
            provider:
            record:

        Returns:
        """
        try:
            id_provider = self.provider_manager.get(provider)
            provider_response = id_provider.update(record)

            provider_content = json.loads(provider_response.content)

            return Response(
                provider_content, status=provider_response.status_code
            )
        except Exception as exc:  # pylint: disable=broad-except
            return Response(
                {
                    "message": f"An unexpected error occurred while updating "
                    f"record: {str(exc)}"
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )

    def get(self, request, provider, record):
        """Retrieve the local data of a given handle record

        Args:
            request:
            provider:
            record:

        Returns:
        """
        try:
            id_provider = self.provider_manager.get(provider)
            provider_response = id_provider.get(record)

            try:
                query_result = get_data_by_pid(
                    json.loads(provider_response.content)["url"], request
                )
                return Response(
                    DataSerializer(query_result).data,
                    status=status.HTTP_200_OK,
                )
            except DoesNotExist:
                # Try to find PID in blobs
                try:
                    query_result = get_blob_by_pid(
                        json.loads(provider_response.content)["url"],
                        request.user,
                    )

                    return get_file_http_response(
                        query_result.blob.read(), query_result.filename
                    )
                except AccessControlError as exception:
                    content = {"message": str(exception)}
                    return Response(content, status=status.HTTP_403_FORBIDDEN)
                except DoesNotExist:
                    content = {
                        "status": "error",
                        "code": status.HTTP_404_NOT_FOUND,
                        "message": "No document with specified handle found",
                    }
                    return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as exc:  # pylint: disable=broad-except
            content = {
                "status": "error",
                "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
                "message": str(exc),
            }
            return Response(
                content, status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

    def delete(
        self,
        request,  # noqa, pylint: disable=unused-argument
        provider,
        record,
    ):
        """Delete a handle record

        Args:
            request:
            provider:
            record:

        Returns:
        """
        try:
            id_provider = self.provider_manager.get(provider)
            provider_response = id_provider.delete(record)

            provider_content = json.loads(provider_response.content)

            return Response(
                provider_content, status=provider_response.status_code
            )
        except Exception as exc:  # pylint: disable=broad-except
            return Response(
                {
                    "message": f"An unexpected error occurred while deleting "
                    f"record: {str(exc)}"
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR,
            )


from flask import abort, render_template, Response, Blueprint

from .. import config, crypto, http
from ..model.room import get_accessible_rooms
from . import auth, converters  # noqa: F401

from io import BytesIO

import qrencode

from PIL.Image import NEAREST

views = Blueprint('views', __name__)

@views.get("/")
def serve_index():
    rooms = get_accessible_rooms()
    if len(rooms) == 0:
        return render_template('setup.html')
    if not config.HTTP_SHOW_INDEX:
        abort(http.FORBIDDEN)
    return render_template(
        "index.html", url_base=config.URL_BASE, rooms=rooms, pubkey=crypto.server_pubkey_hex
    )

@views.get("/r/<Room:room>/")
def view_room(room):
    if not room.default_read:
        abort(http.FORBIDDEN)

    return render_template(
        "view_room.html",
        room=room,
        show_recent=config.HTTP_SHOW_RECENT,
    )

@views.get("/r/<Room:room>/invite.png")
def serve_invite_qr(room):
    if not room.default_read:
        abort(http.FORBIDDEN)

    img = qrencode.encode(room.url)
    data = BytesIO()
    img = img[-1].resize((512, 512), NEAREST)
    img.save(data, "PNG")
    return Response(data.getvalue(), mimetype="image/png")


#!/usr/bin/env python
# Find any files in directory not under version control, and add them
#   to tracking. Escape any space characters in filename.

import subprocess
import re
import time

x = subprocess.Popen('svn stat | grep ?', shell=True, stdout=subprocess.PIPE).communicate()
y = x[0].split('?')
for i in y:
   x = i.strip()
   if len(x) != 0:
      x = re.sub(' ', '\\ ', x)
      subprocess.Popen('svn add ' + x, shell=True)
      time.sleep(0.5)


import sys,urllib.request, json, codecs, time, argparse, re
from bs4 import BeautifulSoup

# Markets - Template

def load_url(url):
    html = urllib.request.urlopen(url).read()
    return BeautifulSoup(html, "html.parser")

def parse_market(bs):
    add = bs.find("address")
    address = {
        "street": "",
        "number": "",
        "plz": "",
        "city": "",
        "location": {
            "lat": 0,
            "lng": 0
        }
    }
    return address

print("PLZ...")
BASE_URL = "http://www.suche-postleitzahl.org/plz-gebiet/"

def leadingzero(x):
    if x < 10:
        return "0"+str(x)
    else:
        return str(x)

print("INSERT INTO tukgrp3.zipsize (zip, size) VALUES ")
for plz in range(2, 3):
    p = leadingzero(plz)
    print(BASE_URL+p)
    bs = load_url(BASE_URL+p)# BeautifulSoup(t, "html.parser")
    a_tag = bs.find('th', text = re.compile('Fläche'))
    area = int(a_tag.parent.findNext('td').text.split(' ')[0].replace('.', '').split(',')[0])
    query = "('{}', '{}'),".format(p, area)
    print(query,)

# base_html = fullfile # urllib.urlopen(BASE_URL).read()
#


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the missingNumbers function below.
def missingNumbers(arr, brr):

   #get all the unique elements in the second list
    unique = list(set(brr))

    missing = [] #holds the missing elements from the first list

    for el in unique:
        if (brr.count(el)>arr.count(el)): #if the count in the second array exceeds
        #that in the first list then the element is missing

            missing.append(el)

    missing.sort()

    return missing

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    m = int(input())

    brr = list(map(int, input().rstrip().split()))

    result = missingNumbers(arr, brr)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()


from datetime import datetime
from dateutil.relativedelta import relativedelta
import pandas as pd
import backtrader as bt
import fxsignal
import fxcmpy
import yaml
import logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    handlers=[logging.FileHandler("./logs/collect.log"), logging.StreamHandler()])

#symbol_list = ['EUR/USD', 'GBP/USD', 'AUD/USD', 'NZD/USD', 'USD/CAD', 'USD/CHF', 'GBP/AUD', 'EUR/JPY', 'EUR/CHF',
#               'AUD/CAD', 'AUD/JPY', 'GBP/NZD', 'GBP/JPY', 'GBP/CHF', 'EUR/NZD', 'AUD/NZD', 'CAD/CHF', 'CAD/JPY',
#               'GBP/CHF', 'NZD/CHF', 'USD/JPY']

# 'AUD/CHF','CHF/JPY','EUR/AUD','EUR/CAD','EUR/GBP','GBP/CAD','NZD/JPY',
symbol_list = ['AUD/USD', 'USD/CAD', 'NZD/USD', 'USD/CHF','USD/JPY']

#symbol_list = ['AUD/CAD','AUD/JPY','AUD/NZD','AUD/USD','CAD/CHF',
#               'CAD/JPY','EUR/CHF','EUR/JPY','EUR/NZD','EUR/USD',
#               'GBP/AUD','GBP/CHF','GBP/JPY','GBP/NZD','GBP/USD',
#               'NZD/CHF','NZD/USD','USD/CAD','USD/CHF','USD/JPY']

#    minutes: m1, m5, m15 and m30,
#    hours: H1, H2, H3, H4, H6 and H8,
#    one day: D1,
#    one week: W1,
#    one month: M1.

def collect(config):
    ''' Collects data for whole period '''
    start_date = datetime(2019, 1, 1)
    end_date = datetime(2019, 9, 1)
    for symbol in symbol_list:

        feed = fxsignal.FxcmFeed(symbol, start_date, end_date, 'm30', config['fxcm'])
        feed.get_feed()
        feed.close()
        data = feed.clean()
        feed.save_csv(data)
        logging.info("{} count: {}".format(symbol, len(data.index)))

def collect_chunk(config):
    ''' Collects data by months (there is 10000 records limitation in fxcmpy API) '''
    start_date = datetime(2010, 1, 1)
    end_date = datetime(2019, 12, 31)
    for symbol in symbol_list:
        chunk_start_date = start_date
        chunk_end_date = start_date + relativedelta(months=+6)
        history = pd.DataFrame()

        while (chunk_start_date < end_date):
            feed = fxsignal.FxcmFeed(symbol, chunk_start_date, chunk_end_date, 'm30', config['fxcm'])
            feed.get_feed()
            feed.close()
            data = feed.clean()
            history = history.append(data, ignore_index = True)
            chunk_start_date = chunk_end_date+relativedelta(seconds=+1)
            chunk_end_date   = chunk_end_date+relativedelta(months=+6)

        feed.save_csv(history)
        logging.info("{} count: {}".format(symbol, len(history.index)))

def run():
    with open('./scripts/config.yaml') as f:
        config = yaml.load(f, Loader=yaml.FullLoader)
    collect_chunk(config)

if __name__ == '__main__':
    run()


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

# EXPLORE THE BEST characteristics of rt

import numpy as np
from pylab import *
import cPickle as pickle
from scipy.stats import sem

monkeys =   { 'g':12701,
              'm':34752,
              'p':27692,
              'r':11634,
              's':13348 }

data = {}
for s in monkeys.keys():
	with open("../data/data_pickle/"+s+".pickle", 'rb') as f:
		data[s] = pickle.load(f)

rt = {}
rtmean = {}
count = {}

for s in monkeys.keys():
	rt[s] = dict({'search':{},'repeat':{}})
	rtmean[s] = dict({'search':{},'repeat':{}})
	count[s] = dict({'search':np.zeros(60),'repeat':np.zeros(60)})
	problem = data[s][0,4]
	search = [data[s][0,8]]
	repeat = []
	phase = 0.0
	for t in xrange(1, len(data[s])):
		phase = data[s][t,2]-data[s][t-1,2] # Si phase == -1, on vient de changer de problem
		if data[s][t,4] == problem and (phase == 0.0 or phase == 1.0): # same problem
			if data[s][t,2] == 0.0: # search trial
				search.append(data[s][t,8]) # append rt of search trial
			elif data[s][t,2] == 1.0: # repeat trial
				repeat.append(data[s][t,8]) # append rt of repeat trial
		else:
			nb_incorrect = len(search) # number of search trial before correct
			nb_repeat = len(repeat)
			count[s]['search'][nb_incorrect] += 1
			count[s]['repeat'][nb_repeat] += 1
			if nb_incorrect in rt[s]['search'].keys(): # append search trial in corresponding error length
				rt[s]['search'][nb_incorrect].append(np.array(search))
				rt[s]['repeat'][nb_incorrect].append(np.array(repeat))
			else:
				rt[s]['search'][nb_incorrect] = [np.array(search)]
				rt[s]['repeat'][nb_incorrect] = [np.array(repeat)]

			problem = data[s][t,4]  # number of new problem
			search = [data[s][t,8]]
			repeat = []

	t = 'search'
	for l in rt[s][t].keys():
		rt[s][t][l] = np.array(rt[s][t][l])
		rtmean[s][t][l] = np.zeros((2,l))
		rtmean[s][t][l][0] = rt[s][t][l].mean(0)
		rtmean[s][t][l][1] = sem(rt[s][t][l])

	t = 'repeat'
	for l in rt[s][t].keys():
		meann = np.zeros((2,7))
		for i in xrange(7):
			tmp = []
			for j in xrange(len(rt[s][t][l])):
				if len(rt[s][t][l][j]) > i:
					tmp.append(rt[s][t][l][j][i])
			meann[0,i] = np.mean(tmp)
			meann[1,i] = sem(tmp)

		rtmean[s][t][l] = meann

# figure(figsize = (20,15))
figure()
colors = {'p':'blue','s':'cyan','r':'black','m':'red','g':'green'}

for i in xrange(1,7):
	subplot(2,3,i)
	for s in rtmean.keys():
	# for s in ['p','s','r','m']:
	# for s in ['p','m']:
		# errorbar(range(i), rtmean[s]['search'][i][0], rtmean[s]['search'][i][1], fmt='o', label = str(s), color = colors[s])
		# errorbar(range(i,i+7), rtmean[s]['repeat'][i][0], rtmean[s]['repeat'][i][1], fmt='*', color = colors[s])
		plot(range(i), rtmean[s]['search'][i][0], 'o', label = str(s), color = colors[s])
		plot(range(i,i+7), rtmean[s]['repeat'][i][0], '*', color = colors[s])
		plot(range(i+7), np.hstack((rtmean[s]['search'][i][0],rtmean[s]['repeat'][i][0])), '-', color = colors[s])
	grid()
	title(str(i)+" SEARCH | 8 REPEAT")
	legend()

figure()
ind = 1
for k in count.keys():
	subplot(2,3,ind)
	plot(count[k]['search'][0:10], 'o-', label = 'Search')
	plot(count[k]['repeat'][0:10], '*-', label = 'Repeat')
	ylabel("Count")
	xlabel("Lenght of search/repeat bloc")
	legend()
	ind+=1
	title(k)

show()


from PIL import Image
import os, sys

dir = sys.argv[1]

# led1
def calcola_luminosita_media(image, x, y, width, height):
    area = image.crop((x, y, x + width, y + height))
    grayscale_area = area.convert("L")  # Converti l'area in scala di grigi
    luminosita_media = sum(grayscale_area.getdata()) / (width * height)
    return luminosita_media

# elenco file da analizzare
files_in_dir = os.listdir(dir)
crop_files = sorted([file for file in files_in_dir if file.startswith("crop")])
for file in crop_files:

    image_path = "{}/{}".format(dir, file)
    image = Image.open(image_path)

    x_area1 = 0  # Coordinata x dell'angolo superiore sinistro dell'area 1
    y_area1 = 100  # Coordinata y dell'angolo superiore sinistro dell'area 1
    width_area1 = 75  # Larghezza dell'area 1
    height_area1 = 50  # Altezza dell'area 1
    luminosita_media_area1 = calcola_luminosita_media(image, x_area1, y_area1, width_area1, height_area1)

    x_area2 = 100
    y_area2 = 100
    width_area2 = 75
    height_area2 = 50
    luminosita_media_area2 = calcola_luminosita_media(image, x_area2, y_area2, width_area2, height_area2)

    x_area3 = 240
    y_area3 = 100
    width_area3 = 75
    height_area3 = 50
    luminosita_media_area3 = calcola_luminosita_media(image, x_area3, y_area3, width_area3, height_area3)

    print("File {}: {}\t{}\t{}".format(file, int(luminosita_media_area1), int(luminosita_media_area2), int(luminosita_media_area3)))

'''
# Definisci il percorso dell'immagine e le coordinate delle due aree
image_path = "percorso/all/immagine.jpg"
x_area1 = 100  # Coordinata x dell'angolo superiore sinistro dell'area 1
y_area1 = 150  # Coordinata y dell'angolo superiore sinistro dell'area 1
width_area1 = 50  # Larghezza dell'area 1
height_area1 = 50  # Altezza dell'area 1

x_area2 = 200  # Coordinata x dell'angolo superiore sinistro dell'area 2
y_area2 = 150  # Coordinata y dell'angolo superiore sinistro dell'area 2
width_area2 = 50  # Larghezza dell'area 2
height_area2 = 50  # Altezza dell'area 2

# Apri l'immagine
image = Image.open(image_path)

# Calcola la luminosità media delle due aree
luminosita_media_area1 = calcola_luminosita_media(image, x_area1, y_area1, width_area1, height_area1)
luminosita_media_area2 = calcola_luminosita_media(image, x_area2, y_area2, width_area2, height_area2)

# Confronta le luminosità medie delle due aree
if luminosita_media_area1 > luminosita_media_area2:
    print("L'area 1 è più chiara dell'area 2.")
elif luminosita_media_area1 < luminosita_media_area2:
    print("L'area 1 è più scura dell'area 2.")
else:
    print("Le due aree hanno la stessa luminosità media.")
'''


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=C0103,W0621
"""
Dataset preparing utils.
"""
__author__ = "GW [http://gw.tnode.com/] <gw.2016@tnode.com>"
__license__ = "GPLv3+"

import numpy as np
import random
from keras.engine.training import make_batches

def build_index(sequences, max_new=None, min_count=1, index=None, vocab_start=2, none_key=None, none_ids=0, oov_key="", oov_ids=1):
    """Build vocabulary index from dicts or lists of strings (reserved ids: 0 = none/padding, 1 = out-of-vocabulary)."""
    if index is None:
        index = {}

    def _traverse_cnt(obj, cnts):
        """Recursively traverse dicts and lists of strings."""
        if isinstance(obj, dict):
            for s in obj.itervalues():
                _traverse_cnt(s, cnts)
        elif isinstance(obj, list) or isinstance(obj, tuple):
            for s in obj:
                _traverse_cnt(s, cnts)
        else:
            try:
                cnts[obj] += 1
            except KeyError:
                cnts[obj] = 1

    # count string occurrences
    cnts = {}
    _traverse_cnt(sequences, cnts)

    # ignore strings with low occurrences
    for k, cnt in cnts.items():
        if cnt < min_count:
            del cnts[k]

    # rank strings by decreasing occurrences and use as index
    index_rev = sorted(cnts, key=cnts.get, reverse=True)
    if max_new is not None:
        index_rev = index_rev[:max_new]  # limit amount of added strings

    # mapping of strings to vocabulary ids
    index.update([ (k, i) for i, k in enumerate(index_rev, start=vocab_start) ])
    index_size = vocab_start + len(index)  # largest vocabulary id + 1

    # add none/padding and out-of-vocabulary ids
    index[none_key] = none_ids
    index[oov_key] = oov_ids
    return index, index_size

def map_sequence(sequence, index, oov_key=""):
    """Map sequence of strings to vocabulary ids."""

    ids = []
    for s in sequence:
        try:
            ids.append(index[s])
        except KeyError:  # missing in vocabulary
            ids.append(index[oov_key])
    return ids

def pad_sequence(sequence, max_len, value=0, max_rand=None):
    """Post-pad sequence of ids as numpy array."""

    # crop sequence if needed
    sequence = sequence[:max_len]

    # convert to numpy array with masked and random post-padding
    if isinstance(value, int):
        x = np.hstack([sequence, np.ones((max_len - len(sequence),), dtype=np.int) * value])
    elif isinstance(value, float):
        x = np.hstack([sequence, np.ones((max_len - len(sequence),), dtype=np.float32) * value])
    elif value == 'rand' and isinstance(max_rand, int):
        x = np.hstack([sequence, np.random.randint(1, max_rand, size=max_len - len(sequence),)])
    else:
        raise ValueError("Padding value '{}' not understood".format(value))
    return x

def batch_generator(dataset, indexes, indexes_size, arg1_len, arg2_len, conn_len, punc_len, batch_size, random_per_sample=1):
    """Batch generator where each sample represents a different discourse relation."""

    batch_size = int(batch_size / (1.0 + random_per_sample))

    rel_ids = list(dataset['rel_ids'])  # copy list
    while True:
        # shuffle relations on each epoch
        random.shuffle(rel_ids)
        for batch_start, batch_end in make_batches(len(rel_ids), batch_size):

            # prepare batch data
            _rel_id = []
            data_in = {}
            data_out = {}
            for rel_id in rel_ids[batch_start:batch_end]:
                # choose sample
                doc_id = dataset['rel_parts'][rel_id]['DocID']
                words_len = len(dataset['words'][doc_id])

                _rel_id.append(rel_id)

                def tokens_np(token_ids, max_len):
                    words_slice = [ dataset['words'][doc_id][i]  for i in token_ids ]

                    ids = map_sequence(words_slice, indexes['words2id'])
                    x_np = pad_sequence(ids, max_len, value=0)
                    return x_np

                # arg1/2/conn/punc/rand inputs
                arg1_np = tokens_np(dataset['rel_parts'][rel_id]['Arg1'], arg1_len)
                try:
                    data_in['arg1_ids'].append(arg1_np)
                except KeyError:
                    data_in['arg1_ids'] = [arg1_np]

                arg2_np = tokens_np(dataset['rel_parts'][rel_id]['Arg2'], arg2_len)
                try:
                    data_in['arg2_ids'].append(arg2_np)
                except KeyError:
                    data_in['arg2_ids'] = [arg2_np]

                conn_np = tokens_np(dataset['rel_parts'][rel_id]['Connective'], conn_len)
                try:
                    data_in['conn_ids'].append(conn_np)
                except KeyError:
                    data_in['conn_ids'] = [conn_np]

                punc_np = tokens_np(dataset['rel_parts'][rel_id]['Punctuation'], punc_len)
                try:
                    data_in['punc_ids'].append(punc_np)
                except KeyError:
                    data_in['punc_ids'] = [punc_np]

                # relation senses output
                def rsenses_np(cat, oov_key=""):
                    try:
                        i = indexes['rel_senses2id'][cat]
                    except KeyError:  # missing in vocabulary
                        i = indexes['rel_senses2id'][oov_key]
                    x_np = np.zeros((indexes_size['rel_senses2id'],), dtype=np.float32)
                    x_np[i] = 1
                    return x_np

                if dataset['rel_senses']:
                    rsenses = rsenses_np(dataset['rel_senses'][rel_id])
                else:
                    rsenses = rsenses_np("")
                try:
                    data_out['rsenses'].append(rsenses)
                except KeyError:
                    data_out['rsenses'] = [rsenses]

                # random noise for each sample
                for _ in range(random_per_sample):
                    j = np.random.randint(3)
                    if j == 0:
                        # random arg1
                        arg1_np = np.random.randint(1, 1 + np.max(arg1_np), size=arg1_np.shape)
                    elif j == 1:
                        # random arg2
                        arg2_np = np.random.randint(1, 1 + np.max(arg2_np), size=arg2_np.shape)
                    else:
                        # random arg1 and arg2
                        arg1_np = np.random.randint(1, 1 + np.max(arg1_np), size=arg1_np.shape)
                        arg2_np = np.random.randint(1, 1 + np.max(arg2_np), size=arg2_np.shape)
                    # random connective
                    if np.max(conn_np) > 0:
                        conn_np = np.random.randint(1, 1 + np.max(conn_np), size=conn_np.shape)
                    # random punctuation
                    if np.max(punc_np) > 0:
                        punc_np = np.random.randint(1, 1 + np.max(punc_np), size=punc_np.shape)
                    # mark as out-of-vocabulary in rsenses
                    rsenses = rsenses_np("")

                    _rel_id.append(rel_id)
                    data_in['arg1_ids'].append(arg1_np)
                    data_in['arg2_ids'].append(arg2_np)
                    data_in['conn_ids'].append(conn_np)
                    data_in['punc_ids'].append(punc_np)
                    data_out['rsenses'].append(rsenses)

            # convert to NumPy array
            for k, v in data_in.items():
                data_in[k] = np.asarray(v)
            for k, v in data_out.items():
                data_out[k] = np.asarray(v)

            # append meta data
            data_in['_rel_id'] = _rel_id

            # yield batch
            yield (data_in, data_out)

def decode_relation(y_np, cats2id, cats2id_size):
    """Decode categories from one-hot vector (sample, cats2id)."""

    # normalize by rows to [0,1] interval
    y_sum = np.sum(y_np)
    totals = y_np / y_sum
    totals[y_sum == 0.] = y_np[y_sum == 0.]  # prevent NaN

    # return most probable category
    cat = None
    max_total = -1.
    for t, j in cats2id.items():
        if totals[j] > max_total:
            max_total = totals[j]
            cat = t
    return cat, totals

def load_word2vec(words2id, words2id_size, words_dim, words2vec_bin=None, words2vec_txt=None):
    if not words2vec_bin and not words2vec_txt:
        return None  # no pre-trained word embeddings

    import numpy as np
    from gensim.models import word2vec
    if words2vec_bin:
        model = word2vec.Word2Vec.load_word2vec_format(words2vec_bin, binary=True)
    else:
        model = word2vec.Word2Vec.load_word2vec_format(words2vec_txt)
    init_weights = np.zeros((words2id_size, words_dim), dtype=np.float32)
    for k, i in words2id.iteritems():
        if not isinstance(k, str):
            continue
        try:
            init_weights[i] = model[k][:words_dim]
        except KeyError:  # missing in word2vec
            pass
    return [init_weights]


"""
IN: MSA fasta format
OUT: MSA fasta but in bynary
"""

from sys import argv

infile = argv[1]
outfile = argv[2]

out = open(outfile,'w')
for line in open(infile):
	if line[0] == ">":
		#no altering
		out.write(line)
	else:
		line = line.strip()
		newline = ""
		for char in line:
			if char == "-":
				newline += "0"
			else:
				newline += "1"
		out.write(newline+'\n')
out.close()


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 21 12:42:30 2014

@author: dim1
"""

import numpy as np
import matplotlib.pyplot as plt
from SSFM import SSFM
from fiber import Fiber
from pulse import Pulse
#from fftw_transforms import fftcomputer as fftw
#from scipy import fftpack

plt.close('all')

steps = 100

centerwl = 850.0
gamma = 0.045
fiber_length = 0.1
P0 = 10e3
T0 = 28.4e-3

init = Pulse(n = 2**13)
init.gen_sech(P0, T0, centerwl)

fiber1 = Fiber()

fiber1.generate_fiber(fiber_length ,centerwl, [-1.276e-2, 8.119e-5, -1.321e-7,
                      3.032e-10, -4.196e-13, 2.570e-16], gamma, 0)

evol = SSFM(disable_Raman = False, disable_self_steepening = False,
            local_error = 0.1, suppress_iteration = True)

y = np.zeros(steps)
AW = np.complex64(np.zeros((init.n, steps)))
AT = np.complex64(np.copy(AW))

y, AW, AT, pulse1, = evol.Fiber_propagate(pulse_in = init, fiber = fiber1,
                                         n_steps = steps)

wl = 2 * np.pi * init.c / (init.W)

loWL = 500
hiWL = 1200

iis = np.logical_and(wl>loWL,wl<hiWL)

iisT = np.logical_and(init.T>-1,init.T<5)

xW = wl[iis]
xT = init.T[iisT]
zW_in = np.transpose(AW)[:,iis]
zT_in = np.transpose(AT)[:,iisT]
zW = 10*np.log10(np.abs(zW_in)**2)
zT = 10*np.log10(np.abs(zT_in)**2)
mlIW = np.max(zW)
mlIT = np.max(zT)

D = fiber1.Beta2_to_D(init)
beta = fiber1.Beta2(init)

plt.figure()
#plt.subplot(121)
plt.pcolormesh(xW, y, zW, vmin = mlIW - 40.0, vmax = mlIW)
plt.autoscale(tight=True)
plt.xlim([loWL, hiWL])
plt.xlabel('Wavelength (nm)')
plt.ylabel('Distance (m)')

#plt.subplot(122)
#plt.pcolormesh(xT, y, zT, vmin = mlIT - 40.0, vmax = mlIT)
#plt.autoscale(tight=True)
#plt.xlabel('Delay (ps)')
#plt.ylabel('Distance (m)')

plt.show()


import unittest
from service.AdaptationManager import AdaptationManager
from tests import TestModelConfig
from service.PredictionService import PredictionService

test_modal_configs = [
    {
        'key': 'c-01',
        'name': 'Config 01',
        'inputs': [
            'input_a',
            'input_b',
            'input_c'
        ],
        'accuracy': 90,
        'energy': 10,
        'path': 'tests.TestModelConfig'
    },
    {
        'key': 'c-02',
        'name': 'Config 02',
        'inputs': [
            'input_a',
            'input_b',
            'input_d'
        ],
        'accuracy': 80,
        'energy': 20,
        'path': 'tests.TestModelConfig'
    },
    {
        'key': 'c-03',
        'name': 'Config 03',
        'inputs': [
            'input_a',
        ],
        'accuracy': 60,
        'energy': 5,
        'path': 'tests.TestModelConfig'
    }
]

class AdaptationManagerTest(unittest.TestCase):

    def test_load_modal_config(self):
        global test_modal_configs
        manager = AdaptationManager()

        manager.load_modal_configs(test_modal_configs)
        self.assertEqual(manager.model_configs, test_modal_configs)

        loaded_config_module = manager.model_config_modules[test_modal_configs[0]['key']]
        self.assertIsNotNone(loaded_config_module)
        self.assertIs(loaded_config_module, TestModelConfig)

    def test_monitor(self):
        manager = AdaptationManager()
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        self.assertTrue(manager.monitor(context))
        self.assertFalse(manager.monitor(context))
        context = {
            'goal': 'POWER_CONSUMPTION',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        self.assertTrue(manager.monitor(context))
        context = {
            'goal': 'POWER_CONSUMPTION',
            'inputs': ['input_a', 'input_b']
        }
        self.assertTrue(manager.monitor(context))

    def test_analyse(self):
        manager = AdaptationManager()
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        manager.model_configs = test_modal_configs
        manager.monitor(context)
        self.assertTrue(contains_same_config_keys(manager.analyse(), ['c-01', 'c-03']))
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a', 'input_b']
        }
        manager.monitor(context)
        self.assertTrue(contains_same_config_keys(manager.analyse(), ['c-03']))

        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_f']
        }
        manager.monitor(context)
        self.assertTrue(contains_same_config_keys(manager.analyse(), []))

    def test_plan(self):
        manager = AdaptationManager()
        manager.current_context['goal'] = "ACCURACY"
        self.assertEqual(manager.plan(test_modal_configs)['key'], 'c-01')
        manager.current_context['goal'] = "ENERGY"
        self.assertEqual(manager.plan(test_modal_configs)['key'], 'c-03')
        manager.current_context['goal'] = "NON_EXISTING_GOAL"
        self.assertEqual(manager.plan(test_modal_configs), None)

    def test_execute(self):
        prediction_service = PredictionService()
        manager = AdaptationManager(prediction_service)

        manager.load_modal_configs(test_modal_configs)
        manager.execute(test_modal_configs[0])
        self.assertIs(prediction_service.get_config_module(), TestModelConfig)
        self.assertIs(prediction_service.get_config_key(), test_modal_configs[0]['key'])
        manager.execute(None)
        self.assertIsNone(prediction_service.get_config_module())

    def test_adapt(self):
        manager = AdaptationManager()
        manager.load_modal_configs(test_modal_configs)
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a', 'input_b']
        }
        result = manager.adapt(context)
        self.assertTrue(result['adapted'])
        self.assertEqual(result['current_model_config']['key'], 'c-03')

        # Send same context
        result = manager.adapt(context)
        self.assertFalse(result['adapted'])

        # Change inputs
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        result = manager.adapt(context)
        self.assertTrue(result['adapted'])
        self.assertEqual(result['current_model_config']['key'], 'c-01')

        # Change goal
        context = {
            'goal': 'ENERGY',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        result = manager.adapt(context)
        self.assertTrue(result['adapted'])
        self.assertEqual(result['current_model_config']['key'], 'c-03')

        # Change context which matches the same modal
        context = {
            'goal': 'ACCURACY',
            'inputs': ['input_a']
        }
        result = manager.adapt(context)
        self.assertFalse(result['adapted'])

        # Non-existing goal
        context = {
            'goal': 'NON_EXISTING_GOAL',
            'inputs': ['input_a']
        }
        result = manager.adapt(context)
        self.assertTrue(result['adapted'])
        self.assertIsNone(result['current_model_config'])

        # Reset context and remove inputs
        context = {
            'goal': 'ENERGY',
            'inputs': ['input_a', 'input_b', 'input_c']
        }
        manager.adapt(context)
        context = {
            'goal': 'ACCURACY',
            'inputs': []
        }
        result = manager.adapt(context)
        self.assertTrue(result['adapted'])
        self.assertIsNone(result['current_model_config'])

def contains_same_config_keys(configs, keys):
    keys_in_config = []
    for config in configs:
        keys_in_config.append(config['key'])
    return keys_in_config == keys

if __name__ == '__main__':
    unittest.main()


from app.models.posts_models import Posts
from flask import jsonify, request
from datetime import datetime
import os

ACCEPTED_KEYS = os.getenv("ACCEPTED_KEYS")

def get_posts_controller():
    get_posts = Posts.get_posts_models()

    get_posts_list = list(get_posts)

    for i in get_posts_list:
        del i["_id"]

    return jsonify(get_posts_list), 200

def get_posts_by_id_controller(id):
    try:
        get_post_by_id = Posts.get_posts_by_id_models(id)

        del get_post_by_id["_id"]

        return get_post_by_id, 200
    except:
        return {"msg": f"ID {id} not found"}, 404

def create_posts_controller():
    try:
        date = datetime.now()
        actual_date = date.strftime("%d/%m/%y %H:%M:%S")

        data = request.get_json()

        get_posts = Posts.get_posts_models()

        get_posts_list = list(get_posts)

        if(len(get_posts_list) == 0):
            new_id = 1
        else:
            previous_object = get_posts_list[-1]
            new_id = previous_object["id"] + 1

        new_data = Posts(new_id, actual_date, actual_date, **data)

        found = Posts.create_posts_models(new_data.__dict__)

        del found["_id"]

        return found, 201

    except:
        return {
            "error": "wrong fields",
            "required_fields": [
                {"title": "Title"},
                {"author": "Author"},
                {"tags": "Tags"},
                {"content": "Content"}
            ]
        }, 400

def delete_post_controller(id):
    try:
        deleted_post = Posts.delete_post_models(id)

        del deleted_post["_id"]

        return deleted_post, 200

    except:
        return {"error": f"ID {id} not found"}, 404

def update_post_controller(id):
    try:
        data = request.get_json()

        accepted = ACCEPTED_KEYS.split(", ")

        for key in data.keys():
            if(key not in accepted):
                return {
                    "error": f"field '{key}' doesn't exists and cannot be updated!"
                }, 400

        if(len(data) == 0):
            return {
                "error": "You must pass the data!"
            }, 400

        date = datetime.now()
        actual_date = date.strftime("%d/%m/%y %H:%M:%S")

        data.update({"updated_at": actual_date})

        updated_post = Posts.update_post_models(data, id)

        show_updated_post = Posts.get_posts_by_id_models(id)

        del show_updated_post["_id"]

        return show_updated_post, 200

    except:
        return {
            "error": f"ID {id} not found!"
        }, 404


# https://encode-decode.com/uuencode-decode-online/

import os

path = "C://Temp/gm.txt"
out_path = "C://Temp/UUE.txt"

def to_UUE(bin_list):
    concated_str = [str(elem) for elem in bin_list]
    concated_str = "".join(concated_str)
    quad_symb_of_UUE = ""
    for i in range(4):
        # first = int(concated_str[0:6], 2) + 32
        # sec = int(concated_str[6:12], 2) + 32
        # third = int(concated_str[12:18], 2) + 32
        # fouth = int(concated_str[18:24], 2) + 32
        one_sym = chr(int(concated_str[i * 6:(i + 1) * 6], 2) + 32)
        if one_sym == ' ':
            one_sym = '`'
        quad_symb_of_UUE += one_sym
    return quad_symb_of_UUE

def to_normal_bin(num):
    res = ""
    while num > 0:
        res += str(num % 2)
        num //= 2
    res += str(0) * (8 - len(res))
    return res[::-1]

def main():
    file = open(path, "a")
    size = os.stat(path).st_size
    remains = size % 3
    if remains > 0:
        adding_str = ' ' * (3 - remains)  # '`'
        file.write(adding_str)
    file.close()
    results = []
    byte_counter = 0
    with open(path, "rb") as file:
        one_UUE_line = ""
        while True:
            local_converted_bytes_2_bin = [] * 3
            triple_bytes = file.read(3)
            if triple_bytes == b"":
                break
            byte_counter += 3
            for i, elem in enumerate(triple_bytes):
                local_converted_bytes_2_bin.append(to_normal_bin(elem))
            one_UUE_line += to_UUE(local_converted_bytes_2_bin)
            if byte_counter == 45:
                byte_counter = 0
                results.append("M")
                results.append(one_UUE_line)
                one_UUE_line = ""
                results.append('\n')
        if byte_counter != 0:
            results.append(chr(32 + byte_counter))
            results.append(one_UUE_line)
    with open(out_path, "w") as out_file:
        for item in results:
            out_file.write(str(item))
        out_file.write('\n`')

if __name__ == '__main__':
    main()


'''
Background information
The Hamming Code is used to correct errors, so-called bit flips, in data transmissions. Later in the description follows a detailed explanation of how it works. In this Kata we will implement the Hamming Code with bit length 3, this has some advantages and disadvantages:

✓ Compared to other versions of hamming code, we can correct more mistakes
✓ It's simple to implement
x The size of the input triples

https://www.codewars.com/kata/5ef9ca8b76be6d001d5e1c3e/train/python
'''

def encode(string):
    return ''.join(['{0:08b}'.format(i) for i in [ord(i) for i in string]]).replace('1','111').replace('0','000')

def decode(bits):
    res = list(map(''.join, zip(*[iter(bits)]*3)))
    lst = []
    for i in res:
        if i.count('1')>i.count('0'):
            lst.append('1')
        else:
            lst.append('0')

    return  ''.join([chr(i) for i in [int(i,2) for i in list(map(''.join, zip(*[iter(''.join(lst))]*8)))]])

6 кю/Error correction #1 - Hamming Code.py


from sentry.models import Integration
from sentry.testutils import APITestCase

class OrganizationIntegrationsListTest(APITestCase):
    endpoint = "sentry-api-0-organization-integrations"

    def setUp(self):
        super().setUp()
        self.login_as(user=self.user)

        self.integration = Integration.objects.create(provider="example", name="Example")
        self.integration.add_organization(self.organization, self.user)

    def test_simple(self):
        response = self.get_success_response(self.organization.slug)

        assert len(response.data) == 1
        assert response.data[0]["id"] == str(self.integration.id)
        assert "configOrganization" in response.data[0]

    def test_no_config(self):
        response = self.get_success_response(self.organization.slug, qs_params={"includeConfig": 0})

        assert "configOrganization" not in response.data[0]


#code to find the lexicographical order of a number given number n from 1 to n

class Solution(object):
    def lexicographical(self, n):
        result = []
        stack = []
        x = 1
        while (x<=n):  #first finish off the integers with 1 as the starting digit
            stack.append(x)
            result.append(x)
            x *= 10

        while(stack):
            y = stack.pop()
            if(y%10 == 9): continue
            y += 1        #start incrementing the digit and continue the same
            while (y<=n):
                stack.append(y)
                result.append(y)
                y *= 10
        return result


import unittest
from tree.buttons.Button_tempo import Button_tempo
from unittest.mock import Mock
from mock import patch
import threading
from time import sleep

class TestButtonTempo(unittest.TestCase):

    @patch('time.sleep', return_value=None)
    @patch('time.time', return_value=0)
    def test_press(self, time, mock_sleep):
        manager = Mock()
        scenar_on, scenar_off = Mock(), Mock()
        button = Button_tempo("test", manager, scenar_on, scenar_off, 12)
        button.press()
        self.assertEqual(manager.do_scenar_principal.call_count, 1)
        self.assertEqual(manager.do_scenar_principal.call_args[0][0], scenar_on)
        self.assertEqual(threading.active_count(), 2)
        self.assertTrue("test" in "".join(i.name for i in threading.enumerate()))
        self.assertTrue("tempo" in "".join(i.name for i in threading.enumerate()))
        time.return_value = 11
        button.press()
        self.assertEqual(threading.active_count(), 2)
        time.return_value = 13
        sleep(0.005)
        self.assertEqual(threading.active_count(), 2)
        time.return_value = 24
        sleep(0.005)
        self.assertEqual(manager.do_scenar_principal.call_count, 2)
        self.assertEqual(manager.do_scenar_principal.call_args[0][0], scenar_off)

    def test_state(self):
        manager = Mock()
        scenar_on, scenar_off = Mock(), Mock()
        button = Button_tempo("test", manager, scenar_on, scenar_off, 0)
        self.assertEqual(button.state(), manager.get_state())

    def test_eq(self):
        manager = Mock()
        scenar_on, scenar_off = Mock(), Mock()
        button = Button_tempo("test", manager, scenar_on, scenar_off, 0)
        button2 = Button_tempo("test", manager, scenar_on, scenar_off, 0)
        self.assertEqual(button, button2)
        self.assertNotEqual(button, 8)
        button2 = Button_tempo("test", manager, Mock(), scenar_off, 0)
        self.assertNotEqual(button, button2)
        button2 = Button_tempo("test", manager, scenar_on, Mock(), 0)
        self.assertNotEqual(button, button2)
        button2 = Button_tempo("test", manager, scenar_on, scenar_off, 12)
        self.assertNotEqual(button, button2)

    def test_str(self):
        manager = Mock()
        scenar_on, scenar_off = Mock(), Mock()
        button = Button_tempo("test", manager, scenar_on, scenar_off, 254)
        self.assertTrue(str(scenar_on.name) in str(button))
        self.assertTrue(str(scenar_off.name) in str(button))
        self.assertTrue("tempo" in str(button))
        self.assertTrue("254" in str(button))


#
# You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
#
# You may assume the two numbers do not contain any leading zero, except the number 0 itself.
#
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
# Output: 7 -> 0 -> 8
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        l1CurrentNode = l1
        l2CurrentNode = l2
        result = []
        add = 0
        while(l1CurrentNode!= None or l2CurrentNode!= None):
            if(l1CurrentNode==None):
                while(l2CurrentNode != None):
                    # Come to the end of l2
                    tempNum1 = l2CurrentNode.val + add
                    add = tempNum1/10
                    result.append(tempNum1 % 10)
                    if (l2CurrentNode.next == None):
                        if (add == 1):
                            result.append(1)
                            return result
                    l2CurrentNode = l2CurrentNode.next
                return result
            if(l2CurrentNode==None):
                while(l1CurrentNode!=None):
                    tempNum2 = l1CurrentNode.val + add
                    add = tempNum2/10
                    result.append(tempNum2 % 10)
                    # Come to the end of l1
                    if(l1CurrentNode.next == None):
                        if(add == 1):
                            result.append(1)
                            return result
                    l1CurrentNode = l1CurrentNode.next
                return result
            temp = l1CurrentNode.val + l2CurrentNode.val + add
            result.append(temp%10)
            add = temp/10
            if(l1CurrentNode.next == None and l2CurrentNode.next ==None):
                if (add == 1):
                    result.append(1)
            l1CurrentNode = l1CurrentNode.next
            l2CurrentNode = l2CurrentNode.next
        return result

l1 = ListNode(3)
l1.next = ListNode(7)
# l1.next.next = ListNode(3)
l2 = ListNode(9)
l2.next = ListNode(2)
# l2.next.next = ListNode(4)
test = Solution()
result = test.addTwoNumbers(l1,l2)
print (result)
# while(testNode.next!=None):
#     print ('['+str(testNode.val)+'->')
#     testNode = testNode.next
# print (']')


from __future__ import print_function
import torch
import os
import numpy as np
import argparse
import yaml
from tqdm import tqdm
import cv2
import sys

sys.path.append(".")
sys.path.append("..")

from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
from models.retinaface import RetinaFace
from utils.box_utils import decode, decode_landm

def check_keys(model, pretrained_state_dict):
    ckpt_keys = set(pretrained_state_dict.keys())
    model_keys = set(model.state_dict().keys())
    used_pretrained_keys = model_keys & ckpt_keys
    unused_pretrained_keys = ckpt_keys - model_keys
    missing_keys = model_keys - ckpt_keys
    print('Missing keys:{}'.format(len(missing_keys)))
    print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
    print('Used keys:{}'.format(len(used_pretrained_keys)))
    assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
    return True

def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()}

def load_model(model, pretrained_path, load_to_cpu):
    print('Loading pretrained model from {}'.format(pretrained_path))
    if load_to_cpu:
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
    else:
        device = torch.cuda.current_device()
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
    if "state_dict" in pretrained_dict.keys():
        pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
    else:
        pretrained_dict = remove_prefix(pretrained_dict, 'module.')
    check_keys(model, pretrained_dict)
    model.load_state_dict(pretrained_dict, strict=False)
    return model

@torch.no_grad()
def test(_args):
    with open(_args.cfg, "r") as f:
        cfg = yaml.load(f.read(), Loader=yaml.FullLoader)

    net = RetinaFace(cfg=cfg)
    net = load_model(net, _args.ckpt, _args.device == "cpu")
    net.eval()

    confidence_threshold = _args.confidence_threshold
    nms_threshold = _args.nms_threshold
    vis_threshold = _args.vis_threshold
    device = _args.device
    origin_size = _args.origin_size
    target_size = _args.target_size
    max_size = _args.max_size

    print('Finished loading model!')

    device = torch.device(device)
    net = net.to(device)

    images_path = [x.path for x in os.scandir(_args.images_path) if x.name.endswith(("png", "jpg", "jpeg"))]

    for i, image_path in tqdm(enumerate(images_path), desc="testing..."):
        img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR)
        img = np.float32(img_raw)
        im_shape = img.shape
        im_size_min = np.min(im_shape[0:2])
        im_size_max = np.max(im_shape[0:2])
        resize = float(target_size) / float(im_size_min)

        # prevent bigger axis from being more than max_size:
        if np.round(resize * im_size_max) > max_size:
            resize = float(max_size) / float(im_size_max)
        if origin_size:
            resize = 1

        if resize != 1:
            img = cv2.resize(img, None, None, fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR)

        im_height, im_width, _ = img.shape
        scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
        img -= (104, 117, 123)
        img /= (57, 57, 58)
        img = img.transpose(2, 0, 1)
        img = torch.from_numpy(img).unsqueeze(0)
        img = img.to(device)
        scale = scale.to(device)

        if cfg['gender']:
            loc, conf, landms, gender = net(img)  # forward pass
        else:
            loc, conf, landms = net(img)  # forward pass

        priorbox = PriorBox(cfg, image_size=(im_height, im_width))
        priors = priorbox.forward()
        priors = priors.to(device)
        prior_data = priors.data
        boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
        boxes = boxes * scale / resize
        boxes = boxes.cpu().numpy()
        scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
        if cfg['gender']:
            genders = gender.squeeze(0).data.cpu().numpy()

        landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
        scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                               img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                               img.shape[3], img.shape[2]])
        scale1 = scale1.to(device)
        landms = landms * scale1 / resize
        landms = landms.cpu().numpy()

        # ignore low scores
        inds = np.where(scores > confidence_threshold)[0]
        boxes = boxes[inds]
        landms = landms[inds]
        scores = scores[inds]
        if cfg['gender']:
            genders = genders[inds]

        # keep top-K before NMS
        order = scores.argsort()[::-1]
        # order = scores.argsort()[::-1][:args.top_k]
        boxes = boxes[order]
        landms = landms[order]
        scores = scores[order]
        if cfg['gender']:
            genders = genders[order]

        # do NMS
        dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
        keep = py_cpu_nms(dets, nms_threshold)
        # keep = nms(dets, args.nms_threshold,force_cpu=args.cpu)
        dets = dets[keep, :]
        landms = landms[keep]
        if cfg['gender']:
            genders = genders[keep]
        # keep top-K faster NMS
        # dets = dets[:args.keep_top_k, :]
        # landms = landms[:args.keep_top_k, :]
        if cfg['gender']:
            dets = np.concatenate((dets, landms, genders * 10000), axis=1).astype(np.float32)
        else:
            dets = np.concatenate((dets, landms), axis=1).astype(np.float32)

        for b in dets:
            if b[4] < vis_threshold:
                continue
            text = "{:.4f}".format(b[4])
            b = list(map(int, b))
            cv2.rectangle(img_raw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2)
            cx = b[0]
            cy = b[1] + 12
            cv2.putText(img_raw, text, (cx, cy),
                        cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
            if cfg['gender']:
                gender = (b[15] / 10000, b[16] / 10000)
                gender_str = "male:" if gender.index(max(gender)) else "female:"
                gender_str += str(max(gender))

                cv2.putText(img_raw, gender_str, (cx, cy - 15),
                            cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 0))
            # landms
            cv2.circle(img_raw, (b[5], b[6]), 3, (0, 0, 255), 3)
            cv2.circle(img_raw, (b[7], b[8]), 3, (0, 255, 255), 3)
            cv2.circle(img_raw, (b[9], b[10]), 3, (255, 0, 255), 3)
            cv2.circle(img_raw, (b[11], b[12]), 3, (0, 255, 0), 3)
            cv2.circle(img_raw, (b[13], b[14]), 3, (255, 0, 0), 3)

        if not os.path.exists(_args.save):
            os.makedirs(_args.save)
        name = os.path.join(_args.save, image_path.split('/')[-1].split('.')[0] + "_{}.jpg".format(cfg["name"]))
        cv2.imwrite(name, img_raw)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="RetinaFace tester.")

    parser.add_argument(
        "--ckpt",
        type=str,
        help="path to the model checkpoint",
    )

    parser.add_argument(
        "--cfg",
        type=str,
        default="",
        help="path to the config yaml file",
    )

    parser.add_argument(
        "--device",
        type=str,
        default="cpu",
        help="path to the config yaml file",
    )

    parser.add_argument(
        "--confidence_threshold",
        type=float,
        default=0.02,
        help="confidence thresh",
    )

    parser.add_argument(
        "--nms_threshold",
        type=float,
        default=0.04,
        help="nms thresh",
    )

    parser.add_argument(
        "--vis_threshold",
        type=float,
        default=0.5,
        help="visual thresh",
    )

    parser.add_argument(
        "--target_size",
        type=int,
        default=512,
        help="target output size",
    )

    parser.add_argument(
        "--max_size",
        type=int,
        default=512,
        help="max output size",
    )

    parser.add_argument(
        '--origin_size',
        action="store_true",
        help='keep origin size.')

    parser.add_argument(
        "--images_path", type=str, help="images path to be test."
    )

    parser.add_argument(
        "--save",
        type=str,
        default="./result",
        help="path to save the result",
    )

    args = parser.parse_args()

    test(args)

    print("Test Finished!")


N = int(input())
V = list(map(int, input().split()))

d1 = {0:0}
d2 = {0:0}
for i, v in enumerate(V):
    d = d2 if i&1 else d1
    if v not in d: d[v]=0
    d[v]+=1

d1 = sorted(list(d1.items()), key=lambda x:-x[1])
d2 = sorted(list(d2.items()), key=lambda x:-x[1])

if d1[0][0]!=d2[0][0]:
    print(N-d1[0][1]-d2[0][1])
else:
    print(min(N-d1[1][1]-d2[0][1], N-d1[0][1]-d2[1][1]))


import yfinance as yf
import pandas as pd

tickers = ['BBDC4.SA', 'BBAS3.SA', 'ITUB4.SA']

start_date = '2018-01-01'

end_date = '2023-04-01'

df = yf.download(tickers, start=start_date, end=end_date)['Adj Close']
jls_extract_var = 'Adj Close'
df_mensal = df = yf.download(tickers, start=start_date, end=end_date).resample("Y").last()

retorno_mensal = df_mensal.pct_change()

df = pd.DataFrame(df)
print(df_mensal)


links = []
stack = []
id = 0

def hanoi(N, start, sub, end):
    global id
    id += 1
    stack.append(id)

    if (len(stack) > 1):
        links.append({"source" : stack[-2], "target" : stack[-1], "move": N, "from" : start, "via" : sub, "to" : end, "type" : "call"})
    else:
        links.append({"source" : "", "target" : stack[-1], "move": N, "from" : start, "via" : sub, "to" : end, "type" : "call"})

    if (N == 1):
        stack.pop()
        return None

    hanoi(N-1, start, end, sub)
    hanoi(N-1, sub, start, end)

    stack.pop()


import time
from fuzzywuzzy import process
from cachetools import TTLCache
from bs4 import BeautifulSoup

from rsi.conf import DEFAULT_RSI_URL
from .session import RSISession

DEFAULT_CACHE_TTL = 300

class OrgAPI(object):
    def __init__(self, symbol, session=None, admin_mode=False, url=DEFAULT_RSI_URL, endpoint='/orgs',
                 members_endpoint='/api/orgs/getOrgMembers', cache_ttl=DEFAULT_CACHE_TTL):
        self.symbol = symbol
        self.url = url.rstrip('/')
        self.endpoint = endpoint
        self.members_endpoint = members_endpoint
        self.admin_mode = admin_mode
        self.session = session or RSISession(url=url)

        self.org_url = "{}/{}/{}".format(self.url, self.endpoint.lstrip('/'), symbol)
        self.members_api = "{}/{}".format(self.url, self.members_endpoint.lstrip('/'))
        self._ttlcache = TTLCache(maxsize=1, ttl=cache_ttl)

        self._update_details()   # pull and cache the org details which will raise 404 if not found

    def clear_cache(self):
        """ Resets the cache """
        for key in self._ttlcache.keys():
            del self._ttlcache[key]

    def _cache(self, key, update_func, *args, **kwargs):
        if key not in self._ttlcache:
            self._ttlcache[key] = update_func(*args, **kwargs)
        return self._ttlcache[key]

    def _update_members(self, search):
        members = []

        params = {
            'symbol': self.symbol,
            'search': search,
            'page': 1
        }

        if self.admin_mode:
            params['admin_mode'] = 1

        # this just gets us going
        totalsize = 1
        members_scanned = 0

        while members_scanned < totalsize:
            r = self.session.post(self.members_api, data=params)

            if r.status_code == 200:
                r = r.json()
                if r is None:
                    continue

                if 'data' in r and r['data'] and 'totalrows' in r['data']:
                    totalsize = int(r['data']['totalrows'])

                if r['success'] == 1:
                    apisoup = BeautifulSoup(r['data']['html'], features='html.parser')
                    for member in apisoup.select('.member-item'):
                        members_scanned += 1
                        if member.select('.member-visibility-restriction'):
                            print('skipping hidden member')
                            continue

                        members.append({
                            'name': member.select_one('.name').text,
                            'handle': member.select_one('.nick').text,
                            'avatar': '{}{}'.format(self.url, member.select_one('img').attrs['src']),
                            'affiliate': member.select_one('.title').text == 'Affiliate',
                            'rank': member.select_one('.rank').text,
                            'roles': [_.text for _ in member.select('.rolelist .role')],
                            'url': '{}{}'.format(self.url, member.select_one('a.membercard').attrs['href']),

                            # defaults for things online admins will be able to get the real values of
                            'id': '',
                            'visibility': 'Membership: Visible',
                            'last_online': '',
                        })

                        if self.admin_mode:
                            members[-1].update({
                                'id': member.attrs.get('data-member-id', ''),
                                'last_online': member.select_one('.frontinfo .lastonline').text,
                                'visibility': member.select_one('.frontinfo .visibility').text,
                            })

                    params['page'] = params['page'] + 1
                else:
                    raise ValueError('Received error fetching Org members: {}'.format(r))
            else:
                raise Exception('Received error fetching Org members: {}'.format(r.status_code))
            time.sleep(0.5)
        return members

    def _update_details(self):
        r = self.session.get(self.org_url)
        data = {}
        r.raise_for_status()

        orgsoup = BeautifulSoup(r.text, features='html.parser')
        data['banner'] = '{}{}'.format(self.url, orgsoup.select_one('.banner img')['src'])
        data['logo'] = '{}{}'.format(self.url, orgsoup.select_one('.logo img')['src'])
        data['name'], data['symbol'] = orgsoup.select_one('.inner h1').text.split(' / ')
        data['model'] = orgsoup.select_one('.inner .tags .model').text
        data['commitment'] = orgsoup.select_one('.inner .tags .commitment').text
        data['primary_focus'] = orgsoup.select_one('.inner .focus .primary img')['alt']
        data['secondary_focus'] = orgsoup.select_one('.inner .focus .secondary img')['alt']
        data['join_us'] = orgsoup.select_one('.join-us .body').text.strip()
        return data

    def search(self, handle, score_cutoff=80, limit=None):
        """
        Return members that match the given handle using fuzzy matching.

        :param handle: Handle to match
        :param score_cutoff: minimum matching score to return
        :param limit: limit the number of matches found
        :return: List of matched results in the form of [(dict, int)] where dict is the ship data and in is the
                 matching confidence
        """
        choices = {i: _['handle'] for i, _ in enumerate(self.members)}
        return [(self.members[_[2]], _[1]) for _ in process.extractBests(handle, choices,
                                                                         score_cutoff=score_cutoff, limit=limit)]

    def search_one(self, handle):
        """
        Return the first member that matches the given handle using fuzzy matching, or None

        :param handle: Handle to match
        :return: The best matching member, or None
        """
        choices = self.search(handle, limit=1)
        if choices:
            return choices[0][0]
        return None

    @property
    def members(self):
        return self._cache('members', self._update_members, search='')

    @property
    def details(self):
        return self._cache('details', self._update_details)

    @property
    def banner(self):
        return self.details['banner']

    @property
    def logo(self):
        return self.details['logo']

    @property
    def name(self):
        return self.details['name']

    @property
    def model(self):
        return self.details['model']

    @property
    def commitment(self):
        return self.details['commitment']

    @property
    def primary_focus(self):
        return self.details['primary_focus']

    @property
    def secondary_focus(self):
        return self.details['secondary_focus']

    @property
    def spectrum_url(self):
        return '{}/spectrum/community/{}'.format(self.url, self.symbol)

    @property
    def join_us(self):
        return self.details['join_us']


## https://cafe.naver.com/largePython

import random
## 함수
def display() :
    for i in range(ROW):
        for k in range(COL):
            print("%3d " % image[i][k], end='')
        print()
    print()
## 변수
ROW, COL = 5, 5
image = None

## 메인
## 메모리 할당
image = [ ]
tmpAry = []
for i in range(ROW) :
    for k in range(COL) :
        tmpAry.append(0)
    image.append(tmpAry)
    tmpAry=[]
## 파일 --> 메모리로 로딩(Loading)
for i in range(ROW) :
    for k in range(COL) :
        pixel = random.randint(0,255)
        image[i][k] = pixel
display()
## 영상 처리
#(1) 영상을 50 밝게 처리하자.
# for i in range(ROW) :
#     for k in range(COL) :
#         if (image[i][k] + 50 > 255) :
#             image[i][k] = 255
#         else :
#             image[i][k] += 50
# display()

# 퀴즈 : 100 어둡게
# for i in range(ROW) :
#     for k in range(COL) :
#         if (image[i][k] - 100 < 0) :
#             image[i][k] = 0
#         else :
#             image[i][k] = image[i][k] - 100
# display()

# # 퀴즈 : 완전 흑백 처리
# for i in range(ROW) :
#     for k in range(COL) :
#         if (image[i][k] < 127) :
#             image[i][k] = 255
#         else :
#             image[i][k] = 0
# display()

# 퀴즈 : 완전 흑백 처리
# hap = 0
# for i in range(ROW) :
#     for k in range(COL) :
#         hap += image[i][k]
# avg = hap / (ROW * COL)
#
# for i in range(ROW) :
#     for k in range(COL) :
#         if (image[i][k] < avg) :
#             image[i][k] = 255
#         else :
#             image[i][k] = 0
# display()

# 퀴즈 : 반전하기
for i in range(ROW) :
    for k in range(COL) :
        image[i][k] = 255 - image[i][k]

display()

## 과제1 : 흑백처리를 중앙값으로 하기 (퀵소트 구현, 선택 정렬)


import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import Input, Output, State
import pandas as pd

########### Define a few variables ######

tabtitle = 'Dash Template'
sourceurl = 'https://plot.ly/python/choropleth-maps/'
githublink = 'https://github.com/austinlasseter/dash-template'

########### Initiate the app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.title=tabtitle

########### Layout

app.layout = html.Div(children=[
    html.H1('This is the title'),
    html.Div(children=['Stuff will go here']),
    html.A('Code on Github', href=githublink),
    html.Br(),
    html.A("Data Source", href=sourceurl),
    ]
)

############ Callbacks

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


from typing import Annotated

from aioredis import Redis
from fastapi import Depends, BackgroundTasks
from sqlalchemy.ext.asyncio import AsyncSession

from app.core import get_aioredis, get_async_session
from app.models import Dish, Menu, Submenu
from app.repositories.db_repository import (
    DishRepository,
    MenuRepository,
    SubmenuRepository,
)
from app.repositories.redis_repository import RedisBaseRepository
from app.services.base import BaseService

async_session = Annotated[AsyncSession, Depends(get_async_session)]
redis = Annotated[Redis, Depends(get_aioredis)]

class MenuService(BaseService):
    def __init__(self, session: async_session, redis: redis, bg_tasks: BackgroundTasks):
        super().__init__(MenuRepository(session), RedisBaseRepository(redis, 'menu:'), bg_tasks)
        self.submenu_redis = RedisBaseRepository(redis, 'submenu:')
        self.dish_redis = RedisBaseRepository(redis, 'dish:')

    async def set_cache_create(self, menu: Menu) -> None:
        await super().set_cache(menu)

    async def set_cache_update(self, menu: Menu) -> None:
        await super().set_cache(menu)

    async def set_cache_delete(self, menu: Menu) -> None:
        for submenu in menu.submenus:
            for dish in submenu.dishes:
                await self.dish_redis.delete_obj(dish)
            await self.submenu_redis.delete_obj(submenu)
        await self.redis.delete_obj(menu)

class SubmenuService(BaseService):
    def __init__(self, session: async_session, redis: redis, bg_tasks: BackgroundTasks):
        super().__init__(SubmenuRepository(session), RedisBaseRepository(redis, 'submenu:'), bg_tasks)
        self.menu_db = MenuRepository(session)
        self.menu_redis = RedisBaseRepository(redis, 'menu:')
        self.dish_redis = RedisBaseRepository(redis, 'dish:')

    async def set_cache_create(self, submenu: Submenu) -> None:
        menu: Menu = await self.menu_db.get_or_404(pk=submenu.menu_id)
        for redis, item in ((self.menu_redis, menu), (self.redis, submenu)):
            await redis.set_obj(item)  # type: ignore [type-var]

    async def set_cache_update(self, submenu: Submenu) -> None:
        await super().set_cache(submenu)

    async def set_cache_delete(self, submenu: Submenu) -> None:
        for dish in submenu.dishes:
            await self.dish_redis.delete_obj(dish)
        await self.redis.delete_obj(submenu)
        # refreshing related models
        menu: Menu = await self.menu_db.get_or_404(pk=submenu.menu_id)
        await self.menu_redis.set_obj(menu)

class DishService(BaseService):
    def __init__(self, session: async_session, redis: redis, bg_tasks: BackgroundTasks):
        super().__init__(DishRepository(session), RedisBaseRepository(redis, 'dish:'), bg_tasks)
        self.submenu_db = SubmenuRepository(session)
        self.submenu_redis = RedisBaseRepository(redis, 'submenu:')
        self.menu_db = MenuRepository(session)
        self.menu_redis = RedisBaseRepository(redis, 'menu:')

    async def set_cache_create(self, dish: Dish) -> None:
        submenu: Submenu = await self.submenu_db.get_or_404(pk=dish.submenu_id)
        menu: Menu = await self.menu_db.get_or_404(pk=submenu.menu_id)
        for redis, item in ((self.menu_redis, menu), (self.submenu_redis, submenu), (self.redis, dish)):
            await redis.set_obj(item)  # type: ignore [type-var]

    async def set_cache_update(self, dish: Dish) -> None:
        await super().set_cache(dish)

    async def set_cache_delete(self, dish: Dish) -> None:
        await self.redis.delete_obj(dish)
        submenu: Submenu = await self.submenu_db.get_or_404(pk=dish.submenu_id)
        menu: Menu = await self.menu_db.get_or_404(pk=submenu.menu_id)
        for redis, item in ((self.menu_redis, menu), (self.submenu_redis, submenu)):
            await redis.set_obj(item)  # type: ignore [type-var]


import os

TEST_FG_NAME = 'lgtester'
TRUE_FG_NAME = 'psm-hackathon'
ANNOTATED_XLSX_REL_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',
                                                       'PSM-hackathon-leaf-flows_annotated.xlsx'))

catalog_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'catalog'))

USLCI_REF = 'local.uslci.olca'
USEEIO_REF = 'local.usepa.useeio.1.1'
ELCD_REF = 'local.elcd.3.2'


def solution(dartResult):
    stack = []
    bonus = {'S': 1, 'D': 2, 'T': 3}
    # 10은 Z로 임시 변경 ('1','0'으로 인식 방지)
    dartResult = dartResult.replace('10', 'Z')

    for i in dartResult:
        if i == 'Z':
            stack.append(10)
        elif i.isdigit():
            stack.append(int(i))
        elif i in ('S', 'D', 'T'):  # 보너스일 경우
            stack[-1] = stack[-1]**bonus[i]
        elif i == '*':  # 스타상
            num = stack.pop()
            if len(stack):
                stack[-1] *= 2
            stack.append(num*2)
        elif i == '#':  # 아차상
            stack[-1] *= -1

    return sum(stack)


import os

dir = 'data_text_combine'

list_file = []
for file in os.listdir(os.path.join(dir, "images")):
    list_file.append(os.path.join(dir, "images") + "/" + file)

number_train = int(len(list_file) * 0.8)

with open("train.lst", "w") as f:
    for i in range(number_train):
        f.write(list_file[i] + "\n")

with open("val.lst", "w") as f:
    for i in range(number_train, len(list_file), 1):
        f.write(list_file[i] + "\n")

# import numpy as np
# import tensorflow as tf
#
# print("Hi there")
#
# pred = np.array([[31, 23,  4, 24, 27, 34],
#                 [18,  3, 25,  0,  6, 35],
#                 [28, 14, 33, 22, 20,  8],
#                 [13, 30, 21, 19,  7,  9],
#                 [16,  1, 26, 32,  2, 29],
#                 [17, 12,  5, 11, 10, 15]])
#
# print("Hello")
# print(tf.argmax(pred, 1))


from django.urls import path

from . import views

app_name = 'navi'

urlpatterns = [
	path('',views.index,name='hi'),
	path('msgb/',views.msgb,name='hi'),
	path('msgb/save/',views.msgsave,name='save'),
	path('wttr/',views.wttr,name='save'),
	path('jcount/',views.viscount,name='jcount')

]


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import NoteCreationForm

@login_required
def notes(request):
    author = request.user
    user_notes = author.notes_set.all()
    context = {'notes': user_notes}
    return render(request, 'notes/notes.html', context)

@login_required
def note_detail(request, pk):
    author = request.user
    user_note = author.notes_set.get(id=pk)
    context = {'note': user_note}
    return render(request, 'notes/note.html', context)

@login_required
def add_note(request):
    author = request.user
    if request.method == 'POST':
        form = NoteCreationForm(request.POST)
        if form.is_valid():
            note = form.save(commit=False)
            note.author = author
            note.save()
            return redirect('notes')
    else:
        form = NoteCreationForm()
    context = {'form': form}
    return render(request, 'notes/note_form.html', context)

@login_required
def update_note(request, pk):
    author = request.user
    note = author.notes_set.get(id=pk)
    if request.method == 'POST':
        form = NoteCreationForm(request.POST, instance=note)
        if form.is_valid():
            form.save()
            return redirect('notes')
    else:
        form = NoteCreationForm(instance=note)
    context = {'form': form}
    return render(request, 'notes/note_form.html', context)

@login_required
def delete_note(request, pk):
    author = request.user
    note = author.notes_set.get(id=pk)
    if request.method == 'POST':
        note.delete()
        return redirect('notes')
    context = {'note': note}
    return render(request, 'notes/delete_note.html', context)


N=int(input())+1 #### Записываем наше простое число
A = [True] * N ### Числа - индексы массива, значение в массиве говорит нам простое оно или нет
A[0]=A[1]=False ### 0,1 - не простые
for k in range(2,N):  ### Действуем след образом:
    if A[k]:          ### Идем по простым числам начиная с двойки, так наталкиваясь на очередное простое (True)
        for m in range (2*k,N,k): ### вычеркиваем все числа ему кратные вплоть до N
            A[m] = False
for i in range(N): ### Вывод данных
    if A[i]:
        print(i, end= ' ')

17(2). Решето Эратосфена.py

17(2). Решето Эратосфена.py


from controller.exceptions import StartupException

DEVICE_PREFIX = "device="
SLIDER_PREFIX = "slider-device="

class DeviceManager:

    def __init__(self, path: str) -> None:
        self._path = path

    def read_port_from_file(self) -> str:
        try:
            f = open(self._path, "r")
            lines = f.readlines()
            f.close()
        except FileNotFoundError:
            raise StartupException(f"Device file '{self._path}' does not exist in the filesystem")

        device = None
        slider = None

        for line in lines:
            if line.startswith(DEVICE_PREFIX):
                device = line[len(DEVICE_PREFIX):].strip()
            if line.startswith(SLIDER_PREFIX):
                slider = line[len(SLIDER_PREFIX):].strip()

        if (not device) or (not slider):
            raise StartupException(f"Invalid device specified in config '{self._path}'")

        return device, slider


# -*- coding: utf-8 -*-
"""
Created on Wed Apr  3 21:58:54 2019

@author: kartik
"""

import speech_recognition as sr

r = sr.Recognizer()
mic_list = sr.Microphone.list_microphone_names()

with sr.Microphone(device_index = 1) as source:
    r.adjust_for_ambient_noise(source)
    print ("Say Something")
    i=1

    audio = r.listen(source,phrase_time_limit=2)

    try:
        text = r.recognize_google(audio)
        print( "you said: " + text )

    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")

    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

print("copmpleted")


#!/usr/bin/python
from src.Video import Video

import ffmpeg

fname = 'data/FIT_building.mp4'
video = Video(fname)

# video.stream = video.stream.trim(start_frame=0, end_frame=100)

video.addAudio("sounds/time/HansZimmer3m10s.mp3", \
    duration=video.duration, fade_in=2, fade_out=1)

scaleLogo = 0.12
video.addLogos( ["logos/mpi.png", "logos/EXC_IntCDC_Logo.png", \
        "logos/UniStuttgart.png", "logos/tub_light.png"], scale = scaleLogo)

video.output('data/FIT_building_annotated.mp4')


import socket
from numeroPrimo import GeradorNumeroPrimo
from gerarChaves import Chaves
from criptografia import Criptografia

p = GeradorNumeroPrimo()
numero_p = p.numero_primo
print('\n P :',str(numero_p))

q = GeradorNumeroPrimo()
numero_q = q.numero_primo
print('\n Q :',str(numero_q))

chaves = Chaves(numero_p, numero_q)
chaves.gerar_chaves()

def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5013  # socket server port number

    client_socket = socket.socket()  # instantiate
    client_socket.connect((host, port))  # connect to the server

    message = input(" -> ")  # take input

    while message.lower().strip() != "bye":
        message.encripta_mensagem(message)
        client_socket.send(message.encode())  # send message
        data = client_socket.recv(1024).decode() # receive response

        print("Received from server: " + data)  # show in terminal

        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection

if __name__ == "__main__":
    client_program()


"""The workflow execution:
1) Get all the EQUITY_* data of the week and collect it into one CSV file WEEKLY_*
2) Create a back of all the EQUITY_* files in a back_up location.
3) Remove the daily logs of the week and the EQUITY_* files.
4) Convert WEEKLY_* files in JSON.
5) Insert it inES index as docs. If it fails retain the WEEKLY_JSON_TMP"""

from datetime import datetime
import glob
import shutil
import os
import pandas as pd
from elasticsearch.exceptions import ConnectionTimeout, ConflictError
from LoggerApi.Logger import Logger
from Harvester import Utils

LOGGER = Logger(__file__, "WEEKLY_GATHERING")
DESTINATION_FOLDER = "/backup_stock_data/"
DAILY_DATA_FILES = [daily_data for daily_data in
                    glob.glob(".\\resources\\*.csv") if "EQUITY" in daily_data]
LOG_FILES = [logs for logs in
             glob.glob(os.path.dirname(os.path.dirname(__file__)) + "\\*LOG*") if "LOG" in logs]
FIRST_FILE_INDICATOR = True
FINAL_DATA = None

for file in DAILY_DATA_FILES:
    date = file.split("\\")[2].split(".")[0].split("_")[-1:][0]
    daily_df = pd.read_csv(file)
    daily_df['processingDate'] = date
    if FIRST_FILE_INDICATOR:
        FIRST_FILE_INDICATOR = False
        column_names = daily_df.columns
        FINAL_DATA = daily_df.copy()
        LOGGER.add("INFO", "First files parsed."
                           " Date:{}".format(date[:2] + "/" + date[2:4] + "/" + date[4:]))
        continue
    FINAL_DATA = pd.concat([FINAL_DATA, daily_df], axis=0)
    LOGGER.add("INFO",
               "Data added for Date:{} ".format(date[:2] + "/" + date[2:4] + "/" + date[4:]))

try:
    FINAL_DATA.to_csv(".\\resources\\WEEKLY_{}.csv".format(datetime.now().strftime('%d%m%Y')),
                      index=False)
    LOGGER.add("INFO", "COMPLETED")
    LOGGER.add("INFO", "Moving the daily data of the week to the back-up folder.")
    for file in DAILY_DATA_FILES:  # move the daily collected files to backup folder
        try:
            shutil.move(file, DESTINATION_FOLDER)
        except shutil.Error:
            LOGGER.add("ERROR", "The file already exists in back-up")
            os.remove(file)
            LOGGER.add("INFO", "The duplicates removed from cwd")
    for file in LOG_FILES:  # removing the logs for the week
        os.remove(file)

    Utils.weekly_report_to_json()  # Converting all the csv weekly report to JSON file
    LOGGER.add("INFO", "Converted to JSON successfully.")

    try:
        Utils.insert_to_es_index(LOGGER)  # Insert the file to ES
        LOGGER.add("INFO", "Insertion into ES was successful")
        for file in [weekly_json for weekly_json in glob.glob(".\\resources\\*JSON*")]:
            os.remove(file)
    except ConnectionTimeout:
        LOGGER.add("ERROR", "Connection Timed out while inserting")
    except ConflictError:
        LOGGER.add("ERROR", "Error while establishing connection. Please check ES host is up.")

    for file in [daily_data for daily_data in glob.glob(".\\resources\\*.csv") if
                 "WEEKLY" in daily_data]:  # Removing the temporary WEEKLY* csv files
        os.remove(file)
    LOGGER.add("INFO", "Weekly temp csv files removed.")
except NameError:
    LOGGER.add("ERROR", "There is not data present in the resource folder.")