text:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-09-08 12:24
from __future__ import unicode_literals

from django.db import migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [("campusonline", "0010_auto_20171002_1450")]

    forward = [
        """
        CREATE FOREIGN TABLE "campusonline"."veranstaltungen" (
            PK_LV numeric,
            REIHUNG numeric,
            TYP varchar,
            NUMMER varchar,
            TITEL varchar,
            DATUM timestamptz,
            ZEIT_VON timestamptz,
            ZEIT_BIS timestamptz,
            PK_GEB numeric,
            GEBAEUDE varchar,
            PK_RAUM numeric,
            RAUM varchar,
            RAUM_BEZ varchar,
            TERMINART varchar,
            ANZEIGE_BIS timestamptz
        )
        SERVER sqlalchemy OPTIONS (
            tablename 'VERANSTALTUNGEN_HEUTE_V',
            db_url '{}'
        );
        """.format(
            settings.MULTICORN.get("campusonline")
        ),
        """
        CREATE MATERIALIZED VIEW "public"."campusonline_event" AS SELECT
            md5(concat(reihung,typ,titel,datum,zeit_von,zeit_bis,pk_geb,pk_raum)) AS id,
            pk_lv::integer AS course_id,
            reihung::integer AS order,
            typ AS category,
            titel AS title,
            datum AS date,
            zeit_von AS start,
            zeit_bis AS end,
            pk_geb::integer AS building_id,
            pk_raum::integer AS room_id,
            anzeige_bis AS show_end
        FROM "campusonline"."veranstaltungen"
        WITH DATA;
        """,
    ]

    reverse = [
        """
        DROP MATERIALIZED VIEW IF EXISTS "public"."campusonline_event";
        """,
        """
        DROP FOREIGN TABLE IF EXISTS "campusonline"."veranstaltungen";
        """,
    ]
    operations = [migrations.RunSQL(forward, reverse)]


text:
from pydub import AudioSegment
from pydub.playback import play
import os
import utils

class audiofile:

    def __init__(self, file):
        """ Init audio stream """
        self.file = file

    def play(self):
        """ Play entire file """
        utils.displayInfoMessage('Playing Audio')
        pathparts = self.file.rsplit(".", 1)
        fileformat = pathparts[1]
        song = AudioSegment.from_file(self.file, format=fileformat)
        play(song)
        utils.displayInfoMessage('')
        utils.displayErrorMessage('')

    def length(self):
        pathparts = self.file.rsplit(".", 1)
        fileformat = pathparts[1]
        song = AudioSegment.from_file(self.file, format=fileformat)
        return song.duration_seconds

domain: python


text:
#
#  Copyright 2019 The FATE Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
import os
import subprocess
import re
import sys
from importlib import import_module
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
CUSTOM_MODULES = set(['arch', 'eggroll', 'federatedml', 'fate_flow'])
USE_SOURCE_MODULES = set(['antlr4', 'mocks', 'TestTokenStreamRewriter'])

class DummyConfig(object):
    def __init__(self, intersphinx_mapping=None, intersphinx_cache_limit=5, intersphinx_timeout=None):
        self.intersphinx_mapping = intersphinx_mapping or {}
        self.intersphinx_cache_limit = intersphinx_cache_limit
        self.intersphinx_timeout = intersphinx_timeout
        self.tls_verify = True

class DummyApp(object):
    def __init__(self):
        self.config = DummyConfig()

def get_python_standard_modules(version=None):
    version = '{}.{}'.format(sys.version_info[0], sys.version_info[1]) if not version else version
    module_cache_file = 'python{}_modules.csv'.format(version.replace('.', '_'))
    if os.path.exists(module_cache_file):
        print('read python {} standard modules'.format(version))
        modules = list()
        with open(module_cache_file, 'r') as fr:
            while True:
                line = fr.readline()
                if not line:
                    break
                modules.append(line.strip())
    else:
        from sphinx.ext.intersphinx import fetch_inventory
        print('fetch python {} standard modules'.format(version))
        url = "http://docs.python.org/{}/objects.inv".format(version)

        modules = sorted(
            list(
                fetch_inventory(DummyApp(), "", url).get("py:module").keys()
            )
        )
        with open(module_cache_file, 'w') as fw:
            fw.write('\n'.join(modules))
    return modules

