from enum import Enum
from typing import Dict, List, Optional, Tuple, Union

from boa3.internal.model.builtin.builtincallable import IBuiltinCallable
from boa3.internal.model.builtin.classmethod import *
from boa3.internal.model.builtin.compile_time import *
from boa3.internal.model.builtin.contract import *
from boa3.internal.model.builtin.decorator import *
from boa3.internal.model.builtin.internal import *
from boa3.internal.model.builtin.interop.interop import Interop
from boa3.internal.model.builtin.math import *
from boa3.internal.model.builtin.method import *
from boa3.internal.model.callable import Callable
from boa3.internal.model.event import Event as EventSymbol
from boa3.internal.model.identifiedsymbol import IdentifiedSymbol
from boa3.internal.model.imports.package import Package
from boa3.internal.model.type.collection.sequence.ecpointtype import ECPointType
from boa3.internal.model.type.collection.sequence.uint160type import UInt160Type
from boa3.internal.model.type.collection.sequence.uint256type import UInt256Type
from boa3.internal.model.type.itype import IType
from boa3.internal.model.type.math import Math
from boa3.internal.model.type.neo import *

class BoaPackage(str, Enum):
    CompileTime = 'compile_time'
    Contract = 'contract'
    Interop = 'interop'
    Type = 'type'
    TypeHelper = 'helper'
    VM = 'vm'

class Builtin:
    @classmethod
    def get_symbol(cls, symbol_id: str) -> Optional[Callable]:
        for method in cls._python_builtins:
            if isinstance(method, IBuiltinCallable) and method.identifier == symbol_id:
                return method

    @classmethod
    def get_by_self(cls, symbol_id: str, self_type: IType) -> Optional[Callable]:
        for name, method in vars(cls).items():
            if (isinstance(method, IBuiltinMethod)
                    and method.identifier == symbol_id
                    and method.validate_self(self_type)):
                return method

    # builtin method
    Abs = AbsMethod()
    Exit = ExitMethod()
    IsInstance = IsInstanceMethod()
    Len = LenMethod()
    NewEvent = CreateEventMethod()
    Max = MaxIntMethod()
    Min = MinIntMethod()
    Print = PrintMethod()
    ScriptHashMethod_ = ScriptHashMethod()
    StrSplit = StrSplitMethod()
    Sum = SumMethod()
    ToHexStr = ToHexStrMethod()

    # python builtin class constructor
    Bool = BoolMethod()
    ByteArray = ByteArrayMethod()
    ByteArrayEncoding = ByteArrayEncodingMethod()
    Exception = ExceptionMethod()
    IntByteString = IntByteStringMethod()
    IntInt = IntIntMethod()
    ListBytesString = ListBytesStringMethod()
    ListGeneric = ListGenericMethod()
    ListMapping = ListMappingMethod()
    ListSequence = ListSequenceMethod()
    Range = RangeMethod()
    Reversed = ReversedMethod()
    StrBool = StrBoolMethod()
    StrBytes = StrBytesMethod()
    StrInt = StrIntMethod()
    Super = SuperMethod()

    # python class method
    BytesStringIndex = IndexBytesStringMethod()
    BytesStringIsDigit = IsDigitMethod()
    BytesStringJoin = JoinMethod()
    BytesStringLower = LowerMethod()
    BytesStringStartswith = StartsWithMethod()
    BytesStringStrip = StripMethod()
    BytesStringUpper = UpperMethod()
    CountSequenceGeneric = CountSequenceGenericMethod()
    CountSequencePrimitive = CountSequencePrimitiveMethod()
    CountStr = CountStrMethod()
    Copy = CopyListMethod()
    SequenceAppend = AppendMethod()
    SequenceClear = ClearMethod()
    SequenceExtend = ExtendMethod()
    SequenceIndex = IndexSequenceMethod()
    SequenceInsert = InsertMethod()
    SequencePop = PopSequenceMethod()
    SequenceRemove = RemoveMethod()
    SequenceReverse = ReverseMethod()
    DictKeys = MapKeysMethod()
    DictPop = PopDictMethod()
    DictPopDefault = PopDictDefaultMethod()
    DictValues = MapValuesMethod()

    # custom class methods
    ConvertToBytes = ToBytesMethod
    ConvertToInt = ToIntMethod
    ConvertToStr = ToStrMethod
    ConvertToBool = ToBoolMethod

    # builtin decorator
    ClassMethodDecorator = ClassMethodDecorator()
    InstanceMethodDecorator = InstanceMethodDecorator()
    PropertyDecorator = PropertyDecorator()
    StaticMethodDecorator = StaticMethodDecorator()

    _python_builtins: List[IdentifiedSymbol] = [Abs,
                                                ByteArray,
                                                ByteArrayEncoding,
                                                BytesStringIndex,
                                                BytesStringIsDigit,
                                                BytesStringJoin,
                                                BytesStringLower,
                                                BytesStringStartswith,
                                                BytesStringStrip,
                                                BytesStringUpper,
                                                ClassMethodDecorator,
                                                ConvertToBool,
                                                ConvertToBytes,
                                                ConvertToInt,
                                                ConvertToStr,
                                                Copy,
                                                CountSequenceGeneric,
                                                CountSequencePrimitive,
                                                CountStr,
                                                DictKeys,
                                                DictValues,
                                                Exception,
                                                Exit,
                                                IsInstance,
                                                Len,
                                                Max,
                                                Min,
                                                Print,
                                                PropertyDecorator,
                                                Range,
                                                Reversed,
                                                ScriptHashMethod_,
                                                SequenceAppend,
                                                SequenceClear,
                                                SequenceExtend,
                                                SequenceIndex,
                                                SequenceInsert,
                                                SequencePop,
                                                SequenceRemove,
                                                SequenceReverse,
                                                StaticMethodDecorator,
                                                StrSplit,
                                                Sum,
                                                Super,
                                                ]

    @classmethod
    def interop_symbols(cls, package: str = None) -> Dict[str, IdentifiedSymbol]:
        return {symbol.raw_identifier if hasattr(symbol, 'raw_identifier') else symbol.identifier: symbol
                for symbol in Interop.interop_symbols(package)}

    # boa builtin decorator
    ContractInterface = ContractDecorator()
    ContractMethodDisplayName = DisplayNameDecorator()
    Metadata = MetadataDecorator()
    Public = PublicDecorator()

    # boa builtin type
    Event = EventType
    UInt160 = UInt160Type.build()
    UInt256 = UInt256Type.build()
    ECPoint = ECPointType.build()
    NeoAccountState = NeoAccountStateType.build()
    Opcode = OpcodeType.build()
    Address = AddressType.build()
    BlockHash = BlockHashType.build()
    PublicKey = PublicKeyType.build()
    ScriptHashType_ = ScriptHashType.build()
    ScriptHashLittleEndian = ScriptHashLittleEndianType.build()
    TransactionId = TransactionIdType.build()

    # boa events
    Nep11Transfer = Nep11TransferEvent()
    Nep17Transfer = Nep17TransferEvent()

    # boa smart contract methods
    Abort = AbortMethod()
    Env = EnvProperty.build()

    # region boa builtin modules

    BuiltinMathCeil = DecimalCeilingMethod()
    BuiltinMathFloor = DecimalFloorMethod()

    MathModule = Package(identifier='math',
                         methods=[Math.Sqrt,
                                  BuiltinMathCeil,
                                  BuiltinMathFloor])

    _symbols = [Env]
    _modules = [MathModule]

    # endregion

    boa_builtins: List[IdentifiedSymbol] = []
    boa_builtins.extend(_modules)
    boa_builtins.extend(_symbols)

    metadata_fields: Dict[str, Union[type, Tuple[type]]] = {
        'name': str,
        'source': (str, type(None)),
        'supported_standards': list,
        'trusts': list,
        'author': (str, type(None)),
        'email': (str, type(None)),
        'description': (str, type(None)),
        'extras': dict
    }

    @classmethod
    def boa_symbols(cls) -> Dict[str, IdentifiedSymbol]:
        return {symbol.identifier: symbol for symbol in cls.boa_builtins}

    @classmethod
    def package_symbols(cls, package: str = None) -> Dict[str, IdentifiedSymbol]:
        if package in BoaPackage.__members__.values():
            return {symbol.identifier: symbol for symbol in cls._boa_symbols[package]}

        return cls.boa_symbols()

    @classmethod
    def builtin_events(cls) -> List[EventSymbol]:
        lst: List[EventSymbol] = [event for event in cls.boa_builtins if isinstance(event, EventSymbol)]

        for symbols in cls._boa_symbols.values():
            lst.extend([event for event in symbols if isinstance(event, EventSymbol)])

        return lst

    _builtin_type_package_symbols = [ECPoint,
                                     UInt160,
                                     UInt256,
                                     Event,
                                     Address,
                                     BlockHash,
                                     PublicKey,
                                     ScriptHashType_,
                                     ScriptHashLittleEndian,
                                     TransactionId,
                                     Package(identifier=BoaPackage.TypeHelper,
                                             methods=[ConvertToBool,
                                                      ConvertToBytes,
                                                      ConvertToInt,
                                                      ConvertToStr,
                                                      ]
                                             )
                                     ]

    _boa_symbols: Dict[BoaPackage, List[IdentifiedSymbol]] = {
        BoaPackage.Contract: [Abort,
                              NeoAccountState,
                              Nep11Transfer,
                              Nep17Transfer,
                              ScriptHashMethod_,
                              ToHexStr,
                              ],
        BoaPackage.Interop: Interop.package_symbols,
        BoaPackage.Type: _builtin_type_package_symbols,
        BoaPackage.VM: [Opcode
                        ],
        BoaPackage.CompileTime: [ContractInterface,
                                 ContractMethodDisplayName,
                                 Metadata,
                                 NeoMetadataType,
                                 Public,
                                 NewEvent
                                 ]
    }

    _internal_methods = [InnerDeployMethod.instance()
                         ]
    internal_methods = {method.raw_identifier: method for method in _internal_methods}


import os
import subprocess
import tempfile
import unittest

from unittest import mock

from acloud import errors
from acloud.create import local_image_local_instance
from acloud.list import instance
from acloud.list import list as list_instance
from acloud.internal import constants
from acloud.internal.lib import driver_test_lib
from acloud.internal.lib import utils

class LocalImageLocalInstanceTest(driver_test_lib.BaseDriverTest):
    """Test LocalImageLocalInstance method."""

    LAUNCH_CVD_CMD_WITH_DISK = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=phone -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -cpus fake -x_res fake -y_res fake -dpi fake -memory_mb fake -blank_data_image_mb fake -data_policy always_create -start_vnc_server=true
EOF"""

    LAUNCH_CVD_CMD_NO_DISK = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=phone -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -cpus fake -x_res fake -y_res fake -dpi fake -memory_mb fake -start_vnc_server=true
EOF"""

    LAUNCH_CVD_CMD_NO_DISK_WITH_GPU = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=phone -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -cpus fake -x_res fake -y_res fake -dpi fake -memory_mb fake -start_vnc_server=true
