import base64
import json
import logging
import os
from shlex import quote as shq
from typing import Any, Dict, List, Optional

from gear.cloud_config import get_global_config
from hailtop.config import get_deploy_config

from ....batch_configuration import DEFAULT_NAMESPACE, DOCKER_PREFIX, DOCKER_ROOT_IMAGE, INTERNAL_GATEWAY_IP
from ....file_store import FileStore
from ....instance_config import InstanceConfig
from ...resource_utils import unreserved_worker_data_disk_size_gib
from ...utils import ACCEPTABLE_QUERY_JAR_URL_PREFIX
from ..resource_utils import azure_machine_type_to_worker_type_and_cores

log = logging.getLogger('create_instance')

BATCH_WORKER_IMAGE = os.environ['HAIL_BATCH_WORKER_IMAGE']

log.info(f'BATCH_WORKER_IMAGE {BATCH_WORKER_IMAGE}')
log.info(f'ACCEPTABLE_QUERY_JAR_URL_PREFIX {ACCEPTABLE_QUERY_JAR_URL_PREFIX}')

def create_vm_config(
    file_store: FileStore,
    resource_rates: Dict[str, float],
    location: str,
    machine_name: str,
    machine_type: str,
    activation_token: str,
    max_idle_time_msecs: int,
    local_ssd_data_disk: bool,
    data_disk_size_gb: int,
    preemptible: bool,
    job_private: bool,
    subscription_id: str,
    resource_group: str,
    ssh_public_key: str,
    max_price: Optional[float],
    instance_config: InstanceConfig,
    feature_flags: dict,
) -> dict:
    _, cores = azure_machine_type_to_worker_type_and_cores(machine_type)

    hail_azure_oauth_scope = os.environ['HAIL_AZURE_OAUTH_SCOPE']
    region = instance_config.region_for(location)

    if max_price is not None and not preemptible:
        raise ValueError(f'max price given for a nonpreemptible machine {max_price}')

    if job_private:
        unreserved_disk_storage_gb = data_disk_size_gb
    else:
        unreserved_disk_storage_gb = unreserved_worker_data_disk_size_gib(data_disk_size_gb, cores)
    assert unreserved_disk_storage_gb >= 0

    worker_data_disk_name = 'data-disk'

    if local_ssd_data_disk:
        data_disks = []
        disk_location = '/dev/disk/azure/resource'
    else:
        data_disks = [
            {
                "name": "[concat(parameters('vmName'), '-data')]",
                "lun": 2,  # because this is 2, the data disk will always be at 'sdc'
                "managedDisk": {"storageAccountType": "Premium_LRS"},
                "createOption": "Empty",
                "diskSizeGB": data_disk_size_gb,
                "deleteOption": 'Delete',
            }
        ]
        disk_location = '/dev/disk/azure/scsi1/lun2'

    make_global_config = ['mkdir /global-config']
    global_config = get_global_config()
    for name, value in global_config.items():
        make_global_config.append(f'echo -n {shq(value)} > /global-config/{name}')
    make_global_config_str = '\n'.join(make_global_config)

    assert instance_config.is_valid_configuration(resource_rates.keys())

    touch_commands: List[str] = []
    for jvm_cores in (1, 2, 4, 8):
        for _ in range(cores // jvm_cores):
            idx = len(touch_commands)
            log_path = f'/batch/jvm-container-logs/jvm-{idx}.log'
            touch_commands.append(f'sudo touch {log_path}')

    jvm_touch_command = '\n'.join(touch_commands)

    startup_script = r'''#cloud-config

mounts:
  - [ ephemeral0, null ]
  - [ ephemeral0.1, null ]

write_files:
  - owner: batch-worker:batch-worker
    path: /startup.sh
    content: |
      #!/bin/sh
      set -ex
      RESOURCE_GROUP=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/resourceGroupName?api-version=2021-02-01&format=text")
      NAME=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/name?api-version=2021-02-01&format=text")
      if [ -f "/started" ]; then
          echo "instance $NAME has previously been started"
          while true; do
          az vm delete -g $RESOURCE_GROUP -n $NAME --yes
          sleep 1
          done
          exit
      else
          touch /started
      fi
      curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/userData?api-version=2021-02-01&format=text" | \
        base64 --decode | \
        jq -r '.run_script' > ./run.sh
      nohup /bin/bash run.sh >run.log 2>&1 &

runcmd:
  - sh /startup.sh
'''
    startup_script = base64.b64encode(startup_script.encode('utf-8')).decode('utf-8')

    run_script = f'''
#!/bin/bash
set -x

WORKER_DATA_DISK_NAME="{worker_data_disk_name}"
UNRESERVED_WORKER_DATA_DISK_SIZE_GB="{unreserved_disk_storage_gb}"
ACCEPTABLE_QUERY_JAR_URL_PREFIX="{ACCEPTABLE_QUERY_JAR_URL_PREFIX}"

# format worker data disk
sudo mkfs.xfs -f -m reflink=1 -n ftype=1 {disk_location}
sudo mkdir -p /mnt/disks/$WORKER_DATA_DISK_NAME
sudo mount -o prjquota {disk_location} /mnt/disks/$WORKER_DATA_DISK_NAME
sudo chmod a+w /mnt/disks/$WORKER_DATA_DISK_NAME
XFS_DEVICE=$(xfs_info /mnt/disks/$WORKER_DATA_DISK_NAME | head -n 1 | awk '{{ print $1 }}' | awk  'BEGIN {{ FS = "=" }}; {{ print $2 }}')

# reconfigure docker to use data disk
sudo service docker stop
sudo mv /var/lib/docker /mnt/disks/$WORKER_DATA_DISK_NAME/docker
sudo ln -s /mnt/disks/$WORKER_DATA_DISK_NAME/docker /var/lib/docker
sudo service docker start

# reconfigure /batch and /logs to use data disk
sudo mkdir -p /mnt/disks/$WORKER_DATA_DISK_NAME/batch/
sudo ln -s /mnt/disks/$WORKER_DATA_DISK_NAME/batch /batch

sudo mkdir -p /batch/jvm-container-logs/
{jvm_touch_command}

sudo mkdir -p /mnt/disks/$WORKER_DATA_DISK_NAME/logs/
sudo ln -s /mnt/disks/$WORKER_DATA_DISK_NAME/logs /logs

sudo mkdir -p /mnt/disks/$WORKER_DATA_DISK_NAME/cloudfuse/
sudo ln -s /mnt/disks/$WORKER_DATA_DISK_NAME/cloudfuse /cloudfuse

# Forward syslog logs to Log Analytics Agent
cat >>/etc/rsyslog.d/95-omsagent.conf <<EOF
kern.warning       @127.0.0.1:25224
user.warning       @127.0.0.1:25224
daemon.warning     @127.0.0.1:25224
auth.warning       @127.0.0.1:25224
uucp.warning       @127.0.0.1:25224
authpriv.warning   @127.0.0.1:25224
ftp.warning        @127.0.0.1:25224
cron.warning       @127.0.0.1:25224
local0.warning     @127.0.0.1:25224
local1.warning     @127.0.0.1:25224
local2.warning     @127.0.0.1:25224
local3.warning     @127.0.0.1:25224
local4.warning     @127.0.0.1:25224
local5.warning     @127.0.0.1:25224
local6.warning     @127.0.0.1:25224
local7.warning     @127.0.0.1:25224
EOF

sudo service rsyslog restart

sudo mkdir -p /etc/netns

curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/userData?api-version=2021-02-01&format=text" | \
  base64 --decode > userdata

SUBSCRIPTION_ID=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/subscriptionId?api-version=2021-02-01&format=text")
RESOURCE_GROUP=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/resourceGroupName?api-version=2021-02-01&format=text")
LOCATION=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?api-version=2021-02-01&format=text")

CORES=$(nproc)
NAMESPACE=$(jq -r '.namespace' userdata)
ACTIVATION_TOKEN=$(jq -r '.activation_token' userdata)
IP_ADDRESS=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress?api-version=2021-02-01&format=text")

BATCH_LOGS_STORAGE_URI=$(jq -r '.batch_logs_storage_uri' userdata)
INSTANCE_ID=$(jq -r '.instance_id' userdata)
INSTANCE_CONFIG=$(jq -r '.instance_config' userdata)
MAX_IDLE_TIME_MSECS=$(jq -r '.max_idle_time_msecs' userdata)
NAME=$(curl -s -H Metadata:true --noproxy "*" "http://169.254.169.254/metadata/instance/compute/name?api-version=2021-02-01&format=text")

BATCH_WORKER_IMAGE=$(jq -r '.batch_worker_image' userdata)
DOCKER_ROOT_IMAGE=$(jq -r '.docker_root_image' userdata)
DOCKER_PREFIX=$(jq -r '.docker_prefix' userdata)
REGION=$(jq -r '.region' userdata)
HAIL_AZURE_OAUTH_SCOPE=$(jq -r '.hail_azure_oauth_scope' userdata)

INTERNAL_GATEWAY_IP=$(jq -r '.internal_ip' userdata)

# private job network = 172.20.0.0/16
# public job network = 172.21.0.0/16
# [all networks] Rewrite traffic coming from containers to masquerade as the host
iptables --table nat --append POSTROUTING --source 172.20.0.0/15 --jump MASQUERADE

# [public]
# Block public traffic to the metadata server
iptables --append FORWARD --source 172.21.0.0/16 --destination 169.254.169.254 --jump DROP
# But allow the internal gateway
iptables --append FORWARD --destination $INTERNAL_GATEWAY_IP --jump ACCEPT
# And this worker
iptables --append FORWARD --destination $IP_ADDRESS --jump ACCEPT
# Forbid outgoing requests to cluster-internal IP addresses
INTERNET_INTERFACE=eth0
iptables --append FORWARD --out-interface $INTERNET_INTERFACE ! --destination 10.128.0.0/16 --jump ACCEPT

cat >> /etc/hosts <<EOF
$INTERNAL_GATEWAY_IP batch-driver.hail
$INTERNAL_GATEWAY_IP batch.hail
$INTERNAL_GATEWAY_IP internal.hail
EOF

{make_global_config_str}

mkdir /deploy-config
cat >/deploy-config/deploy-config.json <<EOF
{ json.dumps(get_deploy_config().with_location('gce').get_config()) }
EOF

# retry once
az acr login --name $DOCKER_PREFIX
docker pull $BATCH_WORKER_IMAGE || \
(echo 'pull failed, retrying' && sleep 15 && docker pull $BATCH_WORKER_IMAGE)

BATCH_WORKER_IMAGE_ID=$(docker inspect $BATCH_WORKER_IMAGE --format='{{{{.Id}}}}' | cut -d':' -f2)

# So here I go it's my shot.
docker run \
--name worker \
-e CLOUD=azure \
-e CORES=$CORES \
-e NAME=$NAME \
-e NAMESPACE=$NAMESPACE \
-e ACTIVATION_TOKEN=$ACTIVATION_TOKEN \
-e IP_ADDRESS=$IP_ADDRESS \
-e BATCH_LOGS_STORAGE_URI=$BATCH_LOGS_STORAGE_URI \
-e INSTANCE_ID=$INSTANCE_ID \
-e SUBSCRIPTION_ID=$SUBSCRIPTION_ID \
-e RESOURCE_GROUP=$RESOURCE_GROUP \
-e LOCATION=$LOCATION \
-e REGION=$REGION \
-e HAIL_AZURE_OAUTH_SCOPE=$HAIL_AZURE_OAUTH_SCOPE \
-e DOCKER_PREFIX=$DOCKER_PREFIX \
-e DOCKER_ROOT_IMAGE=$DOCKER_ROOT_IMAGE \
-e INSTANCE_CONFIG=$INSTANCE_CONFIG \
-e MAX_IDLE_TIME_MSECS=$MAX_IDLE_TIME_MSECS \
-e BATCH_WORKER_IMAGE=$BATCH_WORKER_IMAGE \
-e BATCH_WORKER_IMAGE_ID=$BATCH_WORKER_IMAGE_ID \
-e INTERNET_INTERFACE=$INTERNET_INTERFACE \
-e UNRESERVED_WORKER_DATA_DISK_SIZE_GB=$UNRESERVED_WORKER_DATA_DISK_SIZE_GB \
-e ACCEPTABLE_QUERY_JAR_URL_PREFIX=$ACCEPTABLE_QUERY_JAR_URL_PREFIX \
-e INTERNAL_GATEWAY_IP=$INTERNAL_GATEWAY_IP \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/run/netns:/var/run/netns:shared \
-v /usr/bin/docker:/usr/bin/docker \
-v /batch:/batch:shared \
-v /logs:/logs \
-v /global-config:/global-config \
-v /deploy-config:/deploy-config \
-v /cloudfuse:/cloudfuse:shared \
-v /etc/netns:/etc/netns \
-v /sys/fs/cgroup:/sys/fs/cgroup \
--mount type=bind,source=/mnt/disks/$WORKER_DATA_DISK_NAME,target=/host \
--mount type=bind,source=/dev,target=/dev,bind-propagation=rshared \
-p 5000:5000 \
--device /dev/fuse \
--device $XFS_DEVICE \
--device /dev \
--privileged \
--cap-add SYS_ADMIN \
--security-opt apparmor:unconfined \
--network host \
--cgroupns host \
$BATCH_WORKER_IMAGE \
python3 -u -m batch.worker.worker >worker.log 2>&1

[ $? -eq 0 ] || tail -n 1000 worker.log

while true; do
az vm delete -g $RESOURCE_GROUP -n $NAME --yes
sleep 1
done
'''

    user_data = {
        'run_script': run_script,
        'activation_token': activation_token,
        'batch_worker_image': BATCH_WORKER_IMAGE,
        'docker_root_image': DOCKER_ROOT_IMAGE,
        'docker_prefix': DOCKER_PREFIX,
        'namespace': DEFAULT_NAMESPACE,
        'internal_ip': INTERNAL_GATEWAY_IP,
        'batch_logs_storage_uri': file_store.batch_logs_storage_uri,
        'instance_id': file_store.instance_id,
        'max_idle_time_msecs': max_idle_time_msecs,
        'instance_config': base64.b64encode(json.dumps(instance_config.to_dict()).encode()).decode(),
        'region': region,
        'hail_azure_oauth_scope': hail_azure_oauth_scope,
    }
    user_data_str = base64.b64encode(json.dumps(user_data).encode('utf-8')).decode('utf-8')

    tags = {'namespace': DEFAULT_NAMESPACE, 'batch-worker': '1'}

    vm_resources = []

    if feature_flags['oms_agent']:
        vm_resources.append(
            {
                'apiVersion': '2018-06-01',
                'type': 'extensions',
                'name': 'OMSExtension',
                'location': "[parameters('location')]",
                'tags': tags,
                'dependsOn': ["[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"],
                'properties': {
                    'publisher': 'Microsoft.EnterpriseCloud.Monitoring',
                    'type': 'OmsAgentForLinux',
                    'typeHandlerVersion': '1.13',
                    'autoUpgradeMinorVersion': False,
                    'enableAutomaticUpgrade': False,
                    'settings': {
                        'workspaceId': "[reference(resourceId('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName')), '2015-03-20').customerId]"
                    },
                    'protectedSettings': {
                        'workspaceKey': "[listKeys(resourceId('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName')), '2015-03-20').primarySharedKey]"
                    },
                },
            },
        )

    vm_config: Dict[str, Any] = {
        'apiVersion': '2021-03-01',
        'type': 'Microsoft.Compute/virtualMachines',
        'name': "[parameters('vmName')]",
        'location': "[parameters('location')]",
        'identity': {
            'type': 'UserAssigned',
            'userAssignedIdentities': {
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', parameters('userAssignedIdentityName'))]": {}
            },
        },
        'tags': tags,
        'dependsOn': ["[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"],
        'properties': {
            'hardwareProfile': {'vmSize': machine_type},
            'networkProfile': {
                'networkInterfaces': [
                    {
                        'id': "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]",
                        'properties': {'deleteOption': 'Delete'},
                    }
                ]
            },
            'storageProfile': {
                'osDisk': {
                    'name': "[concat(parameters('vmName'), '-os')]",
                    'createOption': 'FromImage',
                    'deleteOption': 'Delete',
                    'caching': 'ReadOnly',
                    'managedDisk': {'storageAccountType': 'Standard_LRS'},
                },
                'imageReference': "[parameters('imageReference')]",
                'dataDisks': data_disks,
            },
            'osProfile': {
                'computerName': "[parameters('vmName')]",
                'adminUsername': "[parameters('adminUsername')]",
                'customData': "[parameters('startupScript')]",
                'linuxConfiguration': {
                    'disablePasswordAuthentication': True,
                    'ssh': {
                        'publicKeys': [
                            {
                                'keyData': "[parameters('sshKey')]",
                                'path': "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                            }
                        ]
                    },
                },
            },
            'userData': "[parameters('userData')]",
        },
        'resources': vm_resources,
    }

    properties = vm_config['properties']
    if preemptible:
        properties['priority'] = 'Spot'
        properties['evictionPolicy'] = 'Delete'
        properties['billingProfile'] = {'maxPrice': max_price if max_price is not None else -1}
    else:
        properties['priority'] = 'Regular'

    return {
        'tags': tags,
        'properties': {
            'mode': 'Incremental',
            'parameters': {
                'location': {'value': location},
                'vmName': {'value': machine_name},
                'sshKey': {'value': ssh_public_key},
                'subnetId': {
                    'value': f'/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Network/virtualNetworks/default/subnets/batch-worker-subnet'
                },
                'adminUsername': {'value': 'batch-worker'},
                'userAssignedIdentityName': {'value': 'batch-worker'},
                'startupScript': {'value': startup_script},
                'userData': {'value': user_data_str},
                'imageReference': {
                    'value': {
                        'id': f'/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/'
                        f'Microsoft.Compute/galleries/{resource_group}_batch/images/batch-worker-22-04/versions/0.0.13'
                    }
                },
                'workspaceName': {
                    'value': f'{resource_group}-logs',
                },
            },
            'template': {
                '$schema': 'https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#',
                'contentVersion': '1.0.0.0',
                'parameters': {
                    'location': {'type': 'string', 'defaultValue': '[resourceGroup().location]'},
                    'vmName': {'type': 'string'},
                    'sshKey': {'type': 'securestring'},
                    'subnetId': {'type': 'string'},
                    'adminUsername': {'type': 'string', 'defaultValue': 'admin'},
                    'userAssignedIdentityName': {'type': 'string', 'defaultValue': 'batch-worker'},
                    'startupScript': {'type': 'string'},
                    'userData': {'type': 'string'},
                    'imageReference': {
                        'type': 'object',
                        'defaultValue': {
                            'publisher': 'Canonical',
                            'offer': 'UbuntuServer',
                            'sku': '18.04-LTS',
                            'version': 'latest',
                        },
                    },
                    'workspaceName': {'type': 'string'},
                },
                'variables': {
                    'ipName': "[concat(parameters('vmName'), '-ip')]",
                    'nicName': "[concat(parameters('vmName'), '-nic')]",
                    'ipconfigName': "[concat(parameters('vmName'), '-ipconfig')]",
                },
                'resources': [
                    {
                        'apiVersion': '2018-01-01',
                        'type': 'Microsoft.Network/publicIPAddresses',
                        'name': "[variables('ipName')]",
                        'location': "[parameters('location')]",
                        'tags': tags,
                        'dependsOn': [],
                        'properties': {'publicIPAllocationMethod': 'Static'},
                    },
                    {
                        'apiVersion': '2015-06-15',
                        'type': 'Microsoft.Network/networkInterfaces',
                        'name': "[variables('nicName')]",
                        'location': "[parameters('location')]",
                        'tags': tags,
                        'dependsOn': ["[concat('Microsoft.Network/publicIPAddresses/', variables('ipName'))]"],
                        'properties': {
                            'ipConfigurations': [
                                {
                                    'name': "[variables('ipconfigName')]",
                                    'properties': {
                                        'publicIPAddress': {
                                            'id': "[resourceId('Microsoft.Network/publicIpAddresses', variables('ipName'))]",
                                            'properties': {'deleteOption': 'Delete'},
                                        },
                                        'privateIPAllocationMethod': 'Dynamic',
                                        'subnet': {'id': "[parameters('subnetId')]"},
                                    },
                                }
                            ],
                            'networkSecurityGroup': {
                                'id': f'/subscriptions/{subscription_id}/resourceGroups/{resource_group}'
                                f'/providers/Microsoft.Network/networkSecurityGroups/batch-worker-nsg'
                            },
                        },
                    },
                    vm_config,
                ],
                'outputs': {},
            },
        },
    }


import thorpy.flipmount.flipmount as fm

from tango import AttrWriteType, DispLevel, DevState
from tango.server import Device, attribute, command, device_property
from enum import IntEnum

class MirrorState(IntEnum):
    closed = 0
    open = 1
    moving = 2
    unknown = 3

class ThorlabsMFF100(Device):
    mffstate = attribute(
        label="Mirror State",
        dtype=MirrorState,
        display_level=DispLevel.OPERATOR,
        access=AttrWriteType.READ,
        doc="State can be 0=closed, 1=open, 2=moving, 3=unknown",
    )

    serial_num = attribute(
        label="Serialnumber",
        dtype=str,
        display_level=DispLevel.OPERATOR,
        access=AttrWriteType.READ,
        doc="Serial number of device",
    )

    inverted = attribute(
        label="Invert states",
        dtype=bool,
        display_level=DispLevel.EXPERT,
        access=AttrWriteType.READ_WRITE,
        doc="Invert open and close states of mirror in TANGO only!",
        memorized=True,
        hw_memorized=True,
    )

    serial_number = device_property(dtype=str)

    def init_device(self):
        Device.init_device(self)
        self.info_stream(
            "Thorlabs Flip Mirror Mount with serial {:s}".format(self.serial_number)
        )
        self.mount = fm.flipMount(self.serial_number)
        self.__mff_state = 3
        self.__inverted = False

    def always_executed_hook(self):
        if self.mount.is_moving():
            self.set_status("Thorlabs Flip Mirror is MOVING")
            self.__mff_state = 2
            self.set_state(DevState.MOVING)
        elif self.mount.is_open():
            if self.__inverted:
                self.set_status("Thorlabs Flip Mirror is CLOSED")
                self.__mff_state = 0
                self.set_state(DevState.CLOSE)
            else:
                self.set_status("Thorlabs Flip Mirror is OPEN")
                self.__mff_state = 1
                self.set_state(DevState.OPEN)
        elif self.mount.is_close():
            if self.__inverted:
                self.set_status("Thorlabs Flip Mirror is OPEN")
                self.__mff_state = 1
                self.set_state(DevState.OPEN)
            else:
                self.set_status("Thorlabs Flip Mirror is CLOSED")
                self.__mff_state = 0
                self.set_state(DevState.CLOSE)
        else:
            self.set_status("Thorlabs Flip Mirror is UNKOWN")
            self.__mff_state = 3
            self.set_state(DevState.UNKNOWN)

    def read_mffstate(self):
        return self.__mff_state
        if self.__mff_state == 0:
            return MirrorState.closed
        elif self.__mff_state == 1:
            return MirrorState.open
        if self.__mff_state == 2:
            return MirrorState.moving
        else:
            return MirrorState.unknown

    def read_serial_num(self):
        return self.serial_number

    def read_inverted(self):
        return self.__inverted

    def write_inverted(self, value):
        self.__inverted = bool(value)

    def read_info(self):
        return "Information", dict(
            manufacturer="Thorlabs", model="MFF101", version_number=self.__serial_num
        )

    @command
    def Flip(self):
        self.mount.flip()

    @command
    def Close(self):
        if self.__inverted:
            self.mount.open()
        else:
            self.mount.close()

    @command
    def Open(self):
        if self.__inverted:
            self.mount.close()
        else:
            self.mount.open()

    @command
    def Identify(self):
        self.mount.identify()

if __name__ == "__main__":
    ThorlabsMFF100.run_server()


import abc
from contextlib import asynccontextmanager
import functools
import hashlib
import json
import typing
import warnings

from ponty.memo.lock.base import Lock, Locked
from ponty.registry import Registry

class Stampede(Locked):
    """Raised when the cache comes under high load and the lock times out.

    Inherits :class:`Locked <ponty.memo.Locked>`.

    """

class cachemiss:
    """Sentinel value representing a cache miss.

    Returned by subclasses of :class:`CacheStore`.

    """

_T = typing.TypeVar("_T")

class CacheStore(abc.ABC, typing.Generic[_T]):
    """Abstract Base Class. Container for cached items.

    :func:`cache` expects two abstract methods,
    :meth:`get <ponty.memo.CacheStore.get>` and
    :meth:`set <ponty.memo.CacheStore.set>`, and
    :func:`invalidate` expects one,
    :meth:`remove <ponty.memo.CacheStore.remove>`.
    All three must return awaitables.

    Generic on one variable, the type T of the cached item.

    """

    @abc.abstractmethod
    async def get(self, key: str) -> typing.Union[_T, type[cachemiss]]:
        """Fetch the cached value.

        Errors are not captured.

        :param key: unique id for the cached item
        :returns: the cached item,
          or the :class:`cachemiss` sentinel if it does not exist
        :rtype: Union[T, type[:class:`cachemiss`]]

        """

    @abc.abstractmethod
    async def set(self, key: str, data: _T) -> None:
        """Add an item to the cache.

        :param key: unique id for the item
        :param data: the item to be cached

        """

    @abc.abstractmethod
    async def remove(self, key: str) -> bool:
        """Remove an item from the cache.

        :param key: unique id for the cached item
        :returns: True if the item is found and removed, False otherwise

        """

_registry = Registry[tuple[CacheStore, Lock]]()

def cache(store: CacheStore, antistampede: Lock, name: str = ""):
    """Cache the decorated function's return value. Not thread-safe.

    As a rule of thumb, storage mechanics for a cache are
    implemented in `store`, and synchronization rules are handled
    by `antistampede`.
    See the :func:`localcache`
    `source ↗ <https://github.com/csira/ponty/blob/main/ponty/memo/cache/local.py>`__
    for an example.

    :param store: container, implementing get/set mechanics for cached items
    :param antistampede: instance of :class:`ponty.memo.Lock`;
      blocks on simultaneous access to the requested key to avoid a stampede
    :param name: if provided, registers the cache for use by
      :func:`invalidate`. Must be unique

    """
    def wraps(f):
        if name:
            _registry.add(name, (store, antistampede))

        @functools.wraps(f)
        async def wrapper(*a, **kw):
            key = _hash_args(*a, **kw)

            async with antistampede.lock(key):
                data = await store.get(key)
                if data is not cachemiss:
                    return data

                data = await f(*a, **kw)
                await store.set(key, data)

            return data

        return wrapper
    return wraps

@asynccontextmanager
async def invalidate(cachename: str, *a, **kw) -> typing.AsyncIterator[None]:
    """Context manager. Invalidates an item in the cache.

    Waits for then holds the cache's lock until the context manager exits.
    This prevents simultaneous fetches from re-cache-ing the item before
    mutations are committed. This also means functions calling 'invalidate'
    CANNOT be wrapped with a cache decorator, as the decorator will already
    hold the lock.

    :param cachename: name of the registered cache.
      Must match :func:`cache`'s `name` parameter

    Note the \*arg/\**kwarg combo must match the arguments to the
    cache-decorated function EXACTLY in order to get a key hit. E.g.,

    .. code-block:: python
        :emphasize-lines: 2,7,9

        @localcache(name="foo")
        async def get_foo(foo_id):
            ...

        async def update_foo(foo_id, ...):
            async with invalidate("foo", foo_id):  # this hits
                ...
            async with invalidate("foo", foo_id=foo_id):  # this does not
                ...

    """
    key = _hash_args(*a, **kw)

    try:
        store, antistampede = _registry.get(cachename)
    except KeyError:
        warnings.warn(f"'{cachename}' is not registered", category=RuntimeWarning)
        yield
    else:
        async with antistampede.lock(key):
            await store.remove(key)
            yield

def _hash_args(*a, **kw) -> str:
    args = a + tuple(sorted(kw.items()))
    encoded = json.dumps(args).encode("utf-8")
    return hashlib.sha1(encoded).hexdigest()


# -*- coding: utf-8 -*-
"""
Created on Wed Sep 25 16:52:48 2019

@author: Bohrium.Kwong
"""

from keras.models import load_model
from keras.optimizers import SGD
from keras.utils import to_categorical
from tqdm._tqdm import trange
import numpy as np
from skimage import io
from scipy.misc import imresize
import metrics
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
import gc
import glob
import os

class Test_epoch_from_array:
    r"""
    :param model: a model that loaded with keras
    :param input_array: a 4D numpy array [n,h,w,c] which includes n images transform to numpy array(RGB mode)
    :param batch_size: the value of batch_size in model's predicting
    :param verbose: show the information of tdqm method with dataloaders in processing or not
    """
    def __init__(self, model, input_array,batch_size=256,mean = [0,0,0], std = [1,1,1],verbose=False):
        self.model = model
        self.input_array = input_array
        self.batch_size = batch_size
        self.mean = mean
        self.std = std
        self.verbose = verbose

    def predict(self):
        self.input_array /= 255
        for i in range(3):
            self.input_array[:,:,:,i] -= self.mean[i]
            self.input_array[:,:,:,i] /= self.std[i]
        if self.input_array.shape[0] <= self.batch_size :
            output_predict =  self.model.predict(self.input_array,batch_size = self.batch_size)

        else:
            batch_count = self.input_array.shape[0]// self.batch_size
#            with tqdm(dataloaders[phase], desc = phase, file=sys.stdout, disable=not (self.verbose)) as iterator:
            for i in trange(batch_count + 1,disable=not (self.verbose)):
                top = i * self.batch_size
                bottom = min(self.input_array.shape[0], (i+1) * self.batch_size)
                if top < self.input_array.shape[0]:
                    output_predict_batch =  self.model.predict(self.input_array[top:bottom,:,:,:].copy(),batch_size = self.batch_size)
                    if i ==0 :
                        output_predict = output_predict_batch.copy()
                    else:
                        output_predict = np.row_stack((output_predict,output_predict_batch.copy()))
                    del image_tensor,output_predict_batch,output
                    gc.collect()
        return output_predict

def get_model_loaded(file_path,out_objects = False,objects_dict = None):
    """
    load the model trained by Keras base on the file name
    :param file_path: the saved model's file_path
    :param out_objects: True/False ,adds custom_objects or not in loadding model
    :param objects_dict: a dict that matches the model which you want to load
    :return: keras model object
    """
    if out_objects and objects_dict is not None:
        model = load_model(file_path,custom_objects=objects_dict)
    else:
        model = load_model(file_path)

    model.compile(loss='categorical_crossentropy',
              optimizer=SGD(lr=1e-2, momentum=0.1,decay=0.001, nesterov=True,clipvalue=0.7),
              metrics=['accuracy'])
    return model

def plot_confusion_matrix(test_labels, predictions,label_list):
    """
    the method of draw confusion_matrix
    """
    cm = confusion_matrix(test_labels, predictions)
    plt.imshow(cm, interpolation='nearest', cmap='Pastel1')
    plt.title('Confusion matrix', size=15)
    plt.colorbar()
    tick_marks = np.arange(3)
    plt.xticks(tick_marks, label_list,rotation=45, size=10)
    plt.yticks(tick_marks, label_list,size=10)
    plt.tight_layout()
    plt.ylabel('Actual label',size=15)
    plt.xlabel('Predicted label',size=15)
    width, height=cm.shape
    for x in range(width):
        for y in range(height):
            plt.annotate(str(cm[x][y]),xy=(y,x),horizontalalignment='center',verticalalignment='center')
    plt.show()

if __name__ == '__main__':

    np.set_printoptions(suppress=True)

    objects_dict = {'precision':metrics.precision,'recall':metrics.recall,'fmeasure':metrics.fscore}
    #如果模型有使用自定义的指标(metrics.*)，就需要定义objects_dict字典，以便进行加载
    model = get_model_loaded('Xception_299_.hdf5',out_objects = True,objects_dict = objects_dict)

    #以下是预测单张图片的方法
    I = io.imread('./test_png')
    I = np.expand_dims(I, axis=0)
    preds = model.predict_classes(I)
    print('Predicted:', preds)
    prob = model.predict_proba(I)
    print('Probality:', prob)
#    """
#    Predicted: [1]
#    Probality: [[0.00000055 0.9999994 ]]
#    """
    print(prob[0][1])
    # 0.9999994

    #以下是预测指定目录下多张图片的方法
    test_dataset_dir = '/test/'

    test_folders = ["/test/label_0/", "/test/label_1/"]

    data_set_name = test_folders[0].split("/")[-3]

    print("\nImages for Testing")
    print("=" * 30)

    test_images = []
    test_labels = []

    for index, folder in enumerate(test_folders):
        files = glob.glob(folder + "*.png")
        images = io.imread_collection(files)
#        images = [imresize(image, (299, 299)) for image in images] ### Reshape to (299, 299, 3) ###
        labels = [index] * len(images)
        test_images = test_images + images
        test_labels = test_labels + labels
        print("Class: %s. Size: %d" %(folder.split("/")[-2], len(images)))

    test_images = test_images.astype(np.float32) ### Standardise

    test_labels = np.array(test_labels).astype(np.int32)
    Y_test = to_categorical(test_labels, num_classes = np.unique(test_labels).shape[0])

    test_loss, test_accuracy = model.evaluate(test_images/255, Y_test, batch_size = 50)
    # 这里调用的是keras模型自带的evaluate方法进行评分

    print("\n" + test_folders[0].split("/")[1] + " Loss: %.3f" %(test_loss))
    print("\n" + test_folders[0].split("/")[1] + " Accuracy: %.3f" %(test_accuracy))
    print("=" * 30, "\n")

    posteriors = Test_epoch_from_array(model,test_images,256)

    predictions = np.argmax(posteriors, axis = 1)

    print(classification_report(test_labels, predictions, target_names = os.listdir(test_dataset_dir),
                                digits = len(os.listdir(test_dataset_dir))))
    # 打印classification_report

    plot_confusion_matrix(test_labels, predictions,os.listdir(test_dataset_dir))
    # 画混淆矩阵


from flask import render_template
from flask_login import current_user
from datetime import datetime, date
from .models.match import Match
from flask import Blueprint, redirect, url_for, request
bp = Blueprint('waiting', __name__)

@bp.route('/waiting', methods=['POST', 'GET'])
def waiting():
    if not current_user.is_authenticated:
        return redirect(url_for('rankables.login'))
    now = datetime.now()

    # get all available products for sale:
    matches = Match.get_user_waiting_matches(current_user.rankable_id, now)

    return render_template('waiting.html', my_matches=matches)

@bp.route('/waiting_delete/<match_id>', methods=['POST', 'GET'])
def waiting_delete(match_id):
    Match.delete(match_id)
    now = datetime.now()
    matches = Match.get_user_waiting_matches(current_user.rankable_id, now)

    return render_template('waiting.html', my_matches=matches)

@bp.route('/accept/<match_id>', methods=['POST', 'GET'])
def accept(match_id):
    Match.accept(match_id)
    now = datetime.now()
    matches = Match.get_user_waiting_matches(current_user.rankable_id, now)

    return render_template('waiting.html', my_matches=matches)


import utils

class CoreMemory(object):
    def __init__(self, persona, human):
        self.persona = persona
        self.human = human

    def __repr__(self) -> str:
        return f"\n### CORE MEMORY ###" + f"\n=== Persona ===\n{self.persona}" + f"\n\n=== Human ===\n{self.human}"

    def edit_persona(self, new_persona):
        self.persona = new_persona

    def edit_human(self, new_human):
        self.human = new_human

    def append(self, field, content, sep="\n"):
        if field == "persona":
            new_content = self.persona + sep + content
            self.edit_persona(new_content)
        elif field == "human":
            new_content = self.human + sep + content
            self.edit_human(new_content)
        else:
            raise KeyError

    def replace(self, field, old_content, new_content):
        if field == "persona":
            if old_content in self.persona:
                new_persona = self.persona.replace(old_content, new_content)
                self.edit_persona(new_persona)
            else:
                raise ValueError("Content not found in persona (make sure to use exact string)")
        elif field == "human":
            if old_content in self.human:
                new_human = self.human.replace(old_content, new_content)
                self.edit_human(new_human)
            else:
                raise ValueError("Content not found in human (make sure to use exact string)")
        else:
            raise KeyError

class ArchivalMemory(object):
    def __init__(self):
        self.archive = []

    def __len__(self):
        return len(self.archive)

    def __repr__(self) -> str:
        if len(self.archive) == 0:
            memory_str = "<empty>"
        else:
            memory_str = "\n".join([d["content"] for d in self.archive])
        return f"\n### ARCHIVAL MEMORY ###" + f"\n{memory_str}"

    def insert(self, memory_string):
        self.archive.append(
            {
                "timestamp": utils.get_local_time(),
                "content": memory_string,
            }
        )

    def search(self, query_string, count=None, start=None):
        # run an (inefficient) case-insensitive match search
        matches = [s for s in self.archive if query_string.lower() in s["content"].lower()]
        # start/count support paging through results
        if start is not None and count is not None:
            return matches[start: start + count], len(matches)
        elif start is None and count is not None:
            return matches[:count], len(matches)
        elif start is not None and count is None:
            return matches[start:], len(matches)
        else:
            return matches, len(matches)

class RecallMemory(object):
    def __init__(self):
        self.message_logs = []

    def __len__(self):
        return len(self.message_logs)

    def __repr__(self) -> str:
        system_count = user_count = assistant_count = function_count = other_count = 0
        for msg in self.message_logs:
            role = msg["message"]["role"]
            if role == "system":
                system_count += 1
            elif role == "user":
                user_count += 1
            elif role == "assistant":
                assistant_count += 1
            elif role == "function":
                function_count += 1
            else:
                other_count += 1
        memory_str = (
                f"Statistics:"
                + f"\n{len(self.message_logs)} total messages"
                + f"\n{system_count} system"
                + f"\n{user_count} user"
                + f"\n{assistant_count} assistant"
                + f"\n{function_count} function"
                + f"\n{other_count} other"
        )
        return f"\n### RECALL MEMORY ###" + f"\n{memory_str}"

    def text_search(self, query_string, count=None, start=None):
        # run an (inefficient) case-insensitive match search
        message_pool = [d for d in self.message_logs if d["message"]["role"] not in ["system", "function"]]
        matches = [
            d for d in message_pool if
            d["message"]["content"] is not None and query_string.lower() in d["message"]["content"].lower()
        ]
        # start/count support paging through results
        if start is not None and count is not None:
            return matches[start: start + count], len(matches)
        elif start is None and count is not None:
            return matches[:count], len(matches)
        elif start is not None and count is None:
            return matches[start:], len(matches)
        else:
            return matches, len(matches)


# from django.core.mail import send_mail
# from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from .models import Note

# def send_email_reminders():
#     now = datetime.now()
#     due_notes = Note.objects.filter(due_date__lte=now, reminder=True)
#     for note in due_notes:
#         send_mail(
#             'Reminder: ' + note.title,
#             note.body,
#             'mariamtestn@gmail.com',
#             [note.email],
#             fail_silently=False,
#         )

# def start_scheduler():
#     scheduler = BackgroundScheduler()
#     scheduler.add_job(send_email_reminders, 'interval', minutes=2)
#     scheduler.start()
# utils.py
from django.core.mail import send_mail

def send_email_reminders():
    notes = Note.objects.filter(due_date__lt=timezone.now(), reminder=True)

    for note in notes:
        subject = 'Reminder: ' + note.title
        message = 'Your note "{}" is due.'.format(note.title)
        from_email = 'mariamtestn@gmail.com'
        recipient_list = [note.email]

        send_mail(subject, message, from_email, recipient_list, fail_silently=False)


# Python test set -- part 6, built-in types

from test.support import run_unittest, run_with_locale
import collections
import pickle
import locale
import sys
import types
import unittest

class TypesTests(unittest.TestCase):

    def test_truth_values(self):
        if None: self.fail('None is true instead of false')
        if 0: self.fail('0 is true instead of false')
        if 0.0: self.fail('0.0 is true instead of false')
        if '': self.fail('\'\' is true instead of false')
        if not 1: self.fail('1 is false instead of true')
        if not 1.0: self.fail('1.0 is false instead of true')
        if not 'x': self.fail('\'x\' is false instead of true')
        if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
        def f(): pass
        class C: pass
        x = C()
        if not f: self.fail('f is false instead of true')
        if not C: self.fail('C is false instead of true')
        if not sys: self.fail('sys is false instead of true')
        if not x: self.fail('x is false instead of true')

    def test_boolean_ops(self):
        if 0 or 0: self.fail('0 or 0 is true instead of false')
        if 1 and 1: pass
        else: self.fail('1 and 1 is false instead of true')
        if not 1: self.fail('not 1 is true instead of false')

    def test_comparisons(self):
        if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
        else: self.fail('int comparisons failed')
        if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
        else: self.fail('float comparisons failed')
        if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
        else: self.fail('string comparisons failed')
        if None is None: pass
        else: self.fail('identity test failed')

    def test_float_constructor(self):
        self.assertRaises(ValueError, float, '')
        self.assertRaises(ValueError, float, '5\0')

    def test_zero_division(self):
        try: 5.0 / 0.0
        except ZeroDivisionError: pass
        else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")

        try: 5.0 // 0.0
        except ZeroDivisionError: pass
        else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")

        try: 5.0 % 0.0
        except ZeroDivisionError: pass
        else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")

        try: 5 / 0
        except ZeroDivisionError: pass
        else: self.fail("5 / 0 didn't raise ZeroDivisionError")

        try: 5 // 0
        except ZeroDivisionError: pass
        else: self.fail("5 // 0 didn't raise ZeroDivisionError")

        try: 5 % 0
        except ZeroDivisionError: pass
        else: self.fail("5 % 0 didn't raise ZeroDivisionError")

    def test_numeric_types(self):
        if 0 != 0.0 or 1 != 1.0 or -1 != -1.0:
            self.fail('int/float value not equal')
        # calling built-in types without argument must return 0
        if int() != 0: self.fail('int() does not return 0')
        if float() != 0.0: self.fail('float() does not return 0.0')
        if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
        else: self.fail('int() does not round properly')
        if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
        else: self.fail('float() does not work properly')

    def test_float_to_string(self):
        def test(f, result):
            self.assertEqual(f.__format__('e'), result)
            self.assertEqual('%e' % f, result)

        # test all 2 digit exponents, both with __format__ and with
        #  '%' formatting
        for i in range(-99, 100):
            test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))

        # test some 3 digit exponents
        self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
        self.assertEqual('%e' % 1.5e100, '1.500000e+100')

        self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
        self.assertEqual('%e' % 1.5e101, '1.500000e+101')

        self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
        self.assertEqual('%e' % 1.5e-100, '1.500000e-100')

        self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
        self.assertEqual('%e' % 1.5e-101, '1.500000e-101')

        self.assertEqual('%g' % 1.0, '1')
        self.assertEqual('%#g' % 1.0, '1.00000')

    def test_normal_integers(self):
        # Ensure the first 256 integers are shared
        a = 256
        b = 128*2
        if a is not b: self.fail('256 is not shared')
        if 12 + 24 != 36: self.fail('int op')
        if 12 + (-24) != -12: self.fail('int op')
        if (-12) + 24 != 12: self.fail('int op')
        if (-12) + (-24) != -36: self.fail('int op')
        if not 12 < 24: self.fail('int op')
        if not -24 < -12: self.fail('int op')
        # Test for a particular bug in integer multiply
        xsize, ysize, zsize = 238, 356, 4
        if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
            self.fail('int mul commutativity')
        # And another.
        m = -sys.maxsize - 1
        for divisor in 1, 2, 4, 8, 16, 32:
            j = m // divisor
            prod = divisor * j
            if prod != m:
                self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
            if type(prod) is not int:
                self.fail("expected type(prod) to be int, not %r" %
                                   type(prod))
        # Check for unified integral type
        for divisor in 1, 2, 4, 8, 16, 32:
            j = m // divisor - 1
            prod = divisor * j
            if type(prod) is not int:
                self.fail("expected type(%r) to be int, not %r" %
                                   (prod, type(prod)))
        # Check for unified integral type
        m = sys.maxsize
        for divisor in 1, 2, 4, 8, 16, 32:
            j = m // divisor + 1
            prod = divisor * j
            if type(prod) is not int:
                self.fail("expected type(%r) to be int, not %r" %
                                   (prod, type(prod)))

        x = sys.maxsize
        self.assertIsInstance(x + 1, int,
                              "(sys.maxsize + 1) should have returned int")
        self.assertIsInstance(-x - 1, int,
                              "(-sys.maxsize - 1) should have returned int")
        self.assertIsInstance(-x - 2, int,
                              "(-sys.maxsize - 2) should have returned int")

        try: 5 << -5
        except ValueError: pass
        else: self.fail('int negative shift <<')

        try: 5 >> -5
        except ValueError: pass
        else: self.fail('int negative shift >>')

    def test_floats(self):
        if 12.0 + 24.0 != 36.0: self.fail('float op')
        if 12.0 + (-24.0) != -12.0: self.fail('float op')
        if (-12.0) + 24.0 != 12.0: self.fail('float op')
        if (-12.0) + (-24.0) != -36.0: self.fail('float op')
        if not 12.0 < 24.0: self.fail('float op')
        if not -24.0 < -12.0: self.fail('float op')

    def test_strings(self):
        if len('') != 0: self.fail('len(\'\')')
        if len('a') != 1: self.fail('len(\'a\')')
        if len('abcdef') != 6: self.fail('len(\'abcdef\')')
        if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
        if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
        if 0*'abcde' != '': self.fail('string repetition 0*')
        if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
        if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
        else: self.fail('in/not in string')
        x = 'x'*103
        if '%s!'%x != x+'!': self.fail('nasty string formatting bug')

        #extended slices for strings
        a = '0123456789'
        self.assertEqual(a[::], a)
        self.assertEqual(a[::2], '02468')
        self.assertEqual(a[1::2], '13579')
        self.assertEqual(a[::-1],'9876543210')
        self.assertEqual(a[::-2], '97531')
        self.assertEqual(a[3::-2], '31')
        self.assertEqual(a[-100:100:], a)
        self.assertEqual(a[100:-100:-1], a[::-1])
        self.assertEqual(a[-100:100:2], '02468')

    def test_type_function(self):
        self.assertRaises(TypeError, type, 1, 2)
        self.assertRaises(TypeError, type, 1, 2, 3, 4)

    def test_int__format__(self):
        def test(i, format_spec, result):
            # just make sure we have the unified type for integers
            assert type(i) == int
            assert type(format_spec) == str
            self.assertEqual(i.__format__(format_spec), result)

        test(123456789, 'd', '123456789')
        test(123456789, 'd', '123456789')

        test(1, 'c', '\01')

        # sign and aligning are interdependent
        test(1, "-", '1')
        test(-1, "-", '-1')
        test(1, "-3", '  1')
        test(-1, "-3", ' -1')
        test(1, "+3", ' +1')
        test(-1, "+3", ' -1')
        test(1, " 3", '  1')
        test(-1, " 3", ' -1')
        test(1, " ", ' 1')
        test(-1, " ", '-1')

        # hex
        test(3, "x", "3")
        test(3, "X", "3")
        test(1234, "x", "4d2")
        test(-1234, "x", "-4d2")
        test(1234, "8x", "     4d2")
        test(-1234, "8x", "    -4d2")
        test(1234, "x", "4d2")
        test(-1234, "x", "-4d2")
        test(-3, "x", "-3")
        test(-3, "X", "-3")
        test(int('be', 16), "x", "be")
        test(int('be', 16), "X", "BE")
        test(-int('be', 16), "x", "-be")
        test(-int('be', 16), "X", "-BE")

        # octal
        test(3, "o", "3")
        test(-3, "o", "-3")
        test(65, "o", "101")
        test(-65, "o", "-101")
        test(1234, "o", "2322")
        test(-1234, "o", "-2322")
        test(1234, "-o", "2322")
        test(-1234, "-o", "-2322")
        test(1234, " o", " 2322")
        test(-1234, " o", "-2322")
        test(1234, "+o", "+2322")
        test(-1234, "+o", "-2322")

        # binary
        test(3, "b", "11")
        test(-3, "b", "-11")
        test(1234, "b", "10011010010")
        test(-1234, "b", "-10011010010")
        test(1234, "-b", "10011010010")
        test(-1234, "-b", "-10011010010")
        test(1234, " b", " 10011010010")
        test(-1234, " b", "-10011010010")
        test(1234, "+b", "+10011010010")
        test(-1234, "+b", "-10011010010")

        # alternate (#) formatting
        test(0, "#b", '0b0')
        test(0, "-#b", '0b0')
        test(1, "-#b", '0b1')
        test(-1, "-#b", '-0b1')
        test(-1, "-#5b", ' -0b1')
        test(1, "+#5b", ' +0b1')
        test(100, "+#b", '+0b1100100')
        test(100, "#012b", '0b0001100100')
        test(-100, "#012b", '-0b001100100')

        test(0, "#o", '0o0')
        test(0, "-#o", '0o0')
        test(1, "-#o", '0o1')
        test(-1, "-#o", '-0o1')
        test(-1, "-#5o", ' -0o1')
        test(1, "+#5o", ' +0o1')
        test(100, "+#o", '+0o144')
        test(100, "#012o", '0o0000000144')
        test(-100, "#012o", '-0o000000144')

        test(0, "#x", '0x0')
        test(0, "-#x", '0x0')
        test(1, "-#x", '0x1')
        test(-1, "-#x", '-0x1')
        test(-1, "-#5x", ' -0x1')
        test(1, "+#5x", ' +0x1')
        test(100, "+#x", '+0x64')
        test(100, "#012x", '0x0000000064')
        test(-100, "#012x", '-0x000000064')
        test(123456, "#012x", '0x000001e240')
        test(-123456, "#012x", '-0x00001e240')

        test(0, "#X", '0X0')
        test(0, "-#X", '0X0')
        test(1, "-#X", '0X1')
        test(-1, "-#X", '-0X1')
        test(-1, "-#5X", ' -0X1')
        test(1, "+#5X", ' +0X1')
        test(100, "+#X", '+0X64')
        test(100, "#012X", '0X0000000064')
        test(-100, "#012X", '-0X000000064')
        test(123456, "#012X", '0X000001E240')
        test(-123456, "#012X", '-0X00001E240')

        test(123, ',', '123')
        test(-123, ',', '-123')
        test(1234, ',', '1,234')
        test(-1234, ',', '-1,234')
        test(123456, ',', '123,456')
        test(-123456, ',', '-123,456')
        test(1234567, ',', '1,234,567')
        test(-1234567, ',', '-1,234,567')

        # issue 5782, commas with no specifier type
        test(1234, '010,', '00,001,234')

        # Unified type for integers
        test(10**100, 'd', '1' + '0' * 100)
        test(10**100+100, 'd', '1' + '0' * 97 + '100')

        # make sure these are errors

        # precision disallowed
        self.assertRaises(ValueError, 3 .__format__, "1.3")
        # sign not allowed with 'c'
        self.assertRaises(ValueError, 3 .__format__, "+c")
        # format spec must be string
        self.assertRaises(TypeError, 3 .__format__, None)
        self.assertRaises(TypeError, 3 .__format__, 0)
        # can't have ',' with 'n'
        self.assertRaises(ValueError, 3 .__format__, ",n")
        # can't have ',' with 'c'
        self.assertRaises(ValueError, 3 .__format__, ",c")

        # ensure that only int and float type specifiers work
        for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
                            [chr(x) for x in range(ord('A'), ord('Z')+1)]):
            if not format_spec in 'bcdoxXeEfFgGn%':
                self.assertRaises(ValueError, 0 .__format__, format_spec)
                self.assertRaises(ValueError, 1 .__format__, format_spec)
                self.assertRaises(ValueError, (-1) .__format__, format_spec)

        # ensure that float type specifiers work; format converts
        #  the int to a float
        for format_spec in 'eEfFgG%':
            for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
                self.assertEqual(value.__format__(format_spec),
                                 float(value).__format__(format_spec))

        # Issue 6902
        test(123456, "0<20", '12345600000000000000')
        test(123456, "1<20", '12345611111111111111')
        test(123456, "*<20", '123456**************')
        test(123456, "0>20", '00000000000000123456')
        test(123456, "1>20", '11111111111111123456')
        test(123456, "*>20", '**************123456')
        test(123456, "0=20", '00000000000000123456')
        test(123456, "1=20", '11111111111111123456')
        test(123456, "*=20", '**************123456')

    @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
    def test_float__format__locale(self):
        # test locale support for __format__ code 'n'

        for i in range(-10, 10):
            x = 1234567890.0 * (10.0 ** i)
            self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
            self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))

    @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
    def test_int__format__locale(self):
        # test locale support for __format__ code 'n' for integers

        x = 123456789012345678901234567890
        for i in range(0, 30):
            self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))

            # move to the next integer to test
            x = x // 10

        rfmt = ">20n"
        lfmt = "<20n"
        cfmt = "^20n"
        for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
            self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
            self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
            self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))

    def test_float__format__(self):
        def test(f, format_spec, result):
            self.assertEqual(f.__format__(format_spec), result)
            self.assertEqual(format(f, format_spec), result)

        test(0.0, 'f', '0.000000')

        # the default is 'g', except for empty format spec
        test(0.0, '', '0.0')
        test(0.01, '', '0.01')
        test(0.01, 'g', '0.01')

        # test for issue 3411
        test(1.23, '1', '1.23')
        test(-1.23, '1', '-1.23')
        test(1.23, '1g', '1.23')
        test(-1.23, '1g', '-1.23')

        test( 1.0, ' g', ' 1')
        test(-1.0, ' g', '-1')
        test( 1.0, '+g', '+1')
        test(-1.0, '+g', '-1')
        test(1.1234e200, 'g', '1.1234e+200')
        test(1.1234e200, 'G', '1.1234E+200')

        test(1.0, 'f', '1.000000')

        test(-1.0, 'f', '-1.000000')

        test( 1.0, ' f', ' 1.000000')
        test(-1.0, ' f', '-1.000000')
        test( 1.0, '+f', '+1.000000')
        test(-1.0, '+f', '-1.000000')

        # Python versions <= 3.0 switched from 'f' to 'g' formatting for
        # values larger than 1e50.  No longer.
        f = 1.1234e90
        for fmt in 'f', 'F':
            # don't do a direct equality check, since on some
            # platforms only the first few digits of dtoa
            # will be reliable
            result = f.__format__(fmt)
            self.assertEqual(len(result), 98)
            self.assertEqual(result[-7], '.')
            self.assertIn(result[:12], ('112340000000', '112339999999'))
        f = 1.1234e200
        for fmt in 'f', 'F':
            result = f.__format__(fmt)
            self.assertEqual(len(result), 208)
            self.assertEqual(result[-7], '.')
            self.assertIn(result[:12], ('112340000000', '112339999999'))

        test( 1.0, 'e', '1.000000e+00')
        test(-1.0, 'e', '-1.000000e+00')
        test( 1.0, 'E', '1.000000E+00')
        test(-1.0, 'E', '-1.000000E+00')
        test(1.1234e20, 'e', '1.123400e+20')
        test(1.1234e20, 'E', '1.123400E+20')

        # No format code means use g, but must have a decimal
        # and a number after the decimal.  This is tricky, because
        # a totaly empty format specifier means something else.
        # So, just use a sign flag
        test(1e200, '+g', '+1e+200')
        test(1e200, '+', '+1e+200')

        test(1.1e200, '+g', '+1.1e+200')
        test(1.1e200, '+', '+1.1e+200')

        # 0 padding
        test(1234., '010f', '1234.000000')
        test(1234., '011f', '1234.000000')
        test(1234., '012f', '01234.000000')
        test(-1234., '011f', '-1234.000000')
        test(-1234., '012f', '-1234.000000')
        test(-1234., '013f', '-01234.000000')
        test(-1234.12341234, '013f', '-01234.123412')
        test(-123456.12341234, '011.2f', '-0123456.12')

        # issue 5782, commas with no specifier type
        test(1.2, '010,.2', '0,000,001.2')

        # 0 padding with commas
        test(1234., '011,f', '1,234.000000')
        test(1234., '012,f', '1,234.000000')
        test(1234., '013,f', '01,234.000000')
        test(-1234., '012,f', '-1,234.000000')
        test(-1234., '013,f', '-1,234.000000')
        test(-1234., '014,f', '-01,234.000000')
        test(-12345., '015,f', '-012,345.000000')
        test(-123456., '016,f', '-0,123,456.000000')
        test(-123456., '017,f', '-0,123,456.000000')
        test(-123456.12341234, '017,f', '-0,123,456.123412')
        test(-123456.12341234, '013,.2f', '-0,123,456.12')

        # % formatting
        test(-1.0, '%', '-100.000000%')

        # format spec must be string
        self.assertRaises(TypeError, 3.0.__format__, None)
        self.assertRaises(TypeError, 3.0.__format__, 0)

        # other format specifiers shouldn't work on floats,
        #  in particular int specifiers
        for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
                            [chr(x) for x in range(ord('A'), ord('Z')+1)]):
            if not format_spec in 'eEfFgGn%':
                self.assertRaises(ValueError, format, 0.0, format_spec)
                self.assertRaises(ValueError, format, 1.0, format_spec)
                self.assertRaises(ValueError, format, -1.0, format_spec)
                self.assertRaises(ValueError, format, 1e100, format_spec)
                self.assertRaises(ValueError, format, -1e100, format_spec)
                self.assertRaises(ValueError, format, 1e-100, format_spec)
                self.assertRaises(ValueError, format, -1e-100, format_spec)

        # Alternate float formatting
        test(1.0, '.0e', '1e+00')
        test(1.0, '#.0e', '1.e+00')
        test(1.0, '.0f', '1')
        test(1.0, '#.0f', '1.')
        test(1.1, 'g', '1.1')
        test(1.1, '#g', '1.10000')
        test(1.0, '.0%', '100%')
        test(1.0, '#.0%', '100.%')

        # Issue 7094: Alternate formatting (specified by #)
        test(1.0, '0e',  '1.000000e+00')
        test(1.0, '#0e', '1.000000e+00')
        test(1.0, '0f',  '1.000000' )
        test(1.0, '#0f', '1.000000')
        test(1.0, '.1e',  '1.0e+00')
        test(1.0, '#.1e', '1.0e+00')
        test(1.0, '.1f',  '1.0')
        test(1.0, '#.1f', '1.0')
        test(1.0, '.1%',  '100.0%')
        test(1.0, '#.1%', '100.0%')

        # Issue 6902
        test(12345.6, "0<20", '12345.60000000000000')
        test(12345.6, "1<20", '12345.61111111111111')
        test(12345.6, "*<20", '12345.6*************')
        test(12345.6, "0>20", '000000000000012345.6')
        test(12345.6, "1>20", '111111111111112345.6')
        test(12345.6, "*>20", '*************12345.6')
        test(12345.6, "0=20", '000000000000012345.6')
        test(12345.6, "1=20", '111111111111112345.6')
        test(12345.6, "*=20", '*************12345.6')

    def test_format_spec_errors(self):
        # int, float, and string all share the same format spec
        # mini-language parser.

        # Check that we can't ask for too many digits. This is
        # probably a CPython specific test. It tries to put the width
        # into a C long.
        self.assertRaises(ValueError, format, 0, '1'*10000 + 'd')

        # Similar with the precision.
        self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd')

        # And may as well test both.
        self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd')

        # Make sure commas aren't allowed with various type codes
        for code in 'xXobns':
            self.assertRaises(ValueError, format, 0, ',' + code)

    def test_internal_sizes(self):
        self.assertGreater(object.__basicsize__, 0)
        self.assertGreater(tuple.__itemsize__, 0)