def search_require_modules(project_dir):
    grep_cmd = "find {} -name '*.py' | grep -v -E '*_pb2\.py' | grep -v -E '*_pb2_grpc\.py' | grep -v -E 'workflow\.py' | xargs -n1 cat | grep -E '^import|^from'".format(project_dir)
    print(grep_cmd)
    p = subprocess.Popen(grep_cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    stdout, stderr = p.communicate()
    import_lines = stdout.decode('utf-8').strip().split('\n')
    python_standard_modules = get_python_standard_modules('3.6')
    require_modules = set()
    require_lines = dict()
    all_imports = set()
    for line in import_lines:
        import_module = re.sub('^import |^from ', '', line).split(' ')[0].strip()
        require_module = import_module.split('.')[0]
        if len(require_module) == 0:
            continue
        if ',' in require_module:
            tmp = require_module.split(',')
        else:
            tmp = [require_module]
        for r_m in tmp:
            if r_m.startswith('.'):
                continue
            if r_m.endswith('_pb2'):
                continue
            if r_m in USE_SOURCE_MODULES:
                continue
            all_imports.add(line.strip())
            if r_m in python_standard_modules:
                continue
            if r_m in CUSTOM_MODULES:
                continue
            require_modules.add(r_m)
            require_lines[r_m] = line.strip()
    return require_modules, require_lines, all_imports

def conda_env_install(module):
    print('try install: {}'.format(module))
    install_cmd = 'conda install -y {}'.format(module)
    p = subprocess.Popen(install_cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    stdout, stderr = p.communicate()
    if p.returncode != 0:
        print('try install again: {}'.format(module))
        install_cmd = 'conda install -c conda-forge -y {}'.format(module)
        p = subprocess.Popen(install_cmd,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, stderr = p.communicate()
    return p.returncode

def pip_env_install(module):
    print('try install: {}'.format(module))
    install_cmd = 'pip install {}'.format(module)
    p = subprocess.Popen(install_cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    stdout, stderr = p.communicate()
    return p.returncode

def try_import(module):
    try:
        import_module(module)
        return 0
    except Exception as e:
        st = pip_env_install(module)
        if st == 0:
            return 1
        else:
            return 2

def check_require(require_modules, require_lines):
    for require_module in require_modules:
        st = try_import(require_module)
        if st == 0:
            continue
        elif st == 1:
            print('installed {}: {}\n'.format(require_module, require_lines[require_module]))
        elif st == 2:
            print('failed installed {}: {}\n'.format(require_module, require_lines[require_module]))

def check_import(all_imports):
    dependent_modules = set()
    dependent_lines = dict()
    for import_code in all_imports:
        python_cmd = "python -c '{}'".format(import_code)
        p = subprocess.Popen(python_cmd,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        stdout, stderr = p.communicate()
        if p.returncode != 0:
            # import error
            stdout = stdout.decode('utf-8').strip().split('\n')
            for line in stdout:
                if line.startswith('ModuleNotFoundError:'):
                    require_module = line.strip().split(' ')[-1].strip("'").split('.')[0]
                    print('{}: {}'.format(require_module, import_code))
                    if require_module in CUSTOM_MODULES:
                        pass
                        # code error
                    else:
                        dependent_modules.add(require_module)
                        dependent_lines[require_module] = import_code
    return dependent_modules, dependent_lines

if __name__ == '__main__':
    print('project dir is: {}'.format(PROJECT_DIR))
    print('start search import')
    require_modules, require_lines, all_imports = search_require_modules(PROJECT_DIR)
    print()
    print('has {} require modules'.format(len(require_modules)))
    print(require_modules)
    print()
    check_require(require_modules=require_modules, require_lines=require_lines)
    print()
    dependent_modules, dependent_lines = check_import(all_imports=all_imports)
    print()
    require_modules.update(dependent_modules)
    require_lines.update(dependent_lines)
    check_require(require_modules=require_modules, require_lines=require_lines)
    print()


text:
"""
This problem was asked by Amazon.

Given a matrix of 1s and 0s, return the number of "islands" in the matrix.
A 1 represents land and 0 represents water, so an island is a group of 1s
that are neighboring whose perimeter is surrounded by water.

For example, this matrix has 4 islands.

1 0 0 0 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
1 1 0 0 1
1 1 0 0 1
"""

moves = [
    # row, col
    (0, 1),  # west
    (0, -1),  # east
    (1, 0),  # south
    (-1, 0), # north

    (1,1), # south-west
    (1, -1), # south-east
    (-1, 1), # north-west
    (-1, -1) # north-east

]

def mark_island(row, col, land_map, marker):

    if row < 0 or col<0 or row>=len(land_map) or col >= len(land_map[0]):
        return land_map

    if land_map[row][col]== 0:
        return land_map

    if land_map[row][col]== marker:
        return land_map

    if land_map[row][col] == 1:
        land_map[row][col] = marker

    for r,c in moves:
        land_map = mark_island(row+r, col+c, land_map, marker)

    return land_map

def find_num_of_islands(land_map):
    islands_found = 0

    for i in range(len(land_map)):
        for j in range(len(land_map[0])):
            if land_map[i][j] == 1:
                islands_found+= 1
                land_map = mark_island(i, j, land_map, marker='i')

    # print(*land_map, sep='\n')
    return islands_found

if __name__ == '__main__':
    land_map = [
        [1, 0, 0, 0, 0],
        [0, 0, 1, 1, 0],
        [0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 1, 0, 0, 1],
        [1, 1, 0, 0, 1],
    ]

    print(find_num_of_islands(land_map))   #  4

    land_map = [
        [1, 0, 0, 0, 0],
        [0, 0, 1, 1, 0],
        [0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 1, 0, 0, 1],
        [1, 1, 0, 0, 1],
        [1, 0, 0, 0, 0],
        [0, 0, 1, 1, 0],
        [0, 1, 1, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 1, 0, 0, 1],
        [1, 1, 0, 0, 1],
    ]

    print(find_num_of_islands(land_map))  # 7


text:
#
#  The Multiverse Platform is made available under the MIT License.
#
#  Copyright (c) 2012 The Multiverse Foundation
#
#  Permission is hereby granted, free of charge, to any person
#  obtaining a copy of this software and associated documentation
#  files (the "Software"), to deal in the Software without restriction,
#  including without limitation the rights to use, copy, modify,
#  merge, publish, distribute, sublicense, and/or sell copies
#  of the Software, and to permit persons to whom the Software
#  is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
#  OR OTHER DEALINGS IN THE SOFTWARE.
#
#

#!/usr/bin/python

#
# This script sets up a render server machine
#
import sys
import os
import subprocess
import shutil
import glob

# NOTE - if you change machinimaPath, you need to update BuildDirs()
machinimaPath = 'c:\\Multiverse\\Machinima\\'
treePath = 'c:\\Multiverse\\tree\\'

toolsToCopy = [ 'processJob.py', 'sendJob.py', 'mencoder.exe', 'machinima.sshkey.ppk', 'pscp.exe', 'MP4Box.exe' ]
toolSourceDir = os.path.join(treePath, 'Tools\\Machinima')
srcMediaDir = os.path.join(treePath, 'Media')

# map our internal world name to the public world name
# used when copying assets from our source tree to the production media area
worldNameMap = {
    'nyts' : 'times_square',
    'friendworld' : 'friendworld2'
}

# requires a full path name including drive spec
# returns a list of directory components
def SplitDirs(path):
    # split off the drive spec
    drive, path = os.path.splitdrive(path)

    # remove ending slash if it is present
    if path.endswith('\\'):
        path, empty = os.path.split(path)

    pathList = []
    while path is not None:
        path, element = os.path.split(path)
        if len(element) == 0:
            pathList.append(path)
            path = None
        else:
            pathList.append(element)

    pathList.append(drive)
    pathList.reverse()
    return pathList

def BuildPath(pathList, numDirs):
    path = os.path.join(pathList[0], pathList[1])
    for i in range(2,numDirs):
        path = os.path.join(path, pathList[i])
    return path

# make sure all the directories in the path exist
def MakePath(fullpath):
    pathList = SplitDirs(fullpath)
    for i in range(3, len(pathList) + 1):
        path = BuildPath(pathList, i)
        if not os.path.exists(path):
            print 'Making %s' % path
            os.mkdir(path)

def MakeDirIfNeeded(path):
    if not os.path.exists(path):
        os.mkdir(path, 0777)

def BuildDirs():
    MakePath(machinimaPath)

    MakeDirIfNeeded(os.path.join(machinimaPath, 'Tools'))
    MakeDirIfNeeded(os.path.join(machinimaPath, 'Media'))
    MakeDirIfNeeded(os.path.join(machinimaPath, 'Jobs'))

def CopyTools():
    destPath = os.path.join(machinimaPath, 'Tools')
    for tool in toolsToCopy:
        srcPath = os.path.join(toolSourceDir, tool)
        shutil.copy(srcPath, destPath)

def CopyTree(src, dst):
  """Because shutil.copytree() fails if a directory already exists"""
  srcLen = len(src)
  for path, dirs, files in os.walk(src):
    if '.svn' in path:
      continue
    dstPath = path[srcLen:]
    for dir in dirs:
      if '.svn' in dir:
        continue
      dstDir = os.path.join(dst, dstPath, dir)
      MakePath(dstDir)
    for file in files:
      srcPath = os.path.join(path, file)
      subPath = srcPath[srcLen:]
      dstPath = os.path.join(dst, subPath)
      shutil.copy2(srcPath, dstPath)

def CopyMedia():
    sceneDirs = glob.glob(os.path.join(srcMediaDir, '*\\Machinima\\*\\'))
    for sceneDir in sceneDirs:
        dirList = SplitDirs(sceneDir)
        numElements = len(dirList)
        scene = dirList[numElements-1]
        world = worldNameMap[dirList[numElements-3]]
        destMediaDir = os.path.join(machinimaPath, 'Media\\')
        destDir = os.path.join(os.path.join(destMediaDir, world), scene)
        CopyTree(sceneDir, destDir)

    saMediaSrc = os.path.join(srcMediaDir, 'standalone\\')
    saMediaDest = os.path.join(destMediaDir, 'standalone\\')
    CopyTree(saMediaSrc, saMediaDest)

BuildDirs()
CopyTools()
CopyMedia()


text:
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi SDK Generator. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***

import warnings
import pulumi
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
from ... import _utilities
from . import outputs
from ._inputs import *

__all__ = ['PrivateEndpointConnectionArgs', 'PrivateEndpointConnection']

@pulumi.input_type
class PrivateEndpointConnectionArgs:
    def __init__(__self__, *,
                 account_name: pulumi.Input[str],
                 resource_group_name: pulumi.Input[str],
                 group_id: Optional[pulumi.Input[str]] = None,
                 private_endpoint: Optional[pulumi.Input['PrivateEndpointPropertyArgs']] = None,
                 private_endpoint_connection_name: Optional[pulumi.Input[str]] = None,
                 private_link_service_connection_state: Optional[pulumi.Input['PrivateLinkServiceConnectionStatePropertyArgs']] = None,
                 provisioning_state: Optional[pulumi.Input[str]] = None):
        """
        The set of arguments for constructing a PrivateEndpointConnection resource.
        :param pulumi.Input[str] account_name: Cosmos DB database account name.
        :param pulumi.Input[str] resource_group_name: The name of the resource group. The name is case insensitive.
        :param pulumi.Input[str] group_id: Group id of the private endpoint.
        :param pulumi.Input['PrivateEndpointPropertyArgs'] private_endpoint: Private endpoint which the connection belongs to.
        :param pulumi.Input[str] private_endpoint_connection_name: The name of the private endpoint connection.
        :param pulumi.Input['PrivateLinkServiceConnectionStatePropertyArgs'] private_link_service_connection_state: Connection State of the Private Endpoint Connection.
        :param pulumi.Input[str] provisioning_state: Provisioning state of the private endpoint.
        """
        pulumi.set(__self__, "account_name", account_name)
        pulumi.set(__self__, "resource_group_name", resource_group_name)
        if group_id is not None:
            pulumi.set(__self__, "group_id", group_id)
        if private_endpoint is not None:
            pulumi.set(__self__, "private_endpoint", private_endpoint)
        if private_endpoint_connection_name is not None:
            pulumi.set(__self__, "private_endpoint_connection_name", private_endpoint_connection_name)
        if private_link_service_connection_state is not None:
            pulumi.set(__self__, "private_link_service_connection_state", private_link_service_connection_state)
        if provisioning_state is not None:
            pulumi.set(__self__, "provisioning_state", provisioning_state)

    @property
    @pulumi.getter(name="accountName")
    def account_name(self) -> pulumi.Input[str]:
        """
        Cosmos DB database account name.
        """
        return pulumi.get(self, "account_name")

    @account_name.setter
    def account_name(self, value: pulumi.Input[str]):
        pulumi.set(self, "account_name", value)

    @property
    @pulumi.getter(name="resourceGroupName")
    def resource_group_name(self) -> pulumi.Input[str]:
        """
        The name of the resource group. The name is case insensitive.
        """
        return pulumi.get(self, "resource_group_name")

    @resource_group_name.setter
    def resource_group_name(self, value: pulumi.Input[str]):
        pulumi.set(self, "resource_group_name", value)

    @property
    @pulumi.getter(name="groupId")
    def group_id(self) -> Optional[pulumi.Input[str]]:
        """
        Group id of the private endpoint.
        """
        return pulumi.get(self, "group_id")

    @group_id.setter
    def group_id(self, value: Optional[pulumi.Input[str]]):
        pulumi.set(self, "group_id", value)

    @property
    @pulumi.getter(name="privateEndpoint")
    def private_endpoint(self) -> Optional[pulumi.Input['PrivateEndpointPropertyArgs']]:
        """
        Private endpoint which the connection belongs to.
        """
        return pulumi.get(self, "private_endpoint")

    @private_endpoint.setter
    def private_endpoint(self, value: Optional[pulumi.Input['PrivateEndpointPropertyArgs']]):
        pulumi.set(self, "private_endpoint", value)

    @property
    @pulumi.getter(name="privateEndpointConnectionName")
    def private_endpoint_connection_name(self) -> Optional[pulumi.Input[str]]:
        """
        The name of the private endpoint connection.
        """
        return pulumi.get(self, "private_endpoint_connection_name")

    @private_endpoint_connection_name.setter
    def private_endpoint_connection_name(self, value: Optional[pulumi.Input[str]]):
        pulumi.set(self, "private_endpoint_connection_name", value)

    @property
    @pulumi.getter(name="privateLinkServiceConnectionState")
    def private_link_service_connection_state(self) -> Optional[pulumi.Input['PrivateLinkServiceConnectionStatePropertyArgs']]:
        """
        Connection State of the Private Endpoint Connection.
        """
        return pulumi.get(self, "private_link_service_connection_state")

    @private_link_service_connection_state.setter
    def private_link_service_connection_state(self, value: Optional[pulumi.Input['PrivateLinkServiceConnectionStatePropertyArgs']]):
        pulumi.set(self, "private_link_service_connection_state", value)

    @property
    @pulumi.getter(name="provisioningState")
    def provisioning_state(self) -> Optional[pulumi.Input[str]]:
        """
        Provisioning state of the private endpoint.
        """
        return pulumi.get(self, "provisioning_state")

    @provisioning_state.setter
    def provisioning_state(self, value: Optional[pulumi.Input[str]]):
        pulumi.set(self, "provisioning_state", value)

class PrivateEndpointConnection(pulumi.CustomResource):
    @overload
    def __init__(__self__,
                 resource_name: str,
                 opts: Optional[pulumi.ResourceOptions] = None,
                 account_name: Optional[pulumi.Input[str]] = None,
                 group_id: Optional[pulumi.Input[str]] = None,
                 private_endpoint: Optional[pulumi.Input[pulumi.InputType['PrivateEndpointPropertyArgs']]] = None,
                 private_endpoint_connection_name: Optional[pulumi.Input[str]] = None,
                 private_link_service_connection_state: Optional[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionStatePropertyArgs']]] = None,
                 provisioning_state: Optional[pulumi.Input[str]] = None,
                 resource_group_name: Optional[pulumi.Input[str]] = None,
                 __props__=None):
        """
        A private endpoint connection

        :param str resource_name: The name of the resource.
        :param pulumi.ResourceOptions opts: Options for the resource.
        :param pulumi.Input[str] account_name: Cosmos DB database account name.
        :param pulumi.Input[str] group_id: Group id of the private endpoint.
        :param pulumi.Input[pulumi.InputType['PrivateEndpointPropertyArgs']] private_endpoint: Private endpoint which the connection belongs to.
        :param pulumi.Input[str] private_endpoint_connection_name: The name of the private endpoint connection.
        :param pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionStatePropertyArgs']] private_link_service_connection_state: Connection State of the Private Endpoint Connection.
        :param pulumi.Input[str] provisioning_state: Provisioning state of the private endpoint.
        :param pulumi.Input[str] resource_group_name: The name of the resource group. The name is case insensitive.
        """
        ...
    @overload
    def __init__(__self__,
                 resource_name: str,
                 args: PrivateEndpointConnectionArgs,
                 opts: Optional[pulumi.ResourceOptions] = None):
        """
        A private endpoint connection

        :param str resource_name: The name of the resource.
        :param PrivateEndpointConnectionArgs args: The arguments to use to populate this resource's properties.
        :param pulumi.ResourceOptions opts: Options for the resource.
        """
        ...
    def __init__(__self__, resource_name: str, *args, **kwargs):
        resource_args, opts = _utilities.get_resource_args_opts(PrivateEndpointConnectionArgs, pulumi.ResourceOptions, *args, **kwargs)
        if resource_args is not None:
            __self__._internal_init(resource_name, opts, **resource_args.__dict__)
        else:
            __self__._internal_init(resource_name, *args, **kwargs)

    def _internal_init(__self__,
                 resource_name: str,
                 opts: Optional[pulumi.ResourceOptions] = None,
                 account_name: Optional[pulumi.Input[str]] = None,
                 group_id: Optional[pulumi.Input[str]] = None,
                 private_endpoint: Optional[pulumi.Input[pulumi.InputType['PrivateEndpointPropertyArgs']]] = None,
                 private_endpoint_connection_name: Optional[pulumi.Input[str]] = None,
                 private_link_service_connection_state: Optional[pulumi.Input[pulumi.InputType['PrivateLinkServiceConnectionStatePropertyArgs']]] = None,
                 provisioning_state: Optional[pulumi.Input[str]] = None,
                 resource_group_name: Optional[pulumi.Input[str]] = None,
                 __props__=None):
        if opts is None:
            opts = pulumi.ResourceOptions()
        if not isinstance(opts, pulumi.ResourceOptions):
            raise TypeError('Expected resource options to be a ResourceOptions instance')
        if opts.version is None:
            opts.version = _utilities.get_version()
        if opts.id is None:
            if __props__ is not None:
                raise TypeError('__props__ is only valid when passed in combination with a valid opts.id to get an existing resource')
            __props__ = PrivateEndpointConnectionArgs.__new__(PrivateEndpointConnectionArgs)

            if account_name is None and not opts.urn:
                raise TypeError("Missing required property 'account_name'")
            __props__.__dict__["account_name"] = account_name
            __props__.__dict__["group_id"] = group_id
            __props__.__dict__["private_endpoint"] = private_endpoint
            __props__.__dict__["private_endpoint_connection_name"] = private_endpoint_connection_name
            __props__.__dict__["private_link_service_connection_state"] = private_link_service_connection_state
            __props__.__dict__["provisioning_state"] = provisioning_state
            if resource_group_name is None and not opts.urn:
                raise TypeError("Missing required property 'resource_group_name'")
            __props__.__dict__["resource_group_name"] = resource_group_name
            __props__.__dict__["name"] = None
            __props__.__dict__["type"] = None
        alias_opts = pulumi.ResourceOptions(aliases=[pulumi.Alias(type_="azure-nextgen:documentdb/v20210615:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20190801preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20190801preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210115:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210115:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210301preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210301preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210315:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210315:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210401preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210401preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210415:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210415:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210515:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210515:PrivateEndpointConnection"), pulumi.Alias(type_="azure-native:documentdb/v20210701preview:PrivateEndpointConnection"), pulumi.Alias(type_="azure-nextgen:documentdb/v20210701preview:PrivateEndpointConnection")])
        opts = pulumi.ResourceOptions.merge(opts, alias_opts)
        super(PrivateEndpointConnection, __self__).__init__(
            'azure-native:documentdb/v20210615:PrivateEndpointConnection',
            resource_name,
            __props__,
            opts)

    @staticmethod
    def get(resource_name: str,
            id: pulumi.Input[str],
            opts: Optional[pulumi.ResourceOptions] = None) -> 'PrivateEndpointConnection':
        """
        Get an existing PrivateEndpointConnection resource's state with the given name, id, and optional extra
        properties used to qualify the lookup.

        :param str resource_name: The unique name of the resulting resource.
        :param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
        :param pulumi.ResourceOptions opts: Options for the resource.
        """
        opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))

        __props__ = PrivateEndpointConnectionArgs.__new__(PrivateEndpointConnectionArgs)

        __props__.__dict__["group_id"] = None
        __props__.__dict__["name"] = None
        __props__.__dict__["private_endpoint"] = None
        __props__.__dict__["private_link_service_connection_state"] = None
        __props__.__dict__["provisioning_state"] = None
        __props__.__dict__["type"] = None
        return PrivateEndpointConnection(resource_name, opts=opts, __props__=__props__)

    @property
    @pulumi.getter(name="groupId")
    def group_id(self) -> pulumi.Output[Optional[str]]:
        """
        Group id of the private endpoint.
        """
        return pulumi.get(self, "group_id")

    @property
    @pulumi.getter
    def name(self) -> pulumi.Output[str]:
        """
        The name of the resource
        """
        return pulumi.get(self, "name")

    @property
    @pulumi.getter(name="privateEndpoint")
    def private_endpoint(self) -> pulumi.Output[Optional['outputs.PrivateEndpointPropertyResponse']]:
        """
        Private endpoint which the connection belongs to.
        """
        return pulumi.get(self, "private_endpoint")

    @property
    @pulumi.getter(name="privateLinkServiceConnectionState")
    def private_link_service_connection_state(self) -> pulumi.Output[Optional['outputs.PrivateLinkServiceConnectionStatePropertyResponse']]:
        """
        Connection State of the Private Endpoint Connection.
        """
        return pulumi.get(self, "private_link_service_connection_state")

    @property
    @pulumi.getter(name="provisioningState")
    def provisioning_state(self) -> pulumi.Output[Optional[str]]:
        """
        Provisioning state of the private endpoint.
        """
        return pulumi.get(self, "provisioning_state")

    @property
    @pulumi.getter
    def type(self) -> pulumi.Output[str]:
        """
        The type of the resource. E.g. "Microsoft.Compute/virtualMachines" or "Microsoft.Storage/storageAccounts"
        """
        return pulumi.get(self, "type")


text:
from datetime import datetime
import json
from unittest import TestCase

from celery.schedules import schedule, crontab
try:  # celery 3.x
    from celery.utils.timeutils import timezone
except ImportError:  # celery 4.x
    from celery.utils.time import timezone

from redbeat.decoder import RedBeatJSONDecoder, RedBeatJSONEncoder
from redbeat.schedules import rrule

class JSONTestCase(TestCase):

    def dumps(self, d):
        return json.dumps(d, cls=RedBeatJSONEncoder)

    def loads(self, d):
        return json.loads(d, cls=RedBeatJSONDecoder)

    def datetime(self, **kwargs):
        d = {
            '__type__': 'datetime',
            'year': 2015,
            'month': 12,
            'day': 30,
            'hour': 12,
            'minute': 59,
            'second': 22,
            'microsecond': 333,
        }
        d.update(kwargs)
        return d

    def schedule(self, **kwargs):
        d = {
            '__type__': 'interval',
            'every': 60.0,
            'relative': False,
        }
        d.update(kwargs)
        return d

    def crontab(self, **kwargs):
        d = {
            '__type__': 'crontab',
            'minute': '*',
            'hour': '*',
            'day_of_week': '*',
            'day_of_month': '*',
            'month_of_year': '*',
        }
        d.update(kwargs)
        return d

    def rrule(self, **kwargs):
        d = {
            '__type__': 'rrule',
            'freq': 5,
            'dtstart': 1451480362,
            'interval': 1,
            'wkst': None,
            'count': 1,
            'until': None,
            'bysetpos': None,
            'bymonth': None,
            'bymonthday': None,
            'byyearday': None,
            'byeaster': None,
            'byweekno': None,
            'byweekday': None,
            'byhour': None,
            'byminute': None,
            'bysecond': None,
        }
        d.update(kwargs)
        return d

class RedBeatJSONEncoderTestCase(JSONTestCase):

    def test_datetime(self):
        dt = datetime.now()
        result = self.dumps(dt)

        expected = self.datetime()
        for key in (k for k in expected if hasattr(dt, k)):
            expected[key] = getattr(dt, key)

        self.assertEqual(result, json.dumps(expected))

    def test_schedule(self):
        s = schedule(run_every=60.0)
        result = self.dumps(s)
        self.assertEqual(result, json.dumps(self.schedule(every=60.0)))

    def test_crontab(self):
        c = crontab()
        result = self.dumps(c)
        self.assertEqual(result, json.dumps(self.crontab()))

    def test_rrule(self):
        r = rrule('MINUTELY', dtstart=datetime(2015, 12, 30, 12, 59, 22, tzinfo=timezone.utc), count=1)
        result = self.dumps(r)
        self.assertEqual(result, json.dumps(self.rrule()))

    def test_rrule_timezone(self):
        tz = timezone.get_timezone('US/Eastern')

        start1 = datetime(2015, 12, 30, 12, 59, 22, tzinfo=timezone.utc)
        start2 = start1.astimezone(tz)

        r1 = rrule('MINUTELY', dtstart=start1, count=1)
        r2 = rrule('MINUTELY', dtstart=start2, count=1)

        self.assertEqual(self.dumps(r1), self.dumps(r2))

class RedBeatJSONDecoderTestCase(JSONTestCase):

    def test_datetime(self):
        d = self.datetime()

        result = self.loads(json.dumps(d))

        d.pop('__type__')
        self.assertEqual(result, datetime(tzinfo=timezone.utc, **d))

    def test_schedule(self):
        d = self.schedule()

        result = self.loads(json.dumps(d))

        d.pop('__type__')
        self.assertEqual(result, schedule(run_every=60))

    def test_crontab(self):
        d = self.crontab()

        result = self.loads(json.dumps(d))

        d.pop('__type__')
        self.assertEqual(result, crontab())

    def test_rrule(self):
        d = self.rrule()

        result = self.loads(json.dumps(d))

        d.pop('__type__')
        self.assertEqual(
            result,
            rrule('MINUTELY', dtstart=datetime(2015, 12, 30, 12, 59, 22, tzinfo=timezone.utc), count=1),
            )


text:
# Config
NODE_ID = ${NODE_ID}

# hour,set 0 to disable
SPEEDTEST = ${SPEEDTEST}
CLOUDSAFE = ${CLOUDSAFE}
ANTISSATTACK = ${ANTISSATTACK}
AUTOEXEC = ${AUTOEXEC}

MU_SUFFIX = "${MU_SUFFIX}"
MU_REGEX = "${MU_REGEX}"

SERVER_PUB_ADDR = "127.0.0.1"  # mujson_mgr need this to generate ssr link
API_INTERFACE = "${API_INTERFACE}"  # glzjinmod, modwebapi

WEBAPI_URL = "${WEBAPI_URL}"
WEBAPI_TOKEN = "${WEBAPI_TOKEN}"

# mudb
MUDB_FILE = 'mudb.json'

# Mysql
MYSQL_HOST = "${MYSQL_HOST}"
MYSQL_PORT = ${MYSQL_PORT}
MYSQL_USER = "${MYSQL_USER}"
MYSQL_PASS = "${MYSQL_PASS}"
MYSQL_DB = "${MYSQL_DB}"

MYSQL_SSL_ENABLE = 0
MYSQL_SSL_CA = ''
MYSQL_SSL_CERT = ''
MYSQL_SSL_KEY = ''

# API
API_HOST = '127.0.0.1'
API_PORT = 80
API_PATH = '/mu/v2/'
API_TOKEN = 'abcdef'
API_UPDATE_TIME = 60

# Manager (ignore this)
MANAGE_PASS = 'ss233333333'
# if you want manage in other server you should set this value to global ip
MANAGE_BIND_IP = '127.0.0.1'
# make sure this port is idle
MANAGE_PORT = 23333


text:
import re
import requests
import time

class TeleBot(object):

    def __init__(self, import_name):
        self.import_name = import_name
        self.update_rules = list()
        self.config = dict(
            api_key=None,
            requests_kwargs=dict(
                timeout=60,
            ),
        )
        self.offset = 0
        self.whoami = None

    def add_update_rule(self, rule, endpoint=None, view_func=None, **options):
        self.update_rules.append(dict(
            rule=re.compile(rule),
            endpoint=endpoint,
            view_func=view_func,
            options=dict(**options),
        ))

    def route(self, rule, **options):
        """A decorator that is used to register a view function for a
        given URL rule.  This does the same thing as :meth:`add_url_rule`
        but is intended for decorator usage::
            @app.route('/')
            def index():
                return 'Hello World'
        For more information refer to :ref:`url-route-registrations`.
        :param rule: the URL rule as string
        :param endpoint: the endpoint for the registered URL rule.  Flask
                         itself assumes the name of the view function as
                         endpoint
        :param options: the options to be forwarded to the underlying
                        :class:`~werkzeug.routing.Rule` object.  A change
                        to Werkzeug is handling of method options.  methods
                        is a list of methods this rule should be limited
                        to (``GET``, ``POST`` etc.).  By default a rule
                        just listens for ``GET`` (and implicitly ``HEAD``).
                        Starting with Flask 0.6, ``OPTIONS`` is implicitly
                        added and handled by the standard request handling.
        """
        def decorator(f):
            endpoint = options.pop('endpoint', None)
            self.add_update_rule(rule, endpoint, f, **options)
            return f
        return decorator

    def process_update(self, update):
        self.offset = max(self.offset, update.get('update_id', 0)) + 1

        for x in self.update_rules:
            # TODO: Find a good pattern to detect each type and process
            #       accordingly.
            if 'message' in update and 'text' in update['message'] and \
                    x['rule'].match(update['message']['text']):
                m = x['rule'].match(update['message']['text'])
                x['view_func'](update['message'],
                               *m.groups(),
                               **m.groupdict())

    def process_updates(self, updates):
        if updates.get('ok', False) is True:
            for msg in updates['result']:
                self.process_update(msg)

    def _start(self):
        '''Requests bot information based on current api_key, and sets
        self.whoami to dictionary with username, first_name, and id of the
        configured bot.

        '''
        if self.whoami is None:
            me = self.get_me()
            if me.get('ok', False):
                self.whoami = me['result']
            else:
                raise ValueError("Bot Cannot request information, check "
                                 "api_key")

    def poll(self, offset=None, poll_timeout=600, cooldown=60, debug=False):
        '''These should also be in the config section, but some here for
        overrides

        '''
        if self.config['api_key'] is None:
            raise ValueError("config api_key is undefined")

        if offset or self.config.get('offset', None):
            self.offset = offset or self.config.get('offset', None)

        self._start()

        while True:
            try:
                response = self.get_updates(poll_timeout, self.offset)
                if response.get('ok', False) is False:
                    raise ValueError(response['error'])
                else:
                    self.process_updates(response)
            except Exception as e:
                print("Error: Unknown Exception")
                print(e)
                if debug:
                    raise e
                else:
                    time.sleep(cooldown)

    def listen(self):
        raise NotImplemented

    def _bot_cmd(self, method, endpoint, *args, **kwargs):
        base_api = "https://api.telegram.org/bot{api_key}/{endpoint}"
        endpoint = base_api.format(api_key=self.config['api_key'],
                                   endpoint=endpoint)

        try:
            response = method(endpoint,
                              data=kwargs.get('data', None),
                              params=kwargs.get('params', {}),
                              **self.config['requests_kwargs'])

            if response.status_code != 200:
                raise ValueError('Got unexpected response. ({}) - {}'.
                                 format(response.status_code, response.text))

            return response.json()
        except Exception as e:
            return {
                'ok': False,
                'error': str(e),
            }

    def get_me(self):
        '''A simple method for testing your bot's auth token. Requires no
        parameters. Returns basic information about the bot in form of a `User
        object.

        '''
        return self._bot_cmd(requests.get, 'getMe')

    def send_message(self, chat_id, text):
        data = dict(
            chat_id=chat_id,
            text=text,
        )

        return self._bot_cmd(requests.post, 'sendMessage', data=data)

    def forward_message(self):
        raise NotImplemented("forward_message needs work")

    def send_photo(self):
        raise NotImplemented("send_photo needs work")

    def send_audio(self):
        raise NotImplemented("send_audio needs work")

    def send_document(self):
        raise NotImplemented("send_document needs work")

    def send_sticker(self):
        raise NotImplemented("send_sticker needs work")

    def send_video(self):
        raise NotImplemented("send_video needs work")

    def send_location(self):
        raise NotImplemented("send_location needs work")

    def send_chat_action(self):
        raise NotImplemented("send_chat_action needs work")

    def get_user_profile_photos(self):
        raise NotImplemented("get_user_profile_photos needs work")

    def get_updates(self, timeout=0, offset=None):
        params = dict(
            timeout=timeout,
            offset=offset,
        )
        return self._bot_cmd(requests.get, 'getUpdates', params=params)

    def set_webhook(self):
        raise NotImplemented("set_webhook needs work")


text:
import numpy as np # linear algebra
np.random.seed(42)
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.model_selection import train_test_split
from matplotlib import pyplot
import time
import os, glob
import cv2

# parameters
format = "%H%M"
ts = time.strftime(format)
base_name = os.path.splitext(__file__)[0] + "_ts" + ts
input_size = 128

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, GaussianNoise
from keras.layers import GlobalMaxPooling2D, Reshape, UpSampling3D, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.merge import Concatenate
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, Callback, EarlyStopping, CSVLogger, ReduceLROnPlateau
from keras import backend as K

def get_callbacks(save_path, lr=0.001, patience=64):
    csv_logger = CSVLogger(save_path + '_log.csv', append=True)
    # check_path = save_path + '_e{epoch:02d}_vl{val_loss:.5f}.hdf5'
    check_path = save_path
    save_checkpoint = ModelCheckpoint(filepath=check_path, monitor='val_loss', save_best_only=True)
    lerning_rate_schedular = ReduceLROnPlateau(patience=8, min_lr=lr * 0.00001)
    early_stopping = EarlyStopping(monitor='val_loss',
                                   patience=16,
                                   verbose=1,
                                   min_delta=1e-4,
                                   mode='min')
    Callbacks = [csv_logger,
                 save_checkpoint,
                 # lerning_rate_schedular,
                 early_stopping
                 ]
    return Callbacks

def swish(x):
    return x * K.sigmoid(x)

from keras.applications.vgg16 import VGG16
from keras.optimizers import SGD

def get_model(num_class):
    base_model = VGG16(weights='imagenet', include_top=False,
                       input_shape=[input_size,input_size,3], classes=1)
    x = base_model.get_layer('block5_pool').output

    x = GlobalMaxPooling2D()(x)
    x = Dense(512, activation='relu', name='fc2')(x)
    x = Dropout(0.3)(x)
    x = Dense(512, activation='relu', name='fc3')(x)
    x = Dropout(0.3)(x)

    predictions = Dense(num_class, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=predictions)

    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['accuracy'])
    return model

def randomHueSaturationValue(image, hue_shift_limit=(-180, 180),
                             sat_shift_limit=(-255, 255),
                             val_shift_limit=(-255, 255), u=0.5):
    if np.random.random() < u:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        h, s, v = cv2.split(image) # sikisou, saido, meido
        hue_shift = np.random.uniform(hue_shift_limit[0], hue_shift_limit[1])
        h = cv2.add(h, hue_shift)
        sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1])
        s = cv2.add(s, sat_shift)
        val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1])
        v = cv2.add(v, val_shift)
        image = cv2.merge((h, s, v))
        image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)

    return image