EOF"""

    LAUNCH_CVD_CMD_WITH_WEBRTC = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=auto -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -guest_enforce_security=false -vm_manager=crosvm -start_webrtc=true -webrtc_public_ip=0.0.0.0
EOF"""

    LAUNCH_CVD_CMD_WITH_MIXED_IMAGES = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=phone -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -start_vnc_server=true -super_image=fake_super_image -boot_image=fake_boot_image
EOF"""

    LAUNCH_CVD_CMD_WITH_ARGS = """sg group1 <<EOF
sg group2
launch_cvd -daemon -config=phone -run_adb_connector=true -system_image_dir fake_image_dir -instance_dir fake_cvd_dir -undefok=report_anonymous_usage_stats,enable_sandbox,config -report_anonymous_usage_stats=y -enable_sandbox=false -start_vnc_server=true -setupwizard_mode=REQUIRED
EOF"""

    _EXPECTED_DEVICES_IN_REPORT = [
        {
            "instance_name": "local-instance-1",
            "ip": "0.0.0.0:6520",
            "adb_port": 6520,
            "vnc_port": 6444,
            "webrtc_port": 8443
        }
    ]

    _EXPECTED_DEVICES_IN_FAILED_REPORT = [
        {
            "instance_name": "local-instance-1",
            "ip": "0.0.0.0"
        }
    ]

    def setUp(self):
        """Initialize new LocalImageLocalInstance."""
        super().setUp()
        self.local_image_local_instance = local_image_local_instance.LocalImageLocalInstance()

    # pylint: disable=protected-access
    @mock.patch("acloud.create.local_image_local_instance.utils")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "GetImageArtifactsPath")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "_SelectAndLockInstance")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "_CheckRunningCvd")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "_CreateInstance")
    def testCreateAVD(self, mock_create, mock_check_running_cvd,
                      mock_lock_instance, mock_get_image, mock_utils):
        """Test _CreateAVD."""
        mock_utils.IsSupportedPlatform.return_value = True
        mock_get_image.return_value = local_image_local_instance.ArtifactPaths(
            "/image/path", "/host/bin/path", None, None, None, None)
        mock_check_running_cvd.return_value = True
        mock_avd_spec = mock.Mock()
        mock_lock = mock.Mock()
        mock_lock.Unlock.return_value = False
        mock_lock_instance.return_value = (1, mock_lock)

        # Success
        mock_create.return_value = mock.Mock()
        self.local_image_local_instance._CreateAVD(
            mock_avd_spec, no_prompts=True)
        mock_lock_instance.assert_called_once()
        mock_lock.SetInUse.assert_called_once_with(True)
        mock_lock.Unlock.assert_called_once()

        mock_lock_instance.reset_mock()
        mock_lock.SetInUse.reset_mock()
        mock_lock.Unlock.reset_mock()

        # Failure with no report
        mock_create.side_effect = ValueError("unit test")
        with self.assertRaises(ValueError):
            self.local_image_local_instance._CreateAVD(
                mock_avd_spec, no_prompts=True)
        mock_lock_instance.assert_called_once()
        mock_lock.SetInUse.assert_not_called()
        mock_lock.Unlock.assert_called_once()

        # Failure with report
        mock_lock_instance.side_effect = errors.CreateError("unit test")
        report = self.local_image_local_instance._CreateAVD(
            mock_avd_spec, no_prompts=True)
        self.assertEqual(report.errors, ["unit test"])

    def testSelectAndLockInstance(self):
        """test _SelectAndLockInstance."""
        mock_avd_spec = mock.Mock(local_instance_id=0)
        mock_lock = mock.Mock()
        mock_lock.Lock.return_value = True
        mock_lock.LockIfNotInUse.side_effect = (False, True)
        self.Patch(instance, "GetLocalInstanceLock",
                   return_value=mock_lock)

        ins_id, _ = self.local_image_local_instance._SelectAndLockInstance(
            mock_avd_spec)
        self.assertEqual(2, ins_id)
        mock_lock.Lock.assert_not_called()
        self.assertEqual(2, mock_lock.LockIfNotInUse.call_count)

        mock_lock.LockIfNotInUse.reset_mock()

        mock_avd_spec.local_instance_id = 1
        ins_id, _ = self.local_image_local_instance._SelectAndLockInstance(
            mock_avd_spec)
        self.assertEqual(1, ins_id)
        mock_lock.Lock.assert_called_once()
        mock_lock.LockIfNotInUse.assert_not_called()

    @mock.patch("acloud.create.local_image_local_instance.utils")
    @mock.patch("acloud.create.local_image_local_instance.ota_tools")
    @mock.patch("acloud.create.local_image_local_instance.create_common")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "_LaunchCvd")
    @mock.patch.object(local_image_local_instance.LocalImageLocalInstance,
                       "PrepareLaunchCVDCmd")
    @mock.patch.object(instance, "GetLocalInstanceRuntimeDir")
    @mock.patch.object(instance, "GetLocalInstanceHomeDir")
    def testCreateInstance(self, mock_home_dir, _mock_runtime_dir,
                           _mock_prepare_cmd, mock_launch_cvd,
                           _mock_create_common, mock_ota_tools, _mock_utils):
        """Test the report returned by _CreateInstance."""
        self.Patch(instance, "GetLocalInstanceName",
                   return_value="local-instance-1")
        mock_home_dir.return_value = "/local-instance-1"
        artifact_paths = local_image_local_instance.ArtifactPaths(
            "/image/path", "/host/bin/path", "/misc/info/path",
            "/ota/tools/dir", "/system/image/path", "/boot/image/path")
        mock_ota_tools_object = mock.Mock()
        mock_ota_tools.OtaTools.return_value = mock_ota_tools_object
        mock_avd_spec = mock.Mock(unlock_screen=False)
        local_ins = mock.Mock(
            adb_port=6520,
            vnc_port=6444
        )
        local_ins.CvdStatus.return_value = True
        self.Patch(instance, "LocalInstance",
                   return_value=local_ins)
        self.Patch(list_instance, "GetActiveCVD",
                   return_value=local_ins)
        self.Patch(os, "symlink")

        # Success
        report = self.local_image_local_instance._CreateInstance(
            1, artifact_paths, mock_avd_spec, no_prompts=True)

        self.assertEqual(report.data.get("devices"),
                         self._EXPECTED_DEVICES_IN_REPORT)
        mock_ota_tools.OtaTools.assert_called_with("/ota/tools/dir")
        mock_ota_tools_object.BuildSuperImage.assert_called_with(
            "/local-instance-1/mixed_super.img", "/misc/info/path", mock.ANY)

        # Failure
        mock_launch_cvd.side_effect = errors.LaunchCVDFail("unit test")

        report = self.local_image_local_instance._CreateInstance(
            1, artifact_paths, mock_avd_spec, no_prompts=True)

        self.assertEqual(report.data.get("devices_failing_boot"),
                         self._EXPECTED_DEVICES_IN_FAILED_REPORT)
        self.assertIn("unit test", report.errors[0])

    # pylint: disable=protected-access
    @mock.patch("acloud.create.local_image_local_instance.os.path.isfile")
    def testFindCvdHostBinaries(self, mock_isfile):
        """Test FindCvdHostBinaries."""
        cvd_host_dir = "/unit/test"
        mock_isfile.return_value = None

        with mock.patch.dict("acloud.internal.lib.ota_tools.os.environ",
                             {"ANDROID_HOST_OUT": cvd_host_dir,
                              "ANDROID_SOONG_HOST_OUT": cvd_host_dir}, clear=True):
            with self.assertRaises(errors.GetCvdLocalHostPackageError):
                self.local_image_local_instance._FindCvdHostBinaries(
                    [cvd_host_dir])

        mock_isfile.side_effect = (
            lambda path: path == "/unit/test/bin/launch_cvd")

        with mock.patch.dict("acloud.internal.lib.ota_tools.os.environ",
                             {"ANDROID_HOST_OUT": cvd_host_dir,
                              "ANDROID_SOONG_HOST_OUT": cvd_host_dir}, clear=True):
            path = self.local_image_local_instance._FindCvdHostBinaries([])
            self.assertEqual(path, cvd_host_dir)

        with mock.patch.dict("acloud.internal.lib.ota_tools.os.environ",
                             dict(), clear=True):
            path = self.local_image_local_instance._FindCvdHostBinaries(
                [cvd_host_dir])
            self.assertEqual(path, cvd_host_dir)

    @staticmethod
    def _CreateEmptyFile(path):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, "w"):
            pass

    @mock.patch("acloud.create.local_image_local_instance.ota_tools")
    def testGetImageArtifactsPath(self, mock_ota_tools):
        """Test GetImageArtifactsPath without system image dir."""
        with tempfile.TemporaryDirectory() as temp_dir:
            image_dir = "/unit/test"
            cvd_dir = os.path.join(temp_dir, "cvd-host_package")
            self._CreateEmptyFile(os.path.join(cvd_dir, "bin", "launch_cvd"))

            mock_avd_spec = mock.Mock(
                local_image_dir=image_dir,
                local_kernel_image=None,
                local_system_image=None,
                local_tool_dirs=[cvd_dir])

            paths = self.local_image_local_instance.GetImageArtifactsPath(
                mock_avd_spec)

        mock_ota_tools.FindOtaTools.assert_not_called()
        self.assertEqual(paths, (image_dir, cvd_dir, None, None, None, None))

    @mock.patch("acloud.create.local_image_local_instance.ota_tools")
    def testGetImageFromBuildEnvironment(self, mock_ota_tools):
        """Test GetImageArtifactsPath with files in build environment."""
        with tempfile.TemporaryDirectory() as temp_dir:
            image_dir = os.path.join(temp_dir, "image")
            cvd_dir = os.path.join(temp_dir, "cvd-host_package")
            mock_ota_tools.FindOtaTools.return_value = cvd_dir
            extra_image_dir = os.path.join(temp_dir, "extra_image")
            system_image_path = os.path.join(extra_image_dir, "system.img")
            boot_image_path = os.path.join(extra_image_dir, "boot.img")
            misc_info_path = os.path.join(image_dir, "misc_info.txt")
            self._CreateEmptyFile(os.path.join(image_dir, "vbmeta.img"))
            self._CreateEmptyFile(os.path.join(cvd_dir, "bin", "launch_cvd"))
            self._CreateEmptyFile(system_image_path)
            self._CreateEmptyFile(boot_image_path)
            self._CreateEmptyFile(os.path.join(extra_image_dir,
                                               "boot-debug.img"))
            self._CreateEmptyFile(misc_info_path)

            mock_avd_spec = mock.Mock(
                local_image_dir=image_dir,
                local_kernel_image=extra_image_dir,
                local_system_image=extra_image_dir,
                local_tool_dirs=[])

            with mock.patch.dict("acloud.create.local_image_local_instance."
                                 "os.environ",
                                 {"ANDROID_SOONG_HOST_OUT": cvd_dir},
                                 clear=True):
                paths = self.local_image_local_instance.GetImageArtifactsPath(
                    mock_avd_spec)

        mock_ota_tools.FindOtaTools.assert_called_once()
        self.assertEqual(paths,
                         (image_dir, cvd_dir, misc_info_path, cvd_dir,
                          system_image_path, boot_image_path))

    @mock.patch("acloud.create.local_image_local_instance.ota_tools")
    def testGetImageFromTargetFiles(self, mock_ota_tools):
        """Test GetImageArtifactsPath with extracted target files."""
        ota_tools_dir = "/mock_ota_tools"
        mock_ota_tools.FindOtaTools.return_value = ota_tools_dir

        with tempfile.TemporaryDirectory() as temp_dir:
            image_dir = os.path.join(temp_dir, "image")
            cvd_dir = os.path.join(temp_dir, "cvd-host_package")
            system_image_path = os.path.join(temp_dir, "system", "test.img")
            misc_info_path = os.path.join(image_dir, "META", "misc_info.txt")
            boot_image_path = os.path.join(temp_dir, "boot", "test.img")
            self._CreateEmptyFile(os.path.join(image_dir, "IMAGES",
                                               "vbmeta.img"))
            self._CreateEmptyFile(os.path.join(cvd_dir, "bin", "launch_cvd"))
            self._CreateEmptyFile(system_image_path)
            self._CreateEmptyFile(misc_info_path)
            self._CreateEmptyFile(boot_image_path)

            mock_avd_spec = mock.Mock(
                local_image_dir=image_dir,
                local_kernel_image=boot_image_path,
                local_system_image=system_image_path,
                local_tool_dirs=[ota_tools_dir, cvd_dir])

            paths = self.local_image_local_instance.GetImageArtifactsPath(
                mock_avd_spec)

        mock_ota_tools.FindOtaTools.assert_called_once()
        self.assertEqual(paths,
                         (os.path.join(image_dir, "IMAGES"), cvd_dir,
                          misc_info_path, ota_tools_dir, system_image_path,
                          boot_image_path))

    @mock.patch.object(utils, "CheckUserInGroups")
    def testPrepareLaunchCVDCmd(self, mock_usergroups):
        """test PrepareLaunchCVDCmd."""
        mock_usergroups.return_value = False
        hw_property = {"cpu": "fake", "x_res": "fake", "y_res": "fake",
                       "dpi":"fake", "memory": "fake", "disk": "fake"}
        constants.LIST_CF_USER_GROUPS = ["group1", "group2"]

        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, hw_property, True, "fake_image_dir",
            "fake_cvd_dir", False, True, None, None, None, "phone")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_WITH_DISK)

        # "disk" doesn't exist in hw_property.
        hw_property = {"cpu": "fake", "x_res": "fake", "y_res": "fake",
                       "dpi": "fake", "memory": "fake"}
        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, hw_property, True, "fake_image_dir",
            "fake_cvd_dir", False, True, None, None, None, "phone")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_NO_DISK)

        # "gpu" is enabled with "default"
        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, hw_property, True, "fake_image_dir",
            "fake_cvd_dir", False, True, None, None, None, "phone")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_NO_DISK_WITH_GPU)

        # Following test with hw_property is None.
        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, None, True, "fake_image_dir",
            "fake_cvd_dir", True, False, None, None, None, "auto")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_WITH_WEBRTC)

        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, None, True, "fake_image_dir",
            "fake_cvd_dir", False, True, "fake_super_image", "fake_boot_image",
            None, "phone")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_WITH_MIXED_IMAGES)

        # Add args into launch command with "-setupwizard_mode=REQUIRED"
        launch_cmd = self.local_image_local_instance.PrepareLaunchCVDCmd(
            constants.CMD_LAUNCH_CVD, None, True, "fake_image_dir",
            "fake_cvd_dir", False, True, None, None,
            "-setupwizard_mode=REQUIRED", "phone")
        self.assertEqual(launch_cmd, self.LAUNCH_CVD_CMD_WITH_ARGS)

    @mock.patch.object(utils, "GetUserAnswerYes")
    @mock.patch.object(list_instance, "GetActiveCVD")
    def testCheckRunningCvd(self, mock_cvd_running, mock_get_answer):
        """test _CheckRunningCvd."""
        local_instance_id = 3

        # Test that launch_cvd is running.
        mock_cvd_running.return_value = True
        mock_get_answer.return_value = False
        answer = self.local_image_local_instance._CheckRunningCvd(
            local_instance_id)
        self.assertFalse(answer)

        # Test that launch_cvd is not running.
        mock_cvd_running.return_value = False
        answer = self.local_image_local_instance._CheckRunningCvd(
            local_instance_id)
        self.assertTrue(answer)

    # pylint: disable=protected-access
    @mock.patch("acloud.create.local_image_local_instance.subprocess."
                "check_call")
    @mock.patch.dict("os.environ", clear=True)
    def testLaunchCVD(self, mock_check_call):
        """test _LaunchCvd should call subprocess.check_call with the env."""
        local_instance_id = 3
        launch_cvd_cmd = "launch_cvd"
        host_bins_path = "host_bins_path"
        cvd_home_dir = "fake_home"
        timeout = 100
        cvd_env = {}
        cvd_env[constants.ENV_CVD_HOME] = cvd_home_dir
        cvd_env[constants.ENV_CUTTLEFISH_INSTANCE] = str(local_instance_id)
        cvd_env[constants.ENV_ANDROID_SOONG_HOST_OUT] = host_bins_path
        cvd_env[constants.ENV_ANDROID_HOST_OUT] = host_bins_path

        self.local_image_local_instance._LaunchCvd(launch_cvd_cmd,
                                                   local_instance_id,
                                                   host_bins_path,
                                                   cvd_home_dir,
                                                   timeout)

        mock_check_call.assert_called_once_with(launch_cvd_cmd,
                                                shell=True,
                                                stderr=subprocess.STDOUT,
                                                env=cvd_env,
                                                timeout=timeout)

    @mock.patch("acloud.create.local_image_local_instance.subprocess."
                "check_call")
    def testLaunchCVDTimeout(self, mock_check_call):
        """test _LaunchCvd with subprocess errors."""
        mock_check_call.side_effect = subprocess.TimeoutExpired(
            cmd="launch_cvd", timeout=100)
        with self.assertRaises(errors.LaunchCVDFail):
            self.local_image_local_instance._LaunchCvd("launch_cvd",
                                                       3,
                                                       "host_bins_path",
                                                       "cvd_home_dir",
                                                       100)

        mock_check_call.side_effect = subprocess.CalledProcessError(
            cmd="launch_cvd", returncode=1)
        with self.assertRaises(errors.LaunchCVDFail):
            self.local_image_local_instance._LaunchCvd("launch_cvd",
                                                       3,
                                                       "host_bins_path",
                                                       "cvd_home_dir",
                                                       100)

    def testGetWebrtcSigServerPort(self):
        """test GetWebrtcSigServerPort."""
        instance_id = 3
        expected_port = 8445
        self.assertEqual(
            self.local_image_local_instance.GetWebrtcSigServerPort(instance_id),
            expected_port)

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


# Write your solution here
from cProfile import run

def add_movie(database: list, name: str, director: str, year: int, runtime: int):
    movie = {}
    movie["name"] = name
    movie["director"] = director
    movie["year"] = year
    movie["runtime"] = runtime
    database.append(movie)

#OFFICIAL solution:
#def add_movie(database: list, name: str, director: str, year: int, runtime: int):
#    # Python accepts splitting rows from punctuation
#    # The addition becomes more readable when parts are divided into separate rows
#    movie = {"name": name,
#               "director": director,
#               "year": year,
#               "runtime": runtime}
#
#    database.append(movie)
# Write your solution here


#%%
import config
from binance.client import Client
import csv
import talib
#%%

client = Client (config.API_KEY,config.API_SECRET)

prices = client.get_all_tickers()

candles = client.get_klines(symbol='ETHUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

print(len(candles))
# %%
csvfile = open('15min.csv','w',newline='\n')
candlestick_writer = csv.writer(csvfile, delimiter=',')

klines = client.get_historical_klines("ETHUSDT",
Client.KLINE_INTERVAL_15MINUTE, "1 Apr, 2020", "31 Oct, 2021")

for stick in klines:
    candlestick_writer.writerow(stick)

csvfile.close()
# %%


# fare un programma che disegni dei poligoni regolari

import turtle

win_height = 500
win_lenght = 500

disegno = turtle.Turtle()

turtle.screensize(win_height, win_lenght)

n_lati=input("inserisci il numero di lati di cui si vuole formare la figura: ")
while(n_lati<3):
    print("inserisci un numero di lati maggiore di 2")
    n_lati=input("inserisci il numero di lati di cui si vuole formare la figura: ")

lunghezza=input("inserisci la lunghezza del lato: ")
while(lunghezza<0):
    print("la lunghezza deve essere positiva")
    lunghezza=input("inserisci la lunghezza del lato: ")

def draw_poly(n_lati, lunghezza):
    for i in range(n_lati):
        disegno.home()
        disegno.forward(lunghezza)       #disegna un lato
        disegno.left(360/n_lati)      #ruota verso sinistra di un angolo 360/N oppure secondo me (180*(n_lati-2))/n_lati

draw_poly(n_lati, lunghezza)

turtle.mainloop()


#3D plotting of the output of Dotlinker 3D (volocity output processing version)
# vector to mlab.flow plot. diverged from simple plotting of tracks.
#20111202
#20111219 diverged
#Kota Miura (miura@embl.de)

#filename = '/Users/miura/Dropbox/Mette/Tracks.csv'
filename = '/Users/miura/Dropbox/Mette/vecout.csv'
filename = 'c:/dropbox/My Dropbox/Mette/vecout.csv'
filename = 'c:/dropbox/My Dropbox/Mette/vecout27h1.csv'
filename = '/Users/miura/Dropbox/Mette/vecout27h1.csv'
#import csv
# data = csv.reader(open('C:/dropbox/My Dropbox/Pairs_NowCorrectDot.txt', 'rb'), delimiter='\t')
#data = csv.reader(open('/Users/miura/Dropbox/Mette/Tracks.csv'))

#p1 = []
#tA = []
#for row, points in enumerate(data):
#    tA.append(points[2])
#    p1.append(points[3:6])
#    p2.append(points[3:])
#for d in tA:
#    print d

from matplotlib import mlab as matp

ind, frame, gx, gy, gz, ux, vy, wz = matp.load(filename, skiprows=1, delimiter=',', usecols=[0, 1, 2, 3, 4, 5, 6, 7], unpack=True)

#for d in frame:
    #print d

#from mayavi.mlab import points3d
#from mayavi.mlab import plot3d
from mayavi.mlab import quiver3d
from mayavi.mlab import flow
from mayavi import mlab as maya
import numpy as np

##p1s = points3d(sx, sy, sz, scale_factor=.45, color=(1, 0, 0))

#trial with colorlist
colorlist = []

for row in range(len(frame)):
    if frame[row] is not 0:
      tup = (frame[row]/22, 0.3, 1 - frame[row]/22)
      colorlist.append(tup)
    else:
      tup = (0, 0.3, 0)
      colorlist.append(tup)
# here there should be constructing grid and assign vectors to each.
x, y, z = np.mgrid[0:max(gx)+10, 0:max(gy)+10, 0:max(gz)+10]

# then loop the vectors, place them in the right positions, then flow can be generated.

u = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
v = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
w = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
un = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
vn = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
wn = np.zeros((x.shape[0], x.shape[1], x.shape[2]))
for i in zip(gx, gy, gz, ux, vy, wz):
  u[round(i[0]), round(i[1]), round(i[2])] += i[3]
  v[round(i[0]), round(i[1]), round(i[2])] += i[4]
  w[round(i[0]), round(i[1]), round(i[2])] += i[5]
  un[round(i[0]), round(i[1]), round(i[2])] += 1
  vn[round(i[0]), round(i[1]), round(i[2])] += 1
  wn[round(i[0]), round(i[1]), round(i[2])] += 1

unn = np.divide(u, un)
vnn = np.divide(v, vn)
wnn = np.divide(w, wn)
nanpos = np.isnan(unn)
unn[nanpos] = 0.0
nanpos = np.isnan(vnn)
vnn[nanpos] = 0.0
nanpos = np.isnan(wnn)
wnn[nanpos] = 0.0

##### simple show of vectors
#quiver3d( x, y, z, unn, vnn, wnn)# colormap='copper', opacity=0.3, mode='2darrow', scale_factor=1)
#quiver3d( gx, gy, gz, ux, vy, wz, colormap='copper', opacity=0.3, mode='2darrow', scale_factor=1)

##### trial to show flow
ff = flow(x, y, z, unn, vnn, wnn)#, color=(0, 1, 1), opacity=0.7 )

#quiver3d(px, py, pz, vx, vy, vz, color=(0, 1, 1), opacity=0.3, mode='2darrow', scale_factor=1)

#quiver3d( gx, gy, gz, ux, vy, wz, colormap='copper', opacity=0.3, mode='2darrow', scale_factor=1)

##### Color coded tracks.
#for row in range(len(frame)):
  #quiver3d( gx[row], gy[row], gz[row], ux[row], vy[row], wz[row], color=colorlist[row], opacity=1, mode='2darrow', scale_factor=1, line_width=4 )

######vector field example
maya.outline()
#src = maya.pipeline.vector_field(x, y, z, unn, vnn, wnn)
#maya.pipeline.vectors(src, scale_factor=3.0)
######################

maya.show()


# -*- coding: utf-8 -*-
"""Interface module.