class MappingProxyTests(unittest.TestCase):
    mappingproxy = types.MappingProxyType

    def test_constructor(self):
        class userdict(dict):
            pass

        mapping = {'x': 1, 'y': 2}
        self.assertEqual(self.mappingproxy(mapping), mapping)
        mapping = userdict(x=1, y=2)
        self.assertEqual(self.mappingproxy(mapping), mapping)
        mapping = collections.ChainMap({'x': 1}, {'y': 2})
        self.assertEqual(self.mappingproxy(mapping), mapping)

        self.assertRaises(TypeError, self.mappingproxy, 10)
        self.assertRaises(TypeError, self.mappingproxy, ("a", "tuple"))
        self.assertRaises(TypeError, self.mappingproxy, ["a", "list"])

    def test_methods(self):
        attrs = set(dir(self.mappingproxy({}))) - set(dir(object()))
        self.assertEqual(attrs, {
             '__contains__',
             '__getitem__',
             '__iter__',
             '__len__',
             'copy',
             'get',
             'items',
             'keys',
             'values',
        })

    def test_get(self):
        view = self.mappingproxy({'a': 'A', 'b': 'B'})
        self.assertEqual(view['a'], 'A')
        self.assertEqual(view['b'], 'B')
        self.assertRaises(KeyError, view.__getitem__, 'xxx')
        self.assertEqual(view.get('a'), 'A')
        self.assertIsNone(view.get('xxx'))
        self.assertEqual(view.get('xxx', 42), 42)

    def test_missing(self):
        class dictmissing(dict):
            def __missing__(self, key):
                return "missing=%s" % key

        view = self.mappingproxy(dictmissing(x=1))
        self.assertEqual(view['x'], 1)
        self.assertEqual(view['y'], 'missing=y')
        self.assertEqual(view.get('x'), 1)
        self.assertEqual(view.get('y'), None)
        self.assertEqual(view.get('y', 42), 42)
        self.assertTrue('x' in view)
        self.assertFalse('y' in view)

    def test_customdict(self):
        class customdict(dict):
            def __contains__(self, key):
                if key == 'magic':
                    return True
                else:
                    return dict.__contains__(self, key)

            def __iter__(self):
                return iter(('iter',))

            def __len__(self):
                return 500

            def copy(self):
                return 'copy'

            def keys(self):
                return 'keys'

            def items(self):
                return 'items'

            def values(self):
                return 'values'

            def __getitem__(self, key):
                return "getitem=%s" % dict.__getitem__(self, key)

            def get(self, key, default=None):
                return "get=%s" % dict.get(self, key, 'default=%r' % default)

        custom = customdict({'key': 'value'})
        view = self.mappingproxy(custom)
        self.assertTrue('key' in view)
        self.assertTrue('magic' in view)
        self.assertFalse('xxx' in view)
        self.assertEqual(view['key'], 'getitem=value')
        self.assertRaises(KeyError, view.__getitem__, 'xxx')
        self.assertEqual(tuple(view), ('iter',))
        self.assertEqual(len(view), 500)
        self.assertEqual(view.copy(), 'copy')
        self.assertEqual(view.get('key'), 'get=value')
        self.assertEqual(view.get('xxx'), 'get=default=None')
        self.assertEqual(view.items(), 'items')
        self.assertEqual(view.keys(), 'keys')
        self.assertEqual(view.values(), 'values')

    def test_chainmap(self):
        d1 = {'x': 1}
        d2 = {'y': 2}
        mapping = collections.ChainMap(d1, d2)
        view = self.mappingproxy(mapping)
        self.assertTrue('x' in view)
        self.assertTrue('y' in view)
        self.assertFalse('z' in view)
        self.assertEqual(view['x'], 1)
        self.assertEqual(view['y'], 2)
        self.assertRaises(KeyError, view.__getitem__, 'z')
        self.assertEqual(tuple(sorted(view)), ('x', 'y'))
        self.assertEqual(len(view), 2)
        copy = view.copy()
        self.assertIsNot(copy, mapping)
        self.assertIsInstance(copy, collections.ChainMap)
        self.assertEqual(copy, mapping)
        self.assertEqual(view.get('x'), 1)
        self.assertEqual(view.get('y'), 2)
        self.assertIsNone(view.get('z'))
        self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2)))
        self.assertEqual(tuple(sorted(view.keys())), ('x', 'y'))
        self.assertEqual(tuple(sorted(view.values())), (1, 2))

    def test_contains(self):
        view = self.mappingproxy(dict.fromkeys('abc'))
        self.assertTrue('a' in view)
        self.assertTrue('b' in view)
        self.assertTrue('c' in view)
        self.assertFalse('xxx' in view)

    def test_views(self):
        mapping = {}
        view = self.mappingproxy(mapping)
        keys = view.keys()
        values = view.values()
        items = view.items()
        self.assertEqual(list(keys), [])
        self.assertEqual(list(values), [])
        self.assertEqual(list(items), [])
        mapping['key'] = 'value'
        self.assertEqual(list(keys), ['key'])
        self.assertEqual(list(values), ['value'])
        self.assertEqual(list(items), [('key', 'value')])

    def test_len(self):
        for expected in range(6):
            data = dict.fromkeys('abcde'[:expected])
            self.assertEqual(len(data), expected)
            view = self.mappingproxy(data)
            self.assertEqual(len(view), expected)

    def test_iterators(self):
        keys = ('x', 'y')
        values = (1, 2)
        items = tuple(zip(keys, values))
        view = self.mappingproxy(dict(items))
        self.assertEqual(set(view), set(keys))
        self.assertEqual(set(view.keys()), set(keys))
        self.assertEqual(set(view.values()), set(values))
        self.assertEqual(set(view.items()), set(items))

    def test_copy(self):
        original = {'key1': 27, 'key2': 51, 'key3': 93}
        view = self.mappingproxy(original)
        copy = view.copy()
        self.assertEqual(type(copy), dict)
        self.assertEqual(copy, original)
        original['key1'] = 70
        self.assertEqual(view['key1'], 70)
        self.assertEqual(copy['key1'], 27)

