# -*- coding: utf-8 -*-
import scrapy
import requests
from lxml import etree

def getHTMLTEXT(url):
    try:
        response = requests.get(url, timeout=30, verify=False)
        response.raise_for_status()
        if 'charset' not in response.headers.keys():       # 确保编码方式的正确
            response.encoding = response.apparent_encoding
        return response.text
    except:
        return 'getHTMLTEXT Error!'

class ArchispiderSpider(scrapy.Spider):
    name = 'ArchiSpider'
    allowed_domains = ['http://jzsc.mohurd.gov.cn/dataservice/query/comp/list']

    def start_requests(self):
        pageUrlPrefix = 'https://www.80s.tw/movie/list/-----p'  # 网页url的前缀
        crawlNum = 400
        thisPageMovieLinkPrefix = 'https://www.80s.tw'
        for eachNum in range(crawlNum):
            pageUrl = pageUrlPrefix + str(eachNum+1)
            htmlTEXT = getHTMLTEXT(pageUrl)   # 这里获得了每一页的HTML文本文件
            html = etree.HTML(htmlTEXT)  #利用lxml.etree来解析html文本
            thisPageMovieTags = html.xpath('//*[@id="block3"]/div[3]/ul[2]/li/a')  #这么页面所有电影的链接
            for eachTag in thisPageMovieTags:
                thisPageMovieLinkSuffix = eachTag.attrib['href']   #href仅仅是后缀，所以要拼接
                thisPageMOvieLink = thisPageMovieLinkPrefix + thisPageMovieLinkSuffix
                yield scrapy.Request(url=thisPageMOvieLink, callback=self.parseFunc)

    def parse(self, response):
        pass


import f
n=0
def whichTest():
    n=int(input("Choose one number between 1 and 6: "))
    if n==1:
        f.test1()  #test1 is a square
    elif n==2:
        f.test2()   #test2 is a convex polygon
    elif n==3:
        f.test3()   #test3 is a non-convex polygon
    elif n==4:
        f.test4()   #test4 is a non-connvex polygon
    elif n == 5:
        f.test5()    #test5 is the concatenation and area comparing of test1 and test3
    elif n==6:
        f.test6()    #test6 is the concatenation and area comparing of test2 and test4

whichTest()


#!/usr/bin/env python

# Copyright (c) 2018, Amazon.com, Inc. or its affiliates. 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.
# A copy of the License is located at
#
#  http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file 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 json
import os
import sys
import wave
import traceback
import requests
from boto3 import Session
from botocore.credentials import CredentialProvider, RefreshableCredentials
from botocore.session import get_session
from botocore.exceptions import UnknownServiceError
from contextlib import closing
from optparse import OptionParser

import rospy
from tts.srv import Polly, PollyRequest, PollyResponse

def get_ros_param(param, default=None):
    try:
        key = rospy.search_param(param)
        return default if key is None else rospy.get_param(key, default)
    except Exception as e:
        rospy.logwarn('Failed to get ros param {}, will use default {}. Exception: '.format(param, default, e))
        return default

class AwsIotCredentialProvider(CredentialProvider):
    METHOD = 'aws-iot'
    CANONICAL_NAME = 'customIoTwithCertificate'

    DEFAULT_AUTH_CONNECT_TIMEOUT_MS = 5000
    DEFAULT_AUTH_TOTAL_TIMEOUT_MS = 10000

    def __init__(self):
        super(AwsIotCredentialProvider, self).__init__()
        self.ros_param_prefix = 'iot/'

    def get_param(self, param, default=None):
        return get_ros_param(self.ros_param_prefix + param, default)

    def retrieve_credentials(self):
        try:
            cert_file = self.get_param('certfile')
            key_file = self.get_param('keyfile')
            endpoint = self.get_param('endpoint')
            role_alias = self.get_param('role')
            connect_timeout = self.get_param('connect_timeout_ms', self.DEFAULT_AUTH_CONNECT_TIMEOUT_MS)
            total_timeout = self.get_param('total_timeout_ms', self.DEFAULT_AUTH_TOTAL_TIMEOUT_MS)
            thing_name = self.get_param('thing_name', '')

            if any(v is None for v in (cert_file, key_file, endpoint, role_alias, thing_name)):
                return None

            headers = {'x-amzn-iot-thingname': thing_name} if len(thing_name) > 0 else None
            url = 'https://{}/role-aliases/{}/credentials'.format(endpoint, role_alias)
            timeout = (connect_timeout, total_timeout - connect_timeout)  # see also: urllib3/util/timeout.py

            response = requests.get(url, cert=(cert_file, key_file), headers=headers, timeout=timeout)
            d = response.json()['credentials']

            rospy.loginfo('Credentials expiry time: {}'.format(d['expiration']))

            return {
                'access_key': d['accessKeyId'],
                'secret_key': d['secretAccessKey'],
                'token': d['sessionToken'],
                'expiry_time': d['expiration'],
            }
        except Exception as e:
            rospy.logwarn('Failed to fetch credentials from AWS IoT: {}'.format(e))
            return None

    def load(self):
        return RefreshableCredentials.create_from_metadata(
            self.retrieve_credentials(),
            self.retrieve_credentials,
            'aws-iot-with-certificate'
        )

class AmazonPolly:
    """A TTS engine that can be used in two different ways.

    Usage
    -----

    1. It can run as a ROS service node.

    Start a polly node::

        $ rosrun tts polly_node.py

    Call the service from command line::

        $ rosservice call /polly SynthesizeSpeech 'hello polly' '' '' '' '' '' '' '' '' [] [] 0 '' '' '' '' '' '' false

    Call the service programmatically::

        from tts.srv import Polly
        rospy.wait_for_service('polly')
        polly = rospy.ServiceProxy('polly', Polly)
        res = polly(**kw)

    2. It can also be used as a normal python class::

        AmazonPolly().synthesize(text='hi polly')

    PollyRequest supports many parameters, but the majority of the users can safely ignore most of them and just
    use the vanilla version which involves only one argument, ``text``.

    If in some use cases more control is needed, SSML will come handy. Example::

        AmazonPolly().synthesize(
            text='<speak>Mary has a <amazon:effect name="whispered">little lamb.</amazon:effect></speak>',
            text_type='ssml'
        )

    A user can also control the voice, output format and so on. Example::

        AmazonPolly().synthesize(
            text='<speak>Mary has a <amazon:effect name="whispered">little lamb.</amazon:effect></speak>',
            text_type='ssml',
            voice_id='Joey',
            output_format='mp3',
            output_path='/tmp/blah'
        )

    Parameters
    ----------

    Among the parameters defined in Polly.srv, the following are supported while others are reserved for future.

    * polly_action : currently only ``SynthesizeSpeech`` is supported
    * text : the text to speak
    * text_type : can be either ``text`` (default) or ``ssml``
    * voice_id : any voice id supported by Amazon Polly, default is Joanna
    * output_format : ogg (default), mp3 or pcm
    * output_path : where the audio file is saved
    * sample_rate : default is 16000 for pcm or 22050 for mp3 and ogg

    The following are the reserved ones. Note that ``language_code`` is rarely needed (this may seem counter-intuitive).
    See official Amazon Polly documentation for details (link can be found below).

    * language_code
    * lexicon_content
    * lexicon_name
    * lexicon_names
    * speech_mark_types
    * max_results
    * next_token
    * sns_topic_arn
    * task_id
    * task_status
    * output_s3_bucket_name
    * output_s3_key_prefix
    * include_additional_language_codes

    Links
    -----

    Amazon Polly documentation: https://docs.aws.amazon.com/polly/latest/dg/API_SynthesizeSpeech.html

    """

    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, region_name=None):
        if region_name is None:
            region_name = get_ros_param('aws_client_configuration/region', default='us-west-2')

        self.polly = self._get_polly_client(aws_access_key_id, aws_secret_access_key, aws_session_token, region_name)
        self.default_text_type = 'text'
        self.default_voice_id = 'Joanna'
        self.default_output_format = 'ogg_vorbis'
        self.default_output_folder = '.'
        self.default_output_file_basename = 'output'

    def _get_polly_client(self, aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None,
                          region_name=None, with_service_model_patch=False):
        """Note we get a new botocore session each time this function is called.
        This is to avoid potential problems caused by inner state of the session.
        """
        botocore_session = get_session()

        if with_service_model_patch:
            # Older versions of botocore don't have polly. We can possibly fix it by appending
            # extra path with polly service model files to the search path.
            current_dir = os.path.dirname(os.path.abspath(__file__))
            service_model_path = os.path.join(current_dir, 'data', 'models')
            botocore_session.set_config_variable('data_path', service_model_path)
            rospy.loginfo('patching service model data path: {}'.format(service_model_path))

        botocore_session.get_component('credential_provider').insert_after('boto-config', AwsIotCredentialProvider())

        botocore_session.user_agent_extra = self._generate_user_agent_suffix()

        session = Session(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key,
                          aws_session_token=aws_session_token, region_name=region_name,
                          botocore_session=botocore_session)

        try:
            return session.client("polly")
        except UnknownServiceError:
            # the first time we reach here, we try to fix the problem
            if not with_service_model_patch:
                return self._get_polly_client(aws_access_key_id, aws_secret_access_key, aws_session_token, region_name,
                                              with_service_model_patch=True)
            else:
                # we have tried our best, time to panic
                rospy.logerr('Amazon Polly is not available. Please install the latest boto3.')
                raise

    def _generate_user_agent_suffix(self):
        exec_env = get_ros_param('exec_env', 'AWS_RoboMaker').strip()
        if 'AWS_RoboMaker' in exec_env:
            ver = get_ros_param('robomaker_version', None)
            if ver:
                exec_env += '-' + ver.strip()
        ros_distro = get_ros_param('rosdistro', 'Unknown_ROS_DISTRO').strip()
        ros_version = get_ros_param('rosversion', 'Unknown_ROS_VERSION').strip()
        return 'exec-env/{} ros-{}/{}'.format(exec_env, ros_distro, ros_version)

    def _pcm2wav(self, audio_data, wav_filename, sample_rate):
        """per Amazon Polly official doc, the pcm in a signed 16-bit, 1 channel (mono), little-endian format."""
        wavf = wave.open(wav_filename, 'w')
        wavf.setframerate(int(sample_rate))
        wavf.setnchannels(1)  # 1 channel
        wavf.setsampwidth(2)  # 2 bytes == 16 bits
        wavf.writeframes(audio_data)
        wavf.close()

    def _make_audio_file_fullpath(self, output_path, output_format):
        """Makes a full path for audio file based on given output path and format.

        If ``output_path`` doesn't have a path, current path is used.

        :param output_path: the output path received
        :param output_format: the audio format, e.g., mp3, ogg_vorbis, pcm
        :return: a full path for the output audio file. File ext will be constructed from audio format.
        """
        head, tail = os.path.split(output_path)
        if not head:
            head = self.default_output_folder
        if not tail:
            tail = self.default_output_file_basename

        file_ext = {'pcm': '.wav', 'mp3': '.mp3', 'ogg_vorbis': '.ogg'}[output_format.lower()]
        if not tail.endswith(file_ext):
            tail += file_ext

        return os.path.realpath(os.path.join(head, tail))

    def _synthesize_speech_and_save(self, request):
        """Calls Amazon Polly and writes the returned audio data to a local file.

        To make it practical, three things will be returned in a JSON form string, which are audio file path,
        audio type and Amazon Polly response metadata.

        If the Amazon Polly call fails, audio file name will be an empty string and audio type will be "N/A".

        Please see https://boto3.readthedocs.io/reference/services/polly.html#Polly.Client.synthesize_speech
        for more details on Amazon Polly API.

        :param request: an instance of PollyRequest
        :return: a string in JSON form with two attributes, "Audio File" and "Amazon Polly Response".
        """
        kws = {
            'LexiconNames': request.lexicon_names if request.lexicon_names else [],
            'OutputFormat': request.output_format if request.output_format else self.default_output_format,
            'SampleRate': request.sample_rate,
            'SpeechMarkTypes': request.speech_mark_types if request.speech_mark_types else [],
            'Text': request.text,
            'TextType': request.text_type if request.text_type else self.default_text_type,
            'VoiceId': request.voice_id if request.voice_id else self.default_voice_id
        }

        if not kws['SampleRate']:
            kws['SampleRate'] = '16000' if kws['OutputFormat'].lower() == 'pcm' else '22050'

        rospy.loginfo('Amazon Polly Request: {}'.format(kws))
        response = self.polly.synthesize_speech(**kws)
        rospy.loginfo('Amazon Polly Response: {}'.format(response))

        if "AudioStream" in response:
            audiofile = self._make_audio_file_fullpath(request.output_path, kws['OutputFormat'])
            rospy.loginfo('will save audio as {}'.format(audiofile))

            with closing(response["AudioStream"]) as stream:
                if kws['OutputFormat'].lower() == 'pcm':
                    self._pcm2wav(stream.read(), audiofile, kws['SampleRate'])
                else:
                    with open(audiofile, "wb") as f:
                        f.write(stream.read())

            audiotype = response['ContentType']
        else:
            audiofile = ''
            audiotype = 'N/A'

        return json.dumps({
            'Audio File': audiofile,
            'Audio Type': audiotype,
            'Amazon Polly Response Metadata': str(response['ResponseMetadata'])
        })

    def _dispatch(self, request):
        """Amazon Polly supports a number of APIs. This will call the right one based on the content of request.

        Currently "SynthesizeSpeech" is the only recognized action. Basically this method just delegates the work
        to ``self._synthesize_speech_and_save`` and returns the result as is. It will simply raise if a different
        action is passed in.

        :param request: an instance of PollyRequest
        :return: whatever returned by the delegate
        """
        actions = {
            'SynthesizeSpeech': self._synthesize_speech_and_save
            # ... more actions could go in here ...
        }

        if request.polly_action not in actions:
            raise RuntimeError('bad or unsupported Amazon Polly action: "' + request.polly_action + '".')

        return actions[request.polly_action](request)

    def _node_request_handler(self, request):
        """The callback function for processing service request.

        It never raises. If anything unexpected happens, it will return a PollyResponse with details of the exception.

        :param request: an instance of PollyRequest
        :return: a PollyResponse
        """
        rospy.loginfo('Amazon Polly Request: {}'.format(request))

        try:
            response = self._dispatch(request)
            rospy.loginfo('will return {}'.format(response))
            return PollyResponse(result=response)
        except Exception as e:
            current_dir = os.path.dirname(os.path.abspath(__file__))
            exc_type = sys.exc_info()[0]

            # not using `issubclass(exc_type, ConnectionError)` for the condition below because some versions
            # of urllib3 raises exception when doing `from requests.exceptions import ConnectionError`
            error_ogg_filename = 'connerror.ogg' if 'ConnectionError' in exc_type.__name__ else 'error.ogg'

            error_details = {
                'Audio File': os.path.join(current_dir, 'data', error_ogg_filename),
                'Audio Type': 'ogg',
                'Exception': {
                    'Type': str(exc_type),
                    'Module': exc_type.__module__,
                    'Name': exc_type.__name__,
                    'Value': str(e),
                },
                'Traceback': traceback.format_exc()
            }

            error_str = json.dumps(error_details)
            rospy.logerr(error_str)
            return PollyResponse(result=error_str)

    def synthesize(self, **kws):
        """Call this method if you want to use polly but don't want to start a node.

        :param kws: input as defined in Polly.srv
        :return: a string in JSON form with detailed information, success or failure
        """
        req = PollyRequest(polly_action='SynthesizeSpeech', **kws)
        return self._node_request_handler(req)

    def start(self, node_name='polly_node', service_name='polly'):
        """The entry point of a ROS service node.

        Details of the service API can be found in Polly.srv.

        :param node_name: name of ROS node
        :param service_name:  name of ROS service
        :return: it doesn't return
        """
        rospy.init_node(node_name)

        service = rospy.Service(service_name, Polly, self._node_request_handler)

        rospy.loginfo('polly running: {}'.format(service.uri))

        rospy.spin()