Simple CLI interface to run the program.

"""

import logging

from ..funcs.string_operations import print_unicode
from .. import version
from ..handlers import MoneyPlHandler

logger = logging.getLogger(__name__)

class Config(object):
    """Class for program configuration data.

    Attributes:
        opts: sys.args passed while running the program

    """

    opts = None

def get_moneypl():
    """Get money.pl currency data.

    Returns:
        OrderedDict: Parsed data.

    """
    print_unicode('Getting data from website.')
    mpl = MoneyPlHandler()
    data = mpl.get_moneypl()
    print_unicode('data download time: {}'.format(mpl.download_time))
    print_unicode('parse time: {}'.format(mpl.parse_time))
    return data

def pretty_print_data(data):
    """Format received data.

    Args:
        data (ordereddict): OrderedDict to be printed.

    """
    detail_str = ''
    for key, value in data.items():
        if key == 'FOREX':
            print_unicode(key + u'\tkupno\tsprzedaż\n')
        elif key == 'NBP':
            print_unicode(key + u'\tkurs średni\n')

        for name, detail in value.items():
            if type(detail) == list:
                for item in detail:
                    detail_str = detail_str + '\t' + item
            else:
                detail_str = detail
            if name == 'DATA':
                tab = '\t'
            else:
                tab = ''
            if key == 'FOREX':
                print_unicode(name + u'{}'.format(tab) + detail_str)
            elif key == 'NBP':
                print_unicode(name + u'\t' + detail_str)
            detail_str = ''
        print_unicode('')

def display_header():
    """Display header."""
    print_unicode(u'##################')
    print_unicode(u'$ KursyWalut ' + version.__version__ + u' $')
    print_unicode(u'##################\n')

def run(*args):
    """Run the program."""
    Config.opts = args
    display_header()
    data = get_moneypl()
    print_unicode('')
    pretty_print_data(data)


from datacategoryvisitors.processeddatabuilders.ProcessedDataBuilderBase \
    import ProcessedDataBuilderBase

class DataCategorizerBuilder (ProcessedDataBuilderBase):
    """Categorizes preprocessed data into seperate bins specified by the user"""

    def __init__(self, categories:list[float]) -> None:
        """
        Parameters
        ---------------
        categories : an ordered list of the highest value for each category
        """
        super().__init__()
        self._categories = categories

    def _buildProcessedData(self) -> None:
        """Goes through all the values for each passenger in preprocessed data
        and assigns them to a category """
        for value in self._preprocessedData:
            currentData = [0] * len(self._categories)
            i = self._getIndexOfBin(value)
            currentData[i] = 1.0
            self._processedData.append(currentData)

    def _getIndexOfBin(self, value: float) -> int:
        """Looks for the first category in which the value falls below
        the categories cut off and returns this index"""
        for i in range(len(self._categories)):
            if value <= self._categories[i]:
                break
        return i


import csv
from bs4 import BeautifulSoup
from selenium import webdriver
from msedge.selenium_tools import Edge, EdgeOptions
import tkinter as tk

def get_url(search_term):
     template='https://www.amazon.com/s?k={}&crid=3T38SMJFW8AWD&sprefix=speakers%2Caps%2C290&ref=nb_sb_ss_ts-doa-p_1_8'
     search_term=search_term.replace(' ','+')

     URL=template.format(search_term)
     return template.format(search_term)

     URL+='&page{}'
     return URL

def extract_record(item):
    #title(description) and url
    atag=item.h2.a
    description=atag.text.strip()
    url="https://www.amazon.com"+atag.get('href')

    #price
    try:
        price_parent=item.find('span',{'class':'a-price'})
        price=price_parent.find('span',{'class':'a-offscreen'}).text

    except AttributeError:
         return

    #rank and rating
    try:
        rating=item.i.text
        review_count=item.find('span',{'class':'a-size-base s-underline-text'}).text
    except  AttributeError:
         rating=''
         review_count=''

    result=(description,price,rating,review_count,url)
    return result

s=[]

def getInput():
    inp = entry.get()
    s.append(inp)
    root.destroy()

def main(search_item):
    #startup our webdriver
    options=EdgeOptions()
    options.use_chromium=True
    driver=webdriver.Firefox(executable_path='/home/yashika/bin/geckodriver')
    records=[]
    url=get_url(search_item)

    for page in range(1,21):
          driver.get(url.format(page))
          soup =BeautifulSoup(driver.page_source,'html5lib')
          results=soup.find_all('div',{'data-component-type':'s-search-result'})

          for item in results:
                record=extract_record(item)
                if record:
                      records.append(record)
    driver.close()

    with open('resultsDemo.csv','w',newline='',encoding='utf-8') as f:
          writer=csv.writer(f)
          writer.writerow(['Description','Price','Rating','ReviewCount','URL'])
          writer.writerows(records)

#main('speakers bluetooth wireless')

root = tk.Tk()
root.title("Automated Webscraping For Shopping Details:")
root.geometry("400x200")
root.configure(bg="#75AF6F")

# Load the background image
bg_image = tk.PhotoImage(file="w2.png")

# Create a canvas to hold the background image
canvas = tk.Canvas(root, width=400, height=200, bg="#75AF6F")
canvas.pack(fill="both", expand=True)
canvas.create_image(0, 0, image=bg_image, anchor="nw")

title_font = ("Helvetica", 24, "bold")
label_font = ("Helvetica", 12)
button_font = ("Helvetica", 14)

# title_label = tk.Label(canvas, text="Price Comparison", font=title_font, bg="#75AF6F", fg="white")
# title_label.pack(pady=(30,10))

entry_label = tk.Label(canvas, text="Enter product name:", font=label_font, bg="#75AF6F", fg="white")
entry_label.pack(pady=10)

entry = tk.Entry(canvas, font=label_font)
entry.pack(pady=10)

printButton = tk.Button(canvas, text="Search", command=getInput, font=button_font, bg="#4CAF50", fg="white")
printButton.pack()

entry_label = tk.Label(canvas, text="Created by Akshaya Lakshmi & Yashika Haritwal", font=label_font, bg="#75AF6F", fg="white")
entry_label.pack()
root.mainloop()
main(s[0])


from pprint import pprint

from PyInquirer import  prompt
from view.abstract_view import AbstractView
from view.session import Session

class AddTableView(AbstractView):
    def __init__(self) -> None:
        self.__questions = [
            {
                'type': 'input',
                'name': 'table_id',
                'message': 'Table to add, please enter the id.',
            }
        ]

    def display_info(self):
        print("Add table Section")

    def make_choice(self):

        answers = prompt(self.__questions)

        if not str.isdigit(answers['table_id']) or answers['table_id'] == '':
            print('Error : table id must be a number.')

        else:

            from business.table.table import Table
            table = Table.load(answers['table_id'])
            if table:
                if table.active_table():
                    print('Table successfully activated')
                else:
                    print('Something went wrong when you tried to desactivate the table.')
            else:
                print('Table id unrocognised.')

        from view.organiser_view.menu_view import OrganiserMenuView
        return OrganiserMenuView()


"""HATRPO algorithm."""

import numpy as np
import torch
from harl.utils.envs_tools import check
from harl.utils.trpo_util import (
    flat_grad,
    flat_params,
    conjugate_gradient,
    fisher_vector_product,
    update_model,
    kl_divergence,
)
from harl.algorithms.actors.on_policy_base import OnPolicyBase
from harl.models.policy_models.stochastic_policy import StochasticPolicy

class HATRPO(OnPolicyBase):
    def __init__(self, args, obs_space, act_space, device=torch.device("cpu")):
        """Initialize HATRPO algorithm.
        Args:
            args: (dict) arguments.
            obs_space: (gym.spaces or list) observation space.
            act_space: (gym.spaces) action space.
            device: (torch.device) device to use for tensor operations.
        """
        assert (
            act_space.__class__.__name__ != "MultiDiscrete"
        ), "only continuous and discrete action space is supported by HATRPO."
        super(HATRPO, self).__init__(args, obs_space, act_space, device)

        self.kl_threshold = args["kl_threshold"]
        self.ls_step = args["ls_step"]
        self.accept_ratio = args["accept_ratio"]
        self.backtrack_coeff = args["backtrack_coeff"]

    def update(self, sample):
        """Update actor networks.
        Args:
            sample: (Tuple) contains data batch with which to update networks.
        Returns:
            kl: (torch.Tensor) KL divergence between old and new policy.
            loss_improve: (np.float32) loss improvement.
            expected_improve: (np.ndarray) expected loss improvement.
            dist_entropy: (torch.Tensor) action entropies.
            ratio: (torch.Tensor) ratio between new and old policy.
        """

        (
            obs_batch,
            rnn_states_batch,
            actions_batch,
            masks_batch,
            active_masks_batch,
            old_action_log_probs_batch,
            adv_targ,
            available_actions_batch,
            factor_batch,
        ) = sample

        old_action_log_probs_batch = check(old_action_log_probs_batch).to(**self.tpdv)
        adv_targ = check(adv_targ).to(**self.tpdv)
        active_masks_batch = check(active_masks_batch).to(**self.tpdv)
        factor_batch = check(factor_batch).to(**self.tpdv)

        # Reshape to do evaluations for all steps in a single forward pass
        action_log_probs, dist_entropy, _ = self.evaluate_actions(
            obs_batch,
            rnn_states_batch,
            actions_batch,
            masks_batch,
            available_actions_batch,
            active_masks_batch,
        )

        # actor update
        ratio = getattr(torch, self.action_aggregation)(
            torch.exp(action_log_probs - old_action_log_probs_batch),
            dim=-1,
            keepdim=True,
        )
        if self.use_policy_active_masks:
            loss = (
                torch.sum(ratio * factor_batch * adv_targ, dim=-1, keepdim=True)
                * active_masks_batch
            ).sum() / active_masks_batch.sum()
        else:
            loss = torch.sum(
                ratio * factor_batch * adv_targ, dim=-1, keepdim=True
            ).mean()

        loss_grad = torch.autograd.grad(
            loss, self.actor.parameters(), allow_unused=True
        )
        loss_grad = flat_grad(loss_grad)

        step_dir = conjugate_gradient(
            self.actor,
            obs_batch,
            rnn_states_batch,
            actions_batch,
            masks_batch,
            available_actions_batch,
            active_masks_batch,
            loss_grad.data,
            nsteps=10,
            device=self.device,
        )

        loss = loss.data.cpu().numpy()

        params = flat_params(self.actor)
        fvp = fisher_vector_product(
            self.actor,
            obs_batch,
            rnn_states_batch,
            actions_batch,
            masks_batch,
            available_actions_batch,
            active_masks_batch,
            step_dir,
        )
        shs = 0.5 * (step_dir * fvp).sum(0, keepdim=True)
        step_size = 1 / torch.sqrt(shs / self.kl_threshold)[0]
        full_step = step_size * step_dir

        old_actor = StochasticPolicy(
            self.args, self.obs_space, self.act_space, self.device
        )
        update_model(old_actor, params)
        expected_improve = (loss_grad * full_step).sum(0, keepdim=True)
        expected_improve = expected_improve.data.cpu().numpy()

        # Backtracking line search (https://en.wikipedia.org/wiki/Backtracking_line_search)
        flag = False
        fraction = 1
        for i in range(self.ls_step):
            new_params = params + fraction * full_step
            update_model(self.actor, new_params)
            action_log_probs, dist_entropy, _ = self.evaluate_actions(
                obs_batch,
                rnn_states_batch,
                actions_batch,
                masks_batch,
                available_actions_batch,
                active_masks_batch,
            )

            ratio = getattr(torch, self.action_aggregation)(
                torch.exp(action_log_probs - old_action_log_probs_batch),
                dim=-1,
                keepdim=True,
            )
            if self.use_policy_active_masks:
                new_loss = (
                    torch.sum(ratio * factor_batch * adv_targ, dim=-1, keepdim=True)
                    * active_masks_batch
                ).sum() / active_masks_batch.sum()
            else:
                new_loss = torch.sum(
                    ratio * factor_batch * adv_targ, dim=-1, keepdim=True
                ).mean()

            new_loss = new_loss.data.cpu().numpy()
            loss_improve = new_loss - loss

            kl = kl_divergence(
                obs_batch,
                rnn_states_batch,
                actions_batch,
                masks_batch,
                available_actions_batch,
                active_masks_batch,
                new_actor=self.actor,
                old_actor=old_actor,
            )
            kl = kl.mean()

            if (
                kl < self.kl_threshold
                and (loss_improve / expected_improve) > self.accept_ratio
                and loss_improve.item() > 0
            ):
                flag = True
                break
            expected_improve *= self.backtrack_coeff
            fraction *= self.backtrack_coeff

        if not flag:
            params = flat_params(old_actor)
            update_model(self.actor, params)
            print("policy update does not impove the surrogate")

        return kl, loss_improve, expected_improve, dist_entropy, ratio

    def train(self, actor_buffer, advantages, state_type):
        """Perform a training update using minibatch GD.
        Args:
            actor_buffer: (OnPolicyActorBuffer) buffer containing training data related to actor.
            advantages: (np.ndarray) advantages.
            state_type: (str) type of state.
        Returns:
            train_info: (dict) contains information regarding training update (e.g. loss, grad norms, etc).
        """
        train_info = {}
        train_info["kl"] = 0
        train_info["dist_entropy"] = 0
        train_info["loss_improve"] = 0
        train_info["expected_improve"] = 0
        train_info["ratio"] = 0

        if np.all(actor_buffer.active_masks[:-1] == 0.0):
            return train_info

        if state_type == "EP":
            advantages_copy = advantages.copy()
            advantages_copy[actor_buffer.active_masks[:-1] == 0.0] = np.nan
            mean_advantages = np.nanmean(advantages_copy)
            std_advantages = np.nanstd(advantages_copy)
            advantages = (advantages - mean_advantages) / (std_advantages + 1e-5)

        if self.use_recurrent_policy:
            data_generator = actor_buffer.recurrent_generator_actor(
                advantages, 1, self.data_chunk_length
            )
        elif self.use_naive_recurrent_policy:
            data_generator = actor_buffer.naive_recurrent_generator_actor(advantages, 1)
        else:
            data_generator = actor_buffer.feed_forward_generator_actor(advantages, 1)

        for sample in data_generator:
            kl, loss_improve, expected_improve, dist_entropy, imp_weights = self.update(
                sample
            )

            train_info["kl"] += kl
            train_info["loss_improve"] += loss_improve.item()
            train_info["expected_improve"] += expected_improve
            train_info["dist_entropy"] += dist_entropy.item()
            train_info["ratio"] += imp_weights.mean()

        num_updates = 1

        for k in train_info.keys():
            train_info[k] /= num_updates

        return train_info


#!/usr/bin/env python

from .igleg import *

c_1 = get_c1()
c_2 = get_c2()
c_3 = get_c3()

# take a file with hex and convert it to new challenge format
def convert(file0, file1):
    with open(file0, 'r') as f:
        r = f.read()

    r = r.split('\\x')
    r = r[1:]

    # wrap every 3 elements in a list in r
    r = [r[i:i+3] for i in range(0, len(r), 3)]

    # reverse order of 3 strings in each element
    for i in r:
        i.reverse()

    # swap 2nd and 3rd item in each element
    # for i in r:
    #     i[1], i[2] = i[2], i[1]

    # swap 1st and 2nd item in each element
    # for i in r:
    #     i[0], i[1] = i[1], i[0]

    # reverse order of 3 strings in each element
    # for i in r:
    #     i.reverse()

    # use dictionaries to convert each element unless position 0 is 10, in which case use c_3 for position 2 instead of c_2
    for i in r:
        j = i[2]
        k = i[0]
        l = i[1]
        if j in c_1:
            j = c_1[j]
        if j == '01':
            if l in c_2:
                l = c_2[l]
        elif j == '80':
            if l in c_3:
                l = c_3[l]
            if k in c_2:
                k = c_2[k]
        else:
            if k in c_2:
                k = c_2[k]
            if l in c_2:
                l = c_2[l]
        r[r.index(i)] = [k, l, j]

    # combine r elements into one string
    for i in r:
        j = i[0]
        k = i[1]
        l = i[2]
        r[r.index(i)] = j + k + l

    # combine r elements into one string and separate every two characters with "\x"
    r = ''.join(r)
    r = '\\x'.join(r[i:i+2] for i in range(0, len(r), 2))

    # add "\x" to beginning of string
    r = '\\x' + r

    # write string to file
    with open(file1, 'w') as f:
        f.write(r)


import numpy as np
import datetime
import os
import sqlite3
from sqlite3 import Error
from pathlib import Path
import glob
import keras
from keras.models import model_from_json
import pandas as pd
import time
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix
import librosa

import prep_noise as prep_data
from id_ur_speech_func import ID_UR_Speech

def get_date():
    time = datetime.datetime.now()
    time_str = "{}y{}m{}d{}h{}m{}s".format(time.year,time.month,time.day,time.hour,time.minute,time.second)
    return(time_str)

def parser(wavefile,num_mfcc,env_noise=None):
    try:
        y, sr = librosa.load(wavefile, res_type= 'kaiser_fast')
        y = prep_data.normalize(y)

        rand_scale = 0.0
        #randomly assigns speaker data to 1 (train) 2 (validation) or 3 (test)
        if env_noise is not None:
            #at random apply varying amounts of environment noise
            rand_scale = random.choice([0.0,0.25,0.5,0.75,1.0,1.25])
            #logging.info("Scale of noise applied: {}".format(rand_scale))
            if rand_scale:
                #apply *known* environemt noise to signal
                total_length = len(y)/sr
                envnoise_normalized = prep_data.normalize(env_noise)
                envnoise_scaled = prep_data.scale_noise(envnoise_normalized,rand_scale)
                envnoise_matched = prep_data.match_length(envnoise_scaled,sr,total_length)
                if len(envnoise_matched) != len(y):
                    diff = int(len(y) - len(envnoise_matched))
                    if diff < 0:
                        envnoise_matched = envnoise_matched[:diff]
                    else:
                        envnoise_matched = np.append(envnoise_matched,np.zeros(diff,))
                y += envnoise_matched
        mfccs = librosa.feature.mfcc(y, sr, n_mfcc=num_mfcc,hop_length=int(0.010*sr),n_fft=int(0.025*sr))
        return mfccs, sr, rand_scale
    except EOFError as error:
        #logging.exception('def parser() resulted in {} for the file: {}'.format(error,wavefile))
        print('def parser() resulted in {} for the file: {}'.format(error,wavefile))
        print(error)
    except ValueError as ve:
        #logging.exception("Error occured ({}) with the file {}".format(ve,wavefile))
        print("Error occured ({}) with the file {}".format(ve,wavefile))
        print(ve)

    return None, None, None

def insert_data(filename,feature, sr, noise_scale,dataset_group,label):
    if sr:
        columns = list((range(0,num_mfcc)))
        column_str = []
        for i in columns:
            column_str.append(str(i))
        feature_df = pd.DataFrame(feature)
        curr_df = pd.DataFrame.transpose(feature_df)
        curr_df.columns = column_str
        #add additional columns with helpful info such as filename,noise info, label
        curr_df["filename"] = filename
        curr_df["noisegroup"] = noisegroup
        curr_df["noiselevel"] = noise_scale
        curr_df["dataset"] = dataset_group
        curr_df["label"] = label

        x = curr_df.values
        num_cols = num_mfcc + len(['filename','noisegroup','noiselevel','dataset','label'])
        col_var = ""
        for i in range(num_cols):
            if i < num_cols-1:
                col_var+=' ?,'
            else:
                col_var+=' ?'
        c.executemany(' INSERT INTO mfcc_40_user VALUES (%s) ' % col_var,x)
        conn.commit()
    else:
        #logging.exception("Failed MFCC extraction: {} in the directory: {}".format(filename,label))
        print("Failed MFCC extraction: {} in the directory: {}".format(filename,label))

    return None

if __name__ == '__main__':
    try:

        date = get_date()
        database = 'sp_mfcc_{}'.format(date)

        conn = sqlite3.connect(database)
        c = conn.cursor()
        noisegroup = 'none'
        curr_speech = ID_UR_Speech(date)
        directory_user = './user_recordings/'
        directory_processed_speech = './processed_recordings/'
        if not os.path.exists(directory_user):
            os.makedirs(directory_user)
        if not os.path.exists(directory_processed_speech):
            os.makedirs(directory_processed_speech)
        test_mic = curr_speech.start_action('test your mic')
        sec = 5
        if test_mic:
            print("Now recording. Please stay quiet as we measure the background noise.")
            mictest = curr_speech.test_mic(sec)
            if mictest == False:
                print("We couldn't test your mic..")
                print("Please check your settings and connections.")
                curr_speech.cont = False
            while curr_speech.cont == True:
                print("For model testing purposes, which langauge do/will you speak?")
                curr_speech.language = input()
                curr_speech.cont = curr_speech.start_action('test me on your language')
                if curr_speech.cont:
                    curr_speech.play_go()
                    user_speech = curr_speech.record_user(30)
                    time_str = date
                    user_recording_filename = '{}talking_{}.wav'.format(directory_user,time_str)
                    curr_speech.save_rec(user_recording_filename,user_speech,fs=22050)
                    #subtract noise
                    if curr_speech.reduce_noise(user_recording_filename,directory_user+'background_{}.wav'.format(date)):
                        #save speech to MFCCs
                        env_noise = None
                        num_mfcc = 40
                        wav = directory_processed_speech+'rednoise_{}.wav'.format(date)
                        feature,sr,noise_scale = parser(wav, num_mfcc,env_noise)
                        #prepare database to save data

                        columns = list((range(0,num_mfcc)))
                        column_type = []
                        for i in columns:
                            column_type.append('"'+str(i)+'" REAL')

                        c.execute(''' CREATE TABLE IF NOT EXISTS mfcc_40_user(%s,filename  TEXT, noisegroup TEXT, noiselevel REAL, dataset INT,label TEXT) ''' % ", ".join(column_type))
                        conn.commit()
                        dataset_group = 0
                        name = Path(wav).name
                        insert_data(name,feature,sr,noise_scale=0,dataset_group=dataset_group,label=curr_speech.language)
                        conn.commit()

                        #prepare data
                        c.execute("SELECT * FROM mfcc_40_user WHERE dataset='{}'".format(dataset_group))
                        data = c.fetchall()
                        df = pd.DataFrame(data)
                        X = df.iloc[:,:40].values

                        #normalize
                        mean = np.mean(X,axis=0)
                        std = np.std(X,axis=0)
                        X=(X-mean)/std

                        #feature scaling
                        sc = StandardScaler()
                        X = sc.fit_transform(X)

                        for model in glob.glob('./models/*.json'):
                            model_name = os.path.splitext(model)[0]
                            json_file = open(model_name+'.json', 'r')
                            loaded_model_json = json_file.read()
                            json_file.close()
                            loaded_model = model_from_json(loaded_model_json)
                            # load weights into new model
                            loaded_model.load_weights(model_name+".h5")
                            print("Loaded model from disk")

                            # evaluate loaded model on new data
                            print("\nYou identified your language as {}\n".format(curr_speech.language))
                            try:
                                loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
                                classify = loaded_model.predict(X)
                                classify = (classify > 0.5)
                                english = sum(classify == 0)
                                german = sum(classify == 1)
                                max_index = np.argmax([english,german])
                                classification = max_index
                                if classification == 0:
                                    prediction = 'English'
                                elif classification == 1:
                                    prediction = 'German'
                                else:
                                    print("Error ocurred - no language predicted")

                                print("The model '{}' \npredicted your language to be: \n\n{}\n\n\n".format(model_name,prediction))
                            except Exception as e:
                                print(e)

    except Exception as e:
        print (e)
    finally:
        if conn:
            conn.close()
        curr_speech.close_game()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb  9 11:51:01 2017

@author: lifanhong
"""