class ClassCreationTests(unittest.TestCase):

    class Meta(type):
        def __init__(cls, name, bases, ns, **kw):
            super().__init__(name, bases, ns)
        @staticmethod
        def __new__(mcls, name, bases, ns, **kw):
            return super().__new__(mcls, name, bases, ns)
        @classmethod
        def __prepare__(mcls, name, bases, **kw):
            ns = super().__prepare__(name, bases)
            ns["y"] = 1
            ns.update(kw)
            return ns

    def test_new_class_basics(self):
        C = types.new_class("C")
        self.assertEqual(C.__name__, "C")
        self.assertEqual(C.__bases__, (object,))

    def test_new_class_subclass(self):
        C = types.new_class("C", (int,))
        self.assertTrue(issubclass(C, int))

    def test_new_class_meta(self):
        Meta = self.Meta
        settings = {"metaclass": Meta, "z": 2}
        # We do this twice to make sure the passed in dict isn't mutated
        for i in range(2):
            C = types.new_class("C" + str(i), (), settings)
            self.assertIsInstance(C, Meta)
            self.assertEqual(C.y, 1)
            self.assertEqual(C.z, 2)

    def test_new_class_exec_body(self):
        Meta = self.Meta
        def func(ns):
            ns["x"] = 0
        C = types.new_class("C", (), {"metaclass": Meta, "z": 2}, func)
        self.assertIsInstance(C, Meta)
        self.assertEqual(C.x, 0)
        self.assertEqual(C.y, 1)
        self.assertEqual(C.z, 2)

    def test_new_class_metaclass_keywords(self):
        #Test that keywords are passed to the metaclass:
        def meta_func(name, bases, ns, **kw):
            return name, bases, ns, kw
        res = types.new_class("X",
                              (int, object),
                              dict(metaclass=meta_func, x=0))
        self.assertEqual(res, ("X", (int, object), {}, {"x": 0}))

    def test_new_class_defaults(self):
        # Test defaults/keywords:
        C = types.new_class("C", (), {}, None)
        self.assertEqual(C.__name__, "C")
        self.assertEqual(C.__bases__, (object,))

    def test_new_class_meta_with_base(self):
        Meta = self.Meta
        def func(ns):
            ns["x"] = 0
        C = types.new_class(name="C",
                            bases=(int,),
                            kwds=dict(metaclass=Meta, z=2),
                            exec_body=func)
        self.assertTrue(issubclass(C, int))
        self.assertIsInstance(C, Meta)
        self.assertEqual(C.x, 0)
        self.assertEqual(C.y, 1)
        self.assertEqual(C.z, 2)

    # Many of the following tests are derived from test_descr.py
    def test_prepare_class(self):
        # Basic test of metaclass derivation
        expected_ns = {}
        class A(type):
            def __new__(*args, **kwargs):
                return type.__new__(*args, **kwargs)

            def __prepare__(*args):
                return expected_ns

        B = types.new_class("B", (object,))
        C = types.new_class("C", (object,), {"metaclass": A})

        # The most derived metaclass of D is A rather than type.
        meta, ns, kwds = types.prepare_class("D", (B, C), {"metaclass": type})
        self.assertIs(meta, A)
        self.assertIs(ns, expected_ns)
        self.assertEqual(len(kwds), 0)

    def test_metaclass_derivation(self):
        # issue1294232: correct metaclass calculation
        new_calls = []  # to check the order of __new__ calls
        class AMeta(type):
            def __new__(mcls, name, bases, ns):
                new_calls.append('AMeta')
                return super().__new__(mcls, name, bases, ns)
            @classmethod
            def __prepare__(mcls, name, bases):
                return {}

        class BMeta(AMeta):
            def __new__(mcls, name, bases, ns):
                new_calls.append('BMeta')
                return super().__new__(mcls, name, bases, ns)
            @classmethod
            def __prepare__(mcls, name, bases):
                ns = super().__prepare__(name, bases)
                ns['BMeta_was_here'] = True
                return ns

        A = types.new_class("A", (), {"metaclass": AMeta})
        self.assertEqual(new_calls, ['AMeta'])
        new_calls.clear()

        B = types.new_class("B", (), {"metaclass": BMeta})
        # BMeta.__new__ calls AMeta.__new__ with super:
        self.assertEqual(new_calls, ['BMeta', 'AMeta'])
        new_calls.clear()

        C = types.new_class("C", (A, B))
        # The most derived metaclass is BMeta:
        self.assertEqual(new_calls, ['BMeta', 'AMeta'])
        new_calls.clear()
        # BMeta.__prepare__ should've been called:
        self.assertIn('BMeta_was_here', C.__dict__)

        # The order of the bases shouldn't matter:
        C2 = types.new_class("C2", (B, A))
        self.assertEqual(new_calls, ['BMeta', 'AMeta'])
        new_calls.clear()
        self.assertIn('BMeta_was_here', C2.__dict__)

        # Check correct metaclass calculation when a metaclass is declared:
        D = types.new_class("D", (C,), {"metaclass": type})
        self.assertEqual(new_calls, ['BMeta', 'AMeta'])
        new_calls.clear()
        self.assertIn('BMeta_was_here', D.__dict__)

        E = types.new_class("E", (C,), {"metaclass": AMeta})
        self.assertEqual(new_calls, ['BMeta', 'AMeta'])
        new_calls.clear()
        self.assertIn('BMeta_was_here', E.__dict__)

    def test_metaclass_override_function(self):
        # Special case: the given metaclass isn't a class,
        # so there is no metaclass calculation.
        class A(metaclass=self.Meta):
            pass

        marker = object()
        def func(*args, **kwargs):
            return marker

        X = types.new_class("X", (), {"metaclass": func})
        Y = types.new_class("Y", (object,), {"metaclass": func})
        Z = types.new_class("Z", (A,), {"metaclass": func})
        self.assertIs(marker, X)
        self.assertIs(marker, Y)
        self.assertIs(marker, Z)

    def test_metaclass_override_callable(self):
        # The given metaclass is a class,
        # but not a descendant of type.
        new_calls = []  # to check the order of __new__ calls
        prepare_calls = []  # to track __prepare__ calls
        class ANotMeta:
            def __new__(mcls, *args, **kwargs):
                new_calls.append('ANotMeta')
                return super().__new__(mcls)
            @classmethod
            def __prepare__(mcls, name, bases):
                prepare_calls.append('ANotMeta')
                return {}

        class BNotMeta(ANotMeta):
            def __new__(mcls, *args, **kwargs):
                new_calls.append('BNotMeta')
                return super().__new__(mcls)
            @classmethod
            def __prepare__(mcls, name, bases):
                prepare_calls.append('BNotMeta')
                return super().__prepare__(name, bases)

        A = types.new_class("A", (), {"metaclass": ANotMeta})
        self.assertIs(ANotMeta, type(A))
        self.assertEqual(prepare_calls, ['ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['ANotMeta'])
        new_calls.clear()

        B = types.new_class("B", (), {"metaclass": BNotMeta})
        self.assertIs(BNotMeta, type(B))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        C = types.new_class("C", (A, B))
        self.assertIs(BNotMeta, type(C))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        C2 = types.new_class("C2", (B, A))
        self.assertIs(BNotMeta, type(C2))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        # This is a TypeError, because of a metaclass conflict:
        # BNotMeta is neither a subclass, nor a superclass of type
        with self.assertRaises(TypeError):
            D = types.new_class("D", (C,), {"metaclass": type})

        E = types.new_class("E", (C,), {"metaclass": ANotMeta})
        self.assertIs(BNotMeta, type(E))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        F = types.new_class("F", (object(), C))
        self.assertIs(BNotMeta, type(F))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        F2 = types.new_class("F2", (C, object()))
        self.assertIs(BNotMeta, type(F2))
        self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta'])
        prepare_calls.clear()
        self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta'])
        new_calls.clear()

        # TypeError: BNotMeta is neither a
        # subclass, nor a superclass of int
        with self.assertRaises(TypeError):
            X = types.new_class("X", (C, int()))
        with self.assertRaises(TypeError):
            X = types.new_class("X", (int(), C))

class SimpleNamespaceTests(unittest.TestCase):

    def test_constructor(self):
        ns1 = types.SimpleNamespace()
        ns2 = types.SimpleNamespace(x=1, y=2)
        ns3 = types.SimpleNamespace(**dict(x=1, y=2))

        with self.assertRaises(TypeError):
            types.SimpleNamespace(1, 2, 3)

        self.assertEqual(len(ns1.__dict__), 0)
        self.assertEqual(vars(ns1), {})
        self.assertEqual(len(ns2.__dict__), 2)
        self.assertEqual(vars(ns2), {'y': 2, 'x': 1})
        self.assertEqual(len(ns3.__dict__), 2)
        self.assertEqual(vars(ns3), {'y': 2, 'x': 1})

    def test_unbound(self):
        ns1 = vars(types.SimpleNamespace())
        ns2 = vars(types.SimpleNamespace(x=1, y=2))

        self.assertEqual(ns1, {})
        self.assertEqual(ns2, {'y': 2, 'x': 1})

    def test_underlying_dict(self):
        ns1 = types.SimpleNamespace()
        ns2 = types.SimpleNamespace(x=1, y=2)
        ns3 = types.SimpleNamespace(a=True, b=False)
        mapping = ns3.__dict__
        del ns3

        self.assertEqual(ns1.__dict__, {})
        self.assertEqual(ns2.__dict__, {'y': 2, 'x': 1})
        self.assertEqual(mapping, dict(a=True, b=False))

    def test_attrget(self):
        ns = types.SimpleNamespace(x=1, y=2, w=3)

        self.assertEqual(ns.x, 1)
        self.assertEqual(ns.y, 2)
        self.assertEqual(ns.w, 3)
        with self.assertRaises(AttributeError):
            ns.z

    def test_attrset(self):
        ns1 = types.SimpleNamespace()
        ns2 = types.SimpleNamespace(x=1, y=2, w=3)
        ns1.a = 'spam'
        ns1.b = 'ham'
        ns2.z = 4
        ns2.theta = None

        self.assertEqual(ns1.__dict__, dict(a='spam', b='ham'))
        self.assertEqual(ns2.__dict__, dict(x=1, y=2, w=3, z=4, theta=None))

    def test_attrdel(self):
        ns1 = types.SimpleNamespace()
        ns2 = types.SimpleNamespace(x=1, y=2, w=3)

        with self.assertRaises(AttributeError):
            del ns1.spam
        with self.assertRaises(AttributeError):
            del ns2.spam

        del ns2.y
        self.assertEqual(vars(ns2), dict(w=3, x=1))
        ns2.y = 'spam'
        self.assertEqual(vars(ns2), dict(w=3, x=1, y='spam'))
        del ns2.y
        self.assertEqual(vars(ns2), dict(w=3, x=1))

        ns1.spam = 5
        self.assertEqual(vars(ns1), dict(spam=5))
        del ns1.spam
        self.assertEqual(vars(ns1), {})

    def test_repr(self):
        ns1 = types.SimpleNamespace(x=1, y=2, w=3)
        ns2 = types.SimpleNamespace()
        ns2.x = "spam"
        ns2._y = 5
        name = "namespace"

        self.assertEqual(repr(ns1), "{name}(w=3, x=1, y=2)".format(name=name))
        self.assertEqual(repr(ns2), "{name}(_y=5, x='spam')".format(name=name))

    def test_equal(self):
        ns1 = types.SimpleNamespace(x=1)
        ns2 = types.SimpleNamespace()
        ns2.x = 1

        self.assertEqual(types.SimpleNamespace(), types.SimpleNamespace())
        self.assertEqual(ns1, ns2)
        self.assertNotEqual(ns2, types.SimpleNamespace())

    def test_nested(self):
        ns1 = types.SimpleNamespace(a=1, b=2)
        ns2 = types.SimpleNamespace()
        ns3 = types.SimpleNamespace(x=ns1)
        ns2.spam = ns1
        ns2.ham = '?'
        ns2.spam = ns3

        self.assertEqual(vars(ns1), dict(a=1, b=2))
        self.assertEqual(vars(ns2), dict(spam=ns3, ham='?'))
        self.assertEqual(ns2.spam, ns3)
        self.assertEqual(vars(ns3), dict(x=ns1))
        self.assertEqual(ns3.x.a, 1)

    def test_recursive(self):
        ns1 = types.SimpleNamespace(c='cookie')
        ns2 = types.SimpleNamespace()
        ns3 = types.SimpleNamespace(x=1)
        ns1.spam = ns1
        ns2.spam = ns3
        ns3.spam = ns2

        self.assertEqual(ns1.spam, ns1)
        self.assertEqual(ns1.spam.spam, ns1)
        self.assertEqual(ns1.spam.spam, ns1.spam)
        self.assertEqual(ns2.spam, ns3)
        self.assertEqual(ns3.spam, ns2)
        self.assertEqual(ns2.spam.spam, ns2)

    def test_recursive_repr(self):
        ns1 = types.SimpleNamespace(c='cookie')
        ns2 = types.SimpleNamespace()
        ns3 = types.SimpleNamespace(x=1)
        ns1.spam = ns1
        ns2.spam = ns3
        ns3.spam = ns2
        name = "namespace"
        repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name)
        repr2 = "{name}(spam={name}(spam={name}(...), x=1))".format(name=name)

        self.assertEqual(repr(ns1), repr1)
        self.assertEqual(repr(ns2), repr2)

    def test_as_dict(self):
        ns = types.SimpleNamespace(spam='spamspamspam')

        with self.assertRaises(TypeError):
            len(ns)
        with self.assertRaises(TypeError):
            iter(ns)
        with self.assertRaises(TypeError):
            'spam' in ns
        with self.assertRaises(TypeError):
            ns['spam']

    def test_subclass(self):
        class Spam(types.SimpleNamespace):
            pass

        spam = Spam(ham=8, eggs=9)

        self.assertIs(type(spam), Spam)
        self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})

    def test_pickle(self):
        ns = types.SimpleNamespace(breakfast="spam", lunch="spam")

        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
            pname = "protocol {}".format(protocol)
            try:
                ns_pickled = pickle.dumps(ns, protocol)
            except TypeError as e:
                raise TypeError(pname) from e
            ns_roundtrip = pickle.loads(ns_pickled)

            self.assertEqual(ns, ns_roundtrip, pname)

    def test_fake_namespace_compare(self):
        # Issue #24257: Incorrect use of PyObject_IsInstance() caused
        # SystemError.
        class FakeSimpleNamespace(str):
            __class__ = types.SimpleNamespace
        self.assertFalse(types.SimpleNamespace() == FakeSimpleNamespace())
        self.assertTrue(types.SimpleNamespace() != FakeSimpleNamespace())
        with self.assertRaises(TypeError):
            types.SimpleNamespace() < FakeSimpleNamespace()
        with self.assertRaises(TypeError):
            types.SimpleNamespace() <= FakeSimpleNamespace()
        with self.assertRaises(TypeError):
            types.SimpleNamespace() > FakeSimpleNamespace()
        with self.assertRaises(TypeError):
            types.SimpleNamespace() >= FakeSimpleNamespace()