def main():
    usage = '''usage: %prog [options]
    '''

    parser = OptionParser(usage)

    parser.add_option("-n", "--node-name", dest="node_name", default='polly_node',
                      help="name of the ROS node",
                      metavar="NODE_NAME")
    parser.add_option("-s", "--service-name", dest="service_name", default='polly',
                      help="name of the ROS service",
                      metavar="SERVICE_NAME")

    (options, args) = parser.parse_args()

    node_name = options.node_name
    service_name = options.service_name

    AmazonPolly().start(node_name=node_name, service_name=service_name)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
    Methods for solving
      $ \min (x-a)^2+gamma*(f(x)-b)^2 $
    in closed form for various choices of the non-linear neuron response "f".
    @author: Tom
"""
import numexpr as ne
import numpy as np

from . import lookup_table as lut
from utility.serializable import SerializableObject

class AbstractNonlinearity(SerializableObject):
    def func(self, x):
        """
        :param x: A numpy Array of double's
        :return:  An entry-wise function of the inputs representing a nueron's response curve
        """
        raise Exception("Unimplemented Method:  AbstractFitter.func()")
    def fit(self, a, b, gamma):
        """
        :param a: A numpy Array of double's
        :param b: A numpy Array of double's
        :param gamma: A scalar double
        :return: Solution to:    \argmin || x-A ||^2+gamma|| f(x)-B ||^2
        """
        raise Exception("Unimplemented Method:  AbstractFitter.fit()")
    def der(self, x):
        """ The entry-wise derivative of self.func()  """
        raise Exception("Unimplemented Method:  AbstractFitter.der()")

class ReluNonlinearity(AbstractNonlinearity):
    def func(self, x):
        return np.maximum(x, 0)  # define the relu function
    def fit(self, a, b, gamma):
        orig_shp = a.shape
        # x = [ (a + gamma * b) / (1 + gamma), a, np.zeros_like(a) ]
        x = np.array([
            (a + gamma * b) / (1 + gamma),
            a,
            np.zeros_like(a),
        ]).reshape((3,-1))
        a = a.ravel()
        b = b.ravel()
        objs = ne.evaluate("(x-a)**2 + gamma * (where(x>0,x,0)-b)**2")
        idx = [objs.argmin(axis=0)] + [range(s) for s in objs.shape[1:]]
        return x[idx].reshape(orig_shp)
    def der(self, x):
        raise Exception("This function is not differentiable")

class SmoothReluNonlinearity(AbstractNonlinearity):
    def func(self, x):
        return np.log(1+np.exp(x))  # define the relu function
    def der(self, x):
        return np.exp(x)/(1+np.exp(x))

class NonDiffSigNonlinearity(AbstractNonlinearity):
    def func(self, x):
        return np.maximum(np.minimum(x, 1), 0)
    def fit(self, a, b, gamma):
        slantSol =  np.maximum(np.minimum((a+gamma*b)/(1+gamma), 1), 0) #  The argmin on the slanted part of the curve
        # NOTE:  if optimum is on flat part of curve, then solution is just "a"
        objSlanted = (slantSol-a)**2+gamma*(slantSol-b)**2  #  evaluate min objective on slated part
        objFlat = gamma*(np.maximum(np.minimum(a, 1), 0)-b)**2 #  evaluate min objective on flat part

        #  Figure out the entries of "a" where solution lies on slated part.  Everywhere else, the solution is just "a"
        indexWhereMinLiesOnSlated = np.greater(objFlat, objSlanted)
        #  Make sure we're dealing with arrays.  Otherwise, indexing won't work
        indexWhereMinLiesOnSlated = np.asarray(indexWhereMinLiesOnSlated)
        sol = np.copy(a)
        slantSol = np.asarray(slantSol)
        #  Make solution vector
        sol[indexWhereMinLiesOnSlated] = slantSol[indexWhereMinLiesOnSlated]
        return sol
    def der(self, x):
        raise Exception("This function is not differentiable")

#  A non-linearity that uses a hinge function penalty for final output layer
class SvmDataLink(AbstractNonlinearity):
    def __init__(self, zeroOneLabels):
        self.labels = zeroOneLabels==1
        self.relu = ReluNonlinearity()
    def func(self, x):
        if not self.labels.shape==x.shape:
            return None
        rval = np.maximum(x, 0)
        rval[self.labels] = np.minimum(x[self.labels], 1)
        return rval
    def fit(self, a, b, gamma):
        rval = self.relu.fit(a, b, gamma)
        rval[self.labels] = 1-self.relu.fit(1-a[self.labels], 1-b[self.labels], gamma)
        return rval
    def der(self, x):
        raise Exception("This function is not differentiable")

#  A pure linear function with unit slope.  This is for testing a debugging purposes
class LinearLink(AbstractNonlinearity):
    def func(self, x):
        return x
    def fit(self, a, b, gamma):
        return (a+gamma*b)/(1+gamma)
    def der(self, x):
        raise np.ones(x.shape)
    def __str__(self):
        return "Linear Link"

#================= UNIT TESTS ===================#

import unittest

class TestFitters(unittest.TestCase):

    def test_non_diff_sigmoid(self):
        self.specific_nds_minimizer_test(NonDiffSigNonlinearity(), gamma = .2)
        self.specific_nds_minimizer_test(NonDiffSigNonlinearity(), gamma = 1.0)
        self.specific_nds_minimizer_test(NonDiffSigNonlinearity(), gamma = 20.0)

    def test_relu(self):
        self.specific_nds_minimizer_test(ReluNonlinearity(), gamma = .2)
        self.specific_nds_minimizer_test(ReluNonlinearity(), gamma = 1.0)
        self.specific_nds_minimizer_test(ReluNonlinearity(), gamma = 20.0)

    def specific_nds_minimizer_test(self, fitter, gamma, testSize=(7,8,9) ):
        """
        Test the solution returned by the 'fit' method is at least as good as the (highly accurate) non-convex numerical solver
        """
        print("\nTest optimality for method: ", fitter)

        obj = lambda x, a, b: (x-a)**2+gamma*(fitter.func(x)-b)**2

        # Generate random inputs a,b
        print("\tBuilding random test problem of size %s"%(testSize,))
        a = np.random.randn(*testSize) * 5
        a[a>10] = 10
        a[a<-10] = -10
        b = np.random.randn(*testSize) * 5

        #  Get analytical solution
        print("\tComputing analytical solution")
        analyticalSol = fitter.fit(a, b, gamma) #  x.shape is same as a and b's shape
        self.assertTrue(analyticalSol.shape == a.shape)
        # Compute numerical solution
        print("\tComputing numerical solution.  Will take about 5 seconds.")
        solver = lambda a, b: lut.minimize(lambda x: obj(x, a, b), -10, 10)
        solver = np.vectorize(solver)
        numericalSol  = solver(a, b)

        # Verfiy that analytical sol is at least as good as numerical
        print("\tChecking Results")
        numObj = obj(numericalSol, a, b)
        anObj = obj(analyticalSol, a, b)
        isSolution = np.greater_equal( numObj, anObj-1e-8 )

        failures = np.logical_not(isSolution)
        if not np.alltrue(isSolution):
            print("\tTest FAILED")
            print("\t\tfailures (a,b) = ", zip(a[failures], b[failures]))
            print("\t\t(num, ana) solution = ", zip(numericalSol[failures], analyticalSol[failures]))
            print("\t\t(num, ana) objective = ", zip(numObj[failures], anObj[failures]))
        self.assertTrue( np.alltrue( isSolution  ) )
        print("\tTest Passed\n")
if __name__ == '__main__':
    pass

    # print 'Profiling slow relu fitter...'
    # profile_fitter(get_fitter_relu_slow, ntrials=10000)
    # print 'Profiling fast relu fitter...'
    # profile_fitter(get_fitter_relu_fast, ntrials=10000)
    # print 'Profiling numexpr relu fitter...'
    # profile_fitter(get_fitter_relu_numexpr, ntrials=10000)


class Solution(object):

    def findRepeatedDnaSequences(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        set_ = set()
        ans = set()
        for i in range(len(s)-9):
            sub = s[i: i+10]
            if sub in set_:
                ans.add(sub)
            else:
                set_.add(sub)
        return list(ans)

s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
s = "AAAAAAAAAAA"
s = "AAAAAAAAAAAA"
test = Solution()
print(test.findRepeatedDnaSequences(s))


import webapp2
from datetime import datetime, timedelta
from models.dao.tweet import Tweet

class TweetsController(webapp2.RequestHandler):
    def get(self):
        now = datetime.now()
        hours = int(self.request.get("hours", "1"))
        timestamp = datetime(now.year, now.month, now.day, now.hour) - timedelta(hours=hours)
        tweets = Tweet.get_tweets_after(timestamp)
        self.response.headers['Content-Type'] = "application/json"
        self.response.out.write(Tweet.list_to_json(tweets))


#!/usr/bin/env python3
"""Project Euler, Problem 62."""
from collections import defaultdict

N = 5

def sorted_digits(n):
    """Return the digits of n in sorted order."""
    return str(sorted(str(n)))

if __name__ == '__main__':
    counter = defaultdict(lambda: set())
    n = 0
    while True:
        n += 1
        key = sorted_digits(n ** 3)
        counter[key].add(n ** 3)

        if len(counter[key]) == N:
            break

    print(min(counter[key]))


import random as rand;
f1=open("dfp_in_shenlan_bla_ext.csv");
f2=open("dfp_not_in_shenlan_bla_ext.csv");
fo_1=open('datax.csv','w');
fo_2=open('datay.csv','w');
ones=[];
for line in f1:
	cols=line.split(',');
	ones.append((cols[0],cols[1],cols[2],'1'));
zeros=[];
for line in f2:
	cols=line.split(',');
	zeros.append((cols[0],cols[1],cols[2],'0'));
rand.shuffle(zeros);
zeros_new=[];
for x in range(1500):
	zeros_new.append(zeros[x]);
for tup in ones:
	print >> fo_1 , '%s,%s,%s' %(tup[0],tup[1],tup[2]);
	print >> fo_2 , tup[3];
for tup in zeros_new:
	print >> fo_1 , '%s,%s,%s' %(tup[0],tup[1],tup[2]);
	print >> fo_2 , tup[3];


import pygame
import  random
import sys
#sys.path.insert(0, 'G:/Mi unidad/Computación Gráfica')
#from graphic_repository import *
GREEN=[0,255,255]
YELLOW=[250,250,0]
BLACK=[0,0,0]
WIDTH=480
HEIGHT=480
# Pasos:
# crear clase, luego grupos de sprite, el objeto,
# mostrar objeto y actualizarlos

class Player(pygame.sprite.Sprite):
    """Clase jugador"""
    def __init__(self, point):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([40, 50])
        self.image.fill(GREEN)
        # rect posiciona el objeto
        self.rect = self.image.get_rect()
        self.rect.x = point[0]
        self.rect.y = point[1]
        self.velx = 0 # atributo velocidad
        self.vely = 0
        self.sound = pygame.mixer.Sound('Wilhelm_Scream.ogg')

    def update(self):
        self.rect.x += self.velx    # mueve el objeto
        self.rect.y += self.vely

class Block(pygame.sprite.Sprite):
    """Clase jugador"""
    def __init__(self, point, dimensions):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface(dimensions)
        self.image.fill(YELLOW)
        # rect posiciona el objeto
        self.rect = self.image.get_rect()
        self.rect.x = point[0]
        self.rect.y = point[1]

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode([WIDTH, HEIGHT])
    end = False
    end_game = False
    clock = pygame.time.Clock()
    points = 0
    bgm = pygame.mixer.Sound('Common Fight.ogg')
    cont_cuadros = 0
    tasa_cuadros = 60
    # GRUPOS -------------------------------------------------
    players = pygame.sprite.Group() # players es de tipo Group
    blocks = pygame.sprite.Group()
    # --------------------------------------------------------

    p = Player([100, 150]) # instancia la clase
    players.add(p) # aniade p al grupo de jugadores

    b = Block([300, 150], [100, 100])
    blocks.add(b)
    b1 = Block([400, 300], [100, 50])
    blocks.add(b1)
    bgm.play(-1)   # -1 to repeat indefinitely
    while not end and not end_game:
        segundo = cont_cuadros // tasa_cuadros
        print(segundo)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                end = True
            # TECLADO --------------------------------------------------------
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    end = True
                if event.key == pygame.K_RIGHT:
                    p.velx = 5
                    p.vely = 0
                if event.key == pygame.K_LEFT:
                    p.velx = -5
                    p.vely = 0
                if event.key == pygame.K_UP:
                    p.velx = 0
                    p.vely = -5
                if event.key == pygame.K_DOWN:
                    p.velx = 0
                    p.vely = 5
            # -----------------------------------------------------------------

        # CONTROL DE ELEMENTOS
        # --------------------------------------------------------------------------

        for p in players:
            if p.rect.x < 0:
                p.rect.x = 0
            if p.rect.x > (WIDTH - p.rect.width):
                p.rect.x = WIDTH - p.rect.width

        lc = pygame.sprite.spritecollide(p, blocks, False)
        for b in lc:
            p.sound.play()
            # no deja que el jugador se super-ponga con el bloque
            if p.rect.right > b.rect.left and p.velx > 0:
                p.rect.right = b.rect.left
                p.velx = 0
            if p.rect.left < b.rect.right and p.velx < 0:
                p.rect.left = b.rect.right
                p.velx = 0
            if p.rect.bottom > b.rect.top and p.vely > 0:
                p.rect.bottom = b.rect.top
                p.vely = 0
            if p.rect.top < b.rect.bottom and p.vely < 0:
                p.rect.top = b.rect.bottom
                p.vely = 0

        players.update() # cada vez q haya update se aumentara la velx
        # blocks.update()

        # Regresco de pantalla
        screen.fill(BLACK)

        players.draw(screen) # dibuja los elementos de players
        blocks.draw(screen)
        pygame.display.flip()
        clock.tick(tasa_cuadros)
        cont_cuadros += 1


from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import numpy as np

pca = PCA(n_components=2)

df_train = pd.read_csv('./dataset/pfm_train.csv')
df_test = pd.read_csv('./dataset/pfm_test.csv')

# 前文分析过，两个变量方差为0，可以删除。
# EmployeeNumber是唯一识别号码，删除
df_train.drop(
    ['Over18', 'StandardHours', 'EmployeeNumber'], axis=1, inplace=True)
df_test.drop(
    ['Over18', 'StandardHours', 'EmployeeNumber'], axis=1, inplace=True)

# 预测变量
target_var = 'Attrition'

# 连续变量
continuous_var = [
    'Age', 'MonthlyIncome', 'TotalWorkingYears', 'YearsAtCompany',
    'YearsInCurrentRole', 'YearsSinceLastPromotion', 'YearsWithCurrManager'
]

scaler = MinMaxScaler()

df_train[continuous_var] = scaler.fit_transform(np.log(df_train[continuous_var]+0.001))

pca_result = pca.fit_transform(df_train[continuous_var])
##print(pca.components_)
#print(pca_result)
label = df_train[target_var].map({0:'red', 1:'green'})

first_pc = pca.components_[0]
second_pc = pca.components_[1]

fig = plt.figure()
for ii, jj in pca_result:
    plt.scatter(first_pc[0]*ii[0], first_pc[1]*ii[0], c='r')
    plt.scatter(second_pc[0]*ii[1], second_pc[1]*ii[1], c='c')
    #plt.scatter(jj[0], jj[1], c='b')
    plt.scatter(ii[0], ii[1], c='b')

plt.show()
#fig = plt.figure()
#for x in range(len(pca_result)):
#    plt.scatter(pca_result[x][0], pca_result[x][1], alpha=0.7, c=label[x], edgecolors='white')
#plt.show()


#!/usr/bin/env python

def main():

    # Ask the user to enter a starting number
    # The user must not enter a starting number less than 1
    # Create checks and error messages for the above
    while True:
        user_starting = input("Please enter a starting integer :")
        if user_starting.isdigit():
            if int(user_starting) >= 1:
                break
            else:
                print ("The integer should be greater than 1, please try again.")
        else:
            print ("That is not an integer, please try again.")

    # Ask the user to enter an ending number
    # The user must enter an ending number at least 5 times greater than the starting number
    # Create checks and error messages for the above
    while True:
        user_ending = input("Please enter an ending integer, which must be at least 5 times greater than the starting integer :")
        if user_ending.isdigit():
            if int(user_ending) >= (int(user_starting) * 5):
                break
            else:
                print ("The integer should be greater than {}, please try again.".format(int(user_starting)*5))
        else:
            print ("That is not an integer, please try again.")

    # Create a list of integers in the range of the user's starting and ending numbers
    user_list = list(range(int(user_starting),int(user_ending)))
    running_total = 0

    # Print out the number and index of each item in the list that is even, sum all the odd numbers in the list using a for loop
    # I thought I'd do it all in the same for loop...
    for x in enumerate(user_list):
        if x[1] % 2 == 0:
            print ("The number {} is at index {} in the list.".format(x[1],x[0]))
        else:
            running_total += x[1]

    # Print out the cumulative sum calculated above (running total)
    print ("The sum of the odd numbers in your list is {}.".format(running_total))

# Wrap your steps in a function and call the function
main()


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

from django.test import TestCase

from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError

from build.models import Build, BuildItem
from stock.models import StockItem
from part.models import Part, BomItem
from InvenTree import status_codes as status

class BuildTest(TestCase):
    """
    Run some tests to ensure that the Build model is working properly.
    """

    def setUp(self):
        """
        Initialize data to use for these tests.
        """

        # Create a base "Part"
        self.assembly = Part.objects.create(
            name="An assembled part",
            description="Why does it matter what my description is?",
            assembly=True,
            trackable=True,
        )

        self.sub_part_1 = Part.objects.create(
            name="Widget A",
            description="A widget",
            component=True
        )

        self.sub_part_2 = Part.objects.create(
            name="Widget B",
            description="A widget",
            component=True
        )

        # Create BOM item links for the parts
        BomItem.objects.create(
            part=self.assembly,
            sub_part=self.sub_part_1,
            quantity=10
        )

        BomItem.objects.create(
            part=self.assembly,
            sub_part=self.sub_part_2,
            quantity=25
        )

        # Create a "Build" object to make 10x objects
        self.build = Build.objects.create(
            title="This is a build",
            part=self.assembly,
            quantity=10
        )

        # Create some build output (StockItem) objects
        self.output_1 = StockItem.objects.create(
            part=self.assembly,
            quantity=5,
            is_building=True,
            build=self.build
        )

        self.output_2 = StockItem.objects.create(
            part=self.assembly,
            quantity=5,
            is_building=True,
            build=self.build,
        )

        # Create some stock items to assign to the build
        self.stock_1_1 = StockItem.objects.create(part=self.sub_part_1, quantity=1000)
        self.stock_1_2 = StockItem.objects.create(part=self.sub_part_1, quantity=100)

        self.stock_2_1 = StockItem.objects.create(part=self.sub_part_2, quantity=5000)

    def test_init(self):
        # Perform some basic tests before we start the ball rolling

        self.assertEqual(StockItem.objects.count(), 5)

        # Build is PENDING
        self.assertEqual(self.build.status, status.BuildStatus.PENDING)

        # Build has two build outputs
        self.assertEqual(self.build.output_count, 2)

        # None of the build outputs have been completed
        for output in self.build.get_build_outputs().all():
            self.assertFalse(self.build.isFullyAllocated(output))

        self.assertFalse(self.build.isPartFullyAllocated(self.sub_part_1, self.output_1))
        self.assertFalse(self.build.isPartFullyAllocated(self.sub_part_2, self.output_2))

        self.assertEqual(self.build.unallocatedQuantity(self.sub_part_1, self.output_1), 50)
        self.assertEqual(self.build.unallocatedQuantity(self.sub_part_1, self.output_2), 50)
        self.assertEqual(self.build.unallocatedQuantity(self.sub_part_2, self.output_1), 125)
        self.assertEqual(self.build.unallocatedQuantity(self.sub_part_2, self.output_2), 125)

        self.assertFalse(self.build.is_complete)

    def test_build_item_clean(self):
        # Ensure that dodgy BuildItem objects cannot be created

        stock = StockItem.objects.create(part=self.assembly, quantity=99)

        # Create a BuiltItem which points to an invalid StockItem
        b = BuildItem(stock_item=stock, build=self.build, quantity=10)

        with self.assertRaises(ValidationError):
            b.save()

        # Create a BuildItem which has too much stock assigned
        b = BuildItem(stock_item=self.stock_1_1, build=self.build, quantity=9999999)

        with self.assertRaises(ValidationError):
            b.clean()

        # Negative stock? Not on my watch!
        b = BuildItem(stock_item=self.stock_1_1, build=self.build, quantity=-99)

        with self.assertRaises(ValidationError):
            b.clean()

        # Ok, what about we make one that does *not* fail?
        b = BuildItem(stock_item=self.stock_1_1, build=self.build, install_into=self.output_1, quantity=10)
        b.save()

    def test_duplicate_bom_line(self):
        # Try to add a duplicate BOM item - it should fail!

        with self.assertRaises(IntegrityError):
            BomItem.objects.create(
                part=self.assembly,
                sub_part=self.sub_part_1,
                quantity=99
            )

    def allocate_stock(self, q11, q12, q21, output):
        # Assign stock to this build

        if q11 > 0:
            BuildItem.objects.create(
                build=self.build,
                stock_item=self.stock_1_1,
                quantity=q11,
                install_into=output
            )

        if q12 > 0:
            BuildItem.objects.create(
                build=self.build,
                stock_item=self.stock_1_2,
                quantity=q12,
                install_into=output
            )

        if q21 > 0:
            BuildItem.objects.create(
                build=self.build,
                stock_item=self.stock_2_1,
                quantity=q21,
                install_into=output,
            )

            # Attempt to create another identical BuildItem
            b = BuildItem(
                build=self.build,
                stock_item=self.stock_2_1,
                quantity=q21
            )

            with self.assertRaises(ValidationError):
                b.clean()

    def test_partial_allocation(self):
        """
        Partially allocate against output 1
        """

        self.allocate_stock(50, 50, 200, self.output_1)

        self.assertTrue(self.build.isFullyAllocated(self.output_1))
        self.assertFalse(self.build.isFullyAllocated(self.output_2))
        self.assertTrue(self.build.isPartFullyAllocated(self.sub_part_1, self.output_1))
        self.assertTrue(self.build.isPartFullyAllocated(self.sub_part_2, self.output_1))

        self.assertFalse(self.build.isPartFullyAllocated(self.sub_part_1, self.output_2))
        self.assertFalse(self.build.isPartFullyAllocated(self.sub_part_2, self.output_2))

        # Check that the part has been allocated
        self.assertEqual(self.build.allocatedQuantity(self.sub_part_1, self.output_1), 100)

        self.build.unallocateStock(output=self.output_1)
        self.assertEqual(BuildItem.objects.count(), 0)

        # Check that the part has been unallocated
        self.assertEqual(self.build.allocatedQuantity(self.sub_part_1, self.output_1), 0)

    def test_auto_allocate(self):
        """
        Test auto-allocation functionality against the build outputs
        """

        allocations = self.build.getAutoAllocations(self.output_1)

        self.assertEqual(len(allocations), 1)

        self.build.autoAllocate(self.output_1)
        self.assertEqual(BuildItem.objects.count(), 1)

        # Check that one part has been fully allocated to the build output
        self.assertTrue(self.build.isPartFullyAllocated(self.sub_part_2, self.output_1))

        # But, the *other* build output has not been allocated against
        self.assertFalse(self.build.isPartFullyAllocated(self.sub_part_2, self.output_2))

    def test_cancel(self):
        """
        Test cancellation of the build
        """

        # TODO

        """
        self.allocate_stock(50, 50, 200, self.output_1)
        self.build.cancelBuild(None)

        self.assertEqual(BuildItem.objects.count(), 0)
        """
        pass

    def test_complete(self):
        """
        Test completion of a build output
        """

        self.allocate_stock(50, 50, 250, self.output_1)
        self.allocate_stock(50, 50, 250, self.output_2)

        self.assertTrue(self.build.isFullyAllocated(self.output_1))
        self.assertTrue(self.build.isFullyAllocated(self.output_2))

        self.build.completeBuildOutput(self.output_1, None)

        self.assertFalse(self.build.can_complete)

        self.build.completeBuildOutput(self.output_2, None)

        self.assertTrue(self.build.can_complete)

        self.build.complete_build(None)

        self.assertEqual(self.build.status, status.BuildStatus.COMPLETE)

        # the original BuildItem objects should have been deleted!
        self.assertEqual(BuildItem.objects.count(), 0)

        # New stock items should have been created!
        self.assertEqual(StockItem.objects.count(), 4)

        a = StockItem.objects.get(pk=self.stock_1_1.pk)

        # This stock item has been depleted!
        with self.assertRaises(StockItem.DoesNotExist):
            StockItem.objects.get(pk=self.stock_1_2.pk)

        c = StockItem.objects.get(pk=self.stock_2_1.pk)

        # Stock should have been subtracted from the original items
        self.assertEqual(a.quantity, 900)
        self.assertEqual(c.quantity, 4500)

        # And 10 new stock items created for the build output
        outputs = StockItem.objects.filter(build=self.build)

        self.assertEqual(outputs.count(), 2)

        for output in outputs:
            self.assertFalse(output.is_building)


#!/usr/bin/python3
"""Singly linked list"""

class Node:
    """Representation of a list node"""

    def __init__(self, data, next_node=None):
        """Instantiation with data and next_node"""
        self.data = data
        self.next_node = next_node

    @property
    def data(self):
        """Property to retrieve data"""
        return self.__data

    @data.setter
    def data(self, data):
        """Property setter to set data"""
        if type(data) != int:
            raise TypeError("data must be an integer")
        self.__data = data

    @property
    def next_node(self):
        """Property to retrieve next_node"""
        return self.__next_node

    @next_node.setter
    def next_node(self, value):
        """Property setter to set next_node"""
        if type(value) != Node and value is not None:
            raise TypeError("next_node must be a Node object")
        self.__next_node = value

class SinglyLinkedList:
    """Representation of a singly linked list"""

    def __init__(self):
        """Simple instantiation"""
        self.__head = None

    def sorted_insert(self, value):
        """Method that inserts a Node into the correct sorted position"""
        if self.__head is None:
            new_node = Node(value)
            self.__head = new_node
        elif self.__head.data > value:
            new_node = Node(value, self.__head)
            self.__head = new_node
        else:
            temp = self.__head
            while temp.next_node is not None:
                if temp.next_node.data > value:
                    new_node = Node(value, temp.next_node)
                    temp.next_node = new_node
                    return
                temp = temp.next_node
            if temp.next_node is None:
                new_node = Node(value)
                temp.next_node = new_node

    def __str__(self):
        """Custom __str__ method to print the entire list in stdout"""
        temp = self.__head
        msg = ""
        while temp is not None:
            if temp.next_node is None:
                msg += str(temp.data)
            else:
                msg += str(temp.data) + "\n"
            temp = temp.next_node
        return msg


# queue.py
# by James Fulford
# for Joyful Love

# implements needed functions from Buffer API

import utilities
from utilities import get_when
from utilities import dtformat
import buffpy

class Queue(utilities.SaveLoad):
    """
    Represents a single buffer.
        .name() returns a pretty string
            Queue.name(profile) returns pretty string for profile too
        .pending_updates() returns list of updates in Buffer and Reserve
        .get_buffer() returns list of updates in Buffer
        .add(update) tries to add update to Reserve
        .sync() sends reserved updates to
    """

    def __init__(self, profile, path=None):
        self._prof = profile

        part = path
        if not path:
            part = Queue.profile_name(profile)

        try:
            self.reserve = utilities.load(part)["reserve"]
        except IOError:
            self.reserve = []

    def save(self, fp=None):
        from json import dump
        part = ""
        if fp:
            part = fp + "/"
        with open(part + self.name(), "wb") as phile:
            dump(self.dictionary(), phile, indent=4, sort_keys=True)

    def dictionary(self):
        return {
            "reserve": sorted(self.reserve, key=get_when)
        }

    def name(self):
        return Queue.profile_name(self._prof)

    @staticmethod
    def profile_name(prof):
        return prof["formatted_service"] + " " + prof["formatted_username"]

    __str__ = name

    def pending_updates(self):
        """
        Returns list of posts in Buffer and in reserve
        """
        upd = self.get_buffer()
        upd.extend(self.reserve)
        return sorted(upd, key=get_when)

    def get_buffer(self):
        """
        Returns list of posts in Buffer
        """
        return self._prof.updates.pending

    def add(self, update):
        """
        Adds updates to reserve.
        """
        import buffpy
        upds = self.pending_updates()

        if get_when(update) not in map(get_when, upds):
            self.reserve.append(update)
        else:
            msg = "Already a message scheduled for " + get_when(update).strftime(dtformat())
            exp = Exception(msg)
            exp.error = {
                "update": update,
                "queue": self,
            }
            raise exp

    def sync(self):
        """
        Sends reserved updates to Buffer, stores the rest back in reserve
        Returns time of the latest post

        Order matters, so do not do major multithreading if it can be avoided
        """
        upds = self.reserve
        self.reserve = []
        upds.sort(key=get_when)
        while len(self.get_buffer()) < 10 and len(upds) > 0:
            update = upds.pop(0)
            try:
                self.post(update)
            except buffpy.exceptions.BuffpyRestException:
                print(self.name() + " did not post: " + get_when(update).strftime(dtformat()))
        self.reserve.extend(upds)

    def next_time(self):
        to_consider = []
        buff = self.get_buffer()
        if len(buff) > 0:
            to_consider.append(max(map(get_when, buff)))
        if len(self.reserve) > 0:
            to_consider.append(min(map(get_when, self.reserve)))

        if len(to_consider) > 0:
            return min(to_consider)
        return None

    def post(self, update):
        """
        Adds update to profile's buffer.
        TODO: Allow posting by sending email to secret buffer address
        """
        content = ""
        if "content" in update.keys():
            content = update["content"]
        if "text" in update.keys():
            content = update["text"]

        if "timezone" not in update.keys():
            update["timezone"] = self._prof["timezone"]

        def post_no_media(update):
            if "post_at" in update.keys():
                return self._prof.updates.new(content, when=str(get_when(update)))
            else:
                return self._prof.updates.new(content, now=True)

        def post_with_media(update):
            media = update["media"]
            if "post_at" in update.keys():
                when = str(get_when(update))
                return self._prof.updates.new(content, media=media, when=when)
            else:
                return self._prof.updates.new(content, media=media, now=True)

        def post_link(update):
            # assert url_exists(media["link"])
            # remove picture or thumbnail if url is bad
            if "picture" in media.keys():
                # if not url_exists(media["picture"]):
                del media["picture"]
            if "thumbnail" in media.keys():
                # if not url_exists(media["thumbnail"]):
                del media["thumbnail"]
            post_with_media(update)

        def url_exists(url):
            import httplib
            conn = httplib.HTTPConnection(url)
            conn.request("HEAD", "/index.html")
            res = conn.getresponse()
            return res.status < 400

        # now to post
        try:
            assert "media" in update.keys()
            # if there is not media, posts without media.

            media = update["media"]
            # if media is a photo
            if "photo" in media.keys():
                import os
                try:

                    # if photo url fails, post link
                    # assert url_exists(media["photo"])

                    # post photo
                    post_with_media(update)
                except AssertionError:
                    del media["photo"]

                    # if post_link fails, posts without media
                    post_link(update)
            else:
                # if there isn't a photo,
                # post link instead.
                # if post_link fails, posts without media
                post_link(update)
        except AssertionError:
            # posts without media
            post_no_media(update)


import logging
import os
import time

from cassandra.cluster import Cluster
from cassandra.policies import RetryPolicy, RoundRobinPolicy, TokenAwarePolicy

# Set default log.handler to avoid "No handler found" warnings.

stderrLogger = logging.StreamHandler()
f = '%(filename)s: %(levelname)s: %(funcName)s(): %(lineno)d:\t%(message)s'
stderrLogger.setFormatter(logging.Formatter(f))

log = logging.getLogger('hecuba')
log.addHandler(stderrLogger)

if 'DEBUG' in os.environ and os.environ['DEBUG'].lower() == "true":
    log.setLevel(logging.DEBUG)
elif 'HECUBA_LOG' in os.environ:
    log.setLevel(os.environ['HECUBA_LOG'].upper())
else:
    log.setLevel(logging.ERROR)

class _NRetry(RetryPolicy):
    def __init__(self, time_to_retry=5):
        self.time_to_retry = time_to_retry

    def on_unavailable(self, query, consistency, required_replicas, alive_replicas, retry_num):
        if retry_num > self.time_to_retry:
            return self.RETHROW, None
        else:
            return self.RETHROW, None

    def on_write_timeout(self, query, consistency, write_type, required_responses, received_responses, retry_num):
        if retry_num > self.time_to_retry:
            return self.RETHROW, None
        else:
            return self.RETHROW, None

    def on_read_timeout(self, query, consistency, required_responses, received_responses, data_retrieved, retry_num):
        if retry_num > self.time_to_retry:
            return self.RETHROW, None
        else:
            return self.RETHROW, None

class Config:
    class __Config:
        def __init__(self):
            self.configured = False
            ## intercepted : list of numpy intercepted calls
            self.intercepted = {}
            self.configdir = {} # map<string, string>

    instance = __Config()

    def __getattr__(self, item):
        return getattr(Config.instance, item)

    def executequery_withretries(self, query):
        """
            Executes 'query' to cassandra. If the query fails (for example due
            to a timeout) it will resend the query a maximum of NRETRIES(5)
        """
        executed=False
        nretries=0
        while not executed:
            try:
                self.instance.session.execute(query)
                executed=True
            except Exception as ir:
                log.warn("Unable to execute %s %s/5", query, nretries)
                nretries+=1
                if nretries==5: #FIXME the number of retries should be configurable
                    log.error("Too many retries. Aborting. Unable to execute %s", query)
                    raise ir
                time.sleep(1)

    def executelocked(self,query):
        if self.instance.concurrent_creation:
            r=self.instance.session.execute(self.instance._query_to_lock,[query])
            if r[0][0]:
                self.executequery_withretries(query)
            else:
                # FIXME find a better way to do this instead of an sleep... describe?
                time.sleep(.300)
        else:
            self.executequery_withretries(query)

    def __init__(self):
        singleton = Config.instance
        if singleton.configured:
            log.info('setting down')
            return

        singleton.configured = True

        if 'HECUBA_ARROW' in os.environ:
            env_var = os.environ['HECUBA_ARROW'].lower()
            singleton.arrow_enabled = False if env_var == 'no' or env_var == 'false' else True
            log.info('HECUBA_ARROW: {}'.format(singleton.arrow_enabled))
        else:
            singleton.arrow_enabled = False
            log.warn('Arrow access is DISABLED [HECUBA_ARROW=%s]', singleton.arrow_enabled)
        singleton.configdir['hecuba_arrow'] = 'true' if singleton.arrow_enabled else 'false'

        if 'CONCURRENT_CREATION' in os.environ:
            if os.environ['CONCURRENT_CREATION']=='True':
                singleton.concurrent_creation = True
            else:
                singleton.concurrent_creation = False
            log.info('CONCURRENT_CREATION: %s', str(singleton.concurrent_creation))
        else:
            singleton.concurrent_creation = False
            log.warn('Concurrent creation is DISABLED [CONCURRENT_CREATION=False]')
        singleton.configdir['concurrent_creation'] = 'true' if singleton.concurrent_creation else 'false'

        if 'LOAD_ON_DEMAND' in os.environ:
            if os.environ['LOAD_ON_DEMAND']=='False':
                singleton.load_on_demand = False
            else:
                singleton.load_on_demand = True
            log.info('LOAD_ON_DEMAND: %s', str(singleton.load_on_demand))
        else:
            singleton.load_on_demand = True
            log.warn('Load data on demand is ENABLED [LOAD_ON_DEMAND=True]')
        singleton.configdir['load_on_demand'] = 'true' if singleton.load_on_demand else 'false'

        if 'CREATE_SCHEMA' in os.environ:
            env_var = os.environ['CREATE_SCHEMA'].lower()
            singleton.id_create_schema = False if env_var == 'no' or env_var == 'false' else True
            log.info('CREATE_SCHEMA: %d', singleton.id_create_schema)
        else:
            singleton.id_create_schema = True
            log.warn('Creating keyspaces and tables by default [CREATE_SCHEMA=True]')
        singleton.configdir['id_create_schema'] = 'true' if singleton.load_on_demand else 'false'
        try:
            singleton.nodePort = int(os.environ['NODE_PORT'])
            log.info('NODE_PORT: %d', singleton.nodePort)
        except KeyError:
            log.warn('using default NODE_PORT 9042')
            singleton.nodePort = 9042
        singleton.configdir['node_port'] = str(singleton.nodePort)

        try:
            singleton.contact_names = os.environ['CONTACT_NAMES'].split(",")
            log.info('CONTACT_NAMES: %s', str.join(" ", singleton.contact_names))
            # Convert node names to ips if needed
            import socket
            contact_names_ips = []
            show_translation = False
            for h_name in singleton.contact_names:
                IP_addres = socket.gethostbyname(h_name)
                if (IP_addres != h_name):
                    show_translation=True
                contact_names_ips.append(IP_addres)
            singleton.contact_names = contact_names_ips
            if show_translation:
                log.info('CONTACT_NAMES: %s', str.join(" ", singleton.contact_names))

        except KeyError:
            log.warn('using default contact point localhost')
            singleton.contact_names = ['127.0.0.1']
        singleton.configdir['contact_names'] = str.join(",", singleton.contact_names)

        try:
            singleton.kafka_names = os.environ['KAFKA_NAMES'].split(",")
            log.info('KAFKA_NAMES: %s', str.join(" ", singleton.kafka_names))
        except KeyError:
            log.warn('kakfa names defaults to %s', str.join(" ", singleton.contact_names))
            singleton.kafka_names = singleton.contact_names
        singleton.configdir['kafka_names'] = str.join(",", singleton.kafka_names)

        if hasattr(singleton, 'session'):
            log.warn('Shutting down pre-existent sessions and cluster')
            try:
                singleton.session.shutdown()
                singleton.cluster.shutdown()
            except Exception:
                log.warn('error shutting down')
        try:
            singleton.replication_factor = int(os.environ['REPLICA_FACTOR'])
            log.info('REPLICA_FACTOR: %d', singleton.replication_factor)
        except KeyError:
            singleton.replication_factor = 1
            log.warn('using default REPLICA_FACTOR: %d', singleton.replication_factor)
        singleton.configdir['replica_factor'] = str(singleton.replication_factor)

        try:
            user_defined_execution_name = os.environ['EXECUTION_NAME']
            if user_defined_execution_name == 'hecuba':
                raise RuntimeError('Error: the application keyspace cannot be \'hecuba\'. '
                                   'This keyspace is reserved for storing metadata.')
            singleton.execution_name = user_defined_execution_name
            log.info('EXECUTION_NAME: %s', singleton.execution_name)
        except KeyError:
            singleton.execution_name = 'my_app'
            log.warn('using default EXECUTION_NAME: %s', singleton.execution_name)
        singleton.configdir['execution_name'] = singleton.execution_name

        try:
            singleton.splits_per_node = int(os.environ['SPLITS_PER_NODE'])
            log.info('SPLITS_PER_NODE: %d', singleton.splits_per_node)
        except KeyError:
            singleton.splits_per_node = 32
            log.warn('using default SPLITS_PER_NODE: %d', singleton.splits_per_node)
        singleton.configdir['splits_per_node'] = str(singleton.splits_per_node)

        try:
            singleton.token_range_size = int(os.environ['TOKEN_RANGE_SIZE'])
            log.info('TOKEN_RANGE_SIZE: %d', singleton.token_range_size)
            singleton.target_token_range_size = None
        except KeyError:
            singleton.token_range_size = None

            try:
                singleton.target_token_range_size = int(os.environ['TARGET_TOKEN_RANGE_SIZE'])
                log.info('TARGET_TOKEN_RANGE_SIZE: %d', singleton.target_token_range_size)
            except KeyError:
                singleton.target_token_range_size = 64 * 1024
                log.warn('using default TARGET_TOKEN_RANGE_SIZE: %d', singleton.target_token_range_size)
        singleton.configdir['target_token_range_size'] = str(singleton.target_token_range_size)

        try:
            singleton.max_cache_size = int(os.environ['MAX_CACHE_SIZE'])
            log.info('MAX_CACHE_SIZE: %d', singleton.max_cache_size)
        except KeyError:
            singleton.max_cache_size = 1000
            log.warn('using default MAX_CACHE_SIZE: %d', singleton.max_cache_size)
        singleton.configdir['max_cache_size'] = str(singleton.max_cache_size)

        try:
            singleton.replication_strategy = os.environ['REPLICATION_STRATEGY']
            log.info('REPLICATION_STRATEGY: %s', singleton.replication_strategy)
        except KeyError:
            singleton.replication_strategy = "SimpleStrategy"
            log.warn('using default REPLICATION_STRATEGY: %s', singleton.replication_strategy)
        singleton.configdir['replication_strategy'] = singleton.replication_strategy

        try:
            singleton.replication_strategy_options = os.environ['REPLICATION_STRATEGY_OPTIONS']
            log.info('REPLICATION_STRATEGY_OPTIONS: %s', singleton.replication_strategy_options)
        except KeyError:
            singleton.replication_strategy_options = "''"
            log.warn('using default REPLICATION_STRATEGY_OPTIONS: %s', singleton.replication_strategy_options)
        singleton.configdir['replication_strategy_options'] = singleton.replication_strategy_options

        if singleton.replication_strategy == "SimpleStrategy":
            singleton.replication = "{'class' : 'SimpleStrategy', 'replication_factor': %d}" % \
                                    singleton.replication_factor
        else:
            singleton.replication = "{'class' : '%s', %s}" % (
                singleton.replication_strategy, singleton.replication_strategy_options)
        try:
            singleton.hecuba_print_limit = int(os.environ['HECUBA_PRINT_LIMIT'])
            log.info('HECUBA_PRINT_LIMIT: %s', singleton.hecuba_print_limit)
        except KeyError:
            singleton.hecuba_print_limit = 1000
            log.warn('using default HECUBA_PRINT_LIMIT: %s', singleton.hecuba_print_limit)
        singleton.configdir['hecuba_print_limit'] = str(singleton.hecuba_print_limit)

        try:
            singleton.prefetch_size = int(os.environ['PREFETCH_SIZE'])
            log.info('PREFETCH_SIZE: %s', singleton.prefetch_size)
        except KeyError:
            singleton.prefetch_size = 10000
            log.warn('using default PREFETCH_SIZE: %s', singleton.prefetch_size)
        singleton.configdir['prefetch_size'] = str(singleton.prefetch_size)

        try:
            singleton.write_buffer_size = int(os.environ['WRITE_BUFFER_SIZE'])
            log.info('WRITE_BUFFER_SIZE: %s', singleton.write_buffer_size)
        except KeyError:
            singleton.write_buffer_size = 1000
            log.warn('using default WRITE_BUFFER_SIZE: %s', singleton.write_buffer_size)
        singleton.configdir['write_buffer_size'] = str(singleton.write_buffer_size)

        try:
            singleton.write_callbacks_number = int(os.environ['WRITE_CALLBACKS_NUMBER'])
            log.info('WRITE_CALLBACKS_NUMBER: %s', singleton.write_callbacks_number)
        except KeyError:
            singleton.write_callbacks_number = 16
            log.warn('using default WRITE_CALLBACKS_NUMBER: %s', singleton.write_callbacks_number)
        singleton.configdir['write_callbacks_number'] = str(singleton.write_callbacks_number)

        try:
            env_var = os.environ['TIMESTAMPED_WRITES'].lower()
            singleton.timestamped_writes = False if env_var == 'no' or env_var == 'false' else True
            log.info('TIMESTAMPED WRITES ENABLED? {}'.format(singleton.timestamped_writes))
        except KeyError:
            singleton.timestamped_writes = False
            log.warn('using default TIMESTAMPED_WRITES: %s', singleton.timestamped_writes)
        singleton.configdir['timestamped_writes'] = 'true' if singleton.timestamped_writes else 'false'

        if 'HECUBA_SN_SINGLE_TABLE' in os.environ:
            env_var = os.environ['HECUBA_SN_SINGLE_TABLE'].lower()
            singleton.hecuba_sn_single_table = False if env_var == 'no' or env_var == 'false' else True
            log.info('HECUBA_SN_SINGLE_TABLE: %d', singleton.hecuba_sn_single_table)
        else:
            singleton.hecuba_sn_single_table = True
            log.warn('Create storagenumpys in a single table by default [HECUBA_SN_SINGLE_TABLE=True]')
        singleton.configdir['hecuba_sn_single_table'] = 'true' if singleton.hecuba_sn_single_table else 'false'

        if singleton.max_cache_size < singleton.write_buffer_size:
            import warnings
            message = "Defining a MAX_CACHE_SIZE smaller than WRITE_BUFFER_SIZE can result " \
                      "in reading outdated results from the persistent storage"
            warnings.warn(message)

        log.info('Initializing global session')

        singleton.cluster = Cluster(contact_points=singleton.contact_names,
                                    load_balancing_policy=TokenAwarePolicy(RoundRobinPolicy()),
                                    port=singleton.nodePort,
                                    default_retry_policy=_NRetry(5))
        singleton.session = singleton.cluster.connect()
        singleton.session.encoder.mapping[tuple] = singleton.session.encoder.cql_encode_tuple
        if singleton.concurrent_creation:
            configure_lock=[
                """CREATE KEYSPACE IF NOT EXISTS hecuba_locks
                        WITH replication=  {'class': 'SimpleStrategy', 'replication_factor': 1};
                """,
                """CREATE TABLE IF NOT EXISTS hecuba_locks.table_lock
                        (table_name text, PRIMARY KEY (table_name));
                """,
                "TRUNCATE table hecuba_locks.table_lock;"
            ]
            for query in configure_lock:
                try:
                    self.instance.session.execute(query)
                except Exception as e:
                    log.error("Error executing query %s" % query)
                    raise e
            singleton._query_to_lock=singleton.session.prepare("INSERT into hecuba_locks.table_lock (table_name) values (?) if not exists;")

        if singleton.id_create_schema:
            queries = [
                "CREATE KEYSPACE IF NOT EXISTS hecuba  WITH replication = %s" % singleton.replication,
                """CREATE TYPE IF NOT EXISTS hecuba.q_meta(
                mem_filter text,
                from_point frozen<list<double>>,
                to_point frozen<list<double>>,
                precision float);
                """,
                """CREATE TYPE IF NOT EXISTS hecuba.np_meta (flags int, elem_size int, partition_type tinyint,
                dims list<int>, strides list<int>, typekind text, byteorder text)""",
                """CREATE TABLE IF NOT EXISTS hecuba
                .istorage (storage_id uuid,
                class_name text,name text,
                istorage_props map<text,text>,
                tokens list<frozen<tuple<bigint,bigint>>>,
                indexed_on list<text>,
                qbeast_random text,
                qbeast_meta frozen<q_meta>,
                numpy_meta frozen<np_meta>,
                block_id int,
                base_numpy uuid,
                view_serialization blob,
                primary_keys list<frozen<tuple<text,text>>>,
                columns list<frozen<tuple<text,text>>>,
                PRIMARY KEY(storage_id));
                """,
                "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = %s" % (singleton.execution_name, singleton.replication)]
            for query in queries:
                try:
                    self.executelocked(query)
                except Exception as e:
                    log.error("Error executing query %s" % query)
                    raise e

        from hecuba.hfetch import connectCassandra, HArrayMetadata
        # connecting c++ bindings
        connectCassandra(singleton.contact_names, singleton.nodePort, singleton.configdir)

        if singleton.id_create_schema:
            time.sleep(10)
        singleton.cluster.register_user_type('hecuba', 'np_meta', HArrayMetadata)
        # Create a dummy arrayDataStore to generate the TokensToHost variable

global config
config = Config()

from .parser import Parser
from .storageobj import StorageObj
from .hdict import StorageDict
from .hnumpy import StorageNumpy
from .storagestream import StorageStream
from .hfilter import hfilter

if not filter == hfilter:
    import builtins

    builtins.python_filter = filter
    builtins.filter = hfilter

def _intercept_numpy_method(method_name):
    """
    Intercept Numpy.'method_name' and use StorageNumpy.'method_name' instead.
    """
    if not isinstance(method_name, str):
        raise TypeError("Intercepted method name MUST be an string")

    # INTERCEPT Numpy METHODS
    import numpy as np

    if np.__dict__[method_name] != StorageNumpy.__dict__[method_name]:
        config.instance.intercepted[method_name] = np.__dict__[method_name]
        np.__dict__[method_name] = StorageNumpy.__dict__[method_name]
    else:
        #print("WARNING: method {} already intercepted".format(method_name), flush=True)
        if not method_name in config.instance.intercepted:
            print("WARNING: method {} already intercepted but we have lost the original method!!!".format(method_name), flush=True)
            # This only happens executing tests... we think that is due to the reload of hecuba, but not numpy (not 100% sure)
            # therefore our solution is to reload numpy.
            import importlib
            importlib.reload(np)
            config.instance.intercepted[method_name] = np.__dict__[method_name]
            np.__dict__[method_name] = StorageNumpy.__dict__[method_name]

_intercept_numpy_method('dot')
_intercept_numpy_method('array_equal')
_intercept_numpy_method('concatenate')

__all__ = ['StorageObj', 'StorageDict', 'StorageNumpy', 'StorageStream', 'Parser']


from django.urls import path, re_path, include
from . import views

app_name = 'images'
urlpatterns = [

    # 피드뷰 상세 보기
    re_path(
        r'^(?P<image_id>\d+)/$',
        view = views.ImageDetail.as_view(),
        name='feed'
    ),

    re_path(
        r'^search/$',
        view=views.Search.as_view(),
        name='search'
    ),

    re_path(
        r'^$',
        view=views.Feed.as_view(),
        name='feed'
    ),

    # 좋아요
    re_path(
        r'^(?P<image_id>\w+)/likes/',
        view=views.LikeImage.as_view(),
        name="like_image"
    ),
    # 싫어요
    re_path(
        r'^(?P<image_id>\w+)/unlikes/',
        view=views.UnLikeImage.as_view(),
        name="unlike_image"
    ),

    #  삭제에 대한 url 패턴 추가
    re_path(
        r'^(?P<image_id>\d+)/comments/(?P<comment_id>\d+)/$',
        view=views.DeleteComments.as_view(),
        name="delete_Comments"
    ),

    re_path(
        r'^(?P<image_id>\d+)/comments/',
        view=views.CommentOnImage.as_view(),
        name="comment_image"
    ),

]


def verifier (transmitted_message,generator):  #it takes the message and the generator and checks if the reminder is zero or not to check errors in the messagge
    reminder_chk=int(crc_div(transmitted_message,generator),2)
    if(reminder_chk==0):
        return True
    else:
        return False

def alter(message,index):           #it converts the bit in the required index
    message=list(message)
    if message[index-1]=='0':

        message[index-1]='1'
    else:

        message[index-1]='0'
    message=''.join(message)       #convert list to string
    return message


""" `flynt` is a command line tool to automatically convert a project's Python code
from old "%-formatted" and .format(...) strings into Python 3.6+'s f-strings.
Learn more about f-strings at https://www.python.org/dev/peps/pep-0498/"""