'''
Write a procedure called oddTuples, which takes a tuple as input, and returns
a new tuple as output, where every other element of the input tuple is copied,
starting with the first one. So if test is the tuple ('I', 'am', 'a', 'test', 'tuple'),
then evaluating oddTuples on this input would return the tuple ('I', 'a', 'tuple').
'''

#my code
def oddTuples(aTup):
    '''
    aTup: a tuple

    returns: tuple, every other element of aTup.
    '''
    # Your Code Here
    oTup = ()
    for i in range(len(aTup)):
        if i % 2 == 0:
            oTup = oTup + aTup[i:i+1] #also okay for (aTup[i],),should make sure returing a tuple
    return oTup

#examples given by course
def oddTuples1(aTup):
    '''
    aTup: a tuple

    returns: tuple, every other element of aTup.
    '''
    # a placeholder to gather our response
    rTup = ()
    index = 0

    # Idea: Iterate over the elements in aTup, counting by 2
    #  (every other element) and adding that element to
    #  the result
    while index < len(aTup):
        rTup += (aTup[index],)
        index += 2

    return rTup

def oddTuples2(aTup):
    '''
    Another way to solve the problem.

    aTup: a tuple

    returns: tuple, every other element of aTup.
    '''
    # Here is another solution to the problem that uses tuple
    #  slicing by 2 to achieve the same result
    return aTup[::2] #wow so simple