def test_main():
    run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
                 SimpleNamespaceTests)

if __name__ == '__main__':
    test_main()


import numpy as np
import cv2

# Resize an image to a certain width
def resize(img, width):
    r = float(width) / img.shape[1]
    dim = (width, int(img.shape[0] * r))
    img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    return img

# Combine an image that has a transparency alpha channel
def blend_transparent(face_img, sunglasses_img):
    overlay_img = sunglasses_img[:, :, :3]
    overlay_mask = sunglasses_img[:, :, 3:]

    background_mask = 255 - overlay_mask

    overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
    background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)

    face_part = (face_img * (1 / 255.0)) * (background_mask * (1 / 255.0))
    overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))

    return np.uint8(cv2.addWeighted(face_part, 255.0, overlay_part, 255.0, 0.0))

# Find the angle between two points
def angle_between(point_1, point_2):
    angle_1 = np.arctan2(*point_1[::-1])
    angle_2 = np.arctan2(*point_2[::-1])
    return np.rad2deg((angle_1 - angle_2) % (2 * np.pi))

    # Start main program
    # while True:

    # ret, img = video_capture.read()


import pygame

class Lazer:
    """
    Classe qui gère les tirs du vaisseau
    pos: position du lazer
    image: image du lazer
    show_able: si le lazer est à montré ou pas
    speed: vitesse du lazer
    damage: dégat du lazer
    """
    def __init__(self,  posX, posY, image, dmg):
        self.pos = pygame.Rect(posX, posY, 40, 10)
        self.image = image
        self.show_able = False
        self.speed = 30
        self.damage = dmg

    def initialize(self):
        """
            initialize les lazers au début du jeu
        """
        self.show_able = False
        self.pos = pygame.Rect(0, 0, 40, 10)
        self.speed = 30

    def show(self, window):
        """
        affichage du lazer
        """
        if self.show_able:
            window.blit(self.image, self.pos)

    def update(self, pos_spaceship):
        """
        mise à jour de la position
        """
        if self.show_able:
            self.pos.y -= self.speed

        if self.pos.y <= 0:
            self.show_able = False
            self.pos.x = pos_spaceship.x+(self.pos.w / 2)
            self.pos.y = pos_spaceship.y

    def collision(self, meteor):
        """
        détéction des collision avec un météor
        """
        if meteor.show_able and self.show_able:
            return self.pos.colliderect(meteor.pos)
        return False