def randomShiftScaleRotate(image,
                           shift_limit=(-0.0625, 0.0625),
                           scale_limit=(-0.1, 0.1),
                           rotate_limit=(-45, 45), aspect_limit=(0, 0),
                           borderMode=cv2.BORDER_CONSTANT, u=0.5):
    if np.random.random() < u:
        height, width, channel = image.shape

        angle = np.random.uniform(rotate_limit[0], rotate_limit[1])  # degree
        scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
        aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
        sx = scale * aspect / (aspect ** 0.5)
        sy = scale / (aspect ** 0.5)
        dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
        dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)

        cc = np.math.cos(angle / 180 * np.math.pi) * sx
        ss = np.math.sin(angle / 180 * np.math.pi) * sy
        rotate_matrix = np.array([[cc, -ss], [ss, cc]])

        box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
        box1 = box0 - np.array([width / 2, height / 2])
        box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])

        box0 = box0.astype(np.float32)
        box1 = box1.astype(np.float32)
        mat = cv2.getPerspectiveTransform(box0, box1)
        image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
                                    borderValue=(
                                        0, 0,
                                        0,))

    return image

def randomHorizontalFlip(image, u=0.5):
    if np.random.random() < u:
        image = cv2.flip(image, 1)

    return image

def randomVerticalFlip(image, u=0.5):
    if np.random.random() < u:
        image = cv2.flip(image, 0)

    return image