from rna2d.pipelines import RfamNew

RFAM142 = "tests/test-data/rfam142.pdb"
RFAM148 = "tests/test-data/rfam148.pdb"
RESULTS = "tests/test-data/new-results.rpt"

def test_mapping():
    rnew = RfamNew(RESULTS, RFAM142, RFAM148)
    new_mapping = rnew.get_new_mapping()
    assert len(new_mapping) == 1977

def test_newest():
    rnew = RfamNew(RESULTS, RFAM142, RFAM148)
    results, indeces = rnew.get_newest_only()
    assert len(results) == 11
    assert len(indeces) == 11
    assert len(results['spot-rna']) == 989


import tensorflow.compat.v2 as tf
from .utils import *
from .transformer import TransformerLayer

# Model definition
layers = tf.keras.layers
regularizers = tf.keras.regularizers

class NoFeatureExtractPeriodEstimator(tf.keras.models.Model):
    """RepNet model,which has no feature extractor network."""
    def __init__(
            self,
            num_frames=64,
            image_size=112,
            dropout_rate=0.25,
            l2_reg_weight=1e-6,
            conv_channels=32,
            conv_kernel_size=3,
            transformer_layers_config=((512, 4, 512),),
            transformer_dropout_rate=0.0,
            transformer_reorder_ln=True,
            period_fc_channels=(512, 512),
            within_period_fc_channels=(512, 512)):
        super(NoFeatureExtractPeriodEstimator, self).__init__()

        self.num_frames = num_frames
        self.image_size = image_size

        self.dropout_rate = dropout_rate
        self.l2_reg_weight = l2_reg_weight

        self.conv_channels = conv_channels
        self.conv_kernel_size = conv_kernel_size
        self.transformer_layers_config = transformer_layers_config
        # Transformer config in form of (channels, heads, bottleneck channels).
        self.transformer_layers_config = transformer_layers_config
        self.transformer_dropout_rate = transformer_dropout_rate
        self.transformer_reorder_ln = transformer_reorder_ln
        self.period_fc_channels = period_fc_channels
        self.within_period_fc_channels = within_period_fc_channels

        # Counting Module (Self-sim > Conv > Transformer > Classifier)
        self.conv_3x3_layer = layers.Conv2D(self.conv_channels,
                                            self.conv_kernel_size,
                                            padding='same',
                                        activation=tf.nn.relu)

        channels = self.transformer_layers_config[0][0]
        self.input_projection = layers.Dense(
            channels, kernel_regularizer=regularizers.l2(self.l2_reg_weight),
            activation=None)
        self.input_projection2 = layers.Dense(
            channels, kernel_regularizer=regularizers.l2(self.l2_reg_weight),
            activation=None)

        length = self.num_frames
        self.pos_encoding = tf.compat.v1.get_variable(
            name='resnet_period_estimator/pos_encoding',
            shape=[1, length, 1],
            initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.02))
        self.pos_encoding2 = tf.compat.v1.get_variable(
            name='resnet_period_estimator/pos_encoding2',
            shape=[1, length, 1],
            initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.02))

        self.transformer_layers = []
        for d_model, num_heads, dff in self.transformer_layers_config:
            self.transformer_layers.append(
                        TransformerLayer(d_model, num_heads, dff,
                           self.transformer_dropout_rate,
                           self.transformer_reorder_ln))

        self.transformer_layers2 = []
        for d_model, num_heads, dff in self.transformer_layers_config:
            self.transformer_layers2.append(
                        TransformerLayer(d_model, num_heads, dff,
                           self.transformer_dropout_rate,
                           self.transformer_reorder_ln))

        # Period Prediction Module.
        self.dropout_layer = layers.Dropout(self.dropout_rate)
        num_preds = self.num_frames//2
        self.fc_layers = []
        for channels in self.period_fc_channels:
            self.fc_layers.append(layers.Dense(
                    channels, kernel_regularizer=regularizers.l2(self.l2_reg_weight),
                    activation=tf.nn.relu))
        self.fc_layers.append(layers.Dense(
                num_preds, kernel_regularizer=regularizers.l2(self.l2_reg_weight)))

        # Within Period Module
        num_preds = 1
        self.within_period_fc_layers = []
        for channels in self.within_period_fc_channels:
            self.within_period_fc_layers.append(layers.Dense(
                channels, kernel_regularizer=regularizers.l2(self.l2_reg_weight),
                activation=tf.nn.relu))
        self.within_period_fc_layers.append(layers.Dense(
                num_preds, kernel_regularizer=regularizers.l2(self.l2_reg_weight)))

    def call(self,x):
        batch_size = tf.shape(x)[0]  # directly is affinity matrix input
        affinity=x

        # 3x3 conv layer on self-similarity matrix.
        x = self.conv_3x3_layer(x)
        x = tf.reshape(x, [batch_size, self.num_frames, -1])
        within_period_x = x

        # Period prediction.
        x = self.input_projection(x)
        x += self.pos_encoding
        for transformer_layer in self.transformer_layers:
            x = transformer_layer(x)
        x = flatten_sequential_feats(x, batch_size, self.num_frames)
        for fc_layer in self.fc_layers:
            x = self.dropout_layer(x)
            x = fc_layer(x)

        # Within period prediction.
        within_period_x = self.input_projection2(within_period_x)
        within_period_x += self.pos_encoding2
        for transformer_layer in self.transformer_layers2:
            within_period_x = transformer_layer(within_period_x)
        within_period_x = flatten_sequential_feats(within_period_x,
                                                batch_size,
                                                self.num_frames)

        for fc_layer in self.within_period_fc_layers:
            within_period_x = self.dropout_layer(within_period_x)
            within_period_x = fc_layer(within_period_x)

        return x,within_period_x,affinity