__version__ = "0.26"

import argparse

from flynt.api import fstringify

def main():
    parser = argparse.ArgumentParser(
        description=f"flynt v.{__version__}", add_help=True, epilog=__doc__
    )

    verbosity_group = parser.add_mutually_exclusive_group()
    verbosity_group.add_argument("--verbose", action="store_true", help="run with verbose output",
                       default=False)
    verbosity_group.add_argument("--quiet", action="store_true", help="run without output",
                       default=False)

    multiline_group = parser.add_mutually_exclusive_group()
    multiline_group.add_argument("--no_multiline",
                       action="store_true",
                       help="convert only single line expressions",
                       default=False)

    multiline_group.add_argument("--line_length",
                       action="store",
                       help="for expressions spanning multiple lines, convert only if "
                            "the resulting single line will fit into the line length limit. "
                            "Default value is 79 characters.",
                       default=79)

    parser.add_argument(
        "--upgrade", action="store_true", default=False, help="run pyupgrade on .py files"
    )

    parser.add_argument("src", action="store", help="source file or directory")

    args = parser.parse_args()

    fstringify(args.src,
               verbose = args.verbose,
               quiet = args.quiet,
               multiline = not args.no_multiline,
               len_limit = int(args.line_length),
               pyup = args.upgrade)