def get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):
    def eraser(input_img):
        img_h, img_w, img_c = input_img.shape
        p_1 = np.random.rand()

        if p_1 > p:
            return input_img

        while True:
            s = np.random.uniform(s_l, s_h) * img_h * img_w
            r = np.random.uniform(r_1, r_2)
            w = int(np.sqrt(s / r))
            h = int(np.sqrt(s * r))
            left = np.random.randint(0, img_w)
            top = np.random.randint(0, img_h)

            if left + w <= img_w and top + h <= img_h:
                break

        if pixel_level:
            c = np.random.uniform(v_l, v_h, (h, w, img_c))
        else:
            c = np.random.uniform(v_l, v_h)

        input_img[top:top + h, left:left + w, :] = c

        return input_img

    return eraser

from multiprocessing import Pool

def load_img(args):
    img_path = args
    img = cv2.imread(img_path)
    # print("img shape", img.shape)
    img = cv2.resize(img, (input_size, input_size))
    img = randomHueSaturationValue(img,
                                   hue_shift_limit=(-5, 5),
                                   sat_shift_limit=(-1, 1),
                                   val_shift_limit=(-2, 2),
                                   u=0.5)
    img = randomShiftScaleRotate(img,
                                 shift_limit=(-0.2, 0.2),
                                 scale_limit=(-0.2, 0.5),
                                 rotate_limit=(-30, 30),
                                 aspect_limit=(-0.2, 0.2),
                                 u=0.5)
    img = randomHorizontalFlip(img)
    img = randomVerticalFlip(img)
    return img