def get_repnet_model(logdir):
    """Returns a trained RepNet model.

    Args:
        logdir (string): Path to directory where checkpoint will be downloaded.

    Returns:
        model (Keras model): Trained RepNet model.
    """
    # Check if we are in eager mode.
    assert tf.executing_eagerly()

    # Models will be called in eval mode.
    tf.keras.backend.set_learning_phase(0)

    # Define RepNet model.
    model = NoFeatureExtractPeriodEstimator()
    # tf.function for speed.
    model.call = tf.function(model.call)

    # Define checkpoint and checkpoint manager.
    ckpt = tf.train.Checkpoint(model=model)
    ckpt_manager = tf.train.CheckpointManager(
        ckpt, directory=logdir, max_to_keep=10)
    latest_ckpt = ckpt_manager.latest_checkpoint
    print('Loading from: ', latest_ckpt)
    if not latest_ckpt:
        raise ValueError('Path does not have a checkpoint to load.')
    # Restore weights.
    ckpt.restore(latest_ckpt).expect_partial()

    # Pass dummy frames to build graph.

    model(tf.random.uniform((1,64, 64,1)))
    # for v in model.variables:
    #     print(v.name)
    #     print('-'*50)
    #     print(v)

    return model

# def load_weight(model):
#     # ckpt = tf.train.Checkpoint(model=model)
#     for layer in model.layers:
#         print(layer.name)