#!/usr/bin/env python
'''Numba implementation of resampler'''

import numba

def _resample_f(x, y, t_out, interp_win, interp_delta, num_table, scale=1.0):

    index_step = int(scale * num_table)
    time_register = 0.0

    n = 0
    frac = 0.0
    index_frac = 0.0
    offset = 0
    eta = 0.0
    weight = 0.0

    nwin = interp_win.shape[0]
    n_orig = x.shape[0]
    n_out = t_out.shape[0]

    for t in numba.prange(n_out):
        time_register = t_out[t]

        # Grab the top bits as an index to the input buffer
        n = int(time_register)

        # Grab the fractional component of the time index
        frac = scale * (time_register - n)

        # Offset into the filter
        index_frac = frac * num_table
        offset = int(index_frac)

        # Interpolation factor
        eta = index_frac - offset

        # Compute the left wing of the filter response
        i_max = min(n + 1, (nwin - offset) // index_step)
        for i in range(i_max):

            weight = (interp_win[offset + i * index_step] + eta * interp_delta[offset + i * index_step])
            y[t] += weight * x[n-i]

        # Invert P
        frac = scale - frac

        # Offset into the filter
        index_frac = frac * num_table
        offset = int(index_frac)

        # Interpolation factor
        eta = index_frac - offset

        # Compute the right wing of the filter response
        k_max = min(n_orig - n - 1, (nwin - offset) // index_step)
        for k in range(k_max):
            weight = (interp_win[offset + k * index_step] + eta * interp_delta[offset + k * index_step])
            y[t] += weight * x[n + k + 1]

resample_f_p = numba.jit(nopython=True, nogil=True, parallel=True)(_resample_f)
resample_f_s = numba.jit(nopython=True, nogil=True, parallel=False)(_resample_f)


# coding: utf-8

import os
import errno

def clean_files():
    for base, dirs, files in os.walk('../src/'):
        for archive in files:
            if archive.endswith('.pyc'):
                os.remove(os.path.join(base, archive))
            if archive.endswith('.out'):
                os.remove(os.path.join(base, archive))
            if archive.startswith('parsetab'):
                os.remove(os.path.join(base, archive))
    for base, dirs, files in os.walk('..'):
        for archive in files:
            if archive.endswith('.DS_Store'):
                os.remove(os.path.join(base, archive))

def create_folder():
    try:
        os.makedirs('../result')
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

def menu():
    print("""
         ++++++++++++++++++++++++++++++++++++ Compilador para a Linguagem cmm ++++++++++++++++++++++++++++++++++++

         1 - Analisador Léxico
         2 - Analisador Sintático e Analisador Semântico

         0 - Sair
           """)
    clean_files()
    create_folder()

def analisadorLexico():
    from analisadorLexico import test_output_lexer
    from utils import Utils, path_files_result
    print("\n++++++++++++++++++++++++++++++++++++ Análisador Léxico ++++++++++++++++++++++++++++++++++++\n")
    test_output_lexer(Utils.save_archives_test(Utils.archive, path_files_result))
    print()

def analisadorSintatico():
    from analisadorSintatico import test_output_sintatico
    from utils import Utils, path_files_result
    print()
    print("\n++++++++++++++++++++++++++++++++ Árvore Sintática Abstrata ++++++++++++++++++++++++++++++++\n")
    import analisadorSemantico
    print(analisadorSemantico.dicionario_de_simbolos)
    test_output_sintatico(Utils.save_archives_test(Utils.archive, path_files_result))
    print()

def sair():
    print ("\nSaindo ...\n")

def case_default():
    print ('\nVálor inválido, favor digitar uma opção entre 0 e 3\n')
    menu()
    switch(int(input('Escolha uma opção: ')))

def switch(x):
    dict.get(x, case_default)()

dict = {
    1: analisadorLexico,
    2: analisadorSintatico,
    0: sair
    }

try:
    menu()
    switch(int(input('Escolha uma opção: ')))
except ValueError:
    print ('\nO valor digitado não é um número')
    menu()
    switch(int(input('Escolha uma opção: ')))