if __name__ == "__main__":
    main()


from itertools import count
from scipy.optimize import brentq
import logging

class FiberSolver(object):

    """Generic abstract class for callable objects used as fiber solvers."""

    logger = logging.getLogger(__name__)

    def __init__(self, fiber):
        self.fiber = fiber

    def __call__(self, *args, **kwargs):
        raise NotImplementedError()

    def _findFirstRoot(self, fct, args=(), lowbound=0, highbound=None,
                       ipoints=[], delta=0.25, maxiter=None):
        while True:
            if ipoints:
                maxiter = len(ipoints)
            elif highbound:
                maxiter = int((highbound - lowbound) / delta)
            assert maxiter is not None

            a = lowbound
            fa = fct(a, *args)
            if fa == 0:
                return a

            for i in range(1, maxiter+1):
                b = ipoints.pop(0) if ipoints else a + delta
                if highbound:
                    if ((b > highbound > lowbound) or
                            (b < highbound < lowbound)):
                        self.logger.info("_findFirstRoot: no root found within"
                                         " allowed range")
                        return float("nan")
                fb = fct(b, *args)
                if fb == 0:
                    return b

                if (fa > 0 and fb < 0) or (fa < 0 and fb > 0):
                    z = brentq(fct, a, b, args=args, xtol=1e-20)
                    fz = fct(z, *args)
                    if abs(fa) > abs(fz) < abs(fb):  # Skip discontinuities
                        return z

                a, fa = b, fb
            if highbound and maxiter < 100:
                delta /= 10
            else:
                break
        print("maxiter reached", maxiter)
        return float("nan")

    def _findBetween(self, fct, lowbound, highbound, args=()):
        v = [lowbound, highbound]
        s = [fct(lowbound, *args), fct(highbound, *args)]

        for j in count():  # probably not needed...
            for i in range(len(s)-1):
                a, b = v[i], v[i+1]
                fa, fb = s[i], s[i+1]

                if (fa > 0 and fb < 0) or (fa < 0 and fb > 0):
                    z = brentq(fct, a, b, args=args)
                    fz = fct(z, *args)
                    if abs(fa) > abs(fz) < abs(fb):  # Skip discontinuities
                        return z

            ls = len(s)
            for i in range(ls-1):
                a, b = v[2*i], v[2*i+1]
                c = (a + b) / 2
                v.insert(2*i+1, c)
                s.insert(2*i+1, fct(c, *args))