import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from pandas import read_csv

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300, bg='lightsteelblue2', relief='raised')
canvas1.pack()

label1 = tk.Label(root, text='File Conversion Tool', bg='lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getCSV():
    global read_file

    import_file_path = filedialog.askopenfilename()
    read_file = read_csv(import_file_path)

browseButton_CSV = tk.Button(text="      Import CSV File     ", command=getCSV, bg='green', fg='white',
                             font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_CSV)

def convertToExcel():
    global read_file

    export_file_path = filedialog.asksaveasfilename(defaultextension='.xlsx')
    read_file.to_excel(export_file_path, index=None, header=True)

saveAsButton_Excel = tk.Button(text='Convert CSV to Excel', command=convertToExcel, bg='green', fg='white',
                               font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton_Excel)

def exitApplication():
    MsgBox = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application',
                                       icon='warning')
    if MsgBox == 'yes':
        root.destroy()

exitButton = tk.Button(root, text='       Exit Application     ', command=exitApplication, bg='brown', fg='white',
                       font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)

root.mainloop()

#os.chdir(r'C:\Users\Aharon T\Desktop\sample files')
# print(os.listdir())
# print(os.getcwd())

# print(df["col2"])
# print(df["col2"].iloc[5])
# for x in range(8):
#    datapoint= f.readline()
#    try:
#        datapoint.index(",")
#    except ValueError:
#        print ("Not found!")
#     else:
#        print (datapoint.index(","))

#
#
# r = open("pokemon_data.csv", 'w')
#
# r.close()
#


import pygame
from .settings import Settings
from .sprites.player import Player
from .functions import load_map
from gamemap import GameMap

def main(screen: pygame.Surface):
    group = pygame.sprite.Group()
    player = Player(0, 0, group)
    clock = pygame.time.Clock()
    game_map = GameMap(load_map('level_1.txt'), Settings.CELL_SIZE)
    player.add_observer(game_map)
    player.move_ip(300, 200)
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        keys = pygame.key.get_pressed()
        if keys[Settings.KEY_RIGHT]:
            player.move(1, 0)
        if keys[Settings.KEY_LEFT]:
            player.move(-1, 0)
        if keys[Settings.KEY_UP]:
            player.move(0, -1)
        if keys[Settings.KEY_DOWN]:
            player.move(0, 1)
        screen.fill(Settings.BACKGROUND)
        game_map.draw(screen)
        group.update()
        group.draw(screen)
        pygame.display.flip()
        clock.tick(Settings.FPS)


#!/usr/bin/python
# coding=utf-8
""" convert_to_pinyin.py
Usage: convert_to_pinyin.py to_convert
    convert string to_convert to Hanyu pinyin, writing results to stdout.
    Requires internet access.
"""
# going to use this tool: http://www.purpleculture.net/chinese-pinyin-converter/
# how to use: POST with a form, see below.

import urllib,urllib2,re,string,sys, time

class ConversionFailure(Exception):
    None

class NoHanCharacters(Exception):
    None

def get_conversion_result(to_convert,wait_time=0.5):
    # first test the string to make sure it has Han characters in it
    #any_han_yet = False
    if re.search(u'[\u4e00-\u9eff]', to_convert.decode('utf-8')) == None:
        #print >> sys.stderr, "Skipping the internet..."
        raise NoHanCharacters

    converter_url = "http://www.purpleculture.net/chinese-pinyin-converter/"
    conv_option = "number"
    if wait_time > 10:
        raise ConversionFailure

    # New converter, with two options
    # wdqchs:e.g.,中庸之道
    # tone:number

    # build request object
    my_post_data = urllib.urlencode({"wdqchs":to_convert,
                                    "tone": conv_option})
    my_request = urllib2.Request(converter_url, my_post_data)

    try:
        request_result = urllib2.urlopen(my_request)
    except urllib2.HTTPError as e:
        print >> sys.stderr, "Error " + str(e)
        print >> sys.stderr, "Waiting " + wait_time * 1000 + ' ms'
        time.sleep(wait_time)
        wait_time = wait_time * 1.8
        return get_conversion_result(to_convert, wait_time)

    return(request_result)

def extract_pinyin(request_result):
    result_lines = request_result.readlines()
    # get the pinyin out of it
    # our pinyin regex looks like this:
    pinyin_re = re.compile(r'^.*result = "(.*)";.*$')
    pinyin_final_space_re = re.compile(r' $')       # this is for culling the annoying final space
                                                    # introduced by the pinyin converter, which
                                                    # the program ends up interpreting as a missing
                                                    # element
    chinese_re = re.compile(r'^.*ori_cn += +"(.*)";.*$')

    # find the pinyin
    for result_line in result_lines:
        result_match = pinyin_re.match(result_line)
        if result_match != None:
            the_pinyin = pinyin_final_space_re.sub('', result_match.groups()[0])
            break
    # find the chinese
    for result_line in result_lines:
        #if result_line.find("ori_cn") != -1:
            #print >> sys.stderr, result_line
        result_match = chinese_re.match(result_line)
        if result_match != None:
            original_chinese = result_match.groups()[0]
            #print >> sys.stderr, "HIT!"
            break

    # now, we need to parse through the results and add back in characters
    # the converter left out
    # the pinyin data has *extra* spaces to show where stuff got left out
    pinyin_tokens = the_pinyin.split(" ")
    # this has ' ' for missing elements now.  missing elements are one-char-by-one

    original_tokens = list(original_chinese.decode('utf-8'))
    #print >> sys.stderr, "The pinyin: ", the_pinyin
    #print >> sys.stderr, pinyin_tokens
    #print >> sys.stderr, original_tokens
    pinyin_result = ""
    assert len(pinyin_tokens) == len(original_tokens)
    # need to keep track of what kind of token we last put on
    # in order to keep track of whitespace
    # probably a less fragile way of doing this exists
    TT_START = 0
    TT_PINSYL = 1
    TT_OTHERTOKEN = 2
    TT_WHITESPACE = 3
    last_token_type = TT_START
    for token_i in range(0,len(pinyin_tokens)):
        if pinyin_tokens[token_i] == "":
            if original_tokens[token_i] in (' ','\t'):
                last_token_type = TT_WHITESPACE
            else:
                last_token_type = TT_OTHERTOKEN
            pinyin_result = pinyin_result + original_tokens[token_i]
        else:
            py_suffix = " "
            # add a space before pinyin if last token was not pinyin or whitespace or beginning
            if last_token_type == TT_OTHERTOKEN:
                py_prefix = " "
            else:
                py_prefix = ""

            pinyin_result = pinyin_result + py_prefix + pinyin_tokens[token_i] + py_suffix
            last_token_type = TT_PINSYL

    return(pinyin_result.strip())

def main(to_convert):
    try:
        conversion_result = get_conversion_result(to_convert)
    except NoHanCharacters:
        pinyin = to_convert
        return pinyin.decode('utf-8')

    pinyin = extract_pinyin(conversion_result)
    return pinyin.encode('utf-8')

if __name__ == "__main__":
    print(main(sys.argv[1].strip()))


user_first_name = input("What is your first name?\n")
user_last_name = input("What is your last name?\n")

user_full_name = user_first_name + " " + user_last_name

print(f"So your full name is {user_full_name}?\n")

print("The code from here on is done from my desktop!\n")

num = input("Guess a number 1-10!\n")
correct_num = 8

if num == "8":
    print("That's right!\n")
else:
    print("Nope!\n")


from machine import Pin
from dht import DHT11
from time import sleep

dht11 = DHT11(Pin(15))

while True:
    dht11.measure()
    temp = dht11.temperature()
    humid = dht11.humidity()
    print("Temperature: %.2f degree celsius; Humidity: %.2f percent" % (temp, humid))
    sleep(1)


from django import forms

from .models import Comment, Reply

class CommentForm(forms.ModelForm):
    author_name = forms.CharField(
        label="Your name",
        max_length=128,
        widget=forms.TextInput()
    )
    author_email = forms.EmailField(
        label="Your email (will not be displayed)",
        required=True,
    )
    content = forms.CharField(
        label="Comment",
        max_length=8192,
        widget=forms.Textarea(),
        required=True
    )
    subscribe = forms.ChoiceField(
        initial=False,
        label="Subscribe to our newsletter?",
        widget=forms.CheckboxInput(),
        choices=((True, "Yes"), (False, "No"))
    )

    class Meta:
        model = Comment
        fields = ["author_name", "author_email", "content", "subscribe"]