def train_generator(x_train, y_train, img_dir, batch_size, shuffle=True):
    # x_train = x_train.as_matrix()
    # y_train = y_train.as_matrix()
    y_train = np.eye(55)[y_train]
    batch_index = 0
    n = x_train.shape[0]
    # print("n", n)
    eraser = get_random_eraser(v_h=0.)
    pool = Pool()
    while 1:
        if batch_index == 0:
            index_array = np.arange(n)
            if shuffle:
                index_array = np.random.permutation(n)

        current_index = (batch_index * batch_size) % n
        if n >= current_index + batch_size:
            current_batch_size = batch_size
            batch_index += 1
        else:
            current_batch_size = n - current_index
            batch_index = 0

        batch_id = index_array[current_index: current_index + current_batch_size]

        batch_x = pool.map(load_img,
                           [img_dir + '/{}'.format(x_train[id])
                           for id in batch_id])
        for id in range(len(batch_x)):
            img = batch_x[id]
            img =eraser(img)
            # img =eraser(img)
            # img =eraser(img)
            # img =eraser(img)
            # img =eraser(img)
            batch_x[id] = img
        batch_x = np.array(batch_x, np.float32) / 255

        batch_y = y_train[index_array[current_index: current_index + current_batch_size]]

        # print("batch shape", batch_x.shape, batch_y.shape)

        yield (batch_x, batch_y)

def get_mixer(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3):
    def mixer(img1, img2, mask1, mask2):
        img_h, img_w, img_c = img1.shape
        p_1 = np.random.rand()

        if p_1 > p:
            return img1, mask1

        while True:
            s = np.random.uniform(s_l, s_h) * img_h * img_w
            r = np.random.uniform(r_1, r_2)
            w = int(np.sqrt(s / r))
            h = int(np.sqrt(s * r))
            left = np.random.randint(0, img_w)
            top = np.random.randint(0, img_h)

            if left + w <= img_w and top + h <= img_h:
                break

        img1[top:top + h, left:left + w, :] = img2[top:top + h, left:left + w, :]
        mask1[top:top + h, left:left + w, :] = mask2[top:top + h, left:left + w, :]

        return img1, mask1

    return mixer

def mix_generator(X_train, Y_train, img_dir, batch_size, shuffle=True):
    alpha = 0.2
    gen1 = train_generator(X_train, Y_train, img_dir, batch_size, shuffle)
    gen2 = train_generator(X_train, Y_train, img_dir, batch_size, shuffle)
    while True:
        batch1 = next(gen1)
        batch2 = next(gen2)
        current_batch_size = batch1[0].shape[0]
        l = np.random.beta(alpha, alpha, current_batch_size)
        X_l = l.reshape(current_batch_size, 1, 1, 1)
        Y_l = l.reshape(current_batch_size, 1)

        batch_x = batch1[0] * X_l + batch2[0] * (1 - X_l)
        batch_y = batch1[1] * Y_l + batch2[1] * (1 - Y_l)

        yield (batch_x, batch_y)

def test_generator(x_train, img_dir, batch_size, shuffle=True):
    # x_train = x_train.as_matrix()
    # y_train = y_train.as_matrix()
    batch_index = 0
    n = x_train.shape[0]
    # print("n", n)
    eraser = get_random_eraser(v_h=0.)
    while 1:
        if batch_index == 0:
            index_array = np.arange(n)
            if shuffle:
                index_array = np.random.permutation(n)

        current_index = (batch_index * batch_size) % n
        if n >= current_index + batch_size:
            current_batch_size = batch_size
            batch_index += 1
        else:
            current_batch_size = n - current_index
            batch_index = 0

        batch_x = []
        batch_id = index_array[current_index: current_index + current_batch_size]
        # print(batch_x_base)
        for id in batch_id:
            # print(x_train[0])
            # print(x_train[id])
            # print(img_dir + '/{}'.format(x_train[id]))

            img = cv2.imread(img_dir + '/{}'.format(x_train[id]))
            # print("img shape", img.shape)
            img = cv2.resize(img, (input_size, input_size))
            img = randomHueSaturationValue(img,
                                           hue_shift_limit=(-5, 5),
                                           sat_shift_limit=(-1, 1),
                                           val_shift_limit=(-2, 2),
                                           u=0.5)
            img = randomShiftScaleRotate(img,
                                                shift_limit=(-0.2, 0.2),
                                                scale_limit=(-0.2, 0.2),
                                                rotate_limit=(-30, 30),
                                         aspect_limit = (-0.2, 0.2),
                                         u=0.5)
            img = randomHorizontalFlip(img)
            # img =eraser(img)
            batch_x.append(img)
        batch_x = np.array(batch_x, np.float32) / 255

        # batch_y = y_train[index_array[current_index: current_index + current_batch_size]]

        # print("batch shape", batch_x.shape, batch_y.shape)

        yield batch_x

def load_data(train_path="input/train_master.tsv", test_path="input/sample_submit.tsv"):
    train = pd.read_csv(train_path, delimiter="\t", index_col=False)
    test = pd.read_csv(test_path, delimiter="\t", index_col=False, header=None)
    print("train shape", train.shape)
    print(train.head())
    X_train = train['file_name'].as_matrix()
    y_train = train['category_id'].as_matrix()
    # y_train = np.eye(55)[y_train]
    # print(y_train[:5])
    # print(y_train.shape)
    X_test = test.iloc[:,0]

    return X_train, y_train, X_test

from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit
from sklearn.metrics import log_loss

def train(epochs, seed):
    # parameter
    batch_size = 128
    num_class = 55
    save_path = base_name + "_seed" + str(seed)
    model_path = "_"

    # Load data
    X_train, y_train, X_test = load_data()

    # CV
    ids_train_split, ids_valid_split = train_test_split(np.arange(X_train.shape[0]),
                                                        random_state=42, test_size=0.05,
                                                        stratify=y_train)

    # data process
    X_train_cv = X_train[ids_train_split]
    y_train_cv = y_train[ids_train_split]
    X_holdout = X_train[ids_valid_split]
    Y_holdout = y_train[ids_valid_split]
    # print(X_train_cv.head())

    # define file path and get callbacks
    weight_path = "model/" + save_path + '.hdf5'
    callbacks = get_callbacks(weight_path, patience=16)
    gen = mix_generator(X_train_cv, y_train_cv, "input/train", batch_size)
    gen_val = train_generator(X_holdout, Y_holdout, "input/train", batch_size, shuffle=False)
    gen_val_pred = test_generator(X_holdout, "input/train", batch_size, shuffle=False)
    gen_tst_pred = test_generator(X_test, "input/test", batch_size, shuffle=False)
    model = get_model(num_class)
    model.fit_generator(generator=gen,
                        steps_per_epoch=np.ceil(X_train_cv.shape[0] / batch_size),
                        epochs=epochs,
                        verbose=1,
                        callbacks=callbacks,
                        validation_data=gen_val,
                        validation_steps=np.ceil(X_holdout.shape[0] / batch_size),
                        )

    # Getting the Best Model
    model.load_weights(filepath=weight_path)

    # Getting Training Score
    # score = model.evaluate_generator(generator=gen_trn_eval,
    #                                  steps=np.ceil(X_train.shape[0]/batch_size))
    # print('Train loss:', score[0])
    # print('Train accuracy:', score[1])

    # Getting Valid Score
    score = model.evaluate_generator(generator=gen_val,
                                     steps=np.ceil(X_holdout.shape[0]/batch_size))
    print('Valid loss:', score[0])
    print('Valid accuracy:', score[1])

    # Getting validation prediction
    pred_valid = model.predict_generator(generator=gen_val_pred,
                                         steps=np.ceil(X_holdout.shape[0]/batch_size))

    # Getting Test prediction
    pred_test = model.predict_generator(generator=gen_tst_pred,
                                        steps=np.ceil(X_test.shape[0]/batch_size))

    submission = pd.DataFrame({'id': X_test, 'predict': np.argmax(pred_test, axis=1)})
    submit_path = "output/submission" + save_path + "_val_loss" + str(score[0]) + "_val_acc" + str(score[1]) + ".tsv"
    submission.to_csv(submit_path, index=False, header=False, sep='\t')

    np.save("input/" + base_name + "_valid.npy", pred_valid)
    np.save("input/" + base_name + "_test.npy", pred_test)

def main():
    train(epochs=250, seed=0)

if __name__ == "__main__": main()


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

"""
平安行动自动打卡

请事先安装好 lxml 和 requests 模块

    pip install lxml requests

然后修改 27-31 行为自己的数据，未使用的变量保持原样即可
如有需要请自行配置 149-171 行的 SMTP 发信或 174-177 行的 Server 酱微信提醒

Created on 2020-04-13 20:20
@author: ZhangJiawei & Liu Chongpeng & Liu Lu
"""

import requests
import lxml.html
import re
import json
import random
import time
import smtplib
import traceback

myid = "STUDENTID"
mypass = "PASSWORD"
mybound = "BOUNDFIELDS"
mydata = r'FORMDATA'
# mysckey = "SCKEY"

title = ""
msg = ""

proxies = {"http": None, "https": None}

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "MESSAGE_TICKET=%7B%22times%22%3A0%7D; ",
    "Host": "cas.hrbeu.edu.cn",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"
}

def findStr(source, target):
    return source.find(target) != -1