from match3Env import Match3Env
import numpy as np
import os
import shutil
import json
import sys
import gym
import ray
from ray.rllib.agents.a3c import A3CTrainer, DEFAULT_CONFIG
from ray.tune.registry import register_env

os.environ["CUDA_VISIBLE_DEVICES"] = '1'

checkpoint_root = 'checkpoint_4'
# Where checkpoints are written:
# shutil.rmtree(checkpoint_root, ignore_errors=True, onerror=None)
# Where some data will be written and used by Tensorboard below:
ray_results = f'{checkpoint_root}/ray_results/'
shutil.rmtree(ray_results, ignore_errors=True, onerror=None)
info = ray.init(ignore_reinit_error=True, num_gpus=1)

trainer_config = DEFAULT_CONFIG.copy()
# trainer_config['num_workers'] = 4
trainer_config["train_batch_size"] = 400

def do_training(Match3EnvClass, config = trainer_config, iterations=20):
    trainer = A3CTrainer(config, Match3EnvClass)
    if os.path.exists(checkpoint_root):
        checkpoints = os.listdir(checkpoint_root)
        for i in range(len(checkpoints)):
            checkpoints[i] = int(checkpoints[i].replace('checkpoint_', ''))
        checkpoint = max(checkpoints)
        trainer.restore(os.path.join(checkpoint_root, 'checkpoint_%06d' % checkpoint, f'checkpoint-{checkpoint}'))
    results = []
    episode_data = []
    episode_json = []
    for i in range(iterations):
        result = trainer.train()
        results.append(trainer)

        episode = {'n': i,
                'episode_reward_min': result['episode_reward_min'],
                'episode_reward_mean': result['episode_reward_mean'],
                'episode_reward_max': result['episode_reward_max'],
                'episode_len_mean': result['episode_len_mean']}

        episode_data.append(episode)
        episode_json.append(json.dumps(episode))
        file_name = trainer.save(checkpoint_root)

    return trainer