class ReplyForm(forms.Form):
    author_name = forms.CharField(
        label="Your name",
        max_length=128,
        widget=forms.TextInput()
    )
    author_email = forms.EmailField(
        label="Your email (will not be displayed)",
        required=True,
    )
    content = forms.CharField(
        label="Comment",
        max_length=8192,
        widget=forms.Textarea(),
        required=True
    )

    class Meta:
        model = Reply
        fields = ["author_name", "author_email", "content"]


import commlib as cl
import numpy as np
import matplotlib.pyplot as plt

f0 = 2.5e9
RS = 40e6
TS = 1/RS

N = 10000
t = np.linspace(-TS, TS, N)
p = cl.square_pulse(t, TS)

c = cl.carrier(t, f0)

plt.close('all')
p.plot()
c.plot()

psamples = p.samples
to_intergrate = p.samples ** 2 * np.sin(4 * np.pi * f0 * t)
plt.figure()
plt.plot(t, to_intergrate)

I = np.trapz( to_intergrate, t )

I0 = np.trapz( psamples ** 2, t)


from urllib import response
import jwt
import uuid
import requests
import author
import hashlib
from urllib.parse import urlencode
import time
class Upbit():
    def __init__(self):
        self.access_key = author.access_key
        self.secret_key = author.secret_key
        self.server_url = 'https://api.upbit.com/v1/'
        self.non_auth_headers = {"Accept": "application/json"}

    def create_payload(self, query_hash = None): #권한 토큰 생성을 위한 변수 생성
        payload = {'access_key': self.access_key,
                    'nonce': str(uuid.uuid4())}
        if query_hash:
            payload['query_hash'] = query_hash #payload에 쿼리 대입
            payload['query_hash_alg'] = "SHA512"
        return payload

    def get_headers(self, payload): #토큰 형태로 만들어 반환
        jwt_token = jwt.encode(payload, self.secret_key)
        authorize_token = 'Bearer {}'.format(jwt_token)
        headers = {"Authorization": authorize_token}
        return headers #jwt token을 돌려준다.

    def get_hash_query(self,query):
        if query:
            query_string = urlencode(query).encode() #url 인코딩
            m = hashlib.sha512() #해싱
            m.update(query_string)
            query_hash = m.hexdigest() #쿼리 해싱 값 대입
            return query_hash

    def create_request(self,method,url,headers,query=None):
        query_hash = self.get_hash_query(query) #쿼리의 해시 값 구함
        while True:
            try:
                if headers != self.non_auth_headers: #권한이 필요하지 않은 요청의 경우 바로 다시금 요청한다.
                    payload = self.create_payload(query_hash) #uuid를 초기화 해준다.
                    headers = self.get_headers(payload)
                res = requests.request(method, url, params=query, headers=headers)
                if (res.status_code == 200) or (res.status_code == 201):  #알맞은 응답이 오기까지 반복한다.
                    json = res.json() #결과를 json형태로 반환한다.
                    return json
                print(f"Retry to get api: {res.json()}")
            except Exception as e:
                print(e)

    def get_my_account(self): #계좌에 있는 자산 조회
        url = f"{self.server_url}accounts"
        response = self.create_request("GET", url, headers=None)
        return response

    def create_order(self, ticker, side, volume, price, ord_type): #매수,매도 주문을 생성한다.
        query = {
            'market':ticker, #종목명
            'side': side, #매수,매도
            'volume': volume, #매매량
            'price':price,#지정가격
            'ord_type': ord_type, #시장가, 지정가 타입 선택
        }
        url = f"{self.server_url}orders"
        res = self.create_request("POST", url, headers = None, query = query)
        return res

    def get_current_price(self,ticker):
        url = f"{self.server_url}ticker?markets={ticker}"
        response = self.create_request("GET", url, headers=self.non_auth_headers)
        return response

    def get_order_book(self, ticker):
        url = f"{self.server_url}orderbook?markets={ticker}"
        response = self.create_request("GET", url, headers=self.non_auth_headers)
        return response

    def get_day_candle(self, ticker, candle_type="days", count = 1): #candle_type -> min,day, period -> 1min, 3min, 1day..
        url = f"{self.server_url}candles/{candle_type}/?market={ticker}&count={count}"
        response = self.create_request("GET",url, headers=self.non_auth_headers)
        return response


"""Base Perturbation Pattern module."""

import numpy as np
from copy import deepcopy
from sklearn.base import BaseEstimator

class BasePattern(BaseEstimator):
    """Base Perturbation Pattern.

    A pattern analyzes specific feature subsets to fully
    or partially adapt itself, and then create valid and
    coherent perturbations in new data.
    This base class cannot be directly utilized.

    It must be a class implementing the `fit`, `partial_fit` and
    `transform` methods, according to the following signatures:

    `fit(self, X, y=None) -> self`

    `partial_fit(self, X, y=None) -> self`

    `transform(self, X) -> numpy array`

    Parameters
    ----------
    features : int, array-like or None
        Index or array-like of indices of features
        whose values are to be analyzed and perturbed.

        Set to None to use all features.

    probability : float, in the (0.0, 1.0] interval
        Probability of applying the pattern in `transform`.

        Set to 1 to always apply the pattern.

    momentum : float, in the [0.0, 1.0] interval
        Momentum of the `partial_fit` updates.

        Set to 1 to remain fully adapted to the initial data, without updates.

        Set to 0 to always fully adapt to new data, as in `fit`.

    seed : int, None or a generator
        Seed for reproducible random number generation.

        Set to None to disable reproducibility,
        or to a generator to use it unaltered.

    Attributes
    ----------
    generator : numpy generator object
        The random number generator to be used by an inheriting class.
    """

    def __init__(
        self,
        features=None,
        probability=0.5,
        momentum=0.99,
        seed=None,
    ) -> None:

        self.set_momentum(momentum)
        self.set_probability(probability)
        self.set_features(features)
        self.set_seed(seed)

    def to_apply(self) -> bool:
        """Checks if the pattern is to be applied, according to the probability.

        Returns
        -------
        bool
            True if the pattern is to be applied; False otherwise.
        """
        return self.generator.random() < self.probability

    def fit_transform(self, X, y=None) -> np.ndarray:
        """Fully adapts the pattern to new data,
        and then applies it to create data perturbations.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Input data.

        y : ignored
            Parameter compatibility.

        Returns
        -------
        X_perturbed : numpy array of shape (n_samples, n_features)
            Perturbed data.
        """
        return self.fit(X, y).transform(X)

    def partial_fit_transform(self, X, y=None) -> np.ndarray:
        """Partially adapts the pattern to new data, according to the momentum,
        and then applies it to create data perturbations.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Input data.

        y : ignored
            Parameter compatibility.

        Returns
        -------
        X_perturbed : numpy array of shape (n_samples, n_features)
            Perturbed data.
        """
        return self.partial_fit(X, y).transform(X)

    def set_params(self, **params):
        """Sets the parameters.

        Parameters
        ----------
        **params : dict of 'parameter name - value' pairs
            New valid parameters for this pattern.

        Returns
        -------
        self
            This pattern instance.
        """
        mm = params.pop("momentum", self.momentum)
        pb = params.pop("probability", self.probability)
        ft = params.pop("features", self.features)
        sd = params.pop("seed", self.seed)

        super().set_params(params)

        self.set_momentum(mm)
        self.set_probability(pb)
        self.set_features(ft)
        self.set_seed(sd)

        return self

    def set_momentum(self, momentum) -> None:
        """Sets the momentum.

        Parameters
        ----------
        momentum : float, in the [0.0, 1.0] interval
            Momentum of the `partial_fit` updates.

            Set to 1 to remain fully adapted to the initial data, without updates.

            Set to 0 to always fully adapt to new data, as in `fit`.

        Raises
        ------
        ValueError
            If the parameters do not fulfill the constraints.
        """
        if float(momentum) != momentum or momentum < 0.0 or momentum > 1.0:
            raise ValueError("Momentum must be in the [0.0, 1.0] interval.")

        self.momentum = momentum

    def set_probability(self, probability) -> None:
        """Sets the probability.

        Parameters
        ----------
        probability : float, in the (0.0, 1.0] interval
            Probability of applying the pattern in `transform`.

            Set to 1 to always apply the pattern.

        Raises
        ------
        ValueError
            If the parameters do not fulfill the constraints.
        """
        if float(probability) != probability or probability <= 0.0 or probability > 1.0:
            raise ValueError("Probability must be in the (0.0, 1.0] interval.")

        self.probability = probability

    def set_features(self, features) -> None:
        """Sets the features.

        Parameters
        ----------
        features : int, array-like or None
            Index or array-like of indices of features
            whose values are to be perturbed.

            Set to None to use all features.

        Raises
        ------
        ValueError
            If the parameters do not fulfill the constraints.
        """
        if features is None:
            features = None

        elif isinstance(features, int):
            features = np.full(1, features)

        else:
            features = np.array(features, dtype=int)
            features = np.unique(features)

            if features.shape[0] == 0:
                features = None

            if np.min(features) < 0:
                raise ValueError("Feature indices must be positive values.")

        self.features = features

    def set_seed(self, seed) -> None:
        """Sets the seed for random number generation.

        Parameters
        ----------
        seed : int, None or a generator
            Seed for reproducible random number generation.

            Set to None to disable reproducibility, or
            set to a generator to use it unaltered.

        Raises
        ------
        ValueError
            If the parameters do not fulfill the constraints.
        """
        self.seed = deepcopy(seed)
        self.generator = np.random.default_rng(self.seed)


# Filename: retrieve_events.py

import pylink

# connect to the tracker and open an EDF
tk = pylink.EyeLink('100.1.1.1')
tk.openDataFile('ev_test.edf')

tk.sendCommand('sample_rate 1000') # set sampling rate to 1000 Hz

# make sure all types of event data are available over the link
tk.sendCommand("link_event_filter = LEFT,RIGHT,FIXATION,FIXUPDATE,SACCADE,BLINK,BUTTON,INPUT")

# open a window to calibrate the tracker
pylink.openGraphics()
tk.doTrackerSetup()
pylink.closeGraphics()

# start recording
error = tk.startRecording(1,1,1,1)
pylink.pumpDelay(100) # cache some samples for event parsing

t_start = tk.trackerTime() # current tracker time
while True:
    # break after 10 seconds have elapsed
    if tk.trackerTime() - t_start > 5000:
        break

    # retrieve the oldest event in the buffer
    dt = tk.getNextData()
    if dt > 0:
        ev = tk.getFloatData()
        if dt == pylink.STARTSACC:
            tk.sendMessage('STARTSACC %d'% ev.getTime())
        if dt == pylink.ENDSACC:
            tk.sendMessage('ENDSACC %d'% ev.getTime())
        if dt == pylink.STARTFIX:
            tk.sendMessage('STARTFIX %d'% ev.getTime())
        if dt == pylink.ENDFIX:
            tk.sendMessage('ENDFIX %d'% ev.getTime())

tk.stopRecording() # stop recording
tk.closeDataFile() # close EDF data file on the Host
tk.receiveDataFile('ev_test.edf', 'ev_test.edf') # retrieve EDF from Host
tk.close() #close the link to the tracker