if __name__ == '__main__':
    try:
        ## 登陆校园网络认证界面
        url_login = 'https://cas.hrbeu.edu.cn/cas/login?'
        print("============================\n[debug] Begin to login ...")
        sesh = requests.session()
        req = sesh.get(url_login, proxies=proxies)
        html_content = req.text
        login_html = lxml.html.fromstring(html_content)
        hidden_inputs = login_html.xpath( r'//div[@id="main"]//input[@type="hidden"]')
        user_form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
        user_form["username"] = myid
        user_form["password"] = mypass
        user_form["captcha"] = ''
        user_form["submit"] = '登 录'
        headers['Cookie'] = headers['Cookie'] + req.headers['Set-cookie']
        req.url = f'https://cas.hrbeu.edu.cn/cas/login'
        response302 = sesh.post(req.url, data=user_form, headers=headers, proxies=proxies)

        ## 进入平安行动界面
        jkgc_response = sesh.get( "http://jkgc.hrbeu.edu.cn/infoplus/form/JSXNYQSBtest/start", proxies=proxies)
        headers['Accept'] = '*/*'
        headers['Cookie'] = jkgc_response.request.headers['Cookie']
        headers['Host'] = 'jkgc.hrbeu.edu.cn'
        headers['Referer'] = jkgc_response.url
        jkgc_html = lxml.html.fromstring(jkgc_response.text)
        csrfToken = jkgc_html.xpath(r'//meta[@itemscope="csrfToken"]')
        csrfToken = csrfToken.pop().attrib["content"]
        jkgc_form = {
            'idc': 'JSXNYQSBtest',
            'release': '',
            'csrfToken': csrfToken,
            'formData': {
                '_VAR_URL': jkgc_response.url,
                '_VAR_URL_Attr': {}
            }
        }
        jkgc_form['formData'] = json.dumps(jkgc_form['formData'])
        jkgc_url = 'http://jkgc.hrbeu.edu.cn/infoplus/interface/start'
        response3 = sesh.post(jkgc_url, data=jkgc_form, headers=headers, proxies=proxies)

        ## 提交平安行动表单
        form_url = json.loads(response3.text)['entities'][0]
        form_response = sesh.get(form_url)
        headers['Accept'] = 'application/json, text/javascript, */*; q=0.01'
        headers['Referer'] = form_url
        headers['X-Requested-With'] = 'XMLHttpRequest'
        submit_url = 'http://jkgc.hrbeu.edu.cn/infoplus/interface/doAction'
        submit_html = lxml.html.fromstring(form_response.text)
        csrfToken2 = submit_html.xpath(r'//meta[@itemscope="csrfToken"]')
        csrfToken2 = csrfToken2.pop().attrib["content"]
        submit_form = {
            'actionId': '1',
            'boundFields': mybound,                    # boundFields 修改位置
            'csrfToken': csrfToken2,
            'formData': mydata,                        # formData 修改位置
            'lang': 'zh',
            'nextUsers': '{}',
            'rand': str(random.random() * 999),
            'remark': '',
            'stepId': re.match(r'.*form/(\d*?)/', form_response.url).group(1),
            'timestamp': str(int(time.time()+0.5))
        }
        response_end = sesh.post(submit_url, data=submit_form, headers=headers, proxies=proxies)
        resJson = json.loads(response_end.text)

        ## 表单填写完成，返回结果
        print('[debug] Form url: ', form_response.url)
        print('[debug] Form Status: ', resJson['ecode'])
        print('[debug] Form stJson: ', resJson)

        ## 生成提醒返回的标题和信息
        if (resJson['errno'] == 0):
            print('[info] Checkin succeed with jsoncode', resJson['ecode'])
            title = f'打卡成功 <{submit_form["stepId"]}>'
            msg = '\t表单地址: ' + form_response.url + '\n\n\t表单状态: \n\t\terrno：' + str(resJson['errno']) + '\n\t\tecode：' + str(
                resJson['ecode']) + '\n\t\tentities：' + str(resJson['entities']) + '\n\n\n\t完整返回：' + response_end.text
        else:
            print('[error] Checkin error with jsoncode', resJson['ecode'])
            title = f'打卡失败！校网出错'
            msg = '\t表单地址: ' + form_response.url + '\n\n\t错误信息: \n\t\terrno：' + str(resJson['errno']) + '\n\t\tecode：' + str(
                resJson['ecode']) + '\n\t\tentities：' + str(resJson['entities']) + '\n\n\n\t完整返回：' + response_end.text
    except:
        print('\n[error] :.:.:.:.: Except return :.:.:.:.:')
        err = traceback.format_exc()
        print('[error] Python Error: \n', err)
        title = '打卡失败！脚本出错'
        msg = '\t脚本报错: \n\n\t' + err + '============================\n'
    finally:
        print(':.:.:.:.: Finally :.:.:.:.:')

        ## 发送邮件
        # from email.mime.text import MIMEText
        # from email.header import Header
        # mail_host = "smtp.qq.com"                      # SMTP 服务器地址
        # mail_user = "sender@example.com"               # SMTP 发信邮箱用户名
        # mail_pass = "emailpassword"                    # SMTP 发信邮箱密码
        # sender = 'sender@example.com'                  # 发信人邮箱，即 SMTP 发信邮箱用户名
        # receivers = ['receiver@example.com']           # 收信人邮箱，多邮箱以数组形式写
        # message = MIMEText(msg, 'plain', 'utf-8')
        # message['From'] = Header("1@example.com", 'utf-8')        # 发信人邮箱，仅用于显示
        # message['To'] =  Header("2@example.com", 'utf-8')         # 收信人邮箱，仅用于显示
        # subject = title
        # message['Subject'] = Header(subject, 'utf-8')
        # try:
        #     smtpObj = smtplib.SMTP_SSL(mail_host)        # Python 3.7 及以上版本 SSL 加密发信
        #     smtpObj.connect(mail_host, 465)              # Python 3.7 及以上版本 加密发信 SMTP 端口号 465
        #     smtpObj.login(mail_user,mail_pass)
        #     smtpObj.sendmail(sender, receivers, message.as_string())
        #     print ("[info] Success: The email was sent successfully")    # 日志输出
        # except smtplib.SMTPException:
        #     print ("[error] Error: Can not send mail")                   # 日志输出

        ## 或者发送 Server 酱的微信提醒
        # wcurl = 'https://sc.ftqq.com/' + mysckey + '.send'
        # wcdata = {'text': title, 'desp': msg}
        # try:
        #     wcresult = requests.post(wcurl, wcdata)
        #     print('[info] Notification sended at', time.strftime("%Y-%m-%d %H:%M:%S %A", time.localtime()))
        # except:
        #     print('[error] Failed to send notification!')

        print('[info] Task Finished at', time.strftime("%Y-%m-%d %H:%M:%S %A", time.localtime()))
        print('============================\n')


text:
from habit.habit_model import HabitHistory
from habit.complete_habit import complete

def test_overdue_habit(datasett):
    """
    please note the 'double tt' for datasett. This stands to differentiate
    the functional test data from the data used for unit tests.
    habit 1 is the overdue habit since its added first in the func/conftest
    module.
    :param datasett: from func/conftest
    :return:
    """
    session = datasett
    complete(1, session)
    result = session.query(HabitHistory.broken_count).\
        filter(HabitHistory.habitid == 1).all()
    assert result == [(1,)]