# trainer = do_training("CartPole-v1", config=trainer_config, iterations=20)

# env = gym.make('CartPole-v0')
# state = env.reset()

def env_creator(env_config):
    return Match3Env()  # return an env instance

register_env('Match3Env', env_creator)

trainer = do_training('Match3Env', config=trainer_config, iterations=1)

env = Match3Env()
state = env.reset()

done = False
max_state = -1
cumulative_reward = 0
reward = 1
# while not done:
while reward > 0 or not done:
    action = trainer.compute_action(state)
    state, reward, done, results = env.step(action)
    cumulative_reward += reward

print(f'Cumulative reward you received is: {cumulative_reward}. Congratulations!')


import pprint
subscription_key = ""
assert subscription_key
import requests
import json

def keyPhrase(text):
	text_analytics_base_url = "https://eastus.api.cognitive.microsoft.com/text/analytics/v2.0/"
	key_phrase_api_url = text_analytics_base_url + "keyPhrases"

	thetext = text

	documents = {'documents' : [
	  {'id': '1', 'language': 'en', 'text': thetext},
	]}
	headers   = {'Ocp-Apim-Subscription-Key': subscription_key}
	response  = requests.post(key_phrase_api_url, headers=headers, json=documents)
	key_phrases = response.json()["documents"][0]['keyPhrases']
	return key_phrases