def test_a_habit_due_for_completion(datasett):
    """
    habit 2 is the due habit since its added second in the func/conftest
    module.
    :param datasett: from func/conftest
    :return:
    """
    session = datasett
    complete(2, session)
    result = session.query(HabitHistory.streak).\
        filter(HabitHistory.habitid == 2).all()
    assert result == [(1,)]


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

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Test cases for the pulse scheduler passes."""

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, schedule
from qiskit.pulse import (Schedule, DriveChannel, AcquireChannel, Acquire,
                          MeasureChannel, MemorySlot)

from qiskit.test.mock import FakeOpenPulse2Q, FakeOpenPulse3Q
from qiskit.test import QiskitTestCase

class TestBasicSchedule(QiskitTestCase):
    """Scheduling tests."""

    def setUp(self):
        self.backend = FakeOpenPulse2Q()
        self.cmd_def = self.backend.defaults().build_cmd_def()

    def test_alap_pass(self):
        """Test ALAP scheduling."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u2(3.14, 1.57, q[0])
        qc.u2(0.5, 0.25, q[1])
        qc.barrier(q[1])
        qc.u2(0.5, 0.25, q[1])
        qc.barrier(q[0], q[1])
        qc.cx(q[0], q[1])
        qc.measure(q, c)
        sched = schedule(qc, self.backend)
        # X pulse on q0 should end at the start of the CNOT
        expected = Schedule(
            (28, self.cmd_def.get('u2', [0], 3.14, 1.57)),
            self.cmd_def.get('u2', [1], 0.5, 0.25),
            (28, self.cmd_def.get('u2', [1], 0.5, 0.25)),
            (56, self.cmd_def.get('cx', [0, 1])),
            (78, self.cmd_def.get('measure', [0, 1])))
        for actual, expected in zip(sched.instructions, expected.instructions):
            self.assertEqual(actual[0], expected[0])
            self.assertEqual(actual[1].command, expected[1].command)
            self.assertEqual(actual[1].channels, expected[1].channels)

    def test_alap_with_barriers(self):
        """Test that ALAP respects barriers on new qubits."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u2(0, 0, q[0])
        qc.barrier(q[0], q[1])
        qc.u2(0, 0, q[1])
        sched = schedule(qc, self.backend, method='alap')
        expected = Schedule(
            self.cmd_def.get('u2', [0], 0, 0),
            (28, self.cmd_def.get('u2', [1], 0, 0)))
        for actual, expected in zip(sched.instructions, expected.instructions):
            self.assertEqual(actual[0], expected[0])
            self.assertEqual(actual[1].command, expected[1].command)
            self.assertEqual(actual[1].channels, expected[1].channels)

    def test_alap_aligns_end(self):
        """Test that ALAP always acts as though there is a final global barrier."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u3(0, 0, 0, q[0])
        qc.u2(0, 0, q[1])
        sched = schedule(qc, self.backend, method='alap')
        expected_sched = Schedule(
            self.cmd_def.get('u2', [1], 0, 0),
            (26, self.cmd_def.get('u3', [0], 0, 0, 0)))
        for actual, expected in zip(sched.instructions, expected_sched.instructions):
            self.assertEqual(actual[0], expected[0])
            self.assertEqual(actual[1].command, expected[1].command)
            self.assertEqual(actual[1].channels, expected[1].channels)
        self.assertEqual(sched.ch_duration(DriveChannel(0)),
                         expected_sched.ch_duration(DriveChannel(1)))

    def test_asap_pass(self):
        """Test ASAP scheduling."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u2(3.14, 1.57, q[0])
        qc.u2(0.5, 0.25, q[1])
        qc.barrier(q[1])
        qc.u2(0.5, 0.25, q[1])
        qc.barrier(q[0], q[1])
        qc.cx(q[0], q[1])
        qc.measure(q, c)
        sched = schedule(qc, self.backend, method="as_soon_as_possible")
        # X pulse on q0 should start at t=0
        expected = Schedule(
            self.cmd_def.get('u2', [0], 3.14, 1.57),
            self.cmd_def.get('u2', [1], 0.5, 0.25),
            (28, self.cmd_def.get('u2', [1], 0.5, 0.25)),
            (56, self.cmd_def.get('cx', [0, 1])),
            (78, self.cmd_def.get('measure', [0, 1])))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_alap_resource_respecting(self):
        """Test that the ALAP pass properly respects busy resources when backwards scheduling.
        For instance, a CX on 0 and 1 followed by an X on only 1 must respect both qubits'
        timeline."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.cx(q[0], q[1])
        qc.u2(0.5, 0.25, q[1])
        sched = schedule(qc, self.backend, method="as_late_as_possible")
        insts = sched.instructions
        self.assertEqual(insts[0][0], 0)
        self.assertEqual(insts[4][0], 22)

        qc = QuantumCircuit(q, c)
        qc.cx(q[0], q[1])
        qc.u2(0.5, 0.25, q[1])
        qc.measure(q, c)
        sched = schedule(qc, self.backend, method="as_late_as_possible")
        self.assertEqual(sched.instructions[-1][0], 50)

    def test_cmd_def_schedules_unaltered(self):
        """Test that forward scheduling doesn't change relative timing with a command."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.cx(q[0], q[1])
        sched1 = schedule(qc, self.backend, method="as_soon_as_possible")
        sched2 = schedule(qc, self.backend, method="as_late_as_possible")
        self.assertEqual(sched1.instructions, sched2.instructions)
        insts = sched1.instructions
        self.assertEqual(insts[0][0], 0)
        self.assertEqual(insts[1][0], 10)
        self.assertEqual(insts[2][0], 20)
        self.assertEqual(insts[3][0], 20)

    def test_measure_combined(self):
        """
        Test to check for measure on the same qubit which generated another measure schedule.

        The measures on different qubits are combined, but measures on the same qubit
        adds another measure to the schedule.
        """
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.u2(3.14, 1.57, q[0])
        qc.cx(q[0], q[1])
        qc.measure(q[0], c[0])
        qc.measure(q[1], c[1])
        qc.measure(q[1], c[1])
        sched = schedule(qc, self.backend, method="as_soon_as_possible")
        expected = Schedule(
            self.cmd_def.get('u2', [0], 3.14, 1.57),
            (28, self.cmd_def.get('cx', [0, 1])),
            (50, self.cmd_def.get('measure', [0, 1])),
            (60, self.cmd_def.get('measure', [0, 1]).filter(channels=[MeasureChannel(1)])),
            (60, Acquire(duration=10)([AcquireChannel(0), AcquireChannel(1)],
                                      [MemorySlot(0), MemorySlot(1)])))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_3q_schedule(self):
        """Test a schedule that was recommended by David McKay :D """
        backend = FakeOpenPulse3Q()
        cmd_def = backend.defaults().build_cmd_def()
        q = QuantumRegister(3)
        c = ClassicalRegister(3)
        qc = QuantumCircuit(q, c)
        qc.cx(q[0], q[1])
        qc.u2(0.778, 0.122, q[2])
        qc.u3(3.14, 1.57, 0., q[0])
        qc.u2(3.14, 1.57, q[1])
        qc.cx(q[1], q[2])
        qc.u2(0.778, 0.122, q[2])
        sched = schedule(qc, backend)
        expected = Schedule(
            cmd_def.get('cx', [0, 1]),
            (22, cmd_def.get('u2', [1], 3.14, 1.57)),
            (46, cmd_def.get('u2', [2], 0.778, 0.122)),
            (50, cmd_def.get('cx', [1, 2])),
            (72, cmd_def.get('u2', [2], 0.778, 0.122)),
            (74, cmd_def.get('u3', [0], 3.14, 1.57)))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_schedule_multi(self):
        """Test scheduling multiple circuits at once."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc0 = QuantumCircuit(q, c)
        qc0.cx(q[0], q[1])
        qc1 = QuantumCircuit(q, c)
        qc1.cx(q[0], q[1])
        schedules = schedule([qc0, qc1], self.backend)
        expected_insts = schedule(qc0, self.backend).instructions
        self.assertEqual(schedules[0].instructions, expected_insts)

    def test_circuit_name_kept(self):
        """Test that the new schedule gets its name from the circuit."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c, name='CIRCNAME')
        qc.cx(q[0], q[1])
        sched = schedule(qc, self.backend, method="asap")
        self.assertEqual(sched.name, qc.name)
        sched = schedule(qc, self.backend, method="alap")
        self.assertEqual(sched.name, qc.name)

    def test_can_add_gates_into_free_space(self):
        """The scheduler does some time bookkeeping to know when qubits are free to be
        scheduled. Make sure this works for qubits that are used in the future. This was
        a bug, uncovered by this example:

           q0 =  - - - - |X|
           q1 = |X| |u2| |X|

        In ALAP scheduling, the next operation on qubit 0 would be added at t=0 rather
        than immediately before the X gate.
        """
        qr = QuantumRegister(2)
        qc = QuantumCircuit(qr)
        for i in range(2):
            qc.u2(0, 0, [qr[i]])
            qc.u1(3.14, [qr[i]])
            qc.u2(0, 0, [qr[i]])
        sched = schedule(qc, self.backend, method="alap")
        expected = Schedule(
            self.cmd_def.get('u2', [0], 0, 0),
            self.cmd_def.get('u2', [1], 0, 0),
            (28, self.cmd_def.get('u1', [0], 3.14)),
            (28, self.cmd_def.get('u1', [1], 3.14)),
            (28, self.cmd_def.get('u2', [0], 0, 0)),
            (28, self.cmd_def.get('u2', [1], 0, 0)))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_barriers_in_middle(self):
        """As a follow on to `test_can_add_gates_into_free_space`, similar issues
        arose for barriers, specifically.
        """
        qr = QuantumRegister(2)
        qc = QuantumCircuit(qr)
        for i in range(2):
            qc.u2(0, 0, [qr[i]])
            qc.barrier(qr[i])
            qc.u1(3.14, [qr[i]])
            qc.barrier(qr[i])
            qc.u2(0, 0, [qr[i]])
        sched = schedule(qc, self.backend, method="alap")
        expected = Schedule(
            self.cmd_def.get('u2', [0], 0, 0),
            self.cmd_def.get('u2', [1], 0, 0),
            (28, self.cmd_def.get('u1', [0], 3.14)),
            (28, self.cmd_def.get('u1', [1], 3.14)),
            (28, self.cmd_def.get('u2', [0], 0, 0)),
            (28, self.cmd_def.get('u2', [1], 0, 0)))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_only_needed_measures(self):
        """Test that `MeasureChannel`s are only added for measured qubits."""
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.measure(q[1], c[1])
        sched_all_channels = schedule(qc, self.backend, method="as_soon_as_possible").channels
        deleted_channels = [MeasureChannel(0)]
        self.assertNotIn(sched_all_channels, deleted_channels)

    def test_user_mapping_for_memslots(self):
        """
        Test that the new schedule only has required `MeasureChannel`s and that the
        `MemorySlot`s are mapped according to the input circuit.
        """
        q = QuantumRegister(2)
        c = ClassicalRegister(2)
        qc = QuantumCircuit(q, c)
        qc.measure(q[0], c[1])
        sched = schedule(qc, self.backend)
        expected = Schedule(
            self.cmd_def.get('measure', [0, 1]).filter(channels=[MeasureChannel(0)]),
            Acquire(duration=10)([AcquireChannel(0), AcquireChannel(1)],
                                 [MemorySlot(1), MemorySlot(0)]))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_user_mapping_for_memslots_3Q(self):
        """Test measuring two of three qubits."""
        backend = FakeOpenPulse3Q()
        cmd_def = backend.defaults().build_cmd_def()
        q = QuantumRegister(3)
        c = ClassicalRegister(3)
        qc = QuantumCircuit(q, c)
        qc.measure(q[1], c[2])
        qc.measure(q[2], c[0])
        sched = schedule(qc, backend)
        expected = Schedule(
            cmd_def.get('measure', [0, 1, 2]).filter(
                channels=[MeasureChannel(1), MeasureChannel(2)]),
            Acquire(duration=10)([AcquireChannel(0), AcquireChannel(1), AcquireChannel(2)],
                                 [MemorySlot(1), MemorySlot(2), MemorySlot(0)]))
        self.assertEqual(sched.instructions, expected.instructions)

    def test_multiple_measure_in_3Q(self):
        """Test multiple measure, user memslot mapping, 3Q."""
        backend = FakeOpenPulse3Q()
        cmd_def = backend.defaults().build_cmd_def()
        q = QuantumRegister(3)
        c = ClassicalRegister(5)
        qc = QuantumCircuit(q, c)
        qc.measure(q[0], c[2])
        qc.measure(q[0], c[4])
        sched = schedule(qc, backend)
        expected = Schedule(
            cmd_def.get('measure', [0, 1, 2]).filter(channels=[MeasureChannel(0)]),
            Acquire(duration=10)([AcquireChannel(0), AcquireChannel(1), AcquireChannel(2)],
                                 [MemorySlot(2), MemorySlot(0), MemorySlot(1)]),
            (10, cmd_def.get('measure', [0, 1, 2]).filter(channels=[MeasureChannel(0)])),
            (10, Acquire(duration=10)([AcquireChannel(0), AcquireChannel(1), AcquireChannel(2)],
                                      [MemorySlot(4), MemorySlot(0), MemorySlot(1)])))
        self.assertEqual(sched.instructions, expected.instructions)


text:
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-15 06:13
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import jsonfield.fields

class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('pinax_teams', '0002_add_simple_models'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Item',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('data', jsonfield.fields.JSONField()),
            ],
        ),
        migrations.CreateModel(
            name='ItemResponse',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(default=django.utils.timezone.now)),
                ('answer', models.TextField()),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oxlos.Item')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='Project',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=250)),
                ('description', models.TextField()),
                ('description_html', models.TextField(blank=True, editable=False)),
                ('team', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pinax_teams.SimpleTeam')),
            ],
        ),
        migrations.CreateModel(
            name='Task',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=250)),
                ('description', models.TextField()),
                ('description_html', models.TextField(blank=True, editable=False)),
                ('instructions', models.TextField()),
                ('instructions_html', models.TextField(blank=True, editable=False)),
                ('question_template', models.TextField()),
                ('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tasks', to='oxlos.Project')),
            ],
        ),
        migrations.AddField(
            model_name='item',
            name='task',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oxlos.Task'),
        ),
    ]


text:
import tensorflow.compat.v1 as tf
import numpy as np

m = 1740

x_batch = np.random.rand(m)
y_batch = np.random.rand(1)

weights = np.random.rand(m)
biases = np.random.rand(m)

with tf.Session() as sess:

    x = tf.placeholder(tf.float32, shape=(m, ), name='x')
    y = tf.placeholder(tf.float32, shape=(1, ), name='y')
    # w = tf.Variable(np.random.rand(m), name='W', dtype=tf.float32)
    # b = tf.Variable(np.random.rand(m), name='b', dtype=tf.float32)
    w = tf.placeholder(tf.float32, shape=(m, ), name='W')
    b = tf.placeholder(tf.float32, shape=(m, ), name='b')

    mu = tf.constant(1, dtype=tf.float32)

    _ = tf.Variable(initial_value=np.random.rand(1))

    h = tf.reduce_sum(tf.multiply(w, x))
    c = tf.multiply(y, h)
    distances = tf.subtract(1., c)
    # maximum = tf.maximum(0., distances)
    #maximum = tf.boolean_mask(distances, tf.greater(0., distances))

    # Look here for gradient of SVM objective function: http://u.cs.biu.ac.il/~jkeshet/teaching/aml2016/sgd_optimization.pdf
    maximum = tf.cast(tf.greater(distances, 0.), tf.float32)

    g = tf.multiply(maximum, x)

    g = tf.multiply(mu, g)
    w = tf.subtract(w, g, name='update')

    sess.run(tf.initialize_all_variables())
    feed_dict = {x: x_batch, y: y_batch, w: weights, b: biases}
    sess.run(w, feed_dict)
    tf.train.Saver().save(sess, 'model.ckpt')


text:
from __future__ import absolute_import

from django.contrib import admin

from . import models

admin.site.register(models.ExampleModel)
admin.site.register(models.ExampleNonUploadModel)


text:
a, b, c = map(int, input().split())

if(a > c and a > b):
    if(b > c):
        print(b)
    else:
        print(c)
elif(b > a and b > c):
    if(a > c):
        print(a)
    else:
        print(c)
else:
    if(a > b):
        print(a)
    else:
        print(b)


text:
import filecmp
import os
from os.path import abspath, dirname, join, exists
import pytest

from image_diet import storage

THIS_DIR = abspath(dirname(__file__))

@pytest.fixture
def dietstorage():
    dietstorage = storage.DietStorage()
    # Filesystem storage parameters
    dietstorage.location = THIS_DIR
    dietstorage.file_permissions_mode = 0o644
    return dietstorage

def test_the_right_storage_has_been_imported():
    from django.core import files

    assert files.storage.FileSystemStorage == storage.STORAGE_CLASS

def test_get_configuration_returns_parsed_configuration():
    config = storage.get_configuration()

    assert config['commands']['fake'] == 'fakecmd'
    assert config['commands']['advpng'] == 'advpng'

def test_mixin_reads_default_configuration():
    mixin = storage.DietMixin()

    assert hasattr(mixin, 'config')
    assert mixin.config['commands']['optipng'] == 'optipng'
    assert mixin.temp_dir == '/tmp'

def test_mixin_also_reads_custom_configuration():
    mixin = storage.DietMixin()

    assert mixin.config['commands']['optipng'] == 'optipng'
    assert mixin.config['commands']['fake'] == 'fakecmd'
    assert mixin.config['notreal'] == 'not a real value'

def test_save_to_temp_copies_content_to_same_named_file_in_temp_directory():
    mixin = storage.DietMixin()

    filename = 'stockholm.jpg'
    path = join(THIS_DIR, 'test_files', 'stockholm.jpg')
    with open(path, 'rb') as f:
        content = f.read()

    tmppath = join(mixin.temp_dir, filename)

    try:
        assert exists(tmppath) is False
        assert mixin.save_to_temp(path, content) == tmppath
        assert exists(tmppath)
        assert filecmp.cmp(path, tmppath)

    finally:
        os.remove(tmppath)

def test_save_method_saves_text_file(dietstorage):
    filename = 'tempfile.txt'
    content = "This file is empty."
    path = join(THIS_DIR, filename)

    tmppath = dietstorage.save_to_temp(path, content)

    new_path = dietstorage.save(path, open(tmppath, 'r'))

    try:
        assert exists(new_path)
        assert open(new_path, 'r').read() == content
        assert not exists(tmppath)
    finally:
        os.remove(new_path)

def prepare_test_files(filename):
    copy_filename = 'copy_' + filename
    tmppath = join('/tmp', filename)
    copypath = join('/tmp', copy_filename)

    path = join(THIS_DIR, 'test_files', filename)
    with open(path, 'rb') as f:
        content = f.read()

    with open(copypath, 'wb') as f:
        f.write(content)

    # Original test_file , working copy (can be changed), internal copy if it
    # exists and content of original file
    return (path, copypath, tmppath, content)

def test_save_method_saves_binary_file(dietstorage):
    path, copypath, tmppath, content = prepare_test_files('stockholm.jpg')

    new_path = dietstorage.save(path, open(copypath, 'rb'))

    try:
        assert exists(new_path)
        assert open(new_path, 'rb').read() == content
        assert not exists(tmppath)
    finally:
        os.remove(new_path)
        os.remove(copypath)

def test_save_method_compresses(dietstorage):
    path, copypath, tmppath, content = prepare_test_files('png_test.png')

    new_path = dietstorage.save(path, open(copypath, 'rb'))

    try:
        assert exists(new_path)
        assert len(open(new_path, 'rb').read()) < len(content)
        assert not exists(tmppath)
    finally:
        os.remove(new_path)
        os.remove(copypath)

def test_logger_logs_errors(caplog, dietstorage):
    # Delete configuration section so that DietException will be raised
    del dietstorage.config['commands']

    filename = 'stockholm.jpg'
    path = join(THIS_DIR, 'test_files', 'stockholm.jpg')

    tmppath = join(dietstorage.temp_dir, filename)

    try:
        dietstorage.save(path, open(path, 'rb'))
    except storage.diet.DietException as e:
        assert not exists(tmppath)
        assert isinstance(e, storage.diet.ConfigurationErrorDietException)

    records = list(caplog.records())
    assert len(records) == 1
    record = records[0]
    assert record.levelname == 'ERROR'
    assert record.message.startswith('Missing key(s) in configuration: ')

def test_storage_logs_file_access_errors(caplog, dietstorage):
    dietstorage.temp_dir = '/doesnotexist'

    filename = 'stockholm.jpg'
    path = join(THIS_DIR, 'test_files', 'stockholm.jpg')

    tmppath = join(dietstorage.temp_dir, filename)

    try:
        dietstorage.save(path, open(path, 'rb'))
    except (OSError, IOError):
        assert not exists(tmppath)

    records = list(caplog.records())
    assert len(records) == 1
    record = records[0]
    assert record.levelname == 'ERROR'
    assert record.message.startswith('Cannot save to temp dir')

def test_save_method_cleans_temp_directory(dietstorage):
    filename = 'stockholm.jpg'
    path = join(THIS_DIR, 'test_files', 'stockholm.jpg')

    tmppath = join(dietstorage.temp_dir, filename)
    new_path = dietstorage.save(path, open(path, 'rb'))

    try:
        assert not exists(tmppath)
    finally:
        os.remove(new_path)


text:
import numpy as np
import numpy.ma as npma
from scipy import stats
import matplotlib.pyplot as plt
import baspy as bp
import fnmatch

"""
Created on Wed Nov 27 18:34 2019