import numpy as np
from sklearn.decomposition.pca import PCA
from numpy import loadtxt, genfromtxt, savetxt, var, random
from pylab import figure
from matplotlib.pyplot import plot, savefig, xlabel, ylabel, scatter, axis, xlim

#data_dir='../../data/'
data_dir='../../data_all_types/'
out_dir='./plots/'

# load the data and run PCA
derivatives = loadtxt(data_dir+'derivatives.dat')
labels = loadtxt(data_dir+'labels.dat')
spectra_data = genfromtxt(data_dir+'spectra_data.dat',dtype=None)

random.seed(1)

sne_names= set(spectra_data['f0']) # one entry for each SN
train_names=[]
for sn in sne_names:
    if random.random() > .20:
        train_names.append(sn)

train_mask_TL=[]
train_mask_no_TL=[]
test_mask=[]
for i in range(np.size(labels)):
    if labels[i]==1:
        if spectra_data['f0'][i] in train_names:
            train_mask_TL.append(i)
            train_mask_no_TL.append(i)
        else:
            test_mask.append(i)
    if labels[i]==0:
        #if spectra_data['f0'][i] in train_names:
        train_mask_TL.append(i)

#######################################################
#   To run the DL.
from rpy2.robjects import r
def DL( train, test, n ):

    input_string = '120,100,90,50,30,20,%d,20,30,50,90,100,120' % (n)
    seed = 1
    in_param = 'training_frame = train3.hex, activation = "Tanh", autoencoder=T, hidden = c(%s), epochs=100, ignore_const_cols = F, seed = %d '  % (input_string,seed)
    n_layers = 7
    size_in_data = np.shape(train)[1]
    in_param = 'x=1:%d,' % (size_in_data) + in_param
    in_file_cvs='./.temp_file.cvs'
    np.savetxt(in_file_cvs, train, delimiter=',', fmt='%.18f')
    ## define libraries
    r('library(h2o)')
    ## Read data
    r('h2oServer <- h2o.init(nthreads=-1)')
    r('TRAIN = "%s" '% (in_file_cvs))
    r('train3.hex <- h2o.importFile(h2oServer, path = TRAIN, header = F, sep = ",", destination_frame="train3.hex")')
    ## Construct deep learning model
    r('dlmodel <- h2o.deeplearning( %s ) ' % (in_param))
    ## Generate new features
    r('features_dl <- h2o.deepfeatures(dlmodel, train3.hex, layer=%d)' % (n_layers))
    r('head(features_dl)')
    ## new features
    X = r('as.matrix(features_dl)')

    in_file_cvs='./.temp_file.cvs'
    np.savetxt(in_file_cvs, test, delimiter=',', fmt='%.18f')
    ## Read data
    r('h2oServer <- h2o.init(nthreads=-1)')
    r('TEST = "%s" '% (in_file_cvs))
    r('test3.hex <- h2o.importFile(h2oServer, path = TEST, header = F, sep = ",", destination_frame="test3.hex")')
    ## Generate predictions
    r('predictions_dl <- h2o.predict(dlmodel, test3.hex)')
    r('head(predictions_dl)')
    ## new predictions
    pred = r('as.matrix(predictions_dl)')
    return var(pred -test)
################################################################

figure()
variances_table = []

for i in range(2,11,1):
    pca = PCA(n_components=i)
    der = derivatives[train_mask_TL]
    pca.fit(der)
    X = pca.transform(derivatives[test_mask])
    pred_pca_temp = (pca.inverse_transform(X))

    #
    var_fraction_pca_TL = var(pred_pca_temp-derivatives[test_mask])/var(derivatives[test_mask])
    #plot([i], [var(pred_pca_temp-derivatives[test_mask])],'D')

    var_fraction_DL_TL = DL( derivatives[train_mask_TL], derivatives[test_mask], i)/var(derivatives[test_mask])
    #plot([i], [var_DL_TL ],'Dk')

    pca = PCA(n_components=i)
    der = derivatives[train_mask_no_TL]
    pca.fit(der)
    X = pca.transform(derivatives[test_mask])
    pred_pca_temp = (pca.inverse_transform(X))

    var_fraction_pca_no_TL = var(pred_pca_temp-derivatives[test_mask])/var(derivatives[test_mask])
    #plot([i], [var(pred_pca_temp-derivatives[test_mask])],'x')

    var_fraction_DL_no_TL = DL( derivatives[train_mask_no_TL], derivatives[test_mask], i)/var(derivatives[test_mask])
    #plot([i], [var_DL_no_TL ],'xk')

    variances_table.append([i,var_fraction_pca_no_TL, var_fraction_pca_TL, var_fraction_DL_no_TL, var_fraction_DL_TL])

savetxt('./residuals.dat', variances_table, header='nPCs fraction_variance_PCA_at_max fraction_variance_PCA_with_TL fraction_variance_DL_at_max fraction_variance_DL_with_TL'   )

data_to_plot = loadtxt('./residuals.dat').T
figure()
plot(data_to_plot[0], data_to_plot[1],'xr')
plot(data_to_plot[0], data_to_plot[2],'Dr')
plot(data_to_plot[0], data_to_plot[3],'xk')
plot(data_to_plot[0], data_to_plot[4],'Dk')


from ebs_deploy import get, parse_env_config, parse_option_settings