@author: Christine McKenna

========================================================================
Purpose: Plots Supp Fig 2, a pdf of all possible 20-year trends in gsat
         for CMIP6 piControl simulations for each model. First detrends
         the raw gsat time series to remove any long term drift,
         which could bias 20-year trends (e.g. if positive drift,
         pdf of 20-year trends likely biased positive).
         Saves pdf of 20-year trends for models used in Supp Fig 8.
========================================================================
"""

# Required directories
loaddir_CMIP = 'Priestley-Centre/Near_term_warming/analysis_figure_code/'+\
               'SuppFig2/saved_arrays'
savedir = 'Priestley-Centre/Near_term_warming/analysis_figure_code/'+\
          'SuppFig8/saved_data'

### ------ Load in CMIP6 data ------

# Load models
models = np.load(loaddir_CMIP+'/models_gtas_CMIP6_piControl.npy')

# Load catalogue so can extract runids
var = 'tas'
cat_PI = bp.catalogue(dataset='cmip6',Var=var,Experiment='piControl',\
                      CMOR='Amon')
years = np.linspace(1,20,20)

### Process data, one model and RunID at a time
i = 0
fig,axs = plt.subplots(7,7,sharex=True,sharey=True,\
                       figsize=(15,12))
fig.suptitle('PDFs of rolling GSAT trends for 20-year segments of CMIP6 '+\
             'piControl runs',fontsize=20)
axs = axs.ravel()

for model in models:

    ## Get data for model
    filtmod_PI = cat_PI[cat_PI['Model'] == model]

    ## Only keep r1i1p1f?
    runids_PI = np.unique(filtmod_PI['RunID'])
    runids_PI = fnmatch.filter(runids_PI,'r1i1p1f?')

    ## Get data for each RunID
    for runid in runids_PI:

        ## Load gsat data
        gsat_tmp = np.load(loaddir_CMIP+'/gtas_'+model+'_'+runid+\
                           '_CMIP6_piControl.npy')
        ny = len(gsat_tmp)

        ## Remove any drift
        [m,c,_,_,_] = stats.linregress(np.linspace(0,ny-1,ny),gsat_tmp)
        gsat_lin = m*np.linspace(0,ny-1,ny)+c
        gsat = gsat_tmp - gsat_lin

        ## Calculate trends
        gsat_trends = np.zeros([ny-20])
        for y in xrange(0,ny-20):
            [m,_,_,_,_] = stats.linregress(years,gsat[y:y+20])
            gsat_trends[y] = m*10

        ## If model used in Supp Fig 8 save pdf of 20y trends
        if (model == 'BCC-CSM2-MR') or (model == 'MIROC-ES2L'):
            np.save(savedir+'/gsat_20ytrends_CMIP6_piControl_'+\
                    model+'.npy',gsat_trends)

        ### ------ Plot results ------

        ### Plot individual models
        axs[i].hist(gsat_trends,density=True)
        axs[i].set_title(model,fontsize=13)
        axs[i].plot(np.zeros([2]),[0,11],'grey',linewidth=1)
        axs[i].plot(np.ones([2])*(-0.075),[0,11],'black',\
                    linewidth=1,linestyle='--')
        axs[i].plot(np.ones([2])*(0.072),[0,11],'black',\
                    linewidth=1,linestyle='--')
        axs[i].plot(np.ones([2])*(-0.084),[0,11],'black',\
                    linewidth=1,linestyle='--')
        axs[i].plot(np.ones([2])*(0.094),[0,11],'black',\
                    linewidth=1,linestyle='--')
        axs[i].tick_params(labelsize=13)
        i += 1

fig.text(0.5,0.02,'$^{\circ}$C / decade',ha='center',\
         va='center',fontsize=18)
fig.text(0.02,0.5,'Probability density',ha='center',va='center',\
         rotation='vertical',fontsize=18)
axs[i-1].set_xlim([-0.3,0.3])
axs[i-1].set_ylim([0,11])
axs[i].axis('off')
plt.subplots_adjust(top=0.9,bottom=0.07,left=0.07,right=0.97,\
                    wspace=0.17,hspace=0.27)
plt.show()