import boto3

def get_security_group_from_config(option_settings):
    for item in option_settings:
        if 'aws:autoscaling:launchconfiguration' in item:
            if 'SecurityGroups' in item:
                return item[2]

def get_launch_configuration_name(security_group_name):
    autoscaling = boto3.client('autoscaling')
    as_groups = autoscaling.describe_launch_configurations()

    for item in as_groups.get('LaunchConfigurations'):
        if security_group_name == item.get('SecurityGroups'):
            return item.get('LaunchConfigurationName')

def get_auto_scaling_group_name(launch_configuration_name):
    autoscaling = boto3.client('autoscaling')
    as_groups = autoscaling.describe_auto_scaling_groups()
    for item in as_groups.get('AutoScalingGroups'):
        if launch_configuration_name == item.get('LaunchConfigurationName'):
            return item.get('AutoScalingGroupName')

def disable_metrics_collection(auto_scaling_group_name):
    autoscaling = boto3.client('autoscaling')
    autoscaling.disable_metrics_collection(auto_scaling_group_name)

def enable_metrics_collection(auto_scaling_group_name):
    autoscaling = boto3.client('autoscaling')
    autoscaling.enable_metrics_collection(auto_scaling_group_name)

def add_arguments(parser):
    """
    Args for the init command
    """
    parser.add_argument('-e', '--environment',  help='Environment name', required=False, nargs='+')
    parser.add_argument('-w', '--dont-wait', help='Skip waiting for the app to be deleted', action='store_true')

def execute(helper, config, args):
    """
    Updates environments
    """
    environments = []
    if args.environment:
        for env_name in args.environment:
            environments.append(env_name)
    else:
        for env_name, env_config in get(config, 'app.environments').items():
            environments.append(env_name)

    # get data to disable metrics

    wait_environments = []
    for env_name in environments:
        env = parse_env_config(config, env_name)
        option_settings = parse_option_settings(env.get('option_settings', {}))
        security_group = get_security_group_from_config(option_settings)
        launch_configuration = get_launch_configuration_name(security_group)
        auto_scaling_group = get_auto_scaling_group_name(launch_configuration)

        disable_metrics_collection(auto_scaling_group)

        helper.update_environment(env_name,
            description=env.get('description', None),
            option_settings=option_settings,
            tier_type=env.get('tier_type'),
            tier_name=env.get('tier_name'),
            tier_version=env.get('tier_version'))
        wait_environments.append(env_name)

        enable_metrics_collection(auto_scaling_group)

    # wait
    if not args.dont_wait:
        helper.wait_for_environments(wait_environments, health='Green', status='Ready')


#!/usr/bin/python
import numpy as np
import pandas as pd
import os
from sklearn.externals import joblib
import ConfigParser
import ipdb

def main(method="promp",min_max_scaler=None,emg_index=None,
         leftHand_index=None,leftJoint_index=None, datasets_path=None):

    # read csv file
    csv_path = os.path.join(datasets_path, 'info/noise/multiModal_states.csv')
    data = pd.read_csv(csv_path)
    # extract the all signals data ORIENTATION
    emg = data.values[:, 7:15].astype(float)
    left_hand = data.values[:, leftHand_index[0]:leftHand_index[-1]].astype(float)
    left_joints = data.values[:, leftJoint_index[0]:leftJoint_index[-1]].astype(float)  # robot ee actually

    if method == "promp":
        full_data = left_joints[1200:, :]
        full_data = min_max_scaler.transform(left_joints)

    elif method == "emg_ipromp":
        full_data = np.hstack([left_hand, emg, left_joints])[1200:, :]
        full_data = min_max_scaler.transform(full_data)

    elif method =="ipromp":
        full_data = np.hstack([left_hand, left_joints])[1200:, :]
        full_data = min_max_scaler.transform(full_data)

    # compute the noise observation covariance matrix
    noise_cov = np.cov(full_data.T)
    # save it in pkl
    joblib.dump(noise_cov, os.path.join(datasets_path, 'pkl/'+method+'_noise_cov.pkl'))
    print('Saved the noise covariance matrix successfully!')

if __name__ == '__main__':
    main()


'''
@Description: function
@Author: HuiBo
@Date: 2019-10-13 15:54:40
@LastEditTime: 2019-10-13 22:18:50
@LastEditors: Please set LastEditors
'''
from linkMysql import conn_mysql
import os
import time

def Caidan():
    print("*************************背单词系统********************************")
    print("**1.开始背单词")
    print("**2.添加新词汇")
    print("**3.背错词本里的单词")
    print("**4.显示所有单词")
    print("**5.显示所有错词")
    print("**6.删除单词")
    print("**7.添加单词到错词本")
    print("**8.删除错题本中的单词")
    print("**9.查询单词")
    print("**0.退出系统")

# 2, 7
def addWord(table):
    db = conn_mysql()
    os.system('clear')
    print("*************************背单词系统********************************")
    word = input("主人, 请输入您想添加的单词: ")
    meaning = input("主人, 请输入该单词的意思: ")
    flag = db.execute("insert into {} values('{}', '{}')".format(table, word, meaning))
    if flag:
        print("恭喜主人, 成功添加单词至单词本中!")
    else:
        print("很抱歉主人, 没能成功的将单词添加到单词本中!")
    returnCaidan()

# 6, 8

def deleteWord(table):
    db = conn_mysql()
    word = input("主人, 请输入您想删除的单词: ")
    if not db.haveWord(word, table):
        print("主人, 该单词并不在我们单词表里面哦!")
        returnCaidan()
        return
    flag = db.execute("delete from {} where word='{}'".format(table, word))
    if flag == True:
        print("恭喜主人, 成功删除该单词!")
    else:
        print("很抱歉主人, 没能成功的将单词删除!")
    returnCaidan()

# 4, 5
def printAllWords(table):
    db = conn_mysql()
    count = db.getCount(table)
    if count == 0:
        print("主人, 词库里什么东西都没有哦!")
        return
    print("总共{}个单词".format(count))
    db.printAll("select word, meaning from {}".format(table))

def returnCaidan():
    num = input("主人, 返回菜单请输入1: ")
    while num != '1':
        num = input("主人, 返回菜单请输入1: ")

def reciteWord(table):
    os.system('clear')
    print("*************************背单词系统********************************")

    db = conn_mysql()

    num = int(input("主人, 您今天想背诵多少个单词呢, 让我来随机生成它们吧: "))
    means = int(input("主人, 背中文/背英文(0/1): "))
    db.execute("SELECT *,RAND() as r FROM {} ORDER BY r LIMIT 0,{}".format(table, num))
    results = db.cursor.fetchall()

    while num != 0:
        os.system('clear')
        print("*************************背单词系统********************************")

        if means == 0:
            meaning_input = input("主人, 请输入{}的意思: ".format(results[num - 1][0]))
        else:
            meaning_input = input("主人, 请将该中文({})翻译成单词".format(results[num - 1][1]))
        if meaning_input == results[num - 1][1] and means == 0:
            print("恭喜主人, 您答对啦! 剩余{}个单词需要背诵, 一秒后开始下一个单词".format(num - 1))
            num -= 1
            time.sleep(1)

        elif meaning_input == results[num - 1][0] and means == 1:
            print("恭喜主人, 您答对啦! 剩余{}个单词需要背诵, 一秒后开始下一个单词".format(num - 1))
            num -= 1
            time.sleep(1)

        elif meaning_input != results[num - 1][1] and table == "words" and means == 0:
            flag = input("很遗憾主人, 这个单词您还没能完全记住, 要不要把这个单词添加到错题本呢?(1: 要, 0: 不要): ")
            if flag == '0':
                self_input = input("主人, 需要查看该单词的意思吗?(0: 不要, 1: 要): ")
                if self_input == '1':
                    print("主人, 接下来你有5秒钟的时间看这个单词的意思, 请牢牢记住这个单词它有这些含义: " + results[num - 1][1])
                    time.sleep(5)
                continue

            elif flag == '1':
                if db.haveWord(results[num - 1][0], "wrong_words"):
                    print("主人, 这个单词已经在错题本上了哦!")
                else:
                    self_flag = db.execute("insert into wrong_words values('{}', '{}')".format(results[num - 1][0],
                                                                               results[num - 1][1]))
                    if self_flag == True:
                        print("主人, 我成功的把这个单词添加到了错题本中!")
                    else:
                        print("主人, 很遗憾没能成功的将单词添加到错题本中")

                self_input = input("主人, 需要查看该单词的意思吗?(0: 不要, 1: 要)")
                if self_input == '1':
                    print("主人, 请牢牢记住这个单词它有这些含义: " + results[num - 1][1])
                    time.sleep(5)
                continue

        elif meaning_input != results[num - 1][0] and table == "words" and means == 1:
            flag = input("很遗憾主人, 这个单词您还没能完全记住, 要不要把这个单词添加到错题本呢?(1: 要, 0: 不要): ")
            if flag == '0':
                self_input = input("主人, 需要查看该中文怎么翻译成单词吗?(0: 不要, 1: 要): ")
                if self_input == '1':
                    print("主人, 接下来你有5秒钟的时间看这个单词的意思, 请牢牢记住这个单词: " + results[num - 1][0])
                    time.sleep(5)
                continue

            elif flag == '1':
                if db.haveWord(results[num - 1][0], "wrong_words"):
                    print("主人, 这个单词已经在错题本上了哦!")
                else:
                    self_flag = db.execute("insert into wrong_words values('{}', '{}')".format(results[num - 1][0],
                                                                               results[num - 1][1]))
                    if self_flag == True:
                        print("主人, 我成功的把这个单词添加到了错题本中!")
                    else:
                        print("主人, 很遗憾没能成功的将单词添加到错题本中")

                self_input = input("主人, 需要查看该单词的意思吗?(0: 不要, 1: 要)")
                if self_input == '1':
                    print("主人, 接下来你有5秒钟的时间看这个单词的意思, 请牢牢记住这个单词: " + results[num - 1][0])
                    time.sleep(5)
                continue
        elif meaning_input != results[num - 1][1] and table == "wrong_words" and means == 0:
            self_input = input("主人, 需要查看该单词的意思吗?(0: 不要, 1: 要)")
            if self_input == '1':
                print("主人, 接下来你有5秒钟的时间看这个单词的意思, 请牢牢记住这个单词它有这些含义: " + results[num - 1][1])
                time.sleep(5)
                continue

        elif meaning_input != results[num - 1][0] and table == "wrong_words" and means == 1:
            self_input = input("主人, 需要查看该单词的意思吗?(0: 不要, 1: 要)")
            if self_input == '1':
                print("主人, 接下来你有5秒钟的时间看这个单词的意思, 请牢牢记住这个单词: " + results[num - 1][0])
                time.sleep(5)
                continue

    print("恭喜主人, 完成本次的单词背诵啦!")
    returnCaidan()

def searchWord():
    db = conn_mysql()
    word = input("主人, 请输入您想查询的单词: ")
    if db.execute("select word, meaning from words where word='{}'".format(word)):
        result = db.cursor.fetchone()
        print(result[0], result[1])
    else:
        print("主人, 很抱歉, 没能找到您想查询的单词!")

    returnCaidan()


animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}

animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')

def how_many(aDict):
    num = 0
    for k in aDict.values():
        num += len(k)
    return num

def biggest(aDict):
    max_len = 0
    len_k = 0
    for k in aDict:
        if len(aDict[k]) > max_len:
            max_len = len(aDict[k])
            len_k = k
    return len_k

print(biggest(animals))